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