1 (* theory collecting all knowledge for Root
9 theory Root imports Poly begin
13 (*sqrt :: "real => real" Isabelle "NthRoot.sqrt"*)
14 nroot :: "[real, real] => real"
16 axioms (*.not contained in Isabelle2002,
17 stated as axioms, TODO: prove as theorems;
18 theorem-IDs 'xxxI' with ^^^ instead of ^ in 'xxx' in Isabelle2002.*)
20 root_plus_minus: "0 <= b ==>
21 (a^^^2 = b) = ((a = sqrt b) | (a = (-1)*sqrt b))"
22 root_false: "b < 0 ==> (a^^^2 = b) = False"
24 (* for expand_rootbinom *)
25 real_pp_binom_times: "(a + b)*(c + d) = a*c + a*d + b*c + b*d"
26 real_pm_binom_times: "(a + b)*(c - d) = a*c - a*d + b*c - b*d"
27 real_mp_binom_times: "(a - b)*(c + d) = a*c + a*d - b*c - b*d"
28 real_mm_binom_times: "(a - b)*(c - d) = a*c - a*d - b*c + b*d"
29 real_plus_binom_pow3: "(a + b)^^^3 = a^^^3 + 3*a^^^2*b + 3*a*b^^^2 + b^^^3"
30 real_minus_binom_pow3: "(a - b)^^^3 = a^^^3 - 3*a^^^2*b + 3*a*b^^^2 - b^^^3"
31 realpow_mul: "(a*b)^^^n = a^^^n * b^^^n"
33 real_diff_minus: "a - b = a + (-1) * b"
34 real_plus_binom_times: "(a + b)*(a + b) = a^^^2 + 2*a*b + b^^^2"
35 real_minus_binom_times: "(a - b)*(a - b) = a^^^2 - 2*a*b + b^^^2"
36 real_plus_binom_pow2: "(a + b)^^^2 = a^^^2 + 2*a*b + b^^^2"
37 real_minus_binom_pow2: "(a - b)^^^2 = a^^^2 - 2*a*b + b^^^2"
38 real_plus_minus_binom1: "(a + b)*(a - b) = a^^^2 - b^^^2"
39 real_plus_minus_binom2: "(a - b)*(a + b) = a^^^2 - b^^^2"
41 real_root_positive: "0 <= a ==> (x ^^^ 2 = a) = (x = sqrt a)"
42 real_root_negative: "a < 0 ==> (x ^^^ 2 = a) = False"
47 (*-------------------------functions---------------------*)
48 (*evaluation square-root over the integers*)
49 fun eval_sqrt (thmid:string) (op_:string) (t as
50 (Const(op0,t0) $ arg)) thy =
53 (case int_of_str n1 of
57 let val fact = squfact ni;
59 then SOME ("#sqrt #"^(string_of_int ni)^" = #"
60 ^(string_of_int (if ni = 0 then 0
62 Trueprop $ mk_equality (t, term_of_num t1 fact))
63 else if fact = 1 then NONE
64 else SOME ("#sqrt #"^(string_of_int ni)^" = sqrt (#"
65 ^(string_of_int fact)^" * #"
66 ^(string_of_int fact)^" * #"
67 ^(string_of_int (ni div (fact*fact))^")"),
71 (mk_factroot op0 t1 fact
72 (ni div (fact*fact))))))
77 | eval_sqrt _ _ _ _ = NONE;
78 (*val (thmid, op_, t as Const(op0,t0) $ arg) = ("","", str2term "sqrt 0");
79 > eval_sqrt thmid op_ t thy;
80 > val Free (n1,t1) = arg;
81 > val SOME ni = int_of_str n1;
84 calclist':= overwritel (!calclist',
85 [("SQRT" ,("NthRoot.sqrt" ,eval_sqrt "#sqrt_"))
86 (*different types for 'sqrt 4' --- 'Calculate SQRT'*)
90 local (* Vers. 7.10.99.A *)
92 open Term; (* for type order = EQUAL | LESS | GREATER *)
94 fun pr_ord EQUAL = "EQUAL"
95 | pr_ord LESS = "LESS"
96 | pr_ord GREATER = "GREATER";
98 fun dest_hd' (Const (a, T)) = (* ~ term.ML *)
99 (case a of "NthRoot.sqrt" => ((("|||", 0), T), 0) (*WN greatest *)
100 | _ => (((a, 0), T), 0))
101 | dest_hd' (Free (a, T)) = (((a, 0), T), 1)
102 | dest_hd' (Var v) = (v, 2)
103 | dest_hd' (Bound i) = ((("", i), dummyT), 3)
104 | dest_hd' (Abs (_, T, _)) = ((("", 0), T), 4);
105 fun size_of_term' (Const(str,_) $ t) =
106 (case str of "NthRoot.sqrt" => (1000 + size_of_term' t)
107 | _ => 1 + size_of_term' t)
108 | size_of_term' (Abs (_,_,body)) = 1 + size_of_term' body
109 | size_of_term' (f $ t) = size_of_term' f + size_of_term' t
110 | size_of_term' _ = 1;
111 fun term_ord' pr thy (Abs (_, T, t), Abs(_, U, u)) = (* ~ term.ML *)
112 (case term_ord' pr thy (t, u) of EQUAL => Term_Ord.typ_ord (T, U) | ord => ord)
113 | term_ord' pr thy (t, u) =
116 val (f, ts) = strip_comb t and (g, us) = strip_comb u;
117 val _=writeln("t= f@ts= \""^
118 ((Syntax.string_of_term (thy2ctxt thy)) f)^"\" @ \"["^
119 (commas(map(Syntax.string_of_term (thy2ctxt thy)) ts))^"]\"");
120 val _=writeln("u= g@us= \""^
121 ((Syntax.string_of_term (thy2ctxt thy)) g)^"\" @ \"["^
122 (commas(map(Syntax.string_of_term (thy2ctxt thy)) us))^"]\"");
123 val _=writeln("size_of_term(t,u)= ("^
124 (string_of_int(size_of_term' t))^", "^
125 (string_of_int(size_of_term' u))^")");
126 val _=writeln("hd_ord(f,g) = "^((pr_ord o hd_ord)(f,g)));
127 val _=writeln("terms_ord(ts,us) = "^
128 ((pr_ord o terms_ord str false)(ts,us)));
129 val _=writeln("-------");
132 case int_ord (size_of_term' t, size_of_term' u) of
134 let val (f, ts) = strip_comb t and (g, us) = strip_comb u in
135 (case hd_ord (f, g) of EQUAL => (terms_ord str pr) (ts, us)
139 and hd_ord (f, g) = (* ~ term.ML *)
140 prod_ord (prod_ord Term_Ord.indexname_ord Term_Ord.typ_ord) int_ord
141 (dest_hd' f, dest_hd' g)
142 and terms_ord str pr (ts, us) =
143 list_ord (term_ord' pr (assoc_thy "Isac"))(ts, us);
146 (* associates a+(b+c) => (a+b)+c = a+b+c ... avoiding parentheses
147 by (1) size_of_term: less(!) to right, size_of 'sqrt (...)' = 1
148 (2) hd_ord: greater to right, 'sqrt' < numerals < variables
149 (3) terms_ord: recurs. on args, greater to right
153 pr: print trace, WN0509 'sqrt_right true' not used anymore
155 subst: no bound variables, only Root.sqrt
156 tu: the terms to compare (t1, t2) ... *)
157 fun sqrt_right (pr:bool) thy (_:subst) tu =
158 (term_ord' pr thy(***) tu = LESS );
161 rew_ord' := overwritel (!rew_ord',
162 [("termlessI", termlessI),
163 ("sqrt_right", sqrt_right false (theory "Pure"))
166 (*-------------------------rulse-------------------------*)
168 append_rls "Root_crls" Atools_erls
169 [Thm ("real_unari_minus",num_str @{thm real_unari_minus}),
170 Calc ("NthRoot.sqrt" ,eval_sqrt "#sqrt_"),
171 Calc ("HOL.divide",eval_cancel "#divide_e"),
172 Calc ("Atools.pow" ,eval_binop "#power_"),
173 Calc ("op +", eval_binop "#add_"),
174 Calc ("op -", eval_binop "#sub_"),
175 Calc ("op *", eval_binop "#mult_"),
176 Calc ("op =",eval_equal "#equal_")
180 append_rls "Root_erls" Atools_erls
181 [Thm ("real_unari_minus",num_str @{thm real_unari_minus}),
182 Calc ("NthRoot.sqrt" ,eval_sqrt "#sqrt_"),
183 Calc ("HOL.divide",eval_cancel "#divide_e"),
184 Calc ("Atools.pow" ,eval_binop "#power_"),
185 Calc ("op +", eval_binop "#add_"),
186 Calc ("op -", eval_binop "#sub_"),
187 Calc ("op *", eval_binop "#mult_"),
188 Calc ("op =",eval_equal "#equal_")
191 ruleset' := overwritelthy @{theory} (!ruleset',
192 [("Root_erls",Root_erls) (*FIXXXME:del with rls.rls'*)
195 val make_rooteq = prep_rls(
196 Rls{id = "make_rooteq", preconds = []:term list,
197 rew_ord = ("sqrt_right", sqrt_right false thy),
198 erls = Atools_erls, srls = Erls,
201 rules = [Thm ("real_diff_minus",num_str @{thm real_diff_minus}),
202 (*"a - b = a + (-1) * b"*)
204 Thm ("left_distrib" ,num_str @{thm left_distrib}),
205 (*"(z1.0 + z2.0) * w = z1.0 * w + z2.0 * w"*)
206 Thm ("right_distrib",num_str @{thm right_distrib}),
207 (*"w * (z1.0 + z2.0) = w * z1.0 + w * z2.0"*)
208 Thm ("left_diff_distrib" ,num_str @{thm left_diff_distrib}),
209 (*"(z1.0 - z2.0) * w = z1.0 * w - z2.0 * w"*)
210 Thm ("right_diff_distrib",num_str @{thm right_diff_distrib}),
211 (*"w * (z1.0 - z2.0) = w * z1.0 - w * z2.0"*)
213 Thm ("mult_1_left",num_str @{thm mult_1_left}),
215 Thm ("mult_zero_left",num_str @{thm mult_zero_left}),
217 Thm ("add_0_left",num_str @{thm add_0_left}),
220 Thm ("real_mult_commute",num_str @{thm real_mult_commute}),
222 Thm ("real_mult_left_commute",num_str @{thm real_mult_left_commute}),
224 Thm ("real_mult_assoc",num_str @{thm real_mult_assoc}),
226 Thm ("add_commute",num_str @{thm add_commute}),
228 Thm ("add_left_commute",num_str @{thm add_left_commute}),
230 Thm ("add_assoc",num_str @{thm add_assoc}),
233 Thm ("sym_realpow_twoI",
234 num_str (@{thm realpow_twoI} RS @{thm sym})),
235 (*"r1 * r1 = r1 ^^^ 2"*)
236 Thm ("realpow_plus_1",num_str @{thm realpow_plus_1}),
237 (*"r * r ^^^ n = r ^^^ (n + 1)"*)
238 Thm ("sym_real_mult_2",
239 num_str (@{thm real_mult_2} RS @{thm sym})),
240 (*"z1 + z1 = 2 * z1"*)
241 Thm ("real_mult_2_assoc",num_str @{thm real_mult_2_assoc}),
242 (*"z1 + (z1 + k) = 2 * z1 + k"*)
244 Thm ("real_num_collect",num_str @{thm real_num_collect}),
245 (*"[| l is_const; m is_const |]==> l * n + m * n = (l + m) * n"*)
246 Thm ("real_num_collect_assoc",num_str @{thm real_num_collect_assoc}),
247 (*"[| l is_const; m is_const |] ==>
248 l * n + (m * n + k) = (l + m) * n + k"*)
249 Thm ("real_one_collect",num_str @{thm real_one_collect}),
250 (*"m is_const ==> n + m * n = (1 + m) * n"*)
251 Thm ("real_one_collect_assoc",num_str @{thm real_one_collect_assoc}),
252 (*"m is_const ==> k + (n + m * n) = k + (1 + m) * n"*)
254 Calc ("op +", eval_binop "#add_"),
255 Calc ("op *", eval_binop "#mult_"),
256 Calc ("Atools.pow", eval_binop "#power_")
258 scr = Script ((term_of o the o (parse thy)) "empty_script")
260 ruleset' := overwritelthy @{theory} (!ruleset',
261 [("make_rooteq", make_rooteq)
264 val expand_rootbinoms = prep_rls(
265 Rls{id = "expand_rootbinoms", preconds = [],
266 rew_ord = ("termlessI",termlessI),
267 erls = Atools_erls, srls = Erls,
270 rules = [Thm ("real_plus_binom_pow2" ,num_str @{thm real_plus_binom_pow2}),
271 (*"(a + b) ^^^ 2 = a ^^^ 2 + 2 * a * b + b ^^^ 2"*)
272 Thm ("real_plus_binom_times" ,num_str @{thm real_plus_binom_times}),
273 (*"(a + b)*(a + b) = ...*)
274 Thm ("real_minus_binom_pow2" ,num_str @{thm real_minus_binom_pow2}),
275 (*"(a - b) ^^^ 2 = a ^^^ 2 - 2 * a * b + b ^^^ 2"*)
276 Thm ("real_minus_binom_times",num_str @{thm real_minus_binom_times}),
277 (*"(a - b)*(a - b) = ...*)
278 Thm ("real_plus_minus_binom1",num_str @{thm real_plus_minus_binom1}),
279 (*"(a + b) * (a - b) = a ^^^ 2 - b ^^^ 2"*)
280 Thm ("real_plus_minus_binom2",num_str @{thm real_plus_minus_binom2}),
281 (*"(a - b) * (a + b) = a ^^^ 2 - b ^^^ 2"*)
283 Thm ("real_pp_binom_times",num_str @{thm real_pp_binom_times}),
284 (*(a + b)*(c + d) = a*c + a*d + b*c + b*d*)
285 Thm ("real_pm_binom_times",num_str @{thm real_pm_binom_times}),
286 (*(a + b)*(c - d) = a*c - a*d + b*c - b*d*)
287 Thm ("real_mp_binom_times",num_str @{thm real_mp_binom_times}),
288 (*(a - b)*(c p d) = a*c + a*d - b*c - b*d*)
289 Thm ("real_mm_binom_times",num_str @{thm real_mm_binom_times}),
290 (*(a - b)*(c p d) = a*c - a*d - b*c + b*d*)
291 Thm ("realpow_mul",num_str @{thm realpow_mul}),
292 (*(a*b)^^^n = a^^^n * b^^^n*)
294 Thm ("mult_1_left",num_str @{thm mult_1_left}), (*"1 * z = z"*)
295 Thm ("mult_zero_left",num_str @{thm mult_zero_left}), (*"0 * z = 0"*)
296 Thm ("add_0_left",num_str @{thm add_0_left}),
299 Calc ("op +", eval_binop "#add_"),
300 Calc ("op -", eval_binop "#sub_"),
301 Calc ("op *", eval_binop "#mult_"),
302 Calc ("HOL.divide" ,eval_cancel "#divide_e"),
303 Calc ("NthRoot.sqrt",eval_sqrt "#sqrt_"),
304 Calc ("Atools.pow", eval_binop "#power_"),
306 Thm ("sym_realpow_twoI",
307 num_str (@{thm realpow_twoI} RS @{thm sym})),
308 (*"r1 * r1 = r1 ^^^ 2"*)
309 Thm ("realpow_plus_1",num_str @{thm realpow_plus_1}),
310 (*"r * r ^^^ n = r ^^^ (n + 1)"*)
311 Thm ("real_mult_2_assoc",num_str @{thm real_mult_2_assoc}),
312 (*"z1 + (z1 + k) = 2 * z1 + k"*)
314 Thm ("real_num_collect",num_str @{thm real_num_collect}),
315 (*"[| l is_const; m is_const |] ==>l * n + m * n = (l + m) * n"*)
316 Thm ("real_num_collect_assoc",num_str @{thm real_num_collect_assoc}),
317 (*"[| l is_const; m is_const |] ==>
318 l * n + (m * n + k) = (l + m) * n + k"*)
319 Thm ("real_one_collect",num_str @{thm real_one_collect}),
320 (*"m is_const ==> n + m * n = (1 + m) * n"*)
321 Thm ("real_one_collect_assoc",num_str @{thm real_one_collect_assoc}),
322 (*"m is_const ==> k + (n + m * n) = k + (1 + m) * n"*)
324 Calc ("op +", eval_binop "#add_"),
325 Calc ("op -", eval_binop "#sub_"),
326 Calc ("op *", eval_binop "#mult_"),
327 Calc ("HOL.divide" ,eval_cancel "#divide_e"),
328 Calc ("NthRoot.sqrt",eval_sqrt "#sqrt_"),
329 Calc ("Atools.pow", eval_binop "#power_")
331 scr = Script ((term_of o the o (parse thy)) "empty_script")
335 ruleset' := overwritelthy @{theory} (!ruleset',
336 [("expand_rootbinoms", expand_rootbinoms)