1 (* rationals, fractions of multivariate polynomials over the real field
3 Copyright (c) isac team 2002, 2013
4 Use is subject to license terms.
6 depends on Poly (and not on Atools), because
7 fractions with _normalised_ polynomials are canceled, added, etc.
11 imports Poly GCD_Poly_ML
14 section \<open>Constants for evaluation by "Rule.Eval"\<close>
17 is_expanded :: "real => bool" ("_ is'_expanded") (*RL->Poly.thy*)
18 is_ratpolyexp :: "real => bool" ("_ is'_ratpolyexp")
19 get_denominator :: "real => real"
20 get_numerator :: "real => real"
23 (*.the expression contains + - * ^ / only ?.*)
24 fun is_ratpolyexp (Free _) = true
25 | is_ratpolyexp (Const (\<^const_name>\<open>plus\<close>,_) $ Free _ $ Free _) = true
26 | is_ratpolyexp (Const (\<^const_name>\<open>minus\<close>,_) $ Free _ $ Free _) = true
27 | is_ratpolyexp (Const (\<^const_name>\<open>times\<close>,_) $ Free _ $ Free _) = true
28 | is_ratpolyexp (Const (\<^const_name>\<open>powr\<close>,_) $ Free _ $ Free _) = true
29 | is_ratpolyexp (Const (\<^const_name>\<open>divide\<close>,_) $ Free _ $ Free _) = true
30 | is_ratpolyexp (Const (\<^const_name>\<open>plus\<close>,_) $ t1 $ t2) =
31 ((is_ratpolyexp t1) andalso (is_ratpolyexp t2))
32 | is_ratpolyexp (Const (\<^const_name>\<open>minus\<close>,_) $ t1 $ t2) =
33 ((is_ratpolyexp t1) andalso (is_ratpolyexp t2))
34 | is_ratpolyexp (Const (\<^const_name>\<open>times\<close>,_) $ t1 $ t2) =
35 ((is_ratpolyexp t1) andalso (is_ratpolyexp t2))
36 | is_ratpolyexp (Const (\<^const_name>\<open>powr\<close>,_) $ t1 $ t2) =
37 ((is_ratpolyexp t1) andalso (is_ratpolyexp t2))
38 | is_ratpolyexp (Const (\<^const_name>\<open>divide\<close>,_) $ t1 $ t2) =
39 ((is_ratpolyexp t1) andalso (is_ratpolyexp t2))
40 | is_ratpolyexp t = if TermC.is_num t then true else false;
42 (*("is_ratpolyexp", ("Rational.is_ratpolyexp", eval_is_ratpolyexp ""))*)
43 fun eval_is_ratpolyexp (thmid:string) _
44 (t as (Const (\<^const_name>\<open>Rational.is_ratpolyexp\<close>, _) $ arg)) thy =
46 then SOME (TermC.mk_thmid thmid (UnparseC.term_in_thy thy arg) "",
47 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
48 else SOME (TermC.mk_thmid thmid (UnparseC.term_in_thy thy arg) "",
49 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
50 | eval_is_ratpolyexp _ _ _ _ = NONE;
52 (*("get_denominator", ("Rational.get_denominator", eval_get_denominator ""))*)
53 fun eval_get_denominator (thmid:string) _
54 (t as Const (\<^const_name>\<open>Rational.get_denominator\<close>, _) $
55 (Const (\<^const_name>\<open>divide\<close>, _) $ _(*num*) $
57 SOME (TermC.mk_thmid thmid (UnparseC.term_in_thy thy denom) "",
58 HOLogic.Trueprop $ (TermC.mk_equality (t, denom)))
59 | eval_get_denominator _ _ _ _ = NONE;
61 (*("get_numerator", ("Rational.get_numerator", eval_get_numerator ""))*)
62 fun eval_get_numerator (thmid:string) _
63 (t as Const (\<^const_name>\<open>Rational.get_numerator\<close>, _) $
64 (Const (\<^const_name>\<open>divide\<close>, _) $num
66 SOME (TermC.mk_thmid thmid (UnparseC.term_in_thy thy num) "",
67 HOLogic.Trueprop $ (TermC.mk_equality (t, num)))
68 | eval_get_numerator _ _ _ _ = NONE;
71 section \<open>Theorems for rewriting\<close>
73 axiomatization (* naming due to Isabelle2002, but not contained in Isabelle2002;
74 many thms are due to RL and can be removed with updating the equation solver;
75 TODO: replace by equivalent thms in recent Isabelle201x *)
77 mult_cross: "[| b ~= 0; d ~= 0 |] ==> (a / b = c / d) = (a * d = b * c)" and
78 mult_cross1: " b ~= 0 ==> (a / b = c ) = (a = b * c)" and
79 mult_cross2: " d ~= 0 ==> (a = c / d) = (a * d = c)" and
81 add_minus: "a + b - b = a"(*RL->Poly.thy*) and
82 add_minus1: "a - b + b = a"(*RL->Poly.thy*) and
84 rat_mult: "a / b * (c / d) = a * c / (b * d)"(*?Isa02*) and
85 rat_mult2: "a / b * c = a * c / b "(*?Isa02*) and
87 rat_mult_poly_l: "c is_polyexp ==> c * (a / b) = c * a / b" and
88 rat_mult_poly_r: "c is_polyexp ==> (a / b) * c = a * c / b" and
90 (*real_times_divide1_eq .. Isa02*)
91 real_times_divide_1_eq: "-1 * (c / d) = -1 * c / d " and
92 real_times_divide_num: "a is_const ==> a * (c / d) = a * c / d " and
94 real_mult_div_cancel2: "k ~= 0 ==> m * k / (n * k) = m / n" and
95 (*real_mult_div_cancel1: "k ~= 0 ==> k * m / (k * n) = m / n"..Isa02*)
97 real_divide_divide1: "y ~= 0 ==> (u / v) / (y / z) = (u / v) * (z / y)" and
98 real_divide_divide1_mg: "y ~= 0 ==> (u / v) / (y / z) = (u * z) / (y * v)" and
99 (*real_divide_divide2_eq: "x / y / z = x / (y * z)"..Isa02*)
101 rat_power: "(a / b) \<up> n = (a \<up> n) / (b \<up> n)" and
103 rat_add: "[| a is_const; b is_const; c is_const; d is_const |] ==>
104 a / c + b / d = (a * d + b * c) / (c * d)" and
105 rat_add_assoc: "[| a is_const; b is_const; c is_const; d is_const |] ==>
106 a / c +(b / d + e) = (a * d + b * c)/(d * c) + e" and
107 rat_add1: "[| a is_const; b is_const; c is_const |] ==>
108 a / c + b / c = (a + b) / c" and
109 rat_add1_assoc: "[| a is_const; b is_const; c is_const |] ==>
110 a / c + (b / c + e) = (a + b) / c + e" and
111 rat_add2: "[| a is_const; b is_const; c is_const |] ==>
112 a / c + b = (a + b * c) / c" and
113 rat_add2_assoc: "[| a is_const; b is_const; c is_const |] ==>
114 a / c + (b + e) = (a + b * c) / c + e" and
115 rat_add3: "[| a is_const; b is_const; c is_const |] ==>
116 a + b / c = (a * c + b) / c" and
117 rat_add3_assoc: "[| a is_const; b is_const; c is_const |] ==>
118 a + (b / c + e) = (a * c + b) / c + e"
120 section \<open>Cancellation and addition of fractions\<close>
121 subsection \<open>Conversion term <--> poly\<close>
122 subsubsection \<open>Convert a term to the internal representation of a multivariate polynomial\<close>
124 fun monom_of_term vs (_, es) (t as Const (\<^const_name>\<open>zero_class.zero\<close>, _)) =
125 (0, list_update es (find_index (curry op = t) vs) 1)
126 | monom_of_term vs (c, es) (t as Const _) =
127 (c, list_update es (find_index (curry op = t) vs) 1)
128 | monom_of_term _ (c, es) (t as (Const (\<^const_name>\<open>numeral\<close>, _) $ _)) =
129 (t |> HOLogic.dest_number |> snd |> curry op * c, es) (*several numerals in one monom*)
130 | monom_of_term _ (c, es) (t as (Const (\<^const_name>\<open>uminus\<close>, _) $ _)) =
131 (t |> HOLogic.dest_number |> snd |> curry op * c, es) (*several numerals in one monom*)
132 | monom_of_term vs (c, es) (t as Free _) =
133 (c, list_update es (find_index (curry op = t) vs) 1)
134 | monom_of_term vs (c, es) (Const (\<^const_name>\<open>powr\<close>, _) $ (b as Free _) $
135 (e as Const (\<^const_name>\<open>numeral\<close>, _) $ _)) =
136 (c, list_update es (find_index (curry op = b) vs) (e |> HOLogic.dest_number |> snd))
137 | monom_of_term vs (c, es) (Const (\<^const_name>\<open>powr\<close>, _) $ (b as Free _) $
138 (e as Const (\<^const_name>\<open>uminus\<close>, _) $ _)) =
139 (c, list_update es (find_index (curry op = b) vs) (e |> HOLogic.dest_number |> snd))
141 | monom_of_term vs (c, es) (Const (\<^const_name>\<open>times\<close>, _) $ m1 $ m2) =
142 let val (c', es') = monom_of_term vs (c, es) m1
143 in monom_of_term vs (c', es') m2 end
144 | monom_of_term _ _ t = raise ERROR ("poly malformed 1 with " ^ UnparseC.term t)
147 fun monoms_of_term vs (t as Const (\<^const_name>\<open>zero_class.zero\<close>, _)) =
148 [monom_of_term vs (0, replicate (length vs) 0) t]
149 | monoms_of_term vs (t as Const _) =
150 [monom_of_term vs (1, replicate (length vs) 0) t]
151 | monoms_of_term vs (t as Const (\<^const_name>\<open>numeral\<close>, _) $ _) =
152 [monom_of_term vs (1, replicate (length vs) 0) t]
153 | monoms_of_term vs (t as Const (\<^const_name>\<open>uminus\<close>, _) $ _) =
154 [monom_of_term vs (1, replicate (length vs) 0) t]
155 | monoms_of_term vs (t as Free _) =
156 [monom_of_term vs (1, replicate (length vs) 0) t]
157 | monoms_of_term vs (t as Const (\<^const_name>\<open>powr\<close>, _) $ _ $ _) =
158 [monom_of_term vs (1, replicate (length vs) 0) t]
159 | monoms_of_term vs (t as Const (\<^const_name>\<open>times\<close>, _) $ _ $ _) =
160 [monom_of_term vs (1, replicate (length vs) 0) t]
161 | monoms_of_term vs (Const (\<^const_name>\<open>plus\<close>, _) $ ms1 $ ms2) =
162 (monoms_of_term vs ms1) @ (monoms_of_term vs ms2)
163 | monoms_of_term _ t = raise ERROR ("poly malformed 2 with " ^ UnparseC.term t)
165 (* convert a term to the internal representation of a multivariate polynomial;
166 the conversion is quite liberal, see test --- fun poly_of_term ---:
167 * the order of variables and the parentheses within a monomial are arbitrary
168 * the coefficient may be somewhere
169 * he order and the parentheses within monomials are arbitrary
170 But the term must be completely expand + over * (laws of distributivity are not applicable).
172 The function requires the free variables as strings already given,
173 because the gcd involves 2 polynomials (with the same length for their list of exponents).
175 fun poly_of_term vs (t as Const (\<^const_name>\<open>plus\<close>, _) $ _ $ _) =
176 (SOME (t |> monoms_of_term vs |> order)
177 handle ERROR _ => NONE)
178 | poly_of_term vs t = (* 0 for zero polynomial *)
179 (SOME [monom_of_term vs (1, replicate (length vs) 0) t]
180 handle ERROR _ => NONE)
184 val vs = TermC.vars_of t
186 case poly_of_term vs t of SOME _ => true | NONE => false
188 val is_expanded = is_poly (* TODO: check names *)
189 val is_polynomial = is_poly (* TODO: check names *)
192 subsubsection \<open>Convert internal representation of a multivariate polynomial to a term\<close>
194 fun term_of_es _ _ _ [] = [] (*assumes same length for vs and es*)
195 | term_of_es baseT expT (_ :: vs) (0 :: es) = [] @ term_of_es baseT expT vs es
196 | term_of_es baseT expT (v :: vs) (1 :: es) = v :: term_of_es baseT expT vs es
197 | term_of_es baseT expT (v :: vs) (e :: es) =
198 Const (\<^const_name>\<open>Transcendental.powr\<close>, [baseT, expT] ---> baseT) $ v $ (HOLogic.mk_number expT e)
199 :: term_of_es baseT expT vs es
200 | term_of_es _ _ _ _ = raise ERROR "term_of_es: length vs <> length es"
202 fun term_of_monom baseT expT vs ((c, es): monom) =
203 let val es' = term_of_es baseT expT vs es
207 if es' = [] (*if es = [0,0,0,...]*)
208 then HOLogic.mk_number baseT c
209 else foldl (HOLogic.mk_binop "Groups.times_class.times") (hd es', tl es')
210 else foldl (HOLogic.mk_binop "Groups.times_class.times")
211 (HOLogic.mk_number baseT c, es')
214 fun term_of_poly baseT expT vs p =
215 let val monos = map (term_of_monom baseT expT vs) p
216 in foldl (HOLogic.mk_binop \<^const_name>\<open>plus\<close>) (hd monos, tl monos) end
219 subsection \<open>Apply gcd_poly for cancelling and adding fractions as terms\<close>
221 fun mk_noteq_0 baseT t =
222 Const (\<^const_name>\<open>Not\<close>, HOLogic.boolT --> HOLogic.boolT) $
223 (Const (\<^const_name>\<open>HOL.eq\<close>, [baseT, baseT] ---> HOLogic.boolT) $ t $ HOLogic.mk_number HOLogic.realT 0)
225 fun mk_asms baseT ts =
226 let val as' = filter_out TermC.is_num ts (* asm like "2 ~= 0" is needless *)
227 in map (mk_noteq_0 baseT) as' end
230 subsubsection \<open>Factor out gcd for cancellation\<close>
232 fun check_fraction t =
234 Const (\<^const_name>\<open>divide\<close>, _) $ numerator $ denominator
235 => SOME (numerator, denominator)
238 (* prepare a term for cancellation by factoring out the gcd
239 assumes: is a fraction with outmost "/"*)
240 fun factout_p_ (thy: theory) t =
241 let val opt = check_fraction t
245 | SOME (numerator, denominator) =>
247 val vs = TermC.vars_of t
248 val baseT = type_of numerator
249 val expT = HOLogic.realT
251 case (poly_of_term vs numerator, poly_of_term vs denominator) of
254 val ((a', b'), c) = gcd_poly a b
255 val es = replicate (length vs) 0
257 if c = [(1, es)] orelse c = [(~1, es)]
261 val b't = term_of_poly baseT expT vs b'
262 val ct = term_of_poly baseT expT vs c
264 HOLogic.mk_binop \<^const_name>\<open>divide\<close>
265 (HOLogic.mk_binop \<^const_name>\<open>times\<close>
266 (term_of_poly baseT expT vs a', ct),
267 HOLogic.mk_binop \<^const_name>\<open>times\<close> (b't, ct))
268 in SOME (t', mk_asms baseT [b't, ct]) end
270 | _ => NONE : (term * term list) option
275 subsubsection \<open>Cancel a fraction\<close>
277 (* cancel a term by the gcd ("" denote terms with internal algebraic structure)
278 cancel_p_ : theory \<Rightarrow> term \<Rightarrow> (term \<times> term list) option
279 cancel_p_ thy "a / b" = SOME ("a' / b'", ["b' \<noteq> 0"])
280 assumes: a is_polynomial \<and> b is_polynomial \<and> b \<noteq> 0
282 SOME ("a' / b'", ["b' \<noteq> 0"]). gcd_poly a b \<noteq> 1 \<and> gcd_poly a b \<noteq> -1 \<and>
283 a' * gcd_poly a b = a \<and> b' * gcd_poly a b = b
285 fun cancel_p_ (_: theory) t =
286 let val opt = check_fraction t
290 | SOME (numerator, denominator) =>
292 val vs = TermC.vars_of t
293 val baseT = type_of numerator
294 val expT = HOLogic.realT
296 case (poly_of_term vs numerator, poly_of_term vs denominator) of
299 val ((a', b'), c) = gcd_poly a b
300 val es = replicate (length vs) 0
302 if c = [(1, es)] orelse c = [(~1, es)]
306 val bt' = term_of_poly baseT expT vs b'
307 val ct = term_of_poly baseT expT vs c
309 HOLogic.mk_binop \<^const_name>\<open>divide\<close>
310 (term_of_poly baseT expT vs a', bt')
311 val asm = mk_asms baseT [bt']
312 in SOME (t', asm) end
314 | _ => NONE : (term * term list) option
319 subsubsection \<open>Factor out to a common denominator for addition\<close>
321 (* addition of fractions allows (at most) one non-fraction (a monomial) *)
323 (Const (\<^const_name>\<open>plus\<close>, _) $
324 (Const (\<^const_name>\<open>divide\<close>, _) $ n1 $ d1) $
325 (Const (\<^const_name>\<open>divide\<close>, _) $ n2 $ d2))
326 = SOME ((n1, d1), (n2, d2))
328 (Const (\<^const_name>\<open>plus\<close>, _) $
330 (Const (\<^const_name>\<open>divide\<close>, _) $ n2 $ d2))
331 = SOME ((nofrac, HOLogic.mk_number HOLogic.realT 1), (n2, d2))
333 (Const (\<^const_name>\<open>plus\<close>, _) $
334 (Const (\<^const_name>\<open>divide\<close>, _) $ n1 $ d1) $
336 = SOME ((n1, d1), (nofrac, HOLogic.mk_number HOLogic.realT 1))
337 | check_frac_sum _ = NONE
339 (* prepare a term for addition by providing the least common denominator as a product
340 assumes: is a term with outmost "+" and at least one outmost "/" in respective summands*)
341 fun common_nominator_p_ (_: theory) t =
342 let val opt = check_frac_sum t
346 | SOME ((n1, d1), (n2, d2)) =>
348 val vs = TermC.vars_of t
350 case (poly_of_term vs d1, poly_of_term vs d2) of
353 val ((a', b'), c) = gcd_poly a b
354 val (baseT, expT) = (type_of n1, HOLogic.realT)
355 val [d1', d2', c'] = map (term_of_poly baseT expT vs) [a', b', c]
356 (*----- minimum of parentheses & nice result, but breaks tests: -------------
357 val denom = HOLogic.mk_binop \<^const_name>\<open>times\<close>
358 (HOLogic.mk_binop \<^const_name>\<open>times\<close> (d1', d2'), c') -------------*)
360 if c = [(1, replicate (length vs) 0)]
361 then HOLogic.mk_binop \<^const_name>\<open>times\<close> (d1', d2')
363 HOLogic.mk_binop \<^const_name>\<open>times\<close> (c',
364 HOLogic.mk_binop \<^const_name>\<open>times\<close> (d1', d2')) (*--------------*)
366 HOLogic.mk_binop \<^const_name>\<open>plus\<close>
367 (HOLogic.mk_binop \<^const_name>\<open>divide\<close>
368 (HOLogic.mk_binop \<^const_name>\<open>times\<close> (n1, d2'), denom),
369 HOLogic.mk_binop \<^const_name>\<open>divide\<close>
370 (HOLogic.mk_binop \<^const_name>\<open>times\<close> (n2, d1'), denom))
371 val asm = mk_asms baseT [d1', d2', c']
372 in SOME (t', asm) end
373 | _ => NONE : (term * term list) option
378 subsubsection \<open>Addition of at least one fraction within a sum\<close>
381 assumes: is a term with outmost "+" and at least one outmost "/" in respective summands
382 \<and> NONE of the summands is zero.
383 NOTE: the case "(_ + _) + _" need not be considered due to iterated addition *)
384 fun add_fraction_p_ (_: theory) t =
385 case check_frac_sum t of
387 | SOME ((n1, d1), (n2, d2)) =>
389 val vs = TermC.vars_of t
391 case (poly_of_term vs n1, poly_of_term vs d1, poly_of_term vs n2, poly_of_term vs d2) of
392 (SOME _, SOME a, SOME _, SOME b) =>
394 val ((a', b'), c) = gcd_poly a b
395 val (baseT, expT) = (type_of n1, HOLogic.realT)
396 val nomin = term_of_poly baseT expT vs
397 (((the (poly_of_term vs n1)) %%*%% b') %%+%% ((the (poly_of_term vs n2)) %%*%% a'))
398 val denom = term_of_poly baseT expT vs ((c %%*%% a') %%*%% b')
399 val t' = HOLogic.mk_binop \<^const_name>\<open>divide\<close> (nomin, denom)
400 in SOME (t', mk_asms baseT [denom]) end
401 | _ => NONE : (term * term list) option
405 section \<open>Embed cancellation and addition into rewriting\<close>
407 subsection \<open>Rulesets and predicate for embedding\<close>
409 (* evaluates conditions in calculate_Rational *)
412 (Rule_Def.Repeat {id = "calc_rat_erls", preconds = [], rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
413 erls = Rule_Set.empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
415 [Rule.Eval ("Prog_Expr.matches", Prog_Expr.eval_matches "#matches_"),
416 Rule.Eval ("HOL.eq", Prog_Expr.eval_equal "#equal_"),
417 Rule.Eval ("Prog_Expr.is_const", Prog_Expr.eval_const "#is_const_"),
418 Rule.Thm ("not_true", @{thm not_true}),
419 Rule.Thm ("not_false", @{thm not_false})],
420 scr = Rule.Empty_Prog});
422 (* simplifies expressions with numerals;
423 does NOT rearrange the term by AC-rewriting; thus terms with variables
424 need to have constants to be commuted together respectively *)
425 val calculate_Rational =
426 prep_rls' (Rule_Set.merge "calculate_Rational"
427 (Rule_Def.Repeat {id = "divide", preconds = [], rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
428 erls = calc_rat_erls, srls = Rule_Set.Empty,
429 calc = [], errpatts = [],
431 [\<^rule_eval>\<open>divide\<close> (Prog_Expr.eval_cancel "#divide_e"),
433 Rule.Thm ("minus_divide_left", (@{thm minus_divide_left} RS @{thm sym})),
434 (*SYM - ?x / ?y = - (?x / ?y) may come from subst*)
435 \<^rule_thm>\<open>rat_add\<close>,
436 (*"[| a is_const; b is_const; c is_const; d is_const |] ==> \
437 \a / c + b / d = (a * d) / (c * d) + (b * c ) / (d * c)"*)
438 \<^rule_thm>\<open>rat_add1\<close>,
439 (*"[| a is_const; b is_const; c is_const |] ==> a / c + b / c = (a + b) / c"*)
440 \<^rule_thm>\<open>rat_add2\<close>,
441 (*"[| ?a is_const; ?b is_const; ?c is_const |] ==> ?a / ?c + ?b = (?a + ?b * ?c) / ?c"*)
442 \<^rule_thm>\<open>rat_add3\<close>,
443 (*"[| a is_const; b is_const; c is_const |] ==> a + b / c = (a * c) / c + b / c"\
444 .... is_const to be omitted here FIXME*)
446 \<^rule_thm>\<open>rat_mult\<close>,
447 (*a / b * (c / d) = a * c / (b * d)*)
448 \<^rule_thm>\<open>times_divide_eq_right\<close>,
449 (*?x * (?y / ?z) = ?x * ?y / ?z*)
450 \<^rule_thm>\<open>times_divide_eq_left\<close>,
451 (*?y / ?z * ?x = ?y * ?x / ?z*)
453 \<^rule_thm>\<open>real_divide_divide1\<close>,
454 (*"?y ~= 0 ==> ?u / ?v / (?y / ?z) = ?u / ?v * (?z / ?y)"*)
455 \<^rule_thm>\<open>divide_divide_eq_left\<close>,
456 (*"?x / ?y / ?z = ?x / (?y * ?z)"*)
458 \<^rule_thm>\<open>rat_power\<close>,
459 (*"(?a / ?b) \<up> ?n = ?a \<up> ?n / ?b \<up> ?n"*)
461 \<^rule_thm>\<open>mult_cross\<close>,
462 (*"[| b ~= 0; d ~= 0 |] ==> (a / b = c / d) = (a * d = b * c)*)
463 \<^rule_thm>\<open>mult_cross1\<close>,
464 (*" b ~= 0 ==> (a / b = c ) = (a = b * c)*)
465 \<^rule_thm>\<open>mult_cross2\<close>
466 (*" d ~= 0 ==> (a = c / d) = (a * d = c)*)],
467 scr = Rule.Empty_Prog})
470 (*("is_expanded", ("Rational.is_expanded", eval_is_expanded ""))*)
471 fun eval_is_expanded (thmid:string) _
472 (t as (Const (\<^const_name>\<open>Rational.is_expanded\<close>, _) $ arg)) thy =
474 then SOME (TermC.mk_thmid thmid (UnparseC.term_in_thy thy arg) "",
475 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
476 else SOME (TermC.mk_thmid thmid (UnparseC.term_in_thy thy arg) "",
477 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
478 | eval_is_expanded _ _ _ _ = NONE;
480 calculation is_expanded = \<open>eval_is_expanded ""\<close>
483 Rule_Set.merge "rational_erls" calculate_Rational
484 (Rule_Set.append_rules "is_expanded" Atools_erls
485 [\<^rule_eval>\<open>is_expanded\<close> (eval_is_expanded "")]);
488 subsection \<open>Embed cancellation into rewriting\<close>
490 (**)local (* cancel_p *)
492 val {rules = rules, rew_ord = (_, ro), ...} = Rule_Set.rep (assoc_rls' @{theory} "rev_rew_p");
494 fun init_state thy eval_rls ro t =
496 val SOME (t', _) = factout_p_ thy t;
497 val SOME (t'', asm) = cancel_p_ thy t;
498 val der = Derive.steps_reverse thy eval_rls rules ro NONE t';
500 [(\<^rule_thm>\<open>real_mult_div_cancel2\<close>, (t'', asm))]
501 val rs = (Rule.distinct' o (map #1)) der
502 val rs = filter_out (ThmC.member'
503 ["sym_real_add_zero_left", "sym_real_mult_0", "sym_real_mult_1"]) rs
504 in (t, t'', [rs(*one in order to ease locate_rule*)], der) end;
506 fun locate_rule thy eval_rls ro [rs] t r =
507 if member op = ((map (Rule.thm_id)) rs) (Rule.thm_id r)
509 let val ropt = Rewrite.rewrite_ thy ro eval_rls true (Rule.thm r) t;
511 case ropt of SOME ta => [(r, ta)]
513 ("### locate_rule: rewrite " ^ Rule.thm_id r ^ " " ^ UnparseC.term t ^ " = NONE");*) [])
515 else ((*tracing ("### locate_rule: " ^ Rule.thm_id r ^ " not mem rrls");*) [])
516 | locate_rule _ _ _ _ _ _ = raise ERROR "locate_rule: doesnt match rev-sets in istate";
518 fun next_rule thy eval_rls ro [rs] t =
520 val der = Derive.do_one thy eval_rls rs ro NONE t;
521 in case der of (_, r, _) :: _ => SOME r | _ => NONE end
522 | next_rule _ _ _ _ _ = raise ERROR ("next_rule: doesnt match rev-sets in istate");
524 fun attach_form (_: Rule.rule list list) (_: term) (_: term) =
525 [(*TODO*)]: ( Rule.rule * (term * term list)) list;
530 Rule_Set.Rrls {id = "cancel_p", prepat = [],
531 rew_ord=("ord_make_polynomial", ord_make_polynomial false \<^theory>),
532 erls = rational_erls,
534 [("PLUS", (\<^const_name>\<open>plus\<close>, (**)eval_binop "#add_")),
535 ("TIMES" , (\<^const_name>\<open>times\<close>, (**)eval_binop "#mult_")),
536 ("DIVIDE", (\<^const_name>\<open>divide\<close>, Prog_Expr.eval_cancel "#divide_e")),
537 ("POWER", (\<^const_name>\<open>powr\<close>, (**)eval_binop "#power_"))],
540 Rule.Rfuns {init_state = init_state \<^theory> Atools_erls ro,
541 normal_form = cancel_p_ \<^theory>,
542 locate_rule = locate_rule \<^theory> Atools_erls ro,
543 next_rule = next_rule \<^theory> Atools_erls ro,
544 attach_form = attach_form}}
545 (**)end(* local cancel_p *)
548 subsection \<open>Embed addition into rewriting\<close>
550 (**)local (* add_fractions_p *)
552 (*val {rules = rules, rew_ord = (_, ro), ...} = Rule_Set.rep (assoc_rls "make_polynomial");*)
553 val {rules, rew_ord=(_,ro),...} = Rule_Set.rep (assoc_rls' @{theory} "rev_rew_p");
555 fun init_state thy eval_rls ro t =
557 val SOME (t',_) = common_nominator_p_ thy t;
558 val SOME (t'', asm) = add_fraction_p_ thy t;
559 val der = Derive.steps_reverse thy eval_rls rules ro NONE t';
561 [(\<^rule_thm>\<open>real_mult_div_cancel2\<close>, (t'',asm))]
562 val rs = (Rule.distinct' o (map #1)) der;
563 val rs = filter_out (ThmC.member'
564 ["sym_real_add_zero_left", "sym_real_mult_0", "sym_real_mult_1"]) rs;
565 in (t, t'', [rs(*here only _ONE_*)], der) end;
567 fun locate_rule thy eval_rls ro [rs] t r =
568 if member op = ((map (Rule.thm_id)) rs) (Rule.thm_id r)
570 let val ropt = Rewrite.rewrite_ thy ro eval_rls true (Rule.thm r) t;
575 ((*tracing ("### locate_rule: rewrite " ^ Rule.thm_id r ^ " " ^ UnparseC.term t ^ " = NONE");*)
577 else ((*tracing ("### locate_rule: " ^ Rule.thm_id r ^ " not mem rrls");*) [])
578 | locate_rule _ _ _ _ _ _ = raise ERROR "locate_rule: doesnt match rev-sets in istate";
580 fun next_rule thy eval_rls ro [rs] t =
581 let val der = Derive.do_one thy eval_rls rs ro NONE t;
587 | next_rule _ _ _ _ _ = raise ERROR ("next_rule: doesnt match rev-sets in istate");
589 val pat0 = TermC.parse_patt \<^theory> "?r/?s+?u/?v :: real";
590 val pat1 = TermC.parse_patt \<^theory> "?r/?s+?u :: real";
591 val pat2 = TermC.parse_patt \<^theory> "?r +?u/?v :: real";
592 val prepat = [([@{term True}], pat0),
593 ([@{term True}], pat1),
594 ([@{term True}], pat2)];
597 val add_fractions_p =
598 Rule_Set.Rrls {id = "add_fractions_p", prepat=prepat,
599 rew_ord = ("ord_make_polynomial", ord_make_polynomial false \<^theory>),
600 erls = rational_erls,
601 calc = [("PLUS", (\<^const_name>\<open>plus\<close>, (**)eval_binop "#add_")),
602 ("TIMES", (\<^const_name>\<open>times\<close>, (**)eval_binop "#mult_")),
603 ("DIVIDE", (\<^const_name>\<open>divide\<close>, Prog_Expr.eval_cancel "#divide_e")),
604 ("POWER", (\<^const_name>\<open>powr\<close>, (**)eval_binop "#power_"))],
606 scr = Rule.Rfuns {init_state = init_state \<^theory> Atools_erls ro,
607 normal_form = add_fraction_p_ \<^theory>,
608 locate_rule = locate_rule \<^theory> Atools_erls ro,
609 next_rule = next_rule \<^theory> Atools_erls ro,
610 attach_form = attach_form}}
611 (**)end(*local add_fractions_p *)
614 subsection \<open>Cancelling and adding all occurrences in a term /////////////////////////////\<close>
616 (*copying cancel_p_rls + add her caused error in interface.sml*)
619 section \<open>Rulesets for general simplification\<close>
621 (*.all powers over + distributed; atoms over * collected, other distributed
622 contains absolute minimum of thms for context in norm_Rational .*)
623 val powers = prep_rls'(
624 Rule_Def.Repeat {id = "powers", preconds = [], rew_ord = ("dummy_ord",Rewrite_Ord.dummy_ord),
625 erls = powers_erls, srls = Rule_Set.Empty, calc = [], errpatts = [],
626 rules = [\<^rule_thm>\<open>realpow_multI\<close>,
627 (*"(r * s) \<up> n = r \<up> n * s \<up> n"*)
628 \<^rule_thm>\<open>realpow_pow\<close>,
629 (*"(a \<up> b) \<up> c = a \<up> (b * c)"*)
630 \<^rule_thm>\<open>realpow_oneI\<close>,
632 \<^rule_thm>\<open>realpow_minus_even\<close>,
633 (*"n is_even ==> (- r) \<up> n = r \<up> n" ?-->discard_minus?*)
634 \<^rule_thm>\<open>realpow_minus_odd\<close>,
635 (*"Not (n is_even) ==> (- r) \<up> n = -1 * r \<up> n"*)
637 (*----- collect atoms over * -----*)
638 \<^rule_thm>\<open>realpow_two_atom\<close>,
639 (*"r is_atom ==> r * r = r \<up> 2"*)
640 \<^rule_thm>\<open>realpow_plus_1\<close>,
641 (*"r is_atom ==> r * r \<up> n = r \<up> (n + 1)"*)
642 \<^rule_thm>\<open>realpow_addI_atom\<close>,
643 (*"r is_atom ==> r \<up> n * r \<up> m = r \<up> (n + m)"*)
645 (*----- distribute none-atoms -----*)
646 \<^rule_thm>\<open>realpow_def_atom\<close>,
647 (*"[| 1 < n; ~ (r is_atom) |]==>r \<up> n = r * r \<up> (n + -1)"*)
648 \<^rule_thm>\<open>realpow_eq_oneI\<close>,
650 \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_")
652 scr = Rule.Empty_Prog
654 (*.contains absolute minimum of thms for context in norm_Rational.*)
655 val rat_mult_divide = prep_rls'(
656 Rule_Def.Repeat {id = "rat_mult_divide", preconds = [],
657 rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
658 erls = Rule_Set.empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
659 rules = [\<^rule_thm>\<open>rat_mult\<close>,
660 (*(1)"?a / ?b * (?c / ?d) = ?a * ?c / (?b * ?d)"*)
661 \<^rule_thm>\<open>times_divide_eq_right\<close>,
662 (*(2)"?a * (?c / ?d) = ?a * ?c / ?d" must be [2],
663 otherwise inv.to a / b / c = ...*)
664 \<^rule_thm>\<open>times_divide_eq_left\<close>,
665 (*"?a / ?b * ?c = ?a * ?c / ?b" order weights x \<up> n too much
666 and does not commute a / b * c \<up> 2 !*)
668 \<^rule_thm>\<open>divide_divide_eq_right\<close>,
669 (*"?x / (?y / ?z) = ?x * ?z / ?y"*)
670 \<^rule_thm>\<open>divide_divide_eq_left\<close>,
671 (*"?x / ?y / ?z = ?x / (?y * ?z)"*)
672 \<^rule_eval>\<open>divide\<close> (Prog_Expr.eval_cancel "#divide_e")
674 scr = Rule.Empty_Prog
677 (*.contains absolute minimum of thms for context in norm_Rational.*)
678 val reduce_0_1_2 = prep_rls'(
679 Rule_Def.Repeat{id = "reduce_0_1_2", preconds = [], rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
680 erls = Rule_Set.empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
681 rules = [(*\<^rule_thm>\<open>divide_1\<close>,
682 "?x / 1 = ?x" unnecess.for normalform*)
683 \<^rule_thm>\<open>mult_1_left\<close>,
685 (*\<^rule_thm>\<open>real_mult_minus1\<close>,
687 (*\<^rule_thm>\<open>real_minus_mult_cancel\<close>,
688 "- ?x * - ?y = ?x * ?y"*)
690 \<^rule_thm>\<open>mult_zero_left\<close>,
692 \<^rule_thm>\<open>add_0_left\<close>,
694 (*\<^rule_thm>\<open>right_minus\<close>,
697 \<^rule_thm_sym>\<open>real_mult_2\<close>,
698 (*"z1 + z1 = 2 * z1"*)
699 \<^rule_thm>\<open>real_mult_2_assoc\<close>,
700 (*"z1 + (z1 + k) = 2 * z1 + k"*)
702 \<^rule_thm>\<open>division_ring_divide_zero\<close>
704 ], scr = Rule.Empty_Prog});
706 (*erls for calculate_Rational;
707 make local with FIXX@ME result:term *term list WN0609???SKMG*)
708 val norm_rat_erls = prep_rls'(
709 Rule_Def.Repeat {id = "norm_rat_erls", preconds = [], rew_ord = ("dummy_ord",Rewrite_Ord.dummy_ord),
710 erls = Rule_Set.empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
711 rules = [\<^rule_eval>\<open>Prog_Expr.is_const\<close> (Prog_Expr.eval_const "#is_const_")
712 ], scr = Rule.Empty_Prog});
714 (* consists of rls containing the absolute minimum of thms *)
715 (*040209: this version has been used by RL for his equations,
716 which is now replaced by MGs version "norm_Rational" below *)
717 val norm_Rational_min = prep_rls'(
718 Rule_Def.Repeat {id = "norm_Rational_min", preconds = [], rew_ord = ("dummy_ord",Rewrite_Ord.dummy_ord),
719 erls = norm_rat_erls, srls = Rule_Set.Empty, calc = [], errpatts = [],
720 rules = [(*sequence given by operator precedence*)
721 Rule.Rls_ discard_minus,
723 Rule.Rls_ rat_mult_divide,
725 Rule.Rls_ reduce_0_1_2,
726 Rule.Rls_ order_add_mult,
727 Rule.Rls_ collect_numerals,
728 Rule.Rls_ add_fractions_p,
731 scr = Rule.Empty_Prog});
733 val norm_Rational_parenthesized = prep_rls'(
734 Rule_Set.Sequence {id = "norm_Rational_parenthesized", preconds = []:term list,
735 rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
736 erls = Atools_erls, srls = Rule_Set.Empty,
737 calc = [], errpatts = [],
738 rules = [Rule.Rls_ norm_Rational_min,
739 Rule.Rls_ discard_parentheses
741 scr = Rule.Empty_Prog});
743 (*WN030318???SK: simplifies all but cancel and common_nominator*)
744 val simplify_rational =
745 Rule_Set.merge "simplify_rational" expand_binoms
746 (Rule_Set.append_rules "divide" calculate_Rational
747 [\<^rule_thm>\<open>div_by_1\<close>,
749 \<^rule_thm>\<open>rat_mult\<close>,
750 (*(1)"?a / ?b * (?c / ?d) = ?a * ?c / (?b * ?d)"*)
751 \<^rule_thm>\<open>times_divide_eq_right\<close>,
752 (*(2)"?a * (?c / ?d) = ?a * ?c / ?d" must be [2],
753 otherwise inv.to a / b / c = ...*)
754 \<^rule_thm>\<open>times_divide_eq_left\<close>,
755 (*"?a / ?b * ?c = ?a * ?c / ?b"*)
756 \<^rule_thm>\<open>add_minus\<close>,
757 (*"?a + ?b - ?b = ?a"*)
758 \<^rule_thm>\<open>add_minus1\<close>,
759 (*"?a - ?b + ?b = ?a"*)
760 \<^rule_thm>\<open>divide_minus1\<close>
765 val add_fractions_p_rls = prep_rls'(
766 Rule_Def.Repeat {id = "add_fractions_p_rls", preconds = [], rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
767 erls = Rule_Set.empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
768 rules = [Rule.Rls_ add_fractions_p],
769 scr = Rule.Empty_Prog});
771 (* "Rule_Def.Repeat" causes repeated application of cancel_p to one and the same term *)
772 val cancel_p_rls = prep_rls'(
774 {id = "cancel_p_rls", preconds = [], rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
775 erls = Rule_Set.empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
776 rules = [Rule.Rls_ cancel_p],
777 scr = Rule.Empty_Prog});
779 (*. makes 'normal' fractions; 'is_polyexp' inhibits double fractions;
780 used in initial part norm_Rational_mg, see example DA-M02-main.p.60.*)
781 val rat_mult_poly = prep_rls'(
782 Rule_Def.Repeat {id = "rat_mult_poly", preconds = [], rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
783 erls = Rule_Set.append_rules "Rule_Set.empty-is_polyexp" Rule_Set.empty
784 [\<^rule_eval>\<open>is_polyexp\<close> (eval_is_polyexp "")],
785 srls = Rule_Set.Empty, calc = [], errpatts = [],
787 [\<^rule_thm>\<open>rat_mult_poly_l\<close>,
788 (*"?c is_polyexp ==> ?c * (?a / ?b) = ?c * ?a / ?b"*)
789 \<^rule_thm>\<open>rat_mult_poly_r\<close>
790 (*"?c is_polyexp ==> ?a / ?b * ?c = ?a * ?c / ?b"*) ],
791 scr = Rule.Empty_Prog});
793 (*. makes 'normal' fractions; 'is_polyexp' inhibits double fractions;
794 used in looping part norm_Rational_rls, see example DA-M02-main.p.60
795 .. WHERE THE LATTER DOES ALWAYS WORK, BECAUSE erls = Rule_Set.empty,
796 I.E. THE RESPECTIVE ASSUMPTION IS STORED AND Rule.Thm APPLIED; WN051028
798 val rat_mult_div_pow = prep_rls'(
799 Rule_Def.Repeat {id = "rat_mult_div_pow", preconds = [], rew_ord = ("dummy_ord",Rewrite_Ord.dummy_ord),
800 erls = Rule_Set.empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
801 rules = [\<^rule_thm>\<open>rat_mult\<close>,
802 (*"?a / ?b * (?c / ?d) = ?a * ?c / (?b * ?d)"*)
803 \<^rule_thm>\<open>rat_mult_poly_l\<close>,
804 (*"?c is_polyexp ==> ?c * (?a / ?b) = ?c * ?a / ?b"*)
805 \<^rule_thm>\<open>rat_mult_poly_r\<close>,
806 (*"?c is_polyexp ==> ?a / ?b * ?c = ?a * ?c / ?b"*)
808 \<^rule_thm>\<open>real_divide_divide1_mg\<close>,
809 (*"y ~= 0 ==> (u / v) / (y / z) = (u * z) / (y * v)"*)
810 \<^rule_thm>\<open>divide_divide_eq_right\<close>,
811 (*"?x / (?y / ?z) = ?x * ?z / ?y"*)
812 \<^rule_thm>\<open>divide_divide_eq_left\<close>,
813 (*"?x / ?y / ?z = ?x / (?y * ?z)"*)
814 \<^rule_eval>\<open>divide\<close> (Prog_Expr.eval_cancel "#divide_e"),
816 \<^rule_thm>\<open>rat_power\<close>
817 (*"(?a / ?b) \<up> ?n = ?a \<up> ?n / ?b \<up> ?n"*)
819 scr = Rule.Empty_Prog});
821 val rat_reduce_1 = prep_rls'(
822 Rule_Def.Repeat {id = "rat_reduce_1", preconds = [], rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
823 erls = Rule_Set.empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
825 [\<^rule_thm>\<open>div_by_1\<close>,
827 \<^rule_thm>\<open>mult_1_left\<close>
830 scr = Rule.Empty_Prog});
832 (* looping part of norm_Rational *)
833 val norm_Rational_rls = prep_rls' (
834 Rule_Def.Repeat {id = "norm_Rational_rls", preconds = [], rew_ord = ("dummy_ord",Rewrite_Ord.dummy_ord),
835 erls = norm_rat_erls, srls = Rule_Set.Empty, calc = [], errpatts = [],
836 rules = [Rule.Rls_ add_fractions_p_rls,
837 Rule.Rls_ rat_mult_div_pow,
838 Rule.Rls_ make_rat_poly_with_parentheses,
839 Rule.Rls_ cancel_p_rls,
840 Rule.Rls_ rat_reduce_1
842 scr = Rule.Empty_Prog});
844 val norm_Rational = prep_rls' (
846 {id = "norm_Rational", preconds = [], rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
847 erls = norm_rat_erls, srls = Rule_Set.Empty, calc = [], errpatts = [],
848 rules = [Rule.Rls_ discard_minus,
849 Rule.Rls_ rat_mult_poly, (* removes double fractions like a/b/c *)
850 Rule.Rls_ make_rat_poly_with_parentheses,
851 Rule.Rls_ cancel_p_rls,
852 Rule.Rls_ norm_Rational_rls, (* the main rls, looping (#) *)
853 Rule.Rls_ discard_parentheses1 (* mult only *)
855 scr = Rule.Empty_Prog});
859 calculate_Rational = calculate_Rational and
860 calc_rat_erls = calc_rat_erls and
861 rational_erls = rational_erls and
862 cancel_p = cancel_p and
863 add_fractions_p = add_fractions_p and
865 add_fractions_p_rls = add_fractions_p_rls and
866 powers_erls = powers_erls and
868 rat_mult_divide = rat_mult_divide and
869 reduce_0_1_2 = reduce_0_1_2 and
871 rat_reduce_1 = rat_reduce_1 and
872 norm_rat_erls = norm_rat_erls and
873 norm_Rational = norm_Rational and
874 norm_Rational_rls = norm_Rational_rls and
875 norm_Rational_min = norm_Rational_min and
876 norm_Rational_parenthesized = norm_Rational_parenthesized and
878 rat_mult_poly = rat_mult_poly and
879 rat_mult_div_pow = rat_mult_div_pow and
880 cancel_p_rls = cancel_p_rls
882 section \<open>A problem for simplification of rationals\<close>
884 problem pbl_simp_rat : "rational/simplification" =
885 \<open>Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)]\<close>
886 Method: "simplification/of_rationals"
889 Where: "t_t is_ratpolyexp"
890 Find: "normalform n_n"
892 section \<open>A methods for simplification of rationals\<close>
893 (*WN061025 this methods script is copied from (auto-generated) script
894 of norm_Rational in order to ease repair on inform*)
896 partial_function (tailrec) simplify :: "real \<Rightarrow> real"
899 (Try (Rewrite_Set ''discard_minus'') #>
900 Try (Rewrite_Set ''rat_mult_poly'') #>
901 Try (Rewrite_Set ''make_rat_poly_with_parentheses'') #>
902 Try (Rewrite_Set ''cancel_p_rls'') #>
904 Try (Rewrite_Set ''add_fractions_p_rls'') #>
905 Try (Rewrite_Set ''rat_mult_div_pow'') #>
906 Try (Rewrite_Set ''make_rat_poly_with_parentheses'') #>
907 Try (Rewrite_Set ''cancel_p_rls'') #>
908 Try (Rewrite_Set ''rat_reduce_1''))) #>
909 Try (Rewrite_Set ''discard_parentheses1''))
913 method met_simp_rat : "simplification/of_rationals" =
914 \<open>{rew_ord'="tless_true", rls' = Rule_Set.empty, calc = [], srls = Rule_Set.empty,
915 prls = Rule_Set.append_rules "simplification_of_rationals_prls" Rule_Set.empty
916 [(*for preds in where_*) \<^rule_eval>\<open>is_ratpolyexp\<close> (eval_is_ratpolyexp "")],
917 crls = Rule_Set.empty, errpats = [], nrls = norm_Rational_rls}\<close>
918 Program: simplify.simps
920 Where: "t_t is_ratpolyexp"
921 Find: "normalform n_n"