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