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 (*RM* )| Derive of Rule_Set.id( *RM*)
71 | check Tactic.Empty_Tac _ = Applicable.No "Empty_Tac is not applicable"
72 | check (Tactic.Free_Solve) _ = Applicable.Yes (Tactic.Free_Solve') (* always applicable *)
73 | check (Tactic.Apply_Assumption cts') _ =
74 raise ERROR ("Solve_Step.check: not impl. for " ^ Tactic.input_to_string (Tactic.Apply_Assumption cts'))
75 (* 'logical' applicability wrt. script in locate_input_tactic: Inconsistent? *)
76 | check Tactic.Or_to_List (pt, (p, p_)) =
77 if member op = [Pos.Pbl, Pos.Met] p_
78 then Applicable.No ((Tactic.input_to_string Tactic.Or_to_List)^" not for pos "^(Pos.pos'2str (p,p_)))
82 Frm => Ctree.get_obj Ctree.g_form pt p
83 | Pos.Res => (fst o (Ctree.get_obj Ctree.g_result pt)) p
84 | _ => error ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
85 in (let val ls = Prog_Expr.or2list f
86 in Applicable.Yes (Tactic.Or_to_List' (f, ls)) end)
87 handle _ => Applicable.No ("'Or_to_List' not applicable to " ^ UnparseC.term f)
89 | check (m as Tactic.Rewrite thm'') (pt, (p, p_)) =
90 if member op = [Pos.Pbl, Pos.Met] p_
91 then Applicable.No ((Tactic.input_to_string m)^" not for pos "^(Pos.pos'2str (p, p_)))
94 val (msg, thy', ro, rls', _)= ApplicableOLD.from_pblobj_or_detail_thm thm'' p pt;
95 val thy = ThyC.get_theory thy';
97 Frm => Ctree.get_obj Ctree.g_form pt p
98 | Pos.Res => (fst o (Ctree.get_obj Ctree.g_result pt)) p
99 | _ => error ("Solve_Step.check Rewrite: call by " ^ Pos.pos'2str (p, p_));
103 case Rewrite.rewrite_ thy (Rewrite_Ord.assoc_rew_ord ro) rls' false (snd thm'') f of
104 SOME (f',asm) => Applicable.Yes (Tactic.Rewrite' (thy', ro, rls', false, thm'', f, (f', asm)))
105 | NONE => Applicable.No ("'" ^ fst thm'' ^"' not applicable")
106 else Applicable.No msg
108 | check (m as Tactic.Rewrite_Inst (subs, thm'')) (pt, (p, p_)) =
109 if member op = [Pos.Pbl, Pos.Met] p_
110 then Applicable.No ((Tactic.input_to_string m)^" not for pos " ^ Pos.pos'2str (p, p_))
113 val pp = Ctree.par_pblobj pt p;
114 val thy' = Ctree.get_obj Ctree.g_domID pt pp;
115 val thy = ThyC.get_theory thy';
116 val {rew_ord' = ro', erls = erls, ...} = Specify.get_met (Ctree.get_obj Ctree.g_metID pt pp);
117 val (f, _) = case p_ of (*p 12.4.00 unnecessary*)
118 Frm => (Ctree.get_obj Ctree.g_form pt p, p)
119 | Pos.Res => ((fst o (Ctree.get_obj Ctree.g_result pt)) p, Pos.lev_on p)
120 | _ => error ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
123 val subst = Subst.T_from_input thy subs;
125 case Rewrite.rewrite_inst_ thy (Rewrite_Ord.assoc_rew_ord ro') erls false subst (snd thm'') f of
127 Applicable.Yes (Tactic.Rewrite_Inst' (thy', ro', erls, false, subst, thm'', f, (f', asm)))
128 | NONE => Applicable.No ((fst thm'')^" not applicable")
130 handle _ => Applicable.No ("syntax error in "^(subs2str subs))
132 | check (m as Tactic.Rewrite_Set rls) (pt, (p, p_)) =
133 if member op = [Pos.Pbl, Pos.Met] p_
134 then Applicable.No (Tactic.input_to_string m ^ " not for pos " ^ Pos.pos'2str (p, p_))
137 val pp = Ctree.par_pblobj pt p;
138 val thy' = Ctree.get_obj Ctree.g_domID pt pp;
139 val (f, _) = case p_ of
140 Frm => (Ctree.get_obj Ctree.g_form pt p, p)
141 | Pos.Res => ((fst o (Ctree.get_obj Ctree.g_result pt)) p, Pos.lev_on p)
142 | _ => error ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
144 case Rewrite.rewrite_set_ (ThyC.get_theory thy') false (assoc_rls rls) f of
146 => Applicable.Yes (Tactic.Rewrite_Set' (thy', false, assoc_rls rls, f, (f', asm)))
147 | NONE => Applicable.No (rls ^ " not applicable")
149 | check (m as Tactic.Rewrite_Set_Inst (subs, rls)) (pt, (p, p_)) =
150 if member op = [Pos.Pbl, Pos.Met] p_
151 then Applicable.No ((Tactic.input_to_string m)^" not for pos "^(Pos.pos'2str (p,p_)))
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 (f, _) = case p_ of
158 Frm => (Ctree.get_obj Ctree.g_form pt p, p)
159 | Pos.Res => ((fst o (Ctree.get_obj Ctree.g_result pt)) p, Pos.lev_on p)
160 | _ => error ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
161 val subst = Subst.T_from_input thy subs;
163 case Rewrite.rewrite_set_inst_ thy (*put_asm*)false subst (assoc_rls rls) f of
165 => Applicable.Yes (Tactic.Rewrite_Set_Inst' (thy', false, subst, assoc_rls rls, f, (f', asm)))
166 | NONE => Applicable.No (rls ^ " not applicable")
167 handle _ => Applicable.No ("syntax error in " ^(subs2str subs))
169 | check (m as Tactic.Subproblem (domID, pblID)) (_, (p, p_)) =
170 if Pos.on_specification p_
172 Applicable.No (Tactic.input_to_string m ^ " not for pos " ^ Pos.pos'2str (p, p_))
173 else (*some fields filled later in LI*)
174 Applicable.Yes (Tactic.Subproblem' ((domID, pblID, Method.id_empty), [],
175 TermC.empty, [], ContextC.empty, Auto_Prog.subpbl domID pblID))
176 (*Substitute combines two different kind of "substitution":
177 (1) subst_atomic: for ?a..?z
178 (2) Pattern.match: for solving equational systems (which raises exn for ?a..?z)*)
179 | check (m as Tactic.Substitute sube) (pt, (p, p_)) =
180 if member op = [Pos.Pbl, Pos.Met] p_
181 then Applicable.No (Tactic.input_to_string m ^ " not for pos " ^ Pos.pos'2str (p, p_))
184 val pp = Ctree.par_pblobj pt p
185 val thy = ThyC.get_theory (Ctree.get_obj Ctree.g_domID pt pp)
187 Frm => Ctree.get_obj Ctree.g_form pt p
188 | Pos.Res => (fst o (Ctree.get_obj Ctree.g_result pt)) p
189 | _ => error ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
190 val {rew_ord', erls, ...} = Specify.get_met (Ctree.get_obj Ctree.g_metID pt pp)
191 val subte = Subst.input_to_terms sube
192 val subst = Subst.T_from_string_eqs thy sube
193 val ro = Rewrite_Ord.assoc_rew_ord rew_ord'
195 if foldl and_ (true, map TermC.contains_Var subte)
197 let val f' = subst_atomic subst f
199 then Applicable.No (Subst.string_eqs_to_string sube ^ " not applicable")
200 else Applicable.Yes (Tactic.Substitute' (ro, erls, subte, f, f'))
203 case Rewrite.rewrite_terms_ thy ro erls subte f of
204 SOME (f', _) => Applicable.Yes (Tactic.Substitute' (ro, erls, subte, f, f'))
205 | NONE => Applicable.No (Subst.string_eqs_to_string sube ^ " not applicable")
207 | check (Tactic.Tac id) (pt, (p, p_)) =
209 val pp = Ctree.par_pblobj pt p;
210 val thy' = Ctree.get_obj Ctree.g_domID pt pp;
211 val thy = ThyC.get_theory thy';
213 Frm => Ctree.get_obj Ctree.g_form pt p
214 | Pos.Pbl => error "Solve_Step.check (p,Pos.Pbl) pt (Tac id): not at Pos.Pbl"
215 | Pos.Res => (fst o (Ctree.get_obj Ctree.g_result pt)) p
216 | _ => error ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
218 "subproblem_equation_dummy" =>
219 if TermC.is_expliceq f
220 then Applicable.Yes (Tactic.Tac_ (thy, UnparseC.term f, id, "subproblem_equation_dummy (" ^ UnparseC.term f ^ ")"))
221 else Applicable.No "applicable only to equations made explicit"
222 | "solve_equation_dummy" =>
223 let val (id', f') = ApplicableOLD.split_dummy (UnparseC.term f);
225 if id' <> "subproblem_equation_dummy"
226 then Applicable.No "no subproblem"
227 else if (ThyC.to_ctxt thy, f') |-> TermC.parseNEW |> the |> TermC.is_expliceq
228 then Applicable.Yes (Tactic.Tac_ (thy, UnparseC.term f, id, "[" ^ f' ^ "]"))
229 else error ("Solve_Step.check: f= " ^ f')
231 | _ => Applicable.Yes (Tactic.Tac_ (thy, UnparseC.term f, id, UnparseC.term f))
233 | check (Tactic.Take str) _ = Applicable.Yes (Tactic.Take' (TermC.str2term str)) (* always applicable ?*)
234 | check (Tactic.Begin_Trans) (pt, (p, p_)) =
236 val (f, _) = case p_ of (*p 12.4.00 unnecessary, implizit Take in gen*)
237 Pos.Frm => (Ctree.get_obj Ctree.g_form pt p, (Pos.lev_on o Pos.lev_dn) p)
238 | Pos.Res => ((fst o (Ctree.get_obj Ctree.g_result pt)) p, (Pos.lev_on o Pos.lev_dn o Pos.lev_on) p)
239 | _ => error ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
240 in (Applicable.Yes (Tactic.Begin_Trans' f))
241 handle _ => raise ERROR ("Solve_Step.check: Begin_Trans finds syntaxerror in '" ^ UnparseC.term f ^ "'")
243 | check (Tactic.End_Trans) (pt, (p, p_)) = (*TODO: check parent branches*)
245 then Applicable.Yes (Tactic.End_Trans' (Ctree.get_obj Ctree.g_result pt p))
246 else Applicable.No "'End_Trans' is not applicable at the beginning of a transitive sequence"
247 | check Tactic.End_Proof' _ = Applicable.Yes Tactic.End_Proof''
248 | check m _ = raise ERROR ("Solve_Step.check called for " ^ Tactic.input_to_string m);