src/Pure/isac/smltest/Scripts/rewrite.sml
author Walther Neuper <neuper@ist.tugraz.at>
Wed, 21 Jul 2010 13:53:39 +0200
branchisac-from-Isabelle2009-2
changeset 37871 875b6efa7ced
permissions -rw-r--r--
added isac-hook in Pure/thm and isac-code
neuper@37871
     1
(* tests for ME/rewrite.sml
neuper@37871
     2
   TODO.WN0509 collect typical tests from systest here !!!!!
neuper@37871
     3
   author: Walther Neuper 050908
neuper@37871
     4
   (c) copyright due to lincense terms.
neuper@37871
     5
neuper@37871
     6
use"../smltest/Scripts/rewrite.sml";
neuper@37871
     7
use"rewrite.sml";
neuper@37871
     8
*)
neuper@37871
     9
neuper@37871
    10
"-----------------------------------------------------------------";
neuper@37871
    11
"table of contents -----------------------------------------------";
neuper@37871
    12
"-----------------------------------------------------------------";
neuper@37871
    13
"----------- assemble rewrite ------------------------------------";
neuper@37871
    14
"----------- test rewriting without Isac's thys ------------------";
neuper@37871
    15
"----------- conditional rewriting without Isac's thys -----------";
neuper@37871
    16
"----------- rewrite_terms_  -------------------------------------";
neuper@37871
    17
"----------- rewrite_inst_ bdvs ----------------------------------";
neuper@37871
    18
"-----------------------------------------------------------------";
neuper@37871
    19
"-----------------------------------------------------------------";
neuper@37871
    20
"-----------------------------------------------------------------";
neuper@37871
    21
neuper@37871
    22
"----------- assemble rewrite ------------------------------------";
neuper@37871
    23
"----------- assemble rewrite ------------------------------------";
neuper@37871
    24
"----------- assemble rewrite ------------------------------------";
neuper@37871
    25
(*ML {**)
neuper@37871
    26
"===== rewriting by thm with 'a";
neuper@37871
    27
show_types := true;
neuper@37871
    28
val thy = @{theory Complex_Main};
neuper@37871
    29
val ctxt = @{context};
neuper@37871
    30
val thm = @{thm add_commute};
neuper@37871
    31
val t = (term_of o the) (parse thy "((r + u) + t) + s");
neuper@37871
    32
"----- from old: fun rewrite__";
neuper@37871
    33
val bdv = [];
neuper@37871
    34
val r = (((inst_bdv bdv) o norm o #prop o rep_thm) thm);
neuper@37871
    35
"----- from old: and rew_sub";
neuper@37871
    36
val (lhs,rhs) = (dest_equals' o strip_trueprop 
neuper@37871
    37
   	      o Logic.strip_imp_concl) r;
neuper@37871
    38
(* old
neuper@37871
    39
val insts = Pattern.match thy (lhs,t) (Vartab.empty, Vartab.empty);*)
neuper@37871
    40
"----- fun match_rew in Pure/pattern.ML";
neuper@37871
    41
val rtm = the_default rhs (Term.rename_abs lhs t rhs);
neuper@37871
    42
neuper@37871
    43
tracing(Syntax.string_of_term ctxt rtm);
neuper@37871
    44
tracing(Syntax.string_of_term ctxt lhs);
neuper@37871
    45
tracing(Syntax.string_of_term ctxt t);
neuper@37871
    46
neuper@37871
    47
(Pattern.match thy (lhs, t) (Vartab.empty, Vartab.empty));
neuper@37871
    48
val (rew, rhs) = (Envir.subst_term 
neuper@37871
    49
  (Pattern.match thy (lhs, t) (Vartab.empty, Vartab.empty)) rtm, rtm);
neuper@37871
    50
(*lookup in isabelle?trace?response...*)
neuper@37871
    51
writeln(Syntax.string_of_term ctxt rew);
neuper@37871
    52
writeln(Syntax.string_of_term ctxt rhs);
neuper@37871
    53
(*}
neuper@37871
    54
ML {*)
neuper@37871
    55
"===== rewriting: prep insertion into rew_sub";
neuper@37871
    56
val thy = @{theory Complex_Main};
neuper@37871
    57
val ctxt = @{context};
neuper@37871
    58
val thm =  @{thm nonzero_mult_divide_cancel_right};
neuper@37871
    59
val r = Thm.prop_of thm;
neuper@37871
    60
val tm = @{term "x*2 / 2::real"};
neuper@37871
    61
"----- and rew_sub";
neuper@37871
    62
val (lhs, rhs) = (HOLogic.dest_eq o HOLogic.dest_Trueprop
neuper@37871
    63
                  o Logic.strip_imp_concl) r;
neuper@37871
    64
val r' = Envir.subst_term (Pattern.match thy (lhs, tm) 
neuper@37871
    65
                                (Vartab.empty, Vartab.empty)) r;
neuper@37871
    66
val p' = (fst o Logic.strip_prems) (Logic.count_prems r', [], r');
neuper@37871
    67
val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop 
neuper@37871
    68
            o Logic.strip_imp_concl) r';
neuper@37871
    69
neuper@37871
    70
(*is displayed on top of <response> buffer...*)
neuper@37871
    71
Pretty.writeln (ProofContext.pretty_term_abbrev @{context} r');
neuper@37871
    72
Pretty.writeln (ProofContext.pretty_term_abbrev @{context} t');
neuper@37871
    73
(*}*)
neuper@37871
    74
neuper@37871
    75
"----------- test rewriting without Isac's thys ------------------";
neuper@37871
    76
"----------- test rewriting without Isac's thys ------------------";
neuper@37871
    77
"----------- test rewriting without Isac's thys ------------------";
neuper@37871
    78
(*ML {*)
neuper@37871
    79
"===== rewriting with Isabelle2009-1 only, i.e without isac-hacks";
neuper@37871
    80
val thy = @{theory Complex_Main};
neuper@37871
    81
val ctxt = @{context};
neuper@37871
    82
val thm =  @{thm add_commute};
neuper@37871
    83
val tm = @{term "x + y*z::real"};
neuper@37871
    84
neuper@37871
    85
val SOME (r,_) = (rewrite_ thy dummy_ord e_rls false thm tm)
neuper@37871
    86
  handle _ => error "rewrite.sml diff.behav. in rewriting with Isabelle2009-1 only";
neuper@37871
    87
(*is displayed on _TOP_ of <response> buffer...*)
neuper@37871
    88
Pretty.writeln (ProofContext.pretty_term_abbrev @{context} r);
neuper@37871
    89
neuper@37871
    90
"----- rewriting a subterm";
neuper@37871
    91
val tm = @{term "w*(x + y*z)::real"};
neuper@37871
    92
neuper@37871
    93
val SOME (r,_) = (rewrite_ thy dummy_ord e_rls false thm tm)
neuper@37871
    94
  handle _ => error "rewrite.sml diff.behav. in rew_sub with Isabelle2009-1 only";
neuper@37871
    95
neuper@37871
    96
"----- ordered rewriting";
neuper@37871
    97
fun tord (_:subst) pp = TermOrd.termless pp;
neuper@37871
    98
if tord [] (@{term "x + y*z::real"}, @{term "y*z + x::real"}) then ()
neuper@37871
    99
else error "rewrite.sml diff.behav. in ord.rewr. with Isabelle2009-1 only";
neuper@37871
   100
neuper@37871
   101
val NONE = (rewrite_ thy tord e_rls false thm tm)
neuper@37871
   102
  handle _ => error "rewrite.sml diff.behav. in rewriting with Isabelle2009-1 only";
neuper@37871
   103
(*is displayed on _TOP_ of <response> buffer...*)
neuper@37871
   104
Pretty.writeln (ProofContext.pretty_term_abbrev @{context} r);
neuper@37871
   105
neuper@37871
   106
val tm = @{term "x*y + z::real"};
neuper@37871
   107
val SOME (r,_) = (rewrite_ thy tord e_rls false thm tm)
neuper@37871
   108
  handle _ => error "rewrite.sml diff.behav. in rewriting with Isabelle2009-1 only";
neuper@37871
   109
neuper@37871
   110
neuper@37871
   111
(*}*)
neuper@37871
   112
neuper@37871
   113
"----------- conditional rewriting without Isac's thys -----------";
neuper@37871
   114
"----------- conditional rewriting without Isac's thys -----------";
neuper@37871
   115
"----------- conditional rewriting without Isac's thys -----------";
neuper@37871
   116
(*ML {*)
neuper@37871
   117
"===== prepr cond.rew. with Pattern.match";
neuper@37871
   118
val thy = @{theory Complex_Main};
neuper@37871
   119
val ctxt = @{context};
neuper@37871
   120
val thm =  @{thm nonzero_mult_divide_cancel_right};
neuper@37871
   121
val rule = Thm.prop_of thm;
neuper@37871
   122
val tm = @{term "x*2 / 2::real"};
neuper@37871
   123
neuper@37871
   124
val prem = Logic.strip_imp_prems rule;
neuper@37871
   125
val nps = Logic.count_prems rule;
neuper@37871
   126
val prems = Logic.strip_prems (nps, [], rule);
neuper@37871
   127
neuper@37871
   128
val eq = Logic.strip_imp_concl rule;
neuper@37871
   129
val (lhs, rhs) = (HOLogic.dest_eq o HOLogic.dest_Trueprop) eq;
neuper@37871
   130
neuper@37871
   131
val mtcs = Pattern.match thy (lhs, tm) (Vartab.empty, Vartab.empty);
neuper@37871
   132
val rule' = Envir.subst_term mtcs rule;
neuper@37871
   133
neuper@37871
   134
val prems' = (fst o Logic.strip_prems) 
neuper@37871
   135
              (Logic.count_prems rule', [], rule');
neuper@37871
   136
val rhs' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop 
neuper@37871
   137
            o Logic.strip_imp_concl) rule';
neuper@37871
   138
neuper@37871
   139
"----- conditional rewriting creating an assumption";
neuper@37871
   140
"----- conditional rewriting creating an assumption";
neuper@37871
   141
val tm = @{term "x*y / y::real"};
neuper@37871
   142
val SOME (rew,asm) = (rewrite_ thy dummy_ord e_rls false thm tm)
neuper@37871
   143
  handle _ => error "rewrite.sml diff.behav. in cond.rew. with Isabelle2009-1 only a";
neuper@37871
   144
neuper@37871
   145
if rew = @{term "x::real"} then ()
neuper@37871
   146
else error "rewrite.sml diff.behav. in cond.rew. with Isabelle2009-1 only b";
neuper@37871
   147
neuper@37871
   148
if HOLogic.dest_Trueprop (hd asm) = @{term "~ y = (0::real)"} then ()
neuper@37871
   149
else error "rewrite.sml diff.behav. in cond.rew. with Isabelle2009-1 only c";
neuper@37871
   150
neuper@37871
   151
"----- conditional rewriting immediately: can only be done with Isabelle numerals\
neuper@37871
   152
\because erls cannot handle them yet.";
neuper@37871
   153
(*}*)
neuper@37871
   154
neuper@37871
   155
neuper@37871
   156
"----------- rewrite_terms_  -------------------------------------";
neuper@37871
   157
"----------- rewrite_terms_  -------------------------------------";
neuper@37871
   158
"----------- rewrite_terms_  -------------------------------------";
neuper@37871
   159
val subte = [str2term"x = 0"];
neuper@37871
   160
val t = str2term"M_b x = -1 * q_0 * x ^^^ 2 / 2 + x * c + c_2";
neuper@37871
   161
val Some (t',_) = rewrite_terms_ thy dummy_ord Erls subte t;
neuper@37871
   162
if term2str t' = "M_b 0 = -1 * q_0 * 0 ^^^ 2 / 2 + 0 * c + c_2" then ()
neuper@37871
   163
else raise error "rewrite.sml rewrite_terms_ [x = 0]";
neuper@37871
   164
neuper@37871
   165
val subte = [str2term"M_b 0 = 0"];
neuper@37871
   166
val t = str2term"M_b 0 = -1 * q_0 * 0 ^^^ 2 / 2 + 0 * c + c_2";
neuper@37871
   167
val Some (t',_) = rewrite_terms_ thy dummy_ord Erls subte t;
neuper@37871
   168
if term2str t' = "0 = -1 * q_0 * 0 ^^^ 2 / 2 + 0 * c + c_2" then ()
neuper@37871
   169
else raise error "rewrite.sml rewrite_terms_ [M_b 0 = 0]";
neuper@37871
   170
neuper@37871
   171
val subte = [str2term"x = 0", str2term"M_b 0 = 0"];
neuper@37871
   172
val t = str2term"M_b x = -1 * q_0 * x ^^^ 2 / 2 + x * c + c_2";
neuper@37871
   173
val Some (t',_) = rewrite_terms_ thy dummy_ord Erls subte t;
neuper@37871
   174
if term2str t' = "0 = -1 * q_0 * 0 ^^^ 2 / 2 + 0 * c + c_2" then ()
neuper@37871
   175
else raise error "rewrite.sml rewrite_terms_ [x = 0, M_b 0 = 0]";
neuper@37871
   176
neuper@37871
   177
neuper@37871
   178
"----------- rewrite_inst_ bdvs ----------------------------------";
neuper@37871
   179
"----------- rewrite_inst_ bdvs ----------------------------------";
neuper@37871
   180
"----------- rewrite_inst_ bdvs ----------------------------------";
neuper@37871
   181
(*see smltest/Scripts/term_G.sml: inst_bdv 2*)
neuper@37871
   182
val t = str2term"-1 * (q_0 * L ^^^ 2) / 2 + (L * c_3 + c_4) = 0";
neuper@37871
   183
val bdvs = [(str2term"bdv_1",str2term"c"),
neuper@37871
   184
	    (str2term"bdv_2",str2term"c_2"),
neuper@37871
   185
	    (str2term"bdv_3",str2term"c_3"),
neuper@37871
   186
	    (str2term"bdv_4",str2term"c_4")];
neuper@37871
   187
(*------------ outcommented WN071210, after inclusion into ROOT.ML 
neuper@37871
   188
val Some (t,_) = 
neuper@37871
   189
    rewrite_inst_ thy e_rew_ord 
neuper@37871
   190
		  (append_rls "erls_isolate_bdvs" e_rls 
neuper@37871
   191
			      [(Calc ("EqSystem.occur'_exactly'_in", 
neuper@37871
   192
				      eval_occur_exactly_in 
neuper@37871
   193
					  "#eval_occur_exactly_in_"))
neuper@37871
   194
			       ]) 
neuper@37871
   195
		  false bdvs (num_str separate_bdvs_add) t;
neuper@37871
   196
(writeln o term2str) t;
neuper@37871
   197
if term2str t = "L * c_3 + c_4 = 0 + -1 * (-1 * (q_0 * L ^^^ 2) / 2)"
neuper@37871
   198
then () else raise error "rewrite.sml rewrite_inst_ bdvs";
neuper@37871
   199
trace_rewrite:=true;
neuper@37871
   200
trace_rewrite:=false;--------------------------------------------*)
neuper@37871
   201