test/Tools/isac/ProgLang/rewrite.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Fri, 23 Mar 2018 10:14:39 +0100
changeset 59411 3e241a6938ce
parent 59395 862eb17f9e16
child 59462 a3edc91cfe1f
permissions -rw-r--r--
Celem: Test_Isac partially

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