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 (* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
13 (*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
15 ( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
19 structure Solve_Step(** ): SOLVE_STEP( **) =
24 check tactics (input by the user, mostly) for applicability
25 and determine as much of the result of the tactic as possible initially.
27 fun check (Tactic.CAScmd ct') _ =
28 error ("Solve_Step.check: not impl. for " ^ Tactic.input_to_string (Tactic.CAScmd ct'))
29 | check (m as Tactic.Calculate op_) (pt, (p, p_)) =
30 if member op = [Pos.Pbl, Pos.Met] p_
31 then Applicable.No ((Tactic.input_to_string m)^" not for pos "^(Pos.pos'2str (p,p_)))
34 val (msg,thy',isa_fn) = ApplicableOLD.from_pblobj_or_detail_calc op_ p pt;
36 Frm => Ctree.get_obj Ctree.g_form pt p
37 | Pos.Res => (fst o (Ctree.get_obj Ctree.g_result pt)) p
38 | _ => raise ERROR ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
42 case Rewrite.calculate_ (ThyC.get_theory thy') isa_fn f of
44 => Applicable.Yes (Tactic.Calculate' (thy', op_, f, (f', (id, thm))))
45 | NONE => Applicable.No ("'calculate "^op_^"' not applicable")
46 else Applicable.No msg
48 | check (Tactic.Check_Postcond pI) (_, (p, p_)) =
49 if member op = [Pos.Pbl, Pos.Met] p_
50 then Applicable.No ((Tactic.input_to_string (Tactic.Check_Postcond pI)) ^ " not for pos " ^ Pos.pos'2str (p, p_))
51 else Applicable.Yes (Tactic.Check_Postcond' (pI, TermC.empty))
52 | check (m as Tactic.Check_elementwise pred) (pt, (p, p_)) =
53 if member op = [Pos.Pbl, Pos.Met] p_
54 then Applicable.No ((Tactic.input_to_string m) ^ " not for pos " ^ Pos.pos'2str (p, p_))
57 val pp = Ctree.par_pblobj pt p;
58 val thy' = Ctree.get_obj Ctree.g_domID pt pp;
59 val thy = ThyC.get_theory thy'
60 val metID = (Ctree.get_obj Ctree.g_metID pt pp)
61 val {crls, ...} = Specify.get_met metID
62 val (f, asm) = case p_ of
63 Frm => (Ctree.get_obj Ctree.g_form pt p , [])
64 | Pos.Res => Ctree.get_obj Ctree.g_result pt p
65 | _ => error ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
66 val vp = (ThyC.to_ctxt thy, pred) |-> TermC.parseNEW |> the |> ApplicableOLD.mk_set thy pt p f;
68 Applicable.Yes (Tactic.Check_elementwise' (f, pred, (f, asm)))
70 | check Tactic.Empty_Tac _ = Applicable.No "Empty_Tac is not applicable"
71 | check (Tactic.Free_Solve) _ = Applicable.Yes (Tactic.Free_Solve') (* always applicable *)
72 | check Tactic.Or_to_List (pt, (p, p_)) =
73 if member op = [Pos.Pbl, Pos.Met] p_
74 then Applicable.No ((Tactic.input_to_string Tactic.Or_to_List)^" not for pos "^(Pos.pos'2str (p,p_)))
78 Pos.Frm => Ctree.get_obj Ctree.g_form pt p
79 | Pos.Res => (fst o (Ctree.get_obj Ctree.g_result pt)) p
80 | _ => error ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
81 in (let val ls = Prog_Expr.or2list f
82 in Applicable.Yes (Tactic.Or_to_List' (f, ls)) end)
83 handle _ => Applicable.No ("'Or_to_List' not applicable to " ^ UnparseC.term f)
85 | check (m as Tactic.Rewrite thm'') (pt, (p, p_)) =
86 if member op = [Pos.Pbl, Pos.Met] p_
87 then Applicable.No ((Tactic.input_to_string m)^" not for pos "^(Pos.pos'2str (p, p_)))
90 val (msg, thy', ro, rls', _)= ApplicableOLD.from_pblobj_or_detail_thm thm'' p pt;
91 val thy = ThyC.get_theory thy';
93 Frm => Ctree.get_obj Ctree.g_form pt p
94 | Pos.Res => (fst o (Ctree.get_obj Ctree.g_result pt)) p
95 | _ => error ("Solve_Step.check Rewrite: call by " ^ Pos.pos'2str (p, p_));
99 case Rewrite.rewrite_ thy (Rewrite_Ord.assoc_rew_ord ro) rls' false (snd thm'') f of
100 SOME (f',asm) => Applicable.Yes (Tactic.Rewrite' (thy', ro, rls', false, thm'', f, (f', asm)))
101 | NONE => Applicable.No ("'" ^ fst thm'' ^"' not applicable")
102 else Applicable.No msg
104 | check (m as Tactic.Rewrite_Inst (subs, thm'')) (pt, (p, p_)) =
105 if member op = [Pos.Pbl, Pos.Met] p_
106 then Applicable.No ((Tactic.input_to_string m)^" not for pos " ^ Pos.pos'2str (p, p_))
109 val pp = Ctree.par_pblobj pt p;
110 val thy' = Ctree.get_obj Ctree.g_domID pt pp;
111 val thy = ThyC.get_theory thy';
112 val {rew_ord' = ro', erls = erls, ...} = Specify.get_met (Ctree.get_obj Ctree.g_metID pt pp);
113 val (f, _) = case p_ of (*p 12.4.00 unnecessary*)
114 Frm => (Ctree.get_obj Ctree.g_form pt p, p)
115 | Pos.Res => ((fst o (Ctree.get_obj Ctree.g_result pt)) p, Pos.lev_on p)
116 | _ => error ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
119 val subst = Subst.T_from_input thy subs;
121 case Rewrite.rewrite_inst_ thy (Rewrite_Ord.assoc_rew_ord ro') erls false subst (snd thm'') f of
123 Applicable.Yes (Tactic.Rewrite_Inst' (thy', ro', erls, false, subst, thm'', f, (f', asm)))
124 | NONE => Applicable.No ((fst thm'')^" not applicable")
126 handle _ => Applicable.No ("syntax error in "^(subs2str subs))
128 | check (m as Tactic.Rewrite_Set rls) (pt, (p, p_)) =
129 if member op = [Pos.Pbl, Pos.Met] p_
130 then Applicable.No (Tactic.input_to_string m ^ " not for pos " ^ Pos.pos'2str (p, p_))
133 val pp = Ctree.par_pblobj pt p;
134 val thy' = Ctree.get_obj Ctree.g_domID pt pp;
135 val (f, _) = case p_ of
136 Frm => (Ctree.get_obj Ctree.g_form pt p, p)
137 | Pos.Res => ((fst o (Ctree.get_obj Ctree.g_result pt)) p, Pos.lev_on p)
138 | _ => error ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
140 case Rewrite.rewrite_set_ (ThyC.get_theory thy') false (assoc_rls rls) f of
142 => Applicable.Yes (Tactic.Rewrite_Set' (thy', false, assoc_rls rls, f, (f', asm)))
143 | NONE => Applicable.No (rls ^ " not applicable")
145 | check (m as Tactic.Rewrite_Set_Inst (subs, rls)) (pt, (p, p_)) =
146 if member op = [Pos.Pbl, Pos.Met] p_
147 then Applicable.No ((Tactic.input_to_string m)^" not for pos "^(Pos.pos'2str (p,p_)))
150 val pp = Ctree.par_pblobj pt p;
151 val thy' = Ctree.get_obj Ctree.g_domID pt pp;
152 val thy = ThyC.get_theory thy';
153 val (f, _) = case p_ of
154 Frm => (Ctree.get_obj Ctree.g_form pt p, p)
155 | Pos.Res => ((fst o (Ctree.get_obj Ctree.g_result pt)) p, Pos.lev_on p)
156 | _ => error ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
157 val subst = Subst.T_from_input thy subs;
159 case Rewrite.rewrite_set_inst_ thy (*put_asm*)false subst (assoc_rls rls) f of
161 => Applicable.Yes (Tactic.Rewrite_Set_Inst' (thy', false, subst, assoc_rls rls, f, (f', asm)))
162 | NONE => Applicable.No (rls ^ " not applicable")
163 handle _ => Applicable.No ("syntax error in " ^(subs2str subs))
165 | check (m as Tactic.Subproblem (domID, pblID)) (_, (p, p_)) =
166 if Pos.on_specification p_
168 Applicable.No (Tactic.input_to_string m ^ " not for pos " ^ Pos.pos'2str (p, p_))
169 else (*some fields filled later in LI*)
170 Applicable.Yes (Tactic.Subproblem' ((domID, pblID, Method.id_empty), [],
171 TermC.empty, [], ContextC.empty, Auto_Prog.subpbl domID pblID))
172 (*Substitute combines two different kind of "substitution":
173 (1) subst_atomic: for ?a..?z
174 (2) Pattern.match: for solving equational systems (which raises exn for ?a..?z)*)
175 | check (m as Tactic.Substitute sube) (pt, (p, p_)) =
176 if member op = [Pos.Pbl, Pos.Met] p_
177 then Applicable.No (Tactic.input_to_string m ^ " not for pos " ^ Pos.pos'2str (p, p_))
180 val pp = Ctree.par_pblobj pt p
181 val thy = ThyC.get_theory (Ctree.get_obj Ctree.g_domID pt pp)
183 Frm => Ctree.get_obj Ctree.g_form pt p
184 | Pos.Res => (fst o (Ctree.get_obj Ctree.g_result pt)) p
185 | _ => error ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
186 val {rew_ord', erls, ...} = Specify.get_met (Ctree.get_obj Ctree.g_metID pt pp)
187 val subte = Subst.input_to_terms sube
188 val subst = Subst.T_from_string_eqs thy sube
189 val ro = Rewrite_Ord.assoc_rew_ord rew_ord'
191 if foldl and_ (true, map TermC.contains_Var subte)
193 let val f' = subst_atomic subst f
195 then Applicable.No (Subst.string_eqs_to_string sube ^ " not applicable")
196 else Applicable.Yes (Tactic.Substitute' (ro, erls, subte, f, f'))
199 case Rewrite.rewrite_terms_ thy ro erls subte f of
200 SOME (f', _) => Applicable.Yes (Tactic.Substitute' (ro, erls, subte, f, f'))
201 | NONE => Applicable.No (Subst.string_eqs_to_string sube ^ " not applicable")
203 | check (Tactic.Tac id) (pt, (p, p_)) =
205 val pp = Ctree.par_pblobj pt p;
206 val thy' = Ctree.get_obj Ctree.g_domID pt pp;
207 val thy = ThyC.get_theory thy';
209 Frm => Ctree.get_obj Ctree.g_form pt p
210 | Pos.Pbl => error "Solve_Step.check (p,Pos.Pbl) pt (Tac id): not at Pos.Pbl"
211 | Pos.Res => (fst o (Ctree.get_obj Ctree.g_result pt)) p
212 | _ => error ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
214 "subproblem_equation_dummy" =>
215 if TermC.is_expliceq f
216 then Applicable.Yes (Tactic.Tac_ (thy, UnparseC.term f, id, "subproblem_equation_dummy (" ^ UnparseC.term f ^ ")"))
217 else Applicable.No "applicable only to equations made explicit"
218 | "solve_equation_dummy" =>
219 let val (id', f') = ApplicableOLD.split_dummy (UnparseC.term f);
221 if id' <> "subproblem_equation_dummy"
222 then Applicable.No "no subproblem"
223 else if (ThyC.to_ctxt thy, f') |-> TermC.parseNEW |> the |> TermC.is_expliceq
224 then Applicable.Yes (Tactic.Tac_ (thy, UnparseC.term f, id, "[" ^ f' ^ "]"))
225 else error ("Solve_Step.check: f= " ^ f')
227 | _ => Applicable.Yes (Tactic.Tac_ (thy, UnparseC.term f, id, UnparseC.term f))
229 | check (Tactic.Take str) _ = Applicable.Yes (Tactic.Take' (TermC.str2term str)) (* always applicable ?*)
230 | check (Tactic.Begin_Trans) (pt, (p, p_)) =
232 val (f, _) = case p_ of (*p 12.4.00 unnecessary, implizit Take in gen*)
233 Pos.Frm => (Ctree.get_obj Ctree.g_form pt p, (Pos.lev_on o Pos.lev_dn) p)
234 | Pos.Res => ((fst o (Ctree.get_obj Ctree.g_result pt)) p, (Pos.lev_on o Pos.lev_dn o Pos.lev_on) p)
235 | _ => error ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
236 in (Applicable.Yes (Tactic.Begin_Trans' f))
237 handle _ => raise ERROR ("Solve_Step.check: Begin_Trans finds syntaxerror in '" ^ UnparseC.term f ^ "'")
239 | check (Tactic.End_Trans) (pt, (p, p_)) = (*TODO: check parent branches*)
241 then Applicable.Yes (Tactic.End_Trans' (Ctree.get_obj Ctree.g_result pt p))
242 else Applicable.No "'End_Trans' is not applicable at the beginning of a transitive sequence"
243 | check Tactic.End_Proof' _ = Applicable.Yes Tactic.End_Proof''
244 | check m _ = raise ERROR ("Solve_Step.check called for " ^ Tactic.input_to_string m);