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