test/Tools/isac/BaseDefinitions/rewrite-order.sml
author wneuper <walther.neuper@jku.at>
Thu, 15 Jul 2021 14:10:18 +0200
changeset 60324 5c7128feb370
parent 59866 3b194392ea71
child 60330 e5e9a6c45597
permissions -rw-r--r--
ewrite.sml + poly.sml + rational.sml: ok, repair rewrite-orders
walther@59866
     1
(* Title:  "BaseDefinitions/rewrite-order.sml"
walther@59860
     2
   Author: Walther Neuper
walther@59860
     3
   (c) due to copyright terms
walther@59860
     4
*)
walther@59860
     5
walther@59860
     6
"-----------------------------------------------------------------------------------------------";
walther@59860
     7
"table of contents -----------------------------------------------------------------------------";
walther@59860
     8
"-----------------------------------------------------------------------------------------------";
walther@60324
     9
"-------- identify difference in term-order between isa=NEW, isa2+OLD --------------------------";
walther@60324
    10
"-------- check simple terms -------------------------------------------------------------------";
walther@60324
    11
"-------- check all 'not >' in trace of Test_simplify on: x + 1 + - 1 * 2 = 0 ------------------";
walther@59860
    12
"-----------------------------------------------------------------------------------------------";
walther@59860
    13
"-----------------------------------------------------------------------------------------------";
walther@59860
    14
"-----------------------------------------------------------------------------------------------";
walther@59860
    15
walther@59860
    16
walther@60324
    17
"-------- identify difference in term-order between isa=NEW, isa2+OLD --------------------------";
walther@60324
    18
"-------- identify difference in term-order between isa=NEW, isa2+OLD --------------------------";
walther@60324
    19
"-------- identify difference in term-order between isa=NEW, isa2+OLD --------------------------";
walther@60324
    20
val form = TermC.str2term "x + -2 ::real"
walther@60324
    21
val Repeat {rew_ord = ("sqrt_right", rew_ord_), erls, ...} = Test_simplify;
walther@60324
    22
Rewrite.trace_on := true;
walther@60324
    23
(** )val NONE =                                         ( *isa*)
walther@60324
    24
(**)val SOME (form', _) =                             (*isa2*)
walther@60324
    25
      rewrite_ @{theory Test} rew_ord_ erls true @{thm radd_commute} form;
walther@60324
    26
(*
walther@60324
    27
-------------------- code in rew_sub -------------------------------------------------------
walther@60324
    28
((*TOODOO*)
walther@60324
    29
@{print}{a = "@@@rew_sub.ord.rew: ", perm = TermC.perm lhs rhs, tless = not (tless bdv (t', t))};
walther@60324
    30
    if TermC.perm lhs rhs andalso not (tless bdv (t', t))                     (*ordered rewriting*)
walther@60324
    31
    then (trace_eq2 i "not >" thy t t'; raise NO_REWRITE)
walther@60324
    32
    else (t'', p'', [], true)
walther@60324
    33
)(*TOODOO*)
walther@60324
    34
-------------------- output with Rewrite.trace_on := true; ---------------------------------
walther@60324
    35
##  eval asms: "x + -2 = -2 + x" 
walther@60324
    36
{a = "@@@rew_sub.ord.rew: ", b = true, c = false}
walther@60324
    37
*)
walther@60324
    38
"~~~~~ fun rewrite_ , args:"; val (thy, rew_ord, erls, bool, thm, term) =
walther@60324
    39
  (@{theory Test}, rew_ord_, erls, true, @{thm radd_commute}, form);
walther@60324
    40
walther@60324
    41
"~~~~~ fun rewrite__ , args:"; val (thy, i, bdv, tless, rls, put_asm, thm, ct) =
walther@60324
    42
  (thy, 1, []: (term * term) list, rew_ord, erls, bool, thm, term);
walther@60324
    43
walther@60324
    44
    val (t', asms, _(*lrd*), rew) =
walther@60324
    45
           rew_sub thy i bdv tless rls put_asm ([(*root of the term*)]: TermC.path)
walther@60324
    46
		         (TermC.inst_bdv bdv (Eval.norm (Thm.prop_of thm))) ct;
walther@60324
    47
"~~~~~ fun rew_sub , args:"; val (thy, i, bdv, tless, rls, put_asm, lrd, r, t) =
walther@60324
    48
(thy, i, bdv, tless, rls, put_asm, ([(*root of the term*)]: TermC.path),
walther@60324
    49
		         (TermC.inst_bdv bdv (Eval.norm (Thm.prop_of thm))), ct);
walther@60324
    50
    val (lhs, rhs) = (HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r
walther@60324
    51
    val r' = (Envir.subst_term (Pattern.match thy (lhs, t) (Vartab.empty, Vartab.empty)) r)
walther@60324
    52
      handle Pattern.MATCH => raise NO_REWRITE
walther@60324
    53
    val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) (Logic.count_prems r', [], r'))
walther@60324
    54
    val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl) r'
walther@60324
    55
    val _ = trace_in2 i "eval asms" thy r';
walther@60324
    56
    val (t'', p'') =                                                      (*conditional rewriting*)
walther@60324
    57
      let val (simpl_p', nofalse) = Rewrite.eval__true thy (i + 1) p' bdv rls 	     
walther@60324
    58
	    in
walther@60324
    59
	      if nofalse
walther@60324
    60
        then (trace_in4 i "asms accepted" thy p' simpl_p'; (t',simpl_p'))(* uncond.rew.from above*)
walther@60324
    61
        else (trace_in5 i "asms false" thy p'; raise NO_REWRITE)   (* don't go into subtm.of cond*)
walther@60324
    62
	    end;
walther@60324
    63
    (*if*) TermC.perm lhs rhs andalso not (tless bdv (t', t));                     (*ordered rewriting*)
walther@60324
    64
                                      not (tless bdv (t', t)); (*isa = true , isa2 = false*)
walther@60324
    65
                                          (tless bdv (t', t)); (*isa = false, isa2 = true*)
walther@60324
    66
(*WHAT IS ..:
walther@60324
    67
tless <-- Repeat {rew_ord = ("sqrt_right", rew_ord_), erls, ...} = Test_simplify; ..in Test_Some.thy
walther@60324
    68
rew_ord_ <-- sqrt_right false @{theory "Pure"}                                    ..in Test.thy
walther@60324
    69
sqrt_right                                                                        ..in Root.thy
walther@60324
    70
CHECK THIS ...(same error as with ^^^^^^^^^tless
walther@60324
    71
CHECK THIS ...(same error as with code copied into Test_Some.thy
walther@60324
    72
*)
walther@60324
    73
if        (sqrt_right false @{theory "Pure"} bdv (t', t)) (*isa = false, isa2 = true*)
walther@60324
    74
(** )then error "sqrt_right: special case NOT ok" else ();     ( *isa*)
walther@60324
    75
(**)then () else error "sqrt_right: special case OK";                      (*isa2*)
walther@60324
    76
"~~~~~ fun sqrt_right , args:"; val ((pr:bool), thy, (_: subst), tu) =
walther@60324
    77
  (false, @{theory "Pure"}, bdv, (t', t));
walther@60324
    78
walther@60324
    79
"~~~~~ fun term_ord' , args:"; val (pr, _(*thy*), (t, u)) = (pr, thy(***), tu);
walther@60324
    80
(*//----------------------------- open local ------------------------------------------------\\* )
walther@60324
    81
val EQUAL =
walther@60324
    82
    (*case*) int_ord (size_of_term' t, size_of_term' u) (*of*);
walther@60324
    83
      val (f, ts) = strip_comb t and (g, us) = strip_comb u
walther@60324
    84
val EQUAL =
walther@60324
    85
        (*case*) hd_ord (f, g) (*of*);
walther@60324
    86
walther@60324
    87
(**)val GREATER =                                             (*isa*)
walther@60324
    88
(** )val LESS =                                               ( *isa2*)
walther@60324
    89
           terms_ord str pr (ts, us)
walther@60324
    90
"~~~~~ and terms_ord , args:"; val (_(*str*), pr, (ts, us)) = (str, pr, (ts, us));
walther@60324
    91
walther@60324
    92
           list_ord (term_ord' pr (ThyC.get_theory "Isac_Knowledge")) (ts, us);
walther@60324
    93
(*+*)list_ord: ('a * 'b -> order) -> 'a list * 'b list -> order;
walther@60324
    94
(*+*)term_ord' pr (ThyC.get_theory "Isac_Knowledge"): term * term -> order;
walther@60324
    95
"~~~~~ fun list_ord , args:"; val (elem_ord, (xs, ys)) =
walther@60324
    96
  ((term_ord' pr (ThyC.get_theory "Isac_Knowledge")), (ts, us));
walther@60324
    97
(*+*)UnparseC.terms xs = "[\"- 2\", \"x\"]";                  (*isa == ?*)
walther@60324
    98
(*+*)UnparseC.terms ys = "[\"x\", \"- 2\"]";                  (*isa == ?*)
walther@60324
    99
val EQUAL =
walther@60324
   100
      (*case*) int_ord (length xs, length ys) (*of*);
walther@60324
   101
walther@60324
   102
val GREATER =
walther@60324
   103
           dict_ord elem_ord (xs, ys);
walther@60324
   104
"~~~~~ fun dict_ord , args:"; val ( elem_ord, (x :: xs, y :: ys)) = (elem_ord, (xs, ys))
walther@60324
   105
val GREATER =
walther@60324
   106
    (*case*) elem_ord (x, y) (*of*);
walther@60324
   107
val return = GREATER;
walther@60324
   108
( *\\----------------------------- open local ------------------------------------------------//*)
walther@60324
   109
walther@60324
   110
walther@60324
   111
"-------- check simple terms -------------------------------------------------------------------";
walther@60324
   112
"-------- check simple terms -------------------------------------------------------------------";
walther@60324
   113
"-------- check simple terms -------------------------------------------------------------------";
walther@60324
   114
val rew_ord_ = sqrt_right false @{theory "Pure"};
walther@60324
   115
walther@60324
   116
if rew_ord_ [] (@{term "1::real"}, @{term "2::real"}) =    true then () else error "";
walther@60324
   117
if rew_ord_ [] (@{term "3::real"}, @{term "2::real"}) =   false then () else error "";
walther@60324
   118
walther@60324
   119
if rew_ord_ [] (@{term "-1::real"}, @{term "-2::real"}) =  true then () else error "";
walther@60324
   120
if rew_ord_ [] (@{term "-3::real"}, @{term "-2::real"}) = false then () else error "";
walther@60324
   121
walther@60324
   122
if rew_ord_ [] (@{term "1::real"}, @{term "-1::real"}) =  false then () else error "";
walther@60324
   123
if rew_ord_ [] (@{term "-1::real"}, @{term "1::real"}) =   true then () else error "";
walther@60324
   124
walther@60324
   125
if rew_ord_ [] (@{term "x::real"}, @{term "2::real"}) =   false then () else error "";
walther@60324
   126
if rew_ord_ [] (@{term "3::real"}, @{term "x::real"}) =    true then () else error "";
walther@60324
   127
walther@60324
   128
walther@60324
   129
"-------- check all 'not >' in trace of Test_simplify on: x + 1 + - 1 * 2 = 0 ------------------";
walther@60324
   130
"-------- check all 'not >' in trace of Test_simplify on: x + 1 + - 1 * 2 = 0 ------------------";
walther@60324
   131
"-------- check all 'not >' in trace of Test_simplify on: x + 1 + - 1 * 2 = 0 ------------------";
walther@60324
   132
(* the rewrite-order for rewriting single theorems in a Rule_Set is taken from 
walther@60324
   133
*)
walther@60324
   134
val Repeat {rew_ord = ("sqrt_right", rew_ord_), erls, ...} = Test_simplify;
walther@60324
   135
(*#  rls: Test_simplify on: x + 1 + - 1 * 2 = 0   ..selected "not >:" from trace:*)
walther@60324
   136
walther@60324
   137
(*##  not >:           "x + 1 + - 1 * 2" >              "x + 1 + - 1 * 2" *)
walther@60324
   138
if rew_ord_ [] (@{term "x + 1 + - 1 * 2::real"}, @{term "x + 1 + - 1 * 2::real"}) = false
walther@60324
   139
then () else error "term_ord' 1";
walther@60324
   140
walther@60324
   141
(*##  not >:           "1 + x + - 1 * 2" >              "- 1 * 2 + (1 + x)" *)
walther@60324
   142
if rew_ord_ [] (@{term "1 + x + - 1 * 2::real"}, @{term "- 1 * 2 + (1 + x)::real"}) = true
walther@60324
   143
then () else error "term_ord' 2";
walther@60324
   144
walther@60324
   145
(*##  not >:           "1 + x" >              "x + 1" *)
walther@60324
   146
if rew_ord_ [] (@{term "1 + x::real"}, @{term "x + 1::real"}) = true
walther@60324
   147
then () else error "term_ord' 3";
walther@60324
   148
walther@60324
   149
(*##  not >:           "- 1 * 2" >              "2 * - 1" *)
walther@60324
   150
if rew_ord_ [] (@{term "- 1 * 2::real"}, @{term "2 * - 1::real"}) = true
walther@60324
   151
then () else error "term_ord' 4";
walther@60324
   152
walther@60324
   153
(*##  not >:           "1 + (x + - 2)" >              "x + - 2 + 1" *)
walther@60324
   154
if rew_ord_ [] (@{term "1 + (x + - 2)::real"}, @{term "x + - 2 + 1::real"}) = true
walther@60324
   155
then () else error "term_ord' 5";
walther@60324
   156
walther@60324
   157
(*##  not >:           "x + - 2" >              "- 2 + x"*)
walther@60324
   158
if rew_ord_ [] (@{term "x + - 2::real"}, @{term "- 2 + x  ::real"}) = false
walther@60324
   159
then () else error "term_ord' 5a";  (*<<<<<<<<<<<<<<----------------- false in trace isa2*)
walther@60324
   160
walther@60324
   161
(*--------------------------------- ordered rew. (NOT) inhibited*)
walther@60324
   162
walther@60324
   163
(*##  not >:           "1 + (-2 + x)" >              "-2 + x + 1" *)
walther@60324
   164
if rew_ord_ [] (@{term "1 + (-2 + x)::real"}, @{term "-2 + x + 1::real"}) = true
walther@60324
   165
then () else error "term_ord' 6";
walther@60324
   166
walther@60324
   167
(*##  not >:           "- 2 + x" >              "x + - 2" *)
walther@60324
   168
if rew_ord_ [] (@{term "- 2 + x::real"}, @{term "x + - 2::real"}) = true
walther@60324
   169
then () else error "term_ord' 7";
walther@60324
   170
walther@60324
   171
(*##  not >:           "- 2 + (1 + x)" >              "1 + (- 2 + x)" *)
walther@60324
   172
if rew_ord_ [] (@{term "- 2 + (1 + x)::real"}, @{term "1 + (- 2 + x)::real"}) = true
walther@60324
   173
then () else error "term_ord' 8";
walther@60324
   174
walther@60324
   175
(*##  not >            "- 1 + x" >              "x + - 1" *)
walther@60324
   176
if rew_ord_ [] (@{term "- 1 + x::real"}, @{term "x + - 1::real"}) = true
walther@60324
   177
then () else error "term_ord' 9";