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