test/Tools/isac/ProgLang/calculate.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Sun, 25 Feb 2018 10:19:18 +0100
changeset 59384 d92ff7591a11
parent 59382 364ce4699452
child 59387 ae0b7006fc28
permissions -rw-r--r--
Rewrite: clean tests
neuper@38022
     1
(* Title:  test calculation of values for function constants
neuper@38022
     2
   Author: Walther Neuper 2000
neuper@38022
     3
   (c) copyright due to lincense terms.
neuper@37906
     4
neuper@38022
     5
12345678901234567890123456789012345678901234567890123456789012345678901234567890
neuper@38022
     6
        10        20        30        40        50        60        70        80
neuper@37906
     7
*)
neuper@37906
     8
neuper@38022
     9
"--------------------------------------------------------";
neuper@38022
    10
"table of contents --------------------------------------";
neuper@38022
    11
"--------------------------------------------------------";
neuper@55366
    12
"-calculate.thy: provides 'setup' -----------------------";
neuper@41924
    13
"----------- fun norm -----------------------------------";
wneuper@59255
    14
"----------- check return value of adhoc_thm  ----";
neuper@38032
    15
"----------- fun calculate_ -----------------------------";
neuper@55366
    16
"----------- calculate from script --requires 'setup'----";
neuper@38032
    17
"----------- calculate check test-root-equ --------------";
wneuper@59253
    18
"----------- check calcul,ate bottom up -----------------";
neuper@42223
    19
"----------- Atools.pow Power.power_class.power ---------";
neuper@41934
    20
" ================= calculate.sml: calculate_ 2002 ======";
neuper@38032
    21
"----------- get_pair with 3 args -----------------------";
neuper@38032
    22
"----------- calculate (2 * x is_const) -----------------";
neuper@38022
    23
"--------------------------------------------------------";
neuper@38022
    24
"--------------------------------------------------------";
neuper@38022
    25
"--------------------------------------------------------";
neuper@37906
    26
wneuper@59255
    27
"----------- check return value of adhoc_thm  ----";
wneuper@59255
    28
"----------- check return value of adhoc_thm  ----";
wneuper@59255
    29
"----------- check return value of adhoc_thm  ----";
neuper@41924
    30
val thy = @{theory "Poly"};
s1210629013@52153
    31
val cal = snd (assoc_calc' thy "is_polyexp");
neuper@38022
    32
val t = @{term "(x / x) is_polyexp"};
wneuper@59255
    33
val SOME (thmID, thm) = adhoc_thm thy cal t;
wneuper@59188
    34
(HOLogic.dest_Trueprop (Thm.prop_of thm); writeln "all thms wrapped by Trueprop")
neuper@38022
    35
handle TERM _ => 
wneuper@59255
    36
       error "calculate.sml: adhoc_thm must return Trueprop";
neuper@38022
    37
neuper@38033
    38
"----------- fun calculate_ -----------------------------";
neuper@38033
    39
"----------- fun calculate_ -----------------------------";
neuper@38033
    40
"----------- fun calculate_ -----------------------------";
neuper@41924
    41
val thy = @{theory "Test"};
neuper@38033
    42
"===== test 1";
wneuper@59188
    43
val t = (Thm.term_of o the o (parse thy)) "1+2";
wneuper@59255
    44
val SOME (thmID,thm) = adhoc_thm thy (the(assoc(calclist,"PLUS"))) t;
wneuper@59195
    45
term2str (Thm.prop_of thm) = "1 + 2 = 3";
neuper@38033
    46
neuper@38033
    47
"===== test 2";
wneuper@59188
    48
val t = (Thm.term_of o the o (parse thy)) "((1+2)*4/3)^^^2";
wneuper@59255
    49
val SOME (thmID,thm) = adhoc_thm thy (the(assoc(calclist,"PLUS"))) t;
neuper@38033
    50
val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
neuper@41931
    51
if term2str t = "(3 * 4 / 3) ^^^ 2" then ()
neuper@41931
    52
else error "calculate.sml: ((1+2)*4/3)^^^2 --> (3 * 4 / 3) ^^^ 2";
neuper@38033
    53
neuper@38033
    54
"===== test 3b -- 2 contiued";
wneuper@59255
    55
val SOME (thmID,thm) = adhoc_thm thy (the(assoc(calclist,"TIMES"))) t;
neuper@38033
    56
val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
neuper@38033
    57
term2str t;
neuper@38033
    58
(*val it = "(#12 // #3) ^^^ #2" : string*)
neuper@37906
    59
neuper@38033
    60
"===== test 4";
wneuper@59255
    61
val SOME (thmID,thm) = adhoc_thm thy(the(assoc(calclist,"DIVIDE")))t;
neuper@38032
    62
val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
neuper@38033
    63
term2str t;
neuper@38033
    64
(*it = "#4 ^^^ #2" : string*)
neuper@38033
    65
neuper@38033
    66
"===== test 5";
wneuper@59255
    67
val SOME (thmID,thm) = adhoc_thm thy(the(assoc(calclist,"POWER")))t;
neuper@38032
    68
val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
neuper@41931
    69
(*show_types := false;*)
neuper@38037
    70
if term2str t <> "16" then error "calculate.sml: new behaviour in calculate_"
neuper@37906
    71
else ();
neuper@37906
    72
neuper@37906
    73
neuper@37906
    74
val fmz = ["realTestGiven (((1+2)*4/3)^^^2)","realTestFind s"];
neuper@37906
    75
val (dI',pI',mI') =
neuper@38035
    76
  ("Test",["calculate","test"],["Test","test_calculate"]);
neuper@38035
    77
neuper@37906
    78
val (p,_,f,nxt,_,pt) = CalcTreeTEST [(fmz, (dI',pI',mI'))];
neuper@37906
    79
val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
neuper@37906
    80
(*nxt =("Add_Given",Add_Given "realTestGiven (((#1 + #2) * #4 // #3) ^^^#2)")*)
neuper@37906
    81
val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
neuper@37906
    82
(*nxt = ("Add_Find",Add_Find "realTestFind s") : string * tac*)
neuper@37906
    83
val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
neuper@38035
    84
(*nxt = ("Specify_Theory",Specify_Theory "Test") : string * tac*)
neuper@37906
    85
val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
neuper@37906
    86
(*nxt = ("Specify_Problem",Specify_Problem ["calculate","test"])*)
neuper@37906
    87
val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
neuper@38035
    88
(*nxt = ("Specify_Method",Specify_Method ("Test","test_calculate"))*)
neuper@37906
    89
val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
neuper@38035
    90
(*nxt = ("Apply_Method",Apply_Method ("Test","test_calculate"))*)
neuper@37906
    91
neuper@37906
    92
val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
neuper@38033
    93
(*nxt = ("Calculate",Calculate "PLUS")*)
neuper@37906
    94
val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
neuper@38033
    95
(*nxt = ("Calculate",Calculate "TIMES")*)
neuper@37906
    96
val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
neuper@38033
    97
(*nxt = ("Calculate",Calculate "DIVIDE")*)
neuper@37906
    98
val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
neuper@38033
    99
(*nxt = ("Calculate",Calculate "POWER")*)
neuper@37906
   100
val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
neuper@37906
   101
(*nxt = ("Check_Postcond",Check_Postcond ["calculate","test"])*)
neuper@37906
   102
val (p,_,f,nxt,_,pt) = me nxt p [1] pt;
neuper@37906
   103
(*nxt = ("End_Proof'",End_Proof')*)
wneuper@59267
   104
case f of FormKF "16" => () | _ =>
wneuper@59253
   105
error "calculate.sml: script test_calculate changed behaviour";
neuper@37906
   106
neuper@37906
   107
neuper@38032
   108
"----------- calculate check test-root-equ --------------";
neuper@38032
   109
"----------- calculate check test-root-equ --------------";
neuper@38032
   110
"----------- calculate check test-root-equ --------------";
neuper@37906
   111
(*(1): 2nd Test_simplify didn't work:
neuper@37906
   112
val ct =
neuper@37906
   113
  "sqrt (x ^^^ 2 + -3 * x) = (-3 + 2 * x + -1 * (9 + 4 * x)) / (-1 * 2)"
neuper@37906
   114
> val rls = ("Test_simplify");
neuper@37906
   115
> val (ct,_) = the (rewrite_set thy' ("tval_rls") false rls ct);
neuper@37906
   116
val ct = "sqrt (x ^^^ 2 + -3 * x) =
neuper@37906
   117
(-9) / (-2) + (-3 / (-2) + (x * ((-4) / (-2)) + x * (2 / (-2))))";
neuper@37906
   118
ie. cancel does not work properly
neuper@37906
   119
*)
wneuper@59382
   120
 val thy = @{theory "Test"};
wneuper@59382
   121
 val op_ = the (assoc (calclist, "DIVIDE"));
wneuper@59382
   122
 val ct = numbers_to_string @{term
wneuper@59382
   123
   "sqrt (x ^^^ 2 + -3 * x) = (-9) / (-2) + (-3 / (-2) + (x * ((-4) / (-2)) + x * (2 / (-2))))"};
wneuper@59382
   124
case calculate_ thy op_ ct of
wneuper@59382
   125
  SOME _ => ()
wneuper@59382
   126
| NONE => error "calculate_ test-root-equ changed";
neuper@37906
   127
(*
neuper@37906
   128
           sqrt (x ^^^ 2 + -3 * x) =\
neuper@37906
   129
 \(-9) / (-2) + (-3 / (-2) + (x * ((-4) / (-2)) + x * (2 / (-2))))
neuper@37906
   130
............... does not work *)
neuper@37906
   131
neuper@37906
   132
(*--------------(2): does divide work in Test_simplify ?: ------*)
akargl@42188
   133
 val thy = @{theory Test};
wneuper@59188
   134
 val t = (Thm.term_of o the o (parse thy)) "6 / 2";
neuper@37906
   135
 val rls = Test_simplify;
neuper@37906
   136
 val (t,_) = the (rewrite_set_ thy false rls t);
neuper@55279
   137
(*val t = Free ("3","Real.real") : term*)
neuper@37906
   138
neuper@37906
   139
(*--------------(3): is_const works ?: -------------------------------------*)
wneuper@59188
   140
 val t = (Thm.term_of o the o (parse @{theory Test})) "2 is_const";
neuper@37906
   141
 atomty t;
akargl@42188
   142
 rewrite_set_ @{theory Test} false tval_rls t;
neuper@41928
   143
(*val it = SOME (Const ("HOL.True","bool"),[]) ... works*)
neuper@37906
   144
neuper@37906
   145
 val t = str2term "2 * x is_const";
akargl@42188
   146
 val SOME (str,t') = eval_const "" "" t (@{theory "Isac"});
neuper@37906
   147
 term2str t';
neuper@37906
   148
 
neuper@37906
   149
neuper@38032
   150
"----------- check calculate bottom up ------------------";
neuper@38032
   151
"----------- check calculate bottom up ------------------";
neuper@38032
   152
"----------- check calculate bottom up ------------------";
neuper@37906
   153
(*-------------- eval_cancel works: *)
neuper@52070
   154
 trace_rewrite:=false;
akargl@42188
   155
 val thy = @{theory Test};
wneuper@59384
   156
 val rls = Test_simplify;
wneuper@59188
   157
 val t = (Thm.term_of o the o (parse thy)) "(-4) / 2";
akargl@42188
   158
wneuper@59360
   159
val SOME (_, t) = eval_cancel "xxx" "Rings.divide_class.divide" t thy;
akargl@42188
   160
neuper@37906
   161
(*--------------(5): reproduce (1) with simpler term: ------------*)
wneuper@59384
   162
 val t = (Thm.term_of o the o (parse thy)) "(3+5)/2";
wneuper@59384
   163
case rewrite_set_ thy false rls t of
wneuper@59384
   164
  SOME (Free ("4", _), []) => ()
wneuper@59384
   165
| _ => error "rewrite_set_ (3+5)/2 changed";
neuper@37906
   166
wneuper@59384
   167
 val t = (Thm.term_of o the o (parse thy)) "(3+1+2*x)/2";
wneuper@59384
   168
case rewrite_set_ thy false rls t of
wneuper@59384
   169
  SOME (Const ("Groups.plus_class.plus", _) $ Free ("2", _) $ Free ("x", _), []) => ()
wneuper@59384
   170
| _ => error "rewrite_set_ (3+1+2*x)/2 changed";
neuper@37906
   171
neuper@52070
   172
 trace_rewrite:=false; (*=true3.6.03*)
akargl@42218
   173
neuper@37906
   174
(*--- trace_rewrite before correction of ... --------------------
neuper@37906
   175
 val ct = "(-3 + 2 * x + -1) / 2";
neuper@37906
   176
 val (ct,_) = the (rewrite_set thy'  false rls ct);
neuper@37906
   177
:
neuper@37906
   178
### trying thm 'root_ge0_2'
neuper@37906
   179
### rewrite_set_: x + (-1 + -3) / 2
neuper@37906
   180
### trying thm 'radd_real_const_eq'
neuper@37906
   181
### trying thm 'radd_real_const'
neuper@37906
   182
### rewrite_set_: x + (-4) / 2
neuper@37906
   183
### trying thm 'rcollect_right'
neuper@37906
   184
:
neuper@37906
   185
"x + (-4) / 2"
neuper@37906
   186
-------------------------------------while before Isabelle20002:
neuper@37906
   187
 val ct = "(#-3 + #2 * x + #-1) // #2";
neuper@37906
   188
 val (ct,_) = the (rewrite_set thy'  false rls ct);
neuper@37906
   189
:
neuper@37906
   190
### trying thm 'root_ge0_2'
neuper@37906
   191
### rewrite_set_: x + (#-1 + #-3) // #2
neuper@37906
   192
### trying thm 'radd_real_const_eq'
neuper@37906
   193
### trying thm 'radd_real_const'
neuper@37906
   194
### rewrite_set_: x + #-4 // #2
neuper@37906
   195
### rewrite_set_: x + #-2
neuper@37906
   196
### trying thm 'rcollect_right'
neuper@37906
   197
:
neuper@37906
   198
"#-2 + x"
neuper@37906
   199
-----------------------------------------------------------------*)
neuper@37906
   200
neuper@37906
   201
neuper@37906
   202
(*===================*)
neuper@52070
   203
 trace_rewrite:=false; (*WN130722: =true stopped Test_Isac.thy*)
wneuper@59384
   204
 val t = (Thm.term_of o the o (parse thy))  "x + (-1 + -3) / 2";
wneuper@59384
   205
val SOME (res, []) = rewrite_set_ thy false rls t;
wneuper@59384
   206
if term2str res = "-2 + x" then () else error "rewrite_set_  x + (-1 + -3) / 2  changed";
neuper@37906
   207
"x + (-4) / 2";						
neuper@37906
   208
(*
neuper@37906
   209
### trying calc. 'cancel'
neuper@37906
   210
@@@ get_pair: binop, t = x + (-4) / 2
neuper@37906
   211
@@@ get_pair: t else
neuper@38032
   212
@@@ get_pair: t else -> NONE
neuper@37906
   213
@@@ get_pair: binop, t = (-4) / 2
neuper@37906
   214
@@@ get_pair: then 1
neuper@38032
   215
@@@ get_pair: t -> NONE
neuper@38032
   216
@@@ get_pair: t1 -> NONE
wneuper@59255
   217
@@@ adhoc_thm': NONE
neuper@37906
   218
### trying calc. 'pow'
neuper@37906
   219
*)
neuper@37906
   220
neuper@52070
   221
 trace_rewrite:=false; (*WN130722: =true stopped Test_Isac.thy*)
neuper@37906
   222
neuper@42223
   223
"----------- Atools.pow Power.power_class.power ---------";
neuper@42223
   224
"----------- Atools.pow Power.power_class.power ---------";
neuper@42223
   225
"----------- Atools.pow Power.power_class.power ---------";
wneuper@59188
   226
val t = (Thm.term_of o the o (parseold thy)) "1 ^ aaa";
neuper@42223
   227
atomty t;
neuper@37906
   228
(*** -------------
neuper@37906
   229
*** Const ( Nat.power, ['a, nat] => 'a)
neuper@37906
   230
*** . Free ( 1, 'a)
neuper@37906
   231
*** . Free ( aaa, nat) *)
akargl@42218
   232
neuper@42223
   233
val t = str2term "1 ^^^ aaa";
neuper@42223
   234
atomty t;
neuper@42223
   235
(**** 
neuper@42223
   236
*** Const (Atools.pow, real => real => real)
neuper@42223
   237
*** . Free (1, real)
neuper@42223
   238
*** . Free (aaa, real)
neuper@42223
   239
*** *);
neuper@37906
   240
neuper@37906
   241
" ================= calculate.sml: calculate_ 2002 =================== ";
neuper@37906
   242
" ================= calculate.sml: calculate_ 2002 =================== ";
neuper@37906
   243
" ================= calculate.sml: calculate_ 2002 =================== ";
neuper@37906
   244
akargl@42188
   245
val thy = @{theory Test};
wneuper@59188
   246
val t = (Thm.term_of o the o (parse thy)) "12 / 3";
wneuper@59255
   247
val SOME (thmID,thm) = adhoc_thm thy(the(assoc(calclist,"DIVIDE")))t;
neuper@38032
   248
val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
neuper@37906
   249
"12 / 3 = 4";
akargl@42188
   250
val thy = @{theory Test};
wneuper@59188
   251
val t = (Thm.term_of o the o (parse thy)) "4 ^^^ 2";
wneuper@59255
   252
val SOME (thmID,thm) = adhoc_thm thy(the(assoc(calclist,"POWER"))) t;
neuper@38032
   253
val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
neuper@37906
   254
"4 ^ 2 = 16";
neuper@37906
   255
wneuper@59188
   256
 val t = (Thm.term_of o the o (parse thy)) "((1 + 2) * 4 / 3) ^^^ 2";
wneuper@59255
   257
 val SOME (thmID,thm) = adhoc_thm thy (the(assoc(calclist,"PLUS"))) t;
neuper@37906
   258
"1 + 2 = 3";
neuper@38032
   259
 val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
neuper@38033
   260
 term2str t;
neuper@37906
   261
"(3 * 4 / 3) ^^^ 2";
wneuper@59255
   262
 val SOME (thmID,thm) = adhoc_thm thy (the(assoc(calclist,"TIMES")))t;
neuper@37906
   263
"3 * 4 = 12";
neuper@38032
   264
 val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
neuper@38033
   265
 term2str t;
neuper@37906
   266
"(12 / 3) ^^^ 2";
wneuper@59255
   267
 val SOME (thmID,thm) =adhoc_thm thy(the(assoc(calclist,"DIVIDE")))t;
neuper@37906
   268
"12 / 3 = 4";
neuper@38032
   269
 val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
neuper@38033
   270
 term2str t;
neuper@37906
   271
"4 ^^^ 2";
wneuper@59255
   272
 val SOME (thmID,thm) = adhoc_thm thy(the(assoc(calclist,"POWER")))t;
neuper@37906
   273
"4 ^^^ 2 = 16";
neuper@38032
   274
 val SOME (t,_) = rewrite_ thy tless_true tval_rls true thm t;
neuper@38033
   275
 term2str t;
neuper@37906
   276
"16";
neuper@38031
   277
 if it <> "16" then error "calculate.sml: new behaviour in calculate_"
neuper@37906
   278
 else ();
neuper@37906
   279
neuper@37906
   280
(*13.9.02 *** calc: operator = pow not defined*)
wneuper@59188
   281
  val t = (Thm.term_of o the o (parse thy)) "3^^^2";
neuper@38032
   282
  val SOME (thmID,thm) = 
wneuper@59255
   283
      adhoc_thm thy (the(assoc(calclist,"POWER"))) t;
neuper@37906
   284
(*** calc: operator = pow not defined*)
neuper@37906
   285
neuper@38033
   286
  val (op_, eval_fn) = the (assoc(calclist,"POWER"));
neuper@37906
   287
  (*
neuper@37906
   288
val op_ = "Atools.pow" : string
neuper@37906
   289
val eval_fn = fn : string -> term -> theory -> (string * term) option*)
neuper@37906
   290
neuper@38032
   291
  val SOME (thmid,t') = get_pair thy op_ eval_fn t;
neuper@37906
   292
(*** calc: operator = pow not defined*)
neuper@37906
   293
neuper@38032
   294
  val SOME (id,t') = eval_fn op_ t thy;
neuper@37906
   295
(*** calc: operator = pow not defined*)
neuper@37906
   296
neuper@37906
   297
  val (thmid, (Const (op0,t0) $ Free (n1,t1) $ Free(n2,t2))) = (op_, t);
neuper@38032
   298
  val SOME (id,t') = eval_binop thmid op_ t thy;
neuper@37906
   299
(*** calc: operator = pow not defined*)
neuper@37906
   300
akargl@42188
   301
neuper@37906
   302
"----------- get_pair with 3 args --------------------------------";
neuper@37906
   303
"----------- get_pair with 3 args --------------------------------";
neuper@37906
   304
"----------- get_pair with 3 args --------------------------------";
neuper@37906
   305
val (thy, op_, ef, arg) =
neuper@37906
   306
    (thy, "EqSystem.occur'_exactly'_in", 
wneuper@59361
   307
     assoc_calc' (Thy_Info_get_theory "EqSystem") "occur_exactly_in" |> snd |> snd,
neuper@37906
   308
     str2term
akargl@42188
   309
      "[] from [c, c_2, c_3, c_4] occur_exactly_in -1 * (q_0 * L ^^^ 2) / 2"
neuper@37906
   310
      );
neuper@38032
   311
val SOME (str, simpl) = get_pair thy op_ ef arg;
neuper@37906
   312
if str = 
akargl@42188
   313
"[] from [c, c_2, c_3, c_4] occur_exactly_in -1 * (q_0 * L ^^^ 2) / 2 = True"
neuper@38031
   314
then () else error "calculate.sml get_pair with 3 args:occur_exactly_in";
neuper@37906
   315
neuper@37906
   316
neuper@38032
   317
"----------- calculate (2 * x is_const) -----------------";
neuper@38032
   318
"----------- calculate (2 * x is_const) -----------------";
neuper@38032
   319
"----------- calculate (2 * x is_const) -----------------";
neuper@37906
   320
val t = str2term "2 * x is_const";
akargl@42188
   321
val SOME (str, t') = eval_const "" "" t @{theory Test};
neuper@37906
   322
term2str t';
neuper@37906
   323
"(2 * x is_const) = False";
neuper@37906
   324
akargl@42188
   325
val SOME (t',_) = rewrite_set_ @{theory Test} false tval_rls t;
neuper@37906
   326
term2str t';
neuper@41928
   327
"HOL.False";