separate Solve_Step.add, rearrange code, prep. Specify_Step
1 (* Title: Specify/solve-step.sml
3 (c) due to copyright terms
5 Code for the solve-phase in analogy to structure Specify_Step for the specify-phase.
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 val add_general: Tactic.T -> Istate_Def.T * Proof.context -> Calc.T -> Generate.test_out
13 val s_add_general: State_Steps.T ->
14 Ctree.ctree * Pos.pos' list * Pos.pos' -> Ctree.ctree * Pos.pos' list * Pos.pos'
16 (* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
18 (*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
20 ( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
24 structure Solve_Step(** ): SOLVE_STEP( **) =
29 check tactics (input by the user, mostly) for applicability
30 and determine as much of the result of the tactic as possible initially.
32 fun check (Tactic.Apply_Method mI) (pt, (p, _)) =
34 val (dI, pI, probl, ctxt) = case Ctree.get_obj I pt p of
35 Ctree.PblObj {origin = (_, (dI, pI, _), _), probl, ctxt, ...} => (dI, pI, probl, ctxt)
36 | _ => raise ERROR "Specify_Step.check Apply_Method: uncovered case Ctree.get_obj"
37 val {where_, ...} = Specify.get_pbt pI
38 val pres = map (Model.mk_env probl |> subst_atomic) where_
39 val ctxt = if ContextC.is_empty ctxt (*vvvvvvvvvvvvvv DO THAT EARLIER?!?*)
40 then ThyC.get_theory dI |> Proof_Context.init_global |> ContextC.insert_assumptions pres
43 Applicable.Yes (Tactic.Apply_Method' (mI, NONE, Istate_Def.empty (*filled later*), ctxt))
45 | check (Tactic.Calculate op_) (cs as (pt, (p, _))) =
47 val (msg, thy', isa_fn) = ApplicableOLD.from_pblobj_or_detail_calc op_ p pt;
48 val f = Calc.current_formula cs;
52 case Rewrite.calculate_ (ThyC.get_theory thy') isa_fn f of
54 => Applicable.Yes (Tactic.Calculate' (thy', op_, f, (f', (id, thm))))
55 | NONE => Applicable.No ("'calculate " ^ op_ ^ "' not applicable")
56 else Applicable.No msg
58 | check (Tactic.Check_Postcond pI) (_, _) = (*TODO: only applicable, if evaluating to True*)
59 Applicable.Yes (Tactic.Check_Postcond' (pI, TermC.empty))
60 | check (Tactic.Check_elementwise pred) cs =
62 val f = Calc.current_formula cs;
64 Applicable.Yes (Tactic.Check_elementwise' (f, pred, (f, [])))
66 | check Tactic.Empty_Tac _ = Applicable.No "Empty_Tac is not applicable"
67 | check (Tactic.Free_Solve) _ = Applicable.Yes (Tactic.Free_Solve')
68 | check Tactic.Or_to_List cs =
70 val f = Calc.current_formula cs;
71 val ls = Prog_Expr.or2list f;
73 Applicable.Yes (Tactic.Or_to_List' (f, ls))
75 | check (Tactic.Rewrite thm) (cs as (pt, (p, _))) =
77 val (msg, thy', ro, rls', _) = ApplicableOLD.from_pblobj_or_detail_thm thm p pt;
78 val thy = ThyC.get_theory thy';
79 val f = Calc.current_formula cs;
83 case Rewrite.rewrite_ thy (Rewrite_Ord.assoc_rew_ord ro) rls' false (snd thm) f of
84 SOME (f',asm) => Applicable.Yes (Tactic.Rewrite' (thy', ro, rls', false, thm, f, (f', asm)))
85 | NONE => Applicable.No ((thm |> fst |> quote) ^ " not applicable")
86 else Applicable.No msg
88 | check (Tactic.Rewrite_Inst (subs, thm)) (cs as (pt, (p, _))) =
90 val pp = Ctree.par_pblobj pt p;
91 val thy' = Ctree.get_obj Ctree.g_domID pt pp;
92 val thy = ThyC.get_theory thy';
93 val {rew_ord' = ro', erls = erls, ...} = Specify.get_met (Ctree.get_obj Ctree.g_metID pt pp);
94 val f = Calc.current_formula cs;
95 val subst = Subst.T_from_input thy subs; (*TODO: input requires parse _: _ -> _ option*)
97 case Rewrite.rewrite_inst_ thy (Rewrite_Ord.assoc_rew_ord ro') erls false subst (snd thm) f of
99 Applicable.Yes (Tactic.Rewrite_Inst' (thy', ro', erls, false, subst, thm, f, (f', asm)))
100 | NONE => Applicable.No (fst thm ^ " not applicable")
102 | check (Tactic.Rewrite_Set rls) (cs as (pt, (p, _))) =
104 val pp = Ctree.par_pblobj pt p;
105 val thy' = Ctree.get_obj Ctree.g_domID pt pp;
106 val f = Calc.current_formula cs;
108 case Rewrite.rewrite_set_ (ThyC.get_theory thy') false (assoc_rls rls) f of
110 => Applicable.Yes (Tactic.Rewrite_Set' (thy', false, assoc_rls rls, f, (f', asm)))
111 | NONE => Applicable.No (rls ^ " not applicable")
113 | check (Tactic.Rewrite_Set_Inst (subs, rls)) (cs as (pt, (p, _))) =
115 val pp = Ctree.par_pblobj pt p;
116 val thy' = Ctree.get_obj Ctree.g_domID pt pp;
117 val thy = ThyC.get_theory thy';
118 val f = Calc.current_formula cs;
119 val subst = Subst.T_from_input thy subs; (*TODO: input requires parse _: _ -> _ option*)
121 case Rewrite.rewrite_set_inst_ thy false subst (assoc_rls rls) f of
123 => Applicable.Yes (Tactic.Rewrite_Set_Inst' (thy', false, subst, assoc_rls rls, f, (f', asm)))
124 | NONE => Applicable.No (rls ^ " not applicable")
126 | check (Tactic.Subproblem (domID, pblID)) (_, _) =
127 Applicable.Yes (Tactic.Subproblem' ((domID, pblID, Method.id_empty), [],
128 TermC.empty, [], ContextC.empty, Auto_Prog.subpbl domID pblID))
129 | check (Tactic.Substitute sube) (cs as (pt, (p, _))) =
131 val pp = Ctree.par_pblobj pt p
132 val thy = ThyC.get_theory (Ctree.get_obj Ctree.g_domID pt pp)
133 val f = Calc.current_formula cs;
134 val {rew_ord', erls, ...} = Specify.get_met (Ctree.get_obj Ctree.g_metID pt pp)
135 val subte = Subst.input_to_terms sube (*TODO: input requires parse _: _ -> _ option*)
136 val subst = Subst.T_from_string_eqs thy sube
137 val ro = Rewrite_Ord.assoc_rew_ord rew_ord'
139 if foldl and_ (true, map TermC.contains_Var subte)
141 let val f' = subst_atomic subst f
143 then Applicable.No (Subst.string_eqs_to_string sube ^ " not applicable")
144 else Applicable.Yes (Tactic.Substitute' (ro, erls, subte, f, f'))
147 case Rewrite.rewrite_terms_ thy ro erls subte f of
148 SOME (f', _) => Applicable.Yes (Tactic.Substitute' (ro, erls, subte, f, f'))
149 | NONE => Applicable.No (Subst.string_eqs_to_string sube ^ " not applicable")
151 | check (Tactic.Tac id) (cs as (pt, (p, _))) =
153 val pp = Ctree.par_pblobj pt p;
154 val thy' = Ctree.get_obj Ctree.g_domID pt pp;
155 val thy = ThyC.get_theory thy';
156 val f = Calc.current_formula cs;
158 "subproblem_equation_dummy" =>
159 if TermC.is_expliceq f
160 then Applicable.Yes (Tactic.Tac_ (thy, UnparseC.term f, id, "subproblem_equation_dummy (" ^ UnparseC.term f ^ ")"))
161 else Applicable.No "applicable only to equations made explicit"
162 | "solve_equation_dummy" =>
163 let val (id', f') = ApplicableOLD.split_dummy (UnparseC.term f);
165 if id' <> "subproblem_equation_dummy"
166 then Applicable.No "no subproblem"
167 else if (ThyC.to_ctxt thy, f') |-> TermC.parseNEW |> the |> TermC.is_expliceq
168 then Applicable.Yes (Tactic.Tac_ (thy, UnparseC.term f, id, "[" ^ f' ^ "]"))
169 else error ("Solve_Step.check: f= " ^ f')
171 | _ => Applicable.Yes (Tactic.Tac_ (thy, UnparseC.term f, id, UnparseC.term f))
173 | check (Tactic.Take str) _ = Applicable.Yes (Tactic.Take' (TermC.str2term str)) (* always applicable ?*)
174 | check (Tactic.Begin_Trans) cs =
175 Applicable.Yes (Tactic.Begin_Trans' (Calc.current_formula cs))
176 | check (Tactic.End_Trans) (pt, (p, p_)) = (*TODO: check parent branches*)
178 then Applicable.Yes (Tactic.End_Trans' (Ctree.get_obj Ctree.g_result pt p))
179 else Applicable.No "'End_Trans' is not applicable at the beginning of a transitive sequence"
180 | check Tactic.End_Proof' _ = Applicable.Yes Tactic.End_Proof''
181 | check m _ = raise ERROR ("Solve_Step.check called for " ^ Tactic.input_to_string m);
183 fun add (Tactic.Apply_Method' (_, topt, is, _)) (_, ctxt) (pt, pos as (p, _)) =
186 let val (pt, c) = Ctree.cappend_form pt p (is, ctxt) t
187 in (pos, c, Generate.EmptyMout, pt) end
188 | NONE => (pos, [], Generate.EmptyMout, pt))
189 | add (Tactic.Take' t) l (pt, (p, _)) = (* val (Take' t) = m; *)
192 let val (ps, p') = split_last p (* no connex to prev.ppobj *)
193 in if p' = 0 then ps @ [1] else p end
194 val (pt, c) = Ctree.cappend_form pt p l t
196 ((p, Pos.Frm), c, Generate.FormKF (UnparseC.term t), pt)
198 | add (Tactic.Begin_Trans' t) l (pt, (p, Pos.Frm)) =
200 val (pt, c) = Ctree.cappend_form pt p l t
201 val pt = Ctree.update_branch pt p Ctree.TransitiveB (*040312*)
202 (* replace the old PrfOjb ~~~~~ *)
203 val p = (Pos.lev_on o Pos.lev_dn (* starts with [...,0] *)) p
204 val (pt, c') = Ctree.cappend_form pt p l t (*FIXME.0402 same istate ???*)
206 ((p, Pos.Frm), c @ c', Generate.FormKF (UnparseC.term t), pt)
208 | add (Tactic.Begin_Trans' t) l (pt, (p, Pos.Res)) =
209 (*append after existing PrfObj vvvvvvvvvvvvv*)
210 add (Tactic.Begin_Trans' t) l (pt, (Pos.lev_on p, Pos.Frm))
211 | add (Tactic.End_Trans' tasm) l (pt, (p, _)) =
213 val p' = Pos.lev_up p
214 val (pt, c) = Ctree.append_result pt p' l tasm Ctree.Complete
216 ((p', Pos.Res), c, Generate.FormKF "DUMMY" (*term2str t ..ERROR (t) has not been declared*), pt)
218 | add (Tactic.Rewrite_Inst' (_, _, _, _, subs', thm', f, (f', asm))) (is, ctxt) (pt, (p, _)) =
220 val (pt, c) = Ctree.cappend_atomic pt p (is, ctxt) f
221 (Tactic.Rewrite_Inst (Subst.T_to_input subs', thm')) (f',asm) Ctree.Complete;
222 val pt = Ctree.update_branch pt p Ctree.TransitiveB
224 ((p, Pos.Res), c, Generate.FormKF (UnparseC.term f'), pt)
226 | add (Tactic.Rewrite' (_, _, _, _, thm', f, (f', asm))) (is, ctxt) (pt, (p, _)) =
228 val (pt, c) = Ctree.cappend_atomic pt p (is, ctxt) f (Tactic.Rewrite thm') (f', asm) Ctree.Complete
229 val pt = Ctree.update_branch pt p Ctree.TransitiveB
231 ((p, Pos.Res), c, Generate.FormKF (UnparseC.term f'), pt)
233 | add (Tactic.Rewrite_Set_Inst' (_, _, subs', rls', f, (f', asm))) (is, ctxt) (pt, (p, _)) =
235 val (pt, c) = Ctree.cappend_atomic pt p (is, ctxt) f
236 (Tactic.Rewrite_Set_Inst (Subst.T_to_input subs', Rule_Set.id rls')) (f', asm) Ctree.Complete
237 val pt = Ctree.update_branch pt p Ctree.TransitiveB
239 ((p, Pos.Res), c, Generate.FormKF (UnparseC.term f'), pt)
241 | add (Tactic.Rewrite_Set' (_, _, rls', f, (f', asm))) (is, ctxt) (pt, (p, _)) =
243 val (pt, c) = Ctree.cappend_atomic pt p (is, ctxt) f
244 (Tactic.Rewrite_Set (Rule_Set.id rls')) (f', asm) Ctree.Complete
245 val pt = Ctree.update_branch pt p Ctree.TransitiveB
247 ((p, Pos.Res), c, Generate.FormKF (UnparseC.term f'), pt)
249 | add (Tactic.Check_Postcond' (_, scval)) l (pt, (p, _)) =
251 val (pt, c) = Ctree.append_result pt p l (scval, []) Ctree.Complete
253 ((p, Pos.Res), c, Generate.FormKF (UnparseC.term scval), pt)
255 | add (Tactic.Calculate' (_, op_, f, (f', _))) l (pt, (p, _)) =
257 val (pt,c) = Ctree.cappend_atomic pt p l f (Tactic.Calculate op_) (f', []) Ctree.Complete
259 ((p, Pos.Res), c, Generate.FormKF (UnparseC.term f'), pt)
261 | add (Tactic.Check_elementwise' (consts, pred, (f', asm))) l (pt, (p, _)) =
263 val (pt,c) = Ctree.cappend_atomic pt p l consts (Tactic.Check_elementwise pred) (f', asm) Ctree.Complete
265 ((p, Pos.Res), c, Generate.FormKF (UnparseC.term f'), pt)
267 | add (Tactic.Or_to_List' (ors, list)) l (pt, (p, _)) =
269 val (pt,c) = Ctree.cappend_atomic pt p l ors Tactic.Or_to_List (list, []) Ctree.Complete
271 ((p, Pos.Res), c, Generate.FormKF (UnparseC.term list), pt)
273 | add (Tactic.Substitute' (_, _, subte, t, t')) l (pt, (p, _)) =
276 Ctree.cappend_atomic pt p l t (Tactic.Substitute (Subst.eqs_to_input subte)) (t',[]) Ctree.Complete
277 in ((p, Pos.Res), c, Generate.FormKF (UnparseC.term t'), pt)
279 | add (Tactic.Tac_ (_, f, id, f')) l (pt, (p, _)) =
281 val (pt, c) = Ctree.cappend_atomic pt p l (TermC.str2term f) (Tactic.Tac id) (TermC.str2term f', []) Ctree.Complete
283 ((p,Pos.Res), c, Generate.FormKF f', pt)
285 | add (Tactic.Subproblem' ((domID, pblID, metID), oris, hdl, fmz_, ctxt_specify, f))
286 (l as (_, ctxt)) (pt, (p, _)) =
288 val (pt, c) = Ctree.cappend_problem pt p l (fmz_, (domID, pblID, metID))
289 (oris, (domID, pblID, metID), hdl, ctxt_specify)
290 val f = Syntax.string_of_term (ThyC.to_ctxt (Proof_Context.theory_of ctxt)) f
292 ((p, Pos.Pbl), c, Generate.FormKF f, pt)
294 | add m' _ (_, pos) =
295 raise ERROR ("Solve_Step.add: not impl.for " ^ Tactic.string_of m' ^ " at " ^ Pos.pos'2str pos)
297 (* LI switches between solve-phase and specify-phase *)
298 fun add_general tac ic cs =
299 if Tactic.for_specify' tac
300 then Generate.generate1 tac ic cs
303 (* tacis are in reverse order from do_next/specify_: last = fst to insert *)
304 fun s_add_general [] ptp = ptp
305 | s_add_general tacis (pt, c, _) =
307 val (tacis', (_, tac_, (p, is))) = split_last tacis
308 val (p',c',_,pt') = add_general tac_ is (pt, p)
310 s_add_general tacis' (pt', c@c', p')