src/Tools/isac/Knowledge/Test.thy
author Walther Neuper <wneuper@ist.tugraz.at>
Sat, 22 Jun 2019 14:34:06 +0200
changeset 59551 6ea6d9c377a0
parent 59546 1ada701c4811
child 59552 ab7955d2ead3
permissions -rw-r--r--
funpack: remove code unnecessary after switch to partial_function, partially
wneuper@59430
     1
(* Knowledge for tests, specifically simplified or bound to a fixed state
wneuper@59430
     2
   for the purpose of simplifying tests.
neuper@37954
     3
   Author: Walther Neuper 2003
neuper@37954
     4
   (c) due to copyright terms
wneuper@59432
     5
wneuper@59432
     6
Notes on cleanup:
wneuper@59432
     7
(0) revert import Test -- DiophantEq, this raises issues related to (1..4)
wneuper@59432
     8
(1) transfer methods to respective theories, if only test, then hierarchy at ["...", "Test"]:
wneuper@59432
     9
    differentiate, root equatioh, polynomial equation, diophantine equation
wneuper@59432
    10
(2) transfer problems accordingly
wneuper@59432
    11
(3) rearrange rls according to (1..2)
wneuper@59432
    12
(4) rearrange axiomatizations according to (3)
neuper@37954
    13
*) 
neuper@37906
    14
wneuper@59424
    15
theory Test imports Base_Tools Poly Rational Root Diff begin
neuper@37906
    16
 
wneuper@59551
    17
section \<open>consts for problems\<close>
wneuper@59430
    18
consts
neuper@42008
    19
  "is'_root'_free"   :: "'a => bool"      ("is'_root'_free _" 10)
neuper@42008
    20
  "contains'_root"   :: "'a => bool"      ("contains'_root _" 10)
neuper@42008
    21
neuper@42008
    22
  "precond'_rootmet" :: "'a => bool"      ("precond'_rootmet _" 10)
neuper@42008
    23
  "precond'_rootpbl" :: "'a => bool"      ("precond'_rootpbl _" 10)
neuper@42008
    24
  "precond'_submet"  :: "'a => bool"      ("precond'_submet _" 10)
neuper@42008
    25
  "precond'_subpbl"  :: "'a => bool"      ("precond'_subpbl _" 10)
neuper@37906
    26
neuper@37906
    27
wneuper@59472
    28
section \<open>functions\<close>
wneuper@59472
    29
ML \<open>
wneuper@59430
    30
fun bin_o (Const (op_, (Type ("fun", [Type (s2, []), Type ("fun", [Type (s4, _),Type (s5, _)])]))))
wneuper@59430
    31
      = if s2 = s4 andalso s4 = s5 then [op_] else []
wneuper@59430
    32
    | bin_o _ = [];
neuper@37906
    33
wneuper@59430
    34
fun bin_op (t1 $ t2) = union op = (bin_op t1) (bin_op t2)
wneuper@59430
    35
  | bin_op t         =  bin_o t;
wneuper@59430
    36
fun is_bin_op t = (bin_op t <> []);
neuper@37906
    37
wneuper@59430
    38
fun bin_op_arg1 ((Const (_,
wneuper@59430
    39
    (Type ("fun", [Type (_, []), Type ("fun", [Type _, Type _])])))) $ arg1 $ _) = arg1
wneuper@59430
    40
  | bin_op_arg1 t = raise ERROR ("bin_op_arg1: t = " ^ Rule.term2str t);
wneuper@59430
    41
fun bin_op_arg2 ((Const (_,
wneuper@59430
    42
    (Type ("fun", [Type (_, []),Type ("fun", [Type _, Type _])]))))$ _ $ arg2) = arg2
wneuper@59430
    43
  | bin_op_arg2 t = raise ERROR ("bin_op_arg1: t = " ^ Rule.term2str t);
neuper@37906
    44
wneuper@59430
    45
exception NO_EQUATION_TERM;
wneuper@59430
    46
fun is_equation ((Const ("HOL.eq",
wneuper@59430
    47
    (Type ("fun", [Type (_, []), Type ("fun", [Type (_, []),Type ("bool",[])])])))) $ _ $ _) = true
wneuper@59430
    48
  | is_equation _ = false;
wneuper@59430
    49
fun equ_lhs ((Const ("HOL.eq",
wneuper@59430
    50
    (Type ("fun", [Type (_, []), Type ("fun", [Type (_, []),Type ("bool",[])])])))) $ l $ _) = l
wneuper@59430
    51
  | equ_lhs _ = raise NO_EQUATION_TERM;
wneuper@59430
    52
fun equ_rhs ((Const ("HOL.eq", (Type ("fun",
wneuper@59430
    53
		 [Type (_, []), Type ("fun", [Type (_, []), Type ("bool",[])])])))) $ _ $ r) = r
wneuper@59430
    54
  | equ_rhs _ = raise NO_EQUATION_TERM;
neuper@37906
    55
wneuper@59430
    56
fun atom (Const (_, Type (_,[]))) = true
wneuper@59430
    57
  | atom (Free (_, Type (_,[]))) = true
wneuper@59430
    58
  | atom (Var (_, Type (_,[]))) = true
wneuper@59430
    59
  | atom((Const ("Bin.integ_of_bin",_)) $ _) = true
wneuper@59430
    60
  | atom _ = false;
wneuper@59430
    61
wneuper@59430
    62
fun varids (Const (s, Type (_,[]))) = [strip_thy s]
wneuper@59430
    63
  | varids (Free (s, Type (_,[]))) = if TermC.is_num' s then [] else [strip_thy s]  
wneuper@59430
    64
  | varids (Var((s, _),Type (_,[]))) = [strip_thy s]
wneuper@59430
    65
(*| varids (_      (s,"?DUMMY"   )) =   ..ML-error *)
wneuper@59430
    66
  | varids((Const ("Bin.integ_of_bin",_)) $ _)= [](*8.01: superfluous?*)
wneuper@59430
    67
  | varids (Abs (a, _, t)) = union op = [a] (varids t)
wneuper@59430
    68
  | varids (t1 $ t2) = union op = (varids t1) (varids t2)
wneuper@59430
    69
  | varids _ = [];
wneuper@59430
    70
wneuper@59430
    71
fun bin_ops_only ((Const op_) $ t1 $ t2) =
wneuper@59430
    72
    if is_bin_op (Const op_) then bin_ops_only t1 andalso bin_ops_only t2 else false
wneuper@59430
    73
  | bin_ops_only t = if atom t then true else bin_ops_only t;
wneuper@59430
    74
wneuper@59430
    75
fun polynomial opl t _(* bdVar TODO *) =
wneuper@59430
    76
    subset op = (bin_op t, opl) andalso (bin_ops_only t);
wneuper@59430
    77
wneuper@59430
    78
fun poly_equ opl bdVar t = is_equation t (* bdVar TODO *) 
wneuper@59430
    79
    andalso polynomial opl (equ_lhs t) bdVar 
wneuper@59430
    80
    andalso polynomial opl (equ_rhs t) bdVar
wneuper@59430
    81
    andalso (subset op = (varids bdVar, varids (equ_lhs t)) orelse
wneuper@59430
    82
             subset op = (varids bdVar, varids (equ_lhs t)));
wneuper@59430
    83
wneuper@59430
    84
fun max (a,b) = if a < b then b else a;
wneuper@59430
    85
wneuper@59430
    86
fun degree addl mul bdVar t =
wneuper@59430
    87
let
wneuper@59430
    88
fun deg _ _ v (Const  (s, Type (_, []))) = if v=strip_thy s then 1 else 0
wneuper@59430
    89
  | deg _ _ v (Free   (s, Type (_, []))) = if v=strip_thy s then 1 else 0
wneuper@59430
    90
  | deg _ _ v (Var((s, _), Type (_, []))) = if v=strip_thy s then 1 else 0
wneuper@59430
    91
(*| deg _ _ v (_     (s,"?DUMMY"   ))          =   ..ML-error *) 
wneuper@59430
    92
  | deg _ _ _ ((Const ("Bin.integ_of_bin", _)) $ _ ) = 0 
wneuper@59430
    93
  | deg addl mul v (h $ t1 $ t2) =
wneuper@59430
    94
    if subset op = (bin_op h, addl)
wneuper@59430
    95
    then max (deg addl mul v t1  , deg addl mul v t2)
wneuper@59430
    96
    else (*mul!*)(deg addl mul v t1) + (deg addl mul v t2)
wneuper@59430
    97
  | deg _ _ _ t = raise ERROR ("deg: t = " ^ Rule.term2str t)
wneuper@59430
    98
in
wneuper@59430
    99
  if polynomial (addl @ [mul]) t bdVar
wneuper@59430
   100
  then SOME (deg addl mul (id_of bdVar) t)
wneuper@59430
   101
  else (NONE:int option)
wneuper@59430
   102
end;
wneuper@59430
   103
fun degree_ addl mul bdVar t = (* do not export *)
wneuper@59430
   104
let
wneuper@59430
   105
  fun opt (SOME i)= i
wneuper@59430
   106
	  | opt  NONE = 0
wneuper@59430
   107
in opt (degree addl mul bdVar t) end;
wneuper@59430
   108
wneuper@59430
   109
fun linear addl mul t bdVar = (degree_ addl mul bdVar t) < 2;
wneuper@59430
   110
wneuper@59430
   111
fun linear_equ addl mul bdVar t =
wneuper@59430
   112
  if is_equation t 
wneuper@59430
   113
  then
wneuper@59430
   114
    let
wneuper@59430
   115
      val degl = degree_ addl mul bdVar (equ_lhs t);
wneuper@59430
   116
	    val degr = degree_ addl mul bdVar (equ_rhs t)
wneuper@59430
   117
	  in if (degl>0 orelse degr>0)andalso max(degl,degr) < 2 then true else false end
wneuper@59430
   118
  else false;
wneuper@59430
   119
(* strip_thy op_  before *)
wneuper@59430
   120
fun is_div_op (dv, (Const (op_,
wneuper@59430
   121
    (Type ("fun", [Type (_, []), Type ("fun", [Type _, Type _])])))) ) = (dv = strip_thy op_)
wneuper@59430
   122
  | is_div_op _ = false;
wneuper@59430
   123
wneuper@59430
   124
fun is_denom bdVar div_op t =
wneuper@59430
   125
    let fun is bool[v]dv (Const  (s,Type(_,[])))= bool andalso(if v=strip_thy s then true else false)
wneuper@59430
   126
	  | is bool[v]dv (Free   (s,Type(_,[])))= bool andalso(if v=strip_thy s then true else false) 
wneuper@59430
   127
	  | is bool[v]dv (Var((s,_),Type(_,[])))= bool andalso(if v=strip_thy s then true else false)
wneuper@59430
   128
	  | is bool[v]dv((Const ("Bin.integ_of_bin",_)) $ _) = false
wneuper@59430
   129
	  | is bool[v]dv (h$n$d) = 
wneuper@59430
   130
	      if is_div_op(dv,h) 
wneuper@59430
   131
	      then (is false[v]dv n)orelse(is true[v]dv d)
wneuper@59430
   132
	      else (is bool [v]dv n)orelse(is bool[v]dv d)
wneuper@59430
   133
in is false (varids bdVar) (strip_thy div_op) t end;
wneuper@59430
   134
wneuper@59430
   135
wneuper@59430
   136
fun rational t div_op bdVar = 
wneuper@59430
   137
    is_denom bdVar div_op t andalso bin_ops_only t;
wneuper@59430
   138
wneuper@59472
   139
\<close>
wneuper@59430
   140
wneuper@59472
   141
section \<open>axiomatizations\<close>
neuper@52148
   142
axiomatization where (*TODO: prove as theorems*)
neuper@37906
   143
neuper@52148
   144
  radd_mult_distrib2:      "(k::real) * (m + n) = k * m + k * n" and
neuper@52148
   145
  rdistr_right_assoc:      "(k::real) + l * n + m * n = k + (l + m) * n" and
neuper@52148
   146
  rdistr_right_assoc_p:    "l * n + (m * n + (k::real)) = (l + m) * n + k" and
neuper@52148
   147
  rdistr_div_right:        "((k::real) + l) / n = k / n + l / n" and
neuper@37983
   148
  rcollect_right:
neuper@52148
   149
          "[| l is_const; m is_const |] ==> (l::real)*n + m*n = (l + m) * n" and
neuper@37983
   150
  rcollect_one_left:
neuper@52148
   151
          "m is_const ==> (n::real) + m * n = (1 + m) * n" and
neuper@37983
   152
  rcollect_one_left_assoc:
neuper@52148
   153
          "m is_const ==> (k::real) + n + m * n = k + (1 + m) * n" and
neuper@37983
   154
  rcollect_one_left_assoc_p:
neuper@52148
   155
          "m is_const ==> n + (m * n + (k::real)) = (1 + m) * n + k" and
neuper@37906
   156
neuper@52148
   157
  rtwo_of_the_same:        "a + a = 2 * a" and
neuper@52148
   158
  rtwo_of_the_same_assoc:  "(x + a) + a = x + 2 * a" and
neuper@52148
   159
  rtwo_of_the_same_assoc_p:"a + (a + x) = 2 * a + x" and
neuper@37906
   160
neuper@52148
   161
  rcancel_den:             "not(a=0) ==> a * (b / a) = b" and
neuper@52148
   162
  rcancel_const:           "[| a is_const; b is_const |] ==> a*(x/b) = a/b*x" and
neuper@52148
   163
  rshift_nominator:        "(a::real) * b / c = a / c * b" and
neuper@37906
   164
neuper@52148
   165
  exp_pow:                 "(a ^^^ b) ^^^ c = a ^^^ (b * c)" and
neuper@52148
   166
  rsqare:                  "(a::real) * a = a ^^^ 2" and
neuper@52148
   167
  power_1:                 "(a::real) ^^^ 1 = a" and
neuper@52148
   168
  rbinom_power_2:          "((a::real) + b)^^^ 2 = a^^^ 2 + 2*a*b + b^^^ 2" and
neuper@37906
   169
neuper@52148
   170
  rmult_1:                 "1 * k = (k::real)" and
neuper@52148
   171
  rmult_1_right:           "k * 1 = (k::real)" and
neuper@52148
   172
  rmult_0:                 "0 * k = (0::real)" and
neuper@52148
   173
  rmult_0_right:           "k * 0 = (0::real)" and
neuper@52148
   174
  radd_0:                  "0 + k = (k::real)" and
neuper@52148
   175
  radd_0_right:            "k + 0 = (k::real)" and
neuper@37906
   176
neuper@37983
   177
  radd_real_const_eq:
neuper@52148
   178
          "[| a is_const; c is_const; d is_const |] ==> a/d + c/d = (a+c)/(d::real)" and
neuper@37983
   179
  radd_real_const:
neuper@37906
   180
          "[| a is_const; b is_const; c is_const; d is_const |] ==> a/b + c/d = (a*d + b*c)/(b*(d::real))"  
neuper@52148
   181
   and
neuper@37906
   182
(*for AC-operators*)
neuper@52148
   183
  radd_commute:            "(m::real) + (n::real) = n + m" and
neuper@52148
   184
  radd_left_commute:       "(x::real) + (y + z) = y + (x + z)" and
neuper@52148
   185
  radd_assoc:              "(m::real) + n + k = m + (n + k)" and
neuper@52148
   186
  rmult_commute:           "(m::real) * n = n * m" and
neuper@52148
   187
  rmult_left_commute:      "(x::real) * (y * z) = y * (x * z)" and
neuper@52148
   188
  rmult_assoc:             "(m::real) * n * k = m * (n * k)" and
neuper@37906
   189
neuper@37906
   190
(*for equations: 'bdv' is a meta-constant*)
neuper@52148
   191
  risolate_bdv_add:       "((k::real) + bdv = m) = (bdv = m + (-1)*k)" and
neuper@52148
   192
  risolate_bdv_mult_add:  "((k::real) + n*bdv = m) = (n*bdv = m + (-1)*k)" and
neuper@52148
   193
  risolate_bdv_mult:      "((n::real) * bdv = m) = (bdv = m / n)" and
neuper@37906
   194
neuper@37983
   195
  rnorm_equation_add:
neuper@52148
   196
      "~(b =!= 0) ==> (a = b) = (a + (-1)*b = 0)" and
neuper@37906
   197
neuper@37906
   198
(*17.9.02 aus SqRoot.thy------------------------------vvv---*) 
neuper@52148
   199
  root_ge0:            "0 <= a ==> 0 <= sqrt a" and
neuper@37906
   200
  (*should be dropped with better simplification in eval_rls ...*)
neuper@37983
   201
  root_add_ge0:
neuper@52148
   202
	"[| 0 <= a; 0 <= b |] ==> (0 <= sqrt a + sqrt b) = True" and
neuper@37983
   203
  root_ge0_1:
neuper@52148
   204
	"[| 0<=a; 0<=b; 0<=c |] ==> (0 <= a * sqrt b + sqrt c) = True" and
neuper@37983
   205
  root_ge0_2:
neuper@52148
   206
	"[| 0<=a; 0<=b; 0<=c |] ==> (0 <= sqrt a + b * sqrt c) = True" and
neuper@37906
   207
neuper@37906
   208
neuper@52148
   209
  rroot_square_inv:         "(sqrt a)^^^ 2 = a" and
neuper@52148
   210
  rroot_times_root:         "sqrt a * sqrt b = sqrt(a*b)" and
neuper@52148
   211
  rroot_times_root_assoc:   "(a * sqrt b) * sqrt c = a * sqrt(b*c)" and
neuper@52148
   212
  rroot_times_root_assoc_p: "sqrt b * (sqrt c * a)= sqrt(b*c) * a" and
neuper@37906
   213
neuper@37906
   214
neuper@37906
   215
(*for root-equations*)
neuper@37983
   216
  square_equation_left:
neuper@52148
   217
          "[| 0 <= a; 0 <= b |] ==> (((sqrt a)=b)=(a=(b^^^ 2)))" and
neuper@37983
   218
  square_equation_right:
neuper@52148
   219
          "[| 0 <= a; 0 <= b |] ==> ((a=(sqrt b))=((a^^^ 2)=b))" and
neuper@37906
   220
  (*causes frequently non-termination:*)
neuper@37983
   221
  square_equation:  
neuper@52148
   222
          "[| 0 <= a; 0 <= b |] ==> ((a=b)=((a^^^ 2)=b^^^ 2))" and
neuper@37906
   223
  
neuper@52148
   224
  risolate_root_add:        "(a+  sqrt c = d) = (  sqrt c = d + (-1)*a)" and
neuper@52148
   225
  risolate_root_mult:       "(a+b*sqrt c = d) = (b*sqrt c = d + (-1)*a)" and
neuper@52148
   226
  risolate_root_div:        "(a * sqrt c = d) = (  sqrt c = d / a)" and
neuper@37906
   227
neuper@37906
   228
(*for polynomial equations of degree 2; linear case in RatArith*)
neuper@52148
   229
  mult_square:		"(a*bdv^^^2 = b) = (bdv^^^2 = b / a)" and
neuper@52148
   230
  constant_square:       "(a + bdv^^^2 = b) = (bdv^^^2 = b + -1*a)" and
neuper@52148
   231
  constant_mult_square:  "(a + b*bdv^^^2 = c) = (b*bdv^^^2 = c + -1*a)" and
neuper@37906
   232
neuper@37983
   233
  square_equality: 
neuper@52148
   234
	     "0 <= a ==> (x^^^2 = a) = ((x=sqrt a) | (x=-1*sqrt a))" and
neuper@37983
   235
  square_equality_0:
neuper@52148
   236
	     "(x^^^2 = 0) = (x = 0)" and
neuper@37906
   237
neuper@37906
   238
(*isolate root on the LEFT hand side of the equation
neuper@37906
   239
  otherwise shuffling from left to right would not terminate*)  
neuper@37906
   240
neuper@37983
   241
  rroot_to_lhs:
neuper@52148
   242
          "is_root_free a ==> (a = sqrt b) = (a + (-1)*sqrt b = 0)" and
neuper@37983
   243
  rroot_to_lhs_mult:
neuper@52148
   244
          "is_root_free a ==> (a = c*sqrt b) = (a + (-1)*c*sqrt b = 0)" and
neuper@37983
   245
  rroot_to_lhs_add_mult:
neuper@37906
   246
          "is_root_free a ==> (a = d+c*sqrt b) = (a + (-1)*c*sqrt b = d)"
neuper@37906
   247
(*17.9.02 aus SqRoot.thy------------------------------^^^---*)  
neuper@37906
   248
wneuper@59472
   249
section \<open>eval functions\<close>
wneuper@59472
   250
ML \<open>
neuper@37972
   251
val thy = @{theory};
neuper@37972
   252
neuper@37954
   253
(** evaluation of numerals and predicates **)
neuper@37954
   254
neuper@37954
   255
(*does a term contain a root ?*)
neuper@37954
   256
fun eval_contains_root (thmid:string) _ 
neuper@37954
   257
		       (t as (Const("Test.contains'_root",t0) $ arg)) thy = 
neuper@38053
   258
  if member op = (ids_of arg) "sqrt"
wneuper@59416
   259
  then SOME (TermC.mk_thmid thmid (Rule.term_to_string''' thy arg)"",
wneuper@59390
   260
	       HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
wneuper@59416
   261
  else SOME (TermC.mk_thmid thmid (Rule.term_to_string''' thy arg)"",
wneuper@59390
   262
	       HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
neuper@38053
   263
| eval_contains_root _ _ _ _ = NONE; 
neuper@42008
   264
neuper@42008
   265
(*dummy precondition for root-met of x+1=2*)
neuper@42008
   266
fun eval_precond_rootmet (thmid:string) _ (t as (Const ("Test.precond'_rootmet", _) $ arg)) thy = 
wneuper@59416
   267
    SOME (TermC.mk_thmid thmid (Rule.term_to_string''' thy arg)"",
wneuper@59390
   268
      HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
neuper@52070
   269
  | eval_precond_rootmet _ _ _ _ = NONE; 
neuper@42008
   270
neuper@42008
   271
(*dummy precondition for root-pbl of x+1=2*)
neuper@42008
   272
fun eval_precond_rootpbl (thmid:string) _ (t as (Const ("Test.precond'_rootpbl", _) $ arg)) thy = 
wneuper@59416
   273
    SOME (TermC.mk_thmid thmid (Rule.term_to_string''' thy arg) "",
wneuper@59390
   274
	    HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
s1210629013@52159
   275
	| eval_precond_rootpbl _ _ _ _ = NONE;
wneuper@59472
   276
\<close>
wneuper@59472
   277
setup \<open>KEStore_Elems.add_calcs
wneuper@59403
   278
  [("contains_root", ("Test.contains'_root", eval_contains_root"#contains_root_")),
s1210629013@52145
   279
    ("Test.precond'_rootmet", ("Test.precond'_rootmet", eval_precond_rootmet"#Test.precond_rootmet_")),
s1210629013@52145
   280
    ("Test.precond'_rootpbl", ("Test.precond'_rootpbl",
wneuper@59430
   281
        eval_precond_rootpbl"#Test.precond_rootpbl_"))]
wneuper@59472
   282
\<close>
wneuper@59472
   283
ML \<open>
wneuper@59430
   284
(*8.4.03  aus Poly.ML--------------------------------vvv---
wneuper@59430
   285
  make_polynomial  ---> make_poly
wneuper@59430
   286
  ^-- for user          ^-- for systest _ONLY_*)  
wneuper@59430
   287
wneuper@59430
   288
local (*. for make_polytest .*)
wneuper@59430
   289
wneuper@59430
   290
open Term;  (* for type order = EQUAL | LESS | GREATER *)
wneuper@59430
   291
wneuper@59430
   292
fun pr_ord EQUAL = "EQUAL"
wneuper@59430
   293
  | pr_ord LESS  = "LESS"
wneuper@59430
   294
  | pr_ord GREATER = "GREATER";
wneuper@59430
   295
wneuper@59430
   296
fun dest_hd' (Const (a, T)) =                          (* ~ term.ML *)
wneuper@59430
   297
  (case a of
wneuper@59430
   298
     "Atools.pow" => ((("|||||||||||||", 0), T), 0)           (*WN greatest *)
wneuper@59430
   299
   | _ => (((a, 0), T), 0))
wneuper@59430
   300
  | dest_hd' (Free (a, T)) = (((a, 0), T), 1)
wneuper@59430
   301
  | dest_hd' (Var v) = (v, 2)
wneuper@59430
   302
  | dest_hd' (Bound i) = ((("", i), dummyT), 3)
wneuper@59430
   303
  | dest_hd' (Abs (_, T, _)) = ((("", 0), T), 4);
wneuper@59430
   304
(* RL *)
wneuper@59430
   305
fun get_order_pow (t $ (Free(order,_))) = 
wneuper@59430
   306
    	(case TermC.int_of_str_opt (order) of
wneuper@59430
   307
	             SOME d => d
wneuper@59430
   308
		   | NONE   => 0)
wneuper@59430
   309
  | get_order_pow _ = 0;
wneuper@59430
   310
wneuper@59430
   311
fun size_of_term' (Const(str,_) $ t) =
wneuper@59430
   312
  if "Atools.pow"=str then 1000 + size_of_term' t else 1 + size_of_term' t(*WN*)
wneuper@59430
   313
  | size_of_term' (Abs (_,_,body)) = 1 + size_of_term' body
wneuper@59430
   314
  | size_of_term' (f$t) = size_of_term' f  +  size_of_term' t
wneuper@59430
   315
  | size_of_term' _ = 1;
wneuper@59430
   316
fun term_ord' pr thy (Abs (_, T, t), Abs(_, U, u)) =       (* ~ term.ML *)
wneuper@59430
   317
    (case term_ord' pr thy (t, u) of EQUAL => Term_Ord.typ_ord (T, U) 
wneuper@59430
   318
                                   | ord => ord)
wneuper@59430
   319
  | term_ord' pr thy (t, u) =
wneuper@59430
   320
    (if pr then 
wneuper@59430
   321
	 let val (f, ts) = strip_comb t and (g, us) = strip_comb u;
wneuper@59430
   322
	     val _ = tracing ("t= f@ts= \"" ^ Rule.term2str f ^ "\" @ \"[" ^
wneuper@59430
   323
	                      commas(map Rule.term2str ts) ^ "]\"")
wneuper@59430
   324
	     val _ = tracing ("u= g@us= \"" ^ Rule.term2str g ^"\" @ \"[" ^
wneuper@59430
   325
	                      commas(map Rule.term2str us) ^"]\"")
wneuper@59430
   326
	     val _ = tracing ("size_of_term(t,u)= (" ^
wneuper@59430
   327
	                      string_of_int (size_of_term' t) ^ ", " ^
wneuper@59430
   328
	                      string_of_int (size_of_term' u) ^ ")")
wneuper@59430
   329
	     val _ = tracing ("hd_ord(f,g)      = " ^ (pr_ord o hd_ord) (f,g))
wneuper@59430
   330
	     val _ = tracing ("terms_ord(ts,us) = " ^
wneuper@59430
   331
			      (pr_ord o terms_ord str false) (ts,us));
wneuper@59430
   332
	     val _ = tracing "-------"
wneuper@59430
   333
	 in () end
wneuper@59430
   334
       else ();
wneuper@59430
   335
	 case int_ord (size_of_term' t, size_of_term' u) of
wneuper@59430
   336
	   EQUAL =>
wneuper@59430
   337
	     let val (f, ts) = strip_comb t and (g, us) = strip_comb u in
wneuper@59430
   338
	       (case hd_ord (f, g) of EQUAL => (terms_ord str pr) (ts, us) 
wneuper@59430
   339
	     | ord => ord)
wneuper@59430
   340
	     end
wneuper@59430
   341
	 | ord => ord)
wneuper@59430
   342
and hd_ord (f, g) =                                        (* ~ term.ML *)
wneuper@59430
   343
  prod_ord (prod_ord Term_Ord.indexname_ord Term_Ord.typ_ord) int_ord (dest_hd' f, dest_hd' g)
wneuper@59430
   344
and terms_ord str pr (ts, us) = 
wneuper@59430
   345
    list_ord (term_ord' pr (Celem.assoc_thy "Isac"))(ts, us);
wneuper@59430
   346
in
wneuper@59430
   347
wneuper@59430
   348
fun ord_make_polytest (pr:bool) thy (_: Rule.subst) tu = 
wneuper@59430
   349
    (term_ord' pr thy(***) tu = LESS );
wneuper@59430
   350
wneuper@59430
   351
end;(*local*)
wneuper@59472
   352
\<close> 
wneuper@59430
   353
wneuper@59472
   354
section \<open>term order\<close>
wneuper@59472
   355
ML \<open>
wneuper@59416
   356
fun term_order (_: Rule.subst) tu = (term_ordI [] tu = LESS);
wneuper@59472
   357
\<close>
s1210629013@52145
   358
wneuper@59472
   359
section \<open>rulesets\<close>
wneuper@59472
   360
ML \<open>
neuper@37954
   361
val testerls = 
wneuper@59416
   362
  Rule.Rls {id = "testerls", preconds = [], rew_ord = ("termlessI",termlessI), 
wneuper@59416
   363
      erls = Rule.e_rls, srls = Rule.Erls, 
neuper@42451
   364
      calc = [], errpatts = [], 
wneuper@59416
   365
      rules = [Rule.Thm ("refl",TermC.num_str @{thm refl}),
wneuper@59416
   366
	       Rule.Thm ("order_refl",TermC.num_str @{thm order_refl}),
wneuper@59416
   367
	       Rule.Thm ("radd_left_cancel_le",TermC.num_str @{thm radd_left_cancel_le}),
wneuper@59416
   368
	       Rule.Thm ("not_true",TermC.num_str @{thm not_true}),
wneuper@59416
   369
	       Rule.Thm ("not_false",TermC.num_str @{thm not_false}),
wneuper@59416
   370
	       Rule.Thm ("and_true",TermC.num_str @{thm and_true}),
wneuper@59416
   371
	       Rule.Thm ("and_false",TermC.num_str @{thm and_false}),
wneuper@59416
   372
	       Rule.Thm ("or_true",TermC.num_str @{thm or_true}),
wneuper@59416
   373
	       Rule.Thm ("or_false",TermC.num_str @{thm or_false}),
wneuper@59416
   374
	       Rule.Thm ("and_commute",TermC.num_str @{thm and_commute}),
wneuper@59416
   375
	       Rule.Thm ("or_commute",TermC.num_str @{thm or_commute}),
neuper@37954
   376
wneuper@59416
   377
	       Rule.Calc ("Atools.is'_const",eval_const "#is_const_"),
wneuper@59491
   378
	       Rule.Calc ("Tools.matches", Tools.eval_matches ""),
neuper@37954
   379
    
wneuper@59416
   380
	       Rule.Calc ("Groups.plus_class.plus",eval_binop "#add_"),
wneuper@59416
   381
	       Rule.Calc ("Groups.times_class.times",eval_binop "#mult_"),
wneuper@59416
   382
	       Rule.Calc ("Atools.pow" ,eval_binop "#power_"),
neuper@37954
   383
		    
wneuper@59416
   384
	       Rule.Calc ("Orderings.ord_class.less",eval_equ "#less_"),
wneuper@59416
   385
	       Rule.Calc ("Orderings.ord_class.less_eq",eval_equ "#less_equal_"),
neuper@37954
   386
	     	    
wneuper@59416
   387
	       Rule.Calc ("Atools.ident",eval_ident "#ident_")],
wneuper@59416
   388
      scr = Rule.Prog ((Thm.term_of o the o (TermC.parse thy)) "empty_script")
wneuper@59406
   389
      };      
wneuper@59472
   390
\<close>
wneuper@59472
   391
ML \<open>
neuper@37954
   392
(*.for evaluation of conditions in rewrite rules.*)
neuper@37954
   393
(*FIXXXXXXME 10.8.02: handle like _simplify*)
neuper@37954
   394
val tval_rls =  
wneuper@59416
   395
  Rule.Rls{id = "tval_rls", preconds = [], 
wneuper@59397
   396
      rew_ord = ("sqrt_right",sqrt_right false @{theory "Pure"}), 
wneuper@59416
   397
      erls=testerls,srls = Rule.e_rls, 
neuper@42451
   398
      calc=[], errpatts = [],
wneuper@59416
   399
      rules = [Rule.Thm ("refl",TermC.num_str @{thm refl}),
wneuper@59416
   400
	       Rule.Thm ("order_refl",TermC.num_str @{thm order_refl}),
wneuper@59416
   401
	       Rule.Thm ("radd_left_cancel_le",TermC.num_str @{thm radd_left_cancel_le}),
wneuper@59416
   402
	       Rule.Thm ("not_true",TermC.num_str @{thm not_true}),
wneuper@59416
   403
	       Rule.Thm ("not_false",TermC.num_str @{thm not_false}),
wneuper@59416
   404
	       Rule.Thm ("and_true",TermC.num_str @{thm and_true}),
wneuper@59416
   405
	       Rule.Thm ("and_false",TermC.num_str @{thm and_false}),
wneuper@59416
   406
	       Rule.Thm ("or_true",TermC.num_str @{thm or_true}),
wneuper@59416
   407
	       Rule.Thm ("or_false",TermC.num_str @{thm or_false}),
wneuper@59416
   408
	       Rule.Thm ("and_commute",TermC.num_str @{thm and_commute}),
wneuper@59416
   409
	       Rule.Thm ("or_commute",TermC.num_str @{thm or_commute}),
neuper@37954
   410
wneuper@59416
   411
	       Rule.Thm ("real_diff_minus",TermC.num_str @{thm real_diff_minus}),
neuper@37954
   412
wneuper@59416
   413
	       Rule.Thm ("root_ge0",TermC.num_str @{thm root_ge0}),
wneuper@59416
   414
	       Rule.Thm ("root_add_ge0",TermC.num_str @{thm root_add_ge0}),
wneuper@59416
   415
	       Rule.Thm ("root_ge0_1",TermC.num_str @{thm root_ge0_1}),
wneuper@59416
   416
	       Rule.Thm ("root_ge0_2",TermC.num_str @{thm root_ge0_2}),
neuper@37954
   417
wneuper@59416
   418
	       Rule.Calc ("Atools.is'_const",eval_const "#is_const_"),
wneuper@59416
   419
	       Rule.Calc ("Test.contains'_root",eval_contains_root "#eval_contains_root"),
wneuper@59491
   420
	       Rule.Calc ("Tools.matches", Tools.eval_matches ""),
wneuper@59416
   421
	       Rule.Calc ("Test.contains'_root",
neuper@37954
   422
		     eval_contains_root"#contains_root_"),
neuper@37954
   423
    
wneuper@59416
   424
	       Rule.Calc ("Groups.plus_class.plus",eval_binop "#add_"),
wneuper@59416
   425
	       Rule.Calc ("Groups.times_class.times",eval_binop "#mult_"),
wneuper@59416
   426
	       Rule.Calc ("NthRoot.sqrt",eval_sqrt "#sqrt_"),
wneuper@59416
   427
	       Rule.Calc ("Atools.pow" ,eval_binop "#power_"),
neuper@37954
   428
		    
wneuper@59416
   429
	       Rule.Calc ("Orderings.ord_class.less",eval_equ "#less_"),
wneuper@59416
   430
	       Rule.Calc ("Orderings.ord_class.less_eq",eval_equ "#less_equal_"),
neuper@37954
   431
	     	    
wneuper@59416
   432
	       Rule.Calc ("Atools.ident",eval_ident "#ident_")],
wneuper@59416
   433
      scr = Rule.Prog ((Thm.term_of o the o (TermC.parse thy)) "empty_script")
wneuper@59406
   434
      };      
wneuper@59472
   435
\<close>
wneuper@59472
   436
setup \<open>KEStore_Elems.add_rlss [("testerls", (Context.theory_name @{theory}, prep_rls' testerls))]\<close>
neuper@52155
   437
wneuper@59472
   438
ML \<open>
neuper@37954
   439
(*make () dissappear*)   
neuper@37954
   440
val rearrange_assoc =
wneuper@59416
   441
  Rule.Rls{id = "rearrange_assoc", preconds = [], 
wneuper@59416
   442
      rew_ord = ("e_rew_ord",Rule.e_rew_ord), 
wneuper@59416
   443
      erls = Rule.e_rls, srls = Rule.e_rls, calc = [], errpatts = [],
neuper@37954
   444
      rules = 
wneuper@59416
   445
      [Rule.Thm ("sym_add_assoc",TermC.num_str (@{thm add.assoc} RS @{thm sym})),
wneuper@59416
   446
       Rule.Thm ("sym_rmult_assoc",TermC.num_str (@{thm rmult_assoc} RS @{thm sym}))],
wneuper@59416
   447
      scr = Rule.Prog ((Thm.term_of o the o (TermC.parse thy)) "empty_script")
wneuper@59406
   448
      };      
neuper@37954
   449
neuper@37954
   450
val ac_plus_times =
wneuper@59416
   451
  Rule.Rls{id = "ac_plus_times", preconds = [], rew_ord = ("term_order",term_order),
wneuper@59416
   452
      erls = Rule.e_rls, srls = Rule.e_rls, calc = [], errpatts = [],
neuper@37954
   453
      rules = 
wneuper@59416
   454
      [Rule.Thm ("radd_commute",TermC.num_str @{thm radd_commute}),
wneuper@59416
   455
       Rule.Thm ("radd_left_commute",TermC.num_str @{thm radd_left_commute}),
wneuper@59416
   456
       Rule.Thm ("add_assoc",TermC.num_str @{thm add.assoc}),
wneuper@59416
   457
       Rule.Thm ("rmult_commute",TermC.num_str @{thm rmult_commute}),
wneuper@59416
   458
       Rule.Thm ("rmult_left_commute",TermC.num_str @{thm rmult_left_commute}),
wneuper@59416
   459
       Rule.Thm ("rmult_assoc",TermC.num_str @{thm rmult_assoc})],
wneuper@59416
   460
      scr = Rule.Prog ((Thm.term_of o the o (TermC.parse thy)) "empty_script")
wneuper@59406
   461
      };      
neuper@37954
   462
wneuper@59389
   463
(*todo: replace by Rewrite("rnorm_equation_add",TermC.num_str @{thm rnorm_equation_add)*)
neuper@37954
   464
val norm_equation =
wneuper@59416
   465
  Rule.Rls{id = "norm_equation", preconds = [], rew_ord = ("e_rew_ord",Rule.e_rew_ord),
wneuper@59416
   466
      erls = tval_rls, srls = Rule.e_rls, calc = [], errpatts = [],
wneuper@59416
   467
      rules = [Rule.Thm ("rnorm_equation_add",TermC.num_str @{thm rnorm_equation_add})
neuper@37954
   468
	       ],
wneuper@59416
   469
      scr = Rule.Prog ((Thm.term_of o the o (TermC.parse thy)) "empty_script")
wneuper@59406
   470
      };      
wneuper@59472
   471
\<close>
wneuper@59472
   472
ML \<open>
neuper@37954
   473
(* expects * distributed over + *)
neuper@37954
   474
val Test_simplify =
wneuper@59416
   475
  Rule.Rls{id = "Test_simplify", preconds = [], 
wneuper@59397
   476
      rew_ord = ("sqrt_right",sqrt_right false @{theory "Pure"}),
wneuper@59416
   477
      erls = tval_rls, srls = Rule.e_rls, 
s1210629013@55444
   478
      calc=[(*since 040209 filled by prep_rls'*)], errpatts = [],
neuper@37954
   479
      rules = [
wneuper@59416
   480
	       Rule.Thm ("real_diff_minus",TermC.num_str @{thm real_diff_minus}),
wneuper@59416
   481
	       Rule.Thm ("radd_mult_distrib2",TermC.num_str @{thm radd_mult_distrib2}),
wneuper@59416
   482
	       Rule.Thm ("rdistr_right_assoc",TermC.num_str @{thm rdistr_right_assoc}),
wneuper@59416
   483
	       Rule.Thm ("rdistr_right_assoc_p",TermC.num_str @{thm rdistr_right_assoc_p}),
wneuper@59416
   484
	       Rule.Thm ("rdistr_div_right",TermC.num_str @{thm rdistr_div_right}),
wneuper@59416
   485
	       Rule.Thm ("rbinom_power_2",TermC.num_str @{thm rbinom_power_2}),	       
neuper@37954
   486
wneuper@59416
   487
               Rule.Thm ("radd_commute",TermC.num_str @{thm radd_commute}), 
wneuper@59416
   488
	       Rule.Thm ("radd_left_commute",TermC.num_str @{thm radd_left_commute}),
wneuper@59416
   489
	       Rule.Thm ("add_assoc",TermC.num_str @{thm add.assoc}),
wneuper@59416
   490
	       Rule.Thm ("rmult_commute",TermC.num_str @{thm rmult_commute}),
wneuper@59416
   491
	       Rule.Thm ("rmult_left_commute",TermC.num_str @{thm rmult_left_commute}),
wneuper@59416
   492
	       Rule.Thm ("rmult_assoc",TermC.num_str @{thm rmult_assoc}),
neuper@37954
   493
wneuper@59416
   494
	       Rule.Thm ("radd_real_const_eq",TermC.num_str @{thm radd_real_const_eq}),
wneuper@59416
   495
	       Rule.Thm ("radd_real_const",TermC.num_str @{thm radd_real_const}),
neuper@37954
   496
	       (* these 2 rules are invers to distr_div_right wrt. termination.
neuper@37954
   497
		  thus they MUST be done IMMEDIATELY before calc *)
wneuper@59416
   498
	       Rule.Calc ("Groups.plus_class.plus", eval_binop "#add_"), 
wneuper@59416
   499
	       Rule.Calc ("Groups.times_class.times", eval_binop "#mult_"),
wneuper@59416
   500
	       Rule.Calc ("Rings.divide_class.divide", eval_cancel "#divide_e"),
wneuper@59416
   501
	       Rule.Calc ("Atools.pow", eval_binop "#power_"),
neuper@37954
   502
wneuper@59416
   503
	       Rule.Thm ("rcollect_right",TermC.num_str @{thm rcollect_right}),
wneuper@59416
   504
	       Rule.Thm ("rcollect_one_left",TermC.num_str @{thm rcollect_one_left}),
wneuper@59416
   505
	       Rule.Thm ("rcollect_one_left_assoc",TermC.num_str @{thm rcollect_one_left_assoc}),
wneuper@59416
   506
	       Rule.Thm ("rcollect_one_left_assoc_p",TermC.num_str @{thm rcollect_one_left_assoc_p}),
neuper@37954
   507
wneuper@59416
   508
	       Rule.Thm ("rshift_nominator",TermC.num_str @{thm rshift_nominator}),
wneuper@59416
   509
	       Rule.Thm ("rcancel_den",TermC.num_str @{thm rcancel_den}),
wneuper@59416
   510
	       Rule.Thm ("rroot_square_inv",TermC.num_str @{thm rroot_square_inv}),
wneuper@59416
   511
	       Rule.Thm ("rroot_times_root",TermC.num_str @{thm rroot_times_root}),
wneuper@59416
   512
	       Rule.Thm ("rroot_times_root_assoc_p",TermC.num_str @{thm rroot_times_root_assoc_p}),
wneuper@59416
   513
	       Rule.Thm ("rsqare",TermC.num_str @{thm rsqare}),
wneuper@59416
   514
	       Rule.Thm ("power_1",TermC.num_str @{thm power_1}),
wneuper@59416
   515
	       Rule.Thm ("rtwo_of_the_same",TermC.num_str @{thm rtwo_of_the_same}),
wneuper@59416
   516
	       Rule.Thm ("rtwo_of_the_same_assoc_p",TermC.num_str @{thm rtwo_of_the_same_assoc_p}),
neuper@37954
   517
wneuper@59416
   518
	       Rule.Thm ("rmult_1",TermC.num_str @{thm rmult_1}),
wneuper@59416
   519
	       Rule.Thm ("rmult_1_right",TermC.num_str @{thm rmult_1_right}),
wneuper@59416
   520
	       Rule.Thm ("rmult_0",TermC.num_str @{thm rmult_0}),
wneuper@59416
   521
	       Rule.Thm ("rmult_0_right",TermC.num_str @{thm rmult_0_right}),
wneuper@59416
   522
	       Rule.Thm ("radd_0",TermC.num_str @{thm radd_0}),
wneuper@59416
   523
	       Rule.Thm ("radd_0_right",TermC.num_str @{thm radd_0_right})
neuper@37954
   524
	       ],
wneuper@59416
   525
      scr = Rule.Prog ((Thm.term_of o the o (TermC.parse thy)) "empty_script")
s1210629013@55444
   526
		    (*since 040209 filled by prep_rls': STest_simplify*)
wneuper@59406
   527
      };      
wneuper@59472
   528
\<close>
wneuper@59472
   529
ML \<open>
neuper@37954
   530
(*isolate the root in a root-equation*)
neuper@37954
   531
val isolate_root =
wneuper@59416
   532
  Rule.Rls{id = "isolate_root", preconds = [], rew_ord = ("e_rew_ord",Rule.e_rew_ord), 
wneuper@59416
   533
      erls=tval_rls,srls = Rule.e_rls, calc=[], errpatts = [],
wneuper@59416
   534
      rules = [Rule.Thm ("rroot_to_lhs",TermC.num_str @{thm rroot_to_lhs}),
wneuper@59416
   535
	       Rule.Thm ("rroot_to_lhs_mult",TermC.num_str @{thm rroot_to_lhs_mult}),
wneuper@59416
   536
	       Rule.Thm ("rroot_to_lhs_add_mult",TermC.num_str @{thm rroot_to_lhs_add_mult}),
wneuper@59416
   537
	       Rule.Thm ("risolate_root_add",TermC.num_str @{thm risolate_root_add}),
wneuper@59416
   538
	       Rule.Thm ("risolate_root_mult",TermC.num_str @{thm risolate_root_mult}),
wneuper@59416
   539
	       Rule.Thm ("risolate_root_div",TermC.num_str @{thm risolate_root_div})       ],
wneuper@59416
   540
      scr = Rule.Prog ((Thm.term_of o the o (TermC.parse thy)) 
neuper@37954
   541
      "empty_script")
wneuper@59406
   542
      };
neuper@37954
   543
neuper@37954
   544
(*isolate the bound variable in an equation; 'bdv' is a meta-constant*)
neuper@37954
   545
val isolate_bdv =
wneuper@59416
   546
    Rule.Rls{id = "isolate_bdv", preconds = [], rew_ord = ("e_rew_ord",Rule.e_rew_ord),
wneuper@59416
   547
	erls=tval_rls,srls = Rule.e_rls, calc= [], errpatts = [],
neuper@37954
   548
	rules = 
wneuper@59416
   549
	[Rule.Thm ("risolate_bdv_add",TermC.num_str @{thm risolate_bdv_add}),
wneuper@59416
   550
	 Rule.Thm ("risolate_bdv_mult_add",TermC.num_str @{thm risolate_bdv_mult_add}),
wneuper@59416
   551
	 Rule.Thm ("risolate_bdv_mult",TermC.num_str @{thm risolate_bdv_mult}),
wneuper@59416
   552
	 Rule.Thm ("mult_square",TermC.num_str @{thm mult_square}),
wneuper@59416
   553
	 Rule.Thm ("constant_square",TermC.num_str @{thm constant_square}),
wneuper@59416
   554
	 Rule.Thm ("constant_mult_square",TermC.num_str @{thm constant_mult_square})
neuper@37954
   555
	 ],
wneuper@59416
   556
	scr = Rule.Prog ((Thm.term_of o the o (TermC.parse thy)) 
neuper@37954
   557
			  "empty_script")
wneuper@59406
   558
	};      
wneuper@59472
   559
\<close>
wneuper@59472
   560
ML \<open>val prep_rls' = LTool.prep_rls @{theory};\<close>
wneuper@59472
   561
setup \<open>KEStore_Elems.add_rlss 
s1210629013@55444
   562
  [("Test_simplify", (Context.theory_name @{theory}, prep_rls' Test_simplify)), 
s1210629013@55444
   563
  ("tval_rls", (Context.theory_name @{theory}, prep_rls' tval_rls)), 
s1210629013@55444
   564
  ("isolate_root", (Context.theory_name @{theory}, prep_rls' isolate_root)), 
s1210629013@55444
   565
  ("isolate_bdv", (Context.theory_name @{theory}, prep_rls' isolate_bdv)), 
s1210629013@55444
   566
  ("matches", (Context.theory_name @{theory}, prep_rls'
wneuper@59491
   567
    (Rule.append_rls "matches" testerls [Rule.Calc ("Tools.matches", Tools.eval_matches "#matches_")])))]
wneuper@59472
   568
\<close>
neuper@37954
   569
wneuper@59472
   570
subsection \<open>problems\<close>
neuper@37954
   571
(** problem types **)
wneuper@59472
   572
setup \<open>KEStore_Elems.add_pbts
wneuper@59416
   573
  [(Specify.prep_pbt thy "pbl_test" [] Celem.e_pblID (["test"], [], Rule.e_rls, NONE, [])),
wneuper@59406
   574
    (Specify.prep_pbt thy "pbl_test_equ" [] Celem.e_pblID
s1210629013@55339
   575
      (["equation","test"],
s1210629013@55339
   576
        [("#Given" ,["equality e_e","solveFor v_v"]),
s1210629013@55339
   577
           ("#Where" ,["matches (?a = ?b) e_e"]),
s1210629013@55339
   578
           ("#Find"  ,["solutions v_v'i'"])],
s1210629013@55339
   579
        assoc_rls' @{theory} "matches", SOME "solve (e_e::bool, v_v)", [])),
wneuper@59406
   580
    (Specify.prep_pbt thy "pbl_test_uni" [] Celem.e_pblID
s1210629013@55339
   581
      (["univariate","equation","test"],
s1210629013@55339
   582
        [("#Given" ,["equality e_e","solveFor v_v"]),
s1210629013@55339
   583
           ("#Where" ,["matches (?a = ?b) e_e"]),
s1210629013@55339
   584
           ("#Find"  ,["solutions v_v'i'"])],
s1210629013@55339
   585
        assoc_rls' @{theory} "matches", SOME "solve (e_e::bool, v_v)", [])),
wneuper@59406
   586
    (Specify.prep_pbt thy "pbl_test_uni_lin" [] Celem.e_pblID
s1210629013@55339
   587
      (["LINEAR","univariate","equation","test"],
s1210629013@55339
   588
        [("#Given" ,["equality e_e","solveFor v_v"]),
s1210629013@55339
   589
           ("#Where" ,["(matches (   v_v = 0) e_e) | (matches (   ?b*v_v = 0) e_e) |" ^
s1210629013@55339
   590
             "(matches (?a+v_v = 0) e_e) | (matches (?a+?b*v_v = 0) e_e)  "]),
s1210629013@55339
   591
           ("#Find"  ,["solutions v_v'i'"])],
s1210629013@55339
   592
        assoc_rls' @{theory} "matches", 
wneuper@59430
   593
        SOME "solve (e_e::bool, v_v)", [["Test","solve_linear"]]))]
wneuper@59472
   594
\<close>
wneuper@59472
   595
ML \<open>
wneuper@59416
   596
Rule.rew_ord' := overwritel (! Rule.rew_ord',
neuper@37954
   597
[("termlessI", termlessI),
neuper@37954
   598
 ("ord_make_polytest", ord_make_polytest false thy)
neuper@37954
   599
 ]);
neuper@37954
   600
neuper@37954
   601
val make_polytest =
wneuper@59416
   602
  Rule.Rls{id = "make_polytest", preconds = []:term list, 
neuper@52105
   603
      rew_ord = ("ord_make_polytest", ord_make_polytest false @{theory "Poly"}),
wneuper@59416
   604
      erls = testerls, srls = Rule.Erls,
neuper@38014
   605
      calc = [("PLUS"  , ("Groups.plus_class.plus", eval_binop "#add_")), 
neuper@38034
   606
	      ("TIMES" , ("Groups.times_class.times", eval_binop "#mult_")),
neuper@37954
   607
	      ("POWER", ("Atools.pow", eval_binop "#power_"))
neuper@42451
   608
	      ], errpatts = [],
wneuper@59416
   609
      rules = [Rule.Thm ("real_diff_minus",TermC.num_str @{thm real_diff_minus}),
neuper@37954
   610
	       (*"a - b = a + (-1) * b"*)
wneuper@59416
   611
	       Rule.Thm ("distrib_right" ,TermC.num_str @{thm distrib_right}),
neuper@37954
   612
	       (*"(z1.0 + z2.0) * w = z1.0 * w + z2.0 * w"*)
wneuper@59416
   613
	       Rule.Thm ("distrib_left",TermC.num_str @{thm distrib_left}),
neuper@37954
   614
	       (*"w * (z1.0 + z2.0) = w * z1.0 + w * z2.0"*)
wneuper@59416
   615
	       Rule.Thm ("left_diff_distrib" ,TermC.num_str @{thm left_diff_distrib}),
neuper@37954
   616
	       (*"(z1.0 - z2.0) * w = z1.0 * w - z2.0 * w"*)
wneuper@59416
   617
	       Rule.Thm ("right_diff_distrib",TermC.num_str @{thm right_diff_distrib}),
neuper@37954
   618
	       (*"w * (z1.0 - z2.0) = w * z1.0 - w * z2.0"*)
wneuper@59416
   619
	       Rule.Thm ("mult_1_left",TermC.num_str @{thm mult_1_left}),                 
neuper@37954
   620
	       (*"1 * z = z"*)
wneuper@59416
   621
	       Rule.Thm ("mult_zero_left",TermC.num_str @{thm mult_zero_left}),        
neuper@37954
   622
	       (*"0 * z = 0"*)
wneuper@59416
   623
	       Rule.Thm ("add_0_left",TermC.num_str @{thm add_0_left}),
neuper@37954
   624
	       (*"0 + z = z"*)
neuper@37954
   625
neuper@37954
   626
	       (*AC-rewriting*)
wneuper@59416
   627
	       Rule.Thm ("mult_commute",TermC.num_str @{thm mult.commute}),
neuper@37954
   628
	       (* z * w = w * z *)
wneuper@59416
   629
	       Rule.Thm ("real_mult_left_commute",TermC.num_str @{thm real_mult_left_commute}),
neuper@37954
   630
	       (*z1.0 * (z2.0 * z3.0) = z2.0 * (z1.0 * z3.0)*)
wneuper@59416
   631
	       Rule.Thm ("mult_assoc",TermC.num_str @{thm mult.assoc}),		
neuper@37954
   632
	       (*z1.0 * z2.0 * z3.0 = z1.0 * (z2.0 * z3.0)*)
wneuper@59416
   633
	       Rule.Thm ("add_commute",TermC.num_str @{thm add.commute}),	
neuper@37954
   634
	       (*z + w = w + z*)
wneuper@59416
   635
	       Rule.Thm ("add_left_commute",TermC.num_str @{thm add.left_commute}),
neuper@37954
   636
	       (*x + (y + z) = y + (x + z)*)
wneuper@59416
   637
	       Rule.Thm ("add_assoc",TermC.num_str @{thm add.assoc}),	               
neuper@37954
   638
	       (*z1.0 + z2.0 + z3.0 = z1.0 + (z2.0 + z3.0)*)
neuper@37954
   639
wneuper@59416
   640
	       Rule.Thm ("sym_realpow_twoI",
wneuper@59389
   641
                     TermC.num_str (@{thm realpow_twoI} RS @{thm sym})),	
neuper@37954
   642
	       (*"r1 * r1 = r1 ^^^ 2"*)
wneuper@59416
   643
	       Rule.Thm ("realpow_plus_1",TermC.num_str @{thm realpow_plus_1}),		
neuper@37954
   644
	       (*"r * r ^^^ n = r ^^^ (n + 1)"*)
wneuper@59416
   645
	       Rule.Thm ("sym_real_mult_2",
wneuper@59389
   646
                     TermC.num_str (@{thm real_mult_2} RS @{thm sym})),	
neuper@37954
   647
	       (*"z1 + z1 = 2 * z1"*)
wneuper@59416
   648
	       Rule.Thm ("real_mult_2_assoc",TermC.num_str @{thm real_mult_2_assoc}),	
neuper@37954
   649
	       (*"z1 + (z1 + k) = 2 * z1 + k"*)
neuper@37954
   650
wneuper@59416
   651
	       Rule.Thm ("real_num_collect",TermC.num_str @{thm real_num_collect}), 
neuper@37954
   652
	       (*"[| l is_const; m is_const |]==>l * n + m * n = (l + m) * n"*)
wneuper@59416
   653
	       Rule.Thm ("real_num_collect_assoc",TermC.num_str @{thm real_num_collect_assoc}),
neuper@37954
   654
	       (*"[| l is_const; m is_const |] ==>  
neuper@37954
   655
				l * n + (m * n + k) =  (l + m) * n + k"*)
wneuper@59416
   656
	       Rule.Thm ("real_one_collect",TermC.num_str @{thm real_one_collect}),	
neuper@37954
   657
	       (*"m is_const ==> n + m * n = (1 + m) * n"*)
wneuper@59416
   658
	       Rule.Thm ("real_one_collect_assoc",TermC.num_str @{thm real_one_collect_assoc}), 
neuper@37954
   659
	       (*"m is_const ==> k + (n + m * n) = k + (1 + m) * n"*)
neuper@37954
   660
wneuper@59416
   661
	       Rule.Calc ("Groups.plus_class.plus", eval_binop "#add_"), 
wneuper@59416
   662
	       Rule.Calc ("Groups.times_class.times", eval_binop "#mult_"),
wneuper@59416
   663
	       Rule.Calc ("Atools.pow", eval_binop "#power_")
neuper@37954
   664
	       ],
wneuper@59416
   665
      scr = Rule.EmptyScr(*Rule.Prog ((Thm.term_of o the o (parse thy)) 
neuper@37954
   666
      scr_make_polytest)*)
wneuper@59406
   667
      }; 
neuper@37954
   668
neuper@37954
   669
val expand_binomtest =
wneuper@59416
   670
  Rule.Rls{id = "expand_binomtest", preconds = [], 
neuper@37954
   671
      rew_ord = ("termlessI",termlessI),
wneuper@59416
   672
      erls = testerls, srls = Rule.Erls,
neuper@38014
   673
      calc = [("PLUS"  , ("Groups.plus_class.plus", eval_binop "#add_")), 
neuper@38034
   674
	      ("TIMES" , ("Groups.times_class.times", eval_binop "#mult_")),
neuper@37954
   675
	      ("POWER", ("Atools.pow", eval_binop "#power_"))
neuper@42451
   676
	      ], errpatts = [],
neuper@38001
   677
      rules = 
wneuper@59416
   678
      [Rule.Thm ("real_plus_binom_pow2"  ,TermC.num_str @{thm real_plus_binom_pow2}),     
neuper@37954
   679
	       (*"(a + b) ^^^ 2 = a ^^^ 2 + 2 * a * b + b ^^^ 2"*)
wneuper@59416
   680
       Rule.Thm ("real_plus_binom_times" ,TermC.num_str @{thm real_plus_binom_times}),    
neuper@37954
   681
	      (*"(a + b)*(a + b) = ...*)
wneuper@59416
   682
       Rule.Thm ("real_minus_binom_pow2" ,TermC.num_str @{thm real_minus_binom_pow2}),   
neuper@38001
   683
       (*"(a - b) ^^^ 2 = a ^^^ 2 - 2 * a * b + b ^^^ 2"*)
wneuper@59416
   684
       Rule.Thm ("real_minus_binom_times",TermC.num_str @{thm real_minus_binom_times}),   
neuper@38001
   685
       (*"(a - b)*(a - b) = ...*)
wneuper@59416
   686
       Rule.Thm ("real_plus_minus_binom1",TermC.num_str @{thm real_plus_minus_binom1}),   
neuper@38001
   687
        (*"(a + b) * (a - b) = a ^^^ 2 - b ^^^ 2"*)
wneuper@59416
   688
       Rule.Thm ("real_plus_minus_binom2",TermC.num_str @{thm real_plus_minus_binom2}),   
neuper@38001
   689
        (*"(a - b) * (a + b) = a ^^^ 2 - b ^^^ 2"*)
neuper@38001
   690
       (*RL 020915*)
wneuper@59416
   691
       Rule.Thm ("real_pp_binom_times",TermC.num_str @{thm real_pp_binom_times}), 
neuper@38001
   692
        (*(a + b)*(c + d) = a*c + a*d + b*c + b*d*)
wneuper@59416
   693
       Rule.Thm ("real_pm_binom_times",TermC.num_str @{thm real_pm_binom_times}), 
neuper@38001
   694
        (*(a + b)*(c - d) = a*c - a*d + b*c - b*d*)
wneuper@59416
   695
       Rule.Thm ("real_mp_binom_times",TermC.num_str @{thm real_mp_binom_times}), 
neuper@38001
   696
        (*(a - b)*(c p d) = a*c + a*d - b*c - b*d*)
wneuper@59416
   697
       Rule.Thm ("real_mm_binom_times",TermC.num_str @{thm real_mm_binom_times}), 
neuper@38001
   698
        (*(a - b)*(c p d) = a*c - a*d - b*c + b*d*)
wneuper@59416
   699
       Rule.Thm ("realpow_multI",TermC.num_str @{thm realpow_multI}),                
neuper@38001
   700
        (*(a*b)^^^n = a^^^n * b^^^n*)
wneuper@59416
   701
       Rule.Thm ("real_plus_binom_pow3",TermC.num_str @{thm real_plus_binom_pow3}),
neuper@38001
   702
        (* (a + b)^^^3 = a^^^3 + 3*a^^^2*b + 3*a*b^^^2 + b^^^3 *)
wneuper@59416
   703
       Rule.Thm ("real_minus_binom_pow3",TermC.num_str @{thm real_minus_binom_pow3}),
neuper@38001
   704
        (* (a - b)^^^3 = a^^^3 - 3*a^^^2*b + 3*a*b^^^2 - b^^^3 *)
neuper@37954
   705
neuper@37954
   706
wneuper@59416
   707
     (*  Rule.Thm ("distrib_right" ,TermC.num_str @{thm distrib_right}),	
neuper@38001
   708
	 (*"(z1.0 + z2.0) * w = z1.0 * w + z2.0 * w"*)
wneuper@59416
   709
	Rule.Thm ("distrib_left",TermC.num_str @{thm distrib_left}),	
neuper@38001
   710
	(*"w * (z1.0 + z2.0) = w * z1.0 + w * z2.0"*)
wneuper@59416
   711
	Rule.Thm ("left_diff_distrib" ,TermC.num_str @{thm left_diff_distrib}),	
neuper@38001
   712
	(*"(z1.0 - z2.0) * w = z1.0 * w - z2.0 * w"*)
wneuper@59416
   713
	Rule.Thm ("right_diff_distrib",TermC.num_str @{thm right_diff_distrib}),	
neuper@38001
   714
	(*"w * (z1.0 - z2.0) = w * z1.0 - w * z2.0"*)
neuper@38001
   715
	*)
neuper@38001
   716
	
wneuper@59416
   717
	Rule.Thm ("mult_1_left",TermC.num_str @{thm mult_1_left}),              
neuper@38001
   718
         (*"1 * z = z"*)
wneuper@59416
   719
	Rule.Thm ("mult_zero_left",TermC.num_str @{thm mult_zero_left}),              
neuper@38001
   720
         (*"0 * z = 0"*)
wneuper@59416
   721
	Rule.Thm ("add_0_left",TermC.num_str @{thm add_0_left}),
neuper@38001
   722
         (*"0 + z = z"*)
neuper@37954
   723
wneuper@59416
   724
	Rule.Calc ("Groups.plus_class.plus", eval_binop "#add_"), 
wneuper@59416
   725
	Rule.Calc ("Groups.times_class.times", eval_binop "#mult_"),
wneuper@59416
   726
	Rule.Calc ("Atools.pow", eval_binop "#power_"),
neuper@38001
   727
        (*	       
wneuper@59416
   728
	 Rule.Thm ("mult_commute",TermC.num_str @{thm mult_commute}),		
neuper@38001
   729
        (*AC-rewriting*)
wneuper@59416
   730
	Rule.Thm ("real_mult_left_commute",TermC.num_str @{thm real_mult_left_commute}),
wneuper@59416
   731
	Rule.Thm ("mult_assoc",TermC.num_str @{thm mult.assoc}),
wneuper@59416
   732
	Rule.Thm ("add_commute",TermC.num_str @{thm add_commute}),	
wneuper@59416
   733
	Rule.Thm ("add_left_commute",TermC.num_str @{thm add_left_commute}),
wneuper@59416
   734
	Rule.Thm ("add_assoc",TermC.num_str @{thm add_assoc}),
neuper@38001
   735
	*)
neuper@38001
   736
	
wneuper@59416
   737
	Rule.Thm ("sym_realpow_twoI",
wneuper@59389
   738
              TermC.num_str (@{thm realpow_twoI} RS @{thm sym})),
neuper@38001
   739
	(*"r1 * r1 = r1 ^^^ 2"*)
wneuper@59416
   740
	Rule.Thm ("realpow_plus_1",TermC.num_str @{thm realpow_plus_1}),			
neuper@38001
   741
	(*"r * r ^^^ n = r ^^^ (n + 1)"*)
wneuper@59416
   742
	(*Rule.Thm ("sym_real_mult_2",
wneuper@59389
   743
                TermC.num_str (@{thm real_mult_2} RS @{thm sym})),
neuper@38001
   744
	(*"z1 + z1 = 2 * z1"*)*)
wneuper@59416
   745
	Rule.Thm ("real_mult_2_assoc",TermC.num_str @{thm real_mult_2_assoc}),		
neuper@38001
   746
	(*"z1 + (z1 + k) = 2 * z1 + k"*)
neuper@37954
   747
wneuper@59416
   748
	Rule.Thm ("real_num_collect",TermC.num_str @{thm real_num_collect}), 
neuper@38001
   749
	(*"[| l is_const; m is_const |] ==> l * n + m * n = (l + m) * n"*)
wneuper@59416
   750
	Rule.Thm ("real_num_collect_assoc",TermC.num_str @{thm real_num_collect_assoc}),	
neuper@38001
   751
	(*"[| l is_const; m is_const |] ==>  l * n + (m * n + k) =  (l + m) * n + k"*)
wneuper@59416
   752
	Rule.Thm ("real_one_collect",TermC.num_str @{thm real_one_collect}),		
neuper@38001
   753
	(*"m is_const ==> n + m * n = (1 + m) * n"*)
wneuper@59416
   754
	Rule.Thm ("real_one_collect_assoc",TermC.num_str @{thm real_one_collect_assoc}), 
neuper@38001
   755
	(*"m is_const ==> k + (n + m * n) = k + (1 + m) * n"*)
neuper@37954
   756
wneuper@59416
   757
	Rule.Calc ("Groups.plus_class.plus", eval_binop "#add_"), 
wneuper@59416
   758
	Rule.Calc ("Groups.times_class.times", eval_binop "#mult_"),
wneuper@59416
   759
	Rule.Calc ("Atools.pow", eval_binop "#power_")
neuper@38001
   760
	],
wneuper@59416
   761
      scr = Rule.EmptyScr
wneuper@59186
   762
(*Script ((Thm.term_of o the o (parse thy)) scr_expand_binomtest)*)
wneuper@59406
   763
      };      
wneuper@59472
   764
\<close>
wneuper@59472
   765
setup \<open>KEStore_Elems.add_rlss 
s1210629013@55444
   766
  [("make_polytest", (Context.theory_name @{theory}, prep_rls' make_polytest)), 
wneuper@59472
   767
  ("expand_binomtest", (Context.theory_name @{theory}, prep_rls' expand_binomtest))]\<close>
wneuper@59472
   768
setup \<open>KEStore_Elems.add_rlss 
wneuper@59430
   769
  [("norm_equation", (Context.theory_name @{theory}, prep_rls' norm_equation)), 
wneuper@59430
   770
  ("ac_plus_times", (Context.theory_name @{theory}, prep_rls' ac_plus_times)), 
wneuper@59472
   771
  ("rearrange_assoc", (Context.theory_name @{theory}, prep_rls' rearrange_assoc))]\<close>
wneuper@59430
   772
wneuper@59472
   773
section \<open>problems\<close>
wneuper@59472
   774
setup \<open>KEStore_Elems.add_pbts
wneuper@59430
   775
  [(Specify.prep_pbt thy "pbl_test_uni_plain2" [] Celem.e_pblID
wneuper@59430
   776
    (["plain_square","univariate","equation","test"],
wneuper@59430
   777
      [("#Given" ,["equality e_e","solveFor v_v"]),
wneuper@59430
   778
        ("#Where" ,["(matches (?a + ?b*v_v ^^^2 = 0) e_e) |" ^
wneuper@59430
   779
	        "(matches (     ?b*v_v ^^^2 = 0) e_e) |" ^
wneuper@59430
   780
	        "(matches (?a +    v_v ^^^2 = 0) e_e) |" ^
wneuper@59430
   781
	        "(matches (        v_v ^^^2 = 0) e_e)"]),
wneuper@59430
   782
        ("#Find"  ,["solutions v_v'i'"])],
wneuper@59430
   783
      assoc_rls' @{theory} "matches", 
wneuper@59430
   784
      SOME "solve (e_e::bool, v_v)", [["Test","solve_plain_square"]])),
wneuper@59430
   785
    (Specify.prep_pbt thy "pbl_test_uni_poly" [] Celem.e_pblID
wneuper@59430
   786
      (["polynomial","univariate","equation","test"],
wneuper@59430
   787
        [("#Given" ,["equality (v_v ^^^2 + p_p * v_v + q__q = 0)","solveFor v_v"]),
wneuper@59430
   788
          ("#Where" ,["HOL.False"]),
wneuper@59430
   789
          ("#Find"  ,["solutions v_v'i'"])],
wneuper@59430
   790
        Rule.e_rls, SOME "solve (e_e::bool, v_v)", [])),
wneuper@59430
   791
    (Specify.prep_pbt thy "pbl_test_uni_poly_deg2" [] Celem.e_pblID
wneuper@59430
   792
      (["degree_two","polynomial","univariate","equation","test"],
wneuper@59430
   793
        [("#Given" ,["equality (v_v ^^^2 + p_p * v_v + q__q = 0)","solveFor v_v"]),
wneuper@59430
   794
          ("#Find"  ,["solutions v_v'i'"])],
wneuper@59430
   795
        Rule.e_rls, SOME "solve (v_v ^^^2 + p_p * v_v + q__q = 0, v_v)", [])),
wneuper@59430
   796
    (Specify.prep_pbt thy "pbl_test_uni_poly_deg2_pq" [] Celem.e_pblID
wneuper@59430
   797
      (["pq_formula","degree_two","polynomial","univariate","equation","test"],
wneuper@59430
   798
        [("#Given" ,["equality (v_v ^^^2 + p_p * v_v + q__q = 0)","solveFor v_v"]),
wneuper@59430
   799
          ("#Find"  ,["solutions v_v'i'"])],
wneuper@59430
   800
        Rule.e_rls, SOME "solve (v_v ^^^2 + p_p * v_v + q__q = 0, v_v)", [])),
wneuper@59430
   801
    (Specify.prep_pbt thy "pbl_test_uni_poly_deg2_abc" [] Celem.e_pblID
wneuper@59430
   802
      (["abc_formula","degree_two","polynomial","univariate","equation","test"],
wneuper@59430
   803
        [("#Given" ,["equality (a_a * x ^^^2 + b_b * x + c_c = 0)","solveFor v_v"]),
wneuper@59430
   804
          ("#Find"  ,["solutions v_v'i'"])],
wneuper@59430
   805
        Rule.e_rls, SOME "solve (a_a * x ^^^2 + b_b * x + c_c = 0, v_v)", [])),
wneuper@59430
   806
    (Specify.prep_pbt thy "pbl_test_uni_root" [] Celem.e_pblID
wneuper@59430
   807
      (["squareroot","univariate","equation","test"],
wneuper@59430
   808
        [("#Given" ,["equality e_e","solveFor v_v"]),
wneuper@59430
   809
          ("#Where" ,["precond_rootpbl v_v"]),
wneuper@59430
   810
          ("#Find"  ,["solutions v_v'i'"])],
wneuper@59430
   811
        Rule.append_rls "contains_root" Rule.e_rls [Rule.Calc ("Test.contains'_root",
wneuper@59430
   812
            eval_contains_root "#contains_root_")], 
wneuper@59430
   813
        SOME "solve (e_e::bool, v_v)", [["Test","square_equation"]])),
wneuper@59430
   814
    (Specify.prep_pbt thy "pbl_test_uni_norm" [] Celem.e_pblID
wneuper@59430
   815
      (["normalise","univariate","equation","test"],
wneuper@59430
   816
        [("#Given" ,["equality e_e","solveFor v_v"]),
wneuper@59430
   817
          ("#Where" ,[]),
wneuper@59430
   818
          ("#Find"  ,["solutions v_v'i'"])],
wneuper@59430
   819
        Rule.e_rls, SOME "solve (e_e::bool, v_v)", [["Test","norm_univar_equation"]])),
wneuper@59430
   820
    (Specify.prep_pbt thy "pbl_test_uni_roottest" [] Celem.e_pblID
wneuper@59430
   821
      (["sqroot-test","univariate","equation","test"],
wneuper@59430
   822
        [("#Given" ,["equality e_e","solveFor v_v"]),
wneuper@59430
   823
          ("#Where" ,["precond_rootpbl v_v"]),
wneuper@59430
   824
          ("#Find"  ,["solutions v_v'i'"])],
wneuper@59430
   825
        Rule.e_rls, SOME "solve (e_e::bool, v_v)", [])),
wneuper@59430
   826
    (Specify.prep_pbt thy "pbl_test_intsimp" [] Celem.e_pblID
wneuper@59430
   827
      (["inttype","test"],
wneuper@59430
   828
        [("#Given" ,["intTestGiven t_t"]),
wneuper@59430
   829
          ("#Where" ,[]),
wneuper@59430
   830
          ("#Find"  ,["intTestFind s_s"])],
wneuper@59472
   831
      Rule.e_rls, NONE, [["Test","intsimp"]]))]\<close>
wneuper@59430
   832
wneuper@59472
   833
section \<open>methods\<close>
wneuper@59472
   834
subsection \<open>differentiate\<close>
wneuper@59472
   835
setup \<open>KEStore_Elems.add_mets
wneuper@59473
   836
    [Specify.prep_met @{theory "Diff"} "met_test" [] Celem.e_metID
wneuper@59430
   837
      (["Test"], [],
wneuper@59430
   838
        {rew_ord'="tless_true",rls'=Atools_erls,calc = [], srls = Rule.e_rls, prls=Rule.e_rls,
wneuper@59545
   839
          crls=Atools_erls, errpats = [], nrls = Rule.e_rls}, @{thm refl})]
wneuper@59473
   840
\<close>
wneuper@59545
   841
wneuper@59504
   842
partial_function (tailrec) solve_linear :: "bool \<Rightarrow> real \<Rightarrow> bool list"
wneuper@59504
   843
  where
wneuper@59504
   844
"solve_linear e_e v_v =
wneuper@59504
   845
  (let e_e =
wneuper@59504
   846
    Repeat
wneuper@59504
   847
      (((Rewrite_Set_Inst [(''bdv'', v_v)] ''isolate_bdv'' False) @@
wneuper@59504
   848
        (Rewrite_Set ''Test_simplify'' False))) e_e
wneuper@59504
   849
   in [e_e])"
wneuper@59473
   850
setup \<open>KEStore_Elems.add_mets
wneuper@59473
   851
    [Specify.prep_met thy "met_test_solvelin" [] Celem.e_metID
wneuper@59430
   852
      (["Test","solve_linear"],
wneuper@59430
   853
        [("#Given" ,["equality e_e","solveFor v_v"]),
wneuper@59430
   854
          ("#Where" ,["matches (?a = ?b) e_e"]),
wneuper@59430
   855
          ("#Find"  ,["solutions v_v'i'"])],
wneuper@59430
   856
        {rew_ord' = "e_rew_ord", rls' = tval_rls, srls = Rule.e_rls,
wneuper@59430
   857
          prls = assoc_rls' @{theory} "matches", calc = [], crls = tval_rls, errpats = [],
wneuper@59430
   858
          nrls = Test_simplify},
wneuper@59551
   859
        @{thm solve_linear.simps})]
wneuper@59472
   860
\<close>
wneuper@59472
   861
subsection \<open>root equation\<close>
wneuper@59545
   862
wneuper@59504
   863
partial_function (tailrec) solve_root_equ :: "bool \<Rightarrow> real \<Rightarrow> bool list"
wneuper@59504
   864
  where
wneuper@59504
   865
"solve_root_equ e_e v_v =
wneuper@59504
   866
  (let e_e =
wneuper@59504
   867
     ((While (contains_root e_e) Do
wneuper@59504
   868
        ((Rewrite ''square_equation_left'' True) @@
wneuper@59504
   869
         (Try (Rewrite_Set ''Test_simplify'' False)) @@
wneuper@59504
   870
         (Try (Rewrite_Set ''rearrange_assoc'' False)) @@
wneuper@59504
   871
         (Try (Rewrite_Set ''isolate_root'' False)) @@
wneuper@59504
   872
         (Try (Rewrite_Set ''Test_simplify'' False)))) @@
wneuper@59504
   873
      (Try (Rewrite_Set ''norm_equation'' False)) @@
wneuper@59504
   874
      (Try (Rewrite_Set ''Test_simplify'' False)) @@
wneuper@59504
   875
      (Rewrite_Set_Inst [(''bdv'', v_v)] ''isolate_bdv'' False) @@
wneuper@59504
   876
      (Try (Rewrite_Set ''Test_simplify'' False))) e_e                                                                
wneuper@59504
   877
   in [e_e])"
wneuper@59472
   878
setup \<open>KEStore_Elems.add_mets
wneuper@59473
   879
    [Specify.prep_met thy  "met_test_sqrt" [] Celem.e_metID
wneuper@59430
   880
      (*root-equation, version for tests before 8.01.01*)
wneuper@59430
   881
      (["Test","sqrt-equ-test"],
wneuper@59430
   882
        [("#Given" ,["equality e_e","solveFor v_v"]),
wneuper@59430
   883
          ("#Where" ,["contains_root (e_e::bool)"]),
wneuper@59430
   884
          ("#Find"  ,["solutions v_v'i'"])],
wneuper@59430
   885
        {rew_ord'="e_rew_ord",rls'=tval_rls,
wneuper@59430
   886
          srls = Rule.append_rls "srls_contains_root" Rule.e_rls
wneuper@59430
   887
              [Rule.Calc ("Test.contains'_root",eval_contains_root "")],
wneuper@59430
   888
          prls = Rule.append_rls "prls_contains_root" Rule.e_rls 
wneuper@59430
   889
              [Rule.Calc ("Test.contains'_root",eval_contains_root "")],
wneuper@59430
   890
          calc=[], crls=tval_rls, errpats = [], nrls = Rule.e_rls (*,asm_rls=[],
wneuper@59430
   891
          asm_thm=[("square_equation_left",""), ("square_equation_right","")]*)},
wneuper@59551
   892
        @{thm solve_root_equ.simps})]
wneuper@59472
   893
\<close>
wneuper@59477
   894
wneuper@59545
   895
partial_function (tailrec) minisubpbl ::
wneuper@59477
   896
  "bool \<Rightarrow> real \<Rightarrow> bool list"
wneuper@59477
   897
where "minisubpbl e_e v_v =
wneuper@59477
   898
  (let e_e = ((Try (Rewrite_Set ''norm_equation'' False)) @@
wneuper@59477
   899
              (Try (Rewrite_Set ''Test_simplify'' False))) e_e;
wneuper@59477
   900
      (L_L::bool list) =
wneuper@59546
   901
             SubProblem (''Test'', [''LINEAR'', ''univariate'', ''equation'', ''test''],
wneuper@59504
   902
                 [''Test'', ''solve_linear'']) [BOOL e_e, REAL v_v]
wneuper@59477
   903
   in Check_elementwise L_L {(v_v::real). Assumptions})"
wneuper@59472
   904
setup \<open>KEStore_Elems.add_mets
wneuper@59473
   905
    [Specify.prep_met thy "met_test_squ_sub" [] Celem.e_metID
wneuper@59430
   906
      (*tests subproblem fixed linear*)
wneuper@59430
   907
      (["Test","squ-equ-test-subpbl1"],
wneuper@59430
   908
        [("#Given" ,["equality e_e","solveFor v_v"]),
wneuper@59430
   909
          ("#Where" ,["precond_rootmet v_v"]),
wneuper@59430
   910
          ("#Find"  ,["solutions v_v'i'"])],
wneuper@59430
   911
        {rew_ord' = "e_rew_ord", rls' = tval_rls, srls = Rule.e_rls,
wneuper@59430
   912
          prls = Rule.append_rls "prls_met_test_squ_sub" Rule.e_rls
wneuper@59430
   913
              [Rule.Calc ("Test.precond'_rootmet", eval_precond_rootmet "")],
wneuper@59430
   914
          calc=[], crls=tval_rls, errpats = [], nrls = Test_simplify},
wneuper@59551
   915
        @{thm minisubpbl.simps})]
wneuper@59472
   916
\<close>
wneuper@59545
   917
wneuper@59504
   918
partial_function (tailrec) solve_root_equ2 :: "bool \<Rightarrow> real \<Rightarrow> bool list"
wneuper@59545
   919
  where "solve_root_equ2 e_e v_v =
wneuper@59504
   920
(let e_e =
wneuper@59504
   921
      ((While (contains_root e_e) Do
wneuper@59504
   922
         ((Rewrite ''square_equation_left'' True) @@
wneuper@59504
   923
          (Try (Rewrite_Set ''Test_simplify'' False)) @@
wneuper@59504
   924
          (Try (Rewrite_Set ''rearrange_assoc'' False)) @@
wneuper@59504
   925
          (Try (Rewrite_Set ''isolate_root'' False)) @@
wneuper@59504
   926
          (Try (Rewrite_Set ''Test_simplify'' False)))) @@
wneuper@59504
   927
       (Try (Rewrite_Set ''norm_equation'' False)) @@
wneuper@59504
   928
       (Try (Rewrite_Set ''Test_simplify'' False))) e_e;
wneuper@59504
   929
     L_L = SubProblem (''Test'', [''LINEAR'', ''univariate'', ''equation'', ''test''],
wneuper@59504
   930
             [''Test'', ''solve_linear'']) [BOOL e_e, REAL v_v]
wneuper@59504
   931
  in Check_elementwise L_L {(v_v::real). Assumptions})                                       "
wneuper@59472
   932
setup \<open>KEStore_Elems.add_mets
wneuper@59473
   933
    [Specify.prep_met thy  "met_test_eq1" [] Celem.e_metID
wneuper@59430
   934
      (*root-equation1:*)
wneuper@59430
   935
      (["Test","square_equation1"],
wneuper@59430
   936
        [("#Given" ,["equality e_e","solveFor v_v"]),
wneuper@59430
   937
          ("#Find"  ,["solutions v_v'i'"])],
wneuper@59430
   938
        {rew_ord'="e_rew_ord",rls'=tval_rls,
wneuper@59430
   939
          srls = Rule.append_rls "srls_contains_root" Rule.e_rls 
wneuper@59430
   940
            [Rule.Calc ("Test.contains'_root",eval_contains_root"")], prls=Rule.e_rls, calc=[], crls=tval_rls,
wneuper@59430
   941
              errpats = [], nrls = Rule.e_rls(*,asm_rls=[], asm_thm=[("square_equation_left",""),
wneuper@59430
   942
              ("square_equation_right","")]*)},
wneuper@59551
   943
        @{thm solve_root_equ2.simps})]
wneuper@59472
   944
\<close>
wneuper@59545
   945
wneuper@59504
   946
partial_function (tailrec) solve_root_equ3 :: "bool \<Rightarrow> real \<Rightarrow> bool list"
wneuper@59504
   947
  where
wneuper@59545
   948
"solve_root_equ3 e_e v_v =
wneuper@59504
   949
  (let e_e =
wneuper@59504
   950
        ((While (contains_root e_e) Do
wneuper@59504
   951
           (((Rewrite ''square_equation_left'' True) Or
wneuper@59504
   952
             (Rewrite ''square_equation_right'' True)) @@
wneuper@59504
   953
            (Try (Rewrite_Set ''Test_simplify'' False)) @@
wneuper@59504
   954
            (Try (Rewrite_Set ''rearrange_assoc'' False)) @@
wneuper@59504
   955
            (Try (Rewrite_Set ''isolate_root'' False)) @@
wneuper@59504
   956
            (Try (Rewrite_Set ''Test_simplify'' False)))) @@
wneuper@59504
   957
         (Try (Rewrite_Set ''norm_equation'' False)) @@
wneuper@59504
   958
         (Try (Rewrite_Set ''Test_simplify'' False))) e_e;
wneuper@59504
   959
       L_L = SubProblem (''Test'', [''plain_square'', ''univariate'', ''equation'', ''test''],
wneuper@59504
   960
               [''Test'', ''solve_plain_square'']) [BOOL e_e, REAL v_v]
wneuper@59504
   961
    in Check_elementwise L_L {(v_v::real). Assumptions})"
wneuper@59472
   962
setup \<open>KEStore_Elems.add_mets
wneuper@59473
   963
    [Specify.prep_met thy "met_test_squ2" [] Celem.e_metID
wneuper@59430
   964
      (*root-equation2*)
wneuper@59430
   965
        (["Test","square_equation2"],
wneuper@59430
   966
          [("#Given" ,["equality e_e","solveFor v_v"]),
wneuper@59430
   967
          ("#Find"  ,["solutions v_v'i'"])],
wneuper@59430
   968
        {rew_ord'="e_rew_ord",rls'=tval_rls,
wneuper@59430
   969
          srls = Rule.append_rls "srls_contains_root" Rule.e_rls 
wneuper@59430
   970
              [Rule.Calc ("Test.contains'_root",eval_contains_root"")],
wneuper@59430
   971
          prls=Rule.e_rls,calc=[], crls=tval_rls, errpats = [], nrls = Rule.e_rls(*,asm_rls=[],
wneuper@59430
   972
          asm_thm=[("square_equation_left",""), ("square_equation_right","")]*)},
wneuper@59551
   973
        @{thm solve_root_equ3.simps})]
wneuper@59472
   974
\<close>
wneuper@59545
   975
wneuper@59504
   976
partial_function (tailrec) solve_root_equ4 :: "bool \<Rightarrow> real \<Rightarrow> bool list"
wneuper@59504
   977
  where
wneuper@59545
   978
"solve_root_equ4 e_e v_v =
wneuper@59504
   979
(let e_e =
wneuper@59504
   980
      ((While (contains_root e_e) Do
wneuper@59504
   981
         (((Rewrite ''square_equation_left'' True) Or
wneuper@59504
   982
           (Rewrite ''square_equation_right'' True)) @@
wneuper@59504
   983
          (Try (Rewrite_Set ''Test_simplify'' False)) @@
wneuper@59504
   984
          (Try (Rewrite_Set ''rearrange_assoc'' False)) @@
wneuper@59504
   985
          (Try (Rewrite_Set ''isolate_root'' False)) @@
wneuper@59504
   986
          (Try (Rewrite_Set ''Test_simplify'' False)))) @@
wneuper@59504
   987
       (Try (Rewrite_Set ''norm_equation'' False)) @@
wneuper@59504
   988
       (Try (Rewrite_Set ''Test_simplify'' False))) e_e;
wneuper@59504
   989
     L_L = SubProblem (''Test'', [''univariate'', ''equation'', ''test''],
wneuper@59504
   990
             [''no_met'']) [BOOL e_e, REAL v_v]
wneuper@59504
   991
  in Check_elementwise L_L {(v_v::real). Assumptions})"
wneuper@59472
   992
setup \<open>KEStore_Elems.add_mets
wneuper@59473
   993
    [Specify.prep_met thy "met_test_squeq" [] Celem.e_metID
wneuper@59430
   994
      (*root-equation*)
wneuper@59430
   995
      (["Test","square_equation"],
wneuper@59430
   996
        [("#Given" ,["equality e_e","solveFor v_v"]),
wneuper@59430
   997
          ("#Find"  ,["solutions v_v'i'"])],
wneuper@59430
   998
        {rew_ord'="e_rew_ord",rls'=tval_rls,
wneuper@59430
   999
          srls = Rule.append_rls "srls_contains_root" Rule.e_rls 
wneuper@59430
  1000
              [Rule.Calc ("Test.contains'_root",eval_contains_root"")],
wneuper@59430
  1001
          prls=Rule.e_rls,calc=[], crls=tval_rls, errpats = [], nrls = Rule.e_rls (*,asm_rls=[],
wneuper@59430
  1002
          asm_thm=[("square_equation_left",""), ("square_equation_right","")]*)},
wneuper@59551
  1003
        @{thm solve_root_equ4.simps})]
wneuper@59472
  1004
\<close>
wneuper@59545
  1005
wneuper@59504
  1006
partial_function (tailrec) solve_plain_square :: "bool \<Rightarrow> real \<Rightarrow> bool list"
wneuper@59504
  1007
  where
wneuper@59504
  1008
"solve_plain_square e_e v_v =         
wneuper@59504
  1009
 (let e_e = ((Try (Rewrite_Set ''isolate_bdv'' False)) @@
wneuper@59504
  1010
             (Try (Rewrite_Set ''Test_simplify'' False))    @@
wneuper@59504
  1011
             ((Rewrite ''square_equality_0'' False) Or
wneuper@59504
  1012
              (Rewrite ''square_equality'' True)) @@
wneuper@59504
  1013
             (Try (Rewrite_Set ''tval_rls'' False))) e_e
wneuper@59504
  1014
  in Or_to_List e_e)"
wneuper@59472
  1015
setup \<open>KEStore_Elems.add_mets
wneuper@59473
  1016
    [Specify.prep_met thy "met_test_eq_plain" [] Celem.e_metID
wneuper@59430
  1017
      (*solve_plain_square*)
wneuper@59430
  1018
      (["Test","solve_plain_square"],
wneuper@59430
  1019
        [("#Given",["equality e_e","solveFor v_v"]),
wneuper@59430
  1020
          ("#Where" ,["(matches (?a + ?b*v_v ^^^2 = 0) e_e) |" ^
wneuper@59430
  1021
              "(matches (     ?b*v_v ^^^2 = 0) e_e) |" ^
wneuper@59430
  1022
              "(matches (?a +    v_v ^^^2 = 0) e_e) |" ^
wneuper@59430
  1023
              "(matches (        v_v ^^^2 = 0) e_e)"]), 
wneuper@59430
  1024
          ("#Find"  ,["solutions v_v'i'"])],
wneuper@59430
  1025
        {rew_ord'="e_rew_ord",rls'=tval_rls,calc=[],srls=Rule.e_rls,
wneuper@59430
  1026
          prls = assoc_rls' @{theory} "matches", crls=tval_rls, errpats = [], nrls = Rule.e_rls(*,
wneuper@59430
  1027
          asm_rls=[],asm_thm=[]*)},
wneuper@59551
  1028
        @{thm solve_plain_square.simps})]
wneuper@59472
  1029
\<close>
wneuper@59472
  1030
subsection \<open>polynomial equation\<close>
wneuper@59545
  1031
wneuper@59504
  1032
partial_function (tailrec) norm_univariate_equ :: "bool \<Rightarrow> real \<Rightarrow> bool list"
wneuper@59504
  1033
  where
wneuper@59504
  1034
"norm_univariate_equ e_e v_v =
wneuper@59504
  1035
 (let e_e = ((Try (Rewrite ''rnorm_equation_add'' False)) @@
wneuper@59504
  1036
             (Try (Rewrite_Set ''Test_simplify'' False))) e_e
wneuper@59504
  1037
  in SubProblem (''Test'', [''univariate'', ''equation'', ''test''],
wneuper@59504
  1038
        [''no_met'']) [BOOL e_e, REAL v_v])"
wneuper@59472
  1039
setup \<open>KEStore_Elems.add_mets
wneuper@59473
  1040
    [Specify.prep_met thy "met_test_norm_univ" [] Celem.e_metID
wneuper@59430
  1041
      (["Test","norm_univar_equation"],
wneuper@59430
  1042
        [("#Given",["equality e_e","solveFor v_v"]),
wneuper@59430
  1043
          ("#Where" ,[]), 
wneuper@59430
  1044
          ("#Find"  ,["solutions v_v'i'"])],
wneuper@59430
  1045
        {rew_ord'="e_rew_ord",rls'=tval_rls,srls = Rule.e_rls,prls=Rule.e_rls, calc=[], crls=tval_rls,
wneuper@59430
  1046
          errpats = [], nrls = Rule.e_rls},
wneuper@59551
  1047
        @{thm norm_univariate_equ.simps})]
wneuper@59472
  1048
\<close>
wneuper@59472
  1049
subsection \<open>diophantine equation\<close>
wneuper@59545
  1050
wneuper@59504
  1051
partial_function (tailrec) test_simplify :: "int \<Rightarrow> int"
wneuper@59504
  1052
  where
wneuper@59504
  1053
"test_simplify t_t =           
wneuper@59504
  1054
  (Repeat                                    
wneuper@59504
  1055
      ((Try (Calculate ''PLUS'')) @@         
wneuper@59504
  1056
       (Try (Calculate ''TIMES''))) t_t)"
wneuper@59472
  1057
setup \<open>KEStore_Elems.add_mets
wneuper@59473
  1058
    [Specify.prep_met thy "met_test_intsimp" [] Celem.e_metID
wneuper@59430
  1059
      (["Test","intsimp"],
wneuper@59430
  1060
        [("#Given" ,["intTestGiven t_t"]),
wneuper@59430
  1061
          ("#Where" ,[]),
wneuper@59430
  1062
          ("#Find"  ,["intTestFind s_s"])],
wneuper@59430
  1063
        {rew_ord' = "e_rew_ord", rls' = tval_rls, srls = Rule.e_rls,  prls = Rule.e_rls, calc = [],
wneuper@59430
  1064
          crls = tval_rls, errpats = [], nrls = Test_simplify},
wneuper@59551
  1065
        @{thm test_simplify.simps})]
wneuper@59472
  1066
\<close>
wneuper@59430
  1067
neuper@37906
  1068
end