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