test/Tools/isac/ProgLang/evaluate.sml
changeset 60317 638d02a9a96a
parent 60278 343efa173023
child 60318 e6e7a9b9ced7
     1.1 --- a/test/Tools/isac/ProgLang/evaluate.sml	Fri May 07 18:12:51 2021 +0200
     1.2 +++ b/test/Tools/isac/ProgLang/evaluate.sml	Tue Jun 01 15:41:23 2021 +0200
     1.3 @@ -1,5 +1,4 @@
     1.4  (* Title:  "ProgLang/evaluate.sml"
     1.5 -           test calculation of values for function constants
     1.6     Author: Walther Neuper 2000
     1.7     (c) copyright due to lincense terms.
     1.8  *)
     1.9 @@ -14,9 +13,11 @@
    1.10  "----------- calculate from Prog --------------------------------- -----------------------------";
    1.11  "----------- calculate from script --requires 'setup'----";
    1.12  "----------- calculate check test-root-equ --------------";
    1.13 -"----------- check calcul,ate bottom up -----------------";
    1.14 +"----------- check calculate bottom up -----------------";
    1.15  "----------- Prog_Expr.pow Power.power_class.power ---------";
    1.16  " ================= evaluate.sml: calculate_ 2002 ======";
    1.17 +"----------- fun cancel_int --------------------------------------------------------------------";
    1.18 +"----------- RE-BUILD fun calcul ---------------------------------------------------------------";
    1.19  "----------- get_pair with 3 args -----------------------";
    1.20  "----------- calculate (2 * x is_const) -----------------";
    1.21  "----------- fun get_pair: examples ------------------------------------------------------------";
    1.22 @@ -46,11 +47,11 @@
    1.23  val t = TermC.str2term "((1+2)*4/3) \<up> 2";
    1.24  val thy = @{theory};
    1.25  val times =  ("Groups.times_class.times", eval_binop "#mult_") : string * Eval_Def.eval_fn;
    1.26 -val plus =   ("Groups.plus_class.plus",eval_binop "#add_") : string * Eval_Def.eval_fn;
    1.27 -val divide = ("Rings.divide_class.divide"  ,eval_cancel "#divide_e") : string * Eval_Def.eval_fn;
    1.28 -val pow =    ("Transcendental.powr"  ,eval_binop "#power_") : string * Eval_Def.eval_fn;
    1.29 +val plus =   ("Groups.plus_class.plus", eval_binop "#add_") : string * Eval_Def.eval_fn;
    1.30 +val divide = ("Rings.divide_class.divide", eval_cancel "#divide_e") : string * Eval_Def.eval_fn;
    1.31 +val pow =    ("Transcendental.powr", eval_binop "#power_") : string * Eval_Def.eval_fn;
    1.32  
    1.33 -"~~~~~ fun calculate_, args:"; val (thy, isa_fn, t) = (thy, plus, t);
    1.34 +"~~~~~ fun calculate_ , args:"; val (thy, isa_fn, t) = (thy, plus, t);
    1.35  val SOME ("#: 1 + 2 = 3", adh_thm) = adhoc_thm @{theory} isa_fn t;
    1.36  val SOME (t', []) = rewrite__ thy 0 [] e_rew_ord Rule_Set.empty true adh_thm t;
    1.37  if UnparseC.term t' = "(3 * 4 / 3) \<up> 2" then () else error "calculate_  1 + 2 = 3  changed";
    1.38 @@ -76,7 +77,7 @@
    1.39  val thy = @{theory "Test"};
    1.40  val fmz = ["realTestGiven (((1+2)*4/3) \<up> 2)", "realTestFind s"];
    1.41  val (dI',pI',mI') =
    1.42 -  ("Test",["calculate", "test"],["Test", "test_calculate"]);
    1.43 +  ("Test", ["calculate", "test"], ["Test", "test_calculate"]);
    1.44  
    1.45  val (p,_,f,nxt,_,pt) = CalcTreeTEST [(fmz, (dI',pI',mI'))];
    1.46  val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
    1.47 @@ -158,13 +159,17 @@
    1.48  (*--------------(5): reproduce (1) with simpler term: ------------*)
    1.49   val t = (Thm.term_of o the o (TermC.parse thy)) "(3+5)/2";
    1.50  case rewrite_set_ thy false rls t of
    1.51 -  SOME (Free ("4", _), []) => ()
    1.52 -| _ => error "rewrite_set_ (3+5)/2 changed";
    1.53 +  SOME (t', []) =>
    1.54 +    if UnparseC.term t' = "4" then ()
    1.55 +    else error "rewrite_set_ (3+5)/2 changed 1"
    1.56 +| _ => error "rewrite_set_ (3+5)/2 changed 2";
    1.57  
    1.58   val t = (Thm.term_of o the o (TermC.parse thy)) "(3+1+2*x)/2";
    1.59  case rewrite_set_ thy false rls t of
    1.60 -  SOME (Const ("Groups.plus_class.plus", _) $ Free ("2", _) $ Free ("x", _), []) => ()
    1.61 -| _ => error "rewrite_set_ (3+1+2*x)/2 changed";
    1.62 +  SOME (t', _) => 
    1.63 +    if UnparseC.term t' = "x + (1 / 2 + 3 / 2)" (*ERROR should be : 2 * x  SEE ABOVE*) then ()
    1.64 +    else error "rewrite_set_ (3+1+2*x)/2 changed 1"
    1.65 +| _ => error "rewrite_set_ (3+1+2*x)/2 changed 2";
    1.66  
    1.67   Rewrite.trace_on:=false; (*=true3.6.03*)
    1.68  
    1.69 @@ -200,7 +205,7 @@
    1.70   Rewrite.trace_on:=false; (*WN130722: =true stopped Test_Isac.thy*)
    1.71   val t = (Thm.term_of o the o (TermC.parse thy))  "x + (-1 + -3) / 2";
    1.72  val SOME (res, []) = rewrite_set_ thy false rls t;
    1.73 -if UnparseC.term res = "-2 + x" then () else error "rewrite_set_  x + (-1 + -3) / 2  changed";
    1.74 +if UnparseC.term res = "x + - 2" then () else error "rewrite_set_  x + (-1 + -3) / 2  changed";
    1.75  "x + (-4) / 2";						
    1.76  (*
    1.77  ### trying calc. 'cancel'
    1.78 @@ -217,24 +222,6 @@
    1.79  
    1.80   Rewrite.trace_on:=false; (*WN130722: =true stopped Test_Isac.thy*)
    1.81  
    1.82 -"----------- Prog_Expr.pow Power.power_class.power ---------";
    1.83 -"----------- Prog_Expr.pow Power.power_class.power ---------";
    1.84 -"----------- Prog_Expr.pow Power.power_class.power ---------";
    1.85 -val t = (Thm.term_of o the o (parseold thy)) "1 ^ aaa";
    1.86 -TermC.atomty t;
    1.87 -(*** -------------
    1.88 -*** Const ( Nat.power, ['a, nat] => 'a)
    1.89 -*** . Free ( 1, 'a)
    1.90 -*** . Free ( aaa, nat) *)
    1.91 -
    1.92 -val t = TermC.str2term "1 \<up> aaa";
    1.93 -TermC.atomty t;
    1.94 -(**** 
    1.95 -*** Const (Prog_Expr.pow, real => real => real)
    1.96 -*** . Free (1, real)
    1.97 -*** . Free (aaa, real)
    1.98 -*** *);
    1.99 -
   1.100  " ================= evaluate.sml: calculate_ 2002 =================== ";
   1.101  " ================= evaluate.sml: calculate_ 2002 =================== ";
   1.102  " ================= evaluate.sml: calculate_ 2002 =================== ";
   1.103 @@ -291,10 +278,64 @@
   1.104    val SOME (id,t') = eval_fn op_ t thy;
   1.105  (*** calc: operator = pow not defined*)
   1.106  
   1.107 -  val (thmid, (Const (op0,t0) $ Free (n1,t1) $ Free(n2,t2))) = (op_, t);
   1.108 -  val SOME (id,t') = eval_binop thmid op_ t thy;
   1.109 +case (op_, t) of
   1.110 +  ("Transcendental.powr",
   1.111 +    Const ("Transcendental.powr", _) $ (Const ("Num.numeral_class.numeral", _) $ (Const ("Num.num.Bit1", _) $ Const ("Num.num.One", _))) $
   1.112 +      (Const ("Num.numeral_class.numeral", _) $ (Const ("Num.num.Bit0", _) $ Const ("Num.num.One", _)))) => ()
   1.113 +| _ => error "3 \<up> 2 CHANGED";
   1.114 +  val SOME (id, t') = eval_binop thmid op_ t thy;
   1.115  (*** calc: operator = pow not defined*)
   1.116  
   1.117 +if UnparseC.term t' = "3 \<up> 2 = 9" then () else error "eval_binop  3 \<up> 2 = 9  CHANGED";
   1.118 +
   1.119 +
   1.120 +"----------- fun cancel_int --------------------------------------------------------------------";
   1.121 +"----------- fun cancel_int --------------------------------------------------------------------";
   1.122 +"----------- fun cancel_int --------------------------------------------------------------------";
   1.123 +if cancel_int (~4, 2) = (~1, (2, 1)) then () else error "cancel_int (~4, 2) CHANGED";
   1.124 +if cancel_int (4, ~8) = (~1, (1, 2)) then () else error "cancel_int (4, ~8) CHANGED";
   1.125 +if cancel_int (6, 4) = (1, (3, 2)) then () else error "cancel_int (6, 4)CHANGED";
   1.126 +
   1.127 +
   1.128 +"----------- RE-BUILD fun calcul ---------------------------------------------------------------";
   1.129 +"----------- RE-BUILD fun calcul ---------------------------------------------------------------";
   1.130 +"----------- RE-BUILD fun calcul ---------------------------------------------------------------";
   1.131 +val (t1, t2) = (@{term 3}, @{term "2::real"});
   1.132 +
   1.133 +"~~~~~ fun calcul , args:"; val (op_, (t1, t2)) = ("Groups.plus_class.plus", (t1, t2));
   1.134 +    val (Const ("Num.numeral_class.numeral", _) $ n1) = t1;
   1.135 +    val (Const ("Num.numeral_class.numeral", _) $ n2) = t2;
   1.136 +    val (T, _) = HOLogic.dest_number t1
   1.137 +    val (i1, i2) = (HOLogic.dest_numeral n1, HOLogic.dest_numeral n2)
   1.138 +    val result =
   1.139 +      case op_ of
   1.140 +        "Groups.plus_class.plus" => i1 + i2
   1.141 +      | "Groups.minus_class.minus" => i1 - i2
   1.142 +      | "Groups.times_class.times" => i1 * i2
   1.143 +      | "Transcendental.powr" => power i1 i2
   1.144 +      | str => raise ERROR ("calcul not impl.for op_ " ^ str)
   1.145 +  (*in*)
   1.146 +    val xxx = HOLogic.mk_number T result;
   1.147 +  (*end*)
   1.148 +case HOLogic.dest_number xxx of
   1.149 +  (_, 5) => ()
   1.150 +| _ => error "calcul + 2 3 CHANGED";
   1.151 +
   1.152 +case HOLogic.dest_number (calcul "Groups.minus_class.minus" (t1, t2)) of
   1.153 +  (_, 1) => xxx
   1.154 +| _ => error "calcul - 2 3 CHANGED";
   1.155 +
   1.156 +case HOLogic.dest_number (calcul "Groups.times_class.times" (t1, t2)) of
   1.157 +  (_, 6) => xxx
   1.158 +| _ => error "calcul - 2 3 CHANGED";
   1.159 +
   1.160 +(*                       (calcul "Rings.divide_class.divide" (t1, t2)
   1.161 +ERROR: calcul not impl.for op_ Rings.divide_class.divide*)
   1.162 +
   1.163 +case HOLogic.dest_number (calcul "Transcendental.powr" (t1, t2)) of
   1.164 +  (_, 9) => xxx
   1.165 +| _ => error "calcul - 2 3 CHANGED";
   1.166 +
   1.167  
   1.168  "----------- get_pair with 3 args --------------------------------";
   1.169  "----------- get_pair with 3 args --------------------------------";
   1.170 @@ -307,7 +348,7 @@
   1.171        );
   1.172  val SOME (str, simpl) = get_pair thy op_ ef arg;
   1.173  if str = 
   1.174 -"[] from [c, c_2, c_3, c_4] occur_exactly_in -1 * (q_0 * L \<up> 2) / 2 = True"
   1.175 +"[] from [c, c_2, c_3, c_4] occur_exactly_in - 1 * (q_0 * L \<up> 2) / 2 = True"
   1.176  then () else error "evaluate.sml get_pair with 3 args:occur_exactly_in";
   1.177  
   1.178  
   1.179 @@ -316,49 +357,50 @@
   1.180  "----------- calculate (2 * x is_const) -----------------";
   1.181  val t = TermC.str2term "2 * x is_const";
   1.182  val SOME (str, t') = eval_const "" "" t @{theory Test};
   1.183 -UnparseC.term t';
   1.184 -"(2 * x is_const) = False";
   1.185 +if UnparseC.term t' = "(2 * x is_const) = False" then ()
   1.186 +else error "eval_const 2 * x is_const CHANGED";
   1.187  
   1.188  val SOME (t',_) = rewrite_set_ @{theory Test} false tval_rls t;
   1.189 -UnparseC.term t';
   1.190 -"HOL.False";
   1.191 +if UnparseC.term t' = "False" then ()
   1.192 +else error "rewrite_set_ 2 * x is_const CHANGED";
   1.193  
   1.194  "----------- fun get_pair: examples ------------------------------------------------------------";
   1.195  "----------- fun get_pair: examples ------------------------------------------------------------";
   1.196  "----------- fun get_pair: examples ------------------------------------------------------------";
   1.197  val thy = @{theory};
   1.198  val SOME (isa_str, eval_fn) = LibraryC.assoc (KEStore_Elems.get_calcs @{theory}, "PLUS");
   1.199 +if isa_str = "Groups.plus_class.plus" then () else error "eval_fn PLUS changed";
   1.200  
   1.201 -val t = (Thm.term_of o the o (TermC.parse thy)) "3 + 4";
   1.202 +val t = @{term "3 + 4 :: real"};
   1.203  val SOME (str, term) = get_pair thy isa_str eval_fn t;
   1.204 -if str =  "#: 3 + 4 = 7" andalso UnparseC.term term = "3 + 4 = 7"
   1.205 -then () else error "get_pair  3 + 4  changed";
   1.206 +(*+*)if str =  "#: 3 + 4 = 7" andalso UnparseC.term term = "3 + 4 = 7"
   1.207 +(*+*)then () else error "get_pair  3 + 4  changed";
   1.208  
   1.209 -val t = (Thm.term_of o the o (TermC.parse thy)) "(a + 3) + 4";
   1.210 +val t = @{term "(a + 3) + 4 :: real"};
   1.211  val SOME (str, term) = get_pair thy isa_str eval_fn t;
   1.212  if str =  "#: a + 3 + 4 = a + 7" andalso UnparseC.term term = "a + 3 + 4 = a + 7"
   1.213  then () else error "get_pair  (a + 3) + 4  changed";
   1.214  
   1.215 -val t = (Thm.term_of o the o (TermC.parse thy)) "(a + 3) + 4";
   1.216 +val t = @{term "(a + 3) + 4 :: real"};
   1.217  val SOME (str, term) = get_pair thy isa_str eval_fn t;
   1.218  if str =  "#: a + 3 + 4 = a + 7" andalso UnparseC.term term = "a + 3 + 4 = a + 7"
   1.219  then () else error "get_pair  (a + 3) + 4  changed";
   1.220  
   1.221 -val t = (Thm.term_of o the o (TermC.parse thy)) "x = 5 * (3 + (4 + a))";
   1.222 +val t = @{term "x = 5 * (3 + (4 + a) :: real)"};
   1.223  val SOME (str, term) = get_pair thy isa_str eval_fn t;
   1.224  if str =  "#: 3 + (4 + a) = 7 + a" andalso UnparseC.term term = "3 + (4 + a) = 7 + a"
   1.225  then ((* !!! gets subterm !!!*)) else error "get_pair  x = 5 * (3 + (4 + a))  (subterm) changed";
   1.226  
   1.227  val SOME (isa_str, eval_fn) = LibraryC.assoc (KEStore_Elems.get_calcs @{theory}, "DIVIDE");
   1.228  
   1.229 -val t = (Thm.term_of o the o (TermC.parse thy)) "-4 / -2";
   1.230 +val t = @{term "-4 / -2 :: real"};
   1.231  val SOME (str, term) = get_pair thy isa_str eval_fn t;
   1.232 -if str =  "#divide_e-4_-2" andalso UnparseC.term term = "-4 / -2 = 2"
   1.233 +if str = "#divide_e~4_~2" andalso UnparseC.term term = "- 4 / - 2 = 2"
   1.234  then () else error "get_pair  -4 / -2   changed";
   1.235  
   1.236  val SOME (isa_str, eval_fn) = LibraryC.assoc (KEStore_Elems.get_calcs @{theory}, "POWER");
   1.237  
   1.238 -val t = (Thm.term_of o the o (TermC.parse thy)) "2 \<up> 3";
   1.239 +val t = @{term "2 \<up> 3 :: real"};
   1.240  val SOME (str, term) = get_pair thy isa_str eval_fn t;
   1.241  if str =  "#: 2 \<up> 3 = 8" andalso UnparseC.term term = "2 \<up> 3 = 8"
   1.242  then () else error "get_pair  2 \<up> 3   changed";
   1.243 @@ -368,18 +410,17 @@
   1.244  "----------- fun adhoc_thm: examples -----------------------------------------------------------";
   1.245  (*--------------------------------------------------------------------vvvvvvvvvv*)
   1.246  val SOME (isa_str, eval_fn) = LibraryC.assoc (KEStore_Elems.get_calcs @{theory}, "is_const");
   1.247 -val SOME t = parseNEW @{context} "9 is_const";
   1.248 +val t = @{term "9 is_const"};
   1.249  val SOME (str, thm) = adhoc_thm thy (isa_str, eval_fn) t;
   1.250  if str = "#is_const_9_" andalso ThmC_Def.string_of_thm thm = "(9 is_const) = True"
   1.251  then () else error "adhoc_thm  9 is_const  changed";
   1.252  
   1.253 -
   1.254  case assoc_calc thy "Orderings.ord_class.less" of
   1.255    "le" => () | _ => error "Orderings.ord_class.less <-> le changed";
   1.256 -(*--------------------------------------------------------------------vvvvvvvvvv*)
   1.257  val SOME (isa_str, eval_fn) = LibraryC.assoc (KEStore_Elems.get_calcs @{theory}, "le");
   1.258 +if isa_str = "Orderings.ord_class.less" then () else error "adhoc_thm (4 < 4) = False CHANGED";
   1.259  
   1.260 -val SOME t = parseNEW @{context} "4 < 4";
   1.261 +val t = @{term "4 < (4 :: real)"};
   1.262  val SOME (str, thm) = adhoc_thm thy (isa_str, eval_fn) t;
   1.263  if str = "#less_4_4" andalso ThmC_Def.string_of_thm thm = "(4 < 4) = False"
   1.264  then () else error "adhoc_thm  4 < 4  changed";
   1.265 @@ -388,35 +429,30 @@
   1.266  case adhoc_thm thy (isa_str, eval_fn) t of
   1.267  NONE => () | _ => error "adhoc_thm  a < 4  does NOT result in NONE";
   1.268  
   1.269 -
   1.270 -(*--------------------------------------------------------------------vvvvvvvvvv*)
   1.271  val SOME (isa_str, eval_fn) = LibraryC.assoc (KEStore_Elems.get_calcs @{theory}, "PLUS");
   1.272 -val SOME t = parseNEW @{context} "1 + 2";
   1.273 +val SOME t = parseNEW @{context} "1 + (2::real)";
   1.274  val SOME (str, thm) = adhoc_thm thy (isa_str, eval_fn) t;
   1.275  if str = "#: 1 + 2 = 3" andalso ThmC_Def.string_of_thm thm = "1 + 2 = 3"
   1.276  then () else error "adhoc_thm  1 + 2  changed";
   1.277  
   1.278 -(*--------------------------------------------------------------------vvvvvvvvvv*)
   1.279  val SOME (isa_str, eval_fn) = LibraryC.assoc (KEStore_Elems.get_calcs @{theory}, "DIVIDE");
   1.280 -val SOME t = parseNEW @{context} "6 / -8";
   1.281 +val t = @{term "6 / -8 :: real"};
   1.282  val SOME (str, thm) = adhoc_thm thy (isa_str, eval_fn) t;
   1.283 -if str = "#divide_e6_-8" andalso ThmC_Def.string_of_thm thm = "6 / -8 = -3 / 4"
   1.284 -then () else error "adhoc_thm  1 + 2  changed";
   1.285 -
   1.286 +if str = "#divide_e6_~8" andalso ThmC_Def.string_of_thm thm = "6 / - 8 = - 3 / 4"
   1.287 +then () else error "adhoc_thm  6 / -8 = - 3 / 4  changed";
   1.288  
   1.289  case assoc_calc thy "Prog_Expr.ident" of
   1.290    "ident" => () | _ => error "Prog_Expr.ident <-> ident  changed";
   1.291 -(*--------------------------------------------------------------------vvvvvvvvvv*)
   1.292  val SOME (isa_str, eval_fn) = LibraryC.assoc (KEStore_Elems.get_calcs @{theory}, "ident");
   1.293  
   1.294 -val SOME t = parseNEW @{context} "3 =!= 3";
   1.295 +val t = @{term "3 =!= (3 :: real)"};
   1.296  val SOME (str, thm) = adhoc_thm thy (isa_str, eval_fn) t;
   1.297  if str = "#ident_(3)_(3)" andalso ThmC_Def.string_of_thm thm = "(3 =!= 3) = True"
   1.298  then () else error "adhoc_thm  (3 =!= 3)  changed";
   1.299  
   1.300 -val SOME t = parseNEW @{context} "\<not> (4 + (4 * x + x ^ 2) =!= 0)";
   1.301 +val t = @{term "\<not> ((4 :: real) + (4 * x + x \<up> 2) =!= 0)"};
   1.302  val SOME (str, thm) = adhoc_thm thy (isa_str, eval_fn) t;
   1.303 -if str = "#ident_(4 + (4 * x + x ^ 2))_(0)" andalso ThmC_Def.string_of_thm thm = "(4 + (4 * x + x ^ 2) =!= 0) = False"
   1.304 +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"
   1.305  then () else error "adhoc_thm  (\<not> (4 + (4 * x + x ^ 2) =!= 0))  changed";
   1.306  
   1.307  "----------- fun power -------------------------------------------------------------------------";