1 (* Title: interpreter for scripts
2 Author: Walther Neuper 2000
3 (c) due to copyright terms
4 12345678901234567890123456789012345678901234567890123456789012345678901234567890
5 10 20 30 40 50 60 70 80
8 signature LUCAS_INTERPRETER =
11 type step = tac_ * mout * ptree * pos' * pos' list
12 datatype locate = NotLocatable | Steps of istate * step list
14 val next_tac : (*diss: next-tactic-function*)
15 theory' * rls -> ptree * pos' -> scr -> istate * 'a -> tac_ * (istate * 'a) * (term * safe)
16 val locate_gen : (*diss: locate-function*)
17 theory' * rls -> tac_ -> ptree * pos' -> scr * 'a -> istate * Proof.context -> locate
19 (* can these functions be local to Lucin or part of LItools ? *)
20 val sel_rules : ptree -> pos' -> tac list
21 val init_form : 'a -> scr -> (term * term) list -> term option
22 val tac_2tac : tac_ -> tac
23 val init_scrstate : theory -> itm list -> metID -> istate * Proof.context * scr
24 val from_pblobj' : theory' -> pos * pos_ -> ptree -> rls * (istate * Proof.context) * scr
25 val from_pblobj_or_detail' : theory' -> pos * pos_ -> ptree ->
26 rls * (istate * Proof.context) * scr
27 val rule2thm'' : rule -> thm''
28 val rule2rls' : rule -> string
30 (*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\*)
31 datatype asap = Aundef | AssOnly | AssGen
32 datatype appy_ = Napp_ | Skip_
33 val itms2args : 'a -> metID -> itm list -> term list
34 val get_stac : 'a -> term -> term option
35 val handle_leaf : string -> theory' -> rls -> env -> term option -> term -> term ->
36 term option * stacexpr
37 val formal_args : term -> term list
38 val go : loc_ -> term -> term
39 val id_of_scr : term -> string
41 val appy : theory' * rls -> ptree * pos' -> env -> lrd list -> term -> term option -> term -> appy
42 val sel_appl_atomic_tacs : ptree -> pos' -> tac list
43 val nstep_up : theory' * rls -> ptree * pos' -> scr -> env -> lrd list -> appy_ ->
44 term option -> term -> appy
45 val upd_env_opt : env -> term option * term -> env
46 (*\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
49 (* traces the leaves (ie. non-tactical nodes) of Prog found by next_tac, see "and scr" *)
50 val trace_script = Unsynchronized.ref false; (* TODO: how are traces done in Isabelle? *)
53 structure Lucin(*: LUCAS_INTERPRETER*) =
56 (* data for creating a new node in ctree; designed for use as:
57 fun ass* scrstate steps = / ... case ass* scrstate steps of /
58 Assoc (scrstate, steps) => ... ass* scrstate steps *)
60 tac_ (*transformed from associated tac *)
61 * mout (*result with indentation etc. *)
62 * ptree (*containing node created by tac_ + resp. scrstate *)
63 * pos' (*position in ptree; ptree * pos' is the proofstate *)
64 * pos' list; (*of ptree-nodes probably cut (by fst tac_) *)
66 fun rule2thm'' (Thm (id, thm)) = (id, thm) : thm''
67 | rule2thm'' r = error ("rule2thm': not defined for " ^ rule2str r);
68 fun rule2rls' (Rls_ rls) = id_rls rls
69 | rule2rls' r = error ("rule2rls': not defined for " ^ rule2str r);
71 (*.makes a (rule,term) list to a Step (m, mout, pt', p', cid) for solve;
72 complicated with current t in rrlsstate.*)
73 fun rts2steps steps ((pt, p), (f, f'', rss, rts), (thy', ro, er, pa)) [(r, (f', am))] =
75 val thy = assoc_thy thy'
76 val ctxt = get_ctxt pt p |> insert_assumptions am
77 val m = Rewrite' (thy', ro, er, pa, rule2thm'' r, f, (f', am))
78 val is = RrlsState (f', f'', rss, rts)
79 val p = case p of (_, Frm) => p | (p', Res) => (lev_on p', Res) | _ => error "rts2steps: p1"
80 val (p', cid, mout, pt') = generate1 thy m (is, ctxt) p pt
81 in (is, (m, mout, pt', p', cid) :: steps) end
82 | rts2steps steps ((pt, p) ,(f, f'', rss, rts), (thy', ro, er, pa)) ((r, (f', am)) :: rts') =
84 val thy = assoc_thy thy'
85 val ctxt = get_ctxt pt p |> insert_assumptions am
86 val m = Rewrite' (thy', ro, er, pa, rule2thm'' r, f, (f', am))
87 val is = RrlsState (f', f'', rss, rts)
88 val p = case p of (_, Frm) => p | (p', Res) => (lev_on p', Res) | _ => error "rts2steps: p1"
89 val (p', cid, mout, pt') = generate1 thy m (is, ctxt) p pt
90 in rts2steps ((m, mout, pt', p', cid)::steps)
91 ((pt', p'), (f', f'', rss, rts), (thy', ro, er, pa)) rts'
93 | rts2steps _ _ _ = error "rts2steps: uncovered fun-def"
95 (* functions for the environment stack: NOT YET IMPLEMENTED
96 fun accessenv id es = the (assoc ((top es) : env, id))
97 handle _ => error ("accessenv: " ^ free2str id ^ " not in env");
98 fun updateenv id vl (es : env stack) =
99 (push (overwrite(top es, (id, vl))) (pop es)) : env stack;
100 fun pushenv id vl (es : env stack) =
101 (push (overwrite(top es, (id, vl))) es) : env stack;
102 val popenv = pop : env stack -> env stack;
105 fun de_esc_underscore str =
108 | scan (s :: ss) = if s = "'" then (scan ss) else (s :: (scan ss))
109 in (implode o scan o Symbol.explode) str end;
111 (*go at a location in a script and fetch the contents*)
113 | go (D::p) (Abs(_, _, t0)) = go (p : loc_) t0
114 | go (L::p) (t1 $ _) = go p t1
115 | go (R::p) (_ $ t2) = go p t2
116 | go l _ = error ("go: no " ^ loc_2str l);
118 (*.get argument of first stactic in a script for init_form.*)
119 fun get_stac thy (_ $ body) =
121 fun get_t y (Const ("Script.Seq",_) $ e1 $ e2) a =
122 (case get_t y e1 a of NONE => get_t y e2 a | la => la)
123 | get_t y (Const ("Script.Seq",_) $ e1 $ e2 $ a) _ =
124 (case get_t y e1 a of NONE => get_t y e2 a | la => la)
125 | get_t y (Const ("Script.Try",_) $ e) a = get_t y e a
126 | get_t y (Const ("Script.Try",_) $ e $ a) _ = get_t y e a
127 | get_t y (Const ("Script.Repeat",_) $ e) a = get_t y e a
128 | get_t y (Const ("Script.Repeat",_) $ e $ a) _ = get_t y e a
129 | get_t y (Const ("Script.Or",_) $e1 $ e2) a =
130 (case get_t y e1 a of NONE => get_t y e2 a | la => la)
131 | get_t y (Const ("Script.Or",_) $e1 $ e2 $ a) _ =
132 (case get_t y e1 a of NONE => get_t y e2 a | la => la)
133 | get_t y (Const ("Script.While",_) $ _ $ e) a = get_t y e a
134 | get_t y (Const ("Script.While",_) $ _ $ e $ a) _ = get_t y e a
135 | get_t y (Const ("Script.Letpar",_) $ e1 $ Abs (_, _, e2)) a =
136 (case get_t y e1 a of NONE => get_t y e2 a | la => la)
137 (*| get_t y (Const ("HOL.Let",_) $ e1 $ Abs (_,_,e2)) a =
138 (case get_t y e1 a of NONE => get_t y e2 a | la => la)
139 | get_t y (Abs (_,_,e)) a = get_t y e a*)
140 | get_t y (Const ("HOL.Let",_) $ e1 $ Abs (_, _, _)) a =
141 get_t y e1 a (*don't go deeper without evaluation !*)
142 | get_t _ (Const ("If", _) $ _ $ _ $ _) _ = NONE
143 (*(case get_t y e1 a of NONE => get_t y e2 a | la => la)*)
145 | get_t _ (Const ("Script.Rewrite",_) $ _ $ _ $ a) _ = SOME a
146 | get_t _ (Const ("Script.Rewrite",_) $ _ $ _ ) a = SOME a
147 | get_t _ (Const ("Script.Rewrite'_Inst",_) $ _ $ _ $ _ $ a) _ = SOME a
148 | get_t _ (Const ("Script.Rewrite'_Inst",_) $ _ $ _ $ _ ) a = SOME a
149 | get_t _ (Const ("Script.Rewrite'_Set",_) $ _ $ _ $ a) _ = SOME a
150 | get_t _ (Const ("Script.Rewrite'_Set",_) $ _ $ _ ) a = SOME a
151 | get_t _ (Const ("Script.Rewrite'_Set'_Inst",_) $ _ $ _ $ _ $a)_ =SOME a
152 | get_t _ (Const ("Script.Rewrite'_Set'_Inst",_) $ _ $ _ $ _ ) a =SOME a
153 | get_t _ (Const ("Script.Calculate",_) $ _ $ a) _ = SOME a
154 | get_t _ (Const ("Script.Calculate",_) $ _ ) a = SOME a
156 | get_t _ (Const ("Script.Substitute",_) $ _ $ a) _ = SOME a
157 | get_t _ (Const ("Script.Substitute",_) $ _ ) a = SOME a
159 | get_t _ (Const ("Script.SubProblem",_) $ _ $ _) _ = NONE
161 | get_t _ _ _ = ((*tracing ("### get_t yac: list-expr "^(term2str x));*) NONE)
162 in get_t thy body e_term end
163 | get_stac _ t = error ("get_stac: no fun-def. for " ^ term2str t);
165 fun init_form thy (Prog sc) env =
166 (case get_stac thy sc of NONE => NONE | SOME stac => SOME (subst_atomic env stac))
167 | init_form _ _ _ = error "init_form: no match";
169 (* get the arguments of the script out of the scripts parsetree *)
170 fun formal_args scr = (fst o split_last o snd o strip_comb) scr;
172 (* get the identifier of the script out of the scripts parsetree *)
173 fun id_of_scr sc = (id_of o fst o strip_comb) sc;
175 (*WN020526: not clear, when a is available in ass_up for eval_true*)
176 (*WN060906: in "fun handle_leaf" eg. uses "SOME M__"(from some PREVIOUS
177 curried Rewrite) for CURRENT value (which may be different from PREVIOUS);
178 thus "NONE" must be set at the end of currying (ill designed anyway)*)
179 fun upd_env_opt env (SOME a, v) = upd_env env (a, v)
180 | upd_env_opt env (NONE, _) =
181 ((*tracing ("*** upd_env_opt: (NONE," ^ term2str v ^ ")");*) env);
183 type dsc = typ; (* <-> nam..unknow in Descript.thy *)
185 (*.create the actual parameters (args) of script: their order
186 is given by the order in met.pat .*)
187 (*WN.5.5.03: ?: does this allow for different descriptions ???
188 ?: why not taken from formal args of script ???
189 !: FIXXXME penv: push it here in itms2args into script-evaluation*)
190 (* val (thy, mI, itms) = (thy, metID, itms);
192 val errmsg = "ERROR: the guard is missing (#ppc in 'type met' added in prep_met)."
193 fun itms2args _ mI (itms : itm list) =
195 val mvat = max_vt itms
196 fun okv mvat (_, vats, b, _, _) = member op = vats mvat andalso b
197 val itms = filter (okv mvat) itms
198 fun test_dsc d (_, _, _, _, itm_) = (d = d_in itm_)
199 fun itm2arg itms (_,(d,_)) =
200 case find_first (test_dsc d) itms of
201 NONE => error ("itms2args: '" ^ term2str d ^ "' not in itms")
202 | SOME (_, _, _, _, itm_) => penvval_in itm_
203 (*| SOME (_,_,_,_,itm_) => mk_arg thy (d_in itm_) (ts_in itm_);
204 penv postponed; presently penv holds already env for script*)
205 val pats = (#ppc o get_met) mI
206 val _ = if pats = [] then raise ERROR errmsg else ()
207 in (flat o (map (itm2arg itms))) pats end;
209 (* convert a script-tac 'stac' to a tactic 'tac';
210 if stac is an initac, then convert to a 'tac_' (as required in appy).
211 arg ptree for pushing the thy specified in rootpbl into subpbls *)
212 fun stac2tac_ _ thy (Const ("Script.Rewrite", _) $ Free (thmID, _) $ _ $ _) =
214 val tid = (de_esc_underscore o strip_thy) thmID
215 in (Rewrite (tid, assoc_thm'' thy tid), Empty_Tac_) end
216 | stac2tac_ _ thy (Const ("Script.Rewrite'_Inst", _) $ sub $ Free (thmID, _) $ _ $ _) =
218 val subML = ((map isapair2pair) o isalist2list) sub
219 val subStr = subst2subs subML
220 val tid = (de_esc_underscore o strip_thy) thmID (*4.10.02 unnoetig*)
221 in (Rewrite_Inst (subStr, (tid, assoc_thm'' thy tid)), Empty_Tac_) end
222 | stac2tac_ _ _ (Const ("Script.Rewrite'_Set",_) $ Free (rls, _) $ _ $ _) =
223 (Rewrite_Set ((de_esc_underscore o strip_thy) rls), Empty_Tac_)
224 | stac2tac_ _ _ (Const ("Script.Rewrite'_Set'_Inst", _) $ sub $ Free (rls, _) $ _ $ _) =
226 val subML = ((map isapair2pair) o isalist2list) sub;
227 val subStr = subst2subs subML;
228 in (Rewrite_Set_Inst (subStr, rls), Empty_Tac_) end
229 | stac2tac_ _ _ (Const ("Script.Calculate", _) $ Free (op_, _) $ _) = (Calculate op_, Empty_Tac_)
230 | stac2tac_ _ _ (Const ("Script.Take", _) $ t) = (Take (term2str t), Empty_Tac_)
231 | stac2tac_ _ _ (Const ("Script.Substitute", _) $ isasub $ _) =
232 (Substitute ((subte2sube o isalist2list) isasub), Empty_Tac_)
233 | stac2tac_ _ thy (Const("Script.Check'_elementwise", _) $ _ $
234 (Const ("Set.Collect", _) $ Abs (_, _, pred))) =
235 (Check_elementwise (term_to_string''' thy pred), Empty_Tac_)
236 | stac2tac_ _ _ (Const("Script.Or'_to'_List", _) $ _ ) = (Or_to_List, Empty_Tac_)
237 | stac2tac_ _ _ (Const ("Script.Tac", _) $ Free (str, _)) =
238 (Tac ((de_esc_underscore o strip_thy) str), Empty_Tac_)
240 (*compare "| assod _ (Subproblem'"*)
241 | stac2tac_ pt _ (stac as Const ("Script.SubProblem",_) $
242 (Const ("Product_Type.Pair",_) $Free (dI', _) $ (Const ("Product_Type.Pair", _) $ pI' $ mI')) $
245 val dI = ((implode o drop_last(*.."'"*) o Symbol.explode) dI')(*^""*);
246 val thy = maxthy (assoc_thy dI) (rootthy pt);
247 val pI = ((map (de_esc_underscore o free2str)) o isalist2list) pI';
248 val mI = ((map (de_esc_underscore o free2str)) o isalist2list) mI';
249 val ags = isalist2list ags';
254 val pors = (match_ags thy ((#ppc o get_pbt) pI) ags)
255 handle ERROR "actual args do not match formal args"
256 => (match_ags_msg pI stac ags(*raise exn*); [])
257 val pI' = refine_ori' pors pI;
258 in (pI', pors (* refinement over models with diff.prec only *),
259 (hd o #met o get_pbt) pI') end
260 else (pI, (match_ags thy ((#ppc o get_pbt) pI) ags)
261 handle ERROR "actual args do not match formal args"
262 => (match_ags_msg pI stac ags(*raise exn*); []), mI);
263 val (fmz_, vals) = oris2fmz_vals pors;
264 val {cas,ppc,thy,...} = get_pbt pI
265 val dI = theory2theory' thy (*.take dI from _refined_ pbl.*)
266 val dI = theory2theory' (maxthy (assoc_thy dI) (rootthy pt));
267 val ctxt = dI |> Thy_Info.get_theory |> Proof_Context.init_global |> declare_constraints' vals
270 NONE => pblterm dI pI
271 | SOME t => subst_atomic ((vars_of_pbl_' ppc) ~~~ vals) t
272 val f = subpbl (strip_thy dI) pI
273 in (Subproblem (dI, pI), Subproblem' ((dI, pI, mI), pors, hdl, fmz_, ctxt, f))
275 | stac2tac_ _ thy t = error ("stac2tac_ TODO: no match for " ^ term_to_string''' thy t);
277 fun stac2tac pt thy t = (fst o stac2tac_ pt thy) t;
281 tac_ * (* SubProblem gets args instantiated in assod *)
282 term (* for itr_arg, result in ets *)
285 term (*for itr_arg,result in ets*)
288 (* check if tac_ is associated with stac.
289 Additional task: check if term t (the result has been calculated from) in tac_
290 has been changed (see "datatype tac_"); if yes, recalculate result
291 TODO.WN120106 recalculate impl.only for Substitute'
293 pt : ptree for pushing the thy specified in rootpbl into subpbls
294 d : unused (planned for data for comparison)
295 tac_ : from user (via applicable_in); to be compared with ...
296 stac : found in Script
298 Ass : associated: e.g. thmID in stac = thmID in m
299 +++ arg in stac = arg in m
300 AssWeak: weakly ass.:e.g. thmID in stac = thmID in m, //arg//
301 NotAss : e.g. thmID in stac/=/thmID in m (not =)
303 fun assod _ _ (m as Rewrite_Inst' (_, _, _, _, _, thm'' as (thmID, _), f, (f', _))) stac =
305 (Const ("Script.Rewrite'_Inst", _) $ _ $ Free (thmID_, _) $ _ $ f_) =>
309 then ((*tracing"3### assod ..Ass";*) Ass (m,f'))
310 else ((*tracing"3### assod ..AssWeak";*) AssWeak(m, f'))
311 else ((*tracing"3### assod ..NotAss";*) NotAss)
312 | (Const ("Script.Rewrite'_Set'_Inst",_) $ _ $ Free (rls_, _) $ _ $ f_) =>
313 if contains_rule (Thm thm'') (assoc_rls rls_)
314 then if f = f_ then Ass (m,f') else AssWeak (m,f')
317 | assod _ _ (m as Rewrite' (_, _, _, _, thm'' as (thmID, _), f, (f', _))) stac =
319 (Const ("Script.Rewrite", _) $ Free (thmID_, _) $ _ $ f_) =>
320 ((*tracing ("3### assod: stac = " ^ ter2str t);
321 tracing ("3### assod: f(m)= " ^ term2str f);*)
325 then ((*tracing"3### assod ..Ass";*) Ass (m,f'))
327 ((*tracing"### assod ..AssWeak";
328 tracing("### assod: f(m) = " ^ term2str f);
329 tracing("### assod: f(stac)= " ^ term2str f_)*)
331 else ((*tracing"3### assod ..NotAss";*) NotAss))
332 | (Const ("Script.Rewrite'_Set", _) $ Free (rls_, _) $ _ $ f_) =>
333 if contains_rule (Thm thm'') (assoc_rls rls_)
334 then if f = f_ then Ass (m, f') else AssWeak (m, f')
337 | assod _ _ (m as Rewrite_Set_Inst' (_, _, _, rls, f, (f', _)))
338 (Const ("Script.Rewrite'_Set'_Inst", _) $ _ $ Free (rls_, _) $ _ $ f_) =
340 then if f = f_ then Ass (m, f') else AssWeak (m ,f')
342 | assod _ _ (m as Detail_Set_Inst' (_, _, _, rls, f, (f',_)))
343 (Const ("Script.Rewrite'_Set'_Inst", _) $ _ $ Free (rls_, _) $ _ $ f_) =
345 then if f = f_ then Ass (m, f') else AssWeak (m, f')
347 | assod _ _ (m as Rewrite_Set' (_, _, rls, f, (f', _)))
348 (Const ("Script.Rewrite'_Set", _) $ Free (rls_, _) $ _ $ f_) =
350 then if f = f_ then Ass (m, f') else AssWeak (m, f')
352 | assod _ _ (m as Detail_Set' (_, _, rls, f, (f', _)))
353 (Const ("Script.Rewrite'_Set", _) $ Free (rls_, _) $ _ $ f_) =
355 then if f = f_ then Ass (m, f') else AssWeak (m, f')
357 | assod _ _ (m as Calculate' (_, op_, f, (f', _))) stac =
359 (Const ("Script.Calculate",_) $ Free (op__,_) $ f_) =>
361 then if f = f_ then Ass (m, f') else AssWeak (m, f')
363 | (Const ("Script.Rewrite'_Set'_Inst", _) $ _ $ Free(rls_,_) $_$f_) =>
364 let val thy = assoc_thy "Isac";
366 if contains_rule (Calc (assoc_calc' thy op_ |> snd)) (assoc_rls rls_)
367 then if f = f_ then Ass (m, f') else AssWeak (m, f')
370 | (Const ("Script.Rewrite'_Set",_) $ Free (rls_, _) $ _ $ f_) =>
371 let val thy = assoc_thy "Isac";
373 if contains_rule (Calc (assoc_calc' thy op_ |> snd)) (assoc_rls rls_)
374 then if f = f_ then Ass (m,f') else AssWeak (m,f')
378 | assod _ _ (m as Check_elementwise' (consts, _, (consts_chkd, _)))
379 (Const ("Script.Check'_elementwise",_) $ consts' $ _) =
381 then Ass (m, consts_chkd)
383 | assod _ _ (m as Or_to_List' (_, list)) (Const ("Script.Or'_to'_List", _) $ _) = Ass (m, list)
384 | assod _ _ (m as Take' term) (Const ("Script.Take", _) $ _) = Ass (m, term)
385 | assod _ _ (m as Substitute' (ro, erls, subte, f, f')) (Const ("Script.Substitute", _) $ _ $ t) =
386 if f = t then Ass (m, f')
387 else (*compare | applicable_in (p,p_) pt (m as Substitute sube)*)
388 if foldl and_ (true, map contains_Var subte)
390 let val t' = subst_atomic (map HOLogic.dest_eq subte (*TODO subte2subst*)) t
391 in if t = t' then error "assod: Substitute' not applicable to val of Expr"
392 else Ass (Substitute' (ro, erls, subte, t, t'), t')
394 else (case rewrite_terms_ (Isac()) ro erls subte t of
395 SOME (t', _) => Ass (Substitute' (ro, erls, subte, t, t'), t')
396 | NONE => error "assod: Substitute' not applicable to val of Expr")
397 | assod _ _ (m as Tac_ (thy, _, id, f')) (Const ("Script.Tac",_) $ Free (id', _)) =
399 then Ass (m, ((Thm.term_of o the o (parse thy)) f'))
402 (*compare "| stac2tac_ thy (Const ("Script.SubProblem",_)"*)
403 | assod pt _ (Subproblem' ((domID, pblID, _), _, _, _, _, _))
404 (stac as Const ("Script.SubProblem",_) $ (Const ("Product_Type.Pair",_) $
405 Free (dI',_) $ (Const ("Product_Type.Pair",_) $ pI' $ mI')) $ ags') =
407 val dI = ((implode o drop_last(*.."'"*) o Symbol.explode) dI')(*^""*);
408 val thy = maxthy (assoc_thy dI) (rootthy pt);
409 val pI = ((map (de_esc_underscore o free2str)) o isalist2list) pI';
410 val mI = ((map (de_esc_underscore o free2str)) o isalist2list) mI';
411 val ags = isalist2list ags';
416 val pors = (match_ags thy ((#ppc o get_pbt) pI) ags)
417 handle ERROR "actual args do not match formal args"
418 => (match_ags_msg pI stac ags(*raise exn*);[]);
419 val pI' = refine_ori' pors pI;
420 in (pI', pors (*refinement over models with diff.prec only*), (hd o #met o get_pbt) pI')
422 else (pI, (match_ags thy ((#ppc o get_pbt) pI) ags)
423 handle ERROR "actual args do not match formal args"
424 => (match_ags_msg pI stac ags(*raise exn*); []), mI);
425 val (fmz_, vals) = oris2fmz_vals pors;
426 val {cas, ppc, thy, ...} = get_pbt pI
427 val dI = theory2theory' thy (*take dI from _refined_ pbl*)
428 val dI = theory2theory' (maxthy (assoc_thy dI) (rootthy pt))
429 val ctxt = dI |> Thy_Info.get_theory |> Proof_Context.init_global |> declare_constraints' vals
432 NONE => pblterm dI pI
433 | SOME t => subst_atomic ((vars_of_pbl_' ppc) ~~~ vals) t
434 val f = subpbl (strip_thy dI) pI
436 if domID = dI andalso pblID = pI
437 then Ass (Subproblem' ((dI, pI, mI), pors, hdl, fmz_, ctxt, f), f)
442 then tracing("@@@ the 'tac_' proposed to apply does NOT match the leaf found in the script:\n"
443 ^ "@@@ tac_ = " ^ tac_2str m)
447 fun tac_2tac (Refine_Tacitly' (pI, _, _, _, _)) = Refine_Tacitly pI
448 | tac_2tac (Model_Problem' (_, _, _)) = Model_Problem
449 | tac_2tac (Add_Given' (t, _)) = Add_Given t
450 | tac_2tac (Add_Find' (t, _)) = Add_Find t
451 | tac_2tac (Add_Relation' (t, _)) = Add_Relation t
453 | tac_2tac (Specify_Theory' dI) = Specify_Theory dI
454 | tac_2tac (Specify_Problem' (dI, _)) = Specify_Problem dI
455 | tac_2tac (Specify_Method' (dI, _, _)) = Specify_Method dI
457 | tac_2tac (Rewrite' (_, _, _, _, thm, _, _)) = Rewrite thm
458 | tac_2tac (Rewrite_Inst' (_, _, _, _, sub, thm, _, _)) = Rewrite_Inst (subst2subs sub, thm)
460 | tac_2tac (Rewrite_Set' (_, _, rls, _, _)) = Rewrite_Set (id_rls rls)
461 | tac_2tac (Detail_Set' (_, _, rls, _, _)) = Detail_Set (id_rls rls)
463 | tac_2tac (Rewrite_Set_Inst' (_, _, sub, rls, _, _)) =
464 Rewrite_Set_Inst (subst2subs sub,id_rls rls)
465 | tac_2tac (Detail_Set_Inst' (_, _, sub, rls, _, _)) =
466 Detail_Set_Inst (subst2subs sub,id_rls rls)
468 | tac_2tac (Calculate' (_, op_, _, _)) = Calculate (op_)
469 | tac_2tac (Check_elementwise' (_, pred, _)) = Check_elementwise pred
471 | tac_2tac (Or_to_List' _) = Or_to_List
472 | tac_2tac (Take' term) = Take (term2str term)
473 | tac_2tac (Substitute' (_, _, subte, _, _)) = Substitute (subte2sube subte)
474 | tac_2tac (Tac_ (_, _, id, _)) = Tac id
476 | tac_2tac (Subproblem' ((domID, pblID, _), _, _, _,_ ,_)) = Subproblem (domID, pblID)
477 | tac_2tac (Check_Postcond' (pblID, _)) = Check_Postcond pblID
478 | tac_2tac Empty_Tac_ = Empty_Tac
479 | tac_2tac m = error ("tac_2tac: not impl. for "^(tac_2str m));
481 val idT = Type ("Script.ID", []);
483 fun make_rule thy t =
484 let val ct = Thm.global_cterm_of thy (Trueprop $ t)
485 in Thm (term_to_string''' thy (Thm.term_of ct), Thm.make_thm ct) end;
487 fun rep_tac_ (Rewrite_Inst' (thy', _, _, put, subs, (thmID, _), f, (f', _))) =
488 let val fT = type_of f;
489 val b = if put then @{term True} else @{term False};
490 val sT = (type_of o fst o hd) subs;
491 val subs' = list2isalist (HOLogic.mk_prodT (sT, sT)) (map HOLogic.mk_prod subs);
492 val sT' = type_of subs';
493 val lhs = Const ("Script.Rewrite'_Inst", [sT', idT, bool, fT] ---> fT)
494 $ subs' $ Free (thmID, idT) $ b $ f;
495 in (((make_rule (assoc_thy thy')) o HOLogic.mk_eq) (lhs, f'), (lhs, f')) end
496 | rep_tac_ (Rewrite' (thy', _, _, put, (thmID, _), f, (f', _)))=
499 val b = if put then @{term True} else @{term False};
500 val lhs = Const ("Script.Rewrite", [idT, HOLogic.boolT, fT] ---> fT)
501 $ Free (thmID, idT) $ b $ f;
502 in (((make_rule (assoc_thy thy')) o HOLogic.mk_eq) (lhs, f'), (lhs, f')) end
503 | rep_tac_ (Rewrite_Set_Inst' (_, _, _, _, _, (f', _))) = (e_rule, (e_term, f'))
504 | rep_tac_ (Rewrite_Set' (thy', put, rls, f, (f', _))) =
507 val b = if put then @{term True} else @{term False};
508 val lhs = Const ("Script.Rewrite'_Set", [idT, bool, fT] ---> fT)
509 $ Free (id_rls rls, idT) $ b $ f;
510 in (((make_rule (assoc_thy thy')) o HOLogic.mk_eq) (lhs,f'),(lhs,f')) end
511 | rep_tac_ (Calculate' (thy', op_, f, (f', _)))=
514 val lhs = Const ("Script.Calculate",[idT,fT] ---> fT) $ Free (op_,idT) $ f
515 in (((make_rule (assoc_thy thy')) o HOLogic.mk_eq) (lhs,f'),(lhs,f')) end
516 | rep_tac_ (Check_elementwise' (_, _, (t', _))) = (Erule, (e_term, t'))
517 | rep_tac_ (Subproblem' (_, _, _, _, _, t')) = (Erule, (e_term, t'))
518 | rep_tac_ (Take' t') = (Erule, (e_term, t'))
519 | rep_tac_ (Substitute' (_, _, _, t, t')) = (Erule, (t, t'))
520 | rep_tac_ (Or_to_List' (t, t')) = (Erule, (t, t'))
521 | rep_tac_ m = error ("rep_tac_: not impl.for " ^ tac_2str m)
523 fun tac_2res m = (snd o snd o rep_tac_) m;
525 (* handle a leaf at the end of recursive descent:
526 a leaf is either a tactic or an 'expr' in "let v = expr"
527 where "expr" does not contain a tactic.
528 Handling a leaf comprises
529 (1) 'subst_stacexpr' substitute env and complete curried tactic
530 (2) rewrite the leaf by 'srls'
532 fun handle_leaf call thy srls E a v t =
533 (*WN050916 'upd_env_opt' is a blind copy from previous version*)
534 case subst_stacexpr E a v t of
535 (a', STac stac) => (*script-tactic*)
537 eval_listexpr_ (assoc_thy thy) srls (subst_atomic (upd_env_opt E (a,v)) stac)
540 then tracing ("@@@ "^call^" leaf '"^term2str t^"' ---> STac '"^term2str stac ^"'")
544 | (a', Expr lexpr) => (*leaf-expression*)
546 eval_listexpr_ (assoc_thy thy) srls (subst_atomic (upd_env_opt E (a,v)) lexpr)
549 then tracing("@@@ "^call^" leaf '"^term2str t^"' ---> Expr '"^term2str lexpr'^"'")
551 (a', Expr lexpr')) (*lexpr' is the value of the Expr*)
554 (** locate an applicable stac in a script **)
555 datatype assoc = (* ExprVal in the sense of denotational semantics *)
556 Assoc of (* the stac is associated, strongly or weakly *)
557 scrstate * (* the current; returned for next_tac etc. outside ass* *)
558 (step list) (* list of steps done until associated stac found;
559 initiated with the data for doing the 1st step,
560 thus the head holds these data further on,
561 while the tail holds steps finished (incl.scrstate in ptree) *)
562 | NasApp of (* stac not associated, but applicable, ptree-node generated *)
563 scrstate * (step list)
564 | NasNap of (* stac not associated, not applicable, nothing generated;
565 for distinction in Or, for leaving iterations, leaving Seq,
566 evaluate scriptexpressions *)
568 fun assoc2str (Assoc _) = "Assoc"
569 | assoc2str (NasNap _) = "NasNap"
570 | assoc2str (NasApp _) = "NasApp";
572 datatype asap = (* arg. of assy _only_ for distinction w.r.t. Or *)
573 Aundef (* undefined: set only by (topmost) Or *)
574 | AssOnly (* do not execute appl stacs - there could be an associated
575 in parallel Or-branch *)
576 | AssGen; (* no Ass(Weak) found within Or, thus
577 search for _applicable_ stacs, execute and generate pt *)
578 (*this constructions doesnt allow arbitrary nesting of Or !!! *)
580 (* assy, ass_up, astep_up scan for locate_gen in a script.
581 search is clearly separated into (1)-(2):
582 (1) assy is recursive descent;
583 (2) ass_up resumes interpretation at a location somewhere in the script;
584 astep_up does only get to the parentnode of the scriptexpr.
586 * call of (2) means _always_ that in this branch below
587 there was an appl.stac (Repeat, Or e1, ...) found by the previous step.
589 (*WN161112 blanks between list elements left as is until istate is introduced here*)
590 fun assy ya ((E,l,a,v,S,b),ss) (Const ("HOL.Let",_) $ e $ (Abs (id,T,body))) =
591 (case assy ya ((E , l @ [L, R], a,v,S,b),ss) e of
592 NasApp ((E',l,a,v,S,_),ss) =>
594 val id' = mk_Free (id, T);
595 val E' = upd_env E' (id', v);
596 in assy ya ((E', l @ [R, D], a,v,S,b),ss) body end
599 val id' = mk_Free (id, T);
600 val E' = upd_env E (id', v);
601 in assy ya ((E', l @ [R, D], a,v,S,b),ss) body end
603 | assy (ya as (thy,_,srls,_,_)) ((E,l,_,v,S,b),ss) (Const ("Script.While",_) $ c $ e $ a) =
604 if eval_true_ thy srls (subst_atomic (upd_env E (a,v)) c)
605 then assy ya ((E, l @ [L, R], SOME a,v,S,b),ss) e
607 | assy (ya as (thy,_,srls,_,_)) ((E,l,a,v,S,b),ss) (Const ("Script.While",_) $ c $ e) =
608 if eval_true_ thy srls (subst_atomic (upd_env_opt E (a,v)) c)
609 then assy ya ((E, l @ [R], a,v,S,b),ss) e
611 | assy (ya as (thy,_,srls,_,_)) ((E,l,a,v,S,b),ss) (Const ("If",_) $ c $ e1 $ e2) =
612 if eval_true_ thy srls (subst_atomic (upd_env_opt E (a,v)) c)
613 then assy ya ((E, l @ [L, R], a,v,S,b),ss) e1
614 else assy ya ((E, l @ [R], a,v,S,b),ss) e2
615 | assy ya ((E,l,_,v,S,b),ss) (Const ("Script.Try",_) $ e $ a) =
616 (case assy ya ((E, l @ [L, R], SOME a,v,S,b),ss) e of ay => ay)
617 | assy ya ((E,l,a,v,S,b),ss) (Const ("Script.Try",_) $ e) =
618 (case assy ya ((E, l @ [R], a,v,S,b),ss) e of ay => ay)
619 | assy ya ((E,l,_,v,S,b),ss) (Const ("Script.Seq",_) $e1 $ e2 $ a) =
620 (case assy ya ((E, l @ [L, L, R], SOME a,v,S,b),ss) e1 of
621 NasNap (v, E) => assy ya ((E, l @ [L, R], SOME a,v,S,b),ss) e2
622 | NasApp ((E,_,_,v,_,_),ss) => assy ya ((E, l @ [L, R], SOME a,v,S,b),ss) e2
624 | assy ya ((E,l,a,v,S,b),ss) (Const ("Script.Seq",_) $e1 $ e2) =
625 (case assy ya ((E, l @ [L, R], a,v,S,b),ss) e1 of
626 NasNap (v, E) => assy ya ((E, l @ [R], a,v,S,b),ss) e2
627 | NasApp ((E,_,_,v,_,_),ss) => assy ya ((E, l @ [R], a,v,S,b),ss) e2
629 | assy ya ((E,l,_,v,S,b),ss) (Const ("Script.Repeat",_) $ e $ a) =
630 assy ya ((E,(l @ [L, R]),SOME a,v,S,b),ss) e
631 | assy ya ((E,l,a,v,S,b),ss) (Const ("Script.Repeat",_) $ e) =
632 assy ya ((E,(l @ [R]),a,v,S,b),ss) e
633 | assy (y,x,s,sc,Aundef) ((E,l,_,v,S,b),ss) (Const ("Script.Or",_) $e1 $ e2 $ a) =
634 (case assy (y,x,s,sc,AssOnly) ((E,(l @ [L, L, R]),SOME a,v,S,b),ss) e1 of
636 (case assy (y,x,s,sc,AssOnly) ((E,(l @ [L, R]),SOME a,v,S,b),ss) e2 of
638 (case assy (y,x,s,sc,AssGen) ((E,(l @ [L, L, R]),SOME a,v,S,b),ss) e1 of
640 assy (y,x,s,sc,AssGen) ((E, (l @ [L, R]), SOME a,v,S,b),ss) e2
643 | NasApp _ => error ("assy: FIXXXME ///must not return NasApp///")
645 | assy ya ((E,l,a,v,S,b),ss) (Const ("Script.Or",_) $e1 $ e2) =
646 (case assy ya ((E,(l @ [L, R]),a,v,S,b),ss) e1 of
647 NasNap (v, E) => assy ya ((E,(l @ [R]),a,v,S,b),ss) e2
649 (*here is not a tactical like TRY etc, but a tactic creating a step in calculation*)
650 | assy (thy',ctxt,sr,d,ap) ((E,l,a,v,S,_), (m,_,pt,(p,p_),c)::ss) t =
651 (case handle_leaf "locate" thy' sr E a v t of
653 (NasNap (eval_listexpr_ (assoc_thy thy') sr
654 (subst_atomic (upd_env_opt E (a',v)) t), E))
660 | _ => error ("assy: call by " ^ pos'2str (p,p_));
662 case assod pt d m stac of
664 let val (p'',c',f',pt') =
665 generate1 (assoc_thy thy') m (ScrState (E,l,a',v',S,true), ctxt) (p',p_) pt;
666 in Assoc ((E,l,a',v',S,true), (m,f',pt',p'',c @ c')::ss) end
668 let val (p'',c',f',pt') =
669 generate1 (assoc_thy thy') m (ScrState (E,l,a',v',S,false), ctxt) (p',p_) pt;
670 in Assoc ((E,l,a',v',S,false), (m,f',pt',p'',c @ c')::ss) end
672 (case ap of (*switch for Or: 1st AssOnly, 2nd AssGen*)
673 AssOnly => (NasNap (v, E))
675 (case applicable_in (p,p_) pt (stac2tac pt (assoc_thy thy') stac) of
678 val is = (E,l,a',tac_2res m',S,false(*FIXXXME.WN0?*))
679 val (p'',c',f',pt') =
680 generate1 (assoc_thy thy') m' (ScrState is, ctxt) (p',p_) pt;
681 in NasApp (is,(m,f',pt',p'',c @ c')::ss) end
682 | Notappl _ => (NasNap (v, E))
686 | assy _ (_, []) t = error ("assy: uncovered fun-def with " ^ term2str t);
688 (*WN161112 blanks between list elements left as is until istate is introduced here*)
689 fun ass_up (ys as (y,ctxt,s,Prog sc,d)) ((E,l,a,v,S,b),ss) (Const ("HOL.Let",_) $ _) =
691 val l = drop_last l; (*comes from e, goes to Abs*)
694 Const ("HOL.Let",_) $ _ $ (Abs (i, T, body)) => (i, T, body)
695 | t => error ("ass_up..HOL.Let $ _ with " ^ term2str t))
696 val i = mk_Free (i, T);
697 val E = upd_env E (i, v);
698 in case assy (y,ctxt,s,d,Aundef) ((E, l @ [R, D], a,v,S,b),ss) body of
699 Assoc iss => Assoc iss
700 | NasApp iss => astep_up ys iss
701 | NasNap (v, E) => astep_up ys ((E,l,a,v,S,b),ss)
703 | ass_up ys iss (Abs (_,_,_)) = astep_up ys iss (*TODO 5.9.00: env ?*)
704 | ass_up ys iss (Const ("HOL.Let",_) $ _ $ (Abs _)) = astep_up ys iss (*TODO 5.9.00: env ?*)
705 | ass_up ysa iss (Const ("Script.Seq",_) $ _ $ _ $ _) =
706 astep_up ysa iss (*all has been done in (*2*) below*)
707 | ass_up ysa iss (Const ("Script.Seq",_) $ _ $ _) =
708 astep_up ysa iss (*2*: comes from e2*)
710 | ass_up (ysa as (y,ctxt,s,Prog sc,d)) ((E,l,a,v,S,b),ss)
711 (Const ("Script.Seq",_) $ _ ) = (*2*: comes from e1, goes to e2*)
713 val up = drop_last l;
716 Const ("Script.Seq",_) $ _ $ e2 => e2
717 | t => error ("ass_up..Script.Seq $ _ with " ^ term2str t))
718 in case assy (y,ctxt,s,d,Aundef) ((E, up @ [R], a,v,S,b),ss) e2 of
719 NasNap (v,E) => astep_up ysa ((E,up,a,v,S,b),ss)
720 | NasApp iss => astep_up ysa iss
723 | ass_up ysa iss (Const ("Script.Try",_) $ _ $ _) = astep_up ysa iss
724 | ass_up ysa iss (Const ("Script.Try",_) $ _) = astep_up ysa iss
725 | ass_up (ys as (y,ctxt,s,_,d)) ((E,l,_,v,S,b),ss)
726 (*(Const ("Script.While",_) $ c $ e $ a) = WN050930 blind fix*)
727 (t as Const ("Script.While",_) $ c $ e $ a) =
728 if eval_true_ y s (subst_atomic (upd_env E (a,v)) c)
729 then case assy (y,ctxt,s,d,Aundef) ((E, l @ [L, R], SOME a,v,S,b),ss) e of
730 NasNap (v,E') => astep_up ys ((E',l, SOME a,v,S,b),ss)
731 | NasApp ((E',l,a,v,S,b),ss) =>
732 ass_up ys ((E',l,a,v,S,b),ss) t (*WN050930 't' was not assigned*)
734 else astep_up ys ((E,l, SOME a,v,S,b),ss)
735 | ass_up (ys as (y,ctxt,s,_,d)) ((E,l,a,v,S,b),ss)
736 (*(Const ("Script.While",_) $ c $ e) = WN050930 blind fix*)
737 (t as Const ("Script.While",_) $ c $ e) =
738 if eval_true_ y s (subst_atomic (upd_env_opt E (a,v)) c)
739 then case assy (y,ctxt,s,d,Aundef) ((E, l @ [R], a,v,S,b),ss) e of
740 NasNap (v,E') => astep_up ys ((E',l, a,v,S,b),ss)
741 | NasApp ((E',l,a,v,S,b),ss) =>
742 ass_up ys ((E',l,a,v,S,b),ss) t (*WN050930 't' was not assigned*)
744 else astep_up ys ((E,l, a,v,S,b),ss)
745 | ass_up y iss (Const ("If",_) $ _ $ _ $ _) = astep_up y iss
746 | ass_up (ys as (y,ctxt,s,_,d)) ((E,l,_,v,S,b),ss)
747 (t as Const ("Script.Repeat",_) $ e $ a) =
748 (case assy (y,ctxt,s,d, Aundef) ((E, (l @ [L, R]), SOME a,v,S,b),ss) e of
749 NasNap (v,E') => astep_up ys ((E',l, SOME a,v,S,b),ss)
750 | NasApp ((E',l,a,v,S,b),ss) =>
751 ass_up ys ((E',l,a,v,S,b),ss) t
753 | ass_up (ys as (y,ctxt,s,_,d)) ((E,l,a,v,S,b),ss)
754 (t as Const ("Script.Repeat",_) $ e) =
755 (case assy (y,ctxt,s,d,Aundef) ((E, (l @ [R]), a,v,S,b),ss) e of
756 NasNap (v', E') => astep_up ys ((E',l,a,v',S,b),ss)
757 | NasApp ((E',l,a,v',S,_),ss) => ass_up ys ((E',l,a,v',S,b),ss) t
759 | ass_up y iss (Const ("Script.Or",_) $ _ $ _ $ _) = astep_up y iss
760 | ass_up y iss (Const ("Script.Or",_) $ _ $ _) = astep_up y iss
761 | ass_up y ((E,l,a,v,S,b),ss) (Const ("Script.Or",_) $ _ ) =
762 astep_up y ((E, (drop_last l), a,v,S,b),ss)
764 error ("ass_up not impl for t= " ^ term2str t)
765 and astep_up (ys as (_,_,_,Prog sc,_)) ((E,l,a,v,S,b),ss) =
768 let val up = drop_last l;
769 in ass_up ys ((E,up,a,v,S,b),ss) (go up sc) end
771 | astep_up _ ((_,l,_,_,_,_),_) = error ("astep_up: uncovered fun-def with " ^ loc_2str l)
773 (*check if there are tacs for rewriting only*)
774 fun rew_only ([]:step list) = true
775 | rew_only (((Rewrite' _ ,_,_,_,_))::ss) = rew_only ss
776 | rew_only (((Rewrite_Inst' _ ,_,_,_,_))::ss) = rew_only ss
777 | rew_only (((Rewrite_Set' _ ,_,_,_,_))::ss) = rew_only ss
778 | rew_only (((Rewrite_Set_Inst' _ ,_,_,_,_))::ss) = rew_only ss
779 | rew_only (((Calculate' _ ,_,_,_,_))::ss) = rew_only ss
780 | rew_only (((Begin_Trans' _ ,_,_,_,_))::ss) = rew_only ss
781 | rew_only (((End_Trans' _ ,_,_,_,_))::ss) = rew_only ss
782 | rew_only _ = false;
785 Steps of istate (* producing hd of step list (which was latest)
786 for next_tac, for reporting Safe|Unsafe to DG *)
787 * step (* (scrstate producing this step is in ptree !) *)
788 list (* locate_gen may produce intermediate steps *)
789 | NotLocatable; (* no (m Ass m') or (m AssWeak m') found *)
791 (* locate_gen tries to locate an input tac m in the script.
792 pursuing this goal the script is executed until an (m' equiv m) is found,
793 or the end of the script
795 m : input by the user, already checked by applicable_in,
796 (to be searched within Or; and _not_ an m doing the step on ptree !)
797 p,pt: (incl ets) at the time of input
799 d : canonical simplifier for locating Take, Substitute, Subproblems etc.
800 ets : ets at the time of input
801 l : the location (in scr) of the stac which generated the current formula
803 Steps: pt,p (incl. ets) with m done
804 pos' list of proofobjs cut (from generate)
805 safe: implied from last proofobj
807 ///ToDo : ets contains a list of tacs to be done before m can be done
808 NOT IMPL. -- "error: do other step before"
809 NotLocatable: thus generate_hard
811 (*WN161112 blanks between list elements left as is until istate is introduced here*)
812 fun locate_gen (thy', _) (Rewrite' (_, ro, er, pa, thm, f, _)) (pt, p)
813 (Rfuns {locate_rule=lo,...}, _) (RrlsState (_,f'',rss,rts), _) =
814 (case lo rss f (Thm thm) of
816 | rts' => Steps (rts2steps [] ((pt,p),(f,f'',rss,rts),(thy',ro,er,pa)) rts'))
817 | locate_gen (thy',srls) (m:tac_) ((pt,p):ptree * pos')
818 (scr as Prog (_ $ body),d) (ScrState (E,l,a,v,S,b), ctxt) =
819 let val thy = assoc_thy thy';
820 in case if l = [] orelse (
821 (*init.in solve..Apply_Method...*)(last_elem o fst) p = 0 andalso snd p = Res)
822 then (assy (thy',ctxt,srls,d,Aundef) ((E,[R],a,v,S,b), [(m,EmptyMout,pt,p,[])]) body)
823 else (astep_up (thy',ctxt,srls,scr,d) ((E,l,a,v,S,b), [(m,EmptyMout,pt,p,[])]) ) of
824 Assoc ((is as (_,_,_,_,_,strong_ass), ss as (_ :: _))) =>
826 then (Steps (ScrState is, ss))
828 if rew_only ss (*andalso 'not strong_ass'= associated weakly*)
832 val po' = case p_ of Frm => po | Res => lev_on po | _ => error ("locate_gen " ^ pos_2str p_)
833 val (p'',c'',f'',pt'') = generate1 thy m (ScrState is, ctxt) (po',p_) pt
834 in Steps (ScrState is, [(m, f'',pt'',p'',c'')]) end
835 else Steps (ScrState is, ss))
837 | NasApp _ => NotLocatable
838 | err => error ("not-found-in-script: NotLocatable from " ^ PolyML.makestring err)
840 | locate_gen _ m _ (sc,_) (is, _) =
841 error ("locate_gen: wrong arguments,\n tac= " ^ tac_2str m ^ ",\n " ^
842 "scr= " ^ scr2str sc ^ ",\n istate= " ^ istate2str is);
844 (** find the next stactic in a script **)
846 (*appy, nxt_up, nstep_up scanning for next_tac.
847 search is clearly separated into (1)-(2):
848 (1) appy is recursive descent;
849 (2) nxt_up resumes interpretation at a location somewhere in the script;
850 nstep_up does only get to the parentnode of the scriptexpr.
852 * call of (2) means _always_ that in this branch below
853 there was an applicable stac (Repeat, Or e1, ...)
855 datatype appy = (* ExprVal in the sense of denotational semantics *)
856 Appy of (* applicable stac found, search stalled *)
857 tac_ * (* tac_ associated (fun assod) with stac *)
858 scrstate (* after determination of stac WN.18.8.03 *)
859 | Napp of (* stac found was not applicable;
860 this mode may become Skip in Repeat, Try and Or *)
861 env (*stack*)(* popped while nxt_up *)
862 | Skip of (* for restart after Appy, for leaving iterations,
863 for passing the value of scriptexpressions,
864 and for finishing the script successfully *)
865 term * env (*stack*);
867 datatype appy_ = (* as argument in nxt_up, nstep_up, from appy *)
868 (*Appy is only (final) returnvalue, not argument during search *)
869 Napp_ (* ev. detects 'script is not appropriate for this example' *)
870 | Skip_; (* detects 'script successfully finished'
871 also used as init-value for resuming; this works,
872 because 'nxt_up Or e1' treats as Appy *)
874 fun appy thy ptp E l (Const ("HOL.Let",_) $ e $ (Abs (i,T,b))) a v =
875 (case appy thy ptp E (l @ [L, R]) e a v of
877 let val E' = upd_env E (Free (i,T), res);
878 in appy thy ptp E' (l @ [R, D]) b a v end
880 | appy (thy as (th,sr)) ptp E l (Const ("Script.While"(*1*),_) $ c $ e $ a) _ v =
881 (if eval_true_ th sr (subst_atomic (upd_env E (a,v)) c)
882 then appy thy ptp E (l @ [L, R]) e (SOME a) v
884 | appy (thy as (th,sr)) ptp E l (Const ("Script.While"(*2*),_) $ c $ e) a v =
885 (if eval_true_ th sr (subst_atomic (upd_env_opt E (a,v)) c)
886 then appy thy ptp E (l @ [R]) e a v
888 | appy (thy as (th,sr)) ptp E l (Const ("If",_) $ c $ e1 $ e2) a v =
889 (if eval_true_ th sr (subst_atomic (upd_env_opt E (a,v)) c)
890 then appy thy ptp E (l @ [L, R]) e1 a v
891 else appy thy ptp E (l @ [R]) e2 a v)
892 | appy thy ptp E l (Const ("Script.Repeat"(*1*),_) $ e $ a) _ v =
893 appy thy ptp E (l @ [L, R]) e (SOME a) v
894 | appy thy ptp E l (Const ("Script.Repeat"(*2*),_) $ e) a v = appy thy ptp E (l @ [R]) e a v
895 | appy thy ptp E l (Const ("Script.Try",_) $ e $ a) _ v =
896 (case appy thy ptp E (l @ [L, R]) e (SOME a) v of
897 Napp E => (Skip (v, E))
899 | appy thy ptp E l(Const ("Script.Try",_) $ e) a v =
900 (case appy thy ptp E (l @ [R]) e a v of
901 Napp E => (Skip (v, E))
903 | appy thy ptp E l (Const ("Script.Or"(*1*),_) $e1 $ e2 $ a) _ v =
904 (case appy thy ptp E (l @ [L, L, R]) e1 (SOME a) v of
906 | _ => appy thy ptp E (*env*) (l @ [L, R]) e2 (SOME a) v)
907 | appy thy ptp E l (Const ("Script.Or"(*2*),_) $e1 $ e2) a v =
908 (case appy thy ptp E (l @ [L, R]) e1 a v of
910 | _ => appy thy ptp E (l @ [R]) e2 a v)
911 | appy thy ptp E l (Const ("Script.Seq"(*1*),_) $ e1 $ e2 $ a) _ v =
912 (case appy thy ptp E (l @ [L, L, R]) e1 (SOME a) v of
913 Skip (v,E) => appy thy ptp E (l @ [L, R]) e2 (SOME a) v
915 | appy thy ptp E l (Const ("Script.Seq",_) $ e1 $ e2) a v =
916 (case appy thy ptp E (l @ [L,R]) e1 a v of
917 Skip (v,E) => appy thy ptp E (l @ [R]) e2 a v
919 (* a leaf has been found *)
920 | appy ((th,sr)) (pt, p) E l t a v =
921 case handle_leaf "next " th sr E a v t of
922 (_, Expr s) => Skip (s, E)
924 let val (m,m') = stac2tac_ pt (assoc_thy th) stac
926 Subproblem _ => Appy (m', (E,l,a',tac_2res m',Sundef,false))
928 (case applicable_in p pt m of
929 Appl m' => (Appy (m', (E,l,a',tac_2res m',Sundef,false)))
933 fun nxt_up thy ptp (scr as (Prog sc)) E l ay (Const ("HOL.Let", _) $ _) a v = (*comes from let=...*)
935 then nstep_up thy ptp scr E (drop_last l) Napp_ a v
941 Const ("HOL.Let",_) $ _ $ (Abs aa) => aa
942 | t => error ("nxt_up..HOL.Let $ _ with " ^ term2str t))
943 val i = mk_Free (i, T)
944 val E = upd_env E (i, v)
946 case appy thy ptp E (up @ [R,D]) body a v of
948 | Napp E => nstep_up thy ptp scr E up Napp_ a v
949 | Skip (v,E) => nstep_up thy ptp scr E up Skip_ a v
951 | nxt_up thy ptp scr E l ay (Abs _) a v = nstep_up thy ptp scr E l ay a v
952 | nxt_up thy ptp scr E l ay (Const ("HOL.Let",_) $ _ $ (Abs _)) a v =
953 nstep_up thy ptp scr E l ay a v
954 (*no appy_: never causes Napp -> Helpless*)
955 | nxt_up (thy as (th, sr)) ptp scr E l _ (Const ("Script.While"(*1*), _) $ c $ e $ _) a v =
956 if eval_true_ th sr (subst_atomic (upd_env_opt E (a, v)) c)
957 then case appy thy ptp E (l @ [L,R]) e a v of
959 | Napp E => nstep_up thy ptp scr E l Skip_ a v
960 | Skip (v,E) => nstep_up thy ptp scr E l Skip_ a v
961 else nstep_up thy ptp scr E l Skip_ a v
962 (*no appy_: never causes Napp - Helpless*)
963 | nxt_up (thy as (th, sr)) ptp scr E l _ (Const ("Script.While"(*2*), _) $ c $ e) a v =
964 if eval_true_ th sr (subst_atomic (upd_env_opt E (a, v)) c)
965 then case appy thy ptp E (l @ [R]) e a v of
967 | Napp E => nstep_up thy ptp scr E l Skip_ a v
968 | Skip (v,E) => nstep_up thy ptp scr E l Skip_ a v
969 else nstep_up thy ptp scr E l Skip_ a v
970 | nxt_up thy ptp scr E l ay (Const ("If", _) $ _ $ _ $ _) a v = nstep_up thy ptp scr E l ay a v
971 | nxt_up thy ptp scr E l _ (*no appy_: there was already a stac below*)
972 (Const ("Script.Repeat"(*1*), _) $ e $ _) a v =
973 (case appy thy ptp (*upd_env*) E (*a,v)*) ((l @ [L, R]):loc_) e a v of
975 | Napp E => nstep_up thy ptp scr E l Skip_ a v
976 | Skip (v,E) => nstep_up thy ptp scr E l Skip_ a v)
977 | nxt_up thy ptp scr E l _ (*no appy_: there was already a stac below*)
978 (Const ("Script.Repeat"(*2*), _) $ e) a v =
979 (case appy thy ptp (*upd_env*) E (*a,v)*) (l @ [R]) e a v of
981 | Napp E => nstep_up thy ptp scr E l Skip_ a v
982 | Skip (v,E) => nstep_up thy ptp scr E l Skip_ a v)
983 | nxt_up thy ptp scr E l _ (Const ("Script.Try",_) $ _ $ _) a v = (*makes Napp to Skip*)
984 nstep_up thy ptp scr E l Skip_ a v
986 | nxt_up thy ptp scr E l _ (Const ("Script.Try"(*2*), _) $ _) a v = (*makes Napp to Skip*)
987 nstep_up thy ptp scr E l Skip_ a v
988 | nxt_up thy ptp scr E l ay (Const ("Script.Or",_) $ _ $ _ $ _) a v =
989 nstep_up thy ptp scr E l ay a v
990 | nxt_up thy ptp scr E l ay (Const ("Script.Or",_) $ _ $ _) a v = nstep_up thy ptp scr E l ay a v
991 | nxt_up thy ptp scr E l ay (Const ("Script.Or",_) $ _ ) a v =
992 nstep_up thy ptp scr E (drop_last l) ay a v
993 | nxt_up thy ptp scr E l ay (Const ("Script.Seq"(*1*),_) $ _ $ _ $ _) a v =
994 (*all has been done in (*2*) below*) nstep_up thy ptp scr E l ay a v
995 | nxt_up thy ptp scr E l ay (Const ("Script.Seq"(*2*),_) $ _ $ _) a v = (*comes from e2*)
996 nstep_up thy ptp scr E l ay a v
997 | nxt_up thy ptp (scr as Prog sc) E l ay (Const ("Script.Seq",_) $ _) a v = (*comes from e1*)
999 then nstep_up thy ptp scr E (drop_last l) Napp_ a v
1001 let val up = drop_last l;
1004 Const ("Script.Seq"(*2*), _) $ _ $ e2 => e2
1005 | t => error ("nxt_up..Script.Seq $ _ with " ^ term2str t))
1006 in case appy thy ptp E (up @ [R]) e2 a v of
1008 | Napp E => nstep_up thy ptp scr E up Napp_ a v
1009 | Skip (v,E) => nstep_up thy ptp scr E up Skip_ a v end
1010 | nxt_up _ _ _ _ _ _ t _ _ = error ("nxt_up not impl for " ^ term2str t)
1011 and nstep_up thy ptp (Prog sc) E l ay a v =
1014 let val up = drop_last l;
1015 in (nxt_up thy ptp (Prog sc) E up ay (go up sc) a v ) end
1016 else (*interpreted to end*)
1017 if ay = Skip_ then Skip (v, E) else Napp E
1018 | nstep_up _ _ _ _ l _ _ _ = error ("nstep_up: uncovered fun-def at " ^ loc_2str l)
1020 (* decide for the next applicable stac in the script;
1021 returns (stactic, value) - the value in case the script is finished
1022 12.8.02: ~~~~~ and no assumptions ??? FIXME ???
1023 20.8.02: must return p in case of finished, because the next script
1024 consulted need not be the calling script:
1025 in case of detail ie. _inserted_ PrfObjs, the next stac
1026 has to searched in a script with PblObj.status<>Complete !
1027 (.. not true for other details ..PrfObj ??????????????????
1028 20.8.02: do NOT return safe (is only changed in locate !!!)
1030 fun next_tac (thy,_) _ (Rfuns {next_rule, ...}) (RrlsState(f, f', rss, _), ctxt) =
1032 then (End_Detail' (f',[])(*8.6.03*), (Uistate, ctxt),
1033 (f', Sundef(*FIXME is no value of next_tac! vor 8.6.03*))) (*finished*)
1035 (case next_rule rss f of
1036 NONE => (Empty_Tac_, (Uistate, ctxt), (e_term, Sundef)) (*helpless*)
1037 | SOME (Thm thm'')(*8.6.03: muss auch f' liefern ?!!*) =>
1038 (Rewrite' (thy, "e_rew_ord", e_rls, false, thm'', f, (e_term, [(*!?!8.6.03*)])),
1039 (Uistate, ctxt), (e_term, Sundef))) (*next stac*)
1040 | next_tac thy (ptp as (pt, (p, _)):ptree * pos') (sc as Prog (_ $ body))
1041 (ScrState (E,l,a,v,s,_), ctxt) =
1042 (case if l = [] then appy thy ptp E [R] body NONE v
1043 else nstep_up thy ptp sc E l Skip_ a v of
1044 Skip (v, _) => (*finished*)
1045 (case par_pbl_det pt p of
1048 val (_,pblID,_) = get_obj g_spec pt p';
1049 in (Check_Postcond' (pblID, (v, [(*assigned in next step*)])),
1050 (e_istate, ctxt), (v,s))
1052 | _ => (End_Detail' (e_term,[])(*8.6.03*), (e_istate, ctxt), (v,s)))
1053 | Napp _ => (Empty_Tac_, (e_istate, ctxt), (e_term, Sundef)) (*helpless*)
1054 | Appy (m', scrst as (_,_,_,v,_,_)) => (m', (ScrState scrst, ctxt), (v, Sundef))) (*next stac*)
1055 | next_tac _ _ _ (is, _) = error ("next_tac: not impl for " ^ (istate2str is));
1057 (*.create the initial interpreter state from the items of the guard.*)
1059 val errmsg = "ERROR: found no actual arguments for prog. of "
1060 fun msg_miss (sc, metID, formals, actuals) =
1061 "ERROR in creating the environment for '" ^ id_of_scr sc ^
1062 "' from \nthe items of the guard of " ^ metID2str metID ^ ",\n" ^
1063 "formal arg(s), from the script, miss actual arg(s), from the guards env:\n" ^
1064 (string_of_int o length) formals ^ " formals: " ^ terms2str formals ^ "\n" ^
1065 (string_of_int o length) actuals ^ " actuals: " ^ terms2str actuals
1066 fun msg_type (sc, metID, a, f, formals, actuals) =
1067 "ERROR in creating the environment for '" ^
1068 id_of_scr sc ^ "' from \nthe items of the guard of " ^
1069 metID2str metID ^ ",\n" ^
1070 "different types of formal arg, from the script, " ^
1071 "and actual arg, from the guards env:'\n" ^
1072 "formal: '" ^ term2str a ^ "::" ^ (type2str o type_of) a ^ "'\n" ^
1073 "actual: '" ^ term2str f ^ "::" ^ (type2str o type_of) f ^ "'\n" ^
1075 "formals: " ^ terms2str formals ^ "\n" ^
1076 "actuals: " ^ terms2str actuals
1078 fun init_scrstate thy itms metID =
1080 val actuals = itms2args thy metID itms
1081 val _ = if actuals <> [] then () else raise ERROR (errmsg ^ strs2str' metID)
1082 val (scr, sc) = (case (#scr o get_met) metID of
1083 scr as Prog sc => (scr, sc) | _ => raise ERROR ("init_scrstate with " ^ metID2str metID))
1084 val formals = formal_args sc
1085 (*expects same sequence of (actual) args in itms and (formal) args in met*)
1086 fun relate_args env [] [] = env
1087 | relate_args _ _ [] = error (msg_miss (sc, metID, formals, actuals))
1088 | relate_args env [] _ = env (*may drop Find!*)
1089 | relate_args env (a::aa) (f::ff) =
1090 if type_of a = type_of f
1091 then relate_args (env @ [(a, f)]) aa ff
1092 else error (msg_type (sc, metID, a, f, formals, actuals))
1093 val env = relate_args [] formals actuals;
1094 val ctxt = Proof_Context.init_global thy |> declare_constraints' actuals
1095 val {pre, prls, ...} = get_met metID;
1096 val pres = check_preconds thy prls pre itms |> map snd;
1097 val ctxt = ctxt |> insert_assumptions pres;
1098 in (ScrState (env, [], NONE, e_term, Safe, true), ctxt, scr) : istate * Proof.context * scr end;
1101 (* decide, where to get script/istate from:
1102 (* 1 *) from PblObj.env: at begin of script if no init_form
1103 (* 2 *) from PblObj/PrfObj: if stac is in the middle of the script
1104 (* 3 *) from rls/PrfObj: in case of detail a ruleset *)
1105 fun from_pblobj_or_detail' _ (p, p_) pt =
1106 if member op = [Pbl, Met] p_
1107 then case get_obj g_env pt p of
1108 NONE => error "from_pblobj_or_detail': no istate"
1111 val metID = get_obj g_metID pt p
1112 val {srls, ...} = get_met metID
1113 in (srls, is, (#scr o get_met) metID) end
1115 let val (pbl, p', rls') = par_pbl_det pt p
1117 then (*if last_elem p = 0 nothing written to pt yet*) (* 2 *)
1119 val metID = get_obj g_metID pt p'
1120 val {srls,...} = get_met metID
1121 in (srls, get_loc pt (p,p_), (#scr o get_met) metID) end
1122 else (*FIXME.WN0?: get from pbl or met !!! unused for Rrls in locate_gen, next_tac*) (* 3 *)
1123 (e_rls, get_loc pt (p,p_),
1125 Rls {scr = scr,...} => scr
1126 | Seq {scr = scr,...} => scr
1127 | Rrls {scr=rfuns,...} => rfuns
1128 | Erls => error "from_pblobj_or_detail' with Erls")
1131 (*.get script and istate from PblObj, see ( * 1 *)
1132 fun from_pblobj' thy' (p,p_) pt =
1134 val p' = par_pblobj pt p
1135 val thy = assoc_thy thy'
1137 (case get_obj I pt p' of
1138 PblObj {meth = itms, ...} => itms
1139 | PrfObj _ => error "from_pblobj' NOT with PrfObj")
1140 val metID = get_obj g_metID pt p'
1141 val {srls, scr, ...} = get_met metID
1143 if last_elem p = 0 (*nothing written to pt yet*)
1145 let val (is, ctxt, scr) = init_scrstate thy itms metID
1146 in (srls, (is, ctxt), scr) end
1147 else (srls, get_loc pt (p,p_), scr)
1150 (*.get the stactics and problems of a script as tacs
1151 instantiated with the current environment;
1152 l is the location which generated the given formula.*)
1153 (*WN.12.5.03: quick-and-dirty repair for listexpressions*)
1154 fun is_spec_pos Pbl = true
1155 | is_spec_pos Met = true
1156 | is_spec_pos _ = false;
1158 (*. fetch _all_ tactics from script .*)
1159 fun sel_rules _ (([],Res):pos') =
1160 raise PTREE "no tactics applicable at the end of a calculation"
1161 | sel_rules pt (p,p_) =
1163 then [get_obj g_tac pt p]
1166 val pp = par_pblobj pt p;
1167 val thy' = (get_obj g_domID pt pp):theory';
1168 val thy = assoc_thy thy';
1169 val metID = get_obj g_metID pt pp;
1170 val metID' = if metID =e_metID then (thd3 o snd3) (get_obj g_origin pt pp) else metID
1171 val (sc, srls) = (case get_met metID' of
1172 {scr = Prog sc, srls, ...} => (sc, srls) | _ => error "sel_rules 1")
1173 val (env, a, v) = (case get_istate pt (p, p_) of
1174 ScrState (env, _, a, v, _, _) => (env, a, v) | _ => error "sel_rules 2")
1175 in map ((stac2tac pt thy) o rep_stacexpr o #2 o
1176 (handle_leaf "selrul" thy' srls env a v)) (stacpbls sc)
1179 (*. fetch tactics from script and filter _applicable_ tactics;
1180 in case of Rewrite_Set* go down to _atomic_ rewrite-tactics .*)
1181 fun sel_appl_atomic_tacs _ (([], Res) : pos') =
1182 raise PTREE "no tactics applicable at the end of a calculation"
1183 | sel_appl_atomic_tacs pt (p, p_) =
1185 then [get_obj g_tac pt p]
1188 val pp = par_pblobj pt p
1189 val thy' = (get_obj g_domID pt pp):theory'
1190 val thy = assoc_thy thy'
1191 val metID = get_obj g_metID pt pp
1194 then (thd3 o snd3) (get_obj g_origin pt pp)
1196 val (sc, srls, erls, ro) = (case get_met metID' of
1197 {scr = Prog sc, srls, erls, rew_ord' = ro, ...} => (sc, srls, erls, ro)
1198 | _ => error "sel_appl_atomic_tacs 1")
1199 val (env, a, v) = (case get_istate pt (p, p_) of
1200 ScrState (env, _, a, v, _, _) => (env, a, v) | _ => error "sel_appl_atomic_tacs 2")
1201 val alltacs = (*we expect at least 1 stac in a script*)
1202 map ((stac2tac pt thy) o rep_stacexpr o #2 o
1203 (handle_leaf "selrul" thy' srls env a v)) (stacpbls sc)
1205 (case p_ of Frm => get_obj g_form pt p | Res => (fst o (get_obj g_result pt)) p
1207 (*WN071231 ? replace atomic_appl_tacs with applicable_in (ineff!) ?*)
1208 in ((gen_distinct eq_tac) o flat o (map (atomic_appl_tacs thy ro erls f))) alltacs end;