test/Tools/isac/MathEngBasic/rewrite.sml
author wneuper <walther.neuper@jku.at>
Tue, 20 Jul 2021 14:37:56 +0200
changeset 60339 0d22a6bf1fc6
parent 60337 cbad4e18e91b
child 60340 0ee698b0a703
permissions -rw-r--r--
//reduce the number of TermC.parse*; "//"means: tests broken .

broken tests are outcommented with "reduce the number of TermC.parse*"
walther@60318
     1
(* Title: "MathEngBasic/rewrite.sml"
neuper@38036
     2
   Author: Walther Neuper 050908
neuper@37906
     3
   (c) copyright due to lincense terms.
neuper@37906
     4
*)
neuper@37906
     5
walther@60262
     6
"-----------------------------------------------------------------------------------------------";
walther@60262
     7
"table of contents -----------------------------------------------------------------------------";
walther@60262
     8
"-----------------------------------------------------------------------------------------------";
walther@60262
     9
"----------- assemble rewrite ------------------------------------------------------------------";
walther@60262
    10
"----------- test rewriting without Isac's thys ------------------------------------------------";
walther@60262
    11
"----------- test rewriting without Isac's thys, ~~~~~ fun rewrite_ ----------------------------";
walther@60262
    12
"----------- conditional rewriting without Isac's thys -----------------------------------------";
walther@60262
    13
"----------- conditional rewriting without Isac's thys, ~~~~~ fun ------------------------------";
walther@60262
    14
"----------- conditional rewriting creating an assumption---------------------------------------";
walther@60262
    15
"----------- step through 'and rew_sub': -------------------------------------------------------";
walther@60262
    16
"----------- step through 'fun rewrite_terms_'  ------------------------------------------------";
walther@60262
    17
"----------- rewrite_inst_ bdvs ----------------------------------------------------------------";
walther@60262
    18
"----------- check diff 2002--2009-3 -----------------------------------------------------------";
walther@60262
    19
"----------- compare all prepat's existing 2010 ------------------------------------------------";
walther@60262
    20
"----------- fun app_rev, Rrls, ----------------------------------------------------------------";
walther@60262
    21
"----------- 2011 thms with axclasses ----------------------------------------------------------";
walther@59841
    22
"----------- fun rewrite_set_ .. RatEq_eliminate step into to fun chk --------------------------";
wneuper@59252
    23
"----------- fun rewrite_ down to Pattern.match ------------------------------------------------";
wneuper@59411
    24
"----------- fun rewrite_set_ .. norm_equation -------------------------------------------------";
walther@60262
    25
"-----------------------------------------------------------------------------------------------";
walther@60262
    26
"-----------------------------------------------------------------------------------------------";
walther@60262
    27
"-----------------------------------------------------------------------------------------------";
neuper@37906
    28
neuper@38036
    29
walther@60262
    30
"----------- assemble rewrite ------------------------------------------------------------------";
walther@60262
    31
"----------- assemble rewrite ------------------------------------------------------------------";
walther@60262
    32
"----------- assemble rewrite ------------------------------------------------------------------";
neuper@37906
    33
"===== rewriting by thm with 'a";
neuper@41924
    34
(*show_types := true;*)
akargl@42188
    35
neuper@37906
    36
val thy = @{theory Complex_Main};
neuper@37906
    37
val ctxt = @{context};
wneuper@59112
    38
val thm = @{thm add.commute};
walther@60339
    39
val t = TermC.parseNEW'' thy "((r + u) + t) + s";
neuper@37906
    40
"----- from old: fun rewrite__";
neuper@37906
    41
val bdv = [];
walther@60230
    42
val r = TermC.inst_bdv bdv (Eval.norm (Thm.prop_of thm));
neuper@37906
    43
"----- from old: and rew_sub";
wneuper@59395
    44
val (LHS,RHS) = (dest_equals o strip_trueprop 
neuper@37906
    45
   	      o Logic.strip_imp_concl) r;
neuper@37906
    46
(* old
neuper@41942
    47
val insts = Pattern.match thy (LHS,t) (Vartab.empty, Vartab.empty);*)
neuper@37906
    48
"----- fun match_rew in Pure/pattern.ML";
neuper@41942
    49
val rtm = the_default RHS (Term.rename_abs LHS t RHS);
neuper@37906
    50
neuper@38022
    51
writeln(Syntax.string_of_term ctxt rtm);
neuper@41942
    52
writeln(Syntax.string_of_term ctxt LHS);
neuper@38022
    53
writeln(Syntax.string_of_term ctxt t);
neuper@37906
    54
walther@60339
    55
(*broken with "reduce the number of TermC.parse*-------exception MATCH---------------------\\
walther@60339
    56
TOODOO ERROR exception MATCH raised (line 284 of "pattern.ML)
walther@60339
    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');
walther@60339
    82
--broken with "reduce the number of TermC.parse*------------------------------------------//( **)
neuper@37906
    83
walther@60262
    84
"----------- test rewriting without Isac's thys ------------------------------------------------";
walther@60262
    85
"----------- test rewriting without Isac's thys ------------------------------------------------";
walther@60262
    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
walther@60262
    94
val SOME (r,_) = (rewrite_ thy dummy_ord Rule_Set.empty false thm tm);
neuper@37906
    95
(*is displayed on _TOP_ of <response> buffer...*)
neuper@48761
    96
Pretty.writeln (Proof_Context.pretty_term_abbrev @{context} r);
neuper@38022
    97
if r = @{term "y*z + x::real"}
neuper@38022
    98
then () else error "rewrite.sml diff.result in rewriting";
neuper@37906
    99
neuper@37906
   100
"----- rewriting a subterm";
neuper@37906
   101
val tm = @{term "w*(x + y*z)::real"};
neuper@37906
   102
walther@60262
   103
val SOME (r, _) = (rewrite_ thy dummy_ord Rule_Set.empty false thm tm);
neuper@37906
   104
neuper@37906
   105
"----- ordered rewriting";
wneuper@59462
   106
fun tord (_:subst) pp = LibraryC.termless pp;
neuper@37906
   107
if tord [] (@{term "x + y*z::real"}, @{term "y*z + x::real"}) then ()
neuper@38022
   108
else error "rewrite.sml diff.behav. in ord.rewr.";
neuper@37906
   109
walther@60262
   110
val NONE = (rewrite_ thy tord Rule_Set.empty false thm tm);
neuper@37906
   111
(*is displayed on _TOP_ of <response> buffer...*)
neuper@48761
   112
Pretty.writeln (Proof_Context.pretty_term_abbrev @{context} r);
neuper@37906
   113
neuper@37906
   114
val tm = @{term "x*y + z::real"};
walther@60262
   115
val SOME (r,_) = (rewrite_ thy tord Rule_Set.empty false thm tm);
neuper@37906
   116
neuper@37906
   117
walther@60262
   118
"----------- conditional rewriting without Isac's thys -----------------------------------------";
walther@60262
   119
"----------- conditional rewriting without Isac's thys -----------------------------------------";
walther@60262
   120
"----------- conditional rewriting without Isac's thys -----------------------------------------";
neuper@37906
   121
"===== prepr cond.rew. with Pattern.match";
neuper@37906
   122
val thy = @{theory Complex_Main};
neuper@37906
   123
val ctxt = @{context};
walther@60262
   124
val thm =  @{thm nonzero_divide_mult_cancel_right}; (* = "?b \<noteq> 0 \<Longrightarrow> ?b / (?a * ?b) = 1 / ?a":*)
neuper@37906
   125
val rule = Thm.prop_of thm;
wneuper@59358
   126
val tm = @{term "x / (2 * x)::real"};
neuper@37906
   127
val prem = Logic.strip_imp_prems rule;
neuper@37906
   128
val nps = Logic.count_prems rule;
neuper@37906
   129
val prems = Logic.strip_prems (nps, [], rule);
neuper@37906
   130
neuper@37906
   131
val eq = Logic.strip_imp_concl rule;
neuper@41942
   132
val (LHS, RHS) = (HOLogic.dest_eq o HOLogic.dest_Trueprop) eq;
neuper@37906
   133
neuper@41942
   134
val mtcs = Pattern.match thy (LHS, tm) (Vartab.empty, Vartab.empty);
neuper@37906
   135
val rule' = Envir.subst_term mtcs rule;
neuper@37906
   136
walther@60262
   137
val prems' = (fst o Logic.strip_prems) (Logic.count_prems rule', [], rule');
walther@60262
   138
val RHS' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) rule';
neuper@37906
   139
walther@60262
   140
(rule' |> UnparseC.term) = "x \<noteq> 0 \<Longrightarrow> x / (2 * x) = 1 / 2";
walther@60262
   141
 rule';
walther@60262
   142
walther@60262
   143
(rule' |> Logic.strip_imp_concl |> UnparseC.term) = "x / (2 * x) = 1 / 2";
walther@60262
   144
 rule' |> Logic.strip_imp_concl;
walther@60262
   145
walther@60262
   146
(rule' |> Logic.strip_imp_concl |> UnparseC.term) = "x / (2 * x) = 1 / 2";
walther@60262
   147
 rule' |> Logic.strip_imp_concl;
walther@60262
   148
walther@60262
   149
walther@60262
   150
"----------- conditional rewriting creating an assumption---------------------------------------";
walther@60262
   151
"----------- conditional rewriting creating an assumption---------------------------------------";
walther@60262
   152
"----------- conditional rewriting creating an assumption---------------------------------------";
wneuper@59358
   153
val thm =  @{thm nonzero_divide_mult_cancel_right};
wneuper@59358
   154
val tm = @{term "x / (2 * x)::real"};
walther@60262
   155
walther@60262
   156
val SOME (rew, asm) = (rewrite_ thy dummy_ord Rule_Set.empty false thm tm);
neuper@37906
   157
wneuper@59358
   158
if rew = @{term "1 / 2::real"} then () (* the rewrite is _NO_ Trueprop *)
neuper@38022
   159
else error "rewrite.sml diff.result in cond.rew.";
neuper@37906
   160
wneuper@59358
   161
if hd asm = @{term "x \<noteq> (0::real)"} (* dropped Trueprop $ ...*)
neuper@38022
   162
then () else error "rewrite.sml diff.asm in cond.rew.";
neuper@38022
   163
"----- conditional rewriting immediately: can only be done with ";
neuper@38022
   164
"------Isabelle numerals, because erls cannot handle them yet.";
neuper@37906
   165
neuper@37906
   166
walther@60262
   167
"----------- conditional rewriting creating an assumption, ~~~~~ fun rewrite_ ------------------";
walther@60262
   168
"----------- conditional rewriting creating an assumption, ~~~~~ fun rewrite_ ------------------";
walther@60262
   169
"----------- conditional rewriting creating an assumption, ~~~~~ fun rewrite_ ------------------";
walther@60262
   170
val thm = @{thm nonzero_divide_mult_cancel_right};(* = "?b \<noteq> 0 \<Longrightarrow> ?b / (?a * ?b) = 1 / ?a"*)
walther@60262
   171
val tm = @{term "x / (2 * x)::real"};
walther@60262
   172
val erls = eval_rls;
walther@60262
   173
walther@60262
   174
(**)val SOME (rew, asm) = (rewrite_ thy dummy_ord Rule_Set.empty false thm tm);
walther@60262
   175
(** )exception TERM raised (line 196 of "~~/src/HOL/Tools/hologic.ML"):
walther@60262
   176
  dest_Trueprop
walther@60262
   177
  ?b \<noteq> 0 \<Longrightarrow> ?b / (?a * ?b) = 1 / ?a( **)
walther@60262
   178
"~~~~~ fun rewrite_ , args:"; val (thy, rew_ord, erls, bool, thm, term) =
walther@60262
   179
  (thy, dummy_ord, erls, false, thm, tm);
walther@60262
   180
"~~~~~ fun rewrite__ , args:"; val (thy, i, bdv, tless, rls, put_asm, thm, ct) =
walther@60262
   181
  (thy, 1, []: (term * term) list, rew_ord, erls, bool, thm, term);
walther@60262
   182
walther@60262
   183
(**) val (t', asms, _(*lrd*), rew) = rew_sub thy i bdv tless rls put_asm ([(*root of the term*)]: TermC.path)
walther@60262
   184
		  (TermC.inst_bdv bdv (Eval.norm (Thm.prop_of thm))) ct;
walther@60262
   185
(** )exception TERM raised (line 196 of "~~/src/HOL/Tools/hologic.ML"):
walther@60262
   186
  dest_Trueprop
walther@60262
   187
  ?b \<noteq> 0 \<Longrightarrow> ?b / (?a * ?b) = 1 / ?a( **)
walther@60262
   188
"~~~~~ fun rew_sub , args:"; val (thy, i, bdv, tless, rls, put_asm, lrd, r, t) =
walther@60262
   189
  (thy, i, bdv, tless, rls, put_asm, ([(*root of the term*)]: TermC.path),
walther@60262
   190
		  (TermC.inst_bdv bdv (Eval.norm (Thm.prop_of thm))), ct);
walther@60262
   191
(*+*)UnparseC.term r = "?b \<noteq> (0::?'a) \<Longrightarrow> ?b / (?a * ?b) = (1::?'a) / ?a";
walther@60262
   192
walther@60262
   193
    val (lhs, rhs) = (HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r;
walther@60262
   194
(*+*)(UnparseC.term lhs, UnparseC.term rhs) = ("?b / (?a * ?b)", "(1::?'a) / ?a");
walther@60262
   195
    val r' = (Envir.subst_term (Pattern.match thy (lhs, t) (Vartab.empty, Vartab.empty)) r)
walther@60262
   196
      handle Pattern.MATCH => raise NO_REWRITE;
walther@60262
   197
    val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) (Logic.count_prems r', [], r'));
walther@60262
   198
(*+*)UnparseC.term r' = "x \<noteq> 0 \<Longrightarrow> x / (2 * x) = 1 / 2";
walther@60262
   199
(*+*)r';
walther@60262
   200
    val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r'
walther@60262
   201
    val _ = trace_in2 i "eval asms" thy r';
walther@60262
   202
        val (simpl_p', nofalse) = eval__true thy (i + 1) p' bdv rls;
walther@60262
   203
	      (*if*) nofalse (*then*);
walther@60262
   204
      val (t'', p'') = (trace_in4 i "asms accepted" thy p' simpl_p'; (t',simpl_p'));
walther@60262
   205
(*##  asms accepted: [x \<noteq> 0]   stored: [x \<noteq> 0] *)
walther@60262
   206
walther@60262
   207
(*+*)if (UnparseC.term t'', map UnparseC.term p'') = ("1 / 2", ["x \<noteq> 0"]) then ()
walther@60262
   208
(*+*)else error "conditional rewriting x \<noteq> 0 \<Longrightarrow> x / (2 * x) = 1 / 2 CHANGED";
walther@60262
   209
walther@60262
   210
neuper@38022
   211
"----------- step through 'and rew_sub': ----------------";
neuper@38022
   212
"----------- step through 'and rew_sub': ----------------";
neuper@38022
   213
"----------- step through 'and rew_sub': ----------------";
neuper@38022
   214
(*and make asms without Trueprop, beginning with the result:*)
wneuper@59358
   215
val tm = @{term "x / (2 * x)::real"};
walther@59852
   216
val (t', asm, _, _) = rew_sub thy 0 [] dummy_ord Rule_Set.empty true [] (Thm.prop_of thm) tm;
neuper@41924
   217
(*show_types := false;*)
neuper@38022
   218
"----- evaluate arguments";
neuper@38022
   219
val (thy, i, bdv, tless, rls, put_asm, lrd, r, t) = 
walther@59852
   220
    (thy, 0, [], dummy_ord, Rule_Set.empty, true, [], (Thm.prop_of thm), tm);
neuper@41942
   221
"----- step 1: LHS, RHS of rule";
neuper@41942
   222
     val (LHS, RHS) = (HOLogic.dest_eq o HOLogic.dest_Trueprop
neuper@38022
   223
                       o Logic.strip_imp_concl) r;
walther@59868
   224
UnparseC.term r = "?b \<noteq> (0::?'a) \<Longrightarrow> ?b / (?a * ?b) = (1::?'a) / ?a";
walther@59868
   225
UnparseC.term LHS = "?b / (?a * ?b)"; UnparseC.term RHS = "(1::?'a) / ?a";
neuper@38022
   226
"----- step 2: the rule instantiated";
neuper@38025
   227
     val r' = Envir.subst_term 
neuper@41942
   228
                  (Pattern.match thy (LHS, t) (Vartab.empty, Vartab.empty)) r;
walther@59868
   229
UnparseC.term r' = "x \<noteq> 0 \<Longrightarrow> x / (2 * x) = 1 / 2";
neuper@38022
   230
"----- step 3: get the (instantiated) assumption(s)";
neuper@38022
   231
     val p' = (fst o Logic.strip_prems) (Logic.count_prems r', [], r');
walther@59868
   232
UnparseC.term (hd p') = "x \<noteq> 0";
neuper@38022
   233
"=====vvv make asms without Trueprop ---vvv";
neuper@38022
   234
     val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) 
neuper@38022
   235
                                             (Logic.count_prems r', [], r'));
neuper@38022
   236
case p' of
wenzelm@60309
   237
    [Const (\<^const_name>\<open>Not\<close>, _) $ (Const (\<^const_name>\<open>HOL.eq\<close>, _) 
walther@60336
   238
      $ Free ("x", _) $ Const (\<^const_name>\<open>zero_class.zero\<close>, _))] => ()
neuper@38022
   239
  | _ => error "rewrite.sml assumption changed";
walther@60242
   240
"===== \<up>  make asms without Trueprop --- \<up> ";
neuper@41942
   241
"----- step 4: get the (instantiated) RHS";
neuper@38022
   242
     val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop 
neuper@38022
   243
               o Logic.strip_imp_concl) r';
walther@59868
   244
UnparseC.term t' = "1 / 2";
neuper@37906
   245
walther@60262
   246
"----------- step through 'fun rewrite_terms_'  ------------------------------------------------";
walther@60262
   247
"----------- step through 'fun rewrite_terms_'  ------------------------------------------------";
walther@60262
   248
"----------- step through 'fun rewrite_terms_'  ------------------------------------------------";
neuper@38025
   249
"----- step 0: args for rewrite_terms_, local fun";
neuper@38025
   250
val (thy, ord, erls, equs, t) =
walther@60230
   251
    (@{theory "Biegelinie"}, dummy_ord, Rule_Set.Empty, [TermC.str2term "x = 0"],
walther@60242
   252
     TermC.str2term "M_b x = -1 * q_0 * x \<up> 2 / 2 + x * c + c_2");
neuper@38025
   253
"----- step 1: args for rew_";
walther@59861
   254
val ((t', asm'), (rules as r::rs), t) = ((TermC.empty, []), equs, t);
neuper@38025
   255
"----- step 2: rew_sub";
wneuper@59395
   256
rew_sub thy 1 [] ord erls false [] (HOLogic.Trueprop $ r) t;
neuper@38025
   257
"----- step 3: step through rew_sub -- inefficient: goes into subterms";
neuper@38025
   258
neuper@38025
   259
walther@59851
   260
val SOME (t', _) = rewrite_terms_ thy dummy_ord Rule_Set.Empty equs t;
walther@60262
   261
writeln "---------- rewrite_terms_  1---------------------------";
walther@60317
   262
if UnparseC.term t' = "M_b 0 = - 1 * q_0 * 0 \<up> 2 / 2 + 0 * c + c_2" then ()
neuper@38031
   263
else error "rewrite.sml rewrite_terms_ [x = 0]";
neuper@37906
   264
walther@60230
   265
val equs = [TermC.str2term "M_b 0 = 0"];
walther@60317
   266
val t = TermC.str2term "M_b 0 = - 1 * q_0 * 0 \<up> 2 / 2 + 0 * c + c_2";
walther@59851
   267
val SOME (t', _) = rewrite_terms_ thy dummy_ord Rule_Set.Empty equs t;
walther@60262
   268
writeln "---------- rewrite_terms_  2---------------------------";
walther@60317
   269
if UnparseC.term t' = "0 = - 1 * q_0 * 0 \<up> 2 / 2 + 0 * c + c_2" then ()
neuper@38031
   270
else error "rewrite.sml rewrite_terms_ [M_b 0 = 0]";
neuper@37906
   271
walther@60230
   272
val equs = [TermC.str2term "x = 0", TermC.str2term"M_b 0 = 0"];
walther@60317
   273
val t = TermC.str2term "M_b x = - 1 * q_0 * x \<up> 2 / 2 + x * c + c_2";
walther@59851
   274
val SOME (t', _) = rewrite_terms_ thy dummy_ord Rule_Set.Empty equs t;
walther@60262
   275
writeln "---------- rewrite_terms_  3---------------------------";
walther@60317
   276
if UnparseC.term t' = "0 = - 1 * q_0 * 0 \<up> 2 / 2 + 0 * c + c_2" then ()
neuper@38031
   277
else error "rewrite.sml rewrite_terms_ [x = 0, M_b 0 = 0]";
neuper@37906
   278
neuper@37906
   279
walther@60262
   280
"----------- rewrite_inst_ bdvs ----------------------------------------------------------------";
walther@60262
   281
"----------- rewrite_inst_ bdvs ----------------------------------------------------------------";
walther@60262
   282
"----------- rewrite_inst_ bdvs ----------------------------------------------------------------";
neuper@37906
   283
(*see smltest/Scripts/term_G.sml: inst_bdv 2*)
walther@60242
   284
val t = TermC.str2term"-1 * (q_0 * L \<up> 2) / 2 + (L * c_3 + c_4) = 0";
walther@60230
   285
val bdvs = [(TermC.str2term"bdv_1",TermC.str2term"c"),
walther@60230
   286
	    (TermC.str2term"bdv_2",TermC.str2term"c_2"),
walther@60230
   287
	    (TermC.str2term"bdv_3",TermC.str2term"c_3"),
walther@60230
   288
	    (TermC.str2term"bdv_4",TermC.str2term"c_4")];
neuper@37906
   289
(*------------ outcommented WN071210, after inclusion into ROOT.ML 
neuper@37926
   290
val SOME (t,_) = 
neuper@37906
   291
    rewrite_inst_ thy e_rew_ord 
walther@59852
   292
		  (Rule_Set.append_rules "erls_isolate_bdvs" Rule_Set.empty 
walther@60278
   293
			      [(Eval ("EqSystem.occur_exactly_in", 
neuper@37906
   294
				      eval_occur_exactly_in 
neuper@37906
   295
					  "#eval_occur_exactly_in_"))
neuper@37906
   296
			       ]) 
walther@59871
   297
		  false bdvs (ThmC.numerals_to_Free @{separate_bdvs_add) t;
walther@59868
   298
(writeln o UnparseC.term) t;
walther@60242
   299
if UnparseC.term t = "L * c_3 + c_4 = 0 + -1 * (-1 * (q_0 * L \<up> 2) / 2)"
neuper@38031
   300
then () else error "rewrite.sml rewrite_inst_ bdvs";
walther@60330
   301
> Rewrite.trace_on:=true;false
walther@59901
   302
Rewrite.trace_on:=false;--------------------------------------------*)
neuper@37906
   303
neuper@38025
   304
walther@60262
   305
"----------- compare all prepat's existing 2010 ------------------------------------------------";
walther@60262
   306
"----------- compare all prepat's existing 2010 ------------------------------------------------";
walther@60262
   307
"----------- compare all prepat's existing 2010 ------------------------------------------------";
wneuper@59592
   308
val thy = @{theory "Isac_Knowledge"};
neuper@38036
   309
val t = @{term "a + b * x = (0 ::real)"};
walther@60230
   310
val pat = TermC.parse_patt thy "?l = (?r ::real)";
walther@60230
   311
val precond = TermC.parse_patt thy "is_polynomial (?l::real)";(*no infix def*)
walther@60230
   312
val precond = TermC.parse_patt thy "(?l::real) is_expanded"; 
neuper@38036
   313
neuper@38036
   314
val inst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
neuper@38036
   315
val preinst = Envir.subst_term inst precond;
walther@59868
   316
UnparseC.term preinst;
neuper@38036
   317
neuper@38036
   318
"===== Rational.thy: cancel ===";
neuper@38036
   319
(* pat matched with the current term gives an environment 
neuper@38036
   320
   (or not: hen the Rrls not applied);
neuper@48760
   321
   if pre1 and pre2 evaluate to @{term True} in this environment,
neuper@38036
   322
   then the Rrls is applied. *)
walther@60230
   323
val t = TermC.str2term "(a + b) / c ::real";
walther@60230
   324
val pat = TermC.parse_patt thy "?r / ?s ::real";
walther@60230
   325
val pres = [TermC.parse_patt thy "?r is_expanded", TermC.parse_patt thy "?s is_expanded"];
neuper@38036
   326
val prepat = [(pres, pat)];
neuper@38036
   327
val erls = rational_erls;
neuper@38036
   328
(* erls got from Rrls {erls, prepat, scr = Rfuns {normal_form, ...}, ...} *)
neuper@38036
   329
neuper@38036
   330
val subst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
neuper@38036
   331
val asms = map (Envir.subst_term subst) pres;
walther@59997
   332
if UnparseC.terms asms = "[\"a + b is_expanded\", \"c is_expanded\"]"
neuper@38036
   333
then () else error "rewrite.sml: prepat cancel subst";
walther@60318
   334
neuper@38036
   335
if ([], true) = eval__true thy 0 asms [] erls
neuper@38036
   336
then () else error "rewrite.sml: prepat cancel eval__true";
neuper@38036
   337
neuper@52105
   338
"===== Rational.thy: add_fractions_p ===";
walther@60230
   339
(* if each pat* TermC.matches with the current term, the Rrls is applied
neuper@48760
   340
   (there are no preconditions to be checked, they are @{term True}) *)
walther@60230
   341
val t = TermC.str2term "a / b + 1 / 2";
walther@60230
   342
val pat = TermC.parse_patt thy "?r / ?s + ?u / ?v";
neuper@48760
   343
val pres = [@{term True}];
neuper@38036
   344
val prepat = [(pres, pat)];
neuper@38036
   345
val erls = rational_erls;
neuper@38036
   346
(* erls got from Rrls {erls, prepat, scr = Rfuns {normal_form, ...}, ...} *)
neuper@38036
   347
neuper@38036
   348
val subst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
neuper@38036
   349
if ([], true) = eval__true thy 0 (map (Envir.subst_term subst) pres) [] erls
neuper@52105
   350
then () else error "rewrite.sml: prepat add_fractions_p";
neuper@38036
   351
neuper@38036
   352
"===== Poly.thy: order_mult_ ===";
neuper@38036
   353
          (* ?p matched with the current term gives an environment,
neuper@38036
   354
             which evaluates (the instantiated) "p is_multUnordered" to true*)
walther@60242
   355
val t = TermC.str2term "x \<up> 2 * x";
walther@60230
   356
val pat = TermC.parse_patt thy "?p :: real"
walther@60230
   357
val pres = [TermC.parse_patt thy "?p is_multUnordered"];
neuper@38036
   358
val prepat = [(pres, pat)];
walther@59852
   359
val erls = Rule_Set.append_rules "Rule_Set.empty-is_multUnordered" Rule_Set.empty
walther@60278
   360
		      [Eval ("Poly.is_multUnordered", 
neuper@38036
   361
                             eval_is_multUnordered "")];
neuper@38036
   362
neuper@38036
   363
val subst = Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty);
neuper@38036
   364
val asms = map (Envir.subst_term subst) pres;
walther@60242
   365
if UnparseC.terms asms = "[\"x \<up> 2 * x is_multUnordered\"]"
neuper@38036
   366
then () else error "rewrite.sml: prepat order_mult_ subst";
walther@60318
   367
neuper@38036
   368
if ([], true) = eval__true thy 0 asms [] erls
neuper@38036
   369
then () else error "rewrite.sml: prepat order_mult_ eval__true";
neuper@38036
   370
neuper@38036
   371
walther@60262
   372
"----------- fun app_rev, Rrls, ----------------------------------------------------------------";
walther@60262
   373
"----------- fun app_rev, Rrls, ----------------------------------------------------------------";
walther@60262
   374
"----------- fun app_rev, Rrls, ----------------------------------------------------------------";
walther@60242
   375
val t = TermC.str2term "x \<up> 2 * x";
neuper@41928
   376
neuper@38039
   377
if is_multUnordered t then () else error "rewrite.sml diff. is_multUnordered 2";
walther@60242
   378
val tm = TermC.str2term "(x \<up> 2 * x) is_multUnordered";
walther@60318
   379
walther@60318
   380
(*+*)case eval_is_multUnordered "testid" "" tm thy of
walther@60318
   381
(*+*)  SOME
walther@60318
   382
(*+*)    ("testidx \<up> 2 * x_",
walther@60336
   383
(*+*)     Const (\<^const_name>\<open>Trueprop\<close>, _) $
walther@60336
   384
(*+*)       (Const (\<^const_name>\<open>HOL.eq\<close>, _) $
walther@60336
   385
(*+*)         (Const (\<^const_name>\<open>is_multUnordered\<close>, _) $
walther@60336
   386
(*+*)           (Const (\<^const_name>\<open>times\<close>, _) $
walther@60336
   387
(*+*)             (Const (\<^const_name>\<open>powr\<close>, _) $ Free ("x", _) $ _ ) $ Free ("x", _))) $
walther@60336
   388
(*+*)         Const (\<^const_name>\<open>True\<close>, _))) => ()
walther@60318
   389
(*+*)(*                   ^^^^^^             compare ---vvv *)
walther@60318
   390
(*+*)| _ => error "rewrite.sml diff. eval_is_multUnordered 2b CHANGED";
walther@60318
   391
walther@60318
   392
walther@60318
   393
     eval_is_multUnordered "testid" "" tm thy;
neuper@41928
   394
neuper@38039
   395
case eval_is_multUnordered "testid" "" tm thy of
wenzelm@60309
   396
    SOME (_, Const (\<^const_name>\<open>Trueprop\<close>, _) $ 
wenzelm@60309
   397
                   (Const (\<^const_name>\<open>HOL.eq\<close>, _) $
walther@60336
   398
                          (Const (\<^const_name>\<open>is_multUnordered\<close>, _) $ _) $ 
wenzelm@60309
   399
                          Const (\<^const_name>\<open>True\<close>, _))) => ()
neuper@41928
   400
  | _ => error "rewrite.sml diff. eval_is_multUnordered 2b";
neuper@38039
   401
walther@60330
   402
tracing "----- begin rewrite x \<up> 2 * x ---"; Rewrite.trace_on := false; (*true false*)
neuper@38039
   403
val SOME (t', _) = rewrite_set_ thy true order_mult_ t;
walther@60330
   404
tracing "----- end rewrite x \<up> 2 * x ---"; Rewrite.trace_on := false; (*true false*)
walther@60242
   405
if UnparseC.term t' = "x * x \<up> 2" then ()
walther@60278
   406
else error "rewrite.sml Poly.is_multUnordered doesn't work";
neuper@38039
   407
neuper@38039
   408
(* for achieving the previous result, the following code was taken apart *)
neuper@38039
   409
"----- rewrite__set_ ---";
neuper@38039
   410
val (thy, i, _, _, (rrls as Rrls _), t) = (thy, 0, true, [], order_mult_, tm);
neuper@38039
   411
	val (t', asm, rew) = app_rev thy (i+1) rrls t;
neuper@38039
   412
"----- app_rev ---";
neuper@38039
   413
val (thy, i, rrls, t) = (thy, (i+1), rrls, t);
neuper@38039
   414
	fun chk_prepat thy erls [] t = true
neuper@38039
   415
	  | chk_prepat thy erls prepat t =
neuper@38039
   416
	    let fun chk (pres, pat) =
neuper@38039
   417
		    (let val subst: Type.tyenv * Envir.tenv = 
neuper@38039
   418
			     Pattern.match thy (pat, t)
neuper@38039
   419
					    (Vartab.empty, Vartab.empty)
neuper@38039
   420
		     in snd (eval__true thy (i+1) 
neuper@38039
   421
					(map (Envir.subst_term subst) pres)
neuper@38039
   422
					[] erls)
neuper@38039
   423
		     end)
walther@60270
   424
		    handle Pattern.MATCH => false
neuper@38039
   425
		fun scan_ f [] = false (*scan_ NEVER called by []*)
neuper@38039
   426
		  | scan_ f (pp::pps) = if f pp then true
neuper@38039
   427
					else scan_ f pps;
neuper@38039
   428
	    in scan_ chk prepat end;
neuper@38039
   429
neuper@38039
   430
	(*.apply the normal_form of a rev-set.*)
neuper@38039
   431
	fun app_rev' thy (Rrls{erls,prepat,scr=Rfuns{normal_form,...},...}) t =
neuper@38039
   432
	    if chk_prepat thy erls prepat t
walther@59868
   433
	    then ((*tracing("### app_rev': t = "^(UnparseC.term t));*)
neuper@38039
   434
                  normal_form t)
neuper@38039
   435
	    else NONE;
neuper@38039
   436
(*fixme val NONE = app_rev' thy rrls t;*)
neuper@38039
   437
"----- app_rev' ---";
neuper@38039
   438
val (thy, Rrls{erls,prepat,scr=Rfuns{normal_form,...},...}, t) = (thy, rrls, t);
neuper@38039
   439
(*fixme false*)   chk_prepat thy erls prepat t;
neuper@38039
   440
"----- chk_prepat ---";
neuper@38039
   441
val (thy, erls, prepat, t) = (thy, erls, prepat, t);
neuper@38039
   442
                fun chk (pres, pat) =
neuper@38039
   443
		    (let val subst: Type.tyenv * Envir.tenv = 
neuper@38039
   444
			     Pattern.match thy (pat, t)
neuper@38039
   445
					    (Vartab.empty, Vartab.empty)
neuper@38039
   446
		     in snd (eval__true thy (i+1) 
neuper@38039
   447
					(map (Envir.subst_term subst) pres)
neuper@38039
   448
					[] erls)
neuper@38039
   449
		     end)
walther@60270
   450
		    handle Pattern.MATCH => false;
walther@60270
   451
		fun scan_ _ [] = false (*scan_ NEVER called by []*)
neuper@38039
   452
		  | scan_ f (pp::pps) = if f pp then true
neuper@38039
   453
					else scan_ f pps;
neuper@38039
   454
tracing "=== poly.sml: scan_ chk prepat begin";
neuper@38039
   455
scan_ chk prepat;
neuper@38039
   456
tracing "=== poly.sml: scan_ chk prepat end";
neuper@38039
   457
neuper@38039
   458
"----- chk ---";
walther@60242
   459
(*reestablish...*) val t = TermC.str2term "x \<up> 2 * x";
neuper@38039
   460
val [(pres, pat)] = prepat;
neuper@38039
   461
                         val subst: Type.tyenv * Envir.tenv = 
neuper@38039
   462
			     Pattern.match thy (pat, t)
neuper@38039
   463
					    (Vartab.empty, Vartab.empty);
neuper@38039
   464
neuper@38039
   465
(*fixme: asms = ["p is_multUnordered"]...instantiate*)
neuper@38039
   466
"----- eval__true ---";
neuper@38039
   467
val asms = (map (Envir.subst_term subst) pres);
walther@60242
   468
if UnparseC.terms asms = "[\"x \<up> 2 * x is_multUnordered\"]" then ()
neuper@38039
   469
else error "rewrite.sml: diff. is_multUnordered, asms";
neuper@38039
   470
val (thy, i, asms, bdv, rls) = 
walther@60242
   471
    (thy, (i+1), [TermC.str2term "(x \<up> 2 * x) is_multUnordered"], 
neuper@38039
   472
     [] : (term * term) list, erls);
neuper@38039
   473
case eval__true thy i asms bdv rls of 
neuper@38039
   474
    ([], true) => ()
neuper@38039
   475
  | _ => error "rewrite.sml: diff. is_multUnordered, eval__true";
neuper@42223
   476
walther@60262
   477
"----------- 2011 thms with axclasses ----------------------------------------------------------";
walther@60262
   478
"----------- 2011 thms with axclasses ----------------------------------------------------------";
walther@60262
   479
"----------- 2011 thms with axclasses ----------------------------------------------------------";
walther@60337
   480
val thm = @{thm div_by_1};
wneuper@59188
   481
val prop = Thm.prop_of thm;
walther@60230
   482
TermC.atomty prop;                                     (*Type 'a*)
walther@60230
   483
val t = TermC.str2term "(2*x)/1";                      (*Type real*)
neuper@42223
   484
neuper@42223
   485
val (thy, ro, er, inst) = 
wneuper@59592
   486
    (@{theory "Isac_Knowledge"}, tless_true, eval_rls, [(@{term "bdv::real"}, @{term "x::real"})]);
walther@60230
   487
val SOME (t, _) = rewrite_ thy ro er true thm t; (*real TermC.matches 'a ?via ring? etc*)
neuper@42223
   488
walther@59841
   489
"----------- fun rewrite_set_ .. RatEq_eliminate step into to fun chk --------------------------";
walther@59841
   490
"----------- fun rewrite_set_ .. RatEq_eliminate step into to fun chk --------------------------";
walther@59841
   491
"----------- fun rewrite_set_ .. RatEq_eliminate step into to fun chk --------------------------";
walther@59841
   492
val thy = @{theory RatEq};
walther@59841
   493
val ctxt = Proof_Context.init_global thy;
walther@60242
   494
val SOME t = parseNEW ctxt "(3 + -1 * x + x \<up> 2) / (9 * x + -6 * x \<up> 2 + x \<up> 3) = 1 / x";
walther@59841
   495
val rls = assoc_rls "RatEq_eliminate"
neuper@42394
   496
walther@59841
   497
val SOME (t''''', asm''''') =
walther@59841
   498
           rewrite_set_ thy true rls t;
walther@59841
   499
"~~~~~ fun rewrite_set_ , args:"; val (thy, bool, rls, term) = (thy, true, rls, t);
walther@59841
   500
           rewrite__set_ thy 1 bool [] rls term;
walther@59841
   501
"~~~~~ and rewrite__set_ , args:"; val (thy, i, put_asm, bdv, rls, ct)
walther@59841
   502
  = (thy, 1, bool, []:(term * term) list, rls, term);
walther@59841
   503
walther@59841
   504
(*/------- outcomment this code: otherwise the re-definition could infect tests lateron ------\ * )
walther@59920
   505
      datatype switch = Applicable.Yes | Noap;
walther@59841
   506
      fun rew_once _ asm ct Noap [] = (ct, asm) (* ?TODO unify with Prog_Expr.rew_once? *)
walther@59920
   507
        | rew_once ruls asm ct Applicable.Yes [] = 
walther@59851
   508
          (case rls of Rule_Set.Repeat _ => rew_once ruls asm ct Noap ruls
walther@59878
   509
          | Rule_Set.Sequence _ => (ct, asm)
walther@59867
   510
          | rls => raise ERROR ("rew_once not appl. to \"" ^ Rule_Set.id rls ^ "\""))
walther@59841
   511
        | rew_once ruls asm ct apno (rul :: thms) =
walther@59841
   512
          case rul of
walther@59841
   513
            Rule.Thm (thmid, thm) =>
walther@59841
   514
              (trace1 i (" try thm: " ^ thmid);
walther@59852
   515
              case rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule.Rule_Set.rep) rls)
walther@59852
   516
                  ((#erls o Rule.Rule_Set.rep) rls) put_asm thm ct of
walther@59841
   517
                NONE => rew_once ruls asm ct apno thms
walther@59841
   518
              | SOME (ct', asm') => 
walther@59870
   519
                (trace1 i (" rewrites to: " ^ UnparseC.term_in_thy thy ct');
walther@59920
   520
                rew_once ruls (union (op =) asm asm') ct' Applicable.Yes (rul :: thms)))
walther@59841
   521
                (* once again try the same rule, e.g. associativity against "()"*)
walther@59878
   522
          | Rule.Eval (cc as (op_, _)) => 
walther@59841
   523
            let val _= trace1 i (" try calc: " ^ op_ ^ "'")
walther@59841
   524
              val ct = TermC.uminus_to_string ct (*WN190312: superfluous?*)
walther@59878
   525
            in case Eval.adhoc_thm thy cc ct of
walther@59841
   526
                NONE => rew_once ruls asm ct apno thms
walther@59841
   527
              | SOME (_, thm') => 
walther@59841
   528
                let 
walther@59852
   529
                  val pairopt = rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule.Rule_Set.rep) rls)
walther@59852
   530
                    ((#erls o Rule.Rule_Set.rep) rls) put_asm thm' ct;
walther@59841
   531
                  val _ = if pairopt <> NONE then () else error ("rewrite_set_, rewrite_ \"" ^ 
walther@59875
   532
                    ThmC.string_of_thm thm' ^ "\" " ^ UnparseC.term_in_thy thy ct ^ " = NONE")
walther@59870
   533
                  val _ = trace1 i (" calc. to: " ^ UnparseC.term_in_thy thy ((fst o the) pairopt))
walther@59920
   534
                in rew_once ruls asm ((fst o the) pairopt) Applicable.Yes (rul :: thms) end
walther@59841
   535
            end
walther@59841
   536
          | Rule.Cal1 (cc as (op_, _)) => 
walther@59841
   537
            let val _= trace1 i (" try cal1: " ^ op_ ^ "'");
walther@59841
   538
              val ct = TermC.uminus_to_string ct
walther@59878
   539
            in case Eval.adhoc_thm1_ thy cc ct of
walther@59841
   540
                NONE => (ct, asm)
walther@59841
   541
              | SOME (_, thm') =>
walther@59841
   542
                let 
walther@59852
   543
                  val pairopt = rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule.Rule_Set.rep) rls)
walther@59852
   544
                    ((#erls o Rule.Rule_Set.rep) rls) put_asm thm' ct;
walther@59841
   545
                  val _ = if pairopt <> NONE then () else error ("rewrite_set_, rewrite_ \"" ^
walther@59875
   546
                     ThmC.string_of_thm thm' ^ "\" " ^ UnparseC.term_in_thy thy ct ^ " = NONE")
walther@59870
   547
                  val _ = trace1 i (" cal1. to: " ^ UnparseC.term_in_thy thy ((fst o the) pairopt))
walther@59841
   548
                in the pairopt end
walther@59841
   549
            end
walther@59841
   550
          | Rule.Rls_ rls' => 
walther@59841
   551
            (case rewrite__set_ thy (i + 1) put_asm bdv rls' ct of
walther@59920
   552
              SOME (t', asm') => rew_once ruls (union (op =) asm asm') t' Applicable.Yes thms
walther@59841
   553
            | NONE => rew_once ruls asm ct apno thms)
walther@59867
   554
          | r => raise ERROR ("rew_once not appl. to \"" ^ Rule.Rule.to_string r ^ "\"");
walther@59852
   555
      val ruls = (#rules o Rule.Rule_Set.rep) rls;
walther@59870
   556
(*    val _ = trace i (" rls: " ^ Rule_Set.id rls ^ " on: " ^ UnparseC.term_in_thy thy ct)*)
walther@59841
   557
      val (ct', asm') = rew_once ruls [] ct Noap ruls;
walther@59841
   558
"~~~~~ fun rew_once , args:"; val (ruls, asm, ct, apno, (rul :: thms))
walther@59841
   559
  = (ruls, []:term list, ct, Noap, ruls);
walther@59841
   560
           val Rule.Thm (thmid, thm) = (*case*) rul (*of*);
walther@59841
   561
walther@59841
   562
    val SOME (ct', asm') = (*case*)
walther@59852
   563
           rewrite__ thy (i + 1) bdv ((snd o #rew_ord o Rule.Rule_Set.rep) rls)
walther@59852
   564
                  ((#erls o Rule.Rule_Set.rep) rls) put_asm thm ct (*of*);
walther@59841
   565
"~~~~~ fun rewrite__ , args:"; val (thy, i, bdv, tless, rls, put_asm, thm, ct)
walther@59852
   566
  = (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
walther@59841
   569
    val (t', asms, _ (*lrd*), rew) =
walther@59841
   570
           rew_sub thy i bdv tless rls put_asm ([(*root of the term*)]: TermC.path)
wenzelm@60203
   571
		  (((TermC.inst_bdv bdv) o Eval.norm o Thm.prop_of) thm) ct;
walther@59841
   572
"~~~~~ and rew_sub , args:"; val (thy, i, bdv, tless, rls, put_asm, lrd, r, t)
walther@59841
   573
  = (thy, i, bdv, tless, rls, put_asm, ([(*root of the term*)]: TermC.path),
wenzelm@60203
   574
		  (((TermC.inst_bdv bdv) o Eval.norm o Thm.prop_of) thm), ct);
walther@59841
   575
    val (lhs, rhs) = (HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r
walther@59841
   576
    val r' = Envir.subst_term (Pattern.match thy (lhs, t) (Vartab.empty, Vartab.empty)) r
walther@59841
   577
;
walther@59868
   578
(*+*)if UnparseC.term r' =
walther@60242
   579
(*+*)  "\<lbrakk>9 * x + -6 * x \<up> 2 + x \<up> 3 \<noteq> 0; x \<noteq> 0\<rbrakk>\n" ^
walther@60242
   580
(*+*)  "\<Longrightarrow> ((3 + -1 * x + x \<up> 2) /\n" ^
walther@60242
   581
(*+*)  "                   (9 * x + -6 * x \<up> 2 + x \<up> 3) =\n" ^
walther@59841
   582
(*+*)  "                   1 / x) =\n" ^
walther@60242
   583
(*+*)  "                  ((3 + -1 * x + x \<up> 2) * x =\n" ^
walther@60242
   584
(*+*)  "                   1 * (9 * x + -6 * x \<up> 2 + x \<up> 3))"
walther@59841
   585
(*+*)then () else error "instantiated rule CHANGED";
walther@59841
   586
walther@59841
   587
    val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) (Logic.count_prems r', [], r'))
walther@59841
   588
;
walther@60242
   589
(*+*)if map UnparseC.term p' = ["x \<noteq> 0", "9 * x + -6 * x \<up> 2 + x \<up> 3 \<noteq> 0"]
walther@59841
   590
(*+*)then () else error "stored assumptions CHANGED";
walther@59841
   591
walther@59841
   592
    val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r'
walther@59841
   593
;
walther@60242
   594
(*+*)if UnparseC.term t' = "(3 + -1 * x + x \<up> 2) * x = 1 * (9 * x + -6 * x \<up> 2 + x \<up> 3)"
walther@59841
   595
(*+*)then () else error "rewritten term (an equality) CHANGED";
walther@59841
   596
walther@59841
   597
        val (simpl_p', nofalse) =
walther@59841
   598
           eval__true thy (i + 1) p' bdv rls;
walther@59841
   599
"~~~~~ and eval__true , args:"; val (thy, i, asms, bdv, rls) = (thy, (i + 1), p', bdv, rls);
walther@59841
   600
  (*if*) asms = [@{term True}] orelse asms = [] (*else*); 
walther@59841
   601
walther@60242
   602
(*+*)if map UnparseC.term asms = ["x \<noteq> 0", "9 * x + -6 * x \<up> 2 + x \<up> 3 \<noteq> 0"]
walther@59841
   603
(*+*)then () else error "asms before chk CHANGED";
walther@59841
   604
walther@59841
   605
        fun chk indets [] = (indets, true) (*return asms<>True until false*)
walther@59841
   606
          | chk indets (a :: asms) =
walther@59841
   607
            (case rewrite__set_ thy (i + 1) false bdv rls a of
walther@59841
   608
              NONE => (chk (indets @ [a]) asms)
walther@59841
   609
            | SOME (t, a') =>
walther@59841
   610
              if t = @{term True} then (chk (indets @ a') asms) 
walther@59841
   611
              else if t = @{term False} then ([], false)
walther@60242
   612
            (*asm false .. thm not applied  \<up> ; continue until False vvv*)
walther@59841
   613
            else chk (indets @ [t] @ a') asms);
walther@59841
   614
walther@59841
   615
    val (xxx, true) =
walther@59841
   616
           chk [] asms;  (*return from eval__true*);
walther@59841
   617
"~~~~~ fun chk , args:"; val (indets, (a :: asms)) = ([], asms);
walther@59841
   618
walther@59851
   619
(*+*)val Rule_Set.Repeat {id = "rateq_erls", rules, ...} = rls;
walther@59841
   620
(*+*)(*val rules =
wenzelm@60309
   621
(*+*)   [Eval (\<^const_name>\<open>divide\<close>, fn),
walther@59841
   622
(*+*)    Thm ("minus_divide_left", "- ?a1 / ?b1 = - (?a1 / ?b1)"),
walther@59841
   623
(*+*)    :
wenzelm@60309
   624
(*+*)    Eval (\<^const_name>\<open>HOL.eq\<close>, fn),
walther@59841
   625
(*+*)    Thm ("not_true", "(\<not> True) = False"),
walther@59841
   626
(*+*)    Thm ("not_false", "(\<not> False) = True"),
walther@59841
   627
(*+*)    :
wenzelm@60309
   628
(*+*)    Eval (\<^const_name>\<open>powr\<close>, fn),
walther@60278
   629
(*+*)    Eval ("RatEq.is_ratequation_in", fn)]:
walther@59841
   630
(*+*)   rule list*)
walther@59841
   631
(*+*)chk: term list -> term list -> term list * bool
walther@59841
   632
walther@59841
   633
           rewrite__set_ thy (i + 1) false bdv rls a (*of*);
walther@59841
   634
walther@60330
   635
(*+*)Rewrite.trace_on := false; (*true false*)
walther@59841
   636
walther@59841
   637
        (*this was False; vvvv--- means: indeterminate*)
walther@59841
   638
    val (* SOME (t, a') *)NONE = (*case*)
walther@59841
   639
           rewrite__set_ thy (i + 1) false bdv rls a (*of*);
walther@59841
   640
walther@59868
   641
(*+*)UnparseC.term a = "x \<noteq> 0"; (* rewrite__set_ \<rightarrow> @{term True} ----------------- SHOULD BE indet !*)
neuper@42394
   642
(*
neuper@42394
   643
 :
walther@59841
   644
 ####  rls: rateq_erls on: x \<noteq> 0
neuper@42394
   645
 :
neuper@42394
   646
 #####  try calc: HOL.eq'    <<<------------------------------- here the error comes from
walther@59841
   647
 =====  calc. to: ~ False    <<<------------------------------- \<not> x = 0 is NOT False
neuper@42394
   648
 #####  try calc: HOL.eq' 
neuper@42394
   649
 #####  try thm: not_true 
neuper@42394
   650
 #####  try thm: not_false 
walther@59841
   651
 =====  rewrites to: True    <<<------------------------------- so x \<noteq> 0 is NOT True
walther@59841
   652
                                                       and True, False are NOT stored ...
walther@59841
   653
 :                             
walther@59841
   654
 ###  asms accepted: [x \<noteq> 0]   stored: []
walther@59841
   655
 : *)
walther@60330
   656
Rewrite.trace_on := false; (*true false*)
walther@59841
   657
( *\------- outcomment this code: otherwise the re-definition could infect tests lateron ------/*)
walther@59841
   658
neuper@42394
   659
wneuper@59252
   660
"----------- fun rewrite_ down to Pattern.match ------------------------------------------------";
wneuper@59252
   661
"----------- fun rewrite_ down to Pattern.match ------------------------------------------------";
wneuper@59252
   662
"----------- fun rewrite_ down to Pattern.match ------------------------------------------------";
wneuper@59252
   663
"~~~~~ fun rewrite_ , args:"; val (thy, rew_ord, erls, bool, thm, term) =
walther@59852
   664
  (@{theory}, dummy_ord, Rule_Set.empty, false, @{thm distrib_left}, @{term "x * (y + z) :: int"});
wneuper@59252
   665
"~~~~~ fun rewrite__ , args:"; val (thy, i, bdv, tless, rls, put_asm, thm, ct) =
wneuper@59252
   666
  (thy, 1, [], rew_ord, erls, bool, thm, term);
wneuper@59252
   667
"~~~~~ and rew_sub , args:"; val (thy, i, bdv, tless, rls, put_asm, lrd, r, t) =
walther@60230
   668
  (thy, i, bdv, tless, rls, put_asm, [], TermC.inst_bdv bdv (Eval.norm (Thm.prop_of thm)), ct)
walther@60262
   669
     val (lhss, rhss) = (HOLogic.dest_eq o Logic.strip_imp_concl o HOLogic.dest_Trueprop) r
wneuper@59252
   670
     val r' = Envir.subst_term (Pattern.match thy (lhss, t) (Vartab.empty, Vartab.empty)) r
wneuper@59252
   671
     val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) (Logic.count_prems r', [], r'))
walther@60262
   672
     val t' = (snd o HOLogic.dest_eq o Logic.strip_imp_concl o HOLogic.dest_Trueprop) r'
wneuper@59252
   673
;
walther@59868
   674
if UnparseC.term lhss = "?a * (?b + ?c)" andalso UnparseC.term t = "x * (y + z)" then ()
wneuper@59252
   675
else error "ARGS FOR Pattern.match CHANGED";
wneuper@59252
   676
val match = Pattern.match thy (lhss, t) (Vartab.empty, Vartab.empty);
walther@59868
   677
if (Envir.subst_term match r |> UnparseC.term) = "x * (y + z) = x * y + x * z" then ()
wneuper@59252
   678
  else error "Pattern.match CHANGED";
wneuper@59381
   679
walther@60262
   680
"----------- fun rewrite_set_ .. norm_equation, ~~~~~ fun rewrite_set_ -------------------------";
walther@60262
   681
"----------- fun rewrite_set_ .. norm_equation, ~~~~~ fun rewrite_set_ -------------------------";
walther@60262
   682
"----------- fun rewrite_set_ .. norm_equation, ~~~~~ fun rewrite_set_ -------------------------";
wneuper@59411
   683
(* norm_equation is defined in Test.thy, other rls see Knowledg/**)
wneuper@59411
   684
val thy = @{theory};
wneuper@59411
   685
val rls = norm_equation;
walther@60230
   686
val term = TermC.str2term "x + 1 = 2";
wneuper@59411
   687
walther@60262
   688
(**)val SOME (t, asm) = rewrite_set_ thy false rls term;
walther@60262
   689
(** )#####  try thm: "root_ge0" 
walther@60262
   690
exception TERM raised (line 271 of "~~/src/HOL/Tools/hologic.ML"):
walther@60262
   691
  dest_eq
walther@60262
   692
  0 \<le> ?a \<Longrightarrow> (0 \<le> sqrt ?a) = True( **)
walther@60318
   693
if UnparseC.term t = "x + 1 + - 1 * 2 = 0" then () else error "rewrite_set_ norm_equation CHANGED";
wneuper@59411
   694
walther@60262
   695
"~~~~~ fun rewrite_set_ , args:"; val (thy, bool, rls, term) = (thy, false, rls, term);
walther@60262
   696
"~~~~~ and rewrite__set_ , args:"; val (thy, i, put_asm, bdv, rls, ct) =
walther@60262
   697
  (thy, 1, bool, []: (term * term) list, rls, term);
walther@60262
   698
(*deleted after error detection*)
walther@59841
   699