src/Pure/Isar/toplevel.ML
author wenzelm
Mon May 12 12:01:02 2014 +0200 (2014-05-12 ago)
changeset 56937 d11d11da0d90
parent 56897 c668735fb8b5
child 57183 766e7f50c22f
permissions -rw-r--r--
smarter recovery from toplevel type error;
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@5828
    10
  type state
wenzelm@26602
    11
  val toplevel: state
wenzelm@7732
    12
  val is_toplevel: state -> bool
wenzelm@18589
    13
  val is_theory: state -> bool
wenzelm@18589
    14
  val is_proof: state -> bool
wenzelm@51555
    15
  val is_skipped_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@37953
    24
  val end_theory: Position.T -> state -> theory
wenzelm@56893
    25
  val pretty_context: state -> Pretty.T list
wenzelm@56887
    26
  val pretty_state: state -> Pretty.T list
wenzelm@56887
    27
  val print_state: state -> unit
wenzelm@37858
    28
  val pretty_abstract: state -> Pretty.T
wenzelm@32738
    29
  val quiet: bool Unsynchronized.ref
wenzelm@32738
    30
  val interact: bool Unsynchronized.ref
wenzelm@32738
    31
  val timing: bool Unsynchronized.ref
wenzelm@32738
    32
  val profiling: int Unsynchronized.ref
wenzelm@16682
    33
  type transition
wenzelm@5828
    34
  val empty: transition
wenzelm@27427
    35
  val name_of: transition -> string
wenzelm@28105
    36
  val pos_of: transition -> Position.T
wenzelm@56937
    37
  val type_error: transition -> state -> string
wenzelm@5828
    38
  val name: string -> transition -> transition
wenzelm@5828
    39
  val position: Position.T -> transition -> transition
wenzelm@5828
    40
  val interactive: bool -> transition -> transition
wenzelm@44187
    41
  val init_theory: (unit -> theory) -> transition -> transition
wenzelm@44187
    42
  val is_init: transition -> bool
wenzelm@44186
    43
  val modify_init: (unit -> theory) -> transition -> transition
wenzelm@6689
    44
  val exit: transition -> transition
wenzelm@5828
    45
  val keep: (state -> unit) -> transition -> transition
wenzelm@7612
    46
  val keep': (bool -> state -> unit) -> transition -> transition
wenzelm@5828
    47
  val imperative: (unit -> unit) -> transition -> transition
wenzelm@27840
    48
  val ignored: Position.T -> transition
wenzelm@51268
    49
  val is_ignored: transition -> bool
wenzelm@27840
    50
  val malformed: Position.T -> string -> transition
wenzelm@48772
    51
  val is_malformed: transition -> bool
wenzelm@26491
    52
  val generic_theory: (generic_theory -> generic_theory) -> transition -> transition
wenzelm@7612
    53
  val theory': (bool -> theory -> theory) -> transition -> transition
wenzelm@49012
    54
  val theory: (theory -> theory) -> transition -> transition
wenzelm@20985
    55
  val begin_local_theory: bool -> (theory -> local_theory) -> transition -> transition
wenzelm@21007
    56
  val end_local_theory: transition -> transition
wenzelm@47069
    57
  val open_target: (generic_theory -> local_theory) -> transition -> transition
wenzelm@47069
    58
  val close_target: transition -> transition
wenzelm@45488
    59
  val local_theory': (xstring * Position.T) option -> (bool -> local_theory -> local_theory) ->
wenzelm@45488
    60
    transition -> transition
wenzelm@45488
    61
  val local_theory: (xstring * Position.T) option -> (local_theory -> local_theory) ->
wenzelm@29380
    62
    transition -> transition
wenzelm@45488
    63
  val present_local_theory: (xstring * Position.T) option -> (state -> unit) ->
berghofe@24453
    64
    transition -> transition
wenzelm@45488
    65
  val local_theory_to_proof': (xstring * Position.T) option ->
wenzelm@45488
    66
    (bool -> local_theory -> Proof.state) -> transition -> transition
wenzelm@45488
    67
  val local_theory_to_proof: (xstring * Position.T) option ->
wenzelm@45488
    68
    (local_theory -> Proof.state) -> 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@49863
    73
  val proofs': (bool -> Proof.state -> Proof.state Seq.result Seq.seq) -> transition -> transition
wenzelm@17904
    74
  val proof': (bool -> Proof.state -> Proof.state) -> transition -> transition
wenzelm@49863
    75
  val proofs: (Proof.state -> Proof.state Seq.result Seq.seq) -> transition -> transition
wenzelm@21177
    76
  val proof: (Proof.state -> Proof.state) -> transition -> transition
wenzelm@33390
    77
  val actual_proof: (Proof_Node.T -> Proof_Node.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@52536
    80
  val exec_id: Document_ID.exec -> transition -> transition
wenzelm@9512
    81
  val unknown_theory: transition -> transition
wenzelm@9512
    82
  val unknown_proof: transition -> transition
wenzelm@9512
    83
  val unknown_context: transition -> transition
wenzelm@28425
    84
  val setmp_thread_position: transition -> ('a -> 'b) -> 'a -> 'b
wenzelm@28103
    85
  val add_hook: (transition -> state -> state -> unit) -> unit
wenzelm@51423
    86
  val get_timing: transition -> Time.time option
wenzelm@51423
    87
  val put_timing: Time.time option -> transition -> transition
wenzelm@26602
    88
  val transition: bool -> transition -> state -> (state * (exn * string) option) option
wenzelm@51323
    89
  val command_errors: bool -> transition -> state -> Runtime.error list * state option
wenzelm@51284
    90
  val command_exception: bool -> transition -> state -> state
wenzelm@56937
    91
  val reset_theory: state -> state option
wenzelm@56937
    92
  val reset_proof: state -> state option
wenzelm@51332
    93
  type result
wenzelm@51332
    94
  val join_results: result -> (transition * state) list
wenzelm@51332
    95
  val element_result: transition Thy_Syntax.element -> state -> result * state
wenzelm@5828
    96
end;
wenzelm@5828
    97
wenzelm@6965
    98
structure Toplevel: TOPLEVEL =
wenzelm@5828
    99
struct
wenzelm@5828
   100
wenzelm@5828
   101
(** toplevel state **)
wenzelm@5828
   102
wenzelm@31476
   103
exception UNDEF = Runtime.UNDEF;
wenzelm@19063
   104
wenzelm@19063
   105
wenzelm@21294
   106
(* local theory wrappers *)
wenzelm@5828
   107
haftmann@38350
   108
val loc_init = Named_Target.context_cmd;
haftmann@52118
   109
val loc_exit = Local_Theory.assert_bottom true #> Local_Theory.exit_global;
wenzelm@21294
   110
wenzelm@47274
   111
fun loc_begin loc (Context.Theory thy) =
wenzelm@47274
   112
      (Context.Theory o loc_exit, loc_init (the_default ("-", Position.none) loc) thy)
wenzelm@47274
   113
  | loc_begin NONE (Context.Proof lthy) =
wenzelm@47274
   114
      (Context.Proof o Local_Theory.restore, lthy)
wenzelm@47274
   115
  | loc_begin (SOME loc) (Context.Proof lthy) =
wenzelm@55446
   116
      (Context.Proof o Named_Target.reinit lthy,
wenzelm@55446
   117
        loc_init loc (loc_exit (Local_Theory.assert_nonbrittle lthy)));
wenzelm@21294
   118
wenzelm@21294
   119
wenzelm@21958
   120
(* datatype node *)
wenzelm@21294
   121
wenzelm@5828
   122
datatype node =
wenzelm@27576
   123
  Theory of generic_theory * Proof.context option
wenzelm@27576
   124
    (*theory with presentation context*) |
wenzelm@33390
   125
  Proof of Proof_Node.T * ((Proof.context -> generic_theory) * generic_theory)
wenzelm@27576
   126
    (*proof node, finish, original theory*) |
wenzelm@51555
   127
  Skipped_Proof of int * (generic_theory * generic_theory);
wenzelm@27564
   128
    (*proof depth, resulting theory, original theory*)
wenzelm@5828
   129
wenzelm@20963
   130
val theory_node = fn Theory (gthy, _) => SOME gthy | _ => NONE;
wenzelm@18589
   131
val proof_node = fn Proof (prf, _) => SOME prf | _ => NONE;
wenzelm@51555
   132
val skipped_proof_node = fn Skipped_Proof _ => true | _ => false;
wenzelm@18589
   133
wenzelm@20963
   134
fun cases_node f _ (Theory (gthy, _)) = f gthy
wenzelm@33390
   135
  | cases_node _ g (Proof (prf, _)) = g (Proof_Node.current prf)
wenzelm@51555
   136
  | cases_node f _ (Skipped_Proof (_, (gthy, _))) = f gthy;
wenzelm@19063
   137
wenzelm@29066
   138
val context_node = cases_node Context.proof_of Proof.context_of;
wenzelm@29066
   139
wenzelm@21958
   140
wenzelm@21958
   141
(* datatype state *)
wenzelm@21958
   142
wenzelm@37953
   143
datatype state = State of node option * node option;  (*current, previous*)
wenzelm@5828
   144
wenzelm@27576
   145
val toplevel = State (NONE, NONE);
wenzelm@5828
   146
wenzelm@27576
   147
fun is_toplevel (State (NONE, _)) = true
wenzelm@7732
   148
  | is_toplevel _ = false;
wenzelm@7732
   149
wenzelm@27576
   150
fun level (State (NONE, _)) = 0
wenzelm@37953
   151
  | level (State (SOME (Theory _), _)) = 0
wenzelm@37953
   152
  | level (State (SOME (Proof (prf, _)), _)) = Proof.level (Proof_Node.current prf)
wenzelm@51555
   153
  | level (State (SOME (Skipped_Proof (d, _)), _)) = d + 1;   (*different notion of proof depth!*)
wenzelm@17076
   154
wenzelm@52565
   155
fun str_of_state (State (NONE, SOME (Theory (Context.Theory thy, _)))) =
wenzelm@52565
   156
      "at top level, result theory " ^ quote (Context.theory_name thy)
wenzelm@52565
   157
  | str_of_state (State (NONE, _)) = "at top level"
wenzelm@37953
   158
  | str_of_state (State (SOME (Theory (Context.Theory _, _)), _)) = "in theory mode"
wenzelm@37953
   159
  | str_of_state (State (SOME (Theory (Context.Proof _, _)), _)) = "in local theory mode"
wenzelm@37953
   160
  | str_of_state (State (SOME (Proof _), _)) = "in proof mode"
wenzelm@51555
   161
  | str_of_state (State (SOME (Skipped_Proof _), _)) = "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@37953
   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@51555
   171
fun is_skipped_proof state = not (is_toplevel state) andalso skipped_proof_node (node_of state);
wenzelm@18589
   172
wenzelm@19063
   173
fun node_case f g state = cases_node f g (node_of state);
wenzelm@5828
   174
wenzelm@30398
   175
fun presentation_context_of state =
wenzelm@30398
   176
  (case try node_of state of
wenzelm@30398
   177
    SOME (Theory (_, SOME ctxt)) => ctxt
wenzelm@30398
   178
  | SOME node => context_node node
wenzelm@30398
   179
  | NONE => raise UNDEF);
wenzelm@30366
   180
wenzelm@30801
   181
fun previous_context_of (State (_, NONE)) = NONE
wenzelm@37953
   182
  | previous_context_of (State (_, SOME prev)) = SOME (context_node prev);
wenzelm@30801
   183
wenzelm@21506
   184
val context_of = node_case Context.proof_of Proof.context_of;
wenzelm@22089
   185
val generic_theory_of = node_case I (Context.Proof o Proof.context_of);
wenzelm@20963
   186
val theory_of = node_case Context.theory_of Proof.theory_of;
wenzelm@18589
   187
val proof_of = node_case (fn _ => raise UNDEF) I;
wenzelm@17208
   188
wenzelm@18589
   189
fun proof_position_of state =
wenzelm@18589
   190
  (case node_of state of
wenzelm@33390
   191
    Proof (prf, _) => Proof_Node.position prf
wenzelm@18589
   192
  | _ => raise UNDEF);
wenzelm@6664
   193
wenzelm@43667
   194
fun end_theory _ (State (NONE, SOME (Theory (Context.Theory thy, _)))) = thy
wenzelm@48992
   195
  | end_theory pos (State (NONE, _)) = error ("Bad theory" ^ Position.here pos)
wenzelm@48992
   196
  | end_theory pos (State (SOME _, _)) = error ("Unfinished theory" ^ Position.here pos);
wenzelm@37953
   197
wenzelm@5828
   198
wenzelm@16815
   199
(* print state *)
wenzelm@16815
   200
wenzelm@56893
   201
fun pretty_context state =
wenzelm@24795
   202
  (case try node_of state of
wenzelm@21506
   203
    NONE => []
wenzelm@56893
   204
  | SOME node =>
wenzelm@56893
   205
      let
wenzelm@56893
   206
        val gthy =
wenzelm@56893
   207
          (case node of
wenzelm@56893
   208
            Theory (gthy, _) => gthy
wenzelm@56893
   209
          | Proof (_, (_, gthy)) => gthy
wenzelm@56893
   210
          | Skipped_Proof (_, (gthy, _)) => gthy);
wenzelm@56893
   211
        val lthy = Context.cases (Named_Target.theory_init) I gthy;
wenzelm@56893
   212
      in Local_Theory.pretty lthy end);
wenzelm@16815
   213
wenzelm@56887
   214
fun pretty_state state =
wenzelm@23701
   215
  (case try node_of state of
wenzelm@23701
   216
    NONE => []
wenzelm@56887
   217
  | SOME (Theory _) => []
wenzelm@23701
   218
  | SOME (Proof (prf, _)) =>
wenzelm@33390
   219
      Proof.pretty_state (Proof_Node.position prf) (Proof_Node.current prf)
wenzelm@56867
   220
  | SOME (Skipped_Proof (d, _)) => [Pretty.str ("skipped proof: depth " ^ string_of_int d)]);
wenzelm@56867
   221
wenzelm@56887
   222
val print_state = pretty_state #> Pretty.markup_chunks Markup.state #> Pretty.writeln;
wenzelm@16815
   223
wenzelm@37858
   224
fun pretty_abstract state = Pretty.str ("<Isar " ^ str_of_state state ^ ">");
wenzelm@37858
   225
wenzelm@16815
   226
wenzelm@15668
   227
wenzelm@5828
   228
(** toplevel transitions **)
wenzelm@5828
   229
wenzelm@53403
   230
val quiet = Unsynchronized.ref false;  (*Proof General legacy*)
wenzelm@53403
   231
val interact = Unsynchronized.ref false;  (*Proof General legacy*)
wenzelm@53403
   232
val timing = Unsynchronized.ref false;  (*Proof General legacy*)
wenzelm@32738
   233
val profiling = Unsynchronized.ref 0;
wenzelm@16682
   234
wenzelm@53709
   235
wenzelm@27601
   236
(* node transactions -- maintaining stable checkpoints *)
wenzelm@7022
   237
wenzelm@31476
   238
exception FAILURE of state * exn;
wenzelm@31476
   239
wenzelm@6689
   240
local
wenzelm@6689
   241
wenzelm@30366
   242
fun reset_presentation (Theory (gthy, _)) = Theory (gthy, NONE)
wenzelm@30366
   243
  | reset_presentation node = node;
wenzelm@30366
   244
wenzelm@27576
   245
fun map_theory f (Theory (gthy, ctxt)) =
wenzelm@33671
   246
      Theory (Context.mapping f (Local_Theory.raw_theory f) gthy, ctxt)
wenzelm@27576
   247
  | map_theory _ node = node;
wenzelm@6689
   248
wenzelm@6689
   249
in
wenzelm@6689
   250
wenzelm@37953
   251
fun apply_transaction f g node =
wenzelm@20128
   252
  let
wenzelm@27576
   253
    val cont_node = reset_presentation node;
wenzelm@56265
   254
    val context = cases_node I (Context.Proof o Proof.context_of) cont_node;
wenzelm@37953
   255
    fun state_error e nd = (State (SOME nd, SOME node), e);
wenzelm@6689
   256
wenzelm@20128
   257
    val (result, err) =
wenzelm@20128
   258
      cont_node
wenzelm@56303
   259
      |> Runtime.controlled_execution (SOME context) f
wenzelm@26624
   260
      |> state_error NONE
wenzelm@26624
   261
      handle exn => state_error (SOME exn) cont_node;
wenzelm@20128
   262
  in
wenzelm@52696
   263
    (case err of
wenzelm@52696
   264
      NONE => tap g result
wenzelm@52696
   265
    | SOME exn => raise FAILURE (result, exn))
wenzelm@20128
   266
  end;
wenzelm@6689
   267
wenzelm@43667
   268
val exit_transaction =
wenzelm@43667
   269
  apply_transaction
wenzelm@43667
   270
    (fn Theory (Context.Theory thy, _) => Theory (Context.Theory (Theory.end_theory thy), NONE)
wenzelm@43667
   271
      | node => node) (K ())
wenzelm@43667
   272
  #> (fn State (node', _) => State (NONE, node'));
wenzelm@43667
   273
wenzelm@6689
   274
end;
wenzelm@6689
   275
wenzelm@6689
   276
wenzelm@6689
   277
(* primitive transitions *)
wenzelm@6689
   278
wenzelm@5828
   279
datatype trans =
wenzelm@44187
   280
  Init of unit -> theory |               (*init theory*)
wenzelm@37953
   281
  Exit |                                 (*formal exit of theory*)
wenzelm@37953
   282
  Keep of bool -> state -> unit |        (*peek at state*)
wenzelm@30366
   283
  Transaction of (bool -> node -> node) * (state -> unit);  (*node transaction and presentation*)
wenzelm@21958
   284
wenzelm@6689
   285
local
wenzelm@5828
   286
wenzelm@44187
   287
fun apply_tr _ (Init f) (State (NONE, _)) =
wenzelm@56303
   288
      State (SOME (Theory (Context.Theory (Runtime.controlled_execution NONE f ()), NONE)), NONE)
wenzelm@43667
   289
  | apply_tr _ Exit (State (SOME (state as Theory (Context.Theory _, _)), _)) =
wenzelm@43667
   290
      exit_transaction state
wenzelm@32792
   291
  | apply_tr int (Keep f) state =
wenzelm@56303
   292
      Runtime.controlled_execution (try generic_theory_of state) (fn x => tap (f int) x) state
wenzelm@32792
   293
  | apply_tr int (Transaction (f, g)) (State (SOME state, _)) =
wenzelm@32792
   294
      apply_transaction (fn x => f int x) g state
wenzelm@32792
   295
  | apply_tr _ _ _ = raise UNDEF;
wenzelm@5828
   296
wenzelm@32792
   297
fun apply_union _ [] state = raise FAILURE (state, UNDEF)
wenzelm@32792
   298
  | apply_union int (tr :: trs) state =
wenzelm@32792
   299
      apply_union int trs state
wenzelm@32792
   300
        handle Runtime.UNDEF => apply_tr int tr state
wenzelm@32792
   301
          | FAILURE (alt_state, UNDEF) => apply_tr int tr alt_state
wenzelm@6689
   302
          | exn as FAILURE _ => raise exn
wenzelm@6689
   303
          | exn => raise FAILURE (state, exn);
wenzelm@6689
   304
wenzelm@6689
   305
in
wenzelm@6689
   306
wenzelm@32792
   307
fun apply_trans int trs state = (apply_union int trs state, NONE)
skalberg@15531
   308
  handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn);
wenzelm@6689
   309
wenzelm@6689
   310
end;
wenzelm@5828
   311
wenzelm@5828
   312
wenzelm@5828
   313
(* datatype transition *)
wenzelm@5828
   314
wenzelm@5828
   315
datatype transition = Transition of
wenzelm@26621
   316
 {name: string,              (*command name*)
wenzelm@26621
   317
  pos: Position.T,           (*source position*)
wenzelm@53403
   318
  int_only: bool,            (*interactive-only*)  (* TTY / Proof General legacy*)
wenzelm@51423
   319
  timing: Time.time option,  (*prescient timing information*)
wenzelm@26621
   320
  trans: trans list};        (*primitive transitions (union)*)
wenzelm@5828
   321
wenzelm@56895
   322
fun make_transition (name, pos, int_only, timing, trans) =
wenzelm@56895
   323
  Transition {name = name, pos = pos, int_only = int_only,
wenzelm@51658
   324
    timing = timing, trans = trans};
wenzelm@5828
   325
wenzelm@56895
   326
fun map_transition f (Transition {name, pos, int_only, timing, trans}) =
wenzelm@56895
   327
  make_transition (f (name, pos, int_only, timing, trans));
wenzelm@5828
   328
wenzelm@56895
   329
val empty = make_transition ("", Position.none, false, NONE, []);
wenzelm@5828
   330
wenzelm@5828
   331
wenzelm@5828
   332
(* diagnostics *)
wenzelm@5828
   333
wenzelm@27427
   334
fun name_of (Transition {name, ...}) = name;
wenzelm@28105
   335
fun pos_of (Transition {pos, ...}) = pos;
wenzelm@5828
   336
wenzelm@48993
   337
fun command_msg msg tr = msg ^ "command " ^ quote (name_of tr) ^ Position.here (pos_of tr);
wenzelm@38875
   338
fun at_command tr = command_msg "At " tr;
wenzelm@5828
   339
wenzelm@5828
   340
fun type_error tr state =
wenzelm@56937
   341
  command_msg "Illegal application of " tr ^ " " ^ str_of_state state;
wenzelm@5828
   342
wenzelm@5828
   343
wenzelm@5828
   344
(* modify transitions *)
wenzelm@5828
   345
wenzelm@56895
   346
fun name name = map_transition (fn (_, pos, int_only, timing, trans) =>
wenzelm@56895
   347
  (name, pos, int_only, timing, trans));
wenzelm@9010
   348
wenzelm@56895
   349
fun position pos = map_transition (fn (name, _, int_only, timing, trans) =>
wenzelm@56895
   350
  (name, pos, int_only, timing, trans));
wenzelm@14923
   351
wenzelm@56895
   352
fun interactive int_only = map_transition (fn (name, pos, _, timing, trans) =>
wenzelm@56895
   353
  (name, pos, int_only, timing, trans));
wenzelm@16607
   354
wenzelm@56895
   355
fun add_trans tr = map_transition (fn (name, pos, int_only, timing, trans) =>
wenzelm@56895
   356
  (name, pos, int_only, timing, tr :: trans));
wenzelm@28433
   357
wenzelm@56895
   358
val reset_trans = map_transition (fn (name, pos, int_only, timing, _) =>
wenzelm@56895
   359
  (name, pos, int_only, timing, []));
wenzelm@5828
   360
wenzelm@5828
   361
wenzelm@21007
   362
(* basic transitions *)
wenzelm@5828
   363
wenzelm@44187
   364
fun init_theory f = add_trans (Init f);
wenzelm@37977
   365
wenzelm@44187
   366
fun is_init (Transition {trans = [Init _], ...}) = true
wenzelm@44187
   367
  | is_init _ = false;
wenzelm@44187
   368
wenzelm@44187
   369
fun modify_init f tr = if is_init tr then init_theory f (reset_trans tr) else tr;
wenzelm@37977
   370
wenzelm@6689
   371
val exit = add_trans Exit;
wenzelm@7612
   372
val keep' = add_trans o Keep;
wenzelm@30366
   373
wenzelm@30366
   374
fun present_transaction f g = add_trans (Transaction (f, g));
wenzelm@30366
   375
fun transaction f = present_transaction f (K ());
wenzelm@5828
   376
wenzelm@7612
   377
fun keep f = add_trans (Keep (fn _ => f));
wenzelm@5828
   378
fun imperative f = keep (fn _ => f ());
wenzelm@5828
   379
wenzelm@27840
   380
fun ignored pos = empty |> name "<ignored>" |> position pos |> imperative I;
wenzelm@51268
   381
fun is_ignored tr = name_of tr = "<ignored>";
wenzelm@48772
   382
wenzelm@48772
   383
val malformed_name = "<malformed>";
wenzelm@27840
   384
fun malformed pos msg =
wenzelm@48772
   385
  empty |> name malformed_name |> position pos |> imperative (fn () => error msg);
wenzelm@48772
   386
fun is_malformed tr = name_of tr = malformed_name;
wenzelm@27840
   387
wenzelm@21007
   388
val unknown_theory = imperative (fn () => warning "Unknown theory context");
wenzelm@21007
   389
val unknown_proof = imperative (fn () => warning "Unknown proof context");
wenzelm@21007
   390
val unknown_context = imperative (fn () => warning "Unknown context");
wenzelm@15668
   391
wenzelm@21007
   392
wenzelm@49012
   393
(* theory transitions *)
wenzelm@44304
   394
wenzelm@27601
   395
fun generic_theory f = transaction (fn _ =>
wenzelm@26491
   396
  (fn Theory (gthy, _) => Theory (f gthy, NONE)
wenzelm@26491
   397
    | _ => raise UNDEF));
wenzelm@26491
   398
wenzelm@27601
   399
fun theory' f = transaction (fn int =>
wenzelm@33725
   400
  (fn Theory (Context.Theory thy, _) =>
wenzelm@33725
   401
      let val thy' = thy
wenzelm@49012
   402
        |> Sign.new_group
wenzelm@33725
   403
        |> f int
wenzelm@33725
   404
        |> Sign.reset_group;
wenzelm@33725
   405
      in Theory (Context.Theory thy', NONE) end
wenzelm@20963
   406
    | _ => raise UNDEF));
wenzelm@20963
   407
wenzelm@20963
   408
fun theory f = theory' (K f);
wenzelm@20963
   409
wenzelm@27601
   410
fun begin_local_theory begin f = transaction (fn _ =>
wenzelm@20963
   411
  (fn Theory (Context.Theory thy, _) =>
wenzelm@20963
   412
        let
wenzelm@20985
   413
          val lthy = f thy;
wenzelm@21294
   414
          val gthy = if begin then Context.Proof lthy else Context.Theory (loc_exit lthy);
wenzelm@56897
   415
          val _ =
wenzelm@56897
   416
            if begin then
wenzelm@56897
   417
              Pretty.writeln (Pretty.mark Markup.state (Pretty.chunks (Local_Theory.pretty lthy)))
wenzelm@56897
   418
            else ();
wenzelm@21294
   419
        in Theory (gthy, SOME lthy) end
wenzelm@20963
   420
    | _ => raise UNDEF));
wenzelm@17076
   421
wenzelm@27601
   422
val end_local_theory = transaction (fn _ =>
wenzelm@21294
   423
  (fn Theory (Context.Proof lthy, _) => Theory (Context.Theory (loc_exit lthy), SOME lthy)
wenzelm@21007
   424
    | _ => raise UNDEF));
wenzelm@21007
   425
wenzelm@47069
   426
fun open_target f = transaction (fn _ =>
wenzelm@47069
   427
  (fn Theory (gthy, _) =>
wenzelm@47069
   428
        let val lthy = f gthy
wenzelm@47069
   429
        in Theory (Context.Proof lthy, SOME lthy) end
wenzelm@47069
   430
    | _ => raise UNDEF));
wenzelm@47069
   431
wenzelm@47069
   432
val close_target = transaction (fn _ =>
wenzelm@47069
   433
  (fn Theory (Context.Proof lthy, _) =>
wenzelm@47069
   434
        (case try Local_Theory.close_target lthy of
wenzelm@50739
   435
          SOME ctxt' =>
wenzelm@50739
   436
            let
wenzelm@50739
   437
              val gthy' =
wenzelm@50739
   438
                if can Local_Theory.assert ctxt'
wenzelm@50739
   439
                then Context.Proof ctxt'
wenzelm@50739
   440
                else Context.Theory (Proof_Context.theory_of ctxt');
wenzelm@50739
   441
            in Theory (gthy', SOME lthy) end
wenzelm@47069
   442
        | NONE => raise UNDEF)
wenzelm@47069
   443
    | _ => raise UNDEF));
wenzelm@47069
   444
wenzelm@47069
   445
wenzelm@21007
   446
local
wenzelm@21007
   447
wenzelm@30366
   448
fun local_theory_presentation loc f = present_transaction (fn int =>
wenzelm@21294
   449
  (fn Theory (gthy, _) =>
wenzelm@21294
   450
        let
wenzelm@49062
   451
          val (finish, lthy) = loc_begin loc gthy;
wenzelm@47274
   452
          val lthy' = lthy
wenzelm@49012
   453
            |> Local_Theory.new_group
wenzelm@33725
   454
            |> f int
wenzelm@33725
   455
            |> Local_Theory.reset_group;
wenzelm@21294
   456
        in Theory (finish lthy', SOME lthy') end
wenzelm@30366
   457
    | _ => raise UNDEF));
wenzelm@15668
   458
wenzelm@21007
   459
in
wenzelm@21007
   460
wenzelm@30366
   461
fun local_theory' loc f = local_theory_presentation loc f (K ());
wenzelm@29380
   462
fun local_theory loc f = local_theory' loc (K f);
wenzelm@30366
   463
fun present_local_theory loc = local_theory_presentation loc (K I);
wenzelm@18955
   464
wenzelm@21007
   465
end;
wenzelm@21007
   466
wenzelm@21007
   467
wenzelm@21007
   468
(* proof transitions *)
wenzelm@21007
   469
wenzelm@27601
   470
fun end_proof f = transaction (fn int =>
wenzelm@24795
   471
  (fn Proof (prf, (finish, _)) =>
wenzelm@33390
   472
        let val state = Proof_Node.current prf in
wenzelm@21007
   473
          if can (Proof.assert_bottom true) state then
wenzelm@21007
   474
            let
wenzelm@21007
   475
              val ctxt' = f int state;
wenzelm@21007
   476
              val gthy' = finish ctxt';
wenzelm@21007
   477
            in Theory (gthy', SOME ctxt') end
wenzelm@21007
   478
          else raise UNDEF
wenzelm@21007
   479
        end
wenzelm@51555
   480
    | Skipped_Proof (0, (gthy, _)) => Theory (gthy, NONE)
wenzelm@21007
   481
    | _ => raise UNDEF));
wenzelm@21007
   482
wenzelm@21294
   483
local
wenzelm@21294
   484
wenzelm@47274
   485
fun begin_proof init = transaction (fn int =>
wenzelm@21294
   486
  (fn Theory (gthy, _) =>
wenzelm@21294
   487
    let
wenzelm@47274
   488
      val (finish, prf) = init int gthy;
wenzelm@52499
   489
      val skip = Goal.skip_proofs_enabled ();
wenzelm@40960
   490
      val (is_goal, no_skip) =
wenzelm@40960
   491
        (true, Proof.schematic_goal prf) handle ERROR _ => (false, true);
wenzelm@47274
   492
      val _ =
wenzelm@47274
   493
        if is_goal andalso skip andalso no_skip then
wenzelm@47274
   494
          warning "Cannot skip proof of schematic goal statement"
wenzelm@47274
   495
        else ();
wenzelm@21294
   496
    in
wenzelm@40960
   497
      if skip andalso not no_skip then
wenzelm@51555
   498
        Skipped_Proof (0, (finish (Proof.global_skip_proof true prf), gthy))
wenzelm@47274
   499
      else Proof (Proof_Node.init prf, (finish, gthy))
wenzelm@21294
   500
    end
wenzelm@21294
   501
  | _ => raise UNDEF));
wenzelm@21294
   502
wenzelm@21294
   503
in
wenzelm@21294
   504
wenzelm@24780
   505
fun local_theory_to_proof' loc f = begin_proof
wenzelm@47274
   506
  (fn int => fn gthy =>
wenzelm@49062
   507
    let val (finish, lthy) = loc_begin loc gthy
wenzelm@49012
   508
    in (finish o Local_Theory.reset_group, f int (Local_Theory.new_group lthy)) end);
wenzelm@24780
   509
berghofe@24453
   510
fun local_theory_to_proof loc f = local_theory_to_proof' loc (K f);
wenzelm@21294
   511
wenzelm@21294
   512
fun theory_to_proof f = begin_proof
wenzelm@47274
   513
  (fn _ => fn gthy =>
wenzelm@56057
   514
    (Context.Theory o Sign.reset_group o Sign.change_check o Proof_Context.theory_of,
wenzelm@49062
   515
      (case gthy of
wenzelm@52788
   516
        Context.Theory thy => f (Sign.new_group thy)
wenzelm@49012
   517
      | _ => raise UNDEF)));
wenzelm@21294
   518
wenzelm@21294
   519
end;
wenzelm@21294
   520
wenzelm@27601
   521
val forget_proof = transaction (fn _ =>
wenzelm@21007
   522
  (fn Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
wenzelm@51555
   523
    | Skipped_Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
wenzelm@21007
   524
    | _ => raise UNDEF));
wenzelm@21007
   525
wenzelm@30366
   526
val present_proof = present_transaction (fn _ =>
wenzelm@49062
   527
  (fn Proof (prf, x) => Proof (Proof_Node.apply I prf, x)
wenzelm@51555
   528
    | skip as Skipped_Proof _ => skip
wenzelm@30366
   529
    | _ => raise UNDEF));
wenzelm@21177
   530
wenzelm@27601
   531
fun proofs' f = transaction (fn int =>
wenzelm@49062
   532
  (fn Proof (prf, x) => Proof (Proof_Node.applys (f int) prf, x)
wenzelm@51555
   533
    | skip as Skipped_Proof _ => skip
wenzelm@16815
   534
    | _ => raise UNDEF));
wenzelm@15668
   535
wenzelm@49863
   536
fun proof' f = proofs' ((Seq.single o Seq.Result) oo f);
wenzelm@17904
   537
val proofs = proofs' o K;
wenzelm@6689
   538
val proof = proof' o K;
wenzelm@16815
   539
wenzelm@27601
   540
fun actual_proof f = transaction (fn _ =>
wenzelm@21007
   541
  (fn Proof (prf, x) => Proof (f prf, x)
wenzelm@20963
   542
    | _ => raise UNDEF));
wenzelm@16815
   543
wenzelm@53403
   544
(*Proof General legacy*)
wenzelm@27601
   545
fun skip_proof f = transaction (fn _ =>
wenzelm@51555
   546
  (fn Skipped_Proof (h, x) => Skipped_Proof (f h, x)
wenzelm@18563
   547
    | _ => raise UNDEF));
wenzelm@18563
   548
wenzelm@53403
   549
(*Proof General legacy*)
wenzelm@27601
   550
fun skip_proof_to_theory pred = transaction (fn _ =>
wenzelm@51555
   551
  (fn Skipped_Proof (d, (gthy, _)) => if pred d then Theory (gthy, NONE) else raise UNDEF
wenzelm@33725
   552
    | _ => raise UNDEF));
wenzelm@5828
   553
wenzelm@5828
   554
wenzelm@5828
   555
wenzelm@5828
   556
(** toplevel transactions **)
wenzelm@5828
   557
wenzelm@52527
   558
(* runtime position *)
wenzelm@27427
   559
wenzelm@52536
   560
fun exec_id id (tr as Transition {pos, ...}) =
wenzelm@52536
   561
  position (Position.put_id (Document_ID.print id) pos) tr;
wenzelm@25799
   562
wenzelm@25960
   563
fun setmp_thread_position (Transition {pos, ...}) f x =
wenzelm@25819
   564
  Position.setmp_thread_data pos f x;
wenzelm@25799
   565
wenzelm@25799
   566
wenzelm@28095
   567
(* post-transition hooks *)
wenzelm@28095
   568
wenzelm@37905
   569
local
wenzelm@56147
   570
  val hooks =
wenzelm@56147
   571
    Synchronized.var "Toplevel.hooks" ([]: (transition -> state -> state -> unit) list);
wenzelm@37905
   572
in
wenzelm@28095
   573
wenzelm@56147
   574
fun add_hook hook = Synchronized.change hooks (cons hook);
wenzelm@56147
   575
fun get_hooks () = Synchronized.value hooks;
wenzelm@28095
   576
wenzelm@28095
   577
end;
wenzelm@28095
   578
wenzelm@28095
   579
wenzelm@5828
   580
(* apply transitions *)
wenzelm@5828
   581
wenzelm@51217
   582
fun get_timing (Transition {timing, ...}) = timing;
wenzelm@56895
   583
fun put_timing timing = map_transition (fn (name, pos, int_only, _, trans) =>
wenzelm@56895
   584
  (name, pos, int_only, timing, trans));
wenzelm@51217
   585
wenzelm@6664
   586
local
wenzelm@6664
   587
wenzelm@56895
   588
fun app int (tr as Transition {name, trans, ...}) =
wenzelm@25819
   589
  setmp_thread_position tr (fn state =>
wenzelm@25799
   590
    let
wenzelm@51595
   591
      val timing_start = Timing.start ();
wenzelm@25799
   592
wenzelm@51595
   593
      val (result, opt_err) =
wenzelm@51658
   594
         state |> (apply_trans int trans |> ! profiling > 0 ? profile (! profiling));
wenzelm@56895
   595
wenzelm@56895
   596
      val _ =
wenzelm@56895
   597
        if int andalso not (! quiet) andalso Keyword.is_printed name
wenzelm@56895
   598
        then print_state result else ();
wenzelm@51216
   599
wenzelm@51595
   600
      val timing_result = Timing.result timing_start;
wenzelm@51662
   601
      val timing_props =
wenzelm@51662
   602
        Markup.command_timing :: (Markup.nameN, name_of tr) :: Position.properties_of (pos_of tr);
wenzelm@51662
   603
      val _ = Timing.protocol_message timing_props timing_result;
wenzelm@51595
   604
    in
wenzelm@56937
   605
      (result, Option.map (fn UNDEF => ERROR (type_error tr state) | exn => exn) opt_err)
wenzelm@51595
   606
    end);
wenzelm@6664
   607
wenzelm@6664
   608
in
wenzelm@5828
   609
wenzelm@26602
   610
fun transition int tr st =
wenzelm@28095
   611
  let
wenzelm@28095
   612
    val hooks = get_hooks ();
wenzelm@28103
   613
    fun apply_hooks st' = hooks |> List.app (fn f => (try (fn () => f tr st st') (); ()));
wenzelm@28095
   614
wenzelm@28095
   615
    val ctxt = try context_of st;
wenzelm@28095
   616
    val res =
wenzelm@28095
   617
      (case app int tr st of
wenzelm@38888
   618
        (_, SOME Runtime.TERMINATE) => NONE
wenzelm@38888
   619
      | (st', SOME (Runtime.EXCURSION_FAIL exn_info)) => SOME (st', SOME exn_info)
wenzelm@31476
   620
      | (st', SOME exn) => SOME (st', SOME (Runtime.exn_context ctxt exn, at_command tr))
wenzelm@28103
   621
      | (st', NONE) => SOME (st', NONE));
wenzelm@28103
   622
    val _ = (case res of SOME (st', NONE) => apply_hooks st' | _ => ());
wenzelm@28095
   623
  in res end;
wenzelm@6664
   624
wenzelm@6664
   625
end;
wenzelm@5828
   626
wenzelm@5828
   627
wenzelm@51284
   628
(* managed commands *)
wenzelm@5828
   629
wenzelm@51323
   630
fun command_errors int tr st =
wenzelm@51323
   631
  (case transition int tr st of
wenzelm@51323
   632
    SOME (st', NONE) => ([], SOME st')
wenzelm@56303
   633
  | SOME (_, SOME (exn, _)) => (Runtime.exn_messages_ids exn, NONE)
wenzelm@56303
   634
  | NONE => (Runtime.exn_messages_ids Runtime.TERMINATE, NONE));
wenzelm@51323
   635
wenzelm@51284
   636
fun command_exception int tr st =
wenzelm@51284
   637
  (case transition int tr st of
wenzelm@28425
   638
    SOME (st', NONE) => st'
wenzelm@39285
   639
  | SOME (_, SOME (exn, info)) =>
wenzelm@39285
   640
      if Exn.is_interrupt exn then reraise exn else raise Runtime.EXCURSION_FAIL (exn, info)
wenzelm@38888
   641
  | NONE => raise Runtime.EXCURSION_FAIL (Runtime.TERMINATE, at_command tr));
wenzelm@27576
   642
wenzelm@51323
   643
fun command tr = command_exception (! interact) tr;
wenzelm@51284
   644
wenzelm@28433
   645
wenzelm@56937
   646
(* reset state *)
wenzelm@56937
   647
wenzelm@56937
   648
local
wenzelm@56937
   649
wenzelm@56937
   650
fun reset_state check trans st =
wenzelm@56937
   651
  if check st then NONE
wenzelm@56937
   652
  else #2 (command_errors false (trans empty) st);
wenzelm@56937
   653
wenzelm@56937
   654
in
wenzelm@56937
   655
wenzelm@56937
   656
val reset_theory = reset_state is_theory forget_proof;
wenzelm@56937
   657
wenzelm@56937
   658
val reset_proof =
wenzelm@56937
   659
  reset_state is_proof
wenzelm@56937
   660
    (transaction (fn _ =>
wenzelm@56937
   661
      (fn Theory (gthy, _) => Skipped_Proof (0, (gthy, gthy))
wenzelm@56937
   662
        | _ => raise UNDEF)));
wenzelm@56937
   663
wenzelm@56937
   664
end;
wenzelm@56937
   665
wenzelm@56937
   666
wenzelm@46959
   667
(* scheduled proof result *)
wenzelm@28433
   668
wenzelm@51332
   669
datatype result =
wenzelm@51332
   670
  Result of transition * state |
wenzelm@51332
   671
  Result_List of result list |
wenzelm@51332
   672
  Result_Future of result future;
wenzelm@51332
   673
wenzelm@51332
   674
fun join_results (Result x) = [x]
wenzelm@51332
   675
  | join_results (Result_List xs) = maps join_results xs
wenzelm@51332
   676
  | join_results (Result_Future x) = join_results (Future.join x);
wenzelm@51332
   677
wenzelm@51323
   678
local
wenzelm@51323
   679
wenzelm@47417
   680
structure Result = Proof_Data
wenzelm@28974
   681
(
wenzelm@51332
   682
  type T = result;
wenzelm@51332
   683
  val empty: T = Result_List [];
wenzelm@47417
   684
  fun init _ = empty;
wenzelm@28974
   685
);
wenzelm@28974
   686
wenzelm@51332
   687
val get_result = Result.get o Proof.context_of;
wenzelm@51332
   688
val put_result = Proof.map_context o Result.put;
wenzelm@51324
   689
wenzelm@51423
   690
fun timing_estimate include_head elem =
wenzelm@51423
   691
  let
wenzelm@51423
   692
    val trs = Thy_Syntax.flat_element elem |> not include_head ? tl;
wenzelm@51423
   693
    val timings = map get_timing trs;
wenzelm@51423
   694
  in
wenzelm@51423
   695
    if forall is_some timings then
wenzelm@51423
   696
      SOME (fold (curry Time.+ o the) timings Time.zeroTime)
wenzelm@51423
   697
    else NONE
wenzelm@51423
   698
  end;
wenzelm@51423
   699
wenzelm@51423
   700
fun priority NONE = ~1
wenzelm@51423
   701
  | priority (SOME estimate) =
wenzelm@51423
   702
      Int.min (Real.floor (Real.max (Math.log10 (Time.toReal estimate), ~3.0)) - 3, ~1);
wenzelm@51423
   703
wenzelm@51423
   704
fun proof_future_enabled estimate st =
wenzelm@51324
   705
  (case try proof_of st of
wenzelm@51324
   706
    NONE => false
wenzelm@51324
   707
  | SOME state =>
wenzelm@51324
   708
      not (Proof.is_relevant state) andalso
wenzelm@51324
   709
       (if can (Proof.assert_bottom true) state
wenzelm@53189
   710
        then Goal.future_enabled 1
wenzelm@51423
   711
        else
wenzelm@51423
   712
          (case estimate of
wenzelm@53189
   713
            NONE => Goal.future_enabled 2
wenzelm@51423
   714
          | SOME t => Goal.future_enabled_timing t)));
wenzelm@51278
   715
wenzelm@51323
   716
fun atom_result tr st =
wenzelm@51323
   717
  let
wenzelm@51323
   718
    val st' =
wenzelm@53189
   719
      if Goal.future_enabled 1 andalso Keyword.is_diag (name_of tr) then
wenzelm@53192
   720
        (Execution.fork
wenzelm@51605
   721
          {name = "Toplevel.diag", pos = pos_of tr,
wenzelm@51605
   722
            pri = priority (timing_estimate true (Thy_Syntax.atom tr))}
wenzelm@51605
   723
          (fn () => command tr st); st)
wenzelm@51323
   724
      else command tr st;
wenzelm@51332
   725
  in (Result (tr, st'), st') end;
wenzelm@51323
   726
wenzelm@51323
   727
in
wenzelm@51323
   728
wenzelm@51332
   729
fun element_result (Thy_Syntax.Element (tr, NONE)) st = atom_result tr st
wenzelm@51423
   730
  | element_result (elem as Thy_Syntax.Element (head_tr, SOME element_rest)) st =
wenzelm@48633
   731
      let
wenzelm@51324
   732
        val (head_result, st') = atom_result head_tr st;
wenzelm@51332
   733
        val (body_elems, end_tr) = element_rest;
wenzelm@51423
   734
        val estimate = timing_estimate false elem;
wenzelm@51324
   735
      in
wenzelm@51423
   736
        if not (proof_future_enabled estimate st')
wenzelm@51324
   737
        then
wenzelm@51332
   738
          let
wenzelm@51332
   739
            val proof_trs = maps Thy_Syntax.flat_element body_elems @ [end_tr];
wenzelm@51332
   740
            val (proof_results, st'') = fold_map atom_result proof_trs st';
wenzelm@51332
   741
          in (Result_List (head_result :: proof_results), st'') end
wenzelm@51324
   742
        else
wenzelm@51324
   743
          let
wenzelm@51324
   744
            val finish = Context.Theory o Proof_Context.theory_of;
wenzelm@28974
   745
wenzelm@51605
   746
            val future_proof =
wenzelm@51605
   747
              Proof.future_proof (fn state =>
wenzelm@53192
   748
                Execution.fork
wenzelm@51605
   749
                  {name = "Toplevel.future_proof", pos = pos_of head_tr, pri = priority estimate}
wenzelm@51605
   750
                  (fn () =>
wenzelm@51605
   751
                    let
wenzelm@51605
   752
                      val State (SOME (Proof (prf, (_, orig_gthy))), prev) = st';
wenzelm@51605
   753
                      val prf' = Proof_Node.apply (K state) prf;
wenzelm@51605
   754
                      val (result, result_state) =
wenzelm@51605
   755
                        State (SOME (Proof (prf', (finish, orig_gthy))), prev)
wenzelm@51605
   756
                        |> fold_map element_result body_elems ||> command end_tr;
wenzelm@51605
   757
                    in (Result_List result, presentation_context_of result_state) end))
wenzelm@51332
   758
              #> (fn (res, state') => state' |> put_result (Result_Future res));
wenzelm@51332
   759
wenzelm@51332
   760
            val forked_proof =
wenzelm@51332
   761
              proof (future_proof #>
wenzelm@51332
   762
                (fn state => state |> Proof.local_done_proof |> put_result (get_result state))) o
wenzelm@51332
   763
              end_proof (fn _ => future_proof #>
wenzelm@51332
   764
                (fn state => state |> Proof.global_done_proof |> Result.put (get_result state)));
wenzelm@28974
   765
wenzelm@51324
   766
            val st'' = st'
wenzelm@56895
   767
              |> command (head_tr |> reset_trans |> forked_proof);
wenzelm@51332
   768
            val end_result = Result (end_tr, st'');
wenzelm@51324
   769
            val result =
wenzelm@51332
   770
              Result_List [head_result, Result.get (presentation_context_of st''), end_result];
wenzelm@51324
   771
          in (result, st'') end
wenzelm@51324
   772
      end;
wenzelm@28433
   773
wenzelm@6664
   774
end;
wenzelm@51323
   775
wenzelm@51323
   776
end;