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