1 (* WN.020812: theorems in the Reals,
2 necessary for special rule sets, in addition to Isabelle2002.
3 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4 !!! THIS IS THE _least_ NUMBER OF ADDITIONAL THEOREMS !!!
5 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6 xxxI contain \<up> instead of ^ in the respective theorem xxx in 2002
7 changed by: Richard Lang 020912
10 theory Poly imports Simplify begin
12 subsection \<open>remark on term-structure of polynomials\<close>
15 the code below reflects missing coordination between two authors:
16 * ML: built the equation solver; simple rule-sets, programs; better predicates for specifications.
17 * MG: built simplification of polynomials with AC rewriting by ML code
20 *** there are 5 kinds of expanded normalforms ***
22 [1] 'complete polynomial' (Komplettes Polynom), univariate
23 a_0 + a_1.x^1 +...+ a_n.x^n not (a_n = 0)
24 not (a_n = 0), some a_i may be zero (DON'T disappear),
25 variables in monomials lexicographically ordered and complete,
26 x written as 1*x^1, ...
27 [2] 'polynomial' (Polynom), univariate and multivariate
28 a_0 + a_1.x +...+ a_n.x^n not (a_n = 0)
29 a_0 + a_1.x_1.x_2^n_12...x_m^n_1m +...+ a_n.x_1^n.x_2^n_n2...x_m^n_nm
30 not (a_n = 0), some a_i may be zero (ie. monomials disappear),
31 exponents and coefficients equal 1 are not (WN060904.TODO in cancel_p_)shown,
32 and variables in monomials are lexicographically ordered
33 examples: [1]: "1 + (-10) * x \<up> 1 + 25 * x \<up> 2"
34 [1]: "11 + 0 * x \<up> 1 + 1 * x \<up> 2"
35 [2]: "x + (-50) * x \<up> 3"
36 [2]: "(-1) * x * y \<up> 2 + 7 * x \<up> 3"
38 [3] 'expanded_term' (Ausmultiplizierter Term):
39 pull out unary minus to binary minus,
40 as frequently exercised in schools; other conditions for [2] hold however
41 examples: "a \<up> 2 - 2 * a * b + b \<up> 2"
42 "4 * x \<up> 2 - 9 * y \<up> 2"
43 [4] 'polynomial_in' (Polynom in):
44 polynomial in 1 variable with arbitrary coefficients
45 examples: "2 * x + (-50) * x \<up> 3" (poly in x)
46 "(u + v) + (2 * u \<up> 2) * a + (-u) * a \<up> 2 (poly in a)
47 [5] 'expanded_in' (Ausmultiplizierter Termin in):
48 analoguous to [3] with binary minus like [3]
49 examples: "2 * x - 50 * x \<up> 3" (expanded in x)
50 "(u + v) + (2 * u \<up> 2) * a - u * a \<up> 2 (expanded in a)
52 subsection \<open>consts definition for predicates in specifications\<close>
55 is_expanded_in :: "[real, real] => bool" ("_ is'_expanded'_in _")
56 is_poly_in :: "[real, real] => bool" ("_ is'_poly'_in _") (*RL DA *)
57 has_degree_in :: "[real, real] => real" ("_ has'_degree'_in _")(*RL DA *)
58 is_polyrat_in :: "[real, real] => bool" ("_ is'_polyrat'_in _")(*RL030626*)
60 is_multUnordered:: "real => bool" ("_ is'_multUnordered")
61 is_addUnordered :: "real => bool" ("_ is'_addUnordered") (*WN030618*)
62 is_polyexp :: "real => bool" ("_ is'_polyexp")
64 subsection \<open>theorems not yet adopted from Isabelle\<close>
65 axiomatization where (*.not contained in Isabelle2002,
66 stated as axioms, TODO: prove as theorems;
67 theorem-IDs 'xxxI' with \<up> instead of ^ in 'xxx' in Isabelle2002.*)
69 realpow_pow: "(a \<up> b) \<up> c = a \<up> (b * c)" and
70 realpow_addI: "r \<up> (n + m) = r \<up> n * r \<up> m" and
71 realpow_addI_assoc_l: "r \<up> n * (r \<up> m * s) = r \<up> (n + m) * s" and
72 realpow_addI_assoc_r: "s * r \<up> n * r \<up> m = s * r \<up> (n + m)" and
74 realpow_oneI: "r \<up> 1 = r" and
75 realpow_zeroI: "r \<up> 0 = 1" and
76 realpow_eq_oneI: "1 \<up> n = 1" and
77 realpow_multI: "(r * s) \<up> n = r \<up> n * s \<up> n" and
78 realpow_multI_poly: "[| r is_polyexp; s is_polyexp |] ==>
79 (r * s) \<up> n = r \<up> n * s \<up> n" and
80 realpow_minus_oneI: "(- 1) \<up> (2 * n) = 1" and
81 real_diff_0: "0 - x = - (x::real)" and
83 realpow_twoI: "r \<up> 2 = r * r" and
84 realpow_twoI_assoc_l: "r * (r * s) = r \<up> 2 * s" and
85 realpow_twoI_assoc_r: "s * r * r = s * r \<up> 2" and
86 realpow_two_atom: "r is_atom ==> r * r = r \<up> 2" and
87 realpow_plus_1: "r * r \<up> n = r \<up> (n + 1)" and
88 realpow_plus_1_assoc_l: "r * (r \<up> m * s) = r \<up> (1 + m) * s" and
89 realpow_plus_1_assoc_l2: "r \<up> m * (r * s) = r \<up> (1 + m) * s" and
90 realpow_plus_1_assoc_r: "s * r * r \<up> m = s * r \<up> (1 + m)" and
91 realpow_plus_1_atom: "r is_atom ==> r * r \<up> n = r \<up> (1 + n)" and
92 realpow_def_atom: "[| Not (r is_atom); 1 < n |]
93 ==> r \<up> n = r * r \<up> (n + -1)" and
94 realpow_addI_atom: "r is_atom ==> r \<up> n * r \<up> m = r \<up> (n + m)" and
97 realpow_minus_even: "n is_even ==> (- r) \<up> n = r \<up> n" and
98 realpow_minus_odd: "Not (n is_even) ==> (- r) \<up> n = -1 * r \<up> n" and
102 real_pp_binom_times: "(a + b)*(c + d) = a*c + a*d + b*c + b*d" and
103 real_pm_binom_times: "(a + b)*(c - d) = a*c - a*d + b*c - b*d" and
104 real_mp_binom_times: "(a - b)*(c + d) = a*c + a*d - b*c - b*d" and
105 real_mm_binom_times: "(a - b)*(c - d) = a*c - a*d - b*c + b*d" and
106 real_plus_binom_pow3: "(a + b) \<up> 3 = a \<up> 3 + 3*a \<up> 2*b + 3*a*b \<up> 2 + b \<up> 3" and
107 real_plus_binom_pow3_poly: "[| a is_polyexp; b is_polyexp |] ==>
108 (a + b) \<up> 3 = a \<up> 3 + 3*a \<up> 2*b + 3*a*b \<up> 2 + b \<up> 3" and
109 real_minus_binom_pow3: "(a - b) \<up> 3 = a \<up> 3 - 3*a \<up> 2*b + 3*a*b \<up> 2 - b \<up> 3" and
110 real_minus_binom_pow3_p: "(a + -1 * b) \<up> 3 = a \<up> 3 + -3*a \<up> 2*b + 3*a*b \<up> 2 +
112 (* real_plus_binom_pow: "[| n is_const; 3 < n |] ==>
113 (a + b) \<up> n = (a + b) * (a + b)\<up>(n - 1)" *)
114 real_plus_binom_pow4: "(a + b) \<up> 4 = (a \<up> 3 + 3*a \<up> 2*b + 3*a*b \<up> 2 + b \<up> 3)
116 real_plus_binom_pow4_poly: "[| a is_polyexp; b is_polyexp |] ==>
117 (a + b) \<up> 4 = (a \<up> 3 + 3*a \<up> 2*b + 3*a*b \<up> 2 + b \<up> 3)
119 real_plus_binom_pow5: "(a + b) \<up> 5 = (a \<up> 3 + 3*a \<up> 2*b + 3*a*b \<up> 2 + b \<up> 3)
120 *(a \<up> 2 + 2*a*b + b \<up> 2)" and
121 real_plus_binom_pow5_poly: "[| a is_polyexp; b is_polyexp |] ==>
122 (a + b) \<up> 5 = (a \<up> 3 + 3*a \<up> 2*b + 3*a*b \<up> 2
123 + b \<up> 3)*(a \<up> 2 + 2*a*b + b \<up> 2)" and
124 real_diff_plus: "a - b = a + -b" (*17.3.03: do_NOT_use*) and
125 real_diff_minus: "a - b = a + -1 * b" and
126 real_plus_binom_times: "(a + b)*(a + b) = a \<up> 2 + 2*a*b + b \<up> 2" and
127 real_minus_binom_times: "(a - b)*(a - b) = a \<up> 2 - 2*a*b + b \<up> 2" and
128 (*WN071229 changed for Schaerding -----vvv*)
129 (*real_plus_binom_pow2: "(a + b) \<up> 2 = a \<up> 2 + 2*a*b + b \<up> 2"*)
130 real_plus_binom_pow2: "(a + b) \<up> 2 = (a + b) * (a + b)" and
131 (*WN071229 changed for Schaerding -----\<up>*)
132 real_plus_binom_pow2_poly: "[| a is_polyexp; b is_polyexp |] ==>
133 (a + b) \<up> 2 = a \<up> 2 + 2*a*b + b \<up> 2" and
134 real_minus_binom_pow2: "(a - b) \<up> 2 = a \<up> 2 - 2*a*b + b \<up> 2" and
135 real_minus_binom_pow2_p: "(a - b) \<up> 2 = a \<up> 2 + -2*a*b + b \<up> 2" and
136 real_plus_minus_binom1: "(a + b)*(a - b) = a \<up> 2 - b \<up> 2" and
137 real_plus_minus_binom1_p: "(a + b)*(a - b) = a \<up> 2 + -1*b \<up> 2" and
138 real_plus_minus_binom1_p_p: "(a + b)*(a + -1 * b) = a \<up> 2 + -1*b \<up> 2" and
139 real_plus_minus_binom2: "(a - b)*(a + b) = a \<up> 2 - b \<up> 2" and
140 real_plus_minus_binom2_p: "(a - b)*(a + b) = a \<up> 2 + -1*b \<up> 2" and
141 real_plus_minus_binom2_p_p: "(a + -1 * b)*(a + b) = a \<up> 2 + -1*b \<up> 2" and
142 real_plus_binom_times1: "(a + 1*b)*(a + -1*b) = a \<up> 2 + -1*b \<up> 2" and
143 real_plus_binom_times2: "(a + -1*b)*(a + 1*b) = a \<up> 2 + -1*b \<up> 2" and
145 real_num_collect: "[| l is_const; m is_const |] ==>
146 l * n + m * n = (l + m) * n" and
147 (* FIXME.MG.0401: replace 'real_num_collect_assoc'
148 by 'real_num_collect_assoc_l' ... are equal, introduced by MG ! *)
149 real_num_collect_assoc: "[| l is_const; m is_const |] ==>
150 l * n + (m * n + k) = (l + m) * n + k" and
151 real_num_collect_assoc_l: "[| l is_const; m is_const |] ==>
152 l * n + (m * n + k) = (l + m)
154 real_num_collect_assoc_r: "[| l is_const; m is_const |] ==>
155 (k + m * n) + l * n = k + (l + m) * n" and
156 real_one_collect: "m is_const ==> n + m * n = (1 + m) * n" and
157 (* FIXME.MG.0401: replace 'real_one_collect_assoc'
158 by 'real_one_collect_assoc_l' ... are equal, introduced by MG ! *)
159 real_one_collect_assoc: "m is_const ==> n + (m * n + k) = (1 + m)* n + k" and
161 real_one_collect_assoc_l: "m is_const ==> n + (m * n + k) = (1 + m) * n + k" and
162 real_one_collect_assoc_r: "m is_const ==> (k + n) + m * n = k + (1 + m) * n" and
164 (* FIXME.MG.0401: replace 'real_mult_2_assoc'
165 by 'real_mult_2_assoc_l' ... are equal, introduced by MG ! *)
166 real_mult_2_assoc: "z1 + (z1 + k) = 2 * z1 + k" and
167 real_mult_2_assoc_l: "z1 + (z1 + k) = 2 * z1 + k" and
168 real_mult_2_assoc_r: "(k + z1) + z1 = k + 2 * z1" and
170 real_mult_left_commute: "z1 * (z2 * z3) = z2 * (z1 * z3)" and
171 real_mult_minus1: "-1 * z = - (z::real)" and
172 real_mult_2: "2 * z = z + (z::real)" and
174 real_add_mult_distrib_poly: "w is_polyexp ==> (z1 + z2) * w = z1 * w + z2 * w" and
175 real_add_mult_distrib2_poly:"w is_polyexp ==> w * (z1 + z2) = w * z1 + w * z2"
178 subsection \<open>auxiliary functions\<close>
181 ["Groups.plus_class.plus", "Groups.minus_class.minus",
182 "Rings.divide_class.divide", "Groups.times_class.times",
183 "Transcendental.powr"];
185 subsubsection \<open>for predicates in specifications (ML)\<close>
187 (*--- auxiliary for is_expanded_in, is_poly_in, has_degree_in ---*)
188 (*. a 'monomial t in variable v' is a term t with
189 either (1) v NOT existent in t, or (2) v contained in t,
191 if (2) then v is a factor on the very right, ev. with exponent.*)
192 fun factor_right_deg (*case 2*)
193 (Const ("Groups.times_class.times", _) $ t1 $ (Const ("Transcendental.powr",_) $ vv $ Free (d, _))) v =
194 if vv = v andalso not (Prog_Expr.occurs_in v t1) then SOME (TermC.int_of_str d) else NONE
195 | factor_right_deg (Const ("Transcendental.powr",_) $ vv $ Free (d,_)) v =
196 if (vv = v) then SOME (TermC.int_of_str d) else NONE
197 | factor_right_deg (Const ("Groups.times_class.times",_) $ t1 $ vv) v =
198 if vv = v andalso not (Prog_Expr.occurs_in v t1) then SOME 1 else NONE
199 | factor_right_deg vv v =
200 if (vv = v) then SOME 1 else NONE;
201 fun mono_deg_in m v = (*case 1*)
202 if not (Prog_Expr.occurs_in v m) then (*case 1*) SOME 0 else factor_right_deg m v;
204 fun expand_deg_in t v =
206 fun edi ~1 ~1 (Const ("Groups.plus_class.plus", _) $ t1 $ t2) =
207 (case mono_deg_in t2 v of (* $ is left associative*)
208 SOME d' => edi d' d' t1 | NONE => NONE)
209 | edi ~1 ~1 (Const ("Groups.minus_class.minus", _) $ t1 $ t2) =
210 (case mono_deg_in t2 v of
211 SOME d' => edi d' d' t1 | NONE => NONE)
212 | edi d dmax (Const ("Groups.minus_class.minus", _) $ t1 $ t2) =
213 (case mono_deg_in t2 v of (*(d = 0 andalso d' = 0) handle 3+4-...4 +x*)
214 SOME d' => if d > d' orelse (d = 0 andalso d' = 0) then edi d' dmax t1 else NONE
216 | edi d dmax (Const ("Groups.plus_class.plus",_) $ t1 $ t2) =
217 (case mono_deg_in t2 v of
218 SOME d' => (*RL (d = 0 andalso d' = 0) need to handle 3+4-...4 +x*)
219 if d > d' orelse (d = 0 andalso d' = 0) then edi d' dmax t1 else NONE
222 (case mono_deg_in t v of d as SOME _ => d | NONE => NONE)
223 | edi d dmax t = (*basecase last*)
224 (case mono_deg_in t v of
225 SOME d' => if d > d' orelse (d = 0 andalso d' = 0) then SOME dmax else NONE
229 fun poly_deg_in t v =
231 fun edi ~1 ~1 (Const ("Groups.plus_class.plus",_) $ t1 $ t2) =
232 (case mono_deg_in t2 v of (* $ is left associative *)
233 SOME d' => edi d' d' t1
235 | edi d dmax (Const ("Groups.plus_class.plus",_) $ t1 $ t2) =
236 (case mono_deg_in t2 v of
237 SOME d' => (*RL (d = 0 andalso (d' = 0)) handle 3+4-...4 +x*)
238 if d > d' orelse (d = 0 andalso d' = 0) then edi d' dmax t1 else NONE
241 (case mono_deg_in t v of
244 | edi d dmax t = (* basecase last *)
245 (case mono_deg_in t v of
247 if d > d' orelse (d = 0 andalso d' = 0) then SOME dmax else NONE
252 subsubsection \<open>for hard-coded AC rewriting (MG)\<close>
254 (**. MG.03: make_polynomial_ ... uses SML-fun for ordering .**)
256 (*FIXME.0401: make SML-order local to make_polynomial(_) *)
257 (*FIXME.0401: replace 'make_polynomial'(old) by 'make_polynomial_'(MG) *)
258 (* Polynom --> List von Monomen *)
259 fun poly2list (Const ("Groups.plus_class.plus",_) $ t1 $ t2) =
260 (poly2list t1) @ (poly2list t2)
263 (* Monom --> Liste von Variablen *)
264 fun monom2list (Const ("Groups.times_class.times",_) $ t1 $ t2) =
265 (monom2list t1) @ (monom2list t2)
266 | monom2list t = [t];
268 (* liefert Variablenname (String) einer Variablen und Basis bei Potenz *)
269 fun get_basStr (Const ("Transcendental.powr",_) $ Free (str, _) $ _) = str
270 | get_basStr (Free (str, _)) = str
271 | get_basStr _ = "|||"; (* gross gewichtet; für Brüch ect. *)
273 raise ERROR("get_basStr: called with t= "^(UnparseC.term t));*)
275 (* liefert Hochzahl (String) einer Variablen bzw Gewichtstring (zum Sortieren) *)
276 fun get_potStr (Const ("Transcendental.powr",_) $ Free _ $ Free (str, _)) = str
277 | get_potStr (Const ("Transcendental.powr",_) $ Free _ $ _ ) = "|||" (* gross gewichtet *)
278 | get_potStr (Free (_, _)) = "---" (* keine Hochzahl --> kleinst gewichtet *)
279 | get_potStr _ = "||||||"; (* gross gewichtet; für Brüch ect. *)
281 raise ERROR("get_potStr: called with t= "^(UnparseC.term t));*)
283 (* Umgekehrte string_ord *)
284 val string_ord_rev = rev_order o string_ord;
286 (* Ordnung zum lexikographischen Vergleich zweier Variablen (oder Potenzen)
287 innerhalb eines Monomes:
288 - zuerst lexikographisch nach Variablenname
289 - wenn gleich: nach steigender Potenz *)
290 fun var_ord (a,b: term) = prod_ord string_ord string_ord
291 ((get_basStr a, get_potStr a), (get_basStr b, get_potStr b));
293 (* Ordnung zum lexikographischen Vergleich zweier Variablen (oder Potenzen);
294 verwendet zum Sortieren von Monomen mittels Gesamtgradordnung:
295 - zuerst lexikographisch nach Variablenname
296 - wenn gleich: nach sinkender Potenz*)
297 fun var_ord_revPow (a,b: term) = prod_ord string_ord string_ord_rev
298 ((get_basStr a, get_potStr a), (get_basStr b, get_potStr b));
301 (* Ordnet ein Liste von Variablen (und Potenzen) lexikographisch *)
302 val sort_varList = sort var_ord;
304 (* Entfernet aeussersten Operator (Wurzel) aus einem Term und schreibt
305 Argumente in eine Liste *)
306 fun args u : term list =
307 let fun stripc (f$t, ts) = stripc (f, t::ts)
308 | stripc (t as Free _, ts) = (t::ts)
309 | stripc (_, ts) = ts
310 in stripc (u, []) end;
312 (* liefert True, falls der Term (Liste von Termen) nur Zahlen
313 (keine Variablen) enthaelt *)
314 fun filter_num [] = true
315 | filter_num [Free x] = if (TermC.is_num (Free x)) then true
317 | filter_num ((Free _)::_) = false
319 (filter_num o (filter_out TermC.is_num) o flat o (map args)) ts;
321 (* liefert True, falls der Term nur Zahlen (keine Variablen) enthaelt
322 dh. er ist ein numerischer Wert und entspricht einem Koeffizienten *)
323 fun is_nums t = filter_num [t];
325 (* Berechnet den Gesamtgrad eines Monoms *)
327 fun counter (n, []) = n
328 | counter (n, x :: xs) =
333 (Const ("Transcendental.powr", _) $ Free _ $ Free (str_h, T)) =>
334 if (is_nums (Free (str_h, T))) then
335 counter (n + (the (TermC.int_opt_of_string str_h)), xs)
336 else counter (n + 1000, xs) (*FIXME.MG?!*)
337 | (Const ("Transcendental.powr", _) $ Free _ $ _ ) =>
338 counter (n + 1000, xs) (*FIXME.MG?!*)
339 | (Free _) => counter (n + 1, xs)
340 (*| _ => raise ERROR("monom_degree: called with factor: "^(UnparseC.term x)))*)
341 | _ => counter (n + 10000, xs)) (*FIXME.MG?! ... Brüche ect.*)
343 fun monom_degree l = counter (0, l)
346 (* wie Ordnung dict_ord (lexicographische Ordnung zweier Listen, mit Vergleich
347 der Listen-Elemente mit elem_ord) - Elemente die Bedingung cond erfuellen,
348 werden jedoch dabei ignoriert (uebersprungen) *)
349 fun dict_cond_ord _ _ ([], []) = EQUAL
350 | dict_cond_ord _ _ ([], _ :: _) = LESS
351 | dict_cond_ord _ _ (_ :: _, []) = GREATER
352 | dict_cond_ord elem_ord cond (x :: xs, y :: ys) =
353 (case (cond x, cond y) of
354 (false, false) => (case elem_ord (x, y) of
355 EQUAL => dict_cond_ord elem_ord cond (xs, ys)
357 | (false, true) => dict_cond_ord elem_ord cond (x :: xs, ys)
358 | (true, false) => dict_cond_ord elem_ord cond (xs, y :: ys)
359 | (true, true) => dict_cond_ord elem_ord cond (xs, ys) );
361 (* Gesamtgradordnung zum Vergleich von Monomen (Liste von Variablen/Potenzen):
362 zuerst nach Gesamtgrad, bei gleichem Gesamtgrad lexikographisch ordnen -
363 dabei werden Koeffizienten ignoriert (2*3*a \<up> 2*4*b gilt wie a \<up> 2*b) *)
364 fun degree_ord (xs, ys) =
365 prod_ord int_ord (dict_cond_ord var_ord_revPow is_nums)
366 ((monom_degree xs, xs), (monom_degree ys, ys));
368 fun hd_str str = substring (str, 0, 1);
369 fun tl_str str = substring (str, 1, (size str) - 1);
371 (* liefert nummerischen Koeffizienten eines Monoms oder NONE *)
372 fun get_koeff_of_mon [] = raise ERROR("get_koeff_of_mon: called with l = []")
373 | get_koeff_of_mon (x::_) = if is_nums x then SOME x else NONE;
375 (* wandelt Koeffizient in (zum sortieren geeigneten) String um *)
376 fun koeff2ordStr (SOME x) = (case x of
378 if (hd_str str) = "-" then (tl_str str)^"0" (* 3 < -3 *)
380 | _ => "aaa") (* "num.Ausdruck" --> gross *)
381 | koeff2ordStr NONE = "---"; (* "kein Koeff" --> kleinste *)
383 (* Order zum Vergleich von Koeffizienten (strings):
384 "kein Koeff" < "0" < "1" < "-1" < "2" < "-2" < ... < "num.Ausdruck" *)
385 fun compare_koeff_ord (xs, ys) =
386 string_ord ((koeff2ordStr o get_koeff_of_mon) xs,
387 (koeff2ordStr o get_koeff_of_mon) ys);
389 (* Gesamtgradordnung degree_ord + Ordnen nach Koeffizienten falls EQUAL *)
390 fun koeff_degree_ord (xs, ys) =
391 prod_ord degree_ord compare_koeff_ord ((xs, xs), (ys, ys));
393 (* Ordnet ein Liste von Monomen (Monom = Liste von Variablen) mittels
395 val sort_monList = sort koeff_degree_ord;
397 (* Alternativ zu degree_ord koennte auch die viel einfachere und
398 kuerzere Ordnung simple_ord verwendet werden - ist aber nicht
399 fuer unsere Zwecke geeignet!
401 fun simple_ord (al,bl: term list) = dict_ord string_ord
402 (map get_basStr al, map get_basStr bl);
404 val sort_monList = sort simple_ord; *)
406 (* aus 2 Variablen wird eine Summe bzw ein Produkt erzeugt
407 (mit gewuenschtem Typen T) *)
408 fun plus T = Const ("Groups.plus_class.plus", [T,T] ---> T);
409 fun mult T = Const ("Groups.times_class.times", [T,T] ---> T);
410 fun binop op_ t1 t2 = op_ $ t1 $ t2;
411 fun create_prod T (a,b) = binop (mult T) a b;
412 fun create_sum T (a,b) = binop (plus T) a b;
414 (* löscht letztes Element einer Liste *)
415 fun drop_last l = take ((length l)-1,l);
417 (* Liste von Variablen --> Monom *)
418 fun create_monom T vl = foldr (create_prod T) (drop_last vl, last_elem vl);
420 foldr bewirkt rechtslastige Klammerung des Monoms - ist notwendig, damit zwei
421 gleiche Monome zusammengefasst werden können (collect_numerals)!
422 zB: 2*(x*(y*z)) + 3*(x*(y*z)) --> (2+3)*(x*(y*z))*)
424 (* Liste von Monomen --> Polynom *)
425 fun create_polynom T ml = foldl (create_sum T) (hd ml, tl ml);
427 foldl bewirkt linkslastige Klammerung des Polynoms (der Summanten) -
428 bessere Darstellung, da keine Klammern sichtbar!
429 (und discard_parentheses in make_polynomial hat weniger zu tun) *)
431 (* sorts the variables (faktors) of an expanded polynomial lexicographical *)
432 fun sort_variables t =
434 val ll = map monom2list (poly2list t);
435 val lls = map sort_varList ll;
437 val ls = map (create_monom T) lls;
438 in create_polynom T ls end;
440 (* sorts the monoms of an expanded and variable-sorted polynomial
444 val ll = map monom2list (poly2list t);
445 val lls = sort_monList ll;
447 val ls = map (create_monom T) lls;
448 in create_polynom T ls end;
451 subsubsection \<open>rewrite order for hard-coded AC rewriting\<close>
453 local (*. for make_polynomial .*)
455 open Term; (* for type order = EQUAL | LESS | GREATER *)
457 fun pr_ord EQUAL = "EQUAL"
458 | pr_ord LESS = "LESS"
459 | pr_ord GREATER = "GREATER";
461 fun dest_hd' (Const (a, T)) = (* ~ term.ML *)
463 "Transcendental.powr" => ((("|||||||||||||", 0), T), 0) (*WN greatest string*)
464 | _ => (((a, 0), T), 0))
465 | dest_hd' (Free (a, T)) = (((a, 0), T), 1)
466 | dest_hd' (Var v) = (v, 2)
467 | dest_hd' (Bound i) = ((("", i), dummyT), 3)
468 | dest_hd' (Abs (_, T, _)) = ((("", 0), T), 4)
469 | dest_hd' t = raise TERM ("dest_hd'", [t]);
471 fun size_of_term' (Const(str,_) $ t) =
472 if "Transcendental.powr"= str then 1000 + size_of_term' t else 1+size_of_term' t(*WN*)
473 | size_of_term' (Abs (_,_,body)) = 1 + size_of_term' body
474 | size_of_term' (f$t) = size_of_term' f + size_of_term' t
475 | size_of_term' _ = 1;
477 fun term_ord' pr thy (Abs (_, T, t), Abs(_, U, u)) = (* ~ term.ML *)
478 (case term_ord' pr thy (t, u) of EQUAL => Term_Ord.typ_ord (T, U) | ord => ord)
479 | term_ord' pr thy (t, u) =
482 val (f, ts) = strip_comb t and (g, us) = strip_comb u;
483 val _ = tracing ("t= f@ts= \"" ^ UnparseC.term_in_thy thy f ^ "\" @ \"[" ^
484 commas (map (UnparseC.term_in_thy thy) ts) ^ "]\"");
485 val _ = tracing("u= g@us= \"" ^ UnparseC.term_in_thy thy g ^ "\" @ \"[" ^
486 commas (map (UnparseC.term_in_thy thy) us) ^ "]\"");
487 val _ = tracing ("size_of_term(t,u)= (" ^ string_of_int (size_of_term' t) ^ ", " ^
488 string_of_int (size_of_term' u) ^ ")");
489 val _ = tracing ("hd_ord(f,g) = " ^ (pr_ord o hd_ord) (f,g));
490 val _ = tracing ("terms_ord(ts,us) = " ^ (pr_ord o terms_ord str false) (ts, us));
491 val _ = tracing ("-------");
494 case int_ord (size_of_term' t, size_of_term' u) of
496 let val (f, ts) = strip_comb t and (g, us) = strip_comb u in
497 (case hd_ord (f, g) of EQUAL => (terms_ord str pr) (ts, us)
501 and hd_ord (f, g) = (* ~ term.ML *)
502 prod_ord (prod_ord Term_Ord.indexname_ord Term_Ord.typ_ord) int_ord (dest_hd' f, dest_hd' g)
503 and terms_ord _ pr (ts, us) =
504 list_ord (term_ord' pr (ThyC.get_theory "Isac_Knowledge"))(ts, us);
508 fun ord_make_polynomial (pr:bool) thy (_: subst) tu =
509 (term_ord' pr thy(***) tu = LESS );
513 Rewrite_Ord.rew_ord' := overwritel (! Rewrite_Ord.rew_ord', (* TODO: make analogous to KEStore_Elems.add_mets *)
514 [("termlessI", termlessI), ("ord_make_polynomial", ord_make_polynomial false \<^theory>)]);
517 subsection \<open>predicates\<close>
518 subsubsection \<open>in specifications\<close>
520 (* is_polyrat_in becomes true, if no bdv is in the denominator of a fraction*)
521 fun is_polyrat_in t v =
523 fun finddivide (_ $ _ $ _ $ _) _ = raise ERROR("is_polyrat_in:")
524 (* at the moment there is no term like this, but ....*)
525 | finddivide (Const ("Rings.divide_class.divide",_) $ _ $ b) v = not (Prog_Expr.occurs_in v b)
526 | finddivide (_ $ t1 $ t2) v = finddivide t1 v orelse finddivide t2 v
527 | finddivide (_ $ t1) v = finddivide t1 v
528 | finddivide _ _ = false;
529 in finddivide t v end;
531 fun is_expanded_in t v = case expand_deg_in t v of SOME _ => true | NONE => false;
532 fun is_poly_in t v = case poly_deg_in t v of SOME _ => true | NONE => false;
533 fun has_degree_in t v = case expand_deg_in t v of SOME d => d | NONE => ~1;
535 (*.the expression contains + - * ^ only ?
536 this is weaker than 'is_polynomial' !.*)
537 fun is_polyexp (Free _) = true
538 | is_polyexp (Const _) = true (* potential danger: bdv is not considered *)
539 | is_polyexp (Const ("Groups.plus_class.plus",_) $ Free _ $ Free _) = true
540 | is_polyexp (Const ("Groups.minus_class.minus",_) $ Free _ $ Free _) = true
541 | is_polyexp (Const ("Groups.times_class.times",_) $ Free _ $ Free _) = true
542 | is_polyexp (Const ("Transcendental.powr",_) $ Free _ $ Free _) = true
543 | is_polyexp (Const ("Groups.plus_class.plus",_) $ t1 $ t2) =
544 ((is_polyexp t1) andalso (is_polyexp t2))
545 | is_polyexp (Const ("Groups.minus_class.minus",_) $ t1 $ t2) =
546 ((is_polyexp t1) andalso (is_polyexp t2))
547 | is_polyexp (Const ("Groups.times_class.times",_) $ t1 $ t2) =
548 ((is_polyexp t1) andalso (is_polyexp t2))
549 | is_polyexp (Const ("Transcendental.powr",_) $ t1 $ t2) =
550 ((is_polyexp t1) andalso (is_polyexp t2))
551 | is_polyexp _ = false;
554 subsubsection \<open>for hard-coded AC rewriting\<close>
556 (* auch Klammerung muss übereinstimmen;
557 sort_variables klammert Produkte rechtslastig*)
558 fun is_multUnordered t = ((is_polyexp t) andalso not (t = sort_variables t));
560 fun is_addUnordered t = ((is_polyexp t) andalso not (t = sort_monoms t));
563 subsection \<open>evaluations functions\<close>
564 subsubsection \<open>for predicates\<close>
566 fun eval_is_polyrat_in _ _(p as (Const ("Poly.is_polyrat_in",_) $ t $ v)) _ =
568 then SOME ((UnparseC.term p) ^ " = True",
569 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
570 else SOME ((UnparseC.term p) ^ " = True",
571 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False})))
572 | eval_is_polyrat_in _ _ _ _ = ((*tracing"### no matches";*) NONE);
574 (*("is_expanded_in", ("Poly.is_expanded_in", eval_is_expanded_in ""))*)
575 fun eval_is_expanded_in _ _
576 (p as (Const ("Poly.is_expanded_in",_) $ t $ v)) _ =
577 if is_expanded_in t v
578 then SOME ((UnparseC.term p) ^ " = True",
579 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
580 else SOME ((UnparseC.term p) ^ " = True",
581 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False})))
582 | eval_is_expanded_in _ _ _ _ = NONE;
584 (*("is_poly_in", ("Poly.is_poly_in", eval_is_poly_in ""))*)
585 fun eval_is_poly_in _ _
586 (p as (Const ("Poly.is_poly_in",_) $ t $ v)) _ =
588 then SOME ((UnparseC.term p) ^ " = True",
589 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
590 else SOME ((UnparseC.term p) ^ " = True",
591 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False})))
592 | eval_is_poly_in _ _ _ _ = NONE;
594 (*("has_degree_in", ("Poly.has_degree_in", eval_has_degree_in ""))*)
595 fun eval_has_degree_in _ _
596 (p as (Const ("Poly.has_degree_in",_) $ t $ v)) _ =
597 let val d = has_degree_in t v
598 val d' = TermC.term_of_num HOLogic.realT d
599 in SOME ((UnparseC.term p) ^ " = " ^ (string_of_int d),
600 HOLogic.Trueprop $ (TermC.mk_equality (p, d')))
602 | eval_has_degree_in _ _ _ _ = NONE;
604 (*("is_polyexp", ("Poly.is_polyexp", eval_is_polyexp ""))*)
605 fun eval_is_polyexp (thmid:string) _
606 (t as (Const("Poly.is_polyexp", _) $ arg)) thy =
608 then SOME (TermC.mk_thmid thmid (UnparseC.term_in_thy thy arg) "",
609 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
610 else SOME (TermC.mk_thmid thmid (UnparseC.term_in_thy thy arg) "",
611 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
612 | eval_is_polyexp _ _ _ _ = NONE;
615 subsubsection \<open>for hard-coded AC rewriting\<close>
618 (*("is_addUnordered", ("Poly.is_addUnordered", eval_is_addUnordered ""))*)
619 fun eval_is_addUnordered (thmid:string) _
620 (t as (Const("Poly.is_addUnordered", _) $ arg)) thy =
621 if is_addUnordered arg
622 then SOME (TermC.mk_thmid thmid (UnparseC.term_in_thy thy arg) "",
623 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
624 else SOME (TermC.mk_thmid thmid (UnparseC.term_in_thy thy arg) "",
625 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
626 | eval_is_addUnordered _ _ _ _ = NONE;
628 fun eval_is_multUnordered (thmid:string) _
629 (t as (Const("Poly.is_multUnordered", _) $ arg)) thy =
630 if is_multUnordered arg
631 then SOME (TermC.mk_thmid thmid (UnparseC.term_in_thy thy arg) "",
632 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
633 else SOME (TermC.mk_thmid thmid (UnparseC.term_in_thy thy arg) "",
634 HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
635 | eval_is_multUnordered _ _ _ _ = NONE;
637 setup \<open>KEStore_Elems.add_calcs
638 [("is_polyrat_in", ("Poly.is_polyrat_in",
639 eval_is_polyrat_in "#eval_is_polyrat_in")),
640 ("is_expanded_in", ("Poly.is_expanded_in", eval_is_expanded_in "")),
641 ("is_poly_in", ("Poly.is_poly_in", eval_is_poly_in "")),
642 ("has_degree_in", ("Poly.has_degree_in", eval_has_degree_in "")),
643 ("is_polyexp", ("Poly.is_polyexp", eval_is_polyexp "")),
644 ("is_multUnordered", ("Poly.is_multUnordered", eval_is_multUnordered"")),
645 ("is_addUnordered", ("Poly.is_addUnordered", eval_is_addUnordered ""))]\<close>
647 subsection \<open>rule-sets\<close>
648 subsubsection \<open>without specific order\<close>
650 (* used only for merge *)
651 val calculate_Poly = Rule_Set.append_rules "calculate_PolyFIXXXME.not.impl." Rule_Set.empty [];
653 (*.for evaluation of conditions in rewrite rules.*)
654 val Poly_erls = Rule_Set.append_rules "Poly_erls" Atools_erls
655 [\<^rule_eval>\<open>HOL.eq\<close> (Prog_Expr.eval_equal "#equal_"),
656 \<^rule_thm>\<open>real_unari_minus\<close>,
657 \<^rule_eval>\<open>plus\<close> (eval_binop "#add_"),
658 \<^rule_eval>\<open>minus\<close> (eval_binop "#sub_"),
659 \<^rule_eval>\<open>times\<close> (eval_binop "#mult_"),
660 \<^rule_eval>\<open>powr\<close> (eval_binop "#power_")];
662 val poly_crls = Rule_Set.append_rules "poly_crls" Atools_crls
663 [\<^rule_eval>\<open>HOL.eq\<close> (Prog_Expr.eval_equal "#equal_"),
664 \<^rule_thm>\<open>real_unari_minus\<close>,
665 \<^rule_eval>\<open>plus\<close> (eval_binop "#add_"),
666 \<^rule_eval>\<open>minus\<close> (eval_binop "#sub_"),
667 \<^rule_eval>\<open>times\<close> (eval_binop "#mult_"),
668 \<^rule_eval>\<open>powr\<close> (eval_binop "#power_")];
672 Rule_Def.Repeat {id = "expand", preconds = [], rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
673 erls = Rule_Set.empty,srls = Rule_Set.Empty, calc = [], errpatts = [],
674 rules = [\<^rule_thm>\<open>distrib_right\<close>,
675 (*"(z1.0 + z2.0) * w = z1.0 * w + z2.0 * w"*)
676 \<^rule_thm>\<open>distrib_left\<close>
677 (*"w * (z1.0 + z2.0) = w * z1.0 + w * z2.0"*)
678 ], scr = Rule.Empty_Prog};
681 Rule_Def.Repeat {id = "discard_minus", preconds = [], rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
682 erls = Rule_Set.empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
684 [\<^rule_thm>\<open>real_diff_minus\<close>,
685 (*"a - b = a + -1 * b"*)
686 \<^rule_thm_sym>\<open>real_mult_minus1\<close>
687 (*- ?z = "-1 * ?z"*)],
688 scr = Rule.Empty_Prog};
691 Rule_Def.Repeat{id = "expand_poly_", preconds = [],
692 rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
693 erls = Rule_Set.empty,srls = Rule_Set.Empty,
694 calc = [], errpatts = [],
696 [\<^rule_thm>\<open>real_plus_binom_pow4\<close>,
697 (*"(a + b) \<up> 4 = ... "*)
698 \<^rule_thm>\<open>real_plus_binom_pow5\<close>,
699 (*"(a + b) \<up> 5 = ... "*)
700 \<^rule_thm>\<open>real_plus_binom_pow3\<close>,
701 (*"(a + b) \<up> 3 = a \<up> 3 + 3*a \<up> 2*b + 3*a*b \<up> 2 + b \<up> 3" *)
702 (*WN071229 changed/removed for Schaerding -----vvv*)
703 (*\<^rule_thm>\<open>real_plus_binom_pow2\<close>,*)
704 (*"(a + b) \<up> 2 = a \<up> 2 + 2*a*b + b \<up> 2"*)
705 \<^rule_thm>\<open>real_plus_binom_pow2\<close>,
706 (*"(a + b) \<up> 2 = (a + b) * (a + b)"*)
707 (*\<^rule_thm>\<open>real_plus_minus_binom1_p_p\<close>,*)
708 (*"(a + b)*(a + -1 * b) = a \<up> 2 + -1*b \<up> 2"*)
709 (*\<^rule_thm>\<open>real_plus_minus_binom2_p_p\<close>,*)
710 (*"(a + -1 * b)*(a + b) = a \<up> 2 + -1*b \<up> 2"*)
711 (*WN071229 changed/removed for Schaerding -----\<up>*)
713 \<^rule_thm>\<open>distrib_right\<close>,
714 (*"(z1.0 + z2.0) * w = z1.0 * w + z2.0 * w"*)
715 \<^rule_thm>\<open>distrib_left\<close>,
716 (*"w * (z1.0 + z2.0) = w * z1.0 + w * z2.0"*)
718 \<^rule_thm>\<open>realpow_multI\<close>,
719 (*"(r * s) \<up> n = r \<up> n * s \<up> n"*)
720 \<^rule_thm>\<open>realpow_pow\<close>
721 (*"(a \<up> b) \<up> c = a \<up> (b * c)"*)
722 ], scr = Rule.Empty_Prog};
724 val expand_poly_rat_ =
725 Rule_Def.Repeat{id = "expand_poly_rat_", preconds = [],
726 rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
727 erls = Rule_Set.append_rules "Rule_Set.empty-is_polyexp" Rule_Set.empty
728 [\<^rule_eval>\<open>is_polyexp\<close> (eval_is_polyexp "")
730 srls = Rule_Set.Empty,
731 calc = [], errpatts = [],
733 [\<^rule_thm>\<open>real_plus_binom_pow4_poly\<close>,
734 (*"[| a is_polyexp; b is_polyexp |] ==> (a + b) \<up> 4 = ... "*)
735 \<^rule_thm>\<open>real_plus_binom_pow5_poly\<close>,
736 (*"[| a is_polyexp; b is_polyexp |] ==> (a + b) \<up> 5 = ... "*)
737 \<^rule_thm>\<open>real_plus_binom_pow2_poly\<close>,
738 (*"[| a is_polyexp; b is_polyexp |] ==>
739 (a + b) \<up> 2 = a \<up> 2 + 2*a*b + b \<up> 2"*)
740 \<^rule_thm>\<open>real_plus_binom_pow3_poly\<close>,
741 (*"[| a is_polyexp; b is_polyexp |] ==>
742 (a + b) \<up> 3 = a \<up> 3 + 3*a \<up> 2*b + 3*a*b \<up> 2 + b \<up> 3" *)
743 \<^rule_thm>\<open>real_plus_minus_binom1_p_p\<close>,
744 (*"(a + b)*(a + -1 * b) = a \<up> 2 + -1*b \<up> 2"*)
745 \<^rule_thm>\<open>real_plus_minus_binom2_p_p\<close>,
746 (*"(a + -1 * b)*(a + b) = a \<up> 2 + -1*b \<up> 2"*)
748 \<^rule_thm>\<open>real_add_mult_distrib_poly\<close>,
749 (*"w is_polyexp ==> (z1.0 + z2.0) * w = z1.0 * w + z2.0 * w"*)
750 \<^rule_thm>\<open>real_add_mult_distrib2_poly\<close>,
751 (*"w is_polyexp ==> w * (z1.0 + z2.0) = w * z1.0 + w * z2.0"*)
753 \<^rule_thm>\<open>realpow_multI_poly\<close>,
754 (*"[| r is_polyexp; s is_polyexp |] ==>
755 (r * s) \<up> n = r \<up> n * s \<up> n"*)
756 \<^rule_thm>\<open>realpow_pow\<close>
757 (*"(a \<up> b) \<up> c = a \<up> (b * c)"*)
758 ], scr = Rule.Empty_Prog};
760 val simplify_power_ =
761 Rule_Def.Repeat{id = "simplify_power_", preconds = [],
762 rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
763 erls = Rule_Set.empty, srls = Rule_Set.Empty,
764 calc = [], errpatts = [],
765 rules = [(*MG: Reihenfolge der folgenden 2 Rule.Thm muss so bleiben, wegen
766 a*(a*a) --> a*a \<up> 2 und nicht a*(a*a) --> a \<up> 2*a *)
767 \<^rule_thm_sym>\<open>realpow_twoI\<close>,
768 (*"r * r = r \<up> 2"*)
769 \<^rule_thm>\<open>realpow_twoI_assoc_l\<close>,
770 (*"r * (r * s) = r \<up> 2 * s"*)
772 \<^rule_thm>\<open>realpow_plus_1\<close>,
773 (*"r * r \<up> n = r \<up> (n + 1)"*)
774 \<^rule_thm>\<open>realpow_plus_1_assoc_l\<close>,
775 (*"r * (r \<up> m * s) = r \<up> (1 + m) * s"*)
776 (*MG 9.7.03: neues Rule.Thm wegen a*(a*(a*b)) --> a \<up> 2*(a*b) *)
777 \<^rule_thm>\<open>realpow_plus_1_assoc_l2\<close>,
778 (*"r \<up> m * (r * s) = r \<up> (1 + m) * s"*)
780 \<^rule_thm_sym>\<open>realpow_addI\<close>,
781 (*"r \<up> n * r \<up> m = r \<up> (n + m)"*)
782 \<^rule_thm>\<open>realpow_addI_assoc_l\<close>,
783 (*"r \<up> n * (r \<up> m * s) = r \<up> (n + m) * s"*)
785 (* ist in expand_poly - wird hier aber auch gebraucht, wegen:
786 "r * r = r \<up> 2" wenn r=a \<up> b*)
787 \<^rule_thm>\<open>realpow_pow\<close>
788 (*"(a \<up> b) \<up> c = a \<up> (b * c)"*)
789 ], scr = Rule.Empty_Prog};
791 val calc_add_mult_pow_ =
792 Rule_Def.Repeat{id = "calc_add_mult_pow_", preconds = [],
793 rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
794 erls = Atools_erls(*erls3.4.03*),srls = Rule_Set.Empty,
795 calc = [("PLUS" , ("Groups.plus_class.plus", eval_binop "#add_")),
796 ("TIMES" , ("Groups.times_class.times", eval_binop "#mult_")),
797 ("POWER", ("Transcendental.powr", eval_binop "#power_"))
800 rules = [\<^rule_eval>\<open>plus\<close> (eval_binop "#add_"),
801 \<^rule_eval>\<open>times\<close> (eval_binop "#mult_"),
802 \<^rule_eval>\<open>powr\<close> (eval_binop "#power_")
803 ], scr = Rule.Empty_Prog};
805 val reduce_012_mult_ =
806 Rule_Def.Repeat{id = "reduce_012_mult_", preconds = [],
807 rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
808 erls = Rule_Set.empty,srls = Rule_Set.Empty,
809 calc = [], errpatts = [],
810 rules = [(* MG: folgende Rule.Thm müssen hier stehen bleiben: *)
811 \<^rule_thm>\<open>mult_1_right\<close>,
812 (*"z * 1 = z"*) (*wegen "a * b * b \<up> (-1) + a"*)
813 \<^rule_thm>\<open>realpow_zeroI\<close>,
814 (*"r \<up> 0 = 1"*) (*wegen "a*a \<up> (-1)*c + b + c"*)
815 \<^rule_thm>\<open>realpow_oneI\<close>,
817 \<^rule_thm>\<open>realpow_eq_oneI\<close>
819 ], scr = Rule.Empty_Prog};
821 val collect_numerals_ =
822 Rule_Def.Repeat{id = "collect_numerals_", preconds = [],
823 rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
824 erls = Atools_erls, srls = Rule_Set.Empty,
825 calc = [("PLUS" , ("Groups.plus_class.plus", eval_binop "#add_"))
828 [\<^rule_thm>\<open>real_num_collect\<close>,
829 (*"[| l is_const; m is_const |]==>l * n + m * n = (l + m) * n"*)
830 \<^rule_thm>\<open>real_num_collect_assoc_r\<close>,
831 (*"[| l is_const; m is_const |] ==> \
832 \(k + m * n) + l * n = k + (l + m)*n"*)
833 \<^rule_thm>\<open>real_one_collect\<close>,
834 (*"m is_const ==> n + m * n = (1 + m) * n"*)
835 \<^rule_thm>\<open>real_one_collect_assoc_r\<close>,
836 (*"m is_const ==> (k + n) + m * n = k + (m + 1) * n"*)
838 \<^rule_eval>\<open>plus\<close> (eval_binop "#add_"),
840 (*MG: Reihenfolge der folgenden 2 Rule.Thm muss so bleiben, wegen
841 (a+a)+a --> a + 2*a --> 3*a and not (a+a)+a --> 2*a + a *)
842 \<^rule_thm>\<open>real_mult_2_assoc_r\<close>,
843 (*"(k + z1) + z1 = k + 2 * z1"*)
844 \<^rule_thm_sym>\<open>real_mult_2\<close>
845 (*"z1 + z1 = 2 * z1"*)
846 ], scr = Rule.Empty_Prog};
849 Rule_Def.Repeat{id = "reduce_012_", preconds = [],
850 rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
851 erls = Rule_Set.empty,srls = Rule_Set.Empty, calc = [], errpatts = [],
852 rules = [\<^rule_thm>\<open>mult_1_left\<close>,
854 \<^rule_thm>\<open>mult_zero_left\<close>,
856 \<^rule_thm>\<open>mult_zero_right\<close>,
858 \<^rule_thm>\<open>add_0_left\<close>,
860 \<^rule_thm>\<open>add_0_right\<close>,
861 (*"z + 0 = z"*) (*wegen a+b-b --> a+(1-1)*b --> a+0 --> a*)
863 (*\<^rule_thm>\<open>realpow_oneI\<close>*)
864 (*"?r \<up> 1 = ?r"*)
865 \<^rule_thm>\<open>division_ring_divide_zero\<close>
867 ], scr = Rule.Empty_Prog};
869 val discard_parentheses1 =
870 Rule_Set.append_rules "discard_parentheses1" Rule_Set.empty
871 [\<^rule_thm_sym>\<open>mult.assoc\<close>
872 (*"?z1.1 * (?z2.1 * ?z3.1) = ?z1.1 * ?z2.1 * ?z3.1"*)
873 (*\<^rule_thm_sym>\<open>add.assoc\<close>*)
874 (*"?z1.1 + (?z2.1 + ?z3.1) = ?z1.1 + ?z2.1 + ?z3.1"*)
878 Rule_Def.Repeat{id = "expand_poly", preconds = [],
879 rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
880 erls = Rule_Set.empty,srls = Rule_Set.Empty,
881 calc = [], errpatts = [],
883 rules = [\<^rule_thm>\<open>distrib_right\<close>,
884 (*"(z1.0 + z2.0) * w = z1.0 * w + z2.0 * w"*)
885 \<^rule_thm>\<open>distrib_left\<close>,
886 (*"w * (z1.0 + z2.0) = w * z1.0 + w * z2.0"*)
887 (*Rule.Thm ("distrib_right1",ThmC.numerals_to_Free @{thm distrib_right}1),
888 ....... 18.3.03 undefined???*)
890 \<^rule_thm>\<open>real_plus_binom_pow2\<close>,
891 (*"(a + b) \<up> 2 = a \<up> 2 + 2*a*b + b \<up> 2"*)
892 \<^rule_thm>\<open>real_minus_binom_pow2_p\<close>,
893 (*"(a - b) \<up> 2 = a \<up> 2 + -2*a*b + b \<up> 2"*)
894 \<^rule_thm>\<open>real_plus_minus_binom1_p\<close>,
895 (*"(a + b)*(a - b) = a \<up> 2 + -1*b \<up> 2"*)
896 \<^rule_thm>\<open>real_plus_minus_binom2_p\<close>,
897 (*"(a - b)*(a + b) = a \<up> 2 + -1*b \<up> 2"*)
899 \<^rule_thm>\<open>minus_minus\<close>,
901 \<^rule_thm>\<open>real_diff_minus\<close>,
902 (*"a - b = a + -1 * b"*)
903 \<^rule_thm_sym>\<open>real_mult_minus1\<close>
906 (*\<^rule_thm>\<open>real_minus_add_distrib\<close>,*)
907 (*"- (?x + ?y) = - ?x + - ?y"*)
908 (*\<^rule_thm>\<open>real_diff_plus\<close>*)
910 ], scr = Rule.Empty_Prog};
913 Rule_Def.Repeat{id = "simplify_power", preconds = [],
914 rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
915 erls = Rule_Set.empty, srls = Rule_Set.Empty,
916 calc = [], errpatts = [],
917 rules = [\<^rule_thm>\<open>realpow_multI\<close>,
918 (*"(r * s) \<up> n = r \<up> n * s \<up> n"*)
920 \<^rule_thm_sym>\<open>realpow_twoI\<close>,
921 (*"r1 * r1 = r1 \<up> 2"*)
922 \<^rule_thm>\<open>realpow_plus_1\<close>,
923 (*"r * r \<up> n = r \<up> (n + 1)"*)
924 \<^rule_thm>\<open>realpow_pow\<close>,
925 (*"(a \<up> b) \<up> c = a \<up> (b * c)"*)
926 \<^rule_thm_sym>\<open>realpow_addI\<close>,
927 (*"r \<up> n * r \<up> m = r \<up> (n + m)"*)
928 \<^rule_thm>\<open>realpow_oneI\<close>,
930 \<^rule_thm>\<open>realpow_eq_oneI\<close>
932 ], scr = Rule.Empty_Prog};
934 val collect_numerals =
935 Rule_Def.Repeat{id = "collect_numerals", preconds = [],
936 rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
937 erls = Atools_erls(*erls3.4.03*),srls = Rule_Set.Empty,
938 calc = [("PLUS" , ("Groups.plus_class.plus", eval_binop "#add_")),
939 ("TIMES" , ("Groups.times_class.times", eval_binop "#mult_")),
940 ("POWER", ("Transcendental.powr", eval_binop "#power_"))
942 rules = [\<^rule_thm>\<open>real_num_collect\<close>,
943 (*"[| l is_const; m is_const |]==>l * n + m * n = (l + m) * n"*)
944 \<^rule_thm>\<open>real_num_collect_assoc\<close>,
945 (*"[| l is_const; m is_const |] ==>
946 l * n + (m * n + k) = (l + m) * n + k"*)
947 \<^rule_thm>\<open>real_one_collect\<close>,
948 (*"m is_const ==> n + m * n = (1 + m) * n"*)
949 \<^rule_thm>\<open>real_one_collect_assoc\<close>,
950 (*"m is_const ==> k + (n + m * n) = k + (1 + m) * n"*)
951 \<^rule_eval>\<open>plus\<close> (eval_binop "#add_"),
952 \<^rule_eval>\<open>times\<close> (eval_binop "#mult_"),
953 \<^rule_eval>\<open>powr\<close> (eval_binop "#power_")
954 ], scr = Rule.Empty_Prog};
956 Rule_Def.Repeat{id = "reduce_012", preconds = [],
957 rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
958 erls = Rule_Set.empty,srls = Rule_Set.Empty,
959 calc = [], errpatts = [],
960 rules = [\<^rule_thm>\<open>mult_1_left\<close>,
962 (*\<^rule_thm>\<open>real_mult_minus1\<close>,14.3.03*)
964 Rule.Thm ("minus_mult_left", ThmC.numerals_to_Free (@{thm minus_mult_left} RS @{thm sym})),
965 (*- (?x * ?y) = "- ?x * ?y"*)
966 (*\<^rule_thm>\<open>real_minus_mult_cancel\<close>,
967 (*"- ?x * - ?y = ?x * ?y"*)---*)
968 \<^rule_thm>\<open>mult_zero_left\<close>,
970 \<^rule_thm>\<open>add_0_left\<close>,
972 \<^rule_thm>\<open>right_minus\<close>,
974 \<^rule_thm_sym>\<open>real_mult_2\<close>,
975 (*"z1 + z1 = 2 * z1"*)
976 \<^rule_thm>\<open>real_mult_2_assoc\<close>
977 (*"z1 + (z1 + k) = 2 * z1 + k"*)
978 ], scr = Rule.Empty_Prog};
980 val discard_parentheses =
981 Rule_Set.append_rules "discard_parentheses" Rule_Set.empty
982 [\<^rule_thm_sym>\<open>mult.assoc\<close>, \<^rule_thm_sym>\<open>add.assoc\<close>];
985 subsubsection \<open>hard-coded AC rewriting\<close>
987 (*MG.0401: termorders for multivariate polys dropped due to principal problems:
988 (total-degree-)ordering of monoms NOT possible with size_of_term GIVEN*)
990 Rule_Def.Repeat{id = "order_add_mult", preconds = [],
991 rew_ord = ("ord_make_polynomial",ord_make_polynomial false \<^theory>),
992 erls = Rule_Set.empty,srls = Rule_Set.Empty,
993 calc = [], errpatts = [],
994 rules = [\<^rule_thm>\<open>mult.commute\<close>,
996 \<^rule_thm>\<open>real_mult_left_commute\<close>,
997 (*z1.0 * (z2.0 * z3.0) = z2.0 * (z1.0 * z3.0)*)
998 \<^rule_thm>\<open>mult.assoc\<close>,
999 (*z1.0 * z2.0 * z3.0 = z1.0 * (z2.0 * z3.0)*)
1000 \<^rule_thm>\<open>add.commute\<close>,
1002 \<^rule_thm>\<open>add.left_commute\<close>,
1003 (*x + (y + z) = y + (x + z)*)
1004 \<^rule_thm>\<open>add.assoc\<close>
1005 (*z1.0 + z2.0 + z3.0 = z1.0 + (z2.0 + z3.0)*)
1006 ], scr = Rule.Empty_Prog};
1007 (*MG.0401: termorders for multivariate polys dropped due to principal problems:
1008 (total-degree-)ordering of monoms NOT possible with size_of_term GIVEN*)
1010 Rule_Def.Repeat{id = "order_mult", preconds = [],
1011 rew_ord = ("ord_make_polynomial",ord_make_polynomial false \<^theory>),
1012 erls = Rule_Set.empty,srls = Rule_Set.Empty,
1013 calc = [], errpatts = [],
1014 rules = [\<^rule_thm>\<open>mult.commute\<close>,
1016 \<^rule_thm>\<open>real_mult_left_commute\<close>,
1017 (*z1.0 * (z2.0 * z3.0) = z2.0 * (z1.0 * z3.0)*)
1018 \<^rule_thm>\<open>mult.assoc\<close>
1019 (*z1.0 * z2.0 * z3.0 = z1.0 * (z2.0 * z3.0)*)
1020 ], scr = Rule.Empty_Prog};
1023 fun attach_form (_: Rule.rule list list) (_: term) (_: term) = (*still missing*)
1024 []:(Rule.rule * (term * term list)) list;
1025 fun init_state (_: term) = Rule_Set.e_rrlsstate;
1026 fun locate_rule (_: Rule.rule list list) (_: term) (_: Rule.rule) =
1027 ([]:(Rule.rule * (term * term list)) list);
1028 fun next_rule (_: Rule.rule list list) (_: term) = (NONE: Rule.rule option);
1029 fun normal_form t = SOME (sort_variables t, []: term list);
1032 Rule_Set.Rrls {id = "order_mult_",
1034 (* ?p matched with the current term gives an environment,
1035 which evaluates (the instantiated) "?p is_multUnordered" to true *)
1036 [([TermC.parse_patt \<^theory> "?p is_multUnordered"],
1037 TermC.parse_patt \<^theory> "?p :: real")],
1038 rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
1039 erls = Rule_Set.append_rules "Rule_Set.empty-is_multUnordered" Rule_Set.empty
1040 [\<^rule_eval>\<open>is_multUnordered\<close> (eval_is_multUnordered "")],
1041 calc = [("PLUS" , ("Groups.plus_class.plus", eval_binop "#add_")),
1042 ("TIMES" , ("Groups.times_class.times", eval_binop "#mult_")),
1043 ("DIVIDE", ("Rings.divide_class.divide", Prog_Expr.eval_cancel "#divide_e")),
1044 ("POWER" , ("Transcendental.powr", eval_binop "#power_"))],
1046 scr = Rule.Rfuns {init_state = init_state,
1047 normal_form = normal_form,
1048 locate_rule = locate_rule,
1049 next_rule = next_rule,
1050 attach_form = attach_form}};
1051 val order_mult_rls_ =
1052 Rule_Def.Repeat {id = "order_mult_rls_", preconds = [],
1053 rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
1054 erls = Rule_Set.empty,srls = Rule_Set.Empty,
1055 calc = [], errpatts = [],
1056 rules = [Rule.Rls_ order_mult_
1057 ], scr = Rule.Empty_Prog};
1061 fun attach_form (_: Rule.rule list list) (_: term) (_: term) = (*still missing*)
1062 []: (Rule.rule * (term * term list)) list;
1063 fun init_state (_: term) = Rule_Set.e_rrlsstate;
1064 fun locate_rule (_: Rule.rule list list) (_: term) (_: Rule.rule) =
1065 ([]: (Rule.rule * (term * term list)) list);
1066 fun next_rule (_: Rule.rule list list) (_: term) = (NONE: Rule.rule option);
1067 fun normal_form t = SOME (sort_monoms t,[]: term list);
1070 Rule_Set.Rrls {id = "order_add_",
1071 prepat = (*WN.18.6.03 Preconditions und Pattern,
1072 die beide passen muessen, damit das Rule_Set.Rrls angewandt wird*)
1073 [([TermC.parse_patt @{theory} "?p is_addUnordered"],
1074 TermC.parse_patt @{theory} "?p :: real"
1075 (*WN.18.6.03 also KEIN pattern, dieses erzeugt nur das Environment
1076 fuer die Evaluation der Precondition "p is_addUnordered"*))],
1077 rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
1078 erls = Rule_Set.append_rules "Rule_Set.empty-is_addUnordered" Rule_Set.empty(*MG: poly_erls*)
1079 [\<^rule_eval>\<open>is_addUnordered\<close> (eval_is_addUnordered "")],
1080 calc = [("PLUS" ,("Groups.plus_class.plus", eval_binop "#add_")),
1081 ("TIMES" ,("Groups.times_class.times", eval_binop "#mult_")),
1082 ("DIVIDE",("Rings.divide_class.divide", Prog_Expr.eval_cancel "#divide_e")),
1083 ("POWER" ,("Transcendental.powr" , eval_binop "#power_"))],
1085 scr = Rule.Rfuns {init_state = init_state,
1086 normal_form = normal_form,
1087 locate_rule = locate_rule,
1088 next_rule = next_rule,
1089 attach_form = attach_form}};
1091 val order_add_rls_ =
1092 Rule_Def.Repeat {id = "order_add_rls_", preconds = [],
1093 rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
1094 erls = Rule_Set.empty,srls = Rule_Set.Empty,
1095 calc = [], errpatts = [],
1096 rules = [Rule.Rls_ order_add_
1097 ], scr = Rule.Empty_Prog};
1100 text \<open>rule-set make_polynomial also named norm_Poly:
1101 Rewrite order has not been implemented properly; the order is better in
1102 make_polynomial_in (coded in SML).
1103 Notes on state of development:
1104 \# surprise 2006: test --- norm_Poly NOT COMPLETE ---
1105 \# migration Isabelle2002 --> 2011 weakened the rule set, see test
1106 --- Matthias Goldgruber 2003 rewrite orders ---, raise ERROR "ord_make_polynomial_in #16b"
1109 (*. see MG-DA.p.52ff .*)
1110 val make_polynomial(*MG.03, overwrites version from above,
1111 previously 'make_polynomial_'*) =
1112 Rule_Set.Sequence {id = "make_polynomial", preconds = []:term list,
1113 rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
1114 erls = Atools_erls, srls = Rule_Set.Empty,calc = [], errpatts = [],
1115 rules = [Rule.Rls_ discard_minus,
1116 Rule.Rls_ expand_poly_,
1117 \<^rule_eval>\<open>times\<close> (eval_binop "#mult_"),
1118 Rule.Rls_ order_mult_rls_,
1119 Rule.Rls_ simplify_power_,
1120 Rule.Rls_ calc_add_mult_pow_,
1121 Rule.Rls_ reduce_012_mult_,
1122 Rule.Rls_ order_add_rls_,
1123 Rule.Rls_ collect_numerals_,
1124 Rule.Rls_ reduce_012_,
1125 Rule.Rls_ discard_parentheses1
1127 scr = Rule.Empty_Prog
1131 val norm_Poly(*=make_polynomial*) =
1132 Rule_Set.Sequence {id = "norm_Poly", preconds = []:term list,
1133 rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
1134 erls = Atools_erls, srls = Rule_Set.Empty, calc = [], errpatts = [],
1135 rules = [Rule.Rls_ discard_minus,
1136 Rule.Rls_ expand_poly_,
1137 \<^rule_eval>\<open>times\<close> (eval_binop "#mult_"),
1138 Rule.Rls_ order_mult_rls_,
1139 Rule.Rls_ simplify_power_,
1140 Rule.Rls_ calc_add_mult_pow_,
1141 Rule.Rls_ reduce_012_mult_,
1142 Rule.Rls_ order_add_rls_,
1143 Rule.Rls_ collect_numerals_,
1144 Rule.Rls_ reduce_012_,
1145 Rule.Rls_ discard_parentheses1
1147 scr = Rule.Empty_Prog
1151 (* MG:03 Like make_polynomial_ but without Rule.Rls_ discard_parentheses1
1152 and expand_poly_rat_ instead of expand_poly_, see MG-DA.p.56ff*)
1153 (* MG necessary for termination of norm_Rational(*_mg*) in Rational.ML*)
1154 val make_rat_poly_with_parentheses =
1155 Rule_Set.Sequence{id = "make_rat_poly_with_parentheses", preconds = []:term list,
1156 rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
1157 erls = Atools_erls, srls = Rule_Set.Empty, calc = [], errpatts = [],
1158 rules = [Rule.Rls_ discard_minus,
1159 Rule.Rls_ expand_poly_rat_,(*ignors rationals*)
1160 \<^rule_eval>\<open>times\<close> (eval_binop "#mult_"),
1161 Rule.Rls_ order_mult_rls_,
1162 Rule.Rls_ simplify_power_,
1163 Rule.Rls_ calc_add_mult_pow_,
1164 Rule.Rls_ reduce_012_mult_,
1165 Rule.Rls_ order_add_rls_,
1166 Rule.Rls_ collect_numerals_,
1167 Rule.Rls_ reduce_012_
1168 (*Rule.Rls_ discard_parentheses1 *)
1170 scr = Rule.Empty_Prog
1174 (*.a minimal ruleset for reverse rewriting of factions [2];
1175 compare expand_binoms.*)
1177 Rule_Set.Sequence{id = "rev_rew_p", preconds = [], rew_ord = ("termlessI",termlessI),
1178 erls = Atools_erls, srls = Rule_Set.Empty,
1179 calc = [(*("PLUS" , ("Groups.plus_class.plus", eval_binop "#add_")),
1180 ("TIMES" , ("Groups.times_class.times", eval_binop "#mult_")),
1181 ("POWER", ("Transcendental.powr", eval_binop "#power_"))*)
1183 rules = [\<^rule_thm>\<open>real_plus_binom_times\<close>,
1184 (*"(a + b)*(a + b) = a ^ 2 + 2 * a * b + b ^ 2*)
1185 \<^rule_thm>\<open>real_plus_binom_times1\<close>,
1186 (*"(a + 1*b)*(a + -1*b) = a \<up> 2 + -1*b \<up> 2"*)
1187 \<^rule_thm>\<open>real_plus_binom_times2\<close>,
1188 (*"(a + -1*b)*(a + 1*b) = a \<up> 2 + -1*b \<up> 2"*)
1190 \<^rule_thm>\<open>mult_1_left\<close>,(*"1 * z = z"*)
1192 \<^rule_thm>\<open>distrib_right\<close>,
1193 (*"(z1.0 + z2.0) * w = z1.0 * w + z2.0 * w"*)
1194 \<^rule_thm>\<open>distrib_left\<close>,
1195 (*"w * (z1.0 + z2.0) = w * z1.0 + w * z2.0"*)
1197 \<^rule_thm>\<open>mult.assoc\<close>,
1198 (*"?z1.1 * ?z2.1 * ?z3. =1 ?z1.1 * (?z2.1 * ?z3.1)"*)
1199 Rule.Rls_ order_mult_rls_,
1200 (*Rule.Rls_ order_add_rls_,*)
1202 \<^rule_eval>\<open>plus\<close> (eval_binop "#add_"),
1203 \<^rule_eval>\<open>times\<close> (eval_binop "#mult_"),
1204 \<^rule_eval>\<open>powr\<close> (eval_binop "#power_"),
1206 \<^rule_thm_sym>\<open>realpow_twoI\<close>,
1207 (*"r1 * r1 = r1 \<up> 2"*)
1208 \<^rule_thm_sym>\<open>real_mult_2\<close>,
1209 (*"z1 + z1 = 2 * z1"*)
1210 \<^rule_thm>\<open>real_mult_2_assoc\<close>,
1211 (*"z1 + (z1 + k) = 2 * z1 + k"*)
1213 \<^rule_thm>\<open>real_num_collect\<close>,
1214 (*"[| l is_const; m is_const |]==>l * n + m * n = (l + m) * n"*)
1215 \<^rule_thm>\<open>real_num_collect_assoc\<close>,
1216 (*"[| l is_const; m is_const |] ==>
1217 l * n + (m * n + k) = (l + m) * n + k"*)
1218 \<^rule_thm>\<open>real_one_collect\<close>,
1219 (*"m is_const ==> n + m * n = (1 + m) * n"*)
1220 \<^rule_thm>\<open>real_one_collect_assoc\<close>,
1221 (*"m is_const ==> k + (n + m * n) = k + (1 + m) * n"*)
1223 \<^rule_thm>\<open>realpow_multI\<close>,
1224 (*"(r * s) \<up> n = r \<up> n * s \<up> n"*)
1226 \<^rule_eval>\<open>plus\<close> (eval_binop "#add_"),
1227 \<^rule_eval>\<open>times\<close> (eval_binop "#mult_"),
1228 \<^rule_eval>\<open>powr\<close> (eval_binop "#power_"),
1230 \<^rule_thm>\<open>mult_1_left\<close>,(*"1 * z = z"*)
1231 \<^rule_thm>\<open>mult_zero_left\<close>,(*"0 * z = 0"*)
1232 \<^rule_thm>\<open>add_0_left\<close>(*0 + z = z*)
1234 (*Rule.Rls_ order_add_rls_*)
1237 scr = Rule.Empty_Prog};
1240 subsection \<open>rule-sets with explicit program for intermediate steps\<close>
1241 partial_function (tailrec) expand_binoms_2 :: "real \<Rightarrow> real"
1243 "expand_binoms_2 term = (
1245 (Try (Repeat (Rewrite ''real_plus_binom_pow2''))) #>
1246 (Try (Repeat (Rewrite ''real_plus_binom_times''))) #>
1247 (Try (Repeat (Rewrite ''real_minus_binom_pow2''))) #>
1248 (Try (Repeat (Rewrite ''real_minus_binom_times''))) #>
1249 (Try (Repeat (Rewrite ''real_plus_minus_binom1''))) #>
1250 (Try (Repeat (Rewrite ''real_plus_minus_binom2''))) #>
1252 (Try (Repeat (Rewrite ''mult_1_left''))) #>
1253 (Try (Repeat (Rewrite ''mult_zero_left''))) #>
1254 (Try (Repeat (Rewrite ''add_0_left''))) #>
1256 (Try (Repeat (Calculate ''PLUS''))) #>
1257 (Try (Repeat (Calculate ''TIMES''))) #>
1258 (Try (Repeat (Calculate ''POWER''))) #>
1260 (Try (Repeat (Rewrite ''sym_realpow_twoI''))) #>
1261 (Try (Repeat (Rewrite ''realpow_plus_1''))) #>
1262 (Try (Repeat (Rewrite ''sym_real_mult_2''))) #>
1263 (Try (Repeat (Rewrite ''real_mult_2_assoc''))) #>
1265 (Try (Repeat (Rewrite ''real_num_collect''))) #>
1266 (Try (Repeat (Rewrite ''real_num_collect_assoc''))) #>
1268 (Try (Repeat (Rewrite ''real_one_collect''))) #>
1269 (Try (Repeat (Rewrite ''real_one_collect_assoc''))) #>
1271 (Try (Repeat (Calculate ''PLUS''))) #>
1272 (Try (Repeat (Calculate ''TIMES''))) #>
1273 (Try (Repeat (Calculate ''POWER''))))
1277 Rule_Def.Repeat{id = "expand_binoms", preconds = [], rew_ord = ("termlessI",termlessI),
1278 erls = Atools_erls, srls = Rule_Set.Empty,
1279 calc = [("PLUS" , ("Groups.plus_class.plus", eval_binop "#add_")),
1280 ("TIMES" , ("Groups.times_class.times", eval_binop "#mult_")),
1281 ("POWER", ("Transcendental.powr", eval_binop "#power_"))
1283 rules = [\<^rule_thm>\<open>real_plus_binom_pow2\<close>,
1284 (*"(a + b) \<up> 2 = a \<up> 2 + 2 * a * b + b \<up> 2"*)
1285 \<^rule_thm>\<open>real_plus_binom_times\<close>,
1286 (*"(a + b)*(a + b) = ...*)
1287 \<^rule_thm>\<open>real_minus_binom_pow2\<close>,
1288 (*"(a - b) \<up> 2 = a \<up> 2 - 2 * a * b + b \<up> 2"*)
1289 \<^rule_thm>\<open>real_minus_binom_times\<close>,
1290 (*"(a - b)*(a - b) = ...*)
1291 \<^rule_thm>\<open>real_plus_minus_binom1\<close>,
1292 (*"(a + b) * (a - b) = a \<up> 2 - b \<up> 2"*)
1293 \<^rule_thm>\<open>real_plus_minus_binom2\<close>,
1294 (*"(a - b) * (a + b) = a \<up> 2 - b \<up> 2"*)
1296 \<^rule_thm>\<open>real_pp_binom_times\<close>,
1297 (*(a + b)*(c + d) = a*c + a*d + b*c + b*d*)
1298 \<^rule_thm>\<open>real_pm_binom_times\<close>,
1299 (*(a + b)*(c - d) = a*c - a*d + b*c - b*d*)
1300 \<^rule_thm>\<open>real_mp_binom_times\<close>,
1301 (*(a - b)*(c + d) = a*c + a*d - b*c - b*d*)
1302 \<^rule_thm>\<open>real_mm_binom_times\<close>,
1303 (*(a - b)*(c - d) = a*c - a*d - b*c + b*d*)
1304 \<^rule_thm>\<open>realpow_multI\<close>,
1305 (*(a*b) \<up> n = a \<up> n * b \<up> n*)
1306 \<^rule_thm>\<open>real_plus_binom_pow3\<close>,
1307 (* (a + b) \<up> 3 = a \<up> 3 + 3*a \<up> 2*b + 3*a*b \<up> 2 + b \<up> 3 *)
1308 \<^rule_thm>\<open>real_minus_binom_pow3\<close>,
1309 (* (a - b) \<up> 3 = a \<up> 3 - 3*a \<up> 2*b + 3*a*b \<up> 2 - b \<up> 3 *)
1312 (*\<^rule_thm>\<open>distrib_right\<close>,
1313 (*"(z1.0 + z2.0) * w = z1.0 * w + z2.0 * w"*)
1314 \<^rule_thm>\<open>distrib_left\<close>,
1315 (*"w * (z1.0 + z2.0) = w * z1.0 + w * z2.0"*)
1316 \<^rule_thm>\<open>left_diff_distrib\<close>,
1317 (*"(z1.0 - z2.0) * w = z1.0 * w - z2.0 * w"*)
1318 \<^rule_thm>\<open>right_diff_distrib\<close>,
1319 (*"w * (z1.0 - z2.0) = w * z1.0 - w * z2.0"*)
1321 \<^rule_thm>\<open>mult_1_left\<close>,
1323 \<^rule_thm>\<open>mult_zero_left\<close>,
1325 \<^rule_thm>\<open>add_0_left\<close>,(*"0 + z = z"*)
1327 \<^rule_eval>\<open>plus\<close> (eval_binop "#add_"),
1328 \<^rule_eval>\<open>times\<close> (eval_binop "#mult_"),
1329 \<^rule_eval>\<open>powr\<close> (eval_binop "#power_"),
1330 (*\<^rule_thm>\<open>mult.commute\<close>,
1332 \<^rule_thm>\<open>real_mult_left_commute\<close>,
1333 \<^rule_thm>\<open>mult.assoc\<close>,
1334 \<^rule_thm>\<open>add.commute\<close>,
1335 \<^rule_thm>\<open>add.left_commute\<close>,
1336 \<^rule_thm>\<open>add.assoc\<close>,
1338 \<^rule_thm_sym>\<open>realpow_twoI\<close>,
1339 (*"r1 * r1 = r1 \<up> 2"*)
1340 \<^rule_thm>\<open>realpow_plus_1\<close>,
1341 (*"r * r \<up> n = r \<up> (n + 1)"*)
1342 (*\<^rule_thm_sym>\<open>real_mult_2\<close>,
1343 (*"z1 + z1 = 2 * z1"*)*)
1344 \<^rule_thm>\<open>real_mult_2_assoc\<close>,
1345 (*"z1 + (z1 + k) = 2 * z1 + k"*)
1347 \<^rule_thm>\<open>real_num_collect\<close>,
1348 (*"[| l is_const; m is_const |] ==>l * n + m * n = (l + m) * n"*)
1349 \<^rule_thm>\<open>real_num_collect_assoc\<close>,
1350 (*"[| l is_const; m is_const |] ==>
1351 l * n + (m * n + k) = (l + m) * n + k"*)
1352 \<^rule_thm>\<open>real_one_collect\<close>,
1353 (*"m is_const ==> n + m * n = (1 + m) * n"*)
1354 \<^rule_thm>\<open>real_one_collect_assoc\<close>,
1355 (*"m is_const ==> k + (n + m * n) = k + (1 + m) * n"*)
1357 \<^rule_eval>\<open>plus\<close> (eval_binop "#add_"),
1358 \<^rule_eval>\<open>times\<close> (eval_binop "#mult_"),
1359 \<^rule_eval>\<open>powr\<close> (eval_binop "#power_")
1361 scr = Rule.Prog (Program.prep_program @{thm expand_binoms_2.simps})
1365 subsection \<open>add to Know_Store\<close>
1366 subsubsection \<open>rule-sets\<close>
1367 ML \<open>val prep_rls' = Auto_Prog.prep_rls @{theory}\<close>
1370 norm_Poly = \<open>prep_rls' norm_Poly\<close> and
1371 Poly_erls = \<open>prep_rls' Poly_erls\<close> (*FIXXXME:del with rls.rls'*) and
1372 expand = \<open>prep_rls' expand\<close> and
1373 expand_poly = \<open>prep_rls' expand_poly\<close> and
1374 simplify_power = \<open>prep_rls' simplify_power\<close> and
1376 order_add_mult = \<open>prep_rls' order_add_mult\<close> and
1377 collect_numerals = \<open>prep_rls' collect_numerals\<close> and
1378 collect_numerals_= \<open>prep_rls' collect_numerals_\<close> and
1379 reduce_012 = \<open>prep_rls' reduce_012\<close> and
1380 discard_parentheses = \<open>prep_rls' discard_parentheses\<close> and
1382 make_polynomial = \<open>prep_rls' make_polynomial\<close> and
1383 expand_binoms = \<open>prep_rls' expand_binoms\<close> and
1384 rev_rew_p = \<open>prep_rls' rev_rew_p\<close> and
1385 discard_minus = \<open>prep_rls' discard_minus\<close> and
1386 expand_poly_ = \<open>prep_rls' expand_poly_\<close> and
1388 expand_poly_rat_ = \<open>prep_rls' expand_poly_rat_\<close> and
1389 simplify_power_ = \<open>prep_rls' simplify_power_\<close> and
1390 calc_add_mult_pow_ = \<open>prep_rls' calc_add_mult_pow_\<close> and
1391 reduce_012_mult_ = \<open>prep_rls' reduce_012_mult_\<close> and
1392 reduce_012_ = \<open>prep_rls' reduce_012_\<close> and
1394 discard_parentheses1 = \<open>prep_rls' discard_parentheses1\<close> and
1395 order_mult_rls_ = \<open>prep_rls' order_mult_rls_\<close> and
1396 order_add_rls_ = \<open>prep_rls' order_add_rls_\<close> and
1397 make_rat_poly_with_parentheses = \<open>prep_rls' make_rat_poly_with_parentheses\<close>
1399 subsection \<open>problems\<close>
1400 setup \<open>KEStore_Elems.add_pbts
1401 [(Problem.prep_input @{theory} "pbl_simp_poly" [] Problem.id_empty
1402 (["polynomial", "simplification"],
1403 [("#Given" ,["Term t_t"]),
1404 ("#Where" ,["t_t is_polyexp"]),
1405 ("#Find" ,["normalform n_n"])],
1406 Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)
1407 \<^rule_eval>\<open>is_polyexp\<close> (eval_is_polyexp "")],
1408 SOME "Simplify t_t",
1409 [["simplification", "for_polynomials"]]))]\<close>
1411 subsection \<open>methods\<close>
1413 partial_function (tailrec) simplify :: "real \<Rightarrow> real"
1415 "simplify term = ((Rewrite_Set ''norm_Poly'') term)"
1416 setup \<open>KEStore_Elems.add_mets
1417 [MethodC.prep_input @{theory} "met_simp_poly" [] MethodC.id_empty
1418 (["simplification", "for_polynomials"],
1419 [("#Given" ,["Term t_t"]),
1420 ("#Where" ,["t_t is_polyexp"]),
1421 ("#Find" ,["normalform n_n"])],
1422 {rew_ord'="tless_true", rls' = Rule_Set.empty, calc = [], srls = Rule_Set.empty,
1423 prls = Rule_Set.append_rules "simplification_for_polynomials_prls" Rule_Set.empty
1424 [(*for preds in where_*)
1425 \<^rule_eval>\<open>is_polyexp\<close> (eval_is_polyexp"")],
1426 crls = Rule_Set.empty, errpats = [], nrls = norm_Poly},
1427 @{thm simplify.simps})]