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