src/Tools/isac/Knowledge/Root.thy
author Walther Neuper <neuper@ist.tugraz.at>
Thu, 02 Sep 2010 15:11:23 +0200
branchisac-update-Isa09-2
changeset 37974 ececb894db9c
parent 37972 66fc615a1e89
child 37975 13ba73251a32
permissions -rw-r--r--
cleanup within Knowledge/Poly, start with 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 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"    ,("Root.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 "Root.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 "Root.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 => 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 _=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("-------");
   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 indexname_ord typ_ord) int_ord (dest_hd' f, dest_hd' g)
   141 and terms_ord str pr (ts, us) = 
   142     list_ord (term_ord' pr (assoc_thy "Isac.thy"))(ts, us);
   143 
   144 in
   145 (* associates a+(b+c) => (a+b)+c = a+b+c ... avoiding parentheses 
   146   by (1) size_of_term: less(!) to right, size_of 'sqrt (...)' = 1 
   147      (2) hd_ord: greater to right, 'sqrt' < numerals < variables
   148      (3) terms_ord: recurs. on args, greater to right
   149 *)
   150 
   151 (*args
   152    pr: print trace, WN0509 'sqrt_right true' not used anymore
   153    thy:
   154    subst: no bound variables, only Root.sqrt
   155    tu: the terms to compare (t1, t2) ... *)
   156 fun sqrt_right (pr:bool) thy (_:subst) tu = 
   157     (term_ord' pr thy(***) tu = LESS );
   158 end;
   159 
   160 rew_ord' := overwritel (!rew_ord',
   161 [("termlessI", termlessI),
   162  ("sqrt_right", sqrt_right false (theory "Pure"))
   163  ]);
   164 
   165 (*-------------------------rulse-------------------------*)
   166 val Root_crls = 
   167       append_rls "Root_crls" Atools_erls 
   168        [Thm  ("real_unari_minus",num_str @{thm real_unari_minus}),
   169         Calc ("Root.sqrt" ,eval_sqrt "#sqrt_"),
   170         Calc ("HOL.divide",eval_cancel "#divide_"),
   171         Calc ("Atools.pow" ,eval_binop "#power_"),
   172         Calc ("op +", eval_binop "#add_"), 
   173         Calc ("op -", eval_binop "#sub_"),
   174         Calc ("op *", eval_binop "#mult_"),
   175         Calc ("op =",eval_equal "#equal_") 
   176         ];
   177 
   178 val Root_erls = 
   179       append_rls "Root_erls" Atools_erls 
   180        [Thm  ("real_unari_minus",num_str @{thm real_unari_minus}),
   181         Calc ("Root.sqrt" ,eval_sqrt "#sqrt_"),
   182         Calc ("HOL.divide",eval_cancel "#divide_"),
   183         Calc ("Atools.pow" ,eval_binop "#power_"),
   184         Calc ("op +", eval_binop "#add_"), 
   185         Calc ("op -", eval_binop "#sub_"),
   186         Calc ("op *", eval_binop "#mult_"),
   187         Calc ("op =",eval_equal "#equal_") 
   188         ];
   189 
   190 ruleset' := overwritelthy @{theory} (!ruleset',
   191 			[("Root_erls",Root_erls) (*FIXXXME:del with rls.rls'*) 
   192 			 ]);
   193 
   194 val make_rooteq = prep_rls(
   195   Rls{id = "make_rooteq", preconds = []:term list, 
   196       rew_ord = ("sqrt_right", sqrt_right false thy),
   197       erls = Atools_erls, srls = Erls,
   198       calc = [],
   199       (*asm_thm = [],*)
   200       rules = [Thm ("real_diff_minus",num_str @{thm real_diff_minus}),			
   201 	       (*"a - b = a + (-1) * b"*)
   202 
   203 	       Thm ("left_distrib" ,num_str @{thm left_distrib}),	
   204 	       (*"(z1.0 + z2.0) * w = z1.0 * w + z2.0 * w"*)
   205 	       Thm ("right_distrib",num_str @{thm right_distrib}),	
   206 	       (*"w * (z1.0 + z2.0) = w * z1.0 + w * z2.0"*)
   207 	       Thm ("left_diff_distrib" ,num_str @{thm left_diff_distrib}),	
   208 	       (*"(z1.0 - z2.0) * w = z1.0 * w - z2.0 * w"*)
   209 	       Thm ("left_diff_distrib2",num_str @{thm left_diff_distrib2}),	
   210 	       (*"w * (z1.0 - z2.0) = w * z1.0 - w * z2.0"*)
   211 
   212 	       Thm ("mult_1_left",num_str @{thm mult_1_left}),                         
   213 	       (*"1 * z = z"*)
   214 	       Thm ("mult_zero_left",num_str @{thm mult_zero_left}),                         
   215 	       (*"0 * z = 0"*)
   216 	       Thm ("add_0_left",num_str @{thm add_0_left}),		
   217 	       (*"0 + z = z"*)
   218  
   219 	       Thm ("real_mult_commute",num_str @{thm real_mult_commute}),
   220 		(*AC-rewriting*)
   221 	       Thm ("real_mult_left_commute",num_str @{thm real_mult_left_commute}),
   222          	(**)
   223 	       Thm ("real_mult_assoc",num_str @{thm real_mult_assoc}),
   224 	        (**)
   225 	       Thm ("add_commute",num_str @{thm add_commute}),
   226 		(**)
   227 	       Thm ("add_left_commute",num_str @{thm add_left_commute}),
   228 	        (**)
   229 	       Thm ("add_assoc",num_str @{thm add_assoc}),
   230 	        (**)
   231 
   232 	       Thm ("sym_realpow_twoI",
   233                      num_str (@{thm realpow_twoI} RS @{thm sym})),
   234 	       (*"r1 * r1 = r1 ^^^ 2"*)
   235 	       Thm ("realpow_plus_1",num_str @{thm realpow_plus_1}),			
   236 	       (*"r * r ^^^ n = r ^^^ (n + 1)"*)
   237 	       Thm ("sym_real_mult_2",
   238                      num_str (@{thm real_mult_2} RS @{thm sym})),
   239 	       (*"z1 + z1 = 2 * z1"*)
   240 	       Thm ("real_mult_2_assoc",num_str @{thm real_mult_2_assoc}),		
   241 	       (*"z1 + (z1 + k) = 2 * z1 + k"*)
   242 
   243 	       Thm ("real_num_collect",num_str @{thm real_num_collect}), 
   244 	       (*"[| l is_const; m is_const |]==> l * n + m * n = (l + m) * n"*)
   245 	       Thm ("real_num_collect_assoc",num_str @{thm real_num_collect_assoc}),	
   246 	       (*"[| l is_const; m is_const |] ==>  
   247                                    l * n + (m * n + k) =  (l + m) * n + k"*)
   248 	       Thm ("real_one_collect",num_str @{thm real_one_collect}),		
   249 	       (*"m is_const ==> n + m * n = (1 + m) * n"*)
   250 	       Thm ("real_one_collect_assoc",num_str @{thm real_one_collect_assoc}), 
   251 	       (*"m is_const ==> k + (n + m * n) = k + (1 + m) * n"*)
   252 
   253 	       Calc ("op +", eval_binop "#add_"), 
   254 	       Calc ("op *", eval_binop "#mult_"),
   255 	       Calc ("Atools.pow", eval_binop "#power_")
   256 	       ],
   257       scr = Script ((term_of o the o (parse thy)) "empty_script")
   258       }:rls);      
   259 ruleset' := overwritelthy @{theory} (!ruleset',
   260 			[("make_rooteq", make_rooteq)
   261 			 ]);
   262 
   263 val expand_rootbinoms = prep_rls(
   264   Rls{id = "expand_rootbinoms", preconds = [], 
   265       rew_ord = ("termlessI",termlessI),
   266       erls = Atools_erls, srls = Erls,
   267       calc = [],
   268       (*asm_thm = [],*)
   269       rules = [Thm ("real_plus_binom_pow2"  ,num_str @{thm real_plus_binom_pow2}),     
   270 	       (*"(a + b) ^^^ 2 = a ^^^ 2 + 2 * a * b + b ^^^ 2"*)
   271 	       Thm ("real_plus_binom_times" ,num_str @{thm real_plus_binom_times}),    
   272 	       (*"(a + b)*(a + b) = ...*)
   273 	       Thm ("real_minus_binom_pow2" ,num_str @{thm real_minus_binom_pow2}),    
   274 		(*"(a - b) ^^^ 2 = a ^^^ 2 - 2 * a * b + b ^^^ 2"*)
   275 	       Thm ("real_minus_binom_times",num_str @{thm real_minus_binom_times}),   
   276 	       (*"(a - b)*(a - b) = ...*)
   277 	       Thm ("real_plus_minus_binom1",num_str @{thm real_plus_minus_binom1}),   
   278 		(*"(a + b) * (a - b) = a ^^^ 2 - b ^^^ 2"*)
   279 	       Thm ("real_plus_minus_binom2",num_str @{thm real_plus_minus_binom2}),   
   280 		(*"(a - b) * (a + b) = a ^^^ 2 - b ^^^ 2"*)
   281 	       (*RL 020915*)
   282 	       Thm ("real_pp_binom_times",num_str @{thm real_pp_binom_times}), 
   283 		(*(a + b)*(c + d) = a*c + a*d + b*c + b*d*)
   284                Thm ("real_pm_binom_times",num_str @{thm real_pm_binom_times}), 
   285 		(*(a + b)*(c - d) = a*c - a*d + b*c - b*d*)
   286                Thm ("real_mp_binom_times",num_str @{thm real_mp_binom_times}), 
   287 		(*(a - b)*(c p d) = a*c + a*d - b*c - b*d*)
   288                Thm ("real_mm_binom_times",num_str @{thm real_mm_binom_times}), 
   289 		(*(a - b)*(c p d) = a*c - a*d - b*c + b*d*)
   290 	       Thm ("realpow_mul",num_str @{thm realpow_mul}),                 
   291 		(*(a*b)^^^n = a^^^n * b^^^n*)
   292 
   293 	       Thm ("mult_1_left",num_str @{thm mult_1_left}),         (*"1 * z = z"*)
   294 	       Thm ("mult_zero_left",num_str @{thm mult_zero_left}),         (*"0 * z = 0"*)
   295 	       Thm ("add_0_left",num_str @{thm add_0_left}), 
   296                  (*"0 + z = z"*)
   297 
   298 	       Calc ("op +", eval_binop "#add_"), 
   299 	       Calc ("op -", eval_binop "#sub_"), 
   300 	       Calc ("op *", eval_binop "#mult_"),
   301 	       Calc ("HOL.divide"  ,eval_cancel "#divide_"),
   302 	       Calc ("Root.sqrt",eval_sqrt "#sqrt_"),
   303 	       Calc ("Atools.pow", eval_binop "#power_"),
   304 
   305 	       Thm ("sym_realpow_twoI",
   306                      num_str (@{thm realpow_twoI} RS @{thm sym})),
   307 	       (*"r1 * r1 = r1 ^^^ 2"*)
   308 	       Thm ("realpow_plus_1",num_str @{thm realpow_plus_1}),			
   309 	       (*"r * r ^^^ n = r ^^^ (n + 1)"*)
   310 	       Thm ("real_mult_2_assoc",num_str @{thm real_mult_2_assoc}),		
   311 	       (*"z1 + (z1 + k) = 2 * z1 + k"*)
   312 
   313 	       Thm ("real_num_collect",num_str @{thm real_num_collect}), 
   314 	       (*"[| l is_const; m is_const |] ==>l * n + m * n = (l + m) * n"*)
   315 	       Thm ("real_num_collect_assoc",num_str @{thm real_num_collect_assoc}),	
   316 	       (*"[| l is_const; m is_const |] ==>
   317                   l * n + (m * n + k) =  (l + m) * n + k"*)
   318 	       Thm ("real_one_collect",num_str @{thm real_one_collect}),		
   319 	       (*"m is_const ==> n + m * n = (1 + m) * n"*)
   320 	       Thm ("real_one_collect_assoc",num_str @{thm real_one_collect_assoc}), 
   321 	       (*"m is_const ==> k + (n + m * n) = k + (1 + m) * n"*)
   322 
   323 	       Calc ("op +", eval_binop "#add_"), 
   324 	       Calc ("op -", eval_binop "#sub_"), 
   325 	       Calc ("op *", eval_binop "#mult_"),
   326 	       Calc ("HOL.divide"  ,eval_cancel "#divide_"),
   327 	       Calc ("Root.sqrt",eval_sqrt "#sqrt_"),
   328 	       Calc ("Atools.pow", eval_binop "#power_")
   329 	       ],
   330       scr = Script ((term_of o the o (parse thy)) "empty_script")
   331        }:rls);      
   332 
   333 
   334 ruleset' := overwritelthy @{theory} (!ruleset',
   335 			[("expand_rootbinoms", expand_rootbinoms)
   336 			 ]);
   337 *}
   338 
   339 end