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