test/Tools/isac/ProgLang/evaluate.sml
changeset 60242 73ee61385493
parent 60230 0ca0f9363ad3
child 60270 844610c5c943
equal deleted inserted replaced
60240:17db21aa9aed 60242:73ee61385493
    41 
    41 
    42 "----------- fun calculate_ --------------------------------------------------------------------";
    42 "----------- fun calculate_ --------------------------------------------------------------------";
    43 "----------- fun calculate_ --------------------------------------------------------------------";
    43 "----------- fun calculate_ --------------------------------------------------------------------";
    44 "----------- fun calculate_ --------------------------------------------------------------------";
    44 "----------- fun calculate_ --------------------------------------------------------------------";
    45 (* fun rewrite__set_ \<longrightarrow> fun rew_once works the same way *)
    45 (* fun rewrite__set_ \<longrightarrow> fun rew_once works the same way *)
    46 val t = TermC.str2term "((1+2)*4/3)^^^2";
    46 val t = TermC.str2term "((1+2)*4/3) \<up> 2";
    47 val thy = @{theory};
    47 val thy = @{theory};
    48 val times =  ("Groups.times_class.times", eval_binop "#mult_") : string * Eval_Def.eval_fn;
    48 val times =  ("Groups.times_class.times", eval_binop "#mult_") : string * Eval_Def.eval_fn;
    49 val plus =   ("Groups.plus_class.plus",eval_binop "#add_") : string * Eval_Def.eval_fn;
    49 val plus =   ("Groups.plus_class.plus",eval_binop "#add_") : string * Eval_Def.eval_fn;
    50 val divide = ("Rings.divide_class.divide"  ,eval_cancel "#divide_e") : string * Eval_Def.eval_fn;
    50 val divide = ("Rings.divide_class.divide"  ,eval_cancel "#divide_e") : string * Eval_Def.eval_fn;
    51 val pow =    ("Prog_Expr.pow"  ,eval_binop "#power_") : string * Eval_Def.eval_fn;
    51 val pow =    ("Prog_Expr.pow"  ,eval_binop "#power_") : string * Eval_Def.eval_fn;
    52 
    52 
    53 "~~~~~ fun calculate_, args:"; val (thy, isa_fn, t) = (thy, plus, t);
    53 "~~~~~ fun calculate_, args:"; val (thy, isa_fn, t) = (thy, plus, t);
    54 val SOME ("#: 1 + 2 = 3", adh_thm) = adhoc_thm @{theory} isa_fn t;
    54 val SOME ("#: 1 + 2 = 3", adh_thm) = adhoc_thm @{theory} isa_fn t;
    55 val SOME (t', []) = rewrite__ thy 0 [] e_rew_ord Rule_Set.empty true adh_thm t;
    55 val SOME (t', []) = rewrite__ thy 0 [] e_rew_ord Rule_Set.empty true adh_thm t;
    56 if UnparseC.term t' = "(3 * 4 / 3) ^^^ 2" then () else error "calculate_  1 + 2 = 3  changed";
    56 if UnparseC.term t' = "(3 * 4 / 3) \<up> 2" then () else error "calculate_  1 + 2 = 3  changed";
    57 
    57 
    58 "~~~~~ fun calculate_, args:"; val (thy, isa_fn, t) = (thy, times, t');
    58 "~~~~~ fun calculate_, args:"; val (thy, isa_fn, t) = (thy, times, t');
    59 val SOME ("#: 3 * 4 = 12", adh_thm) = adhoc_thm @{theory} isa_fn t';
    59 val SOME ("#: 3 * 4 = 12", adh_thm) = adhoc_thm @{theory} isa_fn t';
    60 val SOME (t'', []) = rewrite__ thy 0 [] e_rew_ord Rule_Set.empty true adh_thm t;
    60 val SOME (t'', []) = rewrite__ thy 0 [] e_rew_ord Rule_Set.empty true adh_thm t;
    61 if UnparseC.term t'' = "(12 / 3) ^^^ 2" then () else error "calculate_  3 * 4 = 12  changed";
    61 if UnparseC.term t'' = "(12 / 3) \<up> 2" then () else error "calculate_  3 * 4 = 12  changed";
    62 
    62 
    63 "~~~~~ fun calculate_, args:"; val (thy, isa_fn, t) = (thy, divide, t'');
    63 "~~~~~ fun calculate_, args:"; val (thy, isa_fn, t) = (thy, divide, t'');
    64 val SOME ("#divide_e12_3", adh_thm) = adhoc_thm @{theory} isa_fn t;
    64 val SOME ("#divide_e12_3", adh_thm) = adhoc_thm @{theory} isa_fn t;
    65 val SOME (t''', []) = rewrite__ thy 0 [] e_rew_ord Rule_Set.empty true adh_thm t;
    65 val SOME (t''', []) = rewrite__ thy 0 [] e_rew_ord Rule_Set.empty true adh_thm t;
    66 if UnparseC.term t''' = "4 ^^^ 2" then () else error "calculate_  12 / 3 = 4  changed";
    66 if UnparseC.term t''' = "4 \<up> 2" then () else error "calculate_  12 / 3 = 4  changed";
    67 
    67 
    68 "~~~~~ fun calculate_, args:"; val (thy, isa_fn, t) = (thy, pow, t''');
    68 "~~~~~ fun calculate_, args:"; val (thy, isa_fn, t) = (thy, pow, t''');
    69 val SOME ("#: 4 ^^^ 2 = 16", adh_thm) = adhoc_thm @{theory} isa_fn t;
    69 val SOME ("#: 4 \<up> 2 = 16", adh_thm) = adhoc_thm @{theory} isa_fn t;
    70 val SOME (t'''', []) = rewrite__ thy 0 [] e_rew_ord Rule_Set.empty true adh_thm t;
    70 val SOME (t'''', []) = rewrite__ thy 0 [] e_rew_ord Rule_Set.empty true adh_thm t;
    71 if UnparseC.term t'''' = "16" then () else error "calculate_  12 / 3 = 4  changed";
    71 if UnparseC.term t'''' = "16" then () else error "calculate_  12 / 3 = 4  changed";
    72 
    72 
    73 "----------- calculate from Prog --------------------------------- -----------------------------";
    73 "----------- calculate from Prog --------------------------------- -----------------------------";
    74 "----------- calculate from Prog --------------------------------- -----------------------------";
    74 "----------- calculate from Prog --------------------------------- -----------------------------";
    75 "----------- calculate from Prog --------------------------------- -----------------------------";
    75 "----------- calculate from Prog --------------------------------- -----------------------------";
    76 val thy = @{theory "Test"};
    76 val thy = @{theory "Test"};
    77 val fmz = ["realTestGiven (((1+2)*4/3)^^^2)", "realTestFind s"];
    77 val fmz = ["realTestGiven (((1+2)*4/3) \<up> 2)", "realTestFind s"];
    78 val (dI',pI',mI') =
    78 val (dI',pI',mI') =
    79   ("Test",["calculate", "test"],["Test", "test_calculate"]);
    79   ("Test",["calculate", "test"],["Test", "test_calculate"]);
    80 
    80 
    81 val (p,_,f,nxt,_,pt) = CalcTreeTEST [(fmz, (dI',pI',mI'))];
    81 val (p,_,f,nxt,_,pt) = CalcTreeTEST [(fmz, (dI',pI',mI'))];
    82 val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
    82 val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
    83 (*nxt =("Add_Given",Add_Given "realTestGiven (((#1 + #2) * #4 // #3) ^^^#2)")*)
    83 (*nxt =("Add_Given",Add_Given "realTestGiven (((#1 + #2) * #4 // #3)  \<up> #2)")*)
    84 val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
    84 val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
    85 (*nxt = ("Add_Find",Add_Find "realTestFind s") : string * tac*)
    85 (*nxt = ("Add_Find",Add_Find "realTestFind s") : string * tac*)
    86 val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
    86 val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
    87 (*nxt = ("Specify_Theory",Specify_Theory "Test") : string * tac*)
    87 (*nxt = ("Specify_Theory",Specify_Theory "Test") : string * tac*)
    88 val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
    88 val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
   105 "----------- calculate check test-root-equ --------------";
   105 "----------- calculate check test-root-equ --------------";
   106 "----------- calculate check test-root-equ --------------";
   106 "----------- calculate check test-root-equ --------------";
   107 "----------- calculate check test-root-equ --------------";
   107 "----------- calculate check test-root-equ --------------";
   108 (*(1): 2nd Test_simplify didn't work:
   108 (*(1): 2nd Test_simplify didn't work:
   109 val ct =
   109 val ct =
   110   "sqrt (x ^^^ 2 + -3 * x) = (-3 + 2 * x + -1 * (9 + 4 * x)) / (-1 * 2)"
   110   "sqrt (x \<up> 2 + -3 * x) = (-3 + 2 * x + -1 * (9 + 4 * x)) / (-1 * 2)"
   111 > val rls = ("Test_simplify");
   111 > val rls = ("Test_simplify");
   112 > val (ct,_) = the (rewrite_set thy' ("tval_rls") false rls ct);
   112 > val (ct,_) = the (rewrite_set thy' ("tval_rls") false rls ct);
   113 val ct = "sqrt (x ^^^ 2 + -3 * x) =
   113 val ct = "sqrt (x \<up> 2 + -3 * x) =
   114 (-9) / (-2) + (-3 / (-2) + (x * ((-4) / (-2)) + x * (2 / (-2))))";
   114 (-9) / (-2) + (-3 / (-2) + (x * ((-4) / (-2)) + x * (2 / (-2))))";
   115 ie. cancel does not work properly
   115 ie. cancel does not work properly
   116 *)
   116 *)
   117  val thy = @{theory "Test"};
   117  val thy = @{theory "Test"};
   118  val op_ = the (LibraryC.assoc (KEStore_Elems.get_calcs @{theory}, "DIVIDE"));
   118  val op_ = the (LibraryC.assoc (KEStore_Elems.get_calcs @{theory}, "DIVIDE"));
   119  val ct = ThmC_Def.num_to_Free @{term
   119  val ct = ThmC_Def.num_to_Free @{term
   120    "sqrt (x ^^^ 2 + -3 * x) = (-9) / (-2) + (-3 / (-2) + (x * ((-4) / (-2)) + x * (2 / (-2))))"};
   120    "sqrt (x \<up> 2 + -3 * x) = (-9) / (-2) + (-3 / (-2) + (x * ((-4) / (-2)) + x * (2 / (-2))))"};
   121 case calculate_ thy op_ ct of
   121 case calculate_ thy op_ ct of
   122   SOME _ => ()
   122   SOME _ => ()
   123 | NONE => error "calculate_ test-root-equ changed";
   123 | NONE => error "calculate_ test-root-equ changed";
   124 (*
   124 (*
   125            sqrt (x ^^^ 2 + -3 * x) =\
   125            sqrt (x \<up> 2 + -3 * x) =\
   126  \(-9) / (-2) + (-3 / (-2) + (x * ((-4) / (-2)) + x * (2 / (-2))))
   126  \(-9) / (-2) + (-3 / (-2) + (x * ((-4) / (-2)) + x * (2 / (-2))))
   127 ............... does not work *)
   127 ............... does not work *)
   128 
   128 
   129 (*--------------(2): does divide work in Test_simplify ?: ------*)
   129 (*--------------(2): does divide work in Test_simplify ?: ------*)
   130  val thy = @{theory Test};
   130  val thy = @{theory Test};
   225 (*** -------------
   225 (*** -------------
   226 *** Const ( Nat.power, ['a, nat] => 'a)
   226 *** Const ( Nat.power, ['a, nat] => 'a)
   227 *** . Free ( 1, 'a)
   227 *** . Free ( 1, 'a)
   228 *** . Free ( aaa, nat) *)
   228 *** . Free ( aaa, nat) *)
   229 
   229 
   230 val t = TermC.str2term "1 ^^^ aaa";
   230 val t = TermC.str2term "1 \<up> aaa";
   231 TermC.atomty t;
   231 TermC.atomty t;
   232 (**** 
   232 (**** 
   233 *** Const (Prog_Expr.pow, real => real => real)
   233 *** Const (Prog_Expr.pow, real => real => real)
   234 *** . Free (1, real)
   234 *** . Free (1, real)
   235 *** . Free (aaa, real)
   235 *** . Free (aaa, real)
   243 val t = (Thm.term_of o the o (TermC.parse thy)) "12 / 3";
   243 val t = (Thm.term_of o the o (TermC.parse thy)) "12 / 3";
   244 val SOME (thmID,thm) = adhoc_thm thy(the(LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"DIVIDE")))t;
   244 val SOME (thmID,thm) = adhoc_thm thy(the(LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"DIVIDE")))t;
   245 val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
   245 val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
   246 "12 / 3 = 4";
   246 "12 / 3 = 4";
   247 val thy = @{theory Test};
   247 val thy = @{theory Test};
   248 val t = (Thm.term_of o the o (TermC.parse thy)) "4 ^^^ 2";
   248 val t = (Thm.term_of o the o (TermC.parse thy)) "4 \<up> 2";
   249 val SOME (thmID,thm) = adhoc_thm thy(the(LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"POWER"))) t;
   249 val SOME (thmID,thm) = adhoc_thm thy(the(LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"POWER"))) t;
   250 val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
   250 val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
   251 "4 ^ 2 = 16";
   251 "4 ^ 2 = 16";
   252 
   252 
   253  val t = (Thm.term_of o the o (TermC.parse thy)) "((1 + 2) * 4 / 3) ^^^ 2";
   253  val t = (Thm.term_of o the o (TermC.parse thy)) "((1 + 2) * 4 / 3) \<up> 2";
   254  val SOME (thmID,thm) = adhoc_thm thy (the(LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"PLUS"))) t;
   254  val SOME (thmID,thm) = adhoc_thm thy (the(LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"PLUS"))) t;
   255 "1 + 2 = 3";
   255 "1 + 2 = 3";
   256  val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
   256  val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
   257  UnparseC.term t;
   257  UnparseC.term t;
   258 "(3 * 4 / 3) ^^^ 2";
   258 "(3 * 4 / 3) \<up> 2";
   259  val SOME (thmID,thm) = adhoc_thm thy (the(LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"TIMES")))t;
   259  val SOME (thmID,thm) = adhoc_thm thy (the(LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"TIMES")))t;
   260 "3 * 4 = 12";
   260 "3 * 4 = 12";
   261  val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
   261  val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
   262  UnparseC.term t;
   262  UnparseC.term t;
   263 "(12 / 3) ^^^ 2";
   263 "(12 / 3) \<up> 2";
   264  val SOME (thmID,thm) =adhoc_thm thy(the(LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"DIVIDE")))t;
   264  val SOME (thmID,thm) =adhoc_thm thy(the(LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"DIVIDE")))t;
   265 "12 / 3 = 4";
   265 "12 / 3 = 4";
   266  val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
   266  val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
   267  UnparseC.term t;
   267  UnparseC.term t;
   268 "4 ^^^ 2";
   268 "4 \<up> 2";
   269  val SOME (thmID,thm) = adhoc_thm thy(the(LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"POWER")))t;
   269  val SOME (thmID,thm) = adhoc_thm thy(the(LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"POWER")))t;
   270 "4 ^^^ 2 = 16";
   270 "4 \<up> 2 = 16";
   271  val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
   271  val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
   272  UnparseC.term t;
   272  UnparseC.term t;
   273 "16";
   273 "16";
   274  if it <> "16" then error "evaluate.sml: new behaviour in calculate_"
   274  if it <> "16" then error "evaluate.sml: new behaviour in calculate_"
   275  else ();
   275  else ();
   276 
   276 
   277 (*13.9.02 *** calc: operator = pow not defined*)
   277 (*13.9.02 *** calc: operator = pow not defined*)
   278   val t = (Thm.term_of o the o (TermC.parse thy)) "3^^^2";
   278   val t = (Thm.term_of o the o (TermC.parse thy)) "3 \<up> 2";
   279   val SOME (thmID,thm) = 
   279   val SOME (thmID,thm) = 
   280       adhoc_thm thy (the(LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"POWER"))) t;
   280       adhoc_thm thy (the(LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"POWER"))) t;
   281 (*** calc: operator = pow not defined*)
   281 (*** calc: operator = pow not defined*)
   282 
   282 
   283   val (op_, eval_fn) = the (LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"POWER"));
   283   val (op_, eval_fn) = the (LibraryC.assoc(KEStore_Elems.get_calcs @{theory},"POWER"));
   301 "----------- get_pair with 3 args --------------------------------";
   301 "----------- get_pair with 3 args --------------------------------";
   302 val (thy, op_, ef, arg) =
   302 val (thy, op_, ef, arg) =
   303     (thy, "EqSystem.occur'_exactly'_in", 
   303     (thy, "EqSystem.occur'_exactly'_in", 
   304      assoc_calc' (@{theory "EqSystem"}) "occur_exactly_in" |> snd |> snd,
   304      assoc_calc' (@{theory "EqSystem"}) "occur_exactly_in" |> snd |> snd,
   305      TermC.str2term
   305      TermC.str2term
   306       "[] from [c, c_2, c_3, c_4] occur_exactly_in -1 * (q_0 * L ^^^ 2) / 2"
   306       "[] from [c, c_2, c_3, c_4] occur_exactly_in -1 * (q_0 * L \<up> 2) / 2"
   307       );
   307       );
   308 val SOME (str, simpl) = get_pair thy op_ ef arg;
   308 val SOME (str, simpl) = get_pair thy op_ ef arg;
   309 if str = 
   309 if str = 
   310 "[] from [c, c_2, c_3, c_4] occur_exactly_in -1 * (q_0 * L ^^^ 2) / 2 = True"
   310 "[] from [c, c_2, c_3, c_4] occur_exactly_in -1 * (q_0 * L \<up> 2) / 2 = True"
   311 then () else error "evaluate.sml get_pair with 3 args:occur_exactly_in";
   311 then () else error "evaluate.sml get_pair with 3 args:occur_exactly_in";
   312 
   312 
   313 
   313 
   314 "----------- calculate (2 * x is_const) -----------------";
   314 "----------- calculate (2 * x is_const) -----------------";
   315 "----------- calculate (2 * x is_const) -----------------";
   315 "----------- calculate (2 * x is_const) -----------------";
   356 if str =  "#divide_e-4_-2" andalso UnparseC.term term = "-4 / -2 = 2"
   356 if str =  "#divide_e-4_-2" andalso UnparseC.term term = "-4 / -2 = 2"
   357 then () else error "get_pair  -4 / -2   changed";
   357 then () else error "get_pair  -4 / -2   changed";
   358 
   358 
   359 val SOME (isa_str, eval_fn) = LibraryC.assoc (KEStore_Elems.get_calcs @{theory}, "POWER");
   359 val SOME (isa_str, eval_fn) = LibraryC.assoc (KEStore_Elems.get_calcs @{theory}, "POWER");
   360 
   360 
   361 val t = (Thm.term_of o the o (TermC.parse thy)) "2 ^^^ 3";
   361 val t = (Thm.term_of o the o (TermC.parse thy)) "2 \<up> 3";
   362 val SOME (str, term) = get_pair thy isa_str eval_fn t;
   362 val SOME (str, term) = get_pair thy isa_str eval_fn t;
   363 if str =  "#: 2 ^^^ 3 = 8" andalso UnparseC.term term = "2 ^^^ 3 = 8"
   363 if str =  "#: 2 \<up> 3 = 8" andalso UnparseC.term term = "2 \<up> 3 = 8"
   364 then () else error "get_pair  2 ^^^ 3   changed";
   364 then () else error "get_pair  2 \<up> 3   changed";
   365 
   365 
   366 "----------- fun adhoc_thm: examples -----------------------------------------------------------";
   366 "----------- fun adhoc_thm: examples -----------------------------------------------------------";
   367 "----------- fun adhoc_thm: examples -----------------------------------------------------------";
   367 "----------- fun adhoc_thm: examples -----------------------------------------------------------";
   368 "----------- fun adhoc_thm: examples -----------------------------------------------------------";
   368 "----------- fun adhoc_thm: examples -----------------------------------------------------------";
   369 (*--------------------------------------------------------------------vvvvvvvvvv*)
   369 (*--------------------------------------------------------------------vvvvvvvvvv*)