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