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"~/proto2/isac/src/sml/ME/mathengine.sml";
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 get_pblID : ptree * pos' -> pblID Library.option *)
36 string * tac_ -> ptree * (int list * pos_) -> lOc_ *)
37 (* val loc_specify_ : tac_ -> ptree * pos' -> lOc_ *)
38 val locatetac : (*tests only*)
40 ptree * (posel list * pos_) ->
41 string * (taci list * pos' list * (ptree * (posel list * pos_)))
46 ptree -> pos' * NEW * mout * tac'_ * safe * ptree
48 val nxt_specify_ : ptree * (int list * pos_) -> CalcHead.calcstate'
49 val step : pos' -> CalcHead.calcstate -> string * CalcHead.calcstate'
50 val trymatch : pblID -> ptree -> int list * 'a -> ptform
51 val tryrefine : pblID -> ptree -> int list * 'a -> ptform
57 structure mathengine : MATHENGINE =
60 fun get_pblID (pt, (p,_):pos') =
61 let val p' = par_pblobj pt p
62 val (_,pI,_) = get_obj g_spec pt p'
63 val (_,(_,oI,_),_) = get_obj g_origin pt p'
64 in if pI <> e_pblID then Some pI
65 else if oI <> e_pblID then Some oI
67 (*fun get_pblID (pt, (p,_):pos') =
68 ((snd3 o (get_obj g_spec pt)) (par_pblobj pt p));*)
71 (*----------------------vvv--------------------dummies for test*)
72 val e_tac_ = Tac_ (Pure.thy,"","","");
74 ERror of string (*after loc_specify, loc_solve*)
75 | UNsafe of calcstate' (*after loc_specify, loc_solve*)
76 | Updated of calcstate'; (*after loc_specify, loc_solve*)
77 fun loc_specify_ m (pt,pos) =
80 let val (p,_,f,_,s,pt) = specify m pos [] pt;
81 (* val (_,_,_,_,_,pt')= specify m pos [] pt;
84 (Error' (Error_ e)) => ERror e
85 | _ => Updated ([], [], (pt,p)) end;
87 fun loc_solve_ m (pt,pos) =
88 (* val (m, pos) = ((mI,m), ip);
89 val (m,(pt,pos) ) = ((mI,m), ptp);
91 let val (*p,_,f,_,s,pt*) (msg, cs') = solve m (pt, pos)
92 (* 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 ev. calls 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.WN.8.03: ~~~~: just check true in itms of pbl/met?*)
177 nxt_spec Pbl 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_istate ptp
182 | _ => nxt_specif tac ptp end
186 (*WN11.9.03--- was only for hide, detail --> dialog ---------
190 -> (ptree * pos') * : always the latest ovewriting the others
191 (tac * tac_ * istate) list : new tacs found PREpended to others
192 -"- calcstate (typeinference doesn get it)
193 -> detail * detail ->
194 hid -> : handles packages of tacs (eg. modeling)
197 fun nxt_me _ _ _ Hmodel = raise error "nxt_me: not impl. for Hmodel"
198 | nxt_me _ _ _ Hspecify = raise error "nxt_me: not impl. for Hspecify"
199 | nxt_me _ _ _ Happly = raise error "nxt_me: not impl. for Happly"
200 (* val ((ptp as (_,(_,p_)), tacis), (hi, de)) = ((ptp, []), (hide, detail));
202 | nxt_me pI (ptp as (_,([],Res)), tacis) _ _ = Nexts (ptp, tacis)
203 | nxt_me pI ((ptp as (_,(_,p_)), tacis):calcstate) (hi, de) _(*Hundef,Htac*)=
204 (* val ((ptp as (_,(_,p_)), tacis), (hi, de)) = ((ptp, []), (hide, detail));
205 val ((ptp as (_,(_,p_)), [] ), (hi, de)) = ((ptp, []), (hide, detail));
207 let val x = (if p_ mem [Pbl,Met]
208 then nxt_specify_ ptp else nxt_solve_ ptp)
209 handle _ => (ptp, []) (*e.g. by Add_Given "equality"///*)
212 | (cs as (ptp, (tac,_,_)::_)) =>
213 let val (cs as (ptp, tacis' as (tac,_,_)::_)) =
214 if is_detail pI tac de then init_detail ptp else cs
215 in case is_hide pI tac hi of
216 Show => Nexts (ptp, tacis' @ tacis)
218 nxt_me pI (ptp, tacis' @ tacis) (hi, de) hid
221 ------------------------------------------------------------------*)
223 (*.does a step forward; returns tactic used, ctree updated.*)
224 fun step ((ip as (_,p_)):pos') ((ptp as (pt,p), tacis):calcstate) =
225 (* val (ip as (_,p_), (ptp as (pt,p), tacis)) = (get_pos 1 1, get_calc 1);
226 val (ip as (_,p_), (ptp as (pt,p), tacis)) = (ip,cs);
227 val (ip as (_,p_), (ptp as (pt,p), tacis)) = (p, ((pt, e_pos'),[]));
229 let val pIopt = get_pblID (pt,ip);
230 in if p = ([],Res) orelse ip = ([],Res)
231 then ("end-of-calculation",(tacis, [], ptp):calcstate') else
234 (* val((tac,_,_)::_) = tacis;
237 then let val (pt',c',p') = generate tacis (pt,[],p)
238 in ("ok", (tacis, c', (pt', p'))) end
239 else (case (if p_ mem [Pbl,Met]
240 then nxt_specify_ (pt,ip) else nxt_solve_ (pt,ip))
241 handle _ => ([],[],ptp)(*e.g.by Add_Given "equality///"*)
242 of cs as ([],_,_) => ("helpless", cs)
244 | _ => (case pIopt of
245 None => ("no-fmz-spec", ([], [], ptp))
247 (* val Some pI = pIopt;
248 val cs = (if p_ mem [Pbl,Met] andalso is_none (get_obj g_env pt (fst p))
249 then nxt_specify_ (pt,ip) else nxt_solve_ (pt,ip))
250 handle _ => ([], ptp);
252 (case (if p_ mem [Pbl,Met]
253 andalso is_none (get_obj g_env pt (fst p))
254 (*........: Apply_Method without init_form*)
255 then nxt_specify_ (pt,ip) else nxt_solve_ (pt,ip))
256 handle _ => ([],[],ptp)(*e.g.by Add_Giv"equality/"*)
257 of cs as ([],_,_) => ("helpless", cs)
263 (* is_complete_mod ptp;
264 is_complete_spec ptp;
265 get_obj g_form pt [1];
266 print_depth 11;pt;print_depth 3;
268 val (str, (_, (pt',p'))) = step ip cs;
269 get_obj g_form pt [1];
271 val (ip, cs as (ptp as (pt,p),tacis)) = (get_pos cI 1, get_calc cI);
273 val (c, ip, cs as (ptp as (_,p),tacis)) = ([]:pos' list, pold, get_calc cI);
276 fun autocalc c ip (cs as (ptp as (_,p),tacis)) (Step s) =
279 _::_ => (*if ip = p then ("ok", generate tacis ptp)##DONE IN step#
280 else*) let val (str, (_, c', ptp)) = step ip cs;(*1*)
281 (*at least does 1 step, ev.1 too much*)
282 in (str, c@c', ptp) end
283 | _ => let val (str, (_, c', ptp)) = step ip cs
284 in (str, c@c', ptp) end
286 _::_ => (*if ip = p then
287 let val ptp as (_, p) = generate tacis ptp
288 in autocalc p (ptp, []) (Step(s-1-(length tacis))) end
289 else*) let val (str, (_, c', ptp as (_, p))) = step ip cs;
291 then autocalc (c@c') p (ptp,[]) (Step(s-1))
292 else (str, c@c', ptp) end
293 | _ => let val (str, (_, c', ptp as (_, p))) = step ip cs;
295 then autocalc (c@c') p (ptp, []) (Step (s-1))
296 else (str, c@c', ptp) end)
297 (*handles autoord <= 3, autoord > 3 handled by all_/complete_solve*)
298 | autocalc c (pos as (_,p_)) ((pt,_), _(*tacis would help 1x in solve*))auto=
299 (* val (pos as (_,p_), ((pt,_), _)) = (get_pos cI 1, get_calc cI);
300 autocalc (get_pos cI 1) (get_calc cI) auto;
302 val (c:pos' list, (pos as (_,p_)),((pt,_),_),auto) =
303 ([], pold, get_calc cI, auto);
305 if autoord auto > 3 andalso just_created (pt, pos)
306 then let val ptp = all_modspec (pt, pos);
307 in all_solve auto c ptp end
310 then if not (is_complete_mod (pt, pos))
311 then let val ptp = complete_mod (pt, pos)
312 in if autoord auto < 3 then ("ok", c, ptp)
314 if not (is_complete_spec ptp)
315 then let val ptp = complete_spec ptp
316 in if autoord auto = 3 then ("ok", c, ptp)
317 else all_solve auto c ptp
319 else if autoord auto = 3 then ("ok", c, ptp)
320 else all_solve auto c ptp
323 if not (is_complete_spec (pt,pos))
324 then let val ptp = complete_spec (pt, pos)
325 in if autoord auto = 3 then ("ok", c, ptp)
326 else all_solve auto c ptp
328 else if autoord auto = 3 then ("ok", c, (pt, pos))
329 else all_solve auto c (pt, pos)
330 else complete_solve auto c (pt, pos);
331 (* val pbl = get_obj g_pbl (fst ptp) [];
332 val (oris,_,_) = get_obj g_origin (fst ptp) [];
339 fun trymatch pI pt (pos as (p,_)) =
340 let val PblObj {probl,origin=(os,_,_),spec=(dI,_,mI),...} = get_obj I pt p
341 val spec = (dI,pI,mI)
342 val {ppc,where_,prls,...} = get_pbt pI
343 val (_, (pbl, pre)) = match_itms_oris (assoc_thy "Isac.thy") probl
345 in ModSpec (ocalhd_complete pbl pre spec,
346 Pbl, e_term, pbl, pre, spec) end;
348 fun tryrefine pI pt (pos as (p,_)) =
349 let val PblObj {probl,origin=(os,_,_),spec=(dI,_,mI),...} = get_obj I pt p
350 in case refine_pbl (assoc_thy "Isac.thy") pI probl of
351 None => (*copy from trymatch*)
352 let val {ppc,where_,prls,...} = get_pbt pI
353 val (_, (pbl, pre)) = match_itms_oris (assoc_thy "Isac.thy")
354 probl (ppc,where_,prls) os
355 in ModSpec (ocalhd_complete pbl pre (dI,pI,mI),
356 Pbl, e_term, pbl, pre, (dI,pI,mI)) end
357 | Some (pI, (pbl, pre)) =>
358 ModSpec (ocalhd_complete pbl pre (dI,pI,mI),
359 Pbl, e_term, pbl, pre, (dI,pI,mI))
362 (* val pos as (p,p_) = ip;
364 fun detailstep pt (pos as (p,p_):pos') =
365 let val nd = get_nd pt p
367 in if null cn andalso (is_rewset o (get_obj g_tac nd)) [] (*subnode!*)
368 then detailrls pt pos
369 else ("donesteps", pt(*, get_formress [] ((lev_on o lev_dn) p) cn*),
370 (p @ [length (children (get_nd pt p))], Res) )
375 (***. for mathematics authoring on sml-toplevel; no XML .***)
378 (* val sp = (dI',pI',mI');
381 (*15.8.03 for me with loc_specify/solve, nxt_specify/solve
382 delete as soon as TESTg_form -> _mout_ dropped*)
386 let val (form,_,_) = pt_extract ptp
388 Form t => Form' (FormKF (~1,EdUndef,0,Nundef,term2str t))
389 | ModSpec (_,p_, head, gfr, pre, _) =>
390 Form' (PpcKF (0,EdUndef,0,Nundef,
391 (case p_ of Pbl => Problem[] | Met => Method[],
392 itms2itemppc (assoc_thy"Isac.thy") gfr pre)))
395 fun CalcTreeTEST [(fmz, sp):fmz] =
396 (* val [(fmz, sp):fmz] = [(fmz, (dI',pI',mI'))];
398 let (*val _ = states:= []; WN050707*)
399 val cs as ((pt,p), (tac,_,_)::_) = nxt_specify_init_calc (fmz, sp);
400 val f = TESTg_form (pt,p);
401 (* val _ = (add_calc cs; IteratorTEST 1; moveActiveRootTEST 1);WN050707*)
402 in (p, []:NEW, f, (tac2IDstr tac, tac), Sundef, pt) end;
404 (*for tests > 15.8.03 after separation setnexttactic / nextTac:
405 external view: me should be used by math-authors as done so far
406 internal view: loc_specify/solve, nxt_specify/solve used
407 i.e. same as in setnexttactic / nextTac*)
408 (*ENDE TESTPHASE 08/10.03:
409 NEW loeschen, eigene Version von locatetac, step
410 meNEW, CalcTreeTEST: tac'_ -replace-> tac, remove [](cid) *)
413 fun me ((_,tac):tac'_) (p:pos') (_:NEW(*remove*)) (pt:ptree) =
414 (* val (_,tac) = nxt;
416 (* val (msg, (tacis, pos's, (pt',p'))) = locatetac tac (pt,p);
417 (writeln o (itms2str thy)) (get_obj g_pbl pt' (fst p'));
418 writeln( istate2str (get_istate pt' ([3],Res)));
419 term2str (fst (get_obj g_result pt' [3]));
420 val (pt,p) = (pt',p');
422 case locatetac tac (pt,p) of
423 ("ok", (_, _, ptp)) => ptp
424 | ("unsafe-ok", (_, _, ptp)) => ptp
425 | ("not-applicable",_) => (pt, p)
426 | ("end-of-calculation", (_, _, ptp)) => ptp
427 | ("failure",_) => raise error "sys-error";
429 (* (writeln o (itms2str thy)) (get_obj g_pbl pt (fst p));
430 val (eee, (ts, (pt'',_))) = step p ((pt, e_pos'),[]);
431 (writeln o (itms2str thy)) (get_obj g_pbl pt'' (fst p));
433 (case step p ((pt, e_pos'),[]) of
434 ("ok", (ts as (tac,_,_)::_, _, _)) => ("",ts)
435 | ("helpless",_) => ("helpless: cannot propose tac", [])
436 | ("no-fmz-spec",_) => raise error "no-fmz-spec"
437 | ("end-of-calculation", (ts, _, _)) => ("",ts))
438 handle _ => raise error "sys-error";
439 val tac = case ts of ((tac,_,_)::_) => tac
440 | _ => if p = ([],Res) then End_Proof'
442 in (p:pos',[]:NEW, TESTg_form (pt, p),
443 (tac2IDstr tac, tac):tac'_, Sundef, pt) end;