test/Tools/isac/ProgLang/rewrite.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Thu, 08 Feb 2018 12:49:52 +0100
changeset 59358 8509ca4e79ec
parent 59348 ddfabb53082c
child 59381 cb7e75507c05
permissions -rw-r--r--
Isabelle2015->17: thm changed
     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_divide_mult_cancel_right};
    66 val r = Thm.prop_of thm;
    67 val tm = @{term "x / (2 * x)::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_divide_mult_cancel_right};
   128 val rule = Thm.prop_of thm;
   129 val tm = @{term "x / (2 * x)::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 thm =  @{thm nonzero_divide_mult_cancel_right};
   149 val tm = @{term "x / (2 * x)::real"};
   150 val SOME (rew, asm) = (rewrite_ thy dummy_ord e_rls false thm tm)
   151   handle _ => error "rewrite.sml diff.behav. in cond.rew.";
   152 
   153 if rew = @{term "1 / 2::real"} then () (* the rewrite is _NO_ Trueprop *)
   154 else error "rewrite.sml diff.result in cond.rew.";
   155 
   156 if hd asm = @{term "x \<noteq> (0::real)"} (* dropped Trueprop $ ...*)
   157 then () else error "rewrite.sml diff.asm in cond.rew.";
   158 "----- conditional rewriting immediately: can only be done with ";
   159 "------Isabelle numerals, because erls cannot handle them yet.";
   160 
   161 
   162 "----------- step through 'and rew_sub': ----------------";
   163 "----------- step through 'and rew_sub': ----------------";
   164 "----------- step through 'and rew_sub': ----------------";
   165 (*and make asms without Trueprop, beginning with the result:*)
   166 val tm = @{term "x / (2 * x)::real"};
   167 val (t', asm, _, _) = rew_sub thy 0 [] dummy_ord e_rls true [] (Thm.prop_of thm) tm;
   168 (*show_types := false;*)
   169 "----- evaluate arguments";
   170 val (thy, i, bdv, tless, rls, put_asm, lrd, r, t) = 
   171     (thy, 0, [], dummy_ord, e_rls, true, [], (Thm.prop_of thm), tm);
   172 "----- step 1: LHS, RHS of rule";
   173      val (LHS, RHS) = (HOLogic.dest_eq o HOLogic.dest_Trueprop
   174                        o Logic.strip_imp_concl) r;
   175 term2str r = "?b \<noteq> (0::?'a) \<Longrightarrow> ?b / (?a * ?b) = (1::?'a) / ?a";
   176 term2str LHS = "?b / (?a * ?b)"; term2str RHS = "(1::?'a) / ?a";
   177 "----- step 2: the rule instantiated";
   178      val r' = Envir.subst_term 
   179                   (Pattern.match thy (LHS, t) (Vartab.empty, Vartab.empty)) r;
   180 term2str r' = "x \<noteq> 0 \<Longrightarrow> x / (2 * x) = 1 / 2";
   181 "----- step 3: get the (instantiated) assumption(s)";
   182      val p' = (fst o Logic.strip_prems) (Logic.count_prems r', [], r');
   183 term2str (hd p') = "x \<noteq> 0";
   184 "=====vvv make asms without Trueprop ---vvv";
   185      val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) 
   186                                              (Logic.count_prems r', [], r'));
   187 case p' of
   188     [Const ("HOL.Not", _) $ (Const ("HOL.eq", _) 
   189       $ Free ("x", _) $ Const ("Groups.zero_class.zero", _))] => ()
   190   | _ => error "rewrite.sml assumption changed";
   191 "=====^^^ make asms without Trueprop ---^^^";
   192 "----- step 4: get the (instantiated) RHS";
   193      val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop 
   194                o Logic.strip_imp_concl) r';
   195 term2str t' = "1 / 2";
   196 
   197 "----------- step through 'fun rewrite_terms_'  ---------";
   198 "----------- step through 'fun rewrite_terms_'  ---------";
   199 "----------- step through 'fun rewrite_terms_'  ---------";
   200 "----- step 0: args for rewrite_terms_, local fun";
   201 val (thy, ord, erls, equs, t) =
   202     (@{theory "Biegelinie"}, dummy_ord, Erls, [str2term "x = 0"],
   203      str2term "M_b x = -1 * q_0 * x ^^^ 2 / 2 + x * c + c_2");
   204 "----- step 1: args for rew_";
   205 val ((t', asm'), (rules as r::rs), t) = ((e_term, []), equs, t);
   206 "----- step 2: rew_sub";
   207 rew_sub thy 1 [] ord erls false [] (Trueprop $ r) t;
   208 "----- step 3: step through rew_sub -- inefficient: goes into subterms";
   209 
   210 
   211 val SOME (t', _) = rewrite_terms_ thy dummy_ord Erls equs t;
   212 writeln "----------- rewrite_terms_  1---------------------------";
   213 if term2str t' = "M_b 0 = -1 * q_0 * 0 ^^^ 2 / 2 + 0 * c + c_2" then ()
   214 else error "rewrite.sml rewrite_terms_ [x = 0]";
   215 
   216 val equs = [str2term "M_b 0 = 0"];
   217 val t = str2term "M_b 0 = -1 * q_0 * 0 ^^^ 2 / 2 + 0 * c + c_2";
   218 val SOME (t', _) = rewrite_terms_ thy dummy_ord Erls equs t;
   219 writeln "----------- rewrite_terms_  2---------------------------";
   220 if term2str t' = "0 = -1 * q_0 * 0 ^^^ 2 / 2 + 0 * c + c_2" then ()
   221 else error "rewrite.sml rewrite_terms_ [M_b 0 = 0]";
   222 
   223 val equs = [str2term "x = 0", str2term"M_b 0 = 0"];
   224 val t = str2term "M_b x = -1 * q_0 * x ^^^ 2 / 2 + x * c + c_2";
   225 val SOME (t', _) = rewrite_terms_ thy dummy_ord Erls equs t;
   226 writeln "----------- rewrite_terms_  3---------------------------";
   227 if term2str t' = "0 = -1 * q_0 * 0 ^^^ 2 / 2 + 0 * c + c_2" then ()
   228 else error "rewrite.sml rewrite_terms_ [x = 0, M_b 0 = 0]";
   229 
   230 
   231 "----------- rewrite_inst_ bdvs -------------------------";
   232 "----------- rewrite_inst_ bdvs -------------------------";
   233 "----------- rewrite_inst_ bdvs -------------------------";
   234 (*see smltest/Scripts/term_G.sml: inst_bdv 2*)
   235 val t = str2term"-1 * (q_0 * L ^^^ 2) / 2 + (L * c_3 + c_4) = 0";
   236 val bdvs = [(str2term"bdv_1",str2term"c"),
   237 	    (str2term"bdv_2",str2term"c_2"),
   238 	    (str2term"bdv_3",str2term"c_3"),
   239 	    (str2term"bdv_4",str2term"c_4")];
   240 (*------------ outcommented WN071210, after inclusion into ROOT.ML 
   241 val SOME (t,_) = 
   242     rewrite_inst_ thy e_rew_ord 
   243 		  (append_rls "erls_isolate_bdvs" e_rls 
   244 			      [(Calc ("EqSystem.occur'_exactly'_in", 
   245 				      eval_occur_exactly_in 
   246 					  "#eval_occur_exactly_in_"))
   247 			       ]) 
   248 		  false bdvs (num_str @{separate_bdvs_add) t;
   249 (writeln o term2str) t;
   250 if term2str t = "L * c_3 + c_4 = 0 + -1 * (-1 * (q_0 * L ^^^ 2) / 2)"
   251 then () else error "rewrite.sml rewrite_inst_ bdvs";
   252 trace_rewrite:=true;
   253 trace_rewrite:=false;--------------------------------------------*)
   254 
   255 
   256 "----------- check diff 2002--2009-3 --------------------";
   257 "----------- check diff 2002--2009-3 --------------------";
   258 "----------- check diff 2002--2009-3 --------------------";
   259 (*----- 2002 -------------------------------------------------------------------
   260 #  rls: norm_Rational_noadd_fractions on: 1 / EI * (L * q_0 * x / 2 + -1 *
   261 q_0 * x ^^^ 2 / 2)
   262 ##  rls: discard_minus_ on: 1 / EI * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2
   263 / 2)
   264 ###  try thm: real_diff_minus
   265 ###  try thm: sym_real_mult_minus1
   266 ##  rls: rat_mult_poly on: 1 / EI * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2
   267 / 2)
   268 ###  try thm: rat_mult_poly_l
   269 ###  try thm: rat_mult_poly_r
   270 ####  eval asms: L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2 is_polyexp
   271 ==> 1 / EI * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2) =
   272     1 * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2) / EI
   273 #####  rls: e_rls-is_polyexp on: L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2
   274 is_polyexp
   275 ######  try calc: Poly.is'_polyexp'
   276 ======  calc. to: False
   277 ######  try calc: Poly.is'_polyexp'
   278 ######  try calc: Poly.is'_polyexp'
   279 ####  asms false: ["L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2 is_polyexp"]
   280 ----- 2002 NONE rewrite --------------------------------------------------------
   281 ----- 2009 should maintain this behaviour, but: --------------------------------
   282 #  rls: norm_Rational_noadd_fractions on: 1 / EI * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2)
   283 ##  rls: discard_minus on: 1 / EI * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2)
   284 ###  try thm: real_diff_minus
   285 ###  try thm: sym_real_mult_minus1
   286 ##  rls: rat_mult_poly on: 1 / EI * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2)
   287 ###  try thm: rat_mult_poly_l
   288 ###  try thm: rat_mult_poly_r
   289 ####  eval asms: L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2 is_polyexp
   290 ==> 1 / EI * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2) =
   291     1 * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2) / EI
   292 #####  rls: e_rls-is_polyexp on: L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2 is_polyexp
   293 ######  try calc: Poly.is'_polyexp'
   294 ======  calc. to: False
   295 ######  try calc: Poly.is'_polyexp'
   296 ######  try calc: Poly.is'_polyexp'
   297 ####  asms accepted: ["L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2 is_polyexp"]   stored: ["False"]
   298 ===  rewrites to: 1 * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2) / EI
   299 ----- 2009 -------------------------------------------------------------------*)
   300 
   301 (*the situation as was before repair (asm without Trueprop) is outcommented*)
   302 val thy = @{theory "Isac"};
   303 "===== example which raised the problem =================";
   304 val t = @{term "1 / EI * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2)"};
   305 "----- rewrite_set_inst_ norm_Rational_noadd_fractions--";
   306 val subs = [(str2term "bdv", str2term "x")];
   307 val rls = norm_Rational_noadd_fractions;
   308 val SOME (t', asms) = rewrite_set_inst_ thy true subs rls t;
   309 if term2str t' = "1 / EI * (L * q_0 * x / 2 + -1 * 1 * q_0 * x ^^^ 2 / 2)" andalso
   310   terms2str asms = "[]" then {}
   311 else error "this was NONE with Isabelle2013-2 ?!?"
   312 "----- rewrite_ rat_mult_poly_r--------------------------";
   313 val thm = @{thm rat_mult_poly_r};
   314          "?c::real is_polyexp ==> (?a::real) / (?b::real) * ?c = ?a * ?c / ?b";
   315 val erls = append_rls "e_rls-is_polyexp" e_rls 
   316                       [Calc ("Poly.is'_polyexp", eval_is_polyexp "")];
   317 val NONE (*SOME (t'', _)*) = rewrite_ thy dummy_ord erls true thm t;
   318 (*t' = t''; (*false because of further rewrites in t'*)*)
   319 "----- rew_sub  --------------------------------";
   320 val (t''', _, _, _) = rew_sub thy 0 [] dummy_ord erls true [] (Thm.prop_of thm) t;
   321 (*t'' = t'''; (*true*)*)
   322 "----- rewrite_set_ erls --------------------------------";
   323 val cond = @{term "(L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2)"};
   324 val NONE = rewrite_set_ thy true erls cond; 
   325 (* ^^^^^ goes with '======  calc. to: False' above from beginning*)
   326 
   327 writeln "===== maximally simplified example =====================";
   328 val t = @{term "a / b * (x / x ^^^ 2)"};
   329          "?c::real is_polyexp ==> (?a::real) / (?b::real) * ?c = ?a * ?c / ?b";
   330 writeln "----- rewrite_set_inst_ norm_Rational_noadd_fractions--";
   331 val (*NONE*) SOME (t', asm) = rewrite_set_inst_ thy true subs rls t;
   332 term2str t' = "a * x / (b * x ^^^ 2)"; (*rew. by thm outside test case*)
   333 (*checked visually: trace_rewrite looks like above for 2009*)
   334 
   335 writeln "----- rewrite_ rat_mult_poly_r--------------------------";
   336 val NONE (*SOME (t'', _)*) = rewrite_ thy dummy_ord erls true thm t;
   337 (*t' = t''; (*false because of further rewrites in t'*)*)
   338 writeln "----- rew_sub  --------------------------------";
   339 val (t''', _, _, _) = rew_sub thy 0 [] dummy_ord erls true [] (Thm.prop_of thm) t;
   340 (*t'' = t'''; (*true*)*)
   341 writeln "----- rewrite_set_ erls --------------------------------";
   342 val cond = @{term "(x / x ^^^ 2)"};
   343 val NONE = rewrite_set_ thy true erls cond; 
   344 (* ^^^^^ goes with '======  calc. to: False' above from beginning*)
   345 
   346 
   347 "----------- compare all prepat's existing 2010 ---------";
   348 "----------- compare all prepat's existing 2010 ---------";
   349 "----------- compare all prepat's existing 2010 ---------";
   350 val thy = @{theory "Isac"};
   351 val t = @{term "a + b * x = (0 ::real)"};
   352 val pat = parse_patt thy "?l = (?r ::real)";
   353 val precond = parse_patt thy "is_polynomial (?l::real)";(*no infix def*)
   354 val precond = parse_patt thy "(?l::real) is_expanded"; 
   355 
   356 val inst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
   357 val preinst = Envir.subst_term inst precond;
   358 term2str preinst;
   359 
   360 "===== Rational.thy: cancel ===";
   361 (* pat matched with the current term gives an environment 
   362    (or not: hen the Rrls not applied);
   363    if pre1 and pre2 evaluate to @{term True} in this environment,
   364    then the Rrls is applied. *)
   365 val t = str2term "(a + b) / c ::real";
   366 val pat = parse_patt thy "?r / ?s ::real";
   367 val pres = [parse_patt thy "?r is_expanded", parse_patt thy "?s is_expanded"];
   368 val prepat = [(pres, pat)];
   369 val erls = rational_erls;
   370 (* erls got from Rrls {erls, prepat, scr = Rfuns {normal_form, ...}, ...} *)
   371 
   372 val subst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
   373 val asms = map (Envir.subst_term subst) pres;
   374 if terms2str asms = "[\"a + b is_expanded\",\"c is_expanded\"]"
   375 then () else error "rewrite.sml: prepat cancel subst";
   376 if ([], true) = eval__true thy 0 asms [] erls
   377 then () else error "rewrite.sml: prepat cancel eval__true";
   378 
   379 "===== Rational.thy: add_fractions_p ===";
   380 (* if each pat* matches with the current term, the Rrls is applied
   381    (there are no preconditions to be checked, they are @{term True}) *)
   382 val t = str2term "a / b + 1 / 2";
   383 val pat = parse_patt thy "?r / ?s + ?u / ?v";
   384 val pres = [@{term True}];
   385 val prepat = [(pres, pat)];
   386 val erls = rational_erls;
   387 (* erls got from Rrls {erls, prepat, scr = Rfuns {normal_form, ...}, ...} *)
   388 
   389 val subst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
   390 if ([], true) = eval__true thy 0 (map (Envir.subst_term subst) pres) [] erls
   391 then () else error "rewrite.sml: prepat add_fractions_p";
   392 
   393 "===== Poly.thy: order_mult_ ===";
   394           (* ?p matched with the current term gives an environment,
   395              which evaluates (the instantiated) "p is_multUnordered" to true*)
   396 val t = str2term "x^^^2 * x";
   397 val pat = parse_patt thy "?p :: real"
   398 val pres = [parse_patt thy "?p is_multUnordered"];
   399 val prepat = [(pres, pat)];
   400 val erls = append_rls "e_rls-is_multUnordered" e_rls
   401 		      [Calc ("Poly.is'_multUnordered", 
   402                              eval_is_multUnordered "")];
   403 
   404 val subst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
   405 val asms = map (Envir.subst_term subst) pres;
   406 if terms2str asms = "[\"x ^^^ 2 * x is_multUnordered\"]"
   407 then () else error "rewrite.sml: prepat order_mult_ subst";
   408 if ([], true) = eval__true thy 0 asms [] erls
   409 then () else error "rewrite.sml: prepat order_mult_ eval__true";
   410 
   411 
   412 "----------- fun app_rev, Rrls, -------------------------";
   413 "----------- fun app_rev, Rrls, -------------------------";
   414 "----------- fun app_rev, Rrls, -------------------------";
   415 val t = str2term "x^^^2 * x";
   416 
   417 if is_multUnordered t then () else error "rewrite.sml diff. is_multUnordered 2";
   418 val tm = str2term "(x^^^2 * x) is_multUnordered";
   419 eval_is_multUnordered "testid" "" tm thy;
   420 
   421 case eval_is_multUnordered "testid" "" tm thy of
   422     SOME (_, Const ("HOL.Trueprop", _) $ 
   423                    (Const ("HOL.eq", _) $
   424                           (Const ("Poly.is'_multUnordered", _) $ _) $ 
   425                           Const ("HOL.True", _))) => ()
   426   | _ => error "rewrite.sml diff. eval_is_multUnordered 2b";
   427 
   428 tracing "----- begin rewrite x^^^2 * x ---"; trace_rewrite := true;
   429 val SOME (t', _) = rewrite_set_ thy true order_mult_ t;
   430 tracing "----- end rewrite x^^^2 * x ---"; trace_rewrite := false;
   431 if term2str t' = "x * x ^^^ 2" then ()
   432 else error "rewrite.sml Poly.is'_multUnordered doesn't work";
   433 
   434 (* for achieving the previous result, the following code was taken apart *)
   435 "----- rewrite__set_ ---";
   436 val (thy, i, _, _, (rrls as Rrls _), t) = (thy, 0, true, [], order_mult_, tm);
   437 	val (t', asm, rew) = app_rev thy (i+1) rrls t;
   438 "----- app_rev ---";
   439 val (thy, i, rrls, t) = (thy, (i+1), rrls, t);
   440 	fun chk_prepat thy erls [] t = true
   441 	  | chk_prepat thy erls prepat t =
   442 	    let fun chk (pres, pat) =
   443 		    (let val subst: Type.tyenv * Envir.tenv = 
   444 			     Pattern.match thy (pat, t)
   445 					    (Vartab.empty, Vartab.empty)
   446 		     in snd (eval__true thy (i+1) 
   447 					(map (Envir.subst_term subst) pres)
   448 					[] erls)
   449 		     end)
   450 		    handle _ => false
   451 		fun scan_ f [] = false (*scan_ NEVER called by []*)
   452 		  | scan_ f (pp::pps) = if f pp then true
   453 					else scan_ f pps;
   454 	    in scan_ chk prepat end;
   455 
   456 	(*.apply the normal_form of a rev-set.*)
   457 	fun app_rev' thy (Rrls{erls,prepat,scr=Rfuns{normal_form,...},...}) t =
   458 	    if chk_prepat thy erls prepat t
   459 	    then ((*tracing("### app_rev': t = "^(term2str t));*)
   460                   normal_form t)
   461 	    else NONE;
   462 (*fixme val NONE = app_rev' thy rrls t;*)
   463 "----- app_rev' ---";
   464 val (thy, Rrls{erls,prepat,scr=Rfuns{normal_form,...},...}, t) = (thy, rrls, t);
   465 (*fixme false*)   chk_prepat thy erls prepat t;
   466 "----- chk_prepat ---";
   467 val (thy, erls, prepat, t) = (thy, erls, prepat, t);
   468                 fun chk (pres, pat) =
   469 		    (let val subst: Type.tyenv * Envir.tenv = 
   470 			     Pattern.match thy (pat, t)
   471 					    (Vartab.empty, Vartab.empty)
   472 		     in snd (eval__true thy (i+1) 
   473 					(map (Envir.subst_term subst) pres)
   474 					[] erls)
   475 		     end)
   476 		    handle _ => false;
   477 		fun scan_ f [] = false (*scan_ NEVER called by []*)
   478 		  | scan_ f (pp::pps) = if f pp then true
   479 					else scan_ f pps;
   480 tracing "=== poly.sml: scan_ chk prepat begin";
   481 scan_ chk prepat;
   482 tracing "=== poly.sml: scan_ chk prepat end";
   483 
   484 "----- chk ---";
   485 (*reestablish...*) val t = str2term "x ^^^ 2 * x";
   486 val [(pres, pat)] = prepat;
   487                          val subst: Type.tyenv * Envir.tenv = 
   488 			     Pattern.match thy (pat, t)
   489 					    (Vartab.empty, Vartab.empty);
   490 
   491 (*fixme: asms = ["p is_multUnordered"]...instantiate*)
   492 "----- eval__true ---";
   493 val asms = (map (Envir.subst_term subst) pres);
   494 if terms2str asms = "[\"x ^^^ 2 * x is_multUnordered\"]" then ()
   495 else error "rewrite.sml: diff. is_multUnordered, asms";
   496 val (thy, i, asms, bdv, rls) = 
   497     (thy, (i+1), [str2term "(x^^^2 * x) is_multUnordered"], 
   498      [] : (term * term) list, erls);
   499 case eval__true thy i asms bdv rls of 
   500     ([], true) => ()
   501   | _ => error "rewrite.sml: diff. is_multUnordered, eval__true";
   502 
   503 "----------- 2011 thms with axclasses -------------------";
   504 "----------- 2011 thms with axclasses -------------------";
   505 "----------- 2011 thms with axclasses -------------------";
   506 val thm = num_str @{thm div_by_1};
   507 val prop = Thm.prop_of thm;
   508 atomty prop;                                     (*Type 'a*)
   509 val t = str2term "(2*x)/1";                      (*Type real*)
   510 
   511 val (thy, ro, er, inst) = 
   512     (@{theory "Isac"}, tless_true, eval_rls, [(@{term "bdv::real"}, @{term "x::real"})]);
   513 val SOME (t, _) = rewrite_ thy ro er true thm t; (*real matches 'a ?via ring? etc*)
   514 
   515 "----------- repair NO asms from rls RatEq_eliminate ----";
   516 "----------- repair NO asms from rls RatEq_eliminate ----";
   517 "----------- repair NO asms from rls RatEq_eliminate ----";
   518 val t = str2term "1 / x = 5";
   519 trace_rewrite := true;
   520 val SOME (t', asm) = rewrite_ thy e_rew_ord e_rls true @{thm rat_mult_denominator_right} t;
   521 term2str t' = "1 = 5 * x";
   522 terms2str asm = "[\"x ~= 0\"]";
   523 (*
   524  ##  eval asms: x ~= 0 ==> (1 / x = 5) = (1 = 5 * x) 
   525  ###  rls: e_rls on: x ~= 0 
   526  ##  asms accepted: ["x ~= 0"]   stored: ["x ~= 0"] 
   527 *)
   528 trace_rewrite := false;
   529 
   530 trace_rewrite := false;
   531 val SOME (t', []) = rewrite_set_ thy true RatEq_eliminate t; (*= [] must be = "x ~= 0"*)
   532 term2str t' = "1 = 5 * x";
   533 (*
   534  :
   535  ####  rls: rateq_erls on: x ~= 0 
   536  :
   537  #####  try calc: HOL.eq'    <<<------------------------------- here the error comes from
   538  =====  calc. to: ~ False 
   539  #####  try calc: HOL.eq' 
   540  #####  try thm: not_true 
   541  #####  try thm: not_false 
   542  =====  rewrites to: True 
   543  :
   544  ###  asms accepted: ["x ~= 0"]   stored: []
   545  :
   546 *)
   547 trace_rewrite := false;
   548 (* WN120317.TODO dropped rateq: the above error is the same in 2002 *)
   549 
   550 "----------- fun assoc_thm' -----------------------------";
   551 "----------- fun assoc_thm' -----------------------------";
   552 "----------- fun assoc_thm' -----------------------------";
   553 val thy = @{theory ProgLang}
   554 
   555 val tth = assoc_thm' thy ("sym_#mult_2_3","6 = 2 * 3");
   556 if string_of_thm' thy tth = "6 = 2 * 3" then ()
   557 else error "assoc_thm' (sym_#mult_2_3, 6 = 2 * 3) changed";
   558 
   559 val tth = assoc_thm' thy ("add_0_left","");
   560 if string_of_thm' thy tth = "0 + ?a = ?a" then ()
   561 else error "assoc_thm' (add_0_left,\"\") changed";
   562 
   563 val tth = assoc_thm' thy ("sym_add_0_left","");
   564 if string_of_thm' thy tth = "?t = 0 + ?t" then ()
   565 else error "assoc_thm' (sym_add_0_left,\"\") changed";
   566 
   567 val tth = assoc_thm' thy ("add_commute","");
   568 if string_of_thm' thy tth = "?a + ?b = ?b + ?a" then ()
   569 else error "assoc_thm' (add_commute,\"\") changed"
   570 
   571 "----------- fun rewrite_ down to Pattern.match ------------------------------------------------";
   572 "----------- fun rewrite_ down to Pattern.match ------------------------------------------------";
   573 "----------- fun rewrite_ down to Pattern.match ------------------------------------------------";
   574 "~~~~~ fun rewrite_ , args:"; val (thy, rew_ord, erls, bool, thm, term) =
   575   (@{theory}, dummy_ord, e_rls, false, @{thm distrib_left}, @{term "x * (y + z) :: int"});
   576 "~~~~~ fun rewrite__ , args:"; val (thy, i, bdv, tless, rls, put_asm, thm, ct) =
   577   (thy, 1, [], rew_ord, erls, bool, thm, term);
   578 "~~~~~ and rew_sub , args:"; val (thy, i, bdv, tless, rls, put_asm, lrd, r, t) =
   579   (thy, i, bdv, tless, rls, put_asm, [], (((inst_bdv bdv) o norm o #prop o Thm.rep_thm) thm), ct)
   580      val (lhss, rhss) = (HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r
   581      val r' = Envir.subst_term (Pattern.match thy (lhss, t) (Vartab.empty, Vartab.empty)) r
   582      val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) (Logic.count_prems r', [], r'))
   583      val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r'
   584 ;
   585 if term2str lhss = "?a * (?b + ?c)" andalso term2str t = "x * (y + z)" then ()
   586 else error "ARGS FOR Pattern.match CHANGED";
   587 val match = Pattern.match thy (lhss, t) (Vartab.empty, Vartab.empty);
   588 if (Envir.subst_term match r |> term2str) = "x * (y + z) = x * y + x * z" then ()
   589   else error "Pattern.match CHANGED";