1 (* The _functional_ mathematics engine, ie. without a state.
2 Input and output are Isabelle's formulae as strings.
3 (c) Walther Neuper 2000
6 use"ME/mathengine.sml";
10 signature MATHENGINE =
13 (* datatype nxt_ = HElpless | Nexts of CalcHead.calcstate *)
19 | UNsafe of CalcHead.calcstate'
20 | Updated of CalcHead.calcstate' *)
24 pos' * NEW * mout * (string * tac) * safe * ptree
26 val TESTg_form : ptree * (int list * pos_) -> mout
30 (ptree * pos') * taci list ->
31 auto -> string * pos' list * (ptree * pos')
32 val detailstep : ptree -> pos' -> string * ptree * pos'
33 (* val e_tac_ : tac_ *)
34 (* val get_pblID : ptree * pos' -> pblID Library.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'
50 val step : pos' -> calcstate -> string * calcstate'
51 val trymatch : pblID -> ptree -> int list * 'a -> ptform
52 val tryrefine : pblID -> ptree -> int list * 'a -> ptform
58 structure MathEngine : MATHENGINE =
61 fun get_pblID (pt, (p,_):pos') =
62 let val p' = par_pblobj pt p
63 val (_,pI,_) = get_obj g_spec pt p'
64 val (_,(_,oI,_),_) = get_obj g_origin pt p'
65 in if pI <> e_pblID then Some pI
66 else if oI <> e_pblID then Some oI
68 (*fun get_pblID (pt, (p,_):pos') =
69 ((snd3 o (get_obj g_spec pt)) (par_pblobj pt p));*)
72 (*----------------------vvv--------------------dummies for test*)
73 val e_tac_ = Tac_ (Pure.thy,"","","");
75 ERror of string (*after loc_specify, loc_solve*)
76 | UNsafe of calcstate' (*after loc_specify, loc_solve*)
77 | Updated of calcstate'; (*after loc_specify, loc_solve*)
78 fun loc_specify_ m (pt,pos) =
81 let val (p,_,f,_,s,pt) = specify m pos [] pt;
82 (* val (_,_,_,_,_,pt')= specify m pos [] pt;
85 (Error' (Error_ e)) => ERror e
86 | _ => Updated ([], [], (pt,p)) end;
88 fun loc_solve_ m (pt,pos) =
89 (* val (m, pos) = ((mI,m), ip);
90 val (m,(pt,pos) ) = ((mI,m), ptp);
92 let val (msg, cs') = solve m (pt, pos);
93 (* val (tacis,dels,(pt',p')) = cs';
94 (writeln o istate2str) (get_istate pt' p');
95 (term2str o fst) (get_obj g_result pt' (fst p'));
104 | Nexts of calcstate; (**)
106 (*report applicability of tac in tacis; pt is dropped in setNextTactic*)
107 fun locatetac _ (ptp as (_,([],Res))) = ("end-of-calculation", ([], [], ptp))
108 | locatetac tac (ptp as (pt, p)) =
109 (* val ptp as (pt, p) = (pt, ip);
110 val ptp as (pt, p) = (pt, p);
112 let val (mI,m) = mk_tac'_ tac;
113 in case applicable_in p pt m of
114 Notappl e => ("not-applicable", ([],[], ptp):calcstate')
116 (* val Appl m = applicable_in p pt m;
118 let val x = if mI mem specsteps
119 then loc_specify_ m ptp else loc_solve_ (mI,m) ptp
121 ERror e => ("failure", ([], [], ptp))
122 (*FIXXXXXME: loc_specify_, loc_solve_ TOGETHER with dropping meOLD+detail.sml*)
123 | UNsafe cs' => ("unsafe-ok", cs')
124 | Updated (cs' as (_,_,(_,p'))) =>
125 (*ev.SEVER.tacs like Begin_Trans*)
126 (if p' = ([],Res) then "end-of-calculation" else "ok",
127 cs')(*for -"- 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 _ => raise 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 => raise 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 (* val (ptp as (pt, pos as (p,p_))) = ptp;
162 val (ptp as (pt, pos as (p,p_))) = (pt,ip);
164 let val pblobj as (PblObj{meth,origin=origin as (oris,(dI',pI',mI'),_),
165 probl,spec=(dI,pI,mI),...}) = get_obj I pt p;
166 in if just_created_ pblobj (*by Subproblem*) andalso origin <> e_origin
168 ["no_met"] => nxt_specif (Refine_Tacitly pI') (pt, (p,Pbl))
169 | _ => nxt_specif (Model_Problem pI') (pt, (p,Pbl))
170 else let val cpI = if pI = e_pblID then pI' else pI;
171 val cmI = if mI = e_metID then mI' else mI;
172 val {ppc,prls,where_,...} = get_pbt cpI;
173 val pre = check_preconds thy prls where_ probl;
174 val pb = foldl and_ (true, map fst pre);
175 (*FIXME.WN0308: ~~~~~: just check true in itms of pbl/met?*)
177 nxt_spec p_ pb oris (dI',pI',mI') (probl,meth)
178 (ppc,(#ppc o get_met) cmI) (dI,pI,mI);
181 (* val Apply_Method mI = tac;
183 nxt_solv (Apply_Method' (mI, None, e_istate)) e_istate ptp
184 | _ => nxt_specif tac ptp end
188 (*WN11.9.03--- was only for hide, detail --> dialog ---------
192 -> (ptree * pos') * : always the latest ovewriting the others
193 (tac * tac_ * istate) list : new tacs found PREpended to others
194 -"- calcstate (typeinference doesn get it)
195 -> detail * detail ->
196 hid -> : handles packages of tacs (eg. modeling)
199 fun nxt_me _ _ _ Hmodel = raise error "nxt_me: not impl. for Hmodel"
200 | nxt_me _ _ _ Hspecify = raise error "nxt_me: not impl. for Hspecify"
201 | nxt_me _ _ _ Happly = raise error "nxt_me: not impl. for Happly"
202 (* val ((ptp as (_,(_,p_)), tacis), (hi, de)) = ((ptp, []), (hide, detail));
204 | nxt_me pI (ptp as (_,([],Res)), tacis) _ _ = Nexts (ptp, tacis)
205 | nxt_me pI ((ptp as (_,(_,p_)), tacis):calcstate) (hi, de) _(*Hundef,Htac*)=
206 (* val ((ptp as (_,(_,p_)), tacis), (hi, de)) = ((ptp, []), (hide, detail));
207 val ((ptp as (_,(_,p_)), [] ), (hi, de)) = ((ptp, []), (hide, detail));
209 let val x = (if p_ mem [Pbl,Met]
210 then nxt_specify_ ptp else nxt_solve_ ptp)
211 handle _ => (ptp, []) (*e.g. by Add_Given "equality"///*)
214 | (cs as (ptp, (tac,_,_)::_)) =>
215 let val (cs as (ptp, tacis' as (tac,_,_)::_)) =
216 if is_detail pI tac de then init_detail ptp else cs
217 in case is_hide pI tac hi of
218 Show => Nexts (ptp, tacis' @ tacis)
220 nxt_me pI (ptp, tacis' @ tacis) (hi, de) hid
223 ------------------------------------------------------------------*)
225 (*.does a step forward; returns tactic used, ctree updated
229 (* val (ip as (_,p_), (ptp as (pt,p), tacis)) = (p, ((pt, e_pos'),[]));
230 val (ip as (_,p_), (ptp as (pt,p), tacis)) = (ip,cs);
231 val (ip as (_,p_), (ptp as (pt,p), tacis)) = (get_pos 1 1, get_calc 1);
233 fun step ((ip as (_,p_)):pos') ((ptp as (pt,p), tacis):calcstate) =
234 let val pIopt = get_pblID (pt,ip);
235 in if p = ([],Res) orelse ip = ([],Res)
236 then ("end-of-calculation",(tacis, [], ptp):calcstate') else
239 (* val((tac,_,_)::_) = tacis;
241 if ip = p (*the request is done where ptp waits for*)
242 then let val (pt',c',p') = generate tacis (pt,[],p)
243 in ("ok", (tacis, c', (pt', p'))) end
244 else (case (if p_ mem [Pbl,Met]
245 then nxt_specify_ (pt,ip) else nxt_solve_ (pt,ip))
246 handle _ => ([],[],ptp)(*e.g.by Add_Given "equality///"*)
247 of cs as ([],_,_) => ("helpless", cs)
249 | _ => (case pIopt of
250 None => ("no-fmz-spec", ([], [], ptp))
252 (* val Some pI = pIopt;
253 val cs = (if p_ mem [Pbl,Met] andalso is_none (get_obj g_env pt (fst p))
254 then nxt_specify_ (pt,ip) else nxt_solve_ (pt,ip))
255 handle _ => ([], ptp);
257 (case (if p_ mem [Pbl,Met]
258 andalso is_none (get_obj g_env pt (fst p))
259 (*^^^^^^^^: Apply_Method without init_form*)
260 then nxt_specify_ (pt,ip) else nxt_solve_ (pt,ip) )
261 handle _ => ([],[],ptp)(*e.g.by Add_Giv"equality/"*)
262 of cs as ([],_,_) =>("helpless", cs)(*FIXXMEdel.handle*)
266 (* (nxt_solve_ (pt,ip)) handle e => print_exn e ;
270 (* is_complete_mod ptp;
271 is_complete_spec ptp;
272 get_obj g_form pt [1];
273 print_depth 11;pt;print_depth 3;
275 val (str, (_, (pt',p'))) = step ip cs;
276 get_obj g_form pt [1];
278 val (ip, cs as (ptp as (pt,p),tacis)) = (get_pos cI 1, get_calc cI);
280 val (c, ip, cs as (ptp as (_,p),tacis), Step s) =
281 ([]:pos' list, pold, get_calc cI, auto);
283 fun autocalc c ip (cs as (ptp as (_,p),tacis)) (Step s) =
286 _::_ => (*if ip = p then ("ok", generate tacis ptp)##DONE IN step#
287 else*) 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 | _ => let val (str, (_, c', ptp)) = step ip cs
291 in (str, c@c', ptp) end
293 _::_ => (*if ip = p then
294 let val ptp as (_, p) = generate tacis ptp
295 in autocalc p (ptp, []) (Step(s-1-(length tacis))) end
296 else*) let val (str, (_, c', ptp as (_, p))) = step ip cs;
298 then autocalc (c@c') p (ptp,[]) (Step(s-1))
299 else (str, c@c', ptp) end
300 | _ => let val (str, (_, c', ptp as (_, p))) = step ip cs;
302 then autocalc (c@c') p (ptp, []) (Step (s-1))
303 else (str, c@c', ptp) end)
304 (*handles autoord <= 3, autoord > 3 handled by all_/complete_solve*)
305 | autocalc c (pos as (_,p_)) ((pt,_), _(*tacis would help 1x in solve*))auto=
306 (* val (c:pos' list, (pos as (_,p_)),((pt,_),_),auto) =
307 ([], pold, get_calc cI, auto);
309 if autoord auto > 3 andalso just_created (pt, pos)
310 then let val ptp = all_modspec (pt, pos);
311 in all_solve auto c ptp end
314 then if not (is_complete_mod (pt, pos))
315 then let val ptp = complete_mod (pt, pos)
316 in if autoord auto < 3 then ("ok", c, ptp)
318 if not (is_complete_spec ptp)
319 then let val ptp = complete_spec ptp
320 in if autoord auto = 3 then ("ok", c, ptp)
321 else all_solve auto c ptp
323 else if autoord auto = 3 then ("ok", c, ptp)
324 else all_solve auto c ptp
327 if not (is_complete_spec (pt,pos))
328 then let val ptp = complete_spec (pt, pos)
329 in if autoord auto = 3 then ("ok", c, ptp)
330 else all_solve auto c ptp
332 else if autoord auto = 3 then ("ok", c, (pt, pos))
333 else all_solve auto c (pt, pos)
334 else complete_solve auto c (pt, pos);
335 (* val pbl = get_obj g_pbl (fst ptp) [];
336 val (oris,_,_) = get_obj g_origin (fst ptp) [];
343 (*.initialiye matching; before 'tryMatch' get the pblID to match with:
344 if no pbl has been specified, take the init from origin.*)
345 fun initmatch pt (pos as (p,_):pos') =
346 let val PblObj {probl,origin=(os,(_,pI,_),_),spec=(dI',pI',mI'),...} =
348 val pblID = if pI' = e_pblID
349 then (*TODO.WN051125 (#init o get_pbt) pI <<<*)
350 takelast (2, pI) (*FIXME.WN051125 a hack, impl.^^^*)
352 val spec = (dI',pblID,mI')
353 val {ppc,where_,prls,...} = get_pbt pblID
354 val (_, (pbl, pre)) = match_itms_oris (assoc_thy "Isac.thy") probl
356 in ModSpec (ocalhd_complete pbl pre spec,
357 Pbl, e_term, pbl, pre, spec) end;
359 (*.match the model of a problem at pos p
360 with the model-pattern of the problem with pblID*)
361 fun trymatch pI pt (pos as (p,_):pos') =
362 let val PblObj {probl,origin=(os,_,_),spec=(dI,_,mI),...} = get_obj I pt p
363 val spec = (dI,pI,mI)
364 val {ppc,where_,prls,...} = get_pbt pI
365 val (_, (pbl, pre)) = match_itms_oris (assoc_thy "Isac.thy") probl
367 in ModSpec (ocalhd_complete pbl pre spec,
368 Pbl, e_term, pbl, pre, spec) end;
370 fun tryrefine pI pt (pos as (p,_):pos') =
371 let val PblObj {probl,origin=(os,_,_),spec=(dI,_,mI),...} = get_obj I pt p
372 in case refine_pbl (assoc_thy "Isac.thy") pI probl of
373 None => (*copy from trymatch*)
374 let val {ppc,where_,prls,...} = get_pbt pI
375 val (_, (pbl, pre)) = match_itms_oris (assoc_thy "Isac.thy")
376 probl (ppc,where_,prls) os
377 in ModSpec (ocalhd_complete pbl pre (dI,pI,mI),
378 Pbl, e_term, pbl, pre, (dI,pI,mI)) end
379 | Some (pI, (pbl, pre)) =>
380 ModSpec (ocalhd_complete pbl pre (dI,pI,mI),
381 Pbl, e_term, pbl, pre, (dI,pI,mI))
384 (* val pos as (p,p_) = ip;
386 fun detailstep pt (pos as (p,p_):pos') =
387 let val nd = get_nd pt p
389 in if null cn andalso (is_rewset o (get_obj g_tac nd)) [] (*subnode!*)
390 then detailrls pt pos
391 else ("donesteps", pt(*, get_formress [] ((lev_on o lev_dn) p) cn*),
392 (p @ [length (children (get_nd pt p))], Res) )
397 (***. for mathematics authoring on sml-toplevel; no XML .***)
400 (* val sp = (dI',pI',mI');
403 (*15.8.03 for me with loc_specify/solve, nxt_specify/solve
404 delete as soon as TESTg_form -> _mout_ dropped*)
408 let val (form,_,_) = pt_extract ptp
410 Form t => Form' (FormKF (~1,EdUndef,0,Nundef,term2str t))
411 | ModSpec (_,p_, head, gfr, pre, _) =>
412 Form' (PpcKF (0,EdUndef,0,Nundef,
413 (case p_ of Pbl => Problem[] | Met => Method[],
414 itms2itemppc (assoc_thy"Isac.thy") gfr pre)))
417 fun CalcTreeTEST [(fmz, sp):fmz] =
418 (* val [(fmz, sp):fmz] = [(fmz, (dI',pI',mI'))];
420 let val cs as ((pt,p), (tac,_,_)::_) = nxt_specify_init_calc (fmz, sp);
421 val f = TESTg_form (pt,p);
422 (* val _ = (add_calc cs; IteratorTEST 1; moveActiveRootTEST 1);WN050707*)
423 in (p, []:NEW, f, (tac2IDstr tac, tac), Sundef, pt) end;
425 (*for tests > 15.8.03 after separation setnexttactic / nextTac:
426 external view: me should be used by math-authors as done so far
427 internal view: loc_specify/solve, nxt_specify/solve used
428 i.e. same as in setnexttactic / nextTac*)
429 (*ENDE TESTPHASE 08/10.03:
430 NEW loeschen, eigene Version von locatetac, step
431 meNEW, CalcTreeTEST: tac'_ -replace-> tac, remove [](cid) *)
434 fun me ((_,tac):tac'_) (p:pos') (_:NEW(*remove*)) (pt:ptree) =
435 (* val (_,tac) = nxt;
437 (* val (msg, (tacis, pos's, (pt',p'))) = locatetac tac (pt,p);
438 f2str (TESTg_form (pt', p'));
439 (writeln o (itms2str thy)) (get_obj g_pbl pt' (fst p'));
440 writeln( istate2str (get_istate pt' ([3],Res)));
441 term2str (fst (get_obj g_result pt' [3]));
442 val (pt,p) = (pt',p');
444 (*locatetac is here for testing by me; step would suffice in me*)
445 case locatetac tac (pt,p) of
446 ("ok", (_, _, ptp)) => ptp
447 | ("unsafe-ok", (_, _, ptp)) => ptp
448 | ("not-applicable",_) => (pt, p)
449 | ("end-of-calculation", (_, _, ptp)) => ptp
450 | ("failure",_) => raise error "sys-error";
452 (* val (eee, (ts, _, (pt'',_))) = step p ((pt, e_pos'),[]);
454 ((*step is abused for getting next tac, resulting pt dropped*)
455 case step p ((pt, e_pos'),[]) of
456 ("ok", (ts as (tac,_,_)::_, _, _)) => ("",ts)
457 | ("helpless",_) => ("helpless: cannot propose tac", [])
458 | ("no-fmz-spec",_) => raise error "no-fmz-spec"
459 | ("end-of-calculation", (ts, _, _)) => ("",ts))
460 handle _ => raise error "sys-error";
461 val tac = case ts of ((tac,_,_)::_) => tac
462 | _ => if p = ([],Res) then End_Proof'
464 (*form output comes from locatetac*)
465 in(p:pos',[]:NEW, TESTg_form (pt, p),
466 (tac2IDstr tac, tac):tac'_, Sundef, pt) end;
468 (*for quick test-print-out, until 'type inout' is removed*)
469 fun f2str (Form' (FormKF (_, _, _, _, cterm'))) = cterm';
471 (*------------------------------------------------------------------
475 ------------------------------------------------------------------*)