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 -> Test_Out.T
13 val add_general: Tactic.T -> Istate_Def.T * Proof.context -> Calc.T -> Test_Out.T
14 val s_add_general: State_Steps.T ->
15 Ctree.ctree * Pos.pos' list * Pos.pos' -> Ctree.ctree * Pos.pos' list * Pos.pos'
17 theory -> Tactic.T -> Pos.pos' -> Ctree.ctree -> Test_Out.T
19 val get_ruleset: 'a -> Pos.pos' -> Ctree.ctree ->
20 string * ThyC.id * Rewrite_Ord.id * Rule_Def.rule_set * bool
21 val get_eval: string -> Pos.pos' -> Ctree.ctree -> string * ThyC.id * Eval.ml
24 val rew_info: Rule_Def.rule_set -> string * Rule_Def.rule_set * Eval.ml_from_prog list
29 structure Solve_Step(**): SOLVE_STEP(**) =
33 (** get data from Calc.T **)
35 (* the source is the parent node, either a problem or a Rule_Set (with inter_steps) *)
36 fun rew_info (Rule_Def.Repeat {asm_rls, rew_ord = (rew_ord, _), calc = ca, ...}) =
37 (rew_ord, asm_rls, ca)
38 | rew_info (Rule_Set.Sequence {asm_rls, rew_ord = (rew_ord, _), calc = ca, ...}) =
39 (rew_ord, asm_rls, ca)
40 | rew_info (Rule_Set.Rrls {asm_rls, rew_ord = (rew_ord, _), calc = ca, ...}) =
41 (rew_ord, asm_rls, ca)
42 | rew_info rls = raise ERROR ("rew_info called with '" ^ Rule_Set.id rls ^ "'");
44 fun get_ruleset _ (pos as (p, _)) pt =
46 val (pbl, p', rls', ctxt) = LItool.parent_node pt pos
51 val thy' = Ctree.get_obj Ctree.g_domID pt p'
52 val {rew_ord, asm_rls, ...} = MethodC.from_store ctxt (Ctree.get_obj Ctree.g_metID pt p')
53 in ("OK", thy', rew_ord, asm_rls, false) end
56 val thy' = Ctree.get_obj Ctree.g_domID pt (Ctree.par_pblobj pt p)
57 val (rew_ord, asm_rls, _) = rew_info rls'
58 in ("OK", thy', rew_ord, asm_rls, false) end
61 fun get_eval scrop (pos as (p, _)) pt =
63 val (pbl, p', rls', ctxt) = LItool.parent_node pt pos
68 val thy' = Ctree.get_obj Ctree.g_domID pt p'
69 val {calc = scr_isa_fns, ...} = MethodC.from_store ctxt (Ctree.get_obj Ctree.g_metID pt p')
70 val opt = assoc (scr_isa_fns, scrop)
73 SOME isa_fn => ("OK", thy', isa_fn)
74 | NONE => ("applicable_in Calculate: unknown '" ^ scrop ^ "'", "", ("", Eval.ml_fun_empty))
78 val thy' = Ctree.get_obj Ctree.g_domID pt (Ctree.par_pblobj pt p);
79 val (_, _,(*_,*)scr_isa_fns) = rew_info rls'(*rls*)
81 case assoc (scr_isa_fns, scrop) of
82 SOME isa_fn => ("OK",thy',isa_fn)
83 | NONE => ("applicable_in Calculate: unknown '" ^ scrop ^ "'", "", ("", Eval.ml_fun_empty))
87 (** get context reliably at switch_specify_solve **)
89 fun at_begin_program (is, Pos.Res) = last_elem is = 0
90 | at_begin_program _ = false;
92 (* strange special case at Apply_Method *)
93 fun get_ctxt_from_PblObj pt (p_, Pos.Res) =
95 val pp = Ctree.par_pblobj pt p_ (*drops the "0"*)
96 val {ctxt, ...} = Ctree.get_obj I pt pp |> Ctree.rep_specify_data
98 | get_ctxt_from_PblObj _ _ = raise ERROR "get_ctxt_from_PblObj called by PrfObj or EmptyPtree";
100 fun get_ctxt pt (p_, Pos.Pbl) =
102 val pp = Ctree.par_pblobj pt p_ (*drops the "0"*)
103 val {ctxt, ...} = Ctree.get_obj I pt pp |> Ctree.rep_specify_data
106 if at_begin_program pos
107 then get_ctxt_from_PblObj pt pos
108 else Ctree.get_ctxt pt pos
112 (** Solve_Step.check **)
115 check tactics (input by the user, mostly) for applicability
116 and determine as much of the result of the tactic as possible initially.
118 fun check (Tactic.Apply_Method mI) (pt, (p, _)) =
120 val (dI, pI, probl, ctxt) = case Ctree.get_obj I pt p of
121 Ctree.PblObj {origin = (_, (dI, pI, _), _), probl, ctxt, ...} => (dI, pI, probl, ctxt)
122 | _ => raise ERROR "Solve_Step.check Apply_Method: uncovered case Ctree.get_obj"
123 val {where_, ...} = Problem.from_store ctxt pI
124 val pres = map (Pre_Conds.environment probl |> subst_atomic) where_
125 val ctxt = if ContextC.is_empty ctxt (*only in case of input by user*)
127 |> Know_Store.get_via_last_thy
128 |> Proof_Context.init_global
129 |> ContextC.insert_assumptions pres
132 Applicable.Yes (Tactic.Apply_Method' (mI, NONE, Istate_Def.empty (*filled later*), ctxt))
134 | check (Tactic.Calculate op_) (cs as (pt, pos)) =
136 val (msg, thy', isa_fn) = get_eval op_ pos pt;
137 val ctxt = Ctree.get_ctxt pt pos
138 val f = Calc.current_formula cs;
142 case Rewrite.calculate_ ctxt isa_fn f of
144 => Applicable.Yes (Tactic.Calculate' (thy', op_, f, (f', (id, thm))))
145 | NONE => Applicable.No ("'calculate " ^ op_ ^ "' not applicable")
146 else Applicable.No msg
148 | check (Tactic.Check_Postcond pI) (_, _) = (*TODO: only applicable, if evaluating to True*)
149 Applicable.Yes (Tactic.Check_Postcond' (pI, TermC.empty))
150 | check (Tactic.Check_elementwise pred) cs =
152 val f = Calc.current_formula cs;
154 Applicable.Yes (Tactic.Check_elementwise' (f, pred, (f, [])))
156 | check Tactic.Empty_Tac _ = Applicable.No "Empty_Tac is not applicable"
157 | check (Tactic.Free_Solve) _ = Applicable.Yes (Tactic.Free_Solve')
158 | check Tactic.Or_to_List cs =
160 val f = Calc.current_formula cs;
161 val ls = Prog_Expr.or2list f;
163 Applicable.Yes (Tactic.Or_to_List' (f, ls))
165 | check (Tactic.Rewrite thm) (cs as (pt, pos)) =
167 val (msg, _, ro, rls', _) = get_ruleset thm pos pt;
168 val ctxt = Ctree.get_ctxt pt pos
169 val thy = ctxt |> Proof_Context.theory_of
170 val f = Calc.current_formula cs;
174 case Rewrite.rewrite_ ctxt (get_rew_ord ctxt ro) rls' false (snd thm) f of
176 Applicable.Yes (Tactic.Rewrite' (ThyC.id_of thy, ro, rls', false, thm, f, (f', asm)))
177 | NONE => Applicable.No ((thm |> fst |> quote) ^ " not applicable")
178 else Applicable.No msg
180 | check (Tactic.Rewrite_Inst (subs, thm)) (cs as (pt, pos as (p, _))) =
182 val pp = Ctree.par_pblobj pt p;
183 val ctxt = Ctree.get_loc pt pos |> snd
184 val thy = Proof_Context.theory_of ctxt
185 val {rew_ord = ro', asm_rls = asm_rls, ...} = MethodC.from_store ctxt (Ctree.get_obj Ctree.g_metID pt pp);
186 val f = Calc.current_formula cs;
187 val subst = Tactic.subst_adapt_to_type ctxt subs;
189 case Rewrite.rewrite_inst_ ctxt (get_rew_ord ctxt ro') asm_rls false subst (snd thm) f of
191 Applicable.Yes (Tactic.Rewrite_Inst'
192 (Context.theory_name thy, ro', asm_rls, false, subst, thm, f, (f', asm)))
193 | NONE => Applicable.No (fst thm ^ " not applicable")
195 | check (Tactic.Rewrite_Set rls) (cs as (pt, pos)) =
197 val ctxt = Ctree.get_loc pt pos |> snd
198 val thy' = ctxt |> Proof_Context.theory_of |> Context.theory_name
199 val f = Calc.current_formula cs;
201 case Rewrite.rewrite_set_ ctxt false (get_rls ctxt rls) f of
203 => Applicable.Yes (Tactic.Rewrite_Set' (thy', false, get_rls ctxt rls, f, (f', asm)))
204 | NONE => Applicable.No (rls ^ " not applicable")
206 | check (Tactic.Rewrite_Set_Inst (subs, rls)) (cs as (pt, pos)) =
208 val ctxt = Ctree.get_loc pt pos |> snd
209 val thy' = ctxt |> Proof_Context.theory_of |> Context.theory_name
210 val f = Calc.current_formula cs;
211 val subst = Tactic.subst_adapt_to_type ctxt subs;
213 case Rewrite.rewrite_set_inst_ ctxt false subst (get_rls ctxt rls) f of
216 (Tactic.Rewrite_Set_Inst' (thy', false, subst, get_rls ctxt rls, f, (f', asm)))
217 | NONE => Applicable.No (rls ^ " not applicable")
219 | check (Tactic.Subproblem (domID, pblID)) (_, _) =
220 Applicable.Yes (Tactic.Subproblem' ((domID, pblID, MethodC.id_empty), [],
221 TermC.empty, [], ContextC.empty, Auto_Prog.subpbl domID pblID))
222 | check (Tactic.Substitute sube) (cs as (pt, pos as (p, _))) =
224 val pp = Ctree.par_pblobj pt p
225 val ctxt = Ctree.get_loc pt pos |> snd
226 val f = Calc.current_formula cs;
227 val {rew_ord, asm_rls, ...} = MethodC.from_store ctxt (Ctree.get_obj Ctree.g_metID pt pp)
228 val subte = Prog_Tac.Substitute_adapt_to_type' ctxt sube
229 val ro = get_rew_ord ctxt rew_ord
231 if foldl and_ (true, map TermC.contains_Var subte)
233 let val f' = subst_atomic (Subst.T_from_string_eqs ctxt sube) f
235 then Applicable.No (Subst.string_eqs_to_string sube ^ " not applicable")
236 else Applicable.Yes (Tactic.Substitute' (ro, asm_rls, subte, f, f'))
239 case Rewrite.rewrite_terms_ ctxt ro asm_rls subte f of
240 SOME (f', _) => Applicable.Yes (Tactic.Substitute' (ro, asm_rls, subte, f, f'))
241 | NONE => Applicable.No (Subst.string_eqs_to_string sube ^ " not applicable")
243 | check (Tactic.Tac id) (cs as (pt, pos)) =
245 val ctxt = Ctree.get_ctxt pt pos
246 val thy = ctxt |> Proof_Context.theory_of
247 val f = Calc.current_formula cs;
248 val f' = UnparseC.term ctxt f
250 Applicable.Yes (Tactic.Tac_ (thy, f', id, f'))
252 | check (Tactic.Take str) (pt, pos) =
254 val ctxt = (*Solve_Step.*)get_ctxt pt pos
255 val t = Prog_Tac.Take_adapt_to_type ctxt str
256 in Applicable.Yes (Tactic.Take' t) end
257 | check (Tactic.Begin_Trans) cs =
258 Applicable.Yes (Tactic.Begin_Trans' (Calc.current_formula cs))
259 | check (Tactic.End_Trans) (pt, (p, p_)) = (*TODO: check parent branches*)
261 then Applicable.Yes (Tactic.End_Trans' (Ctree.get_obj Ctree.g_result pt p))
262 else Applicable.No "Tactic.End_Trans not applicable at the beginning of a transitive sequence"
263 | check Tactic.End_Proof' _ =
264 Applicable.Yes Tactic.End_Proof'' (*TODO! check Post_Cond first !*)
265 | check m (pt, pos) =
267 val ctxt = (*Solve_Step.*)get_ctxt pt pos
269 raise ERROR ("Solve_Step.check called for " ^ Tactic.input_to_string ctxt m)
273 (** Solve_Step.add **)
275 fun add (Tactic.Apply_Method' (_, topt, is, _)) (_, ctxt) (pt, pos as (p, _)) =
278 let val (pt, c) = Ctree.cappend_form pt p (is, ctxt) t
279 in (pos, c, Test_Out.EmptyMout, pt) end
280 | NONE => (pos, [], Test_Out.EmptyMout, pt))
281 | add (Tactic.Take' t) (l as (_, ctxt)) (pt, (p, _)) = (* val (Take' t) = m; *)
284 let val (ps, p') = split_last p (* no connex to prev.ppobj *)
285 in if p' = 0 then ps @ [1] else p end
286 val (pt, c) = Ctree.cappend_form pt p l t
288 ((p, Pos.Frm), c, Test_Out.FormKF (UnparseC.term ctxt t), pt)
290 | add (Tactic.Begin_Trans' t) (l as (_, ctxt)) (pt, (p, Pos.Frm)) =
292 val (pt, c) = Ctree.cappend_form pt p l t
293 val pt = Ctree.update_branch pt p Ctree.TransitiveB (*040312*)
294 (* replace the old PrfOjb ~~~~~ *)
295 val p = (Pos.lev_on o Pos.lev_dn (* starts with [...,0] *)) p
296 val (pt, c') = Ctree.cappend_form pt p l t (*FIXME.0402 same istate ???*)
298 ((p, Pos.Frm), c @ c', Test_Out.FormKF (UnparseC.term ctxt t), pt)
300 | add (Tactic.Begin_Trans' t) l (pt, (p, Pos.Res)) =
301 (*append after existing PrfObj vvvvvvvvvvvvv*)
302 add (Tactic.Begin_Trans' t) l (pt, (Pos.lev_on p, Pos.Frm))
303 | add (Tactic.End_Trans' tasm) (l as (_, _(*ctxt*))) (pt, (p, _)) =
305 val p' = Pos.lev_up p
306 val (pt, c) = Ctree.append_result pt p' l tasm Ctree.Complete
308 ((p', Pos.Res), c, Test_Out.FormKF "DUMMY" (*UnparseC.term ctxt t*), pt)
310 | add (Tactic.Rewrite_Inst' (_, _, _, _, subs', thm', f, (f', asm))) (is, ctxt) (pt, (p, _)) =
312 val (pt, c) = Ctree.cappend_atomic pt p (is, ctxt) f
313 (Tactic.Rewrite_Inst (Subst.T_to_input ctxt subs', thm')) (f',asm) Ctree.Complete;
314 val pt = Ctree.update_branch pt p Ctree.TransitiveB
316 ((p, Pos.Res), c, Test_Out.FormKF (UnparseC.term ctxt f'), pt)
318 | add (Tactic.Rewrite' (_, _, _, _, thm', f, (f', asm))) (is, ctxt) (pt, (p, _)) =
320 val (pt, c) = Ctree.cappend_atomic pt p (is, ctxt) f (Tactic.Rewrite thm') (f', asm) Ctree.Complete
321 val pt = Ctree.update_branch pt p Ctree.TransitiveB
323 ((p, Pos.Res), c, Test_Out.FormKF (UnparseC.term ctxt f'), pt)
325 | add (Tactic.Rewrite_Set_Inst' (_, _, subs', rls', f, (f', asm))) (is, ctxt) (pt, (p, _)) =
327 val (pt, c) = Ctree.cappend_atomic pt p (is, ctxt) f
328 (Tactic.Rewrite_Set_Inst (Subst.T_to_input ctxt subs', Rule_Set.id rls')) (f', asm) Ctree.Complete
329 val pt = Ctree.update_branch pt p Ctree.TransitiveB
331 ((p, Pos.Res), c, Test_Out.FormKF (UnparseC.term ctxt f'), pt)
333 | add (Tactic.Rewrite_Set' (_, _, rls', f, (f', asm))) (is, ctxt) (pt, (p, _)) =
335 val (pt, c) = Ctree.cappend_atomic pt p (is, ctxt) f
336 (Tactic.Rewrite_Set (Rule_Set.id rls')) (f', asm) Ctree.Complete
337 val pt = Ctree.update_branch pt p Ctree.TransitiveB
339 ((p, Pos.Res), c, Test_Out.FormKF (UnparseC.term ctxt f'), pt)
341 | add (Tactic.Check_Postcond' (_, scval)) (l as (_, ctxt)) (pt, (p, _)) =
343 val (pt, c) = Ctree.append_result pt p l (scval, []) Ctree.Complete
345 ((p, Pos.Res), c, Test_Out.FormKF (UnparseC.term ctxt scval), pt)
347 | add (Tactic.Calculate' (_, op_, f, (f', _))) (l as (_, ctxt)) (pt, (p, _)) =
349 val (pt,c) = Ctree.cappend_atomic pt p l f (Tactic.Calculate op_) (f', []) Ctree.Complete
351 ((p, Pos.Res), c, Test_Out.FormKF (UnparseC.term ctxt f'), pt)
353 | add (Tactic.Check_elementwise' (consts, pred, (f', asm))) (l as (_, ctxt)) (pt, (p, _)) =
355 val (pt,c) = Ctree.cappend_atomic pt p l consts (Tactic.Check_elementwise pred) (f', asm) Ctree.Complete
357 ((p, Pos.Res), c, Test_Out.FormKF (UnparseC.term ctxt f'), pt)
359 | add (Tactic.Or_to_List' (ors, list)) (l as (_, ctxt)) (pt, (p, _)) =
361 val (pt,c) = Ctree.cappend_atomic pt p l ors Tactic.Or_to_List (list, []) Ctree.Complete
363 ((p, Pos.Res), c, Test_Out.FormKF (UnparseC.term ctxt list), pt)
365 | add (Tactic.Substitute' (_, _, subte, t, t')) (l as (_, ctxt)) (pt, (p, _)) =
368 Ctree.cappend_atomic pt p l t
369 (Tactic.Substitute (Subst.eqs_to_input ctxt subte)) (t',[]) Ctree.Complete
370 in ((p, Pos.Res), c, Test_Out.FormKF (UnparseC.term ctxt t'), pt)
372 | add (Tactic.Tac_ (_, f, id, f')) l (pt, pos as (p, _)) =
374 val ctxt = Ctree.get_ctxt pt pos
375 val (pt, c) = Ctree.cappend_atomic pt p l
376 (ParseC.term_opt ctxt f |> the) (Tactic.Tac id) (ParseC.term_opt ctxt f' |> the, [])
379 ((p,Pos.Res), c, Test_Out.FormKF f', pt)
381 | add (Tactic.Subproblem' ((domID, pblID, metID), oris, hdl, fmz_, ctxt_specify, f))
382 (l as (_, ctxt)) (pt, (p, _)) =
384 val (pt, c) = Ctree.cappend_problem pt p l (fmz_, (domID, pblID, metID))
385 (oris, (domID, pblID, metID), hdl, ctxt_specify)
386 val f = Syntax.string_of_term ctxt f
388 ((p, Pos.Pbl), c, Test_Out.FormKF f, pt)
390 | add m' (_, ctxt) (_, pos) =
391 raise ERROR ("Solve_Step.add: not impl.for " ^ Tactic.string_of ctxt m' ^ " at " ^ Pos.pos'2str pos)
393 (* LI switches between solve-phase and specify-phase *)
394 fun add_general tac ic cs =
395 if Tactic.for_specify' tac
396 then Specify_Step.add tac ic cs
399 (* the order of State_Steps is reversed: insert last element first *)
400 fun s_add_general [] ptp = ptp
401 | s_add_general tacis (pt, c, _) =
403 val (tacis', (_, tac_, (p, is))) = split_last tacis
404 val (p', c', _, pt') = add_general tac_ is (pt, p)
406 s_add_general tacis' (pt', c@c', p')
409 (* a still undeveloped concept: do a calculation without LI *)
410 fun add_hard _(*thy*) m' (p, p_) pt =
413 Pos.Frm => p | Pos.Res => Pos.lev_on p
414 | _ => raise ERROR ("generate_hard: call by " ^ Pos.pos'2str (p,p_))
416 add_general m' (Istate_Def.empty, ContextC.empty) (pt, (p, p_))