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