test/Tools/isac/ProgLang/rewrite.sml
author Walther Neuper <neuper@ist.tugraz.at>
Fri, 01 Oct 2010 10:23:38 +0200
branchisac-update-Isa09-2
changeset 38036 02a9b2540eb7
parent 38031 460c24a6a6ba
child 38039 99cb0d80ff32
permissions -rw-r--r--
repaired 'prepat's, the patterns and preconditions for Rrls

fun parse_patt still lacks numbers_to_string, typ_a2real
because this causes a strange error in Poly.thy to be removed next
neuper@38036
     1
(* Title: tests for ProgLang/rewrite.sml
neuper@37906
     2
   TODO.WN0509 collect typical tests from systest here !!!!!
neuper@38036
     3
   Author: Walther Neuper 050908
neuper@37906
     4
   (c) copyright due to lincense terms.
neuper@37906
     5
neuper@38022
     6
12345678901234567890123456789012345678901234567890123456789012345678901234567890
neuper@38022
     7
        10        20        30        40        50        60        70        80
neuper@37906
     8
*)
neuper@37906
     9
neuper@38022
    10
"--------------------------------------------------------";
neuper@38022
    11
"table of contents --------------------------------------";
neuper@38022
    12
"--------------------------------------------------------";
neuper@38022
    13
"----------- assemble rewrite ---------------------------";
neuper@38022
    14
"----------- test rewriting without Isac's thys ---------";
neuper@38022
    15
"----------- conditional rewriting without Isac's thys --";
neuper@38022
    16
"----------- step through 'and rew_sub': ----------------";
neuper@38025
    17
"----------- step through 'fun rewrite_terms_'  ---------";
neuper@38022
    18
"----------- rewrite_inst_ bdvs -------------------------";
neuper@38022
    19
"----------- check diff 2002--2009-3 --------------------";
neuper@38036
    20
"----------- fun app_rev, Rrls, prepat ------------------";
neuper@38022
    21
"--------------------------------------------------------";
neuper@38022
    22
"--------------------------------------------------------";
neuper@38022
    23
"--------------------------------------------------------";
neuper@37906
    24
neuper@38036
    25
(*-.-.-.-.-.-isolate response.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
neuper@38036
    26
neuper@38022
    27
"----------- assemble rewrite ---------------------------";
neuper@38022
    28
"----------- assemble rewrite ---------------------------";
neuper@38022
    29
"----------- assemble rewrite ---------------------------";
neuper@37906
    30
"===== rewriting by thm with 'a";
neuper@37906
    31
show_types := true;
neuper@37906
    32
val thy = @{theory Complex_Main};
neuper@37906
    33
val ctxt = @{context};
neuper@37906
    34
val thm = @{thm add_commute};
neuper@37906
    35
val t = (term_of o the) (parse thy "((r + u) + t) + s");
neuper@37906
    36
"----- from old: fun rewrite__";
neuper@37906
    37
val bdv = [];
neuper@37906
    38
val r = (((inst_bdv bdv) o norm o #prop o rep_thm) thm);
neuper@37906
    39
"----- from old: and rew_sub";
neuper@37906
    40
val (lhs,rhs) = (dest_equals' o strip_trueprop 
neuper@37906
    41
   	      o Logic.strip_imp_concl) r;
neuper@37906
    42
(* old
neuper@37906
    43
val insts = Pattern.match thy (lhs,t) (Vartab.empty, Vartab.empty);*)
neuper@37906
    44
"----- fun match_rew in Pure/pattern.ML";
neuper@37906
    45
val rtm = the_default rhs (Term.rename_abs lhs t rhs);
neuper@37906
    46
neuper@38022
    47
writeln(Syntax.string_of_term ctxt rtm);
neuper@38022
    48
writeln(Syntax.string_of_term ctxt lhs);
neuper@38022
    49
writeln(Syntax.string_of_term ctxt t);
neuper@37906
    50
neuper@37906
    51
(Pattern.match thy (lhs, t) (Vartab.empty, Vartab.empty));
neuper@37906
    52
val (rew, rhs) = (Envir.subst_term 
neuper@37906
    53
  (Pattern.match thy (lhs, t) (Vartab.empty, Vartab.empty)) rtm, rtm);
neuper@37906
    54
(*lookup in isabelle?trace?response...*)
neuper@37906
    55
writeln(Syntax.string_of_term ctxt rew);
neuper@37906
    56
writeln(Syntax.string_of_term ctxt rhs);
neuper@38022
    57
neuper@37906
    58
"===== rewriting: prep insertion into rew_sub";
neuper@37906
    59
val thy = @{theory Complex_Main};
neuper@37906
    60
val ctxt = @{context};
neuper@37906
    61
val thm =  @{thm nonzero_mult_divide_cancel_right};
neuper@37906
    62
val r = Thm.prop_of thm;
neuper@37906
    63
val tm = @{term "x*2 / 2::real"};
neuper@37906
    64
"----- and rew_sub";
neuper@37906
    65
val (lhs, rhs) = (HOLogic.dest_eq o HOLogic.dest_Trueprop
neuper@37906
    66
                  o Logic.strip_imp_concl) r;
neuper@37906
    67
val r' = Envir.subst_term (Pattern.match thy (lhs, tm) 
neuper@37906
    68
                                (Vartab.empty, Vartab.empty)) r;
neuper@37906
    69
val p' = (fst o Logic.strip_prems) (Logic.count_prems r', [], r');
neuper@37906
    70
val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop 
neuper@37906
    71
            o Logic.strip_imp_concl) r';
neuper@37906
    72
neuper@37906
    73
(*is displayed on top of <response> buffer...*)
neuper@37906
    74
Pretty.writeln (ProofContext.pretty_term_abbrev @{context} r');
neuper@37906
    75
Pretty.writeln (ProofContext.pretty_term_abbrev @{context} t');
neuper@37906
    76
(*}*)
neuper@37906
    77
neuper@38022
    78
"----------- test rewriting without Isac's thys ---------";
neuper@38022
    79
"----------- test rewriting without Isac's thys ---------";
neuper@38022
    80
"----------- test rewriting without Isac's thys ---------";
neuper@38022
    81
neuper@37906
    82
"===== rewriting with Isabelle2009-1 only, i.e without isac-hacks";
neuper@37906
    83
val thy = @{theory Complex_Main};
neuper@37906
    84
val ctxt = @{context};
neuper@37906
    85
val thm =  @{thm add_commute};
neuper@37906
    86
val tm = @{term "x + y*z::real"};
neuper@37906
    87
neuper@37906
    88
val SOME (r,_) = (rewrite_ thy dummy_ord e_rls false thm tm)
neuper@38022
    89
  handle _ => error "rewrite.sml diff.behav. in rewriting";
neuper@37906
    90
(*is displayed on _TOP_ of <response> buffer...*)
neuper@37906
    91
Pretty.writeln (ProofContext.pretty_term_abbrev @{context} r);
neuper@38022
    92
if r = @{term "y*z + x::real"}
neuper@38022
    93
then () else error "rewrite.sml diff.result in rewriting";
neuper@37906
    94
neuper@37906
    95
"----- rewriting a subterm";
neuper@37906
    96
val tm = @{term "w*(x + y*z)::real"};
neuper@37906
    97
neuper@37906
    98
val SOME (r,_) = (rewrite_ thy dummy_ord e_rls false thm tm)
neuper@38022
    99
  handle _ => error "rewrite.sml diff.behav. in rew_sub";
neuper@37906
   100
neuper@37906
   101
"----- ordered rewriting";
neuper@38022
   102
fun tord (_:subst) pp = Term_Ord.termless pp;
neuper@37906
   103
if tord [] (@{term "x + y*z::real"}, @{term "y*z + x::real"}) then ()
neuper@38022
   104
else error "rewrite.sml diff.behav. in ord.rewr.";
neuper@37906
   105
neuper@37906
   106
val NONE = (rewrite_ thy tord e_rls false thm tm)
neuper@38022
   107
  handle _ => error "rewrite.sml diff.behav. in rewriting";
neuper@37906
   108
(*is displayed on _TOP_ of <response> buffer...*)
neuper@37906
   109
Pretty.writeln (ProofContext.pretty_term_abbrev @{context} r);
neuper@37906
   110
neuper@37906
   111
val tm = @{term "x*y + z::real"};
neuper@37906
   112
val SOME (r,_) = (rewrite_ thy tord e_rls false thm tm)
neuper@38022
   113
  handle _ => error "rewrite.sml diff.behav. in rewriting";
neuper@37906
   114
neuper@37906
   115
neuper@38022
   116
"----------- conditional rewriting without Isac's thys --";
neuper@38022
   117
"----------- conditional rewriting without Isac's thys --";
neuper@38022
   118
"----------- conditional rewriting without Isac's thys --";
neuper@37906
   119
(*ML {*)
neuper@37906
   120
"===== prepr cond.rew. with Pattern.match";
neuper@37906
   121
val thy = @{theory Complex_Main};
neuper@37906
   122
val ctxt = @{context};
neuper@37906
   123
val thm =  @{thm nonzero_mult_divide_cancel_right};
neuper@37906
   124
val rule = Thm.prop_of thm;
neuper@37906
   125
val tm = @{term "x*2 / 2::real"};
neuper@37906
   126
neuper@37906
   127
val prem = Logic.strip_imp_prems rule;
neuper@37906
   128
val nps = Logic.count_prems rule;
neuper@37906
   129
val prems = Logic.strip_prems (nps, [], rule);
neuper@37906
   130
neuper@37906
   131
val eq = Logic.strip_imp_concl rule;
neuper@37906
   132
val (lhs, rhs) = (HOLogic.dest_eq o HOLogic.dest_Trueprop) eq;
neuper@37906
   133
neuper@37906
   134
val mtcs = Pattern.match thy (lhs, tm) (Vartab.empty, Vartab.empty);
neuper@37906
   135
val rule' = Envir.subst_term mtcs rule;
neuper@37906
   136
neuper@37906
   137
val prems' = (fst o Logic.strip_prems) 
neuper@37906
   138
              (Logic.count_prems rule', [], rule');
neuper@37906
   139
val rhs' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop 
neuper@37906
   140
            o Logic.strip_imp_concl) rule';
neuper@37906
   141
neuper@37906
   142
"----- conditional rewriting creating an assumption";
neuper@37906
   143
"----- conditional rewriting creating an assumption";
neuper@37906
   144
val tm = @{term "x*y / y::real"};
neuper@38022
   145
val SOME (rew, asm) = (rewrite_ thy dummy_ord e_rls false thm tm)
neuper@38022
   146
  handle _ => error "rewrite.sml diff.behav. in cond.rew.";
neuper@37906
   147
neuper@38022
   148
if rew = @{term "x::real"} then () (* the rewrite is _NO_ Trueprop *)
neuper@38022
   149
else error "rewrite.sml diff.result in cond.rew.";
neuper@37906
   150
neuper@38022
   151
if hd asm = @{term "~ y = (0::real)"} (* dropped Trueprop $ ...*)
neuper@38022
   152
then () else error "rewrite.sml diff.asm in cond.rew.";
neuper@38022
   153
"----- conditional rewriting immediately: can only be done with ";
neuper@38022
   154
"------Isabelle numerals, because erls cannot handle them yet.";
neuper@37906
   155
neuper@37906
   156
neuper@38022
   157
"----------- step through 'and rew_sub': ----------------";
neuper@38022
   158
"----------- step through 'and rew_sub': ----------------";
neuper@38022
   159
"----------- step through 'and rew_sub': ----------------";
neuper@38022
   160
(*and make asms without Trueprop, beginning with the result:*)
neuper@38025
   161
val tm = @{term "x*y / y::real"};
neuper@38022
   162
val (t', asm, _, _) = rew_sub thy 0 [] dummy_ord e_rls true [] (prop_of thm) tm;
neuper@38022
   163
show_types := false;
neuper@38022
   164
"----- evaluate arguments";
neuper@38022
   165
val (thy, i, bdv, tless, rls, put_asm, lrd, r, t) = 
neuper@38022
   166
    (thy, 0, [], dummy_ord, e_rls, true, [], (prop_of thm), tm);
neuper@38022
   167
"----- step 1: lhs, rhs of rule";
neuper@38022
   168
     val (lhs, rhs) = (HOLogic.dest_eq o HOLogic.dest_Trueprop
neuper@38022
   169
                       o Logic.strip_imp_concl) r;
neuper@38025
   170
term2str r = "?b ~= (0::?'a) ==> ?a * ?b / ?b = ?a";
neuper@38022
   171
term2str lhs = "?a * ?b / ?b"; term2str rhs = "?a";
neuper@38022
   172
"----- step 2: the rule instantiated";
neuper@38025
   173
     val r' = Envir.subst_term 
neuper@38025
   174
                  (Pattern.match thy (lhs, t) (Vartab.empty, Vartab.empty)) r;
neuper@38022
   175
term2str r' = "y ~= 0 ==> x * y / y = x";
neuper@38022
   176
"----- step 3: get the (instantiated) assumption(s)";
neuper@38022
   177
     val p' = (fst o Logic.strip_prems) (Logic.count_prems r', [], r');
neuper@38022
   178
term2str (hd p') = "y ~= 0";
neuper@38022
   179
"=====vvv make asms without Trueprop ---vvv";
neuper@38022
   180
     val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) 
neuper@38022
   181
                                             (Logic.count_prems r', [], r'));
neuper@38022
   182
case p' of
neuper@38022
   183
    [Const ("Not", _) $ (Const ("op =", _) $ Free ("y", _) $
neuper@38022
   184
                               Const ("Groups.zero_class.zero", _))] => ()
neuper@38022
   185
  | _ => error "rewrite.sml assumption changed";
neuper@38022
   186
"=====^^^ make asms without Trueprop ---^^^";
neuper@38022
   187
"----- step 4: get the (instantiated) rhs";
neuper@38022
   188
     val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop 
neuper@38022
   189
               o Logic.strip_imp_concl) r';
neuper@38022
   190
term2str t' = "x";
neuper@37906
   191
neuper@38025
   192
"----------- step through 'fun rewrite_terms_'  ---------";
neuper@38025
   193
"----------- step through 'fun rewrite_terms_'  ---------";
neuper@38025
   194
"----------- step through 'fun rewrite_terms_'  ---------";
neuper@38025
   195
"----- step 0: args for rewrite_terms_, local fun";
neuper@38025
   196
val (thy, ord, erls, equs, t) =
neuper@38025
   197
    (theory "Biegelinie", dummy_ord, Erls, [str2term "x = 0"],
neuper@38025
   198
     str2term "M_b x = -1 * q_0 * x ^^^ 2 / 2 + x * c + c_2");
neuper@38025
   199
"----- step 1: args for rew_";
neuper@38025
   200
val ((t', asm'), (rules as r::rs), t) = ((e_term, []), equs, t);
neuper@38025
   201
"----- step 2: rew_sub";
neuper@38025
   202
rew_sub thy 1 [] ord erls false [] (Trueprop $ r) t;
neuper@38025
   203
"----- step 3: step through rew_sub -- inefficient: goes into subterms";
neuper@38025
   204
neuper@38025
   205
neuper@38025
   206
val SOME (t', _) = rewrite_terms_ thy dummy_ord Erls equs t;
neuper@38025
   207
writeln "----------- rewrite_terms_  1---------------------------";
neuper@37906
   208
if term2str t' = "M_b 0 = -1 * q_0 * 0 ^^^ 2 / 2 + 0 * c + c_2" then ()
neuper@38031
   209
else error "rewrite.sml rewrite_terms_ [x = 0]";
neuper@37906
   210
neuper@38025
   211
val equs = [str2term "M_b 0 = 0"];
neuper@38025
   212
val t = str2term "M_b 0 = -1 * q_0 * 0 ^^^ 2 / 2 + 0 * c + c_2";
neuper@38025
   213
val SOME (t', _) = rewrite_terms_ thy dummy_ord Erls equs t;
neuper@38025
   214
writeln "----------- rewrite_terms_  2---------------------------";
neuper@37906
   215
if term2str t' = "0 = -1 * q_0 * 0 ^^^ 2 / 2 + 0 * c + c_2" then ()
neuper@38031
   216
else error "rewrite.sml rewrite_terms_ [M_b 0 = 0]";
neuper@37906
   217
neuper@38025
   218
val equs = [str2term "x = 0", str2term"M_b 0 = 0"];
neuper@38025
   219
val t = str2term "M_b x = -1 * q_0 * x ^^^ 2 / 2 + x * c + c_2";
neuper@38025
   220
val SOME (t', _) = rewrite_terms_ thy dummy_ord Erls equs t;
neuper@38025
   221
writeln "----------- rewrite_terms_  3---------------------------";
neuper@37906
   222
if term2str t' = "0 = -1 * q_0 * 0 ^^^ 2 / 2 + 0 * c + c_2" then ()
neuper@38031
   223
else error "rewrite.sml rewrite_terms_ [x = 0, M_b 0 = 0]";
neuper@37906
   224
neuper@37906
   225
neuper@38022
   226
"----------- rewrite_inst_ bdvs -------------------------";
neuper@38022
   227
"----------- rewrite_inst_ bdvs -------------------------";
neuper@38022
   228
"----------- rewrite_inst_ bdvs -------------------------";
neuper@37906
   229
(*see smltest/Scripts/term_G.sml: inst_bdv 2*)
neuper@37906
   230
val t = str2term"-1 * (q_0 * L ^^^ 2) / 2 + (L * c_3 + c_4) = 0";
neuper@37906
   231
val bdvs = [(str2term"bdv_1",str2term"c"),
neuper@37906
   232
	    (str2term"bdv_2",str2term"c_2"),
neuper@37906
   233
	    (str2term"bdv_3",str2term"c_3"),
neuper@37906
   234
	    (str2term"bdv_4",str2term"c_4")];
neuper@37906
   235
(*------------ outcommented WN071210, after inclusion into ROOT.ML 
neuper@37926
   236
val SOME (t,_) = 
neuper@37906
   237
    rewrite_inst_ thy e_rew_ord 
neuper@37906
   238
		  (append_rls "erls_isolate_bdvs" e_rls 
neuper@37906
   239
			      [(Calc ("EqSystem.occur'_exactly'_in", 
neuper@37906
   240
				      eval_occur_exactly_in 
neuper@37906
   241
					  "#eval_occur_exactly_in_"))
neuper@37906
   242
			       ]) 
neuper@37967
   243
		  false bdvs (num_str @{separate_bdvs_add) t;
neuper@37906
   244
(writeln o term2str) t;
neuper@37906
   245
if term2str t = "L * c_3 + c_4 = 0 + -1 * (-1 * (q_0 * L ^^^ 2) / 2)"
neuper@38031
   246
then () else error "rewrite.sml rewrite_inst_ bdvs";
neuper@37906
   247
trace_rewrite:=true;
neuper@37906
   248
trace_rewrite:=false;--------------------------------------------*)
neuper@37906
   249
neuper@38025
   250
neuper@38022
   251
"----------- check diff 2002--2009-3 --------------------";
neuper@38022
   252
"----------- check diff 2002--2009-3 --------------------";
neuper@38022
   253
"----------- check diff 2002--2009-3 --------------------";
neuper@38022
   254
(*----- 2002 -------------------------------------------------------------------
neuper@38022
   255
#  rls: norm_Rational_noadd_fractions on: 1 / EI * (L * q_0 * x / 2 + -1 *
neuper@38022
   256
q_0 * x ^^^ 2 / 2)
neuper@38022
   257
##  rls: discard_minus_ on: 1 / EI * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2
neuper@38022
   258
/ 2)
neuper@38022
   259
###  try thm: real_diff_minus
neuper@38022
   260
###  try thm: sym_real_mult_minus1
neuper@38022
   261
##  rls: rat_mult_poly on: 1 / EI * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2
neuper@38022
   262
/ 2)
neuper@38022
   263
###  try thm: rat_mult_poly_l
neuper@38022
   264
###  try thm: rat_mult_poly_r
neuper@38022
   265
####  eval asms: L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2 is_polyexp
neuper@38022
   266
==> 1 / EI * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2) =
neuper@38022
   267
    1 * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2) / EI
neuper@38022
   268
#####  rls: e_rls-is_polyexp on: L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2
neuper@38022
   269
is_polyexp
neuper@38022
   270
######  try calc: Poly.is'_polyexp'
neuper@38022
   271
======  calc. to: False
neuper@38022
   272
######  try calc: Poly.is'_polyexp'
neuper@38022
   273
######  try calc: Poly.is'_polyexp'
neuper@38022
   274
####  asms false: ["L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2 is_polyexp"]
neuper@38022
   275
----- 2002 NONE rewrite --------------------------------------------------------
neuper@38022
   276
----- 2009 should maintain this behaviour, but: --------------------------------
neuper@38022
   277
#  rls: norm_Rational_noadd_fractions on: 1 / EI * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2)
neuper@38022
   278
##  rls: discard_minus on: 1 / EI * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2)
neuper@38022
   279
###  try thm: real_diff_minus
neuper@38022
   280
###  try thm: sym_real_mult_minus1
neuper@38022
   281
##  rls: rat_mult_poly on: 1 / EI * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2)
neuper@38022
   282
###  try thm: rat_mult_poly_l
neuper@38022
   283
###  try thm: rat_mult_poly_r
neuper@38022
   284
####  eval asms: L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2 is_polyexp
neuper@38022
   285
==> 1 / EI * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2) =
neuper@38022
   286
    1 * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2) / EI
neuper@38022
   287
#####  rls: e_rls-is_polyexp on: L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2 is_polyexp
neuper@38022
   288
######  try calc: Poly.is'_polyexp'
neuper@38022
   289
======  calc. to: False
neuper@38022
   290
######  try calc: Poly.is'_polyexp'
neuper@38022
   291
######  try calc: Poly.is'_polyexp'
neuper@38022
   292
####  asms accepted: ["L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2 is_polyexp"]   stored: ["False"]
neuper@38022
   293
===  rewrites to: 1 * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2) / EI
neuper@38022
   294
----- 2009 -------------------------------------------------------------------*)
neuper@38022
   295
neuper@38022
   296
(*the situation as was before repair (asm without Trueprop) is outcommented*)
neuper@38022
   297
val thy = theory "Isac";
neuper@38022
   298
"===== example which raised the problem =================";
neuper@38022
   299
val t = @{term "1 / EI * (L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2)"};
neuper@38022
   300
"----- rewrite_set_inst_ norm_Rational_noadd_fractions--";
neuper@38022
   301
val subs = [(str2term "bdv", str2term "x")];
neuper@38022
   302
val rls = norm_Rational_noadd_fractions;
neuper@38022
   303
val NONE (*SOME (t', _)*) = rewrite_set_inst_ thy true subs rls t;
neuper@38022
   304
"----- rewrite_ rat_mult_poly_r--------------------------";
neuper@38022
   305
val thm = @{thm rat_mult_poly_r};
neuper@38022
   306
         "?c::real is_polyexp ==> (?a::real) / (?b::real) * ?c = ?a * ?c / ?b";
neuper@38022
   307
val erls = append_rls "e_rls-is_polyexp" e_rls 
neuper@38022
   308
                      [Calc ("Poly.is'_polyexp", eval_is_polyexp "")];
neuper@38022
   309
val NONE (*SOME (t'', _)*) = rewrite_ thy dummy_ord erls true thm t;
neuper@38022
   310
(*t' = t''; (*false because of further rewrites in t'*)*)
neuper@38022
   311
"----- rew_sub  --------------------------------";
neuper@38022
   312
val (t''', _, _, _) = rew_sub thy 0 [] dummy_ord erls true [] (prop_of thm) t;
neuper@38022
   313
(*t'' = t'''; (*true*)*)
neuper@38022
   314
"----- rewrite_set_ erls --------------------------------";
neuper@38022
   315
val cond = @{term "(L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2)"};
neuper@38022
   316
val NONE = rewrite_set_ thy true erls cond; 
neuper@38022
   317
(* ^^^^^ goes with '======  calc. to: False' above from beginning*)
neuper@38022
   318
neuper@38022
   319
writeln "===== maximally simplified example =====================";
neuper@38022
   320
val t = @{term "a / b * (x / x ^^^ 2)"};
neuper@38022
   321
         "?c::real is_polyexp ==> (?a::real) / (?b::real) * ?c = ?a * ?c / ?b";
neuper@38022
   322
writeln "----- rewrite_set_inst_ norm_Rational_noadd_fractions--";
neuper@38022
   323
val (*NONE*) SOME (t', asm) = rewrite_set_inst_ thy true subs rls t;
neuper@38022
   324
term2str t' = "a * x / (b * x ^^^ 2)"; (*rew. by thm outside test case*)
neuper@38022
   325
(*checked visually: trace_rewrite looks like above for 2009*)
neuper@38022
   326
neuper@38022
   327
trace_rewrite := false; (*<<<<<<<<<<<<<<<<<<<<<###############################*)
neuper@38022
   328
show_types := false;
neuper@38022
   329
writeln "----- rewrite_ rat_mult_poly_r--------------------------";
neuper@38022
   330
val NONE (*SOME (t'', _)*) = rewrite_ thy dummy_ord erls true thm t;
neuper@38022
   331
(*t' = t''; (*false because of further rewrites in t'*)*)
neuper@38022
   332
writeln "----- rew_sub  --------------------------------";
neuper@38022
   333
val (t''', _, _, _) = rew_sub thy 0 [] dummy_ord erls true [] (prop_of thm) t;
neuper@38022
   334
(*t'' = t'''; (*true*)*)
neuper@38022
   335
writeln "----- rewrite_set_ erls --------------------------------";
neuper@38022
   336
val cond = @{term "(x / x ^^^ 2)"};
neuper@38022
   337
val NONE = rewrite_set_ thy true erls cond; 
neuper@38022
   338
(* ^^^^^ goes with '======  calc. to: False' above from beginning*)
neuper@38022
   339
trace_rewrite := false; (*<<<<<<<<<<<<<<<<<<<<<###############################*)
neuper@38022
   340
"--------------------------------------------------------";
neuper@38025
   341
neuper@38036
   342
-.-.-.-.-.-.-isolate response.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.*)
neuper@38025
   343
neuper@38036
   344
"----------- fun app_rev, Rrls, prepat ------------------";
neuper@38036
   345
"----------- fun app_rev, Rrls, prepat ------------------";
neuper@38036
   346
"----------- fun app_rev, Rrls, prepat ------------------";
neuper@38036
   347
(* Christian Urban, 101001 
neuper@38036
   348
theory Test
neuper@38036
   349
imports Main Complex
neuper@38036
   350
begin
neuper@38036
   351
neuper@38036
   352
ML {*
neuper@38036
   353
let
neuper@38036
   354
  val parser = Args.context -- Scan.lift Args.name_source
neuper@38036
   355
  fun term_pat (ctxt, str) = str |> ProofContext.read_term_pattern ctxt
neuper@38036
   356
  |> ML_Syntax.print_term |> ML_Syntax.atomic
neuper@38036
   357
in
neuper@38036
   358
  ML_Antiquote.inline "term_pat" (parser >> term_pat)
neuper@38036
   359
end
neuper@38036
   360
*}
neuper@38036
   361
neuper@38036
   362
ML {*
neuper@38036
   363
  val t = @{term "a + b * x = (0 ::real)"};
neuper@38036
   364
  val pat = @{term_pat "?l = (?r ::real)"};
neuper@38036
   365
  val precond = @{term_pat "is_polynomial (?l::real)"};
neuper@38036
   366
  val inst = Pattern.match @{theory} (pat, t) (Vartab.empty, Vartab.empty);
neuper@38036
   367
*}
neuper@38036
   368
neuper@38036
   369
ML {*
neuper@38036
   370
  Envir.subst_term inst precond
neuper@38036
   371
  |> Syntax.string_of_term @{context}
neuper@38036
   372
  |> writeln
neuper@38036
   373
*}
neuper@38036
   374
end *)
neuper@38036
   375
val t = @{term "a + b * x = (0 ::real)"};
neuper@38036
   376
val pat = parse_patt thy "?l = (?r ::real)";
neuper@38036
   377
val precond = parse_patt thy "is_polynomial (?l::real)";(*no infix def*)
neuper@38036
   378
val precond = parse_patt thy " (?l::real) is_expanded"; 
neuper@38036
   379
neuper@38036
   380
val inst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
neuper@38036
   381
val preinst = Envir.subst_term inst precond;
neuper@38036
   382
term2str preinst;
neuper@38036
   383
neuper@38036
   384
"===== Rational.thy: cancel ===";
neuper@38036
   385
(* pat matched with the current term gives an environment 
neuper@38036
   386
   (or not: hen the Rrls not applied);
neuper@38036
   387
   if pre1 and pre2 evaluate to HOLogic.true_const in this environment,
neuper@38036
   388
   then the Rrls is applied. *)
neuper@38036
   389
val t = str2term "(a + b) / c ::real";
neuper@38036
   390
val pat = parse_patt thy "?r / ?s ::real";
neuper@38036
   391
val pres = [parse_patt thy "?r is_expanded", parse_patt thy "?s is_expanded"];
neuper@38036
   392
val prepat = [(pres, pat)];
neuper@38036
   393
val erls = rational_erls;
neuper@38036
   394
(* erls got from Rrls {erls, prepat, scr = Rfuns {normal_form, ...}, ...} *)
neuper@38036
   395
neuper@38036
   396
val subst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
neuper@38036
   397
val asms = map (Envir.subst_term subst) pres;
neuper@38036
   398
if terms2str asms = "[\"a + b is_expanded\",\"c is_expanded\"]"
neuper@38036
   399
then () else error "rewrite.sml: prepat cancel subst";
neuper@38036
   400
if ([], true) = eval__true thy 0 asms [] erls
neuper@38036
   401
then () else error "rewrite.sml: prepat cancel eval__true";
neuper@38036
   402
neuper@38036
   403
"===== Rational.thy: common_nominator_p ===";
neuper@38036
   404
(* if each pat* matches with the current term, the Rrls is applied
neuper@38036
   405
   (there are no preconditions to be checked, they are HOLogic.true_const) *)
neuper@38036
   406
val t = str2term "a / b + 1 / 2";
neuper@38036
   407
val pat = parse_patt thy "?r / ?s + ?u / ?v";
neuper@38036
   408
val pres = [HOLogic.true_const];
neuper@38036
   409
val prepat = [(pres, pat)];
neuper@38036
   410
val erls = rational_erls;
neuper@38036
   411
(* erls got from Rrls {erls, prepat, scr = Rfuns {normal_form, ...}, ...} *)
neuper@38036
   412
neuper@38036
   413
val subst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
neuper@38036
   414
if ([], true) = eval__true thy 0 (map (Envir.subst_term subst) pres) [] erls
neuper@38036
   415
then () else error "rewrite.sml: prepat common_nominator_p";
neuper@38036
   416
neuper@38036
   417
"===== Poly.thy: order_mult_ ===";
neuper@38036
   418
          (* ?p matched with the current term gives an environment,
neuper@38036
   419
             which evaluates (the instantiated) "p is_multUnordered" to true*)
neuper@38036
   420
val t = str2term "x^^^2 * x";
neuper@38036
   421
val pat = parse_patt thy "?p :: real"
neuper@38036
   422
val pres = [parse_patt thy "?p is_multUnordered"];
neuper@38036
   423
val prepat = [(pres, pat)];
neuper@38036
   424
val erls = append_rls "e_rls-is_multUnordered" e_rls
neuper@38036
   425
		      [Calc ("Poly.is'_multUnordered", 
neuper@38036
   426
                             eval_is_multUnordered "")];
neuper@38036
   427
neuper@38036
   428
val subst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
neuper@38036
   429
val asms = map (Envir.subst_term subst) pres;
neuper@38036
   430
if terms2str asms = "[\"x ^^^ 2 * x is_multUnordered\"]"
neuper@38036
   431
then () else error "rewrite.sml: prepat order_mult_ subst";
neuper@38036
   432
if ([], true) = eval__true thy 0 asms [] erls
neuper@38036
   433
then () else error "rewrite.sml: prepat order_mult_ eval__true";
neuper@38036
   434
neuper@38036
   435
neuper@38036
   436
(*========== inhibit exn =======================================================
neuper@38036
   437
============ inhibit exn =====================================================*)
neuper@38025
   438
neuper@38030
   439
(*-.-.-.-.-.-isolate response.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
neuper@38030
   440
-.-.-.-.-.-.-isolate response.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.*)