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
12 (* datatype nxt_ = HElpless | Nexts of CalcHead.calcstate *)
18 | UNsafe of CalcHead.calcstate'
19 | Updated of CalcHead.calcstate' *)
23 pos' * NEW * mout * (string * tac) * safe * ptree
25 val TESTg_form : ptree * (int list * pos_) -> mout
29 (ptree * pos') * taci list ->
30 auto -> string * pos' list * (ptree * pos')
31 val detailstep : ptree -> pos' -> string * ptree * pos'
32 (* val e_tac_ : tac_ *)
33 val f2str : mout -> cterm'
34 (* val get_pblID : ptree * pos' -> pblID option *)
35 val initmatch : ptree -> pos' -> ptform
37 string * tac_ -> ptree * (int list * pos_) -> lOc_ *)
38 (* val loc_specify_ : tac_ -> ptree * pos' -> lOc_ *)
39 val locatetac : (*tests only*)
41 ptree * (posel list * pos_) ->
42 string * (taci list * pos' list * (ptree * (posel list * pos_)))
47 ptree -> pos' * NEW * mout * tac'_ * safe * ptree
49 val nxt_specify_ : ptree * (int list * pos_) -> calcstate'(*tests only*)
50 val set_method : metID -> ptree * pos' -> ptree * ocalhd
51 val set_problem : pblID -> ptree * pos' -> ptree * ocalhd
52 val set_theory : thyID -> ptree * pos' -> ptree * ocalhd
53 val step : pos' -> calcstate -> string * calcstate'
54 val trymatch : pblID -> ptree -> pos' -> ptform
55 val tryrefine : pblID -> ptree -> pos' -> ptform
60 (*------------------------------------------------------------------(**)
61 structure MathEngine : MATHENGINE =
63 (**)------------------------------------------------------------------*)
65 fun get_pblID (pt, (p,_):pos') =
66 let val p' = par_pblobj pt p
67 val (_,pI,_) = get_obj g_spec pt p'
68 val (_,(_,oI,_),_) = get_obj g_origin pt p'
69 in if pI <> e_pblID then SOME pI
70 else if oI <> e_pblID then SOME oI
72 (*fun get_pblID (pt, (p,_):pos') =
73 ((snd3 o (get_obj g_spec pt)) (par_pblobj pt p));*)
76 (*--vvv--dummies for test*)
77 val e_tac_ = Tac_ (Pure.thy,"","","");
79 ERror of string (*after loc_specify, loc_solve*)
80 | UNsafe of calcstate' (*after loc_specify, loc_solve*)
81 | Updated of calcstate'; (*after loc_specify, loc_solve*)
82 fun loc_specify_ m (pt,pos) =
85 let val (p,_,f,_,s,pt) = specify m pos [] pt;
86 (* val (_,_,_,_,_,pt')= specify m pos [] pt;
89 (Error' (Error_ e)) => ERror e
90 | _ => Updated ([], [], (pt,p)) end;
92 (*. TODO push return-value cs' into solve and rename solve->loc_solve?_? .*)
93 (* val (m, pos) = ((mI,m), ip);
94 val (m,(pt,pos) ) = ((mI,m), ptp);
96 fun loc_solve_ m (pt,pos) =
97 let val (msg, cs') = solve m (pt, pos);
98 (* val (tacis,dels,(pt',p')) = cs';
99 (tracing o istate2str) (get_istate pt' p');
100 (term2str o fst) (get_obj g_result pt' (fst p'));
109 | Nexts of calcstate; (**)
111 (*. locate a tactic in a script and apply it if possible .*)
112 (*report applicability of tac in tacis; pt is dropped in setNextTactic*)
113 fun locatetac _ (ptp as (_,([],Res))) = ("end-of-calculation", ([], [], ptp))
114 | locatetac tac (ptp as (pt, p)) =
115 let val (mI,m) = mk_tac'_ tac;
116 in case applicable_in p pt m of
117 Notappl e => ("not-applicable", ([],[], ptp):calcstate')
120 val x = if member op = specsteps mI
121 then loc_specify_ m ptp else loc_solve_ (mI,m) ptp
123 ERror e => ("failure", ([], [], ptp))
124 (*FIXXXXXME: loc_specify_, loc_solve_ TOGETHER with dropping meOLD+detail.sml*)
125 | UNsafe cs' => ("unsafe-ok", cs')
126 | Updated (cs' as (_,_,(_,p'))) => (*ev.SEVER.tacs like Begin_Trans*)
127 (if p' = ([],Res) then "end-of-calculation" else "ok", cs')
128 (*for SEVER.tacs user to ask ? *)
132 (*------------------------------------------------------------------
133 fun init_detail ptp = e_calcstate;(*15.8.03.MISSING-->solve.sml!?*)
134 (*----------------------------------------------------from solve.sml*)
135 | nxt_solv (Detail_Set'(thy', rls, t)) (pt, p) =
136 let (*val rls = the (assoc(!ruleset',rls'))
137 handle _ => error ("solve: '"^rls'^"' not known");*)
138 val thy = assoc_thy thy';
141 Rrls {scr=sc as Rfuns {init_state=ii,...},...} =>
142 (e_rls, sc, RrlsState (ii t))
143 | Rls {srls=srls,scr=sc as Script s,...} =>
144 (srls, sc, ScrState ([(one_scr_arg s,t)], [],
145 NONE, e_term, Sundef, true));
146 val pt = update_tac pt (fst p) (Detail_Set (id_rls rls));
147 val (p,cid,_,pt) = generate1 thy (Begin_Trans' t) is p pt;
148 val nx = (tac_2tac o fst3) (next_tac (thy',srls) (pt,p) sc is);
149 val aopt = applicable_in p pt nx;
151 Notappl s => error ("solve Detail_Set: "^s)
152 (* val Appl m = aopt;
154 | Appl m => solve ("discardFIXME",m) p pt end
155 ------------------------------------------------------------------*)
158 (*iterated by nxt_me; there (the resulting) ptp dropped
159 may call nxt_solve Apply_Method --- thus evaluated here after solve.sml*)
160 fun nxt_specify_ (ptp as (pt, pos as (p,p_))) =
161 let val pblobj as (PblObj{meth,origin=origin as (oris,(dI',pI',mI'),_),
162 probl,spec=(dI,pI,mI),...}) = get_obj I pt p;
164 if just_created_ pblobj (*by Subproblem*) andalso origin <> e_origin
167 ["no_met"] => nxt_specif (Refine_Tacitly pI') (pt, (p, Pbl))
168 | _ => nxt_specif Model_Problem (pt, (p,Pbl))
171 val cpI = if pI = e_pblID then pI' else pI;
172 val cmI = if mI = e_metID then mI' else mI;
173 val {ppc, prls, where_, ...} = get_pbt cpI;
174 val pre = check_preconds "thy 100820" prls where_ probl;
175 val pb = foldl and_ (true, map fst pre);
176 (*FIXME.WN0308: ~~~~~: just check true in itms of pbl/met?*)
177 val (_,tac) = nxt_spec p_ pb oris (dI',pI',mI') (probl, meth)
178 (ppc, (#ppc o get_met) cmI) (dI, pI, mI);
181 nxt_solv (Apply_Method' (mI, NONE, e_istate, e_ctxt)) (e_istate, e_ctxt) ptp
182 | _ => nxt_specif tac ptp end
185 (*.specify a new method;
186 WN0512 impl.incomplete, see 'nxt_specif (Specify_Method ' .*)
187 fun set_method (mI:metID) ptp =
188 let val ([(_, Specify_Method' (_, _, mits), _)], [], (pt, pos as (p,_))) =
189 nxt_specif (Specify_Method mI) ptp
190 val pre = [] (*...from Specify_Method'*)
191 val complete = true (*...from Specify_Method'*)
192 (*from Specify_Method' ? vvv, vvv ?*)
193 val PblObj {origin = (_,_,hdf), spec,...} = get_obj I pt p
194 in (pt, (complete, Met, hdf, mits, pre, spec):ocalhd) end;
196 (* val ([(_, Specify_Method' (_, _, mits), _)], [],_) =
197 nxt_specif (Specify_Method mI) ptp;
200 (*.specify a new problem;
201 WN0512 impl.incomplete, see 'nxt_specif (Specify_Problem ' .*)
202 (* val (pI, ptp) = (pI, (pt, ip));
204 fun set_problem pI (ptp: ptree * pos') =
205 let val ([(_, Specify_Problem' (_, (complete, (pits, pre))),_)],
206 _, (pt, pos as (p,_))) = nxt_specif (Specify_Problem pI) ptp
207 (*from Specify_Problem' ? vvv, vvv ?*)
208 val PblObj {origin = (_,_,hdf), spec,...} = get_obj I pt p
209 in (pt, (complete, Pbl, hdf, pits, pre, spec):ocalhd) end;
211 fun set_theory (tI:thyID) (ptp: ptree * pos') =
212 let val ([(_, Specify_Problem' (_, (complete, (pits, pre))),_)],
213 _, (pt, pos as (p,_))) = nxt_specif (Specify_Theory tI) ptp
214 (*from Specify_Theory' ? vvv, vvv ?*)
215 val PblObj {origin = (_,_,hdf), spec,...} = get_obj I pt p
216 in (pt, (complete, Pbl, hdf, pits, pre, spec):ocalhd) end;
218 (*.does a step forward; returns tactic used, ctree updated.
219 TODO.WN0512 redesign after specify-phase became more separated from solve-phase
223 (* val (ip as (_,p_), (ptp as (pt,p), tacis)) = (get_pos 1 1, get_calc 1);
224 val (ip as (_,p_), (ptp as (pt,p), tacis)) = (pos, cs);
225 val (ip as (_,p_), (ptp as (pt,p), tacis)) = (p, ((pt, e_pos'),[]));
226 val (ip as (_,p_), (ptp as (pt,p), tacis)) = (ip,cs);
228 fun step ((ip as (_,p_)):pos') ((ptp as (pt,p), tacis):calcstate) =
229 let val pIopt = get_pblID (pt,ip);
231 if (*p = ([],Res) orelse*) ip = ([],Res)
232 then ("end-of-calculation",(tacis, [], ptp):calcstate')
236 if ip = p (*the request is done where ptp waits for*)
238 let val (pt',c',p') = generate tacis (pt,[],p)
239 in ("ok", (tacis, c', (pt', p'))) end
240 else (case (if member op = [Pbl,Met] p_
241 then nxt_specify_ (pt,ip) else nxt_solve_ (pt,ip))
242 handle _ => ([],[],ptp)(*e.g. Add_Given "equality///"*) of
243 cs as ([],_,_) => ("helpless", cs)
245 | _ => (case pIopt of
246 NONE => ("no-fmz-spec", ([], [], ptp))
248 (case (if member op = [Pbl,Met] p_
249 andalso is_none (get_obj g_env pt (fst p))
250 (*^^^^^^^^: Apply_Method without init_form*)
251 then nxt_specify_ (pt,ip)
252 else nxt_solve_ (pt,ip) )
253 handle _ => ([],[],ptp)(*e.g.by Add_Giv"equality///"*) of
254 cs as ([],_,_) =>("helpless", cs)(*FIXXMEdel.handle*)
258 (* (nxt_solve_ (pt,ip)) handle e => print_exn e ;
265 (*.does several steps within one calculation as given by "type auto";
266 the steps may arbitrarily go into and leave different phases,
267 i.e. specify-phase and solve-phase.*)
268 (*TODO.WN0512 ? redesign after the phases have been more separated
269 at the fron-end in 05:
270 eg. CompleteCalcHead could be done by a separate fun !!!*)
271 (* val (ip, cs as (ptp as (pt,p),tacis)) = (get_pos cI 1, get_calc cI);
272 val (ip, cs as (ptp as (pt,p),tacis)) = (pold, get_calc cI);
273 val (c, ip, cs as (ptp as (_,p),tacis), Step s) =
274 ([]:pos' list, pold, get_calc cI, auto);
276 fun autocalc c ip (cs as (ptp as (_,p),tacis)) (Step s) =
278 then let val (str, (_, c', ptp)) = step ip cs;(*1*)
279 (*at least does 1 step, ev.1 too much*)
280 in (str, c@c', ptp) end
281 else let val (str, (_, c', ptp as (_, p))) = step ip cs;
283 then autocalc (c@c') p (ptp,[]) (Step (s-1))
284 else (str, c@c', ptp) end
285 (*handles autoord <= 3, autoord > 3 handled by all_/complete_solve*)
286 | autocalc c (pos as (_,p_)) ((pt,_), _(*tacis would help 1x in solve*))auto=
287 (* val (c:pos' list, (pos as (_,p_)),((pt,_),_),auto) =
288 ([], pold, get_calc cI, auto);
290 if autoord auto > 3 andalso just_created (pt, pos)
291 then let val ptp = all_modspec (pt, pos);
292 in all_solve auto c ptp end
294 if member op = [Pbl, Met] p_
295 then if not (is_complete_mod (pt, pos))
296 then let val ptp = complete_mod (pt, pos)
297 in if autoord auto < 3 then ("ok", c, ptp)
299 if not (is_complete_spec ptp)
300 then let val ptp = complete_spec ptp
301 in if autoord auto = 3 then ("ok", c, ptp)
302 else all_solve auto c ptp
304 else if autoord auto = 3 then ("ok", c, ptp)
305 else all_solve auto c ptp
308 if not (is_complete_spec (pt,pos))
309 then let val ptp = complete_spec (pt, pos)
310 in if autoord auto = 3 then ("ok", c, ptp)
311 else all_solve auto c ptp
313 else if autoord auto = 3 then ("ok", c, (pt, pos))
314 else all_solve auto c (pt, pos)
315 else complete_solve auto c (pt, pos);
316 (* val pbl = get_obj g_pbl (fst ptp) [];
317 val (oris,_,_) = get_obj g_origin (fst ptp) [];
324 (*.initialiye matching; before 'tryMatch' get the pblID to match with:
325 if no pbl has been specified, take the init from origin.*)
326 (*fun initmatch pt (pos as (p,_):pos') =
327 let val PblObj {probl,origin=(os,(_,pI,_),_),spec=(dI',pI',mI'),...} =
329 val pblID = if pI' = e_pblID
330 then (*TODO.WN051125 (#init o get_pbt) pI <<<*)
331 takelast (2, pI) (*FIXME.WN051125 a hack, impl.^^^*)
333 val spec = (dI',pblID,mI')
334 val {ppc,where_,prls,...} = get_pbt pblID
335 val (model_ok, (pbl, pre)) =
336 match_itms_oris (assoc_thy "Isac") probl (ppc,where_,prls) os
337 in ModSpec (ocalhd_complete pbl pre spec,
338 Pbl, e_term, pbl, pre, spec) end;*)
339 fun initcontext_pbl pt (pos as (p,_):pos') =
340 let val PblObj {probl,origin=(os,(_,pI,_),hdl),spec=(dI',pI',mI'),...} =
342 val pblID = if pI' = e_pblID
343 then (*TODO.WN051125 (#init o get_pbt) pI <<<*)
344 takelast (2, pI) (*FIXME.WN051125 a hack, impl.^^^*)
346 val {ppc,where_,prls,...} = get_pbt pblID
347 val (model_ok, (pbl, pre)) =
348 match_itms_oris (assoc_thy "Isac") probl (ppc,where_,prls) os
349 in (model_ok, pblID, hdl, pbl, pre) end;
351 fun initcontext_met pt (pos as (p,_):pos') =
352 let val PblObj {meth,origin=(os,(_,_,mI), _),spec=(_, _, mI'),...} =
354 val metID = if mI' = e_metID
355 then (*TODO.WN051125 (#init o get_pbt) pI <<<*)
356 takelast (2, mI) (*FIXME.WN051125 a hack, impl.^^^*)
358 val {ppc,pre,prls,scr,...} = get_met metID
359 val (model_ok, (pbl, pre)) =
360 match_itms_oris (assoc_thy "Isac") meth (ppc,pre,prls) os
361 in (model_ok, metID, scr, pbl, pre) end;
363 (*.match the model of a problem at pos p
364 with the model-pattern of the problem with pblID*)
365 fun context_pbl pI pt (p:pos) =
366 let val PblObj {probl,origin=(os,_,hdl),...} = get_obj I pt p
367 val {ppc,where_,prls,...} = get_pbt pI
368 val (model_ok, (pbl, pre)) =
369 match_itms_oris (assoc_thy "Isac") probl (ppc,where_,prls) os
370 in (model_ok, pI, hdl, pbl, pre) end;
372 fun context_met mI pt (p:pos) =
373 let val PblObj {meth,origin=(os,_,hdl),...} = get_obj I pt p
374 val {ppc,pre,prls,scr,...} = get_met mI
375 val (model_ok, (pbl, pre)) =
376 match_itms_oris (assoc_thy "Isac") meth (ppc,pre,prls) os
377 in (model_ok, mI, scr, pbl, pre) end
380 (* val (pI, pt, pos as (p,_)) = (pblID, pt, p);
382 fun tryrefine pI pt (pos as (p,_):pos') =
383 let val PblObj {probl,origin=(os,_,hdl),...} = get_obj I pt p
384 in case refine_pbl (assoc_thy "Isac") pI probl of
385 NONE => (*copy from context_pbl*)
386 let val {ppc,where_,prls,...} = get_pbt pI
387 val (_, (pbl, pre)) = match_itms_oris (assoc_thy "Isac")
388 probl (ppc,where_,prls) os
389 in (false, pI, hdl, pbl, pre) end
390 | SOME (pI, (pbl, pre)) =>
391 (true, pI, hdl, pbl, pre)
394 (* val (pt, (pos as (p,p_):pos')) = (pt, ip);
396 fun detailstep pt (pos as (p,p_):pos') =
397 let val nd = get_nd pt p
400 then if (is_rewset o (get_obj g_tac nd)) [(*root of nd*)]
401 then detailrls pt pos
402 else ("no-Rewrite_Set...", EmptyPtree, e_pos')
403 else ("donesteps", pt(*, get_formress [] ((lev_on o lev_dn) p) cn*),
404 (p @ [length (children (get_nd pt p))], Res) )
409 (***. for mathematics authoring on sml-toplevel; no XML .***)
412 (* val sp = (dI',pI',mI');
415 (*15.8.03 for me with loc_specify/solve, nxt_specify/solve
416 delete as soon as TESTg_form -> _mout_ dropped*)
420 let val (form,_,_) = pt_extract ptp
422 Form t => Form' (FormKF (~1,EdUndef,0,Nundef,term2str t))
423 | ModSpec (_,p_, head, gfr, pre, _) =>
424 Form' (PpcKF (0,EdUndef,0,Nundef,
425 (case p_ of Pbl => Problem[] | Met => Method[],
426 itms2itemppc (assoc_thy"Isac") gfr pre)))
429 (*.create a calc-tree; for use within sml: thus ^^^ NOT decoded to ^;
430 compare "fun CalcTree" which DOES decode.*)
431 fun CalcTreeTEST [(fmz, sp):fmz] =
432 (* val [(fmz, sp):fmz] = [(fmz, (dI',pI',mI'))];
433 val [(fmz, sp):fmz] = [([], ("e_domID", ["e_pblID"], ["e_metID"]))];
435 let val cs as ((pt,p), tacis) = nxt_specify_init_calc (fmz, sp)
436 val tac = case tacis of [] => Empty_Tac | _ => (#1 o hd) tacis
437 val f = TESTg_form (pt,p)
438 in (p, []:NEW, f, (tac2IDstr tac, tac), Sundef, pt) end;
440 (*for tests > 15.8.03 after separation setnexttactic / nextTac:
441 external view: me should be used by math-authors as done so far
442 internal view: loc_specify/solve, nxt_specify/solve used
443 i.e. same as in setnexttactic / nextTac*)
444 (*ENDE TESTPHASE 08/10.03:
445 NEW loeschen, eigene Version von locatetac, step
446 meNEW, CalcTreeTEST: tac'_ -replace-> tac, remove [](cid) *)
448 (* val ((_,tac), p, _, pt) = (nxt, p, c, pt);
450 fun me ((_,tac):tac'_) (p:pos') (_:NEW(*remove*)) (pt:ptree) =
453 (*locatetac is here for testing by me; step would suffice in me*)
454 case locatetac tac (pt,p) of
455 ("ok", (_, _, ptp)) => ptp
456 | ("unsafe-ok", (_, _, ptp)) => ptp
457 | ("not-applicable",_) => (pt, p)
458 | ("end-of-calculation", (_, _, ptp)) => ptp
459 | ("failure",_) => error "sys-error";
460 val (_, ts) = (*WN101102 NOT tested, if step would create _same_ ptp*)
461 (case step p ((pt, e_pos'),[]) of
462 ("ok", (ts as (tac,_,_)::_, _, _)) => ("",ts)
463 | ("helpless",_) => ("helpless: cannot propose tac", [])
464 | ("no-fmz-spec",_) => error "no-fmz-spec"
465 | ("end-of-calculation", (ts, _, _)) => ("",ts))
466 handle _ => error "sys-error";
469 tacis as (_::_) => let val (tac,_,_) = last_elem tacis in tac end
470 | _ => if p = ([],Res) then End_Proof' else Empty_Tac;
471 (*form output comes from locatetac*)
472 in (p:pos', []:NEW, TESTg_form (pt, p),
473 (tac2IDstr tac, tac):tac'_, Sundef, pt) end;
475 (*for quick test-print-out, until 'type inout' is removed*)
476 fun f2str (Form' (FormKF (_, _, _, _, cterm'))) = cterm';
480 (*------------------------------------------------------------------(**)
483 (**)------------------------------------------------------------------*)