test/Tools/isac/MathEngBasic/rewrite.sml
author Walther Neuper <walther.neuper@jku.at>
Tue, 14 Apr 2020 15:56:15 +0200
changeset 59875 995177b6d786
parent 59874 820bf0840029
child 59878 3163e63a5111
permissions -rw-r--r--
use "ThmC_Def" for renaming identifiers
walther@59841
     1
(* Title: "ProgLang/rewrite.sml"
neuper@38036
     2
   Author: Walther Neuper 050908
neuper@37906
     3
   (c) copyright due to lincense terms.
neuper@37906
     4
*)
neuper@37906
     5
neuper@38022
     6
"--------------------------------------------------------";
neuper@38022
     7
"table of contents --------------------------------------";
neuper@38022
     8
"--------------------------------------------------------";
neuper@38022
     9
"----------- assemble rewrite ---------------------------";
wneuper@59382
    10
"----------- test rewriting without Isac session --------";
wneuper@59382
    11
"----------- conditional rewriting without Isac session -";
neuper@38022
    12
"----------- step through 'and rew_sub': ----------------";
neuper@38025
    13
"----------- step through 'fun rewrite_terms_'  ---------";
neuper@38022
    14
"----------- rewrite_inst_ bdvs -------------------------";
neuper@38022
    15
"----------- check diff 2002--2009-3 --------------------";
neuper@38039
    16
"----------- compare all prepat's existing 2010 ---------";
neuper@38039
    17
"----------- fun app_rev, Rrls, -------------------------";
neuper@42223
    18
"----------- 2011 thms with axclasses -------------------";
walther@59841
    19
"----------- fun rewrite_set_ .. RatEq_eliminate step into to fun chk --------------------------";
wneuper@59252
    20
"----------- fun rewrite_ down to Pattern.match ------------------------------------------------";
wneuper@59411
    21
"----------- fun rewrite_set_ .. norm_equation -------------------------------------------------";
walther@59841
    22
"----------- fun rewrite_set_ .. RatEq_eliminate step into to fun chk --------------------------";
neuper@38022
    23
"--------------------------------------------------------";
neuper@38022
    24
"--------------------------------------------------------";
neuper@38022
    25
"--------------------------------------------------------";
neuper@37906
    26
neuper@38036
    27
neuper@38022
    28
"----------- assemble rewrite ---------------------------";
neuper@38022
    29
"----------- assemble rewrite ---------------------------";
neuper@38022
    30
"----------- assemble rewrite ---------------------------";
neuper@37906
    31
"===== rewriting by thm with 'a";
neuper@41924
    32
(*show_types := true;*)
akargl@42188
    33
neuper@37906
    34
val thy = @{theory Complex_Main};
neuper@37906
    35
val ctxt = @{context};
wneuper@59112
    36
val thm = @{thm add.commute};
wneuper@59188
    37
val t = (Thm.term_of o the) (parse thy "((r + u) + t) + s");
neuper@37906
    38
"----- from old: fun rewrite__";
neuper@37906
    39
val bdv = [];
wneuper@59188
    40
val r = (((inst_bdv bdv) o norm o #prop o Thm.rep_thm) thm);
neuper@37906
    41
"----- from old: and rew_sub";
wneuper@59395
    42
val (LHS,RHS) = (dest_equals o strip_trueprop 
neuper@37906
    43
   	      o Logic.strip_imp_concl) r;
neuper@37906
    44
(* old
neuper@41942
    45
val insts = Pattern.match thy (LHS,t) (Vartab.empty, Vartab.empty);*)
neuper@37906
    46
"----- fun match_rew in Pure/pattern.ML";
neuper@41942
    47
val rtm = the_default RHS (Term.rename_abs LHS t RHS);
neuper@37906
    48
neuper@38022
    49
writeln(Syntax.string_of_term ctxt rtm);
neuper@41942
    50
writeln(Syntax.string_of_term ctxt LHS);
neuper@38022
    51
writeln(Syntax.string_of_term ctxt t);
neuper@37906
    52
neuper@41942
    53
(Pattern.match thy (LHS, t) (Vartab.empty, Vartab.empty));
neuper@41942
    54
val (rew, RHS) = (Envir.subst_term 
neuper@41942
    55
  (Pattern.match thy (LHS, t) (Vartab.empty, Vartab.empty)) rtm, rtm);
neuper@37906
    56
(*lookup in isabelle?trace?response...*)
neuper@37906
    57
writeln(Syntax.string_of_term ctxt rew);
neuper@41942
    58
writeln(Syntax.string_of_term ctxt RHS);
neuper@37906
    59
"===== rewriting: prep insertion into rew_sub";
neuper@37906
    60
val thy = @{theory Complex_Main};
neuper@37906
    61
val ctxt = @{context};
wneuper@59358
    62
val thm =  @{thm nonzero_divide_mult_cancel_right};
neuper@37906
    63
val r = Thm.prop_of thm;
wneuper@59358
    64
val tm = @{term "x / (2 * x)::real"};
neuper@37906
    65
"----- and rew_sub";
neuper@41942
    66
val (LHS, RHS) = (HOLogic.dest_eq o HOLogic.dest_Trueprop
neuper@37906
    67
                  o Logic.strip_imp_concl) r;
neuper@41942
    68
val r' = Envir.subst_term (Pattern.match thy (LHS, tm) 
neuper@37906
    69
                                (Vartab.empty, Vartab.empty)) r;
neuper@37906
    70
val p' = (fst o Logic.strip_prems) (Logic.count_prems r', [], r');
neuper@37906
    71
val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop 
neuper@37906
    72
            o Logic.strip_imp_concl) r';
neuper@37906
    73
neuper@37906
    74
(*is displayed on top of <response> buffer...*)
neuper@48761
    75
Pretty.writeln (Proof_Context.pretty_term_abbrev @{context} r');
neuper@48761
    76
Pretty.writeln (Proof_Context.pretty_term_abbrev @{context} t');
akargl@42188
    77
(**)
neuper@37906
    78
neuper@38022
    79
"----------- test rewriting without Isac's thys ---------";
neuper@38022
    80
"----------- test rewriting without Isac's thys ---------";
neuper@38022
    81
"----------- test rewriting without Isac's thys ---------";
neuper@38022
    82
neuper@37906
    83
"===== rewriting with Isabelle2009-1 only, i.e without isac-hacks";
neuper@37906
    84
val thy = @{theory Complex_Main};
neuper@37906
    85
val ctxt = @{context};
wneuper@59112
    86
val thm =  @{thm add.commute};
neuper@37906
    87
val tm = @{term "x + y*z::real"};
neuper@37906
    88
walther@59852
    89
val SOME (r,_) = (rewrite_ thy dummy_ord Rule_Set.empty false thm tm)
neuper@38022
    90
  handle _ => error "rewrite.sml diff.behav. in rewriting";
neuper@37906
    91
(*is displayed on _TOP_ of <response> buffer...*)
neuper@48761
    92
Pretty.writeln (Proof_Context.pretty_term_abbrev @{context} r);
neuper@38022
    93
if r = @{term "y*z + x::real"}
neuper@38022
    94
then () else error "rewrite.sml diff.result in rewriting";
neuper@37906
    95
neuper@37906
    96
"----- rewriting a subterm";
neuper@37906
    97
val tm = @{term "w*(x + y*z)::real"};
neuper@37906
    98
walther@59852
    99
val SOME (r,_) = (rewrite_ thy dummy_ord Rule_Set.empty false thm tm)
neuper@38022
   100
  handle _ => error "rewrite.sml diff.behav. in rew_sub";
neuper@37906
   101
neuper@37906
   102
"----- ordered rewriting";
wneuper@59462
   103
fun tord (_:subst) pp = LibraryC.termless pp;
neuper@37906
   104
if tord [] (@{term "x + y*z::real"}, @{term "y*z + x::real"}) then ()
neuper@38022
   105
else error "rewrite.sml diff.behav. in ord.rewr.";
neuper@37906
   106
walther@59852
   107
val NONE = (rewrite_ thy tord Rule_Set.empty false thm tm)
neuper@38022
   108
  handle _ => error "rewrite.sml diff.behav. in rewriting";
neuper@37906
   109
(*is displayed on _TOP_ of <response> buffer...*)
neuper@48761
   110
Pretty.writeln (Proof_Context.pretty_term_abbrev @{context} r);
neuper@37906
   111
neuper@37906
   112
val tm = @{term "x*y + z::real"};
walther@59852
   113
val SOME (r,_) = (rewrite_ thy tord Rule_Set.empty false thm tm)
neuper@38022
   114
  handle _ => error "rewrite.sml diff.behav. in rewriting";
neuper@37906
   115
neuper@37906
   116
neuper@38022
   117
"----------- conditional rewriting without Isac's thys --";
neuper@38022
   118
"----------- conditional rewriting without Isac's thys --";
neuper@38022
   119
"----------- conditional rewriting without Isac's thys --";
akargl@42224
   120
neuper@37906
   121
"===== prepr cond.rew. with Pattern.match";
neuper@37906
   122
val thy = @{theory Complex_Main};
neuper@37906
   123
val ctxt = @{context};
wneuper@59358
   124
val thm =  @{thm nonzero_divide_mult_cancel_right};
neuper@37906
   125
val rule = Thm.prop_of thm;
wneuper@59358
   126
val tm = @{term "x / (2 * x)::real"};
neuper@37906
   127
neuper@37906
   128
val prem = Logic.strip_imp_prems rule;
neuper@37906
   129
val nps = Logic.count_prems rule;
neuper@37906
   130
val prems = Logic.strip_prems (nps, [], rule);
neuper@37906
   131
neuper@37906
   132
val eq = Logic.strip_imp_concl rule;
neuper@41942
   133
val (LHS, RHS) = (HOLogic.dest_eq o HOLogic.dest_Trueprop) eq;
neuper@37906
   134
neuper@41942
   135
val mtcs = Pattern.match thy (LHS, tm) (Vartab.empty, Vartab.empty);
neuper@37906
   136
val rule' = Envir.subst_term mtcs rule;
neuper@37906
   137
neuper@37906
   138
val prems' = (fst o Logic.strip_prems) 
neuper@37906
   139
              (Logic.count_prems rule', [], rule');
neuper@41942
   140
val RHS' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop 
neuper@37906
   141
            o Logic.strip_imp_concl) rule';
neuper@37906
   142
neuper@37906
   143
"----- conditional rewriting creating an assumption";
neuper@37906
   144
"----- conditional rewriting creating an assumption";
wneuper@59358
   145
val thm =  @{thm nonzero_divide_mult_cancel_right};
wneuper@59358
   146
val tm = @{term "x / (2 * x)::real"};
walther@59852
   147
val SOME (rew, asm) = (rewrite_ thy dummy_ord Rule_Set.empty false thm tm)
neuper@38022
   148
  handle _ => error "rewrite.sml diff.behav. in cond.rew.";
neuper@37906
   149
wneuper@59358
   150
if rew = @{term "1 / 2::real"} then () (* the rewrite is _NO_ Trueprop *)
neuper@38022
   151
else error "rewrite.sml diff.result in cond.rew.";
neuper@37906
   152
wneuper@59358
   153
if hd asm = @{term "x \<noteq> (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:*)
wneuper@59358
   163
val tm = @{term "x / (2 * x)::real"};
walther@59852
   164
val (t', asm, _, _) = rew_sub thy 0 [] dummy_ord Rule_Set.empty true [] (Thm.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) = 
walther@59852
   168
    (thy, 0, [], dummy_ord, Rule_Set.empty, true, [], (Thm.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;
walther@59868
   172
UnparseC.term r = "?b \<noteq> (0::?'a) \<Longrightarrow> ?b / (?a * ?b) = (1::?'a) / ?a";
walther@59868
   173
UnparseC.term LHS = "?b / (?a * ?b)"; UnparseC.term RHS = "(1::?'a) / ?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;
walther@59868
   177
UnparseC.term r' = "x \<noteq> 0 \<Longrightarrow> x / (2 * x) = 1 / 2";
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');
walther@59868
   180
UnparseC.term (hd p') = "x \<noteq> 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
wneuper@59358
   185
    [Const ("HOL.Not", _) $ (Const ("HOL.eq", _) 
wneuper@59358
   186
      $ Free ("x", _) $ 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';
walther@59868
   192
UnparseC.term t' = "1 / 2";
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) =
walther@59851
   199
    (@{theory "Biegelinie"}, dummy_ord, Rule_Set.Empty, [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_";
walther@59861
   202
val ((t', asm'), (rules as r::rs), t) = ((TermC.empty, []), equs, t);
neuper@38025
   203
"----- step 2: rew_sub";
wneuper@59395
   204
rew_sub thy 1 [] ord erls false [] (HOLogic.Trueprop $ r) t;
neuper@38025
   205
"----- step 3: step through rew_sub -- inefficient: goes into subterms";
neuper@38025
   206
neuper@38025
   207
walther@59851
   208
val SOME (t', _) = rewrite_terms_ thy dummy_ord Rule_Set.Empty equs t;
neuper@38025
   209
writeln "----------- rewrite_terms_  1---------------------------";
walther@59868
   210
if UnparseC.term 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";
walther@59851
   215
val SOME (t', _) = rewrite_terms_ thy dummy_ord Rule_Set.Empty equs t;
neuper@38025
   216
writeln "----------- rewrite_terms_  2---------------------------";
walther@59868
   217
if UnparseC.term 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";
walther@59851
   222
val SOME (t', _) = rewrite_terms_ thy dummy_ord Rule_Set.Empty equs t;
neuper@38025
   223
writeln "----------- rewrite_terms_  3---------------------------";
walther@59868
   224
if UnparseC.term 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 
walther@59852
   240
		  (Rule_Set.append_rules "erls_isolate_bdvs" Rule_Set.empty 
walther@59773
   241
			      [(Num_Calc ("EqSystem.occur'_exactly'_in", 
neuper@37906
   242
				      eval_occur_exactly_in 
neuper@37906
   243
					  "#eval_occur_exactly_in_"))
neuper@37906
   244
			       ]) 
walther@59871
   245
		  false bdvs (ThmC.numerals_to_Free @{separate_bdvs_add) t;
walther@59868
   246
(writeln o UnparseC.term) t;
walther@59868
   247
if UnparseC.term 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";
walther@59627
   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
walther@59852
   270
#####  rls: Rule_Set.empty-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
walther@59852
   289
#####  rls: Rule_Set.empty-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*)
wneuper@59592
   299
val thy = @{theory "Isac_Knowledge"};
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--";
wneuper@59382
   303
val subs = [(@{term "bdv"}, @{term  "x"})];
neuper@38022
   304
val rls = norm_Rational_noadd_fractions;
wneuper@59112
   305
val SOME (t', asms) = rewrite_set_inst_ thy true subs rls t;
walther@59868
   306
if UnparseC.term t' = "1 / EI * (L * q_0 * x / 2 + -1 * q_0 * 1 * x ^^^ 2 / 2)" andalso
walther@59868
   307
  UnparseC.terms asms = "[]" then {}
wneuper@59112
   308
else error "this was NONE with Isabelle2013-2 ?!?"
neuper@38022
   309
"----- rewrite_ rat_mult_poly_r--------------------------";
neuper@38022
   310
val thm = @{thm rat_mult_poly_r};
neuper@38022
   311
         "?c::real is_polyexp ==> (?a::real) / (?b::real) * ?c = ?a * ?c / ?b";
walther@59852
   312
val erls = Rule_Set.append_rules "Rule_Set.empty-is_polyexp" Rule_Set.empty 
walther@59773
   313
                      [Num_Calc ("Poly.is'_polyexp", eval_is_polyexp "")];
neuper@38022
   314
val NONE (*SOME (t'', _)*) = rewrite_ thy dummy_ord erls true thm t;
neuper@38022
   315
(*t' = t''; (*false because of further rewrites in t'*)*)
neuper@38022
   316
"----- rew_sub  --------------------------------";
wneuper@59188
   317
val (t''', _, _, _) = rew_sub thy 0 [] dummy_ord erls true [] (Thm.prop_of thm) t;
neuper@38022
   318
(*t'' = t'''; (*true*)*)
neuper@38022
   319
"----- rewrite_set_ erls --------------------------------";
neuper@38022
   320
val cond = @{term "(L * q_0 * x / 2 + -1 * q_0 * x ^^^ 2 / 2)"};
neuper@38022
   321
val NONE = rewrite_set_ thy true erls cond; 
neuper@38022
   322
(* ^^^^^ goes with '======  calc. to: False' above from beginning*)
neuper@38022
   323
neuper@38022
   324
writeln "===== maximally simplified example =====================";
neuper@38022
   325
val t = @{term "a / b * (x / x ^^^ 2)"};
neuper@38022
   326
         "?c::real is_polyexp ==> (?a::real) / (?b::real) * ?c = ?a * ?c / ?b";
neuper@38022
   327
writeln "----- rewrite_set_inst_ norm_Rational_noadd_fractions--";
neuper@38022
   328
val (*NONE*) SOME (t', asm) = rewrite_set_inst_ thy true subs rls t;
walther@59868
   329
UnparseC.term t' = "a * x / (b * x ^^^ 2)"; (*rew. by thm outside test case*)
neuper@38022
   330
(*checked visually: trace_rewrite looks like above for 2009*)
neuper@38022
   331
neuper@38022
   332
writeln "----- rewrite_ rat_mult_poly_r--------------------------";
neuper@38022
   333
val NONE (*SOME (t'', _)*) = rewrite_ thy dummy_ord erls true thm t;
neuper@38022
   334
(*t' = t''; (*false because of further rewrites in t'*)*)
neuper@38022
   335
writeln "----- rew_sub  --------------------------------";
wneuper@59188
   336
val (t''', _, _, _) = rew_sub thy 0 [] dummy_ord erls true [] (Thm.prop_of thm) t;
neuper@38022
   337
(*t'' = t'''; (*true*)*)
neuper@38022
   338
writeln "----- rewrite_set_ erls --------------------------------";
neuper@38022
   339
val cond = @{term "(x / x ^^^ 2)"};
neuper@38022
   340
val NONE = rewrite_set_ thy true erls cond; 
neuper@38022
   341
(* ^^^^^ goes with '======  calc. to: False' above from beginning*)
neuper@42201
   342
neuper@38025
   343
neuper@38039
   344
"----------- compare all prepat's existing 2010 ---------";
neuper@38039
   345
"----------- compare all prepat's existing 2010 ---------";
neuper@38039
   346
"----------- compare all prepat's existing 2010 ---------";
wneuper@59592
   347
val thy = @{theory "Isac_Knowledge"};
neuper@38036
   348
val t = @{term "a + b * x = (0 ::real)"};
neuper@38036
   349
val pat = parse_patt thy "?l = (?r ::real)";
neuper@38036
   350
val precond = parse_patt thy "is_polynomial (?l::real)";(*no infix def*)
neuper@38039
   351
val precond = parse_patt thy "(?l::real) is_expanded"; 
neuper@38036
   352
neuper@38036
   353
val inst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
neuper@38036
   354
val preinst = Envir.subst_term inst precond;
walther@59868
   355
UnparseC.term preinst;
neuper@38036
   356
neuper@38036
   357
"===== Rational.thy: cancel ===";
neuper@38036
   358
(* pat matched with the current term gives an environment 
neuper@38036
   359
   (or not: hen the Rrls not applied);
neuper@48760
   360
   if pre1 and pre2 evaluate to @{term True} in this environment,
neuper@38036
   361
   then the Rrls is applied. *)
neuper@38036
   362
val t = str2term "(a + b) / c ::real";
neuper@38036
   363
val pat = parse_patt thy "?r / ?s ::real";
neuper@38036
   364
val pres = [parse_patt thy "?r is_expanded", parse_patt thy "?s is_expanded"];
neuper@38036
   365
val prepat = [(pres, pat)];
neuper@38036
   366
val erls = rational_erls;
neuper@38036
   367
(* erls got from Rrls {erls, prepat, scr = Rfuns {normal_form, ...}, ...} *)
neuper@38036
   368
neuper@38036
   369
val subst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
neuper@38036
   370
val asms = map (Envir.subst_term subst) pres;
walther@59868
   371
if UnparseC.terms asms = "[\"a + b is_expanded\",\"c is_expanded\"]"
neuper@38036
   372
then () else error "rewrite.sml: prepat cancel subst";
neuper@38036
   373
if ([], true) = eval__true thy 0 asms [] erls
neuper@38036
   374
then () else error "rewrite.sml: prepat cancel eval__true";
neuper@38036
   375
neuper@52105
   376
"===== Rational.thy: add_fractions_p ===";
neuper@38036
   377
(* if each pat* matches with the current term, the Rrls is applied
neuper@48760
   378
   (there are no preconditions to be checked, they are @{term True}) *)
neuper@38036
   379
val t = str2term "a / b + 1 / 2";
neuper@38036
   380
val pat = parse_patt thy "?r / ?s + ?u / ?v";
neuper@48760
   381
val pres = [@{term True}];
neuper@38036
   382
val prepat = [(pres, pat)];
neuper@38036
   383
val erls = rational_erls;
neuper@38036
   384
(* erls got from Rrls {erls, prepat, scr = Rfuns {normal_form, ...}, ...} *)
neuper@38036
   385
neuper@38036
   386
val subst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
neuper@38036
   387
if ([], true) = eval__true thy 0 (map (Envir.subst_term subst) pres) [] erls
neuper@52105
   388
then () else error "rewrite.sml: prepat add_fractions_p";
neuper@38036
   389
neuper@38036
   390
"===== Poly.thy: order_mult_ ===";
neuper@38036
   391
          (* ?p matched with the current term gives an environment,
neuper@38036
   392
             which evaluates (the instantiated) "p is_multUnordered" to true*)
neuper@38036
   393
val t = str2term "x^^^2 * x";
neuper@38036
   394
val pat = parse_patt thy "?p :: real"
neuper@38036
   395
val pres = [parse_patt thy "?p is_multUnordered"];
neuper@38036
   396
val prepat = [(pres, pat)];
walther@59852
   397
val erls = Rule_Set.append_rules "Rule_Set.empty-is_multUnordered" Rule_Set.empty
walther@59773
   398
		      [Num_Calc ("Poly.is'_multUnordered", 
neuper@38036
   399
                             eval_is_multUnordered "")];
neuper@38036
   400
neuper@38036
   401
val subst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
neuper@38036
   402
val asms = map (Envir.subst_term subst) pres;
walther@59868
   403
if UnparseC.terms asms = "[\"x ^^^ 2 * x is_multUnordered\"]"
neuper@38036
   404
then () else error "rewrite.sml: prepat order_mult_ subst";
neuper@38036
   405
if ([], true) = eval__true thy 0 asms [] erls
neuper@38036
   406
then () else error "rewrite.sml: prepat order_mult_ eval__true";
neuper@38036
   407
neuper@38036
   408
neuper@38039
   409
"----------- fun app_rev, Rrls, -------------------------";
neuper@38039
   410
"----------- fun app_rev, Rrls, -------------------------";
neuper@38039
   411
"----------- fun app_rev, Rrls, -------------------------";
neuper@38039
   412
val t = str2term "x^^^2 * x";
neuper@41928
   413
neuper@38039
   414
if is_multUnordered t then () else error "rewrite.sml diff. is_multUnordered 2";
neuper@38039
   415
val tm = str2term "(x^^^2 * x) is_multUnordered";
neuper@41928
   416
eval_is_multUnordered "testid" "" tm thy;
neuper@41928
   417
neuper@38039
   418
case eval_is_multUnordered "testid" "" tm thy of
neuper@41924
   419
    SOME (_, Const ("HOL.Trueprop", _) $ 
neuper@41922
   420
                   (Const ("HOL.eq", _) $
neuper@38039
   421
                          (Const ("Poly.is'_multUnordered", _) $ _) $ 
neuper@41928
   422
                          Const ("HOL.True", _))) => ()
neuper@41928
   423
  | _ => error "rewrite.sml diff. eval_is_multUnordered 2b";
neuper@38039
   424
walther@59627
   425
tracing "----- begin rewrite x^^^2 * x ---"; trace_rewrite := false;
neuper@38039
   426
val SOME (t', _) = rewrite_set_ thy true order_mult_ t;
neuper@38039
   427
tracing "----- end rewrite x^^^2 * x ---"; trace_rewrite := false;
walther@59868
   428
if UnparseC.term t' = "x * x ^^^ 2" then ()
neuper@38039
   429
else error "rewrite.sml Poly.is'_multUnordered doesn't work";
neuper@38039
   430
neuper@38039
   431
(* for achieving the previous result, the following code was taken apart *)
neuper@38039
   432
"----- rewrite__set_ ---";
neuper@38039
   433
val (thy, i, _, _, (rrls as Rrls _), t) = (thy, 0, true, [], order_mult_, tm);
neuper@38039
   434
	val (t', asm, rew) = app_rev thy (i+1) rrls t;
neuper@38039
   435
"----- app_rev ---";
neuper@38039
   436
val (thy, i, rrls, t) = (thy, (i+1), rrls, t);
neuper@38039
   437
	fun chk_prepat thy erls [] t = true
neuper@38039
   438
	  | chk_prepat thy erls prepat t =
neuper@38039
   439
	    let fun chk (pres, pat) =
neuper@38039
   440
		    (let val subst: Type.tyenv * Envir.tenv = 
neuper@38039
   441
			     Pattern.match thy (pat, t)
neuper@38039
   442
					    (Vartab.empty, Vartab.empty)
neuper@38039
   443
		     in snd (eval__true thy (i+1) 
neuper@38039
   444
					(map (Envir.subst_term subst) pres)
neuper@38039
   445
					[] erls)
neuper@38039
   446
		     end)
neuper@38039
   447
		    handle _ => false
neuper@38039
   448
		fun scan_ f [] = false (*scan_ NEVER called by []*)
neuper@38039
   449
		  | scan_ f (pp::pps) = if f pp then true
neuper@38039
   450
					else scan_ f pps;
neuper@38039
   451
	    in scan_ chk prepat end;
neuper@38039
   452
neuper@38039
   453
	(*.apply the normal_form of a rev-set.*)
neuper@38039
   454
	fun app_rev' thy (Rrls{erls,prepat,scr=Rfuns{normal_form,...},...}) t =
neuper@38039
   455
	    if chk_prepat thy erls prepat t
walther@59868
   456
	    then ((*tracing("### app_rev': t = "^(UnparseC.term t));*)
neuper@38039
   457
                  normal_form t)
neuper@38039
   458
	    else NONE;
neuper@38039
   459
(*fixme val NONE = app_rev' thy rrls t;*)
neuper@38039
   460
"----- app_rev' ---";
neuper@38039
   461
val (thy, Rrls{erls,prepat,scr=Rfuns{normal_form,...},...}, t) = (thy, rrls, t);
neuper@38039
   462
(*fixme false*)   chk_prepat thy erls prepat t;
neuper@38039
   463
"----- chk_prepat ---";
neuper@38039
   464
val (thy, erls, prepat, t) = (thy, erls, prepat, t);
neuper@38039
   465
                fun chk (pres, pat) =
neuper@38039
   466
		    (let val subst: Type.tyenv * Envir.tenv = 
neuper@38039
   467
			     Pattern.match thy (pat, t)
neuper@38039
   468
					    (Vartab.empty, Vartab.empty)
neuper@38039
   469
		     in snd (eval__true thy (i+1) 
neuper@38039
   470
					(map (Envir.subst_term subst) pres)
neuper@38039
   471
					[] erls)
neuper@38039
   472
		     end)
neuper@38039
   473
		    handle _ => false;
neuper@38039
   474
		fun scan_ f [] = false (*scan_ NEVER called by []*)
neuper@38039
   475
		  | scan_ f (pp::pps) = if f pp then true
neuper@38039
   476
					else scan_ f pps;
neuper@38039
   477
tracing "=== poly.sml: scan_ chk prepat begin";
neuper@38039
   478
scan_ chk prepat;
neuper@38039
   479
tracing "=== poly.sml: scan_ chk prepat end";
neuper@38039
   480
neuper@38039
   481
"----- chk ---";
neuper@38039
   482
(*reestablish...*) val t = str2term "x ^^^ 2 * x";
neuper@38039
   483
val [(pres, pat)] = prepat;
neuper@38039
   484
                         val subst: Type.tyenv * Envir.tenv = 
neuper@38039
   485
			     Pattern.match thy (pat, t)
neuper@38039
   486
					    (Vartab.empty, Vartab.empty);
neuper@38039
   487
neuper@38039
   488
(*fixme: asms = ["p is_multUnordered"]...instantiate*)
neuper@38039
   489
"----- eval__true ---";
neuper@38039
   490
val asms = (map (Envir.subst_term subst) pres);
walther@59868
   491
if UnparseC.terms asms = "[\"x ^^^ 2 * x is_multUnordered\"]" then ()
neuper@38039
   492
else error "rewrite.sml: diff. is_multUnordered, asms";
neuper@38039
   493
val (thy, i, asms, bdv, rls) = 
neuper@38039
   494
    (thy, (i+1), [str2term "(x^^^2 * x) is_multUnordered"], 
neuper@38039
   495
     [] : (term * term) list, erls);
neuper@38039
   496
case eval__true thy i asms bdv rls of 
neuper@38039
   497
    ([], true) => ()
neuper@38039
   498
  | _ => error "rewrite.sml: diff. is_multUnordered, eval__true";
neuper@42223
   499
neuper@42223
   500
"----------- 2011 thms with axclasses -------------------";
neuper@42223
   501
"----------- 2011 thms with axclasses -------------------";
neuper@42223
   502
"----------- 2011 thms with axclasses -------------------";
walther@59871
   503
val thm = ThmC.numerals_to_Free @{thm div_by_1};
wneuper@59188
   504
val prop = Thm.prop_of thm;
neuper@42223
   505
atomty prop;                                     (*Type 'a*)
neuper@42223
   506
val t = str2term "(2*x)/1";                      (*Type real*)
neuper@42223
   507
neuper@42223
   508
val (thy, ro, er, inst) = 
wneuper@59592
   509
    (@{theory "Isac_Knowledge"}, tless_true, eval_rls, [(@{term "bdv::real"}, @{term "x::real"})]);
neuper@42223
   510
val SOME (t, _) = rewrite_ thy ro er true thm t; (*real matches 'a ?via ring? etc*)
neuper@42223
   511
walther@59841
   512
"----------- fun rewrite_set_ .. RatEq_eliminate step into to fun chk --------------------------";
walther@59841
   513
"----------- fun rewrite_set_ .. RatEq_eliminate step into to fun chk --------------------------";
walther@59841
   514
"----------- fun rewrite_set_ .. RatEq_eliminate step into to fun chk --------------------------";
walther@59841
   515
val thy = @{theory RatEq};
walther@59841
   516
val ctxt = Proof_Context.init_global thy;
walther@59841
   517
val SOME t = parseNEW ctxt "(3 + -1 * x + x ^^^ 2) / (9 * x + -6 * x ^^^ 2 + x ^^^ 3) = 1 / x";
walther@59841
   518
val rls = assoc_rls "RatEq_eliminate"
neuper@42394
   519
walther@59841
   520
val SOME (t''''', asm''''') =
walther@59841
   521
           rewrite_set_ thy true rls t;
walther@59841
   522
"~~~~~ fun rewrite_set_ , args:"; val (thy, bool, rls, term) = (thy, true, rls, t);
walther@59841
   523
           rewrite__set_ thy 1 bool [] rls term;
walther@59841
   524
"~~~~~ and rewrite__set_ , args:"; val (thy, i, put_asm, bdv, rls, ct)
walther@59841
   525
  = (thy, 1, bool, []:(term * term) list, rls, term);
walther@59841
   526
walther@59841
   527
(*/------- outcomment this code: otherwise the re-definition could infect tests lateron ------\ * )
walther@59841
   528
      datatype switch = Appl | Noap;
walther@59841
   529
      fun rew_once _ asm ct Noap [] = (ct, asm) (* ?TODO unify with Prog_Expr.rew_once? *)
walther@59841
   530
        | rew_once ruls asm ct Appl [] = 
walther@59851
   531
          (case rls of Rule_Set.Repeat _ => rew_once ruls asm ct Noap ruls
walther@59851
   532
          | Rule_Set.Seqence _ => (ct, asm)
walther@59867
   533
          | rls => raise ERROR ("rew_once not appl. to \"" ^ Rule_Set.id rls ^ "\""))
walther@59841
   534
        | rew_once ruls asm ct apno (rul :: thms) =
walther@59841
   535
          case rul of
walther@59841
   536
            Rule.Thm (thmid, thm) =>
walther@59841
   537
              (trace1 i (" try thm: " ^ thmid);
walther@59852
   538
              case rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule.Rule_Set.rep) rls)
walther@59852
   539
                  ((#erls o Rule.Rule_Set.rep) rls) put_asm thm ct of
walther@59841
   540
                NONE => rew_once ruls asm ct apno thms
walther@59841
   541
              | SOME (ct', asm') => 
walther@59870
   542
                (trace1 i (" rewrites to: " ^ UnparseC.term_in_thy thy ct');
walther@59841
   543
                rew_once ruls (union (op =) asm asm') ct' Appl (rul :: thms)))
walther@59841
   544
                (* once again try the same rule, e.g. associativity against "()"*)
walther@59841
   545
          | Rule.Num_Calc (cc as (op_, _)) => 
walther@59841
   546
            let val _= trace1 i (" try calc: " ^ op_ ^ "'")
walther@59841
   547
              val ct = TermC.uminus_to_string ct (*WN190312: superfluous?*)
walther@59841
   548
            in case Num_Calc.adhoc_thm thy cc ct of
walther@59841
   549
                NONE => rew_once ruls asm ct apno thms
walther@59841
   550
              | SOME (_, thm') => 
walther@59841
   551
                let 
walther@59852
   552
                  val pairopt = rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule.Rule_Set.rep) rls)
walther@59852
   553
                    ((#erls o Rule.Rule_Set.rep) rls) put_asm thm' ct;
walther@59841
   554
                  val _ = if pairopt <> NONE then () else error ("rewrite_set_, rewrite_ \"" ^ 
walther@59875
   555
                    ThmC.string_of_thm thm' ^ "\" " ^ UnparseC.term_in_thy thy ct ^ " = NONE")
walther@59870
   556
                  val _ = trace1 i (" calc. to: " ^ UnparseC.term_in_thy thy ((fst o the) pairopt))
walther@59841
   557
                in rew_once ruls asm ((fst o the) pairopt) Appl (rul :: thms) end
walther@59841
   558
            end
walther@59841
   559
          | Rule.Cal1 (cc as (op_, _)) => 
walther@59841
   560
            let val _= trace1 i (" try cal1: " ^ op_ ^ "'");
walther@59841
   561
              val ct = TermC.uminus_to_string ct
walther@59841
   562
            in case Num_Calc.adhoc_thm1_ thy cc ct of
walther@59841
   563
                NONE => (ct, asm)
walther@59841
   564
              | SOME (_, thm') =>
walther@59841
   565
                let 
walther@59852
   566
                  val pairopt = rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule.Rule_Set.rep) rls)
walther@59852
   567
                    ((#erls o Rule.Rule_Set.rep) rls) put_asm thm' ct;
walther@59841
   568
                  val _ = if pairopt <> NONE then () else error ("rewrite_set_, rewrite_ \"" ^
walther@59875
   569
                     ThmC.string_of_thm thm' ^ "\" " ^ UnparseC.term_in_thy thy ct ^ " = NONE")
walther@59870
   570
                  val _ = trace1 i (" cal1. to: " ^ UnparseC.term_in_thy thy ((fst o the) pairopt))
walther@59841
   571
                in the pairopt end
walther@59841
   572
            end
walther@59841
   573
          | Rule.Rls_ rls' => 
walther@59841
   574
            (case rewrite__set_ thy (i + 1) put_asm bdv rls' ct of
walther@59841
   575
              SOME (t', asm') => rew_once ruls (union (op =) asm asm') t' Appl thms
walther@59841
   576
            | NONE => rew_once ruls asm ct apno thms)
walther@59867
   577
          | r => raise ERROR ("rew_once not appl. to \"" ^ Rule.Rule.to_string r ^ "\"");
walther@59852
   578
      val ruls = (#rules o Rule.Rule_Set.rep) rls;
walther@59870
   579
(*    val _ = trace i (" rls: " ^ Rule_Set.id rls ^ " on: " ^ UnparseC.term_in_thy thy ct)*)
walther@59841
   580
      val (ct', asm') = rew_once ruls [] ct Noap ruls;
walther@59841
   581
"~~~~~ fun rew_once , args:"; val (ruls, asm, ct, apno, (rul :: thms))
walther@59841
   582
  = (ruls, []:term list, ct, Noap, ruls);
walther@59841
   583
           val Rule.Thm (thmid, thm) = (*case*) rul (*of*);
walther@59841
   584
walther@59841
   585
    val SOME (ct', asm') = (*case*)
walther@59852
   586
           rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule.Rule_Set.rep) rls)
walther@59852
   587
                  ((#erls o Rule.Rule_Set.rep) rls) put_asm thm ct (*of*);
walther@59841
   588
"~~~~~ fun rewrite__ , args:"; val (thy, i, bdv, tless, rls, put_asm, thm, ct)
walther@59852
   589
  = (thy, (i + 1), bdv, ((snd o #rew_ord o Rule.Rule_Set.rep) rls),
walther@59852
   590
                  ((#erls o Rule.Rule_Set.rep) rls), put_asm, thm, ct);
walther@59841
   591
walther@59841
   592
    val (t', asms, _ (*lrd*), rew) =
walther@59841
   593
           rew_sub thy i bdv tless rls put_asm ([(*root of the term*)]: TermC.path)
walther@59841
   594
		  (((TermC.inst_bdv bdv) o Num_Calc.norm o #prop o Thm.rep_thm) thm) ct;
walther@59841
   595
"~~~~~ and rew_sub , args:"; val (thy, i, bdv, tless, rls, put_asm, lrd, r, t)
walther@59841
   596
  = (thy, i, bdv, tless, rls, put_asm, ([(*root of the term*)]: TermC.path),
walther@59841
   597
		  (((TermC.inst_bdv bdv) o Num_Calc.norm o #prop o Thm.rep_thm) thm), ct);
walther@59841
   598
    val (lhs, rhs) = (HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r
walther@59841
   599
    val r' = Envir.subst_term (Pattern.match thy (lhs, t) (Vartab.empty, Vartab.empty)) r
walther@59841
   600
;
walther@59868
   601
(*+*)if UnparseC.term r' =
walther@59841
   602
(*+*)  "\<lbrakk>9 * x + -6 * x ^^^ 2 + x ^^^ 3 \<noteq> 0; x \<noteq> 0\<rbrakk>\n" ^
walther@59841
   603
(*+*)  "\<Longrightarrow> ((3 + -1 * x + x ^^^ 2) /\n" ^
walther@59841
   604
(*+*)  "                   (9 * x + -6 * x ^^^ 2 + x ^^^ 3) =\n" ^
walther@59841
   605
(*+*)  "                   1 / x) =\n" ^
walther@59841
   606
(*+*)  "                  ((3 + -1 * x + x ^^^ 2) * x =\n" ^
walther@59841
   607
(*+*)  "                   1 * (9 * x + -6 * x ^^^ 2 + x ^^^ 3))"
walther@59841
   608
(*+*)then () else error "instantiated rule CHANGED";
walther@59841
   609
walther@59841
   610
    val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) (Logic.count_prems r', [], r'))
walther@59841
   611
;
walther@59868
   612
(*+*)if map UnparseC.term p' = ["x \<noteq> 0", "9 * x + -6 * x ^^^ 2 + x ^^^ 3 \<noteq> 0"]
walther@59841
   613
(*+*)then () else error "stored assumptions CHANGED";
walther@59841
   614
walther@59841
   615
    val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r'
walther@59841
   616
;
walther@59868
   617
(*+*)if UnparseC.term t' = "(3 + -1 * x + x ^^^ 2) * x = 1 * (9 * x + -6 * x ^^^ 2 + x ^^^ 3)"
walther@59841
   618
(*+*)then () else error "rewritten term (an equality) CHANGED";
walther@59841
   619
walther@59841
   620
        val (simpl_p', nofalse) =
walther@59841
   621
           eval__true thy (i + 1) p' bdv rls;
walther@59841
   622
"~~~~~ and eval__true , args:"; val (thy, i, asms, bdv, rls) = (thy, (i + 1), p', bdv, rls);
walther@59841
   623
  (*if*) asms = [@{term True}] orelse asms = [] (*else*); 
walther@59841
   624
walther@59868
   625
(*+*)if map UnparseC.term asms = ["x \<noteq> 0", "9 * x + -6 * x ^^^ 2 + x ^^^ 3 \<noteq> 0"]
walther@59841
   626
(*+*)then () else error "asms before chk CHANGED";
walther@59841
   627
walther@59841
   628
        fun chk indets [] = (indets, true) (*return asms<>True until false*)
walther@59841
   629
          | chk indets (a :: asms) =
walther@59841
   630
            (case rewrite__set_ thy (i + 1) false bdv rls a of
walther@59841
   631
              NONE => (chk (indets @ [a]) asms)
walther@59841
   632
            | SOME (t, a') =>
walther@59841
   633
              if t = @{term True} then (chk (indets @ a') asms) 
walther@59841
   634
              else if t = @{term False} then ([], false)
walther@59841
   635
            (*asm false .. thm not applied ^^^; continue until False vvv*)
walther@59841
   636
            else chk (indets @ [t] @ a') asms);
walther@59841
   637
walther@59841
   638
    val (xxx, true) =
walther@59841
   639
           chk [] asms;  (*return from eval__true*);
walther@59841
   640
"~~~~~ fun chk , args:"; val (indets, (a :: asms)) = ([], asms);
walther@59841
   641
walther@59851
   642
(*+*)val Rule_Set.Repeat {id = "rateq_erls", rules, ...} = rls;
walther@59841
   643
(*+*)(*val rules =
walther@59841
   644
(*+*)   [Num_Calc ("Rings.divide_class.divide", fn),
walther@59841
   645
(*+*)    Thm ("minus_divide_left", "- ?a1 / ?b1 = - (?a1 / ?b1)"),
walther@59841
   646
(*+*)    :
walther@59841
   647
(*+*)    Num_Calc ("HOL.eq", fn),
walther@59841
   648
(*+*)    Thm ("not_true", "(\<not> True) = False"),
walther@59841
   649
(*+*)    Thm ("not_false", "(\<not> False) = True"),
walther@59841
   650
(*+*)    :
walther@59841
   651
(*+*)    Num_Calc ("Prog_Expr.pow", fn),
walther@59841
   652
(*+*)    Num_Calc ("RatEq.is'_ratequation'_in", fn)]:
walther@59841
   653
(*+*)   rule list*)
walther@59841
   654
(*+*)chk: term list -> term list -> term list * bool
walther@59841
   655
walther@59841
   656
           rewrite__set_ thy (i + 1) false bdv rls a (*of*);
walther@59841
   657
walther@59841
   658
(*+*)trace_rewrite := true;
walther@59841
   659
walther@59841
   660
        (*this was False; vvvv--- means: indeterminate*)
walther@59841
   661
    val (* SOME (t, a') *)NONE = (*case*)
walther@59841
   662
           rewrite__set_ thy (i + 1) false bdv rls a (*of*);
walther@59841
   663
walther@59868
   664
(*+*)UnparseC.term a = "x \<noteq> 0"; (* rewrite__set_ \<rightarrow> @{term True} ----------------- SHOULD BE indet !*)
neuper@42394
   665
(*
neuper@42394
   666
 :
walther@59841
   667
 ####  rls: rateq_erls on: x \<noteq> 0
neuper@42394
   668
 :
neuper@42394
   669
 #####  try calc: HOL.eq'    <<<------------------------------- here the error comes from
walther@59841
   670
 =====  calc. to: ~ False    <<<------------------------------- \<not> x = 0 is NOT False
neuper@42394
   671
 #####  try calc: HOL.eq' 
neuper@42394
   672
 #####  try thm: not_true 
neuper@42394
   673
 #####  try thm: not_false 
walther@59841
   674
 =====  rewrites to: True    <<<------------------------------- so x \<noteq> 0 is NOT True
walther@59841
   675
                                                       and True, False are NOT stored ...
walther@59841
   676
 :                             
walther@59841
   677
 ###  asms accepted: [x \<noteq> 0]   stored: []
walther@59841
   678
 : *)
neuper@42394
   679
trace_rewrite := false;
walther@59841
   680
( *\------- outcomment this code: otherwise the re-definition could infect tests lateron ------/*)
walther@59841
   681
neuper@42394
   682
wneuper@59252
   683
"----------- fun rewrite_ down to Pattern.match ------------------------------------------------";
wneuper@59252
   684
"----------- fun rewrite_ down to Pattern.match ------------------------------------------------";
wneuper@59252
   685
"----------- fun rewrite_ down to Pattern.match ------------------------------------------------";
wneuper@59252
   686
"~~~~~ fun rewrite_ , args:"; val (thy, rew_ord, erls, bool, thm, term) =
walther@59852
   687
  (@{theory}, dummy_ord, Rule_Set.empty, false, @{thm distrib_left}, @{term "x * (y + z) :: int"});
wneuper@59252
   688
"~~~~~ fun rewrite__ , args:"; val (thy, i, bdv, tless, rls, put_asm, thm, ct) =
wneuper@59252
   689
  (thy, 1, [], rew_ord, erls, bool, thm, term);
wneuper@59252
   690
"~~~~~ and rew_sub , args:"; val (thy, i, bdv, tless, rls, put_asm, lrd, r, t) =
wneuper@59252
   691
  (thy, i, bdv, tless, rls, put_asm, [], (((inst_bdv bdv) o norm o #prop o Thm.rep_thm) thm), ct)
wneuper@59252
   692
     val (lhss, rhss) = (HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r
wneuper@59252
   693
     val r' = Envir.subst_term (Pattern.match thy (lhss, t) (Vartab.empty, Vartab.empty)) r
wneuper@59252
   694
     val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) (Logic.count_prems r', [], r'))
wneuper@59252
   695
     val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r'
wneuper@59252
   696
;
walther@59868
   697
if UnparseC.term lhss = "?a * (?b + ?c)" andalso UnparseC.term t = "x * (y + z)" then ()
wneuper@59252
   698
else error "ARGS FOR Pattern.match CHANGED";
wneuper@59252
   699
val match = Pattern.match thy (lhss, t) (Vartab.empty, Vartab.empty);
walther@59868
   700
if (Envir.subst_term match r |> UnparseC.term) = "x * (y + z) = x * y + x * z" then ()
wneuper@59252
   701
  else error "Pattern.match CHANGED";
wneuper@59381
   702
wneuper@59411
   703
"----------- fun rewrite_set_ .. norm_equation -------------------------------------------------";
wneuper@59411
   704
"----------- fun rewrite_set_ .. norm_equation -------------------------------------------------";
wneuper@59411
   705
"----------- fun rewrite_set_ .. norm_equation -------------------------------------------------";
wneuper@59411
   706
(* norm_equation is defined in Test.thy, other rls see Knowledg/**)
wneuper@59411
   707
val thy = @{theory};
wneuper@59411
   708
val rls = norm_equation;
wneuper@59411
   709
val term = str2term "x + 1 = 2";
wneuper@59411
   710
wneuper@59411
   711
val SOME (t, asm) = rewrite_set_ thy false rls term;
walther@59868
   712
if UnparseC.term t = "x + 1 + -1 * 2 = 0" then () else error "rewrite_set_ norm_equation CHANGED";
wneuper@59411
   713
wneuper@59411
   714
"~~~~~ fun rewrite_set_, args:"; val (thy, bool, rls, term) = (thy, false, rls, term);
wneuper@59411
   715
"~~~~~ fun rewrite__set_, args:"; val (thy, i, put_asm, bdv, rls, ct) = (thy, 1, bool, [], rls, term);
walther@59841
   716