src/Tools/isac/Interpret/script.sml
author Walther Neuper <neuper@ist.tugraz.at>
Wed, 04 May 2011 09:01:10 +0200
branchdecompose-isar
changeset 41972 22c5483e9bfb
parent 41968 3228aa46fd30
child 41983 4c49adbfadab
permissions -rw-r--r--
update all "Pair" to "Product_Type.Pair"

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