src/Pure/Isar/toplevel.ML
author wenzelm
Mon Mar 30 22:38:37 2009 +0200 (2009-03-30 ago)
changeset 30801 9bdf001bea58
parent 30618 046f4f986fb5
child 31431 6b840c0b7fb6
permissions -rw-r--r--
added Toplevel.previous_node_of;
keep type Toplevel.node private (formerly required in document antiquotations, which now operate on plain Toplevel.state);
wenzelm@5828
     1
(*  Title:      Pure/Isar/toplevel.ML
wenzelm@5828
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5828
     3
wenzelm@26602
     4
Isabelle/Isar toplevel transactions.
wenzelm@5828
     5
*)
wenzelm@5828
     6
wenzelm@5828
     7
signature TOPLEVEL =
wenzelm@5828
     8
sig
wenzelm@19063
     9
  exception UNDEF
wenzelm@20963
    10
  type generic_theory
wenzelm@5828
    11
  type state
wenzelm@26602
    12
  val toplevel: state
wenzelm@7732
    13
  val is_toplevel: state -> bool
wenzelm@18589
    14
  val is_theory: state -> bool
wenzelm@18589
    15
  val is_proof: state -> bool
wenzelm@17076
    16
  val level: state -> int
wenzelm@30398
    17
  val presentation_context_of: state -> Proof.context
wenzelm@30801
    18
  val previous_context_of: state -> Proof.context option
wenzelm@21506
    19
  val context_of: state -> Proof.context
wenzelm@22089
    20
  val generic_theory_of: state -> generic_theory
wenzelm@5828
    21
  val theory_of: state -> theory
wenzelm@5828
    22
  val proof_of: state -> Proof.state
wenzelm@18589
    23
  val proof_position_of: state -> int
wenzelm@21007
    24
  val enter_proof_body: state -> Proof.state
wenzelm@16815
    25
  val print_state_context: state -> unit
wenzelm@16815
    26
  val print_state: bool -> state -> unit
wenzelm@16682
    27
  val quiet: bool ref
wenzelm@16682
    28
  val debug: bool ref
wenzelm@17321
    29
  val interact: bool ref
wenzelm@16682
    30
  val timing: bool ref
wenzelm@16682
    31
  val profiling: int ref
wenzelm@16815
    32
  val skip_proofs: bool ref
wenzelm@5828
    33
  exception TERMINATE
wenzelm@27583
    34
  exception TOPLEVEL_ERROR
wenzelm@26256
    35
  exception CONTEXT of Proof.context * exn
wenzelm@20128
    36
  val exn_message: exn -> string
wenzelm@20128
    37
  val program: (unit -> 'a) -> 'a
wenzelm@16682
    38
  type transition
wenzelm@5828
    39
  val empty: transition
wenzelm@27441
    40
  val init_of: transition -> string option
wenzelm@27427
    41
  val name_of: transition -> string
wenzelm@28105
    42
  val pos_of: transition -> Position.T
wenzelm@27500
    43
  val str_of: transition -> string
wenzelm@5828
    44
  val name: string -> transition -> transition
wenzelm@5828
    45
  val position: Position.T -> transition -> transition
wenzelm@5828
    46
  val interactive: bool -> transition -> transition
wenzelm@5828
    47
  val print: transition -> transition
wenzelm@9010
    48
  val no_timing: transition -> transition
wenzelm@27576
    49
  val init_theory: string -> (bool -> theory) -> (theory -> unit) -> transition -> transition
wenzelm@6689
    50
  val exit: transition -> transition
wenzelm@5828
    51
  val keep: (state -> unit) -> transition -> transition
wenzelm@7612
    52
  val keep': (bool -> state -> unit) -> transition -> transition
wenzelm@5828
    53
  val imperative: (unit -> unit) -> transition -> transition
wenzelm@27840
    54
  val ignored: Position.T -> transition
wenzelm@27840
    55
  val malformed: Position.T -> string -> transition
wenzelm@5828
    56
  val theory: (theory -> theory) -> transition -> transition
wenzelm@26491
    57
  val generic_theory: (generic_theory -> generic_theory) -> transition -> transition
wenzelm@7612
    58
  val theory': (bool -> theory -> theory) -> transition -> transition
wenzelm@20985
    59
  val begin_local_theory: bool -> (theory -> local_theory) -> transition -> transition
wenzelm@21007
    60
  val end_local_theory: transition -> transition
wenzelm@29380
    61
  val local_theory': xstring option -> (bool -> local_theory -> local_theory) ->
wenzelm@29380
    62
    transition -> transition
wenzelm@20963
    63
  val local_theory: xstring option -> (local_theory -> local_theory) -> transition -> transition
wenzelm@30366
    64
  val present_local_theory: xstring option -> (state -> unit) -> transition -> transition
berghofe@24453
    65
  val local_theory_to_proof': xstring option -> (bool -> local_theory -> Proof.state) ->
berghofe@24453
    66
    transition -> transition
wenzelm@21007
    67
  val local_theory_to_proof: xstring option -> (local_theory -> Proof.state) ->
wenzelm@21007
    68
    transition -> transition
wenzelm@17363
    69
  val theory_to_proof: (theory -> Proof.state) -> transition -> transition
wenzelm@21007
    70
  val end_proof: (bool -> Proof.state -> Proof.context) -> transition -> transition
wenzelm@21007
    71
  val forget_proof: transition -> transition
wenzelm@30366
    72
  val present_proof: (state -> unit) -> transition -> transition
wenzelm@21177
    73
  val proofs': (bool -> Proof.state -> Proof.state Seq.seq) -> transition -> transition
wenzelm@17904
    74
  val proof': (bool -> Proof.state -> Proof.state) -> transition -> transition
wenzelm@21177
    75
  val proofs: (Proof.state -> Proof.state Seq.seq) -> transition -> transition
wenzelm@21177
    76
  val proof: (Proof.state -> Proof.state) -> transition -> transition
wenzelm@27564
    77
  val actual_proof: (ProofNode.T -> ProofNode.T) -> transition -> transition
wenzelm@27564
    78
  val skip_proof: (int -> int) -> transition -> transition
wenzelm@17904
    79
  val skip_proof_to_theory: (int -> bool) -> transition -> transition
wenzelm@27427
    80
  val get_id: transition -> string option
wenzelm@27427
    81
  val put_id: string -> transition -> transition
wenzelm@9512
    82
  val unknown_theory: transition -> transition
wenzelm@9512
    83
  val unknown_proof: transition -> transition
wenzelm@9512
    84
  val unknown_context: transition -> transition
wenzelm@28425
    85
  val setmp_thread_position: transition -> ('a -> 'b) -> 'a -> 'b
wenzelm@27606
    86
  val status: transition -> Markup.T -> unit
wenzelm@26602
    87
  val error_msg: transition -> exn * string -> unit
wenzelm@28103
    88
  val add_hook: (transition -> state -> state -> unit) -> unit
wenzelm@26602
    89
  val transition: bool -> transition -> state -> (state * (exn * string) option) option
wenzelm@28425
    90
  val commit_exit: Position.T -> transition
wenzelm@28425
    91
  val command: transition -> state -> state
wenzelm@29516
    92
  val run_command: string -> transition -> state -> state option
wenzelm@29427
    93
  val excursion: (transition * transition list) list -> (transition * state) list lazy
wenzelm@5828
    94
end;
wenzelm@5828
    95
wenzelm@6965
    96
structure Toplevel: TOPLEVEL =
wenzelm@5828
    97
struct
wenzelm@5828
    98
wenzelm@5828
    99
(** toplevel state **)
wenzelm@5828
   100
wenzelm@19063
   101
exception UNDEF;
wenzelm@19063
   102
wenzelm@19063
   103
wenzelm@21294
   104
(* local theory wrappers *)
wenzelm@5828
   105
wenzelm@20963
   106
type generic_theory = Context.generic;    (*theory or local_theory*)
wenzelm@20963
   107
wenzelm@25292
   108
val loc_init = TheoryTarget.context;
wenzelm@28394
   109
val loc_exit = LocalTheory.exit_global;
wenzelm@21294
   110
wenzelm@25292
   111
fun loc_begin loc (Context.Theory thy) = loc_init (the_default "-" loc) thy
wenzelm@21294
   112
  | loc_begin NONE (Context.Proof lthy) = lthy
haftmann@25269
   113
  | loc_begin (SOME loc) (Context.Proof lthy) = loc_init loc (loc_exit lthy);
wenzelm@21294
   114
wenzelm@21294
   115
fun loc_finish _ (Context.Theory _) = Context.Theory o loc_exit
wenzelm@21294
   116
  | loc_finish NONE (Context.Proof _) = Context.Proof o LocalTheory.restore
wenzelm@25292
   117
  | loc_finish (SOME _) (Context.Proof lthy) = fn lthy' =>
wenzelm@25292
   118
      Context.Proof (LocalTheory.reinit (LocalTheory.raw_theory (K (loc_exit lthy')) lthy));
wenzelm@21294
   119
wenzelm@21294
   120
wenzelm@21958
   121
(* datatype node *)
wenzelm@21294
   122
wenzelm@5828
   123
datatype node =
wenzelm@27576
   124
  Theory of generic_theory * Proof.context option
wenzelm@27576
   125
    (*theory with presentation context*) |
wenzelm@27576
   126
  Proof of ProofNode.T * ((Proof.context -> generic_theory) * generic_theory)
wenzelm@27576
   127
    (*proof node, finish, original theory*) |
wenzelm@27564
   128
  SkipProof of int * (generic_theory * generic_theory);
wenzelm@27564
   129
    (*proof depth, resulting theory, original theory*)
wenzelm@5828
   130
wenzelm@22056
   131
val the_global_theory = fn Theory (Context.Theory thy, _) => thy | _ => raise UNDEF;
wenzelm@20963
   132
val theory_node = fn Theory (gthy, _) => SOME gthy | _ => NONE;
wenzelm@18589
   133
val proof_node = fn Proof (prf, _) => SOME prf | _ => NONE;
wenzelm@18589
   134
wenzelm@20963
   135
fun cases_node f _ (Theory (gthy, _)) = f gthy
wenzelm@27564
   136
  | cases_node _ g (Proof (prf, _)) = g (ProofNode.current prf)
wenzelm@21007
   137
  | cases_node f _ (SkipProof (_, (gthy, _))) = f gthy;
wenzelm@19063
   138
wenzelm@29066
   139
val context_node = cases_node Context.proof_of Proof.context_of;
wenzelm@29066
   140
wenzelm@21958
   141
wenzelm@21958
   142
(* datatype state *)
wenzelm@21958
   143
wenzelm@27601
   144
type node_info = node * (theory -> unit);  (*node with exit operation*)
wenzelm@27601
   145
datatype state = State of node_info option * node_info option;  (*current, previous*)
wenzelm@5828
   146
wenzelm@27576
   147
val toplevel = State (NONE, NONE);
wenzelm@5828
   148
wenzelm@27576
   149
fun is_toplevel (State (NONE, _)) = true
wenzelm@7732
   150
  | is_toplevel _ = false;
wenzelm@7732
   151
wenzelm@27576
   152
fun level (State (NONE, _)) = 0
wenzelm@27576
   153
  | level (State (SOME (Theory _, _), _)) = 0
wenzelm@27576
   154
  | level (State (SOME (Proof (prf, _), _), _)) = Proof.level (ProofNode.current prf)
wenzelm@27576
   155
  | level (State (SOME (SkipProof (d, _), _), _)) = d + 1;   (*different notion of proof depth!*)
wenzelm@17076
   156
wenzelm@27576
   157
fun str_of_state (State (NONE, _)) = "at top level"
wenzelm@27576
   158
  | str_of_state (State (SOME (Theory (Context.Theory _, _), _), _)) = "in theory mode"
wenzelm@27576
   159
  | str_of_state (State (SOME (Theory (Context.Proof _, _), _), _)) = "in local theory mode"
wenzelm@27576
   160
  | str_of_state (State (SOME (Proof _, _), _)) = "in proof mode"
wenzelm@27576
   161
  | str_of_state (State (SOME (SkipProof _, _), _)) = "in skipped proof mode";
wenzelm@5946
   162
wenzelm@5946
   163
wenzelm@27576
   164
(* current node *)
wenzelm@5828
   165
wenzelm@27576
   166
fun node_of (State (NONE, _)) = raise UNDEF
wenzelm@27576
   167
  | node_of (State (SOME (node, _), _)) = node;
wenzelm@5828
   168
wenzelm@18589
   169
fun is_theory state = not (is_toplevel state) andalso is_some (theory_node (node_of state));
wenzelm@18589
   170
fun is_proof state = not (is_toplevel state) andalso is_some (proof_node (node_of state));
wenzelm@18589
   171
wenzelm@19063
   172
fun node_case f g state = cases_node f g (node_of state);
wenzelm@5828
   173
wenzelm@30398
   174
fun presentation_context_of state =
wenzelm@30398
   175
  (case try node_of state of
wenzelm@30398
   176
    SOME (Theory (_, SOME ctxt)) => ctxt
wenzelm@30398
   177
  | SOME node => context_node node
wenzelm@30398
   178
  | NONE => raise UNDEF);
wenzelm@30366
   179
wenzelm@30801
   180
fun previous_context_of (State (_, NONE)) = NONE
wenzelm@30801
   181
  | previous_context_of (State (_, SOME (prev, _))) = SOME (context_node prev);
wenzelm@30801
   182
wenzelm@21506
   183
val context_of = node_case Context.proof_of Proof.context_of;
wenzelm@22089
   184
val generic_theory_of = node_case I (Context.Proof o Proof.context_of);
wenzelm@20963
   185
val theory_of = node_case Context.theory_of Proof.theory_of;
wenzelm@18589
   186
val proof_of = node_case (fn _ => raise UNDEF) I;
wenzelm@17208
   187
wenzelm@18589
   188
fun proof_position_of state =
wenzelm@18589
   189
  (case node_of state of
wenzelm@27564
   190
    Proof (prf, _) => ProofNode.position prf
wenzelm@18589
   191
  | _ => raise UNDEF);
wenzelm@6664
   192
wenzelm@21007
   193
val enter_proof_body = node_case (Proof.init o Context.proof_of) Proof.enter_forward;
wenzelm@5828
   194
wenzelm@5828
   195
wenzelm@16815
   196
(* print state *)
wenzelm@16815
   197
wenzelm@25292
   198
val pretty_context = LocalTheory.pretty o Context.cases (TheoryTarget.init NONE) I;
wenzelm@16815
   199
wenzelm@23640
   200
fun print_state_context state =
wenzelm@24795
   201
  (case try node_of state of
wenzelm@21506
   202
    NONE => []
wenzelm@24795
   203
  | SOME (Theory (gthy, _)) => pretty_context gthy
wenzelm@24795
   204
  | SOME (Proof (_, (_, gthy))) => pretty_context gthy
wenzelm@24795
   205
  | SOME (SkipProof (_, (gthy, _))) => pretty_context gthy)
wenzelm@23640
   206
  |> Pretty.chunks |> Pretty.writeln;
wenzelm@16815
   207
wenzelm@23640
   208
fun print_state prf_only state =
wenzelm@23701
   209
  (case try node_of state of
wenzelm@23701
   210
    NONE => []
wenzelm@23701
   211
  | SOME (Theory (gthy, _)) => if prf_only then [] else pretty_context gthy
wenzelm@23701
   212
  | SOME (Proof (prf, _)) =>
wenzelm@27564
   213
      Proof.pretty_state (ProofNode.position prf) (ProofNode.current prf)
wenzelm@27564
   214
  | SOME (SkipProof (d, _)) => [Pretty.str ("skipped proof: depth " ^ string_of_int d)])
wenzelm@23701
   215
  |> Pretty.markup_chunks Markup.state |> Pretty.writeln;
wenzelm@16815
   216
wenzelm@16815
   217
wenzelm@15668
   218
wenzelm@5828
   219
(** toplevel transitions **)
wenzelm@5828
   220
wenzelm@16682
   221
val quiet = ref false;
wenzelm@22135
   222
val debug = Output.debugging;
wenzelm@17321
   223
val interact = ref false;
wenzelm@16682
   224
val timing = Output.timing;
wenzelm@16682
   225
val profiling = ref 0;
wenzelm@16815
   226
val skip_proofs = ref false;
wenzelm@16682
   227
wenzelm@5828
   228
exception TERMINATE;
wenzelm@7022
   229
exception EXCURSION_FAIL of exn * string;
wenzelm@6689
   230
exception FAILURE of state * exn;
wenzelm@24055
   231
exception TOPLEVEL_ERROR;
wenzelm@5828
   232
wenzelm@20128
   233
wenzelm@20128
   234
(* print exceptions *)
wenzelm@20128
   235
wenzelm@26256
   236
exception CONTEXT of Proof.context * exn;
wenzelm@26256
   237
wenzelm@26256
   238
fun exn_context NONE exn = exn
wenzelm@26256
   239
  | exn_context (SOME ctxt) exn = CONTEXT (ctxt, exn);
wenzelm@26256
   240
wenzelm@20128
   241
local
wenzelm@20128
   242
wenzelm@26256
   243
fun if_context NONE _ _ = []
wenzelm@26256
   244
  | if_context (SOME ctxt) f xs = map (f ctxt) xs;
wenzelm@20128
   245
wenzelm@20128
   246
fun raised name [] = "exception " ^ name ^ " raised"
wenzelm@20128
   247
  | raised name [msg] = "exception " ^ name ^ " raised: " ^ msg
wenzelm@20128
   248
  | raised name msgs = cat_lines (("exception " ^ name ^ " raised:") :: msgs);
wenzelm@20128
   249
wenzelm@20128
   250
in
wenzelm@20128
   251
wenzelm@26256
   252
fun exn_message e =
wenzelm@26256
   253
  let
wenzelm@26256
   254
    val detailed = ! debug;
wenzelm@26256
   255
wenzelm@26256
   256
    fun exn_msg _ (CONTEXT (ctxt, exn)) = exn_msg (SOME ctxt) exn
wenzelm@28458
   257
      | exn_msg ctxt (Exn.EXCEPTIONS exns) = cat_lines (map (exn_msg ctxt) exns)
wenzelm@26293
   258
      | exn_msg ctxt (EXCURSION_FAIL (exn, loc)) =
wenzelm@26293
   259
          exn_msg ctxt exn ^ Markup.markup Markup.location ("\n" ^ loc)
wenzelm@26256
   260
      | exn_msg _ TERMINATE = "Exit."
wenzelm@28443
   261
      | exn_msg _ Exn.Interrupt = "Interrupt."
wenzelm@26256
   262
      | exn_msg _ TimeLimit.TimeOut = "Timeout."
wenzelm@26256
   263
      | exn_msg _ TOPLEVEL_ERROR = "Error."
wenzelm@26256
   264
      | exn_msg _ (SYS_ERROR msg) = "## SYSTEM ERROR ##\n" ^ msg
wenzelm@26256
   265
      | exn_msg _ (ERROR msg) = msg
wenzelm@26256
   266
      | exn_msg _ (Fail msg) = raised "Fail" [msg]
wenzelm@26256
   267
      | exn_msg _ (THEORY (msg, thys)) =
wenzelm@26256
   268
          raised "THEORY" (msg :: (if detailed then map Context.str_of_thy thys else []))
wenzelm@26256
   269
      | exn_msg _ (Syntax.AST (msg, asts)) = raised "AST" (msg ::
wenzelm@26256
   270
            (if detailed then map (Pretty.string_of o Syntax.pretty_ast) asts else []))
wenzelm@26256
   271
      | exn_msg ctxt (TYPE (msg, Ts, ts)) = raised "TYPE" (msg ::
wenzelm@26256
   272
            (if detailed then
wenzelm@26256
   273
              if_context ctxt Syntax.string_of_typ Ts @ if_context ctxt Syntax.string_of_term ts
wenzelm@26256
   274
             else []))
wenzelm@26256
   275
      | exn_msg ctxt (TERM (msg, ts)) = raised "TERM" (msg ::
wenzelm@26256
   276
            (if detailed then if_context ctxt Syntax.string_of_term ts else []))
wenzelm@26256
   277
      | exn_msg ctxt (THM (msg, i, thms)) = raised ("THM " ^ string_of_int i) (msg ::
wenzelm@26256
   278
            (if detailed then if_context ctxt ProofContext.string_of_thm thms else []))
wenzelm@26256
   279
      | exn_msg _ exn = raised (General.exnMessage exn) []
wenzelm@26256
   280
  in exn_msg NONE e end;
wenzelm@26256
   281
wenzelm@20128
   282
end;
wenzelm@20128
   283
wenzelm@20128
   284
wenzelm@20128
   285
(* controlled execution *)
wenzelm@20128
   286
wenzelm@20128
   287
local
wenzelm@20128
   288
wenzelm@18685
   289
fun debugging f x =
wenzelm@30422
   290
  if ! debug then
wenzelm@30460
   291
    Exn.release (exception_trace (fn () =>
wenzelm@30460
   292
      Exn.Result (f x) handle
wenzelm@30460
   293
        exn as UNDEF => Exn.Exn exn
wenzelm@30460
   294
      | exn as EXCURSION_FAIL _ => Exn.Exn exn))
wenzelm@18685
   295
  else f x;
wenzelm@18685
   296
wenzelm@26256
   297
fun toplevel_error f x =
wenzelm@26256
   298
  let val ctxt = try ML_Context.the_local_context () in
wenzelm@26256
   299
    f x handle exn =>
wenzelm@26256
   300
      (Output.error_msg (exn_message (exn_context ctxt exn)); raise TOPLEVEL_ERROR)
wenzelm@26256
   301
  end;
wenzelm@24055
   302
wenzelm@20128
   303
in
wenzelm@20128
   304
wenzelm@20128
   305
fun controlled_execution f =
wenzelm@20128
   306
  f
wenzelm@20128
   307
  |> debugging
wenzelm@30618
   308
  |> Future.interruptible_task;
wenzelm@20128
   309
wenzelm@20128
   310
fun program f =
wenzelm@24055
   311
 (f
wenzelm@28463
   312
  |> controlled_execution
wenzelm@24055
   313
  |> toplevel_error) ();
wenzelm@20128
   314
wenzelm@20128
   315
end;
wenzelm@20128
   316
wenzelm@5828
   317
wenzelm@27601
   318
(* node transactions -- maintaining stable checkpoints *)
wenzelm@7022
   319
wenzelm@6689
   320
local
wenzelm@6689
   321
wenzelm@30366
   322
fun reset_presentation (Theory (gthy, _)) = Theory (gthy, NONE)
wenzelm@30366
   323
  | reset_presentation node = node;
wenzelm@30366
   324
wenzelm@26624
   325
fun is_draft_theory (Theory (gthy, _)) = Context.is_draft (Context.theory_of gthy)
wenzelm@26624
   326
  | is_draft_theory _ = false;
wenzelm@26624
   327
wenzelm@27601
   328
fun is_stale state = Context.is_stale (theory_of state) handle UNDEF => false;
wenzelm@27601
   329
wenzelm@26624
   330
fun stale_error NONE = SOME (ERROR "Stale theory encountered after successful execution!")
wenzelm@26624
   331
  | stale_error some = some;
wenzelm@16815
   332
wenzelm@27576
   333
fun map_theory f (Theory (gthy, ctxt)) =
wenzelm@27576
   334
      Theory (Context.mapping f (LocalTheory.raw_theory f) gthy, ctxt)
wenzelm@27576
   335
  | map_theory _ node = node;
wenzelm@6689
   336
wenzelm@6689
   337
in
wenzelm@6689
   338
wenzelm@30366
   339
fun apply_transaction pos f g (node, exit) =
wenzelm@20128
   340
  let
wenzelm@27576
   341
    val _ = is_draft_theory node andalso error "Illegal draft theory in toplevel state";
wenzelm@27576
   342
    val cont_node = reset_presentation node;
wenzelm@27576
   343
    val back_node = map_theory (Theory.checkpoint o Theory.copy) cont_node;
wenzelm@27601
   344
    fun state_error e nd = (State (SOME (nd, exit), SOME (node, exit)), e);
wenzelm@6689
   345
wenzelm@20128
   346
    val (result, err) =
wenzelm@20128
   347
      cont_node
wenzelm@27576
   348
      |> controlled_execution f
wenzelm@26624
   349
      |> map_theory Theory.checkpoint
wenzelm@26624
   350
      |> state_error NONE
wenzelm@26624
   351
      handle exn => state_error (SOME exn) cont_node;
wenzelm@26624
   352
wenzelm@26624
   353
    val (result', err') =
wenzelm@26624
   354
      if is_stale result then state_error (stale_error err) back_node
wenzelm@26624
   355
      else (result, err);
wenzelm@20128
   356
  in
wenzelm@26624
   357
    (case err' of
wenzelm@30366
   358
      NONE => tap g result'
wenzelm@26624
   359
    | SOME exn => raise FAILURE (result', exn))
wenzelm@20128
   360
  end;
wenzelm@6689
   361
wenzelm@6689
   362
end;
wenzelm@6689
   363
wenzelm@6689
   364
wenzelm@6689
   365
(* primitive transitions *)
wenzelm@6689
   366
wenzelm@5828
   367
datatype trans =
wenzelm@27576
   368
  Init of string * (bool -> theory) * (theory -> unit) | (*theory name, init, exit*)
wenzelm@27576
   369
  Exit |                                         (*formal exit of theory -- without committing*)
wenzelm@27576
   370
  CommitExit |                                   (*exit and commit final theory*)
wenzelm@27576
   371
  Keep of bool -> state -> unit |                (*peek at state*)
wenzelm@30366
   372
  Transaction of (bool -> node -> node) * (state -> unit);  (*node transaction and presentation*)
wenzelm@21958
   373
wenzelm@6689
   374
local
wenzelm@5828
   375
wenzelm@27576
   376
fun apply_tr int _ (Init (_, f, exit)) (State (NONE, _)) =
wenzelm@27576
   377
      State (SOME (Theory (Context.Theory (Theory.checkpoint (f int)), NONE), exit), NONE)
wenzelm@27603
   378
  | apply_tr _ _ Exit (State (prev as SOME (Theory (Context.Theory _, _), _), _)) =
wenzelm@27603
   379
      State (NONE, prev)
wenzelm@27601
   380
  | apply_tr _ _ CommitExit (State (NONE, SOME (Theory (Context.Theory thy, _), exit))) =
wenzelm@27576
   381
      (controlled_execution exit thy; toplevel)
wenzelm@27576
   382
  | apply_tr int _ (Keep f) state =
wenzelm@27576
   383
      controlled_execution (fn x => tap (f int) x) state
wenzelm@30366
   384
  | apply_tr int pos (Transaction (f, g)) (State (SOME state, _)) =
wenzelm@30366
   385
      apply_transaction pos (fn x => f int x) g state
wenzelm@23363
   386
  | apply_tr _ _ _ _ = raise UNDEF;
wenzelm@5828
   387
wenzelm@23363
   388
fun apply_union _ _ [] state = raise FAILURE (state, UNDEF)
wenzelm@23363
   389
  | apply_union int pos (tr :: trs) state =
wenzelm@28451
   390
      apply_union int pos trs state
wenzelm@28451
   391
        handle UNDEF => apply_tr int pos tr state
wenzelm@28451
   392
          | FAILURE (alt_state, UNDEF) => apply_tr int pos tr alt_state
wenzelm@6689
   393
          | exn as FAILURE _ => raise exn
wenzelm@6689
   394
          | exn => raise FAILURE (state, exn);
wenzelm@6689
   395
wenzelm@6689
   396
in
wenzelm@6689
   397
wenzelm@23363
   398
fun apply_trans int pos trs state = (apply_union int pos trs state, NONE)
skalberg@15531
   399
  handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn);
wenzelm@6689
   400
wenzelm@6689
   401
end;
wenzelm@5828
   402
wenzelm@5828
   403
wenzelm@5828
   404
(* datatype transition *)
wenzelm@5828
   405
wenzelm@5828
   406
datatype transition = Transition of
wenzelm@26621
   407
 {name: string,              (*command name*)
wenzelm@26621
   408
  pos: Position.T,           (*source position*)
wenzelm@26621
   409
  int_only: bool,            (*interactive-only*)
wenzelm@26621
   410
  print: bool,               (*print result state*)
wenzelm@26621
   411
  no_timing: bool,           (*suppress timing*)
wenzelm@26621
   412
  trans: trans list};        (*primitive transitions (union)*)
wenzelm@5828
   413
wenzelm@26621
   414
fun make_transition (name, pos, int_only, print, no_timing, trans) =
wenzelm@26621
   415
  Transition {name = name, pos = pos, int_only = int_only, print = print, no_timing = no_timing,
wenzelm@26621
   416
    trans = trans};
wenzelm@5828
   417
wenzelm@26621
   418
fun map_transition f (Transition {name, pos, int_only, print, no_timing, trans}) =
wenzelm@26621
   419
  make_transition (f (name, pos, int_only, print, no_timing, trans));
wenzelm@5828
   420
wenzelm@27441
   421
val empty = make_transition ("", Position.none, false, false, false, []);
wenzelm@5828
   422
wenzelm@5828
   423
wenzelm@5828
   424
(* diagnostics *)
wenzelm@5828
   425
wenzelm@27441
   426
fun init_of (Transition {trans = [Init (name, _, _)], ...}) = SOME name
wenzelm@27441
   427
  | init_of _ = NONE;
wenzelm@27441
   428
wenzelm@27427
   429
fun name_of (Transition {name, ...}) = name;
wenzelm@28105
   430
fun pos_of (Transition {pos, ...}) = pos;
wenzelm@28105
   431
fun str_of tr = quote (name_of tr) ^ Position.str_of (pos_of tr);
wenzelm@5828
   432
wenzelm@27427
   433
fun command_msg msg tr = msg ^ "command " ^ str_of tr;
wenzelm@5828
   434
fun at_command tr = command_msg "At " tr ^ ".";
wenzelm@5828
   435
wenzelm@5828
   436
fun type_error tr state =
wenzelm@18685
   437
  ERROR (command_msg "Illegal application of " tr ^ " " ^ str_of_state state);
wenzelm@5828
   438
wenzelm@5828
   439
wenzelm@5828
   440
(* modify transitions *)
wenzelm@5828
   441
wenzelm@28451
   442
fun name name = map_transition (fn (_, pos, int_only, print, no_timing, trans) =>
wenzelm@28451
   443
  (name, pos, int_only, print, no_timing, trans));
wenzelm@9010
   444
wenzelm@26621
   445
fun position pos = map_transition (fn (name, _, int_only, print, no_timing, trans) =>
wenzelm@26621
   446
  (name, pos, int_only, print, no_timing, trans));
wenzelm@5828
   447
wenzelm@26621
   448
fun interactive int_only = map_transition (fn (name, pos, _, print, no_timing, trans) =>
wenzelm@26621
   449
  (name, pos, int_only, print, no_timing, trans));
wenzelm@14923
   450
wenzelm@26621
   451
val no_timing = map_transition (fn (name, pos, int_only, print, _, trans) =>
wenzelm@26621
   452
  (name, pos, int_only, print, true, trans));
wenzelm@17363
   453
wenzelm@26621
   454
fun add_trans tr = map_transition (fn (name, pos, int_only, print, no_timing, trans) =>
wenzelm@28451
   455
  (name, pos, int_only, print, no_timing, tr :: trans));
wenzelm@16607
   456
wenzelm@28433
   457
val reset_trans = map_transition (fn (name, pos, int_only, print, no_timing, _) =>
wenzelm@28433
   458
  (name, pos, int_only, print, no_timing, []));
wenzelm@28433
   459
wenzelm@28453
   460
fun set_print print = map_transition (fn (name, pos, int_only, _, no_timing, trans) =>
wenzelm@28453
   461
  (name, pos, int_only, print, no_timing, trans));
wenzelm@28453
   462
wenzelm@28453
   463
val print = set_print true;
wenzelm@5828
   464
wenzelm@5828
   465
wenzelm@21007
   466
(* basic transitions *)
wenzelm@5828
   467
wenzelm@27576
   468
fun init_theory name f exit = add_trans (Init (name, f, exit));
wenzelm@6689
   469
val exit = add_trans Exit;
wenzelm@7612
   470
val keep' = add_trans o Keep;
wenzelm@30366
   471
wenzelm@30366
   472
fun present_transaction f g = add_trans (Transaction (f, g));
wenzelm@30366
   473
fun transaction f = present_transaction f (K ());
wenzelm@5828
   474
wenzelm@7612
   475
fun keep f = add_trans (Keep (fn _ => f));
wenzelm@5828
   476
fun imperative f = keep (fn _ => f ());
wenzelm@5828
   477
wenzelm@27840
   478
fun ignored pos = empty |> name "<ignored>" |> position pos |> imperative I;
wenzelm@27840
   479
fun malformed pos msg =
wenzelm@27840
   480
  empty |> name "<malformed>" |> position pos |> imperative (fn () => error msg);
wenzelm@27840
   481
wenzelm@21007
   482
val unknown_theory = imperative (fn () => warning "Unknown theory context");
wenzelm@21007
   483
val unknown_proof = imperative (fn () => warning "Unknown proof context");
wenzelm@21007
   484
val unknown_context = imperative (fn () => warning "Unknown context");
wenzelm@15668
   485
wenzelm@21007
   486
wenzelm@21007
   487
(* theory transitions *)
wenzelm@15668
   488
wenzelm@27601
   489
fun generic_theory f = transaction (fn _ =>
wenzelm@26491
   490
  (fn Theory (gthy, _) => Theory (f gthy, NONE)
wenzelm@26491
   491
    | _ => raise UNDEF));
wenzelm@26491
   492
wenzelm@27601
   493
fun theory' f = transaction (fn int =>
wenzelm@20963
   494
  (fn Theory (Context.Theory thy, _) => Theory (Context.Theory (f int thy), NONE)
wenzelm@20963
   495
    | _ => raise UNDEF));
wenzelm@20963
   496
wenzelm@20963
   497
fun theory f = theory' (K f);
wenzelm@20963
   498
wenzelm@27601
   499
fun begin_local_theory begin f = transaction (fn _ =>
wenzelm@20963
   500
  (fn Theory (Context.Theory thy, _) =>
wenzelm@20963
   501
        let
wenzelm@20985
   502
          val lthy = f thy;
wenzelm@21294
   503
          val gthy = if begin then Context.Proof lthy else Context.Theory (loc_exit lthy);
wenzelm@21294
   504
        in Theory (gthy, SOME lthy) end
wenzelm@20963
   505
    | _ => raise UNDEF));
wenzelm@17076
   506
wenzelm@27601
   507
val end_local_theory = transaction (fn _ =>
wenzelm@21294
   508
  (fn Theory (Context.Proof lthy, _) => Theory (Context.Theory (loc_exit lthy), SOME lthy)
wenzelm@21007
   509
    | _ => raise UNDEF));
wenzelm@21007
   510
wenzelm@21007
   511
local
wenzelm@21007
   512
wenzelm@30366
   513
fun local_theory_presentation loc f = present_transaction (fn int =>
wenzelm@21294
   514
  (fn Theory (gthy, _) =>
wenzelm@21294
   515
        let
wenzelm@21294
   516
          val finish = loc_finish loc gthy;
wenzelm@29380
   517
          val lthy' = f int (loc_begin loc gthy);
wenzelm@21294
   518
        in Theory (finish lthy', SOME lthy') end
wenzelm@30366
   519
    | _ => raise UNDEF));
wenzelm@15668
   520
wenzelm@21007
   521
in
wenzelm@21007
   522
wenzelm@30366
   523
fun local_theory' loc f = local_theory_presentation loc f (K ());
wenzelm@29380
   524
fun local_theory loc f = local_theory' loc (K f);
wenzelm@30366
   525
fun present_local_theory loc = local_theory_presentation loc (K I);
wenzelm@18955
   526
wenzelm@21007
   527
end;
wenzelm@21007
   528
wenzelm@21007
   529
wenzelm@21007
   530
(* proof transitions *)
wenzelm@21007
   531
wenzelm@27601
   532
fun end_proof f = transaction (fn int =>
wenzelm@24795
   533
  (fn Proof (prf, (finish, _)) =>
wenzelm@27564
   534
        let val state = ProofNode.current prf in
wenzelm@21007
   535
          if can (Proof.assert_bottom true) state then
wenzelm@21007
   536
            let
wenzelm@21007
   537
              val ctxt' = f int state;
wenzelm@21007
   538
              val gthy' = finish ctxt';
wenzelm@21007
   539
            in Theory (gthy', SOME ctxt') end
wenzelm@21007
   540
          else raise UNDEF
wenzelm@21007
   541
        end
wenzelm@27564
   542
    | SkipProof (0, (gthy, _)) => Theory (gthy, NONE)
wenzelm@21007
   543
    | _ => raise UNDEF));
wenzelm@21007
   544
wenzelm@21294
   545
local
wenzelm@21294
   546
wenzelm@27601
   547
fun begin_proof init finish = transaction (fn int =>
wenzelm@21294
   548
  (fn Theory (gthy, _) =>
wenzelm@21294
   549
    let
berghofe@24453
   550
      val prf = init int gthy;
wenzelm@28433
   551
      val skip = ! skip_proofs;
wenzelm@21294
   552
      val schematic = Proof.schematic_goal prf;
wenzelm@21294
   553
    in
wenzelm@28433
   554
      if skip andalso schematic then
wenzelm@21294
   555
        warning "Cannot skip proof of schematic goal statement"
wenzelm@21294
   556
      else ();
wenzelm@28433
   557
      if skip andalso not schematic then
wenzelm@27564
   558
        SkipProof (0, (finish gthy (Proof.global_skip_proof int prf), gthy))
wenzelm@27564
   559
      else Proof (ProofNode.init prf, (finish gthy, gthy))
wenzelm@21294
   560
    end
wenzelm@21294
   561
  | _ => raise UNDEF));
wenzelm@21294
   562
wenzelm@21294
   563
in
wenzelm@21294
   564
wenzelm@24780
   565
fun local_theory_to_proof' loc f = begin_proof
wenzelm@25960
   566
  (fn int => fn gthy => f int (loc_begin loc gthy))
wenzelm@24780
   567
  (loc_finish loc);
wenzelm@24780
   568
berghofe@24453
   569
fun local_theory_to_proof loc f = local_theory_to_proof' loc (K f);
wenzelm@21294
   570
wenzelm@21294
   571
fun theory_to_proof f = begin_proof
wenzelm@24780
   572
  (K (fn Context.Theory thy => f thy | _ => raise UNDEF))
wenzelm@24780
   573
  (K (Context.Theory o ProofContext.theory_of));
wenzelm@21294
   574
wenzelm@21294
   575
end;
wenzelm@21294
   576
wenzelm@27601
   577
val forget_proof = transaction (fn _ =>
wenzelm@21007
   578
  (fn Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
wenzelm@21007
   579
    | SkipProof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
wenzelm@21007
   580
    | _ => raise UNDEF));
wenzelm@21007
   581
wenzelm@30366
   582
val present_proof = present_transaction (fn _ =>
wenzelm@27564
   583
  (fn Proof (prf, x) => Proof (ProofNode.apply I prf, x)
wenzelm@27564
   584
    | skip as SkipProof _ => skip
wenzelm@30366
   585
    | _ => raise UNDEF));
wenzelm@21177
   586
wenzelm@27601
   587
fun proofs' f = transaction (fn int =>
wenzelm@27564
   588
  (fn Proof (prf, x) => Proof (ProofNode.applys (f int) prf, x)
wenzelm@27564
   589
    | skip as SkipProof _ => skip
wenzelm@16815
   590
    | _ => raise UNDEF));
wenzelm@15668
   591
wenzelm@17904
   592
fun proof' f = proofs' (Seq.single oo f);
wenzelm@17904
   593
val proofs = proofs' o K;
wenzelm@6689
   594
val proof = proof' o K;
wenzelm@16815
   595
wenzelm@27601
   596
fun actual_proof f = transaction (fn _ =>
wenzelm@21007
   597
  (fn Proof (prf, x) => Proof (f prf, x)
wenzelm@20963
   598
    | _ => raise UNDEF));
wenzelm@16815
   599
wenzelm@27601
   600
fun skip_proof f = transaction (fn _ =>
wenzelm@21007
   601
  (fn SkipProof (h, x) => SkipProof (f h, x)
wenzelm@18563
   602
    | _ => raise UNDEF));
wenzelm@18563
   603
wenzelm@27601
   604
fun skip_proof_to_theory pred = transaction (fn _ =>
wenzelm@27564
   605
  (fn SkipProof (d, (gthy, _)) => if pred d then Theory (gthy, NONE) else raise UNDEF
wenzelm@17904
   606
  | _ => raise UNDEF));
wenzelm@5828
   607
wenzelm@5828
   608
wenzelm@5828
   609
wenzelm@5828
   610
(** toplevel transactions **)
wenzelm@5828
   611
wenzelm@27427
   612
(* identification *)
wenzelm@27427
   613
wenzelm@27427
   614
fun get_id (Transition {pos, ...}) = Position.get_id pos;
wenzelm@27427
   615
fun put_id id (tr as Transition {pos, ...}) = position (Position.put_id id pos) tr;
wenzelm@27427
   616
wenzelm@27427
   617
wenzelm@25960
   618
(* thread position *)
wenzelm@25799
   619
wenzelm@25960
   620
fun setmp_thread_position (Transition {pos, ...}) f x =
wenzelm@25819
   621
  Position.setmp_thread_data pos f x;
wenzelm@25799
   622
wenzelm@27606
   623
fun status tr m =
wenzelm@27606
   624
  setmp_thread_position tr (fn () => Output.status (Markup.markup m "")) ();
wenzelm@27606
   625
wenzelm@26602
   626
fun error_msg tr exn_info =
wenzelm@26602
   627
  setmp_thread_position tr (fn () => Output.error_msg (exn_message (EXCURSION_FAIL exn_info))) ();
wenzelm@26602
   628
wenzelm@25799
   629
wenzelm@28095
   630
(* post-transition hooks *)
wenzelm@28095
   631
wenzelm@28103
   632
local val hooks = ref ([]: (transition -> state -> state -> unit) list) in
wenzelm@28095
   633
wenzelm@28095
   634
fun add_hook f = CRITICAL (fn () => change hooks (cons f));
wenzelm@28095
   635
fun get_hooks () = CRITICAL (fn () => ! hooks);
wenzelm@28095
   636
wenzelm@28095
   637
end;
wenzelm@28095
   638
wenzelm@28095
   639
wenzelm@5828
   640
(* apply transitions *)
wenzelm@5828
   641
wenzelm@6664
   642
local
wenzelm@6664
   643
wenzelm@30571
   644
fun app int (tr as Transition {trans, pos, print, no_timing, ...}) =
wenzelm@25819
   645
  setmp_thread_position tr (fn state =>
wenzelm@25799
   646
    let
wenzelm@25799
   647
      fun do_timing f x = (warning (command_msg "" tr); timeap f x);
wenzelm@25799
   648
      fun do_profiling f x = profile (! profiling) f x;
wenzelm@25799
   649
wenzelm@26256
   650
      val (result, status) =
wenzelm@25799
   651
         state |> (apply_trans int pos trans
wenzelm@25799
   652
          |> (if ! profiling > 0 andalso not no_timing then do_profiling else I)
wenzelm@25799
   653
          |> (if ! profiling > 0 orelse ! timing andalso not no_timing then do_timing else I));
wenzelm@26256
   654
wenzelm@26621
   655
      val _ = if int andalso not (! quiet) andalso print then print_state false result else ();
wenzelm@26256
   656
    in (result, Option.map (fn UNDEF => type_error tr state | exn => exn) status) end);
wenzelm@6664
   657
wenzelm@6664
   658
in
wenzelm@5828
   659
wenzelm@26602
   660
fun transition int tr st =
wenzelm@28095
   661
  let
wenzelm@28095
   662
    val hooks = get_hooks ();
wenzelm@28103
   663
    fun apply_hooks st' = hooks |> List.app (fn f => (try (fn () => f tr st st') (); ()));
wenzelm@28095
   664
wenzelm@28095
   665
    val ctxt = try context_of st;
wenzelm@28095
   666
    val res =
wenzelm@28095
   667
      (case app int tr st of
wenzelm@28095
   668
        (_, SOME TERMINATE) => NONE
wenzelm@28103
   669
      | (st', SOME (EXCURSION_FAIL exn_info)) => SOME (st', SOME exn_info)
wenzelm@28103
   670
      | (st', SOME exn) => SOME (st', SOME (exn_context ctxt exn, at_command tr))
wenzelm@28103
   671
      | (st', NONE) => SOME (st', NONE));
wenzelm@28103
   672
    val _ = (case res of SOME (st', NONE) => apply_hooks st' | _ => ());
wenzelm@28095
   673
  in res end;
wenzelm@6664
   674
wenzelm@6664
   675
end;
wenzelm@5828
   676
wenzelm@5828
   677
wenzelm@27601
   678
(* commit final exit *)
wenzelm@27601
   679
wenzelm@28425
   680
fun commit_exit pos =
wenzelm@27601
   681
  name "end" empty
wenzelm@28425
   682
  |> position pos
wenzelm@27601
   683
  |> add_trans CommitExit
wenzelm@27601
   684
  |> imperative (fn () => warning "Expected to find finished theory");
wenzelm@27601
   685
wenzelm@27601
   686
wenzelm@28425
   687
(* nested commands *)
wenzelm@5828
   688
wenzelm@28425
   689
fun command tr st =
wenzelm@27576
   690
  (case transition (! interact) tr st of
wenzelm@28425
   691
    SOME (st', NONE) => st'
wenzelm@28425
   692
  | SOME (_, SOME exn_info) => raise EXCURSION_FAIL exn_info
wenzelm@27576
   693
  | NONE => raise EXCURSION_FAIL (TERMINATE, at_command tr));
wenzelm@27576
   694
wenzelm@29483
   695
fun command_result tr st =
wenzelm@29483
   696
  let val st' = command tr st
wenzelm@29483
   697
  in (st', st') end;
wenzelm@29483
   698
wenzelm@29516
   699
fun run_command thy_name tr st =
wenzelm@29516
   700
  (case
wenzelm@29516
   701
      (case init_of tr of
wenzelm@29516
   702
        SOME name => Exn.capture (fn () => ThyLoad.check_name thy_name name) ()
wenzelm@29516
   703
      | NONE => Exn.Result ()) of
wenzelm@29516
   704
    Exn.Result () =>
wenzelm@29516
   705
      (case transition true tr st of
wenzelm@29516
   706
        SOME (st', NONE) => (status tr Markup.finished; SOME st')
wenzelm@29516
   707
      | SOME (_, SOME exn_info) => (error_msg tr exn_info; status tr Markup.failed; NONE)
wenzelm@29516
   708
      | NONE => (error_msg tr (TERMINATE, at_command tr); status tr Markup.failed; NONE))
wenzelm@29516
   709
  | Exn.Exn exn => (error_msg tr (exn, at_command tr); status tr Markup.failed; NONE));
wenzelm@29483
   710
wenzelm@28433
   711
wenzelm@28433
   712
(* excursion of units, consisting of commands with proof *)
wenzelm@28433
   713
wenzelm@28974
   714
structure States = ProofDataFun
wenzelm@28974
   715
(
wenzelm@28974
   716
  type T = state list future option;
wenzelm@28974
   717
  fun init _ = NONE;
wenzelm@28974
   718
);
wenzelm@28974
   719
wenzelm@28974
   720
fun proof_result immediate (tr, proof_trs) st =
wenzelm@28433
   721
  let val st' = command tr st in
wenzelm@28453
   722
    if immediate orelse null proof_trs orelse
wenzelm@28453
   723
      not (can proof_of st') orelse Proof.is_relevant (proof_of st')
wenzelm@28433
   724
    then
wenzelm@28453
   725
      let val (states, st'') = fold_map command_result proof_trs st'
wenzelm@28974
   726
      in (Lazy.value ((tr, st') :: (proof_trs ~~ states)), st'') end
wenzelm@28453
   727
    else
wenzelm@28433
   728
      let
wenzelm@28433
   729
        val (body_trs, end_tr) = split_last proof_trs;
wenzelm@28453
   730
        val finish = Context.Theory o ProofContext.theory_of;
wenzelm@28974
   731
wenzelm@29386
   732
        val future_proof = Proof.global_future_proof
wenzelm@28974
   733
          (fn prf =>
wenzelm@29343
   734
            Future.fork_pri ~1 (fn () =>
wenzelm@30366
   735
              let val (states, result_state) =
wenzelm@28974
   736
                (case st' of State (SOME (Proof (_, (_, orig_gthy)), exit), prev)
wenzelm@28974
   737
                  => State (SOME (Proof (ProofNode.init prf, (finish, orig_gthy)), exit), prev))
wenzelm@28974
   738
                |> fold_map command_result body_trs
wenzelm@28974
   739
                ||> command (end_tr |> set_print false);
wenzelm@30398
   740
              in (states, presentation_context_of result_state) end))
wenzelm@28974
   741
          #> (fn (states, ctxt) => States.put (SOME states) ctxt);
wenzelm@28974
   742
wenzelm@28974
   743
        val st'' = st' |> command (end_tr |> reset_trans |> end_proof (K future_proof));
wenzelm@28974
   744
wenzelm@28974
   745
        val states =
wenzelm@30398
   746
          (case States.get (presentation_context_of st'') of
wenzelm@28974
   747
            NONE => sys_error ("No future states for " ^ name_of tr ^ Position.str_of (pos_of tr))
wenzelm@28974
   748
          | SOME states => states);
wenzelm@28974
   749
        val result = Lazy.lazy
wenzelm@28974
   750
          (fn () => (tr, st') :: (body_trs ~~ Future.join states) @ [(end_tr, st'')]);
wenzelm@28974
   751
wenzelm@28974
   752
      in (result, st'') end
wenzelm@28433
   753
  end;
wenzelm@28433
   754
wenzelm@29068
   755
fun excursion input =
wenzelm@28425
   756
  let
wenzelm@28433
   757
    val end_pos = if null input then error "No input" else pos_of (fst (List.last input));
wenzelm@28433
   758
wenzelm@29448
   759
    val immediate = not (Goal.future_enabled ());
wenzelm@29427
   760
    val (results, end_state) = fold_map (proof_result immediate) input toplevel;
wenzelm@28433
   761
    val _ =
wenzelm@28433
   762
      (case end_state of
wenzelm@29427
   763
        State (NONE, SOME (Theory (Context.Theory _, _), _)) =>
wenzelm@29427
   764
          command (commit_exit end_pos) end_state
wenzelm@28433
   765
      | _ => error "Unfinished development at end of input");
wenzelm@29427
   766
  in Lazy.lazy (fn () => maps Lazy.force results) end;
wenzelm@7062
   767
wenzelm@6664
   768
end;