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