[Text_Isac/../inverse_z_transform.sml] lucin: prep. "fun associate" for sig. locate_input_tactic
note: the ONLY Test_Isac which does NOT work is
--- test [SignalProcessing,Z_Transform,Inverse_sub] me
1 (* The _functional_ mathematics engine, ie. without a state.
2 Input and output are Isabelle's formulae as strings.
3 authors: Walther Neuper 2000
4 (c) due to copyright terms
7 signature MATH_ENGINE =
9 type NEW (* TODO: refactor "fun me" with calcstate and remove *)
10 val me : Solve.tac'_ -> Ctree.pos' -> NEW ->
11 Ctree.ctree -> Ctree.pos' * NEW * Generate.mout * Solve.tac'_ * Istate.safe * Ctree.ctree
12 val autocalc : Ctree.pos' list -> Ctree.pos' -> (Ctree.state) * Generate.taci list ->
13 Solve.auto -> string * Ctree.pos' list * (Ctree.state)
15 Tactic.input -> Ctree.state -> string * (Generate.taci list * Ctree.pos' list * (Ctree.state))
16 val step : Ctree.pos' -> Chead.calcstate -> string * Chead.calcstate'
17 val detailstep : Ctree.ctree -> Ctree.pos' -> string * Ctree.ctree * Ctree.pos'
18 val get_pblID : Ctree.state -> Celem.pblID option
20 val initcontext_met : Ctree.ctree -> Ctree.pos' -> bool * string list * Rule.program * Model.itm list * (bool * term) list
21 val initcontext_pbl : Ctree.ctree -> Ctree.pos' -> bool * string list * term * Model.itm list * (bool * term) list
22 val context_met : Celem.metID -> Ctree.ctree -> Ctree.pos -> bool * Celem.metID * Rule.program * Model.itm list * (bool * term) list
23 val context_pbl : Celem.pblID -> Ctree.ctree -> Ctree.pos -> bool * Celem.pblID * term * Model.itm list * (bool * term) list
24 val set_method : Celem.metID -> Ctree.state -> Ctree.ctree * Ctree.ocalhd
25 val set_problem : Celem.pblID -> Ctree.state -> Ctree.ctree * Ctree.ocalhd
26 val set_theory : Rule.thyID -> Ctree.state -> Ctree.ctree * Ctree.ocalhd
27 val tryrefine : Celem.pblID -> Ctree.ctree -> Ctree.pos' -> bool * Celem.pblID * term * Model.itm list * (bool * term) list
28 (* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
29 val CalcTreeTEST : Selem.fmz list -> Ctree.pos' * NEW * Generate.mout * (string * Tactic.input) * Istate.safe * Ctree.ctree
30 val f2str : Generate.mout -> Rule.cterm'
31 (*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
33 datatype lOc_ = ERror of string | UNsafe of Chead.calcstate' | Updated of Chead.calcstate'
34 val loc_solve_ : string * Tactic.T -> Ctree.ctree * Ctree.pos' -> lOc_
35 val loc_specify_ : Tactic.T -> Ctree.state -> lOc_
36 val nxt_specify_: Ctree.ctree * Ctree.pos' -> Chead.calcstate'
37 val TESTg_form : Ctree.state -> Generate.mout
38 ( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
40 (*----- unused code, kept as hints to design ideas ---------------------------------------------*)
45 structure Math_Engine(**): MATH_ENGINE(**) =
49 fun get_pblID (pt, (p, _): Ctree.pos') =
50 let val p' = Ctree.par_pblobj pt p
51 val (_, pI, _) = Ctree.get_obj Ctree.g_spec pt p'
52 val (_, (_, oI, _), _) = Ctree.get_obj Ctree.g_origin pt p'
54 if pI <> Celem.e_pblID
57 if oI <> Celem.e_pblID then SOME oI else NONE end;
60 ERror of string (*after loc_specify, loc_solve*)
61 | UNsafe of Chead.calcstate' (*after loc_specify, loc_solve*)
62 | Updated of Chead.calcstate' (*after loc_specify, loc_solve*)
64 fun loc_specify_ m (pt, pos) =
66 val (p, _, f, _, _, pt) = Chead.specify m pos [] pt;
68 case f of (Generate.Error' e) => ERror e | _ => Updated ([], [], (pt,p))
71 (* TODO push return-value cs' into solve and rename solve->loc_solve?_? *)
72 fun loc_solve_ m (pt, pos) =
74 val (msg, cs') = Solve.solve m (pt, pos);
76 case msg of "ok" => Updated cs' | msg => ERror msg
81 | Nexts of Chead.calcstate (**)
83 (* locate a tactic in a script and apply it if possible;
84 report of tacs' applicability in tacis; pt is dropped in setNextTactic*)
85 fun locatetac _ (ptp as (_, ([], Ctree.Res))) = ("end-of-calculation", ([], [], ptp))
86 | locatetac tac (ptp as (pt, p)) =
88 val (mI, m) = Solve.mk_tac'_ tac;
90 case Applicable.applicable_in p pt m of
91 Chead.Notappl _ => ("not-applicable", ([],[], ptp): Chead.calcstate')
94 val x = if member op = Solve.specsteps mI
95 then loc_specify_ m ptp else loc_solve_ (mI, m) ptp
98 ERror _ => ("failure", ([], [], ptp))
99 (*FIXXXXXME: loc_specify_, loc_solve_ TOGETHER with dropping meOLD+detail.sml*)
100 | UNsafe cs' => ("unsafe-ok", cs')
101 | Updated (cs' as (_, _, (_, p'))) => (*ev.SEVER.tacs like Begin_Trans*)
102 (if p' = ([], Ctree.Res) then "end-of-calculation" else "ok", cs')
103 (*for SEVER.tacs user to ask ? *)
107 (* iterated by nxt_me; there (the resulting) ptp dropped
108 may call nxt_solve Apply_Method --- thus evaluated here after solve.sml *)
109 fun nxt_specify_ (ptp as (pt, (p, p_))) =
111 val (pblobj, meth, origin, oris, dI', pI', mI', probl, dI, pI, mI) =
112 case Ctree.get_obj I pt p of
113 pblobj as (Ctree.PblObj {meth, origin = origin as (oris, (dI', pI', mI'), _),
114 probl, spec = (dI, pI, mI), ...}) => (pblobj, meth, origin, oris, dI', pI', mI', probl, dI, pI, mI)
115 | Ctree.PrfObj _ => error "nxt_specify_: not on PrfObj"
117 if Ctree.just_created_ pblobj (*by Subproblem*) andalso origin <> Ctree.e_origin
120 ["no_met"] => Chead.nxt_specif (Tactic.Refine_Tacitly pI') (pt, (p, Ctree.Pbl))
121 | _ => Chead.nxt_specif Tactic.Model_Problem (pt, (p, Ctree.Pbl))
124 val cpI = if pI = Celem.e_pblID then pI' else pI;
125 val cmI = if mI = Celem.e_metID then mI' else mI;
126 val {ppc, prls, where_, ...} = Specify.get_pbt cpI;
127 val pre = Stool.check_preconds "thy 100820" prls where_ probl;
128 val pb = foldl and_ (true, map fst pre);
129 (*FIXME.WN0308: ~~~~~: just check true in itms of pbl/met?*)
131 Chead.nxt_spec p_ pb oris (dI', pI', mI') (probl, meth) (ppc, (#ppc o Specify.get_met) cmI) (dI, pI, mI)
134 Tactic.Apply_Method mI =>
135 LucinNEW.begin_end_prog (Tactic.Apply_Method' (mI, NONE, Istate.e_istate, Selem.e_ctxt)) (Istate.e_istate, Selem.e_ctxt) ptp
136 | _ => Chead.nxt_specif tac ptp
140 (* specify a new method; WN0512 impl.incomplete, see 'nxt_specif (Specify_Method ' *)
141 fun set_method mI ptp =
144 case Chead.nxt_specif (Tactic.Specify_Method mI) ptp of
145 ([(_, Tactic.Specify_Method' (_, _, mits), _)], [], (pt, (p, _))) => (mits, pt, p)
146 | _ => error "set_method: case 1 uncovered"
147 val pre = [] (*...from Specify_Method'*)
148 val complete = true (*...from Specify_Method'*)
149 (*from Specify_Method' ? vvv, vvv ?*)
151 case Ctree.get_obj I pt p of
152 Ctree.PblObj {origin = (_, _, hdf), spec, ...} => (hdf, spec)
153 | Ctree.PrfObj _ => error "set_method: case 2 uncovered"
155 (pt, (complete, Ctree.Met, hdf, mits, pre, spec) : Ctree.ocalhd)
158 (* specify a new problem; WN0512 impl.incomplete, see 'nxt_specif (Specify_Problem ' *)
159 fun set_problem pI ptp =
161 val (complete, pits, pre, pt, p) =
162 case Chead.nxt_specif (Tactic.Specify_Problem pI) ptp of
163 ([(_, Tactic.Specify_Problem' (_, (complete, (pits, pre))),_)], _, (pt, (p,_)))
164 => (complete, pits, pre, pt, p)
165 | _ => error "set_problem: case 1 uncovered"
166 (*from Specify_Problem' ? vvv, vvv ?*)
168 case Ctree.get_obj I pt p of
169 Ctree.PblObj {origin = (_, _, hdf), spec, ...} => (hdf, spec)
170 | Ctree.PrfObj _ => error "set_problem: case 2 uncovered"
172 (pt, (complete, Ctree.Pbl, hdf, pits, pre, spec) : Ctree.ocalhd)
175 fun set_theory tI ptp =
177 val (complete, pits, pre, pt, p) =
178 case Chead.nxt_specif (Tactic.Specify_Theory tI) ptp of
179 ([(_, Tactic.Specify_Problem' (_, (complete, (pits, pre))), _)], _, (pt, (p, _)))
180 => (complete, pits, pre, pt, p)
181 | _ => error "set_theory: case 1 uncovered"
182 (*from Specify_Theory' ? vvv, vvv ?*)
184 case Ctree.get_obj I pt p of
185 Ctree.PblObj {origin = (_, _, hdf), spec, ...} => (hdf, spec)
186 | Ctree.PrfObj _ => error "set_theory: case 2 uncovered"
187 in (pt, (complete, Ctree.Pbl, hdf, pits, pre, spec) : Ctree.ocalhd) end;
189 (* does a step forward; returns tactic used, ctree updated.
190 TODO.WN0512 redesign after specify-phase became more separated from solve-phase *)
191 fun step (ip as (_, p_)) (ptp as (pt,p), tacis) =
192 let val pIopt = get_pblID (pt,ip);
194 if ip = ([], Ctree.Res)
195 then ("end-of-calculation", (tacis, [], ptp): Chead.calcstate')
199 if ip = p (*the request is done where ptp waits for*)
201 let val (pt',c',p') = Generate.generate tacis (pt,[],p)
202 in ("ok", (tacis, c', (pt', p'))) end
203 else (case (if member op = [Ctree.Pbl, Ctree.Met] p_
204 then nxt_specify_ (pt, ip) else LucinNEW.do_solve_step (pt, ip))
205 handle ERROR msg => (writeln ("*** " ^ msg);
206 ([], [], ptp)) (*e.g. Add_Given "equality///"*) of
207 cs as ([],_,_) => ("helpless", cs)
209 | _ => (case pIopt of
210 NONE => ("no-fmz-spec", ([], [], ptp))
211 | SOME _ => (*vvvvvv: Apply_Method without init_form*)
212 (case if member op = [Ctree.Pbl, Ctree.Met] p_
213 andalso is_none (Ctree.get_obj Ctree.g_env pt (fst p))
214 then nxt_specify_ (pt, ip)
215 else (LucinNEW.do_solve_step (pt,ip))
216 handle ERROR msg => (writeln ("*** " ^ msg);
217 ([],[],ptp)) (*e.g. Add_Given "equality///"*) of
218 cs as ([],_,_) =>("helpless", cs) (*FIXXME del.handle*)
222 (* does several steps within one calculation as given by "type auto";
223 the steps may arbitrarily go into and leave different phases,
224 i.e. specify-phase and solve-phase *)
225 fun autocalc c ip cs (Solve.Step s) =
228 let val (str, (_, c', ptp)) = step ip cs; (* autoord = 1, probably does 1 step too much*)
229 in (str, c@c', ptp) end
231 let val (str, (_, c', ptp as (_, p))) = step ip cs;
234 then autocalc (c@c') p (ptp, []) (Solve.Step (s - 1))
235 else (str, c@c', ptp) end
236 (* handles autoord <= 3, autoord > 3 handled by all_solve, complete_solve *)
237 | autocalc c (pos as (_, p_)) ((pt, _), _(*tacis could help 1x in solve*)) auto =
238 if Solve.autoord auto > 3 andalso Ctree.just_created (pt, pos)
240 let val ptp = Chead.all_modspec (pt, pos);
241 in Solve.all_solve auto c ptp end (*... auto = 4 | 5 | 6 *)
243 if member op = [Ctree.Pbl, Ctree.Met] p_
245 if not (Chead.is_complete_mod (pt, pos))
247 let val ptp = Chead.complete_mod (pt, pos) (*... auto = 2 | 3 *)
249 if Solve.autoord auto < 3 then ("ok", c, ptp)
251 if not (Chead.is_complete_spec ptp)
253 let val ptp = Chead.complete_spec ptp
255 if Solve.autoord auto = 3 then ("ok", c, ptp) else Solve.all_solve auto c ptp
257 else if Solve.autoord auto = 3 then ("ok", c, ptp) else Solve.all_solve auto c ptp
260 if not (Chead.is_complete_spec (pt,pos))
262 let val ptp = Chead.complete_spec (pt, pos)
264 if Solve.autoord auto = 3 then ("ok", c, ptp) else Solve.all_solve auto c ptp
267 if Solve.autoord auto = 3 then ("ok", c, (pt, pos)) else Solve.all_solve auto c (pt, pos)
268 else Solve.complete_solve auto c (pt, pos);
270 (*.initialiye matching; before 'tryMatch' get the pblID to match with:
271 if no pbl has been specified, take the init from origin.*)
272 fun initcontext_pbl pt (p, _) =
274 val (probl, os, pI, hdl, pI') =
275 case Ctree.get_obj I pt p of
276 Ctree.PblObj {probl, origin = (os, (_, pI, _), hdl), spec=(_, pI', _), ...}
277 => (probl, os, pI, hdl, pI')
278 | Ctree.PrfObj _ => error "initcontext_pbl: uncovered case"
280 if pI' = Celem.e_pblID
281 then (* TODO.WN051125 (#init o get_pbt) pI *) takelast (2, pI)
283 val {ppc, where_, prls, ...} = Specify.get_pbt pblID
284 val (model_ok, (pbl, pre)) = Specify.match_itms_oris (Celem.assoc_thy "Isac") probl (ppc, where_, prls) os
286 (model_ok, pblID, hdl, pbl, pre)
289 fun initcontext_met pt (p,_) =
291 val (meth, os, mI, mI') =
292 case Ctree.get_obj I pt p of
293 Ctree.PblObj {meth, origin = (os, (_, _, mI), _), spec=(_, _, mI'), ...} => (meth, os, mI, mI')
294 | Ctree.PrfObj _ => error "initcontext_met: uncovered case"
295 val metID = if mI' = Celem.e_metID
296 then (*TODO.WN051125 (#init o get_pbt) pI *) takelast (2, mI)
298 val {ppc, pre, prls, scr, ...} = Specify.get_met metID
299 val (model_ok, (pbl, pre)) = Specify.match_itms_oris (Celem.assoc_thy "Isac") meth (ppc,pre,prls) os
301 (model_ok, metID, scr, pbl, pre)
304 (* match the model of a problem at pos p with the model-pattern of the problem with pblID *)
305 fun context_pbl pI pt p =
307 val (probl, os, hdl) =
308 case Ctree.get_obj I pt p of
309 Ctree.PblObj {probl,origin = (os, _, hdl),...} => (probl, os, hdl)
310 | Ctree.PrfObj _ => error "context_pbl: uncovered case"
311 val {ppc,where_,prls,...} = Specify.get_pbt pI
312 val (model_ok, (pbl, pre)) = Specify.match_itms_oris (Celem.assoc_thy "Isac") probl (ppc,where_,prls) os
314 (model_ok, pI, hdl, pbl, pre)
317 fun context_met mI pt p =
320 case Ctree.get_obj I pt p of
321 Ctree.PblObj {meth, origin = (os, _, _),...} => (meth, os)
322 | Ctree.PrfObj _ => error "context_met: uncovered case"
323 val {ppc,pre,prls,scr,...} = Specify.get_met mI
324 val (model_ok, (pbl, pre)) = Specify.match_itms_oris (Celem.assoc_thy "Isac") meth (ppc,pre,prls) os
326 (model_ok, mI, scr, pbl, pre)
329 fun tryrefine pI pt (p,_) =
331 val (probl, os, hdl) =
332 case Ctree.get_obj I pt p of
333 Ctree.PblObj {probl, origin = (os, _, hdl), ...} => (probl, os, hdl)
334 | Ctree.PrfObj _ => error "context_met: uncovered case"
336 case Specify.refine_pbl (Celem.assoc_thy "Isac") pI probl of
337 NONE => (*copy from context_pbl*)
339 val {ppc,where_,prls,...} = Specify.get_pbt pI
340 val (_, (pbl, pre)) = Specify.match_itms_oris (Celem.assoc_thy "Isac") probl (ppc,where_,prls) os
342 (false, pI, hdl, pbl, pre)
344 | SOME (pI, (pbl, pre)) => (true, pI, hdl, pbl, pre)
347 fun detailstep pt (pos as (p, _)) =
349 val nd = Ctree.get_nd pt p
350 val cn = Ctree.children nd
354 if (Tactic.is_rewset o (Ctree.get_obj Ctree.g_tac nd)) [(*root of nd*)]
355 then Solve.detailrls pt pos
356 else ("no-Rewrite_Set...", Ctree.EmptyPtree, Ctree.e_pos')
357 else ("donesteps", pt, (p @ [length (Ctree.children (Ctree.get_nd pt p))], Ctree.Res))
361 (*** for mathematics authoring on sml-toplevel; no XML ***)
365 (* 15.8.03 for me with loc_specify/solve, nxt_specify/solve
366 delete as soon as TESTg_form -> _mout_ dropped *)
369 val (form, _, _) = Chead.pt_extract ptp
372 Ctree.Form t => Generate.FormKF (Rule.term2str t)
373 | Ctree.ModSpec (_, p_, _, gfr, pre, _) =>
375 (case p_ of Ctree.Pbl => Generate.Problem []
376 | Ctree.Met => Generate.Method []
377 | _ => error "TESTg_form: uncovered case",
378 Specify.itms2itemppc (Celem.assoc_thy"Isac") gfr pre))
381 (* create a calc-tree; for use within sml: thus "^^^" NOT decoded to "^" etc;
382 compare "fun CalcTree" which DOES decode *)
383 fun CalcTreeTEST [(fmz, sp)] =
385 val ((pt, p), tacis) = Chead.nxt_specify_init_calc (fmz, sp)
386 val tac = case tacis of [] => Tactic.Empty_Tac | _ => (#1 o hd) tacis
387 val f = TESTg_form (pt,p)
388 in (p, []:NEW, f, (Tactic.tac2IDstr tac, tac), Istate.Sundef, pt) end
389 | CalcTreeTEST _ = error "CalcTreeTEST: uncovered case"
391 (*for tests > 15.8.03 after separation setnexttactic / nextTac:
392 external view: me should be used by math-authors as done so far
393 internal view: loc_specify/solve, nxt_specify/solve used
394 i.e. same as in setnexttactic / nextTac*)
395 (*ENDE TESTPHASE 08/10.03:
396 NEW loeschen, eigene Version von locatetac, step
397 meNEW, CalcTreeTEST: tac'_ -replace-> tac, remove [](cid) *)
399 fun me (_, tac) p _(*NEW remove*) pt =
402 (*locatetac is here for testing by me; step would suffice in me*)
403 case locatetac tac (pt, p) of
404 ("ok", (_, _, ptp)) => ptp
405 | ("unsafe-ok", (_, _, ptp)) => ptp
406 | ("not-applicable",_) => (pt, p)
407 | ("end-of-calculation", (_, _, ptp)) => ptp
408 | ("failure", _) => error "sys-error"
409 | _ => error "me: uncovered case"
411 (case step p ((pt, Ctree.e_pos'), []) of
412 ("ok", (ts as (_, _, _) :: _, _, _)) => ("", ts)
413 | ("helpless", _) => ("helpless: cannot propose tac", [])
414 | ("no-fmz-spec", _) => error "no-fmz-spec"
415 | ("end-of-calculation", (ts, _, _)) => ("", ts)
416 | _ => error "me: uncovered case")
417 handle ERROR msg => raise ERROR msg
420 tacis as (_::_) => let val (tac, _, _) = last_elem tacis in tac end
421 | _ => if p = ([], Ctree.Res) then Tactic.End_Proof' else Tactic.Empty_Tac;
422 in (p, [] : NEW, TESTg_form (pt, p) (* form output comes from locatetac *),
423 (Tactic.tac2IDstr tac, tac), Istate.Sundef, pt)
426 (* for quick test-print-out, until 'type inout' is removed *)
427 fun f2str (Generate.FormKF cterm') = cterm'
428 | f2str _ = error "f2str: uncovered case in fun.def.";