src/Tools/isac/Interpret/script.sml
author Walther Neuper <neuper@ist.tugraz.at>
Tue, 03 May 2011 11:16:55 +0200
branchdecompose-isar
changeset 41968 3228aa46fd30
parent 41956 03151cfbdc02
child 41972 22c5483e9bfb
permissions -rw-r--r--
updated all 'Const ("Let"..' to 'Const ("HOL.Let"..'

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