src/Tools/isac/Interpret/script.sml
author Walther Neuper <neuper@ist.tugraz.at>
Tue, 28 Sep 2010 09:06:56 +0200
branchisac-update-Isa09-2
changeset 38031 460c24a6a6ba
parent 38029 bd062a85ec67
child 38050 4c52ad406c20
permissions -rw-r--r--
tuned error and writeln

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