test/Tools/isac/MathEngBasic/rewrite.sml
author wneuper <walther.neuper@jku.at>
Mon, 03 May 2021 08:49:50 +0200
changeset 60275 98ee674d18d3
parent 60270 844610c5c943
child 60278 343efa173023
permissions -rw-r--r--
replace Isac's power with Isabelle's Transcendental.powr
     1 (* Title: "ProgLang/rewrite.sml"
     2    Author: Walther Neuper 050908
     3    (c) copyright due to lincense terms.
     4 *)
     5 
     6 "-----------------------------------------------------------------------------------------------";
     7 "table of contents -----------------------------------------------------------------------------";
     8 "-----------------------------------------------------------------------------------------------";
     9 "----------- assemble rewrite ------------------------------------------------------------------";
    10 "----------- test rewriting without Isac's thys ------------------------------------------------";
    11 "----------- test rewriting without Isac's thys, ~~~~~ fun rewrite_ ----------------------------";
    12 "----------- conditional rewriting without Isac's thys -----------------------------------------";
    13 "----------- conditional rewriting without Isac's thys, ~~~~~ fun ------------------------------";
    14 "----------- conditional rewriting creating an assumption---------------------------------------";
    15 "----------- step through 'and rew_sub': -------------------------------------------------------";
    16 "----------- step through 'fun rewrite_terms_'  ------------------------------------------------";
    17 "----------- rewrite_inst_ bdvs ----------------------------------------------------------------";
    18 "----------- check diff 2002--2009-3 -----------------------------------------------------------";
    19 "----------- compare all prepat's existing 2010 ------------------------------------------------";
    20 "----------- fun app_rev, Rrls, ----------------------------------------------------------------";
    21 "----------- 2011 thms with axclasses ----------------------------------------------------------";
    22 "----------- fun rewrite_set_ .. RatEq_eliminate step into to fun chk --------------------------";
    23 "----------- fun rewrite_ down to Pattern.match ------------------------------------------------";
    24 "----------- fun rewrite_set_ .. norm_equation -------------------------------------------------";
    25 "-----------------------------------------------------------------------------------------------";
    26 "-----------------------------------------------------------------------------------------------";
    27 "-----------------------------------------------------------------------------------------------";
    28 
    29 
    30 "----------- assemble rewrite ------------------------------------------------------------------";
    31 "----------- assemble rewrite ------------------------------------------------------------------";
    32 "----------- assemble rewrite ------------------------------------------------------------------";
    33 "===== rewriting by thm with 'a";
    34 (*show_types := true;*)
    35 
    36 val thy = @{theory Complex_Main};
    37 val ctxt = @{context};
    38 val thm = @{thm add.commute};
    39 val t = (Thm.term_of o the) (TermC.parse thy "((r + u) + t) + s");
    40 "----- from old: fun rewrite__";
    41 val bdv = [];
    42 val r = TermC.inst_bdv bdv (Eval.norm (Thm.prop_of thm));
    43 "----- from old: and rew_sub";
    44 val (LHS,RHS) = (dest_equals o strip_trueprop 
    45    	      o Logic.strip_imp_concl) r;
    46 (* old
    47 val insts = Pattern.match thy (LHS,t) (Vartab.empty, Vartab.empty);*)
    48 "----- fun match_rew in Pure/pattern.ML";
    49 val rtm = the_default RHS (Term.rename_abs LHS t RHS);
    50 
    51 writeln(Syntax.string_of_term ctxt rtm);
    52 writeln(Syntax.string_of_term ctxt LHS);
    53 writeln(Syntax.string_of_term ctxt t);
    54 
    55 (Pattern.match thy (LHS, t) (Vartab.empty, Vartab.empty));
    56 val (rew, RHS) = (Envir.subst_term 
    57   (Pattern.match thy (LHS, t) (Vartab.empty, Vartab.empty)) rtm, rtm);
    58 (*lookup in isabelle?trace?response...*)
    59 writeln(Syntax.string_of_term ctxt rew);
    60 writeln(Syntax.string_of_term ctxt RHS);
    61 "===== rewriting: prep insertion into rew_sub";
    62 val thy = @{theory Complex_Main};
    63 val ctxt = @{context};
    64 val thm =  @{thm nonzero_divide_mult_cancel_right};
    65 val r = Thm.prop_of thm;
    66 val tm = @{term "x / (2 * x)::real"};
    67 "----- and rew_sub";
    68 val (LHS, RHS) = (HOLogic.dest_eq o HOLogic.dest_Trueprop
    69                   o Logic.strip_imp_concl) r;
    70 val r' = Envir.subst_term (Pattern.match thy (LHS, tm) 
    71                                 (Vartab.empty, Vartab.empty)) r;
    72 val p' = (fst o Logic.strip_prems) (Logic.count_prems r', [], r');
    73 val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop 
    74             o Logic.strip_imp_concl) r';
    75 
    76 (*is displayed on top of <response> buffer...*)
    77 Pretty.writeln (Proof_Context.pretty_term_abbrev @{context} r');
    78 Pretty.writeln (Proof_Context.pretty_term_abbrev @{context} t');
    79 (**)
    80 
    81 "----------- test rewriting without Isac's thys ------------------------------------------------";
    82 "----------- test rewriting without Isac's thys ------------------------------------------------";
    83 "----------- test rewriting without Isac's thys ------------------------------------------------";
    84 
    85 "===== rewriting with Isabelle2009-1 only, i.e without isac-hacks";
    86 val thy = @{theory Complex_Main};
    87 val ctxt = @{context};
    88 val thm =  @{thm add.commute};
    89 val tm = @{term "x + y*z::real"};
    90 
    91 val SOME (r,_) = (rewrite_ thy dummy_ord Rule_Set.empty false thm tm);
    92 (*is displayed on _TOP_ of <response> buffer...*)
    93 Pretty.writeln (Proof_Context.pretty_term_abbrev @{context} r);
    94 if r = @{term "y*z + x::real"}
    95 then () else error "rewrite.sml diff.result in rewriting";
    96 
    97 "----- rewriting a subterm";
    98 val tm = @{term "w*(x + y*z)::real"};
    99 
   100 val SOME (r, _) = (rewrite_ thy dummy_ord Rule_Set.empty false thm tm);
   101 
   102 "----- ordered rewriting";
   103 fun tord (_:subst) pp = LibraryC.termless pp;
   104 if tord [] (@{term "x + y*z::real"}, @{term "y*z + x::real"}) then ()
   105 else error "rewrite.sml diff.behav. in ord.rewr.";
   106 
   107 val NONE = (rewrite_ thy tord Rule_Set.empty false thm tm);
   108 (*is displayed on _TOP_ of <response> buffer...*)
   109 Pretty.writeln (Proof_Context.pretty_term_abbrev @{context} r);
   110 
   111 val tm = @{term "x*y + z::real"};
   112 val SOME (r,_) = (rewrite_ thy tord Rule_Set.empty false thm tm);
   113 
   114 
   115 "----------- conditional rewriting without Isac's thys -----------------------------------------";
   116 "----------- conditional rewriting without Isac's thys -----------------------------------------";
   117 "----------- conditional rewriting without Isac's thys -----------------------------------------";
   118 "===== prepr cond.rew. with Pattern.match";
   119 val thy = @{theory Complex_Main};
   120 val ctxt = @{context};
   121 val thm =  @{thm nonzero_divide_mult_cancel_right}; (* = "?b \<noteq> 0 \<Longrightarrow> ?b / (?a * ?b) = 1 / ?a":*)
   122 val rule = Thm.prop_of thm;
   123 val tm = @{term "x / (2 * x)::real"};
   124 val prem = Logic.strip_imp_prems rule;
   125 val nps = Logic.count_prems rule;
   126 val prems = Logic.strip_prems (nps, [], rule);
   127 
   128 val eq = Logic.strip_imp_concl rule;
   129 val (LHS, RHS) = (HOLogic.dest_eq o HOLogic.dest_Trueprop) eq;
   130 
   131 val mtcs = Pattern.match thy (LHS, tm) (Vartab.empty, Vartab.empty);
   132 val rule' = Envir.subst_term mtcs rule;
   133 
   134 val prems' = (fst o Logic.strip_prems) (Logic.count_prems rule', [], rule');
   135 val RHS' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) rule';
   136 
   137 (rule' |> UnparseC.term) = "x \<noteq> 0 \<Longrightarrow> x / (2 * x) = 1 / 2";
   138  rule';
   139 
   140 (rule' |> Logic.strip_imp_concl |> UnparseC.term) = "x / (2 * x) = 1 / 2";
   141  rule' |> Logic.strip_imp_concl;
   142 
   143 (rule' |> Logic.strip_imp_concl |> UnparseC.term) = "x / (2 * x) = 1 / 2";
   144  rule' |> Logic.strip_imp_concl;
   145 
   146 
   147 "----------- conditional rewriting creating an assumption---------------------------------------";
   148 "----------- conditional rewriting creating an assumption---------------------------------------";
   149 "----------- conditional rewriting creating an assumption---------------------------------------";
   150 val thm =  @{thm nonzero_divide_mult_cancel_right};
   151 val tm = @{term "x / (2 * x)::real"};
   152 
   153 val SOME (rew, asm) = (rewrite_ thy dummy_ord Rule_Set.empty false thm tm);
   154 
   155 if rew = @{term "1 / 2::real"} then () (* the rewrite is _NO_ Trueprop *)
   156 else error "rewrite.sml diff.result in cond.rew.";
   157 
   158 if hd asm = @{term "x \<noteq> (0::real)"} (* dropped Trueprop $ ...*)
   159 then () else error "rewrite.sml diff.asm in cond.rew.";
   160 "----- conditional rewriting immediately: can only be done with ";
   161 "------Isabelle numerals, because erls cannot handle them yet.";
   162 
   163 
   164 "----------- conditional rewriting creating an assumption, ~~~~~ fun rewrite_ ------------------";
   165 "----------- conditional rewriting creating an assumption, ~~~~~ fun rewrite_ ------------------";
   166 "----------- conditional rewriting creating an assumption, ~~~~~ fun rewrite_ ------------------";
   167 val thm = @{thm nonzero_divide_mult_cancel_right};(* = "?b \<noteq> 0 \<Longrightarrow> ?b / (?a * ?b) = 1 / ?a"*)
   168 val tm = @{term "x / (2 * x)::real"};
   169 val erls = eval_rls;
   170 
   171 (**)val SOME (rew, asm) = (rewrite_ thy dummy_ord Rule_Set.empty false thm tm);
   172 (** )exception TERM raised (line 196 of "~~/src/HOL/Tools/hologic.ML"):
   173   dest_Trueprop
   174   ?b \<noteq> 0 \<Longrightarrow> ?b / (?a * ?b) = 1 / ?a( **)
   175 "~~~~~ fun rewrite_ , args:"; val (thy, rew_ord, erls, bool, thm, term) =
   176   (thy, dummy_ord, erls, false, thm, tm);
   177 "~~~~~ fun rewrite__ , args:"; val (thy, i, bdv, tless, rls, put_asm, thm, ct) =
   178   (thy, 1, []: (term * term) list, rew_ord, erls, bool, thm, term);
   179 
   180 (**) val (t', asms, _(*lrd*), rew) = rew_sub thy i bdv tless rls put_asm ([(*root of the term*)]: TermC.path)
   181 		  (TermC.inst_bdv bdv (Eval.norm (Thm.prop_of thm))) ct;
   182 (** )exception TERM raised (line 196 of "~~/src/HOL/Tools/hologic.ML"):
   183   dest_Trueprop
   184   ?b \<noteq> 0 \<Longrightarrow> ?b / (?a * ?b) = 1 / ?a( **)
   185 "~~~~~ fun rew_sub , args:"; val (thy, i, bdv, tless, rls, put_asm, lrd, r, t) =
   186   (thy, i, bdv, tless, rls, put_asm, ([(*root of the term*)]: TermC.path),
   187 		  (TermC.inst_bdv bdv (Eval.norm (Thm.prop_of thm))), ct);
   188 (*+*)UnparseC.term r = "?b \<noteq> (0::?'a) \<Longrightarrow> ?b / (?a * ?b) = (1::?'a) / ?a";
   189 
   190     val (lhs, rhs) = (HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r;
   191 (*+*)(UnparseC.term lhs, UnparseC.term rhs) = ("?b / (?a * ?b)", "(1::?'a) / ?a");
   192     val r' = (Envir.subst_term (Pattern.match thy (lhs, t) (Vartab.empty, Vartab.empty)) r)
   193       handle Pattern.MATCH => raise NO_REWRITE;
   194     val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) (Logic.count_prems r', [], r'));
   195 (*+*)UnparseC.term r' = "x \<noteq> 0 \<Longrightarrow> x / (2 * x) = 1 / 2";
   196 (*+*)r';
   197     val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r'
   198     val _ = trace_in2 i "eval asms" thy r';
   199         val (simpl_p', nofalse) = eval__true thy (i + 1) p' bdv rls;
   200 	      (*if*) nofalse (*then*);
   201       val (t'', p'') = (trace_in4 i "asms accepted" thy p' simpl_p'; (t',simpl_p'));
   202 (*##  asms accepted: [x \<noteq> 0]   stored: [x \<noteq> 0] *)
   203 
   204 (*+*)if (UnparseC.term t'', map UnparseC.term p'') = ("1 / 2", ["x \<noteq> 0"]) then ()
   205 (*+*)else error "conditional rewriting x \<noteq> 0 \<Longrightarrow> x / (2 * x) = 1 / 2 CHANGED";
   206 
   207 
   208 "----------- step through 'and rew_sub': ----------------";
   209 "----------- step through 'and rew_sub': ----------------";
   210 "----------- step through 'and rew_sub': ----------------";
   211 (*and make asms without Trueprop, beginning with the result:*)
   212 val tm = @{term "x / (2 * x)::real"};
   213 val (t', asm, _, _) = rew_sub thy 0 [] dummy_ord Rule_Set.empty true [] (Thm.prop_of thm) tm;
   214 (*show_types := false;*)
   215 "----- evaluate arguments";
   216 val (thy, i, bdv, tless, rls, put_asm, lrd, r, t) = 
   217     (thy, 0, [], dummy_ord, Rule_Set.empty, true, [], (Thm.prop_of thm), tm);
   218 "----- step 1: LHS, RHS of rule";
   219      val (LHS, RHS) = (HOLogic.dest_eq o HOLogic.dest_Trueprop
   220                        o Logic.strip_imp_concl) r;
   221 UnparseC.term r = "?b \<noteq> (0::?'a) \<Longrightarrow> ?b / (?a * ?b) = (1::?'a) / ?a";
   222 UnparseC.term LHS = "?b / (?a * ?b)"; UnparseC.term RHS = "(1::?'a) / ?a";
   223 "----- step 2: the rule instantiated";
   224      val r' = Envir.subst_term 
   225                   (Pattern.match thy (LHS, t) (Vartab.empty, Vartab.empty)) r;
   226 UnparseC.term r' = "x \<noteq> 0 \<Longrightarrow> x / (2 * x) = 1 / 2";
   227 "----- step 3: get the (instantiated) assumption(s)";
   228      val p' = (fst o Logic.strip_prems) (Logic.count_prems r', [], r');
   229 UnparseC.term (hd p') = "x \<noteq> 0";
   230 "=====vvv make asms without Trueprop ---vvv";
   231      val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) 
   232                                              (Logic.count_prems r', [], r'));
   233 case p' of
   234     [Const ("HOL.Not", _) $ (Const ("HOL.eq", _) 
   235       $ Free ("x", _) $ Const ("Groups.zero_class.zero", _))] => ()
   236   | _ => error "rewrite.sml assumption changed";
   237 "===== \<up>  make asms without Trueprop --- \<up> ";
   238 "----- step 4: get the (instantiated) RHS";
   239      val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop 
   240                o Logic.strip_imp_concl) r';
   241 UnparseC.term t' = "1 / 2";
   242 
   243 "----------- step through 'fun rewrite_terms_'  ------------------------------------------------";
   244 "----------- step through 'fun rewrite_terms_'  ------------------------------------------------";
   245 "----------- step through 'fun rewrite_terms_'  ------------------------------------------------";
   246 "----- step 0: args for rewrite_terms_, local fun";
   247 val (thy, ord, erls, equs, t) =
   248     (@{theory "Biegelinie"}, dummy_ord, Rule_Set.Empty, [TermC.str2term "x = 0"],
   249      TermC.str2term "M_b x = -1 * q_0 * x \<up> 2 / 2 + x * c + c_2");
   250 "----- step 1: args for rew_";
   251 val ((t', asm'), (rules as r::rs), t) = ((TermC.empty, []), equs, t);
   252 "----- step 2: rew_sub";
   253 rew_sub thy 1 [] ord erls false [] (HOLogic.Trueprop $ r) t;
   254 "----- step 3: step through rew_sub -- inefficient: goes into subterms";
   255 
   256 
   257 val SOME (t', _) = rewrite_terms_ thy dummy_ord Rule_Set.Empty equs t;
   258 writeln "---------- rewrite_terms_  1---------------------------";
   259 if UnparseC.term t' = "M_b 0 = -1 * q_0 * 0 \<up> 2 / 2 + 0 * c + c_2" then ()
   260 else error "rewrite.sml rewrite_terms_ [x = 0]";
   261 
   262 val equs = [TermC.str2term "M_b 0 = 0"];
   263 val t = TermC.str2term "M_b 0 = -1 * q_0 * 0 \<up> 2 / 2 + 0 * c + c_2";
   264 val SOME (t', _) = rewrite_terms_ thy dummy_ord Rule_Set.Empty equs t;
   265 writeln "---------- rewrite_terms_  2---------------------------";
   266 if UnparseC.term t' = "0 = -1 * q_0 * 0 \<up> 2 / 2 + 0 * c + c_2" then ()
   267 else error "rewrite.sml rewrite_terms_ [M_b 0 = 0]";
   268 
   269 val equs = [TermC.str2term "x = 0", TermC.str2term"M_b 0 = 0"];
   270 val t = TermC.str2term "M_b x = -1 * q_0 * x \<up> 2 / 2 + x * c + c_2";
   271 val SOME (t', _) = rewrite_terms_ thy dummy_ord Rule_Set.Empty equs t;
   272 writeln "---------- rewrite_terms_  3---------------------------";
   273 if UnparseC.term t' = "0 = -1 * q_0 * 0 \<up> 2 / 2 + 0 * c + c_2" then ()
   274 else error "rewrite.sml rewrite_terms_ [x = 0, M_b 0 = 0]";
   275 
   276 
   277 "----------- rewrite_inst_ bdvs ----------------------------------------------------------------";
   278 "----------- rewrite_inst_ bdvs ----------------------------------------------------------------";
   279 "----------- rewrite_inst_ bdvs ----------------------------------------------------------------";
   280 (*see smltest/Scripts/term_G.sml: inst_bdv 2*)
   281 val t = TermC.str2term"-1 * (q_0 * L \<up> 2) / 2 + (L * c_3 + c_4) = 0";
   282 val bdvs = [(TermC.str2term"bdv_1",TermC.str2term"c"),
   283 	    (TermC.str2term"bdv_2",TermC.str2term"c_2"),
   284 	    (TermC.str2term"bdv_3",TermC.str2term"c_3"),
   285 	    (TermC.str2term"bdv_4",TermC.str2term"c_4")];
   286 (*------------ outcommented WN071210, after inclusion into ROOT.ML 
   287 val SOME (t,_) = 
   288     rewrite_inst_ thy e_rew_ord 
   289 		  (Rule_Set.append_rules "erls_isolate_bdvs" Rule_Set.empty 
   290 			      [(Eval ("EqSystem.occur'_exactly'_in", 
   291 				      eval_occur_exactly_in 
   292 					  "#eval_occur_exactly_in_"))
   293 			       ]) 
   294 		  false bdvs (ThmC.numerals_to_Free @{separate_bdvs_add) t;
   295 (writeln o UnparseC.term) t;
   296 if UnparseC.term t = "L * c_3 + c_4 = 0 + -1 * (-1 * (q_0 * L \<up> 2) / 2)"
   297 then () else error "rewrite.sml rewrite_inst_ bdvs";
   298 > Rewrite.trace_on:=true;
   299 Rewrite.trace_on:=false;--------------------------------------------*)
   300 
   301 
   302 "----------- compare all prepat's existing 2010 ------------------------------------------------";
   303 "----------- compare all prepat's existing 2010 ------------------------------------------------";
   304 "----------- compare all prepat's existing 2010 ------------------------------------------------";
   305 val thy = @{theory "Isac_Knowledge"};
   306 val t = @{term "a + b * x = (0 ::real)"};
   307 val pat = TermC.parse_patt thy "?l = (?r ::real)";
   308 val precond = TermC.parse_patt thy "is_polynomial (?l::real)";(*no infix def*)
   309 val precond = TermC.parse_patt thy "(?l::real) is_expanded"; 
   310 
   311 val inst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
   312 val preinst = Envir.subst_term inst precond;
   313 UnparseC.term preinst;
   314 
   315 "===== Rational.thy: cancel ===";
   316 (* pat matched with the current term gives an environment 
   317    (or not: hen the Rrls not applied);
   318    if pre1 and pre2 evaluate to @{term True} in this environment,
   319    then the Rrls is applied. *)
   320 val t = TermC.str2term "(a + b) / c ::real";
   321 val pat = TermC.parse_patt thy "?r / ?s ::real";
   322 val pres = [TermC.parse_patt thy "?r is_expanded", TermC.parse_patt thy "?s is_expanded"];
   323 val prepat = [(pres, pat)];
   324 val erls = rational_erls;
   325 (* erls got from Rrls {erls, prepat, scr = Rfuns {normal_form, ...}, ...} *)
   326 
   327 val subst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
   328 val asms = map (Envir.subst_term subst) pres;
   329 if UnparseC.terms asms = "[\"a + b is_expanded\", \"c is_expanded\"]"
   330 then () else error "rewrite.sml: prepat cancel subst";
   331 if ([], true) = eval__true thy 0 asms [] erls
   332 then () else error "rewrite.sml: prepat cancel eval__true";
   333 
   334 "===== Rational.thy: add_fractions_p ===";
   335 (* if each pat* TermC.matches with the current term, the Rrls is applied
   336    (there are no preconditions to be checked, they are @{term True}) *)
   337 val t = TermC.str2term "a / b + 1 / 2";
   338 val pat = TermC.parse_patt thy "?r / ?s + ?u / ?v";
   339 val pres = [@{term True}];
   340 val prepat = [(pres, pat)];
   341 val erls = rational_erls;
   342 (* erls got from Rrls {erls, prepat, scr = Rfuns {normal_form, ...}, ...} *)
   343 
   344 val subst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
   345 if ([], true) = eval__true thy 0 (map (Envir.subst_term subst) pres) [] erls
   346 then () else error "rewrite.sml: prepat add_fractions_p";
   347 
   348 "===== Poly.thy: order_mult_ ===";
   349           (* ?p matched with the current term gives an environment,
   350              which evaluates (the instantiated) "p is_multUnordered" to true*)
   351 val t = TermC.str2term "x \<up> 2 * x";
   352 val pat = TermC.parse_patt thy "?p :: real"
   353 val pres = [TermC.parse_patt thy "?p is_multUnordered"];
   354 val prepat = [(pres, pat)];
   355 val erls = Rule_Set.append_rules "Rule_Set.empty-is_multUnordered" Rule_Set.empty
   356 		      [Eval ("Poly.is'_multUnordered", 
   357                              eval_is_multUnordered "")];
   358 
   359 val subst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
   360 val asms = map (Envir.subst_term subst) pres;
   361 if UnparseC.terms asms = "[\"x \<up> 2 * x is_multUnordered\"]"
   362 then () else error "rewrite.sml: prepat order_mult_ subst";
   363 if ([], true) = eval__true thy 0 asms [] erls
   364 then () else error "rewrite.sml: prepat order_mult_ eval__true";
   365 
   366 
   367 "----------- fun app_rev, Rrls, ----------------------------------------------------------------";
   368 "----------- fun app_rev, Rrls, ----------------------------------------------------------------";
   369 "----------- fun app_rev, Rrls, ----------------------------------------------------------------";
   370 val t = TermC.str2term "x \<up> 2 * x";
   371 
   372 if is_multUnordered t then () else error "rewrite.sml diff. is_multUnordered 2";
   373 val tm = TermC.str2term "(x \<up> 2 * x) is_multUnordered";
   374 eval_is_multUnordered "testid" "" tm thy;
   375 
   376 case eval_is_multUnordered "testid" "" tm thy of
   377     SOME (_, Const ("HOL.Trueprop", _) $ 
   378                    (Const ("HOL.eq", _) $
   379                           (Const ("Poly.is'_multUnordered", _) $ _) $ 
   380                           Const ("HOL.True", _))) => ()
   381   | _ => error "rewrite.sml diff. eval_is_multUnordered 2b";
   382 
   383 tracing "----- begin rewrite x \<up> 2 * x ---"; Rewrite.trace_on := false;
   384 val SOME (t', _) = rewrite_set_ thy true order_mult_ t;
   385 tracing "----- end rewrite x \<up> 2 * x ---"; Rewrite.trace_on := false;
   386 if UnparseC.term t' = "x * x \<up> 2" then ()
   387 else error "rewrite.sml Poly.is'_multUnordered doesn't work";
   388 
   389 (* for achieving the previous result, the following code was taken apart *)
   390 "----- rewrite__set_ ---";
   391 val (thy, i, _, _, (rrls as Rrls _), t) = (thy, 0, true, [], order_mult_, tm);
   392 	val (t', asm, rew) = app_rev thy (i+1) rrls t;
   393 "----- app_rev ---";
   394 val (thy, i, rrls, t) = (thy, (i+1), rrls, t);
   395 	fun chk_prepat thy erls [] t = true
   396 	  | chk_prepat thy erls prepat t =
   397 	    let fun chk (pres, pat) =
   398 		    (let val subst: Type.tyenv * Envir.tenv = 
   399 			     Pattern.match thy (pat, t)
   400 					    (Vartab.empty, Vartab.empty)
   401 		     in snd (eval__true thy (i+1) 
   402 					(map (Envir.subst_term subst) pres)
   403 					[] erls)
   404 		     end)
   405 		    handle Pattern.MATCH => false
   406 		fun scan_ f [] = false (*scan_ NEVER called by []*)
   407 		  | scan_ f (pp::pps) = if f pp then true
   408 					else scan_ f pps;
   409 	    in scan_ chk prepat end;
   410 
   411 	(*.apply the normal_form of a rev-set.*)
   412 	fun app_rev' thy (Rrls{erls,prepat,scr=Rfuns{normal_form,...},...}) t =
   413 	    if chk_prepat thy erls prepat t
   414 	    then ((*tracing("### app_rev': t = "^(UnparseC.term t));*)
   415                   normal_form t)
   416 	    else NONE;
   417 (*fixme val NONE = app_rev' thy rrls t;*)
   418 "----- app_rev' ---";
   419 val (thy, Rrls{erls,prepat,scr=Rfuns{normal_form,...},...}, t) = (thy, rrls, t);
   420 (*fixme false*)   chk_prepat thy erls prepat t;
   421 "----- chk_prepat ---";
   422 val (thy, erls, prepat, t) = (thy, erls, prepat, t);
   423                 fun chk (pres, pat) =
   424 		    (let val subst: Type.tyenv * Envir.tenv = 
   425 			     Pattern.match thy (pat, t)
   426 					    (Vartab.empty, Vartab.empty)
   427 		     in snd (eval__true thy (i+1) 
   428 					(map (Envir.subst_term subst) pres)
   429 					[] erls)
   430 		     end)
   431 		    handle Pattern.MATCH => false;
   432 		fun scan_ _ [] = false (*scan_ NEVER called by []*)
   433 		  | scan_ f (pp::pps) = if f pp then true
   434 					else scan_ f pps;
   435 tracing "=== poly.sml: scan_ chk prepat begin";
   436 scan_ chk prepat;
   437 tracing "=== poly.sml: scan_ chk prepat end";
   438 
   439 "----- chk ---";
   440 (*reestablish...*) val t = TermC.str2term "x \<up> 2 * x";
   441 val [(pres, pat)] = prepat;
   442                          val subst: Type.tyenv * Envir.tenv = 
   443 			     Pattern.match thy (pat, t)
   444 					    (Vartab.empty, Vartab.empty);
   445 
   446 (*fixme: asms = ["p is_multUnordered"]...instantiate*)
   447 "----- eval__true ---";
   448 val asms = (map (Envir.subst_term subst) pres);
   449 if UnparseC.terms asms = "[\"x \<up> 2 * x is_multUnordered\"]" then ()
   450 else error "rewrite.sml: diff. is_multUnordered, asms";
   451 val (thy, i, asms, bdv, rls) = 
   452     (thy, (i+1), [TermC.str2term "(x \<up> 2 * x) is_multUnordered"], 
   453      [] : (term * term) list, erls);
   454 case eval__true thy i asms bdv rls of 
   455     ([], true) => ()
   456   | _ => error "rewrite.sml: diff. is_multUnordered, eval__true";
   457 
   458 "----------- 2011 thms with axclasses ----------------------------------------------------------";
   459 "----------- 2011 thms with axclasses ----------------------------------------------------------";
   460 "----------- 2011 thms with axclasses ----------------------------------------------------------";
   461 val thm = ThmC.numerals_to_Free @{thm div_by_1};
   462 val prop = Thm.prop_of thm;
   463 TermC.atomty prop;                                     (*Type 'a*)
   464 val t = TermC.str2term "(2*x)/1";                      (*Type real*)
   465 
   466 val (thy, ro, er, inst) = 
   467     (@{theory "Isac_Knowledge"}, tless_true, eval_rls, [(@{term "bdv::real"}, @{term "x::real"})]);
   468 val SOME (t, _) = rewrite_ thy ro er true thm t; (*real TermC.matches 'a ?via ring? etc*)
   469 
   470 "----------- fun rewrite_set_ .. RatEq_eliminate step into to fun chk --------------------------";
   471 "----------- fun rewrite_set_ .. RatEq_eliminate step into to fun chk --------------------------";
   472 "----------- fun rewrite_set_ .. RatEq_eliminate step into to fun chk --------------------------";
   473 val thy = @{theory RatEq};
   474 val ctxt = Proof_Context.init_global thy;
   475 val SOME t = parseNEW ctxt "(3 + -1 * x + x \<up> 2) / (9 * x + -6 * x \<up> 2 + x \<up> 3) = 1 / x";
   476 val rls = assoc_rls "RatEq_eliminate"
   477 
   478 val SOME (t''''', asm''''') =
   479            rewrite_set_ thy true rls t;
   480 "~~~~~ fun rewrite_set_ , args:"; val (thy, bool, rls, term) = (thy, true, rls, t);
   481            rewrite__set_ thy 1 bool [] rls term;
   482 "~~~~~ and rewrite__set_ , args:"; val (thy, i, put_asm, bdv, rls, ct)
   483   = (thy, 1, bool, []:(term * term) list, rls, term);
   484 
   485 (*/------- outcomment this code: otherwise the re-definition could infect tests lateron ------\ * )
   486       datatype switch = Applicable.Yes | Noap;
   487       fun rew_once _ asm ct Noap [] = (ct, asm) (* ?TODO unify with Prog_Expr.rew_once? *)
   488         | rew_once ruls asm ct Applicable.Yes [] = 
   489           (case rls of Rule_Set.Repeat _ => rew_once ruls asm ct Noap ruls
   490           | Rule_Set.Sequence _ => (ct, asm)
   491           | rls => raise ERROR ("rew_once not appl. to \"" ^ Rule_Set.id rls ^ "\""))
   492         | rew_once ruls asm ct apno (rul :: thms) =
   493           case rul of
   494             Rule.Thm (thmid, thm) =>
   495               (trace1 i (" try thm: " ^ thmid);
   496               case rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule.Rule_Set.rep) rls)
   497                   ((#erls o Rule.Rule_Set.rep) rls) put_asm thm ct of
   498                 NONE => rew_once ruls asm ct apno thms
   499               | SOME (ct', asm') => 
   500                 (trace1 i (" rewrites to: " ^ UnparseC.term_in_thy thy ct');
   501                 rew_once ruls (union (op =) asm asm') ct' Applicable.Yes (rul :: thms)))
   502                 (* once again try the same rule, e.g. associativity against "()"*)
   503           | Rule.Eval (cc as (op_, _)) => 
   504             let val _= trace1 i (" try calc: " ^ op_ ^ "'")
   505               val ct = TermC.uminus_to_string ct (*WN190312: superfluous?*)
   506             in case Eval.adhoc_thm thy cc ct of
   507                 NONE => rew_once ruls asm ct apno thms
   508               | SOME (_, thm') => 
   509                 let 
   510                   val pairopt = rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule.Rule_Set.rep) rls)
   511                     ((#erls o Rule.Rule_Set.rep) rls) put_asm thm' ct;
   512                   val _ = if pairopt <> NONE then () else error ("rewrite_set_, rewrite_ \"" ^ 
   513                     ThmC.string_of_thm thm' ^ "\" " ^ UnparseC.term_in_thy thy ct ^ " = NONE")
   514                   val _ = trace1 i (" calc. to: " ^ UnparseC.term_in_thy thy ((fst o the) pairopt))
   515                 in rew_once ruls asm ((fst o the) pairopt) Applicable.Yes (rul :: thms) end
   516             end
   517           | Rule.Cal1 (cc as (op_, _)) => 
   518             let val _= trace1 i (" try cal1: " ^ op_ ^ "'");
   519               val ct = TermC.uminus_to_string ct
   520             in case Eval.adhoc_thm1_ thy cc ct of
   521                 NONE => (ct, asm)
   522               | SOME (_, thm') =>
   523                 let 
   524                   val pairopt = rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule.Rule_Set.rep) rls)
   525                     ((#erls o Rule.Rule_Set.rep) rls) put_asm thm' ct;
   526                   val _ = if pairopt <> NONE then () else error ("rewrite_set_, rewrite_ \"" ^
   527                      ThmC.string_of_thm thm' ^ "\" " ^ UnparseC.term_in_thy thy ct ^ " = NONE")
   528                   val _ = trace1 i (" cal1. to: " ^ UnparseC.term_in_thy thy ((fst o the) pairopt))
   529                 in the pairopt end
   530             end
   531           | Rule.Rls_ rls' => 
   532             (case rewrite__set_ thy (i + 1) put_asm bdv rls' ct of
   533               SOME (t', asm') => rew_once ruls (union (op =) asm asm') t' Applicable.Yes thms
   534             | NONE => rew_once ruls asm ct apno thms)
   535           | r => raise ERROR ("rew_once not appl. to \"" ^ Rule.Rule.to_string r ^ "\"");
   536       val ruls = (#rules o Rule.Rule_Set.rep) rls;
   537 (*    val _ = trace i (" rls: " ^ Rule_Set.id rls ^ " on: " ^ UnparseC.term_in_thy thy ct)*)
   538       val (ct', asm') = rew_once ruls [] ct Noap ruls;
   539 "~~~~~ fun rew_once , args:"; val (ruls, asm, ct, apno, (rul :: thms))
   540   = (ruls, []:term list, ct, Noap, ruls);
   541            val Rule.Thm (thmid, thm) = (*case*) rul (*of*);
   542 
   543     val SOME (ct', asm') = (*case*)
   544            rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule.Rule_Set.rep) rls)
   545                   ((#erls o Rule.Rule_Set.rep) rls) put_asm thm ct (*of*);
   546 "~~~~~ fun rewrite__ , args:"; val (thy, i, bdv, tless, rls, put_asm, thm, ct)
   547   = (thy, (i + 1), bdv, ((snd o #rew_ord o Rule.Rule_Set.rep) rls),
   548                   ((#erls o Rule.Rule_Set.rep) rls), put_asm, thm, ct);
   549 
   550     val (t', asms, _ (*lrd*), rew) =
   551            rew_sub thy i bdv tless rls put_asm ([(*root of the term*)]: TermC.path)
   552 		  (((TermC.inst_bdv bdv) o Eval.norm o Thm.prop_of) thm) ct;
   553 "~~~~~ and rew_sub , args:"; val (thy, i, bdv, tless, rls, put_asm, lrd, r, t)
   554   = (thy, i, bdv, tless, rls, put_asm, ([(*root of the term*)]: TermC.path),
   555 		  (((TermC.inst_bdv bdv) o Eval.norm o Thm.prop_of) thm), ct);
   556     val (lhs, rhs) = (HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r
   557     val r' = Envir.subst_term (Pattern.match thy (lhs, t) (Vartab.empty, Vartab.empty)) r
   558 ;
   559 (*+*)if UnparseC.term r' =
   560 (*+*)  "\<lbrakk>9 * x + -6 * x \<up> 2 + x \<up> 3 \<noteq> 0; x \<noteq> 0\<rbrakk>\n" ^
   561 (*+*)  "\<Longrightarrow> ((3 + -1 * x + x \<up> 2) /\n" ^
   562 (*+*)  "                   (9 * x + -6 * x \<up> 2 + x \<up> 3) =\n" ^
   563 (*+*)  "                   1 / x) =\n" ^
   564 (*+*)  "                  ((3 + -1 * x + x \<up> 2) * x =\n" ^
   565 (*+*)  "                   1 * (9 * x + -6 * x \<up> 2 + x \<up> 3))"
   566 (*+*)then () else error "instantiated rule CHANGED";
   567 
   568     val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) (Logic.count_prems r', [], r'))
   569 ;
   570 (*+*)if map UnparseC.term p' = ["x \<noteq> 0", "9 * x + -6 * x \<up> 2 + x \<up> 3 \<noteq> 0"]
   571 (*+*)then () else error "stored assumptions CHANGED";
   572 
   573     val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r'
   574 ;
   575 (*+*)if UnparseC.term t' = "(3 + -1 * x + x \<up> 2) * x = 1 * (9 * x + -6 * x \<up> 2 + x \<up> 3)"
   576 (*+*)then () else error "rewritten term (an equality) CHANGED";
   577 
   578         val (simpl_p', nofalse) =
   579            eval__true thy (i + 1) p' bdv rls;
   580 "~~~~~ and eval__true , args:"; val (thy, i, asms, bdv, rls) = (thy, (i + 1), p', bdv, rls);
   581   (*if*) asms = [@{term True}] orelse asms = [] (*else*); 
   582 
   583 (*+*)if map UnparseC.term asms = ["x \<noteq> 0", "9 * x + -6 * x \<up> 2 + x \<up> 3 \<noteq> 0"]
   584 (*+*)then () else error "asms before chk CHANGED";
   585 
   586         fun chk indets [] = (indets, true) (*return asms<>True until false*)
   587           | chk indets (a :: asms) =
   588             (case rewrite__set_ thy (i + 1) false bdv rls a of
   589               NONE => (chk (indets @ [a]) asms)
   590             | SOME (t, a') =>
   591               if t = @{term True} then (chk (indets @ a') asms) 
   592               else if t = @{term False} then ([], false)
   593             (*asm false .. thm not applied  \<up> ; continue until False vvv*)
   594             else chk (indets @ [t] @ a') asms);
   595 
   596     val (xxx, true) =
   597            chk [] asms;  (*return from eval__true*);
   598 "~~~~~ fun chk , args:"; val (indets, (a :: asms)) = ([], asms);
   599 
   600 (*+*)val Rule_Set.Repeat {id = "rateq_erls", rules, ...} = rls;
   601 (*+*)(*val rules =
   602 (*+*)   [Eval ("Rings.divide_class.divide", fn),
   603 (*+*)    Thm ("minus_divide_left", "- ?a1 / ?b1 = - (?a1 / ?b1)"),
   604 (*+*)    :
   605 (*+*)    Eval ("HOL.eq", fn),
   606 (*+*)    Thm ("not_true", "(\<not> True) = False"),
   607 (*+*)    Thm ("not_false", "(\<not> False) = True"),
   608 (*+*)    :
   609 (*+*)    Eval ("Transcendental.powr", fn),
   610 (*+*)    Eval ("RatEq.is'_ratequation'_in", fn)]:
   611 (*+*)   rule list*)
   612 (*+*)chk: term list -> term list -> term list * bool
   613 
   614            rewrite__set_ thy (i + 1) false bdv rls a (*of*);
   615 
   616 (*+*)Rewrite.trace_on := true;
   617 
   618         (*this was False; vvvv--- means: indeterminate*)
   619     val (* SOME (t, a') *)NONE = (*case*)
   620            rewrite__set_ thy (i + 1) false bdv rls a (*of*);
   621 
   622 (*+*)UnparseC.term a = "x \<noteq> 0"; (* rewrite__set_ \<rightarrow> @{term True} ----------------- SHOULD BE indet !*)
   623 (*
   624  :
   625  ####  rls: rateq_erls on: x \<noteq> 0
   626  :
   627  #####  try calc: HOL.eq'    <<<------------------------------- here the error comes from
   628  =====  calc. to: ~ False    <<<------------------------------- \<not> x = 0 is NOT False
   629  #####  try calc: HOL.eq' 
   630  #####  try thm: not_true 
   631  #####  try thm: not_false 
   632  =====  rewrites to: True    <<<------------------------------- so x \<noteq> 0 is NOT True
   633                                                        and True, False are NOT stored ...
   634  :                             
   635  ###  asms accepted: [x \<noteq> 0]   stored: []
   636  : *)
   637 Rewrite.trace_on := false;
   638 ( *\------- outcomment this code: otherwise the re-definition could infect tests lateron ------/*)
   639 
   640 
   641 "----------- fun rewrite_ down to Pattern.match ------------------------------------------------";
   642 "----------- fun rewrite_ down to Pattern.match ------------------------------------------------";
   643 "----------- fun rewrite_ down to Pattern.match ------------------------------------------------";
   644 "~~~~~ fun rewrite_ , args:"; val (thy, rew_ord, erls, bool, thm, term) =
   645   (@{theory}, dummy_ord, Rule_Set.empty, false, @{thm distrib_left}, @{term "x * (y + z) :: int"});
   646 "~~~~~ fun rewrite__ , args:"; val (thy, i, bdv, tless, rls, put_asm, thm, ct) =
   647   (thy, 1, [], rew_ord, erls, bool, thm, term);
   648 "~~~~~ and rew_sub , args:"; val (thy, i, bdv, tless, rls, put_asm, lrd, r, t) =
   649   (thy, i, bdv, tless, rls, put_asm, [], TermC.inst_bdv bdv (Eval.norm (Thm.prop_of thm)), ct)
   650      val (lhss, rhss) = (HOLogic.dest_eq o Logic.strip_imp_concl o HOLogic.dest_Trueprop) r
   651      val r' = Envir.subst_term (Pattern.match thy (lhss, t) (Vartab.empty, Vartab.empty)) r
   652      val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) (Logic.count_prems r', [], r'))
   653      val t' = (snd o HOLogic.dest_eq o Logic.strip_imp_concl o HOLogic.dest_Trueprop) r'
   654 ;
   655 if UnparseC.term lhss = "?a * (?b + ?c)" andalso UnparseC.term t = "x * (y + z)" then ()
   656 else error "ARGS FOR Pattern.match CHANGED";
   657 val match = Pattern.match thy (lhss, t) (Vartab.empty, Vartab.empty);
   658 if (Envir.subst_term match r |> UnparseC.term) = "x * (y + z) = x * y + x * z" then ()
   659   else error "Pattern.match CHANGED";
   660 
   661 "----------- fun rewrite_set_ .. norm_equation, ~~~~~ fun rewrite_set_ -------------------------";
   662 "----------- fun rewrite_set_ .. norm_equation, ~~~~~ fun rewrite_set_ -------------------------";
   663 "----------- fun rewrite_set_ .. norm_equation, ~~~~~ fun rewrite_set_ -------------------------";
   664 (* norm_equation is defined in Test.thy, other rls see Knowledg/**)
   665 val thy = @{theory};
   666 val rls = norm_equation;
   667 val term = TermC.str2term "x + 1 = 2";
   668 
   669 (**)val SOME (t, asm) = rewrite_set_ thy false rls term;
   670 (** )#####  try thm: "root_ge0" 
   671 exception TERM raised (line 271 of "~~/src/HOL/Tools/hologic.ML"):
   672   dest_eq
   673   0 \<le> ?a \<Longrightarrow> (0 \<le> sqrt ?a) = True( **)
   674 if UnparseC.term t = "x + 1 + -1 * 2 = 0" then () else error "rewrite_set_ norm_equation CHANGED";
   675 
   676 "~~~~~ fun rewrite_set_ , args:"; val (thy, bool, rls, term) = (thy, false, rls, term);
   677 "~~~~~ and rewrite__set_ , args:"; val (thy, i, put_asm, bdv, rls, ct) =
   678   (thy, 1, bool, []: (term * term) list, rls, term);
   679 (*deleted after error detection*)
   680