src/Tools/isac/Knowledge/Root.thy
author Walther Neuper <neuper@ist.tugraz.at>
Wed, 01 Sep 2010 15:17:43 +0200
branchisac-update-Isa09-2
changeset 37969 81922154e742
parent 37967 bd4f7a35e892
child 37972 66fc615a1e89
permissions -rw-r--r--
fixed all @{thm } in src+test
     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 @{thm 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 @{thm 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 @{theory} (!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 @{thm 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_distrib2}),	
   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_distrib2}),	
   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 @{thm real_mult_commute}),
   218 		(*AC-rewriting*)
   219 	       Thm ("real_mult_left_commute",num_str @{thm real_mult_left_commute}),
   220          	(**)
   221 	       Thm ("real_mult_assoc",num_str @{thm 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",
   231                      num_str (@{thm realpow_twoI} RS @{thm sym})),
   232 	       (*"r1 * r1 = r1 ^^^ 2"*)
   233 	       Thm ("realpow_plus_1",num_str @{thm realpow_plus_1}),			
   234 	       (*"r * r ^^^ n = r ^^^ (n + 1)"*)
   235 	       Thm ("sym_real_mult_2",
   236                      num_str (@{thm real_mult_2} RS @{thm sym})),
   237 	       (*"z1 + z1 = 2 * z1"*)
   238 	       Thm ("real_mult_2_assoc",num_str @{thm real_mult_2_assoc}),		
   239 	       (*"z1 + (z1 + k) = 2 * z1 + k"*)
   240 
   241 	       Thm ("real_num_collect",num_str @{thm real_num_collect}), 
   242 	       (*"[| l is_const; m is_const |]==> l * n + m * n = (l + m) * n"*)
   243 	       Thm ("real_num_collect_assoc",num_str @{thm real_num_collect_assoc}),	
   244 	       (*"[| l is_const; m is_const |] ==>  
   245                                    l * n + (m * n + k) =  (l + m) * n + k"*)
   246 	       Thm ("real_one_collect",num_str @{thm real_one_collect}),		
   247 	       (*"m is_const ==> n + m * n = (1 + m) * n"*)
   248 	       Thm ("real_one_collect_assoc",num_str @{thm real_one_collect_assoc}), 
   249 	       (*"m is_const ==> k + (n + m * n) = k + (1 + m) * n"*)
   250 
   251 	       Calc ("op +", eval_binop "#add_"), 
   252 	       Calc ("op *", eval_binop "#mult_"),
   253 	       Calc ("Atools.pow", eval_binop "#power_")
   254 	       ],
   255       scr = Script ((term_of o the o (parse thy)) "empty_script")
   256       }:rls);      
   257 ruleset' := overwritelthy @{theory} (!ruleset',
   258 			[("make_rooteq", make_rooteq)
   259 			 ]);
   260 
   261 val expand_rootbinoms = prep_rls(
   262   Rls{id = "expand_rootbinoms", preconds = [], 
   263       rew_ord = ("termlessI",termlessI),
   264       erls = Atools_erls, srls = Erls,
   265       calc = [],
   266       (*asm_thm = [],*)
   267       rules = [Thm ("real_plus_binom_pow2"  ,num_str @{thm real_plus_binom_pow2}),     
   268 	       (*"(a + b) ^^^ 2 = a ^^^ 2 + 2 * a * b + b ^^^ 2"*)
   269 	       Thm ("real_plus_binom_times" ,num_str @{thm real_plus_binom_times}),    
   270 	       (*"(a + b)*(a + b) = ...*)
   271 	       Thm ("real_minus_binom_pow2" ,num_str @{thm real_minus_binom_pow2}),    
   272 		(*"(a - b) ^^^ 2 = a ^^^ 2 - 2 * a * b + b ^^^ 2"*)
   273 	       Thm ("real_minus_binom_times",num_str @{thm real_minus_binom_times}),   
   274 	       (*"(a - b)*(a - b) = ...*)
   275 	       Thm ("real_plus_minus_binom1",num_str @{thm real_plus_minus_binom1}),   
   276 		(*"(a + b) * (a - b) = a ^^^ 2 - b ^^^ 2"*)
   277 	       Thm ("real_plus_minus_binom2",num_str @{thm real_plus_minus_binom2}),   
   278 		(*"(a - b) * (a + b) = a ^^^ 2 - b ^^^ 2"*)
   279 	       (*RL 020915*)
   280 	       Thm ("real_pp_binom_times",num_str @{thm real_pp_binom_times}), 
   281 		(*(a + b)*(c + d) = a*c + a*d + b*c + b*d*)
   282                Thm ("real_pm_binom_times",num_str @{thm real_pm_binom_times}), 
   283 		(*(a + b)*(c - d) = a*c - a*d + b*c - b*d*)
   284                Thm ("real_mp_binom_times",num_str @{thm real_mp_binom_times}), 
   285 		(*(a - b)*(c p d) = a*c + a*d - b*c - b*d*)
   286                Thm ("real_mm_binom_times",num_str @{thm real_mm_binom_times}), 
   287 		(*(a - b)*(c p d) = a*c - a*d - b*c + b*d*)
   288 	       Thm ("realpow_mul",num_str @{thm realpow_mul}),                 
   289 		(*(a*b)^^^n = a^^^n * b^^^n*)
   290 
   291 	       Thm ("mult_1_left",num_str @{thm mult_1_left}),         (*"1 * z = z"*)
   292 	       Thm ("mult_zero_left",num_str @{thm mult_zero_left}),         (*"0 * z = 0"*)
   293 	       Thm ("add_0_left",num_str @{thm add_0_left}), 
   294                  (*"0 + z = z"*)
   295 
   296 	       Calc ("op +", eval_binop "#add_"), 
   297 	       Calc ("op -", eval_binop "#sub_"), 
   298 	       Calc ("op *", eval_binop "#mult_"),
   299 	       Calc ("HOL.divide"  ,eval_cancel "#divide_"),
   300 	       Calc ("Root.sqrt",eval_sqrt "#sqrt_"),
   301 	       Calc ("Atools.pow", eval_binop "#power_"),
   302 
   303 	       Thm ("sym_realpow_twoI",
   304                      num_str (@{thm realpow_twoI} RS @{thm sym})),
   305 	       (*"r1 * r1 = r1 ^^^ 2"*)
   306 	       Thm ("realpow_plus_1",num_str @{thm realpow_plus_1}),			
   307 	       (*"r * r ^^^ n = r ^^^ (n + 1)"*)
   308 	       Thm ("real_mult_2_assoc",num_str @{thm real_mult_2_assoc}),		
   309 	       (*"z1 + (z1 + k) = 2 * z1 + k"*)
   310 
   311 	       Thm ("real_num_collect",num_str @{thm real_num_collect}), 
   312 	       (*"[| l is_const; m is_const |] ==>l * n + m * n = (l + m) * n"*)
   313 	       Thm ("real_num_collect_assoc",num_str @{thm real_num_collect_assoc}),	
   314 	       (*"[| l is_const; m is_const |] ==>
   315                   l * n + (m * n + k) =  (l + m) * n + k"*)
   316 	       Thm ("real_one_collect",num_str @{thm real_one_collect}),		
   317 	       (*"m is_const ==> n + m * n = (1 + m) * n"*)
   318 	       Thm ("real_one_collect_assoc",num_str @{thm real_one_collect_assoc}), 
   319 	       (*"m is_const ==> k + (n + m * n) = k + (1 + m) * n"*)
   320 
   321 	       Calc ("op +", eval_binop "#add_"), 
   322 	       Calc ("op -", eval_binop "#sub_"), 
   323 	       Calc ("op *", eval_binop "#mult_"),
   324 	       Calc ("HOL.divide"  ,eval_cancel "#divide_"),
   325 	       Calc ("Root.sqrt",eval_sqrt "#sqrt_"),
   326 	       Calc ("Atools.pow", eval_binop "#power_")
   327 	       ],
   328       scr = Script ((term_of o the o (parse thy)) "empty_script")
   329        }:rls);      
   330 
   331 
   332 ruleset' := overwritelthy @{theory} (!ruleset',
   333 			[("expand_rootbinoms", expand_rootbinoms)
   334 			 ]);
   335 *}
   336 
   337 end