test/Tools/isac/ProgLang/auto_prog.sml
author Walther Neuper <walther.neuper@jku.at>
Sun, 22 Sep 2019 16:52:14 +0200
changeset 59633 f854e130f851
child 59635 9fc1bb69813c
permissions -rw-r--r--
adopt new files of ProgLang in test/..
walther@59633
     1
(* Title:  ProgLang/auto_prog.sml
walther@59633
     2
   Author: Walther Neuper 190922
walther@59633
     3
   (c) due to copyright terms
walther@59633
     4
*)
walther@59633
     5
walther@59633
     6
"-----------------------------------------------------------------------------------------------";
walther@59633
     7
"-----------------------------------------------------------------------------------------------";
walther@59633
     8
"table of contents -----------------------------------------------------------------------------";
walther@59633
     9
"-----------------------------------------------------------------------------------------------";
walther@59633
    10
"-------- check auto-gen.script for Rewrite_Set_Inst -------------------------------------------";
walther@59633
    11
"-------- test the same called by interSteps norm_Poly -----------------------------------------";
walther@59633
    12
"-------- test the same called by interSteps norm_Rational -------------------------------------";
walther@59633
    13
"-------- fun op @@ ----------------------------------------------------------------------------";
walther@59633
    14
"-------- fun rules2scr_Rls --------------------------------------------------------------------";
walther@59633
    15
"-------- fun rule2stac, fun rule2stac_inst ----------------------------------------------------";
walther@59633
    16
"-------- fun stacpbls -------------------------------------------------------------------------";
walther@59633
    17
"-------- fun is_calc, fun op_of_calc ----------------------------------------------------------";
walther@59633
    18
"-----------------------------------------------------------------------------------------------";
walther@59633
    19
"-----------------------------------------------------------------------------------------------";
walther@59633
    20
"-----------------------------------------------------------------------------------------------";
walther@59633
    21
walther@59633
    22
walther@59633
    23
"-------- check auto-gen.script for Rewrite_Set_Inst -------------------------------------------";
walther@59633
    24
"-------- check auto-gen.script for Rewrite_Set_Inst -------------------------------------------";
walther@59633
    25
"-------- check auto-gen.script for Rewrite_Set_Inst -------------------------------------------";
walther@59633
    26
val rls = assoc_rls "integration";
walther@59633
    27
val Seq {scr = Prog auto_script,...} = rls;
walther@59633
    28
writeln(term2str auto_script);
walther@59633
    29
walther@59633
    30
if contain_bdv (get_rules rls) then ()
walther@59633
    31
else error "scrtools.sml: contain_bdv doesnt work for 'integration'";
walther@59633
    32
walther@59633
    33
if terms2str (formal_args auto_script) = "[\"t_t\",\"v\"]"
walther@59633
    34
then () else error "formal_args of auto-gen.script changed";
walther@59633
    35
init_istate (Rewrite_Set_Inst (["(''bdv'', x)"], "integration_rules")) 
walther@59633
    36
			      (str2term "someTermWithBdv");
walther@59633
    37
walther@59633
    38
"-------- test the same called by interSteps norm_Poly -----------------------------------------";
walther@59633
    39
"-------- test the same called by interSteps norm_Poly -----------------------------------------";
walther@59633
    40
"-------- test the same called by interSteps norm_Poly -----------------------------------------";
walther@59633
    41
val Seq {scr = Prog auto_script,...} = assoc_rls "norm_Poly";
walther@59633
    42
writeln(term2str auto_script);
walther@59633
    43
(*Program Stepwise t_t   =
walther@59633
    44
 (Try (Rewrite_Set discard_minus False) @@
walther@59633
    45
  Try (Rewrite_Set expand_poly_ False) @@
walther@59633
    46
  Try (Repeat (Calculate TIMES)) @@
walther@59633
    47
  Try (Rewrite_Set order_mult_rls_ False) @@
walther@59633
    48
  Try (Rewrite_Set simplify_power_ False) @@
walther@59633
    49
  Try (Rewrite_Set calc_add_mult_pow_ False) @@
walther@59633
    50
  Try (Rewrite_Set reduce_012_mult_ False) @@
walther@59633
    51
  Try (Rewrite_Set order_add_rls_ False) @@
walther@59633
    52
  Try (Rewrite_Set collect_numerals_ False) @@
walther@59633
    53
  Try (Rewrite_Set reduce_012_ False) @@
walther@59633
    54
  Try (Rewrite_Set discard_parentheses1 False))
walther@59633
    55
  ??.empty                                          ..WORKS, NEVERTHELESS *)
walther@59633
    56
atomty auto_script;
walther@59633
    57
walther@59633
    58
reset_states (); (*<-- evaluate, if ML-blocks are edited below*)
walther@59633
    59
CalcTree
walther@59633
    60
[(["Term (b + a - b)", "normalform N"], 
walther@59633
    61
  ("Poly",["polynomial","simplification"],
walther@59633
    62
  ["simplification","for_polynomials"]))];
walther@59633
    63
Iterator 1;
walther@59633
    64
moveActiveRoot 1;
walther@59633
    65
walther@59633
    66
autoCalculate 1 CompleteCalc;
walther@59633
    67
val ((pt,p),_) = get_calc 1;
walther@59633
    68
show_pt pt;
walther@59633
    69
(* isabisac17 = 15 [
walther@59633
    70
(([], Frm), Simplify (b + a - b)),
walther@59633
    71
(([1], Frm), b + a - b),
walther@59633
    72
(([1], Res), a),
walther@59633
    73
(([], Res), a)] *)
walther@59633
    74
walther@59633
    75
interSteps 1 ([], Res);
walther@59633
    76
val ((pt,p),_) = get_calc 1; show_pt pt;
walther@59633
    77
show_pt pt;
walther@59633
    78
(* isabisac17 = 15 [
walther@59633
    79
(([], Frm), Simplify (b + a - b)),
walther@59633
    80
(([1], Frm), b + a - b),
walther@59633
    81
(([1], Res), a),
walther@59633
    82
(([], Res), a)] *)
walther@59633
    83
walther@59633
    84
interSteps 1 ([1], Res);(*<SYSERROR><CALCID>1</CALCID><ERROR>error in kernel 8</ERROR></SYSERROR>*)
walther@59633
    85
"~~~~~ fun interSteps, args:"; val (cI, ip) = (1, ([1], Res));
walther@59633
    86
 val ((pt, p), tacis) = get_calc cI;
walther@59633
    87
(*if*) (not o is_interpos) ip = false;
walther@59633
    88
val ip' = lev_pred' pt ip;
walther@59633
    89
walther@59633
    90
(*Math_Engine.detailstep pt ip      ..ERROR interSteps>..>init_istate: "norm_Poly" has EmptyScr*)
walther@59633
    91
val ("donesteps", ctree_BEFORE_step_into, pos'_BEFORE_step_into) = Math_Engine.detailstep pt ip;
walther@59633
    92
"~~~~~ fun detailstep, args:"; val (pt, (pos as (p, _))) = (pt, ip);
walther@59633
    93
    val nd = Ctree.get_nd pt p
walther@59633
    94
    val cn = Ctree.children nd;
walther@59633
    95
(*if*) null cn = false;
walther@59633
    96
walther@59633
    97
"~~~~~ to detailrls return val:";
walther@59633
    98
(*else*) 
walther@59633
    99
val return_val = ("donesteps", pt, (p @ [length (Ctree.children (Ctree.get_nd pt p))], Ctree.Res));
walther@59633
   100
if p @ [length (Ctree.children (Ctree.get_nd pt p))] = [1, 4]
walther@59633
   101
then () else error "detailrls: auto-generated norm_Poly doesnt work";
walther@59633
   102
walther@59633
   103
val ((pt,p),_) = get_calc 1; show_pt pt;
walther@59633
   104
show_pt pt; (*[
walther@59633
   105
(([], Frm), Simplify (b + a - b)),
walther@59633
   106
(([1], Frm), b + a - b),
walther@59633
   107
(([1,1], Frm), b + a - b),
walther@59633
   108
(([1,1], Res), b + a + -1 * b),
walther@59633
   109
(([1,2], Res), a + b + -1 * b),
walther@59633
   110
(([1,3], Res), a + 0 * b),
walther@59633
   111
(([1,4], Res), a),
walther@59633
   112
(([1], Res), a),
walther@59633
   113
(([], Res), a)]  *)
walther@59633
   114
if existpt' ([1,4], Res) pt then ()
walther@59633
   115
else error  "auto-generated norm_Poly doesnt work";
walther@59633
   116
walther@59633
   117
"-------- test the same called by interSteps norm_Rational -------------------------------------";
walther@59633
   118
"-------- test the same called by interSteps norm_Rational -------------------------------------";
walther@59633
   119
"-------- test the same called by interSteps norm_Rational -------------------------------------";
walther@59633
   120
val Seq {scr = Prog auto_script,...} = assoc_rls "norm_Rational";
walther@59633
   121
writeln(term2str auto_script);
walther@59633
   122
atomty auto_script;
walther@59633
   123
(*** 
walther@59633
   124
*** Const (Program.Stepwise, 'z => 'z => 'z)
walther@59633
   125
*** . Free (t_t, 'z)
walther@59633
   126
*** . Const (Program.Seq, ('a => 'a) => ('a => 'a) => 'a => 'a)
walther@59633
   127
*** . . Const (Program.Try, ('a => 'a) => 'a => 'a)
walther@59633
   128
*** . . . Const (Program.Rewrite'_Set, ID => bool => 'a => 'a)
walther@59633
   129
*** . . . . Free (discard_minus, ID)
walther@59633
   130
*** . . . . Const (HOL.False, bool)
walther@59633
   131
*** . . Const (Program.Seq, ('a => 'a) => ('a => 'a) => 'a => 'a)
walther@59633
   132
*** . . . Const (Program.Try, ('a => 'a) => 'a => 'a)
walther@59633
   133
*** . . . . Const (Program.Rewrite'_Set, ID => bool => 'a => 'a)
walther@59633
   134
*** . . . . . Free (rat_mult_poly, ID)
walther@59633
   135
*** . . . . . Const (HOL.False, bool)
walther@59633
   136
*** . . . Const (Program.Seq, ('a => 'a) => ('a => 'a) => 'a => 'a)
walther@59633
   137
*** . . . . Const (Program.Try, ('a => 'a) => 'a => 'a)
walther@59633
   138
*** . . . . . Const (Program.Rewrite'_Set, ID => bool => 'a => 'a)
walther@59633
   139
*** . . . . . . Free (make_rat_poly_with_parentheses, ID)
walther@59633
   140
*** . . . . . . Const (HOL.False, bool)
walther@59633
   141
*** . . . . Const (Program.Seq, ('a => 'a) => ('a => 'a) => 'a => 'a)
walther@59633
   142
*** . . . . . Const (Program.Try, ('a => 'a) => 'a => 'a)
walther@59633
   143
*** . . . . . . Const (Program.Rewrite'_Set, ID => bool => 'a => 'a)
walther@59633
   144
*** . . . . . . . Free (cancel_p_rls, ID)
walther@59633
   145
*** . . . . . . . Const (HOL.False, bool)
walther@59633
   146
*** . . . . . Const (Program.Seq, ('a => 'a) => ('a => 'a) => 'a => 'a)
walther@59633
   147
*** . . . . . . Const (Program.Try, ('a => 'a) => 'a => 'a)
walther@59633
   148
*** . . . . . . . Const (Program.Rewrite'_Set, ID => bool => 'a => 'a)
walther@59633
   149
*** . . . . . . . . Free (norm_Rational_rls, ID)
walther@59633
   150
*** . . . . . . . . Const (HOL.False, bool)
walther@59633
   151
*** . . . . . . Const (Program.Try, ('a => 'a) => 'a => 'a)
walther@59633
   152
*** . . . . . . . Const (Program.Rewrite'_Set, ID => bool => 'a => 'a)
walther@59633
   153
*** . . . . . . . . Free (discard_parentheses1, ID)
walther@59633
   154
*** . . . . . . . . Const (HOL.False, bool)
walther@59633
   155
*** . . Const (empty, 'a)
walther@59633
   156
***)
walther@59633
   157
reset_states ();
walther@59633
   158
CalcTree
walther@59633
   159
[(["Term (b + a - b)", "normalform N"], 
walther@59633
   160
  ("Poly",["polynomial","simplification"],
walther@59633
   161
  ["simplification","of_rationals"]))];
walther@59633
   162
Iterator 1;
walther@59633
   163
moveActiveRoot 1;
walther@59633
   164
autoCalculate 1 CompleteCalc;
walther@59633
   165
walther@59633
   166
interSteps 1 ([], Res);
walther@59633
   167
val ((pt,p),_) = get_calc 1; show_pt pt;
walther@59633
   168
walther@59633
   169
interSteps 1 ([1], Res);
walther@59633
   170
val ((pt,p),_) = get_calc 1; show_pt pt;
walther@59633
   171
walther@59633
   172
(*with "Program SimplifyScript (t_::real) =                \
walther@59633
   173
       \  ((Rewrite_Set norm_Rational False) t_)"
walther@59633
   174
val (Form form, SOME tac, asm) = pt_extract (pt, ([1], Res));
walther@59633
   175
*)
walther@59633
   176
val (Form form, SOME tac, asm) = pt_extract (pt, ([2], Res));
walther@59633
   177
case (term2str form, tac, terms2strs asm) of
walther@59633
   178
    ("a", Check_Postcond ["polynomial", "simplification"], []) => ()
walther@59633
   179
  | _ => error "scrtools.sml: auto-generated norm_Rational doesnt work";
walther@59633
   180
walther@59633
   181
"-------- fun op @@ ----------------------------------------------------------------------------";
walther@59633
   182
"-------- fun op @@ ----------------------------------------------------------------------------";
walther@59633
   183
"-------- fun op @@ ----------------------------------------------------------------------------";
walther@59633
   184
 val rules = (#rules o rep_rls) isolate_root;
walther@59633
   185
 val rs = map (rule2stac @{theory}) rules;
walther@59633
   186
 val t = @@ rs;
walther@59633
   187
if term2str t = "Try (Repeat (Rewrite ''rroot_to_lhs'' False)) @@\n" ^ 
walther@59633
   188
  "Try (Repeat (Rewrite ''rroot_to_lhs_mult'' False)) @@\n" ^ 
walther@59633
   189
  "Try (Repeat (Rewrite ''rroot_to_lhs_add_mult'' False)) @@\n" ^ 
walther@59633
   190
  "Try (Repeat (Rewrite ''risolate_root_add'' False)) @@\n" ^ 
walther@59633
   191
  "Try (Repeat (Rewrite ''risolate_root_mult'' False)) @@\n" ^ 
walther@59633
   192
  "Try (Repeat (Rewrite ''risolate_root_div'' False))"
walther@59633
   193
then () else error "fun @@ changed"
walther@59633
   194
walther@59633
   195
"-------- fun rules2scr_Rls --------------------------------------------------------------------";
walther@59633
   196
"-------- fun rules2scr_Rls --------------------------------------------------------------------";
walther@59633
   197
"-------- fun rules2scr_Rls --------------------------------------------------------------------";
walther@59633
   198
(*compare Prog_Expr.*)
walther@59633
   199
val prog = Auto_Prog.rules2scr_Rls @{theory} [Rule.Thm ("thm111", @{thm thm111}), Rule.Thm ("refl", @{thm refl})]
walther@59633
   200
;
walther@59633
   201
writeln (t2str @{theory} prog);
walther@59633
   202
(*auto_generated t_t =
walther@59633
   203
Repeat
walther@59633
   204
 ((Try (Repeat (Rewrite ''thm111'' False)) @@
walther@59633
   205
   Try (Repeat (Rewrite ''refl'' False)))
walther@59633
   206
   ??.empty)*)
walther@59633
   207
;
walther@59633
   208
if t2str @{theory} prog =
walther@59633
   209
"auto_generated t_t =\nRepeat\n ((Try (Repeat (Rewrite ''thm111'' False)) @@\n   Try (Repeat (Rewrite ''refl'' False)))\n   ??.empty)"
walther@59633
   210
then () else error "rules2scr_Rls auto_generated changed"
walther@59633
   211
walther@59633
   212
"-------- fun rule2stac, fun rule2stac_inst ----------------------------------------------------";
walther@59633
   213
"-------- fun rule2stac, fun rule2stac_inst ----------------------------------------------------";
walther@59633
   214
"-------- fun rule2stac, fun rule2stac_inst ----------------------------------------------------";
walther@59633
   215
val t = rule2stac @{theory} (Thm ("real_diff_minus", num_str @{thm real_diff_minus}));
walther@59633
   216
if term2str t = "Try (Repeat (Rewrite ''real_diff_minus'' False))" then ()
walther@59633
   217
else error "rule2stac Thm.. changed";
walther@59633
   218
walther@59633
   219
val t = rule2stac @{theory} (Calc ("Groups.plus_class.plus", eval_binop "#add_"));
walther@59633
   220
if term2str t = "Try (Repeat (Calculate ''PLUS''))" then ()
walther@59633
   221
else error "rule2stac Calc.. changed";
walther@59633
   222
walther@59633
   223
val t = rule2stac @{theory} (Rls_ rearrange_assoc);
walther@59633
   224
if term2str t = "Try (Rewrite_Set ''rearrange_assoc'' False)" then ()
walther@59633
   225
else error "rule2stac Rls_.. changed";
walther@59633
   226
walther@59633
   227
val t = rule2stac_inst @{theory} (Thm ("risolate_bdv_add", num_str @{thm risolate_bdv_add}));
walther@59633
   228
if term2str t = "Try (Repeat (Rewrite_Inst [(''bdv'', v)] ''risolate_bdv_add'' False))" then ()
walther@59633
   229
else error "rule2stac_inst Thm.. changed";
walther@59633
   230
case t of
walther@59633
   231
  (Const ("Tactical.Try", _) $
walther@59633
   232
    (Const ("Tactical.Repeat", _) $
walther@59633
   233
      (Const ("Prog_Tac.Rewrite'_Inst", _) $
walther@59633
   234
        (Const ("List.list.Cons", _) $
walther@59633
   235
          (Const ("Product_Type.Pair", _) $
walther@59633
   236
            bdv $
walther@59633
   237
            Free ("v", _)) $
walther@59633
   238
          Const ("List.list.Nil", _)) $
walther@59633
   239
        risolate_bdv_add $
walther@59633
   240
        Const ("HOL.False", _)))) => 
walther@59633
   241
      (if HOLogic.dest_string bdv = "bdv" andalso 
walther@59633
   242
        HOLogic.dest_string risolate_bdv_add = "risolate_bdv_add" then ()
walther@59633
   243
      else error "rule2stac_inst changed 1")
walther@59633
   244
| _ => error "rule2stac_inst changed 2";
walther@59633
   245
walther@59633
   246
"-------- fun stacpbls -------------------------------------------------------------------------";
walther@59633
   247
"-------- fun stacpbls -------------------------------------------------------------------------";
walther@59633
   248
"-------- fun stacpbls -------------------------------------------------------------------------";
walther@59633
   249
val {scr, ...} = get_met ["Test","sqrt-equ-test"]; val Prog sc = scr; val ts = stacpbls sc;
walther@59633
   250
case stacpbls sc of
walther@59633
   251
  [Const ("Prog_Tac.Rewrite", _) $ square_equation_left $ Const ("HOL.True", _),
walther@59633
   252
   Const ("Prog_Tac.Rewrite'_Set", _) $ Test_simplify $ Const ("HOL.False", _),
walther@59633
   253
   Const ("Prog_Tac.Rewrite'_Set", _) $ rearrange_assoc $ Const ("HOL.False", _),
walther@59633
   254
   Const ("Prog_Tac.Rewrite'_Set", _) $ isolate_root $ Const ("HOL.False", _),
walther@59633
   255
   Const ("Prog_Tac.Rewrite'_Set", _) $ norm_equation $ Const ("HOL.False", _),
walther@59633
   256
   Const ("Prog_Tac.Rewrite'_Set'_Inst", _) $
walther@59633
   257
     (Const ("List.list.Cons", _) $ (Const ("Product_Type.Pair", _) $ bdv $ Free ("v_v", _)) $
walther@59633
   258
       Const ("List.list.Nil", _)) $
walther@59633
   259
      isolate_bdv $ Const ("HOL.False", _)] => 
walther@59633
   260
     if HOLogic.dest_string square_equation_left = "square_equation_left" andalso
walther@59633
   261
       HOLogic.dest_string Test_simplify = "Test_simplify" andalso
walther@59633
   262
       HOLogic.dest_string rearrange_assoc = "rearrange_assoc" andalso
walther@59633
   263
       HOLogic.dest_string isolate_root = "isolate_root" andalso
walther@59633
   264
       HOLogic.dest_string norm_equation = "norm_equation" andalso
walther@59633
   265
       HOLogic.dest_string bdv = "bdv" andalso
walther@59633
   266
       HOLogic.dest_string isolate_bdv = "isolate_bdv"
walther@59633
   267
     then () else error "stacpbls (Test.Solve_root_equation) changed 2"
walther@59633
   268
| _  => error "stacpbls (Test.Solve_root_equation) changed 1";
walther@59633
   269
walther@59633
   270
(* inappropriate input is skipped *)
walther@59633
   271
val t = @{term "(a::real) = (rhs (NTH 1 sol_a))"};
walther@59633
   272
case stacpbls t of [] => () | _ => error "stacpbls rhs (NTH 1 sol_a) changed";
walther@59633
   273
walther@59633
   274
@{term "(SubProblem (BiegelinieX,[vonBelastungZu,Biegelinien], [Biegelinien,ausBelastung]) [REAL q__q, REAL v_v])"};
walther@59633
   275
case stacpbls t of [] => () | _ => error "stacpbls (SubProblem ...) changed";
walther@59633
   276
walther@59633
   277
"-------- fun is_calc, fun op_of_calc ----------------------------------------------------------";
walther@59633
   278
"-------- fun is_calc, fun op_of_calc ----------------------------------------------------------";
walther@59633
   279
"-------- fun is_calc, fun op_of_calc ----------------------------------------------------------";
walther@59633
   280
val rls = prep_rls @{theory} Test_simplify; (* adds the Program *)
walther@59633
   281
val Prog sc = (#scr o rep_rls) rls;
walther@59633
   282
val stacs = stacpbls sc;
walther@59633
   283
walther@59633
   284
val calcs = filter is_calc stacs;
walther@59633
   285
val ids = map op_of_calc calcs;
walther@59633
   286
case ids of
walther@59633
   287
  ["PLUS", "TIMES", "DIVIDE", "POWER"] => ()
walther@59633
   288
| _ => error "op_of_calc";
walther@59633
   289
walther@59633
   290
val calcs = ((assoc_calc' @{theory} |> map) o (map Auto_Prog.op_of_calc) o
walther@59633
   291
  (filter Auto_Prog.is_calc) o Auto_Prog.stacpbls) sc;
walther@59633
   292
case calcs of
walther@59633
   293
  [("PLUS", ("Groups.plus_class.plus", _)), ("TIMES", ("Groups.times_class.times", _)), 
walther@59633
   294
  ("DIVIDE", ("Rings.divide_class.divide", _)), ("POWER", ("Prog_Expr.pow", _))] => ()
walther@59633
   295
| _ => error "get_calcs changed"