src/Tools/isac/Interpret/solve-step.sml
author Walther Neuper <walther.neuper@jku.at>
Mon, 04 May 2020 12:38:16 +0200
changeset 59935 16927a749dd7
parent 59933 92214be419b2
child 59936 554030065b5b
permissions -rw-r--r--
end cleanup Interpret/*, preliminary

preliminary means: no canonical argument order
     1 (* Title:  Specify/solve-step.sml
     2    Author: Walther Neuper
     3    (c) due to copyright terms
     4 
     5 Code for the solve-phase in analogy to structure Specify_Step for the specify-phase.
     6 *)
     7 
     8 signature SOLVE_STEP =
     9 sig
    10   val check: Tactic.input -> Calc.T -> Applicable.T
    11   val add: Tactic.T -> Istate_Def.T * Proof.context -> Calc.T -> Generate.test_out
    12 
    13   val add_general: Tactic.T -> Istate_Def.T * Proof.context -> Calc.T -> Generate.test_out
    14   val s_add_general: State_Steps.T ->
    15     Ctree.ctree * Pos.pos' list * Pos.pos' -> Ctree.ctree * Pos.pos' list * Pos.pos'
    16   val add_hard:
    17     theory -> Tactic.T -> Pos.pos' -> Ctree.ctree -> Generate.test_out
    18 
    19   val get_ruleset: 'a -> Pos.pos -> Ctree.ctree ->
    20     string * ThyC.id * Rule_Def.rew_ord' * Rule_Def.rule_set * bool
    21   val get_eval: string -> Pos.pos ->Ctree.ctree ->
    22     string * ThyC.id * (string * Rule_Def.eval_fn)
    23 
    24 (* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
    25   (*NONE*)                                                     
    26 (*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
    27   val rew_info: Rule_Def.rule_set -> string * Rule_Def.rule_set * Rule_Def.calc list
    28 ( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
    29 end
    30 
    31 (**)
    32 structure Solve_Step(** ): SOLVE_STEP( **) =
    33 struct
    34 (**)
    35 
    36 (** get data from Calc.T **)
    37 
    38 (* the source is the parent node, either a problem or a Rule_Set (with inter_steps) *)
    39 fun rew_info (Rule_Def.Repeat {erls, rew_ord = (rew_ord', _), calc = ca, ...}) =
    40     (rew_ord', erls, ca)
    41   | rew_info (Rule_Set.Sequence {erls, rew_ord = (rew_ord', _), calc = ca, ...}) =
    42     (rew_ord', erls, ca)
    43   | rew_info (Rule_Set.Rrls {erls, rew_ord = (rew_ord', _), calc = ca, ...}) =
    44     (rew_ord', erls, ca)
    45   | rew_info rls = error ("rew_info called with '" ^ Rule_Set.id rls ^ "'");
    46 
    47 fun get_ruleset _ p pt = 
    48   let 
    49     val (pbl, p', rls') = Ctree.parent_node pt p
    50   in                                                      
    51     if pbl
    52     then 
    53       let 
    54         val thy' = Ctree.get_obj Ctree.g_domID pt p'
    55         val {rew_ord', erls, ...} = Specify.get_met (Ctree.get_obj Ctree.g_metID pt p')              
    56 	    in ("OK", thy', rew_ord', erls, false) end
    57      else 
    58       let
    59         val thy' = Ctree.get_obj Ctree.g_domID pt (Ctree.par_pblobj pt p)
    60 		    val (rew_ord', erls, _) = rew_info rls'
    61 		  in ("OK", thy', rew_ord', erls, false) end
    62   end;
    63 
    64 fun get_eval scrop p pt = 
    65   let
    66     val (pbl, p', rls') =  Ctree.parent_node pt p
    67   in
    68     if pbl
    69     then
    70       let
    71         val thy' = Ctree.get_obj Ctree.g_domID pt p'
    72         val {calc = scr_isa_fns, ...} = Specify.get_met (Ctree.get_obj Ctree.g_metID pt p')
    73         val opt = assoc (scr_isa_fns, scrop)
    74 	    in
    75 	      case opt of
    76 	        SOME isa_fn => ("OK", thy', isa_fn)
    77 	      | NONE => ("applicable_in Calculate: unknown '" ^ scrop ^ "'", "", ("", Eval_Def.e_evalfn))
    78 	    end
    79     else 
    80 		  let
    81 		    val thy' = Ctree.get_obj Ctree.g_domID pt (Ctree.par_pblobj pt p);
    82 		    val (_, _,(*_,*)scr_isa_fns) = rew_info rls'(*rls*)
    83 		  in
    84 		    case assoc (scr_isa_fns, scrop) of
    85 		      SOME isa_fn => ("OK",thy',isa_fn)
    86 		    | NONE => ("applicable_in Calculate: unknown '" ^ scrop ^ "'", "", ("", Eval_Def.e_evalfn))
    87 		  end
    88   end;
    89 
    90 (** Solve_Step.check **)
    91 
    92 (*
    93   check tactics (input by the user, mostly) for applicability
    94   and determine as much of the result of the tactic as possible initially.
    95 *)
    96 fun check (Tactic.Apply_Method mI) (pt, (p, _)) =
    97       let
    98         val (dI, pI, probl, ctxt) = case Ctree.get_obj I pt p of
    99           Ctree.PblObj {origin = (_, (dI, pI, _), _), probl, ctxt, ...} => (dI, pI, probl, ctxt)
   100         | _ => raise ERROR "Specify_Step.check Apply_Method: uncovered case Ctree.get_obj"
   101         val {where_, ...} = Specify.get_pbt pI
   102         val pres = map (Model.mk_env probl |> subst_atomic) where_
   103         val ctxt = if ContextC.is_empty ctxt (*vvvvvvvvvvvvvv DO THAT EARLIER?!?*)
   104           then ThyC.get_theory dI |> Proof_Context.init_global |> ContextC.insert_assumptions pres
   105           else ctxt
   106       in
   107         Applicable.Yes (Tactic.Apply_Method' (mI, NONE, Istate_Def.empty (*filled later*), ctxt))
   108       end
   109   | check (Tactic.Calculate op_) (cs as (pt, (p, _))) =
   110       let 
   111         val (msg, thy', isa_fn) = get_eval op_ p pt;
   112         val f = Calc.current_formula cs;
   113       in
   114         if msg = "OK"
   115         then
   116     	    case Rewrite.calculate_ (ThyC.get_theory thy') isa_fn f of
   117     	      SOME (f', (id, thm))
   118     	        => Applicable.Yes (Tactic.Calculate' (thy', op_, f, (f', (id, thm))))
   119     	    | NONE => Applicable.No ("'calculate " ^ op_ ^ "' not applicable") 
   120         else Applicable.No msg                                              
   121       end
   122   | check (Tactic.Check_Postcond pI) (_, _) = (*TODO: only applicable, if evaluating to True*)
   123       Applicable.Yes (Tactic.Check_Postcond' (pI, TermC.empty))
   124   | check (Tactic.Check_elementwise pred) cs =
   125       let 
   126         val f = Calc.current_formula cs;
   127       in
   128         Applicable.Yes (Tactic.Check_elementwise' (f, pred, (f, [])))
   129       end
   130   | check Tactic.Empty_Tac _ = Applicable.No "Empty_Tac is not applicable"
   131   | check (Tactic.Free_Solve) _ = Applicable.Yes (Tactic.Free_Solve')
   132   | check Tactic.Or_to_List cs =
   133        let 
   134         val f = Calc.current_formula cs;
   135         val ls = Prog_Expr.or2list f;
   136       in
   137         Applicable.Yes (Tactic.Or_to_List' (f, ls))
   138       end
   139   | check (Tactic.Rewrite thm) (cs as (pt, (p, _))) = 
   140       let
   141         val (msg, thy', ro, rls', _) = get_ruleset thm p pt;
   142         val thy = ThyC.get_theory thy';
   143         val f = Calc.current_formula cs;
   144       in
   145         if msg = "OK" 
   146         then
   147           case Rewrite.rewrite_ thy (Rewrite_Ord.assoc_rew_ord ro) rls' false (snd thm) f of
   148             SOME (f',asm) => Applicable.Yes (Tactic.Rewrite' (thy', ro, rls', false, thm, f, (f', asm)))
   149           | NONE => Applicable.No ((thm |> fst |> quote) ^ " not applicable") 
   150         else Applicable.No msg
   151       end
   152   | check (Tactic.Rewrite_Inst (subs, thm)) (cs as (pt, (p, _))) = 
   153       let 
   154         val pp = Ctree.par_pblobj pt p;
   155         val thy' = Ctree.get_obj Ctree.g_domID pt pp;
   156         val thy = ThyC.get_theory thy';
   157         val {rew_ord' = ro', erls = erls, ...} = Specify.get_met (Ctree.get_obj Ctree.g_metID pt pp);
   158         val f = Calc.current_formula cs;
   159         val subst = Subst.T_from_input thy subs; (*TODO: input requires parse _: _ -> _ option*)
   160       in 
   161         case Rewrite.rewrite_inst_ thy (Rewrite_Ord.assoc_rew_ord ro') erls false subst (snd thm) f of
   162           SOME (f', asm) =>
   163             Applicable.Yes (Tactic.Rewrite_Inst' (thy', ro', erls, false, subst, thm, f, (f', asm)))
   164         | NONE => Applicable.No (fst thm ^ " not applicable")
   165       end
   166   | check (Tactic.Rewrite_Set rls) (cs as (pt, (p, _))) =
   167       let 
   168         val pp = Ctree.par_pblobj pt p; 
   169         val thy' = Ctree.get_obj Ctree.g_domID pt pp;
   170         val f = Calc.current_formula cs;
   171       in
   172         case Rewrite.rewrite_set_ (ThyC.get_theory thy') false (assoc_rls rls) f of
   173           SOME (f', asm)
   174             => Applicable.Yes (Tactic.Rewrite_Set' (thy', false, assoc_rls rls, f, (f', asm)))
   175           | NONE => Applicable.No (rls ^ " not applicable")
   176       end
   177   | check (Tactic.Rewrite_Set_Inst (subs, rls)) (cs as (pt, (p, _))) =
   178       let 
   179         val pp = Ctree.par_pblobj pt p;
   180         val thy' = Ctree.get_obj Ctree.g_domID pt pp;
   181         val thy = ThyC.get_theory thy';
   182         val f = Calc.current_formula cs;
   183     	  val subst = Subst.T_from_input thy subs; (*TODO: input requires parse _: _ -> _ option*)
   184       in 
   185         case Rewrite.rewrite_set_inst_ thy false subst (assoc_rls rls) f of
   186           SOME (f', asm)
   187             => Applicable.Yes (Tactic.Rewrite_Set_Inst' (thy', false, subst, assoc_rls rls, f, (f', asm)))
   188         | NONE => Applicable.No (rls ^ " not applicable")
   189       end
   190   | check (Tactic.Subproblem (domID, pblID)) (_, _) = 
   191       Applicable.Yes (Tactic.Subproblem' ((domID, pblID, Method.id_empty), [], 
   192 			  TermC.empty, [], ContextC.empty, Auto_Prog.subpbl domID pblID))
   193    | check (Tactic.Substitute sube) (cs as (pt, (p, _))) =
   194       let
   195         val pp = Ctree.par_pblobj pt p
   196         val thy = ThyC.get_theory (Ctree.get_obj Ctree.g_domID pt pp)
   197         val f = Calc.current_formula cs;
   198 		    val {rew_ord', erls, ...} = Specify.get_met (Ctree.get_obj Ctree.g_metID pt pp)
   199 		    val subte = Subst.input_to_terms sube (*TODO: input requires parse _: _ -> _ option*)
   200 		    val subst = Subst.T_from_string_eqs thy sube
   201 		    val ro = Rewrite_Ord.assoc_rew_ord rew_ord'
   202 		  in
   203 		    if foldl and_ (true, map TermC.contains_Var subte)
   204 		    then (*1*)
   205 		      let val f' = subst_atomic subst f
   206 		      in if f = f'
   207 		        then Applicable.No (Subst.string_eqs_to_string sube ^ " not applicable")
   208 		        else Applicable.Yes (Tactic.Substitute' (ro, erls, subte, f, f'))
   209 		      end
   210 		    else (*2*)
   211 		      case Rewrite.rewrite_terms_ thy ro erls subte f of
   212 		        SOME (f', _) =>  Applicable.Yes (Tactic.Substitute' (ro, erls, subte, f, f'))
   213 		      | NONE => Applicable.No (Subst.string_eqs_to_string sube ^ " not applicable")
   214 		  end
   215   | check (Tactic.Tac id) (cs as (pt, (p, _))) =
   216       let 
   217         val pp = Ctree.par_pblobj pt p; 
   218         val thy' = Ctree.get_obj Ctree.g_domID pt pp;
   219         val thy = ThyC.get_theory thy';
   220         val f = Calc.current_formula cs;
   221       in case id of
   222         "subproblem_equation_dummy" =>
   223     	  if TermC.is_expliceq f
   224     	  then Applicable.Yes (Tactic.Tac_ (thy, UnparseC.term f, id, "subproblem_equation_dummy (" ^ UnparseC.term f ^ ")"))
   225     	  else Applicable.No "applicable only to equations made explicit"
   226       | "solve_equation_dummy" =>
   227     	  let val (id', f') = ApplicableOLD.split_dummy (UnparseC.term f);
   228     	  in
   229     	    if id' <> "subproblem_equation_dummy"
   230     	    then Applicable.No "no subproblem"
   231     	    else if (ThyC.to_ctxt thy, f') |-> TermC.parseNEW |> the |> TermC.is_expliceq
   232     		    then Applicable.Yes (Tactic.Tac_ (thy, UnparseC.term f, id, "[" ^ f' ^ "]"))
   233     		    else error ("Solve_Step.check: f= " ^ f')
   234         end
   235       | _ => Applicable.Yes (Tactic.Tac_ (thy, UnparseC.term f, id, UnparseC.term f))
   236       end
   237   | check (Tactic.Take str) _ = Applicable.Yes (Tactic.Take' (TermC.str2term str)) (* always applicable ?*)
   238   | check (Tactic.Begin_Trans) cs =
   239       Applicable.Yes (Tactic.Begin_Trans' (Calc.current_formula cs))
   240   | check (Tactic.End_Trans) (pt, (p, p_)) = (*TODO: check parent branches*)
   241     if p_ = Pos.Res 
   242 	  then Applicable.Yes (Tactic.End_Trans' (Ctree.get_obj Ctree.g_result pt p))
   243     else Applicable.No "'End_Trans' is not applicable at the beginning of a transitive sequence"
   244   | check Tactic.End_Proof' _ = Applicable.Yes Tactic.End_Proof''
   245   | check m _ = raise ERROR ("Solve_Step.check called for " ^ Tactic.input_to_string m);
   246 
   247 
   248 (** Solve_Step.add **)
   249 
   250 fun add (Tactic.Apply_Method' (_, topt, is, _)) (_, ctxt) (pt, pos as (p, _)) = 
   251     (case topt of 
   252       SOME t => 
   253         let val (pt, c) = Ctree.cappend_form pt p (is, ctxt) t
   254         in (pos, c, Generate.EmptyMout, pt) end
   255     | NONE => (pos, [], Generate.EmptyMout, pt))
   256   | add (Tactic.Take' t) l (pt, (p, _)) = (* val (Take' t) = m; *)
   257     let
   258       val p =
   259         let val (ps, p') = split_last p (* no connex to prev.ppobj *)
   260 	      in if p' = 0 then ps @ [1] else p end
   261       val (pt, c) = Ctree.cappend_form pt p l t
   262     in
   263       ((p, Pos.Frm), c, Generate.FormKF (UnparseC.term t), pt)
   264     end
   265   | add (Tactic.Begin_Trans' t) l (pt, (p, Pos.Frm)) =
   266     let
   267       val (pt, c) = Ctree.cappend_form pt p l t
   268       val pt = Ctree.update_branch pt p Ctree.TransitiveB (*040312*)
   269       (* replace the old PrfOjb ~~~~~ *)
   270       val p = (Pos.lev_on o Pos.lev_dn (* starts with [...,0] *)) p
   271       val (pt, c') = Ctree.cappend_form pt p l t (*FIXME.0402 same istate ???*)
   272     in
   273       ((p, Pos.Frm), c @ c', Generate.FormKF (UnparseC.term t), pt)
   274     end
   275   | add (Tactic.Begin_Trans' t) l (pt, (p, Pos.Res)) = 
   276     (*append after existing PrfObj    vvvvvvvvvvvvv*)
   277     add (Tactic.Begin_Trans' t) l (pt, (Pos.lev_on p, Pos.Frm))
   278   | add (Tactic.End_Trans' tasm) l (pt, (p, _)) =
   279     let
   280       val p' = Pos.lev_up p
   281       val (pt, c) = Ctree.append_result pt p' l tasm Ctree.Complete
   282     in
   283       ((p', Pos.Res), c, Generate.FormKF "DUMMY" (*term2str t ..ERROR (t) has not been declared*), pt)
   284     end
   285   | add (Tactic.Rewrite_Inst' (_, _, _, _, subs', thm', f, (f', asm))) (is, ctxt) (pt, (p, _)) =
   286     let
   287       val (pt, c) = Ctree.cappend_atomic pt p (is, ctxt) f
   288         (Tactic.Rewrite_Inst (Subst.T_to_input subs', thm')) (f',asm) Ctree.Complete;
   289       val pt = Ctree.update_branch pt p Ctree.TransitiveB
   290     in
   291       ((p, Pos.Res), c, Generate.FormKF (UnparseC.term f'), pt)
   292     end
   293  | add (Tactic.Rewrite' (_, _, _, _, thm', f, (f', asm))) (is, ctxt) (pt, (p, _)) =
   294    let
   295      val (pt, c) = Ctree.cappend_atomic pt p (is, ctxt) f (Tactic.Rewrite thm') (f', asm) Ctree.Complete
   296      val pt = Ctree.update_branch pt p Ctree.TransitiveB
   297    in
   298     ((p, Pos.Res), c, Generate.FormKF (UnparseC.term f'), pt)
   299    end
   300   | add (Tactic.Rewrite_Set_Inst' (_, _, subs', rls', f, (f', asm))) (is, ctxt) (pt, (p, _)) =
   301     let
   302       val (pt, c) = Ctree.cappend_atomic pt p (is, ctxt) f 
   303         (Tactic.Rewrite_Set_Inst (Subst.T_to_input subs', Rule_Set.id rls')) (f', asm) Ctree.Complete
   304       val pt = Ctree.update_branch pt p Ctree.TransitiveB
   305     in
   306       ((p, Pos.Res), c, Generate.FormKF (UnparseC.term f'), pt)
   307     end
   308   | add (Tactic.Rewrite_Set' (_, _, rls', f, (f', asm))) (is, ctxt) (pt, (p, _)) =
   309     let
   310       val (pt, c) = Ctree.cappend_atomic pt p (is, ctxt) f 
   311         (Tactic.Rewrite_Set (Rule_Set.id rls')) (f', asm) Ctree.Complete
   312       val pt = Ctree.update_branch pt p Ctree.TransitiveB
   313     in
   314       ((p, Pos.Res), c, Generate.FormKF (UnparseC.term f'), pt)
   315     end
   316   | add (Tactic.Check_Postcond' (_, scval)) l (pt, (p, _)) =
   317       let
   318         val (pt, c) = Ctree.append_result pt p l (scval, []) Ctree.Complete
   319       in
   320         ((p, Pos.Res), c, Generate.FormKF (UnparseC.term scval), pt)
   321       end
   322   | add (Tactic.Calculate' (_, op_, f, (f', _))) l (pt, (p, _)) =
   323       let
   324         val (pt,c) = Ctree.cappend_atomic pt p l f (Tactic.Calculate op_) (f', []) Ctree.Complete
   325       in
   326         ((p, Pos.Res), c, Generate.FormKF (UnparseC.term f'), pt)
   327       end
   328   | add (Tactic.Check_elementwise' (consts, pred, (f', asm))) l (pt, (p, _)) =
   329       let
   330         val (pt,c) = Ctree.cappend_atomic pt p l consts (Tactic.Check_elementwise pred) (f', asm) Ctree.Complete
   331       in
   332         ((p, Pos.Res), c, Generate.FormKF (UnparseC.term f'), pt)
   333       end
   334   | add (Tactic.Or_to_List' (ors, list)) l (pt, (p, _)) =
   335       let
   336         val (pt,c) = Ctree.cappend_atomic pt p l ors Tactic.Or_to_List (list, []) Ctree.Complete
   337       in
   338         ((p, Pos.Res), c, Generate.FormKF (UnparseC.term list), pt)
   339       end
   340   | add (Tactic.Substitute' (_, _, subte, t, t')) l (pt, (p, _)) =
   341       let
   342         val (pt,c) =
   343           Ctree.cappend_atomic pt p l t (Tactic.Substitute (Subst.eqs_to_input subte)) (t',[]) Ctree.Complete
   344         in ((p, Pos.Res), c, Generate.FormKF (UnparseC.term t'), pt) 
   345         end
   346   | add (Tactic.Tac_ (_, f, id, f')) l (pt, (p, _)) =
   347       let
   348         val (pt, c) = Ctree.cappend_atomic pt p l (TermC.str2term f) (Tactic.Tac id) (TermC.str2term f', []) Ctree.Complete
   349       in
   350         ((p,Pos.Res), c, Generate.FormKF f', pt)
   351       end
   352   | add (Tactic.Subproblem' ((domID, pblID, metID), oris, hdl, fmz_, ctxt_specify, f))
   353       (l as (_, ctxt)) (pt, (p, _)) =
   354       let
   355   	    val (pt, c) = Ctree.cappend_problem pt p l (fmz_, (domID, pblID, metID))
   356   	      (oris, (domID, pblID, metID), hdl, ctxt_specify)
   357   	    val f = Syntax.string_of_term (ThyC.to_ctxt (Proof_Context.theory_of ctxt)) f
   358       in
   359         ((p, Pos.Pbl), c, Generate.FormKF f, pt)
   360       end
   361   | add m' _ (_, pos) =
   362       raise ERROR ("Solve_Step.add: not impl.for " ^ Tactic.string_of m' ^ " at " ^ Pos.pos'2str pos)
   363 
   364 (* LI switches between solve-phase and specify-phase *)
   365 fun add_general tac ic cs =
   366   if Tactic.for_specify' tac
   367   then Specify_Step.add tac ic cs
   368   else add tac ic cs
   369 
   370 (* the order of State_Steps is reversed: insert last element first  *)
   371 fun s_add_general [] ptp = ptp
   372   | s_add_general tacis (pt, c, _) = 
   373     let
   374       val (tacis', (_, tac_, (p, is))) = split_last tacis
   375 	    val (p', c', _, pt') = add_general tac_ is (pt, p)
   376     in
   377       s_add_general tacis' (pt', c@c', p')
   378     end
   379 
   380 (* a still undeveloped concept: do a calculation without LI *)
   381 fun add_hard _(*thy*) m' (p, p_) pt =
   382   let  
   383     val p = case p_ of
   384       Pos.Frm => p | Pos.Res => Pos.lev_on p
   385     | _ => error ("generate_hard: call by " ^ Pos.pos'2str (p,p_))
   386   in
   387     add_general m' (Istate_Def.empty, ContextC.empty) (pt, (p, p_))
   388   end
   389 
   390 (**)end(**);