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