test/Tools/isac/ProgLang/rewrite.sml
author Walther Neuper <neuper@ist.tugraz.at>
Sat, 17 Mar 2012 11:06:46 +0100
changeset 42394 977788dfed26
parent 42224 46e72a5805b1
child 48760 5e1e45b3ddef
permissions -rw-r--r--
uncomment test/../rateq.sml (Isabelle 2002 --> 2011)

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