src/Tools/isac/Interpret/script.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Thu, 22 Dec 2016 10:25:49 +0100
changeset 59276 56dc790071cb
parent 59272 1d3ef477d9c8
child 59279 255c853ea2f0
permissions -rw-r--r--
--- closed structure Ctree

Notes:
# "closed" means removed "open Ctree" immediately after respective end.
# test do not work (with "! activate for Test_Isac") due to 1 "open Ctree,
see next changeset.
neuper@42248
     1
(* Title:  interpreter for scripts
neuper@42248
     2
   Author: Walther Neuper 2000
neuper@42248
     3
   (c) due to copyright terms
neuper@42248
     4
12345678901234567890123456789012345678901234567890123456789012345678901234567890
neuper@42248
     5
        10        20        30        40        50        60        70        80
neuper@42248
     6
*)
neuper@37906
     7
wneuper@59257
     8
signature LUCAS_INTERPRETER =
neuper@37906
     9
sig
neuper@37906
    10
wneuper@59276
    11
  type step = Ctree.tac_ * Generate.mout * Ctree.ptree * Ctree.pos' * Ctree.pos' list
wneuper@59276
    12
  datatype locate = NotLocatable | Steps of Ctree.istate * step list
wneuper@59257
    13
  
wneuper@59257
    14
  val next_tac : (*diss: next-tactic-function*)
wneuper@59276
    15
    theory' * rls -> Ctree.ptree * Ctree.pos' -> scr -> Ctree.istate * 'a -> Ctree.tac_ * (Ctree.istate * 'a) * (term * Ctree.safe)
wneuper@59257
    16
  val locate_gen : (*diss: locate-function*)
wneuper@59276
    17
    theory' * rls -> Ctree.tac_ -> Ctree.ptree * Ctree.pos' -> scr * 'a -> Ctree.istate * Proof.context -> locate
wneuper@59258
    18
wneuper@59258
    19
(* can these functions be local to Lucin or part of LItools ? *)
wneuper@59276
    20
  val sel_rules : Ctree.ptree -> Ctree.pos' -> Ctree.tac list 
wneuper@59257
    21
  val init_form : 'a -> scr -> (term * term) list -> term option
wneuper@59276
    22
  val tac_2tac : Ctree.tac_ -> Ctree.tac
wneuper@59276
    23
  val init_scrstate : theory -> itm list -> metID -> Ctree.istate * Proof.context * scr
wneuper@59276
    24
  val from_pblobj' : theory' -> Ctree.pos' -> Ctree.ptree -> rls * (Ctree.istate * Proof.context) * scr
wneuper@59276
    25
  val from_pblobj_or_detail' : theory' -> Ctree.pos' -> Ctree.ptree -> 
wneuper@59276
    26
    rls * (Ctree.istate * Proof.context) * scr
wneuper@59257
    27
  val rule2thm'' : rule -> thm''
wneuper@59257
    28
  val rule2rls' : rule -> string
wneuper@59257
    29
wneuper@59261
    30
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
wneuper@59258
    31
  datatype asap = Aundef | AssOnly | AssGen
wneuper@59276
    32
  datatype appy = Appy of Ctree.tac_ * Ctree.scrstate | Napp of env | Skip of term * env
wneuper@59258
    33
  datatype appy_ = Napp_ | Skip_
wneuper@59276
    34
  val appy : theory' * rls -> Ctree.ptree * Ctree.pos' -> env -> lrd list -> term -> term option -> term -> appy
wneuper@59260
    35
  val formal_args : term -> term list
wneuper@59258
    36
  val get_stac : 'a -> term -> term option 
wneuper@59260
    37
  val go : loc_ -> term -> term
wneuper@59258
    38
  val handle_leaf : string -> theory' -> rls -> env -> term option -> term -> term -> 
wneuper@59258
    39
    term option * stacexpr
wneuper@59258
    40
  val id_of_scr : term -> string
wneuper@59276
    41
   val is_spec_pos : Ctree.pos_ -> bool
wneuper@59260
    42
  val itms2args : 'a -> metID -> itm list -> term list
wneuper@59276
    43
  val nstep_up : theory' * rls -> Ctree.ptree * Ctree.pos' -> scr -> env -> lrd list -> appy_ -> 
wneuper@59258
    44
    term option -> term -> appy
wneuper@59276
    45
  val sel_appl_atomic_tacs : Ctree.ptree -> Ctree.pos' -> Ctree.tac list
wneuper@59276
    46
  val stac2tac : Ctree.ptree -> theory -> term -> Ctree.tac
wneuper@59276
    47
  val stac2tac_ : Ctree.ptree -> theory -> term -> Ctree.tac * Ctree.tac_
wneuper@59258
    48
  val upd_env_opt : env -> term option * term -> env
wneuper@59259
    49
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
neuper@37906
    50
end 
neuper@37906
    51
wneuper@59258
    52
(* traces the leaves (ie. non-tactical nodes) of Prog found by next_tac, see "and scr" *)   
wneuper@59258
    53
val trace_script = Unsynchronized.ref false; (* TODO: how are traces done in Isabelle? *)
wneuper@59258
    54
wneuper@59257
    55
(**)
wneuper@59260
    56
structure Lucin(**): LUCAS_INTERPRETER(**) =
neuper@37906
    57
struct
wneuper@59276
    58
open Ctree
wneuper@59276
    59
wneuper@59257
    60
(**)
wneuper@59257
    61
(* data for creating a new node in ctree; designed for use as:
wneuper@59257
    62
   fun ass* scrstate steps = / ... case ass* scrstate steps of /
wneuper@59257
    63
   Assoc (scrstate, steps) => ... ass* scrstate steps *)
wneuper@59257
    64
type step =
wneuper@59257
    65
    tac_         (*transformed from associated tac                   *)
wneuper@59271
    66
    * Generate.mout (*result with indentation etc.                      *)
wneuper@59257
    67
    * ptree      (*containing node created by tac_ + resp. scrstate  *)
wneuper@59257
    68
    * pos'       (*position in ptree; ptree * pos' is the proofstate *)
wneuper@59257
    69
    * pos' list; (*of ptree-nodes probably cut (by fst tac_)         *)
neuper@37906
    70
wneuper@59253
    71
fun rule2thm'' (Thm (id, thm)) = (id, thm) : thm''
wneuper@59257
    72
  | rule2thm'' r = error ("rule2thm': not defined for " ^ rule2str r);
neuper@37906
    73
fun rule2rls' (Rls_ rls) = id_rls rls
wneuper@59257
    74
  | rule2rls' r = error ("rule2rls': not defined for " ^ rule2str r);
neuper@37906
    75
neuper@37906
    76
(*.makes a (rule,term) list to a Step (m, mout, pt', p', cid) for solve;
neuper@37906
    77
   complicated with current t in rrlsstate.*)
wneuper@59257
    78
fun rts2steps steps ((pt, p), (f, f'', rss, rts), (thy', ro, er, pa)) [(r, (f', am))] =
neuper@41996
    79
      let
neuper@41996
    80
        val thy = assoc_thy thy'
neuper@41996
    81
        val ctxt = get_ctxt pt p |> insert_assumptions am
wneuper@59253
    82
	      val m = Rewrite' (thy', ro, er, pa, rule2thm'' r, f, (f', am))
neuper@41996
    83
	      val is = RrlsState (f', f'', rss, rts)
wneuper@59257
    84
	      val p = case p of (_, Frm) => p | (p', Res) => (lev_on p', Res) | _ => error "rts2steps: p1"
wneuper@59271
    85
	      val (p', cid, mout, pt') = Generate.generate1 thy m (is, ctxt) p pt
neuper@41996
    86
      in (is, (m, mout, pt', p', cid) :: steps) end
wneuper@59257
    87
  | rts2steps steps ((pt, p) ,(f, f'', rss, rts), (thy', ro, er, pa)) ((r, (f', am)) :: rts') =
neuper@41996
    88
      let
neuper@41996
    89
        val thy = assoc_thy thy'
neuper@41996
    90
        val ctxt = get_ctxt pt p |> insert_assumptions am
wneuper@59257
    91
	      val m = Rewrite' (thy', ro, er, pa, rule2thm'' r, f, (f', am))
wneuper@59257
    92
	      val is = RrlsState (f', f'', rss, rts)
wneuper@59257
    93
	      val p = case p of (_, Frm) => p | (p', Res) => (lev_on p', Res) | _ => error "rts2steps: p1"
wneuper@59271
    94
	      val (p', cid, mout, pt') = Generate.generate1 thy m (is, ctxt) p pt
neuper@41996
    95
      in rts2steps ((m, mout, pt', p', cid)::steps) 
wneuper@59257
    96
		    ((pt', p'), (f', f'', rss, rts), (thy', ro, er, pa)) rts'
wneuper@59257
    97
		  end
wneuper@59257
    98
  | rts2steps _ _ _ = error "rts2steps: uncovered fun-def"
neuper@37906
    99
wneuper@59257
   100
(* functions for the environment stack: NOT YET IMPLEMENTED
wneuper@59257
   101
fun accessenv id es = the (assoc ((top es) : env, id))
wneuper@59257
   102
    handle _ => error ("accessenv: " ^ free2str id ^ " not in env");
wneuper@59257
   103
fun updateenv id vl (es : env stack) = 
wneuper@59257
   104
    (push (overwrite(top es, (id, vl))) (pop es)) : env stack;
wneuper@59257
   105
fun pushenv id vl (es : env stack) = 
wneuper@59257
   106
    (push (overwrite(top es, (id, vl))) es) : env stack;
wneuper@59257
   107
val popenv = pop : env stack -> env stack;
wneuper@59257
   108
*)
neuper@37906
   109
neuper@37906
   110
fun de_esc_underscore str =
wneuper@59257
   111
  let
wneuper@59257
   112
    fun scan [] = []
wneuper@59257
   113
    | scan (s :: ss) = if s = "'" then (scan ss) else (s :: (scan ss))
neuper@40836
   114
  in (implode o scan o Symbol.explode) str end;
neuper@37906
   115
neuper@37906
   116
(*go at a location in a script and fetch the contents*)
neuper@37906
   117
fun go [] t = t
wneuper@59257
   118
  | go (D::p) (Abs(_, _, t0)) = go (p : loc_) t0
wneuper@59257
   119
  | go (L::p) (t1 $ _) = go p t1
wneuper@59257
   120
  | go (R::p) (_ $ t2) = go p t2
wneuper@59257
   121
  | go l _ = error ("go: no " ^ loc_2str l);
neuper@37906
   122
neuper@37906
   123
(*.get argument of first stactic in a script for init_form.*)
wneuper@59257
   124
fun get_stac thy (_ $ body) =
neuper@37906
   125
  let
neuper@37906
   126
    fun get_t y (Const ("Script.Seq",_) $ e1 $ e2) a = 
wneuper@59257
   127
    	  (case get_t y e1 a of NONE => get_t y e2 a | la => la)
neuper@37906
   128
      | get_t y (Const ("Script.Seq",_) $ e1 $ e2 $ a) _ = 
wneuper@59257
   129
    	  (case get_t y e1 a of NONE => get_t y e2 a | la => la)
neuper@37906
   130
      | get_t y (Const ("Script.Try",_) $ e) a = get_t y e a
neuper@37906
   131
      | get_t y (Const ("Script.Try",_) $ e $ a) _ = get_t y e a
neuper@37906
   132
      | get_t y (Const ("Script.Repeat",_) $ e) a = get_t y e a
neuper@37906
   133
      | get_t y (Const ("Script.Repeat",_) $ e $ a) _ = get_t y e a
neuper@37906
   134
      | get_t y (Const ("Script.Or",_) $e1 $ e2) a =
wneuper@59257
   135
    	  (case get_t y e1 a of NONE => get_t y e2 a | la => la)
neuper@37906
   136
      | get_t y (Const ("Script.Or",_) $e1 $ e2 $ a) _ =
wneuper@59257
   137
    	  (case get_t y e1 a of NONE => get_t y e2 a | la => la)
wneuper@59257
   138
      | get_t y (Const ("Script.While",_) $ _ $ e) a = get_t y e a
wneuper@59257
   139
      | get_t y (Const ("Script.While",_) $ _ $ e $ a) _ = get_t y e a
wneuper@59257
   140
      | get_t y (Const ("Script.Letpar",_) $ e1 $ Abs (_, _, e2)) a = 
wneuper@59257
   141
    	  (case get_t y e1 a of NONE => get_t y e2 a | la => la)
neuper@41968
   142
    (*| get_t y (Const ("HOL.Let",_) $ e1 $ Abs (_,_,e2)) a =
wneuper@59257
   143
	      (case get_t y e1 a of NONE => get_t y e2 a | la => la)
neuper@37906
   144
      | get_t y (Abs (_,_,e)) a = get_t y e a*)
wneuper@59257
   145
      | get_t y (Const ("HOL.Let",_) $ e1 $ Abs (_, _, _)) a =
neuper@37906
   146
    	get_t y e1 a (*don't go deeper without evaluation !*)
wneuper@59257
   147
      | get_t _ (Const ("If", _) $ _ $ _ $ _) _ = NONE
neuper@37926
   148
    	(*(case get_t y e1 a of NONE => get_t y e2 a | la => la)*)
neuper@37906
   149
    
wneuper@59257
   150
      | get_t _ (Const ("Script.Rewrite",_) $ _ $ _ $ a) _ = SOME a
wneuper@59257
   151
      | get_t _ (Const ("Script.Rewrite",_) $ _ $ _    ) a = SOME a
wneuper@59257
   152
      | get_t _ (Const ("Script.Rewrite'_Inst",_) $ _ $ _ $ _ $ a) _ = SOME a
wneuper@59257
   153
      | get_t _ (Const ("Script.Rewrite'_Inst",_) $ _ $ _ $ _ )    a = SOME a
wneuper@59257
   154
      | get_t _ (Const ("Script.Rewrite'_Set",_) $ _ $ _ $ a) _ = SOME a
wneuper@59257
   155
      | get_t _ (Const ("Script.Rewrite'_Set",_) $ _ $ _ )    a = SOME a
wneuper@59257
   156
      | get_t _ (Const ("Script.Rewrite'_Set'_Inst",_) $ _ $ _ $ _ $a)_ =SOME a
wneuper@59257
   157
      | get_t _ (Const ("Script.Rewrite'_Set'_Inst",_) $ _ $ _ $ _ )  a =SOME a
wneuper@59257
   158
      | get_t _ (Const ("Script.Calculate",_) $ _ $ a) _ = SOME a
wneuper@59257
   159
      | get_t _ (Const ("Script.Calculate",_) $ _ )    a = SOME a
neuper@37906
   160
    
wneuper@59257
   161
      | get_t _ (Const ("Script.Substitute",_) $ _ $ a) _ = SOME a
wneuper@59257
   162
      | get_t _ (Const ("Script.Substitute",_) $ _ )    a = SOME a
neuper@37906
   163
    
wneuper@59257
   164
      | get_t _ (Const ("Script.SubProblem",_) $ _ $ _) _ = NONE
neuper@37906
   165
wneuper@59257
   166
      | get_t _ _ _ = ((*tracing ("### get_t yac: list-expr "^(term2str x));*) NONE)
wneuper@59257
   167
    in get_t thy body e_term end
wneuper@59257
   168
  | get_stac _ t = error ("get_stac: no fun-def. for " ^ term2str t);
neuper@37906
   169
    
neuper@48763
   170
fun init_form thy (Prog sc) env =
wneuper@59257
   171
    (case get_stac thy sc of NONE => NONE | SOME stac => SOME (subst_atomic env stac))
neuper@38031
   172
  | init_form _ _ _ = error "init_form: no match";
neuper@37906
   173
wneuper@59257
   174
(* get the arguments of the script out of the scripts parsetree *)
wneuper@59257
   175
fun formal_args scr = (fst o split_last o snd o strip_comb) scr;
neuper@37906
   176
wneuper@59257
   177
(* get the identifier of the script out of the scripts parsetree *)
neuper@37906
   178
fun id_of_scr sc = (id_of o fst o strip_comb) sc;
neuper@37906
   179
wneuper@59257
   180
(*WN020526: not clear, when a is available in ass_up for eval_true*)
neuper@37926
   181
(*WN060906: in "fun handle_leaf" eg. uses "SOME M__"(from some PREVIOUS
neuper@37906
   182
  curried Rewrite) for CURRENT value (which may be different from PREVIOUS);
neuper@37926
   183
  thus "NONE" must be set at the end of currying (ill designed anyway)*)
wneuper@59257
   184
fun upd_env_opt env (SOME a, v) = upd_env env (a, v)
wneuper@59257
   185
  | upd_env_opt env (NONE, _) = 
wneuper@59257
   186
      ((*tracing ("*** upd_env_opt: (NONE," ^ term2str v ^ ")");*) env);
neuper@37906
   187
wneuper@59257
   188
type dsc = typ; (* <-> nam..unknow in Descript.thy *)
neuper@37906
   189
neuper@37906
   190
(*.create the actual parameters (args) of script: their order 
neuper@37906
   191
  is given by the order in met.pat .*)
neuper@37906
   192
(*WN.5.5.03: ?: does this allow for different descriptions ???
neuper@37906
   193
             ?: why not taken from formal args of script ???
neuper@37906
   194
!: FIXXXME penv: push it here in itms2args into script-evaluation*)
neuper@37906
   195
(* val (thy, mI, itms) = (thy, metID, itms);
neuper@37906
   196
   *)
wneuper@59257
   197
val errmsg = "ERROR: the guard is missing (#ppc in 'type met' added in prep_met)."
wneuper@59257
   198
fun itms2args _ mI (itms : itm list) =
wneuper@59257
   199
  let
wneuper@59257
   200
    val mvat = max_vt itms
wneuper@59257
   201
    fun okv mvat (_, vats, b, _, _) = member op = vats mvat andalso b
wneuper@59257
   202
    val itms = filter (okv mvat) itms
wneuper@59257
   203
    fun test_dsc d (_, _, _, _, itm_) = (d = d_in itm_)
wneuper@59257
   204
    fun itm2arg itms (_,(d,_)) =
wneuper@59257
   205
        case find_first (test_dsc d) itms of
wneuper@59257
   206
          NONE => error ("itms2args: '" ^ term2str d ^ "' not in itms")
wneuper@59257
   207
        | SOME (_, _, _, _, itm_) => penvval_in itm_
wneuper@59257
   208
      (*| SOME (_,_,_,_,itm_) => mk_arg thy (d_in itm_) (ts_in itm_);
wneuper@59257
   209
            penv postponed; presently penv holds already env for script*)
wneuper@59269
   210
    val pats = (#ppc o Specify.get_met) mI
wneuper@59257
   211
    val _ = if pats = [] then raise ERROR errmsg else ()
wneuper@59257
   212
  in (flat o (map (itm2arg itms))) pats end;
neuper@37906
   213
wneuper@59257
   214
(* convert a script-tac 'stac' to a tactic 'tac';
wneuper@59257
   215
   if stac is an initac, then convert to a 'tac_' (as required in appy).
wneuper@59257
   216
   arg ptree for pushing the thy specified in rootpbl into subpbls    *)
wneuper@59257
   217
fun stac2tac_ _ thy (Const ("Script.Rewrite", _) $ Free (thmID, _) $ _ $ _) =
wneuper@59257
   218
    let
wneuper@59257
   219
      val tid = (de_esc_underscore o strip_thy) thmID
wneuper@59257
   220
    in (Rewrite (tid, assoc_thm'' thy tid), Empty_Tac_) end
wneuper@59257
   221
  | stac2tac_ _ thy (Const ("Script.Rewrite'_Inst", _) $ sub $ Free (thmID, _) $ _ $ _) =
wneuper@59257
   222
    let
wneuper@59257
   223
      val subML = ((map isapair2pair) o isalist2list) sub
wneuper@59257
   224
      val subStr = subst2subs subML
wneuper@59257
   225
      val tid = (de_esc_underscore o strip_thy) thmID (*4.10.02 unnoetig*)
wneuper@59257
   226
    in (Rewrite_Inst (subStr, (tid, assoc_thm'' thy tid)), Empty_Tac_) end
wneuper@59257
   227
  | stac2tac_ _ _ (Const ("Script.Rewrite'_Set",_) $ Free (rls, _) $ _ $ _) =
wneuper@59257
   228
     (Rewrite_Set ((de_esc_underscore o strip_thy) rls), Empty_Tac_)
wneuper@59257
   229
  | stac2tac_ _ _ (Const ("Script.Rewrite'_Set'_Inst", _) $ sub $ Free (rls, _) $ _ $ _) =
wneuper@59257
   230
    let
wneuper@59257
   231
      val subML = ((map isapair2pair) o isalist2list) sub;
wneuper@59257
   232
      val subStr = subst2subs subML;
wneuper@59257
   233
    in (Rewrite_Set_Inst (subStr, rls), Empty_Tac_) end
wneuper@59257
   234
  | stac2tac_ _ _ (Const ("Script.Calculate", _) $ Free (op_, _) $ _) = (Calculate op_, Empty_Tac_)
wneuper@59257
   235
  | stac2tac_ _ _ (Const ("Script.Take", _) $ t) = (Take (term2str t), Empty_Tac_)
wneuper@59257
   236
  | stac2tac_ _ _ (Const ("Script.Substitute", _) $ isasub $ _) =
wneuper@59257
   237
    (Substitute ((subte2sube o isalist2list) isasub), Empty_Tac_)
wneuper@59257
   238
  | stac2tac_ _ thy (Const("Script.Check'_elementwise", _) $ _ $ 
wneuper@59257
   239
    (Const ("Set.Collect", _) $ Abs (_, _, pred))) =
wneuper@59257
   240
      (Check_elementwise (term_to_string''' thy pred), Empty_Tac_)
wneuper@59257
   241
  | stac2tac_ _ _ (Const("Script.Or'_to'_List", _) $ _ ) = (Or_to_List, Empty_Tac_)
wneuper@59257
   242
  | stac2tac_ _ _ (Const ("Script.Tac", _) $ Free (str, _)) = 
wneuper@59257
   243
    (Tac ((de_esc_underscore o strip_thy) str),  Empty_Tac_) 
neuper@37906
   244
neuper@41990
   245
    (*compare "| assod _ (Subproblem'"*)
wneuper@59257
   246
  | stac2tac_ pt _ (stac as Const ("Script.SubProblem",_) $
wneuper@59257
   247
	  (Const ("Product_Type.Pair",_) $Free (dI', _) $ (Const ("Product_Type.Pair", _) $ pI' $ mI')) $ 
wneuper@59257
   248
	    ags') =
wneuper@59257
   249
    let
wneuper@59257
   250
      val dI = ((implode o drop_last(*.."'"*) o Symbol.explode) dI')(*^""*);
wneuper@59257
   251
      val thy = maxthy (assoc_thy dI) (rootthy pt);
wneuper@59257
   252
	    val pI = ((map (de_esc_underscore o free2str)) o isalist2list) pI';
wneuper@59257
   253
	    val mI = ((map (de_esc_underscore o free2str)) o isalist2list) mI';
wneuper@59257
   254
	    val ags = isalist2list ags';
wneuper@59257
   255
	    val (pI, pors, mI) = 
wneuper@59257
   256
	      if mI = ["no_met"] 
wneuper@59257
   257
	      then
wneuper@59257
   258
          let
wneuper@59269
   259
            val pors = (Chead.match_ags thy ((#ppc o Specify.get_pbt) pI) ags)
wneuper@59257
   260
		          handle ERROR "actual args do not match formal args" 
wneuper@59265
   261
			        => (Chead.match_ags_msg pI stac ags(*raise exn*); [])
wneuper@59269
   262
		        val pI' = Specify.refine_ori' pors pI;
wneuper@59257
   263
		      in (pI', pors (* refinement over models with diff.prec only *), 
wneuper@59269
   264
		          (hd o #met o Specify.get_pbt) pI') end
wneuper@59269
   265
	      else (pI, (Chead.match_ags thy ((#ppc o Specify.get_pbt) pI) ags)
wneuper@59257
   266
		      handle ERROR "actual args do not match formal args"
wneuper@59265
   267
		      => (Chead.match_ags_msg pI stac ags(*raise exn*); []), mI);
wneuper@59265
   268
      val (fmz_, vals) = Chead.oris2fmz_vals pors;
wneuper@59269
   269
	    val {cas,ppc,thy,...} = Specify.get_pbt pI
wneuper@59257
   270
	    val dI = theory2theory' thy (*.take dI from _refined_ pbl.*)
wneuper@59257
   271
	    val dI = theory2theory' (maxthy (assoc_thy dI) (rootthy pt));
wneuper@59257
   272
      val ctxt = dI |> Thy_Info.get_theory |> Proof_Context.init_global |> declare_constraints' vals
wneuper@59257
   273
	    val hdl =
wneuper@59257
   274
        case cas of
wneuper@59257
   275
		      NONE => pblterm dI pI
wneuper@59265
   276
		    | SOME t => subst_atomic ((Chead.vars_of_pbl_' ppc) ~~~ vals) t
wneuper@59257
   277
      val f = subpbl (strip_thy dI) pI
wneuper@59257
   278
    in (Subproblem (dI, pI),	Subproblem' ((dI, pI, mI), pors, hdl, fmz_, ctxt, f))
wneuper@59257
   279
    end
wneuper@59257
   280
  | stac2tac_ _ thy t = error ("stac2tac_ TODO: no match for " ^ term_to_string''' thy t);
neuper@37906
   281
neuper@37906
   282
fun stac2tac pt thy t = (fst o stac2tac_ pt thy) t;
neuper@37906
   283
neuper@37906
   284
datatype ass = 
neuper@41983
   285
    Ass of
neuper@41983
   286
      tac_ *   (* SubProblem gets args instantiated in assod *)
neuper@41983
   287
  	  term     (* for itr_arg, result in ets *)
neuper@41983
   288
  | AssWeak of
neuper@41983
   289
      tac_ *
neuper@41983
   290
  	  term     (*for itr_arg,result in ets*)
neuper@41983
   291
  | NotAss;
neuper@37906
   292
neuper@42360
   293
(* check if tac_ is associated with stac.
neuper@42360
   294
   Additional task: check if term t (the result has been calculated from) in tac_
neuper@42360
   295
   has been changed (see "datatype tac_"); if yes, recalculate result
neuper@42360
   296
   TODO.WN120106 recalculate impl.only for Substitute'
neuper@37906
   297
args
neuper@42360
   298
  pt     : ptree for pushing the thy specified in rootpbl into subpbls
neuper@42360
   299
  d      : unused (planned for data for comparison)
neuper@42360
   300
  tac_   : from user (via applicable_in); to be compared with ...
neuper@42360
   301
  stac   : found in Script
neuper@37906
   302
returns
neuper@42360
   303
  Ass    : associated: e.g. thmID in stac = thmID in m
neuper@37906
   304
                       +++ arg   in stac = arg   in m
neuper@42360
   305
  AssWeak: weakly ass.:e.g. thmID in stac = thmID in m, //arg//
neuper@42360
   306
  NotAss :             e.g. thmID in stac/=/thmID in m (not =)
neuper@42360
   307
*)
wneuper@59257
   308
fun assod _ _ (m as Rewrite_Inst' (_, _, _, _, _, thm'' as (thmID, _), f, (f', _))) stac =
wneuper@59252
   309
    (case stac of
wneuper@59257
   310
	    (Const ("Script.Rewrite'_Inst", _) $ _ $ Free (thmID_, _) $ _ $ f_) =>
wneuper@59257
   311
	      if thmID = thmID_
wneuper@59257
   312
        then 
wneuper@59257
   313
	        if f = f_ 
wneuper@59257
   314
          then ((*tracing"3### assod ..Ass";*) Ass (m,f')) 
wneuper@59257
   315
	        else ((*tracing"3### assod ..AssWeak";*) AssWeak(m, f'))
wneuper@59257
   316
	      else ((*tracing"3### assod ..NotAss";*) NotAss)
wneuper@59257
   317
    | (Const ("Script.Rewrite'_Set'_Inst",_) $ _ $ Free (rls_, _) $ _ $ f_) =>
wneuper@59263
   318
	      if Rtools.contains_rule (Thm thm'') (assoc_rls rls_)
wneuper@59257
   319
        then if f = f_ then Ass (m,f') else AssWeak (m,f')
wneuper@59257
   320
	      else NotAss
wneuper@59257
   321
    | _ => NotAss)
wneuper@59257
   322
  | assod _ _ (m as Rewrite' (_, _, _, _, thm'' as (thmID, _), f, (f', _))) stac =
wneuper@59252
   323
    (case stac of
wneuper@59257
   324
	    (Const ("Script.Rewrite", _) $ Free (thmID_, _) $ _ $ f_) =>
wneuper@59257
   325
	      ((*tracing ("3### assod: stac = " ^ ter2str t);
wneuper@59257
   326
	       tracing ("3### assod: f(m)= " ^ term2str f);*)
wneuper@59257
   327
	      if thmID = thmID_
wneuper@59257
   328
        then 
wneuper@59257
   329
	        if f = f_
wneuper@59257
   330
          then ((*tracing"3### assod ..Ass";*) Ass (m,f')) 
wneuper@59257
   331
	        else 
wneuper@59257
   332
            ((*tracing"### assod ..AssWeak";
wneuper@59257
   333
		         tracing("### assod: f(m)  = " ^ term2str f);
wneuper@59257
   334
		         tracing("### assod: f(stac)= " ^ term2str f_)*)
wneuper@59257
   335
		         AssWeak (m,f'))
wneuper@59257
   336
	      else ((*tracing"3### assod ..NotAss";*) NotAss))
wneuper@59257
   337
    | (Const ("Script.Rewrite'_Set", _) $ Free (rls_, _) $ _ $ f_) =>
wneuper@59263
   338
	       if Rtools.contains_rule (Thm thm'') (assoc_rls rls_)
wneuper@59257
   339
         then if f = f_ then Ass (m, f') else AssWeak (m, f')
wneuper@59257
   340
	       else NotAss
wneuper@59257
   341
    | _ => NotAss)
wneuper@59257
   342
  | assod _ _ (m as Rewrite_Set_Inst' (_, _, _, rls, f, (f', _))) 
wneuper@59257
   343
      (Const ("Script.Rewrite'_Set'_Inst", _) $ _ $ Free (rls_, _) $ _ $ f_) = 
wneuper@59257
   344
    if id_rls rls = rls_ 
wneuper@59257
   345
    then if f = f_ then Ass (m, f') else AssWeak (m ,f')
wneuper@59257
   346
    else NotAss
wneuper@59257
   347
  | assod _ _ (m as Detail_Set_Inst' (_, _, _, rls, f, (f',_))) 
wneuper@59257
   348
      (Const ("Script.Rewrite'_Set'_Inst", _) $ _ $ Free (rls_, _) $ _ $ f_) = 
wneuper@59257
   349
    if id_rls rls = rls_
wneuper@59257
   350
    then if f = f_ then Ass (m, f') else AssWeak (m, f')
wneuper@59257
   351
    else NotAss
wneuper@59257
   352
  | assod _ _ (m as Rewrite_Set' (_, _, rls, f, (f', _))) 
wneuper@59257
   353
      (Const ("Script.Rewrite'_Set", _) $ Free (rls_, _) $ _ $ f_) = 
wneuper@59257
   354
    if id_rls rls = rls_
wneuper@59257
   355
    then if f = f_ then Ass (m, f') else AssWeak (m, f')
wneuper@59257
   356
    else NotAss
wneuper@59257
   357
  | assod _ _ (m as Detail_Set' (_, _, rls, f, (f', _))) 
wneuper@59257
   358
      (Const ("Script.Rewrite'_Set", _) $ Free (rls_, _) $ _ $ f_) = 
wneuper@59257
   359
    if id_rls rls = rls_
wneuper@59257
   360
    then if f = f_ then Ass (m, f') else AssWeak (m, f')
wneuper@59257
   361
    else NotAss
wneuper@59257
   362
  | assod _ _ (m as Calculate' (_, op_, f, (f', _))) stac =
wneuper@59257
   363
    (case stac of
wneuper@59257
   364
	    (Const ("Script.Calculate",_) $ Free (op__,_) $ f_) =>
wneuper@59257
   365
	      if op_ = op__
wneuper@59257
   366
        then if f = f_ then Ass (m, f') else AssWeak (m, f')
wneuper@59257
   367
	      else NotAss
wneuper@59257
   368
    | (Const ("Script.Rewrite'_Set'_Inst", _) $ _ $ Free(rls_,_) $_$f_)  =>
wneuper@59257
   369
        let val thy = assoc_thy "Isac";
wneuper@59257
   370
        in
wneuper@59263
   371
          if Rtools.contains_rule (Calc (assoc_calc' thy op_ |> snd)) (assoc_rls rls_)
wneuper@59257
   372
          then if f = f_ then Ass (m, f') else AssWeak (m, f')
wneuper@59257
   373
          else NotAss
wneuper@59257
   374
        end
wneuper@59257
   375
    | (Const ("Script.Rewrite'_Set",_) $ Free (rls_, _) $ _ $ f_) =>
wneuper@59257
   376
        let val thy = assoc_thy "Isac";
wneuper@59257
   377
        in
wneuper@59263
   378
          if Rtools.contains_rule (Calc (assoc_calc' thy op_ |> snd)) (assoc_rls rls_)
wneuper@59257
   379
          then if f = f_ then Ass (m,f') else AssWeak (m,f')
wneuper@59257
   380
          else NotAss
wneuper@59257
   381
        end
wneuper@59257
   382
    | _ => NotAss)
wneuper@59257
   383
  | assod _ _ (m as Check_elementwise' (consts, _, (consts_chkd, _)))
wneuper@59257
   384
      (Const ("Script.Check'_elementwise",_) $ consts' $ _) =
wneuper@59257
   385
    if consts = consts'
wneuper@59257
   386
    then Ass (m, consts_chkd)
wneuper@59257
   387
    else NotAss
wneuper@59257
   388
  | assod _ _ (m as Or_to_List' (_, list)) (Const ("Script.Or'_to'_List", _) $ _) = Ass (m, list) 
wneuper@59257
   389
  | assod _ _ (m as Take' term) (Const ("Script.Take", _) $ _) = Ass (m, term)
wneuper@59257
   390
  | assod _ _ (m as Substitute' (ro, erls, subte, f, f')) (Const ("Script.Substitute", _) $ _ $ t) =
wneuper@59257
   391
	  if f = t then Ass (m, f')
wneuper@59257
   392
	  else (*compare | applicable_in (p,p_) pt (m as Substitute sube)*)
wneuper@59257
   393
		  if foldl and_ (true, map contains_Var subte)
wneuper@59257
   394
		  then
wneuper@59257
   395
		    let val t' = subst_atomic (map HOLogic.dest_eq subte (*TODO subte2subst*)) t
wneuper@59257
   396
		    in if t = t' then error "assod: Substitute' not applicable to val of Expr"
wneuper@59257
   397
		       else Ass (Substitute' (ro, erls, subte, t, t'), t')
wneuper@59257
   398
		    end
wneuper@59257
   399
		  else (case rewrite_terms_ (Isac()) ro erls subte t of
neuper@42360
   400
		         SOME (t', _) =>  Ass (Substitute' (ro, erls, subte, t, t'), t')
neuper@42360
   401
		       | NONE => error "assod: Substitute' not applicable to val of Expr")
wneuper@59257
   402
  | assod _ _ (m as Tac_ (thy, _, id, f')) (Const ("Script.Tac",_) $ Free (id', _)) =
wneuper@59257
   403
    if id = id'
wneuper@59257
   404
    then Ass (m, ((Thm.term_of o the o (parse thy)) f'))
wneuper@59257
   405
    else NotAss
neuper@37906
   406
neuper@41990
   407
    (*compare "| stac2tac_ thy (Const ("Script.SubProblem",_)"*)
wneuper@59257
   408
  | assod pt _ (Subproblem' ((domID, pblID, _), _, _, _, _, _))
wneuper@59257
   409
	    (stac as Const ("Script.SubProblem",_) $ (Const ("Product_Type.Pair",_) $
wneuper@59257
   410
		    Free (dI',_) $ (Const ("Product_Type.Pair",_) $ pI' $ mI')) $ ags') =
wneuper@59257
   411
    let 
wneuper@59257
   412
      val dI = ((implode o drop_last(*.."'"*) o Symbol.explode) dI')(*^""*);
wneuper@59257
   413
      val thy = maxthy (assoc_thy dI) (rootthy pt);
wneuper@59257
   414
	    val pI = ((map (de_esc_underscore o free2str)) o isalist2list) pI';
wneuper@59257
   415
	    val mI = ((map (de_esc_underscore o free2str)) o isalist2list) mI';
wneuper@59257
   416
	    val ags = isalist2list ags';
wneuper@59257
   417
	    val (pI, pors, mI) = 
wneuper@59257
   418
	      if mI = ["no_met"] 
wneuper@59257
   419
	      then
wneuper@59257
   420
          let
wneuper@59269
   421
            val pors = (Chead.match_ags thy ((#ppc o Specify.get_pbt) pI) ags)
wneuper@59257
   422
		          handle ERROR "actual args do not match formal args"
wneuper@59265
   423
			          => (Chead.match_ags_msg pI stac ags(*raise exn*);[]);
wneuper@59269
   424
		        val pI' = Specify.refine_ori' pors pI;
wneuper@59269
   425
		      in (pI', pors (*refinement over models with diff.prec only*), (hd o #met o Specify.get_pbt) pI')
wneuper@59257
   426
          end
wneuper@59269
   427
	      else (pI, (Chead.match_ags thy ((#ppc o Specify.get_pbt) pI) ags)
wneuper@59257
   428
		      handle ERROR "actual args do not match formal args"
wneuper@59265
   429
		      => (Chead.match_ags_msg pI stac ags(*raise exn*); []), mI);
wneuper@59265
   430
      val (fmz_, vals) = Chead.oris2fmz_vals pors;
wneuper@59269
   431
	    val {cas, ppc, thy, ...} = Specify.get_pbt pI
wneuper@59257
   432
	    val dI = theory2theory' thy (*take dI from _refined_ pbl*)
wneuper@59257
   433
	    val dI = theory2theory' (maxthy (assoc_thy dI) (rootthy pt))
wneuper@59257
   434
	    val ctxt = dI |> Thy_Info.get_theory |> Proof_Context.init_global |> declare_constraints' vals
wneuper@59257
   435
	    val hdl = 
wneuper@59257
   436
        case cas of
wneuper@59257
   437
		      NONE => pblterm dI pI
wneuper@59265
   438
		    | SOME t => subst_atomic ((Chead.vars_of_pbl_' ppc) ~~~ vals) t
wneuper@59257
   439
      val f = subpbl (strip_thy dI) pI
wneuper@59257
   440
    in 
wneuper@59257
   441
      if domID = dI andalso pblID = pI
wneuper@59257
   442
      then Ass (Subproblem' ((dI, pI, mI), pors, hdl, fmz_, ctxt, f), f) 
wneuper@59257
   443
      else NotAss
wneuper@59257
   444
    end
wneuper@59257
   445
  | assod _ _ m _ = 
wneuper@59257
   446
    (if (!trace_script) 
wneuper@59257
   447
     then tracing("@@@ the 'tac_' proposed to apply does NOT match the leaf found in the script:\n"
wneuper@59257
   448
		   ^ "@@@ tac_ = " ^ tac_2str m)
wneuper@59257
   449
     else ();
wneuper@59257
   450
    NotAss);
neuper@37906
   451
wneuper@59257
   452
fun tac_2tac (Refine_Tacitly' (pI, _, _, _, _)) = Refine_Tacitly pI
wneuper@59257
   453
  | tac_2tac (Model_Problem' (_, _, _)) = Model_Problem
wneuper@59257
   454
  | tac_2tac (Add_Given' (t, _)) = Add_Given t
wneuper@59257
   455
  | tac_2tac (Add_Find' (t, _)) = Add_Find t
wneuper@59257
   456
  | tac_2tac (Add_Relation' (t, _)) = Add_Relation t
neuper@37906
   457
 
wneuper@59257
   458
  | tac_2tac (Specify_Theory' dI) = Specify_Theory dI
wneuper@59257
   459
  | tac_2tac (Specify_Problem' (dI, _)) = Specify_Problem dI
wneuper@59257
   460
  | tac_2tac (Specify_Method' (dI, _, _)) = Specify_Method dI
neuper@37906
   461
  
wneuper@59253
   462
  | tac_2tac (Rewrite' (_, _, _, _, thm, _, _)) = Rewrite thm
wneuper@59253
   463
  | tac_2tac (Rewrite_Inst' (_, _, _, _, sub, thm, _, _)) = Rewrite_Inst (subst2subs sub, thm)
neuper@37906
   464
wneuper@59257
   465
  | tac_2tac (Rewrite_Set' (_, _, rls, _, _)) = Rewrite_Set (id_rls rls)
wneuper@59257
   466
  | tac_2tac (Detail_Set' (_, _, rls, _, _)) = Detail_Set (id_rls rls)
neuper@37906
   467
wneuper@59257
   468
  | tac_2tac (Rewrite_Set_Inst' (_, _, sub, rls, _, _)) = 
wneuper@59257
   469
    Rewrite_Set_Inst (subst2subs sub,id_rls rls)
wneuper@59257
   470
  | tac_2tac (Detail_Set_Inst' (_, _, sub, rls, _, _)) = 
wneuper@59257
   471
    Detail_Set_Inst (subst2subs sub,id_rls rls)
neuper@37906
   472
wneuper@59257
   473
  | tac_2tac (Calculate' (_, op_, _, _)) = Calculate (op_)
wneuper@59257
   474
  | tac_2tac (Check_elementwise' (_, pred, _)) = Check_elementwise pred
neuper@37906
   475
neuper@37906
   476
  | tac_2tac (Or_to_List' _) = Or_to_List
neuper@37906
   477
  | tac_2tac (Take' term) = Take (term2str term)
wneuper@59257
   478
  | tac_2tac (Substitute' (_, _, subte, _, _)) = Substitute (subte2sube subte) 
wneuper@59257
   479
  | tac_2tac (Tac_ (_, _, id, _)) = Tac id
neuper@37906
   480
neuper@42018
   481
  | tac_2tac (Subproblem' ((domID, pblID, _), _, _, _,_ ,_)) = Subproblem (domID, pblID)
neuper@42018
   482
  | tac_2tac (Check_Postcond' (pblID, _)) = Check_Postcond pblID
neuper@37906
   483
  | tac_2tac Empty_Tac_ = Empty_Tac
wneuper@59257
   484
  | tac_2tac m = error ("tac_2tac: not impl. for "^(tac_2str m));
neuper@37906
   485
wneuper@59257
   486
val idT = Type ("Script.ID", []);
neuper@37938
   487
neuper@37906
   488
fun make_rule thy t =
wneuper@59184
   489
  let val ct = Thm.global_cterm_of thy (Trueprop $ t)
wneuper@59186
   490
  in Thm (term_to_string''' thy (Thm.term_of ct), Thm.make_thm ct) end;
neuper@37906
   491
wneuper@59257
   492
fun rep_tac_ (Rewrite_Inst' (thy', _, _, put, subs, (thmID, _), f, (f', _))) = 
wneuper@59257
   493
    let val fT = type_of f;
wneuper@59257
   494
      val b = if put then @{term True} else @{term False};
wneuper@59257
   495
      val sT = (type_of o fst o hd) subs;
wneuper@59257
   496
      val subs' = list2isalist (HOLogic.mk_prodT (sT, sT)) (map HOLogic.mk_prod subs);
wneuper@59257
   497
      val sT' = type_of subs';
wneuper@59257
   498
      val lhs = Const ("Script.Rewrite'_Inst", [sT', idT, bool, fT] ---> fT) 
wneuper@59257
   499
        $ subs' $ Free (thmID, idT) $ b $ f;
wneuper@59257
   500
    in (((make_rule (assoc_thy thy')) o HOLogic.mk_eq) (lhs, f'), (lhs, f')) end
wneuper@59257
   501
  | rep_tac_ (Rewrite' (thy', _, _, put, (thmID, _), f, (f', _)))=
wneuper@59257
   502
    let 
wneuper@59257
   503
      val fT = type_of f;
wneuper@59257
   504
      val b = if put then @{term True} else @{term False};
wneuper@59257
   505
      val lhs = Const ("Script.Rewrite", [idT, HOLogic.boolT, fT] ---> fT)
wneuper@59257
   506
        $ Free (thmID, idT) $ b $ f;
wneuper@59257
   507
    in (((make_rule (assoc_thy thy')) o HOLogic.mk_eq) (lhs, f'), (lhs, f')) end
wneuper@59257
   508
  | rep_tac_ (Rewrite_Set_Inst' (_, _, _, _, _, (f', _))) = (e_rule, (e_term, f'))
wneuper@59257
   509
  | rep_tac_ (Rewrite_Set' (thy', put, rls, f, (f', _))) =
wneuper@59257
   510
    let 
wneuper@59257
   511
      val fT = type_of f;
wneuper@59257
   512
      val b = if put then @{term True} else @{term False};
wneuper@59257
   513
      val lhs = Const ("Script.Rewrite'_Set", [idT, bool, fT] ---> fT) 
wneuper@59257
   514
        $ Free (id_rls rls, idT) $ b $ f;
wneuper@59257
   515
    in (((make_rule (assoc_thy thy')) o HOLogic.mk_eq) (lhs,f'),(lhs,f')) end
wneuper@59257
   516
  | rep_tac_ (Calculate' (thy', op_, f, (f', _)))=
wneuper@59257
   517
    let
wneuper@59257
   518
      val fT = type_of f;
wneuper@59257
   519
      val lhs = Const ("Script.Calculate",[idT,fT] ---> fT) $ Free (op_,idT) $ f
wneuper@59257
   520
    in (((make_rule (assoc_thy thy')) o HOLogic.mk_eq) (lhs,f'),(lhs,f')) end
wneuper@59257
   521
  | rep_tac_ (Check_elementwise' (_, _, (t', _))) = (Erule, (e_term, t'))
wneuper@59257
   522
  | rep_tac_ (Subproblem' (_, _, _, _, _, t')) = (Erule, (e_term, t'))
wneuper@59257
   523
  | rep_tac_ (Take' t') = (Erule, (e_term, t'))
wneuper@59257
   524
  | rep_tac_ (Substitute' (_, _, _, t, t')) = (Erule, (t, t'))
wneuper@59257
   525
  | rep_tac_ (Or_to_List' (t, t')) = (Erule, (t, t'))
wneuper@59257
   526
  | rep_tac_ m = error ("rep_tac_: not impl.for " ^ tac_2str m)
neuper@37906
   527
wneuper@59257
   528
fun tac_2res m = (snd o snd o rep_tac_) m;
neuper@37906
   529
neuper@41996
   530
(* handle a leaf at the end of recursive descent:
neuper@42360
   531
   a leaf is either a tactic or an 'expr' in "let v = expr"
neuper@42360
   532
   where "expr" does not contain a tactic.
neuper@42360
   533
   Handling a leaf comprises
neuper@37906
   534
   (1) 'subst_stacexpr' substitute env and complete curried tactic
neuper@37906
   535
   (2) rewrite the leaf by 'srls'
neuper@41996
   536
*)
neuper@37906
   537
fun handle_leaf call thy srls E a v t =
neuper@41996
   538
      (*WN050916 'upd_env_opt' is a blind copy from previous version*)
wneuper@59257
   539
    case subst_stacexpr E a v t of
wneuper@59257
   540
	    (a', STac stac) => (*script-tactic*)
wneuper@59257
   541
	      let val stac' =
wneuper@59257
   542
            eval_listexpr_ (assoc_thy thy) srls (subst_atomic (upd_env_opt E (a,v)) stac)
wneuper@59257
   543
	      in
wneuper@59257
   544
          (if (! trace_script) 
wneuper@59257
   545
	         then tracing ("@@@ "^call^" leaf '"^term2str t^"' ---> STac '"^term2str stac ^"'")
wneuper@59257
   546
	         else ();
wneuper@59257
   547
	         (a', STac stac'))
wneuper@59257
   548
	      end
wneuper@59257
   549
    | (a', Expr lexpr) => (*leaf-expression*)
wneuper@59257
   550
	      let val lexpr' =
wneuper@59257
   551
            eval_listexpr_ (assoc_thy thy) srls (subst_atomic (upd_env_opt E (a,v)) lexpr)
wneuper@59257
   552
	      in
wneuper@59257
   553
          (if (! trace_script) 
wneuper@59257
   554
	         then tracing("@@@ "^call^" leaf '"^term2str t^"' ---> Expr '"^term2str lexpr'^"'")
wneuper@59257
   555
	         else ();
wneuper@59257
   556
	         (a', Expr lexpr')) (*lexpr' is the value of the Expr*)
wneuper@59257
   557
	      end;
neuper@37906
   558
wneuper@59257
   559
(** locate an applicable stac in a script **)
wneuper@59257
   560
datatype assoc = (* ExprVal in the sense of denotational semantics               *)
wneuper@59257
   561
  Assoc of       (* the stac is associated, strongly or weakly                   *)
wneuper@59257
   562
  scrstate *     (* the current; returned for next_tac etc. outside ass*         *)  
wneuper@59257
   563
  (step list)    (* list of steps done until associated stac found;
wneuper@59257
   564
	                  initiated with the data for doing the 1st step,
wneuper@59257
   565
                    thus the head holds these data further on,
wneuper@59257
   566
		                while the tail holds steps finished (incl.scrstate in ptree) *)
wneuper@59257
   567
| NasApp of      (* stac not associated, but applicable, ptree-node generated    *)
neuper@37906
   568
  scrstate * (step list)
wneuper@59257
   569
| NasNap of      (* stac not associated, not applicable, nothing generated;
wneuper@59257
   570
	                  for distinction in Or, for leaving iterations, leaving Seq,
wneuper@59257
   571
		                evaluate scriptexpressions                                   *)
neuper@37906
   572
  term * env;
wneuper@59257
   573
fun assoc2str (Assoc _) = "Assoc"
wneuper@59257
   574
  | assoc2str (NasNap _) = "NasNap"
neuper@37906
   575
  | assoc2str (NasApp _) = "NasApp";
neuper@37906
   576
wneuper@59257
   577
datatype asap = (* arg. of assy _only_ for distinction w.r.t. Or                 *)
wneuper@59257
   578
  Aundef        (* undefined: set only by (topmost) Or                           *)
wneuper@59257
   579
| AssOnly       (* do not execute appl stacs - there could be an associated
wneuper@59257
   580
	                 in parallel Or-branch                                         *)
wneuper@59257
   581
| AssGen;       (* no Ass(Weak) found within Or, thus 
wneuper@59257
   582
                   search for _applicable_ stacs, execute and generate pt        *)
wneuper@59257
   583
(*this constructions doesnt allow arbitrary nesting of Or !!!                    *)
neuper@37906
   584
wneuper@59257
   585
(* assy, ass_up, astep_up scan for locate_gen in a script.
wneuper@59257
   586
   search is clearly separated into (1)-(2):
wneuper@59257
   587
   (1) assy is recursive descent;
wneuper@59257
   588
   (2) ass_up resumes interpretation at a location somewhere in the script;
wneuper@59257
   589
       astep_up does only get to the parentnode of the scriptexpr.
wneuper@59257
   590
   consequence:
wneuper@59257
   591
   * call of (2) means _always_ that in this branch below
wneuper@59257
   592
     there was an appl.stac (Repeat, Or e1, ...) found by the previous step.
wneuper@59257
   593
*)
wneuper@59257
   594
(*WN161112 blanks between list elements left as is until istate is introduced here*)
wneuper@59257
   595
fun assy ya ((E,l,a,v,S,b),ss) (Const ("HOL.Let",_) $ e $ (Abs (id,T,body))) =
wneuper@59257
   596
    (case assy ya ((E , l @ [L, R], a,v,S,b),ss) e of
wneuper@59257
   597
       NasApp ((E',l,a,v,S,_),ss) => 
wneuper@59257
   598
         let
wneuper@59257
   599
           val id' = mk_Free (id, T);
wneuper@59257
   600
           val E' = upd_env E' (id', v);
wneuper@59257
   601
         in assy ya ((E', l @ [R, D], a,v,S,b),ss) body end
wneuper@59257
   602
     | NasNap (v,E) =>
wneuper@59257
   603
         let
wneuper@59257
   604
           val id' = mk_Free (id, T);
wneuper@59257
   605
           val E' = upd_env E (id', v);
wneuper@59257
   606
         in assy ya ((E', l @ [R, D], a,v,S,b),ss) body end
wneuper@59257
   607
     | ay => ay)
wneuper@59257
   608
  | assy (ya as (thy,_,srls,_,_)) ((E,l,_,v,S,b),ss) (Const ("Script.While",_) $ c $ e $ a) =
wneuper@59257
   609
    if eval_true_ thy srls (subst_atomic (upd_env E (a,v)) c) 
wneuper@59257
   610
    then assy ya ((E, l @ [L, R], SOME a,v,S,b),ss)  e
wneuper@59257
   611
    else NasNap (v, E)
wneuper@59257
   612
  | assy (ya as (thy,_,srls,_,_)) ((E,l,a,v,S,b),ss) (Const ("Script.While",_) $ c $ e) =
wneuper@59257
   613
    if eval_true_ thy srls (subst_atomic (upd_env_opt E (a,v)) c) 
wneuper@59257
   614
    then assy ya ((E, l @ [R], a,v,S,b),ss) e
wneuper@59257
   615
    else NasNap (v, E)
wneuper@59257
   616
  | assy (ya as (thy,_,srls,_,_)) ((E,l,a,v,S,b),ss) (Const ("If",_) $ c $ e1 $ e2) =
wneuper@59257
   617
    if eval_true_ thy srls (subst_atomic (upd_env_opt E (a,v)) c) 
wneuper@59257
   618
    then assy ya ((E, l @ [L, R], a,v,S,b),ss) e1
wneuper@59257
   619
    else assy ya ((E, l @ [R], a,v,S,b),ss) e2 
wneuper@59257
   620
  | assy ya ((E,l,_,v,S,b),ss) (Const ("Script.Try",_) $ e $ a) =
wneuper@59257
   621
    (case assy ya ((E, l @ [L, R], SOME a,v,S,b),ss) e of ay => ay) 
wneuper@59257
   622
  | assy ya ((E,l,a,v,S,b),ss) (Const ("Script.Try",_) $ e) =
wneuper@59257
   623
    (case assy ya ((E, l @ [R], a,v,S,b),ss) e of ay => ay)
wneuper@59257
   624
  | assy ya ((E,l,_,v,S,b),ss) (Const ("Script.Seq",_) $e1 $ e2 $ a) =
wneuper@59257
   625
    (case assy ya ((E, l @ [L, L, R], SOME a,v,S,b),ss) e1 of
wneuper@59257
   626
	     NasNap (v, E) => assy ya ((E, l @ [L, R], SOME a,v,S,b),ss) e2
wneuper@59257
   627
     | NasApp ((E,_,_,v,_,_),ss) => assy ya ((E, l @ [L, R], SOME a,v,S,b),ss) e2
wneuper@59257
   628
     | ay => ay)
wneuper@59257
   629
  | assy ya ((E,l,a,v,S,b),ss) (Const ("Script.Seq",_) $e1 $ e2) =
wneuper@59257
   630
    (case assy ya ((E, l @ [L, R], a,v,S,b),ss) e1 of
wneuper@59257
   631
	     NasNap (v, E) => assy ya ((E, l @ [R], a,v,S,b),ss) e2
wneuper@59257
   632
     | NasApp ((E,_,_,v,_,_),ss) => assy ya ((E, l @ [R], a,v,S,b),ss) e2
wneuper@59257
   633
     | ay => ay)
wneuper@59257
   634
  | assy ya ((E,l,_,v,S,b),ss) (Const ("Script.Repeat",_) $ e $ a) =
wneuper@59257
   635
    assy ya ((E,(l @ [L, R]),SOME a,v,S,b),ss) e
wneuper@59257
   636
  | assy ya ((E,l,a,v,S,b),ss) (Const ("Script.Repeat",_) $ e) =
wneuper@59257
   637
    assy ya ((E,(l @ [R]),a,v,S,b),ss) e
wneuper@59257
   638
  | assy (y,x,s,sc,Aundef) ((E,l,_,v,S,b),ss) (Const ("Script.Or",_) $e1 $ e2 $ a) =
wneuper@59257
   639
    (case assy (y,x,s,sc,AssOnly) ((E,(l @ [L, L, R]),SOME a,v,S,b),ss) e1 of
wneuper@59257
   640
	     NasNap (v, E) => 
wneuper@59257
   641
	       (case assy (y,x,s,sc,AssOnly) ((E,(l @ [L, R]),SOME a,v,S,b),ss) e2 of
wneuper@59257
   642
	          NasNap (v, E) => 
wneuper@59257
   643
	            (case assy (y,x,s,sc,AssGen) ((E,(l @ [L, L, R]),SOME a,v,S,b),ss) e1 of
wneuper@59257
   644
	               NasNap (v, E) => 
wneuper@59257
   645
	                 assy (y,x,s,sc,AssGen) ((E, (l @ [L, R]), SOME a,v,S,b),ss) e2
wneuper@59257
   646
	             | ay => ay)
wneuper@59257
   647
	        | ay =>ay)
wneuper@59257
   648
     | NasApp _ => error ("assy: FIXXXME ///must not return NasApp///")
wneuper@59257
   649
     | ay => (ay))
wneuper@59257
   650
  | assy ya ((E,l,a,v,S,b),ss) (Const ("Script.Or",_) $e1 $ e2) =
wneuper@59257
   651
    (case assy ya ((E,(l @ [L, R]),a,v,S,b),ss) e1 of
wneuper@59257
   652
	     NasNap (v, E) => assy ya ((E,(l @ [R]),a,v,S,b),ss) e2
wneuper@59257
   653
     | ay => (ay))
wneuper@59257
   654
    (*here is not a tactical like TRY etc, but a tactic creating a step in calculation*)
wneuper@59257
   655
  | assy (thy',ctxt,sr,d,ap) ((E,l,a,v,S,_), (m,_,pt,(p,p_),c)::ss) t =
wneuper@59257
   656
    (case handle_leaf "locate" thy' sr E a v t of
wneuper@59257
   657
	     (a', Expr _) => 
wneuper@59257
   658
	        (NasNap (eval_listexpr_ (assoc_thy thy') sr
wneuper@59257
   659
		     (subst_atomic (upd_env_opt E (a',v)) t), E))
wneuper@59257
   660
     | (a', STac stac) =>
wneuper@59257
   661
	       let
wneuper@59257
   662
           val p' = 
wneuper@59257
   663
             case p_ of Frm => p 
wneuper@59257
   664
             | Res => lev_on p
wneuper@59257
   665
		         | _ => error ("assy: call by " ^ pos'2str (p,p_));
wneuper@59257
   666
	       in
wneuper@59257
   667
           case assod pt d m stac of
wneuper@59257
   668
	         Ass (m,v') =>
wneuper@59257
   669
	           let val (p'',c',f',pt') =
wneuper@59271
   670
                 Generate.generate1 (assoc_thy thy') m (ScrState (E,l,a',v',S,true), ctxt) (p',p_) pt;
wneuper@59257
   671
	           in Assoc ((E,l,a',v',S,true), (m,f',pt',p'',c @ c')::ss) end
wneuper@59257
   672
           | AssWeak (m,v') => 
wneuper@59257
   673
	           let val (p'',c',f',pt') =
wneuper@59271
   674
               Generate.generate1 (assoc_thy thy') m (ScrState (E,l,a',v',S,false), ctxt) (p',p_) pt;
wneuper@59257
   675
	           in Assoc ((E,l,a',v',S,false), (m,f',pt',p'',c @ c')::ss) end
wneuper@59257
   676
           | NotAss =>
wneuper@59257
   677
             (case ap of   (*switch for Or: 1st AssOnly, 2nd AssGen*)
wneuper@59257
   678
                AssOnly => (NasNap (v, E))
wneuper@59257
   679
              | _ =>
wneuper@59272
   680
                  (case Applicable.applicable_in (p,p_) pt (stac2tac pt (assoc_thy thy') stac) of
wneuper@59265
   681
		                 Chead.Appl m' =>
wneuper@59257
   682
		                   let
wneuper@59257
   683
                         val is = (E,l,a',tac_2res m',S,false(*FIXXXME.WN0?*))
wneuper@59257
   684
		                     val (p'',c',f',pt') =
wneuper@59271
   685
		                       Generate.generate1 (assoc_thy thy') m' (ScrState is, ctxt) (p', p_) pt;
wneuper@59257
   686
		                   in NasApp (is,(m,f',pt',p'',c @ c')::ss) end
wneuper@59265
   687
		               | Chead.Notappl _ => (NasNap (v, E))
wneuper@59257
   688
		              )
wneuper@59257
   689
		         )
wneuper@59257
   690
         end)
wneuper@59257
   691
  | assy _ (_, []) t = error ("assy: uncovered fun-def with " ^ term2str t);
neuper@37906
   692
wneuper@59257
   693
(*WN161112 blanks between list elements left as is until istate is introduced here*)
wneuper@59257
   694
fun ass_up (ys as (y,ctxt,s,Prog sc,d)) ((E,l,a,v,S,b),ss) (Const ("HOL.Let",_) $ _) =
wneuper@59257
   695
    let 
wneuper@59257
   696
	    val l = drop_last l; (*comes from e, goes to Abs*)
wneuper@59257
   697
      val (i, T, body) =
wneuper@59257
   698
        (case go l sc of
wneuper@59257
   699
           Const ("HOL.Let",_) $ _ $ (Abs (i, T, body)) => (i, T, body)
wneuper@59257
   700
         | t => error ("ass_up..HOL.Let $ _ with " ^ term2str t))
wneuper@59257
   701
      val i = mk_Free (i, T);
wneuper@59257
   702
      val E = upd_env E (i, v);
wneuper@59257
   703
    in case assy (y,ctxt,s,d,Aundef) ((E, l @ [R, D], a,v,S,b),ss) body of
wneuper@59257
   704
	       Assoc iss => Assoc iss
wneuper@59257
   705
	     | NasApp iss => astep_up ys iss 
wneuper@59257
   706
	     | NasNap (v, E) => astep_up ys ((E,l,a,v,S,b),ss) 
wneuper@59257
   707
	  end
wneuper@59257
   708
  | ass_up ys iss (Abs (_,_,_)) = astep_up ys iss (*TODO 5.9.00: env ?*)
wneuper@59257
   709
  | ass_up ys iss (Const ("HOL.Let",_) $ _ $ (Abs _)) = astep_up ys iss (*TODO 5.9.00: env ?*)
wneuper@59257
   710
  | ass_up ysa iss (Const ("Script.Seq",_) $ _ $ _ $ _) =
wneuper@59257
   711
    astep_up ysa iss (*all has been done in (*2*) below*)
wneuper@59257
   712
  | ass_up ysa iss (Const ("Script.Seq",_) $ _ $ _) =
wneuper@59257
   713
    astep_up ysa iss (*2*: comes from e2*)
neuper@37906
   714
wneuper@59257
   715
  | ass_up (ysa as (y,ctxt,s,Prog sc,d)) ((E,l,a,v,S,b),ss)
wneuper@59257
   716
	  (Const ("Script.Seq",_) $ _ ) = (*2*: comes from e1, goes to e2*)
wneuper@59257
   717
     let 
wneuper@59257
   718
       val up = drop_last l;
wneuper@59257
   719
     val e2 =
wneuper@59257
   720
       (case go up sc of
wneuper@59257
   721
          Const ("Script.Seq",_) $ _ $ e2 => e2
wneuper@59257
   722
        | t => error ("ass_up..Script.Seq $ _ with " ^ term2str t))
wneuper@59257
   723
     in case assy (y,ctxt,s,d,Aundef) ((E, up @ [R], a,v,S,b),ss) e2 of
wneuper@59257
   724
         NasNap (v,E) => astep_up ysa ((E,up,a,v,S,b),ss)
wneuper@59257
   725
       | NasApp iss => astep_up ysa iss
wneuper@59257
   726
       | ay => ay 
wneuper@59257
   727
     end
wneuper@59257
   728
  | ass_up ysa iss (Const ("Script.Try",_) $ _ $ _) = astep_up ysa iss
wneuper@59257
   729
  | ass_up ysa iss (Const ("Script.Try",_) $ _) = astep_up ysa iss
neuper@42282
   730
  | ass_up (ys as (y,ctxt,s,_,d)) ((E,l,_,v,S,b),ss)
neuper@37906
   731
	   (*(Const ("Script.While",_) $ c $ e $ a) = WN050930 blind fix*)
wneuper@59257
   732
	    (t as Const ("Script.While",_) $ c $ e $ a) =
wneuper@59257
   733
    if eval_true_ y s (subst_atomic (upd_env E (a,v)) c)
wneuper@59257
   734
    then case assy (y,ctxt,s,d,Aundef) ((E, l @ [L, R], SOME a,v,S,b),ss) e of 
wneuper@59257
   735
      NasNap (v,E') => astep_up ys ((E',l, SOME a,v,S,b),ss)
wneuper@59257
   736
    | NasApp ((E',l,a,v,S,b),ss) =>
wneuper@59257
   737
      ass_up ys ((E',l,a,v,S,b),ss) t (*WN050930 't' was not assigned*)
wneuper@59257
   738
    | ay => ay
neuper@37926
   739
    else astep_up ys ((E,l, SOME a,v,S,b),ss)
neuper@42282
   740
  | ass_up (ys as (y,ctxt,s,_,d)) ((E,l,a,v,S,b),ss)
neuper@37906
   741
	   (*(Const ("Script.While",_) $ c $ e) = WN050930 blind fix*)
wneuper@59257
   742
	     (t as Const ("Script.While",_) $ c $ e) =
neuper@37906
   743
    if eval_true_ y s (subst_atomic (upd_env_opt E (a,v)) c)
wneuper@59257
   744
    then case assy (y,ctxt,s,d,Aundef) ((E, l @ [R], a,v,S,b),ss) e of 
neuper@37906
   745
       NasNap (v,E') => astep_up ys ((E',l, a,v,S,b),ss)
neuper@37906
   746
     | NasApp ((E',l,a,v,S,b),ss) =>
neuper@37906
   747
       ass_up ys ((E',l,a,v,S,b),ss) t (*WN050930 't' was not assigned*)
wneuper@59257
   748
     | ay => ay
wneuper@59257
   749
    else astep_up ys ((E,l, a,v,S,b),ss)
wneuper@59257
   750
  | ass_up y iss (Const ("If",_) $ _ $ _ $ _) = astep_up y iss
wneuper@59257
   751
  | ass_up (ys as (y,ctxt,s,_,d)) ((E,l,_,v,S,b),ss)
wneuper@59257
   752
	    (t as Const ("Script.Repeat",_) $ e $ a) =
wneuper@59257
   753
    (case assy (y,ctxt,s,d, Aundef) ((E, (l @ [L, R]), SOME a,v,S,b),ss) e of 
wneuper@59257
   754
      NasNap (v,E') => astep_up ys ((E',l, SOME a,v,S,b),ss)
wneuper@59257
   755
    | NasApp ((E',l,a,v,S,b),ss) =>
wneuper@59257
   756
      ass_up ys ((E',l,a,v,S,b),ss) t
wneuper@59257
   757
    | ay => ay)
wneuper@59257
   758
  | ass_up (ys as (y,ctxt,s,_,d)) ((E,l,a,v,S,b),ss)
wneuper@59257
   759
	    (t as Const ("Script.Repeat",_) $ e) =
wneuper@59257
   760
    (case assy (y,ctxt,s,d,Aundef) ((E, (l @ [R]), a,v,S,b),ss) e of 
wneuper@59257
   761
       NasNap (v', E') => astep_up ys ((E',l,a,v',S,b),ss)
wneuper@59257
   762
     | NasApp ((E',l,a,v',S,_),ss) => ass_up ys ((E',l,a,v',S,b),ss) t
neuper@37906
   763
     | ay => ay)
neuper@37906
   764
  | ass_up y iss (Const ("Script.Or",_) $ _ $ _ $ _) = astep_up y iss
neuper@37906
   765
  | ass_up y iss (Const ("Script.Or",_) $ _ $ _) = astep_up y iss
neuper@37906
   766
  | ass_up y ((E,l,a,v,S,b),ss) (Const ("Script.Or",_) $ _ ) = 
neuper@37906
   767
    astep_up y ((E, (drop_last l), a,v,S,b),ss)
wneuper@59257
   768
  | ass_up _ _ t =
wneuper@59257
   769
    error ("ass_up not impl for t= " ^ term2str t)
neuper@48763
   770
and astep_up (ys as (_,_,_,Prog sc,_)) ((E,l,a,v,S,b),ss) =
neuper@37906
   771
  if 1 < length l
wneuper@59257
   772
  then 
wneuper@59257
   773
    let val up = drop_last l;
wneuper@59257
   774
    in ass_up ys ((E,up,a,v,S,b),ss) (go up sc) end
neuper@37906
   775
  else (NasNap (v, E))
wneuper@59257
   776
  | astep_up _ ((_,l,_,_,_,_),_) = error ("astep_up: uncovered fun-def with " ^ loc_2str l)
neuper@37906
   777
neuper@37906
   778
(*check if there are tacs for rewriting only*)
neuper@37906
   779
fun rew_only ([]:step list) = true
neuper@37906
   780
  | rew_only (((Rewrite' _          ,_,_,_,_))::ss) = rew_only ss
neuper@37906
   781
  | rew_only (((Rewrite_Inst' _     ,_,_,_,_))::ss) = rew_only ss
neuper@37906
   782
  | rew_only (((Rewrite_Set' _      ,_,_,_,_))::ss) = rew_only ss
neuper@37906
   783
  | rew_only (((Rewrite_Set_Inst' _ ,_,_,_,_))::ss) = rew_only ss
neuper@37906
   784
  | rew_only (((Calculate' _        ,_,_,_,_))::ss) = rew_only ss
neuper@37906
   785
  | rew_only (((Begin_Trans' _      ,_,_,_,_))::ss) = rew_only ss
neuper@37906
   786
  | rew_only (((End_Trans' _        ,_,_,_,_))::ss) = rew_only ss
neuper@37906
   787
  | rew_only _ = false; 
neuper@37906
   788
neuper@37906
   789
datatype locate =
wneuper@59257
   790
  Steps of istate (* producing hd of step list (which was latest)
wneuper@59257
   791
	                   for next_tac, for reporting Safe|Unsafe to DG  *)
wneuper@59257
   792
	   * step       (* (scrstate producing this step is in ptree !)   *) 
wneuper@59257
   793
		 list         (* locate_gen may produce intermediate steps      *)
wneuper@59257
   794
| NotLocatable;   (* no (m Ass m') or (m AssWeak m') found          *)
neuper@37906
   795
neuper@37906
   796
(* locate_gen tries to locate an input tac m in the script. 
neuper@37906
   797
   pursuing this goal the script is executed until an (m' equiv m) is found,
neuper@37906
   798
   or the end of the script
neuper@37906
   799
args
neuper@37906
   800
   m   : input by the user, already checked by applicable_in,
neuper@37906
   801
         (to be searched within Or; and _not_ an m doing the step on ptree !)
neuper@37906
   802
   p,pt: (incl ets) at the time of input
neuper@37906
   803
   scr : the script
neuper@37906
   804
   d   : canonical simplifier for locating Take, Substitute, Subproblems etc.
neuper@37906
   805
   ets : ets at the time of input
neuper@37906
   806
   l   : the location (in scr) of the stac which generated the current formula
neuper@37906
   807
returns
neuper@37906
   808
   Steps: pt,p (incl. ets) with m done
neuper@37906
   809
          pos' list of proofobjs cut (from generate)
neuper@37906
   810
          safe: implied from last proofobj
neuper@37906
   811
	  ets:
neuper@37906
   812
   ///ToDo : ets contains a list of tacs to be done before m can be done
neuper@37906
   813
          NOT IMPL. -- "error: do other step before"
neuper@37906
   814
   NotLocatable: thus generate_hard
neuper@37906
   815
*)
wneuper@59257
   816
(*WN161112 blanks between list elements left as is until istate is introduced here*)
wneuper@59257
   817
fun locate_gen (thy', _) (Rewrite' (_, ro, er, pa, thm, f, _)) (pt, p) 
wneuper@59257
   818
	    (Rfuns {locate_rule=lo,...}, _) (RrlsState (_,f'',rss,rts), _) = 
wneuper@59257
   819
    (case lo rss f (Thm thm) of
wneuper@59257
   820
	    [] => NotLocatable
wneuper@59257
   821
    | rts' => Steps (rts2steps [] ((pt,p),(f,f'',rss,rts),(thy',ro,er,pa)) rts'))
neuper@42282
   822
  | locate_gen (thy',srls) (m:tac_) ((pt,p):ptree * pos') 
wneuper@59257
   823
	    (scr as Prog (_ $ body),d) (ScrState (E,l,a,v,S,b), ctxt)  = 
wneuper@59257
   824
    let val thy = assoc_thy thy';
wneuper@59257
   825
    in case if l = [] orelse (
wneuper@59257
   826
		       (*init.in solve..Apply_Method...*)(last_elem o fst) p = 0 andalso snd p = Res)
wneuper@59271
   827
	       then (assy (thy',ctxt,srls,d,Aundef) ((E,[R],a,v,S,b), [(m,Generate.EmptyMout,pt,p,[])]) body)
wneuper@59271
   828
	       else (astep_up (thy',ctxt,srls,scr,d) ((E,l,a,v,S,b), [(m,Generate.EmptyMout,pt,p,[])]) ) of
wneuper@59257
   829
	    Assoc ((is as (_,_,_,_,_,strong_ass), ss as (_ :: _))) =>
wneuper@59257
   830
	      (if strong_ass
wneuper@59257
   831
         then (Steps (ScrState is, ss))
wneuper@59257
   832
	       else
wneuper@59257
   833
           if rew_only ss (*andalso 'not strong_ass'= associated weakly*)
wneuper@59257
   834
	         then
wneuper@59257
   835
             let
wneuper@59257
   836
               val (po,p_) = p
wneuper@59257
   837
               val po' = case p_ of Frm => po | Res => lev_on po | _ => error ("locate_gen " ^ pos_2str p_)
wneuper@59271
   838
               val (p'',c'',f'',pt'') = Generate.generate1 thy m (ScrState is, ctxt) (po',p_) pt
wneuper@59257
   839
	           in Steps (ScrState is, [(m, f'',pt'',p'',c'')]) end
wneuper@59257
   840
	         else Steps (ScrState is, ss))
wneuper@59257
   841
	  
wneuper@59257
   842
    | NasApp _ => NotLocatable
wneuper@59257
   843
    | err => error ("not-found-in-script: NotLocatable from " ^ PolyML.makestring err)
wneuper@59257
   844
    end
bonzai@41951
   845
  | locate_gen _ m _ (sc,_) (is, _) = 
wneuper@59257
   846
    error ("locate_gen: wrong arguments,\n tac= " ^ tac_2str m ^ ",\n " ^
wneuper@59257
   847
      "scr= " ^ scr2str sc ^ ",\n istate= " ^ istate2str is);
neuper@37906
   848
neuper@37906
   849
(** find the next stactic in a script **)
neuper@37906
   850
neuper@37906
   851
(*appy, nxt_up, nstep_up scanning for next_tac.
neuper@37906
   852
  search is clearly separated into (1)-(2):
neuper@37906
   853
  (1) appy is recursive descent;
neuper@37906
   854
  (2) nxt_up resumes interpretation at a location somewhere in the script;
neuper@37906
   855
      nstep_up does only get to the parentnode of the scriptexpr.
neuper@37906
   856
  consequence:
neuper@37906
   857
  * call of (2) means _always_ that in this branch below
neuper@37906
   858
    there was an applicable stac (Repeat, Or e1, ...)
neuper@37906
   859
*)
wneuper@59257
   860
datatype appy =  (* ExprVal in the sense of denotational semantics  *)
wneuper@59257
   861
    Appy of      (* applicable stac found, search stalled           *)
wneuper@59257
   862
    tac_ *       (* tac_ associated (fun assod) with stac           *)
wneuper@59257
   863
    scrstate     (* after determination of stac WN.18.8.03          *)
wneuper@59257
   864
  | Napp of      (* stac found was not applicable; 
wneuper@59257
   865
	                  this mode may become Skip in Repeat, Try and Or *)
wneuper@59257
   866
    env (*stack*)(* popped while nxt_up                             *)
wneuper@59257
   867
  | Skip of      (* for restart after Appy, for leaving iterations,
wneuper@59257
   868
	                  for passing the value of scriptexpressions,
wneuper@59257
   869
		                and for finishing the script successfully       *)
wneuper@59257
   870
    term * env (*stack*);
neuper@37906
   871
wneuper@59257
   872
datatype appy_ = (* as argument in nxt_up, nstep_up, from appy               *)
wneuper@59257
   873
(*Appy              is only (final) returnvalue, not argument during search  *)
wneuper@59257
   874
  Napp_          (* ev. detects 'script is not appropriate for this example' *)
wneuper@59257
   875
| Skip_;         (* detects 'script successfully finished'
wneuper@59257
   876
		                also used as init-value for resuming; this works,
wneuper@59257
   877
	                  because 'nxt_up Or e1' treats as Appy                    *)
neuper@37906
   878
wneuper@59257
   879
fun appy thy ptp E l (Const ("HOL.Let",_) $ e $ (Abs (i,T,b))) a v =
wneuper@59257
   880
    (case appy thy ptp E (l @ [L, R]) e a v of
wneuper@59257
   881
      Skip (res, E) => 
wneuper@59257
   882
        let val E' = upd_env E (Free (i,T), res);
wneuper@59257
   883
        in appy thy ptp E' (l @ [R, D]) b a v end
wneuper@59257
   884
    | ay => ay)
wneuper@59257
   885
  | appy (thy as (th,sr)) ptp E l (Const ("Script.While"(*1*),_) $ c $ e $ a) _ v =
wneuper@59257
   886
    (if eval_true_ th sr (subst_atomic (upd_env E (a,v)) c)
wneuper@59257
   887
     then appy thy ptp E (l @ [L, R]) e (SOME a) v
wneuper@59257
   888
     else Skip (v, E))
wneuper@59257
   889
  | appy (thy as (th,sr)) ptp E l (Const ("Script.While"(*2*),_) $ c $ e) a v =
wneuper@59257
   890
    (if eval_true_ th sr (subst_atomic (upd_env_opt E (a,v)) c)
wneuper@59257
   891
     then appy thy ptp E (l @ [R]) e a v
wneuper@59257
   892
     else Skip (v, E))
wneuper@59257
   893
  | appy (thy as (th,sr)) ptp E l (Const ("If",_) $ c $ e1 $ e2) a v =
wneuper@59257
   894
    (if eval_true_ th sr (subst_atomic (upd_env_opt E (a,v)) c)
wneuper@59257
   895
     then appy thy ptp E (l @ [L, R]) e1 a v
wneuper@59257
   896
     else appy thy ptp E (l @ [R]) e2 a v)
neuper@42007
   897
  | appy thy ptp E l (Const ("Script.Repeat"(*1*),_) $ e $ a) _ v = 
wneuper@59257
   898
    appy thy ptp E (l @ [L, R]) e (SOME a) v
wneuper@59257
   899
  | appy thy ptp E l (Const ("Script.Repeat"(*2*),_) $ e) a v = appy thy ptp E (l @ [R]) e a v
wneuper@59257
   900
  | appy thy ptp E l (Const ("Script.Try",_) $ e $ a) _ v =
wneuper@59257
   901
    (case appy thy ptp E (l @ [L, R]) e (SOME a) v of
wneuper@59257
   902
      Napp E => (Skip (v, E))
wneuper@59257
   903
    | ay => ay)
wneuper@59257
   904
  | appy thy ptp E l(Const ("Script.Try",_) $ e) a v =
wneuper@59257
   905
    (case appy thy ptp E (l @ [R]) e a v of
wneuper@59257
   906
      Napp E => (Skip (v, E))
wneuper@59257
   907
    | ay => ay)
neuper@42007
   908
  | appy thy ptp E l (Const ("Script.Or"(*1*),_) $e1 $ e2 $ a) _ v =
wneuper@59257
   909
    (case appy thy ptp E (l @ [L, L, R]) e1 (SOME a) v of
wneuper@59257
   910
	    Appy lme => Appy lme
wneuper@59257
   911
    | _ => appy thy ptp E (*env*) (l @ [L, R]) e2 (SOME a) v)
neuper@42007
   912
  | appy thy ptp E l (Const ("Script.Or"(*2*),_) $e1 $ e2) a v =
wneuper@59257
   913
    (case appy thy ptp E (l @ [L, R]) e1 a v of
wneuper@59257
   914
	    Appy lme => Appy lme
wneuper@59257
   915
    | _ => appy thy ptp E (l @ [R]) e2 a v)
neuper@42007
   916
  | appy thy ptp E l (Const ("Script.Seq"(*1*),_) $ e1 $ e2 $ a) _ v =
wneuper@59257
   917
    (case appy thy ptp E (l @ [L, L, R]) e1 (SOME a) v of
wneuper@59257
   918
	    Skip (v,E) => appy thy ptp E (l @ [L, R]) e2 (SOME a) v
wneuper@59257
   919
    | ay => ay)
neuper@42007
   920
  | appy thy ptp E l (Const ("Script.Seq",_) $ e1 $ e2) a v =
wneuper@59257
   921
    (case appy thy ptp E (l @ [L,R]) e1 a v of
wneuper@59257
   922
	    Skip (v,E) => appy thy ptp E (l @ [R]) e2 a v
wneuper@59257
   923
    | ay => ay)
neuper@42007
   924
  (* a leaf has been found *)   
wneuper@59257
   925
  | appy ((th,sr)) (pt, p) E l t a v =
wneuper@59257
   926
    case handle_leaf "next  " th sr E a v t of
wneuper@59257
   927
	    (_, Expr s) => Skip (s, E)
wneuper@59257
   928
    | (a', STac stac) =>
wneuper@59257
   929
	    let val (m,m') = stac2tac_ pt (assoc_thy th) stac
wneuper@59257
   930
      in case m of 
wneuper@59257
   931
	      Subproblem _ => Appy (m', (E,l,a',tac_2res m',Sundef,false))
wneuper@59257
   932
	    | _ =>
wneuper@59272
   933
        (case Applicable.applicable_in p pt m of
wneuper@59265
   934
		       Chead.Appl m' => (Appy (m', (E,l,a',tac_2res m',Sundef,false)))
wneuper@59257
   935
		     | _ => Napp E)
wneuper@59257
   936
	    end
wneuper@59257
   937
(*GOON*)
wneuper@59257
   938
fun nxt_up thy ptp (scr as (Prog sc)) E l ay (Const ("HOL.Let", _) $ _) a v = (*comes from let=...*)
neuper@37906
   939
    if ay = Napp_
neuper@37906
   940
    then nstep_up thy ptp scr E (drop_last l) Napp_ a v
neuper@37906
   941
    else (*Skip_*)
wneuper@59257
   942
	    let
wneuper@59257
   943
        val up = drop_last l
wneuper@59257
   944
        val (i, T, body) =
wneuper@59257
   945
          (case go up sc of
wneuper@59257
   946
             Const ("HOL.Let",_) $ _ $ (Abs aa) => aa
wneuper@59257
   947
           | t => error ("nxt_up..HOL.Let $ _ with " ^ term2str t))
wneuper@59257
   948
        val i = mk_Free (i, T)
wneuper@59257
   949
        val E = upd_env E (i, v)
wneuper@59257
   950
      in
wneuper@59257
   951
        case appy thy ptp E (up @ [R,D]) body a v  of
wneuper@59257
   952
	        Appy lre => Appy lre
wneuper@59257
   953
	      | Napp E => nstep_up thy ptp scr E up Napp_ a v
wneuper@59257
   954
	      | Skip (v,E) => nstep_up thy ptp scr E up Skip_ a v
wneuper@59257
   955
	    end
wneuper@59257
   956
  | nxt_up thy ptp scr E l ay (Abs _) a v =  nstep_up thy ptp scr E l ay a v
wneuper@59257
   957
  | nxt_up thy ptp scr E l ay (Const ("HOL.Let",_) $ _ $ (Abs _)) a v =
wneuper@59257
   958
    nstep_up thy ptp scr E l ay a v
wneuper@59257
   959
  (*no appy_: never causes Napp -> Helpless*)
wneuper@59257
   960
  | nxt_up (thy as (th, sr)) ptp scr E l _ (Const ("Script.While"(*1*), _) $ c $ e $ _) a v = 
wneuper@59257
   961
    if eval_true_ th sr (subst_atomic (upd_env_opt E (a, v)) c) 
wneuper@59257
   962
    then case appy thy ptp E (l @ [L,R]) e a v of
wneuper@59257
   963
	     Appy lr => Appy lr
wneuper@59257
   964
	  | Napp E => nstep_up thy ptp scr E l Skip_ a v
wneuper@59257
   965
	  | Skip (v,E) => nstep_up thy ptp scr E l Skip_ a v
wneuper@59257
   966
    else nstep_up thy ptp scr E l Skip_ a v
wneuper@59257
   967
  (*no appy_: never causes Napp - Helpless*)
wneuper@59257
   968
  | nxt_up (thy as (th, sr)) ptp scr E l _ (Const ("Script.While"(*2*), _) $ c $ e) a v = 
wneuper@59257
   969
    if eval_true_ th sr (subst_atomic (upd_env_opt E (a, v)) c) 
wneuper@59257
   970
    then case appy thy ptp E (l @ [R]) e a v of
neuper@37906
   971
	    Appy lr => Appy lr
wneuper@59257
   972
	  | Napp E => nstep_up thy ptp scr E l Skip_ a v
wneuper@59257
   973
	  | Skip (v,E) => nstep_up thy ptp scr E l Skip_ a v
wneuper@59257
   974
    else nstep_up thy ptp scr E l Skip_ a v
wneuper@59257
   975
  | nxt_up thy ptp scr E l ay (Const ("If", _) $ _ $ _ $ _) a v = nstep_up thy ptp scr E l ay a v
wneuper@59257
   976
  | nxt_up thy ptp scr E l _ (*no appy_: there was already a stac below*)
wneuper@59257
   977
      (Const ("Script.Repeat"(*1*), _) $ e $ _) a v =
wneuper@59257
   978
    (case appy thy ptp (*upd_env*) E (*a,v)*) ((l @ [L, R]):loc_) e a v  of
wneuper@59257
   979
      Appy lr => Appy lr
wneuper@59257
   980
    | Napp E =>  nstep_up thy ptp scr E l Skip_ a v
wneuper@59257
   981
    | Skip (v,E) =>  nstep_up thy ptp scr E l Skip_ a v)
wneuper@59257
   982
  | nxt_up thy ptp scr E l _ (*no appy_: there was already a stac below*)
wneuper@59257
   983
      (Const ("Script.Repeat"(*2*), _) $ e) a v =
wneuper@59257
   984
    (case appy thy ptp (*upd_env*) E (*a,v)*) (l @ [R]) e a v  of
wneuper@59257
   985
      Appy lr => Appy lr
wneuper@59257
   986
    | Napp E => nstep_up thy ptp scr E l Skip_ a v
wneuper@59257
   987
    | Skip (v,E) => nstep_up thy ptp scr E l Skip_ a v)
wneuper@59257
   988
  | nxt_up thy ptp scr E l _ (Const ("Script.Try",_) $ _ $ _) a v = (*makes Napp to Skip*)
wneuper@59257
   989
    nstep_up thy ptp scr E l Skip_ a v
neuper@37906
   990
wneuper@59257
   991
  | nxt_up thy ptp scr E l _ (Const ("Script.Try"(*2*), _) $ _) a v = (*makes Napp to Skip*)
wneuper@59257
   992
    nstep_up thy ptp scr E l Skip_ a v
wneuper@59257
   993
  | nxt_up thy ptp scr E l ay (Const ("Script.Or",_) $ _ $ _ $ _) a v =
wneuper@59257
   994
    nstep_up thy ptp scr E l ay a v
wneuper@59257
   995
  | nxt_up thy ptp scr E l ay (Const ("Script.Or",_) $ _ $ _) a v = nstep_up thy ptp scr E l ay a v
wneuper@59257
   996
  | nxt_up thy ptp scr E l ay (Const ("Script.Or",_) $ _ ) a v = 
wneuper@59257
   997
    nstep_up thy ptp scr E (drop_last l) ay a v
wneuper@59257
   998
  | nxt_up thy ptp scr E l ay (Const ("Script.Seq"(*1*),_) $ _ $ _ $ _) a v =
wneuper@59257
   999
    (*all has been done in (*2*) below*) nstep_up thy ptp scr E l ay a v
wneuper@59257
  1000
  | nxt_up thy ptp scr E l ay (Const ("Script.Seq"(*2*),_) $ _ $ _) a v = (*comes from e2*)
wneuper@59257
  1001
    nstep_up thy ptp scr E l ay a v
wneuper@59257
  1002
  | nxt_up thy ptp (scr as Prog sc) E l ay (Const ("Script.Seq",_) $ _) a v = (*comes from e1*)
wneuper@59257
  1003
    if ay = Napp_
wneuper@59257
  1004
    then nstep_up thy ptp scr E (drop_last l) Napp_ a v
wneuper@59257
  1005
    else (*Skip_*)
wneuper@59257
  1006
      let val up = drop_last l;
wneuper@59257
  1007
          val e2 =
wneuper@59257
  1008
            (case go up sc of
wneuper@59257
  1009
               Const ("Script.Seq"(*2*), _) $ _ $ e2 => e2
wneuper@59257
  1010
             | t => error ("nxt_up..Script.Seq $ _ with " ^ term2str t))
wneuper@59257
  1011
      in case appy thy ptp E (up @ [R]) e2 a v  of
wneuper@59257
  1012
        Appy lr => Appy lr
wneuper@59257
  1013
      | Napp E => nstep_up thy ptp scr E up Napp_ a v
wneuper@59257
  1014
      | Skip (v,E) => nstep_up thy ptp scr E up Skip_ a v end
wneuper@59257
  1015
  | nxt_up _ _ _ _ _ _ t _ _ = error ("nxt_up not impl for " ^ term2str t)
neuper@48763
  1016
and nstep_up thy ptp (Prog sc) E l ay a v = 
wneuper@59257
  1017
    if 1 < length l
wneuper@59257
  1018
    then 
wneuper@59257
  1019
      let val up = drop_last l; 
wneuper@59257
  1020
      in (nxt_up thy ptp (Prog sc) E up ay (go up sc) a v ) end
wneuper@59257
  1021
    else (*interpreted to end*)
wneuper@59257
  1022
      if ay = Skip_ then Skip (v, E) else Napp E 
wneuper@59257
  1023
  | nstep_up _ _ _ _ l _ _ _ = error ("nstep_up: uncovered fun-def at " ^ loc_2str l)
neuper@37906
  1024
neuper@37906
  1025
(* decide for the next applicable stac in the script;
neuper@37906
  1026
   returns (stactic, value) - the value in case the script is finished 
neuper@37906
  1027
   12.8.02:         ~~~~~ and no assumptions ??? FIXME ???
neuper@37906
  1028
   20.8.02: must return p in case of finished, because the next script
neuper@37906
  1029
            consulted need not be the calling script:
neuper@37906
  1030
            in case of detail ie. _inserted_ PrfObjs, the next stac
neuper@37906
  1031
            has to searched in a script with PblObj.status<>Complete !
neuper@37906
  1032
            (.. not true for other details ..PrfObj ??????????????????
neuper@37906
  1033
   20.8.02: do NOT return safe (is only changed in locate !!!)
neuper@37906
  1034
*)
wneuper@59257
  1035
fun next_tac (thy,_) _ (Rfuns {next_rule, ...}) (RrlsState(f, f', rss, _), ctxt) =
neuper@42394
  1036
    if f = f'
neuper@42394
  1037
    then (End_Detail' (f',[])(*8.6.03*), (Uistate, ctxt), 
neuper@42394
  1038
    	(f', Sundef(*FIXME is no value of next_tac! vor 8.6.03*)))  (*finished*)
neuper@42394
  1039
    else
neuper@42394
  1040
      (case next_rule rss f of
wneuper@59257
  1041
    	  NONE => (Empty_Tac_, (Uistate, ctxt), (e_term, Sundef))   (*helpless*)
wneuper@59257
  1042
    	| SOME (Thm thm'')(*8.6.03: muss auch f' liefern ?!!*) => 
wneuper@59257
  1043
    	    (Rewrite' (thy, "e_rew_ord", e_rls, false, thm'', f, (e_term, [(*!?!8.6.03*)])),
wneuper@59257
  1044
  	         (Uistate, ctxt), (e_term, Sundef)))                  (*next stac*)
wneuper@59257
  1045
  | next_tac thy (ptp as (pt, (p, _)):ptree * pos') (sc as Prog (_ $ body)) 
wneuper@59257
  1046
	    (ScrState (E,l,a,v,s,_), ctxt) =
neuper@42394
  1047
    (case if l = [] then appy thy ptp E [R] body NONE v
neuper@42394
  1048
          else nstep_up thy ptp sc E l Skip_ a v of
neuper@42394
  1049
       Skip (v, _) =>                                              (*finished*)
neuper@42394
  1050
         (case par_pbl_det pt p of
neuper@42394
  1051
	          (true, p', _) => 
neuper@42394
  1052
	             let
neuper@42394
  1053
	               val (_,pblID,_) = get_obj g_spec pt p';
neuper@42394
  1054
	              in (Check_Postcond' (pblID, (v, [(*assigned in next step*)])), 
neuper@42394
  1055
	                   (e_istate, ctxt), (v,s)) 
neuper@42394
  1056
                end
wneuper@59257
  1057
	        | _ => (End_Detail' (e_term,[])(*8.6.03*), (e_istate, ctxt), (v,s)))
neuper@42394
  1058
     | Napp _ => (Empty_Tac_, (e_istate, ctxt), (e_term, Sundef))   (*helpless*)
neuper@42394
  1059
     | Appy (m', scrst as (_,_,_,v,_,_)) => (m', (ScrState scrst, ctxt), (v, Sundef))) (*next stac*)
neuper@41972
  1060
  | next_tac _ _ _ (is, _) = error ("next_tac: not impl for " ^ (istate2str is));
neuper@37906
  1061
neuper@37906
  1062
(*.create the initial interpreter state from the items of the guard.*)
wneuper@59257
  1063
local
wneuper@59240
  1064
val errmsg = "ERROR: found no actual arguments for prog. of "
wneuper@59257
  1065
fun msg_miss (sc, metID, formals, actuals) =
wneuper@59257
  1066
  "ERROR in creating the environment for '" ^ id_of_scr sc ^ 
wneuper@59257
  1067
	"' from \nthe items of the guard of " ^ metID2str metID ^ ",\n" ^
wneuper@59257
  1068
	"formal arg(s), from the script, miss actual arg(s), from the guards env:\n" ^
wneuper@59257
  1069
	(string_of_int o length) formals ^ " formals: " ^ terms2str formals ^ "\n" ^
wneuper@59257
  1070
	(string_of_int o length) actuals ^ " actuals: " ^ terms2str actuals
wneuper@59257
  1071
fun msg_type (sc, metID, a, f, formals, actuals) =
wneuper@59257
  1072
  "ERROR in creating the environment for '" ^
wneuper@59257
  1073
	id_of_scr sc ^ "' from \nthe items of the guard of " ^
wneuper@59257
  1074
	metID2str metID ^ ",\n" ^
wneuper@59257
  1075
	"different types of formal arg, from the script, " ^
wneuper@59257
  1076
	"and actual arg, from the guards env:'\n" ^
wneuper@59257
  1077
	"formal: '" ^ term2str a ^ "::" ^ (type2str o type_of) a ^ "'\n" ^
wneuper@59257
  1078
	"actual: '" ^ term2str f ^ "::" ^ (type2str o type_of) f ^ "'\n" ^
wneuper@59257
  1079
	"in\n" ^
wneuper@59257
  1080
	"formals: " ^ terms2str formals ^ "\n" ^
wneuper@59257
  1081
	"actuals: " ^ terms2str actuals
wneuper@59257
  1082
in
neuper@42011
  1083
fun init_scrstate thy itms metID =
neuper@41996
  1084
  let
neuper@42011
  1085
    val actuals = itms2args thy metID itms
wneuper@59240
  1086
    val _ = if actuals <> [] then () else raise ERROR (errmsg ^ strs2str' metID)
wneuper@59269
  1087
    val (scr, sc) = (case (#scr o Specify.get_met) metID of
wneuper@59257
  1088
       scr as Prog sc => (scr, sc) | _ => raise ERROR ("init_scrstate with " ^ metID2str metID))
neuper@42011
  1089
    val formals = formal_args sc    
neuper@41996
  1090
	  (*expects same sequence of (actual) args in itms and (formal) args in met*)
neuper@41996
  1091
	  fun relate_args env [] [] = env
wneuper@59257
  1092
	    | relate_args _ _ [] = error (msg_miss (sc, metID, formals, actuals))
wneuper@59257
  1093
	    | relate_args env [] _ = env (*may drop Find!*)
neuper@41996
  1094
	    | relate_args env (a::aa) (f::ff) = 
wneuper@59257
  1095
	      if type_of a = type_of f 
wneuper@59257
  1096
	      then relate_args (env @ [(a, f)]) aa ff
wneuper@59257
  1097
        else error (msg_type (sc, metID, a, f, formals, actuals))
wneuper@59257
  1098
    val env = relate_args [] formals actuals;
wneuper@59257
  1099
    val ctxt = Proof_Context.init_global thy |> declare_constraints' actuals
wneuper@59269
  1100
    val {pre, prls, ...} = Specify.get_met metID;
wneuper@59257
  1101
    val pres = check_preconds thy prls pre itms |> map snd;
wneuper@59257
  1102
    val ctxt = ctxt |> insert_assumptions pres;
wneuper@59257
  1103
  in (ScrState (env, [], NONE, e_term, Safe, true), ctxt, scr) : istate * Proof.context * scr end;
wneuper@59257
  1104
end (*local*)
neuper@37906
  1105
neuper@41996
  1106
(* decide, where to get script/istate from:
wneuper@59257
  1107
   (* 1 *) from PblObj.env: at begin of script if no init_form
wneuper@59257
  1108
   (* 2 *) from PblObj/PrfObj: if stac is in the middle of the script
wneuper@59257
  1109
   (* 3 *) from rls/PrfObj: in case of detail a ruleset *)
wneuper@59257
  1110
fun from_pblobj_or_detail' _ (p, p_) pt =
wneuper@59257
  1111
  if member op = [Pbl, Met] p_
wneuper@59257
  1112
  then case get_obj g_env pt p of
wneuper@59257
  1113
    NONE => error "from_pblobj_or_detail': no istate"
wneuper@59257
  1114
  | SOME is =>
wneuper@59257
  1115
      let
wneuper@59257
  1116
        val metID = get_obj g_metID pt p
wneuper@59269
  1117
        val {srls, ...} = Specify.get_met metID
wneuper@59269
  1118
      in (srls, is, (#scr o Specify.get_met) metID) end
wneuper@59257
  1119
  else
wneuper@59257
  1120
    let val (pbl, p', rls') = par_pbl_det pt p
wneuper@59257
  1121
    in if pbl 
wneuper@59257
  1122
       then (*if last_elem p = 0 nothing written to pt yet*)                                (* 2 *)
wneuper@59257
  1123
         let
wneuper@59257
  1124
	         val metID = get_obj g_metID pt p'
wneuper@59269
  1125
	         val {srls,...} = Specify.get_met metID
wneuper@59269
  1126
	       in (srls, get_loc pt (p,p_), (#scr o Specify.get_met) metID) end
wneuper@59257
  1127
       else (*FIXME.WN0?: get from pbl or met !!! unused for Rrls in locate_gen, next_tac*) (* 3 *)
wneuper@59257
  1128
	       (e_rls, get_loc pt (p,p_),
wneuper@59257
  1129
	          case rls' of
wneuper@59257
  1130
		          Rls {scr = scr,...} => scr
wneuper@59257
  1131
	          | Seq {scr = scr,...} => scr
wneuper@59257
  1132
	          | Rrls {scr=rfuns,...} => rfuns
wneuper@59257
  1133
	          | Erls => error "from_pblobj_or_detail' with Erls")
wneuper@59257
  1134
    end
neuper@37906
  1135
wneuper@59257
  1136
(*.get script and istate from PblObj, see                                                  ( * 1 *)
bonzai@41948
  1137
fun from_pblobj' thy' (p,p_) pt =
neuper@42001
  1138
  let
neuper@42001
  1139
    val p' = par_pblobj pt p
neuper@42001
  1140
	  val thy = assoc_thy thy'
wneuper@59257
  1141
	  val itms =
wneuper@59257
  1142
	    (case get_obj I pt p' of
wneuper@59257
  1143
	      PblObj {meth = itms, ...} => itms
wneuper@59257
  1144
	    | PrfObj _ => error "from_pblobj' NOT with PrfObj")
neuper@42001
  1145
	  val metID = get_obj g_metID pt p'
wneuper@59269
  1146
	  val {srls, scr, ...} = Specify.get_met metID
neuper@42001
  1147
  in
neuper@42001
  1148
    if last_elem p = 0 (*nothing written to pt yet*)
neuper@42001
  1149
    then
neuper@42014
  1150
       let val (is, ctxt, scr) = init_scrstate thy itms metID
neuper@42001
  1151
	     in (srls, (is, ctxt), scr) end
wneuper@59257
  1152
    else (srls, get_loc pt (p,p_), scr)
wneuper@59257
  1153
  end;
neuper@37906
  1154
    
neuper@37906
  1155
(*.get the stactics and problems of a script as tacs
neuper@37906
  1156
  instantiated with the current environment;
neuper@37906
  1157
  l is the location which generated the given formula.*)
neuper@37906
  1158
(*WN.12.5.03: quick-and-dirty repair for listexpressions*)
neuper@37906
  1159
fun is_spec_pos Pbl = true
neuper@37906
  1160
  | is_spec_pos Met = true
neuper@37906
  1161
  | is_spec_pos _ = false;
neuper@37906
  1162
neuper@37906
  1163
(*. fetch _all_ tactics from script .*)
neuper@37906
  1164
fun sel_rules _ (([],Res):pos') = 
neuper@37906
  1165
    raise PTREE "no tactics applicable at the end of a calculation"
wneuper@59257
  1166
  | sel_rules pt (p,p_) =
wneuper@59257
  1167
    if is_spec_pos p_ 
wneuper@59257
  1168
    then [get_obj g_tac pt p]
wneuper@59257
  1169
    else
wneuper@59257
  1170
      let
wneuper@59257
  1171
        val pp = par_pblobj pt p;
wneuper@59257
  1172
        val thy' = (get_obj g_domID pt pp):theory';
wneuper@59257
  1173
        val thy = assoc_thy thy';
wneuper@59257
  1174
        val metID = get_obj g_metID pt pp;
wneuper@59257
  1175
        val metID' = if metID =e_metID then (thd3 o snd3) (get_obj g_origin pt pp) else metID
wneuper@59269
  1176
        val (sc, srls) = (case Specify.get_met metID' of
wneuper@59257
  1177
            {scr = Prog sc, srls, ...} => (sc, srls) | _ => error "sel_rules 1")
wneuper@59257
  1178
        val (env, a, v) = (case get_istate pt (p, p_) of
wneuper@59257
  1179
            ScrState (env, _, a, v, _, _) => (env, a, v) | _ => error "sel_rules 2")
wneuper@59257
  1180
      in map ((stac2tac pt thy) o rep_stacexpr o #2 o
wneuper@59257
  1181
  	    (handle_leaf "selrul" thy' srls env a v)) (stacpbls sc)
wneuper@59257
  1182
  	  end;
neuper@37906
  1183
neuper@37906
  1184
(*. fetch tactics from script and filter _applicable_ tactics;
neuper@37906
  1185
    in case of Rewrite_Set* go down to _atomic_ rewrite-tactics .*)
wneuper@59257
  1186
fun sel_appl_atomic_tacs _ (([], Res) : pos') = 
neuper@37906
  1187
    raise PTREE "no tactics applicable at the end of a calculation"
wneuper@59257
  1188
  | sel_appl_atomic_tacs pt (p, p_) =
neuper@37906
  1189
    if is_spec_pos p_ 
neuper@37906
  1190
    then [get_obj g_tac pt p]
neuper@37906
  1191
    else
neuper@42438
  1192
      let
neuper@42438
  1193
        val pp = par_pblobj pt p
neuper@42438
  1194
        val thy' = (get_obj g_domID pt pp):theory'
neuper@42438
  1195
        val thy = assoc_thy thy'
neuper@42438
  1196
        val metID = get_obj g_metID pt pp
neuper@42438
  1197
        val metID' =
neuper@42438
  1198
          if metID = e_metID 
neuper@42438
  1199
          then (thd3 o snd3) (get_obj g_origin pt pp)
neuper@42438
  1200
          else metID
wneuper@59269
  1201
        val (sc, srls, erls, ro) = (case Specify.get_met metID' of
wneuper@59257
  1202
            {scr = Prog sc, srls, erls, rew_ord' = ro, ...} => (sc, srls, erls, ro)
wneuper@59257
  1203
          | _ => error "sel_appl_atomic_tacs 1")
wneuper@59257
  1204
        val (env, a, v) = (case get_istate pt (p, p_) of
wneuper@59257
  1205
            ScrState (env, _, a, v, _, _) => (env, a, v) | _ => error "sel_appl_atomic_tacs 2")
neuper@42438
  1206
        val alltacs = (*we expect at least 1 stac in a script*)
neuper@42438
  1207
          map ((stac2tac pt thy) o rep_stacexpr o #2 o
neuper@42438
  1208
           (handle_leaf "selrul" thy' srls env a v)) (stacpbls sc)
neuper@42438
  1209
        val f =
wneuper@59257
  1210
          (case p_ of Frm => get_obj g_form pt p | Res => (fst o (get_obj g_result pt)) p
wneuper@59257
  1211
          | _ => error "")
neuper@42438
  1212
          (*WN071231 ? replace atomic_appl_tacs with applicable_in (ineff!) ?*)
wneuper@59263
  1213
      in ((gen_distinct eq_tac) o flat o (map (Rtools.atomic_appl_tacs thy ro erls f))) alltacs end;
wneuper@59257
  1214
(**)
neuper@37906
  1215
end
wneuper@59257
  1216
(**)