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