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