test/Tools/isac/ProgLang/evaluate.sml
author wenzelm
Fri, 24 Sep 2021 14:18:10 +0200
changeset 60407 eca042e683b9
parent 60405 d4ebe139100d
child 60424 c3acf9c442ac
permissions -rw-r--r--
proper simp_ctxt;
walther@59902
     1
(* Title:  "ProgLang/evaluate.sml"
walther@60278
     2
           test calculation of values for function constants
neuper@38022
     3
   Author: Walther Neuper 2000
neuper@38022
     4
   (c) copyright due to lincense terms.
neuper@37906
     5
*)
neuper@37906
     6
neuper@38022
     7
"--------------------------------------------------------";
neuper@38022
     8
"table of contents --------------------------------------";
neuper@38022
     9
"--------------------------------------------------------";
neuper@55366
    10
"-calculate.thy: provides 'setup' -----------------------";
neuper@41924
    11
"----------- fun norm -----------------------------------";
wneuper@59255
    12
"----------- check return value of adhoc_thm  ----";
wneuper@59411
    13
"----------- fun calculate_ --------------------------------------------------------------------";
wneuper@59411
    14
"----------- calculate from Prog --------------------------------- -----------------------------";
neuper@55366
    15
"----------- calculate from script --requires 'setup'----";
neuper@38032
    16
"----------- calculate check test-root-equ --------------";
walther@60317
    17
"----------- check calculate bottom up -----------------";
walther@59603
    18
"----------- Prog_Expr.pow Power.power_class.power ---------";
walther@60317
    19
"----------- fun cancel_int --------------------------------------------------------------------";
walther@60317
    20
"----------- RE-BUILD fun calcul ---------------------------------------------------------------";
neuper@38032
    21
"----------- get_pair with 3 args -----------------------";
walther@60387
    22
"----------- calculate (2 * x is_num) -------------------";
wneuper@59387
    23
"----------- fun get_pair: examples ------------------------------------------------------------";
wneuper@59387
    24
"----------- fun adhoc_thm: examples -----------------------------------------------------------";
walther@60391
    25
"----------- fun adhoc_thm + fun eval_cancel ---------------------------------------------------";
walther@60357
    26
"----------- fun adhoc_thm \<longrightarrow> exception TYPE --------------------------------------------------";
wneuper@59403
    27
"----------- fun power -------------------------------------------------------------------------";
wneuper@59403
    28
"----------- fun divisors ----------------------------------------------------------------------";
wneuper@59403
    29
"----------- fun doubles, fun squfact ----------------------------------------------------------";
neuper@38022
    30
"--------------------------------------------------------";
neuper@38022
    31
"--------------------------------------------------------";
neuper@38022
    32
"--------------------------------------------------------";
neuper@37906
    33
wneuper@59255
    34
"----------- check return value of adhoc_thm  ----";
wneuper@59255
    35
"----------- check return value of adhoc_thm  ----";
wneuper@59255
    36
"----------- check return value of adhoc_thm  ----";
neuper@41924
    37
val thy = @{theory "Poly"};
s1210629013@52153
    38
val cal = snd (assoc_calc' thy "is_polyexp");
neuper@38022
    39
val t = @{term "(x / x) is_polyexp"};
wneuper@59255
    40
val SOME (thmID, thm) = adhoc_thm thy cal t;
wneuper@59188
    41
(HOLogic.dest_Trueprop (Thm.prop_of thm); writeln "all thms wrapped by Trueprop")
neuper@38022
    42
handle TERM _ => 
walther@59902
    43
       error "evaluate.sml: adhoc_thm must return Trueprop";
neuper@38022
    44
wneuper@59411
    45
"----------- fun calculate_ --------------------------------------------------------------------";
wneuper@59411
    46
"----------- fun calculate_ --------------------------------------------------------------------";
wneuper@59411
    47
"----------- fun calculate_ --------------------------------------------------------------------";
wneuper@59411
    48
(* fun rewrite__set_ \<longrightarrow> fun rew_once works the same way *)
walther@60242
    49
val t = TermC.str2term "((1+2)*4/3) \<up> 2";
wneuper@59411
    50
val thy = @{theory};
walther@59919
    51
val times =  ("Groups.times_class.times", eval_binop "#mult_") : string * Eval_Def.eval_fn;
walther@60317
    52
val plus =   ("Groups.plus_class.plus", eval_binop "#add_") : string * Eval_Def.eval_fn;
walther@60317
    53
val divide = ("Rings.divide_class.divide", eval_cancel "#divide_e") : string * Eval_Def.eval_fn;
wenzelm@60405
    54
val pow =    (\<^const_name>\<open>realpow\<close>, eval_binop "#power_") : string * Eval_Def.eval_fn;
wneuper@59411
    55
walther@60317
    56
"~~~~~ fun calculate_ , args:"; val (thy, isa_fn, t) = (thy, plus, t);
wneuper@59411
    57
val SOME ("#: 1 + 2 = 3", adh_thm) = adhoc_thm @{theory} isa_fn t;
walther@59852
    58
val SOME (t', []) = rewrite__ thy 0 [] e_rew_ord Rule_Set.empty true adh_thm t;
walther@60242
    59
if UnparseC.term t' = "(3 * 4 / 3) \<up> 2" then () else error "calculate_  1 + 2 = 3  changed";
wneuper@59411
    60
wneuper@59411
    61
"~~~~~ fun calculate_, args:"; val (thy, isa_fn, t) = (thy, times, t');
wneuper@59411
    62
val SOME ("#: 3 * 4 = 12", adh_thm) = adhoc_thm @{theory} isa_fn t';
walther@59852
    63
val SOME (t'', []) = rewrite__ thy 0 [] e_rew_ord Rule_Set.empty true adh_thm t;
walther@60242
    64
if UnparseC.term t'' = "(12 / 3) \<up> 2" then () else error "calculate_  3 * 4 = 12  changed";
wneuper@59411
    65
wneuper@59411
    66
"~~~~~ fun calculate_, args:"; val (thy, isa_fn, t) = (thy, divide, t'');
wneuper@59411
    67
val SOME ("#divide_e12_3", adh_thm) = adhoc_thm @{theory} isa_fn t;
walther@59852
    68
val SOME (t''', []) = rewrite__ thy 0 [] e_rew_ord Rule_Set.empty true adh_thm t;
walther@60242
    69
if UnparseC.term t''' = "4 \<up> 2" then () else error "calculate_  12 / 3 = 4  changed";
wneuper@59411
    70
wneuper@59411
    71
"~~~~~ fun calculate_, args:"; val (thy, isa_fn, t) = (thy, pow, t''');
walther@60242
    72
val SOME ("#: 4 \<up> 2 = 16", adh_thm) = adhoc_thm @{theory} isa_fn t;
walther@59852
    73
val SOME (t'''', []) = rewrite__ thy 0 [] e_rew_ord Rule_Set.empty true adh_thm t;
walther@59868
    74
if UnparseC.term t'''' = "16" then () else error "calculate_  12 / 3 = 4  changed";
wneuper@59411
    75
wneuper@59411
    76
"----------- calculate from Prog --------------------------------- -----------------------------";
wneuper@59411
    77
"----------- calculate from Prog --------------------------------- -----------------------------";
wneuper@59411
    78
"----------- calculate from Prog --------------------------------- -----------------------------";
neuper@41924
    79
val thy = @{theory "Test"};
walther@60242
    80
val fmz = ["realTestGiven (((1+2)*4/3) \<up> 2)", "realTestFind s"];
neuper@37906
    81
val (dI',pI',mI') =
walther@60317
    82
  ("Test", ["calculate", "test"], ["Test", "test_calculate"]);
neuper@38035
    83
neuper@37906
    84
val (p,_,f,nxt,_,pt) = CalcTreeTEST [(fmz, (dI',pI',mI'))];
neuper@37906
    85
val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
walther@60242
    86
(*nxt =("Add_Given",Add_Given "realTestGiven (((#1 + #2) * #4 // #3)  \<up> #2)")*)
neuper@37906
    87
val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
neuper@37906
    88
(*nxt = ("Add_Find",Add_Find "realTestFind s") : string * tac*)
neuper@37906
    89
val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
neuper@38035
    90
(*nxt = ("Specify_Theory",Specify_Theory "Test") : string * tac*)
neuper@37906
    91
val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
walther@59997
    92
(*nxt = ("Specify_Problem",Specify_Problem ["calculate", "test"])*)
neuper@37906
    93
val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
walther@59997
    94
(*nxt = ("Specify_Method",Specify_Method ("Test", "test_calculate"))*)
neuper@37906
    95
val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
walther@59997
    96
(*nxt = ("Apply_Method",Apply_Method ("Test", "test_calculate"))*)
neuper@37906
    97
walther@59713
    98
(*[1], Frm*)val (p,_,f,nxt,_,pt) = me nxt p [1] pt; (*nxt = ("Calculate",Calculate "PLUS")*)
walther@59713
    99
(*[1], Res*)val (p,_,f,nxt,_,pt) = me nxt p [1] pt; (*nxt = ("Calculate",Calculate "TIMES")*)
walther@59713
   100
(*[2], Res*)val (p,_,f,nxt,_,pt) = me nxt p [1] pt; (*nxt = ("Calculate",Calculate "DIVIDE")*)
walther@59713
   101
(*[3], Res*)val (p,_,f,nxt,_,pt) = me nxt p [1] pt; (*nxt = ("Calculate",Calculate "POWER")*)
walther@59997
   102
(*[4], Res*)val (p,_,f,nxt,_,pt) = me nxt p [1] pt; (*nxt = ("Check_Postcond",Check_Postcond ["calculate", "test"])*)
walther@59713
   103
(*[], Res*)val (p,_,f,nxt,_,pt) = me nxt p [1] pt; (*nxt = ("End_Proof'",End_Proof')*)
walther@59959
   104
case f of Test_Out.FormKF "16" => () | _ =>
walther@59902
   105
error "evaluate.sml: script test_calculate changed behaviour";
neuper@37906
   106
neuper@37906
   107
neuper@38032
   108
"----------- calculate check test-root-equ --------------";
neuper@38032
   109
"----------- calculate check test-root-equ --------------";
neuper@38032
   110
"----------- calculate check test-root-equ --------------";
neuper@37906
   111
(*(1): 2nd Test_simplify didn't work:
neuper@37906
   112
val ct =
walther@60329
   113
  "sqrt (x \<up> 2 + -3 * x) = (-3 + 2 * x + - 1 * (9 + 4 * x)) / (- 1 * 2)"
neuper@37906
   114
> val rls = ("Test_simplify");
neuper@37906
   115
> val (ct,_) = the (rewrite_set thy' ("tval_rls") false rls ct);
walther@60242
   116
val ct = "sqrt (x \<up> 2 + -3 * x) =
walther@60329
   117
(-9) / (- 2) + (-3 / (- 2) + (x * ((-4) / (- 2)) + x * (2 / (- 2))))";
neuper@37906
   118
ie. cancel does not work properly
neuper@37906
   119
*)
wneuper@59382
   120
 val thy = @{theory "Test"};
walther@59686
   121
 val op_ = the (LibraryC.assoc (KEStore_Elems.get_calcs @{theory}, "DIVIDE"));
walther@60337
   122
 val ct = @{term
walther@60329
   123
   "sqrt (x \<up> 2 + -3 * x) = (-9) / (- 2) + (-3 / (- 2) + (x * ((-4) / (- 2)) + x * (2 / (- 2))))"};
wneuper@59382
   124
case calculate_ thy op_ ct of
wneuper@59382
   125
  SOME _ => ()
wneuper@59382
   126
| NONE => error "calculate_ test-root-equ changed";
neuper@37906
   127
(*
walther@60242
   128
           sqrt (x \<up> 2 + -3 * x) =\
walther@60329
   129
 \(-9) / (- 2) + (-3 / (- 2) + (x * ((-4) / (- 2)) + x * (2 / (- 2))))
neuper@37906
   130
............... does not work *)
neuper@37906
   131
neuper@37906
   132
(*--------------(2): does divide work in Test_simplify ?: ------*)
akargl@42188
   133
 val thy = @{theory Test};
walther@60340
   134
 val t = (Thm.term_of o the o (TermC.parse thy)) "6 / 2";
neuper@37906
   135
 val rls = Test_simplify;
neuper@37906
   136
 val (t,_) = the (rewrite_set_ thy false rls t);
walther@59997
   137
(*val t = Free ("3", "Real.real") : term*)
neuper@37906
   138
walther@60387
   139
(*--------------(3): is_num works ?: -------------------------------------*)
walther@60387
   140
 val t = (Thm.term_of o the o (TermC.parse @{theory Test})) "2 is_num";
walther@60230
   141
 TermC.atomty t;
akargl@42188
   142
 rewrite_set_ @{theory Test} false tval_rls t;
wenzelm@60309
   143
(*val it = SOME (Const (\<^const_name>\<open>True\<close>, "bool"),[]) ... works*)
neuper@37906
   144
walther@60387
   145
 val t = TermC.str2term "2 * x is_num";
walther@60387
   146
 val NONE = eval_is_num "" "" t (@{theory "Isac_Knowledge"});
neuper@37906
   147
 
neuper@37906
   148
neuper@38032
   149
"----------- check calculate bottom up ------------------";
neuper@38032
   150
"----------- check calculate bottom up ------------------";
neuper@38032
   151
"----------- check calculate bottom up ------------------";
neuper@37906
   152
(*-------------- eval_cancel works: *)
walther@60330
   153
 Rewrite.trace_on := false; (*true false*)
akargl@42188
   154
 val thy = @{theory Test};
wneuper@59384
   155
 val rls = Test_simplify;
walther@60340
   156
 val t = (Thm.term_of o the o (TermC.parse thy)) "(-4) / 2";
akargl@42188
   157
wenzelm@60309
   158
val SOME (_, t) = eval_cancel "xxx" \<^const_name>\<open>divide\<close> t thy;
akargl@42188
   159
neuper@37906
   160
(*--------------(5): reproduce (1) with simpler term: ------------*)
walther@60340
   161
 val t = (Thm.term_of o the o (TermC.parse thy)) "(3+5)/2";
wneuper@59384
   162
case rewrite_set_ thy false rls t of
walther@60317
   163
  SOME (t', []) =>
walther@60317
   164
    if UnparseC.term t' = "4" then ()
walther@60317
   165
    else error "rewrite_set_ (3+5)/2 changed 1"
walther@60317
   166
| _ => error "rewrite_set_ (3+5)/2 changed 2";
neuper@37906
   167
walther@60340
   168
 val t = (Thm.term_of o the o (TermC.parse thy)) "(3+1+2*x)/2";
wneuper@59384
   169
case rewrite_set_ thy false rls t of
walther@60356
   170
  SOME (t', _) =>
walther@60324
   171
    if UnparseC.term t' = "2 + x" then () else error "rewrite_set_ (3+1+2*x)/2 changed 1"
walther@60317
   172
| _ => error "rewrite_set_ (3+1+2*x)/2 changed 2";
neuper@37906
   173
walther@60330
   174
 Rewrite.trace_on := false; (*true false*)
akargl@42218
   175
walther@59901
   176
(*--- Rewrite.trace_on before correction of ... --------------------
walther@60329
   177
 val ct = "(-3 + 2 * x + - 1) / 2";
neuper@37906
   178
 val (ct,_) = the (rewrite_set thy'  false rls ct);
neuper@37906
   179
:
neuper@37906
   180
### trying thm 'root_ge0_2'
walther@60329
   181
### rewrite_set_: x + (- 1 + -3) / 2
neuper@37906
   182
### trying thm 'radd_real_const_eq'
neuper@37906
   183
### trying thm 'radd_real_const'
neuper@37906
   184
### rewrite_set_: x + (-4) / 2
neuper@37906
   185
### trying thm 'rcollect_right'
neuper@37906
   186
:
neuper@37906
   187
"x + (-4) / 2"
neuper@37906
   188
-------------------------------------while before Isabelle20002:
walther@60329
   189
 val ct = "(#-3 + #2 * x + #- 1) // #2";
neuper@37906
   190
 val (ct,_) = the (rewrite_set thy'  false rls ct);
neuper@37906
   191
:
neuper@37906
   192
### trying thm 'root_ge0_2'
walther@60329
   193
### rewrite_set_: x + (#- 1 + #-3) // #2
neuper@37906
   194
### trying thm 'radd_real_const_eq'
neuper@37906
   195
### trying thm 'radd_real_const'
neuper@37906
   196
### rewrite_set_: x + #-4 // #2
walther@60329
   197
### rewrite_set_: x + #- 2
neuper@37906
   198
### trying thm 'rcollect_right'
neuper@37906
   199
:
walther@60329
   200
"#- 2 + x"
neuper@37906
   201
-----------------------------------------------------------------*)
neuper@37906
   202
neuper@37906
   203
neuper@37906
   204
(*===================*)
walther@59901
   205
 Rewrite.trace_on:=false; (*WN130722: =true stopped Test_Isac.thy*)
walther@60340
   206
 val t = (Thm.term_of o the o (TermC.parse thy))  "x + (- 1 + -3) / 2";
wneuper@59384
   207
val SOME (res, []) = rewrite_set_ thy false rls t;
walther@60329
   208
if UnparseC.term res = "- 2 + x" then () else error "rewrite_set_  x + (- 1 + -3) / 2  changed";
neuper@37906
   209
"x + (-4) / 2";						
neuper@37906
   210
(*
neuper@37906
   211
### trying calc. 'cancel'
neuper@37906
   212
@@@ get_pair: binop, t = x + (-4) / 2
neuper@37906
   213
@@@ get_pair: t else
neuper@38032
   214
@@@ get_pair: t else -> NONE
neuper@37906
   215
@@@ get_pair: binop, t = (-4) / 2
neuper@37906
   216
@@@ get_pair: then 1
neuper@38032
   217
@@@ get_pair: t -> NONE
neuper@38032
   218
@@@ get_pair: t1 -> NONE
wneuper@59255
   219
@@@ adhoc_thm': NONE
neuper@37906
   220
### trying calc. 'pow'
neuper@37906
   221
*)
neuper@37906
   222
walther@59901
   223
 Rewrite.trace_on:=false; (*WN130722: =true stopped Test_Isac.thy*)
neuper@37906
   224
walther@59902
   225
" ================= evaluate.sml: calculate_ 2002 =================== ";
walther@59902
   226
" ================= evaluate.sml: calculate_ 2002 =================== ";
walther@59902
   227
" ================= evaluate.sml: calculate_ 2002 =================== ";
neuper@37906
   228
akargl@42188
   229
val thy = @{theory Test};
walther@60340
   230
val t = (Thm.term_of o the o (TermC.parse thy)) "12 / 3";
walther@59686
   231
val SOME (thmID,thm) = adhoc_thm thy(the(LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"DIVIDE")))t;
neuper@38032
   232
val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
neuper@37906
   233
"12 / 3 = 4";
akargl@42188
   234
val thy = @{theory Test};
walther@60340
   235
val t = (Thm.term_of o the o (TermC.parse thy)) "4 \<up> 2";
walther@59686
   236
val SOME (thmID,thm) = adhoc_thm thy(the(LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"POWER"))) t;
neuper@38032
   237
val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
neuper@37906
   238
"4 ^ 2 = 16";
neuper@37906
   239
walther@60340
   240
 val t = (Thm.term_of o the o (TermC.parse thy)) "((1 + 2) * 4 / 3) \<up> 2";
walther@59686
   241
 val SOME (thmID,thm) = adhoc_thm thy (the(LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"PLUS"))) t;
neuper@37906
   242
"1 + 2 = 3";
neuper@38032
   243
 val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
walther@59868
   244
 UnparseC.term t;
walther@60242
   245
"(3 * 4 / 3) \<up> 2";
walther@59686
   246
 val SOME (thmID,thm) = adhoc_thm thy (the(LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"TIMES")))t;
neuper@37906
   247
"3 * 4 = 12";
neuper@38032
   248
 val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
walther@59868
   249
 UnparseC.term t;
walther@60242
   250
"(12 / 3) \<up> 2";
walther@59686
   251
 val SOME (thmID,thm) =adhoc_thm thy(the(LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"DIVIDE")))t;
neuper@37906
   252
"12 / 3 = 4";
neuper@38032
   253
 val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
walther@59868
   254
 UnparseC.term t;
walther@60242
   255
"4 \<up> 2";
walther@59686
   256
 val SOME (thmID,thm) = adhoc_thm thy(the(LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"POWER")))t;
walther@60242
   257
"4 \<up> 2 = 16";
neuper@38032
   258
 val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
walther@59868
   259
 UnparseC.term t;
neuper@37906
   260
"16";
walther@59902
   261
 if it <> "16" then error "evaluate.sml: new behaviour in calculate_"
neuper@37906
   262
 else ();
neuper@37906
   263
neuper@37906
   264
(*13.9.02 *** calc: operator = pow not defined*)
walther@60340
   265
  val t = (Thm.term_of o the o (TermC.parse thy)) "3 \<up> 2";
neuper@38032
   266
  val SOME (thmID,thm) = 
walther@59686
   267
      adhoc_thm thy (the(LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"POWER"))) t;
neuper@37906
   268
(*** calc: operator = pow not defined*)
neuper@37906
   269
walther@59686
   270
  val (op_, eval_fn) = the (LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"POWER"));
neuper@37906
   271
  (*
wenzelm@60405
   272
val op_ = \<^const_name>\<open>realpow\<close> : string
neuper@37906
   273
val eval_fn = fn : string -> term -> theory -> (string * term) option*)
neuper@37906
   274
neuper@38032
   275
  val SOME (thmid,t') = get_pair thy op_ eval_fn t;
neuper@37906
   276
(*** calc: operator = pow not defined*)
neuper@37906
   277
neuper@38032
   278
  val SOME (id,t') = eval_fn op_ t thy;
neuper@37906
   279
(*** calc: operator = pow not defined*)
neuper@37906
   280
walther@60317
   281
case (op_, t) of
wenzelm@60405
   282
  (\<^const_name>\<open>realpow\<close>,
wenzelm@60405
   283
    Const (\<^const_name>\<open>realpow\<close>, _) $ (Const (\<^const_name>\<open>numeral\<close>, _) $ (Const (\<^const_name>\<open>num.Bit1\<close>, _) $ Const (\<^const_name>\<open>num.One\<close>, _))) $
walther@60336
   284
      (Const (\<^const_name>\<open>numeral\<close>, _) $ (Const (\<^const_name>\<open>num.Bit0\<close>, _) $ Const (\<^const_name>\<open>num.One\<close>, _)))) => ()
walther@60317
   285
| _ => error "3 \<up> 2 CHANGED";
walther@60317
   286
  val SOME (id, t') = eval_binop thmid op_ t thy;
neuper@37906
   287
(*** calc: operator = pow not defined*)
neuper@37906
   288
walther@60317
   289
if UnparseC.term t' = "3 \<up> 2 = 9" then () else error "eval_binop  3 \<up> 2 = 9  CHANGED";
walther@60317
   290
walther@60317
   291
walther@60317
   292
"----------- fun cancel_int --------------------------------------------------------------------";
walther@60317
   293
"----------- fun cancel_int --------------------------------------------------------------------";
walther@60317
   294
"----------- fun cancel_int --------------------------------------------------------------------";
walther@60317
   295
if cancel_int (~4, 2) = (~1, (2, 1)) then () else error "cancel_int (~4, 2) CHANGED";
walther@60317
   296
if cancel_int (4, ~8) = (~1, (1, 2)) then () else error "cancel_int (4, ~8) CHANGED";
walther@60317
   297
if cancel_int (6, 4) = (1, (3, 2)) then () else error "cancel_int (6, 4)CHANGED";
walther@60317
   298
walther@60317
   299
walther@60317
   300
"----------- RE-BUILD fun calcul ---------------------------------------------------------------";
walther@60317
   301
"----------- RE-BUILD fun calcul ---------------------------------------------------------------";
walther@60317
   302
"----------- RE-BUILD fun calcul ---------------------------------------------------------------";
walther@60401
   303
val t = @{term "2 + 3 ::real"};
walther@60317
   304
walther@60401
   305
"~~~~~ fun calcul , args:"; val (thy, lhs) = (@{theory}, t);
walther@60401
   306
    val simp_ctxt =
walther@60401
   307
      Proof_Context.init_global thy
wenzelm@60407
   308
      |> put_simpset (Simplifier.simpset_of @{theory_context BaseDefinitions});
walther@60401
   309
    val eq = Simplifier.rewrite simp_ctxt (Thm.global_cterm_of thy lhs);
walther@60317
   310
walther@60401
   311
if ThmC.string_of_thm eq = "2 + 3 \<equiv> 5" then () else error "calcul 1";
walther@60317
   312
walther@60401
   313
    val rhs = Thm.term_of (Thm.rhs_of eq);
walther@60401
   314
    val _ = \<^assert> (is_num rhs);
walther@60317
   315
walther@60401
   316
(*return value*) rhs;
walther@60401
   317
if TermC.to_string rhs = "5" then () else error "calcul 2";
walther@60317
   318
akargl@42188
   319
neuper@37906
   320
"----------- get_pair with 3 args --------------------------------";
neuper@37906
   321
"----------- get_pair with 3 args --------------------------------";
neuper@37906
   322
"----------- get_pair with 3 args --------------------------------";
neuper@37906
   323
val (thy, op_, ef, arg) =
walther@60278
   324
    (thy, "EqSystem.occur_exactly_in", 
wneuper@59462
   325
     assoc_calc' (@{theory "EqSystem"}) "occur_exactly_in" |> snd |> snd,
walther@60230
   326
     TermC.str2term
walther@60329
   327
      "[] from [c, c_2, c_3, c_4] occur_exactly_in - 1 * (q_0 * L \<up> 2) / 2"
neuper@37906
   328
      );
neuper@38032
   329
val SOME (str, simpl) = get_pair thy op_ ef arg;
neuper@37906
   330
if str = 
walther@60317
   331
"[] from [c, c_2, c_3, c_4] occur_exactly_in - 1 * (q_0 * L \<up> 2) / 2 = True"
walther@59902
   332
then () else error "evaluate.sml get_pair with 3 args:occur_exactly_in";
neuper@37906
   333
neuper@37906
   334
walther@60387
   335
"----------- calculate (2 * x is_num) -------------------";
walther@60387
   336
"----------- calculate (2 * x is_num) -------------------";
walther@60387
   337
"----------- calculate (2 * x is_num) -------------------";
walther@60387
   338
val t = TermC.str2term "(2::real) * x is_num";
walther@60387
   339
walther@60387
   340
val SOME (str, t') = eval_is_num "" "Prog_Expr.is_num" t @{theory Test};
walther@60387
   341
if UnparseC.term t' = "(2 * x is_num) = False" then ()
walther@60387
   342
else error "is_num 2 * x is_num CHANGED";
neuper@37906
   343
akargl@42188
   344
val SOME (t',_) = rewrite_set_ @{theory Test} false tval_rls t;
walther@60317
   345
if UnparseC.term t' = "False" then ()
walther@60387
   346
else error "rewrite_set_ 2 * x is_num CHANGED";
wneuper@59387
   347
wneuper@59387
   348
"----------- fun get_pair: examples ------------------------------------------------------------";
wneuper@59387
   349
"----------- fun get_pair: examples ------------------------------------------------------------";
wneuper@59387
   350
"----------- fun get_pair: examples ------------------------------------------------------------";
wneuper@59388
   351
val thy = @{theory};
walther@59686
   352
val SOME (isa_str, eval_fn) = LibraryC.assoc (KEStore_Elems.get_calcs @{theory}, "PLUS");
walther@60317
   353
if isa_str = "Groups.plus_class.plus" then () else error "eval_fn PLUS changed";
wneuper@59388
   354
walther@60317
   355
val t = @{term "3 + 4 :: real"};
wneuper@59388
   356
val SOME (str, term) = get_pair thy isa_str eval_fn t;
walther@60317
   357
(*+*)if str =  "#: 3 + 4 = 7" andalso UnparseC.term term = "3 + 4 = 7"
walther@60317
   358
(*+*)then () else error "get_pair  3 + 4  changed";
wneuper@59388
   359
walther@60317
   360
val t = @{term "(a + 3) + 4 :: real"};
wneuper@59388
   361
val SOME (str, term) = get_pair thy isa_str eval_fn t;
walther@59868
   362
if str =  "#: a + 3 + 4 = a + 7" andalso UnparseC.term term = "a + 3 + 4 = a + 7"
wneuper@59388
   363
then () else error "get_pair  (a + 3) + 4  changed";
wneuper@59388
   364
walther@60317
   365
val t = @{term "(a + 3) + 4 :: real"};
wneuper@59388
   366
val SOME (str, term) = get_pair thy isa_str eval_fn t;
walther@59868
   367
if str =  "#: a + 3 + 4 = a + 7" andalso UnparseC.term term = "a + 3 + 4 = a + 7"
wneuper@59388
   368
then () else error "get_pair  (a + 3) + 4  changed";
wneuper@59388
   369
walther@60317
   370
val t = @{term "x = 5 * (3 + (4 + a) :: real)"};
wneuper@59388
   371
val SOME (str, term) = get_pair thy isa_str eval_fn t;
walther@59868
   372
if str =  "#: 3 + (4 + a) = 7 + a" andalso UnparseC.term term = "3 + (4 + a) = 7 + a"
wneuper@59388
   373
then ((* !!! gets subterm !!!*)) else error "get_pair  x = 5 * (3 + (4 + a))  (subterm) changed";
wneuper@59388
   374
walther@59686
   375
val SOME (isa_str, eval_fn) = LibraryC.assoc (KEStore_Elems.get_calcs @{theory}, "DIVIDE");
wneuper@59388
   376
walther@60329
   377
val t = @{term "-4 / - 2 :: real"};
wneuper@59388
   378
val SOME (str, term) = get_pair thy isa_str eval_fn t;
walther@60317
   379
if str = "#divide_e~4_~2" andalso UnparseC.term term = "- 4 / - 2 = 2"
walther@60329
   380
then () else error "get_pair  -4 / - 2   changed";
wneuper@59388
   381
walther@59686
   382
val SOME (isa_str, eval_fn) = LibraryC.assoc (KEStore_Elems.get_calcs @{theory}, "POWER");
wneuper@59388
   383
walther@60317
   384
val t = @{term "2 \<up> 3 :: real"};
wneuper@59388
   385
val SOME (str, term) = get_pair thy isa_str eval_fn t;
walther@60242
   386
if str =  "#: 2 \<up> 3 = 8" andalso UnparseC.term term = "2 \<up> 3 = 8"
walther@60242
   387
then () else error "get_pair  2 \<up> 3   changed";
wneuper@59388
   388
wneuper@59387
   389
"----------- fun adhoc_thm: examples -----------------------------------------------------------";
wneuper@59387
   390
"----------- fun adhoc_thm: examples -----------------------------------------------------------";
wneuper@59387
   391
"----------- fun adhoc_thm: examples -----------------------------------------------------------";
wneuper@59388
   392
(*--------------------------------------------------------------------vvvvvvvvvv*)
walther@60387
   393
val SOME (isa_str, eval_fn) = LibraryC.assoc (KEStore_Elems.get_calcs @{theory}, "is_num");
walther@60387
   394
val t = @{term "9 is_num"};
wneuper@59388
   395
val SOME (str, thm) = adhoc_thm thy (isa_str, eval_fn) t;
walther@60387
   396
if str = "#is_num_9_" andalso ThmC_Def.string_of_thm thm = "(9 is_num) = True"
walther@60387
   397
then () else error "adhoc_thm  9 is_num  changed";
wneuper@59387
   398
wenzelm@60309
   399
case assoc_calc thy \<^const_name>\<open>less\<close> of
wneuper@59388
   400
  "le" => () | _ => error "Orderings.ord_class.less <-> le changed";
walther@59686
   401
val SOME (isa_str, eval_fn) = LibraryC.assoc (KEStore_Elems.get_calcs @{theory}, "le");
walther@60317
   402
if isa_str = "Orderings.ord_class.less" then () else error "adhoc_thm (4 < 4) = False CHANGED";
wneuper@59387
   403
walther@60317
   404
val t = @{term "4 < (4 :: real)"};
wneuper@59388
   405
val SOME (str, thm) = adhoc_thm thy (isa_str, eval_fn) t;
walther@59876
   406
if str = "#less_4_4" andalso ThmC_Def.string_of_thm thm = "(4 < 4) = False"
wneuper@59388
   407
then () else error "adhoc_thm  4 < 4  changed";
wneuper@59387
   408
wneuper@59388
   409
val SOME t = parseNEW @{context} "a < 4";
wneuper@59388
   410
case adhoc_thm thy (isa_str, eval_fn) t of
wneuper@59388
   411
NONE => () | _ => error "adhoc_thm  a < 4  does NOT result in NONE";
wneuper@59387
   412
walther@59686
   413
val SOME (isa_str, eval_fn) = LibraryC.assoc (KEStore_Elems.get_calcs @{theory}, "PLUS");
walther@60317
   414
val SOME t = parseNEW @{context} "1 + (2::real)";
wneuper@59388
   415
val SOME (str, thm) = adhoc_thm thy (isa_str, eval_fn) t;
walther@59876
   416
if str = "#: 1 + 2 = 3" andalso ThmC_Def.string_of_thm thm = "1 + 2 = 3"
wneuper@59388
   417
then () else error "adhoc_thm  1 + 2  changed";
wneuper@59387
   418
walther@59686
   419
val SOME (isa_str, eval_fn) = LibraryC.assoc (KEStore_Elems.get_calcs @{theory}, "DIVIDE");
walther@60317
   420
val t = @{term "6 / -8 :: real"};
wneuper@59388
   421
val SOME (str, thm) = adhoc_thm thy (isa_str, eval_fn) t;
walther@60317
   422
if str = "#divide_e6_~8" andalso ThmC_Def.string_of_thm thm = "6 / - 8 = - 3 / 4"
walther@60317
   423
then () else error "adhoc_thm  6 / -8 = - 3 / 4  changed";
wneuper@59387
   424
walther@59603
   425
case assoc_calc thy "Prog_Expr.ident" of
walther@59603
   426
  "ident" => () | _ => error "Prog_Expr.ident <-> ident  changed";
walther@59686
   427
val SOME (isa_str, eval_fn) = LibraryC.assoc (KEStore_Elems.get_calcs @{theory}, "ident");
wneuper@59387
   428
walther@60317
   429
val t = @{term "3 =!= (3 :: real)"};
wneuper@59388
   430
val SOME (str, thm) = adhoc_thm thy (isa_str, eval_fn) t;
walther@59876
   431
if str = "#ident_(3)_(3)" andalso ThmC_Def.string_of_thm thm = "(3 =!= 3) = True"
wneuper@59388
   432
then () else error "adhoc_thm  (3 =!= 3)  changed";
wneuper@59387
   433
walther@60317
   434
val t = @{term "\<not> ((4 :: real) + (4 * x + x \<up> 2) =!= 0)"};
wneuper@59388
   435
val SOME (str, thm) = adhoc_thm thy (isa_str, eval_fn) t;
walther@60317
   436
if str = "#ident_(4 + (4 * x + x \<up> 2))_(0)" andalso ThmC_Def.string_of_thm thm = "(4 + (4 * x + x \<up> 2) =!= 0) = False"
wneuper@59388
   437
then () else error "adhoc_thm  (\<not> (4 + (4 * x + x ^ 2) =!= 0))  changed";
wneuper@59403
   438
walther@60391
   439
"----------- fun adhoc_thm + fun eval_cancel ---------------------------------------------------";
walther@60391
   440
"----------- fun adhoc_thm + fun eval_cancel ---------------------------------------------------";
walther@60391
   441
"----------- fun adhoc_thm + fun eval_cancel ---------------------------------------------------";
walther@60391
   442
val eval_ = ("Rings.divide_class.divide", eval_cancel "#divide_e" : eval_fn);
walther@60391
   443
val t = @{term "- 1 / 2 ::real"};
walther@60391
   444
(* 
walther@60391
   445
  ML\<open>Eval.adhoc_thm\<close> is called while searching terms for adjacent numerals 
walther@60391
   446
  given a certain ML_type\<open>eval_fn\<close> and a certain  ML\<open>term\<close> ..
walther@60391
   447
walther@60391
   448
  THE ERROR WAS:
walther@60391
   449
    rew_once:
walther@60391
   450
    Eval.get_pair for \<^const_name>\<open>divide\<close> \<longrightarrow> SOME (_, "- 1 / 2 = - 1 / 2")
walther@60391
   451
    but rewrite__ on "x = - 0 \<or> x = - 1 / 2" \<longrightarrow> NONE
walther@60391
   452
walther@60391
   453
  STEP-INTO BEVORE REMOVING THE ERROR:
walther@60391
   454
val SOME ("#divide_e~1_2", adhoc_thm) =
walther@60391
   455
      Eval.adhoc_thm @{theory} eval_ t;
walther@60391
   456
"~~~~~ fun adhoc_thm , args:"; val (thy, (op_, eval_fn), t) = (@{theory}, eval_, t);
walther@60391
   457
val SOME
walther@60391
   458
    ("#divide_e~1_2", t'') =
walther@60391
   459
  (*case*) get_pair thy op_ eval_fn t (*of*);
walther@60391
   460
(*
walther@60391
   461
  get_pair finds two adjacent numerals and does NOT distinguish between different kinds of 
walther@60391
   462
  \<^ML_type>\<open>eval_fn\<close>. In case of \<^ML>\<open>eval_cancel\<close> the return value WAS the same as the input..
walther@60391
   463
*)
walther@60391
   464
(*+*)ThmC.string_of_thm adhoc_thm = "- 1 / 2 = - 1 / 2"
walther@60391
   465
*)
walther@60391
   466
walther@60393
   467
val NONE = adhoc_thm @{theory} eval_ t;
walther@60393
   468
"~~~~~ fun adhoc_thm , args:"; val (thy, (op_, eval_fn), t) = (@{theory}, eval_, t);
walther@60391
   469
val NONE =
walther@60391
   470
  (*case*) get_pair thy op_ eval_fn t (*of*);
walther@60391
   471
walther@60391
   472
walther@60329
   473
"----------- fun adhoc_thm \<longrightarrow> exception TYPE --------------------------------------------------";
walther@60329
   474
"----------- fun adhoc_thm \<longrightarrow> exception TYPE --------------------------------------------------";
walther@60329
   475
"----------- fun adhoc_thm \<longrightarrow> exception TYPE --------------------------------------------------";
walther@60329
   476
val t = TermC.str2term "sqrt 4";
wenzelm@60381
   477
Eval.adhoc_thm (ThyC.get_theory "Isac_Knowledge") ("NthRoot.sqrt", eval_sqrt "#sqrt_") t;
walther@60329
   478
walther@60329
   479
"~~~~~ fun adhoc_thm , args:"; val (thy, (op_, eval_fn), ct) =
walther@60329
   480
  ((ThyC.get_theory "Isac_Knowledge"),
walther@60329
   481
    ("NthRoot.sqrt", eval_sqrt "#sqrt_": string -> term -> theory -> (string * term) option), t);
walther@60356
   482
walther@60329
   483
val SOME (thmid, t) =
walther@60329
   484
  (*case*) get_pair thy op_ eval_fn ct (*of*);
walther@60356
   485
(*+*)val "sqrt 4 = 2" = UnparseC.term t;
walther@60356
   486
walther@60329
   487
(** )
walther@60356
   488
      Skip_Proof.make_thm thy t;
walther@60356
   489
walther@60329
   490
  exception TYPE raised (line 169 of "consts.ML"): Illegal type
walther@60329
   491
   for constant "HOL.eq" :: real \<Rightarrow> (num \<Rightarrow> real) \<Rightarrow> bool (**)
walther@60329
   492
( **)
walther@60329
   493
wneuper@59403
   494
"----------- fun power -------------------------------------------------------------------------";
wneuper@59403
   495
"----------- fun power -------------------------------------------------------------------------";
wneuper@59403
   496
"----------- fun power -------------------------------------------------------------------------";
wneuper@59403
   497
if power 2 3 = 8 then () else error "power 2 3 = 8";
wneuper@59403
   498
if power ~2 3 = ~8 then () else error "power ~2 3 = ~8";
wneuper@59403
   499
if power ~3 2 = 9 then () else error "power ~3 2 = 9";
walther@60270
   500
case \<^try>\<open> power 3 ~2 \<close> of
walther@60270
   501
  SOME _ => raise error "power 3 ~2: should raise an exn 1"
walther@60271
   502
| NONE => ();
wneuper@59403
   503
wneuper@59403
   504
"----------- fun divisors ----------------------------------------------------------------------";
wneuper@59403
   505
"----------- fun divisors ----------------------------------------------------------------------";
wneuper@59403
   506
"----------- fun divisors ----------------------------------------------------------------------";
wneuper@59403
   507
if divisors 30 = [5, 3, 2] then () else error "divisors 30 = [5, 3, 2]";
wneuper@59403
   508
if divisors 32 = [2, 2, 2, 2, 2] then () else error "divisors 32 = [2, 2, 2, 2, 2]";
wneuper@59403
   509
if divisors 60 = [5, 3, 2, 2] then () else error "divisors 60 = [5, 3, 2, 2]";
wneuper@59403
   510
if divisors 11 = [11] then () else error "divisors 11 = [11]";
wneuper@59403
   511
wneuper@59403
   512
"----------- fun doubles, fun squfact ----------------------------------------------------------";
wneuper@59403
   513
"----------- fun doubles, fun squfact ----------------------------------------------------------";
wneuper@59403
   514
"----------- fun doubles, fun squfact ----------------------------------------------------------";
wneuper@59403
   515
if doubles [2,3,4] = [] then () else error "doubles [2,3,4] changed";
wneuper@59403
   516
if doubles [2,3,3,5,5,7] = [5, 3] then () else error "doubles [2,3,3,5,5,7] changed";
wneuper@59403
   517
wneuper@59403
   518
if squfact 30 = 1 then () else error "squfact  30  changed";
wneuper@59403
   519
if squfact 32 = 4 then () else error "squfact  32  changed";
wneuper@59403
   520
if squfact 60 = 2 then () else error "squfact  60  changed";
wneuper@59403
   521
if squfact 11 = 1 then () else error "squfact  11  changed";