src/Tools/isac/ME/script.sml
author Walther Neuper <neuper@ist.tugraz.at>
Thu, 12 Aug 2010 11:02:32 +0200
branchisac-update-Isa09-2
changeset 37906 e2b23ba9df13
child 37922 30eff896074c
permissions -rw-r--r--
moved isac + test to final dire-structure
     1 (* interpreter for scripts
     2    (c) Walther Neuper 2000
     3 
     4 use"ME/script.sml";
     5 use"script.sml";
     6 *)
     7 signature INTERPRETER =
     8 sig
     9   (*type ets (list of executed tactics) see sequent.sml*)
    10 
    11   datatype locate
    12     = NotLocatable
    13     | Steps of (tac_ * mout * ptree * pos' * cid * safe (* ets*)) list
    14 (*    | ToDo of ets 28.4.02*)
    15 
    16   (*diss: next-tactic-function*)
    17   val next_tac : theory' -> ptree * pos' -> metID -> scr -> ets -> tac_
    18   (*diss: locate-function*)
    19   val locate_gen : theory'
    20                    -> tac_
    21                       -> ptree * pos' -> scr * rls -> ets -> loc_ -> locate
    22 
    23   val sel_rules : ptree -> pos' -> tac list
    24   val init_form : scr -> ets -> loc_ * term option (*FIXME not up to date*)
    25   val formal_args : term -> term list
    26 
    27   (*shift to library ...*)
    28   val inst_abs : theory' -> term -> term
    29   val itms2args : metID -> itm list -> term list
    30   val user_interrupt : loc_ * (tac_ * env * env * term * term * safe)
    31   (*val empty : term*) 
    32 end 
    33 
    34 
    35 
    36 
    37 (*
    38 structure Interpreter : INTERPRETER =
    39 struct
    40 *)
    41 
    42 (*.traces the leaves (ie. non-tactical nodes) of the script
    43    found by next_tac.
    44    a leaf is either a tactic or an 'exp' in 'let v = expr'
    45    where 'exp' does not contain a tactic.*)   
    46 val trace_script = ref false;
    47 
    48 type step =     (*data for creating a new node in the ptree;
    49 		 designed for use:
    50                	 fun ass* scrstate steps =
    51                	 ... case ass* scrstate steps of
    52                	     Assoc (scrstate, steps) => ... ass* scrstate steps*)
    53     tac_       (*transformed from associated tac*)
    54     * mout       (*result with indentation etc.*)
    55     * ptree      (*containing node created by tac_ + resp. scrstate*)
    56     * pos'       (*position in ptree; ptree * pos' is the proofstate*)
    57     * pos' list; (*of ptree-nodes probably cut (by fst tac_)*)
    58 val e_step = (Empty_Tac_, EmptyMout, EmptyPtree, e_pos',[]:pos' list):step;
    59 
    60 fun rule2thm' (Thm (id, thm)) = (id, string_of_thmI thm):thm'
    61   | rule2thm' r = raise error ("rule2thm': not defined for "^(rule2str r));
    62 fun rule2rls' (Rls_ rls) = id_rls rls
    63   | rule2rls' r = raise error ("rule2rls': not defined for "^(rule2str r));
    64 
    65 (*.makes a (rule,term) list to a Step (m, mout, pt', p', cid) for solve;
    66    complicated with current t in rrlsstate.*)
    67 fun rts2steps steps ((pt,p),(f,f'',rss,rts),(thy',ro,er,pa)) [(r, (f', am))] =
    68     let val thy = assoc_thy thy'
    69 	val m = Rewrite' (thy',ro,er,pa, rule2thm' r, f, (f', am))
    70 	val is = RrlsState (f',f'',rss,rts)
    71 	val p = case p of (p',Frm) => p | (p',Res) => (lev_on p',Res)
    72 	val (p', cid, mout, pt') = generate1 thy m is p pt
    73     in (is, (m, mout, pt', p', cid)::steps) end
    74   | rts2steps steps ((pt,p),(f,f'',rss,rts),(thy',ro,er,pa)) 
    75 	      ((r, (f', am))::rts') =
    76     let val thy = assoc_thy thy'
    77 	val m = Rewrite' (thy',ro,er,pa, rule2thm' r, f, (f', am))
    78 	val is = RrlsState (f',f'',rss,rts)
    79 	val p = case p of (p',Frm) => p | (p',Res) => (lev_on p',Res)
    80 	val (p', cid, mout, pt') = generate1 thy m is p pt
    81     in rts2steps ((m, mout, pt', p', cid)::steps) 
    82 		 ((pt',p'),(f',f'',rss,rts),(thy',ro,er,pa)) rts' end;
    83 
    84 
    85 (*. functions for the environment stack .*)
    86 fun accessenv id es = the (assoc((top es):env, id))
    87     handle _ => error ("accessenv: "^(free2str id)^" not in env");
    88 fun updateenv id vl (es:env stack) = 
    89     (push (overwrite(top es, (id, vl))) (pop es)):env stack;
    90 fun pushenv id vl (es:env stack) = 
    91     (push (overwrite(top es, (id, vl))) es):env stack;
    92 val popenv = pop:env stack -> env stack;
    93 
    94 
    95 
    96 fun de_esc_underscore str =
    97   let fun scan [] = []
    98 	| scan (s::ss) = if s = "'" then (scan ss)
    99 			 else (s::(scan ss))
   100   in (implode o scan o explode) str end;
   101 (*
   102 > val str = "Rewrite_Set_Inst";
   103 > val esc = esc_underscore str;
   104 val it = "Rewrite'_Set'_Inst" : string
   105 > val des = de_esc_underscore esc;
   106  val des = de_esc_underscore esc;*)
   107 
   108 
   109 (*WN.12.5.03 not used any more,
   110   tacs are more stable than listepxr: subst_tacexpr
   111 fun is_listexpr t = 
   112   (((ids_of o head_of) t) inter (!listexpr)) <> [];
   113 ----*)
   114 
   115 (*go at a location in a script and fetch the contents*)
   116 fun go [] t = t
   117   | go (D::p) (Abs(s,ty,t0)) = go (p:loc_) t0
   118   | go (L::p) (t1 $ t2) = go p t1
   119   | go (R::p) (t1 $ t2) = go p t2
   120   | go l _ = raise error ("go: no "^(loc_2str l));
   121 (*
   122 > val t = (term_of o the o (parse thy)) "a+b";
   123 val it = Const (#,#) $ Free (#,#) $ Free ("b","RealDef.real") : term
   124 > val plus_a = go [L] t; 
   125 > val b = go [R] t; 
   126 > val plus = go [L,L] t; 
   127 > val a = go [L,R] t;
   128 
   129 > val t = (term_of o the o (parse thy)) "a+b+c";
   130 val t = Const (#,#) $ (# $ # $ Free #) $ Free ("c","RealDef.real") : term
   131 > val pl_pl_a_b = go [L] t; 
   132 > val c = go [R] t; 
   133 > val a = go [L,R,L,R] t; 
   134 > val b = go [L,R,R] t; 
   135 *)
   136 
   137 
   138 (* get a subterm t with test t, and record location *)
   139 fun get l test (t as Const (s,T)) = 
   140     if test t then Some (l,t) else None
   141   | get l test (t as Free (s,T)) = 
   142     if test t then Some (l,t) else None 
   143   | get l test (t as Bound n) =
   144     if test t then Some (l,t) else None 
   145   | get l test (t as Var (s,T)) =
   146     if test t then Some (l,t) else None
   147   | get l test (t as Abs (s,T,body)) =
   148     if test t then Some (l:loc_,t) else get ((l@[D]):loc_) test body
   149   | get l test (t as t1 $ t2) =
   150     if test t then Some (l,t) 
   151     else case get (l@[L]) test t1 of 
   152       None => get (l@[R]) test t2
   153     | Some (l',t') => Some (l',t');
   154 (*18.6.00
   155 > val sss = ((term_of o the o (parse thy))
   156   "Script Solve_root_equation (eq_::bool) (v_::real) (err_::bool) =\
   157    \ (let e_ = Try (Rewrite square_equation_left True eq_) \
   158    \  in [e_])");
   159           ______ compares head_of !!
   160 > get [] (eq_str "Let") sss;            [R]
   161 > get [] (eq_str "Script.Try") sss;     [R,L,R]
   162 > get [] (eq_str "Script.Rewrite") sss; [R,L,R,R]
   163 > get [] (eq_str "True") sss;           [R,L,R,R,L,R]
   164 > get [] (eq_str "e_") sss;             [R,R]
   165 *)
   166 
   167 fun test_negotiable t = ((strip_thy o (term_str Script.thy) o head_of) t) 
   168   mem (!negotiable);
   169 
   170 (*30.4.02: vvv--- doesnt work with curried functions ---> get_tac ------
   171 (*18.6.00: below _ALL_ negotiables must be in fun-patterns !
   172   then the last (non)pattern must be a subproblem*)
   173 fun init_frm thy (Const ("Script.Rewrite",_) $ _ $ _ $ eq) = Some eq
   174   | init_frm thy (Const ("Script.Rewrite'_Inst",_) $ _ $ _ $ _ $ eq) = Some eq
   175   | init_frm thy (Const ("Script.Rewrite'_Set",_) $ _ $ _ $ eq) = Some eq
   176   | init_frm thy (Const ("Script.Rewrite'_Set'_Inst",_) $ _ $ _ $ _ $ eq) = 
   177     Some eq
   178   | init_frm thy (Const ("Script.Calculate",_) $ _ $ t) = Some t
   179   | init_frm thy t = 
   180   (*if ((strip_thy o (term_str thy) o head_of) t) mem (!subpbls)
   181     then None 
   182   else *)raise error ("init_frm: not impl. for "^
   183 		    (Sign.string_of_term (sign_of thy) t));
   184 
   185 > val t = (term_of o the o (parse thy)) 
   186  "Rewrite square_equation_left True (sqrt(#9+#4*x)=sqrt x + sqrt(#5+x))";
   187 > val Some ini = init_frm thy t;
   188 > Sign.string_of_term (sign_of thy) ini;
   189 val it = "sqrt (#9 + #4 * x) = sqrt x + sqrt (#5 + x)" : string
   190 
   191 > val t = (term_of o the o (parse thy)) 
   192  "solve_univar (Reals, [univar,equation], no_met) e1_ v1_";
   193 > val ini = init_frm thy t;
   194 > Sign.string_of_term (sign_of thy) ini;
   195 val it = "empty" : string
   196 
   197 > val t = (term_of o the o (parse thy)) 
   198  "Rewrite_Set norm_equation False x + #1 = #2";
   199 > val Some ini = init_frm thy t;
   200 > Sign.string_of_term (sign_of thy) ini;
   201 val it = "x + #1 = #2" : string                                                
   202 
   203 > val t = (term_of o the o (parse thy)) 
   204  "Rewrite_Set_Inst [(bdv,x)] isolate_bdv False x + #1 = #2";
   205 > val Some ini = init_frm thy t;
   206 > Sign.string_of_term (sign_of thy) ini;
   207 val it = "x + #1 = #2" : string                           *)
   208 
   209 
   210 (*.get argument of first stactic in a script for init_form.*)
   211 fun get_stac thy (h $ body) =
   212 (* 
   213    *)
   214   let
   215     fun get_t y (Const ("Script.Seq",_) $ e1 $ e2) a = 
   216     	(case get_t y e1 a of None => get_t y e2 a | la => la)
   217       | get_t y (Const ("Script.Seq",_) $ e1 $ e2 $ a) _ = 
   218     	(case get_t y e1 a of None => get_t y e2 a | la => la)
   219       | get_t y (Const ("Script.Try",_) $ e) a = get_t y e a
   220       | get_t y (Const ("Script.Try",_) $ e $ a) _ = get_t y e a
   221       | get_t y (Const ("Script.Repeat",_) $ e) a = get_t y e a
   222       | get_t y (Const ("Script.Repeat",_) $ e $ a) _ = get_t y e a
   223       | get_t y (Const ("Script.Or",_) $e1 $ e2) a =
   224     	(case get_t y e1 a of None => get_t y e2 a | la => la)
   225       | get_t y (Const ("Script.Or",_) $e1 $ e2 $ a) _ =
   226     	(case get_t y e1 a of None => get_t y e2 a | la => la)
   227       | get_t y (Const ("Script.While",_) $ c $ e) a = get_t y e a
   228       | get_t y (Const ("Script.While",_) $ c $ e $ a) _ = get_t y e a
   229       | get_t y (Const ("Script.Letpar",_) $ e1 $ Abs (_,_,e2)) a = 
   230     	(case get_t y e1 a of None => get_t y e2 a | la => la)
   231     (*| get_t y (Const ("Let",_) $ e1 $ Abs (_,_,e2)) a =
   232     	(writeln("get_t: Let e1= "^(term2str e1)^", e2= "^(term2str e2));
   233 	 case get_t y e1 a of None => get_t y e2 a | la => la)
   234       | get_t y (Abs (_,_,e)) a = get_t y e a*)
   235       | get_t y (Const ("Let",_) $ e1 $ Abs (_,_,e2)) a =
   236     	get_t y e1 a (*don't go deeper without evaluation !*)
   237       | get_t y (Const ("If",_) $ c $ e1 $ e2) a = None
   238     	(*(case get_t y e1 a of None => get_t y e2 a | la => la)*)
   239     
   240       | get_t y (Const ("Script.Rewrite",_) $ _ $ _ $ a) _ = Some a
   241       | get_t y (Const ("Script.Rewrite",_) $ _ $ _    ) a = Some a
   242       | get_t y (Const ("Script.Rewrite'_Inst",_) $ _ $ _ $ _ $ a) _ = Some a
   243       | get_t y (Const ("Script.Rewrite'_Inst",_) $ _ $ _ $ _ )    a = Some a
   244       | get_t y (Const ("Script.Rewrite'_Set",_) $ _ $ _ $ a) _ = Some a
   245       | get_t y (Const ("Script.Rewrite'_Set",_) $ _ $ _ )    a = Some a
   246       | get_t y (Const ("Script.Rewrite'_Set'_Inst",_) $ _ $ _ $ _ $a)_ =Some a
   247       | get_t y (Const ("Script.Rewrite'_Set'_Inst",_) $ _ $ _ $ _ )  a =Some a
   248       | get_t y (Const ("Script.Calculate",_) $ _ $ a) _ = Some a
   249       | get_t y (Const ("Script.Calculate",_) $ _ )    a = Some a
   250     
   251       | get_t y (Const ("Script.Substitute",_) $ _ $ a) _ = Some a
   252       | get_t y (Const ("Script.Substitute",_) $ _ )    a = Some a
   253     
   254       | get_t y (Const ("Script.SubProblem",_) $ _ $ _) _ = None
   255 
   256       | get_t y x _ =  
   257 	((*writeln ("### get_t yac: list-expr "^(term2str x));*)
   258 	 None)
   259 in get_t thy body e_term end;
   260     
   261 (*FIXME: get 1st stac by next_stac [] instead of ... ?? 29.7.02*)
   262 (* val Script sc = scr;
   263    *)
   264 fun init_form thy (Script sc) env =
   265   (case get_stac thy sc of
   266      None => None (*raise error ("init_form: no 1st stac in "^
   267 			  (Sign.string_of_term (sign_of thy) sc))*)
   268    | Some stac => Some (subst_atomic env stac))
   269   | init_form _ _ _ = raise error "init_form: no match";
   270 
   271 (* use"ME/script.sml";
   272    use"script.sml";
   273    *)
   274 
   275 
   276 
   277 (*the 'iteration-argument' of a stac (args not eval)*)
   278 fun itr_arg _ (Const ("Script.Rewrite'_Inst",_) $ _ $ _ $ _ $ v) = v
   279   | itr_arg _ (Const ("Script.Rewrite",_) $ _ $ _ $ v) = v
   280   | itr_arg _ (Const ("Script.Rewrite'_Set'_Inst",_) $ _ $ _ $ _ $ v) = v
   281   | itr_arg _ (Const ("Script.Rewrite'_Set",_) $ _ $ _ $ v) = v
   282   | itr_arg _ (Const ("Script.Calculate",_) $ _ $ v) = v
   283   | itr_arg _ (Const ("Script.Check'_elementwise",_) $ consts $ _) = consts
   284   | itr_arg _ (Const ("Script.Or'_to'_List",_) $ _) = e_term
   285   | itr_arg _ (Const ("Script.Tac",_) $ _) = e_term
   286   | itr_arg _ (Const ("Script.SubProblem",_) $ _ $ _) = e_term
   287   | itr_arg thy t = raise error 
   288     ("itr_arg not impl. for "^
   289      (Sign.string_of_term (sign_of (assoc_thy thy)) t));
   290 (* val t = (term_of o the o (parse thy))"Rewrite rroot_square_inv False e_";
   291 > itr_arg "Script.thy" t;
   292 val it = Free ("e_","RealDef.real") : term 
   293 > val t = (term_of o the o (parse thy))"xxx";
   294 > itr_arg "Script.thy" t;
   295 *** itr_arg not impl. for xxx
   296 uncaught exception ERROR
   297   raised at: library.ML:1114.35-1114.40*)
   298 
   299 
   300 (*.get the arguments of the script out of the scripts parsetree.*)
   301 fun formal_args scr = (fst o split_last o snd o strip_comb) scr;
   302 (*
   303 > formal_args scr;
   304   [Free ("f_","RealDef.real"),Free ("v_","RealDef.real"),
   305    Free ("eqs_","bool List.list")] : term list
   306 *)
   307 
   308 (*.get the identifier of the script out of the scripts parsetree.*)
   309 fun id_of_scr sc = (id_of o fst o strip_comb) sc;
   310 
   311 
   312 (*WN020526: not clear, when a is available in ass_up for eva-_true*)
   313 (*WN060906: in "fun handle_leaf" eg. uses "Some M__"(from some PREVIOUS
   314   curried Rewrite) for CURRENT value (which may be different from PREVIOUS);
   315   thus "None" must be set at the end of currying (ill designed anyway)*)
   316 fun upd_env_opt env (Some a, v) = upd_env env (a,v)
   317   | upd_env_opt env (None, v) = 
   318     (writeln("*** upd_env_opt: (None,"^(term2str v)^")");env);
   319 
   320 
   321 type dsc = typ; (*<-> nam..unknow in Descript.thy*)
   322 fun typ_str (Type (s,_)) = s
   323   | typ_str (TFree(s,_)) = s
   324   | typ_str (TVar ((s,i),_)) = s^(string_of_int i);
   325 	     
   326 (*get the _result_-type of a description*)
   327 fun dsc_valT (Const (_,(Type (_,[_,T])))) = (strip_thy o typ_str) T;
   328 (*> val t = (term_of o the o (parse thy)) "equality";
   329 > val T = type_of t;
   330 val T = "bool => Tools.una" : typ
   331 > val dsc = dsc_valT t;
   332 val dsc = "una" : string
   333 
   334 > val t = (term_of o the o (parse thy)) "fixedValues";
   335 > val T = type_of t;
   336 val T = "bool List.list => Tools.nam" : typ
   337 > val dsc = dsc_valT t;
   338 val dsc = "nam" : string*)
   339 
   340 (*.from penv in itm_ make args for script depending on type of description.*)
   341 (*6.5.03 TODO: push penv into script -- and drop mk_arg here || drop penv
   342   9.5.03 penv postponed: penv = env for script at the moment, (*mk_arg*)*)
   343 fun mk_arg thy d [] = raise error ("mk_arg: no data for "^
   344 			       (Sign.string_of_term (sign_of thy) d))
   345   | mk_arg thy d [t] = 
   346     (case dsc_valT d of
   347 	 "una" => [t]
   348        | "nam" => 
   349 	 [case t of
   350 	      r as (Const ("op =",_) $ _ $ _) => r
   351 	    | _ => raise error 
   352 			     ("mk_arg: dsc-typ 'nam' applied to non-equality "^
   353 			      (Sign.string_of_term (sign_of thy) t))]
   354        | s => raise error ("mk_arg: not impl. for "^s))
   355     
   356   | mk_arg thy d (t::ts) = (mk_arg thy d [t]) @ (mk_arg thy d ts);
   357 (* 
   358  val d = d_in itm_;
   359  val [t] = ts_in itm_;
   360 mk_arg thy
   361 *)
   362 
   363 
   364 
   365 
   366 (*.create the actual parameters (args) of script: their order 
   367   is given by the order in met.pat .*)
   368 (*WN.5.5.03: ?: does this allow for different descriptions ???
   369              ?: why not taken from formal args of script ???
   370 !: FIXXXME penv: push it here in itms2args into script-evaluation*)
   371 (* val (thy, mI, itms) = (thy, metID, itms);
   372    *)
   373 fun itms2args thy mI (itms:itm list) =
   374     let val mvat = max_vt itms
   375 	fun okv mvat (_,vats,b,_,_) = mvat mem vats andalso b
   376 	val itms = filter (okv mvat) itms
   377 	fun test_dsc d (_,_,_,_,itm_) = (d = d_in itm_)
   378 	fun itm2arg itms (_,(d,_)) =
   379 	    case find_first (test_dsc d) itms of
   380 		None => 
   381 		raise error ("itms2args: '"^term2str d^"' not in itms")
   382 	      (*| Some (_,_,_,_,itm_) => mk_arg thy (d_in itm_) (ts_in itm_);
   383                penv postponed; presently penv holds already env for script*)
   384 	      | Some (_,_,_,_,itm_) => penvval_in itm_
   385 	fun sel_given_find (s,_) = (s = "#Given") orelse (s = "#Find")
   386 	val pats = (#ppc o get_met) mI
   387     in (flat o (map (itm2arg itms))) pats end;
   388 (*
   389 > val sc = ... Solve_root_equation ...
   390 > val mI = ("Script.thy","sqrt-equ-test");
   391 > val PblObj{meth={ppc=itms,...},...} = get_obj I pt [];
   392 > val ts = itms2args thy mI itms;
   393 > map (Sign.string_of_term (sign_of thy)) ts;
   394 ["sqrt (#9 + #4 * x) = sqrt x + sqrt (#5 + x)","x","#0"] : string list
   395 *)
   396 
   397 
   398 (*["bool_ (1+x=2)","real_ x"] --match_ags--> oris 
   399   --oris2fmz_vals--> ["equality (1+x=2)","boundVariable x","solutions L"]*)
   400 fun oris2fmz_vals oris =
   401     let fun ori2fmz_vals ((_,_,_,dsc,ts):ori) = 
   402 	    ((term2str o comp_dts') (dsc, ts), last_elem ts) 
   403 	    handle _ => raise error ("ori2fmz_env called with "^terms2str ts)
   404     in (split_list o (map ori2fmz_vals)) oris end;
   405 
   406 (*detour necessary, because generate1 delivers a string-result*)
   407 fun mout2term thy (Form' (FormKF (_,_,_,_,res))) = 
   408   (term_of o the o (parse (assoc_thy thy))) res
   409   | mout2term thy (Form' (PpcKF _)) = e_term;(*3.8.01: res of subpbl 
   410 					   at time of detection in script*)
   411 
   412 (*.convert a script-tac 'stac' to a tactic 'tac'; if stac is an initac,
   413    then convert to a 'tac_' (as required in appy).
   414    arg pt:ptree for pushing the thy specified in rootpbl into subpbls.*)
   415 fun stac2tac_ pt thy (Const ("Script.Rewrite",_) $ Free (thmID,_) $ _ $ f) =
   416 (* val (pt, thy, (Const ("Script.Rewrite",_) $ Free (thmID,_) $ _ $ f)) = 
   417        (pt, (assoc_thy th), stac);
   418    *)
   419     let val tid = (de_esc_underscore o strip_thy) thmID
   420     in (Rewrite (tid, (string_of_thmI o 
   421 		       (assoc_thm' thy)) (tid,"")), Empty_Tac_) end
   422 (* val (thy,
   423 	mm as(Const ("Script.Rewrite'_Inst",_) $  sub $ Free(thmID,_) $ _ $ f))
   424      = (assoc_thy th,stac);
   425    stac2tac_ pt thy mm;
   426 
   427    assoc_thm' (assoc_thy "Isac.thy") (tid,"");
   428    assoc_thm' Isac.thy (tid,"");
   429    *)
   430   | stac2tac_ pt thy (Const ("Script.Rewrite'_Inst",_) $ 
   431 	       sub $ Free (thmID,_) $ _ $ f) =
   432   let val subML = ((map isapair2pair) o isalist2list) sub
   433     val subStr = subst2subs subML
   434     val tid = (de_esc_underscore o strip_thy) thmID (*4.10.02 unnoetig*)
   435   in (Rewrite_Inst 
   436 	  (subStr, (tid, (string_of_thmI o
   437 			  (assoc_thm' thy)) (tid,""))), Empty_Tac_) end
   438       
   439   | stac2tac_ pt thy (Const ("Script.Rewrite'_Set",_) $ Free (rls,_) $ _ $ f)=
   440   (Rewrite_Set ((de_esc_underscore o strip_thy) rls), Empty_Tac_)
   441 
   442   | stac2tac_ pt thy (Const ("Script.Rewrite'_Set'_Inst",_) $ 
   443 	       sub $ Free (rls,_) $ _ $ f) =
   444   let val subML = ((map isapair2pair) o isalist2list) sub;
   445     val subStr = subst2subs subML;
   446   in (Rewrite_Set_Inst (subStr,rls), Empty_Tac_) end
   447 
   448   | stac2tac_ pt thy (Const ("Script.Calculate",_) $ Free (op_,_) $ f) =
   449   (Calculate op_, Empty_Tac_)
   450 
   451   | stac2tac_ pt thy (Const ("Script.Take",_) $ t) =
   452   (Take (term2str t), Empty_Tac_)
   453 
   454   | stac2tac_ pt thy (Const ("Script.Substitute",_) $ isasub $ arg) =
   455   (Substitute ((subte2sube o isalist2list) isasub), Empty_Tac_)
   456 (* val t = str2term"Substitute [x = L, M_b L = 0] (M_b x = q_0 * x + c)";
   457    val Const ("Script.Substitute", _) $ isasub $ arg = t;
   458    *)
   459 
   460 (*12.1.01.*)
   461   | stac2tac_ pt thy (Const("Script.Check'_elementwise",_) $ _ $ 
   462 		    (set as Const ("Collect",_) $ Abs (_,_,pred))) = 
   463   (Check_elementwise (Sign.string_of_term (sign_of thy) pred), 
   464    (*set*)Empty_Tac_)
   465 
   466   | stac2tac_ pt thy (Const("Script.Or'_to'_List",_) $ _ ) = 
   467   (Or_to_List, Empty_Tac_)
   468 
   469 (*12.1.01.for subproblem_equation_dummy in root-equation *)
   470   | stac2tac_ pt thy (Const ("Script.Tac",_) $ Free (str,_)) = 
   471   (Tac ((de_esc_underscore o strip_thy) str),  Empty_Tac_) 
   472 		    (*L_ will come from pt in appl_in*)
   473 
   474   (*3.12.03 copied from assod SubProblem*)
   475 (* val Const ("Script.SubProblem",_) $
   476 			 (Const ("Pair",_) $
   477 				Free (dI',_) $ 
   478 				(Const ("Pair",_) $ pI' $ mI')) $ ags' =
   479     str2term 
   480     "SubProblem (EqSystem_, [linear, system], [no_met])\
   481     \            [bool_list_ [c_2 = 0, L * c + c_2 = q_0 * L ^^^ 2 / 2],\
   482     \             real_list_ [c, c_2]]";
   483 *)
   484   | stac2tac_ pt thy (stac as Const ("Script.SubProblem",_) $
   485 			 (Const ("Pair",_) $
   486 				Free (dI',_) $ 
   487 			(Const ("Pair",_) $ pI' $ mI')) $ ags') =
   488 (*compare "| assod _ (Subproblem'"*)
   489     let val dI = ((implode o drop_last(*.._*) o explode) dI')^".thy";
   490         val thy = maxthy (assoc_thy dI) (rootthy pt);
   491 	val pI = ((map (de_esc_underscore o free2str)) o isalist2list) pI';
   492 	val mI = ((map (de_esc_underscore o free2str)) o isalist2list) mI';
   493 	val ags = isalist2list ags';
   494 	val (pI, pors, mI) = 
   495 	    if mI = ["no_met"] 
   496 	    then let val pors = (match_ags thy ((#ppc o get_pbt) pI) ags)
   497 			 handle _ =>(match_ags_msg pI stac ags(*raise exn*);[])
   498 		     val pI' = refine_ori' pors pI;
   499 		 in (pI', pors (*refinement over models with diff.prec only*), 
   500 		     (hd o #met o get_pbt) pI') end
   501 	    else (pI, (match_ags thy ((#ppc o get_pbt) pI) ags)
   502 		  handle _ => (match_ags_msg pI stac ags(*raise exn*); []), 
   503 		  mI);
   504         val (fmz_, vals) = oris2fmz_vals pors;
   505 	val {cas,ppc,thy,...} = get_pbt pI
   506 	val dI = theory2theory' thy (*.take dI from _refined_ pbl.*)
   507 	val dI = theory2theory' (maxthy (assoc_thy dI) (rootthy pt));
   508 	val hdl = case cas of
   509 		      None => pblterm dI pI
   510 		    | Some t => subst_atomic ((vars_of_pbl_' ppc) ~~~ vals) t
   511         val f = subpbl (strip_thy dI) pI
   512     in (Subproblem (dI, pI),
   513 	Subproblem' ((dI, pI, mI), pors, hdl, fmz_, f))
   514     end
   515 
   516   | stac2tac_ pt thy t = raise error 
   517   ("stac2tac_ TODO: no match for "^
   518    (Sign.string_of_term (sign_of thy) t));
   519 (*
   520 > val t = (term_of o the o (parse thy)) 
   521  "Rewrite_Set_Inst [(bdv,v_::real)] isolate_bdv False (x=a+#1)";
   522 > stac2tac_ pt t;
   523 val it = Rewrite_Set_Inst ([(#,#)],"isolate_bdv") : tac
   524 
   525 > val t = (term_of o the o (parse SqRoot.thy)) 
   526 "(SubProblem (SqRoot_,[equation,univariate],(SqRoot_,solve_linear))\
   527    \         [bool_ e_, real_ v_])::bool list";
   528 > stac2tac_ pt SqRoot.thy t;
   529 val it = (Subproblem ("SqRoot.thy",[#,#]),Const (#,#) $ (# $ # $ (# $ #)))
   530 *)
   531 
   532 fun stac2tac pt thy t = (fst o stac2tac_ pt thy) t;
   533 
   534 
   535 
   536 
   537 (*test a term for being a _list_ (set ?) of constants; could be more rigorous*)
   538 fun list_of_consts (Const ("List.list.Cons",_) $ _ $ _) = true
   539   | list_of_consts (Const ("List.list.Nil",_)) = true
   540   | list_of_consts _ = false;
   541 (*val ttt = (term_of o the o (parse thy)) "[x=#1,x=#2,x=#3]";
   542 > list_of_consts ttt;
   543 val it = true : bool
   544 > val ttt = (term_of o the o (parse thy)) "[]";
   545 > list_of_consts ttt;
   546 val it = true : bool*)
   547 
   548 
   549 
   550 
   551 
   552 (* 15.1.01: evaluation of preds only works occasionally,
   553             but luckily for the 2 examples of root-equ:
   554 > val s = ((term_of o the o (parse thy)) "x",
   555 	   (term_of o the o (parse thy)) "-#5//#12");
   556 > val asm = (term_of o the o (parse thy)) 
   557              "#0 <= #9 + #4 * x  &  #0 <= sqrt x + sqrt (#-3 + x)";
   558 > val pred = subst_atomic [s] asm;
   559 > rewrite_set_ thy false (cterm_of (sign_of thy) pred);
   560 val it = None : (cterm * cterm list) option !!!!!!!!!!!!!!!!!!!!!!!!!!!!
   561 > eval_true' (string_of_thy thy) "eval_rls" (subst_atomic [s] pred);
   562 val it = false : bool
   563 
   564 > val s = ((term_of o the o (parse thy)) "x",
   565 	   (term_of o the o (parse thy)) "#4");
   566 > val asm = (term_of o the o (parse thy)) 
   567              "#0 <= #9 + #4 * x  &  #0 <= sqrt x + sqrt (#5 + x)";
   568 > val pred = subst_atomic [s] asm;
   569 > rewrite_set_ thy false (cterm_of (sign_of thy) pred);
   570 val it = Some ("True & True",[]) : (cterm * cterm list) option
   571 > eval_true' (string_of_thy thy) "eval_rls" (subst_atomic [s] pred);
   572 val it = true : bool`*)
   573 
   574 (*for check_elementwise: take apart the set, ev. instantiate assumptions
   575 fun rep_set thy pt p (set as Const ("Collect",_) $ Abs _) =
   576   let val (_ $ Abs (bdv,T,pred)) = inst_abs thy set;
   577     val bdv = Free (bdv,T);
   578     val pred = if pred <> Const ("Script.Assumptions",bool)
   579 		 then pred 
   580 	       else (mk_and o (map fst)) (get_assumptions_ pt (p,Res))
   581   in (bdv, pred) end
   582   | rep_set thy _ _ set = 
   583     raise error ("check_elementwise: no set "^ (*from script*)
   584 		 (Sign.string_of_term (sign_of thy) set));
   585 (*> val set = (term_of o the o (parse thy)) "{(x::real). Assumptions}";
   586 > val p = [];
   587 > val pt = union_asm pt p [("#0 <= sqrt x + sqrt (#5 + x)",[11]),
   588                            ("#0 <= #9 + #4 * x",[22]),
   589 			   ("#0 <= x ^^^ #2 + #5 * x",[33]),
   590 			   ("#0 <= #2 + x",[44])];
   591 > val (bdv,pred) = rep_set thy pt p set;
   592 val bdv = Free ("x","RealDef.real") : term
   593 > writeln (Sign.string_of_term (sign_of thy) pred);
   594 ((#0 <= sqrt x + sqrt (#5 + x) & #0 <= #9 + #4 * x) &
   595  #0 <= x ^^^ #2 + #5 * x) &
   596 #0 <= #2 + x
   597 *)
   598 --------------------------------------------11.6.03--was unused*)
   599 
   600 
   601 
   602 
   603 datatype ass = 
   604   Ass of tac_ *  (*SubProblem gets args instantiated in assod*)
   605 	 term      (*for itr_arg,result in ets*)
   606 | AssWeak of tac_ *
   607 	     term  (*for itr_arg,result in ets*)
   608 | NotAss;
   609 
   610 (*.assod: tac_ associated with stac w.r.t. d
   611 args
   612  pt:ptree for pushing the thy specified in rootpbl into subpbls
   613 returns
   614  Ass    : associated: e.g. thmID in stac = thmID in m
   615                        +++ arg   in stac = arg   in m
   616  AssWeak: weakly ass.:e.g. thmID in stac = thmID in m, //arg//
   617  NotAss :             e.g. thmID in stac/=/thmID in m (not =)
   618 8.01:
   619  tac_ SubProblem with args completed from script
   620 .*)
   621 fun assod pt d (m as Rewrite_Inst' (thy',rod,rls,put,subs,(thmID,thm),f,(f',asm))) stac =
   622     (case stac of
   623 	 (Const ("Script.Rewrite'_Inst",_) $ subs_ $ Free (thmID_,idT) $b$f_)=>
   624 	 if thmID = thmID_ then 
   625 	     if f = f_ then ((*writeln"3### assod ..Ass";*)Ass (m,f')) 
   626 	     else ((*writeln"3### assod ..AssWeak";*)AssWeak(m, f'))
   627 	 else ((*writeln"3### assod ..NotAss";*)NotAss)
   628        | (Const ("Script.Rewrite'_Set'_Inst",_) $ sub_ $ Free (rls_,_) $_$f_)=>
   629 	 if contains_rule (Thm (thmID, refl(*dummy*))) (assoc_rls rls_) then 
   630 	     if f = f_ then Ass (m,f') else AssWeak (m,f')
   631 	 else NotAss
   632        | _ => NotAss)
   633 
   634   | assod pt d (m as Rewrite' (thy,rod,rls,put,(thmID,thm),f,(f',asm))) stac =
   635     (case stac of
   636 	 (t as Const ("Script.Rewrite",_) $ Free (thmID_,idT) $ b $ f_) =>
   637 	 ((*writeln("3### assod: stac = "^
   638 		    (Sign.string_of_term (sign_of (assoc_thy thy)) t));
   639 	   writeln("3### assod: f(m)= "^
   640 		   (Sign.string_of_term (sign_of (assoc_thy thy)) f));*)
   641 	  if thmID = thmID_ then 
   642 	      if f = f_ then ((*writeln"3### assod ..Ass";*)Ass (m,f')) 
   643 	      else ((*writeln"### assod ..AssWeak";
   644 		     writeln("### assod: f(m)  = "^
   645 			     (Sign.string_of_term (sign_of(assoc_thy thy)) f));
   646 		     writeln("### assod: f(stac)= "^
   647 			     (Sign.string_of_term(sign_of(assoc_thy thy))f_))*)
   648 		    AssWeak (m,f'))
   649 	  else ((*writeln"3### assod ..NotAss";*)NotAss))
   650        | (Const ("Script.Rewrite'_Set",_) $ Free (rls_,_) $ _ $ f_) =>
   651 	 if contains_rule (Thm (thmID, refl(*dummy*))) (assoc_rls rls_) then
   652 	      if f = f_ then Ass (m,f') else AssWeak (m,f')
   653 	  else NotAss
   654        | _ => NotAss)
   655 
   656 (*val f = (term_of o the o (parse thy))"#0+(sqrt(sqrt(sqrt a))^^^#2)^^^#2=#0";
   657 > val f'= (term_of o the o (parse thy))"#0+(sqrt(sqrt a))^^^#2=#0";
   658 > val m =   Rewrite'("Script.thy","tless_true","eval_rls",false,
   659  ("rroot_square_inv",""),f,(f',[]));
   660 > val stac = (term_of o the o (parse thy))
   661  "Rewrite rroot_square_inv False (#0+(sqrt(sqrt(sqrt a))^^^#2)^^^#2=#0)";
   662 > assod e_rls m stac;
   663 val it =
   664   (Some (Rewrite' (#,#,#,#,#,#,#)),Const ("empty","RealDef.real"),
   665    Const ("empty","RealDef.real")) : tac_ option * term * term*)
   666 
   667   | assod pt d (m as Rewrite_Set_Inst' (thy',put,sub,rls,f,(f',asm))) 
   668   (Const ("Script.Rewrite'_Set'_Inst",_) $ sub_ $ Free (rls_,_) $ _ $ f_)= 
   669   if id_rls rls = rls_ then 
   670     if f = f_ then Ass (m,f') else AssWeak (m,f')
   671   else NotAss
   672 
   673   | assod pt d (m as Detail_Set_Inst' (thy',put,sub,rls,f,(f',asm))) 
   674   (Const ("Script.Rewrite'_Set'_Inst",_) $ sub_ $ Free (rls_,_) $ _ $ f_)= 
   675   if id_rls rls = rls_ then 
   676     if f = f_ then Ass (m,f') else AssWeak (m,f')
   677   else NotAss
   678 
   679   | assod pt d (m as Rewrite_Set' (thy,put,rls,f,(f',asm))) 
   680   (Const ("Script.Rewrite'_Set",_) $ Free (rls_,_) $ _ $ f_) = 
   681   if id_rls rls = rls_ then 
   682     if f = f_ then Ass (m,f') else AssWeak (m,f')
   683   else NotAss
   684 
   685   | assod pt d (m as Detail_Set' (thy,put,rls,f,(f',asm))) 
   686   (Const ("Script.Rewrite'_Set",_) $ Free (rls_,_) $ _ $ f_) = 
   687   if id_rls rls = rls_ then 
   688     if f = f_ then Ass (m,f') else AssWeak (m,f')
   689   else NotAss
   690 
   691   | assod pt d (m as Calculate' (thy',op_,f,(f',thm'))) stac =
   692     (case stac of
   693 	 (Const ("Script.Calculate",_) $ Free (op__,_) $ f_) =>
   694 	 if op_ = op__ then
   695 	     if f = f_ then Ass (m,f') else AssWeak (m,f')
   696 	 else NotAss
   697        | (Const ("Script.Rewrite'_Set'_Inst",_) $ sub_ $ Free(rls_,_) $_$f_)=> 
   698 	 if contains_rule (Calc (snd (assoc1 (!calclist', op_)))) 
   699 			  (assoc_rls rls_) then
   700 	     if f = f_ then Ass (m,f') else AssWeak (m,f')
   701 	 else NotAss
   702        | (Const ("Script.Rewrite'_Set",_) $ Free (rls_, _) $ _ $ f_) =>
   703 	 if contains_rule (Calc (snd (assoc1 (!calclist', op_)))) 
   704 			  (assoc_rls rls_) then
   705 	     if f = f_ then Ass (m,f') else AssWeak (m,f')
   706 	 else NotAss
   707        | _ => NotAss)
   708 
   709   | assod pt _ (m as Check_elementwise' (consts,_,(consts_chkd,_)))
   710     (Const ("Script.Check'_elementwise",_) $ consts' $ _) =
   711     ((*writeln("### assod Check'_elementwise: consts= "^(term2str consts)^
   712 	     ", consts'= "^(term2str consts'));
   713      atomty consts; atomty consts';*)
   714      if consts = consts' then ((*writeln"### assod Check'_elementwise: Ass";*)
   715 			       Ass (m, consts_chkd))
   716      else ((*writeln"### assod Check'_elementwise: NotAss";*) NotAss))
   717 
   718   | assod pt _ (m as Or_to_List' (ors, list)) 
   719 	  (Const ("Script.Or'_to'_List",_) $ _) =
   720 	  Ass (m, list) 
   721 
   722   | assod pt _ (m as Take' term) 
   723 	  (Const ("Script.Take",_) $ _) =
   724 	  Ass (m, term)
   725 
   726   | assod pt _ (m as Substitute' (_, _, res)) 
   727 	  (Const ("Script.Substitute",_) $ _ $ _) =
   728 	  Ass (m, res) 
   729 (* val t = str2term "Substitute [(x, 3)] (x^^^2 + x + 1)";
   730    val (Const ("Script.Substitute",_) $ _ $ _) = t;
   731    *)
   732 
   733   | assod pt _ (m as Tac_ (thy,f,id,f'))  
   734     (Const ("Script.Tac",_) $ Free (id',_)) =
   735     if id = id' then Ass (m, ((term_of o the o (parse thy)) f'))
   736     else NotAss
   737 
   738 
   739 (* val t = str2term 
   740               "SubProblem (DiffApp_,[make,function],[no_met]) \
   741 	      \[real_ m_, real_ v_, bool_list_ rs_]";
   742 
   743  val (Subproblem' ((domID,pblID,metID),_,_,_,f)) = m;
   744  val (Const ("Script.SubProblem",_) $
   745 		 (Const ("Pair",_) $
   746 			Free (dI',_) $
   747 			(Const ("Pair",_) $ pI' $ mI')) $ ags') = stac;
   748  *)
   749   | assod pt _ (Subproblem' ((domID,pblID,metID),_,_,_,f))
   750 	  (stac as Const ("Script.SubProblem",_) $
   751 		 (Const ("Pair",_) $
   752 			Free (dI',_) $ 
   753 			(Const ("Pair",_) $ pI' $ mI')) $ ags') =
   754 (*compare "| stac2tac_ thy (Const ("Script.SubProblem",_)"*)
   755     let val dI = ((implode o drop_last o explode) dI')^".thy";
   756         val thy = maxthy (assoc_thy dI) (rootthy pt);
   757 	val pI = ((map (de_esc_underscore o free2str)) o isalist2list) pI';
   758 	val mI = ((map (de_esc_underscore o free2str)) o isalist2list) mI';
   759 	val ags = isalist2list ags';
   760 	val (pI, pors, mI) = 
   761 	    if mI = ["no_met"] 
   762 	    then let val pors = (match_ags thy ((#ppc o get_pbt) pI) ags)
   763 			 handle _=>(match_ags_msg pI stac ags(*raise exn*);[]);
   764 		     val pI' = refine_ori' pors pI;
   765 		 in (pI', pors (*refinement over models with diff.prec only*), 
   766 		     (hd o #met o get_pbt) pI') end
   767 	    else (pI, (match_ags thy ((#ppc o get_pbt) pI) ags)
   768 		      handle _ => (match_ags_msg pI stac ags(*raise exn*);[]), 
   769 		  mI);
   770         val (fmz_, vals) = oris2fmz_vals pors;
   771 	val {cas, ppc,...} = get_pbt pI
   772 	val {cas, ppc, thy,...} = get_pbt pI
   773 	val dI = theory2theory' thy (*take dI from _refined_ pbl*)
   774 	val dI = theory2theory' (maxthy (assoc_thy dI) (rootthy pt))
   775 	val hdl = case cas of
   776 		      None => pblterm dI pI
   777 		    | Some t => subst_atomic ((vars_of_pbl_' ppc) ~~~ vals) t
   778         val f = subpbl (strip_thy dI) pI
   779     in if domID = dI andalso pblID = pI
   780        then Ass (Subproblem' ((dI, pI, mI), pors, hdl, fmz_, f), f) 
   781        else NotAss
   782     end
   783 
   784   | assod pt d m t = 
   785     (if (!trace_script) 
   786      then writeln("@@@ the 'tac_' proposed to apply does NOT match the leaf found in the script:\n"^
   787 		  "@@@ tac_ = "^(tac_2str m))
   788      else ();
   789      NotAss);
   790 
   791 
   792 
   793 fun tac_2tac (Refine_Tacitly' (pI,_,_,_,_)) = Refine_Tacitly pI
   794   | tac_2tac (Model_Problem' (pI,_,_))      = Model_Problem
   795   | tac_2tac (Add_Given' (t,_))             = Add_Given t
   796   | tac_2tac (Add_Find' (t,_))              = Add_Find t
   797   | tac_2tac (Add_Relation' (t,_))          = Add_Relation t
   798  
   799   | tac_2tac (Specify_Theory' dI)           = Specify_Theory dI
   800   | tac_2tac (Specify_Problem' (dI,_))      = Specify_Problem dI
   801   | tac_2tac (Specify_Method' (dI,_,_))     = Specify_Method dI
   802   
   803   | tac_2tac (Rewrite' (thy,rod,erls,put,(thmID,thm),f,(f',asm))) =
   804     Rewrite (thmID,thm)
   805 
   806   | tac_2tac (Rewrite_Inst' (thy,rod,erls,put,sub,(thmID,thm),f,(f',asm)))=
   807     Rewrite_Inst (subst2subs sub,(thmID,thm))
   808 
   809   | tac_2tac (Rewrite_Set' (thy,put,rls,f,(f',asm))) = 
   810     Rewrite_Set (id_rls rls)
   811 
   812   | tac_2tac (Detail_Set' (thy,put,rls,f,(f',asm))) = 
   813     Detail_Set (id_rls rls)
   814 
   815   | tac_2tac (Rewrite_Set_Inst' (thy,put,sub,rls,f,(f',asm))) = 
   816     Rewrite_Set_Inst (subst2subs sub,id_rls rls)
   817 
   818   | tac_2tac (Detail_Set_Inst' (thy,put,sub,rls,f,(f',asm))) = 
   819     Detail_Set_Inst (subst2subs sub,id_rls rls)
   820 
   821   | tac_2tac (Calculate' (thy,op_,t,(t',thm'))) = Calculate (op_)
   822 
   823   | tac_2tac (Check_elementwise' (consts,pred,consts')) =
   824     Check_elementwise pred
   825 
   826   | tac_2tac (Or_to_List' _) = Or_to_List
   827   | tac_2tac (Take' term) = Take (term2str term)
   828   | tac_2tac (Substitute' (subte, t, res)) = Substitute (subte2sube subte) 
   829 
   830   | tac_2tac (Tac_ (_,f,id,f')) = Tac id
   831 
   832   | tac_2tac (Subproblem' ((domID, pblID, _), _, _,_,_)) = 
   833 		  Subproblem (domID, pblID)
   834   | tac_2tac (Check_Postcond' (pblID, _)) = 
   835 		  Check_Postcond pblID
   836   | tac_2tac Empty_Tac_ = Empty_Tac
   837 
   838   | tac_2tac m = 
   839   raise error ("tac_2tac: not impl. for "^(tac_2str m));
   840 
   841 
   842 
   843 
   844 (** decompose tac_ to a rule and to (lhs,rhs)
   845     unly needed                            ~~~ **)
   846 
   847 val idT = Type ("Script.ID",[]);
   848 (*val tt = (term_of o the o (parse thy)) "square_equation_left::ID";
   849 type_of tt = idT;
   850 val it = true : bool
   851 *)
   852 (* 13.3.01
   853 v
   854 *)
   855 fun make_rule thy t =
   856   let val ct = cterm_of (sign_of thy) (Trueprop $ t)
   857   in Thm (string_of_cterm ct, make_thm ct) end;
   858 
   859 (* val (Rewrite_Inst'(thy',rod,rls,put,subs,(thmID,thm),f,(f',asm)))=m;
   860    *)
   861 (*decompose tac_ to a rule and to (lhs,rhs) for ets FIXME.12.03: obsolete!
   862  NOTE.12.03: also used for msg 'not locatable' ?!: 'Subproblem' missing !!!
   863 WN0508 only use in tac_2res, which uses only last return-value*)
   864 fun rep_tac_ (Rewrite_Inst' 
   865 		 (thy',rod,rls,put,subs,(thmID,thm),f,(f',asm))) = 
   866   let val fT = type_of f;
   867     val b = if put then HOLogic.true_const else HOLogic.false_const;
   868     val sT = (type_of o fst o hd) subs;
   869     val subs' = list2isalist (HOLogic.mk_prodT (sT, sT))
   870       (map HOLogic.mk_prod subs);
   871     val sT' = type_of subs';
   872     val lhs = Const ("Script.Rewrite'_Inst",[sT',idT,(*fT*)bool,fT] ---> fT) 
   873       $ subs' $ Free (thmID,idT) $ b $ f;
   874   in (((make_rule (assoc_thy thy')) o HOLogic.mk_eq) (lhs,f'),(lhs,f')) end
   875 (*Fehlersuche 25.4.01
   876 (a)----- als String zusammensetzen:
   877 ML> Sign.string_of_term (sign_of thy)f; 
   878 val it = "d_d x #4 + d_d x (x ^^^ #2 + #3 * x)" : string
   879 ML> Sign.string_of_term (sign_of thy)f'; 
   880 val it = "#0 + d_d x (x ^^^ #2 + #3 * x)" : string
   881 ML> subs;
   882 val it = [(Free ("bdv","RealDef.real"),Free ("x","RealDef.real"))] : subst
   883 > val tt = (term_of o the o (parse thy))
   884   "(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))";
   885 > atomty tt;
   886 ML> writeln(Sign.string_of_term (sign_of thy)tt); 
   887 (Rewrite_Inst [(bdv,x)] diff_const False d_d x #4 + d_d x (x ^^^ #2 + #3 * x)) =
   888  #0 + d_d x (x ^^^ #2 + #3 * x)
   889 
   890 (b)----- laut rep_tac_:
   891 > val ttt=HOLogic.mk_eq (lhs,f');
   892 > atomty ttt;
   893 
   894 
   895 (*Fehlersuche 1-2Monate vor 4.01:*)
   896 > val tt = (term_of o the o (parse thy))
   897   "Rewrite_Inst[(bdv,x)]square_equation_left True(x=#1+#2)";
   898 > atomty tt;
   899 
   900 > val f = (term_of o the o (parse thy)) "x=#1+#2";
   901 > val f' = (term_of o the o (parse thy)) "x=#3";
   902 > val subs = [((term_of o the o (parse thy)) "bdv",
   903 	       (term_of o the o (parse thy)) "x")];
   904 > val sT = (type_of o fst o hd) subs;
   905 > val subs' = list2isalist (HOLogic.mk_prodT (sT, sT))
   906 			      (map HOLogic.mk_prod subs);
   907 > val sT' = type_of subs';
   908 > val lhs = Const ("Script.Rewrite'_Inst",[sT',idT,fT,fT] ---> fT) 
   909   $ subs' $ Free (thmID,idT) $ HOLogic.true_const $ f;
   910 > lhs = tt;
   911 val it = true : bool
   912 > rep_tac_ (Rewrite_Inst' 
   913 	       ("Script.thy","tless_true","eval_rls",false,subs,
   914 		("square_equation_left",""),f,(f',[])));
   915 *)
   916   | rep_tac_ (Rewrite' (thy',rod,rls,put,(thmID,thm),f,(f',asm)))=
   917   let 
   918     val fT = type_of f;
   919     val b = if put then HOLogic.true_const else HOLogic.false_const;
   920     val lhs = Const ("Script.Rewrite",[idT,HOLogic.boolT,fT] ---> fT)
   921       $ Free (thmID,idT) $ b $ f;
   922   in (((make_rule (assoc_thy thy')) o HOLogic.mk_eq) (lhs,f'),(lhs,f')) end
   923 (* 
   924 > val tt = (term_of o the o (parse thy)) (*____   ____..test*)
   925   "Rewrite square_equation_left True (x=#1+#2) = (x=#3)";
   926 
   927 > val f = (term_of o the o (parse thy)) "x=#1+#2";
   928 > val f' = (term_of o the o (parse thy)) "x=#3";
   929 > val Thm (id,thm) = 
   930   rep_tac_ (Rewrite' 
   931    ("Script.thy","tless_true","eval_rls",false,
   932     ("square_equation_left",""),f,(f',[])));
   933 > val Some ct = parse thy   
   934   "Rewrite square_equation_left True (x=#1+#2)"; 
   935 > rewrite_ Script.thy tless_true eval_rls true thm ct;
   936 val it = Some ("x = #3",[]) : (cterm * cterm list) option
   937 *)
   938   | rep_tac_ (Rewrite_Set_Inst' 
   939 		 (thy',put,subs,rls,f,(f',asm))) =
   940     (e_rule, (e_term, f'))
   941 (*WN050824: type error ...
   942   let val fT = type_of f;
   943     val sT = (type_of o fst o hd) subs;
   944     val subs' = list2isalist (HOLogic.mk_prodT (sT, sT))
   945       (map HOLogic.mk_prod subs);
   946     val sT' = type_of subs';
   947     val b = if put then HOLogic.true_const else HOLogic.false_const
   948     val lhs = Const ("Script.Rewrite'_Set'_Inst",
   949 		     [sT',idT,fT,fT] ---> fT) 
   950       $ subs' $ Free (id_rls rls,idT) $ b $ f;
   951   in (((make_rule (assoc_thy thy')) o HOLogic.mk_eq) (lhs,f'),(lhs,f')) end*)
   952 (* ... vals from Rewrite_Inst' ...
   953 > rep_tac_ (Rewrite_Set_Inst' 
   954 	       ("Script.thy",false,subs,
   955 		"isolate_bdv",f,(f',[])));
   956 *)
   957 (* val (Rewrite_Set' (thy',put,rls,f,(f',asm)))=m;
   958 *)
   959   | rep_tac_ (Rewrite_Set' (thy',put,rls,f,(f',asm)))=
   960   let val fT = type_of f;
   961     val b = if put then HOLogic.true_const else HOLogic.false_const;
   962     val lhs = Const ("Script.Rewrite'_Set",[idT,bool,fT] ---> fT) 
   963       $ Free (id_rls rls,idT) $ b $ f;
   964   in (((make_rule (assoc_thy thy')) o HOLogic.mk_eq) (lhs,f'),(lhs,f')) end
   965 (* 13.3.01:
   966 val thy = assoc_thy thy';
   967 val t = HOLogic.mk_eq (lhs,f');
   968 make_rule thy t;
   969 --------------------------------------------------
   970 val lll = (term_of o the o (parse thy)) 
   971   "Rewrite_Set SqRoot_simplify False (d_d x (x ^^^ #2 + #3 * x) + d_d x #4)";
   972 
   973 --------------------------------------------------
   974 > val f = (term_of o the o (parse thy)) "x=#1+#2";
   975 > val f' = (term_of o the o (parse thy)) "x=#3";
   976 > val Thm (id,thm) = 
   977   rep_tac_ (Rewrite_Set' 
   978    ("Script.thy",false,"SqRoot_simplify",f,(f',[])));
   979 val id = "(Rewrite_Set SqRoot_simplify True x = #1 + #2) = (x = #3)" : string
   980 val thm = "(Rewrite_Set SqRoot_simplify True x = #1 + #2) = (x = #3)" : thm
   981 *)
   982   | rep_tac_ (Calculate' (thy',op_,f,(f',thm')))=
   983   let val fT = type_of f;
   984     val lhs = Const ("Script.Calculate",[idT,fT] ---> fT) 
   985       $ Free (op_,idT) $ f
   986   in (((make_rule (assoc_thy thy')) o HOLogic.mk_eq) (lhs,f'),(lhs,f')) end
   987 (*
   988 > val lhs'=(term_of o the o (parse thy))"Calculate plus (#1+#2)";
   989   ... test-root-equ.sml: calculate ...
   990 > val Appl m'=applicable_in p pt (Calculate "plus");
   991 > val (lhs,_)=tac_2etac m';
   992 > lhs'=lhs;
   993 val it = true : bool*)
   994   | rep_tac_ (Check_elementwise' (t,str,(t',asm)))  = (Erule, (e_term, t'))
   995   | rep_tac_ (Subproblem' (_,_,_,_,t'))  = (Erule, (e_term, t'))
   996   | rep_tac_ (Take' (t'))  = (Erule, (e_term, t'))
   997   | rep_tac_ (Substitute' (subst,t,t'))  = (Erule, (t, t'))
   998   | rep_tac_ (Or_to_List' (t, t'))  = (Erule, (t, t'))
   999   | rep_tac_ m = raise error ("rep_tac_: not impl.for "^
  1000 				 (tac_2str m));
  1001 
  1002 (*"N.3.6.03------
  1003 fun tac_2rule m = (fst o rep_tac_) m;
  1004 fun tac_2etac m = (snd o rep_tac_) m;
  1005 fun tac_2tac m = (fst o snd o rep_tac_) m;*)
  1006 fun tac_2res m = (snd o snd o rep_tac_) m;(*ONLYuse of rep_tac_
  1007 					        FIXXXXME: simplify rep_tac_*)
  1008 
  1009 
  1010 (*.handle a leaf;
  1011    a leaf is either a tactic or an 'exp' in 'let v = expr'
  1012    where 'exp' does not contain a tactic.
  1013    handling a leaf comprises
  1014    (1) 'subst_stacexpr' substitute env and complete curried tactic
  1015    (2) rewrite the leaf by 'srls'
  1016 WN060906 quick and dirty fix: return a' too (for updating E later)
  1017 .*)
  1018 fun handle_leaf call thy srls E a v t =
  1019     (*WN050916 'upd_env_opt' is a blind copy from previous version*)
  1020     case subst_stacexpr E a v t of
  1021 	(a', STac stac) => (*script-tactic*)
  1022 	let val stac' = eval_listexpr_ (assoc_thy thy) srls
  1023 			(subst_atomic (upd_env_opt E (a,v)) stac)
  1024 	in (if (!trace_script) 
  1025 	    then writeln ("@@@ "^call^" leaf '"^term2str t^"' ---> STac '"^
  1026 			  term2str stac'^"'")
  1027 	    else ();
  1028 	    (a', STac stac'))
  1029 	end
  1030       | (a', Expr lexpr) => (*leaf-expression*)
  1031 	let val lexpr' = eval_listexpr_ (assoc_thy thy) srls
  1032 			 (subst_atomic (upd_env_opt E (a,v)) lexpr)
  1033 	in (if (!trace_script) 
  1034 	    then writeln("@@@ "^call^" leaf '"^term2str t^"' ---> Expr '"^
  1035 			 term2str lexpr'^"'")
  1036 	    else ();
  1037 	    (a', Expr lexpr'))
  1038 	end;
  1039 
  1040 
  1041 
  1042 (** locate an applicable stactic in a script **)
  1043 
  1044 datatype assoc = (*ExprVal in the sense of denotational semantics*)
  1045   Assoc of     (*the stac is associated, strongly or weakly*)
  1046   scrstate *       (*the current; returned for next_tac etc. outside ass* *)  
  1047   (step list)    (*list of steps done until associated stac found;
  1048 	           initiated with the data for doing the 1st step,
  1049                    thus the head holds these data further on,
  1050 		   while the tail holds steps finished (incl.scrstate in ptree)*)
  1051 | NasApp of   (*stac not associated, but applicable, ptree-node generated*)
  1052   scrstate * (step list)
  1053 | NasNap of     (*stac not associated, not applicable, nothing generated;
  1054 	         for distinction in Or, for leaving iterations, leaving Seq,
  1055 		 evaluate scriptexpressions*)
  1056   term * env;
  1057 fun assoc2str (Assoc     _) = "Assoc"
  1058   | assoc2str (NasNap  _) = "NasNap"
  1059   | assoc2str (NasApp _) = "NasApp";
  1060 
  1061 
  1062 datatype asap = (*arg. of assy _only_ for distinction w.r.t. Or*)
  1063   Aundef   (*undefined: set only by (topmost) Or*)
  1064 | AssOnly  (*do not execute appl stacs - there could be an associated
  1065 	     in parallel Or-branch*)
  1066 | AssGen;  (*no Ass(Weak) found within Or, thus 
  1067              search for _applicable_ stacs, execute and generate pt*)
  1068 (*this constructions doesnt allow arbitrary nesting of Or !!!*)
  1069 
  1070 
  1071 (*assy, ass_up, astep_up scanning for locate_gen at stactic in a script.
  1072   search is clearly separated into (1)-(2):
  1073   (1) assy is recursive descent;
  1074   (2) ass_up resumes interpretation at a location somewhere in the script;
  1075       astep_up does only get to the parentnode of the scriptexpr.
  1076   consequence:
  1077   * call of (2) means _always_ that in this branch below
  1078     there was an appl.stac (Repeat, Or e1, ...)
  1079 *)
  1080 fun assy ya (is as (E,l,a,v,S,b),ss)
  1081 	  (Const ("Let",_) $ e $ (Abs (id,T,body))) =
  1082 (* val (ya, (is as (E,l,a,v,S,b),ss),Const ("Let",_) $ e $ (Abs (id,T,body))) =
  1083   (*1*)(((ts,d),Aundef), ((E,[R],a,v,S,b),[(m,EmptyMout,pt,p,[])]), body);
  1084    *)
  1085     ((*writeln("### assy Let$e$Abs: is=");
  1086      writeln(istate2str (ScrState is));*)
  1087      case assy ya ((E , l@[L,R], a,v,S,b),ss) e of
  1088 	 NasApp ((E',l,a,v,S,bb),ss) => 
  1089 	 let val id' = mk_Free (id, T);
  1090 	     val E' = upd_env E' (id', v);
  1091 	 (*val _=writeln("### assy Let -> NasApp");*)
  1092 	 in assy ya ((E', l@[R,D], a,v,S,b),ss) body end
  1093      | NasNap (v,E) => 	 
  1094 	 let val id' = mk_Free (id, T);
  1095 	   val E' = upd_env E (id', v);
  1096 	   (*val _=writeln("### assy Let -> NasNap");*)
  1097 	 in assy ya ((E', l@[R,D], a,v,S,b),ss) body end
  1098      | ay => ay)
  1099 
  1100   | assy (ya as (((thy,srls),_),_)) ((E,l,_,v,S,b),ss) 
  1101 	 (Const ("Script.While",_) $ c $ e $ a) =
  1102     ((*writeln("### assy While $ c $ e $ a, upd_env= "^
  1103 	     (subst2str (upd_env E (a,v))));*)
  1104      if eval_true_ thy srls (subst_atomic (upd_env E (a,v)) c) 
  1105      then assy ya ((E, l@[L,R], Some a,v,S,b),ss)  e
  1106      else NasNap (v, E))
  1107    
  1108   | assy (ya as (((thy,srls),_),_)) ((E,l,a,v,S,b),ss) 
  1109 	 (Const ("Script.While",_) $ c $ e) =
  1110     ((*writeln("### assy While, l= "^(loc_2str l));*)
  1111      if eval_true_ thy srls (subst_atomic (upd_env_opt E (a,v)) c) 
  1112      then assy ya ((E, l@[R], a,v,S,b),ss) e
  1113      else NasNap (v, E)) 
  1114 
  1115   | assy (ya as (((thy,srls),_),_)) ((E,l,a,v,S,b),ss) 
  1116 	 (Const ("If",_) $ c $ e1 $ e2) =
  1117     (if eval_true_ thy srls (subst_atomic (upd_env_opt E (a,v)) c) 
  1118      then assy ya ((E, l@[L,R], a,v,S,b),ss) e1
  1119      else assy ya ((E, l@[  R], a,v,S,b),ss) e2) 
  1120 
  1121   | assy ya ((E,l,_,v,S,b),ss) (Const ("Script.Try",_) $ e $ a) =
  1122   ((*writeln("### assy Try $ e $ a, l= "^(loc_2str l));*)
  1123     case assy ya ((E, l@[L,R], Some a,v,S,b),ss) e of
  1124      ay => ay) 
  1125 
  1126   | assy ya ((E,l,a,v,S,b),ss) (Const ("Script.Try",_) $ e) =
  1127   ((*writeln("### assy Try $ e, l= "^(loc_2str l));*)
  1128     case assy ya ((E, l@[R], a,v,S,b),ss) e of
  1129      ay => ay)
  1130 (* val (ya, ((E,l,_,v,S,b),ss), (Const ("Script.Seq",_) $e1 $ e2 $ a)) = 
  1131   (*2*)(ya, ((E , l@[L,R], a,v,S,b),ss), e);
  1132    *)
  1133   | assy ya ((E,l,_,v,S,b),ss) (Const ("Script.Seq",_) $e1 $ e2 $ a) =
  1134     ((*writeln("### assy Seq $e1 $ e2 $ a, E= "^(subst2str E));*)
  1135      case assy ya ((E, l@[L,L,R], Some a,v,S,b),ss) e1 of
  1136 	 NasNap (v, E) => assy ya ((E, l@[L,R], Some a,v,S,b),ss) e2
  1137        | NasApp ((E,_,_,v,_,_),ss) => 
  1138 	 assy ya ((E, l@[L,R], Some a,v,S,b),ss) e2
  1139        | ay => ay)
  1140 
  1141   | assy ya ((E,l,a,v,S,b),ss) (Const ("Script.Seq",_) $e1 $ e2) =
  1142     (case assy ya ((E, l@[L,R], a,v,S,b),ss) e1 of
  1143 	 NasNap (v, E) => assy ya ((E, l@[R], a,v,S,b),ss) e2
  1144        | NasApp ((E,_,_,v,_,_),ss) => 
  1145 	 assy ya ((E, l@[R], a,v,S,b),ss) e2
  1146        | ay => ay)
  1147     
  1148   | assy ya ((E,l,_,v,S,b),ss) (Const ("Script.Repeat",_) $ e $ a) =
  1149     assy ya ((E,(l@[L,R]),Some a,v,S,b),ss) e
  1150 
  1151   | assy ya ((E,l,a,v,S,b),ss) (Const ("Script.Repeat",_) $ e) =
  1152     assy ya ((E,(l@[R]),a,v,S,b),ss) e
  1153 
  1154 (*15.6.02: ass,app Or nochmals "uberlegen FIXXXME*)
  1155   | assy (y, Aundef) ((E,l,_,v,S,b),ss) (Const ("Script.Or",_) $e1 $ e2 $ a) =
  1156     (case assy (y, AssOnly) ((E,(l@[L,L,R]),Some a,v,S,b),ss) e1 of
  1157 	 NasNap (v, E) => 
  1158 	 (case assy (y, AssOnly) ((E,(l@[L,R]),Some a,v,S,b),ss) e2 of
  1159 	      NasNap (v, E) => 
  1160 	      (case assy (y, AssGen) ((E,(l@[L,L,R]),Some a,v,S,b),ss) e1 of
  1161 	       NasNap (v, E) => 
  1162 	       assy (y, AssGen) ((E, (l@[L,R]), Some a,v,S,b),ss) e2
  1163 	     | ay => ay)
  1164 	    | ay =>(ay))
  1165        | NasApp _ => raise error ("assy: FIXXXME ///must not return NasApp///")
  1166        | ay => (ay))
  1167 
  1168   | assy ya ((E,l,a,v,S,b),ss) (Const ("Script.Or",_) $e1 $ e2) =
  1169     (case assy ya ((E,(l@[L,R]),a,v,S,b),ss) e1 of
  1170 	 NasNap (v, E) => 
  1171 	 assy ya ((E,(l@[R]),a,v,S,b),ss) e2
  1172        | ay => (ay)) 
  1173 (* val ((m,_,pt,(p,p_),c)::ss) = [(m,EmptyMout,pt,p,[])];
  1174    val t = (term_of o the o (parse Isac.thy)) "Rewrite rmult_1 False";
  1175 
  1176    val (ap,(p,p_),c,ss) = (Aundef,p,[],[]);
  1177    assy (((thy',srls),d),ap) ((E,l,a,v,S,b), (m,EmptyMout,pt,(p,p_),c)::ss) t;
  1178 val ((((thy',sr),d),ap), (is as (E,l,a,v,S,b), (m,_,pt,(p,p_),c)::ss), t) =
  1179     ();
  1180    *) 
  1181 
  1182   | assy (((thy',sr),d),ap) (is as (E,l,a,v,S,b), (m,_,pt,(p,p_),c)::ss) t =
  1183     ((*writeln("### assy, m = "^tac_2str m);
  1184      writeln("### assy, (p,p_) = "^pos'2str (p,p_));
  1185      writeln("### assy, is= ");
  1186      writeln(istate2str (ScrState is));*)
  1187      case handle_leaf "locate" thy' sr E a v t of
  1188 	(a', Expr s) => 
  1189 	((*writeln("### assy: listexpr t= "^(term2str t)); 
  1190          writeln("### assy, E= "^(env2str E));
  1191 	 writeln("### assy, eval(..)= "^(term2str
  1192 	       (eval_listexpr_ (assoc_thy thy') sr
  1193 			       (subst_atomic (upd_env_opt E (a',v)) t))));*)
  1194 	  NasNap (eval_listexpr_ (assoc_thy thy') sr
  1195 			       (subst_atomic (upd_env_opt E (a',v)) t), E))
  1196       (* val (_,STac stac) = subst_stacexpr E a v t;
  1197          *)
  1198       | (a', STac stac) =>
  1199 	let (*val _=writeln("### assy, stac = "^term2str stac);*)
  1200 	    val p' = case p_ of Frm => p | Res => lev_on p
  1201 			      | _ => raise error ("assy: call by "^
  1202 						  (pos'2str (p,p_)));
  1203 	in case assod pt d m stac of
  1204 	 Ass (m,v') =>
  1205 	 let (*val _=writeln("### assy: Ass ("^tac_2str m^", "^
  1206 			       term2str v'^")");*)
  1207 	     val (p'',c',f',pt') = generate1 (assoc_thy thy') m 
  1208 			        (ScrState (E,l,a',v',S,true)) (p',p_) pt;
  1209 	   in Assoc ((E,l,a',v',S,true), (m,f',pt',p'',c @ c')::ss) end
  1210        | AssWeak (m,v') => 
  1211 	   let (*val _=writeln("### assy: Ass Weak("^tac_2str m^", "^
  1212 			       term2str v'^")");*)
  1213 	      val (p'',c',f',pt') = generate1 (assoc_thy thy') m 
  1214 			         (ScrState (E,l,a',v',S,false)) (p',p_) pt;
  1215 	   in Assoc ((E,l,a',v',S,false), (m,f',pt',p'',c @ c')::ss) end
  1216        | NotAss =>
  1217 	   ((*writeln("### assy, NotAss");*)
  1218 	    case ap of   (*switch for Or: 1st AssOnly, 2nd AssGen*)
  1219 	      AssOnly => (NasNap (v, E))
  1220 	    | gen => (case applicable_in (p,p_) pt 
  1221 					 (stac2tac pt (assoc_thy thy') stac) of
  1222 			Appl m' =>
  1223 			  let val is = (E,l,a',tac_2res m',S,false(*FIXXXME*))
  1224 			      val (p'',c',f',pt') =
  1225 			      generate1 (assoc_thy thy') m' (ScrState is) (p',p_) pt;
  1226 			  in NasApp (is,(m,f',pt',p'',c @ c')::ss) end
  1227 		      | Notappl _ => 
  1228 			    (NasNap (v, E))
  1229 			    )
  1230 		)
  1231        end);
  1232 (* (astep_up ((thy',scr,d),NasApp_) ((E,l,a,v,S,b),[(m,EmptyMout,pt,p,[])])) handle e => print_exn_G e;
  1233   *)
  1234 
  1235 
  1236 (* val (ys as (y,s,Script sc,d),(is as (E,l,a,v,S,b),ss),Const ("Let",_) $ _) =
  1237        (ys, ((E,up,a,v,S,b),ss), go up sc);
  1238    *)
  1239 fun ass_up (ys as (y,s,Script sc,d)) (is as (E,l,a,v,S,b),ss) 
  1240 	   (Const ("Let",_) $ _) =
  1241     let (*val _= writeln("### ass_up1 Let$e: is=")
  1242 	val _= writeln(istate2str (ScrState is))*)
  1243 	val l = drop_last l; (*comes from e, goes to Abs*)
  1244       val (Const ("Let",_) $ e $ (Abs (i,T,body))) = go l sc;
  1245       val i = mk_Free (i, T);
  1246       val E = upd_env E (i, v);
  1247       (*val _=writeln("### ass_up2 Let$e: E="^(subst2str E));*)
  1248     in case assy (((y,s),d),Aundef) ((E, l@[R,D], a,v,S,b),ss) body of
  1249 	   Assoc iss => Assoc iss
  1250 	 | NasApp iss => astep_up ys iss 
  1251 	 | NasNap (v, E) => astep_up ys ((E,l,a,v,S,b),ss) end
  1252 
  1253   | ass_up ys (iss as (is,_)) (Abs (_,_,_)) = 
  1254     ((*writeln("### ass_up  Abs: is=");
  1255      writeln(istate2str (ScrState is));*)
  1256      astep_up ys iss) (*TODO 5.9.00: env ?*)
  1257 
  1258   | ass_up ys (iss as (is,_)) (Const ("Let",_) $ e $ (Abs (i,T,b)))=
  1259     ((*writeln("### ass_up Let $ e $ Abs: is=");
  1260      writeln(istate2str (ScrState is));*)
  1261      astep_up ys iss) (*TODO 5.9.00: env ?*)
  1262 
  1263     (* val (ysa, iss,                 (Const ("Script.Seq",_) $ _ $ _ $ _)) =
  1264 	   (ys,  ((E,up,a,v,S,b),ss), (go up sc));
  1265        *)
  1266   | ass_up ysa iss (Const ("Script.Seq",_) $ _ $ _ $ _) =
  1267     astep_up ysa iss (*all has been done in (*2*) below*)
  1268 
  1269   | ass_up ysa iss (Const ("Script.Seq",_) $ _ $ _) =
  1270     (* val (ysa, iss,                 (Const ("Script.Seq",_) $ _ $ _)) =
  1271 	   (ys,  ((E,up,a,v,S,b),ss), (go up sc));
  1272        *)
  1273     astep_up ysa iss (*2*: comes from e2*)
  1274 
  1275   | ass_up (ysa as (y,s,Script sc,d)) (is as (E,l,a,v,S,b),ss)
  1276 	   (Const ("Script.Seq",_) $ _ ) = (*2*: comes from e1, goes to e2*)
  1277 	   (* val ((ysa as (y,s,Script sc,d)), (is as (E,l,a,v,S,b),ss),
  1278 	                                  (Const ("Script.Seq",_) $ _ )) = 
  1279 		  (ys,   ((E,up,a,v,S,b),ss), (go up sc));
  1280 	      *)
  1281     let val up = drop_last l;
  1282 	val Const ("Script.Seq",_) $ _ $ e2 = go up sc
  1283 	(*val _= writeln("### ass_up Seq$e: is=")
  1284 	val _= writeln(istate2str (ScrState is))*)
  1285     in case assy (((y,s),d),Aundef) ((E, up@[R], a,v,S,b),ss) e2 of
  1286 	   NasNap (v,E) => astep_up ysa ((E,up,a,v,S,b),ss)
  1287 	 | NasApp iss => astep_up ysa iss
  1288 	 | ay => ay end
  1289 
  1290     (* val (ysa, iss,                 (Const ("Script.Try",_) $ e $ _)) =
  1291 	   (ys,  ((E,up,a,v,S,b),ss), (go up sc));
  1292        *)
  1293   | ass_up ysa iss (Const ("Script.Try",_) $ e $ _) =
  1294     astep_up ysa iss
  1295 
  1296   (* val (ysa, iss, (Const ("Script.Try",_) $ e)) =
  1297 	 (ys,  ((E,up,a,v,S,b),ss), (go up sc));
  1298      *)
  1299   | ass_up ysa iss (Const ("Script.Try",_) $ e) =
  1300     ((*writeln("### ass_up Try $ e");*)
  1301      astep_up ysa iss)
  1302 
  1303   | ass_up (ys as (y,s,_,d)) ((E,l,_,v,S,b),ss)
  1304 	   (*(Const ("Script.While",_) $ c $ e $ a) = WN050930 blind fix*)
  1305 	   (t as Const ("Script.While",_) $ c $ e $ a) =
  1306     ((*writeln("### ass_up: While c= "^
  1307 	     (term2str (subst_atomic (upd_env E (a,v)) c)));*)
  1308      if eval_true_ y s (subst_atomic (upd_env E (a,v)) c)
  1309     then (case assy (((y,s),d),Aundef) ((E, l@[L,R], Some a,v,S,b),ss) e of 
  1310        NasNap (v,E') => astep_up ys ((E',l, Some a,v,S,b),ss)
  1311      | NasApp ((E',l,a,v,S,b),ss) =>
  1312        ass_up ys ((E',l,a,v,S,b),ss) t (*WN050930 't' was not assigned*)
  1313      | ay => ay)
  1314     else astep_up ys ((E,l, Some a,v,S,b),ss)
  1315 	 )
  1316 
  1317   | ass_up (ys as (y,s,_,d)) ((E,l,a,v,S,b),ss)
  1318 	   (*(Const ("Script.While",_) $ c $ e) = WN050930 blind fix*)
  1319 	   (t as Const ("Script.While",_) $ c $ e) =
  1320     if eval_true_ y s (subst_atomic (upd_env_opt E (a,v)) c)
  1321     then (case assy (((y,s),d),Aundef) ((E, l@[R], a,v,S,b),ss) e of 
  1322        NasNap (v,E') => astep_up ys ((E',l, a,v,S,b),ss)
  1323      | NasApp ((E',l,a,v,S,b),ss) =>
  1324        ass_up ys ((E',l,a,v,S,b),ss) t (*WN050930 't' was not assigned*)
  1325      | ay => ay)
  1326     else astep_up ys ((E,l, a,v,S,b),ss)
  1327 
  1328   | ass_up y iss (Const ("If",_) $ _ $ _ $ _) = astep_up y iss
  1329 
  1330   | ass_up (ys as (y,s,_,d)) ((E,l,_,v,S,b),ss)
  1331 	   (t as Const ("Script.Repeat",_) $ e $ a) =
  1332   (case assy (((y,s),d), Aundef) ((E, (l@[L,R]), Some a,v,S,b),ss) e of 
  1333        NasNap (v,E') => astep_up ys ((E',l, Some a,v,S,b),ss)
  1334      | NasApp ((E',l,a,v,S,b),ss) =>
  1335        ass_up ys ((E',l,a,v,S,b),ss) t
  1336      | ay => ay)
  1337 
  1338   | ass_up (ys as (y,s,_,d)) (is as ((E,l,a,v,S,b),ss)) 
  1339 	   (t as Const ("Script.Repeat",_) $ e) =
  1340   (case assy (((y,s),d), Aundef) ((E, (l@[R]), a,v,S,b),ss) e of 
  1341        NasNap (v', E') => astep_up ys ((E',l,a,v',S,b),ss)
  1342      | NasApp ((E',l,a,v',S,bb),ss) => 
  1343        ass_up ys ((E',l,a,v',S,b),ss) t
  1344      | ay => ay)
  1345 
  1346   | ass_up y iss (Const ("Script.Or",_) $ _ $ _ $ _) = astep_up y iss
  1347 
  1348   | ass_up y iss (Const ("Script.Or",_) $ _ $ _) = astep_up y iss
  1349 
  1350   | ass_up y ((E,l,a,v,S,b),ss) (Const ("Script.Or",_) $ _ ) = 
  1351     astep_up y ((E, (drop_last l), a,v,S,b),ss)
  1352 
  1353   | ass_up y iss t =
  1354     raise error ("ass_up not impl for t= "^(term2str t))
  1355 (* 9.6.03
  1356    val (ys as (_,_,Script sc,_), ss) = 
  1357        ((thy',srls,scr,d), [(m,EmptyMout,pt,p,[])]:step list);
  1358    astep_up ys ((E,l,a,v,S,b),ss);
  1359    val ((ys as (_,_,Script sc,_)), ((E,l,a,v,S,b),ss)) = 
  1360        (ysa, iss);
  1361    val ((ys as (_,_,Script sc,_)), ((E,l,a,v,S,b),ss)) = 
  1362        ((thy',srls,scr,d), ((E,l,a,v,S,b), [(m,EmptyMout,pt,p,[])]));
  1363    *)  
  1364 and astep_up (ys as (_,_,Script sc,_)) ((E,l,a,v,S,b),ss) =
  1365   if 1 < length l
  1366     then 
  1367       let val up = drop_last l;
  1368 	  (*val _= writeln("### astep_up: E= "^env2str E);*)
  1369       in ass_up ys ((E,up,a,v,S,b),ss) (go up sc) end
  1370   else (NasNap (v, E))
  1371 ;
  1372 
  1373 
  1374 
  1375 
  1376 
  1377 (* use"ME/script.sml";
  1378    use"script.sml";
  1379  term2str (go up sc);
  1380 
  1381    *)
  1382 
  1383 (*check if there are tacs for rewriting only*)
  1384 fun rew_only ([]:step list) = true
  1385   | rew_only (((Rewrite' _          ,_,_,_,_))::ss) = rew_only ss
  1386   | rew_only (((Rewrite_Inst' _     ,_,_,_,_))::ss) = rew_only ss
  1387   | rew_only (((Rewrite_Set' _      ,_,_,_,_))::ss) = rew_only ss
  1388   | rew_only (((Rewrite_Set_Inst' _ ,_,_,_,_))::ss) = rew_only ss
  1389   | rew_only (((Calculate' _        ,_,_,_,_))::ss) = rew_only ss
  1390   | rew_only (((Begin_Trans' _      ,_,_,_,_))::ss) = rew_only ss
  1391   | rew_only (((End_Trans' _        ,_,_,_,_))::ss) = rew_only ss
  1392   | rew_only _ = false; 
  1393   
  1394 
  1395 datatype locate =
  1396   Steps of istate      (*producing hd of step list (which was latest)
  1397 	                 for next_tac, for reporting Safe|Unsafe to DG*)
  1398 	   * step      (*(scrstate producing this step is in ptree !)*) 
  1399 		 list  (*locate_gen may produce intermediate steps*)
  1400 | NotLocatable;        (*no (m Ass m') or (m AssWeak m') found*)
  1401 
  1402 
  1403 
  1404 (* locate_gen tries to locate an input tac m in the script. 
  1405    pursuing this goal the script is executed until an (m' equiv m) is found,
  1406    or the end of the script
  1407 args
  1408    m   : input by the user, already checked by applicable_in,
  1409          (to be searched within Or; and _not_ an m doing the step on ptree !)
  1410    p,pt: (incl ets) at the time of input
  1411    scr : the script
  1412    d   : canonical simplifier for locating Take, Substitute, Subproblems etc.
  1413    ets : ets at the time of input
  1414    l   : the location (in scr) of the stac which generated the current formula
  1415 returns
  1416    Steps: pt,p (incl. ets) with m done
  1417           pos' list of proofobjs cut (from generate)
  1418           safe: implied from last proofobj
  1419 	  ets:
  1420    ///ToDo : ets contains a list of tacs to be done before m can be done
  1421           NOT IMPL. -- "error: do other step before"
  1422    NotLocatable: thus generate_hard
  1423 *)
  1424 (* val (Rewrite'(_,ro,er,pa,(id,str),f,_), p, Rfuns {locate_rule=lo,...},
  1425 	RrlsState (_,f'',rss,rts)) = (m, (p,p_), sc, is);
  1426    *)
  1427 fun locate_gen (thy',_) (Rewrite'(_,ro,er,pa,(id,str),f,_)) (pt,p) 
  1428 	       (Rfuns {locate_rule=lo,...}, d) (RrlsState (_,f'',rss,rts)) = 
  1429     (case lo rss f (Thm (id, mk_thm (assoc_thy thy') str)) of
  1430 	 [] => NotLocatable
  1431        | rts' => 
  1432 	 Steps (rts2steps [] ((pt,p),(f,f'',rss,rts),(thy',ro,er,pa)) rts'))
  1433 (* val p as(p',p_)=(p,p_);val scr as Script(h $ body)=sc;val (E,l,a,v,S,bb)=is;
  1434    locate_gen (thy':theory') (m:tac_) ((pt,p):ptree * pos') 
  1435 	      (scr,d) (E,l,a,v,S,bb);
  1436    9.6.03
  1437    val ts = (thy',srls);
  1438    val p = (p,p_);
  1439    val (scr as Script (h $ body)) = (sc);
  1440    val ScrState (E,l,a,v,S,b) = (is);
  1441 
  1442    val (ts as (thy',srls), m, (pt,p), 
  1443 	(scr as Script (h $ body),d), (ScrState (E,l,a,v,S,b))) = 
  1444        ((thy',srls), m,  (pt,(p,p_)), (sc,d), is);
  1445    locate_gen (thy',srls) m (pt,p) (Script(h $ body),d)(ScrState(E,l,a,v,S,b));
  1446 
  1447    val (ts as (thy',srls), m, (pt,p), 
  1448 	(scr as Script (h $ body),d), (ScrState (E,l,a,v,S,b))) = 
  1449        ((thy',srls), m',  (pt,(lev_on p,Frm)), (sc,d), is');
  1450 
  1451    val (ts as (thy',srls), m, (pt,p), 
  1452 	(scr as Script (h $ body),d), (ScrState (E,l,a,v,S,b))) = 
  1453        ((thy',srls), m',  (pt,(p, Res)), (sc,d), is');
  1454 
  1455    val (ts as (thy',srls), m, (pt,p), 
  1456 	(scr as Script (h $ body),d), (ScrState (E,l,a,v,S,b))) = 
  1457        ((thy',srls), m,  (pt,(p,p_)), (sc,d), is);
  1458    *)
  1459   | locate_gen (ts as (thy',srls)) (m:tac_) ((pt,p):ptree * pos') 
  1460 	       (scr as Script (h $ body),d) (ScrState (E,l,a,v,S,b))  = 
  1461   let (*val _= writeln("### locate_gen-----------------: is=");
  1462       val _= writeln( istate2str (ScrState (E,l,a,v,S,b)));
  1463       val _= writeln("### locate_gen: l= "^loc_2str l^", p= "^pos'2str p)*)
  1464       val thy = assoc_thy thy';
  1465   in case if l=[] orelse ((*init.in solve..Apply_Method...*)
  1466 			  (last_elem o fst) p = 0 andalso snd p = Res)
  1467 	  then (assy ((ts,d),Aundef) ((E,[R],a,v,S,b),
  1468 				      [(m,EmptyMout,pt,p,[])]) body)
  1469 (* val Assoc (iss as (is as (_,_,_,_,_,bb), ss as ((m',f',pt',p',c')::_))) =
  1470        (astep_up (thy',srls,scr,d) ((E,l,a,v,S,b),[(m,EmptyMout,pt,p,[])]));
  1471        (assy ((ts,d),Aundef) ((E,[R],a,v,S,b),[(m,EmptyMout,pt,p,[])]) body);
  1472   *)
  1473 	  else (astep_up (thy',srls,scr,d) ((E,l,a,v,S,b),
  1474 					    [(m,EmptyMout,pt,p,[])]) ) of
  1475 	 Assoc (iss as (is as (_,_,_,_,_,bb), ss as ((m',f',pt',p',c')::_))) =>
  1476 (* val Assoc (iss as (is as (_,_,_,_,_,bb), ss as ((m',f',pt',p',c')::_))) =
  1477        (astep_up (thy',srls,scr,d) ((E,l,a,v,S,b),
  1478 				    [(m,EmptyMout,pt,p,[])]) );
  1479    *)
  1480 	 ((*writeln("### locate_gen Assoc: p'="^(pos'2str p'));*)
  1481 	  if bb then Steps (ScrState is, ss)
  1482 	  else if rew_only ss (*andalso 'not bb'= associated weakly*)
  1483 	  then let val (po,p_) = p
  1484                    val po' = case p_ of Frm => po | Res => lev_on po
  1485 		  (*WN.12.03: noticed, that pos is also updated in assy !?!
  1486 		   instead take p' from Assoc ?????????????????????????????*)
  1487                   val (p'',c'',f'',pt'') = 
  1488 		      generate1 thy m (ScrState is) (po',p_) pt;
  1489 	      (*val _=writeln("### locate_gen, aft g1: p''="^(pos'2str p''));*)
  1490 	      (*drop the intermediate steps !*)
  1491 	      in Steps (ScrState is, [(m, f'',pt'',p'',c'')]) end
  1492 	 else Steps (ScrState is, ss))
  1493 	
  1494      | NasApp _ (*[((E,l,a,v,S,bb),(m',f',pt',p',c'))] => 
  1495 	   raise error ("locate_gen: should not have got NasApp, ets =")*)
  1496        => NotLocatable
  1497      | NasNap (_,_) =>
  1498        if l=[] then NotLocatable
  1499        else (*scan from begin of script for rew_only*)
  1500 	   (case assy ((ts,d),Aundef) ((E,[R],a,v,Unsafe,b),
  1501 					 [(m,EmptyMout,pt,p,[])]) body  of
  1502 		Assoc (iss as (is as (_,_,_,_,_,bb), 
  1503 			       ss as ((m',f',pt',p',c')::_))) =>
  1504 		    ((*writeln"4### locate_gen Assoc after Fini";*)
  1505 		     if rew_only ss
  1506 		     then let val(p'',c'',f'',pt'') = 
  1507 				 generate1 thy m (ScrState is) p' pt;
  1508 			  (*drop the intermediate steps !*)
  1509 			  in Steps (ScrState is, [(m, f'',pt'',p'',c'')]) end
  1510 		     else NotLocatable)
  1511 	      | _ => ((*writeln ("#### locate_gen: after Fini");*)
  1512 		      NotLocatable))
  1513   end
  1514   | locate_gen _ m _ (sc,_) is = 
  1515     raise error ("locate_gen: wrong arguments,\n tac= "^(tac_2str m)^
  1516 		 ",\n scr= "^(scr2str sc)^",\n istate= "^(istate2str is));
  1517 
  1518 
  1519 
  1520 (** find the next stactic in a script **)
  1521 
  1522 datatype appy =  (*ExprVal in the sense of denotational semantics*)
  1523     Appy of      (*applicable stac found, search stalled*)
  1524     tac_ *       (*tac_ associated (fun assod) with stac*)
  1525     scrstate     (*after determination of stac WN.18.8.03*)
  1526   | Napp of      (*stac found was not applicable; 
  1527 	           this mode may become Skip in Repeat, Try and Or*)
  1528     env (*stack*)  (*popped while nxt_up*)
  1529   | Skip of      (*for restart after Appy, for leaving iterations,
  1530 	           for passing the value of scriptexpressions,
  1531 		   and for finishing the script successfully*)
  1532     term * env (*stack*);
  1533 
  1534 (*appy, nxt_up, nstep_up scanning for next_tac.
  1535   search is clearly separated into (1)-(2):
  1536   (1) appy is recursive descent;
  1537   (2) nxt_up resumes interpretation at a location somewhere in the script;
  1538       nstep_up does only get to the parentnode of the scriptexpr.
  1539   consequence:
  1540   * call of (2) means _always_ that in this branch below
  1541     there was an applicable stac (Repeat, Or e1, ...)
  1542 *)
  1543 
  1544 
  1545 datatype appy_ = (*as argument in nxt_up, nstep_up, from appy*)
  1546        (*  Appy is only (final) returnvalue, not argument during search
  1547        |*) Napp_ (*ev. detects 'script is not appropriate for this example'*)
  1548        | Skip_;  (*detects 'script successfully finished'
  1549 		   also used as init-value for resuming; this works,
  1550 	           because 'nxt_up Or e1' treats as Appy*)
  1551 
  1552 fun appy thy ptp E l
  1553   (t as Const ("Let",_) $ e $ (Abs (i,T,b))) a v =
  1554 (* val (thy, ptp, E, l,        t as Const ("Let",_) $ e $ (Abs (i,T,b)),a, v)=
  1555        (thy, ptp, E, up@[R,D], body,                                    a, v);
  1556    appy thy ptp E l t a v;
  1557    *)
  1558   ((*writeln("### appy Let$e$Abs: is=");
  1559    writeln(istate2str (ScrState (E,l,a,v,Sundef,false)));*)
  1560    case appy thy ptp E (l@[L,R]) e a v of
  1561      Skip (res, E) => 
  1562        let (*val _= writeln("### appy Let "^(term2str t));
  1563 	 val _= writeln("### appy Let: Skip res ="^(term2str res));*)
  1564        (*val (i',b') = variant_abs (i,T,b); WN.15.5.03
  1565 	 val i = mk_Free(i',T);             WN.15.5.03 *)   
  1566 	 val E' = upd_env E (Free (i,T), res);
  1567        in appy thy ptp E' (l@[R,D]) b a v end
  1568    | ay => ay)
  1569 
  1570   | appy (thy as (th,sr)) ptp E l
  1571   (t as Const ("Script.While"(*1*),_) $ c $ e $ a) _ v = (*ohne n. 28.9.00*)
  1572   ((*writeln("### appy While $ c $ e $ a, upd_env= "^
  1573 	   (subst2str (upd_env E (a,v))));*)
  1574    if eval_true_ th sr (subst_atomic (upd_env E (a,v)) c)
  1575     then appy thy ptp E (l@[L,R]) e (Some a) v
  1576   else Skip (v, E))
  1577 
  1578   | appy (thy as (th,sr)) ptp E l
  1579   (t as Const ("Script.While"(*2*),_) $ c $ e) a v =(*ohne nachdenken 28.9.00*)
  1580   ((*writeln("### appy While $ c $ e, upd_env= "^
  1581 	   (subst2str (upd_env_opt E (a,v))));*)
  1582    if eval_true_ th sr (subst_atomic (upd_env_opt E (a,v)) c)
  1583     then appy thy ptp E (l@[R]) e a v
  1584   else Skip (v, E))
  1585 
  1586   | appy (thy as (th,sr)) ptp E l (t as Const ("If",_) $ c $ e1 $ e2) a v =
  1587     ((*writeln("### appy If: t= "^(term2str t));
  1588      writeln("### appy If: c= "^(term2str(subst_atomic(upd_env_opt E(a,v))c)));
  1589      writeln("### appy If: thy= "^(fst thy));*)
  1590      if eval_true_ th sr (subst_atomic (upd_env_opt E (a,v)) c)
  1591      then ((*writeln("### appy If: true");*)appy thy ptp E (l@[L,R]) e1 a v)
  1592      else ((*writeln("### appy If: false");*)appy thy ptp E (l@[  R]) e2 a v))
  1593 (* val (thy, ptp, E, l,     (Const ("Script.Repeat",_) $ e $ a), _, v) =
  1594        (thy, ptp, E, (l@[R]), e,                                 a, v);
  1595    *)
  1596   | appy thy ptp E (*env*) l
  1597   (Const ("Script.Repeat"(*1*),_) $ e $ a) _ v = 
  1598     ((*writeln("### appy Repeat a: ");*)
  1599      appy thy ptp E (*env*) (l@[L,R]) e (Some a) v)
  1600 (* val (thy, ptp, E, l,     (Const ("Script.Repeat",_) $ e), _, v) =
  1601        (thy, ptp, E, (l@[R]), e,                             a, v);
  1602    *)
  1603   | appy thy ptp E (*env*) l
  1604   (Const ("Script.Repeat"(*2*),_) $ e) a v = 
  1605     ((*writeln("3### appy Repeat: a= "^
  1606 	     (Sign.string_of_term (sign_of (assoc_thy thy)) a));*)
  1607      appy thy ptp E (*env*) (l@[R]) e a v)
  1608 (* val (thy, ptp, E, l,      (t as Const ("Script.Try",_) $ e $ a), _, v)=
  1609        (thy, ptp, E, (l@[R]), e2,                                   a, v);
  1610    *)
  1611   | appy thy ptp E l
  1612   (t as Const ("Script.Try",_) $ e $ a) _ v =
  1613   (case appy thy ptp E (l@[L,R]) e (Some a) v of
  1614      Napp E => ((*writeln("### appy Try "^
  1615 			  (Sign.string_of_term (sign_of (assoc_thy thy)) t));*)
  1616 		 Skip (v, E))
  1617    | ay => ay)
  1618 (* val (thy, ptp, E, l,      (t as Const ("Script.Try",_) $ e), _, v)=
  1619        (thy, ptp, E, (l@[R]), e2,                               a, v);
  1620    val (thy, ptp, E, l,        (t as Const ("Script.Try",_) $ e), _, v)=
  1621        (thy, ptp, E, (l@[L,R]), e1,                               a, v);
  1622    *)
  1623   | appy thy ptp E l
  1624   (t as Const ("Script.Try",_) $ e) a v =
  1625   (case appy thy ptp E (l@[R]) e a v of
  1626      Napp E => ((*writeln("### appy Try "^
  1627 			  (Sign.string_of_term (sign_of (assoc_thy thy)) t));*)
  1628 		 Skip (v, E))
  1629    | ay => ay)
  1630 
  1631 
  1632   | appy thy ptp E l
  1633 	 (Const ("Script.Or"(*1*),_) $e1 $ e2 $ a) _ v =
  1634     (case appy thy ptp E (l@[L,L,R]) e1 (Some a) v of
  1635 	 Appy lme => Appy lme
  1636        | _ => appy thy ptp E (*env*) (l@[L,R]) e2 (Some a) v)
  1637     
  1638   | appy thy ptp E l
  1639 	 (Const ("Script.Or"(*2*),_) $e1 $ e2) a v =
  1640     (case appy thy ptp E (l@[L,R]) e1 a v of
  1641 	 Appy lme => Appy lme
  1642        | _ => appy thy ptp E (l@[R]) e2 a v)
  1643 
  1644 (* val (thy, ptp, E, l,     (Const ("Script.Seq",_) $ e1 $ e2 $ a), _, v)=
  1645        (thy, ptp, E,(up@[R]),e2,                                    a, v);
  1646    val (thy, ptp, E, l,     (Const ("Script.Seq",_) $ e1 $ e2 $ a), _, v)=
  1647        (thy, ptp, E,(up@[R,D]),body,                                a, v);
  1648    *)
  1649   | appy thy ptp E l
  1650 	 (Const ("Script.Seq"(*1*),_) $ e1 $ e2 $ a) _ v =
  1651     ((*writeln("### appy Seq $ e1 $ e2 $ a, upd_env= "^
  1652 	     (subst2str (upd_env E (a,v))));*)
  1653      case appy thy ptp E (l@[L,L,R]) e1 (Some a) v of
  1654 	 Skip (v,E) => appy thy ptp E (l@[L,R]) e2 (Some a) v
  1655        | ay => ay)
  1656 
  1657 (* val (thy, ptp, E, l,     (Const ("Script.Seq",_) $ e1 $ e2), _, v)=
  1658        (thy, ptp, E,(up@[R]),e2,                                a, v);
  1659    val (thy, ptp, E, l,     (Const ("Script.Seq",_) $ e1 $ e2), _, v)=
  1660        (thy, ptp, E,(l@[R]), e2,                                a, v);
  1661    val (thy, ptp, E, l,     (Const ("Script.Seq",_) $ e1 $ e2), _, v)=
  1662        (thy, ptp, E,(up@[R,D]),body,                            a, v);
  1663    *)
  1664   | appy thy ptp E l
  1665 	 (Const ("Script.Seq",_) $ e1 $ e2) a v =
  1666     (case appy thy ptp E (l@[L,R]) e1 a v of
  1667 	 Skip (v,E) => appy thy ptp E (l@[R]) e2 a v
  1668        | ay => ay)
  1669 
  1670   (*.a leaf has been found*)   
  1671   | appy (thy as (th,sr)) (pt, p) E l t a v =
  1672 (* val (thy as (th,sr),(pt, p),E, l,        t,    a, v) = 
  1673        (thy,            ptp,   E, up@[R,D], body, a, v);
  1674    val (thy as (th,sr),(pt, p),E, l,       t, a, v) = 
  1675        (thy,            ptp,   E, l@[L,R], e, a, v);
  1676    val (thy as (th,sr),(pt, p),E, l,       t, a, v) =
  1677        (thy,            ptp,   E,(l@[R]),  e, a, v);
  1678    *)
  1679     (case handle_leaf "next  " th sr E a v t of
  1680 (* val (a', Expr s) = handle_leaf "next  " th sr E a v t;
  1681    *)
  1682 	(a', Expr s) => Skip (s, E)
  1683 (* val (a', STac stac) = handle_leaf "next  " th sr E a v t;
  1684    *)
  1685      | (a', STac stac) =>
  1686 	let
  1687 	 (*val _= writeln("### appy t, vor  stac2tac_ is="); 
  1688            val _= writeln(istate2str (ScrState (E,l,a',v,Sundef,false)));*)
  1689 	   val (m,m') = stac2tac_ pt (assoc_thy th) stac
  1690        in case m of 
  1691 	      Subproblem _ => Appy (m', (E,l,a',tac_2res m',Sundef,false))
  1692 	    | _ => (case applicable_in p pt m of
  1693 (* val Appl m' = applicable_in p pt m;
  1694    *)
  1695 			Appl m' => 
  1696 			((*writeln("### appy: Appy");*)
  1697 			 Appy (m', (E,l,a',tac_2res m',Sundef,false)))
  1698 		      | _ => ((*writeln("### appy: Napp");*)Napp E)) 
  1699 	end);
  1700 	 
  1701 
  1702 (* val (scr as Script sc, l, t as Const ("Let",_) $ _) =
  1703        (Script sc, up, go up sc);
  1704    nxt_up thy ptp (Script sc) E l ay t a v;
  1705 
  1706    val (thy,ptp,scr as (Script sc),E,l, ay, t as Const ("Let",_) $ _, a, v)=
  1707        (thy,ptp,Script sc,         E,up,ay, go up sc,                 a, v);
  1708    nxt_up thy ptp scr E l ay t a v;
  1709    *)
  1710 fun nxt_up thy ptp (scr as (Script sc)) E l ay
  1711     (t as Const ("Let",_) $ _) a v = (*comes from let=...*)
  1712     ((*writeln("### nxt_up1 Let$e: is=");
  1713      writeln(istate2str (ScrState (E,l,a,v,Sundef,false)));*)
  1714      if ay = Napp_
  1715     then nstep_up thy ptp scr E (drop_last l) Napp_ a v
  1716     else (*Skip_*)
  1717 	let val up = drop_last l;
  1718 	    val (Const ("Let",_) $ e $ (Abs (i,T,body))) = go up sc;
  1719             val i = mk_Free (i, T);
  1720             val E = upd_env E (i, v);
  1721           (*val _= writeln("### nxt_up2 Let$e: is=");
  1722             val _= writeln(istate2str (ScrState (E,l,a,v,Sundef,false)));*)
  1723 	in case appy thy ptp (E) (up@[R,D]) body a v  of
  1724 	       Appy lre => Appy lre
  1725 	     | Napp E => nstep_up thy ptp scr E up Napp_ a v
  1726 	     | Skip (v,E) => nstep_up thy ptp scr E up Skip_ a v end)
  1727 	    
  1728   | nxt_up thy ptp scr E l ay
  1729     (t as Abs (_,_,_)) a v = 
  1730     ((*writeln("### nxt_up Abs: "^
  1731 	     (Sign.string_of_term (sign_of (assoc_thy thy)) t));*)
  1732      nstep_up thy ptp scr E (*enr*) l ay a v)
  1733 
  1734   | nxt_up thy ptp scr E l ay
  1735     (t as Const ("Let",_) $ e $ (Abs (i,T,b))) a v =
  1736     ((*writeln("### nxt_up Let$e$Abs: is=");
  1737      writeln(istate2str (ScrState (E,l,a,v,Sundef,false)));*)
  1738      (*writeln("### nxt_up Let e Abs: "^
  1739 	     (Sign.string_of_term (sign_of (assoc_thy thy)) t));*)
  1740      nstep_up thy ptp scr (*upd_env*) E (*a,v)*) 
  1741 	      (*eno,upd_env env (iar,res),iar,res,saf*) l ay a v)
  1742 
  1743   (*no appy_: never causes Napp -> Helpless*)
  1744   | nxt_up (thy as (th,sr)) ptp scr E l _ 
  1745   (Const ("Script.While"(*1*),_) $ c $ e $ _) a v = 
  1746   if eval_true_ th sr (subst_atomic (upd_env_opt E (a,v)) c) 
  1747     then case appy thy ptp E (l@[L,R]) e a v of
  1748 	     Appy lr => Appy lr
  1749 	   | Napp E => nstep_up thy ptp scr E l Skip_ a v
  1750 	   | Skip (v,E) => nstep_up thy ptp scr E l Skip_ a v
  1751   else nstep_up thy ptp scr E l Skip_ a v
  1752 
  1753   (*no appy_: never causes Napp - Helpless*)
  1754   | nxt_up (thy as (th,sr)) ptp scr E l _ 
  1755   (Const ("Script.While"(*2*),_) $ c $ e) a v = 
  1756   if eval_true_ th sr (subst_atomic (upd_env_opt E (a,v)) c) 
  1757     then case appy thy ptp E (l@[R]) e a v of
  1758 	     Appy lr => Appy lr
  1759 	   | Napp E => nstep_up thy ptp scr E l Skip_ a v
  1760 	   | Skip (v,E) => nstep_up thy ptp scr E l Skip_ a v
  1761   else nstep_up thy ptp scr E l Skip_ a v
  1762 
  1763 (* val (scr, l) = (Script sc, up);
  1764    *)
  1765   | nxt_up thy ptp scr E l ay (Const ("If",_) $ _ $ _ $ _) a v = 
  1766     nstep_up thy ptp scr E l ay a v
  1767 
  1768   | nxt_up thy ptp scr E l _ (*no appy_: there was already a stac below*)
  1769   (Const ("Script.Repeat"(*1*),T) $ e $ _) a v =
  1770     (case appy thy ptp (*upd_env*) E (*a,v)*) ((l@[L,R]):loc_) e a v  of
  1771       Appy lr => Appy lr
  1772     | Napp E => ((*writeln("### nxt_up Repeat a: ");*)
  1773 		 nstep_up thy ptp scr E l Skip_ a v)
  1774     | Skip (v,E) => ((*writeln("### nxt_up Repeat: Skip res ="^
  1775 		(Sign.string_of_term(sign_of (assoc_thy thy)) res'));*)
  1776 		    nstep_up thy ptp scr E l Skip_ a v))
  1777 
  1778   | nxt_up thy ptp scr E l _ (*no appy_: there was already a stac below*)
  1779   (Const ("Script.Repeat"(*2*),T) $ e) a v =
  1780     (case appy thy ptp (*upd_env*) E (*a,v)*) ((l@[R]):loc_) e a v  of
  1781       Appy lr => Appy lr
  1782     | Napp E => ((*writeln("### nxt_up Repeat a: ");*)
  1783 		 nstep_up thy ptp scr E l Skip_ a v)
  1784     | Skip (v,E) => ((*writeln("### nxt_up Repeat: Skip res ="^
  1785 		(Sign.string_of_term(sign_of (assoc_thy thy)) res'));*)
  1786 		    nstep_up thy ptp scr E l Skip_ a v))
  1787 (* val (thy, ptp, scr, E, l,   _,(t as Const ("Script.Try",_) $ e $ _), a, v) =
  1788        (thy, ptp, (Script sc), 
  1789 	               E, up, ay,(go up sc),                            a, v);
  1790    *)
  1791   | nxt_up thy ptp scr E l _ (*makes Napp to Skip*)
  1792   (t as Const ("Script.Try",_) $ e $ _) a v = 
  1793     ((*writeln("### nxt_up Try "^
  1794 	     (Sign.string_of_term (sign_of (assoc_thy thy)) t));*)
  1795      nstep_up thy ptp scr E l Skip_ a v )
  1796 (* val (thy, ptp, scr, E, l,   _,(t as Const ("Script.Try",_) $ e), a, v) =
  1797        (thy, ptp, (Script sc), 
  1798 	               E, up, ay,(go up sc),                        a, v);
  1799    *)
  1800   | nxt_up thy ptp scr E l _ (*makes Napp to Skip*)
  1801   (t as Const ("Script.Try"(*2*),_) $ e) a v = 
  1802     ((*writeln("### nxt_up Try "^
  1803 	     (Sign.string_of_term (sign_of (assoc_thy thy)) t));*)
  1804      nstep_up thy ptp scr E l Skip_ a v)
  1805 
  1806 
  1807   | nxt_up thy ptp scr E l ay
  1808   (Const ("Script.Or",_) $ _ $ _ $ _) a v = nstep_up thy ptp scr E l ay a v
  1809 
  1810   | nxt_up thy ptp scr E l ay
  1811   (Const ("Script.Or",_) $ _ $ _) a v = nstep_up thy ptp scr E l ay a v
  1812 
  1813   | nxt_up thy ptp scr E l ay
  1814   (Const ("Script.Or",_) $ _ ) a v = 
  1815     nstep_up thy ptp scr E (drop_last l) ay a v
  1816 (* val (thy, ptp, scr, E, l, ay, (Const ("Script.Seq",_) $ _ $ _ $ _), a, v) =
  1817        (thy, ptp, (Script sc), 
  1818 		       E, up, ay,(go up sc),                           a, v);
  1819    *)
  1820   | nxt_up thy ptp scr E l ay (*all has been done in (*2*) below*)
  1821   (Const ("Script.Seq"(*1*),_) $ _ $ _ $ _) a v =
  1822     nstep_up thy ptp scr E l ay a v
  1823 (* val (thy, ptp, scr, E, l, ay, (Const ("Script.Seq",_) $ _ $ e2), a, v) =
  1824        (thy, ptp, (Script sc), 
  1825 		       E, up, ay,(go up sc),                        a, v);
  1826    *)
  1827   | nxt_up thy ptp scr E l ay (*comes from e2*)
  1828 	   (Const ("Script.Seq"(*2*),_) $ _ $ e2) a v =
  1829     nstep_up thy ptp scr E l ay a v
  1830 (* val (thy, ptp, scr, E, l, ay, (Const ("Script.Seq",_) $ _), a, v) =
  1831        (thy, ptp, (Script sc), 
  1832 		       E, up, ay,(go up sc),                   a, v);
  1833    *)
  1834   | nxt_up thy ptp (scr as Script sc) E l ay (*comes from e1*)
  1835 	   (Const ("Script.Seq",_) $ _) a v = 
  1836     if ay = Napp_
  1837     then nstep_up thy ptp scr E (drop_last l) Napp_ a v
  1838     else (*Skip_*)
  1839 	let val up = drop_last l;
  1840 	    val Const ("Script.Seq"(*2*),_) $ _ $ e2 = go up sc;
  1841 	in case appy thy ptp E (up@[R]) e2 a v  of
  1842 	    Appy lr => Appy lr
  1843 	  | Napp E => nstep_up thy ptp scr E up Napp_ a v
  1844 	  | Skip (v,E) => nstep_up thy ptp scr E up Skip_ a v end
  1845 
  1846   | nxt_up (thy,_) ptp scr E l ay t a v =
  1847   raise error ("nxt_up not impl for "^
  1848 	       (Sign.string_of_term (sign_of (assoc_thy thy)) t))
  1849 
  1850 (* val (thy, ptp, (Script sc), E, l, ay,    a, v)=
  1851        (thy, ptp, scr,         E, l, Skip_, a, v);
  1852    val (thy, ptp, (Script sc), E, l, ay,    a, v)=
  1853        (thy, ptp, sc,          E, l, Skip_, a, v);
  1854    *)
  1855 and nstep_up thy ptp (Script sc) E l ay a v = 
  1856   ((*writeln("### nstep_up from: "^(loc_2str l));
  1857    writeln("### nstep_up from: "^
  1858 	   (Sign.string_of_term (sign_of (assoc_thy thy)) (go l sc)));*)
  1859    if 1 < length l
  1860    then 
  1861        let 
  1862 	   val up = drop_last l; 
  1863        in ((*writeln("### nstep_up to: "^
  1864 	      (Sign.string_of_term (sign_of (assoc_thy thy)) (go up sc)));*)
  1865 	   nxt_up thy ptp (Script sc) E up ay (go up sc) a v ) end
  1866    else (*interpreted to end*)
  1867        if ay = Skip_ then Skip (v, E) else Napp E 
  1868 );
  1869 
  1870 (* decide for the next applicable stac in the script;
  1871    returns (stactic, value) - the value in case the script is finished 
  1872    12.8.02:         ~~~~~ and no assumptions ??? FIXME ???
  1873    20.8.02: must return p in case of finished, because the next script
  1874             consulted need not be the calling script:
  1875             in case of detail ie. _inserted_ PrfObjs, the next stac
  1876             has to searched in a script with PblObj.status<>Complete !
  1877             (.. not true for other details ..PrfObj ??????????????????
  1878    20.8.02: do NOT return safe (is only changed in locate !!!)
  1879 *)
  1880 (* val (thy, (pt,p), Rfuns {next_rule=ne,...}, RrlsState (f,f',rss,_)) = 
  1881        (thy', (pt,p), sc, RrlsState (ii t));
  1882    val (thy, (pt,p), Rfuns {next_rule=ne,...}, RrlsState (f,f',rss,_)) = 
  1883        (thy', (pt',p'), sc, is');
  1884    *)
  1885 fun next_tac (thy,_) (pt,p) (Rfuns {next_rule,...}) (RrlsState(f,f',rss,_))=
  1886     if f = f' then (End_Detail' (f',[])(*8.6.03*), Uistate, 
  1887 		    (f', Sundef(*FIXME is no value of next_tac! vor 8.6.03*)))
  1888                                                           (*finished*)
  1889     else (case next_rule rss f of
  1890 	      None => (Empty_Tac_, Uistate, (e_term, Sundef)) 	  (*helpless*)
  1891 (* val Some (Thm (id,thm)) = next_rule rss f;
  1892    *)
  1893 	    | Some (Thm (id,thm))(*8.6.03: muss auch f' liefern ?!!*) => 
  1894 	      (Rewrite' (thy, "e_rew_ord", e_rls,(*!?!8.6.03*) false,
  1895 			 (id, string_of_thmI thm), f,(e_term,[(*!?!8.6.03*)])),
  1896 	       Uistate, (e_term, Sundef)))                 (*next stac*)
  1897 
  1898 (* val(thy, ptp as (pt,(p,_)), sc as Script (h $ body),ScrState (E,l,a,v,s,b))=
  1899       ((thy',srls), (pt,pos),  sc,                     is);
  1900    *)
  1901   | next_tac thy (ptp as (pt,(p,_)):ptree * pos') (sc as Script (h $ body)) 
  1902 	     (ScrState (E,l,a,v,s,b)) =
  1903   ((*writeln("### next_tac-----------------: E= ");
  1904    writeln( istate2str (ScrState (E,l,a,v,s,b)));*)
  1905    case if l=[] then appy thy ptp E [R] body None v
  1906        else nstep_up thy ptp sc E l Skip_ a v of
  1907       Skip (v,_) =>                                              (*finished*)
  1908       (case par_pbl_det pt p of
  1909 	   (true, p', _) => 
  1910 	   let val (_,pblID,_) = get_obj g_spec pt p';
  1911 	   in (Check_Postcond' (pblID, (v, [(*8.6.03 NO asms???*)])), 
  1912 	       e_istate, (v,s)) end
  1913 	 | (_,p',rls') => (End_Detail' (e_term,[])(*8.6.03*), e_istate, (v,s)))
  1914     | Napp _ => (Empty_Tac_, e_istate, (e_term, Sundef))         (*helpless*)
  1915     | Appy (m', scrst as (_,_,_,v,_,_)) => (m', ScrState scrst,
  1916 			   (v, Sundef)))                         (*next stac*)
  1917 
  1918   | next_tac _ _ _ is = raise error ("next_tac: not impl for "^
  1919 				     (istate2str is));
  1920 
  1921 
  1922 
  1923 
  1924 (*.create the initial interpreter state from the items of the guard.*)
  1925 (* val (thy, itms, metID) = (thy, itms, mI);
  1926    *)
  1927 fun init_scrstate thy itms metID =
  1928     let val actuals = itms2args thy metID itms;
  1929 	val scr as Script sc = (#scr o get_met) metID;
  1930         val formals = formal_args sc
  1931 	(*expects same sequence of (actual) args in itms 
  1932           and (formal) args in met*)
  1933 	fun relate_args env [] [] = env
  1934 	  | relate_args env _ [] = 
  1935 	    raise error ("ERROR in creating the environment for '"
  1936 			 ^id_of_scr sc^"' from \nthe items of the guard of "
  1937 			 ^metID2str metID^",\n\
  1938 			 \formal arg(s), from the script,\
  1939 			 \ miss actual arg(s), from the guards env:\n"
  1940 			 ^(string_of_int o length) formals
  1941 			 ^" formals: "^terms2str formals^"\n"
  1942 			 ^(string_of_int o length) actuals
  1943 			 ^" actuals: "^terms2str actuals)
  1944 	  | relate_args env [] actual_finds = env (*may drop Find!*)
  1945 	  | relate_args env (a::aa) (f::ff) = 
  1946 	    if type_of a = type_of f 
  1947 	    then relate_args (env @ [(a, f)]) aa ff else 
  1948 	    raise error ("ERROR in creating the environment for '"
  1949 			 ^id_of_scr sc^"' from \nthe items of the guard of "
  1950 			 ^metID2str metID^",\n\			 
  1951 			 \different types of formal arg, from the script,\
  1952 			 \ and actual arg, from the guards env:'\n\
  1953 			 \formal: '"^term2str a^"::"^(type2str o type_of) a^"'\n\
  1954 			 \actual: '"^term2str f^"::"^(type2str o type_of) f^"'\n\
  1955 			 \in\n\
  1956 			 \formals: "^terms2str formals^"\n\
  1957 			 \actuals: "^terms2str actuals)
  1958         val env = relate_args [] formals actuals;
  1959     in (ScrState (env,[],None,e_term,Safe,true), scr):istate * scr end;
  1960 
  1961 (*.decide, where to get script/istate from:
  1962    (*1*) from PblObj.env: at begin of script if no init_form
  1963    (*2*) from PblObj/PrfObj: if stac is in the middle of the script
  1964    (*3*) from rls/PrfObj: in case of detail a ruleset.*)
  1965 (* val (thy', (p,p_), pt) = (thy', (p,p_), pt);
  1966    *)
  1967 fun from_pblobj_or_detail' thy' (p,p_) pt =
  1968     if p_ mem [Pbl,Met]
  1969     then case get_obj g_env pt p of
  1970 	     None => raise error "from_pblobj_or_detail': no istate"
  1971 	   | Some is =>
  1972 	     let val metID = get_obj g_metID pt p
  1973 		 val {srls,...} = get_met metID
  1974 	     in (srls, is, (#scr o get_met) metID) end
  1975     else
  1976     let val (pbl,p',rls') = par_pbl_det pt p
  1977     in if pbl 
  1978        then (*2*)
  1979 	   let val thy = assoc_thy thy'
  1980 	       val PblObj{meth=itms,...} = get_obj I pt p'
  1981 	       val metID = get_obj g_metID pt p'
  1982 	       val {srls,...} = get_met metID
  1983 	   in (*if last_elem p = 0 (*nothing written to pt yet*)
  1984 	      then let val (is, sc) = init_scrstate thy itms metID
  1985 		   in (srls, is, sc) end
  1986 	      else*) (srls, get_istate pt (p,p_), (#scr o get_met) metID)
  1987 	   end
  1988        else (*3*)
  1989 	   (e_rls, (*FIXME: get from pbl or met !!!
  1990 		    unused for Rrls in locate_gen, next_tac*)
  1991 	    get_istate pt (p,p_),
  1992 	    case rls' of
  1993 		Rls {scr=scr,...} => scr
  1994 	      | Seq {scr=scr,...} => scr
  1995 	      | Rrls {scr=rfuns,...} => rfuns)
  1996     end;
  1997 
  1998 (*.get script and istate from PblObj, see (*1*) above.*)
  1999 fun from_pblobj' thy' (p,p_) pt = 
  2000     let val p' = par_pblobj pt p
  2001 	val thy = assoc_thy thy'
  2002 	val PblObj{meth=itms,...} = get_obj I pt p'
  2003 	val metID = get_obj g_metID pt p'
  2004 	val {srls,scr,...} = get_met metID
  2005     in if last_elem p = 0 (*nothing written to pt yet*)
  2006        then let val (is, scr) = init_scrstate thy itms metID
  2007 	    in (srls, is, scr) end
  2008        else (srls, get_istate pt (p,p_), scr)
  2009     end;
  2010     
  2011 (*.get the stactics and problems of a script as tacs
  2012   instantiated with the current environment;
  2013   l is the location which generated the given formula.*)
  2014 (*WN.12.5.03: quick-and-dirty repair for listexpressions*)
  2015 fun is_spec_pos Pbl = true
  2016   | is_spec_pos Met = true
  2017   | is_spec_pos _ = false;
  2018 
  2019 (*. fetch _all_ tactics from script .*)
  2020 fun sel_rules _ (([],Res):pos') = 
  2021     raise PTREE "no tactics applicable at the end of a calculation"
  2022 | sel_rules pt (p,p_) =
  2023   if is_spec_pos p_ 
  2024   then [get_obj g_tac pt p]
  2025   else
  2026     let val pp = par_pblobj pt p;
  2027 	val thy' = (get_obj g_domID pt pp):theory';
  2028 	val thy = assoc_thy thy';
  2029 	val metID = get_obj g_metID pt pp;
  2030 	val metID' =if metID =e_metID then(thd3 o snd3)(get_obj g_origin pt pp)
  2031 		     else metID
  2032 	val {scr=Script sc,srls,...} = get_met metID'
  2033 	val ScrState (env,_,a,v,_,_) = get_istate pt (p,p_);
  2034     in map ((stac2tac pt thy) o rep_stacexpr o #2 o
  2035 	    (handle_leaf "selrul" thy' srls env a v)) (stacpbls sc) end;
  2036 (*
  2037 > val Script sc = (#scr o get_met) ("SqRoot.thy","sqrt-equ-test");
  2038 > val env = [((term_of o the o (parse Isac.thy)) "bdv",
  2039              (term_of o the o (parse Isac.thy)) "x")];
  2040 > map ((stac2tac pt thy) o #2 o(subst_stacexpr env None e_term)) (stacpbls sc);
  2041 *)
  2042 
  2043 
  2044 (*. fetch tactics from script and filter _applicable_ tactics;
  2045     in case of Rewrite_Set* go down to _atomic_ rewrite-tactics .*)
  2046 fun sel_appl_atomic_tacs _ (([],Res):pos') = 
  2047     raise PTREE "no tactics applicable at the end of a calculation"
  2048   | sel_appl_atomic_tacs pt (p,p_) =
  2049     if is_spec_pos p_ 
  2050     then [get_obj g_tac pt p]
  2051     else
  2052 	let val pp = par_pblobj pt p
  2053 	    val thy' = (get_obj g_domID pt pp):theory'
  2054 	    val thy = assoc_thy thy'
  2055 	    val metID = get_obj g_metID pt pp
  2056 	    val metID' =if metID = e_metID 
  2057 			then (thd3 o snd3) (get_obj g_origin pt pp)
  2058 			else metID
  2059 	    val {scr=Script sc,srls,erls,rew_ord'=ro,...} = get_met metID'
  2060 	    val ScrState (env,_,a,v,_,_) = get_istate pt (p,p_)
  2061 	    val alltacs = (*we expect at least 1 stac in a script*)
  2062 		map ((stac2tac pt thy) o rep_stacexpr o #2 o
  2063 		     (handle_leaf "selrul" thy' srls env a v)) (stacpbls sc)
  2064 	    val f = case p_ of
  2065 			Frm => get_obj g_form pt p
  2066 		      | Res => (fst o (get_obj g_result pt)) p
  2067 	(*WN071231 ? replace atomic_appl_tacs with applicable_in (ineff!) ?*)
  2068 	in (distinct o flat o 
  2069 	    (map (atomic_appl_tacs thy ro erls f))) alltacs end;
  2070 	
  2071 
  2072 (*
  2073 end
  2074 open Interpreter;
  2075 *)
  2076 
  2077 (* use"ME/script.sml";
  2078    use"script.sml";
  2079    *)