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