test/Tools/isac/MathEngBasic/rewrite.sml
changeset 59868 d77aa0992e0f
parent 59867 bb153a08645b
child 59870 0042fe0bc764
equal deleted inserted replaced
59867:bb153a08645b 59868:d77aa0992e0f
   169 val (thy, i, bdv, tless, rls, put_asm, lrd, r, t) = 
   169 val (thy, i, bdv, tless, rls, put_asm, lrd, r, t) = 
   170     (thy, 0, [], dummy_ord, Rule_Set.empty, true, [], (Thm.prop_of thm), tm);
   170     (thy, 0, [], dummy_ord, Rule_Set.empty, true, [], (Thm.prop_of thm), tm);
   171 "----- step 1: LHS, RHS of rule";
   171 "----- step 1: LHS, RHS of rule";
   172      val (LHS, RHS) = (HOLogic.dest_eq o HOLogic.dest_Trueprop
   172      val (LHS, RHS) = (HOLogic.dest_eq o HOLogic.dest_Trueprop
   173                        o Logic.strip_imp_concl) r;
   173                        o Logic.strip_imp_concl) r;
   174 UnparseC.term2str r = "?b \<noteq> (0::?'a) \<Longrightarrow> ?b / (?a * ?b) = (1::?'a) / ?a";
   174 UnparseC.term r = "?b \<noteq> (0::?'a) \<Longrightarrow> ?b / (?a * ?b) = (1::?'a) / ?a";
   175 UnparseC.term2str LHS = "?b / (?a * ?b)"; UnparseC.term2str RHS = "(1::?'a) / ?a";
   175 UnparseC.term LHS = "?b / (?a * ?b)"; UnparseC.term RHS = "(1::?'a) / ?a";
   176 "----- step 2: the rule instantiated";
   176 "----- step 2: the rule instantiated";
   177      val r' = Envir.subst_term 
   177      val r' = Envir.subst_term 
   178                   (Pattern.match thy (LHS, t) (Vartab.empty, Vartab.empty)) r;
   178                   (Pattern.match thy (LHS, t) (Vartab.empty, Vartab.empty)) r;
   179 UnparseC.term2str r' = "x \<noteq> 0 \<Longrightarrow> x / (2 * x) = 1 / 2";
   179 UnparseC.term r' = "x \<noteq> 0 \<Longrightarrow> x / (2 * x) = 1 / 2";
   180 "----- step 3: get the (instantiated) assumption(s)";
   180 "----- step 3: get the (instantiated) assumption(s)";
   181      val p' = (fst o Logic.strip_prems) (Logic.count_prems r', [], r');
   181      val p' = (fst o Logic.strip_prems) (Logic.count_prems r', [], r');
   182 UnparseC.term2str (hd p') = "x \<noteq> 0";
   182 UnparseC.term (hd p') = "x \<noteq> 0";
   183 "=====vvv make asms without Trueprop ---vvv";
   183 "=====vvv make asms without Trueprop ---vvv";
   184      val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) 
   184      val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) 
   185                                              (Logic.count_prems r', [], r'));
   185                                              (Logic.count_prems r', [], r'));
   186 case p' of
   186 case p' of
   187     [Const ("HOL.Not", _) $ (Const ("HOL.eq", _) 
   187     [Const ("HOL.Not", _) $ (Const ("HOL.eq", _) 
   189   | _ => error "rewrite.sml assumption changed";
   189   | _ => error "rewrite.sml assumption changed";
   190 "=====^^^ make asms without Trueprop ---^^^";
   190 "=====^^^ make asms without Trueprop ---^^^";
   191 "----- step 4: get the (instantiated) RHS";
   191 "----- step 4: get the (instantiated) RHS";
   192      val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop 
   192      val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop 
   193                o Logic.strip_imp_concl) r';
   193                o Logic.strip_imp_concl) r';
   194 UnparseC.term2str t' = "1 / 2";
   194 UnparseC.term t' = "1 / 2";
   195 
   195 
   196 "----------- step through 'fun rewrite_terms_'  ---------";
   196 "----------- step through 'fun rewrite_terms_'  ---------";
   197 "----------- step through 'fun rewrite_terms_'  ---------";
   197 "----------- step through 'fun rewrite_terms_'  ---------";
   198 "----------- step through 'fun rewrite_terms_'  ---------";
   198 "----------- step through 'fun rewrite_terms_'  ---------";
   199 "----- step 0: args for rewrite_terms_, local fun";
   199 "----- step 0: args for rewrite_terms_, local fun";
   207 "----- step 3: step through rew_sub -- inefficient: goes into subterms";
   207 "----- step 3: step through rew_sub -- inefficient: goes into subterms";
   208 
   208 
   209 
   209 
   210 val SOME (t', _) = rewrite_terms_ thy dummy_ord Rule_Set.Empty equs t;
   210 val SOME (t', _) = rewrite_terms_ thy dummy_ord Rule_Set.Empty equs t;
   211 writeln "----------- rewrite_terms_  1---------------------------";
   211 writeln "----------- rewrite_terms_  1---------------------------";
   212 if UnparseC.term2str t' = "M_b 0 = -1 * q_0 * 0 ^^^ 2 / 2 + 0 * c + c_2" then ()
   212 if UnparseC.term t' = "M_b 0 = -1 * q_0 * 0 ^^^ 2 / 2 + 0 * c + c_2" then ()
   213 else error "rewrite.sml rewrite_terms_ [x = 0]";
   213 else error "rewrite.sml rewrite_terms_ [x = 0]";
   214 
   214 
   215 val equs = [str2term "M_b 0 = 0"];
   215 val equs = [str2term "M_b 0 = 0"];
   216 val t = str2term "M_b 0 = -1 * q_0 * 0 ^^^ 2 / 2 + 0 * c + c_2";
   216 val t = str2term "M_b 0 = -1 * q_0 * 0 ^^^ 2 / 2 + 0 * c + c_2";
   217 val SOME (t', _) = rewrite_terms_ thy dummy_ord Rule_Set.Empty equs t;
   217 val SOME (t', _) = rewrite_terms_ thy dummy_ord Rule_Set.Empty equs t;
   218 writeln "----------- rewrite_terms_  2---------------------------";
   218 writeln "----------- rewrite_terms_  2---------------------------";
   219 if UnparseC.term2str t' = "0 = -1 * q_0 * 0 ^^^ 2 / 2 + 0 * c + c_2" then ()
   219 if UnparseC.term t' = "0 = -1 * q_0 * 0 ^^^ 2 / 2 + 0 * c + c_2" then ()
   220 else error "rewrite.sml rewrite_terms_ [M_b 0 = 0]";
   220 else error "rewrite.sml rewrite_terms_ [M_b 0 = 0]";
   221 
   221 
   222 val equs = [str2term "x = 0", str2term"M_b 0 = 0"];
   222 val equs = [str2term "x = 0", str2term"M_b 0 = 0"];
   223 val t = str2term "M_b x = -1 * q_0 * x ^^^ 2 / 2 + x * c + c_2";
   223 val t = str2term "M_b x = -1 * q_0 * x ^^^ 2 / 2 + x * c + c_2";
   224 val SOME (t', _) = rewrite_terms_ thy dummy_ord Rule_Set.Empty equs t;
   224 val SOME (t', _) = rewrite_terms_ thy dummy_ord Rule_Set.Empty equs t;
   225 writeln "----------- rewrite_terms_  3---------------------------";
   225 writeln "----------- rewrite_terms_  3---------------------------";
   226 if UnparseC.term2str t' = "0 = -1 * q_0 * 0 ^^^ 2 / 2 + 0 * c + c_2" then ()
   226 if UnparseC.term t' = "0 = -1 * q_0 * 0 ^^^ 2 / 2 + 0 * c + c_2" then ()
   227 else error "rewrite.sml rewrite_terms_ [x = 0, M_b 0 = 0]";
   227 else error "rewrite.sml rewrite_terms_ [x = 0, M_b 0 = 0]";
   228 
   228 
   229 
   229 
   230 "----------- rewrite_inst_ bdvs -------------------------";
   230 "----------- rewrite_inst_ bdvs -------------------------";
   231 "----------- rewrite_inst_ bdvs -------------------------";
   231 "----------- rewrite_inst_ bdvs -------------------------";
   243 			      [(Num_Calc ("EqSystem.occur'_exactly'_in", 
   243 			      [(Num_Calc ("EqSystem.occur'_exactly'_in", 
   244 				      eval_occur_exactly_in 
   244 				      eval_occur_exactly_in 
   245 					  "#eval_occur_exactly_in_"))
   245 					  "#eval_occur_exactly_in_"))
   246 			       ]) 
   246 			       ]) 
   247 		  false bdvs (num_str @{separate_bdvs_add) t;
   247 		  false bdvs (num_str @{separate_bdvs_add) t;
   248 (writeln o UnparseC.term2str) t;
   248 (writeln o UnparseC.term) t;
   249 if UnparseC.term2str t = "L * c_3 + c_4 = 0 + -1 * (-1 * (q_0 * L ^^^ 2) / 2)"
   249 if UnparseC.term t = "L * c_3 + c_4 = 0 + -1 * (-1 * (q_0 * L ^^^ 2) / 2)"
   250 then () else error "rewrite.sml rewrite_inst_ bdvs";
   250 then () else error "rewrite.sml rewrite_inst_ bdvs";
   251 > trace_rewrite:=true;
   251 > trace_rewrite:=true;
   252 trace_rewrite:=false;--------------------------------------------*)
   252 trace_rewrite:=false;--------------------------------------------*)
   253 
   253 
   254 
   254 
   303 val t = @{term "1 / EI * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2)"};
   303 val t = @{term "1 / EI * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2)"};
   304 "----- rewrite_set_inst_ norm_Rational_noadd_fractions--";
   304 "----- rewrite_set_inst_ norm_Rational_noadd_fractions--";
   305 val subs = [(@{term "bdv"}, @{term  "x"})];
   305 val subs = [(@{term "bdv"}, @{term  "x"})];
   306 val rls = norm_Rational_noadd_fractions;
   306 val rls = norm_Rational_noadd_fractions;
   307 val SOME (t', asms) = rewrite_set_inst_ thy true subs rls t;
   307 val SOME (t', asms) = rewrite_set_inst_ thy true subs rls t;
   308 if UnparseC.term2str t' = "1 / EI * (L * q_0 * x / 2 + -1 * q_0 * 1 * x ^^^ 2 / 2)" andalso
   308 if UnparseC.term t' = "1 / EI * (L * q_0 * x / 2 + -1 * q_0 * 1 * x ^^^ 2 / 2)" andalso
   309   UnparseC.terms2str asms = "[]" then {}
   309   UnparseC.terms asms = "[]" then {}
   310 else error "this was NONE with Isabelle2013-2 ?!?"
   310 else error "this was NONE with Isabelle2013-2 ?!?"
   311 "----- rewrite_ rat_mult_poly_r--------------------------";
   311 "----- rewrite_ rat_mult_poly_r--------------------------";
   312 val thm = @{thm rat_mult_poly_r};
   312 val thm = @{thm rat_mult_poly_r};
   313          "?c::real is_polyexp ==> (?a::real) / (?b::real) * ?c = ?a * ?c / ?b";
   313          "?c::real is_polyexp ==> (?a::real) / (?b::real) * ?c = ?a * ?c / ?b";
   314 val erls = Rule_Set.append_rules "Rule_Set.empty-is_polyexp" Rule_Set.empty 
   314 val erls = Rule_Set.append_rules "Rule_Set.empty-is_polyexp" Rule_Set.empty 
   326 writeln "===== maximally simplified example =====================";
   326 writeln "===== maximally simplified example =====================";
   327 val t = @{term "a / b * (x / x ^^^ 2)"};
   327 val t = @{term "a / b * (x / x ^^^ 2)"};
   328          "?c::real is_polyexp ==> (?a::real) / (?b::real) * ?c = ?a * ?c / ?b";
   328          "?c::real is_polyexp ==> (?a::real) / (?b::real) * ?c = ?a * ?c / ?b";
   329 writeln "----- rewrite_set_inst_ norm_Rational_noadd_fractions--";
   329 writeln "----- rewrite_set_inst_ norm_Rational_noadd_fractions--";
   330 val (*NONE*) SOME (t', asm) = rewrite_set_inst_ thy true subs rls t;
   330 val (*NONE*) SOME (t', asm) = rewrite_set_inst_ thy true subs rls t;
   331 UnparseC.term2str t' = "a * x / (b * x ^^^ 2)"; (*rew. by thm outside test case*)
   331 UnparseC.term t' = "a * x / (b * x ^^^ 2)"; (*rew. by thm outside test case*)
   332 (*checked visually: trace_rewrite looks like above for 2009*)
   332 (*checked visually: trace_rewrite looks like above for 2009*)
   333 
   333 
   334 writeln "----- rewrite_ rat_mult_poly_r--------------------------";
   334 writeln "----- rewrite_ rat_mult_poly_r--------------------------";
   335 val NONE (*SOME (t'', _)*) = rewrite_ thy dummy_ord erls true thm t;
   335 val NONE (*SOME (t'', _)*) = rewrite_ thy dummy_ord erls true thm t;
   336 (*t' = t''; (*false because of further rewrites in t'*)*)
   336 (*t' = t''; (*false because of further rewrites in t'*)*)
   352 val precond = parse_patt thy "is_polynomial (?l::real)";(*no infix def*)
   352 val precond = parse_patt thy "is_polynomial (?l::real)";(*no infix def*)
   353 val precond = parse_patt thy "(?l::real) is_expanded"; 
   353 val precond = parse_patt thy "(?l::real) is_expanded"; 
   354 
   354 
   355 val inst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
   355 val inst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
   356 val preinst = Envir.subst_term inst precond;
   356 val preinst = Envir.subst_term inst precond;
   357 UnparseC.term2str preinst;
   357 UnparseC.term preinst;
   358 
   358 
   359 "===== Rational.thy: cancel ===";
   359 "===== Rational.thy: cancel ===";
   360 (* pat matched with the current term gives an environment 
   360 (* pat matched with the current term gives an environment 
   361    (or not: hen the Rrls not applied);
   361    (or not: hen the Rrls not applied);
   362    if pre1 and pre2 evaluate to @{term True} in this environment,
   362    if pre1 and pre2 evaluate to @{term True} in this environment,
   368 val erls = rational_erls;
   368 val erls = rational_erls;
   369 (* erls got from Rrls {erls, prepat, scr = Rfuns {normal_form, ...}, ...} *)
   369 (* erls got from Rrls {erls, prepat, scr = Rfuns {normal_form, ...}, ...} *)
   370 
   370 
   371 val subst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
   371 val subst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
   372 val asms = map (Envir.subst_term subst) pres;
   372 val asms = map (Envir.subst_term subst) pres;
   373 if UnparseC.terms2str asms = "[\"a + b is_expanded\",\"c is_expanded\"]"
   373 if UnparseC.terms asms = "[\"a + b is_expanded\",\"c is_expanded\"]"
   374 then () else error "rewrite.sml: prepat cancel subst";
   374 then () else error "rewrite.sml: prepat cancel subst";
   375 if ([], true) = eval__true thy 0 asms [] erls
   375 if ([], true) = eval__true thy 0 asms [] erls
   376 then () else error "rewrite.sml: prepat cancel eval__true";
   376 then () else error "rewrite.sml: prepat cancel eval__true";
   377 
   377 
   378 "===== Rational.thy: add_fractions_p ===";
   378 "===== Rational.thy: add_fractions_p ===";
   400 		      [Num_Calc ("Poly.is'_multUnordered", 
   400 		      [Num_Calc ("Poly.is'_multUnordered", 
   401                              eval_is_multUnordered "")];
   401                              eval_is_multUnordered "")];
   402 
   402 
   403 val subst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
   403 val subst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
   404 val asms = map (Envir.subst_term subst) pres;
   404 val asms = map (Envir.subst_term subst) pres;
   405 if UnparseC.terms2str asms = "[\"x ^^^ 2 * x is_multUnordered\"]"
   405 if UnparseC.terms asms = "[\"x ^^^ 2 * x is_multUnordered\"]"
   406 then () else error "rewrite.sml: prepat order_mult_ subst";
   406 then () else error "rewrite.sml: prepat order_mult_ subst";
   407 if ([], true) = eval__true thy 0 asms [] erls
   407 if ([], true) = eval__true thy 0 asms [] erls
   408 then () else error "rewrite.sml: prepat order_mult_ eval__true";
   408 then () else error "rewrite.sml: prepat order_mult_ eval__true";
   409 
   409 
   410 
   410 
   425   | _ => error "rewrite.sml diff. eval_is_multUnordered 2b";
   425   | _ => error "rewrite.sml diff. eval_is_multUnordered 2b";
   426 
   426 
   427 tracing "----- begin rewrite x^^^2 * x ---"; trace_rewrite := false;
   427 tracing "----- begin rewrite x^^^2 * x ---"; trace_rewrite := false;
   428 val SOME (t', _) = rewrite_set_ thy true order_mult_ t;
   428 val SOME (t', _) = rewrite_set_ thy true order_mult_ t;
   429 tracing "----- end rewrite x^^^2 * x ---"; trace_rewrite := false;
   429 tracing "----- end rewrite x^^^2 * x ---"; trace_rewrite := false;
   430 if UnparseC.term2str t' = "x * x ^^^ 2" then ()
   430 if UnparseC.term t' = "x * x ^^^ 2" then ()
   431 else error "rewrite.sml Poly.is'_multUnordered doesn't work";
   431 else error "rewrite.sml Poly.is'_multUnordered doesn't work";
   432 
   432 
   433 (* for achieving the previous result, the following code was taken apart *)
   433 (* for achieving the previous result, the following code was taken apart *)
   434 "----- rewrite__set_ ---";
   434 "----- rewrite__set_ ---";
   435 val (thy, i, _, _, (rrls as Rrls _), t) = (thy, 0, true, [], order_mult_, tm);
   435 val (thy, i, _, _, (rrls as Rrls _), t) = (thy, 0, true, [], order_mult_, tm);
   453 	    in scan_ chk prepat end;
   453 	    in scan_ chk prepat end;
   454 
   454 
   455 	(*.apply the normal_form of a rev-set.*)
   455 	(*.apply the normal_form of a rev-set.*)
   456 	fun app_rev' thy (Rrls{erls,prepat,scr=Rfuns{normal_form,...},...}) t =
   456 	fun app_rev' thy (Rrls{erls,prepat,scr=Rfuns{normal_form,...},...}) t =
   457 	    if chk_prepat thy erls prepat t
   457 	    if chk_prepat thy erls prepat t
   458 	    then ((*tracing("### app_rev': t = "^(UnparseC.term2str t));*)
   458 	    then ((*tracing("### app_rev': t = "^(UnparseC.term t));*)
   459                   normal_form t)
   459                   normal_form t)
   460 	    else NONE;
   460 	    else NONE;
   461 (*fixme val NONE = app_rev' thy rrls t;*)
   461 (*fixme val NONE = app_rev' thy rrls t;*)
   462 "----- app_rev' ---";
   462 "----- app_rev' ---";
   463 val (thy, Rrls{erls,prepat,scr=Rfuns{normal_form,...},...}, t) = (thy, rrls, t);
   463 val (thy, Rrls{erls,prepat,scr=Rfuns{normal_form,...},...}, t) = (thy, rrls, t);
   488 					    (Vartab.empty, Vartab.empty);
   488 					    (Vartab.empty, Vartab.empty);
   489 
   489 
   490 (*fixme: asms = ["p is_multUnordered"]...instantiate*)
   490 (*fixme: asms = ["p is_multUnordered"]...instantiate*)
   491 "----- eval__true ---";
   491 "----- eval__true ---";
   492 val asms = (map (Envir.subst_term subst) pres);
   492 val asms = (map (Envir.subst_term subst) pres);
   493 if UnparseC.terms2str asms = "[\"x ^^^ 2 * x is_multUnordered\"]" then ()
   493 if UnparseC.terms asms = "[\"x ^^^ 2 * x is_multUnordered\"]" then ()
   494 else error "rewrite.sml: diff. is_multUnordered, asms";
   494 else error "rewrite.sml: diff. is_multUnordered, asms";
   495 val (thy, i, asms, bdv, rls) = 
   495 val (thy, i, asms, bdv, rls) = 
   496     (thy, (i+1), [str2term "(x^^^2 * x) is_multUnordered"], 
   496     (thy, (i+1), [str2term "(x^^^2 * x) is_multUnordered"], 
   497      [] : (term * term) list, erls);
   497      [] : (term * term) list, erls);
   498 case eval__true thy i asms bdv rls of 
   498 case eval__true thy i asms bdv rls of 
   539               (trace1 i (" try thm: " ^ thmid);
   539               (trace1 i (" try thm: " ^ thmid);
   540               case rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule.Rule_Set.rep) rls)
   540               case rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule.Rule_Set.rep) rls)
   541                   ((#erls o Rule.Rule_Set.rep) rls) put_asm thm ct of
   541                   ((#erls o Rule.Rule_Set.rep) rls) put_asm thm ct of
   542                 NONE => rew_once ruls asm ct apno thms
   542                 NONE => rew_once ruls asm ct apno thms
   543               | SOME (ct', asm') => 
   543               | SOME (ct', asm') => 
   544                 (trace1 i (" rewrites to: " ^ UnparseC.t2str thy ct');
   544                 (trace1 i (" rewrites to: " ^ UnparseC.term_thy thy ct');
   545                 rew_once ruls (union (op =) asm asm') ct' Appl (rul :: thms)))
   545                 rew_once ruls (union (op =) asm asm') ct' Appl (rul :: thms)))
   546                 (* once again try the same rule, e.g. associativity against "()"*)
   546                 (* once again try the same rule, e.g. associativity against "()"*)
   547           | Rule.Num_Calc (cc as (op_, _)) => 
   547           | Rule.Num_Calc (cc as (op_, _)) => 
   548             let val _= trace1 i (" try calc: " ^ op_ ^ "'")
   548             let val _= trace1 i (" try calc: " ^ op_ ^ "'")
   549               val ct = TermC.uminus_to_string ct (*WN190312: superfluous?*)
   549               val ct = TermC.uminus_to_string ct (*WN190312: superfluous?*)
   552               | SOME (_, thm') => 
   552               | SOME (_, thm') => 
   553                 let 
   553                 let 
   554                   val pairopt = rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule.Rule_Set.rep) rls)
   554                   val pairopt = rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule.Rule_Set.rep) rls)
   555                     ((#erls o Rule.Rule_Set.rep) rls) put_asm thm' ct;
   555                     ((#erls o Rule.Rule_Set.rep) rls) put_asm thm' ct;
   556                   val _ = if pairopt <> NONE then () else error ("rewrite_set_, rewrite_ \"" ^ 
   556                   val _ = if pairopt <> NONE then () else error ("rewrite_set_, rewrite_ \"" ^ 
   557                     Rule.string_of_thmI thm' ^ "\" " ^ UnparseC.t2str thy ct ^ " = NONE")
   557                     Rule.string_of_thmI thm' ^ "\" " ^ UnparseC.term_thy thy ct ^ " = NONE")
   558                   val _ = trace1 i (" calc. to: " ^ UnparseC.t2str thy ((fst o the) pairopt))
   558                   val _ = trace1 i (" calc. to: " ^ UnparseC.term_thy thy ((fst o the) pairopt))
   559                 in rew_once ruls asm ((fst o the) pairopt) Appl (rul :: thms) end
   559                 in rew_once ruls asm ((fst o the) pairopt) Appl (rul :: thms) end
   560             end
   560             end
   561           | Rule.Cal1 (cc as (op_, _)) => 
   561           | Rule.Cal1 (cc as (op_, _)) => 
   562             let val _= trace1 i (" try cal1: " ^ op_ ^ "'");
   562             let val _= trace1 i (" try cal1: " ^ op_ ^ "'");
   563               val ct = TermC.uminus_to_string ct
   563               val ct = TermC.uminus_to_string ct
   566               | SOME (_, thm') =>
   566               | SOME (_, thm') =>
   567                 let 
   567                 let 
   568                   val pairopt = rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule.Rule_Set.rep) rls)
   568                   val pairopt = rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule.Rule_Set.rep) rls)
   569                     ((#erls o Rule.Rule_Set.rep) rls) put_asm thm' ct;
   569                     ((#erls o Rule.Rule_Set.rep) rls) put_asm thm' ct;
   570                   val _ = if pairopt <> NONE then () else error ("rewrite_set_, rewrite_ \"" ^
   570                   val _ = if pairopt <> NONE then () else error ("rewrite_set_, rewrite_ \"" ^
   571                      Rule.string_of_thmI thm' ^ "\" " ^ UnparseC.t2str thy ct ^ " = NONE")
   571                      Rule.string_of_thmI thm' ^ "\" " ^ UnparseC.term_thy thy ct ^ " = NONE")
   572                   val _ = trace1 i (" cal1. to: " ^ UnparseC.t2str thy ((fst o the) pairopt))
   572                   val _ = trace1 i (" cal1. to: " ^ UnparseC.term_thy thy ((fst o the) pairopt))
   573                 in the pairopt end
   573                 in the pairopt end
   574             end
   574             end
   575           | Rule.Rls_ rls' => 
   575           | Rule.Rls_ rls' => 
   576             (case rewrite__set_ thy (i + 1) put_asm bdv rls' ct of
   576             (case rewrite__set_ thy (i + 1) put_asm bdv rls' ct of
   577               SOME (t', asm') => rew_once ruls (union (op =) asm asm') t' Appl thms
   577               SOME (t', asm') => rew_once ruls (union (op =) asm asm') t' Appl thms
   578             | NONE => rew_once ruls asm ct apno thms)
   578             | NONE => rew_once ruls asm ct apno thms)
   579           | r => raise ERROR ("rew_once not appl. to \"" ^ Rule.Rule.to_string r ^ "\"");
   579           | r => raise ERROR ("rew_once not appl. to \"" ^ Rule.Rule.to_string r ^ "\"");
   580       val ruls = (#rules o Rule.Rule_Set.rep) rls;
   580       val ruls = (#rules o Rule.Rule_Set.rep) rls;
   581 (*    val _ = trace i (" rls: " ^ Rule_Set.id rls ^ " on: " ^ UnparseC.t2str thy ct)*)
   581 (*    val _ = trace i (" rls: " ^ Rule_Set.id rls ^ " on: " ^ UnparseC.term_thy thy ct)*)
   582       val (ct', asm') = rew_once ruls [] ct Noap ruls;
   582       val (ct', asm') = rew_once ruls [] ct Noap ruls;
   583 "~~~~~ fun rew_once , args:"; val (ruls, asm, ct, apno, (rul :: thms))
   583 "~~~~~ fun rew_once , args:"; val (ruls, asm, ct, apno, (rul :: thms))
   584   = (ruls, []:term list, ct, Noap, ruls);
   584   = (ruls, []:term list, ct, Noap, ruls);
   585            val Rule.Thm (thmid, thm) = (*case*) rul (*of*);
   585            val Rule.Thm (thmid, thm) = (*case*) rul (*of*);
   586 
   586 
   598   = (thy, i, bdv, tless, rls, put_asm, ([(*root of the term*)]: TermC.path),
   598   = (thy, i, bdv, tless, rls, put_asm, ([(*root of the term*)]: TermC.path),
   599 		  (((TermC.inst_bdv bdv) o Num_Calc.norm o #prop o Thm.rep_thm) thm), ct);
   599 		  (((TermC.inst_bdv bdv) o Num_Calc.norm o #prop o Thm.rep_thm) thm), ct);
   600     val (lhs, rhs) = (HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r
   600     val (lhs, rhs) = (HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r
   601     val r' = Envir.subst_term (Pattern.match thy (lhs, t) (Vartab.empty, Vartab.empty)) r
   601     val r' = Envir.subst_term (Pattern.match thy (lhs, t) (Vartab.empty, Vartab.empty)) r
   602 ;
   602 ;
   603 (*+*)if UnparseC.term2str r' =
   603 (*+*)if UnparseC.term r' =
   604 (*+*)  "\<lbrakk>9 * x + -6 * x ^^^ 2 + x ^^^ 3 \<noteq> 0; x \<noteq> 0\<rbrakk>\n" ^
   604 (*+*)  "\<lbrakk>9 * x + -6 * x ^^^ 2 + x ^^^ 3 \<noteq> 0; x \<noteq> 0\<rbrakk>\n" ^
   605 (*+*)  "\<Longrightarrow> ((3 + -1 * x + x ^^^ 2) /\n" ^
   605 (*+*)  "\<Longrightarrow> ((3 + -1 * x + x ^^^ 2) /\n" ^
   606 (*+*)  "                   (9 * x + -6 * x ^^^ 2 + x ^^^ 3) =\n" ^
   606 (*+*)  "                   (9 * x + -6 * x ^^^ 2 + x ^^^ 3) =\n" ^
   607 (*+*)  "                   1 / x) =\n" ^
   607 (*+*)  "                   1 / x) =\n" ^
   608 (*+*)  "                  ((3 + -1 * x + x ^^^ 2) * x =\n" ^
   608 (*+*)  "                  ((3 + -1 * x + x ^^^ 2) * x =\n" ^
   609 (*+*)  "                   1 * (9 * x + -6 * x ^^^ 2 + x ^^^ 3))"
   609 (*+*)  "                   1 * (9 * x + -6 * x ^^^ 2 + x ^^^ 3))"
   610 (*+*)then () else error "instantiated rule CHANGED";
   610 (*+*)then () else error "instantiated rule CHANGED";
   611 
   611 
   612     val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) (Logic.count_prems r', [], r'))
   612     val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) (Logic.count_prems r', [], r'))
   613 ;
   613 ;
   614 (*+*)if map UnparseC.term2str p' = ["x \<noteq> 0", "9 * x + -6 * x ^^^ 2 + x ^^^ 3 \<noteq> 0"]
   614 (*+*)if map UnparseC.term p' = ["x \<noteq> 0", "9 * x + -6 * x ^^^ 2 + x ^^^ 3 \<noteq> 0"]
   615 (*+*)then () else error "stored assumptions CHANGED";
   615 (*+*)then () else error "stored assumptions CHANGED";
   616 
   616 
   617     val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r'
   617     val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r'
   618 ;
   618 ;
   619 (*+*)if UnparseC.term2str t' = "(3 + -1 * x + x ^^^ 2) * x = 1 * (9 * x + -6 * x ^^^ 2 + x ^^^ 3)"
   619 (*+*)if UnparseC.term t' = "(3 + -1 * x + x ^^^ 2) * x = 1 * (9 * x + -6 * x ^^^ 2 + x ^^^ 3)"
   620 (*+*)then () else error "rewritten term (an equality) CHANGED";
   620 (*+*)then () else error "rewritten term (an equality) CHANGED";
   621 
   621 
   622         val (simpl_p', nofalse) =
   622         val (simpl_p', nofalse) =
   623            eval__true thy (i + 1) p' bdv rls;
   623            eval__true thy (i + 1) p' bdv rls;
   624 "~~~~~ and eval__true , args:"; val (thy, i, asms, bdv, rls) = (thy, (i + 1), p', bdv, rls);
   624 "~~~~~ and eval__true , args:"; val (thy, i, asms, bdv, rls) = (thy, (i + 1), p', bdv, rls);
   625   (*if*) asms = [@{term True}] orelse asms = [] (*else*); 
   625   (*if*) asms = [@{term True}] orelse asms = [] (*else*); 
   626 
   626 
   627 (*+*)if map UnparseC.term2str asms = ["x \<noteq> 0", "9 * x + -6 * x ^^^ 2 + x ^^^ 3 \<noteq> 0"]
   627 (*+*)if map UnparseC.term asms = ["x \<noteq> 0", "9 * x + -6 * x ^^^ 2 + x ^^^ 3 \<noteq> 0"]
   628 (*+*)then () else error "asms before chk CHANGED";
   628 (*+*)then () else error "asms before chk CHANGED";
   629 
   629 
   630         fun chk indets [] = (indets, true) (*return asms<>True until false*)
   630         fun chk indets [] = (indets, true) (*return asms<>True until false*)
   631           | chk indets (a :: asms) =
   631           | chk indets (a :: asms) =
   632             (case rewrite__set_ thy (i + 1) false bdv rls a of
   632             (case rewrite__set_ thy (i + 1) false bdv rls a of
   661 
   661 
   662         (*this was False; vvvv--- means: indeterminate*)
   662         (*this was False; vvvv--- means: indeterminate*)
   663     val (* SOME (t, a') *)NONE = (*case*)
   663     val (* SOME (t, a') *)NONE = (*case*)
   664            rewrite__set_ thy (i + 1) false bdv rls a (*of*);
   664            rewrite__set_ thy (i + 1) false bdv rls a (*of*);
   665 
   665 
   666 (*+*)UnparseC.term2str a = "x \<noteq> 0"; (* rewrite__set_ \<rightarrow> @{term True} ----------------- SHOULD BE indet !*)
   666 (*+*)UnparseC.term a = "x \<noteq> 0"; (* rewrite__set_ \<rightarrow> @{term True} ----------------- SHOULD BE indet !*)
   667 (*
   667 (*
   668  :
   668  :
   669  ####  rls: rateq_erls on: x \<noteq> 0
   669  ####  rls: rateq_erls on: x \<noteq> 0
   670  :
   670  :
   671  #####  try calc: HOL.eq'    <<<------------------------------- here the error comes from
   671  #####  try calc: HOL.eq'    <<<------------------------------- here the error comes from
   715      val (lhss, rhss) = (HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r
   715      val (lhss, rhss) = (HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r
   716      val r' = Envir.subst_term (Pattern.match thy (lhss, t) (Vartab.empty, Vartab.empty)) r
   716      val r' = Envir.subst_term (Pattern.match thy (lhss, t) (Vartab.empty, Vartab.empty)) r
   717      val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) (Logic.count_prems r', [], r'))
   717      val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) (Logic.count_prems r', [], r'))
   718      val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r'
   718      val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r'
   719 ;
   719 ;
   720 if UnparseC.term2str lhss = "?a * (?b + ?c)" andalso UnparseC.term2str t = "x * (y + z)" then ()
   720 if UnparseC.term lhss = "?a * (?b + ?c)" andalso UnparseC.term t = "x * (y + z)" then ()
   721 else error "ARGS FOR Pattern.match CHANGED";
   721 else error "ARGS FOR Pattern.match CHANGED";
   722 val match = Pattern.match thy (lhss, t) (Vartab.empty, Vartab.empty);
   722 val match = Pattern.match thy (lhss, t) (Vartab.empty, Vartab.empty);
   723 if (Envir.subst_term match r |> UnparseC.term2str) = "x * (y + z) = x * y + x * z" then ()
   723 if (Envir.subst_term match r |> UnparseC.term) = "x * (y + z) = x * y + x * z" then ()
   724   else error "Pattern.match CHANGED";
   724   else error "Pattern.match CHANGED";
   725 
   725 
   726 "----------- fun mk_thm ------------------------------------------------------------------------";
   726 "----------- fun mk_thm ------------------------------------------------------------------------";
   727 "----------- fun mk_thm ------------------------------------------------------------------------";
   727 "----------- fun mk_thm ------------------------------------------------------------------------";
   728 "----------- fun mk_thm ------------------------------------------------------------------------";
   728 "----------- fun mk_thm ------------------------------------------------------------------------";
   740 case parse thy term_str of
   740 case parse thy term_str of
   741   NONE => ()
   741   NONE => ()
   742 | _ => writeln "parse does NOT take patterns with '?'";
   742 | _ => writeln "parse does NOT take patterns with '?'";
   743 
   743 
   744 val t1 = (#prop o Thm.rep_thm) (num_str thm);
   744 val t1 = (#prop o Thm.rep_thm) (num_str thm);
   745 if UnparseC.term2str t1 = patt_str then () else error "realpow_twoI (\<rightarrow> string) NOT equal to given string";
   745 if UnparseC.term t1 = patt_str then () else error "realpow_twoI (\<rightarrow> string) NOT equal to given string";
   746 
   746 
   747 val t2 = HOLogic.Trueprop $ (((parse_patt thy)) patt_str) : term;
   747 val t2 = HOLogic.Trueprop $ (((parse_patt thy)) patt_str) : term;
   748 if t1 = t2 then () else error "prop of realpow_twoI NOT equal to parsed string";
   748 if t1 = t2 then () else error "prop of realpow_twoI NOT equal to parsed string";
   749 
   749 
   750 (mk_thm thy patt_str) handle _ => mk_thm thy "mk_thm = does NOT handle patterns";
   750 (mk_thm thy patt_str) handle _ => mk_thm thy "mk_thm = does NOT handle patterns";
   772 case parse thy term_str of
   772 case parse thy term_str of
   773   NONE => ()
   773   NONE => ()
   774 | _ => writeln "parse does NOT take patterns with '?'";
   774 | _ => writeln "parse does NOT take patterns with '?'";
   775 
   775 
   776 val t1 = (#prop o Thm.rep_thm) (num_str thm);
   776 val t1 = (#prop o Thm.rep_thm) (num_str thm);
   777 if UnparseC.term2str t1 = patt_str then () else error "realpow_twoI (\<rightarrow> string) NOT equal to given string";
   777 if UnparseC.term t1 = patt_str then () else error "realpow_twoI (\<rightarrow> string) NOT equal to given string";
   778 
   778 
   779 val t2 = (*Trueprop $*) (((parse_patt thy)) patt_str) : term;
   779 val t2 = (*Trueprop $*) (((parse_patt thy)) patt_str) : term;
   780 if t1 = t2 then () else error "prop of realpow_twoI NOT equal to parsed string";
   780 if t1 = t2 then () else error "prop of realpow_twoI NOT equal to parsed string";
   781 
   781 
   782 (mk_thm thy patt_str) handle _ => mk_thm thy "mk_thm = does NOT handle patterns";
   782 (mk_thm thy patt_str) handle _ => mk_thm thy "mk_thm = does NOT handle patterns";
   796 val thy = @{theory};
   796 val thy = @{theory};
   797 val rls = norm_equation;
   797 val rls = norm_equation;
   798 val term = str2term "x + 1 = 2";
   798 val term = str2term "x + 1 = 2";
   799 
   799 
   800 val SOME (t, asm) = rewrite_set_ thy false rls term;
   800 val SOME (t, asm) = rewrite_set_ thy false rls term;
   801 if UnparseC.term2str t = "x + 1 + -1 * 2 = 0" then () else error "rewrite_set_ norm_equation CHANGED";
   801 if UnparseC.term t = "x + 1 + -1 * 2 = 0" then () else error "rewrite_set_ norm_equation CHANGED";
   802 
   802 
   803 "~~~~~ fun rewrite_set_, args:"; val (thy, bool, rls, term) = (thy, false, rls, term);
   803 "~~~~~ fun rewrite_set_, args:"; val (thy, bool, rls, term) = (thy, false, rls, term);
   804 "~~~~~ fun rewrite__set_, args:"; val (thy, i, put_asm, bdv, rls, ct) = (thy, 1, bool, [], rls, term);
   804 "~~~~~ fun rewrite__set_, args:"; val (thy, i, put_asm, bdv, rls, ct) = (thy, 1, bool, [], rls, term);
   805 
   805