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