src/Tools/isac/Interpret/lucas-interpreter.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Mon, 24 Jun 2019 14:44:51 +0200
changeset 59555 125a54fa7be0
parent 59554 23bb6e7026c7
child 59556 bbc27b8c8ee7
permissions -rw-r--r--
lucin: shift "locate input formula" into lucas-interpreter.sml
wneuper@59554
     1
(* Title:  Interpret/lucase-interpreter.sml
wneuper@59554
     2
   Author: Walther Neuper 2019
wneuper@59554
     3
   (c) due to copyright terms
wneuper@59554
     4
*)
wneuper@59554
     5
wneuper@59554
     6
signature LUCAS_INTERPRETER =
wneuper@59554
     7
sig
wneuper@59554
     8
  val next_tac : (*diss: next-tactic-function*)
wneuper@59554
     9
    Rule.theory' * Rule.rls -> Ctree.state -> Rule.scr -> Selem.istate * 'a -> Tac.tac_ * (Selem.istate * 'a) * (term * Selem.safe)
wneuper@59555
    10
  val inform : Chead.calcstate' -> string -> string * Chead.calcstate'
wneuper@59555
    11
  val nxt_solve_ : Ctree.ctree * Ctree.pos' ->
wneuper@59555
    12
    (Tac.tac * Tac.tac_ * (Ctree.pos' * (Selem.istate * Proof.context))) list *
wneuper@59555
    13
      Ctree.pos' list * Ctree.state
wneuper@59554
    14
wneuper@59555
    15
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
wneuper@59555
    16
  val compare_step : Generate.taci list * Ctree.pos' list * (Ctree.state) -> term -> string * Chead.calcstate'
wneuper@59555
    17
wneuper@59555
    18
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
wneuper@59554
    19
end
wneuper@59554
    20
wneuper@59554
    21
structure LucinNEW(** ): LUCAS_INTERPRETER( **) = (*LucinNEW \<rightarrow> Lucin after code re-arrangement*)
wneuper@59554
    22
struct
wneuper@59554
    23
open Ctree
wneuper@59554
    24
wneuper@59554
    25
(*go at a location in a script and fetch the contents*)
wneuper@59554
    26
fun go [] t = t
wneuper@59554
    27
  | go (Celem.D :: p) (Abs(_, _, t0)) = go (p : Celem.loc_) t0
wneuper@59554
    28
  | go (Celem.L :: p) (t1 $ _) = go p t1
wneuper@59554
    29
  | go (Celem.R :: p) (_ $ t2) = go p t2
wneuper@59554
    30
  | go l _ = error ("go: no " ^ Celem.loc_2str l);
wneuper@59554
    31
wneuper@59554
    32
(** find the next stactic in a program **)
wneuper@59554
    33
wneuper@59554
    34
(* handle a leaf at the end of recursive descent:
wneuper@59554
    35
   a leaf is either a tactic or an 'expr' in "let v = expr"
wneuper@59554
    36
   where "expr" does not contain a tactic.
wneuper@59554
    37
   Handling a leaf comprises
wneuper@59554
    38
   (1) 'subst_stacexpr' substitute LTool.env and complete curried tactic
wneuper@59554
    39
   (2) rewrite the leaf by 'srls'
wneuper@59554
    40
*)
wneuper@59554
    41
fun handle_leaf call thy srls E a v t =
wneuper@59554
    42
      (*WN050916 'upd_env_opt' is a blind copy from previous version*)
wneuper@59554
    43
    case LTool.subst_stacexpr E a v t of
wneuper@59554
    44
	    (a', LTool.STac stac) => (*script-tactic*)
wneuper@59554
    45
	      let val stac' =
wneuper@59554
    46
            Rewrite.eval_listexpr_ (Celem.assoc_thy thy) srls (subst_atomic (Lucin.upd_env_opt E (a,v)) stac)
wneuper@59554
    47
	      in
wneuper@59554
    48
          (if (! trace_script) 
wneuper@59554
    49
	         then tracing ("@@@ "^call^" leaf '" ^ Rule.term2str t^"' ---> STac '" ^ Rule.term2str stac ^"'")
wneuper@59554
    50
	         else ();
wneuper@59554
    51
	         (a', LTool.STac stac'))
wneuper@59554
    52
	      end
wneuper@59554
    53
    | (a', LTool.Expr lexpr) => (*leaf-expression*)
wneuper@59554
    54
	      let val lexpr' =
wneuper@59554
    55
            Rewrite.eval_listexpr_ (Celem.assoc_thy thy) srls (subst_atomic (Lucin.upd_env_opt E (a,v)) lexpr)
wneuper@59554
    56
	      in
wneuper@59554
    57
          (if (! trace_script) 
wneuper@59554
    58
	         then tracing("@@@ "^call^" leaf '" ^ Rule.term2str t^"' ---> Expr '" ^ Rule.term2str lexpr'^"'")
wneuper@59554
    59
	         else ();
wneuper@59554
    60
	         (a', LTool.Expr lexpr')) (*lexpr' is the value of the Expr*)
wneuper@59554
    61
	      end;
wneuper@59554
    62
wneuper@59554
    63
(*appy, nxt_up, nstep_up scanning for next_tac.
wneuper@59554
    64
  search is clearly separated into (1)-(2):
wneuper@59554
    65
  (1) appy is recursive descent;
wneuper@59554
    66
  (2) nxt_up resumes interpretation at a location somewhere in the script;
wneuper@59554
    67
      nstep_up does only get to the parentnode of the scriptexpr.
wneuper@59554
    68
  consequence:
wneuper@59554
    69
  * call of (2) means _always_ that in this branch below
wneuper@59554
    70
    there was an applicable stac (Repeat, Or e1, ...)
wneuper@59554
    71
*)
wneuper@59554
    72
datatype appy =    (* ExprVal in the sense of denotational semantics  *)
wneuper@59554
    73
    Appy of        (* applicable stac found, search stalled           *)
wneuper@59554
    74
    Tac.tac_ *     (* tac_ associated (fun assod) with stac           *)
wneuper@59554
    75
    Selem.scrstate (* after determination of stac WN.18.8.03          *)
wneuper@59554
    76
  | Napp of        (* stac found was not applicable; 
wneuper@59554
    77
	                    this mode may become Skip in Repeat, Try and Or *)
wneuper@59554
    78
    LTool.env      (* popped while nxt_up                             *)
wneuper@59554
    79
  | Skip of        (* for restart after Appy, for leaving iterations,
wneuper@59554
    80
	                    for passing the value of scriptexpressions,
wneuper@59554
    81
		                  and for finishing the script successfully       *)
wneuper@59554
    82
    term * LTool.env  (*a stack*);
wneuper@59554
    83
wneuper@59554
    84
datatype appy_ = (* as argument in nxt_up, nstep_up, from appy               *)
wneuper@59554
    85
(*Appy              is only (final) returnvalue, not argument during search  *)
wneuper@59554
    86
  Napp_          (* ev. detects 'script is not appropriate for this example' *)
wneuper@59554
    87
| Skip_;         (* detects 'script successfully finished'
wneuper@59554
    88
		                also used as init-value for resuming; this works,
wneuper@59554
    89
	                  because 'nxt_up Or e1' treats as Appy                    *)
wneuper@59554
    90
wneuper@59554
    91
fun appy thy ptp E l (Const ("HOL.Let",_) $ e $ (Abs (i,T,b))) a v =
wneuper@59554
    92
    (case appy thy ptp E (l @ [Celem.L, Celem.R]) e a v of
wneuper@59554
    93
      Skip (res, E) => 
wneuper@59554
    94
        let val E' = LTool.upd_env E (Free (i,T), res);
wneuper@59554
    95
        in appy thy ptp E' (l @ [Celem.R, Celem.D]) b a v end
wneuper@59554
    96
    | ay => ay)
wneuper@59554
    97
  | appy (thy as (th,sr)) ptp E l (Const ("Script.While"(*1*),_) $ c $ e $ a) _ v =
wneuper@59554
    98
    (if Rewrite.eval_true_ th sr (subst_atomic (LTool.upd_env E (a,v)) c)
wneuper@59554
    99
     then appy thy ptp E (l @ [Celem.L, Celem.R]) e (SOME a) v
wneuper@59554
   100
     else Skip (v, E))
wneuper@59554
   101
  | appy (thy as (th,sr)) ptp E l (Const ("Script.While"(*2*),_) $ c $ e) a v =
wneuper@59554
   102
    (if Rewrite.eval_true_ th sr (subst_atomic (Lucin.upd_env_opt E (a,v)) c)
wneuper@59554
   103
     then appy thy ptp E (l @ [Celem.R]) e a v
wneuper@59554
   104
     else Skip (v, E))
wneuper@59554
   105
  | appy (thy as (th,sr)) ptp E l (Const ("If",_) $ c $ e1 $ e2) a v =
wneuper@59554
   106
    (if Rewrite.eval_true_ th sr (subst_atomic (Lucin.upd_env_opt E (a,v)) c)
wneuper@59554
   107
     then appy thy ptp E (l @ [Celem.L, Celem.R]) e1 a v
wneuper@59554
   108
     else appy thy ptp E (l @ [Celem.R]) e2 a v)
wneuper@59554
   109
  | appy thy ptp E l (Const ("Script.Repeat"(*1*),_) $ e $ a) _ v = 
wneuper@59554
   110
    appy thy ptp E (l @ [Celem.L, Celem.R]) e (SOME a) v
wneuper@59554
   111
  | appy thy ptp E l (Const ("Script.Repeat"(*2*),_) $ e) a v = appy thy ptp E (l @ [Celem.R]) e a v
wneuper@59554
   112
  | appy thy ptp E l (Const ("Script.Try"(*2*),_) $ e $ a) _ v =
wneuper@59554
   113
    (case appy thy ptp E (l @ [Celem.L, Celem.R]) e (SOME a) v of
wneuper@59554
   114
      Napp E => (Skip (v, E))
wneuper@59554
   115
    | ay => ay)
wneuper@59554
   116
  | appy thy ptp E l(Const ("Script.Try"(*1*),_) $ e) a v =
wneuper@59554
   117
    (case appy thy ptp E (l @ [Celem.R]) e a v of
wneuper@59554
   118
      Napp E => (Skip (v, E))
wneuper@59554
   119
    | ay => ay)
wneuper@59554
   120
  | appy thy ptp E l (Const ("Script.Or"(*1*),_) $e1 $ e2 $ a) _ v =
wneuper@59554
   121
    (case appy thy ptp E (l @ [Celem.L, Celem.L, Celem.R]) e1 (SOME a) v of
wneuper@59554
   122
	    Appy lme => Appy lme
wneuper@59554
   123
    | _ => appy thy ptp E (*LTool.env*) (l @ [Celem.L, Celem.R]) e2 (SOME a) v)
wneuper@59554
   124
  | appy thy ptp E l (Const ("Script.Or"(*2*),_) $e1 $ e2) a v =
wneuper@59554
   125
    (case appy thy ptp E (l @ [Celem.L, Celem.R]) e1 a v of
wneuper@59554
   126
	    Appy lme => Appy lme
wneuper@59554
   127
    | _ => appy thy ptp E (l @ [Celem.R]) e2 a v)
wneuper@59554
   128
  | appy thy ptp E l (Const ("Script.Seq"(*2*),_) $ e1 $ e2 $ a) _ v =
wneuper@59554
   129
    (case appy thy ptp E (l @ [Celem.L, Celem.L, Celem.R]) e1 (SOME a) v of
wneuper@59554
   130
	    Skip (v,E) => appy thy ptp E (l @ [Celem.L, Celem.R]) e2 (SOME a) v
wneuper@59554
   131
    | ay => ay)
wneuper@59554
   132
  | appy thy ptp E l (Const ("Script.Seq"(*1*),_) $ e1 $ e2) a v =
wneuper@59554
   133
    (case appy thy ptp E (l @ [Celem.L, Celem.R]) e1 a v of
wneuper@59554
   134
	    Skip (v,E) => appy thy ptp E (l @ [Celem.R]) e2 a v
wneuper@59554
   135
    | ay => ay)
wneuper@59554
   136
  (* a leaf has been found *)   
wneuper@59554
   137
  | appy ((th,sr)) (pt, p) E l t a v =
wneuper@59554
   138
    case handle_leaf "next  " th sr E a v t of
wneuper@59554
   139
	    (_, LTool.Expr s) => Skip (s, E)
wneuper@59554
   140
    | (a', LTool.STac stac) =>
wneuper@59554
   141
	    let val (m,m') = Lucin.stac2tac_ pt (Celem.assoc_thy th) stac
wneuper@59554
   142
      in case m of
wneuper@59554
   143
	      Tac.Subproblem _ => Appy (m', (E,l,a',Lucin.tac_2res m',Selem.Sundef,false))
wneuper@59554
   144
	    | _ =>
wneuper@59554
   145
        (case Applicable.applicable_in p pt m of
wneuper@59554
   146
		       Chead.Appl m' => (Appy (m', (E,l,a',Lucin.tac_2res m',Selem.Sundef,false)))
wneuper@59554
   147
		     | _ => Napp E)
wneuper@59554
   148
	    end
wneuper@59554
   149
wneuper@59554
   150
fun nxt_up thy ptp (scr as (Rule.Prog sc)) E l ay (Const ("HOL.Let", _) $ _) a v = (*comes from let=...*)
wneuper@59554
   151
    if ay = Napp_
wneuper@59554
   152
    then nstep_up thy ptp scr E (drop_last l) Napp_ a v
wneuper@59554
   153
    else (*Skip_*)
wneuper@59554
   154
	    let
wneuper@59554
   155
        val up = drop_last l
wneuper@59554
   156
        val (i, T, body) =
wneuper@59554
   157
          (case go up sc of
wneuper@59554
   158
             Const ("HOL.Let",_) $ _ $ (Abs aa) => aa
wneuper@59554
   159
           | t => error ("nxt_up..HOL.Let $ _ with " ^ Rule.term2str t))
wneuper@59554
   160
        val i = TermC.mk_Free (i, T)
wneuper@59554
   161
        val E = LTool.upd_env E (i, v)
wneuper@59554
   162
      in
wneuper@59554
   163
        case appy thy ptp E (up @ [Celem.R, Celem.D]) body a v  of
wneuper@59554
   164
	        Appy lre => Appy lre
wneuper@59554
   165
	      | Napp E => nstep_up thy ptp scr E up Napp_ a v
wneuper@59554
   166
	      | Skip (v,E) => nstep_up thy ptp scr E up Skip_ a v
wneuper@59554
   167
	    end
wneuper@59554
   168
  | nxt_up thy ptp scr E l ay (Abs _) a v =  nstep_up thy ptp scr E l ay a v
wneuper@59554
   169
  | nxt_up thy ptp scr E l ay (Const ("HOL.Let",_) $ _ $ (Abs _)) a v =
wneuper@59554
   170
    nstep_up thy ptp scr E l ay a v
wneuper@59554
   171
  (*no appy_: never causes Napp -> Helpless*)
wneuper@59554
   172
  | nxt_up (thy as (th, sr)) ptp scr E l _ (Const ("Script.While"(*1*), _) $ c $ e $ _) a v = 
wneuper@59554
   173
    if Rewrite.eval_true_ th sr (subst_atomic (Lucin.upd_env_opt E (a, v)) c) 
wneuper@59554
   174
    then case appy thy ptp E (l @ [Celem.L, Celem.R]) e a v of
wneuper@59554
   175
	     Appy lr => Appy lr
wneuper@59554
   176
	  | Napp E => nstep_up thy ptp scr E l Skip_ a v
wneuper@59554
   177
	  | Skip (v,E) => nstep_up thy ptp scr E l Skip_ a v
wneuper@59554
   178
    else nstep_up thy ptp scr E l Skip_ a v
wneuper@59554
   179
  (*no appy_: never causes Napp - Helpless*)
wneuper@59554
   180
  | nxt_up (thy as (th, sr)) ptp scr E l _ (Const ("Script.While"(*2*), _) $ c $ e) a v = 
wneuper@59554
   181
    if Rewrite.eval_true_ th sr (subst_atomic (Lucin.upd_env_opt E (a, v)) c) 
wneuper@59554
   182
    then case appy thy ptp E (l @ [Celem.R]) e a v of
wneuper@59554
   183
	    Appy lr => Appy lr
wneuper@59554
   184
	  | Napp E => nstep_up thy ptp scr E l Skip_ a v
wneuper@59554
   185
	  | Skip (v,E) => nstep_up thy ptp scr E l Skip_ a v
wneuper@59554
   186
    else nstep_up thy ptp scr E l Skip_ a v
wneuper@59554
   187
  | nxt_up thy ptp scr E l ay (Const ("If", _) $ _ $ _ $ _) a v = nstep_up thy ptp scr E l ay a v
wneuper@59554
   188
  | nxt_up thy ptp scr E l _ (*no appy_: there was already a stac below*)
wneuper@59554
   189
      (Const ("Script.Repeat"(*1*), _) $ e $ _) a v =
wneuper@59554
   190
    (case appy thy ptp (*upd_env*) E (*a,v)*) (l @ [Celem.L, Celem.R]) e a v  of
wneuper@59554
   191
      Appy lr => Appy lr
wneuper@59554
   192
    | Napp E =>  nstep_up thy ptp scr E l Skip_ a v
wneuper@59554
   193
    | Skip (v,E) =>  nstep_up thy ptp scr E l Skip_ a v)
wneuper@59554
   194
  | nxt_up thy ptp scr E l _ (*no appy_: there was already a stac below*)
wneuper@59554
   195
      (Const ("Script.Repeat"(*2*), _) $ e) a v =
wneuper@59554
   196
    (case appy thy ptp (*upd_env*) E (*a,v)*) (l @ [Celem.R]) e a v  of
wneuper@59554
   197
      Appy lr => Appy lr
wneuper@59554
   198
    | Napp E => nstep_up thy ptp scr E l Skip_ a v
wneuper@59554
   199
    | Skip (v,E) => nstep_up thy ptp scr E l Skip_ a v)
wneuper@59554
   200
  | nxt_up thy ptp scr E l _ (Const ("Script.Try"(*2*),_) $ _ $ _) a v = (*makes Napp to Skip*)
wneuper@59554
   201
    nstep_up thy ptp scr E l Skip_ a v
wneuper@59554
   202
wneuper@59554
   203
  | nxt_up thy ptp scr E l _ (Const ("Script.Try"(*1*), _) $ _) a v = (*makes Napp to Skip*)
wneuper@59554
   204
    nstep_up thy ptp scr E l Skip_ a v
wneuper@59554
   205
  | nxt_up thy ptp scr E l ay (Const ("Script.Or",_) $ _ $ _ $ _) a v =
wneuper@59554
   206
    nstep_up thy ptp scr E l ay a v
wneuper@59554
   207
  | nxt_up thy ptp scr E l ay (Const ("Script.Or",_) $ _ $ _) a v = nstep_up thy ptp scr E l ay a v
wneuper@59554
   208
  | nxt_up thy ptp scr E l ay (Const ("Script.Or",_) $ _ ) a v = 
wneuper@59554
   209
    nstep_up thy ptp scr E (drop_last l) ay a v
wneuper@59554
   210
  | nxt_up thy ptp scr E l ay (Const ("Script.Seq"(*1*),_) $ _ $ _ $ _) a v =
wneuper@59554
   211
    (*all has been done in (*2*) below*) nstep_up thy ptp scr E l ay a v
wneuper@59554
   212
  | nxt_up thy ptp scr E l ay (Const ("Script.Seq"(*2*),_) $ _ $ _) a v = (*comes from e2*)
wneuper@59554
   213
    nstep_up thy ptp scr E l ay a v
wneuper@59554
   214
  | nxt_up thy ptp (scr as Rule.Prog sc) E l ay (Const ("Script.Seq"(*3*),_) $ _) a v = (*comes from e1*)
wneuper@59554
   215
    if ay = Napp_
wneuper@59554
   216
    then nstep_up thy ptp scr E (drop_last l) Napp_ a v
wneuper@59554
   217
    else (*Skip_*)
wneuper@59554
   218
      let val up = drop_last l;
wneuper@59554
   219
          val e2 =
wneuper@59554
   220
            (case go up sc of
wneuper@59554
   221
               Const ("Script.Seq"(*2*), _) $ _ $ e2 => e2
wneuper@59554
   222
             | t => error ("nxt_up..Script.Seq $ _ with " ^ Rule.term2str t))
wneuper@59554
   223
      in case appy thy ptp E (up @ [Celem.R]) e2 a v  of
wneuper@59554
   224
        Appy lr => Appy lr
wneuper@59554
   225
      | Napp E => nstep_up thy ptp scr E up Napp_ a v
wneuper@59554
   226
      | Skip (v,E) => nstep_up thy ptp scr E up Skip_ a v end
wneuper@59554
   227
  | nxt_up _ _ _ _ _ _ t _ _ = error ("nxt_up not impl for " ^ Rule.term2str t)
wneuper@59554
   228
and nstep_up thy ptp (Rule.Prog sc) E l ay a v = 
wneuper@59554
   229
    if 1 < length l
wneuper@59554
   230
    then 
wneuper@59554
   231
      let val up = drop_last l; 
wneuper@59554
   232
      in (nxt_up thy ptp (Rule.Prog sc) E up ay (go up sc) a v ) end
wneuper@59554
   233
    else (*interpreted to end*)
wneuper@59554
   234
      if ay = Skip_ then Skip (v, E) else Napp E 
wneuper@59554
   235
  | nstep_up _ _ _ _ l _ _ _ = error ("nstep_up: uncovered fun-def at " ^ Celem.loc_2str l)
wneuper@59554
   236
wneuper@59554
   237
(* decide for the next applicable stac in the script;
wneuper@59554
   238
   returns (stactic, value) - the value in case the script is finished 
wneuper@59554
   239
   12.8.02:         ~~~~~ and no assumptions ??? FIXME ???
wneuper@59554
   240
   20.8.02: must return p in case of finished, because the next script
wneuper@59554
   241
            consulted need not be the calling script:
wneuper@59554
   242
            in case of detail ie. _inserted_ PrfObjs, the next stac
wneuper@59554
   243
            has to searched in a script with PblObj.status<>Complete !
wneuper@59554
   244
            (.. not true for other details ..PrfObj ??????????????????
wneuper@59554
   245
   20.8.02: do NOT return safe (is only changed in locate !!!)
wneuper@59554
   246
*)
wneuper@59554
   247
fun next_tac (thy,_) _ (Rule.Rfuns {next_rule, ...}) (Selem.RrlsState(f, f', rss, _), ctxt) =
wneuper@59554
   248
    if f = f'
wneuper@59554
   249
    then (Tac.End_Detail' (f',[])(*8.6.03*), (Selem.Uistate, ctxt), 
wneuper@59554
   250
    	(f', Selem.Sundef(*FIXME is no value of next_tac! vor 8.6.03*)))                (*finished*)
wneuper@59554
   251
    else
wneuper@59554
   252
      (case next_rule rss f of
wneuper@59554
   253
    	  NONE => (Tac.Empty_Tac_, (Selem.Uistate, ctxt), (Rule.e_term, Selem.Sundef))       (*helpless*)
wneuper@59554
   254
    	| SOME (Rule.Thm thm'')(*8.6.03: muss auch f' liefern ?!!*) => 
wneuper@59554
   255
    	    (Tac.Rewrite' (thy, "e_rew_ord", Rule.e_rls, false, thm'', f, (Rule.e_term, [(*!?!8.6.03*)])),
wneuper@59554
   256
  	         (Selem.Uistate, ctxt), (Rule.e_term, Selem.Sundef))                          (*next stac*)
wneuper@59554
   257
      | _ => error "next_tac: uncovered case next_rule")
wneuper@59554
   258
  | next_tac thy (ptp as (pt, (p, _))) (sc as Rule.Prog prog) 
wneuper@59554
   259
	    (Selem.ScrState (E,l,a,v,s,_), ctxt) =
wneuper@59554
   260
    (case if l = [] then appy thy ptp E [Celem.R] (LTool.body_of prog) NONE v
wneuper@59554
   261
          else nstep_up thy ptp sc E l Skip_ a v of
wneuper@59554
   262
       Skip (v, _) =>                                                                 (*finished*)
wneuper@59554
   263
         (case par_pbl_det pt p of
wneuper@59554
   264
	          (true, p', _) => 
wneuper@59554
   265
	             let
wneuper@59554
   266
	               val (_,pblID,_) = get_obj g_spec pt p';
wneuper@59554
   267
	              in (Tac.Check_Postcond' (pblID, (v, [(*assigned in next step*)])), 
wneuper@59554
   268
	                   (Selem.e_istate, ctxt), (v,s)) 
wneuper@59554
   269
                end
wneuper@59554
   270
	        | _ => (Tac.End_Detail' (Rule.e_term,[])(*8.6.03*), (Selem.e_istate, ctxt), (v,s)))
wneuper@59554
   271
     | Napp _ => (Tac.Empty_Tac_, (Selem.e_istate, ctxt), (Rule.e_term, Selem.Sundef))     (*helpless*)
wneuper@59554
   272
     | Appy (m', scrst as (_,_,_,v,_,_)) =>
wneuper@59554
   273
         (m', (Selem.ScrState scrst, ctxt), (v, Selem.Sundef)))                      (*next stac*)
wneuper@59554
   274
  | next_tac _ _ _ (is, _) = error ("next_tac: not impl for " ^ (Selem.istate2str is));
wneuper@59554
   275
wneuper@59555
   276
(* FIXME.WN050821 compare fun solve ... fun nxt_solv
wneuper@59555
   277
   nxt_solv (Apply_Method'     vvv FIXME: get args in applicable_in *)
wneuper@59555
   278
fun nxt_solv (Tac.Apply_Method' (mI, _, _, _)) _ (pt, pos as (p, _)) =
wneuper@59555
   279
   let
wneuper@59555
   280
     val {ppc, ...} = Specify.get_met mI;
wneuper@59555
   281
     val (itms, oris, probl) = case get_obj I pt p of
wneuper@59555
   282
       PblObj {meth = itms, origin = (oris, _, _), probl, ...} => (itms, oris, probl)
wneuper@59555
   283
     | _ => error "nxt_solv Apply_Method': uncovered case get_obj"
wneuper@59555
   284
     val itms = if itms <> [] then itms else Chead.complete_metitms oris probl [] ppc
wneuper@59555
   285
     val thy' = get_obj g_domID pt p;
wneuper@59555
   286
     val thy = Celem.assoc_thy thy';
wneuper@59555
   287
(*//----------- hack for funpack: generalise handling of meths which extend problem items -----\\*)
wneuper@59555
   288
val itms =
wneuper@59555
   289
  if mI = ["Biegelinien", "ausBelastung"]
wneuper@59555
   290
  then itms @
wneuper@59555
   291
    [(4, [1], true, "#Given", Model.Cor ((Const ("Biegelinie.Biegelinie", Type ("fun", [Type ("fun", [Type ("Real.real", []), Type ("Real.real", [])]), Type ("Tools.una", [])])),
wneuper@59555
   292
        [Free ("y", Type ("fun", [Type ("Real.real", []), Type ("Real.real", [])]))]),
wneuper@59555
   293
      (Free ("id_fun", Type ("fun", [Type ("Real.real", []), Type ("Real.real", [])])),
wneuper@59555
   294
        [Free ("y", Type ("fun", [Type ("Real.real", []), Type ("Real.real", [])]))] ))),
wneuper@59555
   295
    (5, [1], true, "#Given", Model.Cor ((Const ("Biegelinie.AbleitungBiegelinie", Type ("fun", [Type ("fun", [Type ("Real.real", []), Type ("Real.real", [])]), Type ("Tools.una", [])])),
wneuper@59555
   296
        [Free ("dy", Type ("fun", [Type ("Real.real", []), Type ("Real.real", [])]))]),
wneuper@59555
   297
      (Free ("id_abl", Type ("fun", [Type ("Real.real", []), Type ("Real.real", [])])),
wneuper@59555
   298
        [Free ("dy", Type ("fun", [Type ("Real.real", []), Type ("Real.real", [])]))] )))]
wneuper@59555
   299
  else itms
wneuper@59555
   300
(*\\----------- hack for funpack: generalise handling of meths which extend problem items -----//*)
wneuper@59555
   301
     val (is, env, ctxt, scr) = case Lucin.init_scrstate thy itms mI of
wneuper@59555
   302
       (is as Selem.ScrState (env,_,_,_,_,_), ctxt, scr) => (is, env, ctxt, scr)
wneuper@59555
   303
     | _ => error "nxt_solv Apply_Method': uncovered case init_scrstate"
wneuper@59555
   304
     val ini = Lucin.init_form thy scr env;
wneuper@59555
   305
   in 
wneuper@59555
   306
     case ini of
wneuper@59555
   307
       SOME t =>
wneuper@59555
   308
       let
wneuper@59555
   309
         val pos = ((lev_on o lev_dn) p, Frm)
wneuper@59555
   310
	       val tac_ = Tac.Apply_Method' (mI, SOME t, is, ctxt);
wneuper@59555
   311
	       val (pos, c, _, pt) = Generate.generate1 thy tac_ (is, ctxt) pos pt (* implicit Take *)
wneuper@59555
   312
       in
wneuper@59555
   313
        ([(Tac.Apply_Method mI, tac_, (pos, (is, ctxt)))], c, (pt, pos))
wneuper@59555
   314
       end
wneuper@59555
   315
     | NONE =>
wneuper@59555
   316
       let
wneuper@59555
   317
         val pt = update_env pt (fst pos) (SOME (is, ctxt))
wneuper@59555
   318
	       val (tacis, c, ptp) = nxt_solve_ (pt, pos)
wneuper@59555
   319
       in (tacis @ [(Tac.Apply_Method mI, Tac.Apply_Method' (mI, NONE, Selem.e_istate, ctxt), (pos, (is, ctxt)))],
wneuper@59555
   320
	       c, ptp)
wneuper@59555
   321
       end
wneuper@59555
   322
    end
wneuper@59555
   323
  | nxt_solv (Tac.Check_Postcond' (pI, _)) _ (pt, (p, p_))  =
wneuper@59555
   324
    let
wneuper@59555
   325
      val pp = par_pblobj pt p
wneuper@59555
   326
      val asm = (case get_obj g_tac pt p of
wneuper@59555
   327
		    Tac.Check_elementwise _ => (snd o (get_obj g_result pt)) p (*collects and instantiates asms*)
wneuper@59555
   328
		  | _ => get_assumptions_ pt (p, p_))
wneuper@59555
   329
	      handle _ => [] (*FIXME.WN030527 asms in subpbls not completely clear*)
wneuper@59555
   330
      val metID = get_obj g_metID pt pp;
wneuper@59555
   331
      val {srls = srls, scr = sc, ...} = Specify.get_met metID;
wneuper@59555
   332
      val (loc, E, l, a, b, ctxt) = case get_loc pt (p, p_) of
wneuper@59555
   333
        loc as (Selem.ScrState (E,l,a,_,_,b), ctxt) => (loc, E, l, a, b, ctxt)
wneuper@59555
   334
      | _ => error "nxt_solv Check_Postcond': uncovered case get_loc"
wneuper@59555
   335
      val thy' = get_obj g_domID pt pp;
wneuper@59555
   336
      val thy = Celem.assoc_thy thy';
wneuper@59555
   337
      val (_, _, (scval, scsaf)) = next_tac (thy', srls) (pt, (p, p_)) sc loc;
wneuper@59555
   338
    in
wneuper@59555
   339
      if pp = []
wneuper@59555
   340
      then 
wneuper@59555
   341
	      let
wneuper@59555
   342
          val is = Selem.ScrState (E, l, a, scval, scsaf, b)
wneuper@59555
   343
	        val tac_ = Tac.Check_Postcond'(pI,(scval, asm))
wneuper@59555
   344
	        val ((p, p_), ps, _, pt) = Generate.generate1 thy tac_ (is, ctxt) (pp, Res) pt;
wneuper@59555
   345
	      in ([(Tac.Check_Postcond pI, tac_, ((pp, Res), (is, ctxt)))], ps, (pt, (p, p_))) end
wneuper@59555
   346
      else
wneuper@59555
   347
        let (*resume script of parpbl, transfer value of subpbl-script*)
wneuper@59555
   348
          val ppp = par_pblobj pt (lev_up p);
wneuper@59555
   349
	        val thy' = get_obj g_domID pt ppp;
wneuper@59555
   350
          val thy = Celem.assoc_thy thy';
wneuper@59555
   351
          val (E, l, a, b, ctxt') = case get_loc pt (pp, Frm) of
wneuper@59555
   352
            (Selem.ScrState (E,l,a,_,_,b), ctxt') => (E, l, a, b, ctxt')
wneuper@59555
   353
          | _ => error "nxt_solv Check_Postcond' script of parpbl: uncovered case get_loc"
wneuper@59555
   354
	        val ctxt'' = Stool.from_subpbl_to_caller ctxt scval ctxt'
wneuper@59555
   355
          val tac_ = Tac.Check_Postcond' (pI, (scval, asm))
wneuper@59555
   356
	        val is = Selem.ScrState (E,l,a,scval,scsaf,b)
wneuper@59555
   357
          val ((p, p_), ps, _, pt) = Generate.generate1 thy tac_ (is, ctxt'') (pp, Res) pt;
wneuper@59555
   358
        in ([(Tac.Check_Postcond pI, tac_, ((pp, Res), (is, ctxt'')))], ps, (pt, (p, p_))) end
wneuper@59555
   359
    end
wneuper@59555
   360
  | nxt_solv (Tac.End_Proof'') _ ptp = ([], [], ptp)
wneuper@59555
   361
  | nxt_solv tac_ is (pt, pos) =
wneuper@59555
   362
    let
wneuper@59555
   363
      val pos = case pos of
wneuper@59555
   364
 		   (p, Met) => ((lev_on o lev_dn) p, Frm) (* begin script        *)
wneuper@59555
   365
 		  | (p, Res) => (lev_on p, Res)            (* somewhere in script *)
wneuper@59555
   366
 		  | _ => pos
wneuper@59555
   367
 	    val (pos', c, _, pt) = Generate.generate1 (Celem.assoc_thy "Isac") tac_ is pos pt;
wneuper@59555
   368
    in
wneuper@59555
   369
      ([(Lucin.tac_2tac tac_, tac_, (pos, is))], c, (pt, pos'))
wneuper@59555
   370
    end
wneuper@59555
   371
(* find the next tac from the script, nxt_solv will update the ctree *)
wneuper@59555
   372
and nxt_solve_ (ptp as (pt, pos as (p, p_))) =
wneuper@59555
   373
    if Celem.e_metID = get_obj g_metID pt (par_pblobj pt p)
wneuper@59555
   374
    then ([], [], (pt, (p, p_)))
wneuper@59555
   375
    else 
wneuper@59555
   376
      let 
wneuper@59555
   377
        val thy' = get_obj g_domID pt (par_pblobj pt p);
wneuper@59555
   378
	      val (srls, is, sc) = Lucin.from_pblobj_or_detail' thy' (p,p_) pt;
wneuper@59555
   379
	      val (tac_, is, (t, _)) = next_tac (thy', srls) (pt, pos) sc is;
wneuper@59555
   380
	      (* TODO here  ^^^  return finished/helpless/ok !*)
wneuper@59555
   381
	    in case tac_ of
wneuper@59555
   382
		    Tac.End_Detail' _ => ([(Tac.End_Detail, Tac.End_Detail' (t, [(*FIXME.04*)]), (pos, is))], [], (pt, pos))
wneuper@59555
   383
	    | _ => nxt_solv tac_ is ptp
wneuper@59555
   384
      end;
wneuper@59555
   385
 
wneuper@59555
   386
(* compare inform with ctree.form at current pos by nrls;
wneuper@59555
   387
   if found, embed the derivation generated during comparison
wneuper@59555
   388
   if not, let the mat-engine compute the next ctree.form *)
wneuper@59555
   389
(* code's structure is copied from complete_solve
wneuper@59555
   390
   CAUTION: tacis in returned calcstate' do NOT construct resulting ptp --
wneuper@59555
   391
            all_modspec etc. has to be inserted at Subproblem'*)
wneuper@59555
   392
fun compare_step ((tacis, c, ptp as (pt, pos as (p,p_))): Chead.calcstate') ifo =
wneuper@59555
   393
  let
wneuper@59555
   394
    val fo =
wneuper@59555
   395
      case p_ of
wneuper@59555
   396
        Ctree.Frm => Ctree.get_obj Ctree.g_form pt p
wneuper@59555
   397
			| Ctree.Res => (fst o (Ctree.get_obj Ctree.g_result pt)) p
wneuper@59555
   398
			| _ => Rule.e_term (*on PblObj is fo <> ifo*);
wneuper@59555
   399
	  val {nrls, ...} = Specify.get_met (Ctree.get_obj Ctree.g_metID pt (Ctree.par_pblobj pt p))
wneuper@59555
   400
	  val {rew_ord, erls, rules, ...} = Rule.rep_rls nrls
wneuper@59555
   401
	  val (found, der) = Inform.concat_deriv rew_ord erls rules fo ifo; (*<---------------*)
wneuper@59555
   402
  in
wneuper@59555
   403
    if found
wneuper@59555
   404
    then
wneuper@59555
   405
       let
wneuper@59555
   406
         val tacis' = map (Inform.mk_tacis rew_ord erls) der;
wneuper@59555
   407
		     val (c', ptp) = Generate.embed_deriv tacis' ptp;
wneuper@59555
   408
	     in ("ok", (tacis (*@ tacis'?WN050408*), c @ c', ptp)) end
wneuper@59555
   409
     else 
wneuper@59555
   410
	     if pos = ([], Ctree.Res) (*TODO: we should stop earlier with trying subproblems *)
wneuper@59555
   411
	     then ("no derivation found", (tacis, c, ptp): Chead.calcstate') 
wneuper@59555
   412
	     else
wneuper@59555
   413
         let
wneuper@59555
   414
           val cs' as (tacis, c', ptp) = nxt_solve_ ptp; (*<---------------------*)
wneuper@59555
   415
		       val (tacis, c'', ptp) = case tacis of
wneuper@59555
   416
			       ((Tac.Subproblem _, _, _)::_) => 
wneuper@59555
   417
			         let
wneuper@59555
   418
                 val ptp as (pt, (p,_)) = Chead.all_modspec ptp (*<--------------------*)
wneuper@59555
   419
				         val mI = Ctree.get_obj Ctree.g_metID pt p
wneuper@59555
   420
			         in
wneuper@59555
   421
			           nxt_solv (Tac.Apply_Method' (mI, NONE, Selem.e_istate, Selem.e_ctxt)) (Selem.e_istate, Selem.e_ctxt) ptp
wneuper@59555
   422
               end
wneuper@59555
   423
			     | _ => cs';
wneuper@59555
   424
		     in compare_step (tacis, c @ c' @ c'', ptp) ifo end
wneuper@59555
   425
  end
wneuper@59555
   426
wneuper@59554
   427
end