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 (* val ptp as (pt, p) = (pt, p);
115 val ptp as (pt, p) = (pt, ip);
117 | locatetac tac (ptp as (pt, p)) =
118 let val (mI,m) = mk_tac'_ tac;
119 in case applicable_in p pt m of
120 Notappl e => ("not-applicable", ([],[], ptp):calcstate')
122 (* val Appl m = applicable_in p pt m;
124 let val x = if member op = specsteps mI
125 then loc_specify_ m ptp else loc_solve_ (mI,m) ptp
127 ERror e => ("failure", ([], [], ptp))
128 (*FIXXXXXME: loc_specify_, loc_solve_ TOGETHER with dropping meOLD+detail.sml*)
129 | UNsafe cs' => ("unsafe-ok", cs')
130 | Updated (cs' as (_,_,(_,p'))) =>
131 (*ev.SEVER.tacs like Begin_Trans*)
132 (if p' = ([],Res) then "end-of-calculation" else "ok",
133 cs')(*for -"- user to ask ? *)
138 (*------------------------------------------------------------------
139 fun init_detail ptp = e_calcstate;(*15.8.03.MISSING-->solve.sml!?*)
140 (*----------------------------------------------------from solve.sml*)
141 | nxt_solv (Detail_Set'(thy', rls, t)) (pt, p) =
142 let (*val rls = the (assoc(!ruleset',rls'))
143 handle _ => error ("solve: '"^rls'^"' not known");*)
144 val thy = assoc_thy thy';
147 Rrls {scr=sc as Rfuns {init_state=ii,...},...} =>
148 (e_rls, sc, RrlsState (ii t))
149 | Rls {srls=srls,scr=sc as Script s,...} =>
150 (srls, sc, ScrState ([(one_scr_arg s,t)], [],
151 NONE, e_term, Sundef, true));
152 val pt = update_tac pt (fst p) (Detail_Set (id_rls rls));
153 val (p,cid,_,pt) = generate1 thy (Begin_Trans' t) is p pt;
154 val nx = (tac_2tac o fst3) (next_tac (thy',srls) (pt,p) sc is);
155 val aopt = applicable_in p pt nx;
157 Notappl s => error ("solve Detail_Set: "^s)
158 (* val Appl m = aopt;
160 | Appl m => solve ("discardFIXME",m) p pt end
161 ------------------------------------------------------------------*)
164 (*iterated by nxt_me; there (the resulting) ptp dropped
165 may call nxt_solve Apply_Method --- thus evaluated here after solve.sml*)
166 (* val (ptp as (pt, pos as (p,p_))) = ptp;
167 val (ptp as (pt, pos as (p,p_))) = (pt,ip);
169 fun nxt_specify_ (ptp as (pt, pos as (p,p_))) =
170 let val pblobj as (PblObj{meth,origin=origin as (oris,(dI',pI',mI'),_),
171 probl,spec=(dI,pI,mI),...}) = get_obj I pt p;
172 in if just_created_ pblobj (*by Subproblem*) andalso origin <> e_origin
174 ["no_met"] => nxt_specif (Refine_Tacitly pI') (pt, (p, Pbl))
175 | _ => nxt_specif Model_Problem (pt, (p,Pbl))
176 else let val cpI = if pI = e_pblID then pI' else pI;
177 val cmI = if mI = e_metID then mI' else mI;
178 val {ppc,prls,where_,...} = get_pbt cpI;
179 val pre = check_preconds "thy 100820" prls where_ probl;
180 val pb = foldl and_ (true, map fst pre);
181 (*FIXME.WN0308: ~~~~~: just check true in itms of pbl/met?*)
183 nxt_spec p_ pb oris (dI',pI',mI') (probl, meth)
184 (ppc, (#ppc o get_met) cmI) (dI, pI, mI);
187 (* val Apply_Method mI = tac;
189 nxt_solv (Apply_Method' (mI, NONE, e_istate, e_ctxt)) (e_istate, e_ctxt) ptp
190 | _ => nxt_specif tac ptp end
194 (*.specify a new method;
195 WN0512 impl.incomplete, see 'nxt_specif (Specify_Method ' .*)
196 fun set_method (mI:metID) ptp =
197 let val ([(_, Specify_Method' (_, _, mits), _)], [], (pt, pos as (p,_))) =
198 nxt_specif (Specify_Method mI) ptp
199 val pre = [] (*...from Specify_Method'*)
200 val complete = true (*...from Specify_Method'*)
201 (*from Specify_Method' ? vvv, vvv ?*)
202 val PblObj {origin = (_,_,hdf), spec,...} = get_obj I pt p
203 in (pt, (complete, Met, hdf, mits, pre, spec):ocalhd) end;
205 (* val ([(_, Specify_Method' (_, _, mits), _)], [],_) =
206 nxt_specif (Specify_Method mI) ptp;
209 (*.specify a new problem;
210 WN0512 impl.incomplete, see 'nxt_specif (Specify_Problem ' .*)
211 (* val (pI, ptp) = (pI, (pt, ip));
213 fun set_problem pI (ptp: ptree * pos') =
214 let val ([(_, Specify_Problem' (_, (complete, (pits, pre))),_)],
215 _, (pt, pos as (p,_))) = nxt_specif (Specify_Problem pI) ptp
216 (*from Specify_Problem' ? vvv, vvv ?*)
217 val PblObj {origin = (_,_,hdf), spec,...} = get_obj I pt p
218 in (pt, (complete, Pbl, hdf, pits, pre, spec):ocalhd) end;
220 fun set_theory (tI:thyID) (ptp: ptree * pos') =
221 let val ([(_, Specify_Problem' (_, (complete, (pits, pre))),_)],
222 _, (pt, pos as (p,_))) = nxt_specif (Specify_Theory tI) ptp
223 (*from Specify_Theory' ? vvv, vvv ?*)
224 val PblObj {origin = (_,_,hdf), spec,...} = get_obj I pt p
225 in (pt, (complete, Pbl, hdf, pits, pre, spec):ocalhd) end;
227 (*.does a step forward; returns tactic used, ctree updated.
228 TODO.WN0512 redesign after specify-phase became more separated from solve-phase
232 (* val (ip as (_,p_), (ptp as (pt,p), tacis)) = (get_pos 1 1, get_calc 1);
233 val (ip as (_,p_), (ptp as (pt,p), tacis)) = (pos, cs);
234 val (ip as (_,p_), (ptp as (pt,p), tacis)) = (p, ((pt, e_pos'),[]));
235 val (ip as (_,p_), (ptp as (pt,p), tacis)) = (ip,cs);
237 fun step ((ip as (_,p_)):pos') ((ptp as (pt,p), tacis):calcstate) =
238 let val pIopt = get_pblID (pt,ip);
240 if (*p = ([],Res) orelse*) ip = ([],Res)
241 then ("end-of-calculation",(tacis, [], ptp):calcstate')
245 if ip = p (*the request is done where ptp waits for*)
247 let val (pt',c',p') = generate tacis (pt,[],p)
248 in ("ok", (tacis, c', (pt', p'))) end
249 else (case (if member op = [Pbl,Met] p_
250 then nxt_specify_ (pt,ip) else nxt_solve_ (pt,ip))
251 handle _ => ([],[],ptp)(*e.g. Add_Given "equality///"*) of
252 cs as ([],_,_) => ("helpless", cs)
254 | _ => (case pIopt of
255 NONE => ("no-fmz-spec", ([], [], ptp))
257 (case (if member op = [Pbl,Met] p_
258 andalso is_none (get_obj g_env pt (fst p))
259 (*^^^^^^^^: Apply_Method without init_form*)
260 then nxt_specify_ (pt,ip)
261 else nxt_solve_ (pt,ip) )
262 handle _ => ([],[],ptp)(*e.g.by Add_Giv"equality///"*) of
263 cs as ([],_,_) =>("helpless", cs)(*FIXXMEdel.handle*)
267 (* (nxt_solve_ (pt,ip)) handle e => print_exn e ;
274 (*.does several steps within one calculation as given by "type auto";
275 the steps may arbitrarily go into and leave different phases,
276 i.e. specify-phase and solve-phase.*)
277 (*TODO.WN0512 ? redesign after the phases have been more separated
278 at the fron-end in 05:
279 eg. CompleteCalcHead could be done by a separate fun !!!*)
280 (* val (ip, cs as (ptp as (pt,p),tacis)) = (get_pos cI 1, get_calc cI);
281 val (ip, cs as (ptp as (pt,p),tacis)) = (pold, get_calc cI);
282 val (c, ip, cs as (ptp as (_,p),tacis), Step s) =
283 ([]:pos' list, pold, get_calc cI, auto);
285 fun autocalc c ip (cs as (ptp as (_,p),tacis)) (Step s) =
287 then let val (str, (_, c', ptp)) = step ip cs;(*1*)
288 (*at least does 1 step, ev.1 too much*)
289 in (str, c@c', ptp) end
290 else let val (str, (_, c', ptp as (_, p))) = step ip cs;
292 then autocalc (c@c') p (ptp,[]) (Step (s-1))
293 else (str, c@c', ptp) end
294 (*handles autoord <= 3, autoord > 3 handled by all_/complete_solve*)
295 | autocalc c (pos as (_,p_)) ((pt,_), _(*tacis would help 1x in solve*))auto=
296 (* val (c:pos' list, (pos as (_,p_)),((pt,_),_),auto) =
297 ([], pold, get_calc cI, auto);
299 if autoord auto > 3 andalso just_created (pt, pos)
300 then let val ptp = all_modspec (pt, pos);
301 in all_solve auto c ptp end
303 if member op = [Pbl, Met] p_
304 then if not (is_complete_mod (pt, pos))
305 then let val ptp = complete_mod (pt, pos)
306 in if autoord auto < 3 then ("ok", c, ptp)
308 if not (is_complete_spec ptp)
309 then let val ptp = complete_spec ptp
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, ptp)
314 else all_solve auto c ptp
317 if not (is_complete_spec (pt,pos))
318 then let val ptp = complete_spec (pt, pos)
319 in if autoord auto = 3 then ("ok", c, ptp)
320 else all_solve auto c ptp
322 else if autoord auto = 3 then ("ok", c, (pt, pos))
323 else all_solve auto c (pt, pos)
324 else complete_solve auto c (pt, pos);
325 (* val pbl = get_obj g_pbl (fst ptp) [];
326 val (oris,_,_) = get_obj g_origin (fst ptp) [];
333 (*.initialiye matching; before 'tryMatch' get the pblID to match with:
334 if no pbl has been specified, take the init from origin.*)
335 (*fun initmatch pt (pos as (p,_):pos') =
336 let val PblObj {probl,origin=(os,(_,pI,_),_),spec=(dI',pI',mI'),...} =
338 val pblID = if pI' = e_pblID
339 then (*TODO.WN051125 (#init o get_pbt) pI <<<*)
340 takelast (2, pI) (*FIXME.WN051125 a hack, impl.^^^*)
342 val spec = (dI',pblID,mI')
343 val {ppc,where_,prls,...} = get_pbt pblID
344 val (model_ok, (pbl, pre)) =
345 match_itms_oris (assoc_thy "Isac") probl (ppc,where_,prls) os
346 in ModSpec (ocalhd_complete pbl pre spec,
347 Pbl, e_term, pbl, pre, spec) end;*)
348 fun initcontext_pbl pt (pos as (p,_):pos') =
349 let val PblObj {probl,origin=(os,(_,pI,_),hdl),spec=(dI',pI',mI'),...} =
351 val pblID = if pI' = e_pblID
352 then (*TODO.WN051125 (#init o get_pbt) pI <<<*)
353 takelast (2, pI) (*FIXME.WN051125 a hack, impl.^^^*)
355 val {ppc,where_,prls,...} = get_pbt pblID
356 val (model_ok, (pbl, pre)) =
357 match_itms_oris (assoc_thy "Isac") probl (ppc,where_,prls) os
358 in (model_ok, pblID, hdl, pbl, pre) end;
360 fun initcontext_met pt (pos as (p,_):pos') =
361 let val PblObj {meth,origin=(os,(_,_,mI), _),spec=(_, _, mI'),...} =
363 val metID = if mI' = e_metID
364 then (*TODO.WN051125 (#init o get_pbt) pI <<<*)
365 takelast (2, mI) (*FIXME.WN051125 a hack, impl.^^^*)
367 val {ppc,pre,prls,scr,...} = get_met metID
368 val (model_ok, (pbl, pre)) =
369 match_itms_oris (assoc_thy "Isac") meth (ppc,pre,prls) os
370 in (model_ok, metID, scr, pbl, pre) end;
372 (*.match the model of a problem at pos p
373 with the model-pattern of the problem with pblID*)
374 fun context_pbl pI pt (p:pos) =
375 let val PblObj {probl,origin=(os,_,hdl),...} = get_obj I pt p
376 val {ppc,where_,prls,...} = get_pbt pI
377 val (model_ok, (pbl, pre)) =
378 match_itms_oris (assoc_thy "Isac") probl (ppc,where_,prls) os
379 in (model_ok, pI, hdl, pbl, pre) end;
381 fun context_met mI pt (p:pos) =
382 let val PblObj {meth,origin=(os,_,hdl),...} = get_obj I pt p
383 val {ppc,pre,prls,scr,...} = get_met mI
384 val (model_ok, (pbl, pre)) =
385 match_itms_oris (assoc_thy "Isac") meth (ppc,pre,prls) os
386 in (model_ok, mI, scr, pbl, pre) end
389 (* val (pI, pt, pos as (p,_)) = (pblID, pt, p);
391 fun tryrefine pI pt (pos as (p,_):pos') =
392 let val PblObj {probl,origin=(os,_,hdl),...} = get_obj I pt p
393 in case refine_pbl (assoc_thy "Isac") pI probl of
394 NONE => (*copy from context_pbl*)
395 let val {ppc,where_,prls,...} = get_pbt pI
396 val (_, (pbl, pre)) = match_itms_oris (assoc_thy "Isac")
397 probl (ppc,where_,prls) os
398 in (false, pI, hdl, pbl, pre) end
399 | SOME (pI, (pbl, pre)) =>
400 (true, pI, hdl, pbl, pre)
403 (* val (pt, (pos as (p,p_):pos')) = (pt, ip);
405 fun detailstep pt (pos as (p,p_):pos') =
406 let val nd = get_nd pt p
409 then if (is_rewset o (get_obj g_tac nd)) [(*root of nd*)]
410 then detailrls pt pos
411 else ("no-Rewrite_Set...", EmptyPtree, e_pos')
412 else ("donesteps", pt(*, get_formress [] ((lev_on o lev_dn) p) cn*),
413 (p @ [length (children (get_nd pt p))], Res) )
418 (***. for mathematics authoring on sml-toplevel; no XML .***)
421 (* val sp = (dI',pI',mI');
424 (*15.8.03 for me with loc_specify/solve, nxt_specify/solve
425 delete as soon as TESTg_form -> _mout_ dropped*)
429 let val (form,_,_) = pt_extract ptp
431 Form t => Form' (FormKF (~1,EdUndef,0,Nundef,term2str t))
432 | ModSpec (_,p_, head, gfr, pre, _) =>
433 Form' (PpcKF (0,EdUndef,0,Nundef,
434 (case p_ of Pbl => Problem[] | Met => Method[],
435 itms2itemppc (assoc_thy"Isac") gfr pre)))
438 (*.create a calc-tree; for use within sml: thus ^^^ NOT decoded to ^;
439 compare "fun CalcTree" which DOES decode.*)
440 fun CalcTreeTEST [(fmz, sp):fmz] =
441 (* val [(fmz, sp):fmz] = [(fmz, (dI',pI',mI'))];
442 val [(fmz, sp):fmz] = [([], ("e_domID", ["e_pblID"], ["e_metID"]))];
444 let val cs as ((pt,p), tacis) = nxt_specify_init_calc (fmz, sp)
445 val tac = case tacis of [] => Empty_Tac | _ => (#1 o hd) tacis
446 val f = TESTg_form (pt,p)
447 in (p, []:NEW, f, (tac2IDstr tac, tac), Sundef, pt) end;
449 (*for tests > 15.8.03 after separation setnexttactic / nextTac:
450 external view: me should be used by math-authors as done so far
451 internal view: loc_specify/solve, nxt_specify/solve used
452 i.e. same as in setnexttactic / nextTac*)
453 (*ENDE TESTPHASE 08/10.03:
454 NEW loeschen, eigene Version von locatetac, step
455 meNEW, CalcTreeTEST: tac'_ -replace-> tac, remove [](cid) *)
457 (* val ((_,tac), p, _, pt) = (nxt, p, c, pt);
459 fun me ((_,tac):tac'_) (p:pos') (_:NEW(*remove*)) (pt:ptree) =
462 (*locatetac is here for testing by me; step would suffice in me*)
463 case locatetac tac (pt,p) of
464 ("ok", (_, _, ptp)) => ptp
465 | ("unsafe-ok", (_, _, ptp)) => ptp
466 | ("not-applicable",_) => (pt, p)
467 | ("end-of-calculation", (_, _, ptp)) => ptp
468 | ("failure",_) => error "sys-error";
469 val (_, ts) = (*WN101102 NOT tested, if step would create _same_ ptp*)
470 (case step p ((pt, e_pos'),[]) of
471 ("ok", (ts as (tac,_,_)::_, _, _)) => ("",ts)
472 | ("helpless",_) => ("helpless: cannot propose tac", [])
473 | ("no-fmz-spec",_) => error "no-fmz-spec"
474 | ("end-of-calculation", (ts, _, _)) => ("",ts))
475 handle _ => error "sys-error";
478 tacis as (_::_) => let val (tac,_,_) = last_elem tacis in tac end
479 | _ => if p = ([],Res) then End_Proof' else Empty_Tac;
480 (*form output comes from locatetac*)
481 in (p:pos', []:NEW, TESTg_form (pt, p),
482 (tac2IDstr tac, tac):tac'_, Sundef, pt) end;
484 (*for quick test-print-out, until 'type inout' is removed*)
485 fun f2str (Form' (FormKF (_, _, _, _, cterm'))) = cterm';
489 (*------------------------------------------------------------------(**)
492 (**)------------------------------------------------------------------*)