src/Tools/isac/Interpret/lucas-interpreter.sml
author Walther Neuper <walther.neuper@jku.at>
Thu, 07 Nov 2019 11:49:00 +0100
changeset 59692 a889efd0037b
parent 59691 53c60fa9c41c
child 59694 2f86079ee85a
permissions -rw-r--r--
lucin: remove step-construction by Rrls

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