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