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