src/Tools/isac/Knowledge/Root.thy
author Walther Neuper <neuper@ist.tugraz.at>
Tue, 28 Sep 2010 10:10:26 +0200
branchisac-update-Isa09-2
changeset 38034 928cebc9c4aa
parent 38015 67ba02dffacc
child 38053 bb6004e10e71
permissions -rw-r--r--
updated "op *" --> Groups.times_class.times in src and test

find . -type f -exec sed -i s/"\"op \*\""/"\"Groups.times_class.times\""/g {} \;
     1 (* theory collecting all knowledge for Root
     2    created by: 
     3          date: 
     4    changed by: rlang
     5    last change by: rlang
     6              date: 02.10.21
     7 *)
     8 
     9 theory Root imports Poly begin
    10 
    11 consts
    12 
    13   (*sqrt   :: "real => real"         Isabelle "NthRoot.sqrt"*)
    14   nroot  :: "[real, real] => real"
    15 
    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.*)
    19 
    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"
    23 
    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"
    32 
    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"
    40 
    41   real_root_positive:      "0 <= a ==> (x ^^^ 2 = a) = (x = sqrt a)"
    42   real_root_negative:      "a <  0 ==> (x ^^^ 2 = a) = False"
    43 
    44 ML {*
    45 val thy = @{theory};
    46 
    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 = 
    51     (case arg of 
    52 	Free (n1,t1) =>
    53 	(case int_of_str n1 of
    54 	     SOME ni => 
    55 	     if ni < 0 then NONE
    56 	     else
    57 		 let val fact = squfact ni;
    58 		 in if fact*fact = ni 
    59 		    then SOME ("#sqrt #"^(string_of_int ni)^" = #"
    60 			       ^(string_of_int (if ni = 0 then 0
    61 						else ni div fact)),
    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))^")"),
    68 			       Trueprop $ 
    69 					(mk_equality 
    70 					     (t, 
    71 					      (mk_factroot op0 t1 fact 
    72 						(ni div (fact*fact))))))
    73 	         end
    74 	   | NONE => NONE)
    75       | _ => NONE)
    76 
    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;
    82 *)
    83 
    84 calclist':= overwritel (!calclist', 
    85    [("SQRT"    ,("NthRoot.sqrt"   ,eval_sqrt "#sqrt_"))
    86     (*different types for 'sqrt 4' --- 'Calculate SQRT'*)
    87     ]);
    88 
    89 
    90 local (* Vers. 7.10.99.A *)
    91 
    92 open Term;  (* for type order = EQUAL | LESS | GREATER *)
    93 
    94 fun pr_ord EQUAL = "EQUAL"
    95   | pr_ord LESS  = "LESS"
    96   | pr_ord GREATER = "GREATER";
    97 
    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) =
   114       (if pr then 
   115 	 let
   116 	   val (f, ts) = strip_comb t and (g, us) = strip_comb u;
   117 	   val _=tracing("t= f@ts= \""^
   118 	      ((Syntax.string_of_term (thy2ctxt thy)) f)^"\" @ \"["^
   119 	      (commas(map(Syntax.string_of_term (thy2ctxt thy)) ts))^"]\"");
   120 	   val _=tracing("u= g@us= \""^
   121 	      ((Syntax.string_of_term (thy2ctxt thy)) g)^"\" @ \"["^
   122 	      (commas(map(Syntax.string_of_term (thy2ctxt thy)) us))^"]\"");
   123 	   val _=tracing("size_of_term(t,u)= ("^
   124 	      (string_of_int(size_of_term' t))^", "^
   125 	      (string_of_int(size_of_term' u))^")");
   126 	   val _=tracing("hd_ord(f,g)      = "^((pr_ord o hd_ord)(f,g)));
   127 	   val _=tracing("terms_ord(ts,us) = "^
   128 			   ((pr_ord o terms_ord str false)(ts,us)));
   129 	   val _=tracing("-------");
   130 	 in () end
   131        else ();
   132 	 case int_ord (size_of_term' t, size_of_term' u) of
   133 	   EQUAL =>
   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) 
   136 	     | ord => ord)
   137 	     end
   138 	 | ord => ord)
   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);
   144 
   145 in
   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
   150 *)
   151 
   152 (*args
   153    pr: print trace, WN0509 'sqrt_right true' not used anymore
   154    thy:
   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 );
   159 end;
   160 
   161 rew_ord' := overwritel (!rew_ord',
   162 [("termlessI", termlessI),
   163  ("sqrt_right", sqrt_right false (theory "Pure"))
   164  ]);
   165 
   166 (*-------------------------rulse-------------------------*)
   167 val Root_crls = 
   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 ("Rings.inverse_class.divide",eval_cancel "#divide_e"),
   172         Calc ("Atools.pow" ,eval_binop "#power_"),
   173         Calc ("Groups.plus_class.plus", eval_binop "#add_"), 
   174         Calc ("Groups.minus_class.minus", eval_binop "#sub_"),
   175         Calc ("Groups.times_class.times", eval_binop "#mult_"),
   176         Calc ("op =",eval_equal "#equal_") 
   177         ];
   178 
   179 val Root_erls = 
   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 ("Rings.inverse_class.divide",eval_cancel "#divide_e"),
   184         Calc ("Atools.pow" ,eval_binop "#power_"),
   185         Calc ("Groups.plus_class.plus", eval_binop "#add_"), 
   186         Calc ("Groups.minus_class.minus", eval_binop "#sub_"),
   187         Calc ("Groups.times_class.times", eval_binop "#mult_"),
   188         Calc ("op =",eval_equal "#equal_") 
   189         ];
   190 
   191 ruleset' := overwritelthy @{theory} (!ruleset',
   192 			[("Root_erls",Root_erls) (*FIXXXME:del with rls.rls'*) 
   193 			 ]);
   194 
   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,
   199       calc = [],
   200       (*asm_thm = [],*)
   201       rules = [Thm ("real_diff_minus",num_str @{thm real_diff_minus}),			
   202 	       (*"a - b = a + (-1) * b"*)
   203 
   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"*)
   212 
   213 	       Thm ("mult_1_left",num_str @{thm mult_1_left}),                         
   214 	       (*"1 * z = z"*)
   215 	       Thm ("mult_zero_left",num_str @{thm mult_zero_left}),                         
   216 	       (*"0 * z = 0"*)
   217 	       Thm ("add_0_left",num_str @{thm add_0_left}),		
   218 	       (*"0 + z = z"*)
   219  
   220 	       Thm ("real_mult_commute",num_str @{thm real_mult_commute}),
   221 		(*AC-rewriting*)
   222 	       Thm ("real_mult_left_commute",num_str @{thm real_mult_left_commute}),
   223          	(**)
   224 	       Thm ("real_mult_assoc",num_str @{thm real_mult_assoc}),
   225 	        (**)
   226 	       Thm ("add_commute",num_str @{thm add_commute}),
   227 		(**)
   228 	       Thm ("add_left_commute",num_str @{thm add_left_commute}),
   229 	        (**)
   230 	       Thm ("add_assoc",num_str @{thm add_assoc}),
   231 	        (**)
   232 
   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"*)
   243 
   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"*)
   253 
   254 	       Calc ("Groups.plus_class.plus", eval_binop "#add_"), 
   255 	       Calc ("Groups.times_class.times", eval_binop "#mult_"),
   256 	       Calc ("Atools.pow", eval_binop "#power_")
   257 	       ],
   258       scr = Script ((term_of o the o (parse thy)) "empty_script")
   259       }:rls);      
   260 ruleset' := overwritelthy @{theory} (!ruleset',
   261 			[("make_rooteq", make_rooteq)
   262 			 ]);
   263 
   264 val expand_rootbinoms = prep_rls(
   265   Rls{id = "expand_rootbinoms", preconds = [], 
   266       rew_ord = ("termlessI",termlessI),
   267       erls = Atools_erls, srls = Erls,
   268       calc = [],
   269       (*asm_thm = [],*)
   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"*)
   282 	       (*RL 020915*)
   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*)
   293 
   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}), 
   297                  (*"0 + z = z"*)
   298 
   299 	       Calc ("Groups.plus_class.plus", eval_binop "#add_"), 
   300 	       Calc ("Groups.minus_class.minus", eval_binop "#sub_"), 
   301 	       Calc ("Groups.times_class.times", eval_binop "#mult_"),
   302 	       Calc ("Rings.inverse_class.divide"  ,eval_cancel "#divide_e"),
   303 	       Calc ("NthRoot.sqrt",eval_sqrt "#sqrt_"),
   304 	       Calc ("Atools.pow", eval_binop "#power_"),
   305 
   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"*)
   313 
   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"*)
   323 
   324 	       Calc ("Groups.plus_class.plus", eval_binop "#add_"), 
   325 	       Calc ("Groups.minus_class.minus", eval_binop "#sub_"), 
   326 	       Calc ("Groups.times_class.times", eval_binop "#mult_"),
   327 	       Calc ("Rings.inverse_class.divide"  ,eval_cancel "#divide_e"),
   328 	       Calc ("NthRoot.sqrt",eval_sqrt "#sqrt_"),
   329 	       Calc ("Atools.pow", eval_binop "#power_")
   330 	       ],
   331       scr = Script ((term_of o the o (parse thy)) "empty_script")
   332        }:rls);      
   333 
   334 
   335 ruleset' := overwritelthy @{theory} (!ruleset',
   336 			[("expand_rootbinoms", expand_rootbinoms)
   337 			 ]);
   338 *}
   339 
   340 end