src/Tools/isac/Interpret/script.sml
branchisac-update-Isa09-2
changeset 37947 22235e4dbe5f
parent 37938 f6164be9280d
child 37984 972a73d7c50b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Tools/isac/Interpret/script.sml	Wed Aug 25 16:20:07 2010 +0200
     1.3 @@ -0,0 +1,2031 @@
     1.4 +(* interpreter for scripts
     1.5 +   (c) Walther Neuper 2000
     1.6 +
     1.7 +use"ME/script.sml";
     1.8 +use"script.sml";
     1.9 +*)
    1.10 +signature INTERPRETER =
    1.11 +sig
    1.12 +  (*type ets (list of executed tactics) see sequent.sml*)
    1.13 +
    1.14 +  datatype locate
    1.15 +    = NotLocatable
    1.16 +    | Steps of (tac_ * mout * ptree * pos' * cid * safe (* ets*)) list
    1.17 +(*    | ToDo of ets 28.4.02*)
    1.18 +
    1.19 +  (*diss: next-tactic-function*)
    1.20 +  val next_tac : theory' -> ptree * pos' -> metID -> scr -> ets -> tac_
    1.21 +  (*diss: locate-function*)
    1.22 +  val locate_gen : theory'
    1.23 +                   -> tac_
    1.24 +                      -> ptree * pos' -> scr * rls -> ets -> loc_ -> locate
    1.25 +
    1.26 +  val sel_rules : ptree -> pos' -> tac list
    1.27 +  val init_form : scr -> ets -> loc_ * term option (*FIXME not up to date*)
    1.28 +  val formal_args : term -> term list
    1.29 +
    1.30 +  (*shift to library ...*)
    1.31 +  val inst_abs : theory' -> term -> term
    1.32 +  val itms2args : metID -> itm list -> term list
    1.33 +  val user_interrupt : loc_ * (tac_ * env * env * term * term * safe)
    1.34 +  (*val empty : term*) 
    1.35 +end 
    1.36 +
    1.37 +
    1.38 +
    1.39 +
    1.40 +(*
    1.41 +structure Interpreter : INTERPRETER =
    1.42 +struct
    1.43 +*)
    1.44 +
    1.45 +(*.traces the leaves (ie. non-tactical nodes) of the script
    1.46 +   found by next_tac.
    1.47 +   a leaf is either a tactic or an 'exp' in 'let v = expr'
    1.48 +   where 'exp' does not contain a tactic.*)   
    1.49 +val trace_script = ref false;
    1.50 +
    1.51 +type step =     (*data for creating a new node in the ptree;
    1.52 +		 designed for use:
    1.53 +               	 fun ass* scrstate steps =
    1.54 +               	 ... case ass* scrstate steps of
    1.55 +               	     Assoc (scrstate, steps) => ... ass* scrstate steps*)
    1.56 +    tac_       (*transformed from associated tac*)
    1.57 +    * mout       (*result with indentation etc.*)
    1.58 +    * ptree      (*containing node created by tac_ + resp. scrstate*)
    1.59 +    * pos'       (*position in ptree; ptree * pos' is the proofstate*)
    1.60 +    * pos' list; (*of ptree-nodes probably cut (by fst tac_)*)
    1.61 +val e_step = (Empty_Tac_, EmptyMout, EmptyPtree, e_pos',[]:pos' list):step;
    1.62 +
    1.63 +fun rule2thm' (Thm (id, thm)) = (id, string_of_thmI thm):thm'
    1.64 +  | rule2thm' r = raise error ("rule2thm': not defined for "^(rule2str r));
    1.65 +fun rule2rls' (Rls_ rls) = id_rls rls
    1.66 +  | rule2rls' r = raise error ("rule2rls': not defined for "^(rule2str r));
    1.67 +
    1.68 +(*.makes a (rule,term) list to a Step (m, mout, pt', p', cid) for solve;
    1.69 +   complicated with current t in rrlsstate.*)
    1.70 +fun rts2steps steps ((pt,p),(f,f'',rss,rts),(thy',ro,er,pa)) [(r, (f', am))] =
    1.71 +    let val thy = assoc_thy thy'
    1.72 +	val m = Rewrite' (thy',ro,er,pa, rule2thm' r, f, (f', am))
    1.73 +	val is = RrlsState (f',f'',rss,rts)
    1.74 +	val p = case p of (p',Frm) => p | (p',Res) => (lev_on p',Res)
    1.75 +	val (p', cid, mout, pt') = generate1 thy m is p pt
    1.76 +    in (is, (m, mout, pt', p', cid)::steps) end
    1.77 +  | rts2steps steps ((pt,p),(f,f'',rss,rts),(thy',ro,er,pa)) 
    1.78 +	      ((r, (f', am))::rts') =
    1.79 +    let val thy = assoc_thy thy'
    1.80 +	val m = Rewrite' (thy',ro,er,pa, rule2thm' r, f, (f', am))
    1.81 +	val is = RrlsState (f',f'',rss,rts)
    1.82 +	val p = case p of (p',Frm) => p | (p',Res) => (lev_on p',Res)
    1.83 +	val (p', cid, mout, pt') = generate1 thy m is p pt
    1.84 +    in rts2steps ((m, mout, pt', p', cid)::steps) 
    1.85 +		 ((pt',p'),(f',f'',rss,rts),(thy',ro,er,pa)) rts' end;
    1.86 +
    1.87 +
    1.88 +(*. functions for the environment stack .*)
    1.89 +fun accessenv id es = the (assoc((top es):env, id))
    1.90 +    handle _ => error ("accessenv: "^(free2str id)^" not in env");
    1.91 +fun updateenv id vl (es:env stack) = 
    1.92 +    (push (overwrite(top es, (id, vl))) (pop es)):env stack;
    1.93 +fun pushenv id vl (es:env stack) = 
    1.94 +    (push (overwrite(top es, (id, vl))) es):env stack;
    1.95 +val popenv = pop:env stack -> env stack;
    1.96 +
    1.97 +
    1.98 +
    1.99 +fun de_esc_underscore str =
   1.100 +  let fun scan [] = []
   1.101 +	| scan (s::ss) = if s = "'" then (scan ss)
   1.102 +			 else (s::(scan ss))
   1.103 +  in (implode o scan o explode) str end;
   1.104 +(*
   1.105 +> val str = "Rewrite_Set_Inst";
   1.106 +> val esc = esc_underscore str;
   1.107 +val it = "Rewrite'_Set'_Inst" : string
   1.108 +> val des = de_esc_underscore esc;
   1.109 + val des = de_esc_underscore esc;*)
   1.110 +
   1.111 +(*go at a location in a script and fetch the contents*)
   1.112 +fun go [] t = t
   1.113 +  | go (D::p) (Abs(s,ty,t0)) = go (p:loc_) t0
   1.114 +  | go (L::p) (t1 $ t2) = go p t1
   1.115 +  | go (R::p) (t1 $ t2) = go p t2
   1.116 +  | go l _ = raise error ("go: no "^(loc_2str l));
   1.117 +(*
   1.118 +> val t = (term_of o the o (parse thy)) "a+b";
   1.119 +val it = Const (#,#) $ Free (#,#) $ Free ("b","RealDef.real") : term
   1.120 +> val plus_a = go [L] t; 
   1.121 +> val b = go [R] t; 
   1.122 +> val plus = go [L,L] t; 
   1.123 +> val a = go [L,R] t;
   1.124 +
   1.125 +> val t = (term_of o the o (parse thy)) "a+b+c";
   1.126 +val t = Const (#,#) $ (# $ # $ Free #) $ Free ("c","RealDef.real") : term
   1.127 +> val pl_pl_a_b = go [L] t; 
   1.128 +> val c = go [R] t; 
   1.129 +> val a = go [L,R,L,R] t; 
   1.130 +> val b = go [L,R,R] t; 
   1.131 +*)
   1.132 +
   1.133 +
   1.134 +(* get a subterm t with test t, and record location *)
   1.135 +fun get l test (t as Const (s,T)) = 
   1.136 +    if test t then SOME (l,t) else NONE
   1.137 +  | get l test (t as Free (s,T)) = 
   1.138 +    if test t then SOME (l,t) else NONE 
   1.139 +  | get l test (t as Bound n) =
   1.140 +    if test t then SOME (l,t) else NONE 
   1.141 +  | get l test (t as Var (s,T)) =
   1.142 +    if test t then SOME (l,t) else NONE
   1.143 +  | get l test (t as Abs (s,T,body)) =
   1.144 +    if test t then SOME (l:loc_,t) else get ((l@[D]):loc_) test body
   1.145 +  | get l test (t as t1 $ t2) =
   1.146 +    if test t then SOME (l,t) 
   1.147 +    else case get (l@[L]) test t1 of 
   1.148 +      NONE => get (l@[R]) test t2
   1.149 +    | SOME (l',t') => SOME (l',t');
   1.150 +(*18.6.00
   1.151 +> val sss = ((term_of o the o (parse thy))
   1.152 +  "Script Solve_root_equation (eq_::bool) (v_::real) (err_::bool) =\
   1.153 +   \ (let e_ = Try (Rewrite square_equation_left True eq_) \
   1.154 +   \  in [e_])");
   1.155 +          ______ compares head_of !!
   1.156 +> get [] (eq_str "Let") sss;            [R]
   1.157 +> get [] (eq_str "Script.Try") sss;     [R,L,R]
   1.158 +> get [] (eq_str "Script.Rewrite") sss; [R,L,R,R]
   1.159 +> get [] (eq_str "True") sss;           [R,L,R,R,L,R]
   1.160 +> get [] (eq_str "e_") sss;             [R,R]
   1.161 +*)
   1.162 +
   1.163 +fun test_negotiable t = 
   1.164 +    member op = (!negotiable) 
   1.165 +           ((strip_thy o (term_str (theory "Script")) o head_of) t);
   1.166 +
   1.167 +(*.get argument of first stactic in a script for init_form.*)
   1.168 +fun get_stac thy (h $ body) =
   1.169 +(* 
   1.170 +   *)
   1.171 +  let
   1.172 +    fun get_t y (Const ("Script.Seq",_) $ e1 $ e2) a = 
   1.173 +    	(case get_t y e1 a of NONE => get_t y e2 a | la => la)
   1.174 +      | get_t y (Const ("Script.Seq",_) $ e1 $ e2 $ a) _ = 
   1.175 +    	(case get_t y e1 a of NONE => get_t y e2 a | la => la)
   1.176 +      | get_t y (Const ("Script.Try",_) $ e) a = get_t y e a
   1.177 +      | get_t y (Const ("Script.Try",_) $ e $ a) _ = get_t y e a
   1.178 +      | get_t y (Const ("Script.Repeat",_) $ e) a = get_t y e a
   1.179 +      | get_t y (Const ("Script.Repeat",_) $ e $ a) _ = get_t y e a
   1.180 +      | get_t y (Const ("Script.Or",_) $e1 $ e2) a =
   1.181 +    	(case get_t y e1 a of NONE => get_t y e2 a | la => la)
   1.182 +      | get_t y (Const ("Script.Or",_) $e1 $ e2 $ a) _ =
   1.183 +    	(case get_t y e1 a of NONE => get_t y e2 a | la => la)
   1.184 +      | get_t y (Const ("Script.While",_) $ c $ e) a = get_t y e a
   1.185 +      | get_t y (Const ("Script.While",_) $ c $ e $ a) _ = get_t y e a
   1.186 +      | get_t y (Const ("Script.Letpar",_) $ e1 $ Abs (_,_,e2)) a = 
   1.187 +    	(case get_t y e1 a of NONE => get_t y e2 a | la => la)
   1.188 +    (*| get_t y (Const ("Let",_) $ e1 $ Abs (_,_,e2)) a =
   1.189 +    	(writeln("get_t: Let e1= "^(term2str e1)^", e2= "^(term2str e2));
   1.190 +	 case get_t y e1 a of NONE => get_t y e2 a | la => la)
   1.191 +      | get_t y (Abs (_,_,e)) a = get_t y e a*)
   1.192 +      | get_t y (Const ("Let",_) $ e1 $ Abs (_,_,e2)) a =
   1.193 +    	get_t y e1 a (*don't go deeper without evaluation !*)
   1.194 +      | get_t y (Const ("If",_) $ c $ e1 $ e2) a = NONE
   1.195 +    	(*(case get_t y e1 a of NONE => get_t y e2 a | la => la)*)
   1.196 +    
   1.197 +      | get_t y (Const ("Script.Rewrite",_) $ _ $ _ $ a) _ = SOME a
   1.198 +      | get_t y (Const ("Script.Rewrite",_) $ _ $ _    ) a = SOME a
   1.199 +      | get_t y (Const ("Script.Rewrite'_Inst",_) $ _ $ _ $ _ $ a) _ = SOME a
   1.200 +      | get_t y (Const ("Script.Rewrite'_Inst",_) $ _ $ _ $ _ )    a = SOME a
   1.201 +      | get_t y (Const ("Script.Rewrite'_Set",_) $ _ $ _ $ a) _ = SOME a
   1.202 +      | get_t y (Const ("Script.Rewrite'_Set",_) $ _ $ _ )    a = SOME a
   1.203 +      | get_t y (Const ("Script.Rewrite'_Set'_Inst",_) $ _ $ _ $ _ $a)_ =SOME a
   1.204 +      | get_t y (Const ("Script.Rewrite'_Set'_Inst",_) $ _ $ _ $ _ )  a =SOME a
   1.205 +      | get_t y (Const ("Script.Calculate",_) $ _ $ a) _ = SOME a
   1.206 +      | get_t y (Const ("Script.Calculate",_) $ _ )    a = SOME a
   1.207 +    
   1.208 +      | get_t y (Const ("Script.Substitute",_) $ _ $ a) _ = SOME a
   1.209 +      | get_t y (Const ("Script.Substitute",_) $ _ )    a = SOME a
   1.210 +    
   1.211 +      | get_t y (Const ("Script.SubProblem",_) $ _ $ _) _ = NONE
   1.212 +
   1.213 +      | get_t y x _ =  
   1.214 +	((*writeln ("### get_t yac: list-expr "^(term2str x));*)
   1.215 +	 NONE)
   1.216 +in get_t thy body e_term end;
   1.217 +    
   1.218 +(*FIXME: get 1st stac by next_stac [] instead of ... ?? 29.7.02*)
   1.219 +(* val Script sc = scr;
   1.220 +   *)
   1.221 +fun init_form thy (Script sc) env =
   1.222 +  (case get_stac thy sc of
   1.223 +     NONE => NONE (*raise error ("init_form: no 1st stac in "^
   1.224 +			  (Syntax.string_of_term (thy2ctxt thy) sc))*)
   1.225 +   | SOME stac => SOME (subst_atomic env stac))
   1.226 +  | init_form _ _ _ = raise error "init_form: no match";
   1.227 +
   1.228 +(* use"ME/script.sml";
   1.229 +   use"script.sml";
   1.230 +   *)
   1.231 +
   1.232 +
   1.233 +
   1.234 +(*the 'iteration-argument' of a stac (args not eval)*)
   1.235 +fun itr_arg _ (Const ("Script.Rewrite'_Inst",_) $ _ $ _ $ _ $ v) = v
   1.236 +  | itr_arg _ (Const ("Script.Rewrite",_) $ _ $ _ $ v) = v
   1.237 +  | itr_arg _ (Const ("Script.Rewrite'_Set'_Inst",_) $ _ $ _ $ _ $ v) = v
   1.238 +  | itr_arg _ (Const ("Script.Rewrite'_Set",_) $ _ $ _ $ v) = v
   1.239 +  | itr_arg _ (Const ("Script.Calculate",_) $ _ $ v) = v
   1.240 +  | itr_arg _ (Const ("Script.Check'_elementwise",_) $ consts $ _) = consts
   1.241 +  | itr_arg _ (Const ("Script.Or'_to'_List",_) $ _) = e_term
   1.242 +  | itr_arg _ (Const ("Script.Tac",_) $ _) = e_term
   1.243 +  | itr_arg _ (Const ("Script.SubProblem",_) $ _ $ _) = e_term
   1.244 +  | itr_arg thy t = raise error 
   1.245 +    ("itr_arg not impl. for "^
   1.246 +     (Syntax.string_of_term (thy2ctxt (assoc_thy thy)) t));
   1.247 +(* val t = (term_of o the o (parse thy))"Rewrite rroot_square_inv False e_";
   1.248 +> itr_arg "Script.thy" t;
   1.249 +val it = Free ("e_","RealDef.real") : term 
   1.250 +> val t = (term_of o the o (parse thy))"xxx";
   1.251 +> itr_arg "Script.thy" t;
   1.252 +*** itr_arg not impl. for xxx
   1.253 +uncaught exception ERROR
   1.254 +  raised at: library.ML:1114.35-1114.40*)
   1.255 +
   1.256 +
   1.257 +(*.get the arguments of the script out of the scripts parsetree.*)
   1.258 +fun formal_args scr = (fst o split_last o snd o strip_comb) scr;
   1.259 +(*
   1.260 +> formal_args scr;
   1.261 +  [Free ("f_","RealDef.real"),Free ("v_","RealDef.real"),
   1.262 +   Free ("eqs_","bool List.list")] : term list
   1.263 +*)
   1.264 +
   1.265 +(*.get the identifier of the script out of the scripts parsetree.*)
   1.266 +fun id_of_scr sc = (id_of o fst o strip_comb) sc;
   1.267 +
   1.268 +
   1.269 +(*WN020526: not clear, when a is available in ass_up for eva-_true*)
   1.270 +(*WN060906: in "fun handle_leaf" eg. uses "SOME M__"(from some PREVIOUS
   1.271 +  curried Rewrite) for CURRENT value (which may be different from PREVIOUS);
   1.272 +  thus "NONE" must be set at the end of currying (ill designed anyway)*)
   1.273 +fun upd_env_opt env (SOME a, v) = upd_env env (a,v)
   1.274 +  | upd_env_opt env (NONE, v) = 
   1.275 +    (writeln("*** upd_env_opt: (NONE,"^(term2str v)^")");env);
   1.276 +
   1.277 +
   1.278 +type dsc = typ; (*<-> nam..unknow in Descript.thy*)
   1.279 +fun typ_str (Type (s,_)) = s
   1.280 +  | typ_str (TFree(s,_)) = s
   1.281 +  | typ_str (TVar ((s,i),_)) = s^(string_of_int i);
   1.282 +	     
   1.283 +(*get the _result_-type of a description*)
   1.284 +fun dsc_valT (Const (_,(Type (_,[_,T])))) = (strip_thy o typ_str) T;
   1.285 +(*> val t = (term_of o the o (parse thy)) "equality";
   1.286 +> val T = type_of t;
   1.287 +val T = "bool => Tools.una" : typ
   1.288 +> val dsc = dsc_valT t;
   1.289 +val dsc = "una" : string
   1.290 +
   1.291 +> val t = (term_of o the o (parse thy)) "fixedValues";
   1.292 +> val T = type_of t;
   1.293 +val T = "bool List.list => Tools.nam" : typ
   1.294 +> val dsc = dsc_valT t;
   1.295 +val dsc = "nam" : string*)
   1.296 +
   1.297 +(*.from penv in itm_ make args for script depending on type of description.*)
   1.298 +(*6.5.03 TODO: push penv into script -- and drop mk_arg here || drop penv
   1.299 +  9.5.03 penv postponed: penv = env for script at the moment, (*mk_arg*)*)
   1.300 +fun mk_arg thy d [] = raise error ("mk_arg: no data for "^
   1.301 +			       (Syntax.string_of_term (thy2ctxt thy) d))
   1.302 +  | mk_arg thy d [t] = 
   1.303 +    (case dsc_valT d of
   1.304 +	 "una" => [t]
   1.305 +       | "nam" => 
   1.306 +	 [case t of
   1.307 +	      r as (Const ("op =",_) $ _ $ _) => r
   1.308 +	    | _ => raise error 
   1.309 +			     ("mk_arg: dsc-typ 'nam' applied to non-equality "^
   1.310 +			      (Syntax.string_of_term (thy2ctxt thy) t))]
   1.311 +       | s => raise error ("mk_arg: not impl. for "^s))
   1.312 +    
   1.313 +  | mk_arg thy d (t::ts) = (mk_arg thy d [t]) @ (mk_arg thy d ts);
   1.314 +(* 
   1.315 + val d = d_in itm_;
   1.316 + val [t] = ts_in itm_;
   1.317 +mk_arg thy
   1.318 +*)
   1.319 +
   1.320 +
   1.321 +
   1.322 +
   1.323 +(*.create the actual parameters (args) of script: their order 
   1.324 +  is given by the order in met.pat .*)
   1.325 +(*WN.5.5.03: ?: does this allow for different descriptions ???
   1.326 +             ?: why not taken from formal args of script ???
   1.327 +!: FIXXXME penv: push it here in itms2args into script-evaluation*)
   1.328 +(* val (thy, mI, itms) = (thy, metID, itms);
   1.329 +   *)
   1.330 +fun itms2args thy mI (itms:itm list) =
   1.331 +    let val mvat = max_vt itms
   1.332 +	fun okv mvat (_,vats,b,_,_) = member op = vats mvat andalso b
   1.333 +	val itms = filter (okv mvat) itms
   1.334 +	fun test_dsc d (_,_,_,_,itm_) = (d = d_in itm_)
   1.335 +	fun itm2arg itms (_,(d,_)) =
   1.336 +	    case find_first (test_dsc d) itms of
   1.337 +		NONE => 
   1.338 +		raise error ("itms2args: '"^term2str d^"' not in itms")
   1.339 +	      (*| SOME (_,_,_,_,itm_) => mk_arg thy (d_in itm_) (ts_in itm_);
   1.340 +               penv postponed; presently penv holds already env for script*)
   1.341 +	      | SOME (_,_,_,_,itm_) => penvval_in itm_
   1.342 +	fun sel_given_find (s,_) = (s = "#Given") orelse (s = "#Find")
   1.343 +	val pats = (#ppc o get_met) mI
   1.344 +    in (flat o (map (itm2arg itms))) pats end;
   1.345 +(*
   1.346 +> val sc = ... Solve_root_equation ...
   1.347 +> val mI = ("Script.thy","sqrt-equ-test");
   1.348 +> val PblObj{meth={ppc=itms,...},...} = get_obj I pt [];
   1.349 +> val ts = itms2args thy mI itms;
   1.350 +> map (Syntax.string_of_term (thy2ctxt thy)) ts;
   1.351 +["sqrt (#9 + #4 * x) = sqrt x + sqrt (#5 + x)","x","#0"] : string list
   1.352 +*)
   1.353 +
   1.354 +
   1.355 +(*["bool_ (1+x=2)","real_ x"] --match_ags--> oris 
   1.356 +  --oris2fmz_vals--> ["equality (1+x=2)","boundVariable x","solutions L"]*)
   1.357 +fun oris2fmz_vals oris =
   1.358 +    let fun ori2fmz_vals ((_,_,_,dsc,ts):ori) = 
   1.359 +	    ((term2str o comp_dts') (dsc, ts), last_elem ts) 
   1.360 +	    handle _ => raise error ("ori2fmz_env called with "^terms2str ts)
   1.361 +    in (split_list o (map ori2fmz_vals)) oris end;
   1.362 +
   1.363 +(*detour necessary, because generate1 delivers a string-result*)
   1.364 +fun mout2term thy (Form' (FormKF (_,_,_,_,res))) = 
   1.365 +  (term_of o the o (parse (assoc_thy thy))) res
   1.366 +  | mout2term thy (Form' (PpcKF _)) = e_term;(*3.8.01: res of subpbl 
   1.367 +					   at time of detection in script*)
   1.368 +
   1.369 +(*.convert a script-tac 'stac' to a tactic 'tac'; if stac is an initac,
   1.370 +   then convert to a 'tac_' (as required in appy).
   1.371 +   arg pt:ptree for pushing the thy specified in rootpbl into subpbls.*)
   1.372 +fun stac2tac_ pt thy (Const ("Script.Rewrite",_) $ Free (thmID,_) $ _ $ f) =
   1.373 +(* val (pt, thy, (Const ("Script.Rewrite",_) $ Free (thmID,_) $ _ $ f)) = 
   1.374 +       (pt, (assoc_thy th), stac);
   1.375 +   *)
   1.376 +    let val tid = (de_esc_underscore o strip_thy) thmID
   1.377 +    in (Rewrite (tid, (string_of_thmI o 
   1.378 +		       (assoc_thm' thy)) (tid,"")), Empty_Tac_) end
   1.379 +(* val (thy,
   1.380 +	mm as(Const ("Script.Rewrite'_Inst",_) $  sub $ Free(thmID,_) $ _ $ f))
   1.381 +     = (assoc_thy th,stac);
   1.382 +   stac2tac_ pt thy mm;
   1.383 +
   1.384 +   assoc_thm' (assoc_thy "Isac.thy") (tid,"");
   1.385 +   assoc_thm' Isac.thy (tid,"");
   1.386 +   *)
   1.387 +  | stac2tac_ pt thy (Const ("Script.Rewrite'_Inst",_) $ 
   1.388 +	       sub $ Free (thmID,_) $ _ $ f) =
   1.389 +  let val subML = ((map isapair2pair) o isalist2list) sub
   1.390 +    val subStr = subst2subs subML
   1.391 +    val tid = (de_esc_underscore o strip_thy) thmID (*4.10.02 unnoetig*)
   1.392 +  in (Rewrite_Inst 
   1.393 +	  (subStr, (tid, (string_of_thmI o
   1.394 +			  (assoc_thm' thy)) (tid,""))), Empty_Tac_) end
   1.395 +      
   1.396 +  | stac2tac_ pt thy (Const ("Script.Rewrite'_Set",_) $ Free (rls,_) $ _ $ f)=
   1.397 +  (Rewrite_Set ((de_esc_underscore o strip_thy) rls), Empty_Tac_)
   1.398 +
   1.399 +  | stac2tac_ pt thy (Const ("Script.Rewrite'_Set'_Inst",_) $ 
   1.400 +	       sub $ Free (rls,_) $ _ $ f) =
   1.401 +  let val subML = ((map isapair2pair) o isalist2list) sub;
   1.402 +    val subStr = subst2subs subML;
   1.403 +  in (Rewrite_Set_Inst (subStr,rls), Empty_Tac_) end
   1.404 +
   1.405 +  | stac2tac_ pt thy (Const ("Script.Calculate",_) $ Free (op_,_) $ f) =
   1.406 +  (Calculate op_, Empty_Tac_)
   1.407 +
   1.408 +  | stac2tac_ pt thy (Const ("Script.Take",_) $ t) =
   1.409 +  (Take (term2str t), Empty_Tac_)
   1.410 +
   1.411 +  | stac2tac_ pt thy (Const ("Script.Substitute",_) $ isasub $ arg) =
   1.412 +  (Substitute ((subte2sube o isalist2list) isasub), Empty_Tac_)
   1.413 +(* val t = str2term"Substitute [x = L, M_b L = 0] (M_b x = q_0 * x + c)";
   1.414 +   val Const ("Script.Substitute", _) $ isasub $ arg = t;
   1.415 +   *)
   1.416 +
   1.417 +(*12.1.01.*)
   1.418 +  | stac2tac_ pt thy (Const("Script.Check'_elementwise",_) $ _ $ 
   1.419 +		    (set as Const ("Collect",_) $ Abs (_,_,pred))) = 
   1.420 +  (Check_elementwise (Syntax.string_of_term (thy2ctxt thy) pred), 
   1.421 +   (*set*)Empty_Tac_)
   1.422 +
   1.423 +  | stac2tac_ pt thy (Const("Script.Or'_to'_List",_) $ _ ) = 
   1.424 +  (Or_to_List, Empty_Tac_)
   1.425 +
   1.426 +(*12.1.01.for subproblem_equation_dummy in root-equation *)
   1.427 +  | stac2tac_ pt thy (Const ("Script.Tac",_) $ Free (str,_)) = 
   1.428 +  (Tac ((de_esc_underscore o strip_thy) str),  Empty_Tac_) 
   1.429 +		    (*L_ will come from pt in appl_in*)
   1.430 +
   1.431 +  (*3.12.03 copied from assod SubProblem*)
   1.432 +(* val Const ("Script.SubProblem",_) $
   1.433 +			 (Const ("Pair",_) $
   1.434 +				Free (dI',_) $ 
   1.435 +				(Const ("Pair",_) $ pI' $ mI')) $ ags' =
   1.436 +    str2term 
   1.437 +    "SubProblem (EqSystem_, [linear, system], [no_met])\
   1.438 +    \            [bool_list_ [c_2 = 0, L * c + c_2 = q_0 * L ^^^ 2 / 2],\
   1.439 +    \             real_list_ [c, c_2]]";
   1.440 +*)
   1.441 +  | stac2tac_ pt thy (stac as Const ("Script.SubProblem",_) $
   1.442 +			 (Const ("Pair",_) $
   1.443 +				Free (dI',_) $ 
   1.444 +			(Const ("Pair",_) $ pI' $ mI')) $ ags') =
   1.445 +(*compare "| assod _ (Subproblem'"*)
   1.446 +    let val dI = ((implode o drop_last(*.._*) o explode) dI')^".thy";
   1.447 +        val thy = maxthy (assoc_thy dI) (rootthy pt);
   1.448 +	val pI = ((map (de_esc_underscore o free2str)) o isalist2list) pI';
   1.449 +	val mI = ((map (de_esc_underscore o free2str)) o isalist2list) mI';
   1.450 +	val ags = isalist2list ags';
   1.451 +	val (pI, pors, mI) = 
   1.452 +	    if mI = ["no_met"] 
   1.453 +	    then let val pors = (match_ags thy ((#ppc o get_pbt) pI) ags)
   1.454 +			 handle _ =>(match_ags_msg pI stac ags(*raise exn*);[])
   1.455 +		     val pI' = refine_ori' pors pI;
   1.456 +		 in (pI', pors (*refinement over models with diff.prec only*), 
   1.457 +		     (hd o #met o get_pbt) pI') end
   1.458 +	    else (pI, (match_ags thy ((#ppc o get_pbt) pI) ags)
   1.459 +		  handle _ => (match_ags_msg pI stac ags(*raise exn*); []), 
   1.460 +		  mI);
   1.461 +        val (fmz_, vals) = oris2fmz_vals pors;
   1.462 +	val {cas,ppc,thy,...} = get_pbt pI
   1.463 +	val dI = theory2theory' thy (*.take dI from _refined_ pbl.*)
   1.464 +	val dI = theory2theory' (maxthy (assoc_thy dI) (rootthy pt));
   1.465 +	val hdl = case cas of
   1.466 +		      NONE => pblterm dI pI
   1.467 +		    | SOME t => subst_atomic ((vars_of_pbl_' ppc) ~~~ vals) t
   1.468 +        val f = subpbl (strip_thy dI) pI
   1.469 +    in (Subproblem (dI, pI),
   1.470 +	Subproblem' ((dI, pI, mI), pors, hdl, fmz_, f))
   1.471 +    end
   1.472 +
   1.473 +  | stac2tac_ pt thy t = raise error 
   1.474 +  ("stac2tac_ TODO: no match for "^
   1.475 +   (Syntax.string_of_term (thy2ctxt thy) t));
   1.476 +(*
   1.477 +> val t = (term_of o the o (parse thy)) 
   1.478 + "Rewrite_Set_Inst [(bdv,v_::real)] isolate_bdv False (x=a+#1)";
   1.479 +> stac2tac_ pt t;
   1.480 +val it = Rewrite_Set_Inst ([(#,#)],"isolate_bdv") : tac
   1.481 +
   1.482 +> val t = (term_of o the o (parse SqRoot.thy)) 
   1.483 +"(SubProblem (SqRoot_,[equation,univariate],(SqRoot_,solve_linear))\
   1.484 +   \         [bool_ e_, real_ v_])::bool list";
   1.485 +> stac2tac_ pt SqRoot.thy t;
   1.486 +val it = (Subproblem ("SqRoot.thy",[#,#]),Const (#,#) $ (# $ # $ (# $ #)))
   1.487 +*)
   1.488 +
   1.489 +fun stac2tac pt thy t = (fst o stac2tac_ pt thy) t;
   1.490 +
   1.491 +
   1.492 +
   1.493 +
   1.494 +(*test a term for being a _list_ (set ?) of constants; could be more rigorous*)
   1.495 +fun list_of_consts (Const ("List.list.Cons",_) $ _ $ _) = true
   1.496 +  | list_of_consts (Const ("List.list.Nil",_)) = true
   1.497 +  | list_of_consts _ = false;
   1.498 +(*val ttt = (term_of o the o (parse thy)) "[x=#1,x=#2,x=#3]";
   1.499 +> list_of_consts ttt;
   1.500 +val it = true : bool
   1.501 +> val ttt = (term_of o the o (parse thy)) "[]";
   1.502 +> list_of_consts ttt;
   1.503 +val it = true : bool*)
   1.504 +
   1.505 +
   1.506 +
   1.507 +
   1.508 +
   1.509 +(* 15.1.01: evaluation of preds only works occasionally,
   1.510 +            but luckily for the 2 examples of root-equ:
   1.511 +> val s = ((term_of o the o (parse thy)) "x",
   1.512 +	   (term_of o the o (parse thy)) "-#5//#12");
   1.513 +> val asm = (term_of o the o (parse thy)) 
   1.514 +             "#0 <= #9 + #4 * x  &  #0 <= sqrt x + sqrt (#-3 + x)";
   1.515 +> val pred = subst_atomic [s] asm;
   1.516 +> rewrite_set_ thy false ((cterm_of thy) pred);
   1.517 +val it = NONE : (cterm * cterm list) option !!!!!!!!!!!!!!!!!!!!!!!!!!!!
   1.518 +> eval_true' (string_of_thy thy) "eval_rls" (subst_atomic [s] pred);
   1.519 +val it = false : bool
   1.520 +
   1.521 +> val s = ((term_of o the o (parse thy)) "x",
   1.522 +	   (term_of o the o (parse thy)) "#4");
   1.523 +> val asm = (term_of o the o (parse thy)) 
   1.524 +             "#0 <= #9 + #4 * x  &  #0 <= sqrt x + sqrt (#5 + x)";
   1.525 +> val pred = subst_atomic [s] asm;
   1.526 +> rewrite_set_ thy false ((cterm_of thy) pred);
   1.527 +val it = SOME ("True & True",[]) : (cterm * cterm list) option
   1.528 +> eval_true' (string_of_thy thy) "eval_rls" (subst_atomic [s] pred);
   1.529 +val it = true : bool`*)
   1.530 +
   1.531 +(*for check_elementwise: take apart the set, ev. instantiate assumptions
   1.532 +fun rep_set thy pt p (set as Const ("Collect",_) $ Abs _) =
   1.533 +  let val (_ $ Abs (bdv,T,pred)) = inst_abs thy set;
   1.534 +    val bdv = Free (bdv,T);
   1.535 +    val pred = if pred <> Const ("Script.Assumptions",bool)
   1.536 +		 then pred 
   1.537 +	       else (mk_and o (map fst)) (get_assumptions_ pt (p,Res))
   1.538 +  in (bdv, pred) end
   1.539 +  | rep_set thy _ _ set = 
   1.540 +    raise error ("check_elementwise: no set "^ (*from script*)
   1.541 +		 (Syntax.string_of_term (thy2ctxt thy) set));
   1.542 +(*> val set = (term_of o the o (parse thy)) "{(x::real). Assumptions}";
   1.543 +> val p = [];
   1.544 +> val pt = union_asm pt p [("#0 <= sqrt x + sqrt (#5 + x)",[11]),
   1.545 +                           ("#0 <= #9 + #4 * x",[22]),
   1.546 +			   ("#0 <= x ^^^ #2 + #5 * x",[33]),
   1.547 +			   ("#0 <= #2 + x",[44])];
   1.548 +> val (bdv,pred) = rep_set thy pt p set;
   1.549 +val bdv = Free ("x","RealDef.real") : term
   1.550 +> writeln (Syntax.string_of_term (thy2ctxt thy) pred);
   1.551 +((#0 <= sqrt x + sqrt (#5 + x) & #0 <= #9 + #4 * x) &
   1.552 + #0 <= x ^^^ #2 + #5 * x) &
   1.553 +#0 <= #2 + x
   1.554 +*)
   1.555 +--------------------------------------------11.6.03--was unused*)
   1.556 +
   1.557 +
   1.558 +
   1.559 +
   1.560 +datatype ass = 
   1.561 +  Ass of tac_ *  (*SubProblem gets args instantiated in assod*)
   1.562 +	 term      (*for itr_arg,result in ets*)
   1.563 +| AssWeak of tac_ *
   1.564 +	     term  (*for itr_arg,result in ets*)
   1.565 +| NotAss;
   1.566 +
   1.567 +(*.assod: tac_ associated with stac w.r.t. d
   1.568 +args
   1.569 + pt:ptree for pushing the thy specified in rootpbl into subpbls
   1.570 +returns
   1.571 + Ass    : associated: e.g. thmID in stac = thmID in m
   1.572 +                       +++ arg   in stac = arg   in m
   1.573 + AssWeak: weakly ass.:e.g. thmID in stac = thmID in m, //arg//
   1.574 + NotAss :             e.g. thmID in stac/=/thmID in m (not =)
   1.575 +8.01:
   1.576 + tac_ SubProblem with args completed from script
   1.577 +.*)
   1.578 +fun assod pt d (m as Rewrite_Inst' (thy',rod,rls,put,subs,(thmID,thm),f,(f',asm))) stac =
   1.579 +    (case stac of
   1.580 +	 (Const ("Script.Rewrite'_Inst",_) $ subs_ $ Free (thmID_,idT) $b$f_)=>
   1.581 +	 if thmID = thmID_ then 
   1.582 +	     if f = f_ then ((*writeln"3### assod ..Ass";*)Ass (m,f')) 
   1.583 +	     else ((*writeln"3### assod ..AssWeak";*)AssWeak(m, f'))
   1.584 +	 else ((*writeln"3### assod ..NotAss";*)NotAss)
   1.585 +       | (Const ("Script.Rewrite'_Set'_Inst",_) $ sub_ $ Free (rls_,_) $_$f_)=>
   1.586 +	 if contains_rule (Thm (thmID, refl(*dummy*))) (assoc_rls rls_) then 
   1.587 +	     if f = f_ then Ass (m,f') else AssWeak (m,f')
   1.588 +	 else NotAss
   1.589 +       | _ => NotAss)
   1.590 +
   1.591 +  | assod pt d (m as Rewrite' (thy,rod,rls,put,(thmID,thm),f,(f',asm))) stac =
   1.592 +    (case stac of
   1.593 +	 (t as Const ("Script.Rewrite",_) $ Free (thmID_,idT) $ b $ f_) =>
   1.594 +	 ((*writeln("3### assod: stac = "^
   1.595 +		    (Syntax.string_of_term (thy2ctxt (assoc_thy thy)) t));
   1.596 +	   writeln("3### assod: f(m)= "^
   1.597 +		   (Syntax.string_of_term (thy2ctxt (assoc_thy thy)) f));*)
   1.598 +	  if thmID = thmID_ then 
   1.599 +	      if f = f_ then ((*writeln"3### assod ..Ass";*)Ass (m,f')) 
   1.600 +	      else ((*writeln"### assod ..AssWeak";
   1.601 +		     writeln("### assod: f(m)  = "^
   1.602 +			     (Sign.string_of_term (sign_of(assoc_thy thy)) f));
   1.603 +		     writeln("### assod: f(stac)= "^
   1.604 +			     (Sign.string_of_term(sign_of(assoc_thy thy))f_))*)
   1.605 +		    AssWeak (m,f'))
   1.606 +	  else ((*writeln"3### assod ..NotAss";*)NotAss))
   1.607 +       | (Const ("Script.Rewrite'_Set",_) $ Free (rls_,_) $ _ $ f_) =>
   1.608 +	 if contains_rule (Thm (thmID, refl(*dummy*))) (assoc_rls rls_) then
   1.609 +	      if f = f_ then Ass (m,f') else AssWeak (m,f')
   1.610 +	  else NotAss
   1.611 +       | _ => NotAss)
   1.612 +
   1.613 +(*val f = (term_of o the o (parse thy))"#0+(sqrt(sqrt(sqrt a))^^^#2)^^^#2=#0";
   1.614 +> val f'= (term_of o the o (parse thy))"#0+(sqrt(sqrt a))^^^#2=#0";
   1.615 +> val m =   Rewrite'("Script.thy","tless_true","eval_rls",false,
   1.616 + ("rroot_square_inv",""),f,(f',[]));
   1.617 +> val stac = (term_of o the o (parse thy))
   1.618 + "Rewrite rroot_square_inv False (#0+(sqrt(sqrt(sqrt a))^^^#2)^^^#2=#0)";
   1.619 +> assod e_rls m stac;
   1.620 +val it =
   1.621 +  (SOME (Rewrite' (#,#,#,#,#,#,#)),Const ("empty","RealDef.real"),
   1.622 +   Const ("empty","RealDef.real")) : tac_ option * term * term*)
   1.623 +
   1.624 +  | assod pt d (m as Rewrite_Set_Inst' (thy',put,sub,rls,f,(f',asm))) 
   1.625 +  (Const ("Script.Rewrite'_Set'_Inst",_) $ sub_ $ Free (rls_,_) $ _ $ f_)= 
   1.626 +  if id_rls rls = rls_ then 
   1.627 +    if f = f_ then Ass (m,f') else AssWeak (m,f')
   1.628 +  else NotAss
   1.629 +
   1.630 +  | assod pt d (m as Detail_Set_Inst' (thy',put,sub,rls,f,(f',asm))) 
   1.631 +  (Const ("Script.Rewrite'_Set'_Inst",_) $ sub_ $ Free (rls_,_) $ _ $ f_)= 
   1.632 +  if id_rls rls = rls_ then 
   1.633 +    if f = f_ then Ass (m,f') else AssWeak (m,f')
   1.634 +  else NotAss
   1.635 +
   1.636 +  | assod pt d (m as Rewrite_Set' (thy,put,rls,f,(f',asm))) 
   1.637 +  (Const ("Script.Rewrite'_Set",_) $ Free (rls_,_) $ _ $ f_) = 
   1.638 +  if id_rls rls = rls_ then 
   1.639 +    if f = f_ then Ass (m,f') else AssWeak (m,f')
   1.640 +  else NotAss
   1.641 +
   1.642 +  | assod pt d (m as Detail_Set' (thy,put,rls,f,(f',asm))) 
   1.643 +  (Const ("Script.Rewrite'_Set",_) $ Free (rls_,_) $ _ $ f_) = 
   1.644 +  if id_rls rls = rls_ then 
   1.645 +    if f = f_ then Ass (m,f') else AssWeak (m,f')
   1.646 +  else NotAss
   1.647 +
   1.648 +  | assod pt d (m as Calculate' (thy',op_,f,(f',thm'))) stac =
   1.649 +    (case stac of
   1.650 +	 (Const ("Script.Calculate",_) $ Free (op__,_) $ f_) =>
   1.651 +	 if op_ = op__ then
   1.652 +	     if f = f_ then Ass (m,f') else AssWeak (m,f')
   1.653 +	 else NotAss
   1.654 +       | (Const ("Script.Rewrite'_Set'_Inst",_) $ sub_ $ Free(rls_,_) $_$f_)=> 
   1.655 +	 if contains_rule (Calc (snd (assoc1 (!calclist', op_)))) 
   1.656 +			  (assoc_rls rls_) then
   1.657 +	     if f = f_ then Ass (m,f') else AssWeak (m,f')
   1.658 +	 else NotAss
   1.659 +       | (Const ("Script.Rewrite'_Set",_) $ Free (rls_, _) $ _ $ f_) =>
   1.660 +	 if contains_rule (Calc (snd (assoc1 (!calclist', op_)))) 
   1.661 +			  (assoc_rls rls_) then
   1.662 +	     if f = f_ then Ass (m,f') else AssWeak (m,f')
   1.663 +	 else NotAss
   1.664 +       | _ => NotAss)
   1.665 +
   1.666 +  | assod pt _ (m as Check_elementwise' (consts,_,(consts_chkd,_)))
   1.667 +    (Const ("Script.Check'_elementwise",_) $ consts' $ _) =
   1.668 +    ((*writeln("### assod Check'_elementwise: consts= "^(term2str consts)^
   1.669 +	     ", consts'= "^(term2str consts'));
   1.670 +     atomty consts; atomty consts';*)
   1.671 +     if consts = consts' then ((*writeln"### assod Check'_elementwise: Ass";*)
   1.672 +			       Ass (m, consts_chkd))
   1.673 +     else ((*writeln"### assod Check'_elementwise: NotAss";*) NotAss))
   1.674 +
   1.675 +  | assod pt _ (m as Or_to_List' (ors, list)) 
   1.676 +	  (Const ("Script.Or'_to'_List",_) $ _) =
   1.677 +	  Ass (m, list) 
   1.678 +
   1.679 +  | assod pt _ (m as Take' term) 
   1.680 +	  (Const ("Script.Take",_) $ _) =
   1.681 +	  Ass (m, term)
   1.682 +
   1.683 +  | assod pt _ (m as Substitute' (_, _, res)) 
   1.684 +	  (Const ("Script.Substitute",_) $ _ $ _) =
   1.685 +	  Ass (m, res) 
   1.686 +(* val t = str2term "Substitute [(x, 3)] (x^^^2 + x + 1)";
   1.687 +   val (Const ("Script.Substitute",_) $ _ $ _) = t;
   1.688 +   *)
   1.689 +
   1.690 +  | assod pt _ (m as Tac_ (thy,f,id,f'))  
   1.691 +    (Const ("Script.Tac",_) $ Free (id',_)) =
   1.692 +    if id = id' then Ass (m, ((term_of o the o (parse thy)) f'))
   1.693 +    else NotAss
   1.694 +
   1.695 +
   1.696 +(* val t = str2term 
   1.697 +              "SubProblem (DiffApp_,[make,function],[no_met]) \
   1.698 +	      \[real_ m_, real_ v_, bool_list_ rs_]";
   1.699 +
   1.700 + val (Subproblem' ((domID,pblID,metID),_,_,_,f)) = m;
   1.701 + val (Const ("Script.SubProblem",_) $
   1.702 +		 (Const ("Pair",_) $
   1.703 +			Free (dI',_) $
   1.704 +			(Const ("Pair",_) $ pI' $ mI')) $ ags') = stac;
   1.705 + *)
   1.706 +  | assod pt _ (Subproblem' ((domID,pblID,metID),_,_,_,f))
   1.707 +	  (stac as Const ("Script.SubProblem",_) $
   1.708 +		 (Const ("Pair",_) $
   1.709 +			Free (dI',_) $ 
   1.710 +			(Const ("Pair",_) $ pI' $ mI')) $ ags') =
   1.711 +(*compare "| stac2tac_ thy (Const ("Script.SubProblem",_)"*)
   1.712 +    let val dI = ((implode o drop_last o explode) dI')^".thy";
   1.713 +        val thy = maxthy (assoc_thy dI) (rootthy pt);
   1.714 +	val pI = ((map (de_esc_underscore o free2str)) o isalist2list) pI';
   1.715 +	val mI = ((map (de_esc_underscore o free2str)) o isalist2list) mI';
   1.716 +	val ags = isalist2list ags';
   1.717 +	val (pI, pors, mI) = 
   1.718 +	    if mI = ["no_met"] 
   1.719 +	    then let val pors = (match_ags thy ((#ppc o get_pbt) pI) ags)
   1.720 +			 handle _=>(match_ags_msg pI stac ags(*raise exn*);[]);
   1.721 +		     val pI' = refine_ori' pors pI;
   1.722 +		 in (pI', pors (*refinement over models with diff.prec only*), 
   1.723 +		     (hd o #met o get_pbt) pI') end
   1.724 +	    else (pI, (match_ags thy ((#ppc o get_pbt) pI) ags)
   1.725 +		      handle _ => (match_ags_msg pI stac ags(*raise exn*);[]), 
   1.726 +		  mI);
   1.727 +        val (fmz_, vals) = oris2fmz_vals pors;
   1.728 +	val {cas, ppc,...} = get_pbt pI
   1.729 +	val {cas, ppc, thy,...} = get_pbt pI
   1.730 +	val dI = theory2theory' thy (*take dI from _refined_ pbl*)
   1.731 +	val dI = theory2theory' (maxthy (assoc_thy dI) (rootthy pt))
   1.732 +	val hdl = case cas of
   1.733 +		      NONE => pblterm dI pI
   1.734 +		    | SOME t => subst_atomic ((vars_of_pbl_' ppc) ~~~ vals) t
   1.735 +        val f = subpbl (strip_thy dI) pI
   1.736 +    in if domID = dI andalso pblID = pI
   1.737 +       then Ass (Subproblem' ((dI, pI, mI), pors, hdl, fmz_, f), f) 
   1.738 +       else NotAss
   1.739 +    end
   1.740 +
   1.741 +  | assod pt d m t = 
   1.742 +    (if (!trace_script) 
   1.743 +     then writeln("@@@ the 'tac_' proposed to apply does NOT match the leaf found in the script:\n"^
   1.744 +		  "@@@ tac_ = "^(tac_2str m))
   1.745 +     else ();
   1.746 +     NotAss);
   1.747 +
   1.748 +
   1.749 +
   1.750 +fun tac_2tac (Refine_Tacitly' (pI,_,_,_,_)) = Refine_Tacitly pI
   1.751 +  | tac_2tac (Model_Problem' (pI,_,_))      = Model_Problem
   1.752 +  | tac_2tac (Add_Given' (t,_))             = Add_Given t
   1.753 +  | tac_2tac (Add_Find' (t,_))              = Add_Find t
   1.754 +  | tac_2tac (Add_Relation' (t,_))          = Add_Relation t
   1.755 + 
   1.756 +  | tac_2tac (Specify_Theory' dI)           = Specify_Theory dI
   1.757 +  | tac_2tac (Specify_Problem' (dI,_))      = Specify_Problem dI
   1.758 +  | tac_2tac (Specify_Method' (dI,_,_))     = Specify_Method dI
   1.759 +  
   1.760 +  | tac_2tac (Rewrite' (thy,rod,erls,put,(thmID,thm),f,(f',asm))) =
   1.761 +    Rewrite (thmID,thm)
   1.762 +
   1.763 +  | tac_2tac (Rewrite_Inst' (thy,rod,erls,put,sub,(thmID,thm),f,(f',asm)))=
   1.764 +    Rewrite_Inst (subst2subs sub,(thmID,thm))
   1.765 +
   1.766 +  | tac_2tac (Rewrite_Set' (thy,put,rls,f,(f',asm))) = 
   1.767 +    Rewrite_Set (id_rls rls)
   1.768 +
   1.769 +  | tac_2tac (Detail_Set' (thy,put,rls,f,(f',asm))) = 
   1.770 +    Detail_Set (id_rls rls)
   1.771 +
   1.772 +  | tac_2tac (Rewrite_Set_Inst' (thy,put,sub,rls,f,(f',asm))) = 
   1.773 +    Rewrite_Set_Inst (subst2subs sub,id_rls rls)
   1.774 +
   1.775 +  | tac_2tac (Detail_Set_Inst' (thy,put,sub,rls,f,(f',asm))) = 
   1.776 +    Detail_Set_Inst (subst2subs sub,id_rls rls)
   1.777 +
   1.778 +  | tac_2tac (Calculate' (thy,op_,t,(t',thm'))) = Calculate (op_)
   1.779 +
   1.780 +  | tac_2tac (Check_elementwise' (consts,pred,consts')) =
   1.781 +    Check_elementwise pred
   1.782 +
   1.783 +  | tac_2tac (Or_to_List' _) = Or_to_List
   1.784 +  | tac_2tac (Take' term) = Take (term2str term)
   1.785 +  | tac_2tac (Substitute' (subte, t, res)) = Substitute (subte2sube subte) 
   1.786 +
   1.787 +  | tac_2tac (Tac_ (_,f,id,f')) = Tac id
   1.788 +
   1.789 +  | tac_2tac (Subproblem' ((domID, pblID, _), _, _,_,_)) = 
   1.790 +		  Subproblem (domID, pblID)
   1.791 +  | tac_2tac (Check_Postcond' (pblID, _)) = 
   1.792 +		  Check_Postcond pblID
   1.793 +  | tac_2tac Empty_Tac_ = Empty_Tac
   1.794 +
   1.795 +  | tac_2tac m = 
   1.796 +  raise error ("tac_2tac: not impl. for "^(tac_2str m));
   1.797 +
   1.798 +
   1.799 +
   1.800 +
   1.801 +(** decompose tac_ to a rule and to (lhs,rhs)
   1.802 +    unly needed                            ~~~ **)
   1.803 +
   1.804 +val idT = Type ("Script.ID",[]);
   1.805 +(*val tt = (term_of o the o (parse thy)) "square_equation_left::ID";
   1.806 +type_of tt = idT;
   1.807 +val it = true : bool
   1.808 +*)
   1.809 +
   1.810 +fun make_rule thy t =
   1.811 +  let val ct = cterm_of thy (Trueprop $ t)
   1.812 +  in Thm (Syntax.string_of_term (thy2ctxt thy) (term_of ct), make_thm ct) end;
   1.813 +
   1.814 +(* val (Rewrite_Inst'(thy',rod,rls,put,subs,(thmID,thm),f,(f',asm)))=m;
   1.815 +   *)
   1.816 +(*decompose tac_ to a rule and to (lhs,rhs) for ets FIXME.12.03: obsolete!
   1.817 + NOTE.12.03: also used for msg 'not locatable' ?!: 'Subproblem' missing !!!
   1.818 +WN0508 only use in tac_2res, which uses only last return-value*)
   1.819 +fun rep_tac_ (Rewrite_Inst' 
   1.820 +		 (thy',rod,rls,put,subs,(thmID,thm),f,(f',asm))) = 
   1.821 +  let val fT = type_of f;
   1.822 +    val b = if put then HOLogic.true_const else HOLogic.false_const;
   1.823 +    val sT = (type_of o fst o hd) subs;
   1.824 +    val subs' = list2isalist (HOLogic.mk_prodT (sT, sT))
   1.825 +      (map HOLogic.mk_prod subs);
   1.826 +    val sT' = type_of subs';
   1.827 +    val lhs = Const ("Script.Rewrite'_Inst",[sT',idT,(*fT*)bool,fT] ---> fT) 
   1.828 +      $ subs' $ Free (thmID,idT) $ b $ f;
   1.829 +  in (((make_rule (assoc_thy thy')) o HOLogic.mk_eq) (lhs,f'),(lhs,f')) end
   1.830 +(*Fehlersuche 25.4.01
   1.831 +(a)----- als String zusammensetzen:
   1.832 +ML> Syntax.string_of_term (thy2ctxt thy)f; 
   1.833 +val it = "d_d x #4 + d_d x (x ^^^ #2 + #3 * x)" : string
   1.834 +ML> Syntax.string_of_term (thy2ctxt thy)f'; 
   1.835 +val it = "#0 + d_d x (x ^^^ #2 + #3 * x)" : string
   1.836 +ML> subs;
   1.837 +val it = [(Free ("bdv","RealDef.real"),Free ("x","RealDef.real"))] : subst
   1.838 +> val tt = (term_of o the o (parse thy))
   1.839 +  "(Rewrite_Inst[(bdv,x)]diff_const False(d_d x #4 + d_d x (x ^^^ #2 + #3 * x)))=(#0 + d_d x (x ^^^ #2 + #3 * x))";
   1.840 +> atomty tt;
   1.841 +ML> writeln(Syntax.string_of_term (thy2ctxt thy)tt); 
   1.842 +(Rewrite_Inst [(bdv,x)] diff_const False d_d x #4 + d_d x (x ^^^ #2 + #3 * x)) =
   1.843 + #0 + d_d x (x ^^^ #2 + #3 * x)
   1.844 +
   1.845 +(b)----- laut rep_tac_:
   1.846 +> val ttt=HOLogic.mk_eq (lhs,f');
   1.847 +> atomty ttt;
   1.848 +
   1.849 +
   1.850 +(*Fehlersuche 1-2Monate vor 4.01:*)
   1.851 +> val tt = (term_of o the o (parse thy))
   1.852 +  "Rewrite_Inst[(bdv,x)]square_equation_left True(x=#1+#2)";
   1.853 +> atomty tt;
   1.854 +
   1.855 +> val f = (term_of o the o (parse thy)) "x=#1+#2";
   1.856 +> val f' = (term_of o the o (parse thy)) "x=#3";
   1.857 +> val subs = [((term_of o the o (parse thy)) "bdv",
   1.858 +	       (term_of o the o (parse thy)) "x")];
   1.859 +> val sT = (type_of o fst o hd) subs;
   1.860 +> val subs' = list2isalist (HOLogic.mk_prodT (sT, sT))
   1.861 +			      (map HOLogic.mk_prod subs);
   1.862 +> val sT' = type_of subs';
   1.863 +> val lhs = Const ("Script.Rewrite'_Inst",[sT',idT,fT,fT] ---> fT) 
   1.864 +  $ subs' $ Free (thmID,idT) $ HOLogic.true_const $ f;
   1.865 +> lhs = tt;
   1.866 +val it = true : bool
   1.867 +> rep_tac_ (Rewrite_Inst' 
   1.868 +	       ("Script.thy","tless_true","eval_rls",false,subs,
   1.869 +		("square_equation_left",""),f,(f',[])));
   1.870 +*)
   1.871 +  | rep_tac_ (Rewrite' (thy',rod,rls,put,(thmID,thm),f,(f',asm)))=
   1.872 +  let 
   1.873 +    val fT = type_of f;
   1.874 +    val b = if put then HOLogic.true_const else HOLogic.false_const;
   1.875 +    val lhs = Const ("Script.Rewrite",[idT,HOLogic.boolT,fT] ---> fT)
   1.876 +      $ Free (thmID,idT) $ b $ f;
   1.877 +  in (((make_rule (assoc_thy thy')) o HOLogic.mk_eq) (lhs,f'),(lhs,f')) end
   1.878 +(* 
   1.879 +> val tt = (term_of o the o (parse thy)) (*____   ____..test*)
   1.880 +  "Rewrite square_equation_left True (x=#1+#2) = (x=#3)";
   1.881 +
   1.882 +> val f = (term_of o the o (parse thy)) "x=#1+#2";
   1.883 +> val f' = (term_of o the o (parse thy)) "x=#3";
   1.884 +> val Thm (id,thm) = 
   1.885 +  rep_tac_ (Rewrite' 
   1.886 +   ("Script.thy","tless_true","eval_rls",false,
   1.887 +    ("square_equation_left",""),f,(f',[])));
   1.888 +> val SOME ct = parse thy   
   1.889 +  "Rewrite square_equation_left True (x=#1+#2)"; 
   1.890 +> rewrite_ Script.thy tless_true eval_rls true thm ct;
   1.891 +val it = SOME ("x = #3",[]) : (cterm * cterm list) option
   1.892 +*)
   1.893 +  | rep_tac_ (Rewrite_Set_Inst' 
   1.894 +		 (thy',put,subs,rls,f,(f',asm))) =
   1.895 +    (e_rule, (e_term, f'))
   1.896 +(*WN050824: type error ...
   1.897 +  let val fT = type_of f;
   1.898 +    val sT = (type_of o fst o hd) subs;
   1.899 +    val subs' = list2isalist (HOLogic.mk_prodT (sT, sT))
   1.900 +      (map HOLogic.mk_prod subs);
   1.901 +    val sT' = type_of subs';
   1.902 +    val b = if put then HOLogic.true_const else HOLogic.false_const
   1.903 +    val lhs = Const ("Script.Rewrite'_Set'_Inst",
   1.904 +		     [sT',idT,fT,fT] ---> fT) 
   1.905 +      $ subs' $ Free (id_rls rls,idT) $ b $ f;
   1.906 +  in (((make_rule (assoc_thy thy')) o HOLogic.mk_eq) (lhs,f'),(lhs,f')) end*)
   1.907 +(* ... vals from Rewrite_Inst' ...
   1.908 +> rep_tac_ (Rewrite_Set_Inst' 
   1.909 +	       ("Script.thy",false,subs,
   1.910 +		"isolate_bdv",f,(f',[])));
   1.911 +*)
   1.912 +(* val (Rewrite_Set' (thy',put,rls,f,(f',asm)))=m;
   1.913 +*)
   1.914 +  | rep_tac_ (Rewrite_Set' (thy',put,rls,f,(f',asm)))=
   1.915 +  let val fT = type_of f;
   1.916 +    val b = if put then HOLogic.true_const else HOLogic.false_const;
   1.917 +    val lhs = Const ("Script.Rewrite'_Set",[idT,bool,fT] ---> fT) 
   1.918 +      $ Free (id_rls rls,idT) $ b $ f;
   1.919 +  in (((make_rule (assoc_thy thy')) o HOLogic.mk_eq) (lhs,f'),(lhs,f')) end
   1.920 +(* 13.3.01:
   1.921 +val thy = assoc_thy thy';
   1.922 +val t = HOLogic.mk_eq (lhs,f');
   1.923 +make_rule thy t;
   1.924 +--------------------------------------------------
   1.925 +val lll = (term_of o the o (parse thy)) 
   1.926 +  "Rewrite_Set SqRoot_simplify False (d_d x (x ^^^ #2 + #3 * x) + d_d x #4)";
   1.927 +
   1.928 +--------------------------------------------------
   1.929 +> val f = (term_of o the o (parse thy)) "x=#1+#2";
   1.930 +> val f' = (term_of o the o (parse thy)) "x=#3";
   1.931 +> val Thm (id,thm) = 
   1.932 +  rep_tac_ (Rewrite_Set' 
   1.933 +   ("Script.thy",false,"SqRoot_simplify",f,(f',[])));
   1.934 +val id = "(Rewrite_Set SqRoot_simplify True x = #1 + #2) = (x = #3)" : string
   1.935 +val thm = "(Rewrite_Set SqRoot_simplify True x = #1 + #2) = (x = #3)" : thm
   1.936 +*)
   1.937 +  | rep_tac_ (Calculate' (thy',op_,f,(f',thm')))=
   1.938 +  let val fT = type_of f;
   1.939 +    val lhs = Const ("Script.Calculate",[idT,fT] ---> fT) 
   1.940 +      $ Free (op_,idT) $ f
   1.941 +  in (((make_rule (assoc_thy thy')) o HOLogic.mk_eq) (lhs,f'),(lhs,f')) end
   1.942 +(*
   1.943 +> val lhs'=(term_of o the o (parse thy))"Calculate plus (#1+#2)";
   1.944 +  ... test-root-equ.sml: calculate ...
   1.945 +> val Appl m'=applicable_in p pt (Calculate "PLUS");
   1.946 +> val (lhs,_)=tac_2etac m';
   1.947 +> lhs'=lhs;
   1.948 +val it = true : bool*)
   1.949 +  | rep_tac_ (Check_elementwise' (t,str,(t',asm)))  = (Erule, (e_term, t'))
   1.950 +  | rep_tac_ (Subproblem' (_,_,_,_,t'))  = (Erule, (e_term, t'))
   1.951 +  | rep_tac_ (Take' (t'))  = (Erule, (e_term, t'))
   1.952 +  | rep_tac_ (Substitute' (subst,t,t'))  = (Erule, (t, t'))
   1.953 +  | rep_tac_ (Or_to_List' (t, t'))  = (Erule, (t, t'))
   1.954 +  | rep_tac_ m = raise error ("rep_tac_: not impl.for "^
   1.955 +				 (tac_2str m));
   1.956 +
   1.957 +(*"N.3.6.03------
   1.958 +fun tac_2rule m = (fst o rep_tac_) m;
   1.959 +fun tac_2etac m = (snd o rep_tac_) m;
   1.960 +fun tac_2tac m = (fst o snd o rep_tac_) m;*)
   1.961 +fun tac_2res m = (snd o snd o rep_tac_) m;(*ONLYuse of rep_tac_
   1.962 +					        FIXXXXME: simplify rep_tac_*)
   1.963 +
   1.964 +
   1.965 +(*.handle a leaf;
   1.966 +   a leaf is either a tactic or an 'exp' in 'let v = expr'
   1.967 +   where 'exp' does not contain a tactic.
   1.968 +   handling a leaf comprises
   1.969 +   (1) 'subst_stacexpr' substitute env and complete curried tactic
   1.970 +   (2) rewrite the leaf by 'srls'
   1.971 +WN060906 quick and dirty fix: return a' too (for updating E later)
   1.972 +.*)
   1.973 +fun handle_leaf call thy srls E a v t =
   1.974 +    (*WN050916 'upd_env_opt' is a blind copy from previous version*)
   1.975 +    case subst_stacexpr E a v t of
   1.976 +	(a', STac stac) => (*script-tactic*)
   1.977 +	let val stac' = eval_listexpr_ (assoc_thy thy) srls
   1.978 +			(subst_atomic (upd_env_opt E (a,v)) stac)
   1.979 +	in (if (!trace_script) 
   1.980 +	    then writeln ("@@@ "^call^" leaf '"^term2str t^"' ---> STac '"^
   1.981 +			  term2str stac'^"'")
   1.982 +	    else ();
   1.983 +	    (a', STac stac'))
   1.984 +	end
   1.985 +      | (a', Expr lexpr) => (*leaf-expression*)
   1.986 +	let val lexpr' = eval_listexpr_ (assoc_thy thy) srls
   1.987 +			 (subst_atomic (upd_env_opt E (a,v)) lexpr)
   1.988 +	in (if (!trace_script) 
   1.989 +	    then writeln("@@@ "^call^" leaf '"^term2str t^"' ---> Expr '"^
   1.990 +			 term2str lexpr'^"'")
   1.991 +	    else ();
   1.992 +	    (a', Expr lexpr'))
   1.993 +	end;
   1.994 +
   1.995 +
   1.996 +
   1.997 +(** locate an applicable stactic in a script **)
   1.998 +
   1.999 +datatype assoc = (*ExprVal in the sense of denotational semantics*)
  1.1000 +  Assoc of     (*the stac is associated, strongly or weakly*)
  1.1001 +  scrstate *       (*the current; returned for next_tac etc. outside ass* *)  
  1.1002 +  (step list)    (*list of steps done until associated stac found;
  1.1003 +	           initiated with the data for doing the 1st step,
  1.1004 +                   thus the head holds these data further on,
  1.1005 +		   while the tail holds steps finished (incl.scrstate in ptree)*)
  1.1006 +| NasApp of   (*stac not associated, but applicable, ptree-node generated*)
  1.1007 +  scrstate * (step list)
  1.1008 +| NasNap of     (*stac not associated, not applicable, nothing generated;
  1.1009 +	         for distinction in Or, for leaving iterations, leaving Seq,
  1.1010 +		 evaluate scriptexpressions*)
  1.1011 +  term * env;
  1.1012 +fun assoc2str (Assoc     _) = "Assoc"
  1.1013 +  | assoc2str (NasNap  _) = "NasNap"
  1.1014 +  | assoc2str (NasApp _) = "NasApp";
  1.1015 +
  1.1016 +
  1.1017 +datatype asap = (*arg. of assy _only_ for distinction w.r.t. Or*)
  1.1018 +  Aundef   (*undefined: set only by (topmost) Or*)
  1.1019 +| AssOnly  (*do not execute appl stacs - there could be an associated
  1.1020 +	     in parallel Or-branch*)
  1.1021 +| AssGen;  (*no Ass(Weak) found within Or, thus 
  1.1022 +             search for _applicable_ stacs, execute and generate pt*)
  1.1023 +(*this constructions doesnt allow arbitrary nesting of Or !!!*)
  1.1024 +
  1.1025 +
  1.1026 +(*assy, ass_up, astep_up scanning for locate_gen at stactic in a script.
  1.1027 +  search is clearly separated into (1)-(2):
  1.1028 +  (1) assy is recursive descent;
  1.1029 +  (2) ass_up resumes interpretation at a location somewhere in the script;
  1.1030 +      astep_up does only get to the parentnode of the scriptexpr.
  1.1031 +  consequence:
  1.1032 +  * call of (2) means _always_ that in this branch below
  1.1033 +    there was an appl.stac (Repeat, Or e1, ...)
  1.1034 +*)
  1.1035 +fun assy ya (is as (E,l,a,v,S,b),ss)
  1.1036 +	  (Const ("Let",_) $ e $ (Abs (id,T,body))) =
  1.1037 +(* val (ya, (is as (E,l,a,v,S,b),ss),Const ("Let",_) $ e $ (Abs (id,T,body))) =
  1.1038 +  (*1*)(((ts,d),Aundef), ((E,[R],a,v,S,b),[(m,EmptyMout,pt,p,[])]), body);
  1.1039 +   *)
  1.1040 +    ((*writeln("### assy Let$e$Abs: is=");
  1.1041 +     writeln(istate2str (ScrState is));*)
  1.1042 +     case assy ya ((E , l@[L,R], a,v,S,b),ss) e of
  1.1043 +	 NasApp ((E',l,a,v,S,bb),ss) => 
  1.1044 +	 let val id' = mk_Free (id, T);
  1.1045 +	     val E' = upd_env E' (id', v);
  1.1046 +	 (*val _=writeln("### assy Let -> NasApp");*)
  1.1047 +	 in assy ya ((E', l@[R,D], a,v,S,b),ss) body end
  1.1048 +     | NasNap (v,E) => 	 
  1.1049 +	 let val id' = mk_Free (id, T);
  1.1050 +	   val E' = upd_env E (id', v);
  1.1051 +	   (*val _=writeln("### assy Let -> NasNap");*)
  1.1052 +	 in assy ya ((E', l@[R,D], a,v,S,b),ss) body end
  1.1053 +     | ay => ay)
  1.1054 +
  1.1055 +  | assy (ya as (((thy,srls),_),_)) ((E,l,_,v,S,b),ss) 
  1.1056 +	 (Const ("Script.While",_) $ c $ e $ a) =
  1.1057 +    ((*writeln("### assy While $ c $ e $ a, upd_env= "^
  1.1058 +	     (subst2str (upd_env E (a,v))));*)
  1.1059 +     if eval_true_ thy srls (subst_atomic (upd_env E (a,v)) c) 
  1.1060 +     then assy ya ((E, l@[L,R], SOME a,v,S,b),ss)  e
  1.1061 +     else NasNap (v, E))
  1.1062 +   
  1.1063 +  | assy (ya as (((thy,srls),_),_)) ((E,l,a,v,S,b),ss) 
  1.1064 +	 (Const ("Script.While",_) $ c $ e) =
  1.1065 +    ((*writeln("### assy While, l= "^(loc_2str l));*)
  1.1066 +     if eval_true_ thy srls (subst_atomic (upd_env_opt E (a,v)) c) 
  1.1067 +     then assy ya ((E, l@[R], a,v,S,b),ss) e
  1.1068 +     else NasNap (v, E)) 
  1.1069 +
  1.1070 +  | assy (ya as (((thy,srls),_),_)) ((E,l,a,v,S,b),ss) 
  1.1071 +	 (Const ("If",_) $ c $ e1 $ e2) =
  1.1072 +    (if eval_true_ thy srls (subst_atomic (upd_env_opt E (a,v)) c) 
  1.1073 +     then assy ya ((E, l@[L,R], a,v,S,b),ss) e1
  1.1074 +     else assy ya ((E, l@[  R], a,v,S,b),ss) e2) 
  1.1075 +
  1.1076 +  | assy ya ((E,l,_,v,S,b),ss) (Const ("Script.Try",_) $ e $ a) =
  1.1077 +  ((*writeln("### assy Try $ e $ a, l= "^(loc_2str l));*)
  1.1078 +    case assy ya ((E, l@[L,R], SOME a,v,S,b),ss) e of
  1.1079 +     ay => ay) 
  1.1080 +
  1.1081 +  | assy ya ((E,l,a,v,S,b),ss) (Const ("Script.Try",_) $ e) =
  1.1082 +  ((*writeln("### assy Try $ e, l= "^(loc_2str l));*)
  1.1083 +    case assy ya ((E, l@[R], a,v,S,b),ss) e of
  1.1084 +     ay => ay)
  1.1085 +(* val (ya, ((E,l,_,v,S,b),ss), (Const ("Script.Seq",_) $e1 $ e2 $ a)) = 
  1.1086 +  (*2*)(ya, ((E , l@[L,R], a,v,S,b),ss), e);
  1.1087 +   *)
  1.1088 +  | assy ya ((E,l,_,v,S,b),ss) (Const ("Script.Seq",_) $e1 $ e2 $ a) =
  1.1089 +    ((*writeln("### assy Seq $e1 $ e2 $ a, E= "^(subst2str E));*)
  1.1090 +     case assy ya ((E, l@[L,L,R], SOME a,v,S,b),ss) e1 of
  1.1091 +	 NasNap (v, E) => assy ya ((E, l@[L,R], SOME a,v,S,b),ss) e2
  1.1092 +       | NasApp ((E,_,_,v,_,_),ss) => 
  1.1093 +	 assy ya ((E, l@[L,R], SOME a,v,S,b),ss) e2
  1.1094 +       | ay => ay)
  1.1095 +
  1.1096 +  | assy ya ((E,l,a,v,S,b),ss) (Const ("Script.Seq",_) $e1 $ e2) =
  1.1097 +    (case assy ya ((E, l@[L,R], a,v,S,b),ss) e1 of
  1.1098 +	 NasNap (v, E) => assy ya ((E, l@[R], a,v,S,b),ss) e2
  1.1099 +       | NasApp ((E,_,_,v,_,_),ss) => 
  1.1100 +	 assy ya ((E, l@[R], a,v,S,b),ss) e2
  1.1101 +       | ay => ay)
  1.1102 +    
  1.1103 +  | assy ya ((E,l,_,v,S,b),ss) (Const ("Script.Repeat",_) $ e $ a) =
  1.1104 +    assy ya ((E,(l@[L,R]),SOME a,v,S,b),ss) e
  1.1105 +
  1.1106 +  | assy ya ((E,l,a,v,S,b),ss) (Const ("Script.Repeat",_) $ e) =
  1.1107 +    assy ya ((E,(l@[R]),a,v,S,b),ss) e
  1.1108 +
  1.1109 +(*15.6.02: ass,app Or nochmals "uberlegen FIXXXME*)
  1.1110 +  | assy (y, Aundef) ((E,l,_,v,S,b),ss) (Const ("Script.Or",_) $e1 $ e2 $ a) =
  1.1111 +    (case assy (y, AssOnly) ((E,(l@[L,L,R]),SOME a,v,S,b),ss) e1 of
  1.1112 +	 NasNap (v, E) => 
  1.1113 +	 (case assy (y, AssOnly) ((E,(l@[L,R]),SOME a,v,S,b),ss) e2 of
  1.1114 +	      NasNap (v, E) => 
  1.1115 +	      (case assy (y, AssGen) ((E,(l@[L,L,R]),SOME a,v,S,b),ss) e1 of
  1.1116 +	       NasNap (v, E) => 
  1.1117 +	       assy (y, AssGen) ((E, (l@[L,R]), SOME a,v,S,b),ss) e2
  1.1118 +	     | ay => ay)
  1.1119 +	    | ay =>(ay))
  1.1120 +       | NasApp _ => raise error ("assy: FIXXXME ///must not return NasApp///")
  1.1121 +       | ay => (ay))
  1.1122 +
  1.1123 +  | assy ya ((E,l,a,v,S,b),ss) (Const ("Script.Or",_) $e1 $ e2) =
  1.1124 +    (case assy ya ((E,(l@[L,R]),a,v,S,b),ss) e1 of
  1.1125 +	 NasNap (v, E) => 
  1.1126 +	 assy ya ((E,(l@[R]),a,v,S,b),ss) e2
  1.1127 +       | ay => (ay)) 
  1.1128 +(* val ((m,_,pt,(p,p_),c)::ss) = [(m,EmptyMout,pt,p,[])];
  1.1129 +   val t = (term_of o the o (parse Isac.thy)) "Rewrite rmult_1 False";
  1.1130 +
  1.1131 +   val (ap,(p,p_),c,ss) = (Aundef,p,[],[]);
  1.1132 +   assy (((thy',srls),d),ap) ((E,l,a,v,S,b), (m,EmptyMout,pt,(p,p_),c)::ss) t;
  1.1133 +val ((((thy',sr),d),ap), (is as (E,l,a,v,S,b), (m,_,pt,(p,p_),c)::ss), t) =
  1.1134 +    ();
  1.1135 +   *) 
  1.1136 +
  1.1137 +  | assy (((thy',sr),d),ap) (is as (E,l,a,v,S,b), (m,_,pt,(p,p_),c)::ss) t =
  1.1138 +    ((*writeln("### assy, m = "^tac_2str m);
  1.1139 +     writeln("### assy, (p,p_) = "^pos'2str (p,p_));
  1.1140 +     writeln("### assy, is= ");
  1.1141 +     writeln(istate2str (ScrState is));*)
  1.1142 +     case handle_leaf "locate" thy' sr E a v t of
  1.1143 +	(a', Expr s) => 
  1.1144 +	((*writeln("### assy: listexpr t= "^(term2str t)); 
  1.1145 +         writeln("### assy, E= "^(env2str E));
  1.1146 +	 writeln("### assy, eval(..)= "^(term2str
  1.1147 +	       (eval_listexpr_ (assoc_thy thy') sr
  1.1148 +			       (subst_atomic (upd_env_opt E (a',v)) t))));*)
  1.1149 +	  NasNap (eval_listexpr_ (assoc_thy thy') sr
  1.1150 +			       (subst_atomic (upd_env_opt E (a',v)) t), E))
  1.1151 +      (* val (_,STac stac) = subst_stacexpr E a v t;
  1.1152 +         *)
  1.1153 +      | (a', STac stac) =>
  1.1154 +	let (*val _=writeln("### assy, stac = "^term2str stac);*)
  1.1155 +	    val p' = case p_ of Frm => p | Res => lev_on p
  1.1156 +			      | _ => raise error ("assy: call by "^
  1.1157 +						  (pos'2str (p,p_)));
  1.1158 +	in case assod pt d m stac of
  1.1159 +	 Ass (m,v') =>
  1.1160 +	 let (*val _=writeln("### assy: Ass ("^tac_2str m^", "^
  1.1161 +			       term2str v'^")");*)
  1.1162 +	     val (p'',c',f',pt') = generate1 (assoc_thy thy') m 
  1.1163 +			        (ScrState (E,l,a',v',S,true)) (p',p_) pt;
  1.1164 +	   in Assoc ((E,l,a',v',S,true), (m,f',pt',p'',c @ c')::ss) end
  1.1165 +       | AssWeak (m,v') => 
  1.1166 +	   let (*val _=writeln("### assy: Ass Weak("^tac_2str m^", "^
  1.1167 +			       term2str v'^")");*)
  1.1168 +	      val (p'',c',f',pt') = generate1 (assoc_thy thy') m 
  1.1169 +			         (ScrState (E,l,a',v',S,false)) (p',p_) pt;
  1.1170 +	   in Assoc ((E,l,a',v',S,false), (m,f',pt',p'',c @ c')::ss) end
  1.1171 +       | NotAss =>
  1.1172 +	   ((*writeln("### assy, NotAss");*)
  1.1173 +	    case ap of   (*switch for Or: 1st AssOnly, 2nd AssGen*)
  1.1174 +	      AssOnly => (NasNap (v, E))
  1.1175 +	    | gen => (case applicable_in (p,p_) pt 
  1.1176 +					 (stac2tac pt (assoc_thy thy') stac) of
  1.1177 +			Appl m' =>
  1.1178 +			  let val is = (E,l,a',tac_2res m',S,false(*FIXXXME*))
  1.1179 +			      val (p'',c',f',pt') =
  1.1180 +			      generate1 (assoc_thy thy') m' (ScrState is) (p',p_) pt;
  1.1181 +			  in NasApp (is,(m,f',pt',p'',c @ c')::ss) end
  1.1182 +		      | Notappl _ => 
  1.1183 +			    (NasNap (v, E))
  1.1184 +			    )
  1.1185 +		)
  1.1186 +       end);
  1.1187 +(* (astep_up ((thy',scr,d),NasApp_) ((E,l,a,v,S,b),[(m,EmptyMout,pt,p,[])])) handle e => print_exn_G e;
  1.1188 +  *)
  1.1189 +
  1.1190 +
  1.1191 +(* val (ys as (y,s,Script sc,d),(is as (E,l,a,v,S,b),ss),Const ("Let",_) $ _) =
  1.1192 +       (ys, ((E,up,a,v,S,b),ss), go up sc);
  1.1193 +   *)
  1.1194 +fun ass_up (ys as (y,s,Script sc,d)) (is as (E,l,a,v,S,b),ss) 
  1.1195 +	   (Const ("Let",_) $ _) =
  1.1196 +    let (*val _= writeln("### ass_up1 Let$e: is=")
  1.1197 +	val _= writeln(istate2str (ScrState is))*)
  1.1198 +	val l = drop_last l; (*comes from e, goes to Abs*)
  1.1199 +      val (Const ("Let",_) $ e $ (Abs (i,T,body))) = go l sc;
  1.1200 +      val i = mk_Free (i, T);
  1.1201 +      val E = upd_env E (i, v);
  1.1202 +      (*val _=writeln("### ass_up2 Let$e: E="^(subst2str E));*)
  1.1203 +    in case assy (((y,s),d),Aundef) ((E, l@[R,D], a,v,S,b),ss) body of
  1.1204 +	   Assoc iss => Assoc iss
  1.1205 +	 | NasApp iss => astep_up ys iss 
  1.1206 +	 | NasNap (v, E) => astep_up ys ((E,l,a,v,S,b),ss) end
  1.1207 +
  1.1208 +  | ass_up ys (iss as (is,_)) (Abs (_,_,_)) = 
  1.1209 +    ((*writeln("### ass_up  Abs: is=");
  1.1210 +     writeln(istate2str (ScrState is));*)
  1.1211 +     astep_up ys iss) (*TODO 5.9.00: env ?*)
  1.1212 +
  1.1213 +  | ass_up ys (iss as (is,_)) (Const ("Let",_) $ e $ (Abs (i,T,b)))=
  1.1214 +    ((*writeln("### ass_up Let $ e $ Abs: is=");
  1.1215 +     writeln(istate2str (ScrState is));*)
  1.1216 +     astep_up ys iss) (*TODO 5.9.00: env ?*)
  1.1217 +
  1.1218 +    (* val (ysa, iss,                 (Const ("Script.Seq",_) $ _ $ _ $ _)) =
  1.1219 +	   (ys,  ((E,up,a,v,S,b),ss), (go up sc));
  1.1220 +       *)
  1.1221 +  | ass_up ysa iss (Const ("Script.Seq",_) $ _ $ _ $ _) =
  1.1222 +    astep_up ysa iss (*all has been done in (*2*) below*)
  1.1223 +
  1.1224 +  | ass_up ysa iss (Const ("Script.Seq",_) $ _ $ _) =
  1.1225 +    (* val (ysa, iss,                 (Const ("Script.Seq",_) $ _ $ _)) =
  1.1226 +	   (ys,  ((E,up,a,v,S,b),ss), (go up sc));
  1.1227 +       *)
  1.1228 +    astep_up ysa iss (*2*: comes from e2*)
  1.1229 +
  1.1230 +  | ass_up (ysa as (y,s,Script sc,d)) (is as (E,l,a,v,S,b),ss)
  1.1231 +	   (Const ("Script.Seq",_) $ _ ) = (*2*: comes from e1, goes to e2*)
  1.1232 +	   (* val ((ysa as (y,s,Script sc,d)), (is as (E,l,a,v,S,b),ss),
  1.1233 +	                                  (Const ("Script.Seq",_) $ _ )) = 
  1.1234 +		  (ys,   ((E,up,a,v,S,b),ss), (go up sc));
  1.1235 +	      *)
  1.1236 +    let val up = drop_last l;
  1.1237 +	val Const ("Script.Seq",_) $ _ $ e2 = go up sc
  1.1238 +	(*val _= writeln("### ass_up Seq$e: is=")
  1.1239 +	val _= writeln(istate2str (ScrState is))*)
  1.1240 +    in case assy (((y,s),d),Aundef) ((E, up@[R], a,v,S,b),ss) e2 of
  1.1241 +	   NasNap (v,E) => astep_up ysa ((E,up,a,v,S,b),ss)
  1.1242 +	 | NasApp iss => astep_up ysa iss
  1.1243 +	 | ay => ay end
  1.1244 +
  1.1245 +    (* val (ysa, iss,                 (Const ("Script.Try",_) $ e $ _)) =
  1.1246 +	   (ys,  ((E,up,a,v,S,b),ss), (go up sc));
  1.1247 +       *)
  1.1248 +  | ass_up ysa iss (Const ("Script.Try",_) $ e $ _) =
  1.1249 +    astep_up ysa iss
  1.1250 +
  1.1251 +  (* val (ysa, iss, (Const ("Script.Try",_) $ e)) =
  1.1252 +	 (ys,  ((E,up,a,v,S,b),ss), (go up sc));
  1.1253 +     *)
  1.1254 +  | ass_up ysa iss (Const ("Script.Try",_) $ e) =
  1.1255 +    ((*writeln("### ass_up Try $ e");*)
  1.1256 +     astep_up ysa iss)
  1.1257 +
  1.1258 +  | ass_up (ys as (y,s,_,d)) ((E,l,_,v,S,b),ss)
  1.1259 +	   (*(Const ("Script.While",_) $ c $ e $ a) = WN050930 blind fix*)
  1.1260 +	   (t as Const ("Script.While",_) $ c $ e $ a) =
  1.1261 +    ((*writeln("### ass_up: While c= "^
  1.1262 +	     (term2str (subst_atomic (upd_env E (a,v)) c)));*)
  1.1263 +     if eval_true_ y s (subst_atomic (upd_env E (a,v)) c)
  1.1264 +    then (case assy (((y,s),d),Aundef) ((E, l@[L,R], SOME a,v,S,b),ss) e of 
  1.1265 +       NasNap (v,E') => astep_up ys ((E',l, SOME a,v,S,b),ss)
  1.1266 +     | NasApp ((E',l,a,v,S,b),ss) =>
  1.1267 +       ass_up ys ((E',l,a,v,S,b),ss) t (*WN050930 't' was not assigned*)
  1.1268 +     | ay => ay)
  1.1269 +    else astep_up ys ((E,l, SOME a,v,S,b),ss)
  1.1270 +	 )
  1.1271 +
  1.1272 +  | ass_up (ys as (y,s,_,d)) ((E,l,a,v,S,b),ss)
  1.1273 +	   (*(Const ("Script.While",_) $ c $ e) = WN050930 blind fix*)
  1.1274 +	   (t as Const ("Script.While",_) $ c $ e) =
  1.1275 +    if eval_true_ y s (subst_atomic (upd_env_opt E (a,v)) c)
  1.1276 +    then (case assy (((y,s),d),Aundef) ((E, l@[R], a,v,S,b),ss) e of 
  1.1277 +       NasNap (v,E') => astep_up ys ((E',l, a,v,S,b),ss)
  1.1278 +     | NasApp ((E',l,a,v,S,b),ss) =>
  1.1279 +       ass_up ys ((E',l,a,v,S,b),ss) t (*WN050930 't' was not assigned*)
  1.1280 +     | ay => ay)
  1.1281 +    else astep_up ys ((E,l, a,v,S,b),ss)
  1.1282 +
  1.1283 +  | ass_up y iss (Const ("If",_) $ _ $ _ $ _) = astep_up y iss
  1.1284 +
  1.1285 +  | ass_up (ys as (y,s,_,d)) ((E,l,_,v,S,b),ss)
  1.1286 +	   (t as Const ("Script.Repeat",_) $ e $ a) =
  1.1287 +  (case assy (((y,s),d), Aundef) ((E, (l@[L,R]), SOME a,v,S,b),ss) e of 
  1.1288 +       NasNap (v,E') => astep_up ys ((E',l, SOME a,v,S,b),ss)
  1.1289 +     | NasApp ((E',l,a,v,S,b),ss) =>
  1.1290 +       ass_up ys ((E',l,a,v,S,b),ss) t
  1.1291 +     | ay => ay)
  1.1292 +
  1.1293 +  | ass_up (ys as (y,s,_,d)) (is as ((E,l,a,v,S,b),ss)) 
  1.1294 +	   (t as Const ("Script.Repeat",_) $ e) =
  1.1295 +  (case assy (((y,s),d), Aundef) ((E, (l@[R]), a,v,S,b),ss) e of 
  1.1296 +       NasNap (v', E') => astep_up ys ((E',l,a,v',S,b),ss)
  1.1297 +     | NasApp ((E',l,a,v',S,bb),ss) => 
  1.1298 +       ass_up ys ((E',l,a,v',S,b),ss) t
  1.1299 +     | ay => ay)
  1.1300 +
  1.1301 +  | ass_up y iss (Const ("Script.Or",_) $ _ $ _ $ _) = astep_up y iss
  1.1302 +
  1.1303 +  | ass_up y iss (Const ("Script.Or",_) $ _ $ _) = astep_up y iss
  1.1304 +
  1.1305 +  | ass_up y ((E,l,a,v,S,b),ss) (Const ("Script.Or",_) $ _ ) = 
  1.1306 +    astep_up y ((E, (drop_last l), a,v,S,b),ss)
  1.1307 +
  1.1308 +  | ass_up y iss t =
  1.1309 +    raise error ("ass_up not impl for t= "^(term2str t))
  1.1310 +(* 9.6.03
  1.1311 +   val (ys as (_,_,Script sc,_), ss) = 
  1.1312 +       ((thy',srls,scr,d), [(m,EmptyMout,pt,p,[])]:step list);
  1.1313 +   astep_up ys ((E,l,a,v,S,b),ss);
  1.1314 +   val ((ys as (_,_,Script sc,_)), ((E,l,a,v,S,b),ss)) = 
  1.1315 +       (ysa, iss);
  1.1316 +   val ((ys as (_,_,Script sc,_)), ((E,l,a,v,S,b),ss)) = 
  1.1317 +       ((thy',srls,scr,d), ((E,l,a,v,S,b), [(m,EmptyMout,pt,p,[])]));
  1.1318 +   *)  
  1.1319 +and astep_up (ys as (_,_,Script sc,_)) ((E,l,a,v,S,b),ss) =
  1.1320 +  if 1 < length l
  1.1321 +    then 
  1.1322 +      let val up = drop_last l;
  1.1323 +	  (*val _= writeln("### astep_up: E= "^env2str E);*)
  1.1324 +      in ass_up ys ((E,up,a,v,S,b),ss) (go up sc) end
  1.1325 +  else (NasNap (v, E))
  1.1326 +;
  1.1327 +
  1.1328 +
  1.1329 +
  1.1330 +
  1.1331 +
  1.1332 +(* use"ME/script.sml";
  1.1333 +   use"script.sml";
  1.1334 + term2str (go up sc);
  1.1335 +
  1.1336 +   *)
  1.1337 +
  1.1338 +(*check if there are tacs for rewriting only*)
  1.1339 +fun rew_only ([]:step list) = true
  1.1340 +  | rew_only (((Rewrite' _          ,_,_,_,_))::ss) = rew_only ss
  1.1341 +  | rew_only (((Rewrite_Inst' _     ,_,_,_,_))::ss) = rew_only ss
  1.1342 +  | rew_only (((Rewrite_Set' _      ,_,_,_,_))::ss) = rew_only ss
  1.1343 +  | rew_only (((Rewrite_Set_Inst' _ ,_,_,_,_))::ss) = rew_only ss
  1.1344 +  | rew_only (((Calculate' _        ,_,_,_,_))::ss) = rew_only ss
  1.1345 +  | rew_only (((Begin_Trans' _      ,_,_,_,_))::ss) = rew_only ss
  1.1346 +  | rew_only (((End_Trans' _        ,_,_,_,_))::ss) = rew_only ss
  1.1347 +  | rew_only _ = false; 
  1.1348 +  
  1.1349 +
  1.1350 +datatype locate =
  1.1351 +  Steps of istate      (*producing hd of step list (which was latest)
  1.1352 +	                 for next_tac, for reporting Safe|Unsafe to DG*)
  1.1353 +	   * step      (*(scrstate producing this step is in ptree !)*) 
  1.1354 +		 list  (*locate_gen may produce intermediate steps*)
  1.1355 +| NotLocatable;        (*no (m Ass m') or (m AssWeak m') found*)
  1.1356 +
  1.1357 +
  1.1358 +
  1.1359 +(* locate_gen tries to locate an input tac m in the script. 
  1.1360 +   pursuing this goal the script is executed until an (m' equiv m) is found,
  1.1361 +   or the end of the script
  1.1362 +args
  1.1363 +   m   : input by the user, already checked by applicable_in,
  1.1364 +         (to be searched within Or; and _not_ an m doing the step on ptree !)
  1.1365 +   p,pt: (incl ets) at the time of input
  1.1366 +   scr : the script
  1.1367 +   d   : canonical simplifier for locating Take, Substitute, Subproblems etc.
  1.1368 +   ets : ets at the time of input
  1.1369 +   l   : the location (in scr) of the stac which generated the current formula
  1.1370 +returns
  1.1371 +   Steps: pt,p (incl. ets) with m done
  1.1372 +          pos' list of proofobjs cut (from generate)
  1.1373 +          safe: implied from last proofobj
  1.1374 +	  ets:
  1.1375 +   ///ToDo : ets contains a list of tacs to be done before m can be done
  1.1376 +          NOT IMPL. -- "error: do other step before"
  1.1377 +   NotLocatable: thus generate_hard
  1.1378 +*)
  1.1379 +(* val (Rewrite'(_,ro,er,pa,(id,str),f,_), p, Rfuns {locate_rule=lo,...},
  1.1380 +	RrlsState (_,f'',rss,rts)) = (m, (p,p_), sc, is);
  1.1381 +   *)
  1.1382 +fun locate_gen (thy',_) (Rewrite'(_,ro,er,pa,(id,str),f,_)) (pt,p) 
  1.1383 +	       (Rfuns {locate_rule=lo,...}, d) (RrlsState (_,f'',rss,rts)) = 
  1.1384 +    (case lo rss f (Thm (id, mk_thm (assoc_thy thy') str)) of
  1.1385 +	 [] => NotLocatable
  1.1386 +       | rts' => 
  1.1387 +	 Steps (rts2steps [] ((pt,p),(f,f'',rss,rts),(thy',ro,er,pa)) rts'))
  1.1388 +(* val p as(p',p_)=(p,p_);val scr as Script(h $ body)=sc;val (E,l,a,v,S,bb)=is;
  1.1389 +   locate_gen (thy':theory') (m:tac_) ((pt,p):ptree * pos') 
  1.1390 +	      (scr,d) (E,l,a,v,S,bb);
  1.1391 +   9.6.03
  1.1392 +   val ts = (thy',srls);
  1.1393 +   val p = (p,p_);
  1.1394 +   val (scr as Script (h $ body)) = (sc);
  1.1395 +   val ScrState (E,l,a,v,S,b) = (is);
  1.1396 +
  1.1397 +   val (ts as (thy',srls), m, (pt,p), 
  1.1398 +	(scr as Script (h $ body),d), (ScrState (E,l,a,v,S,b))) = 
  1.1399 +       ((thy',srls), m,  (pt,(p,p_)), (sc,d), is);
  1.1400 +   locate_gen (thy',srls) m (pt,p) (Script(h $ body),d)(ScrState(E,l,a,v,S,b));
  1.1401 +
  1.1402 +   val (ts as (thy',srls), m, (pt,p), 
  1.1403 +	(scr as Script (h $ body),d), (ScrState (E,l,a,v,S,b))) = 
  1.1404 +       ((thy',srls), m',  (pt,(lev_on p,Frm)), (sc,d), is');
  1.1405 +
  1.1406 +   val (ts as (thy',srls), m, (pt,p), 
  1.1407 +	(scr as Script (h $ body),d), (ScrState (E,l,a,v,S,b))) = 
  1.1408 +       ((thy',srls), m',  (pt,(p, Res)), (sc,d), is');
  1.1409 +
  1.1410 +   val (ts as (thy',srls), m, (pt,p), 
  1.1411 +	(scr as Script (h $ body),d), (ScrState (E,l,a,v,S,b))) = 
  1.1412 +       ((thy',srls), m,  (pt,(p,p_)), (sc,d), is);
  1.1413 +   *)
  1.1414 +  | locate_gen (ts as (thy',srls)) (m:tac_) ((pt,p):ptree * pos') 
  1.1415 +	       (scr as Script (h $ body),d) (ScrState (E,l,a,v,S,b))  = 
  1.1416 +  let (*val _= writeln("### locate_gen-----------------: is=");
  1.1417 +      val _= writeln( istate2str (ScrState (E,l,a,v,S,b)));
  1.1418 +      val _= writeln("### locate_gen: l= "^loc_2str l^", p= "^pos'2str p)*)
  1.1419 +      val thy = assoc_thy thy';
  1.1420 +  in case if l=[] orelse ((*init.in solve..Apply_Method...*)
  1.1421 +			  (last_elem o fst) p = 0 andalso snd p = Res)
  1.1422 +	  then (assy ((ts,d),Aundef) ((E,[R],a,v,S,b),
  1.1423 +				      [(m,EmptyMout,pt,p,[])]) body)
  1.1424 +(* val Assoc (iss as (is as (_,_,_,_,_,bb), ss as ((m',f',pt',p',c')::_))) =
  1.1425 +       (astep_up (thy',srls,scr,d) ((E,l,a,v,S,b),[(m,EmptyMout,pt,p,[])]));
  1.1426 +       (assy ((ts,d),Aundef) ((E,[R],a,v,S,b),[(m,EmptyMout,pt,p,[])]) body);
  1.1427 +  *)
  1.1428 +	  else (astep_up (thy',srls,scr,d) ((E,l,a,v,S,b),
  1.1429 +					    [(m,EmptyMout,pt,p,[])]) ) of
  1.1430 +	 Assoc (iss as (is as (_,_,_,_,_,bb), ss as ((m',f',pt',p',c')::_))) =>
  1.1431 +(* val Assoc (iss as (is as (_,_,_,_,_,bb), ss as ((m',f',pt',p',c')::_))) =
  1.1432 +       (astep_up (thy',srls,scr,d) ((E,l,a,v,S,b),
  1.1433 +				    [(m,EmptyMout,pt,p,[])]) );
  1.1434 +   *)
  1.1435 +	 ((*writeln("### locate_gen Assoc: p'="^(pos'2str p'));*)
  1.1436 +	  if bb then Steps (ScrState is, ss)
  1.1437 +	  else if rew_only ss (*andalso 'not bb'= associated weakly*)
  1.1438 +	  then let val (po,p_) = p
  1.1439 +                   val po' = case p_ of Frm => po | Res => lev_on po
  1.1440 +		  (*WN.12.03: noticed, that pos is also updated in assy !?!
  1.1441 +		   instead take p' from Assoc ?????????????????????????????*)
  1.1442 +                  val (p'',c'',f'',pt'') = 
  1.1443 +		      generate1 thy m (ScrState is) (po',p_) pt;
  1.1444 +	      (*val _=writeln("### locate_gen, aft g1: p''="^(pos'2str p''));*)
  1.1445 +	      (*drop the intermediate steps !*)
  1.1446 +	      in Steps (ScrState is, [(m, f'',pt'',p'',c'')]) end
  1.1447 +	 else Steps (ScrState is, ss))
  1.1448 +	
  1.1449 +     | NasApp _ (*[((E,l,a,v,S,bb),(m',f',pt',p',c'))] => 
  1.1450 +	   raise error ("locate_gen: should not have got NasApp, ets =")*)
  1.1451 +       => NotLocatable
  1.1452 +     | NasNap (_,_) =>
  1.1453 +       if l=[] then NotLocatable
  1.1454 +       else (*scan from begin of script for rew_only*)
  1.1455 +	   (case assy ((ts,d),Aundef) ((E,[R],a,v,Unsafe,b),
  1.1456 +					 [(m,EmptyMout,pt,p,[])]) body  of
  1.1457 +		Assoc (iss as (is as (_,_,_,_,_,bb), 
  1.1458 +			       ss as ((m',f',pt',p',c')::_))) =>
  1.1459 +		    ((*writeln"4### locate_gen Assoc after Fini";*)
  1.1460 +		     if rew_only ss
  1.1461 +		     then let val(p'',c'',f'',pt'') = 
  1.1462 +				 generate1 thy m (ScrState is) p' pt;
  1.1463 +			  (*drop the intermediate steps !*)
  1.1464 +			  in Steps (ScrState is, [(m, f'',pt'',p'',c'')]) end
  1.1465 +		     else NotLocatable)
  1.1466 +	      | _ => ((*writeln ("#### locate_gen: after Fini");*)
  1.1467 +		      NotLocatable))
  1.1468 +  end
  1.1469 +  | locate_gen _ m _ (sc,_) is = 
  1.1470 +    raise error ("locate_gen: wrong arguments,\n tac= "^(tac_2str m)^
  1.1471 +		 ",\n scr= "^(scr2str sc)^",\n istate= "^(istate2str is));
  1.1472 +
  1.1473 +
  1.1474 +
  1.1475 +(** find the next stactic in a script **)
  1.1476 +
  1.1477 +datatype appy =  (*ExprVal in the sense of denotational semantics*)
  1.1478 +    Appy of      (*applicable stac found, search stalled*)
  1.1479 +    tac_ *       (*tac_ associated (fun assod) with stac*)
  1.1480 +    scrstate     (*after determination of stac WN.18.8.03*)
  1.1481 +  | Napp of      (*stac found was not applicable; 
  1.1482 +	           this mode may become Skip in Repeat, Try and Or*)
  1.1483 +    env (*stack*)  (*popped while nxt_up*)
  1.1484 +  | Skip of      (*for restart after Appy, for leaving iterations,
  1.1485 +	           for passing the value of scriptexpressions,
  1.1486 +		   and for finishing the script successfully*)
  1.1487 +    term * env (*stack*);
  1.1488 +
  1.1489 +(*appy, nxt_up, nstep_up scanning for next_tac.
  1.1490 +  search is clearly separated into (1)-(2):
  1.1491 +  (1) appy is recursive descent;
  1.1492 +  (2) nxt_up resumes interpretation at a location somewhere in the script;
  1.1493 +      nstep_up does only get to the parentnode of the scriptexpr.
  1.1494 +  consequence:
  1.1495 +  * call of (2) means _always_ that in this branch below
  1.1496 +    there was an applicable stac (Repeat, Or e1, ...)
  1.1497 +*)
  1.1498 +
  1.1499 +
  1.1500 +datatype appy_ = (*as argument in nxt_up, nstep_up, from appy*)
  1.1501 +       (*  Appy is only (final) returnvalue, not argument during search
  1.1502 +       |*) Napp_ (*ev. detects 'script is not appropriate for this example'*)
  1.1503 +       | Skip_;  (*detects 'script successfully finished'
  1.1504 +		   also used as init-value for resuming; this works,
  1.1505 +	           because 'nxt_up Or e1' treats as Appy*)
  1.1506 +
  1.1507 +fun appy thy ptp E l
  1.1508 +  (t as Const ("Let",_) $ e $ (Abs (i,T,b))) a v =
  1.1509 +(* val (thy, ptp, E, l,        t as Const ("Let",_) $ e $ (Abs (i,T,b)),a, v)=
  1.1510 +       (thy, ptp, E, up@[R,D], body,                                    a, v);
  1.1511 +   appy thy ptp E l t a v;
  1.1512 +   *)
  1.1513 +  ((*writeln("### appy Let$e$Abs: is=");
  1.1514 +   writeln(istate2str (ScrState (E,l,a,v,Sundef,false)));*)
  1.1515 +   case appy thy ptp E (l@[L,R]) e a v of
  1.1516 +     Skip (res, E) => 
  1.1517 +       let (*val _= writeln("### appy Let "^(term2str t));
  1.1518 +	 val _= writeln("### appy Let: Skip res ="^(term2str res));*)
  1.1519 +       (*val (i',b') = variant_abs (i,T,b); WN.15.5.03
  1.1520 +	 val i = mk_Free(i',T);             WN.15.5.03 *)   
  1.1521 +	 val E' = upd_env E (Free (i,T), res);
  1.1522 +       in appy thy ptp E' (l@[R,D]) b a v end
  1.1523 +   | ay => ay)
  1.1524 +
  1.1525 +  | appy (thy as (th,sr)) ptp E l
  1.1526 +  (t as Const ("Script.While"(*1*),_) $ c $ e $ a) _ v = (*ohne n. 28.9.00*)
  1.1527 +  ((*writeln("### appy While $ c $ e $ a, upd_env= "^
  1.1528 +	   (subst2str (upd_env E (a,v))));*)
  1.1529 +   if eval_true_ th sr (subst_atomic (upd_env E (a,v)) c)
  1.1530 +    then appy thy ptp E (l@[L,R]) e (SOME a) v
  1.1531 +  else Skip (v, E))
  1.1532 +
  1.1533 +  | appy (thy as (th,sr)) ptp E l
  1.1534 +  (t as Const ("Script.While"(*2*),_) $ c $ e) a v =(*ohne nachdenken 28.9.00*)
  1.1535 +  ((*writeln("### appy While $ c $ e, upd_env= "^
  1.1536 +	   (subst2str (upd_env_opt E (a,v))));*)
  1.1537 +   if eval_true_ th sr (subst_atomic (upd_env_opt E (a,v)) c)
  1.1538 +    then appy thy ptp E (l@[R]) e a v
  1.1539 +  else Skip (v, E))
  1.1540 +
  1.1541 +  | appy (thy as (th,sr)) ptp E l (t as Const ("If",_) $ c $ e1 $ e2) a v =
  1.1542 +    ((*writeln("### appy If: t= "^(term2str t));
  1.1543 +     writeln("### appy If: c= "^(term2str(subst_atomic(upd_env_opt E(a,v))c)));
  1.1544 +     writeln("### appy If: thy= "^(fst thy));*)
  1.1545 +     if eval_true_ th sr (subst_atomic (upd_env_opt E (a,v)) c)
  1.1546 +     then ((*writeln("### appy If: true");*)appy thy ptp E (l@[L,R]) e1 a v)
  1.1547 +     else ((*writeln("### appy If: false");*)appy thy ptp E (l@[  R]) e2 a v))
  1.1548 +(* val (thy, ptp, E, l,     (Const ("Script.Repeat",_) $ e $ a), _, v) =
  1.1549 +       (thy, ptp, E, (l@[R]), e,                                 a, v);
  1.1550 +   *)
  1.1551 +  | appy thy ptp E (*env*) l
  1.1552 +  (Const ("Script.Repeat"(*1*),_) $ e $ a) _ v = 
  1.1553 +    ((*writeln("### appy Repeat a: ");*)
  1.1554 +     appy thy ptp E (*env*) (l@[L,R]) e (SOME a) v)
  1.1555 +(* val (thy, ptp, E, l,     (Const ("Script.Repeat",_) $ e), _, v) =
  1.1556 +       (thy, ptp, E, (l@[R]), e,                             a, v);
  1.1557 +   *)
  1.1558 +  | appy thy ptp E (*env*) l
  1.1559 +  (Const ("Script.Repeat"(*2*),_) $ e) a v = 
  1.1560 +    ((*writeln("3### appy Repeat: a= "^
  1.1561 +	     (Syntax.string_of_term (thy2ctxt (assoc_thy thy)) a));*)
  1.1562 +     appy thy ptp E (*env*) (l@[R]) e a v)
  1.1563 +(* val (thy, ptp, E, l,      (t as Const ("Script.Try",_) $ e $ a), _, v)=
  1.1564 +       (thy, ptp, E, (l@[R]), e2,                                   a, v);
  1.1565 +   *)
  1.1566 +  | appy thy ptp E l
  1.1567 +  (t as Const ("Script.Try",_) $ e $ a) _ v =
  1.1568 +  (case appy thy ptp E (l@[L,R]) e (SOME a) v of
  1.1569 +     Napp E => ((*writeln("### appy Try "^
  1.1570 +			  (Syntax.string_of_term (thy2ctxt (assoc_thy thy)) t));*)
  1.1571 +		 Skip (v, E))
  1.1572 +   | ay => ay)
  1.1573 +(* val (thy, ptp, E, l,      (t as Const ("Script.Try",_) $ e), _, v)=
  1.1574 +       (thy, ptp, E, (l@[R]), e2,                               a, v);
  1.1575 +   val (thy, ptp, E, l,        (t as Const ("Script.Try",_) $ e), _, v)=
  1.1576 +       (thy, ptp, E, (l@[L,R]), e1,                               a, v);
  1.1577 +   *)
  1.1578 +  | appy thy ptp E l
  1.1579 +  (t as Const ("Script.Try",_) $ e) a v =
  1.1580 +  (case appy thy ptp E (l@[R]) e a v of
  1.1581 +     Napp E => ((*writeln("### appy Try "^
  1.1582 +			  (Syntax.string_of_term (thy2ctxt (assoc_thy thy)) t));*)
  1.1583 +		 Skip (v, E))
  1.1584 +   | ay => ay)
  1.1585 +
  1.1586 +
  1.1587 +  | appy thy ptp E l
  1.1588 +	 (Const ("Script.Or"(*1*),_) $e1 $ e2 $ a) _ v =
  1.1589 +    (case appy thy ptp E (l@[L,L,R]) e1 (SOME a) v of
  1.1590 +	 Appy lme => Appy lme
  1.1591 +       | _ => appy thy ptp E (*env*) (l@[L,R]) e2 (SOME a) v)
  1.1592 +    
  1.1593 +  | appy thy ptp E l
  1.1594 +	 (Const ("Script.Or"(*2*),_) $e1 $ e2) a v =
  1.1595 +    (case appy thy ptp E (l@[L,R]) e1 a v of
  1.1596 +	 Appy lme => Appy lme
  1.1597 +       | _ => appy thy ptp E (l@[R]) e2 a v)
  1.1598 +
  1.1599 +(* val (thy, ptp, E, l,     (Const ("Script.Seq",_) $ e1 $ e2 $ a), _, v)=
  1.1600 +       (thy, ptp, E,(up@[R]),e2,                                    a, v);
  1.1601 +   val (thy, ptp, E, l,     (Const ("Script.Seq",_) $ e1 $ e2 $ a), _, v)=
  1.1602 +       (thy, ptp, E,(up@[R,D]),body,                                a, v);
  1.1603 +   *)
  1.1604 +  | appy thy ptp E l
  1.1605 +	 (Const ("Script.Seq"(*1*),_) $ e1 $ e2 $ a) _ v =
  1.1606 +    ((*writeln("### appy Seq $ e1 $ e2 $ a, upd_env= "^
  1.1607 +	     (subst2str (upd_env E (a,v))));*)
  1.1608 +     case appy thy ptp E (l@[L,L,R]) e1 (SOME a) v of
  1.1609 +	 Skip (v,E) => appy thy ptp E (l@[L,R]) e2 (SOME a) v
  1.1610 +       | ay => ay)
  1.1611 +
  1.1612 +(* val (thy, ptp, E, l,     (Const ("Script.Seq",_) $ e1 $ e2), _, v)=
  1.1613 +       (thy, ptp, E,(up@[R]),e2,                                a, v);
  1.1614 +   val (thy, ptp, E, l,     (Const ("Script.Seq",_) $ e1 $ e2), _, v)=
  1.1615 +       (thy, ptp, E,(l@[R]), e2,                                a, v);
  1.1616 +   val (thy, ptp, E, l,     (Const ("Script.Seq",_) $ e1 $ e2), _, v)=
  1.1617 +       (thy, ptp, E,(up@[R,D]),body,                            a, v);
  1.1618 +   *)
  1.1619 +  | appy thy ptp E l
  1.1620 +	 (Const ("Script.Seq",_) $ e1 $ e2) a v =
  1.1621 +    (case appy thy ptp E (l@[L,R]) e1 a v of
  1.1622 +	 Skip (v,E) => appy thy ptp E (l@[R]) e2 a v
  1.1623 +       | ay => ay)
  1.1624 +
  1.1625 +  (*.a leaf has been found*)   
  1.1626 +  | appy (thy as (th,sr)) (pt, p) E l t a v =
  1.1627 +(* val (thy as (th,sr),(pt, p),E, l,        t,    a, v) = 
  1.1628 +       (thy,            ptp,   E, up@[R,D], body, a, v);
  1.1629 +   val (thy as (th,sr),(pt, p),E, l,       t, a, v) = 
  1.1630 +       (thy,            ptp,   E, l@[L,R], e, a, v);
  1.1631 +   val (thy as (th,sr),(pt, p),E, l,       t, a, v) =
  1.1632 +       (thy,            ptp,   E,(l@[R]),  e, a, v);
  1.1633 +   *)
  1.1634 +    (case handle_leaf "next  " th sr E a v t of
  1.1635 +(* val (a', Expr s) = handle_leaf "next  " th sr E a v t;
  1.1636 +   *)
  1.1637 +	(a', Expr s) => Skip (s, E)
  1.1638 +(* val (a', STac stac) = handle_leaf "next  " th sr E a v t;
  1.1639 +   *)
  1.1640 +     | (a', STac stac) =>
  1.1641 +	let
  1.1642 +	 (*val _= writeln("### appy t, vor  stac2tac_ is="); 
  1.1643 +           val _= writeln(istate2str (ScrState (E,l,a',v,Sundef,false)));*)
  1.1644 +	   val (m,m') = stac2tac_ pt (assoc_thy th) stac
  1.1645 +       in case m of 
  1.1646 +	      Subproblem _ => Appy (m', (E,l,a',tac_2res m',Sundef,false))
  1.1647 +	    | _ => (case applicable_in p pt m of
  1.1648 +(* val Appl m' = applicable_in p pt m;
  1.1649 +   *)
  1.1650 +			Appl m' => 
  1.1651 +			((*writeln("### appy: Appy");*)
  1.1652 +			 Appy (m', (E,l,a',tac_2res m',Sundef,false)))
  1.1653 +		      | _ => ((*writeln("### appy: Napp");*)Napp E)) 
  1.1654 +	end);
  1.1655 +	 
  1.1656 +
  1.1657 +(* val (scr as Script sc, l, t as Const ("Let",_) $ _) =
  1.1658 +       (Script sc, up, go up sc);
  1.1659 +   nxt_up thy ptp (Script sc) E l ay t a v;
  1.1660 +
  1.1661 +   val (thy,ptp,scr as (Script sc),E,l, ay, t as Const ("Let",_) $ _, a, v)=
  1.1662 +       (thy,ptp,Script sc,         E,up,ay, go up sc,                 a, v);
  1.1663 +   nxt_up thy ptp scr E l ay t a v;
  1.1664 +   *)
  1.1665 +fun nxt_up thy ptp (scr as (Script sc)) E l ay
  1.1666 +    (t as Const ("Let",_) $ _) a v = (*comes from let=...*)
  1.1667 +    ((*writeln("### nxt_up1 Let$e: is=");
  1.1668 +     writeln(istate2str (ScrState (E,l,a,v,Sundef,false)));*)
  1.1669 +     if ay = Napp_
  1.1670 +    then nstep_up thy ptp scr E (drop_last l) Napp_ a v
  1.1671 +    else (*Skip_*)
  1.1672 +	let val up = drop_last l;
  1.1673 +	    val (Const ("Let",_) $ e $ (Abs (i,T,body))) = go up sc;
  1.1674 +            val i = mk_Free (i, T);
  1.1675 +            val E = upd_env E (i, v);
  1.1676 +          (*val _= writeln("### nxt_up2 Let$e: is=");
  1.1677 +            val _= writeln(istate2str (ScrState (E,l,a,v,Sundef,false)));*)
  1.1678 +	in case appy thy ptp (E) (up@[R,D]) body a v  of
  1.1679 +	       Appy lre => Appy lre
  1.1680 +	     | Napp E => nstep_up thy ptp scr E up Napp_ a v
  1.1681 +	     | Skip (v,E) => nstep_up thy ptp scr E up Skip_ a v end)
  1.1682 +	    
  1.1683 +  | nxt_up thy ptp scr E l ay
  1.1684 +    (t as Abs (_,_,_)) a v = 
  1.1685 +    ((*writeln("### nxt_up Abs: "^
  1.1686 +	     (Syntax.string_of_term (thy2ctxt (assoc_thy thy)) t));*)
  1.1687 +     nstep_up thy ptp scr E (*enr*) l ay a v)
  1.1688 +
  1.1689 +  | nxt_up thy ptp scr E l ay
  1.1690 +    (t as Const ("Let",_) $ e $ (Abs (i,T,b))) a v =
  1.1691 +    ((*writeln("### nxt_up Let$e$Abs: is=");
  1.1692 +     writeln(istate2str (ScrState (E,l,a,v,Sundef,false)));*)
  1.1693 +     (*writeln("### nxt_up Let e Abs: "^
  1.1694 +	     (Syntax.string_of_term (thy2ctxt (assoc_thy thy)) t));*)
  1.1695 +     nstep_up thy ptp scr (*upd_env*) E (*a,v)*) 
  1.1696 +	      (*eno,upd_env env (iar,res),iar,res,saf*) l ay a v)
  1.1697 +
  1.1698 +  (*no appy_: never causes Napp -> Helpless*)
  1.1699 +  | nxt_up (thy as (th,sr)) ptp scr E l _ 
  1.1700 +  (Const ("Script.While"(*1*),_) $ c $ e $ _) a v = 
  1.1701 +  if eval_true_ th sr (subst_atomic (upd_env_opt E (a,v)) c) 
  1.1702 +    then case appy thy ptp E (l@[L,R]) e a v of
  1.1703 +	     Appy lr => Appy lr
  1.1704 +	   | Napp E => nstep_up thy ptp scr E l Skip_ a v
  1.1705 +	   | Skip (v,E) => nstep_up thy ptp scr E l Skip_ a v
  1.1706 +  else nstep_up thy ptp scr E l Skip_ a v
  1.1707 +
  1.1708 +  (*no appy_: never causes Napp - Helpless*)
  1.1709 +  | nxt_up (thy as (th,sr)) ptp scr E l _ 
  1.1710 +  (Const ("Script.While"(*2*),_) $ c $ e) a v = 
  1.1711 +  if eval_true_ th sr (subst_atomic (upd_env_opt E (a,v)) c) 
  1.1712 +    then case appy thy ptp E (l@[R]) e a v of
  1.1713 +	     Appy lr => Appy lr
  1.1714 +	   | Napp E => nstep_up thy ptp scr E l Skip_ a v
  1.1715 +	   | Skip (v,E) => nstep_up thy ptp scr E l Skip_ a v
  1.1716 +  else nstep_up thy ptp scr E l Skip_ a v
  1.1717 +
  1.1718 +(* val (scr, l) = (Script sc, up);
  1.1719 +   *)
  1.1720 +  | nxt_up thy ptp scr E l ay (Const ("If",_) $ _ $ _ $ _) a v = 
  1.1721 +    nstep_up thy ptp scr E l ay a v
  1.1722 +
  1.1723 +  | nxt_up thy ptp scr E l _ (*no appy_: there was already a stac below*)
  1.1724 +  (Const ("Script.Repeat"(*1*),T) $ e $ _) a v =
  1.1725 +    (case appy thy ptp (*upd_env*) E (*a,v)*) ((l@[L,R]):loc_) e a v  of
  1.1726 +      Appy lr => Appy lr
  1.1727 +    | Napp E => ((*writeln("### nxt_up Repeat a: ");*)
  1.1728 +		 nstep_up thy ptp scr E l Skip_ a v)
  1.1729 +    | Skip (v,E) => ((*writeln("### nxt_up Repeat: Skip res ="^
  1.1730 +		(Sign.string_of_term(sign_of (assoc_thy thy)) res'));*)
  1.1731 +		    nstep_up thy ptp scr E l Skip_ a v))
  1.1732 +
  1.1733 +  | nxt_up thy ptp scr E l _ (*no appy_: there was already a stac below*)
  1.1734 +  (Const ("Script.Repeat"(*2*),T) $ e) a v =
  1.1735 +    (case appy thy ptp (*upd_env*) E (*a,v)*) ((l@[R]):loc_) e a v  of
  1.1736 +      Appy lr => Appy lr
  1.1737 +    | Napp E => ((*writeln("### nxt_up Repeat a: ");*)
  1.1738 +		 nstep_up thy ptp scr E l Skip_ a v)
  1.1739 +    | Skip (v,E) => ((*writeln("### nxt_up Repeat: Skip res ="^
  1.1740 +		(Sign.string_of_term(sign_of (assoc_thy thy)) res'));*)
  1.1741 +		    nstep_up thy ptp scr E l Skip_ a v))
  1.1742 +(* val (thy, ptp, scr, E, l,   _,(t as Const ("Script.Try",_) $ e $ _), a, v) =
  1.1743 +       (thy, ptp, (Script sc), 
  1.1744 +	               E, up, ay,(go up sc),                            a, v);
  1.1745 +   *)
  1.1746 +  | nxt_up thy ptp scr E l _ (*makes Napp to Skip*)
  1.1747 +  (t as Const ("Script.Try",_) $ e $ _) a v = 
  1.1748 +    ((*writeln("### nxt_up Try "^
  1.1749 +	     (Syntax.string_of_term (thy2ctxt (assoc_thy thy)) t));*)
  1.1750 +     nstep_up thy ptp scr E l Skip_ a v )
  1.1751 +(* val (thy, ptp, scr, E, l,   _,(t as Const ("Script.Try",_) $ e), a, v) =
  1.1752 +       (thy, ptp, (Script sc), 
  1.1753 +	               E, up, ay,(go up sc),                        a, v);
  1.1754 +   *)
  1.1755 +  | nxt_up thy ptp scr E l _ (*makes Napp to Skip*)
  1.1756 +  (t as Const ("Script.Try"(*2*),_) $ e) a v = 
  1.1757 +    ((*writeln("### nxt_up Try "^
  1.1758 +	     (Syntax.string_of_term (thy2ctxt (assoc_thy thy)) t));*)
  1.1759 +     nstep_up thy ptp scr E l Skip_ a v)
  1.1760 +
  1.1761 +
  1.1762 +  | nxt_up thy ptp scr E l ay
  1.1763 +  (Const ("Script.Or",_) $ _ $ _ $ _) a v = nstep_up thy ptp scr E l ay a v
  1.1764 +
  1.1765 +  | nxt_up thy ptp scr E l ay
  1.1766 +  (Const ("Script.Or",_) $ _ $ _) a v = nstep_up thy ptp scr E l ay a v
  1.1767 +
  1.1768 +  | nxt_up thy ptp scr E l ay
  1.1769 +  (Const ("Script.Or",_) $ _ ) a v = 
  1.1770 +    nstep_up thy ptp scr E (drop_last l) ay a v
  1.1771 +(* val (thy, ptp, scr, E, l, ay, (Const ("Script.Seq",_) $ _ $ _ $ _), a, v) =
  1.1772 +       (thy, ptp, (Script sc), 
  1.1773 +		       E, up, ay,(go up sc),                           a, v);
  1.1774 +   *)
  1.1775 +  | nxt_up thy ptp scr E l ay (*all has been done in (*2*) below*)
  1.1776 +  (Const ("Script.Seq"(*1*),_) $ _ $ _ $ _) a v =
  1.1777 +    nstep_up thy ptp scr E l ay a v
  1.1778 +(* val (thy, ptp, scr, E, l, ay, (Const ("Script.Seq",_) $ _ $ e2), a, v) =
  1.1779 +       (thy, ptp, (Script sc), 
  1.1780 +		       E, up, ay,(go up sc),                        a, v);
  1.1781 +   *)
  1.1782 +  | nxt_up thy ptp scr E l ay (*comes from e2*)
  1.1783 +	   (Const ("Script.Seq"(*2*),_) $ _ $ e2) a v =
  1.1784 +    nstep_up thy ptp scr E l ay a v
  1.1785 +(* val (thy, ptp, scr, E, l, ay, (Const ("Script.Seq",_) $ _), a, v) =
  1.1786 +       (thy, ptp, (Script sc), 
  1.1787 +		       E, up, ay,(go up sc),                   a, v);
  1.1788 +   *)
  1.1789 +  | nxt_up thy ptp (scr as Script sc) E l ay (*comes from e1*)
  1.1790 +	   (Const ("Script.Seq",_) $ _) a v = 
  1.1791 +    if ay = Napp_
  1.1792 +    then nstep_up thy ptp scr E (drop_last l) Napp_ a v
  1.1793 +    else (*Skip_*)
  1.1794 +	let val up = drop_last l;
  1.1795 +	    val Const ("Script.Seq"(*2*),_) $ _ $ e2 = go up sc;
  1.1796 +	in case appy thy ptp E (up@[R]) e2 a v  of
  1.1797 +	    Appy lr => Appy lr
  1.1798 +	  | Napp E => nstep_up thy ptp scr E up Napp_ a v
  1.1799 +	  | Skip (v,E) => nstep_up thy ptp scr E up Skip_ a v end
  1.1800 +
  1.1801 +  | nxt_up (thy,_) ptp scr E l ay t a v =
  1.1802 +  raise error ("nxt_up not impl for "^
  1.1803 +	       (Syntax.string_of_term (thy2ctxt (assoc_thy thy)) t))
  1.1804 +
  1.1805 +(* val (thy, ptp, (Script sc), E, l, ay,    a, v)=
  1.1806 +       (thy, ptp, scr,         E, l, Skip_, a, v);
  1.1807 +   val (thy, ptp, (Script sc), E, l, ay,    a, v)=
  1.1808 +       (thy, ptp, sc,          E, l, Skip_, a, v);
  1.1809 +   *)
  1.1810 +and nstep_up thy ptp (Script sc) E l ay a v = 
  1.1811 +  ((*writeln("### nstep_up from: "^(loc_2str l));
  1.1812 +   writeln("### nstep_up from: "^
  1.1813 +	   (Syntax.string_of_term (thy2ctxt (assoc_thy thy)) (go l sc)));*)
  1.1814 +   if 1 < length l
  1.1815 +   then 
  1.1816 +       let 
  1.1817 +	   val up = drop_last l; 
  1.1818 +       in ((*writeln("### nstep_up to: "^
  1.1819 +	      (Syntax.string_of_term (thy2ctxt (assoc_thy thy)) (go up sc)));*)
  1.1820 +	   nxt_up thy ptp (Script sc) E up ay (go up sc) a v ) end
  1.1821 +   else (*interpreted to end*)
  1.1822 +       if ay = Skip_ then Skip (v, E) else Napp E 
  1.1823 +);
  1.1824 +
  1.1825 +(* decide for the next applicable stac in the script;
  1.1826 +   returns (stactic, value) - the value in case the script is finished 
  1.1827 +   12.8.02:         ~~~~~ and no assumptions ??? FIXME ???
  1.1828 +   20.8.02: must return p in case of finished, because the next script
  1.1829 +            consulted need not be the calling script:
  1.1830 +            in case of detail ie. _inserted_ PrfObjs, the next stac
  1.1831 +            has to searched in a script with PblObj.status<>Complete !
  1.1832 +            (.. not true for other details ..PrfObj ??????????????????
  1.1833 +   20.8.02: do NOT return safe (is only changed in locate !!!)
  1.1834 +*)
  1.1835 +(* val (thy, (pt,p), Rfuns {next_rule=ne,...}, RrlsState (f,f',rss,_)) = 
  1.1836 +       (thy', (pt,p), sc, RrlsState (ii t));
  1.1837 +   val (thy, (pt,p), Rfuns {next_rule=ne,...}, RrlsState (f,f',rss,_)) = 
  1.1838 +       (thy', (pt',p'), sc, is');
  1.1839 +   *)
  1.1840 +fun next_tac (thy,_) (pt,p) (Rfuns {next_rule,...}) (RrlsState(f,f',rss,_))=
  1.1841 +    if f = f' then (End_Detail' (f',[])(*8.6.03*), Uistate, 
  1.1842 +		    (f', Sundef(*FIXME is no value of next_tac! vor 8.6.03*)))
  1.1843 +                                                          (*finished*)
  1.1844 +    else (case next_rule rss f of
  1.1845 +	      NONE => (Empty_Tac_, Uistate, (e_term, Sundef)) 	  (*helpless*)
  1.1846 +(* val SOME (Thm (id,thm)) = next_rule rss f;
  1.1847 +   *)
  1.1848 +	    | SOME (Thm (id,thm))(*8.6.03: muss auch f' liefern ?!!*) => 
  1.1849 +	      (Rewrite' (thy, "e_rew_ord", e_rls,(*!?!8.6.03*) false,
  1.1850 +			 (id, string_of_thmI thm), f,(e_term,[(*!?!8.6.03*)])),
  1.1851 +	       Uistate, (e_term, Sundef)))                 (*next stac*)
  1.1852 +
  1.1853 +(* val(thy, ptp as (pt,(p,_)), sc as Script (h $ body),ScrState (E,l,a,v,s,b))=
  1.1854 +      ((thy',srls), (pt,pos),  sc,                     is);
  1.1855 +   *)
  1.1856 +  | next_tac thy (ptp as (pt,(p,_)):ptree * pos') (sc as Script (h $ body)) 
  1.1857 +	     (ScrState (E,l,a,v,s,b)) =
  1.1858 +  ((*writeln("### next_tac-----------------: E= ");
  1.1859 +   writeln( istate2str (ScrState (E,l,a,v,s,b)));*)
  1.1860 +   case if l=[] then appy thy ptp E [R] body NONE v
  1.1861 +       else nstep_up thy ptp sc E l Skip_ a v of
  1.1862 +      Skip (v,_) =>                                              (*finished*)
  1.1863 +      (case par_pbl_det pt p of
  1.1864 +	   (true, p', _) => 
  1.1865 +	   let val (_,pblID,_) = get_obj g_spec pt p';
  1.1866 +	   in (Check_Postcond' (pblID, (v, [(*8.6.03 NO asms???*)])), 
  1.1867 +	       e_istate, (v,s)) end
  1.1868 +	 | (_,p',rls') => (End_Detail' (e_term,[])(*8.6.03*), e_istate, (v,s)))
  1.1869 +    | Napp _ => (Empty_Tac_, e_istate, (e_term, Sundef))         (*helpless*)
  1.1870 +    | Appy (m', scrst as (_,_,_,v,_,_)) => (m', ScrState scrst,
  1.1871 +			   (v, Sundef)))                         (*next stac*)
  1.1872 +
  1.1873 +  | next_tac _ _ _ is = raise error ("next_tac: not impl for "^
  1.1874 +				     (istate2str is));
  1.1875 +
  1.1876 +
  1.1877 +
  1.1878 +
  1.1879 +(*.create the initial interpreter state from the items of the guard.*)
  1.1880 +(* val (thy, itms, metID) = (thy, itms, mI);
  1.1881 +   *)
  1.1882 +fun init_scrstate thy itms metID =
  1.1883 +    let val actuals = itms2args thy metID itms;
  1.1884 +	val scr as Script sc = (#scr o get_met) metID;
  1.1885 +        val formals = formal_args sc
  1.1886 +	(*expects same sequence of (actual) args in itms 
  1.1887 +          and (formal) args in met*)
  1.1888 +	fun relate_args env [] [] = env
  1.1889 +	  | relate_args env _ [] = 
  1.1890 +	    raise error ("ERROR in creating the environment for '"
  1.1891 +			 ^id_of_scr sc^"' from \nthe items of the guard of "
  1.1892 +			 ^metID2str metID^",\n\
  1.1893 +			 \formal arg(s), from the script,\
  1.1894 +			 \ miss actual arg(s), from the guards env:\n"
  1.1895 +			 ^(string_of_int o length) formals
  1.1896 +			 ^" formals: "^terms2str formals^"\n"
  1.1897 +			 ^(string_of_int o length) actuals
  1.1898 +			 ^" actuals: "^terms2str actuals)
  1.1899 +	  | relate_args env [] actual_finds = env (*may drop Find!*)
  1.1900 +	  | relate_args env (a::aa) (f::ff) = 
  1.1901 +	    if type_of a = type_of f 
  1.1902 +	    then relate_args (env @ [(a, f)]) aa ff else 
  1.1903 +	    raise error ("ERROR in creating the environment for '"
  1.1904 +			 ^id_of_scr sc^"' from \nthe items of the guard of "
  1.1905 +			 ^metID2str metID^",\n\			 
  1.1906 +			 \different types of formal arg, from the script,\
  1.1907 +			 \ and actual arg, from the guards env:'\n\
  1.1908 +			 \formal: '"^term2str a^"::"^(type2str o type_of) a^"'\n\
  1.1909 +			 \actual: '"^term2str f^"::"^(type2str o type_of) f^"'\n\
  1.1910 +			 \in\n\
  1.1911 +			 \formals: "^terms2str formals^"\n\
  1.1912 +			 \actuals: "^terms2str actuals)
  1.1913 +        val env = relate_args [] formals actuals;
  1.1914 +    in (ScrState (env,[],NONE,e_term,Safe,true), scr):istate * scr end;
  1.1915 +
  1.1916 +(*.decide, where to get script/istate from:
  1.1917 +   (*1*) from PblObj.env: at begin of script if no init_form
  1.1918 +   (*2*) from PblObj/PrfObj: if stac is in the middle of the script
  1.1919 +   (*3*) from rls/PrfObj: in case of detail a ruleset.*)
  1.1920 +(* val (thy', (p,p_), pt) = (thy', (p,p_), pt);
  1.1921 +   *)
  1.1922 +fun from_pblobj_or_detail' thy' (p,p_) pt =
  1.1923 +    if member op = [Pbl,Met] p_
  1.1924 +    then case get_obj g_env pt p of
  1.1925 +	     NONE => raise error "from_pblobj_or_detail': no istate"
  1.1926 +	   | SOME is =>
  1.1927 +	     let val metID = get_obj g_metID pt p
  1.1928 +		 val {srls,...} = get_met metID
  1.1929 +	     in (srls, is, (#scr o get_met) metID) end
  1.1930 +    else
  1.1931 +    let val (pbl,p',rls') = par_pbl_det pt p
  1.1932 +    in if pbl 
  1.1933 +       then (*2*)
  1.1934 +	   let val thy = assoc_thy thy'
  1.1935 +	       val PblObj{meth=itms,...} = get_obj I pt p'
  1.1936 +	       val metID = get_obj g_metID pt p'
  1.1937 +	       val {srls,...} = get_met metID
  1.1938 +	   in (*if last_elem p = 0 (*nothing written to pt yet*)
  1.1939 +	      then let val (is, sc) = init_scrstate thy itms metID
  1.1940 +		   in (srls, is, sc) end
  1.1941 +	      else*) (srls, get_istate pt (p,p_), (#scr o get_met) metID)
  1.1942 +	   end
  1.1943 +       else (*3*)
  1.1944 +	   (e_rls, (*FIXME: get from pbl or met !!!
  1.1945 +		    unused for Rrls in locate_gen, next_tac*)
  1.1946 +	    get_istate pt (p,p_),
  1.1947 +	    case rls' of
  1.1948 +		Rls {scr=scr,...} => scr
  1.1949 +	      | Seq {scr=scr,...} => scr
  1.1950 +	      | Rrls {scr=rfuns,...} => rfuns)
  1.1951 +    end;
  1.1952 +
  1.1953 +(*.get script and istate from PblObj, see (*1*) above.*)
  1.1954 +fun from_pblobj' thy' (p,p_) pt = 
  1.1955 +    let val p' = par_pblobj pt p
  1.1956 +	val thy = assoc_thy thy'
  1.1957 +	val PblObj{meth=itms,...} = get_obj I pt p'
  1.1958 +	val metID = get_obj g_metID pt p'
  1.1959 +	val {srls,scr,...} = get_met metID
  1.1960 +    in if last_elem p = 0 (*nothing written to pt yet*)
  1.1961 +       then let val (is, scr) = init_scrstate thy itms metID
  1.1962 +	    in (srls, is, scr) end
  1.1963 +       else (srls, get_istate pt (p,p_), scr)
  1.1964 +    end;
  1.1965 +    
  1.1966 +(*.get the stactics and problems of a script as tacs
  1.1967 +  instantiated with the current environment;
  1.1968 +  l is the location which generated the given formula.*)
  1.1969 +(*WN.12.5.03: quick-and-dirty repair for listexpressions*)
  1.1970 +fun is_spec_pos Pbl = true
  1.1971 +  | is_spec_pos Met = true
  1.1972 +  | is_spec_pos _ = false;
  1.1973 +
  1.1974 +(*. fetch _all_ tactics from script .*)
  1.1975 +fun sel_rules _ (([],Res):pos') = 
  1.1976 +    raise PTREE "no tactics applicable at the end of a calculation"
  1.1977 +| sel_rules pt (p,p_) =
  1.1978 +  if is_spec_pos p_ 
  1.1979 +  then [get_obj g_tac pt p]
  1.1980 +  else
  1.1981 +    let val pp = par_pblobj pt p;
  1.1982 +	val thy' = (get_obj g_domID pt pp):theory';
  1.1983 +	val thy = assoc_thy thy';
  1.1984 +	val metID = get_obj g_metID pt pp;
  1.1985 +	val metID' =if metID =e_metID then(thd3 o snd3)(get_obj g_origin pt pp)
  1.1986 +		     else metID
  1.1987 +	val {scr=Script sc,srls,...} = get_met metID'
  1.1988 +	val ScrState (env,_,a,v,_,_) = get_istate pt (p,p_);
  1.1989 +    in map ((stac2tac pt thy) o rep_stacexpr o #2 o
  1.1990 +	    (handle_leaf "selrul" thy' srls env a v)) (stacpbls sc) end;
  1.1991 +(*
  1.1992 +> val Script sc = (#scr o get_met) ("SqRoot.thy","sqrt-equ-test");
  1.1993 +> val env = [((term_of o the o (parse Isac.thy)) "bdv",
  1.1994 +             (term_of o the o (parse Isac.thy)) "x")];
  1.1995 +> map ((stac2tac pt thy) o #2 o(subst_stacexpr env NONE e_term)) (stacpbls sc);
  1.1996 +*)
  1.1997 +
  1.1998 +
  1.1999 +(*. fetch tactics from script and filter _applicable_ tactics;
  1.2000 +    in case of Rewrite_Set* go down to _atomic_ rewrite-tactics .*)
  1.2001 +fun sel_appl_atomic_tacs _ (([],Res):pos') = 
  1.2002 +    raise PTREE "no tactics applicable at the end of a calculation"
  1.2003 +  | sel_appl_atomic_tacs pt (p,p_) =
  1.2004 +    if is_spec_pos p_ 
  1.2005 +    then [get_obj g_tac pt p]
  1.2006 +    else
  1.2007 +	let val pp = par_pblobj pt p
  1.2008 +	    val thy' = (get_obj g_domID pt pp):theory'
  1.2009 +	    val thy = assoc_thy thy'
  1.2010 +	    val metID = get_obj g_metID pt pp
  1.2011 +	    val metID' =if metID = e_metID 
  1.2012 +			then (thd3 o snd3) (get_obj g_origin pt pp)
  1.2013 +			else metID
  1.2014 +	    val {scr=Script sc,srls,erls,rew_ord'=ro,...} = get_met metID'
  1.2015 +	    val ScrState (env,_,a,v,_,_) = get_istate pt (p,p_)
  1.2016 +	    val alltacs = (*we expect at least 1 stac in a script*)
  1.2017 +		map ((stac2tac pt thy) o rep_stacexpr o #2 o
  1.2018 +		     (handle_leaf "selrul" thy' srls env a v)) (stacpbls sc)
  1.2019 +	    val f = case p_ of
  1.2020 +			Frm => get_obj g_form pt p
  1.2021 +		      | Res => (fst o (get_obj g_result pt)) p
  1.2022 +	(*WN071231 ? replace atomic_appl_tacs with applicable_in (ineff!) ?*)
  1.2023 +	in (distinct o flat o 
  1.2024 +	    (map (atomic_appl_tacs thy ro erls f))) alltacs end;
  1.2025 +	
  1.2026 +
  1.2027 +(*
  1.2028 +end
  1.2029 +open Interpreter;
  1.2030 +*)
  1.2031 +
  1.2032 +(* use"ME/script.sml";
  1.2033 +   use"script.sml";
  1.2034 +   *)