test/Tools/isac/ProgLang/auto_prog.sml
author Walther Neuper <walther.neuper@jku.at>
Mon, 23 Mar 2020 13:31:29 +0100
changeset 59833 9331e61f55dd
parent 59802 43160c1e775a
child 59850 f3cac3053e7b
permissions -rw-r--r--
separate structure Detail_Step
     1 (* Title:  ProgLang/auto_prog.sml
     2    Author: Walther Neuper 190922
     3    (c) due to copyright terms
     4 *)
     5 
     6 "-----------------------------------------------------------------------------------------------";
     7 "-----------------------------------------------------------------------------------------------";
     8 "table of contents -----------------------------------------------------------------------------";
     9 "-----------------------------------------------------------------------------------------------";
    10 "-------- check auto-gen.script for Rewrite_Set_Inst -------------------------------------------";
    11 "-------- test the same called by interSteps norm_Poly -----------------------------------------";
    12 "-------- test the same called by interSteps norm_Rational -------------------------------------";
    13 "-------- fun op #> ----------------------------------------------------------------------------";
    14 "-------- fun rules2scr_Rls --------------------------------------------------------------------";
    15 "-------- fun rule2stac, fun rule2stac_inst ----------------------------------------------------";
    16 "-------- fun stacpbls, fun eval_leaf -----------------------------------";
    17 "-------- fun is_calc, fun op_of_calc ----------------------------------------------------------";
    18 "-------- fun subst_typ ------------------------------------------------------------------------";
    19 "-------- fun subst_typs -----------------------------------------------------------------------";
    20 "-----------------------------------------------------------------------------------------------";
    21 "-----------------------------------------------------------------------------------------------";
    22 "-----------------------------------------------------------------------------------------------";
    23 
    24 
    25 "-------- check auto-gen.script for Rewrite_Set_Inst -------------------------------------------";
    26 "-------- check auto-gen.script for Rewrite_Set_Inst -------------------------------------------";
    27 "-------- check auto-gen.script for Rewrite_Set_Inst -------------------------------------------";
    28 val rls = assoc_rls "integration";
    29 val thy' = @{theory "Integrate"}
    30 val t = @{term "ttt :: real"};
    31 
    32  Auto_Prog.gen thy' t rls;
    33 "~~~~~ fun generate , args:"; val (thy, t, rls) = (thy', t, rls);
    34     val prog = case rls of
    35 	      Rule.Rls {rules, ...} => rules2scr_Rls thy rules
    36 	    | Rule.Seq {rules, ...} => rules2scr_Seq thy rules
    37     val auto_script = subst_typs prog (type_of t) (TermC.guess_bdv_typ t) (*return from generate*);
    38 
    39 if term2str auto_script =
    40   "auto_generated_inst t_t v =\n" ^
    41   "(Try (Rewrite_Set_Inst [(''bdv'', v)] ''integration_rules'') #>\n" ^
    42   " Try (Rewrite_Set_Inst [(''bdv'', v)] ''add_new_c'') #>\n" ^
    43   " Try (Rewrite_Set_Inst [(''bdv'', v)] ''simplify_Integral''))\n" ^
    44   " ??.empty"
    45 then () else error "Auto_Prog.gen integration CHANGED";
    46 
    47 if contain_bdv (get_rules rls) then ()
    48 else error "scrtools.sml: contain_bdv doesnt work for 'integration'";
    49 
    50 if terms2str (formal_args auto_script) = "[\"t_t\",\"v\"]"
    51 then () else error "formal_args of auto-gen.script changed";
    52 
    53            init_istate (Rewrite_Set_Inst (["(''bdv'', x)"], "integration_rules"))
    54 			       (str2term "someTermWithBdv");
    55 "~~~~~ fun init_istate , args:"; val ((Tactic.Rewrite_Set_Inst (subs, rls)), t)
    56   = ((Rewrite_Set_Inst (["(''bdv'', x)"], "integration_rules")), str2term "someTermWithBdv");
    57       val v = case Selem.subs2subst (Celem.assoc_thy "Isac_Knowledge") subs of
    58         (_, v) :: _ => v;
    59     (*case*) assoc_rls rls (*of*);
    60 
    61 
    62 "-------- test the same called by interSteps norm_Poly -----------------------------------------";
    63 "-------- test the same called by interSteps norm_Poly -----------------------------------------";
    64 "-------- test the same called by interSteps norm_Poly -----------------------------------------";
    65 val rls = assoc_rls "norm_Poly";
    66 val thy' = @{theory "Poly"}
    67 val t = @{term "ttt :: real"}
    68 val auto_script = Auto_Prog.gen thy' t rls;
    69 
    70 if term2str auto_script =
    71   "auto_generated t_t =\n" ^
    72   "(Try (Rewrite_Set ''discard_minus'') #>\n" ^
    73   " Try (Rewrite_Set ''expand_poly_'') #>\n" ^
    74   " Try (Repeat (Calculate ''TIMES'')) #>\n" ^
    75   " Try (Rewrite_Set ''order_mult_rls_'') #>\n" ^
    76   " Try (Rewrite_Set ''simplify_power_'') #>\n" ^
    77   " Try (Rewrite_Set ''calc_add_mult_pow_'') #>\n" ^
    78   " Try (Rewrite_Set ''reduce_012_mult_'') #>\n" ^
    79   " Try (Rewrite_Set ''order_add_rls_'') #>\n" ^
    80   " Try (Rewrite_Set ''collect_numerals_'') #>\n" ^
    81   " Try (Rewrite_Set ''reduce_012_'') #>\n" ^
    82   " Try (Rewrite_Set ''discard_parentheses1''))\n" ^
    83   " ??.empty"  (*                          ..WORKS, NEVERTHELESS*)
    84 then () else error "Auto_Prog.gen norm_Poly CHANGED";
    85 
    86 reset_states (); (*<-- evaluate, if ML-blocks are edited below*)
    87 CalcTree
    88 [(["Term (b + a - b)", "normalform N"], 
    89   ("Poly",["polynomial","simplification"],
    90   ["simplification","for_polynomials"]))];
    91 Iterator 1;
    92 moveActiveRoot 1;
    93 
    94 autoCalculate 1 CompleteCalc;
    95 val ((pt,p),_) = get_calc 1;
    96 show_pt pt;
    97 (* isabisac17 = 15 [
    98 (([], Frm), Simplify (b + a - b)),
    99 (([1], Frm), b + a - b),
   100 (([1], Res), a),
   101 (([], Res), a)] *)
   102 
   103 interSteps 1 ([], Res);
   104 val ((pt,p),_) = get_calc 1; show_pt pt;
   105 show_pt pt;
   106 (* isabisac17 = 15 [
   107 (([], Frm), Simplify (b + a - b)),
   108 (([1], Frm), b + a - b),
   109 (([1], Res), a),
   110 (([], Res), a)] *)
   111 
   112 interSteps 1 ([1], Res);(*<SYSERROR><CALCID>1</CALCID><ERROR>error in kernel 8</ERROR></SYSERROR>*)
   113 "~~~~~ fun interSteps, args:"; val (cI, ip) = (1, ([1], Res));
   114  val ((pt, p), tacis) = get_calc cI;
   115 (*if*) (not o is_interpos) ip = false;
   116 val ip' = lev_pred' pt ip;
   117 
   118 (*Detail_Step.go pt ip      ..ERROR interSteps>..>init_istate: "norm_Poly" has EmptyScr*)
   119 val ("donesteps", ctree_BEFORE_step_into, pos'_BEFORE_step_into) = Detail_Step.go pt ip;
   120 "~~~~~ fun go, args:"; val (pt, (pos as (p, _))) = (pt, ip);
   121     val nd = Ctree.get_nd pt p
   122     val cn = Ctree.children nd;
   123 (*if*) null cn = false;
   124 
   125 "~~~~~ to detailrls return val:";
   126 (*else*) 
   127 val return_val = ("donesteps", pt, (p @ [length (Ctree.children (Ctree.get_nd pt p))], Pos.Res));
   128 if p @ [length (Ctree.children (Ctree.get_nd pt p))] = [1, 4]
   129 then () else error "detailrls: auto-generated norm_Poly doesnt work";
   130 
   131 val ((pt,p),_) = get_calc 1; show_pt pt;
   132 show_pt pt; (*[
   133 (([], Frm), Simplify (b + a - b)),
   134 (([1], Frm), b + a - b),
   135 (([1,1], Frm), b + a - b),
   136 (([1,1], Res), b + a + -1 * b),
   137 (([1,2], Res), a + b + -1 * b),
   138 (([1,3], Res), a + 0 * b),
   139 (([1,4], Res), a),
   140 (([1], Res), a),
   141 (([], Res), a)]  *)
   142 if existpt' ([1,4], Res) pt then ()
   143 else error  "auto-generated norm_Poly doesnt work";
   144 
   145 
   146 "-------- test the same called by interSteps norm_Rational -------------------------------------";
   147 "-------- test the same called by interSteps norm_Rational -------------------------------------";
   148 "-------- test the same called by interSteps norm_Rational -------------------------------------";
   149 val auto_script =
   150   Auto_Prog.gen  @{theory "Poly"} @{term "ttt :: real"} (assoc_rls "norm_Rational");
   151 writeln(term2str auto_script);
   152 atomty auto_script;
   153 (*** 
   154 *** Const (Program.Stepwise, 'z => 'z => 'z)
   155 *** . Free (t_t, 'z)
   156 *** . Const (Program.Seq, ('a => 'a) => ('a => 'a) => 'a => 'a)
   157 *** . . Const (Program.Try, ('a => 'a) => 'a => 'a)
   158 *** . . . Const (Program.Rewrite'_Set, ID => 'a => 'a)
   159 *** . . . . Free (discard_minus, ID)
   160 *** . . Const (Program.Seq, ('a => 'a) => ('a => 'a) => 'a => 'a)
   161 *** . . . Const (Program.Try, ('a => 'a) => 'a => 'a)
   162 *** . . . . Const (Program.Rewrite'_Set, ID  => 'a => 'a)
   163 *** . . . . . Free (rat_mult_poly, ID)
   164 *** . . . Const (Program.Seq, ('a => 'a) => ('a => 'a) => 'a => 'a)
   165 *** . . . . Const (Program.Try, ('a => 'a) => 'a => 'a)
   166 *** . . . . . Const (Program.Rewrite'_Set, ID  => 'a => 'a)
   167 *** . . . . . . Free (make_rat_poly_with_parentheses, ID)
   168 *** . . . . Const (Program.Seq, ('a => 'a) => ('a => 'a) => 'a => 'a)
   169 *** . . . . . Const (Program.Try, ('a => 'a) => 'a => 'a)
   170 *** . . . . . . Const (Program.Rewrite'_Set, ID  => 'a => 'a)
   171 *** . . . . . . . Free (cancel_p_rls, ID)
   172 *** . . . . . Const (Program.Seq, ('a => 'a) => ('a => 'a) => 'a => 'a)
   173 *** . . . . . . Const (Program.Try, ('a => 'a) => 'a => 'a)
   174 *** . . . . . . . Const (Program.Rewrite'_Set, ID  => 'a => 'a)
   175 *** . . . . . . . . Free (norm_Rational_rls, ID)
   176 *** . . . . . . Const (Program.Try, ('a => 'a) => 'a => 'a)
   177 *** . . . . . . . Const (Program.Rewrite'_Set, ID => 'a => 'a)
   178 *** . . . . . . . . Free (discard_parentheses1, ID)
   179 *** . . Const (empty, 'a)
   180 ***)
   181 reset_states ();
   182 CalcTree
   183 [(["Term (b + a - b)", "normalform N"], 
   184   ("Poly",["polynomial","simplification"],
   185   ["simplification","of_rationals"]))];
   186 Iterator 1;
   187 moveActiveRoot 1;
   188 autoCalculate 1 CompleteCalc;
   189 
   190 interSteps 1 ([], Res);
   191 val ((pt,p),_) = get_calc 1; show_pt pt;
   192 
   193 interSteps 1 ([1], Res);
   194 val ((pt,p),_) = get_calc 1; show_pt pt;
   195 
   196 (*with "Program SimplifyScript (t_::real) =                \
   197        \  ((Rewrite_Set norm_Rational) t_)"
   198 val (Form form, SOME tac, asm) = pt_extract (pt, ([1], Res));
   199 *)
   200 val (Form form, SOME tac, asm) = pt_extract (pt, ([2], Res));
   201 case (term2str form, tac, terms2strs asm) of
   202     ("a", Check_Postcond ["polynomial", "simplification"], []) => ()
   203   | _ => error "scrtools.sml: auto-generated norm_Rational doesnt work";
   204 
   205 
   206 "-------- fun op #> ----------------------------------------------------------------------------";
   207 "-------- fun op #> ----------------------------------------------------------------------------";
   208 "-------- fun op #> ----------------------------------------------------------------------------";
   209  val rules = (#rules o rep_rls) isolate_root;
   210  val rs = map (rule2stac @{theory}) rules;
   211  val t = #> rs;
   212 if term2str t = "Try (Repeat (Rewrite ''rroot_to_lhs'')) #>\n" ^ 
   213   "Try (Repeat (Rewrite ''rroot_to_lhs_mult'')) #>\n" ^ 
   214   "Try (Repeat (Rewrite ''rroot_to_lhs_add_mult'')) #>\n" ^ 
   215   "Try (Repeat (Rewrite ''risolate_root_add'')) #>\n" ^ 
   216   "Try (Repeat (Rewrite ''risolate_root_mult'')) #>\n" ^ 
   217   "Try (Repeat (Rewrite ''risolate_root_div''))"
   218 then () else error "fun #> changed"
   219 
   220 
   221 "-------- fun rules2scr_Rls --------------------------------------------------------------------";
   222 "-------- fun rules2scr_Rls --------------------------------------------------------------------";
   223 "-------- fun rules2scr_Rls --------------------------------------------------------------------";
   224 (*compare Prog_Expr.*)
   225 val prog = Auto_Prog.rules2scr_Rls @{theory} [Rule.Thm ("thm111", @{thm thm111}), Rule.Thm ("refl", @{thm refl})]
   226 ;
   227 writeln (t2str @{theory} prog);
   228 (*auto_generated t_t =
   229 Repeat
   230  ((Try (Repeat (Rewrite ''thm111'')) #>
   231    Try (Repeat (Rewrite ''refl'')))
   232    ??.empty)*)
   233 ;
   234 if t2str @{theory} prog =
   235   "auto_generated t_t =\n" ^
   236   "Repeat\n" ^
   237   " ((Try (Repeat (Rewrite ''thm111'')) #> Try (Repeat (Rewrite ''refl'')))\n" ^
   238   "   ??.empty)"
   239 then () else error "rules2scr_Rls auto_generated changed";
   240 
   241 
   242 "-------- fun rule2stac, fun rule2stac_inst ----------------------------------------------------";
   243 "-------- fun rule2stac, fun rule2stac_inst ----------------------------------------------------";
   244 "-------- fun rule2stac, fun rule2stac_inst ----------------------------------------------------";
   245 val t = rule2stac @{theory} (Thm ("real_diff_minus", num_str @{thm real_diff_minus}));
   246 if term2str t = "Try (Repeat (Rewrite ''real_diff_minus''))" then ()
   247 else error "rule2stac Thm.. changed";
   248 
   249 val t = rule2stac @{theory} (Num_Calc ("Groups.plus_class.plus", eval_binop "#add_"));
   250 if term2str t = "Try (Repeat (Calculate ''PLUS''))" then ()
   251 else error "rule2stac Num_Calc.. changed";
   252 
   253 val t = rule2stac @{theory} (Rls_ rearrange_assoc);
   254 if term2str t = "Try (Rewrite_Set ''rearrange_assoc'')" then ()
   255 else error "rule2stac Rls_.. changed";
   256 
   257 val t = rule2stac_inst @{theory} (Thm ("risolate_bdv_add", num_str @{thm risolate_bdv_add}));
   258 if term2str t = "Try (Repeat (Rewrite_Inst [(''bdv'', v)] ''risolate_bdv_add''))" then ()
   259 else error "rule2stac_inst Thm.. changed";
   260 case t of
   261   (Const ("Tactical.Try", _) $
   262     (Const ("Tactical.Repeat", _) $
   263       (Const ("Prog_Tac.Rewrite'_Inst", _) $
   264         (Const ("List.list.Cons", _) $
   265           (Const ("Product_Type.Pair", _) $
   266             bdv $
   267             Free ("v", _)) $
   268           Const ("List.list.Nil", _)) $
   269         risolate_bdv_add))) => 
   270       (if HOLogic.dest_string bdv = "bdv" andalso 
   271         HOLogic.dest_string risolate_bdv_add = "risolate_bdv_add" then ()
   272       else error "rule2stac_inst changed 1")
   273 | _ => error "rule2stac_inst changed 2";
   274 
   275 
   276 "-------- fun stacpbls, fun eval_leaf -----------------------------------";
   277 "-------- fun stacpbls, fun eval_leaf -----------------------------------";
   278 "-------- fun stacpbls, fun eval_leaf -----------------------------------";
   279 val {scr, ...} = get_met ["Test","sqrt-equ-test"]; val Prog sc = scr; val ts = stacpbls sc;
   280 case stacpbls sc of
   281   [Const ("Prog_Tac.Rewrite", _) $ square_equation_left,
   282    Const ("Prog_Tac.Rewrite'_Set", _) $ Test_simplify,
   283    Const ("Prog_Tac.Rewrite'_Set", _) $ rearrange_assoc,
   284    Const ("Prog_Tac.Rewrite'_Set", _) $ isolate_root,
   285    Const ("Prog_Tac.Rewrite'_Set", _) $ norm_equation,
   286    Const ("Prog_Tac.Rewrite'_Set'_Inst", _) $
   287      (Const ("List.list.Cons", _) $ (Const ("Product_Type.Pair", _) $ bdv $ Free ("v_v", _)) $
   288        Const ("List.list.Nil", _)) $ isolate_bdv] => 
   289      if HOLogic.dest_string square_equation_left = "square_equation_left" andalso
   290        HOLogic.dest_string Test_simplify = "Test_simplify" andalso
   291        HOLogic.dest_string rearrange_assoc = "rearrange_assoc" andalso
   292        HOLogic.dest_string isolate_root = "isolate_root" andalso
   293        HOLogic.dest_string norm_equation = "norm_equation" andalso
   294        HOLogic.dest_string bdv = "bdv" andalso
   295        HOLogic.dest_string isolate_bdv = "isolate_bdv"
   296      then () else error "stacpbls (Test.Solve_root_equation) changed 2"
   297 | _  => error "stacpbls (Test.Solve_root_equation) changed 1";
   298 
   299 (* inappropriate input is skipped *)
   300 val t = @{term "(a::real) = (rhs (NTH 1 sol_a))"};
   301 case stacpbls t of [] => () | _ => error "stacpbls rhs (NTH 1 sol_a) changed";
   302 
   303 @{term "(SubProblem (BiegelinieX,[vonBelastungZu,Biegelinien], [Biegelinien,ausBelastung]) [REAL q__q, REAL v_v])"};
   304 case stacpbls t of [] => () | _ => error "stacpbls (SubProblem ...) changed";
   305 
   306 
   307 "-------- fun is_calc, fun op_of_calc ----------------------------------------------------------";
   308 "-------- fun is_calc, fun op_of_calc ----------------------------------------------------------";
   309 "-------- fun is_calc, fun op_of_calc ----------------------------------------------------------";
   310 val rls = prep_rls @{theory} Test_simplify; (* adds the Program *)
   311 val sc = Auto_Prog.gen thy' t rls;
   312 val stacs = stacpbls sc;
   313 
   314 val calcs = filter is_calc stacs;
   315 val ids = map op_of_calc calcs;
   316 case ids of
   317   ["PLUS", "TIMES", "DIVIDE", "POWER"] => ()
   318 | _ => error "op_of_calc";
   319 
   320 val calcs = ((assoc_calc' @{theory} |> map) o (map Auto_Prog.op_of_calc) o
   321   (filter Auto_Prog.is_calc) o Auto_Prog.stacpbls) sc;
   322 case calcs of
   323   [("PLUS", ("Groups.plus_class.plus", _)), ("TIMES", ("Groups.times_class.times", _)), 
   324   ("DIVIDE", ("Rings.divide_class.divide", _)), ("POWER", ("Prog_Expr.pow", _))] => ()
   325 | _ => error "get_calcs changed"
   326 
   327 
   328 "-------- fun subst_typ ------------------------------------------------------------------------";
   329 "-------- fun subst_typ ------------------------------------------------------------------------";
   330 "-------- fun subst_typ ------------------------------------------------------------------------";
   331 val prog = Auto_Prog.gen @{theory Isac_Knowledge} t (assoc_rls "isolate_bdv");
   332 (* term2str prog |> writeln
   333 auto_generated_inst t_t v =
   334 Repeat
   335  ((Try (Repeat (Rewrite_Inst [(''bdv'', v)] ''risolate_bdv_add'')) #>
   336    Try (Repeat (Rewrite_Inst [(''bdv'', v)] ''risolate_bdv_mult_add'')) #>
   337    Try (Repeat (Rewrite_Inst [(''bdv'', v)] ''risolate_bdv_mult'')) #>
   338    Try (Repeat (Rewrite_Inst [(''bdv'', v)] ''mult_square'')) #>
   339    Try (Repeat (Rewrite_Inst [(''bdv'', v)] ''constant_square'')) #>
   340    Try (Repeat (Rewrite_Inst [(''bdv'', v)] ''constant_mult_square'')))
   341    ??.empty) 
   342 *)
   343 
   344 val frees = vars prog; (* = [Free ("t_t", "'z"), Free ("v", "real")]*)
   345 if length frees = 2 then () else error "test setup Auto_Prog.subst_typ changed 1";
   346 
   347 val args = formal_args prog; (* = [Free ("t_t", "'z"), Free ("v", "real")]*)
   348 if length args = 2 then () else error "test setup Auto_Prog.subst_typ changed 2";
   349 
   350 val typ_t = HOLogic.realT
   351 val typ_bdv = HOLogic.realT
   352 
   353 val vars_v = (* = [Free ("v", "real"), Free ("v", "'a")]*)
   354   filter (fn t => curry op = (t |> dest_Free |> fst) "v") frees
   355 val typs_v = (* = ["real", "'a"]*)
   356   map (fn t => (t |> dest_Free |> snd)) vars_v;
   357 
   358 val subst_ty = subst_typ "v" HOLogic.realT frees; (* = [("real", "real")]*)
   359 if length subst_ty = 1 then () else error "Auto_Prog.subst_typ changed";
   360 
   361 
   362 "-------- fun subst_typs -----------------------------------------------------------------------";
   363 "-------- fun subst_typs -----------------------------------------------------------------------";
   364 "-------- fun subst_typs -----------------------------------------------------------------------";
   365 val prog = Auto_Prog.gen thy' t (assoc_rls "isolate_bdv");
   366 val frees = vars prog; (* = [Free ("t_t", "'z"), Free ("v", "real"), Free ("v", "'a")]*)
   367 if length frees = 2 then () else error "test setup Auto_Prog.subst_typs changed 1";
   368 
   369 val args = formal_args prog; (* = [Free ("t_t", "'z"), Free ("v", "real")]*)
   370 if length args = 2 then () else error "test setup Auto_Prog.subst_typs changed 2";
   371 
   372 val prog' = subst_typs prog HOLogic.realT HOLogic.realT
   373 val frees' = vars prog'; (* = [Free ("t_t", "'z"), Free ("v", "real")]*);
   374 if length frees' = 2 then () else error "Auto_Prog.subst_typs changed 2"