src/sml/Scripts/calculate.sml
branchgriesmayer
changeset 332 c6f2398d854a
child 705 7bd5676a8ceb
equal deleted inserted replaced
331:84c2eef06e26 332:c6f2398d854a
       
     1 (* use"calculate.sml";
       
     2    use"Isa02/rewrite-parse.sml";
       
     3    use"../Isa02/rewrite-parse.sml";
       
     4    WN.6.1.00
       
     5 
       
     6    both, the rewriter and the parser based on isabelle
       
     7    in one file
       
     8 *)
       
     9 
       
    10 (*val trace_order = ref false;   25.5.02 loeschen !!!*)
       
    11 
       
    12 (*.trace internal steps of isac's rewriter*)
       
    13 val trace_rewrite = ref false;
       
    14 (*.depth of recursion in traces of the rewriter, if trace_rewrite:=true.*)
       
    15 val depth = ref 99999;
       
    16 
       
    17 
       
    18 
       
    19 (* dirty type-conversion 30.1.00 for "fixed_values [R=R]" *)
       
    20 
       
    21 val aT = Type ("'a", []);
       
    22 (* isas types for Free, parseold: (1) "R=R" or (2) "R=(R::real)": 
       
    23 (1)
       
    24 > val (TFree(ss2,TT2)) = T2;
       
    25 val ss2 = "'a" : string
       
    26 val TT2 = ["term"] : sort
       
    27 (2)
       
    28 > val (Type(ss2',TT2')) = T2';
       
    29 val ss2' = "RealDef.real" : string
       
    30 val TT2' = [] : typ list
       
    31 (3)
       
    32 val realType = TFree ("RealDef.real", HOLogic.termS);
       
    33 is different internally, too;
       
    34 
       
    35 (1) .. (3) are displayed equally !!!
       
    36 *)
       
    37 
       
    38 
       
    39 
       
    40 (* 30.1.00: generating special terms for ME:
       
    41    (1) binary numerals reconverted to Free ("#num",...) 
       
    42        by libarary_G.num_str: called from parse (below) and 
       
    43        interface_ME_ISA for all thms used
       
    44        (compare HOLogic.dest_binum)
       
    45    (2) 'a types converted to RealDef.real by typ_a2real
       
    46        in parse below
       
    47    (3) binary operators fixed to type real in RatArith.thy
       
    48        (trick by Markus Wenzel)
       
    49 *)
       
    50 
       
    51 
       
    52 
       
    53 
       
    54 (** calculate numerals **)
       
    55 
       
    56 (*27.3.00: problems with patterns below:
       
    57 "Var (a // #2 = r * xxxxx b)" doesn't work, but 
       
    58 "Var (a // #2 = r * sqrt b)" works
       
    59 *)
       
    60 
       
    61 
       
    62 (* scan a term for applying eval_fn ef 
       
    63 args
       
    64   thy:
       
    65   op_: operator (as string) selecting the root of the pair
       
    66   ef : fn : (string -> term -> theory -> (string * term) option)
       
    67              ^^^^^^... for creating the string for the resulting theorem
       
    68   t  : term to be scanned
       
    69 result:
       
    70   (string * term) option: found by the eval_* -function of type
       
    71        fn : string -> string -> term -> theory -> (string * term) option
       
    72             ^^^^^^... the selecting operator op_ (variable for eval_binop)
       
    73 *)
       
    74 fun get_pair thy op_ (ef:string -> term -> theory -> (string * term) option) 
       
    75     (t as (Const(op0,t0) $ arg)) =                      (* unary fns *)
       
    76     if op_ = (*strip_thy*) op0 then 
       
    77 	let val popt = ef op_ t thy
       
    78 	in case popt of
       
    79 	       Some _ => popt
       
    80 	     | None => get_pair thy op_ ef arg end
       
    81     else get_pair thy op_ ef arg
       
    82  
       
    83   | get_pair thy "Atools.ident" ef (t as (Const("Atools.ident",t0) $ _ $ _ )) =
       
    84     ef "Atools.ident" t thy                                 (* not nested *)
       
    85 (* val (thy,op_,ef,t as (Const(op0,_)$t1$t2)) =
       
    86        (thy,op_,eval_fn,ct);
       
    87    *)
       
    88   | get_pair thy op_ ef (t as (Const(op0,_) $ t1 $ t2)) =  (* binary funs*)
       
    89     ((*writeln("@@@ get_pair: binop, t = "^
       
    90 	     (Sign.string_of_term (sign_of thy) t));*)
       
    91      if op_ = op0 then 
       
    92 	 let (*val _=writeln"@@@ get_pair: then 1"*)
       
    93 	     val popt = ef op_ t thy;
       
    94 	 in case popt of 
       
    95 		Some (id,_) => ((*writeln("@@@ get_pair: t -> Some "^id);*)
       
    96 				popt)
       
    97 	      | None => 
       
    98 		let (*val _= writeln"@@@ get_pair: t -> None"*)
       
    99 		    val popt = get_pair thy op_ ef t1
       
   100 		in case popt of 
       
   101 		       Some (id,_) => 
       
   102 		       ((*writeln("@@@ get_pair: t1 -> Some "^id);*)
       
   103 			popt)
       
   104 		     | None => ((*writeln"@@@ get_pair: t1 -> None";*)
       
   105 				get_pair thy op_ ef t2) 
       
   106 		end
       
   107 	 end
       
   108      else (*search subterms*)
       
   109 	 let (*val _= writeln"@@@ get_pair: t else"*)
       
   110 	     val popt = get_pair thy op_ ef t1
       
   111 	 in case popt of 
       
   112 		Some (id,_) => ((*writeln("@@@ get_pair: t1 -> Some "^id);*)
       
   113 				popt)
       
   114 	      | None => ((*writeln"@@@ get_pair: t1 else -> None";*)
       
   115 			 get_pair thy op_ ef t2) 
       
   116 	 end)
       
   117 
       
   118   | get_pair thy op_ ef (Const _) = None
       
   119   | get_pair thy op_ ef (Free _) = None
       
   120   | get_pair thy op_ ef (Var _) = None
       
   121   | get_pair thy op_ ef (Bound _) = None
       
   122   | get_pair thy op_ ef (Abs(a,T,body)) = get_pair thy op_ ef body
       
   123   | get_pair thy op_ ef (t1$t2) = 
       
   124     let (*val _= writeln"### get_pair: t1 $ t2"*)
       
   125 	val popt = get_pair thy op_ ef t1
       
   126     in case popt of 
       
   127 	   Some _ => popt
       
   128 	 | None => ((*writeln"### get_pair: t1 $ t2 -> None";*)
       
   129 		    get_pair thy op_ ef t2) 
       
   130     end;
       
   131  (*
       
   132 >  val t = (term_of o the o (parse thy)) "#3 + #4";
       
   133 >  val eval_fn = the (assoc (!eval_list, "op +"));
       
   134 >  val (Some (id,t')) = get_pair thy "op +" eval_fn t;
       
   135 >  Sign.string_of_term (sign_of thy) t';
       
   136 >  atomty thy t';
       
   137 > 
       
   138 >  val t = (term_of o the o (parse thy)) "(a + #3) + #4";
       
   139 >  val (Some (id,t')) = get_pair thy "op +" eval_fn t;
       
   140 >  Sign.string_of_term (sign_of thy) t';
       
   141 > 
       
   142 >  val t = (term_of o the o (parse thy)) "#3 + (#4 + (a::real))";
       
   143 >  val (Some (id,t')) = get_pair thy "op +" eval_fn t;
       
   144 >  Sign.string_of_term (sign_of thy) t';
       
   145 > 
       
   146 >  val t = (term_of o the o (parse thy)) "x = #5 * (#3 + (#4 + a))";
       
   147 >  atomty thy t;
       
   148 >  val (Some (id,t')) = get_pair thy "op +" eval_fn t;
       
   149 >  Sign.string_of_term (sign_of thy) t';
       
   150 >  val it = "#3 + (#4 + a) = #7 + a" : string
       
   151 >
       
   152 >
       
   153 >  val t = (term_of o the o (parse thy)) "#-4//#-2";
       
   154 >  val eval_fn = the (assoc (!eval_list, "cancel"));
       
   155 >  val (Some (id,t')) = get_pair thy "cancel" eval_fn t;
       
   156 >  Sign.string_of_term (sign_of thy) t';
       
   157 > 
       
   158 >  val t = (term_of o the o (parse thy)) "#2^^^#3";
       
   159 >  eval_binop "xxx" "pow" t thy;
       
   160 >  val eval_fn = (eval_binop "xxx")
       
   161 >		 : string -> term -> theory -> (string * term) option;
       
   162 >  val Some (id,t') = get_pair thy "pow" eval_fn t;
       
   163 >  Sign.string_of_term (sign_of thy) t';
       
   164 >  val eval_fn = the (assoc (!eval_list, "pow"));
       
   165 >  val (Some (id,t')) = get_pair thy "pow" eval_fn t;
       
   166 >  Sign.string_of_term (sign_of thy) t';
       
   167 > 
       
   168 >  val t = (term_of o the o (parse thy)) "x = #0 + #-1 * #-4";
       
   169 >  val eval_fn = the (assoc (!eval_list, "op *"));
       
   170 >  val (Some (id,t')) = get_pair thy "op *" eval_fn t;
       
   171 >  Sign.string_of_term (sign_of thy) t';
       
   172 > 
       
   173 >  val t = (term_of o the o (parse thy)) "#0 < #4";
       
   174 >  val eval_fn = the (assoc (!eval_list, "op <"));
       
   175 >  val (Some (id,t')) = get_pair thy "op <" eval_fn t;
       
   176 >  Sign.string_of_term (sign_of thy) t';
       
   177 >  val t = (term_of o the o (parse thy)) "#0 < #-4";
       
   178 >  val (Some (id,t')) = get_pair thy "op <" eval_fn t;
       
   179 >  Sign.string_of_term (sign_of thy) t';
       
   180 > 
       
   181 >  val t = (term_of o the o (parse thy)) "#3 is_const";
       
   182 >  val eval_fn = the (assoc (!eval_list, "is'_const"));
       
   183 >  val (Some (id,t')) = get_pair thy "is'_const" eval_fn t;
       
   184 >  Sign.string_of_term (sign_of thy) t';
       
   185 >  val t = (term_of o the o (parse thy)) "a is_const";
       
   186 >  val (Some (id,t')) = get_pair thy "is'_const" eval_fn t;
       
   187 >  Sign.string_of_term (sign_of thy) t';
       
   188 > 
       
   189 >  val t = (term_of o the o (parse thy)) "#6//(#8::real)";
       
   190 >  val eval_fn = the (assoc (!eval_list, "cancel"));
       
   191 >  val (Some (id,t')) = get_pair thy "cancel" eval_fn t;
       
   192 >  Sign.string_of_term (sign_of thy) t';
       
   193 > 
       
   194 >  val t = (term_of o the o (parse thy)) "sqrt #12";
       
   195 >  val eval_fn = the (assoc (!eval_list, "SqRoot.sqrt"));
       
   196 >  val (Some (id,t')) = get_pair thy "SqRoot.sqrt" eval_fn t;
       
   197 >  Sign.string_of_term (sign_of thy) t';
       
   198 >  val it = "sqrt #12 = #2 * sqrt #3 " : string
       
   199 >
       
   200 >  val t = (term_of o the o (parse thy)) "sqrt #9";
       
   201 >  val (Some (id,t')) = get_pair thy "SqRoot.sqrt" eval_fn t;
       
   202 >  Sign.string_of_term (sign_of thy) t';
       
   203 >
       
   204 >  val t = (term_of o the o (parse thy)) "Nth #2 [#11,#22,#33]";
       
   205 >  val eval_fn = the (assoc (!eval_list, "Tools.Nth"));
       
   206 >  val (Some (id,t')) = get_pair thy "Tools.Nth" eval_fn t;
       
   207 >  Sign.string_of_term (sign_of thy) t';
       
   208 *)
       
   209 
       
   210 (* val ((op_, eval_fn),ct)=(cc,pre);
       
   211    (get_calculation_ Isac.thy (op_, eval_fn) ct) handle e => print_exn e;
       
   212    parse thy ""
       
   213    *)
       
   214 (*.apply ONLY to (app_num_tr'2 term), app_num_tr'2 (- 4711) --> (-4711).*)
       
   215 fun get_calculation_ thy (op_, eval_fn) ct =
       
   216   case get_pair thy op_ eval_fn ct of
       
   217 	 None => ((*writeln("@@@ get_calculation: None, op_="^op_);
       
   218 		  writeln("@@@ get_calculation: ct= ");atomty thy ct;*)
       
   219 		  None)
       
   220        | Some (thmid,t) =>
       
   221 	   ((*writeln("@@@ get_calculation: None, op_="^op_);
       
   222 	    writeln("@@@ get_calculation: ct= ");atomty thy ct;*)
       
   223 	    Some (thmid, (make_thm o (cterm_of (sign_of thy))) t));
       
   224 (*
       
   225 > val ct = (the o (parse thy)) "#9 is_const";
       
   226 > get_calculation_ thy ("is'_const",the (assoc(!eval_list,"is'_const"))) ct;
       
   227 val it = Some ("is_const9_","(is_const 9 ) = True  [(is_const 9 ) = True]")
       
   228 
       
   229 > val ct = (the o (parse thy)) "sqrt #9";
       
   230 > get_calculation_ thy ("sqrt",the (assoc(!eval_list,"sqrt"))) ct;
       
   231 val it = Some ("sqrt_9_","sqrt 9  = 3  [sqrt 9  = 3]") : (string * thm) option
       
   232 
       
   233 > val ct = (the o (parse thy)) "#4<#4";
       
   234 > get_calculation_ thy ("op <",the (assoc(!eval_list,"op <"))) ct;fun is_no str = (hd o explode) str = "#";
       
   235 
       
   236 val it = Some ("less_5_4","(5 < 4) = False  [(5 < 4) = False]")
       
   237 
       
   238 > val ct = (the o (parse thy)) "a<#4";
       
   239 > get_calculation_ thy ("op <",the (assoc(!eval_list,"op <"))) ct;
       
   240 val it = None : (string * thm) option
       
   241 
       
   242 > val ct = (the o (parse thy)) "#5<=#4";
       
   243 > get_calculation_ thy ("op <=",the (assoc(!eval_list,"op <="))) ct;
       
   244 val it = Some ("less_equal_5_4","(5 <= 4) = False  [(5 <= 4) = False]")
       
   245 
       
   246 -------------------------------------------------------------------6.8.02:
       
   247  val thy = SqRoot.thy;
       
   248  val t = (term_of o the o (parse thy)) "1+2";
       
   249  get_calculation_ thy (the(assoc(!calc_list,"plus"))) t;
       
   250  val it = Some ("add_3_4","3 + 4 = 7  [3 + 4 = 7]") : (string * thm) option
       
   251 -------------------------------------------------------------------6.8.02:
       
   252  val t = (term_of o the o (parse thy)) "-1";
       
   253  atomty Isac.thy t;
       
   254  val t = (term_of o the o (parse thy)) "0";
       
   255  atomty Isac.thy t;
       
   256  val t = (term_of o the o (parse thy)) "1";
       
   257  atomty Isac.thy t;
       
   258  val t = (term_of o the o (parse thy)) "2";
       
   259  atomty Isac.thy t;
       
   260  val t = (term_of o the o (parse thy)) "999999999";
       
   261  atomty Isac.thy t;
       
   262 -------------------------------------------------------------------6.8.02:
       
   263 
       
   264 > val ct = (the o (parse thy)) "a+#3+#4";
       
   265 > get_calculation_ thy ("op +",the (assoc(!eval_list,"op +"))) ct;
       
   266 val it = Some ("add_3_4","a + 3 + 4 = a + 7  [a + 3 + 4 = a + 7]")
       
   267  
       
   268 > val ct = (the o (parse thy)) "#3+(#4+a)";
       
   269 > get_calculation_ thy ("op +",the (assoc(!eval_list,"op +"))) ct;
       
   270 val it = Some ("add_3_4","3 + (4 + a) = 7 + a  [3 + (4 + a) = 7 + a]")
       
   271  
       
   272 > val ct = (the o (parse thy)) "a+(#3+#4)+#5";
       
   273 > get_calculation_ thy ("op +",the (assoc(!eval_list,"op +"))) ct;
       
   274 val it = Some ("add_3_4","3 + 4 = 7  [3 + 4 = 7]") : (string * thm) option
       
   275 
       
   276 > val ct = (the o (parse thy)) "#3*(#4*a)";
       
   277 > get_calculation_ thy ("op *",the (assoc(!eval_list,"op *"))) ct;
       
   278 val it = Some ("mult_3_4","3 * (4 * a) = 12 * a  [3 * (4 * a) = 12 * a]")
       
   279 
       
   280 > val ct = (the o (parse thy)) "#3 + #4^^^#2 + #5";
       
   281 > get_calculation_ thy ("pow",the (assoc(!eval_list,"pow"))) ct;
       
   282 val it = Some ("4_(+2)","4 ^ 2 = 16  [4 ^ 2 = 16]") : (string * thm) option
       
   283 
       
   284 > val ct = (the o (parse thy)) "#-4//#-2";
       
   285 > get_calculation_ thy ("cancel",the (assoc(!eval_list,"cancel"))) ct;
       
   286 val it = Some ("cancel_(-4)_(-2)","(-4) // (-2) = (+2)  [(-4) // (-2) = (+2)]")
       
   287 
       
   288 > val ct = (the o (parse thy)) "#6//#-8";
       
   289 > get_calculation_ thy ("cancel",the (assoc(!eval_list,"cancel"))) ct;
       
   290 val it = Some ("cancel_6_(-8)","6 // (-8) = (-3) // 4  [6 // (-8) = (-3) // 4]")
       
   291 
       
   292 *) 
       
   293 
       
   294 
       
   295 (*
       
   296 > val ct = (the o (parse thy)) "a + 3*4";
       
   297 > applicable "calculate" (Calc("op *", "mult_")) ct;
       
   298 val it = Some "3 * 4 = 12  [3 * 4 = 12]" : thm option
       
   299 
       
   300 --------------------------
       
   301 > val ct = (the o (parse thy)) "3 =!= 3";
       
   302 > val (thmid, thm) = the (get_calculation_ thy "Atools.ident" ct);
       
   303 val thm = "(3 =!= 3) = True  [(3 =!= 3) = True]" : thm
       
   304 
       
   305 > val ct = (the o (parse thy)) "~ (3 =!= 3)";
       
   306 > val (thmid, thm) = the (get_calculation_ thy "Atools.ident" ct);
       
   307 val thm = "(3 =!= 3) = True  [(3 =!= 3) = True]" : thm
       
   308 
       
   309 > val ct = (the o (parse thy)) "3 =!= 4";
       
   310 > val (thmid, thm) = the (get_calculation_ thy "Atools.ident" ct);
       
   311 val thm = "(3 =!= 4) = False  [(3 =!= 4) = False]" : thm
       
   312 
       
   313 > val ct = (the o (parse thy)) "( 4 + (4 * x + x ^ 2) =!= (+0))";
       
   314 > val (thmid, thm) = the (get_calculation_ thy "Atools.ident" ct);
       
   315   "(4 + (4 * x + x ^ 2) =!= (+0)) = False"
       
   316 
       
   317 > val ct = (the o (parse thy)) "~ ( 4 + (4 * x + x ^ 2) =!= (+0))";
       
   318 > val (thmid, thm) = the (get_calculation_ thy "Atools.ident" ct);
       
   319   "(4 + (4 * x + x ^ 2) =!= (+0)) = False"
       
   320 
       
   321 > val ct = (the o (parse thy)) "~ ( 4 + (4 * x + x ^ 2) =!= (+0))";
       
   322 > val rls = eval_rls;
       
   323 > val (ct,_) = the (rewrite_set_ thy false rls ct);
       
   324 val ct = "True" : cterm
       
   325 --------------------------
       
   326 *)
       
   327 
       
   328 
       
   329 (*.substitute bdv in an rls and leave Calc as they are.(*28.10.02*)
       
   330 fun inst_thm' subs (Thm (id, thm)) = 
       
   331     Thm (id, (*read_instantiate throws: *** No such variable in term: ?bdv*)
       
   332 	 (read_instantiate subs thm) handle _ => thm)
       
   333   | inst_thm' _ calc = calc; 
       
   334 fun inst_thm' (subs as (bdv,_)::_) (Thm (id, thm)) = 
       
   335     Thm (id, (writeln("@@@ inst_thm': thm= "^(string_of_thm thm));
       
   336 	      if bdv mem (vars_str o #prop o rep_thm) thm
       
   337 	     then (writeln("@@@ inst_thm': read_instantiate, thm="^((string_of_thm thm)));
       
   338 		   read_instantiate subs thm)
       
   339 	     else (writeln("@@@ inst_thm': not mem.. "^bdv);
       
   340 		   thm)))
       
   341   | inst_thm' _ calc = calc; 
       
   342 
       
   343 fun instantiate_rls subs 
       
   344   (Rls{preconds=preconds,rew_ord=rew_ord,erls=ev,srls=sr,calc=ca,
       
   345        asm_thm=at,rules=rules,scr=scr}:rls) =
       
   346   (Rls{preconds=preconds,rew_ord=rew_ord,erls=ev,srls=sr,calc=ca,
       
   347        asm_thm=at,scr=scr,
       
   348    rules = map (inst_thm' subs) rules}:rls);---------------------------*)
       
   349 
       
   350 
       
   351 
       
   352 (** rewriting: ordered, conditional **)
       
   353 
       
   354 fun mk_rule (prems,l,r) = 
       
   355     Trueprop $ (list_implies (prems, mk_equality (l,r)));
       
   356 
       
   357 (* 'norms' a rule, e.g.
       
   358 (*1*) a = 1 ==> a*(b+c) = b+c 
       
   359                 =>  a = 1 ==> a*(b+c) = b+c          no change
       
   360 (*2*) t = t     =>  (t=t) = True                        !!
       
   361 (*3*) [| k < l; m + l = k + n |] ==> m < n
       
   362 	        =>  [| k<l; m+l=k+n |] ==> m < n = True !! *)
       
   363 (* val it = fn : term -> term *)
       
   364 fun norm rule =
       
   365   let
       
   366     val (prems,concl)=(map strip_trueprop(Logic.strip_imp_prems rule),
       
   367 		       (strip_trueprop o  Logic.strip_imp_concl)rule)
       
   368   in if is_equality concl then 
       
   369       let val (l,r) = dest_equals' concl
       
   370       in if l = r then 
       
   371 	 (*2*) mk_rule(prems,concl,true_as_term)
       
   372 	 else (*1*) rule end
       
   373      else (*3*) mk_rule(prems,concl,true_as_term)
       
   374   end;
       
   375 
       
   376 
       
   377 
       
   378 
       
   379 
       
   380 
       
   381 
       
   382