src/Tools/isac/Interpret/lucas-interpreter.sml
author Walther Neuper <walther.neuper@jku.at>
Mon, 04 Nov 2019 09:04:23 +0100
changeset 59684 62eeb8e5a978
parent 59683 931d651bfcbb
child 59685 08512202c2c6
permissions -rw-r--r--
[Test_Isac] lucin: reorganise args of appy, assy & Co

Note: All tests run except lucas-interpreter.sml, Minisubpbl/*
The latter will be updated after final cleanup of arguments of assy/appy & Co
wneuper@59562
     1
(* Title:  Interpret/lucas-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@59567
     8
  datatype input_tactic_result =
walther@59655
     9
      Safe_Step of Istate.T * Proof.context * Tactic.T
walther@59655
    10
    | Unsafe_Step of Istate.T * Proof.context * Tactic.T
wneuper@59569
    11
    | Not_Locatable of string
wneuper@59572
    12
  val locate_input_tactic : Rule.program -> Ctree.state -> Istate.T -> Proof.context -> Tactic.T
wneuper@59569
    13
    -> input_tactic_result
wneuper@59557
    14
wneuper@59564
    15
(*datatype input_formula_result = FStep of calcstate' | Not_Derivable *)
wneuper@59562
    16
  datatype input_formula_result =
wneuper@59572
    17
    Step of Ctree.state * Istate.T * Proof.context
wneuper@59562
    18
  | Not_Derivable of Chead.calcstate'
wneuper@59574
    19
(*val locate_input_formula : Ctree.state -> term -> input_formula_result *)
wneuper@59572
    20
  val locate_input_formula : Rule.program -> Ctree.state -> Istate.T -> Proof.context -> term
wneuper@59562
    21
    -> input_formula_result
wneuper@59564
    22
(*val begin_end_prog : ???*)
wneuper@59572
    23
  val begin_end_prog : Tactic.T -> Istate.T * Proof.context -> Ctree.state ->
wneuper@59572
    24
    (Tactic.input * Tactic.T * (Ctree.pos' * (Istate.T * Proof.context))) list * Ctree.pos' list * Ctree.state
wneuper@59574
    25
(*val do_solve_step : Ctree.state -> Ctree.state ???*)
wneuper@59574
    26
  val do_solve_step : Ctree.state ->
wneuper@59574
    27
    (Tactic.input * Tactic.T * (Ctree.pos' * (Istate.T * Proof.context))) list * Ctree.pos' list * Ctree.state
wneuper@59554
    28
wneuper@59569
    29
  datatype next_tactic_result =
wneuper@59572
    30
      NStep of Ctree.state * Istate.T * Proof.context * tactic
wneuper@59569
    31
    | Helpless | End_Program
wneuper@59569
    32
(*val determine_next_tactic :
wneuper@59572
    33
    Rule.program -> Ctree.state -> Istate.T -> Proof.context -> next_tactic_result *)
wneuper@59572
    34
  val determine_next_tactic : Rule.theory' * Rule.rls -> Ctree.state -> Rule.program -> Istate.T * 'a ->
walther@59675
    35
    Tactic.T * (Istate.T * 'a) * (term * Telem.safe)
wneuper@59569
    36
wneuper@59557
    37
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
wneuper@59567
    38
  datatype assoc =
walther@59667
    39
      Assoc of Istate.pstate * Proof.context * Tactic.T
walther@59667
    40
    | NasApp of Istate.pstate * Proof.context * Tactic.T
walther@59659
    41
    | NasNap of  term * Env.T;
wneuper@59557
    42
  val assoc2str : assoc -> string
walther@59683
    43
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
walther@59667
    44
  datatype appy = Appy of Tactic.T * Istate.pstate | Napp of Env.T | Skip of term * Env.T
wneuper@59555
    45
  val compare_step : Generate.taci list * Ctree.pos' list * (Ctree.state) -> term -> string * Chead.calcstate'
walther@59684
    46
(*old* )val appy: Rule.theory' * Rule.rls -> Ctree.state -> Istate.pstate -> term -> appy( *old*)
walther@59684
    47
(*new*)val appy: Ctree.state * Rule.theory'(*Proof.context*) -> Istate.pstate -> term -> appy(*new*)
walther@59684
    48
(*old* )val nxt_up: Rule.theory' * Rule.rls -> Ctree.state -> Rule.program -> Istate.pstate -> Istate.appy_ -> term -> appy( *old*)
walther@59684
    49
(*new*)val nxt_up: Rule.program * (Ctree.state * Rule.theory') -> Istate.pstate -> term -> appy(*new*)
walther@59684
    50
(*old* )val nstep_up: Rule.theory' * Rule.rls -> Ctree.state -> Rule.program -> Istate.pstate -> Istate.appy_ -> appy( *old*)
walther@59684
    51
(*new*)val nstep_up: Rule.program * (Ctree.state * Rule.theory') -> Istate.pstate -> appy(*new*)
wneuper@59557
    52
  val go : Celem.loc_ -> term -> term
walther@59668
    53
  val go_up: Celem.loc_ -> term -> term
wneuper@59572
    54
  val execute_progr_1 : Rule.theory' * Rule.rls -> Ctree.state -> Rule.program -> Istate.T -> appy
wneuper@59572
    55
  val execute_progr_2 : Rule.rls -> Tactic.T -> Ctree.state -> Rule.program * 'a -> Istate.T * Proof.context -> assoc
walther@59664
    56
  val check_Let_up : Celem.loc_ -> term -> term * term
walther@59664
    57
  val check_Seq_up: Celem.loc_ -> term -> term
walther@59684
    58
(*old* )val assy :  Proof.context * Rule.rls * Ctree.state * Istate.asap -> Istate.pstate * Tactic.T -> term -> assoc( *old*)
walther@59684
    59
(*new*)val assy: (Ctree.state * Proof.context * Tactic.T) -> Istate.pstate -> term -> assoc(*new*)
walther@59684
    60
(*old* )val ass_up : Proof.context * Rule.rls * Rule.program * Ctree.state -> Istate.pstate * Tactic.T -> term -> assoc( *old*)
walther@59684
    61
(*new*)val ass_up: Rule.program * (Ctree.state * Proof.context * Tactic.T) -> Istate.pstate -> term -> assoc;(*new*)
walther@59684
    62
(*old* )val astep_up : Proof.context * Rule.rls * Rule.program * Ctree.state -> Istate.pstate * Tactic.T -> assoc( *old*)
walther@59684
    63
(*new*)val astep_up: Rule.program * (Ctree.state * Proof.context * Tactic.T) -> Istate.pstate -> assoc;(*new*)
walther@59683
    64
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
wneuper@59554
    65
end
wneuper@59554
    66
walther@59684
    67
(**)
wneuper@59557
    68
structure LucinNEW(**): LUCAS_INTERPRETER(**) = (*LucinNEW \<rightarrow> Lucin after code re-arrangement*)
wneuper@59554
    69
struct
walther@59684
    70
(**)
wneuper@59554
    71
open Ctree
walther@59659
    72
open Celem
walther@59661
    73
open Istate
wneuper@59554
    74
wneuper@59567
    75
type program = term
wneuper@59567
    76
wneuper@59557
    77
(*go to a location in a script and fetch the contents*)
wneuper@59554
    78
fun go [] t = t
walther@59661
    79
  | go (D :: p) (Abs(_, _, t0)) = go (p : Celem.loc_) t0
walther@59661
    80
  | go (L :: p) (t1 $ _) = go p t1
walther@59661
    81
  | go (R :: p) (_ $ t2) = go p t2
wneuper@59554
    82
  | go l _ = error ("go: no " ^ Celem.loc_2str l);
walther@59668
    83
fun go_up l t =
walther@59668
    84
  if length l > 1 then go (drop_last l) t else raise ERROR ("go_up [] " ^ Rule.term2str t)
wneuper@59554
    85
walther@59664
    86
fun check_Let_up path prog =
walther@59668
    87
  case go (drop_last path) prog of
walther@59664
    88
    Const ("HOL.Let",_) $ _ $ (Abs (i, T, body)) => (TermC.mk_Free (i, T), body)
walther@59672
    89
  | t => raise ERROR ("ass_up..\"HOL.Let $ _\" with: \"" ^ Rule.term2str t ^ "\"")
walther@59664
    90
fun check_Seq_up path prog =
walther@59664
    91
  case go (drop_last path) prog of
walther@59664
    92
    Const ("Tactical.Seq",_) $ _ $ e2=> e2
walther@59672
    93
  | t => raise ERROR ("ass_up..\"Tactical.Seq $ _\" with: \"" ^ Rule.term2str t ^ "\"")
wneuper@59562
    94
wneuper@59562
    95
(**** find the next tactic by executing the program ****)
wneuper@59554
    96
wneuper@59572
    97
  datatype next_tactic_result = NStep of Ctree.state * Istate.T * Proof.context * tactic
wneuper@59567
    98
    | Helpless | End_Program
wneuper@59566
    99
wneuper@59559
   100
(*appy, nxt_up, nstep_up scanning for determine_next_tactic.
wneuper@59554
   101
  search is clearly separated into (1)-(2):
wneuper@59554
   102
  (1) appy is recursive descent;
wneuper@59554
   103
  (2) nxt_up resumes interpretation at a location somewhere in the script;
wneuper@59554
   104
      nstep_up does only get to the parentnode of the scriptexpr.
wneuper@59554
   105
  consequence:
wneuper@59554
   106
  * call of (2) means _always_ that in this branch below
wneuper@59554
   107
    there was an applicable stac (Repeat, Or e1, ...)
wneuper@59554
   108
*)
wneuper@59554
   109
datatype appy =    (* ExprVal in the sense of denotational semantics  *)
wneuper@59554
   110
    Appy of        (* applicable stac found, search stalled           *)
walther@59639
   111
    Tactic.T *     (* tac_ associated (fun associate) with stac       *)
walther@59667
   112
    Istate.pstate (* after determination of stac WN.180803          *)
wneuper@59554
   113
  | Napp of        (* stac found was not applicable; 
wneuper@59554
   114
	                    this mode may become Skip in Repeat, Try and Or *)
walther@59659
   115
    Env.T      (* popped while nxt_up                             *)
wneuper@59554
   116
  | Skip of        (* for restart after Appy, for leaving iterations,
wneuper@59554
   117
	                    for passing the value of scriptexpressions,
wneuper@59554
   118
		                  and for finishing the script successfully       *)
walther@59639
   119
    term *         (* ???*) 
walther@59659
   120
    Env.T      (* TODO: a stack of env for nested let?            *);
wneuper@59554
   121
walther@59684
   122
fun appy xxx ist (Const ("HOL.Let"(*1*), _) $ e $ (Abs (i, T, b))) =
walther@59684
   123
     (case appy xxx (ist |> path_down [L, R]) e of
walther@59684
   124
       Skip (res, E) => appy xxx (ist |> path_down [R, D] |> upd_env'' (E , (Free (i, T), res))) b
walther@59684
   125
     | rrr => rrr)
walther@59678
   126
walther@59684
   127
  | appy (xxx as (_, ctxt)) ist (Const ("Tactical.While"(*1*), _) $ c $ e $ a) =
walther@59684
   128
    if Rewrite.eval_true_ ctxt (get_rls ist) (subst_atomic (ist |> get_act_env |> Env.upd_env' a) c)
walther@59684
   129
    then appy xxx (ist |> path_down_form ([L, R], a)) e
walther@59678
   130
    else Skip (get_act_env ist)
walther@59684
   131
  | appy (xxx as (_, ctxt)) ist (Const ("Tactical.While"(*2*), _) $ c $ e) =
walther@59684
   132
    if Rewrite.eval_true_ ctxt (get_rls ist) (subst_atomic (Env.upd_env_opt' (get_subst ist)) c)
walther@59684
   133
    then appy xxx (ist |> path_down [R]) e
walther@59671
   134
    else Skip (get_act_env ist)
walther@59678
   135
walther@59684
   136
  | appy (xxx as (_, ctxt)) ist (Const ("Tactical.If"(*1*), _) $ c $ e1 $ e2) =
walther@59684
   137
    if Rewrite.eval_true_ ctxt (get_rls ist) (subst_atomic (Env.upd_env_opt' (get_subst ist)) c)
walther@59684
   138
    then appy xxx (ist |> path_down [L, R]) e1
walther@59684
   139
    else appy xxx (ist |> path_down [R]) e2
walther@59678
   140
walther@59684
   141
  | appy xxx ist (Const ("Tactical.Repeat"(*1*), _) $ e $ a) = 
walther@59684
   142
    appy xxx (ist |> path_down_form ([L, R], a)) e
walther@59684
   143
  | appy xxx ist (Const ("Tactical.Repeat"(*2*), _) $ e) =
walther@59684
   144
    appy xxx (ist |> path_down [R]) e
walther@59678
   145
walther@59684
   146
  | appy xxx ist (Const ("Tactical.Try"(*1*), _) $ e $ a) =
walther@59684
   147
    (case appy xxx (ist|> path_down_form ([L, R], a)) e of
walther@59671
   148
      Napp E => (Skip (get_act ist, E))
walther@59671
   149
    | ay => ay)
walther@59684
   150
  | appy xxx ist (Const ("Tactical.Try"(*2*), _) $ e) =
walther@59684
   151
    (case appy xxx (ist |> path_down [R]) e of
walther@59671
   152
      Napp E => (Skip (get_act ist, E))
walther@59671
   153
    | ay => ay)
walther@59678
   154
walther@59684
   155
  | appy xxx ist (Const ("Tactical.Or"(*1*), _) $e1 $ e2 $ a) =
walther@59684
   156
    (case appy xxx (ist |> path_down_form ([L, L, R], a)) e1 of
walther@59671
   157
	    Appy lme => Appy lme
walther@59684
   158
    | _ => appy xxx (ist |> path_down_form ([L, R], a)) e2)
walther@59684
   159
 | appy xxx ist (Const ("Tactical.Or"(*2*), _) $e1 $ e2) =
walther@59684
   160
    (case appy xxx (ist |> path_down [L, R]) e1 of
walther@59671
   161
	    Appy lme => Appy lme
walther@59684
   162
    | _ => appy xxx (ist |> path_down [R]) e2)
walther@59678
   163
walther@59684
   164
  | appy xxx ist (Const ("Tactical.Seq"(*1*), _) $ e1 $ e2 $ a) =
walther@59684
   165
    (case appy xxx (ist |> path_down_form ([L, L, R], a)) e1 of
walther@59684
   166
	    Skip (v, E) => appy xxx (ist |> path_down_form ([L, R], a) |> upd_act_env (v, E)) e2 (*UPD*)
walther@59671
   167
    | ay => ay)
walther@59684
   168
  | appy xxx ist (Const ("Tactical.Seq"(*2*), _) $ e1 $ e2) =
walther@59684
   169
    (case appy xxx (ist |> path_down [L, R]) e1 of
walther@59684
   170
	    Skip (v, E) => appy xxx (ist |> path_down [R] |> upd_act_env (v, E)) e2
walther@59671
   171
    | ay => ay)
walther@59672
   172
    (*========== a leaf has been found ==========*)   
walther@59684
   173
  | appy ((pt, p), ctxt) ist t =
walther@59684
   174
    case Lucin.handle_leaf "next  " ctxt (get_rls ist) (get_subst ist) t of
walther@59671
   175
	    (_, Celem.Expr s) => Skip (s, get_env ist)
walther@59671
   176
    | (a', Celem.STac stac) =>
walther@59671
   177
	    let
walther@59684
   178
	      val (m, m') = Lucin.stac2tac_ pt (Celem.assoc_thy ctxt) stac
walther@59671
   179
      in
walther@59671
   180
        case m of (*TODO?: make applicable_in..Subproblem analogous to other tacs ?refine problem?*)
walther@59680
   181
  	      Tactic.Subproblem _ => Appy (m', ist |> upd_subst_reset (a', Lucin.tac_2res m'))
walther@59671
   182
  	    | _ =>
walther@59671
   183
          (case Applicable.applicable_in p pt m of
walther@59680
   184
  		      Chead.Appl m' => (Appy (m', ist |> upd_subst_reset (a', Lucin.tac_2res m')))
walther@59671
   185
  		    | _ => Napp (get_env ist))
wneuper@59554
   186
	    end
wneuper@59554
   187
walther@59684
   188
fun nxt_up (yyy as (Rule.Prog sc, xxx)) ist (Const ("HOL.Let"(*1a*), _) $ _) =
walther@59684
   189
    if get_fini ist = Napp_
walther@59684
   190
    then nstep_up yyy (ist |> path_up)
walther@59671
   191
    else (*Skip_*)
walther@59671
   192
	    let
walther@59671
   193
        val (i, body) = check_Let_up (get_path ist) sc
walther@59671
   194
      in
walther@59684
   195
        case appy xxx (ist |> path_up_down [R, D] |> upd_env' i) body of
walther@59671
   196
	        Appy lre => Appy lre
walther@59684
   197
	      | Napp E => nstep_up yyy (ist |> path_up |> upd_env E)
walther@59684
   198
	      | Skip (v, E) => nstep_up yyy (ist |> path_up |> upd_act_env (v, E) |> upd_appy Skip_)
walther@59671
   199
	    end
walther@59684
   200
  | nxt_up yyy ist (Abs _(*2*)) =  nstep_up yyy ist
walther@59684
   201
  | nxt_up yyy ist (Const ("HOL.Let"(*3*),_) $ _ $ (Abs _)) =
walther@59684
   202
    nstep_up yyy ist
walther@59678
   203
walther@59672
   204
    (* no appy_: never causes Napp -> Helpless *)
walther@59684
   205
  | nxt_up (yyy as (_, xxx as (_, ctxt))) ist (Const ("Tactical.While"(*1*), _) $ c $ e $ _) = 
walther@59684
   206
    if Rewrite.eval_true_ ctxt (get_rls ist) (subst_atomic (Env.upd_env_opt' (get_subst ist)) c) 
walther@59671
   207
    then
walther@59684
   208
      case appy xxx (ist |> path_down [L, R]) e of
walther@59671
   209
  	     Appy lr => Appy lr
walther@59684
   210
  	  | Napp E => nstep_up yyy (ist |> upd_env E |> upd_appy Skip_)
walther@59684
   211
  	  | Skip (v, E) => nstep_up yyy (ist |> upd_act_env (v, E) |> upd_appy Skip_)
walther@59671
   212
    else
walther@59684
   213
      nstep_up yyy (ist |> upd_appy Skip_)
walther@59672
   214
    (* no appy_: never causes Napp - Helpless *)
walther@59684
   215
  | nxt_up  (yyy as (_, xxx as (_, ctxt))) ist (Const ("Tactical.While"(*2*), _) $ c $ e) = 
walther@59684
   216
    if Rewrite.eval_true_ ctxt (get_rls ist) (subst_atomic (Env.upd_env_opt' (get_subst ist)) c) 
walther@59671
   217
    then
walther@59684
   218
      case appy xxx (ist |> path_down [R]) e of
walther@59671
   219
  	    Appy lr => Appy lr
walther@59684
   220
  	  | Napp E => nstep_up yyy (ist |> upd_env E |> upd_appy Skip_)
walther@59684
   221
  	  | Skip (v, E) => nstep_up yyy (ist |> upd_act_env (v, E) |> upd_appy Skip_)
walther@59671
   222
    else
walther@59684
   223
      nstep_up yyy (ist |> upd_appy Skip_)
walther@59678
   224
walther@59684
   225
  | nxt_up yyy ist (Const ("Tactical.If"(*1*), _) $ _ $ _ $ _) = nstep_up yyy ist
walther@59678
   226
walther@59672
   227
    (* no appy_: there was already a stac below *)
walther@59684
   228
  | nxt_up (yyy as (_, xxx)) (ist) (Const ("Tactical.Repeat"(*1*), _) $ e $ _) =
walther@59684
   229
    (case appy xxx (ist |> path_down [L, R]) e of
walther@59671
   230
      Appy lr => Appy lr
walther@59684
   231
    | Napp E =>  nstep_up yyy (ist |> upd_env E |> upd_appy Skip_)
walther@59684
   232
    | Skip (v, E) =>  nstep_up yyy (ist |> upd_act_env (v, E) |> upd_appy Skip_))
walther@59672
   233
    (* no appy_: there was already a stac below *)
walther@59684
   234
  | nxt_up  (yyy as (_, xxx)) ist (Const ("Tactical.Repeat"(*2*), _) $ e) =
walther@59684
   235
    (case appy xxx (ist |> path_down [R]) e of
walther@59671
   236
      Appy lr => Appy lr
walther@59684
   237
    | Napp E => nstep_up yyy (ist |> upd_env E |> upd_appy Skip_)
walther@59684
   238
    | Skip (v, E) => nstep_up yyy (ist |> upd_act_env (v, E) |> upd_appy Skip_))
walther@59678
   239
walther@59672
   240
    (* makes Napp to Skip *)
walther@59684
   241
  | nxt_up yyy ist (Const ("Tactical.Try"(*1*), _) $ _ $ _) = nstep_up yyy (ist |> upd_appy Skip_)
walther@59684
   242
  | nxt_up yyy ist (Const ("Tactical.Try"(*2*), _) $ _) = nstep_up yyy (ist |> upd_appy Skip_)
walther@59678
   243
walther@59684
   244
  | nxt_up yyy ist (Const ("Tactical.Or"(*1*), _) $ _ $ _ $ _) = nstep_up yyy ist
walther@59684
   245
  | nxt_up yyy ist (Const ("Tactical.Or"(*2*), _) $ _ $ _) = nstep_up yyy ist
walther@59684
   246
  | nxt_up yyy ist (Const ("Tactical.Or"(*3*), _) $ _ ) = nstep_up yyy ist
walther@59678
   247
walther@59684
   248
  | nxt_up yyy ist (Const ("Tactical.Seq"(*1*), _) $ _ $ _ $ _) = nstep_up yyy ist
walther@59684
   249
  | nxt_up yyy ist (Const ("Tactical.Seq"(*2*), _) $ _ $ _) = nstep_up yyy ist
walther@59678
   250
walther@59684
   251
  | nxt_up (yyy as (Rule.Prog sc, xxx)) ist (Const ("Tactical.Seq"(*3*), _) $ _) =
walther@59684
   252
    if get_fini ist = Napp_
walther@59684
   253
    then nstep_up yyy (ist |> path_up)
walther@59671
   254
    else (*Skip_*)
walther@59671
   255
      let 
walther@59671
   256
        val e2 = check_Seq_up (get_path ist) sc
walther@59671
   257
      in
walther@59684
   258
        case appy xxx (ist |> path_up_down [R]) e2 of
walther@59671
   259
          Appy lr => Appy lr
walther@59684
   260
        | Napp E => nstep_up yyy (ist |> path_up |> upd_env E)
walther@59684
   261
        | Skip (v, E) => nstep_up yyy (ist |> path_up |> upd_act_env (v, E) |> upd_appy Skip_)
walther@59671
   262
      end
walther@59684
   263
  | nxt_up _ _ t = error ("nxt_up not impl for " ^ Rule.term2str t)
walther@59684
   264
and nstep_up (yyy as (Rule.Prog sc, _)) ist = 
walther@59671
   265
    if 1 < length (get_path ist)
walther@59671
   266
    then 
walther@59684
   267
      nxt_up yyy (ist |> path_up) (go_up (get_path ist) sc)
walther@59671
   268
    else (*interpreted to end*)
walther@59684
   269
      if (get_fini ist) = Skip_ then Skip (get_act_env ist) else Napp (get_env ist) 
walther@59684
   270
  | nstep_up _ ist = error ("nstep_up: uncovered fun-def at " ^ Celem.loc_2str (get_path ist))
wneuper@59554
   271
walther@59684
   272
walther@59684
   273
walther@59684
   274
        fun execute_progr_1 thy ptp (sc as Rule.Prog prog) (Istate.Pstate ist) =
walther@59684
   275
          if 0 = length (get_path ist)
walther@59684
   276
(*old* )   then appy thy ptp (trans_scan_down2 ist) (Program.body_of prog)( *old*)
walther@59684
   277
(*new*)   then appy (ptp, (fst thy)) (trans_scan_down2 ist)(*sets AppUndef?!?*) (Program.body_of prog)(*new*)
walther@59684
   278
(*old* )   else nstep_up thy ptp sc (trans_scan_up2 ist) Skip_( *old*)
walther@59684
   279
(*new*)   else nstep_up (sc, (ptp, (fst thy))) (trans_scan_up2 ist |> upd_appy Skip_)(*sets AppUndef?!?*)(*new*)
walther@59684
   280
        | execute_progr_1 _ _ _ _ = raise ERROR "execute_progr_1: uncovered pattern";
wneuper@59565
   281
walther@59678
   282
(* decide for the next applicable Prog_Tac in the program *)
walther@59679
   283
fun determine_next_tactic thy (ptp as (pt, (p, _))) sc (Istate.Pstate ist, ctxt) =
walther@59679
   284
   (case execute_progr_1 thy ptp sc (Istate.Pstate ist) of
walther@59675
   285
      Skip (v, _) =>                                                        (* program finished *)
walther@59675
   286
        (case par_pbl_det pt p of
walther@59675
   287
	        (true, p', _) => 
walther@59675
   288
	          let
walther@59680
   289
	            val (_, pblID, _) = get_obj g_spec pt p';
walther@59675
   290
	          in
walther@59675
   291
              (Tactic.Check_Postcond' (pblID, (v, [(*assigned in next step*)])), 
walther@59675
   292
	              (Istate.e_istate, ctxt), (v, Telem.Safe)) 
walther@59675
   293
            end
walther@59675
   294
	      | _ =>
walther@59675
   295
          (Tactic.End_Detail' (Rule.e_term,[])(*8.6.03*), (Istate.e_istate, ctxt), (v, Telem.Safe)))
walther@59675
   296
    | Napp _ =>
walther@59675
   297
        (Tactic.Empty_Tac_, (Istate.e_istate, ctxt), (Rule.e_term, Telem.Helpless_))(* helpless *)
walther@59680
   298
    | Appy (m', ist) =>
walther@59680
   299
        (m', (Pstate ist, ctxt), (get_act ist, Telem.Safe)))                  (* found next tac *)
wneuper@59560
   300
  | determine_next_tactic _ _ _ (is, _) =
wneuper@59572
   301
    error ("determine_next_tactic: not impl for " ^ (Istate.istate2str is));
wneuper@59554
   302
walther@59656
   303
(**** locate an input tactic within a program ****)
wneuper@59557
   304
wneuper@59567
   305
datatype input_tactic_result =
walther@59655
   306
    Safe_Step of Istate.T * Proof.context * Tactic.T
walther@59655
   307
  | Unsafe_Step of Istate.T * Proof.context * Tactic.T
walther@59656
   308
  | Not_Locatable of string
wneuper@59567
   309
walther@59656
   310
datatype assoc =    (* "ExprVal" in the sense of denotational semantics                      *)
walther@59656
   311
  Assoc of          (* the stac is associated, strongly or weakly                            *)
walther@59667
   312
  Istate.pstate * (* the current; returned for determine_next_tactic etc. outside ass      *)
walther@59641
   313
  Proof.context *   (* carry from assy via Ass: T * term * Proof.context -> ass to fun solve *)
walther@59658
   314
  Tactic.T          (* the tactic located in the program                                     *)
walther@59656
   315
| NasApp of         (* stac not associated, but applicable, ctree-node generated             *)
walther@59667
   316
  Istate.pstate * Proof.context *  Tactic.T
walther@59656
   317
| NasNap of         (* stac not associated, not applicable, nothing generated;               
walther@59656
   318
	                     for distinction in Or, for leaving iterations, leaving Seq,           
walther@59656
   319
		                   evaluate Prog_Expr                                                    *)
walther@59659
   320
  term * Env.T;
wneuper@59557
   321
fun assoc2str (Assoc _) = "Assoc"
wneuper@59557
   322
  | assoc2str (NasNap _) = "NasNap"
wneuper@59557
   323
  | assoc2str (NasApp _) = "NasApp";
wneuper@59557
   324
walther@59684
   325
fun assy xxx ist (Const ("HOL.Let", _) $ e $ (Abs (id, T, b))) =
walther@59684
   326
    (case assy xxx (ist |> path_down [L, R]) e of
walther@59684
   327
       NasApp (ist', ctxt, ss) =>
walther@59684
   328
         assy xxx (ist' |> path_down [R, D] |> upd_env' (TermC.mk_Free (id, T)) |> trans_ass ist) b
walther@59661
   329
     | NasNap (v, E) =>
walther@59684
   330
         assy xxx (ist |> path_down [R, D] |> upd_env (Env.upd_env E (TermC.mk_Free (id, T), v))) b
walther@59661
   331
     | ay => ay)
walther@59678
   332
walther@59684
   333
  | assy xxx ist (Const ("Tactical.While"(*1*), _) $ c $ e $ a) =
walther@59677
   334
    if Rewrite.eval_true_ "Isac_Knowledge" (get_rls ist)
walther@59661
   335
      (subst_atomic (ist |> get_act_env |> Env.upd_env' a) c)
walther@59684
   336
    then assy xxx (ist |> path_down [L, R] |> upd_form a) e
walther@59661
   337
    else NasNap (ist |> get_act_env)
walther@59684
   338
  | assy xxx ist (Const ("Tactical.While"(*2*),_) $ c $ e) =
walther@59677
   339
    if Rewrite.eval_true_ "Isac_Knowledge" (get_rls ist) (subst_atomic (Env.upd_env_opt' (get_subst ist)) c) 
walther@59684
   340
    then assy xxx (ist |> path_down [R]) e
walther@59676
   341
    else NasNap (ist |> get_act_env)
walther@59678
   342
walther@59684
   343
  | assy xxx ist (Const ("Tactical.Try"(*1*), _) $ e $ a) =
walther@59684
   344
    (case assy xxx (ist |> path_down_form ([L, R], a)) e of ay => ay) 
walther@59684
   345
  | assy xxx ist (Const ("Tactical.Try"(*2*), _) $ e) =
walther@59684
   346
    (case assy xxx (ist |> path_down [R]) e of ay => ay)
walther@59678
   347
walther@59684
   348
  | assy (xxx as (cstate, _, _)) ist (Const ("Tactical.Seq"(*1*), _) $ e1 $ e2 $ a) =
walther@59684
   349
    (case assy xxx (ist |> path_down_form ([L, L, R], a)) e1 of
walther@59684
   350
	     NasNap (v, E)
walther@59684
   351
        => assy xxx (ist |> path_down [L, R] |> upd_subst E (a, v)) e2
walther@59684
   352
     | NasApp (ist', ctxt', tac')
walther@59684
   353
       => assy (cstate, ctxt', tac')  (ist |> path_down [L, R] |> trans_env_act ist' |> upd_form a) e2
wneuper@59557
   354
     | ay => ay)
walther@59684
   355
  | assy (xxx as (cstate, _, _)) ist (Const ("Tactical.Seq"(*2*), _) $e1 $ e2) =
walther@59684
   356
    (case assy xxx (ist |> path_down [L, R]) e1 of
walther@59684
   357
	     NasNap (v, E) => assy xxx (ist |> path_down [R] |> upd_act_env (v, E)) e2
walther@59661
   358
     | NasApp (ist', ctxt', tac')
walther@59684
   359
        => assy (cstate, ctxt', tac') (ist |> path_down [R] |> trans_env_act ist') e2
walther@59656
   360
     | ay => ay)
walther@59678
   361
walther@59684
   362
  | assy xxx ist (Const ("Tactical.Repeat"(*1*), _) $ e $ a) =
walther@59684
   363
    assy xxx (ist |> path_down [L, R] |> upd_form a) e
walther@59684
   364
  | assy xxx ist (Const ("Tactical.Repeat"(*2*), _) $ e) =
walther@59684
   365
    assy xxx (ist |> path_down [R]) e
walther@59678
   366
walther@59684
   367
  | assy xxx (ist as {or = Aundef, ...}) (Const ("Tactical.Or"(*1*), _) $e1 $ e2 $ a) =
walther@59684
   368
    (case assy xxx (ist |> path_down_form ([L, L, R], a) |> upd_or AssOnly) e1 of
walther@59661
   369
	     NasNap (v, E) =>
walther@59684
   370
	       (case assy xxx (ist |> path_down [L, R] |> upd_subst E (a, v) |> upd_or AssOnly) e2 of
walther@59661
   371
	          NasNap (v, E) => 
walther@59684
   372
	            (case assy xxx (ist |> path_down [L, L, R] |> upd_subst E (a, v) |> upd_or AssGen) e1 of
walther@59661
   373
	               NasNap (v, E) => 
walther@59684
   374
	                 assy xxx (ist |> path_down [L, R] |> upd_subst E (a, v) |> upd_or AssGen) e2
walther@59661
   375
	             | ay => ay)
walther@59661
   376
	        | ay =>ay)
walther@59661
   377
     | NasApp _ => raise ERROR ("assy Tactical.Or(*1*): must not return NasApp")
walther@59661
   378
     | ay => (ay))
walther@59684
   379
  | assy xxx ist (Const ("Tactical.Or"(*2*), _) $e1 $ e2) =
walther@59684
   380
    (case assy xxx (ist |> path_down [L, R]) e1 of
walther@59684
   381
	     NasNap (v, E) => assy xxx (ist |> path_down [R] |> upd_act_env (v, E)) e2
wneuper@59557
   382
     | ay => (ay))
walther@59655
   383
wneuper@59579
   384
    (*======= end of scanning tacticals, a leaf =======*)
walther@59684
   385
  | assy ((pt, p), ctxt, tac) ist t =
walther@59677
   386
    (case Lucin.handle_leaf "locate" (Context.theory_name (Rule.Isac"")) (get_rls ist) (get_subst ist) t of
walther@59661
   387
	     (a', Celem.Expr _) => 
walther@59677
   388
	       (NasNap (Rewrite.eval_listexpr_ (Celem.assoc_thy "Isac_Knowledge") (get_rls ist)
walther@59668
   389
		       (subst_atomic (Env.upd_env_opt'' (get_act_env ist, a')) t), get_env ist))
walther@59661
   390
     | (a', Celem.STac stac) =>
walther@59684
   391
         (case Lucin.associate pt ctxt (tac, stac) of
walther@59661
   392
            (* HERE ----------------vvvv ContextC.insert_assumptions asm ctxt DONE *)
walther@59661
   393
	           Lucin.Ass      (m, v', ctxt) => Assoc (ist |> upd_subst_true  (a', v'), ctxt, m)
walther@59661
   394
           | Lucin.Ass_Weak (m, v', ctxt) => Assoc (ist |> upd_subst_false (a', v'), ctxt, m)
walther@59661
   395
walther@59661
   396
           | Lucin.NotAss =>
walther@59684
   397
             (case get_or ist  of (* switch for Tactical.Or: 1st AssOnly, 2nd AssGen *)
walther@59661
   398
                AssOnly => (NasNap (ist |> get_act_env))
walther@59661
   399
              | _(*Aundef*) =>
walther@59661
   400
                 case Applicable.applicable_in p pt (Lucin.stac2tac pt (Celem.assoc_thy "Isac_Knowledge") stac) of
walther@59684
   401
                    Chead.Appl m' => NasApp (ist |> upd_subst_false (a', Lucin.tac_2res m'), ctxt, tac)
walther@59661
   402
                  | Chead.Notappl _ => (NasNap (ist |> get_act_env)))
walther@59661
   403
    ));
wneuper@59557
   404
walther@59684
   405
fun ass_up (yyy as (prog as Rule.Prog p, xxx as (cstate, ctxt, tac))) ist (Const ("HOL.Let"(*1*), _) $ _) =
walther@59664
   406
    let 
walther@59684
   407
      val (i, body) = check_Let_up (get_path ist) p
walther@59684
   408
    in case assy xxx (ist |> path_up_down [R, D] |> upd_env' i |> upd_or Aundef) body of
walther@59664
   409
	       Assoc iss => Assoc iss
walther@59684
   410
	     | NasApp (ist', ctxt', tac') => astep_up (prog, (cstate, ctxt', tac')) ist'
walther@59684
   411
	     | NasNap (v, E) => astep_up yyy (ist |> path_up |> upd_act_env (v, E)) 
walther@59664
   412
	  end
walther@59684
   413
  | ass_up yyy ist (Abs(*2*) _) = astep_up yyy ist
walther@59684
   414
  | ass_up yyy ist (Const ("HOL.Let"(*3*), _) $ _ $ (Abs _)) = astep_up yyy ist
walther@59678
   415
walther@59684
   416
    (*all has been done in (*2*) below*)
walther@59684
   417
  | ass_up yyy ist (Const ("Tactical.Seq"(*1*), _) $ _ $ _ $ _) = astep_up yyy ist 
walther@59684
   418
  | ass_up yyy ist (Const ("Tactical.Seq"(*2*), _) $ _ $ _) = astep_up yyy ist (*2*: comes from e2*)
walther@59664
   419
    (* comes from e1, goes to e2 *)
walther@59684
   420
  | ass_up (yyy as (prog as Rule.Prog p, xxx as (cstate, _, _))) ist (Const ("Tactical.Seq"(*3*), _) $ _ ) =
walther@59664
   421
     let 
walther@59684
   422
       val e2 = check_Seq_up (get_path ist) p
walther@59671
   423
     in
walther@59684
   424
       case assy xxx (ist |> path_up_down[R] |> upd_or Aundef) e2 of
walther@59684
   425
         NasNap (v, E) => astep_up yyy (ist |> path_up |> upd_act_env (v, E))
walther@59684
   426
       | NasApp (ist', ctxt', tac') => astep_up (prog, (cstate, ctxt', tac')) ist'
walther@59684
   427
       | zzz => zzz 
walther@59664
   428
     end
walther@59678
   429
walther@59684
   430
  | ass_up yyy ist (Const ("Tactical.Try"(*1*),_) $ _ $ _) = astep_up yyy ist
walther@59684
   431
  | ass_up yyy ist (Const ("Tactical.Try"(*2*),_) $ _) = astep_up yyy ist
walther@59668
   432
walther@59684
   433
  | ass_up (yyy as (prog, xxx as (cstate, _, _))) ist (t as Const ("Tactical.While"(*1*),_) $ c $ e $ a) =
walther@59677
   434
    if Rewrite.eval_true_ "Isac_Knowledge" (get_rls ist) (subst_atomic (Env.upd_env' a (get_act_env ist)) c)
walther@59668
   435
    then
walther@59684
   436
      case assy xxx (ist |> path_down_form ([L, R], a) |> upd_or Aundef) e of 
walther@59684
   437
        NasNap (v, E') => astep_up yyy (ist |> upd_act_env (v, E') |> upd_form a)
walther@59668
   438
walther@59684
   439
      | NasApp (ist', ctxt', tac') => ass_up (prog, (cstate, ctxt', tac')) ist' t
walther@59668
   440
      | ay => ay
walther@59684
   441
    else astep_up yyy (ist |> upd_form a)
walther@59684
   442
  | ass_up (yyy as (prog, xxx as (cstate, _, _))) ist (t as Const ("Tactical.While"(*2*), _) $ c $ e) =
walther@59677
   443
    if Rewrite.eval_true_ "Isac_Knowledge" (get_rls ist) (subst_atomic (Env.upd_env_opt' (get_subst ist)) c)
walther@59668
   444
    then
walther@59684
   445
      case assy xxx (ist |> path_down [R] |> upd_or Aundef) e of 
walther@59684
   446
        NasNap (v, E') => astep_up yyy (ist |> upd_act_env (v, E'))
walther@59684
   447
       | NasApp (ist', ctxt', tac') => ass_up (prog, (cstate, ctxt', tac')) ist' t
walther@59668
   448
       | ay => ay
walther@59684
   449
    else astep_up yyy ist
walther@59678
   450
walther@59684
   451
  | ass_up yyy ist (Const ("If", _) $ _ $ _ $ _) = astep_up yyy ist
walther@59668
   452
walther@59684
   453
  | ass_up (yyy as (prog, xxx as (cstate, _, _))) ist (t as Const ("Tactical.Repeat"(*1*), _) $ e $ a) =
walther@59684
   454
    (case assy xxx (ist |> path_down_form ([L, R], a) |> upd_or Aundef) e of 
walther@59684
   455
      NasNap (v, E') => astep_up yyy (ist |> upd_form_env (v, E') |> upd_form a)
walther@59684
   456
    | NasApp (ist', ctxt', tac') => ass_up (prog, (cstate, ctxt', tac')) ist' t
walther@59684
   457
    | zzz => zzz)
walther@59684
   458
  | ass_up (yyy as (prog, xxx as (cstate, _, _))) ist (t as Const ("Tactical.Repeat"(*2*), _) $ e) =
walther@59684
   459
    (case assy xxx (ist |> path_down [R] |> upd_or Aundef) e of
walther@59684
   460
       NasNap (v', E') => astep_up yyy (ist |> upd_act_env (v', E'))
walther@59684
   461
     | NasApp (ist', ctxt', tac') => ass_up (prog, (cstate, ctxt', tac')) ist' t
wneuper@59557
   462
     | ay => ay)
walther@59678
   463
walther@59684
   464
  | ass_up yyy ist (Const ("Tactical.Or"(*1*), _) $ _ $ _ $ _) = astep_up yyy ist
walther@59684
   465
  | ass_up yyy ist (Const ("Tactical.Or"(*2*), _) $ _ $ _) = astep_up yyy ist
walther@59684
   466
  | ass_up yyy ist (Const ("Tactical.Or"(*3*), _) $ _ ) = astep_up yyy (ist |> path_up)
walther@59678
   467
walther@59658
   468
  | ass_up _ _ t = error ("ass_up not impl for t= " ^ Rule.term2str t)
walther@59684
   469
and astep_up (yyy as (Rule.Prog sc, _)) ist =
walther@59678
   470
    if 1 < length (get_path ist)
walther@59684
   471
    then ass_up yyy (ist |> path_up) (go (get_path_up ist) sc)
walther@59678
   472
    else (NasNap (get_act_env ist))
walther@59684
   473
  | astep_up _ ist = error ("astep_up: uncovered fun-def with " ^ Celem.loc_2str (get_path ist))
wneuper@59557
   474
walther@59684
   475
walther@59684
   476
   fun execute_progr_2 _ m (pt, p) (scr as Rule.Prog sc, _) (Istate.Pstate ist, ctxt) =
walther@59684
   477
       if get_path ist = [] orelse ((last_elem o fst) p = 0 andalso snd p = Res)
walther@59684
   478
(*old* )then assy (ctxt,Rule.e_rls, (pt, p), Aundef(*\<rightarrow> Istate*)) (ist |> upd_path [R], m) (Program.body_of sc)( *old*)
walther@59684
   479
(*new*)then assy ((pt, p), ctxt, m) (ist |> upd_path [R] |> upd_or Aundef) (Program.body_of sc)(*new*)
walther@59684
   480
(*old* )else astep_up (ctxt,Rule.e_rls,scr, (pt, p)) (ist, m)( *old*)
walther@59684
   481
(*new*)else astep_up (scr, ((pt, p), ctxt, m)) ist(*new*)
wneuper@59579
   482
  | execute_progr_2 _ _ _ _ _ = raise ERROR "execute_progr_2: uncovered pattern in fun.def"
wneuper@59567
   483
walther@59658
   484
fun locate_input_tactic (progr as Rule.Prog _) cstate istate ctxt tac =
wneuper@59567
   485
    let
walther@59677
   486
      val _ = Lucin.get_simplifier cstate (*TODO: shift into Istate.T*)
wneuper@59569
   487
    in
walther@59677
   488
      (case execute_progr_2 Rule.e_rls tac cstate (progr, Rule.e_rls) (istate, ctxt) of
walther@59680
   489
         Assoc (ist, ctxt, tac') =>
walther@59680
   490
          if get_assoc ist
walther@59680
   491
          then Safe_Step (Istate.Pstate ist, ctxt, tac')
walther@59680
   492
 	        else Unsafe_Step (Istate.Pstate ist, ctxt, tac')
wneuper@59571
   493
      | NasApp _ => Not_Locatable (Tactic.tac_2str tac ^ " not locatable")
wneuper@59569
   494
      | err => raise ERROR ("not-found-in-script: NotLocatable from " ^ @{make_string} err))
wneuper@59557
   495
    end
wneuper@59569
   496
  | locate_input_tactic _ _ _ _ _ = raise ERROR "locate_input_tactic: uncovered case in definition"
wneuper@59557
   497
wneuper@59557
   498
wneuper@59562
   499
(**** locate an input formula in the program ****)
wneuper@59569
   500
wneuper@59562
   501
datatype input_formula_result =
wneuper@59572
   502
    Step of Ctree.state * Istate.T * Proof.context
wneuper@59562
   503
  | Not_Derivable of Chead.calcstate'
wneuper@59562
   504
wneuper@59562
   505
(* FIXME.WN050821 compare fun solve ... fun begin_end_prog
wneuper@59562
   506
   begin_end_prog (Apply_Method'     vvv FIXME: get args in applicable_in *)
wneuper@59582
   507
fun begin_end_prog (Tactic.Apply_Method' (mI, _, _, ctxt)) _ (pt, pos as (p, _)) =
wneuper@59562
   508
    let
wneuper@59562
   509
      val {ppc, ...} = Specify.get_met mI;
wneuper@59562
   510
      val (itms, oris, probl) = case get_obj I pt p of
wneuper@59562
   511
        PblObj {meth = itms, origin = (oris, _, _), probl, ...} => (itms, oris, probl)
wneuper@59562
   512
      | _ => error "begin_end_prog Apply_Method': uncovered case get_obj"
wneuper@59562
   513
      val itms = if itms <> [] then itms else Chead.complete_metitms oris probl [] ppc
wneuper@59562
   514
      val thy' = get_obj g_domID pt p;
wneuper@59562
   515
      val thy = Celem.assoc_thy thy';
walther@59677
   516
      val srls = Lucin.get_simplifier (pt, pos)
wneuper@59555
   517
(*//----------- hack for funpack: generalise handling of meths which extend problem items -----\\*)
wneuper@59555
   518
val itms =
wneuper@59555
   519
  if mI = ["Biegelinien", "ausBelastung"]
wneuper@59555
   520
  then itms @
wneuper@59597
   521
    [(4, [1], true, "#Given", Model.Cor ((Const ("Biegelinie.Biegelinie", Type ("fun", [Type ("fun", [Type ("Real.real", []), Type ("Real.real", [])]), Type ("Input_Descript.una", [])])),
wneuper@59555
   522
        [Free ("y", Type ("fun", [Type ("Real.real", []), Type ("Real.real", [])]))]),
wneuper@59555
   523
      (Free ("id_fun", Type ("fun", [Type ("Real.real", []), Type ("Real.real", [])])),
wneuper@59555
   524
        [Free ("y", Type ("fun", [Type ("Real.real", []), Type ("Real.real", [])]))] ))),
wneuper@59597
   525
    (5, [1], true, "#Given", Model.Cor ((Const ("Biegelinie.AbleitungBiegelinie", Type ("fun", [Type ("fun", [Type ("Real.real", []), Type ("Real.real", [])]), Type ("Input_Descript.una", [])])),
wneuper@59555
   526
        [Free ("dy", Type ("fun", [Type ("Real.real", []), Type ("Real.real", [])]))]),
wneuper@59555
   527
      (Free ("id_abl", Type ("fun", [Type ("Real.real", []), Type ("Real.real", [])])),
wneuper@59555
   528
        [Free ("dy", Type ("fun", [Type ("Real.real", []), Type ("Real.real", [])]))] )))]
wneuper@59555
   529
  else itms
wneuper@59555
   530
(*\\----------- hack for funpack: generalise handling of meths which extend problem items -----//*)
walther@59677
   531
     val (is, env, ctxt, scr) = case Lucin.init_pstate srls ctxt itms mI of
walther@59676
   532
         (is as Istate.Pstate pst, ctxt, scr) => (is, Istate.get_env pst, ctxt, scr)
wneuper@59583
   533
       | _ => error "begin_end_prog Apply_Method': uncovered case init_pstate"
wneuper@59555
   534
     val ini = Lucin.init_form thy scr env;
wneuper@59555
   535
   in 
wneuper@59555
   536
     case ini of
wneuper@59555
   537
       SOME t =>
wneuper@59555
   538
       let
wneuper@59555
   539
         val pos = ((lev_on o lev_dn) p, Frm)
wneuper@59571
   540
	       val tac_ = Tactic.Apply_Method' (mI, SOME t, is, ctxt);
wneuper@59555
   541
	       val (pos, c, _, pt) = Generate.generate1 thy tac_ (is, ctxt) pos pt (* implicit Take *)
wneuper@59555
   542
       in
wneuper@59571
   543
        ([(Tactic.Apply_Method mI, tac_, (pos, (is, ctxt)))], c, (pt, pos))
wneuper@59555
   544
       end
wneuper@59555
   545
     | NONE =>
wneuper@59555
   546
       let
wneuper@59555
   547
         val pt = update_env pt (fst pos) (SOME (is, ctxt))
wneuper@59562
   548
	       val (tacis, c, ptp) = do_solve_step (pt, pos)
walther@59658
   549
       in (tacis @ [(Tactic.Apply_Method mI,
walther@59658
   550
            Tactic.Apply_Method' (mI, NONE, Istate.e_istate, ctxt), (pos, (is, ctxt)))], c, ptp)
wneuper@59555
   551
       end
wneuper@59555
   552
    end
wneuper@59571
   553
  | begin_end_prog (Tactic.Check_Postcond' (pI, _)) _ (pt, (p, p_))  =
wneuper@59555
   554
    let
wneuper@59555
   555
      val pp = par_pblobj pt p
wneuper@59555
   556
      val asm = (case get_obj g_tac pt p of
wneuper@59571
   557
		    Tactic.Check_elementwise _ => (snd o (get_obj g_result pt)) p (*collects and instantiates asms*)
wneuper@59555
   558
		  | _ => get_assumptions_ pt (p, p_))
wneuper@59555
   559
      val metID = get_obj g_metID pt pp;
wneuper@59555
   560
      val {srls = srls, scr = sc, ...} = Specify.get_met metID;
walther@59676
   561
      val (loc, pst, ctxt) = case get_loc pt (p, p_) of
walther@59676
   562
        loc as (Istate.Pstate pst, ctxt) => (loc, pst, ctxt)
wneuper@59562
   563
      | _ => error "begin_end_prog Check_Postcond': uncovered case get_loc"
wneuper@59555
   564
      val thy' = get_obj g_domID pt pp;
wneuper@59555
   565
      val thy = Celem.assoc_thy thy';
walther@59676
   566
      val (_, _, (scval, _(*scsaf*))) = determine_next_tactic (thy', srls) (pt, (p, p_)) sc loc;
walther@59675
   567
      (*                 Telem.safe should go on to Check_Postcond'   vvvvv *)
wneuper@59555
   568
    in
wneuper@59555
   569
      if pp = []
wneuper@59555
   570
      then 
wneuper@59555
   571
	      let
walther@59676
   572
          val is = Istate.Pstate (pst |> Istate.upd_act scval |> Istate.upd_appy Istate.Skip_)
walther@59675
   573
	        val tac_ = Tactic.Check_Postcond' (pI, (scval, asm))
walther@59675
   574
	          (*extend Tactic.Check_Postcond' (pI, (scval, asm), scsaf) ^^^^^ *)
wneuper@59555
   575
	        val ((p, p_), ps, _, pt) = Generate.generate1 thy tac_ (is, ctxt) (pp, Res) pt;
wneuper@59571
   576
	      in ([(Tactic.Check_Postcond pI, tac_, ((pp, Res), (is, ctxt)))], ps, (pt, (p, p_))) end
wneuper@59555
   577
      else
walther@59675
   578
        let (*resume script of parent PblObj, transfer value of subpbl-script*)
wneuper@59555
   579
          val ppp = par_pblobj pt (lev_up p);
wneuper@59555
   580
	        val thy' = get_obj g_domID pt ppp;
wneuper@59555
   581
          val thy = Celem.assoc_thy thy';
walther@59676
   582
walther@59676
   583
          val (pst', ctxt') = case get_loc pt (pp, Frm) of
walther@59676
   584
            (Istate.Pstate pst', ctxt') => (pst', ctxt')
wneuper@59562
   585
          | _ => error "begin_end_prog Check_Postcond' script of parpbl: uncovered case get_loc"
wneuper@59577
   586
	        val ctxt'' = ContextC.from_subpbl_to_caller ctxt scval ctxt'
wneuper@59571
   587
          val tac_ = Tactic.Check_Postcond' (pI, (scval, asm))
walther@59676
   588
	        val is = Istate.Pstate (pst' |> Istate.upd_act scval |> Istate.upd_appy Istate.Skip_)
wneuper@59555
   589
          val ((p, p_), ps, _, pt) = Generate.generate1 thy tac_ (is, ctxt'') (pp, Res) pt;
wneuper@59571
   590
        in ([(Tactic.Check_Postcond pI, tac_, ((pp, Res), (is, ctxt'')))], ps, (pt, (p, p_))) end
wneuper@59555
   591
    end
wneuper@59571
   592
  | begin_end_prog (Tactic.End_Proof'') _ ptp = ([], [], ptp)
wneuper@59562
   593
  | begin_end_prog tac_ is (pt, pos) =
wneuper@59555
   594
    let
wneuper@59555
   595
      val pos = case pos of
wneuper@59555
   596
 		   (p, Met) => ((lev_on o lev_dn) p, Frm) (* begin script        *)
wneuper@59555
   597
 		  | (p, Res) => (lev_on p, Res)            (* somewhere in script *)
wneuper@59555
   598
 		  | _ => pos
wneuper@59592
   599
 	    val (pos', c, _, pt) = Generate.generate1 (Celem.assoc_thy "Isac_Knowledge") tac_ is pos pt;
wneuper@59555
   600
    in
wneuper@59555
   601
      ([(Lucin.tac_2tac tac_, tac_, (pos, is))], c, (pt, pos'))
wneuper@59555
   602
    end
wneuper@59562
   603
(* find the next tac from the script, begin_end_prog will update the ctree *)
wneuper@59569
   604
and do_solve_step (ptp as (pt, pos as (p, p_))) = (* WN1907: ?only for Begin_/End_Detail' DEL!!!*)
wneuper@59555
   605
    if Celem.e_metID = get_obj g_metID pt (par_pblobj pt p)
wneuper@59555
   606
    then ([], [], (pt, (p, p_)))
wneuper@59555
   607
    else 
wneuper@59555
   608
      let 
wneuper@59555
   609
        val thy' = get_obj g_domID pt (par_pblobj pt p);
wneuper@59555
   610
	      val (srls, is, sc) = Lucin.from_pblobj_or_detail' thy' (p,p_) pt;
wneuper@59559
   611
	      val (tac_, is, (t, _)) = determine_next_tactic (thy', srls) (pt, pos) sc is;
wneuper@59569
   612
	      (* TODO here  ^^^  return finished/helpless/ok ?*)
wneuper@59555
   613
	    in case tac_ of
walther@59658
   614
		    Tactic.End_Detail' _ => ([(Tactic.End_Detail,
walther@59658
   615
          Tactic.End_Detail' (t, [(*FIXME.04*)]), (pos, is))], [], (pt, pos))
wneuper@59562
   616
	    | _ => begin_end_prog tac_ is ptp
wneuper@59555
   617
      end;
wneuper@59555
   618
 
wneuper@59562
   619
(* compare inform with ctree.form at current pos by nrls;
wneuper@59555
   620
   if found, embed the derivation generated during comparison
wneuper@59555
   621
   if not, let the mat-engine compute the next ctree.form *)
wneuper@59555
   622
(* code's structure is copied from complete_solve
wneuper@59555
   623
   CAUTION: tacis in returned calcstate' do NOT construct resulting ptp --
wneuper@59555
   624
            all_modspec etc. has to be inserted at Subproblem'*)
wneuper@59555
   625
fun compare_step ((tacis, c, ptp as (pt, pos as (p,p_))): Chead.calcstate') ifo =
wneuper@59555
   626
  let
wneuper@59555
   627
    val fo =
wneuper@59555
   628
      case p_ of
wneuper@59555
   629
        Ctree.Frm => Ctree.get_obj Ctree.g_form pt p
wneuper@59555
   630
			| Ctree.Res => (fst o (Ctree.get_obj Ctree.g_result pt)) p
wneuper@59555
   631
			| _ => Rule.e_term (*on PblObj is fo <> ifo*);
wneuper@59555
   632
	  val {nrls, ...} = Specify.get_met (Ctree.get_obj Ctree.g_metID pt (Ctree.par_pblobj pt p))
wneuper@59555
   633
	  val {rew_ord, erls, rules, ...} = Rule.rep_rls nrls
wneuper@59555
   634
	  val (found, der) = Inform.concat_deriv rew_ord erls rules fo ifo; (*<---------------*)
wneuper@59555
   635
  in
wneuper@59555
   636
    if found
wneuper@59555
   637
    then
wneuper@59555
   638
       let
wneuper@59555
   639
         val tacis' = map (Inform.mk_tacis rew_ord erls) der;
wneuper@59555
   640
		     val (c', ptp) = Generate.embed_deriv tacis' ptp;
wneuper@59555
   641
	     in ("ok", (tacis (*@ tacis'?WN050408*), c @ c', ptp)) end
wneuper@59555
   642
     else 
wneuper@59555
   643
	     if pos = ([], Ctree.Res) (*TODO: we should stop earlier with trying subproblems *)
wneuper@59555
   644
	     then ("no derivation found", (tacis, c, ptp): Chead.calcstate') 
wneuper@59555
   645
	     else
wneuper@59555
   646
         let
wneuper@59562
   647
           val cs' as (tacis, c', ptp) = do_solve_step ptp; (*<---------------------*)
wneuper@59555
   648
		       val (tacis, c'', ptp) = case tacis of
wneuper@59571
   649
			       ((Tactic.Subproblem _, _, _)::_) => 
wneuper@59555
   650
			         let
wneuper@59555
   651
                 val ptp as (pt, (p,_)) = Chead.all_modspec ptp (*<--------------------*)
wneuper@59555
   652
				         val mI = Ctree.get_obj Ctree.g_metID pt p
wneuper@59555
   653
			         in
wneuper@59581
   654
			           begin_end_prog (Tactic.Apply_Method' (mI, NONE, Istate.e_istate, ContextC.e_ctxt)) (Istate.e_istate, ContextC.e_ctxt) ptp
wneuper@59555
   655
               end
wneuper@59555
   656
			     | _ => cs';
wneuper@59555
   657
		     in compare_step (tacis, c @ c' @ c'', ptp) ifo end
wneuper@59555
   658
  end
wneuper@59555
   659
wneuper@59556
   660
(*.handle a user-input formula, which may be a CAS-command, too.
wneuper@59556
   661
CAS-command:
wneuper@59556
   662
   create a calchead, and do 1 step
wneuper@59556
   663
   TOOODO.WN0602 works only for the root-problem !!!
wneuper@59556
   664
formula, which is no CAS-command:
wneuper@59556
   665
   compare iform with calc-tree.form at pos by equ_nrls and all subsequent pos;
wneuper@59556
   666
   collect all the tacs applied by the way;
wneuper@59556
   667
   if "no derivation found" then check_error_patterns.
wneuper@59556
   668
   ALTERNATIVE: check_error_patterns _within_ compare_step seems too expensive.*)
wneuper@59562
   669
(*                       vvvv           vvvvvv vvvv    NEW args for compare_step *)
walther@59658
   670
fun locate_input_formula (Rule.Prog _)  ((pt, pos) : Ctree.state) (_ : Istate.T) (_ : Proof.context) tm =
walther@59658
   671
     let                                                          
walther@59658
   672
   		val pos_pred = Ctree.lev_back' pos (*f_pred ---"step pos cs"---> f_succ in appendFormula*)
walther@59658
   673
   		val _(*f_pred*) = Ctree.get_curr_formula (pt, pos_pred)
walther@59658
   674
     in
walther@59658
   675
       (*TODO: use prog, istate, ctxt in compare_step ...*)
walther@59658
   676
   		case compare_step ([], [], (pt, pos_pred)) tm of
walther@59658
   677
   		  ("no derivation found", calcstate') => Not_Derivable calcstate'
walther@59658
   678
       | ("ok", (_, _, cstate as (pt', pos'))) => 
walther@59658
   679
           Step (cstate, get_istate pt' pos', get_ctxt pt' pos')
walther@59658
   680
       | _ => raise ERROR "compare_step: uncovered case"
walther@59658
   681
     end
walther@59658
   682
  | locate_input_formula _ _ _ _ _ = error ""
wneuper@59556
   683
walther@59684
   684
(**)end(**)