test/Tools/isac/ProgLang/rewrite.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Mon, 07 Dec 2015 14:10:59 +0100
changeset 59188 c477d0f79ab9
parent 59112 8a4f7ec213f4
child 59252 7d3dbc1171ff
permissions -rw-r--r--
Isabelle2014-->15: closed Thm.thy applied to tests

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