test/Tools/isac/ProgLang/rewrite.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Tue, 18 Oct 2016 12:05:03 +0200
changeset 59252 7d3dbc1171ff
parent 59188 c477d0f79ab9
child 59348 ddfabb53082c
permissions -rw-r--r--
back-track after desing error in previous changeset

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