1 (* Title: the calctree, which holds a calculation
2 Author: Walther Neuper 1999
3 (c) due to copyright terms
6 signature BASIC_CALC_TREE =
8 (** definitions required for datatype ctree, renamed later appropriately **)
10 (** the basic datatype **)
17 datatype branch = AndB | CollectB | IntersectB | MapB | NoBranch | OrB | SequenceB | TransitiveB
18 datatype ostate = Complete | Incomplete | Inconsistent
22 loc: (Istate_Def.T * Proof.context) option * (Istate_Def.T * Proof.context) option,
27 origin: Model.ori list * Celem.spec * term,
28 probl: Model.itm list,
34 loc: (Istate_Def.T * Proof.context) option * (Istate_Def.T * Proof.context) option,
40 datatype ctree = EmptyPtree | Nd of ppobj * ctree list
42 (** basic functions **)
43 val e_ctree : ctree (* TODO: replace by EmptyPtree*)
44 val existpt' : Pos.pos' -> ctree -> bool (* for interface.sml *)
45 val is_interpos : Pos.pos' -> bool (* for interface.sml *)
46 val lev_pred' : ctree -> Pos.pos' -> Pos.pos' (* for interface.sml *)
47 val ins_chn : ctree list -> ctree -> Pos.pos -> ctree (* for solve.sml *)
48 val children : ctree -> ctree list (* for solve.sml *)
49 val get_nd : ctree -> Pos.pos -> ctree (* for solve.sml *)
50 val just_created_ : ppobj -> bool (* for mathengine.sml *)
51 val just_created : state -> bool (* for mathengine.sml *)
52 val e_origin : Model.ori list * Celem.spec * term (* for mathengine.sml *)
54 val is_pblobj : ppobj -> bool
55 val is_pblobj' : ctree -> Pos.pos -> bool
56 val is_pblnd : ctree -> bool
58 val g_spec : ppobj -> Celem.spec
59 val g_loc : ppobj -> (Istate_Def.T * Proof.context) option * (Istate_Def.T * Proof.context) option
60 val g_form : ppobj -> term
61 val g_pbl : ppobj -> Model.itm list
62 val g_met : ppobj -> Model.itm list
63 val g_metID : ppobj -> Celem.metID
64 val g_result : ppobj -> Selem.result
65 val g_tac : ppobj -> Tactic.input
66 val g_domID : ppobj -> ThyC.domID (* for appl.sml TODO: replace by thyID *)
68 val g_origin : ppobj -> Model.ori list * Celem.spec * term (* for script.sml *)
69 val get_loc : ctree -> Pos.pos' -> Istate_Def.T * Proof.context (* for script.sml *)
70 val get_istate_LI : ctree -> Pos.pos' -> Istate_Def.T (* for script.sml *)
71 val get_ctxt_LI: ctree -> Pos.pos' -> Proof.context
72 val get_ctxt : ctree -> Pos.pos' -> Proof.context (*DEPRECATED*)
73 val get_obj : (ppobj -> 'a) -> ctree -> Pos.pos -> 'a
74 val get_curr_formula : state -> term
75 val get_assumptions : ctree -> Pos.pos' -> term list (* for appl.sml *)
77 val new_val : term -> Istate_Def.T -> Istate_Def.T
78 (* for calchead.sml *)
79 type cid = cellID list
80 type ocalhd = bool * Pos.pos_ * term * Model.itm list * (bool * term) list * Celem.spec
81 datatype ptform = Form of term | ModSpec of ocalhd
82 val get_somespec' : Celem.spec -> Celem.spec -> Celem.spec
83 exception PTREE of string;
85 val parent_node : ctree -> Pos.pos -> bool * Pos.pos * Rule_Set.T (* for appl.sml *)
86 val rootthy : ctree -> theory (* for script.sml *)
87 (* ---- made visible ONLY for structure CTaccess : CALC_TREE_ACCESS --------------------------- *)
88 val appl_obj : (ppobj -> ppobj) -> ctree -> Pos.pos -> ctree
89 val existpt : Pos.pos -> ctree -> bool (* also for tests *)
90 val cut_tree : ctree -> Pos.pos * 'a -> ctree * Pos.pos' list (* also for tests *)
91 val insert_pt : ppobj -> ctree -> int list -> ctree
92 (* ---- made visible ONLY for structure CTnavi : CALC_TREE_NAVIGATION ------------------------- *)
93 val g_branch : ppobj -> branch
94 val g_form' : ctree -> term
95 val g_ostate : ppobj -> ostate
96 val g_ostate' : ctree -> ostate
97 val g_res : ppobj -> term
98 val g_res' : ctree -> term
99 (*/---- duplicates in CTnavi, reconsider structs -----------------------------------------------
100 val lev_dn : CTbasic.Pos.pos -> Pos.pos (* duplicate in ctree-navi.sml *)
101 val par_pblobj : CTbasic.ctree -> Pos.pos -> Pos.pos (* duplicate in ctree-navi.sml *)
102 ---- duplicates in CTnavi, reconsider structs ----------------------------------------------/*)
104 (*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
105 val pr_ctree : (Pos.pos -> ppobj -> string) -> ctree -> string
106 val pr_short : Pos.pos -> ppobj -> string
107 val g_ctxt : ppobj -> Proof.context
108 val g_fmz : ppobj -> Selem.fmz
109 val get_allp : Pos.pos' list -> Pos.pos * (int list * Pos.pos_) -> ctree -> Pos.pos' list
110 val get_allps : (Pos.pos * Pos.pos_) list -> Pos.posel list -> ctree list -> Pos.pos' list
111 val get_allpos' : Pos.pos * Pos.posel -> ctree -> Pos.pos' list
112 val get_allpos's : Pos.pos * Pos.posel -> ctree list -> (Pos.pos * Pos.pos_) list
113 val cut_bottom : Pos.pos * Pos.posel -> ctree -> (ctree * Pos.pos' list) * bool
114 val cut_level : Pos.pos' list -> Pos.pos -> ctree -> int list * Pos.pos_ -> ctree * Pos.pos' list
115 val cut_level_'_ : Pos.pos' list -> Pos.pos -> ctree -> int list * Pos.pos_ -> ctree * Pos.pos' list
116 val get_trace : ctree -> int list -> int list -> int list list
117 val branch2str : branch -> string
118 ( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
120 (*----- unused code, kept as hints to design ideas ---------------------------------------------*)
125 structure CTbasic(**): BASIC_CALC_TREE(**) =
131 NoBranch | AndB | OrB
132 | TransitiveB (* FIXXXME.0308: set branch from met in Apply_Method
133 FIXXXME.0402: -"- in Begin_Trans'*)
134 | SequenceB | IntersectB | CollectB | MapB;
136 fun branch2str NoBranch = "NoBranch" (* for tests only *)
137 | branch2str AndB = "AndB"
138 | branch2str OrB = "OrB"
139 | branch2str TransitiveB = "TransitiveB"
140 | branch2str SequenceB = "SequenceB"
141 | branch2str IntersectB = "IntersectB"
142 | branch2str CollectB = "CollectB"
143 | branch2str MapB = "MapB";
146 Incomplete | Complete | Inconsistent (* WN041020 latter still unused *);
147 fun ostate2str Incomplete = "Incomplete" (* for tests only *)
148 | ostate2str Complete = "Complete"
149 | ostate2str Inconsistent = "Inconsistent";
152 type cid = cellID list;
155 type iist = Istate_Def.T option * Istate_Def.T option;
156 (*val e_iist = (empty, empty); --- sinnlos f"ur NICHT-equality-type*)
159 fun new_val v (Istate_Def.Pstate pst) =
160 (Istate_Def.Pstate (Istate_Def.set_act v pst))
161 | new_val _ _ = error "new_val: only for Pstate";
163 datatype con = land | lor;
165 (* executed tactics (tac_s) with local environment etc.;
166 used for continuing eval script + for generate *)
168 (TermC.path *(* of tactic in scr, tactic (weakly) associated with tac_ *)
169 (Tactic.T * (* (for generate) *)
170 Env.T * (* with 'tactic=result' as rule, tactic ev. _not_ ready for 'parallel let' *)
171 Env.T * (* with results of (ready) tacs *)
172 term * (* itr_arg of tactic, for upd. env at Repeat, Try *)
173 term * (* result value of the tac *)
177 fun ets2s (l,(m,eno,env,iar,res,s)) =
178 "\n(" ^ TermC.string_of_path l ^ ",(" ^ Tactic.string_of m ^
179 ",\n ens= " ^ Env.subst2str eno ^
180 ",\n env= " ^ Env.subst2str env ^
181 ",\n iar= " ^ Rule.term2str iar ^
182 ",\n res= " ^ Rule.term2str res ^
183 ",\n " ^ Telem.safe2str s ^ "))";
184 fun ets2str (ets: ets) = (strs2str o (map ets2s)) ets; (* for tests only *)
186 type envp =(*9.5.03: unused, delete with field in ctree.PblObj FIXXXME*)
187 (int * term list) list * (* assoc-list: args of met*)
188 (int * Rule_Set.T) list * (* assoc-list: tacs already done ///15.9.00*)
189 (int * ets) list * (* assoc-list: tacs etc. already done*)
190 (string * pos) list; (* asms * from where*)
192 datatype ppobj = (* TODO: arrange according to signature *)
193 PrfObj of (* a proof step triggered by a tactic *)
194 {form : term, (* where tactic is applied to *)
195 tac : Tactic.input, (* tactic as presented to users *)
196 loc : (Istate_Def.T * (* program interpreter state *)
197 Proof.context) (* context for provers, type inference *)
198 option * (* both for interpreter location on Frm, Pbl, Met *)
199 (Istate_Def.T * (* script interpreter state *)
200 Proof.context) (* context for provers, type inference *)
201 option, (* both for interpreter location on Res *)
202 (*(NONE,NONE) <==> empty ! see update_loc, get_loc *)
203 branch: branch, (* only rudimentary *)
204 result: Selem.result, (* result and assumptions *)
205 ostate: ostate} (* Complete <=> result is OK *)
206 | PblObj of (* a step serving a whole specification-phase *)
207 {fmz : Selem.fmz, (* from init:FIXME never use this spec;-drop *)
208 origin: (Model.ori list) *(* from fmz+pbt+met for efficiently adding items to probl, meth *)
209 Celem.spec * (* updated by Refine_Tacitly *)
210 term, (* headline of calc-head, as calculated initially(!) *)
211 spec : Celem.spec, (* explicitly input *)
212 probl : Model.itm list, (* itms explicitly input *)
213 meth : Model.itm list, (* itms automatically added to copy of probl *)
214 ctxt : Proof.context, (* used while specifying this SubProblem *)
215 loc : (Istate_Def.T * Proof.context) option (* like in PrfObj, calling this SubProblem *)
216 * (Istate_Def.T * Proof.context) option, (* like in PrfObj, finishing the SubProblem *)
217 branch: branch, (* like PrfObj *)
218 result: Selem.result, (* like PrfObj *)
219 ostate: ostate}; (* like PrfObj *)
221 (* this tree contains isac's calculations;
222 the tree's structure has been copied from an early version of Theorema(c);
223 it has the disadvantage, that there is no space
224 for the first tactic in a script generating the first formula at (p,Frm);
225 this trouble has been covered by 'implicit_take' and 'Take' so far,
226 but it is crucial if the first tactic in a script is eg. 'Subproblem';
227 see 'type tac', Apply_Method.
231 | Nd of ppobj * (ctree list);
232 val e_ctree = EmptyPtree;
233 type state = ctree * pos'
234 val e_state = (EmptyPtree , e_pos')
236 fun is_pblobj (PblObj _) = true
237 | is_pblobj _ = false;
239 exception PTREE of string;
240 fun nth _ [] = raise PTREE "nth _ []"
242 | nth n (_ :: xs) = nth (n - 1) xs;
243 (*> nth 2 [11,22,33]; -->> val it = 22 : int*)
246 (** convert ctree to a string **)
248 (* convert a pos from list to string *)
249 fun pr_pos ps = (space_implode "." (map string_of_int ps))^". ";
250 (* show hd origin or form only *)
251 fun pr_short p (PblObj _) = pr_pos p ^ " ----- pblobj -----\n" (* for tests only *)
252 | pr_short p (PrfObj {form = form, ...}) = pr_pos p ^ Rule.term2str form ^ "\n";
253 fun pr_ctree f pt = (* for tests only *)
255 fun pr_pt _ _ EmptyPtree = ""
256 | pr_pt pfn ps (Nd (b, [])) = pfn ps b
257 | pr_pt pfn ps (Nd (b, ts)) = pfn ps b ^ prts pfn ps 1 ts
258 and prts _ _ _ [] = ""
259 | prts pfn ps p (t :: ts) = (pr_pt pfn (ps @ [p]) t)^
260 (prts pfn ps (p + 1) ts)
261 in pr_pt f [] pt end;
263 (** access the branches of ctree **)
265 fun repl [] _ _ = raise PTREE "repl [] _ _"
266 | repl (_ :: ls) 1 e = e :: ls
267 | repl (l :: ls) n e = l :: (repl ls (n-1) e);
268 fun repl_app ls n e =
270 val lim = 1 + length ls
273 then raise PTREE "repl_app: n > lim"
276 else repl ls n e end;
278 (* get from obj at pos by f : ppobj -> 'a *)
279 fun get_obj _ EmptyPtree _ = raise PTREE "get_obj f EmptyPtree"
280 | get_obj f (Nd (b, _)) [] = f b
281 | get_obj f (Nd (_, bs)) (p :: ps) =
284 handle _ => raise PTREE ("get_obj: pos = " ^ ints2str' (p::ps) ^ " does not exist");
286 (get_obj f (nth p bs) ps)
287 handle _ => raise PTREE ("get_obj: pos = " ^ ints2str' (p::ps) ^ " does not exist")
289 fun get_nd EmptyPtree _ = raise PTREE "get_nd EmptyPtree"
291 | get_nd (Nd (_, nds)) (pos as p :: ps) = (get_nd (nth p nds) ps)
292 handle _ => raise PTREE ("get_nd: not existent pos = " ^ ints2str' pos);
294 (* for use by get_obj *)
295 fun g_form (PrfObj {form = f,...}) = f
296 | g_form (PblObj {origin= (_,_,f),...}) = f;
297 fun g_form' (Nd (PrfObj {form = f, ...}, _)) = f
298 | g_form' (Nd (PblObj {origin= (_, _, f),...}, _)) = f
299 | g_form' _ = error "g_form': uncovered fun def.";
300 (* | g_form _ = raise PTREE "g_form not for PblObj";*)
301 fun g_origin (PblObj {origin = ori, ...}) = ori
302 | g_origin _ = raise PTREE "g_origin not for PrfObj";
303 fun g_fmz (PblObj {fmz = f, ...}) = f (* for tests only *)
304 | g_fmz _ = raise PTREE "g_fmz not for PrfObj";
305 fun g_spec (PblObj {spec = s, ...}) = s
306 | g_spec _ = raise PTREE "g_spec not for PrfObj";
307 fun g_pbl (PblObj {probl = p, ...}) = p
308 | g_pbl _ = raise PTREE "g_pbl not for PrfObj";
309 fun g_met (PblObj {meth = p, ...}) = p
310 | g_met _ = raise PTREE "g_met not for PrfObj";
311 fun g_domID (PblObj {spec = (d, _, _), ...}) = d
312 | g_domID _ = raise PTREE "g_metID not for PrfObj";
313 fun g_metID (PblObj {spec = (_, _, m), ...}) = m
314 | g_metID _ = raise PTREE "g_metID not for PrfObj";
315 fun g_ctxt (PblObj {ctxt, ...}) = ctxt
316 | g_ctxt _ = raise PTREE "g_ctxt not for PrfObj";
317 fun g_loc (PblObj {loc = l, ...}) = l
318 | g_loc (PrfObj {loc = l, ...}) = l;
319 fun g_branch (PblObj {branch = b, ...}) = b
320 | g_branch (PrfObj {branch = b, ...}) = b;
321 fun g_tac (PblObj {spec = (_, _, m),...}) = Tactic.Apply_Method m
322 | g_tac (PrfObj {tac = m, ...}) = m;
323 fun g_result (PblObj {result = r, ...}) = r
324 | g_result (PrfObj {result = r, ...}) = r;
325 fun g_res (PblObj {result = (r, _) ,...}) = r
326 | g_res (PrfObj {result = (r, _),...}) = r;
327 fun g_res' (Nd (PblObj {result = (r, _), ...}, _)) = r
328 | g_res' (Nd (PrfObj {result = (r, _),...}, _)) = r
329 | g_res' _ = raise PTREE "g_res': uncovered fun def.";
330 fun g_ostate (PblObj {ostate = r, ...}) = r
331 | g_ostate (PrfObj {ostate = r, ...}) = r;
332 fun g_ostate' (Nd (PblObj {ostate = r, ...}, _)) = r
333 | g_ostate' (Nd (PrfObj {ostate = r, ...}, _)) = r
334 | g_ostate' _ = raise PTREE "g_ostate': uncovered fun def.";
336 (* get the formula preceeding the current position in a calculation *)
337 fun get_curr_formula (pt, (p, p_)) =
339 Frm => get_obj g_form pt p
340 | Res => (fst o (get_obj g_result pt)) p
341 | _ => #3 (get_obj g_origin pt p);
343 (* in CalcTree/Subproblem an 'just_created_' model is created;
344 this is filled to 'untouched' by Model/Refine_Problem *)
345 fun just_created_ (PblObj {meth, probl, spec, ...}) =
346 null meth andalso null probl andalso spec = Celem.e_spec
347 | just_created_ _ = raise PTREE "g_ostate': uncovered fun def.";
348 val e_origin = ([], Celem.e_spec, Rule.e_term);
350 fun just_created (pt, (p, _)) =
351 let val ppobj = get_obj I pt p
352 in is_pblobj ppobj andalso just_created_ ppobj end;
354 (* does the pos in the ctree exist ? *)
355 fun existpt pos pt = can (get_obj I pt) pos;
356 (* does the pos' in the ctree exist, ie. extra check for result in the node *)
357 fun existpt' (p, p_) pt =
358 if can (get_obj I pt) p
360 Res => get_obj g_ostate pt p = Complete
364 (* is this position appropriate for calculating intermediate steps? *)
365 fun is_interpos (_, Res) = true
366 | is_interpos _ = false;
368 (* get the children of a node in ctree *)
369 fun children (Nd (PblObj _, cn)) = cn
370 | children (Nd (PrfObj _, cn)) = cn
371 | children _ = error "children: uncovered fun def.";
373 (*/--------------- duplicates in ctree-navi.sml: required also here below ---------------\*)
374 fun lev_up [] = raise PTREE "lev_up []"
375 | lev_up p = (drop_last p):pos;
376 (* find the position of the next parent which is a PblObj in ctree *)
377 fun par_pblobj _ [] = []
382 if is_pblobj (get_obj I pt p)
384 else par pt (lev_up p)
385 in par pt (lev_up p) end;
386 (*\--------------- duplicates in ctree-navi.sml: required also here below ---------------/*)
388 (* find the next parent, which is either a PblObj or a PrfObj *)
389 fun parent_node _ [] = (true, [], Rule_Set.Empty)
392 fun par _ [] = (true, [], Rule_Set.Empty)
394 if is_pblobj (get_obj I pt p)
395 then (true, p, Rule_Set.Empty)
396 else case get_obj g_tac pt p of
397 Tactic.Rewrite_Set rls' => (false, p, assoc_rls rls')
398 | Tactic.Rewrite_Set_Inst (_, rls') => (false, p, assoc_rls rls')
399 | _ => par pt (lev_up p)
400 in par pt (lev_up p) end;
402 (* insert obj b into ctree at pos, ev.overwriting this pos *)
403 fun insert_pt b EmptyPtree [] = Nd (b, [])
404 | insert_pt _ EmptyPtree _ = raise PTREE "insert_pt b Empty _"
405 | insert_pt b _ [] = Nd (b, [])
406 | insert_pt b (Nd (b', bs)) (p :: []) = Nd (b', repl_app bs p (Nd (b, [])))
407 | insert_pt b (Nd (b', bs)) (p :: ps) = Nd (b', repl_app bs p (insert_pt b (nth p bs) ps));
409 (* insert children to a node without children. compare: fun insert_pt *)
410 fun ins_chn _ EmptyPtree _ = raise PTREE "ins_chn: EmptyPtree"
411 | ins_chn _ (Nd _) [] = raise PTREE "ins_chn: pos = []"
412 | ins_chn ns (Nd (b, bs)) (p :: []) =
414 then raise PTREE "ins_chn: pos not existent"
417 val (b', bs') = case nth p bs of
418 Nd (b', bs') => (b', bs')
419 | _ => error "ins_chn: uncovered case nth"
422 then Nd (b, repl_app bs p (Nd (b', ns)))
423 else raise PTREE "ins_chn: pos mustNOT be overwritten"
425 | ins_chn ns (Nd (b, bs)) (p::ps) = Nd (b, repl_app bs p (ins_chn ns (nth p bs) ps));
427 (* apply f to obj at pos, f: ppobj -> ppobj *)
428 fun appl_to_node f (Nd (b, bs)) = Nd (f b, bs)
429 | appl_to_node _ _ = error "appl_to_node: uncovered fun def.";
430 fun appl_obj _ EmptyPtree [] = EmptyPtree
431 | appl_obj _ EmptyPtree _ = raise PTREE "appl_obj f Empty _"
432 | appl_obj f (Nd (b, bs)) [] = Nd (f b, bs)
433 | appl_obj f (Nd (b, bs)) (p :: []) = Nd (b, repl_app bs p (((appl_to_node f) o (nth p)) bs))
434 | appl_obj f (Nd (b, bs)) (p :: ps) = Nd (b, repl_app bs p (appl_obj f (nth p bs) (ps:pos)));
438 bool * (* ALL itms+preconds true *)
439 pos_ * (* model belongs to Problem | Method *)
440 term * (* header: Problem... or Cas FIXME.0312: item for marking syntaxerrors *)
441 Model.itm list * (* model: given, find, relate *)
442 ((bool * term) list) *(* model: preconds *)
443 Celem.spec; (* specification *)
444 val e_ocalhd = (false, Und, Rule.e_term, [Model.e_itm], [(false, Rule.e_term)], Celem.e_spec);
446 datatype ptform = Form of term | ModSpec of ocalhd;
448 (* for cut_level; (deprecated) *)
449 fun test_trans (PrfObj {branch, ...}) = true andalso branch = TransitiveB
450 | test_trans (PblObj {branch, ...}) = true andalso branch = TransitiveB;
452 fun is_pblobj' pt p =
453 let val ppobj = get_obj I pt p
454 in is_pblobj ppobj end;
456 fun del_res (PblObj {fmz, origin, spec, probl, meth, ctxt, loc = (l1, _), branch, ...}) =
457 PblObj {fmz = fmz, origin = origin, spec = spec, probl = probl, meth = meth,
458 ctxt = ctxt, loc= (l1, NONE), branch = branch,
459 result = (Rule.e_term, []), ostate = Incomplete}
460 | del_res (PrfObj {form, tac, loc= (l1, _), branch, ...}) =
461 PrfObj {form = form, tac = tac, loc = (l1, NONE), branch = branch,
462 result = (Rule.e_term, []), ostate = Incomplete};
465 fun get_loc EmptyPtree _ = (Istate_Def.empty, ContextC.empty)
466 | get_loc pt (p, Res) =
467 (case get_obj g_loc pt p of
469 | (NONE , NONE) => (Istate_Def.empty, ContextC.empty)
471 | get_loc pt (p, _) =
472 (case get_obj g_loc pt p of
473 (NONE , SOME i) => i (*13.8.02 just copied from ^^^: too liberal ?*)
474 | (NONE , NONE) => (Istate_Def.empty, ContextC.empty)
476 fun get_istate_LI pt p = get_loc pt p |> #1;
477 fun get_ctxt_LI pt p = get_loc pt p |> #2;
478 fun get_ctxt pt (pos as (p, p_)) =
479 if member op = [Frm, Res] p_
480 then get_loc pt pos |> #2 (*for script interpretation rely on fun get_loc*)
481 else get_obj g_ctxt pt p (*for specify phase take ctx from PblObj*)
483 fun get_assumptions pt p = get_ctxt pt p |> ContextC.get_assumptions;
485 fun get_somespec' (dI, pI, mI) (dI', pI', mI') =
487 val domID = if dI = ThyC.e_domID then dI' else dI
488 val pblID = if pI = Celem.e_pblID then pI' else pI
489 val metID = if mI = Celem.e_metID then mI' else mI
490 in (domID, pblID, metID) end;
492 (**.development for extracting an 'interval' from ptree.**)
494 (*WN0510 version stopped in favour of get_interval with !!!move_dn, getFormulaeFromTo
495 actually used (inefficient) version with move_dn: see modspec.sml*)
498 fun hdp [] = 1 | hdp [0] = 1 | hdp x = hd x;(*start with first*)
499 fun hdq [] = 99999 | hdq [0] = 99999 | hdq x = hd x;(*take until last*)
500 fun tlp [] = [0] | tlp [_] = [0] | tlp x = tl x;
501 fun tlq [] = [99999] | tlq [_] = [99999] | tlq x = tl x;
503 fun getnd i (b,p) q (Nd (po, nds)) =
504 (if i <= 0 then [[b]] else []) @
505 (getnds (i-1) true (b@[hdp p], tlp p) (tlq q)
506 (take_fromto (hdp p) (hdq q) nds))
508 and getnds _ _ _ _ [] = [] (*no children*)
509 | getnds i _ (b,p) q [nd] = (getnd i (b,p) q nd) (*l+r-margin*)
511 | getnds i true (b,p) q [n1, n2] = (*l-margin, r-margin*)
512 (getnd i ( b, p ) [99999] n1) @
513 (getnd ~99999 (lev_on b,[0]) q n2)
515 | getnds i _ (b,p) q [n1, n2] = (*intern, r-margin*)
516 (getnd i ( b,[0]) [99999] n1) @
517 (getnd ~99999 (lev_on b,[0]) q n2)
519 | getnds i true (b,p) q (nd::(nds as _::_)) = (*l-margin, intern*)
520 (getnd i ( b, p ) [99999] nd) @
521 (getnds ~99999 false (lev_on b,[0]) q nds)
523 | getnds i _ (b,p) q (nd::(nds as _::_)) = (*intern, ...*)
524 (getnd i ( b,[0]) [99999] nd) @
525 (getnds ~99999 false (lev_on b,[0]) q nds);
527 (*get an 'interval from to' from a ptree as 'intervals f t' of respective nodes
528 where 'from' are pos, i.e. a key as int list, 'f' an int (to,t analoguous)
529 (1) the 'f' are given
530 (1a) by 'from' if 'f' = the respective element of 'from' (left margin)
531 (1b) -inifinity, if 'f' > the respective element of 'from' (internal node)
533 (2a) by 'to' if 't' = the respective element of 'to' (right margin)
534 (2b) inifinity, if 't' < the respective element of 'to (internal node)'
535 the 'f' and 't' are set by hdp,... *)
536 fun get_trace pt p q =
537 (flat o (getnds ((length p) -1) true ([hdp p], tlp p) (tlq q)))
538 (take_fromto (hdp p) (hdq q) (children pt));
541 (*extract a formula or model from ctree for itms2itemppc or model2xml*)
542 fun preconds2str bts =
543 (strs2str o (map (Celem.linefeed o pair2str o
544 (apsnd Rule.term2str) o
545 (apfst bool2str)))) bts;
546 fun ocalhd2str (b, p, hdf, itms, prec, spec) = (* for tests only *)
547 "(" ^ bool2str b ^ ", " ^ pos_2str p ^ ", " ^ Rule.term2str hdf ^
548 ", " ^ Model.itms2str_ (ThyC.thy2ctxt' "Isac_Knowledge") itms ^
549 ", " ^ preconds2str prec ^ ", \n" ^ Celem.spec2str spec ^ " )";
551 fun is_pblnd (Nd (ppobj, _)) = is_pblobj ppobj
552 | is_pblnd _ = error "is_pblnd: uncovered fun def.";
555 (* determine the previous pos' on the same level
556 WN0502 made for interSteps; _only_ works for branch TransitiveB WN120517 compare lev_back *)
557 fun lev_pred' _ ([], Res) = ([], Pbl)
558 | lev_pred' pt (p, Res) =
559 let val (p', last) = split_last p
562 then if (is_pblobj o (get_obj I pt)) p then (p, Pbl) else (p, Frm)
563 else if get_obj g_res pt (p' @ [last - 1]) = get_obj g_form pt p
564 then (p' @ [last - 1], Res) (* TransitiveB *)
565 else if (is_pblobj o (get_obj I pt)) p then (p,Pbl) else (p, Frm)
567 | lev_pred' _ _ = error "";
570 (**.insert into ctree and cut branches accordingly.**)
572 (* get all positions of certain intervals on the ctree.
573 old VERSION without move_dn; kept for occasional redesign
574 get all pos's to be cut in a ctree
575 below a pos or from a ctree list after i-th element (NO level_up) *)
576 fun get_allpos' (_, _) EmptyPtree = []
577 | get_allpos' (p, 1) (Nd (b, bs)) = (* p is pos of Nd *)
578 if g_ostate b = Incomplete
579 then (p, Frm) :: (get_allpos's (p, 1) bs)
580 else (p, Frm) :: (get_allpos's (p, 1) bs) @ [(p, Res)]
581 | get_allpos' (p, i) (Nd (b, bs)) = (* p is pos of Nd *)
582 if length bs > 0 orelse is_pblobj b
583 then if g_ostate b = Incomplete
584 then [(p,Frm)] @ (get_allpos's (p, 1) bs)
585 else [(p,Frm)] @ (get_allpos's (p, 1) bs) @ [(p, Res)]
586 else if g_ostate b = Incomplete then [] else [(p, Res)]
587 and get_allpos's _ [] = []
588 | get_allpos's (p, i) (pt :: pts) = (* p is pos of parent-Nd *)
589 (get_allpos' (p @ [i], i) pt) @ (get_allpos's (p, i + 1) pts);
591 (*WN050106 like cut_level, but deletes exactly 1 node *)
592 fun cut_level_'_ _ _ EmptyPtree _ =raise PTREE "cut_level_'_ Empty _" (* for tests ONLY *)
593 | cut_level_'_ _ _ (Nd ( _, _)) ([], _) = raise PTREE "cut_level_'_ _ []"
594 | cut_level_'_ cuts P (Nd (b, bs)) (p :: [], p_) =
597 (Nd (b, drop_nth [] (p:Pos.posel, bs)),
598 cuts @ (if p_ = Frm then [(P @ [p], Res)] else []) @
599 (get_allpos's (P, p + 1) (drop_nth [] (p, bs))))
600 else (Nd (b, bs), cuts)
601 | cut_level_'_ cuts P (Nd (b, bs)) ((p :: ps), p_) =
603 val (bs', cuts') = cut_level_'_ cuts P (nth p bs) (ps, p_)
604 in (Nd (b, repl_app bs p bs'), cuts @ cuts') end;
606 fun cut_level _ _ EmptyPtree _ = raise PTREE "cut_level EmptyPtree _"
607 | cut_level _ _ (Nd ( _, _)) ([],_) = raise PTREE "cut_level _ []"
608 | cut_level cuts P (Nd (b, bs)) (p :: [], p_) =
611 (Nd (b, take (p:Pos.posel, bs)),
613 (if p_ = Frm andalso (*#*) g_ostate b = Complete then [(P@[p],Res)] else ([]:pos' list)) @
614 (get_allpos's (P, p+1) (takerest (p, bs))))
615 else (Nd (b, bs), cuts)
616 | cut_level cuts P (Nd (b, bs)) ((p :: ps), p_) =
618 val (bs', cuts') = cut_level cuts P (nth p bs) (ps, p_)
619 in (Nd (b, repl_app bs p bs'), cuts @ cuts') end;
621 (*old version before WN050219, overwritten below*)
622 fun cut_tree _ ([], _) = raise PTREE "cut_tree _ ([],_)" (* for test only *)
623 | cut_tree pt (pos as ([_], _)) =
625 val (pt', cuts) = cut_level [] [] pt pos
627 (pt', cuts @ (if get_obj g_ostate pt [] = Incomplete then [] else [([], Res)]))
629 | cut_tree pt (p,p_) =
631 fun cutfn pt cuts (p, p_) =
633 val (pt', cuts') = cut_level [] (lev_up p) pt (p,p_)
635 if length cuts' > 0 andalso length p > 1
636 then cutfn pt' (cuts @ cuts') (lev_up p, Frm(*-->(p,Res)*))
637 else (pt', cuts @ cuts')
639 val (pt', cuts) = cutfn pt [] (p, p_)
641 (pt', cuts @ (if get_obj g_ostate pt [] = Incomplete then [] else [([], Res)]))
645 fun move_dn _ (Nd (_, ns)) ([],p_) = (* root problem *)
647 Res => raise PTREE "move_dn: end of calculation"
649 if null ns (* go down from Pbl + Met *)
650 then raise PTREE "move_dn: solve problem not started"
652 | move_dn P (Nd (_, ns)) (p :: (ps as (_ :: _)), p_) = (* iterate to end of pos *)
654 then raise PTREE "move_dn: pos not existent 2"
655 else move_dn (P @ [p]) (nth p ns) (ps, p_)
656 | move_dn P (Nd (c, ns)) ([p], p_) = (* act on last element of pos *)
658 then raise PTREE "move_dn: pos not existent 3"
662 if p = length ns (* last Res on this level: go a level up *)
663 then if g_ostate c = Complete
665 else raise PTREE (ints2str' P ^ " not complete 1")
666 else (* go to the next Nd on this level, or down into the next Nd *)
667 if is_pblnd (nth (p + 1) ns) then (P@[p + 1], Pbl)
668 else if g_res' (nth p ns) = g_form' (nth (p + 1) ns)
669 then if (null o children o (nth (p + 1))) ns
670 then (* take the Res if Complete *)
671 if g_ostate' (nth (p + 1) ns) = Complete
672 then (P@[p + 1], Res)
673 else raise PTREE (ints2str' (P@[p + 1]) ^ " not complete 2")
674 else (P@[p + 1, 1], Frm) (* go down into the next PrfObj *)
675 else (P@[p + 1], Frm) (* take Frm: exists if the Nd exists *)
676 | Frm => (*go down or to the Res of this Nd*)
677 if (null o children o (nth p)) ns
678 then if g_ostate' (nth p ns) = Complete then (P @ [p], Res)
679 else raise PTREE (ints2str' (P @ [p])^" not complete 3")
680 else (P @ [p, 1], Frm)
681 | _ => (* is Pbl or Met *)
682 if (null o children o (nth p)) ns
683 then raise PTREE "move_dn:solve subproblem not startd"
685 if (is_pblnd o hd o children o (nth p)) ns
687 | move_dn _ _ _ = error "";
689 (* get all positions in a ctree until ([],Res) or ostate=Incomplete
691 pos' list -> : accumulated, start with []
692 pos -> : the offset for subtrees wrt the root
694 pos' : initialization (the last pos' before ...)
695 -> pos' list : of positions in this (sub) tree (relative to the root)
697 fun get_allp cuts (P, pos) pt =
699 val nxt = move_dn [] pt pos (*exn if Incomplete reached*)
702 then get_allp (cuts @ [nxt]) (P, nxt) pt
703 else map (apfst (curry op @ P)) (cuts @ [nxt])
705 handle PTREE _ => (map (apfst (curry op@ P)) cuts);
708 (* the pts are assumed to be on the same level *)
709 fun get_allps cuts _ [] = cuts
710 | get_allps cuts P (pt :: pts) =
712 val below = get_allp [] (P, ([], Frm)) pt
715 then (P, Pbl) :: below
716 else if last_elem P = 1
717 then (P, Frm) :: below
719 val levres = levfrm @ (if null below then [(P, Res)] else [])
721 get_allps (cuts @ levres) (lev_on P) pts
724 (** these 2 funs decide on how far cut_tree goes **)
725 (* shall the nodes _after_ the pos to be inserted at be deleted?
726 shall cutting be continued on the higher level(s)? the Nd regarded will NOT be changed *)
727 fun test_trans (PrfObj {branch, ...}) = (branch = TransitiveB orelse branch = NoBranch)
728 | test_trans (PblObj {branch, ...}) = (branch = TransitiveB orelse branch = NoBranch);
730 (* cut_bottom new sml603..608
731 cut the level at the bottom of the pos (used by cappend_...)
732 and handle the parent in order to avoid extra case for root
733 fn: ctree -> : the _whole_ ctree for cut_levup
734 pos * Pos.posel -> : the pos after split_last
735 ctree -> : the parent of the Nd to be cut
737 (ctree * : the updated ctree
738 pos' list) * : the pos's cut
739 bool : cutting shall be continued on the higher level(s)
741 fun cut_bottom _ (pt' as Nd (b, [])) = ((pt', []), test_trans b)
742 | cut_bottom (P, p) (Nd (b, bs)) =
743 let (*divide level into 3 parts...*)
744 val keep = take (p - 1, bs)
745 val pt' = case nth p bs of
747 | _ => error "cut_bottom: uncovered case nth p bs"
748 (*^^^^^_here_ will be 'insert_pt'ed by 'append_..'*)
749 val (tail, _) = (takerest (p, bs), if null (takerest (p, bs)) then 0 else p + 1)
750 val (children, cuts) =
753 (keep, (if is_pblnd pt' then [(P @ [p], Pbl)] else [])
754 @ (get_allp [] (P @ [p], (P, Frm)) pt')
755 @ (get_allps [] (P @ [p + 1]) tail))
756 else (keep @ [(*'insert_pt'ed by 'append_..'*)] @ tail,
757 get_allp [] (P @ [p], (P, Frm)) pt')
760 then (Nd (del_res b, children), cuts @ (if g_ostate b = Incomplete then [] else [(P, Res)]))
761 else (Nd (b, children), cuts)
762 in ((pt'', cuts), test_trans b) end
763 | cut_bottom _ _ = error "cut_bottom: uncovered fun def.";
766 (* go all levels from the bottom of 'pos' up to the root,
767 on each level compose the children of a node and accumulate the cut Nds
769 pos' list -> : for accumulation
770 bool -> : cutting shall be continued on the higher level(s)
771 ctree -> : the whole ctree for 'get_nd pt P' on each level
772 ctree -> : the Nd from the lower level for insertion at path
773 pos * Pos.posel -> : pos=path split for convenience
774 ctree -> : Nd the children of are under consideration on this call
776 ctree * pos' list : the updated parent-Nd and the pos's of the Nds cut
778 fun cut_levup (cuts:pos' list) clevup pt pt' (P:pos, p:Pos.posel) (Nd (b, bs)) =
779 let (*divide level into 3 parts...*)
780 val keep = take (p - 1, bs)
781 (*val pt' comes as argument from below*)
783 (takerest (p, bs), if null (takerest (p, bs)) then 0 else p + 1)
784 val (children, cuts') =
786 then (keep @ [pt'], get_allps [] (P @ [p+1]) tail)
787 else (keep @ [pt'] @ tail, [])
788 val clevup' = if clevup then test_trans b else false
789 (*the first Nd with false stops cutting on all levels above*)
792 then (Nd (del_res b, children), cuts' @ (if g_ostate b = Incomplete then [] else [(P, Res)]))
793 else (Nd (b, children), cuts')
796 then (pt'', cuts @ cuts')
798 let val (P, p) = split_last P
799 in cut_levup (cuts @ cuts') clevup' pt pt'' (P, p) (get_nd pt P) end
801 | cut_levup _ _ _ _ _ _ = error "cut_levup: uncovered fun def.";
803 (* cut nodes after and below an inserted node in the ctree;
804 the cuts range is limited by the predicate 'fun cutlevup' *)
805 fun cut_tree pt (pos, _) =
806 if not (existpt pos pt)
807 then (pt, []) (*appending a formula never cuts anything*)
810 val (P, p) = split_last pos
811 val ((pt', cuts), clevup) = cut_bottom (P, p) (get_nd pt P)
812 (* pt' is the updated parent of the Nd to cappend_..*)
817 let val (P, p) = split_last P
818 in cut_levup cuts clevup pt pt' (P, p) (get_nd pt P) end
821 (* get the theory explicitly specified for the rootpbl;
822 thus use this function _after_ finishing specification *)
823 fun rootthy (Nd (PblObj {spec = (thyID, _, _), ...}, _)) = Celem.assoc_thy thyID
824 | rootthy _ = error "rootthy: uncovered fun def.";