src/Tools/isac/Knowledge/Root.thy
author Walther Neuper <neuper@ist.tugraz.at>
Tue, 31 Aug 2010 15:36:57 +0200
branchisac-update-Isa09-2
changeset 37965 9c11005c33b8
parent 37953 369b3012f6f6
child 37966 78938fc8e022
permissions -rw-r--r--
updated Knowledge/Atools.thy + some changes + changes ahead

# replaced thms ahead by ./thms-replace-Isa02-Isa09-2.sml
# Knowledge/Delete.thy takes intermediate code:
* fun calc, fun term_of_float,fun var_op_float, fun float_op_var
for Float, which have already been deleted
* thms which are available only with long.identifiers
which cannot be handled in isac's Scripts
# added ProgLang/Language.thy collecting all data about Scripts
     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 Simplify begin
    10 
    11 consts
    12 
    13   sqrt   :: "real => real"         (*"(sqrt _ )" [80] 80*)
    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 (*-------------------------functions---------------------*)
    46 (*evaluation square-root over the integers*)
    47 fun eval_sqrt (thmid:string) (op_:string) (t as 
    48 	       (Const(op0,t0) $ arg)) thy = 
    49     (case arg of 
    50 	Free (n1,t1) =>
    51 	(case int_of_str n1 of
    52 	     SOME ni => 
    53 	     if ni < 0 then NONE
    54 	     else
    55 		 let val fact = squfact ni;
    56 		 in if fact*fact = ni 
    57 		    then SOME ("#sqrt #"^(string_of_int ni)^" = #"
    58 			       ^(string_of_int (if ni = 0 then 0
    59 						else ni div fact)),
    60 			       Trueprop $ mk_equality (t, term_of_num t1 fact))
    61 		    else if fact = 1 then NONE
    62 		    else SOME ("#sqrt #"^(string_of_int ni)^" = sqrt (#"
    63 			       ^(string_of_int fact)^" * #"
    64 			       ^(string_of_int fact)^" * #"
    65 			       ^(string_of_int (ni div (fact*fact))^")"),
    66 			       Trueprop $ 
    67 					(mk_equality 
    68 					     (t, 
    69 					      (mk_factroot op0 t1 fact 
    70 						(ni div (fact*fact))))))
    71 	         end
    72 	   | NONE => NONE)
    73       | _ => NONE)
    74 
    75   | eval_sqrt _ _ _ _ = NONE;
    76 (*val (thmid, op_, t as Const(op0,t0) $ arg) = ("","", str2term "sqrt 0");
    77 > eval_sqrt thmid op_ t thy;
    78 > val Free (n1,t1) = arg; 
    79 > val SOME ni = int_of_str n1;
    80 *)
    81 
    82 calclist':= overwritel (!calclist', 
    83    [("SQRT"    ,("Root.sqrt"   ,eval_sqrt "#sqrt_"))
    84     (*different types for 'sqrt 4' --- 'Calculate sqrt_'*)
    85     ]);
    86 
    87 
    88 local (* Vers. 7.10.99.A *)
    89 
    90 open Term;  (* for type order = EQUAL | LESS | GREATER *)
    91 
    92 fun pr_ord EQUAL = "EQUAL"
    93   | pr_ord LESS  = "LESS"
    94   | pr_ord GREATER = "GREATER";
    95 
    96 fun dest_hd' (Const (a, T)) =                          (* ~ term.ML *)
    97   (case a of "Root.sqrt"  => ((("|||", 0), T), 0)      (*WN greatest *)
    98 	   | _ => (((a, 0), T), 0))
    99   | dest_hd' (Free (a, T)) = (((a, 0), T), 1)
   100   | dest_hd' (Var v) = (v, 2)
   101   | dest_hd' (Bound i) = ((("", i), dummyT), 3)
   102   | dest_hd' (Abs (_, T, _)) = ((("", 0), T), 4);
   103 fun size_of_term' (Const(str,_) $ t) =
   104     (case str of "Root.sqrt"  => (1000 + size_of_term' t)
   105                | _ => 1 + size_of_term' t)
   106   | size_of_term' (Abs (_,_,body)) = 1 + size_of_term' body
   107   | size_of_term' (f $ t) = size_of_term' f  +  size_of_term' t
   108   | size_of_term' _ = 1;
   109 fun term_ord' pr thy (Abs (_, T, t), Abs(_, U, u)) =       (* ~ term.ML *)
   110       (case term_ord' pr thy (t, u) of EQUAL => typ_ord (T, U) | ord => ord)
   111   | term_ord' pr thy (t, u) =
   112       (if pr then 
   113 	 let
   114 	   val (f, ts) = strip_comb t and (g, us) = strip_comb u;
   115 	   val _=writeln("t= f@ts= \""^
   116 	      ((Syntax.string_of_term (thy2ctxt thy)) f)^"\" @ \"["^
   117 	      (commas(map(Syntax.string_of_term (thy2ctxt thy)) ts))^"]\"");
   118 	   val _=writeln("u= g@us= \""^
   119 	      ((Syntax.string_of_term (thy2ctxt thy)) g)^"\" @ \"["^
   120 	      (commas(map(Syntax.string_of_term (thy2ctxt thy)) us))^"]\"");
   121 	   val _=writeln("size_of_term(t,u)= ("^
   122 	      (string_of_int(size_of_term' t))^", "^
   123 	      (string_of_int(size_of_term' u))^")");
   124 	   val _=writeln("hd_ord(f,g)      = "^((pr_ord o hd_ord)(f,g)));
   125 	   val _=writeln("terms_ord(ts,us) = "^
   126 			   ((pr_ord o terms_ord str false)(ts,us)));
   127 	   val _=writeln("-------");
   128 	 in () end
   129        else ();
   130 	 case int_ord (size_of_term' t, size_of_term' u) of
   131 	   EQUAL =>
   132 	     let val (f, ts) = strip_comb t and (g, us) = strip_comb u in
   133 	       (case hd_ord (f, g) of EQUAL => (terms_ord str pr) (ts, us) 
   134 	     | ord => ord)
   135 	     end
   136 	 | ord => ord)
   137 and hd_ord (f, g) =                                        (* ~ term.ML *)
   138   prod_ord (prod_ord indexname_ord typ_ord) int_ord (dest_hd' f, dest_hd' g)
   139 and terms_ord str pr (ts, us) = 
   140     list_ord (term_ord' pr (assoc_thy "Isac.thy"))(ts, us);
   141 
   142 in
   143 (* associates a+(b+c) => (a+b)+c = a+b+c ... avoiding parentheses 
   144   by (1) size_of_term: less(!) to right, size_of 'sqrt (...)' = 1 
   145      (2) hd_ord: greater to right, 'sqrt' < numerals < variables
   146      (3) terms_ord: recurs. on args, greater to right
   147 *)
   148 
   149 (*args
   150    pr: print trace, WN0509 'sqrt_right true' not used anymore
   151    thy:
   152    subst: no bound variables, only Root.sqrt
   153    tu: the terms to compare (t1, t2) ... *)
   154 fun sqrt_right (pr:bool) thy (_:subst) tu = 
   155     (term_ord' pr thy(***) tu = LESS );
   156 end;
   157 
   158 rew_ord' := overwritel (!rew_ord',
   159 [("termlessI", termlessI),
   160  ("sqrt_right", sqrt_right false (theory "Pure"))
   161  ]);
   162 
   163 (*-------------------------rulse-------------------------*)
   164 val Root_crls = 
   165       append_rls "Root_crls" Atools_erls 
   166        [Thm  ("real_unari_minus",num_str real_unari_minus),
   167         Calc ("Root.sqrt" ,eval_sqrt "#sqrt_"),
   168         Calc ("HOL.divide",eval_cancel "#divide_"),
   169         Calc ("Atools.pow" ,eval_binop "#power_"),
   170         Calc ("op +", eval_binop "#add_"), 
   171         Calc ("op -", eval_binop "#sub_"),
   172         Calc ("op *", eval_binop "#mult_"),
   173         Calc ("op =",eval_equal "#equal_") 
   174         ];
   175 
   176 val Root_erls = 
   177       append_rls "Root_erls" Atools_erls 
   178        [Thm  ("real_unari_minus",num_str real_unari_minus),
   179         Calc ("Root.sqrt" ,eval_sqrt "#sqrt_"),
   180         Calc ("HOL.divide",eval_cancel "#divide_"),
   181         Calc ("Atools.pow" ,eval_binop "#power_"),
   182         Calc ("op +", eval_binop "#add_"), 
   183         Calc ("op -", eval_binop "#sub_"),
   184         Calc ("op *", eval_binop "#mult_"),
   185         Calc ("op =",eval_equal "#equal_") 
   186         ];
   187 
   188 ruleset' := overwritelthy thy (!ruleset',
   189 			[("Root_erls",Root_erls) (*FIXXXME:del with rls.rls'*) 
   190 			 ]);
   191 
   192 val make_rooteq = prep_rls(
   193   Rls{id = "make_rooteq", preconds = []:term list, 
   194       rew_ord = ("sqrt_right", sqrt_right false (theory "Root")),
   195       erls = Atools_erls, srls = Erls,
   196       calc = [],
   197       (*asm_thm = [],*)
   198       rules = [Thm ("real_diff_minus",num_str real_diff_minus),			
   199 	       (*"a - b = a + (-1) * b"*)
   200 
   201 	       Thm ("left_distrib" ,num_str @{thm left_distrib}),	
   202 	       (*"(z1.0 + z2.0) * w = z1.0 * w + z2.0 * w"*)
   203 	       Thm ("left_distrib2",num_str @{thm left_distrib}2),	
   204 	       (*"w * (z1.0 + z2.0) = w * z1.0 + w * z2.0"*)
   205 	       Thm ("left_diff_distrib" ,num_str @{thm left_diff_distrib}),	
   206 	       (*"(z1.0 - z2.0) * w = z1.0 * w - z2.0 * w"*)
   207 	       Thm ("left_diff_distrib2",num_str @{thm left_diff_distrib}2),	
   208 	       (*"w * (z1.0 - z2.0) = w * z1.0 - w * z2.0"*)
   209 
   210 	       Thm ("mult_1_left",num_str @{thm mult_1_left}),                         
   211 	       (*"1 * z = z"*)
   212 	       Thm ("mult_zero_left",num_str @{thm mult_zero_left}),                         
   213 	       (*"0 * z = 0"*)
   214 	       Thm ("add_0_left",num_str @{thm add_0_left}),		
   215 	       (*"0 + z = z"*)
   216  
   217 	       Thm ("real_mult_commute",num_str real_mult_commute),
   218 		(*AC-rewriting*)
   219 	       Thm ("real_mult_left_commute",num_str real_mult_left_commute),
   220          	(**)
   221 	       Thm ("real_mult_assoc",num_str real_mult_assoc),
   222 	        (**)
   223 	       Thm ("add_commute",num_str @{thm add_commute}),
   224 		(**)
   225 	       Thm ("add_left_commute",num_str @{thm add_left_commute}),
   226 	        (**)
   227 	       Thm ("add_assoc",num_str @{thm add_assoc}),
   228 	        (**)
   229 
   230 	       Thm ("sym_realpow_twoI",num_str (realpow_twoI RS sym)),		
   231 	       (*"r1 * r1 = r1 ^^^ 2"*)
   232 	       Thm ("realpow_plus_1",num_str realpow_plus_1),			
   233 	       (*"r * r ^^^ n = r ^^^ (n + 1)"*)
   234 	       Thm ("sym_real_mult_2",num_str (real_mult_2 RS sym)),		
   235 	       (*"z1 + z1 = 2 * z1"*)
   236 	       Thm ("real_mult_2_assoc",num_str real_mult_2_assoc),		
   237 	       (*"z1 + (z1 + k) = 2 * z1 + k"*)
   238 
   239 	       Thm ("real_num_collect",num_str real_num_collect), 
   240 	       (*"[| l is_const; m is_const |]==> l * n + m * n = (l + m) * n"*)
   241 	       Thm ("real_num_collect_assoc",num_str real_num_collect_assoc),	
   242 	       (*"[| l is_const; m is_const |] ==>  
   243                                    l * n + (m * n + k) =  (l + m) * n + k"*)
   244 	       Thm ("real_one_collect",num_str real_one_collect),		
   245 	       (*"m is_const ==> n + m * n = (1 + m) * n"*)
   246 	       Thm ("real_one_collect_assoc",num_str real_one_collect_assoc), 
   247 	       (*"m is_const ==> k + (n + m * n) = k + (1 + m) * n"*)
   248 
   249 	       Calc ("op +", eval_binop "#add_"), 
   250 	       Calc ("op *", eval_binop "#mult_"),
   251 	       Calc ("Atools.pow", eval_binop "#power_")
   252 	       ],
   253       scr = Script ((term_of o the o (parse thy)) "empty_script")
   254       }:rls);      
   255 ruleset' := overwritelthy thy (!ruleset',
   256 			[("make_rooteq", make_rooteq)
   257 			 ]);
   258 
   259 val expand_rootbinoms = prep_rls(
   260   Rls{id = "expand_rootbinoms", preconds = [], 
   261       rew_ord = ("termlessI",termlessI),
   262       erls = Atools_erls, srls = Erls,
   263       calc = [],
   264       (*asm_thm = [],*)
   265       rules = [Thm ("real_plus_binom_pow2"  ,num_str real_plus_binom_pow2),     
   266 	       (*"(a + b) ^^^ 2 = a ^^^ 2 + 2 * a * b + b ^^^ 2"*)
   267 	       Thm ("real_plus_binom_times" ,num_str real_plus_binom_times),    
   268 	       (*"(a + b)*(a + b) = ...*)
   269 	       Thm ("real_minus_binom_pow2" ,num_str real_minus_binom_pow2),    
   270 		(*"(a - b) ^^^ 2 = a ^^^ 2 - 2 * a * b + b ^^^ 2"*)
   271 	       Thm ("real_minus_binom_times",num_str real_minus_binom_times),   
   272 	       (*"(a - b)*(a - b) = ...*)
   273 	       Thm ("real_plus_minus_binom1",num_str real_plus_minus_binom1),   
   274 		(*"(a + b) * (a - b) = a ^^^ 2 - b ^^^ 2"*)
   275 	       Thm ("real_plus_minus_binom2",num_str real_plus_minus_binom2),   
   276 		(*"(a - b) * (a + b) = a ^^^ 2 - b ^^^ 2"*)
   277 	       (*RL 020915*)
   278 	       Thm ("real_pp_binom_times",num_str real_pp_binom_times), 
   279 		(*(a + b)*(c + d) = a*c + a*d + b*c + b*d*)
   280                Thm ("real_pm_binom_times",num_str real_pm_binom_times), 
   281 		(*(a + b)*(c - d) = a*c - a*d + b*c - b*d*)
   282                Thm ("real_mp_binom_times",num_str real_mp_binom_times), 
   283 		(*(a - b)*(c p d) = a*c + a*d - b*c - b*d*)
   284                Thm ("real_mm_binom_times",num_str real_mm_binom_times), 
   285 		(*(a - b)*(c p d) = a*c - a*d - b*c + b*d*)
   286 	       Thm ("realpow_mul",num_str realpow_mul),                 
   287 		(*(a*b)^^^n = a^^^n * b^^^n*)
   288 
   289 	       Thm ("mult_1_left",num_str @{thm mult_1_left}),         (*"1 * z = z"*)
   290 	       Thm ("mult_zero_left",num_str @{thm mult_zero_left}),         (*"0 * z = 0"*)
   291 	       Thm ("add_0_left",num_str @{thm add_0_left}), 
   292                  (*"0 + z = z"*)
   293 
   294 	       Calc ("op +", eval_binop "#add_"), 
   295 	       Calc ("op -", eval_binop "#sub_"), 
   296 	       Calc ("op *", eval_binop "#mult_"),
   297 	       Calc ("HOL.divide"  ,eval_cancel "#divide_"),
   298 	       Calc ("Root.sqrt",eval_sqrt "#sqrt_"),
   299 	       Calc ("Atools.pow", eval_binop "#power_"),
   300 
   301 	       Thm ("sym_realpow_twoI",num_str (realpow_twoI RS sym)),		
   302 	       (*"r1 * r1 = r1 ^^^ 2"*)
   303 	       Thm ("realpow_plus_1",num_str realpow_plus_1),			
   304 	       (*"r * r ^^^ n = r ^^^ (n + 1)"*)
   305 	       Thm ("real_mult_2_assoc",num_str real_mult_2_assoc),		
   306 	       (*"z1 + (z1 + k) = 2 * z1 + k"*)
   307 
   308 	       Thm ("real_num_collect",num_str real_num_collect), 
   309 	       (*"[| l is_const; m is_const |] ==>l * n + m * n = (l + m) * n"*)
   310 	       Thm ("real_num_collect_assoc",num_str real_num_collect_assoc),	
   311 	       (*"[| l is_const; m is_const |] ==>
   312                   l * n + (m * n + k) =  (l + m) * n + k"*)
   313 	       Thm ("real_one_collect",num_str real_one_collect),		
   314 	       (*"m is_const ==> n + m * n = (1 + m) * n"*)
   315 	       Thm ("real_one_collect_assoc",num_str real_one_collect_assoc), 
   316 	       (*"m is_const ==> k + (n + m * n) = k + (1 + m) * n"*)
   317 
   318 	       Calc ("op +", eval_binop "#add_"), 
   319 	       Calc ("op -", eval_binop "#sub_"), 
   320 	       Calc ("op *", eval_binop "#mult_"),
   321 	       Calc ("HOL.divide"  ,eval_cancel "#divide_"),
   322 	       Calc ("Root.sqrt",eval_sqrt "#sqrt_"),
   323 	       Calc ("Atools.pow", eval_binop "#power_")
   324 	       ],
   325       scr = Script ((term_of o the o (parse thy)) "empty_script")
   326        }:rls);      
   327 
   328 
   329 ruleset' := overwritelthy thy (!ruleset',
   330 			[("expand_rootbinoms", expand_rootbinoms)
   331 			 ]);
   332 *}
   333 
   334 end