src/Pure/Isar/toplevel.ML
author wenzelm
Sun Mar 10 00:21:34 2019 +0100 (3 months ago ago)
changeset 70068 b9985133805d
parent 70067 0cb8753bdb50
child 70069 6db51f45b5f9
permissions -rw-r--r--
added semantic document markers;
emulate old-style tags as "tag" markers, with subtle change of semantics for multiples tags (ever used?);
tuned;
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@70067
    11
  val init_toplevel: unit -> state
wenzelm@62895
    12
  val theory_toplevel: theory -> state
wenzelm@7732
    13
  val is_toplevel: state -> bool
wenzelm@18589
    14
  val is_theory: state -> bool
wenzelm@18589
    15
  val is_proof: state -> bool
wenzelm@51555
    16
  val is_skipped_proof: state -> bool
wenzelm@17076
    17
  val level: state -> int
wenzelm@67391
    18
  val previous_theory_of: state -> theory 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@68505
    24
  val is_end_theory: state -> bool
wenzelm@37953
    25
  val end_theory: Position.T -> state -> theory
wenzelm@67381
    26
  val presentation_context: state -> Proof.context
wenzelm@67381
    27
  val presentation_state: Proof.context -> state
wenzelm@56893
    28
  val pretty_context: state -> Pretty.T list
wenzelm@56887
    29
  val pretty_state: state -> Pretty.T list
wenzelm@61208
    30
  val string_of_state: state -> string
wenzelm@37858
    31
  val pretty_abstract: state -> Pretty.T
wenzelm@16682
    32
  type transition
wenzelm@5828
    33
  val empty: transition
wenzelm@27427
    34
  val name_of: transition -> string
wenzelm@28105
    35
  val pos_of: transition -> Position.T
wenzelm@70058
    36
  val timing_of: transition -> Time.time
wenzelm@60076
    37
  val type_error: transition -> string
wenzelm@5828
    38
  val name: string -> transition -> transition
wenzelm@5828
    39
  val position: Position.T -> transition -> transition
wenzelm@70068
    40
  val markers: Input.source list -> transition -> transition
wenzelm@70058
    41
  val timing: Time.time -> transition -> transition
wenzelm@44187
    42
  val init_theory: (unit -> theory) -> transition -> transition
wenzelm@44187
    43
  val is_init: transition -> bool
wenzelm@44186
    44
  val modify_init: (unit -> theory) -> transition -> transition
wenzelm@6689
    45
  val exit: transition -> transition
wenzelm@5828
    46
  val keep: (state -> unit) -> transition -> transition
wenzelm@7612
    47
  val keep': (bool -> state -> unit) -> transition -> transition
wenzelm@60190
    48
  val keep_proof: (state -> unit) -> transition -> transition
wenzelm@27840
    49
  val ignored: Position.T -> transition
wenzelm@51268
    50
  val is_ignored: transition -> bool
wenzelm@27840
    51
  val malformed: Position.T -> string -> transition
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@59939
    59
  val local_theory': (bool * Position.T) option -> (xstring * Position.T) option ->
wenzelm@59923
    60
    (bool -> local_theory -> local_theory) -> transition -> transition
wenzelm@59939
    61
  val local_theory: (bool * Position.T) option -> (xstring * Position.T) option ->
wenzelm@59923
    62
    (local_theory -> local_theory) -> transition -> transition
wenzelm@45488
    63
  val present_local_theory: (xstring * Position.T) option -> (state -> unit) ->
berghofe@24453
    64
    transition -> transition
wenzelm@59939
    65
  val local_theory_to_proof': (bool * Position.T) option -> (xstring * Position.T) option ->
wenzelm@45488
    66
    (bool -> local_theory -> Proof.state) -> transition -> transition
wenzelm@59939
    67
  val local_theory_to_proof: (bool * Position.T) option -> (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@68876
    71
  val forget_proof: transition -> transition
wenzelm@49863
    72
  val proofs': (bool -> Proof.state -> Proof.state Seq.result Seq.seq) -> transition -> transition
wenzelm@17904
    73
  val proof': (bool -> Proof.state -> Proof.state) -> transition -> transition
wenzelm@49863
    74
  val proofs: (Proof.state -> Proof.state Seq.result Seq.seq) -> transition -> transition
wenzelm@21177
    75
  val proof: (Proof.state -> Proof.state) -> transition -> transition
wenzelm@33390
    76
  val actual_proof: (Proof_Node.T -> Proof_Node.T) -> transition -> transition
wenzelm@60695
    77
  val skip_proof: (unit -> unit) -> transition -> transition
wenzelm@60695
    78
  val skip_proof_open: transition -> transition
wenzelm@60695
    79
  val skip_proof_close: transition -> transition
wenzelm@52536
    80
  val exec_id: Document_ID.exec -> transition -> transition
wenzelm@28425
    81
  val setmp_thread_position: transition -> ('a -> 'b) -> 'a -> 'b
wenzelm@28103
    82
  val add_hook: (transition -> state -> state -> unit) -> unit
wenzelm@59055
    83
  val transition: bool -> transition -> state -> state * (exn * string) option
wenzelm@51323
    84
  val command_errors: bool -> transition -> state -> Runtime.error list * state option
wenzelm@51284
    85
  val command_exception: bool -> transition -> state -> state
wenzelm@56937
    86
  val reset_theory: state -> state option
wenzelm@56937
    87
  val reset_proof: state -> state option
wenzelm@68877
    88
  val reset_notepad: state -> state option
wenzelm@70068
    89
  val fork_presentation: transition -> transition * transition
wenzelm@51332
    90
  type result
wenzelm@51332
    91
  val join_results: result -> (transition * state) list
wenzelm@68839
    92
  val element_result: Keyword.keywords -> transition Thy_Element.element -> state -> result * state
wenzelm@5828
    93
end;
wenzelm@5828
    94
wenzelm@6965
    95
structure Toplevel: TOPLEVEL =
wenzelm@5828
    96
struct
wenzelm@5828
    97
wenzelm@5828
    98
(** toplevel state **)
wenzelm@5828
    99
wenzelm@31476
   100
exception UNDEF = Runtime.UNDEF;
wenzelm@19063
   101
wenzelm@19063
   102
wenzelm@21958
   103
(* datatype node *)
wenzelm@21294
   104
wenzelm@5828
   105
datatype node =
wenzelm@70064
   106
  Toplevel
wenzelm@70064
   107
    (*toplevel outside of theory body*) |
wenzelm@70059
   108
  Theory of generic_theory
wenzelm@70059
   109
    (*global or local theory*) |
wenzelm@33390
   110
  Proof of Proof_Node.T * ((Proof.context -> generic_theory) * generic_theory)
wenzelm@27576
   111
    (*proof node, finish, original theory*) |
wenzelm@51555
   112
  Skipped_Proof of int * (generic_theory * generic_theory);
wenzelm@27564
   113
    (*proof depth, resulting theory, original theory*)
wenzelm@5828
   114
wenzelm@70059
   115
val theory_node = fn Theory gthy => SOME gthy | _ => NONE;
wenzelm@18589
   116
val proof_node = fn Proof (prf, _) => SOME prf | _ => NONE;
wenzelm@51555
   117
val skipped_proof_node = fn Skipped_Proof _ => true | _ => false;
wenzelm@18589
   118
wenzelm@70064
   119
fun cases_node f _ _ Toplevel = f ()
wenzelm@70064
   120
  | cases_node _ g _ (Theory gthy) = g gthy
wenzelm@70064
   121
  | cases_node _ _ h (Proof (prf, _)) = h (Proof_Node.current prf)
wenzelm@70064
   122
  | cases_node _ g _ (Skipped_Proof (_, (gthy, _))) = g gthy;
wenzelm@70064
   123
wenzelm@70064
   124
fun cases_proper_node g h = cases_node (fn () => raise UNDEF) g h;
wenzelm@70064
   125
wenzelm@70064
   126
val get_theory = cases_node (K NONE) (SOME o Context.theory_of) (SOME o Proof.theory_of);
wenzelm@19063
   127
wenzelm@21958
   128
wenzelm@21958
   129
(* datatype state *)
wenzelm@21958
   130
wenzelm@70064
   131
type node_presentation = node * Proof.context;
wenzelm@5828
   132
wenzelm@70067
   133
fun init_presentation () =
wenzelm@70067
   134
  Proof_Context.init_global (Theory.get_pure_bootstrap ());
wenzelm@70067
   135
wenzelm@70067
   136
fun node_presentation node =
wenzelm@70067
   137
  (node, cases_node init_presentation Context.proof_of Proof.context_of node);
wenzelm@70067
   138
wenzelm@70059
   139
wenzelm@70064
   140
datatype state =
wenzelm@70064
   141
  State of node_presentation * theory option;
wenzelm@70064
   142
    (*current node with presentation context, previous theory*)
wenzelm@62895
   143
wenzelm@70064
   144
fun node_of (State ((node, _), _)) = node;
wenzelm@70064
   145
fun previous_theory_of (State (_, prev_thy)) = prev_thy;
wenzelm@5828
   146
wenzelm@70068
   147
fun init_toplevel () = State (node_presentation Toplevel, NONE);
wenzelm@70068
   148
fun theory_toplevel thy = State (node_presentation (Theory (Context.Theory thy)), NONE);
wenzelm@70064
   149
wenzelm@70067
   150
wenzelm@70064
   151
fun level state =
wenzelm@70064
   152
  (case node_of state of
wenzelm@70064
   153
    Toplevel => 0
wenzelm@70064
   154
  | Theory _ => 0
wenzelm@70064
   155
  | Proof (prf, _) => Proof.level (Proof_Node.current prf)
wenzelm@70064
   156
  | Skipped_Proof (d, _) => d + 1);   (*different notion of proof depth!*)
wenzelm@17076
   157
wenzelm@70064
   158
fun str_of_state state =
wenzelm@70064
   159
  (case node_of state of
wenzelm@70064
   160
    Toplevel =>
wenzelm@70064
   161
      (case previous_theory_of state of
wenzelm@70064
   162
        NONE => "at top level"
wenzelm@70064
   163
      | SOME thy => "at top level, result theory " ^ quote (Context.theory_name thy))
wenzelm@70064
   164
  | Theory (Context.Theory _) => "in theory mode"
wenzelm@70064
   165
  | Theory (Context.Proof _) => "in local theory mode"
wenzelm@70064
   166
  | Proof _ => "in proof mode"
wenzelm@70064
   167
  | Skipped_Proof _ => "in skipped proof mode");
wenzelm@5946
   168
wenzelm@5946
   169
wenzelm@27576
   170
(* current node *)
wenzelm@5828
   171
wenzelm@70064
   172
fun is_toplevel state = (case node_of state of Toplevel => true | _ => false);
wenzelm@5828
   173
wenzelm@70064
   174
fun is_theory state =
wenzelm@70064
   175
  not (is_toplevel state) andalso is_some (theory_node (node_of state));
wenzelm@18589
   176
wenzelm@70064
   177
fun is_proof state =
wenzelm@70064
   178
  not (is_toplevel state) andalso is_some (proof_node (node_of state));
wenzelm@5828
   179
wenzelm@70064
   180
fun is_skipped_proof state =
wenzelm@70064
   181
  not (is_toplevel state) andalso skipped_proof_node (node_of state);
wenzelm@30801
   182
wenzelm@70064
   183
fun proper_node_of state = if is_toplevel state then raise UNDEF else node_of state;
wenzelm@70064
   184
fun proper_node_case f g state = cases_proper_node f g (proper_node_of state);
wenzelm@70064
   185
wenzelm@70064
   186
val context_of = proper_node_case Context.proof_of Proof.context_of;
wenzelm@70064
   187
val generic_theory_of = proper_node_case I (Context.Proof o Proof.context_of);
wenzelm@70064
   188
val theory_of = proper_node_case Context.theory_of Proof.theory_of;
wenzelm@70064
   189
val proof_of = proper_node_case (fn _ => error "No proof state") I;
wenzelm@17208
   190
wenzelm@18589
   191
fun proof_position_of state =
wenzelm@70064
   192
  (case proper_node_of state of
wenzelm@33390
   193
    Proof (prf, _) => Proof_Node.position prf
wenzelm@60096
   194
  | _ => ~1);
wenzelm@6664
   195
wenzelm@70064
   196
fun is_end_theory (State ((Toplevel, _), SOME _)) = true
wenzelm@68505
   197
  | is_end_theory _ = false;
wenzelm@68505
   198
wenzelm@70064
   199
fun end_theory _ (State ((Toplevel, _), SOME thy)) = thy
wenzelm@68869
   200
  | end_theory pos _ = error ("Malformed theory" ^ Position.here pos);
wenzelm@37953
   201
wenzelm@5828
   202
wenzelm@67381
   203
(* presentation context *)
wenzelm@67381
   204
wenzelm@67381
   205
structure Presentation_State = Proof_Data
wenzelm@67381
   206
(
wenzelm@67381
   207
  type T = state option;
wenzelm@67381
   208
  fun init _ = NONE;
wenzelm@67381
   209
);
wenzelm@67381
   210
wenzelm@70064
   211
fun presentation_context0 (State ((_, pr_ctxt), _)) = pr_ctxt;
wenzelm@67642
   212
wenzelm@67642
   213
fun presentation_context (state as State (current, _)) =
wenzelm@67642
   214
  presentation_context0 state
wenzelm@67642
   215
  |> Presentation_State.put (SOME (State (current, NONE)));
wenzelm@67381
   216
wenzelm@67381
   217
fun presentation_state ctxt =
wenzelm@67381
   218
  (case Presentation_State.get ctxt of
wenzelm@70067
   219
    NONE => State (node_presentation (Theory (Context.Proof ctxt)), NONE)
wenzelm@67381
   220
  | SOME state => state);
wenzelm@67381
   221
wenzelm@67381
   222
wenzelm@16815
   223
(* print state *)
wenzelm@16815
   224
wenzelm@56893
   225
fun pretty_context state =
wenzelm@70064
   226
  if is_toplevel state then []
wenzelm@70064
   227
  else
wenzelm@70064
   228
    let
wenzelm@70064
   229
      val gthy =
wenzelm@70064
   230
        (case node_of state of
wenzelm@70064
   231
          Toplevel => raise Match
wenzelm@70064
   232
        | Theory gthy => gthy
wenzelm@70064
   233
        | Proof (_, (_, gthy)) => gthy
wenzelm@70064
   234
        | Skipped_Proof (_, (_, gthy)) => gthy);
wenzelm@70064
   235
      val lthy = Context.cases Named_Target.theory_init I gthy;
wenzelm@70064
   236
    in Local_Theory.pretty lthy end;
wenzelm@16815
   237
wenzelm@56887
   238
fun pretty_state state =
wenzelm@70064
   239
  (case node_of state of
wenzelm@70064
   240
    Toplevel => []
wenzelm@70064
   241
  | Theory _ => []
wenzelm@70064
   242
  | Proof (prf, _) => Proof.pretty_state (Proof_Node.current prf)
wenzelm@70064
   243
  | Skipped_Proof (d, _) => [Pretty.str ("skipped proof: depth " ^ string_of_int d)]);
wenzelm@56867
   244
wenzelm@61208
   245
val string_of_state = pretty_state #> Pretty.chunks #> Pretty.string_of;
wenzelm@16815
   246
wenzelm@37858
   247
fun pretty_abstract state = Pretty.str ("<Isar " ^ str_of_state state ^ ">");
wenzelm@37858
   248
wenzelm@62819
   249
val _ = ML_system_pp (fn _ => fn _ => Pretty.to_polyml o pretty_abstract);
wenzelm@62663
   250
wenzelm@16815
   251
wenzelm@15668
   252
wenzelm@5828
   253
(** toplevel transitions **)
wenzelm@5828
   254
wenzelm@27601
   255
(* node transactions -- maintaining stable checkpoints *)
wenzelm@7022
   256
wenzelm@31476
   257
exception FAILURE of state * exn;
wenzelm@31476
   258
wenzelm@37953
   259
fun apply_transaction f g node =
wenzelm@20128
   260
  let
wenzelm@70067
   261
    val node_pr = node_presentation node;
wenzelm@70064
   262
    val context = cases_proper_node I (Context.Proof o Proof.context_of) node;
wenzelm@70064
   263
    fun state_error e node_pr' = (State (node_pr', get_theory node), e);
wenzelm@6689
   264
wenzelm@20128
   265
    val (result, err) =
wenzelm@70059
   266
      node
wenzelm@56303
   267
      |> Runtime.controlled_execution (SOME context) f
wenzelm@26624
   268
      |> state_error NONE
wenzelm@70059
   269
      handle exn => state_error (SOME exn) node_pr;
wenzelm@20128
   270
  in
wenzelm@52696
   271
    (case err of
wenzelm@52696
   272
      NONE => tap g result
wenzelm@52696
   273
    | SOME exn => raise FAILURE (result, exn))
wenzelm@20128
   274
  end;
wenzelm@6689
   275
wenzelm@6689
   276
wenzelm@6689
   277
(* primitive transitions *)
wenzelm@6689
   278
wenzelm@5828
   279
datatype trans =
wenzelm@70059
   280
  (*init theory*)
wenzelm@70059
   281
  Init of unit -> theory |
wenzelm@70059
   282
  (*formal exit of theory*)
wenzelm@70059
   283
  Exit |
wenzelm@70059
   284
  (*peek at state*)
wenzelm@70059
   285
  Keep of bool -> state -> unit |
wenzelm@70059
   286
  (*node transaction and presentation*)
wenzelm@70059
   287
  Transaction of (bool -> node -> node_presentation) * (state -> unit);
wenzelm@21958
   288
wenzelm@6689
   289
local
wenzelm@5828
   290
wenzelm@70068
   291
fun apply_tr int trans state =
wenzelm@70068
   292
  (case (trans, node_of state) of
wenzelm@70068
   293
    (Init f, Toplevel) =>
wenzelm@70068
   294
      Runtime.controlled_execution NONE (fn () =>
wenzelm@70068
   295
        State (node_presentation (Theory (Context.Theory (f ()))), NONE)) ()
wenzelm@70068
   296
  | (Exit, node as Theory (Context.Theory thy)) =>
wenzelm@70064
   297
      let
wenzelm@70064
   298
        val State ((node', pr_ctxt), _) =
wenzelm@70064
   299
          node |> apply_transaction
wenzelm@70067
   300
            (fn _ => node_presentation (Theory (Context.Theory (Theory.end_theory thy))))
wenzelm@70064
   301
            (K ());
wenzelm@70064
   302
      in State ((Toplevel, pr_ctxt), get_theory node') end
wenzelm@70068
   303
  | (Keep f, node) =>
wenzelm@70068
   304
      Runtime.controlled_execution (try generic_theory_of state)
wenzelm@70068
   305
        (fn () => (f int state; State (node_presentation node, previous_theory_of state))) ()
wenzelm@70068
   306
  | (Transaction _, Toplevel) => raise UNDEF
wenzelm@70068
   307
  | (Transaction (f, g), node) => apply_transaction (fn x => f int x) g node
wenzelm@70068
   308
  | _ => raise UNDEF);
wenzelm@5828
   309
wenzelm@32792
   310
fun apply_union _ [] state = raise FAILURE (state, UNDEF)
wenzelm@32792
   311
  | apply_union int (tr :: trs) state =
wenzelm@32792
   312
      apply_union int trs state
wenzelm@32792
   313
        handle Runtime.UNDEF => apply_tr int tr state
wenzelm@32792
   314
          | FAILURE (alt_state, UNDEF) => apply_tr int tr alt_state
wenzelm@6689
   315
          | exn as FAILURE _ => raise exn
wenzelm@6689
   316
          | exn => raise FAILURE (state, exn);
wenzelm@6689
   317
wenzelm@70068
   318
fun apply_markers markers (state as State ((node, pr_ctxt), prev_thy)) =
wenzelm@70068
   319
  let
wenzelm@70068
   320
    val state' =
wenzelm@70068
   321
      Runtime.controlled_execution (try generic_theory_of state)
wenzelm@70068
   322
        (fn () => State ((node, fold Document_Marker.evaluate markers pr_ctxt), prev_thy)) ();
wenzelm@70068
   323
  in (state', NONE) end
wenzelm@70068
   324
  handle exn => (state, SOME exn);
wenzelm@70068
   325
wenzelm@6689
   326
in
wenzelm@6689
   327
wenzelm@70068
   328
fun apply_trans int trans markers state =
wenzelm@70068
   329
  (apply_union int trans state |> apply_markers markers)
skalberg@15531
   330
  handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn);
wenzelm@6689
   331
wenzelm@6689
   332
end;
wenzelm@5828
   333
wenzelm@5828
   334
wenzelm@5828
   335
(* datatype transition *)
wenzelm@5828
   336
wenzelm@5828
   337
datatype transition = Transition of
wenzelm@70068
   338
 {name: string,               (*command name*)
wenzelm@70068
   339
  pos: Position.T,            (*source position*)
wenzelm@70068
   340
  markers: Input.source list, (*semantic document markers*)
wenzelm@70068
   341
  timing: Time.time,          (*prescient timing information*)
wenzelm@70068
   342
  trans: trans list};         (*primitive transitions (union)*)
wenzelm@5828
   343
wenzelm@70068
   344
fun make_transition (name, pos, markers, timing, trans) =
wenzelm@70068
   345
  Transition {name = name, pos = pos, markers = markers, timing = timing, trans = trans};
wenzelm@5828
   346
wenzelm@70068
   347
fun map_transition f (Transition {name, pos, markers, timing, trans}) =
wenzelm@70068
   348
  make_transition (f (name, pos, markers, timing, trans));
wenzelm@5828
   349
wenzelm@70068
   350
val empty = make_transition ("", Position.none, [], Time.zeroTime, []);
wenzelm@5828
   351
wenzelm@5828
   352
wenzelm@5828
   353
(* diagnostics *)
wenzelm@5828
   354
wenzelm@27427
   355
fun name_of (Transition {name, ...}) = name;
wenzelm@28105
   356
fun pos_of (Transition {pos, ...}) = pos;
wenzelm@70058
   357
fun timing_of (Transition {timing, ...}) = timing;
wenzelm@5828
   358
wenzelm@60076
   359
fun command_msg msg tr =
wenzelm@60076
   360
  msg ^ "command " ^ quote (Markup.markup Markup.keyword1 (name_of tr)) ^
wenzelm@60076
   361
    Position.here (pos_of tr);
wenzelm@5828
   362
wenzelm@60076
   363
fun at_command tr = command_msg "At " tr;
wenzelm@60076
   364
fun type_error tr = command_msg "Bad context for " tr;
wenzelm@5828
   365
wenzelm@5828
   366
wenzelm@5828
   367
(* modify transitions *)
wenzelm@5828
   368
wenzelm@70068
   369
fun name name = map_transition (fn (_, pos, markers, timing, trans) =>
wenzelm@70068
   370
  (name, pos, markers, timing, trans));
wenzelm@9010
   371
wenzelm@70068
   372
fun position pos = map_transition (fn (name, _, markers, timing, trans) =>
wenzelm@70068
   373
  (name, pos, markers, timing, trans));
wenzelm@70068
   374
wenzelm@70068
   375
fun markers markers = map_transition (fn (name, pos, _, timing, trans) =>
wenzelm@70068
   376
  (name, pos, markers, timing, trans));
wenzelm@14923
   377
wenzelm@70068
   378
fun timing timing = map_transition (fn (name, pos, markers, _, trans) =>
wenzelm@70068
   379
  (name, pos, markers, timing, trans));
wenzelm@70058
   380
wenzelm@70068
   381
fun add_trans tr = map_transition (fn (name, pos, markers, timing, trans) =>
wenzelm@70068
   382
  (name, pos, markers, timing, tr :: trans));
wenzelm@16607
   383
wenzelm@70068
   384
val reset_trans = map_transition (fn (name, pos, markers, timing, _) =>
wenzelm@70068
   385
  (name, pos, markers, timing, []));
wenzelm@5828
   386
wenzelm@5828
   387
wenzelm@21007
   388
(* basic transitions *)
wenzelm@5828
   389
wenzelm@44187
   390
fun init_theory f = add_trans (Init f);
wenzelm@37977
   391
wenzelm@44187
   392
fun is_init (Transition {trans = [Init _], ...}) = true
wenzelm@44187
   393
  | is_init _ = false;
wenzelm@44187
   394
wenzelm@44187
   395
fun modify_init f tr = if is_init tr then init_theory f (reset_trans tr) else tr;
wenzelm@37977
   396
wenzelm@6689
   397
val exit = add_trans Exit;
wenzelm@7612
   398
val keep' = add_trans o Keep;
wenzelm@30366
   399
wenzelm@30366
   400
fun present_transaction f g = add_trans (Transaction (f, g));
wenzelm@30366
   401
fun transaction f = present_transaction f (K ());
wenzelm@70067
   402
fun transaction0 f = present_transaction (node_presentation oo f) (K ());
wenzelm@5828
   403
wenzelm@7612
   404
fun keep f = add_trans (Keep (fn _ => f));
wenzelm@5828
   405
wenzelm@60190
   406
fun keep_proof f =
wenzelm@60190
   407
  keep (fn st =>
wenzelm@60190
   408
    if is_proof st then f st
wenzelm@60190
   409
    else if is_skipped_proof st then ()
wenzelm@60190
   410
    else warning "No proof state");
wenzelm@60190
   411
wenzelm@60189
   412
fun ignored pos = empty |> name "<ignored>" |> position pos |> keep (fn _ => ());
wenzelm@51268
   413
fun is_ignored tr = name_of tr = "<ignored>";
wenzelm@48772
   414
wenzelm@27840
   415
fun malformed pos msg =
wenzelm@60189
   416
  empty |> name "<malformed>" |> position pos |> keep (fn _ => error msg);
wenzelm@27840
   417
wenzelm@21007
   418
wenzelm@49012
   419
(* theory transitions *)
wenzelm@44304
   420
wenzelm@27601
   421
fun generic_theory f = transaction (fn _ =>
wenzelm@70067
   422
  (fn Theory gthy => node_presentation (Theory (f gthy))
wenzelm@26491
   423
    | _ => raise UNDEF));
wenzelm@26491
   424
wenzelm@27601
   425
fun theory' f = transaction (fn int =>
wenzelm@70059
   426
  (fn Theory (Context.Theory thy) =>
wenzelm@33725
   427
      let val thy' = thy
wenzelm@49012
   428
        |> Sign.new_group
wenzelm@33725
   429
        |> f int
wenzelm@33725
   430
        |> Sign.reset_group;
wenzelm@70067
   431
      in node_presentation (Theory (Context.Theory thy')) end
wenzelm@20963
   432
    | _ => raise UNDEF));
wenzelm@20963
   433
wenzelm@20963
   434
fun theory f = theory' (K f);
wenzelm@20963
   435
wenzelm@27601
   436
fun begin_local_theory begin f = transaction (fn _ =>
wenzelm@70059
   437
  (fn Theory (Context.Theory thy) =>
wenzelm@20963
   438
        let
wenzelm@20985
   439
          val lthy = f thy;
haftmann@57483
   440
          val gthy = if begin then Context.Proof lthy else Context.Theory (Named_Target.exit lthy);
wenzelm@56897
   441
          val _ =
wenzelm@60245
   442
            (case Local_Theory.pretty lthy of
wenzelm@60245
   443
              [] => ()
wenzelm@60245
   444
            | prts => Output.state (Pretty.string_of (Pretty.chunks prts)));
wenzelm@70059
   445
        in (Theory gthy, lthy) end
wenzelm@20963
   446
    | _ => raise UNDEF));
wenzelm@17076
   447
wenzelm@27601
   448
val end_local_theory = transaction (fn _ =>
wenzelm@70059
   449
  (fn Theory (Context.Proof lthy) => (Theory (Context.Theory (Named_Target.exit lthy)), lthy)
wenzelm@21007
   450
    | _ => raise UNDEF));
wenzelm@21007
   451
wenzelm@70059
   452
fun open_target f = transaction0 (fn _ =>
wenzelm@70059
   453
  (fn Theory gthy =>
wenzelm@47069
   454
        let val lthy = f gthy
wenzelm@70059
   455
        in Theory (Context.Proof lthy) end
wenzelm@47069
   456
    | _ => raise UNDEF));
wenzelm@47069
   457
wenzelm@47069
   458
val close_target = transaction (fn _ =>
wenzelm@70059
   459
  (fn Theory (Context.Proof lthy) =>
wenzelm@47069
   460
        (case try Local_Theory.close_target lthy of
wenzelm@50739
   461
          SOME ctxt' =>
wenzelm@50739
   462
            let
wenzelm@50739
   463
              val gthy' =
wenzelm@50739
   464
                if can Local_Theory.assert ctxt'
wenzelm@50739
   465
                then Context.Proof ctxt'
wenzelm@50739
   466
                else Context.Theory (Proof_Context.theory_of ctxt');
wenzelm@70059
   467
            in (Theory gthy', lthy) end
wenzelm@47069
   468
        | NONE => raise UNDEF)
wenzelm@47069
   469
    | _ => raise UNDEF));
wenzelm@47069
   470
wenzelm@59990
   471
fun restricted_context (SOME (strict, scope)) =
wenzelm@59990
   472
      Proof_Context.map_naming (Name_Space.restricted strict scope)
wenzelm@59990
   473
  | restricted_context NONE = I;
wenzelm@59939
   474
wenzelm@59990
   475
fun local_theory' restricted target f = present_transaction (fn int =>
wenzelm@70059
   476
  (fn Theory gthy =>
wenzelm@21294
   477
        let
wenzelm@59923
   478
          val (finish, lthy) = Named_Target.switch target gthy;
wenzelm@47274
   479
          val lthy' = lthy
wenzelm@59990
   480
            |> restricted_context restricted
wenzelm@49012
   481
            |> Local_Theory.new_group
wenzelm@33725
   482
            |> f int
wenzelm@33725
   483
            |> Local_Theory.reset_group;
wenzelm@70059
   484
        in (Theory (finish lthy'), lthy') end
wenzelm@59032
   485
    | _ => raise UNDEF))
wenzelm@59032
   486
  (K ());
wenzelm@15668
   487
wenzelm@59990
   488
fun local_theory restricted target f = local_theory' restricted target (K f);
wenzelm@21007
   489
wenzelm@65054
   490
fun present_local_theory target = present_transaction (fn _ =>
wenzelm@70059
   491
  (fn Theory gthy =>
wenzelm@59923
   492
        let val (finish, lthy) = Named_Target.switch target gthy;
wenzelm@70059
   493
        in (Theory (finish lthy), lthy) end
wenzelm@59032
   494
    | _ => raise UNDEF));
wenzelm@21007
   495
wenzelm@21007
   496
wenzelm@21007
   497
(* proof transitions *)
wenzelm@21007
   498
wenzelm@27601
   499
fun end_proof f = transaction (fn int =>
wenzelm@24795
   500
  (fn Proof (prf, (finish, _)) =>
wenzelm@33390
   501
        let val state = Proof_Node.current prf in
wenzelm@21007
   502
          if can (Proof.assert_bottom true) state then
wenzelm@21007
   503
            let
wenzelm@21007
   504
              val ctxt' = f int state;
wenzelm@21007
   505
              val gthy' = finish ctxt';
wenzelm@70059
   506
            in (Theory gthy', ctxt') end
wenzelm@21007
   507
          else raise UNDEF
wenzelm@21007
   508
        end
wenzelm@70067
   509
    | Skipped_Proof (0, (gthy, _)) => node_presentation (Theory gthy)
wenzelm@21007
   510
    | _ => raise UNDEF));
wenzelm@21007
   511
wenzelm@21294
   512
local
wenzelm@21294
   513
wenzelm@70063
   514
fun begin_proof init_proof = transaction0 (fn int =>
wenzelm@70059
   515
  (fn Theory gthy =>
wenzelm@21294
   516
    let
wenzelm@70063
   517
      val (finish, prf) = init_proof int gthy;
wenzelm@67157
   518
      val document = Options.default_string "document";
wenzelm@67157
   519
      val skip = (document = "" orelse document = "false") andalso Goal.skip_proofs_enabled ();
wenzelm@58795
   520
      val schematic_goal = try Proof.schematic_goal prf;
wenzelm@47274
   521
      val _ =
wenzelm@58795
   522
        if skip andalso schematic_goal = SOME true then
wenzelm@47274
   523
          warning "Cannot skip proof of schematic goal statement"
wenzelm@47274
   524
        else ();
wenzelm@21294
   525
    in
wenzelm@58795
   526
      if skip andalso schematic_goal = SOME false then
wenzelm@51555
   527
        Skipped_Proof (0, (finish (Proof.global_skip_proof true prf), gthy))
wenzelm@47274
   528
      else Proof (Proof_Node.init prf, (finish, gthy))
wenzelm@21294
   529
    end
wenzelm@21294
   530
  | _ => raise UNDEF));
wenzelm@21294
   531
wenzelm@21294
   532
in
wenzelm@21294
   533
wenzelm@59990
   534
fun local_theory_to_proof' restricted target f = begin_proof
wenzelm@47274
   535
  (fn int => fn gthy =>
wenzelm@59923
   536
    let
wenzelm@59923
   537
      val (finish, lthy) = Named_Target.switch target gthy;
wenzelm@59923
   538
      val prf = lthy
wenzelm@59990
   539
        |> restricted_context restricted
wenzelm@59923
   540
        |> Local_Theory.new_group
wenzelm@59923
   541
        |> f int;
wenzelm@59923
   542
    in (finish o Local_Theory.reset_group, prf) end);
wenzelm@24780
   543
wenzelm@59990
   544
fun local_theory_to_proof restricted target f =
wenzelm@59990
   545
  local_theory_to_proof' restricted target (K f);
wenzelm@21294
   546
wenzelm@21294
   547
fun theory_to_proof f = begin_proof
wenzelm@47274
   548
  (fn _ => fn gthy =>
wenzelm@56057
   549
    (Context.Theory o Sign.reset_group o Sign.change_check o Proof_Context.theory_of,
wenzelm@49062
   550
      (case gthy of
wenzelm@52788
   551
        Context.Theory thy => f (Sign.new_group thy)
wenzelm@49012
   552
      | _ => raise UNDEF)));
wenzelm@21294
   553
wenzelm@21294
   554
end;
wenzelm@21294
   555
wenzelm@70059
   556
val forget_proof = transaction0 (fn _ =>
wenzelm@58798
   557
  (fn Proof (prf, (_, orig_gthy)) =>
wenzelm@68876
   558
        if Proof.is_notepad (Proof_Node.current prf) then raise UNDEF
wenzelm@70059
   559
        else Theory orig_gthy
wenzelm@70059
   560
    | Skipped_Proof (_, (_, orig_gthy)) => Theory orig_gthy
wenzelm@21007
   561
    | _ => raise UNDEF));
wenzelm@21007
   562
wenzelm@70059
   563
fun proofs' f = transaction0 (fn int =>
wenzelm@49062
   564
  (fn Proof (prf, x) => Proof (Proof_Node.applys (f int) prf, x)
wenzelm@51555
   565
    | skip as Skipped_Proof _ => skip
wenzelm@16815
   566
    | _ => raise UNDEF));
wenzelm@15668
   567
wenzelm@49863
   568
fun proof' f = proofs' ((Seq.single o Seq.Result) oo f);
wenzelm@17904
   569
val proofs = proofs' o K;
wenzelm@6689
   570
val proof = proof' o K;
wenzelm@16815
   571
wenzelm@60695
   572
wenzelm@60695
   573
(* skipped proofs *)
wenzelm@60695
   574
wenzelm@70059
   575
fun actual_proof f = transaction0 (fn _ =>
wenzelm@21007
   576
  (fn Proof (prf, x) => Proof (f prf, x)
wenzelm@20963
   577
    | _ => raise UNDEF));
wenzelm@16815
   578
wenzelm@70059
   579
fun skip_proof f = transaction0 (fn _ =>
wenzelm@60695
   580
  (fn skip as Skipped_Proof _ => (f (); skip)
wenzelm@18563
   581
    | _ => raise UNDEF));
wenzelm@18563
   582
wenzelm@70059
   583
val skip_proof_open = transaction0 (fn _ =>
wenzelm@60695
   584
  (fn Skipped_Proof (d, x) => Skipped_Proof (d + 1, x)
wenzelm@60695
   585
    | _ => raise UNDEF));
wenzelm@60695
   586
wenzelm@70059
   587
val skip_proof_close = transaction0 (fn _ =>
wenzelm@70059
   588
  (fn Skipped_Proof (0, (gthy, _)) => Theory gthy
wenzelm@60695
   589
    | Skipped_Proof (d, x) => Skipped_Proof (d - 1, x)
wenzelm@33725
   590
    | _ => raise UNDEF));
wenzelm@5828
   591
wenzelm@5828
   592
wenzelm@5828
   593
wenzelm@5828
   594
(** toplevel transactions **)
wenzelm@5828
   595
wenzelm@52527
   596
(* runtime position *)
wenzelm@27427
   597
wenzelm@52536
   598
fun exec_id id (tr as Transition {pos, ...}) =
wenzelm@52536
   599
  position (Position.put_id (Document_ID.print id) pos) tr;
wenzelm@25799
   600
wenzelm@25960
   601
fun setmp_thread_position (Transition {pos, ...}) f x =
wenzelm@25819
   602
  Position.setmp_thread_data pos f x;
wenzelm@25799
   603
wenzelm@25799
   604
wenzelm@28095
   605
(* post-transition hooks *)
wenzelm@28095
   606
wenzelm@37905
   607
local
wenzelm@56147
   608
  val hooks =
wenzelm@56147
   609
    Synchronized.var "Toplevel.hooks" ([]: (transition -> state -> state -> unit) list);
wenzelm@37905
   610
in
wenzelm@28095
   611
wenzelm@56147
   612
fun add_hook hook = Synchronized.change hooks (cons hook);
wenzelm@56147
   613
fun get_hooks () = Synchronized.value hooks;
wenzelm@28095
   614
wenzelm@28095
   615
end;
wenzelm@28095
   616
wenzelm@28095
   617
wenzelm@5828
   618
(* apply transitions *)
wenzelm@5828
   619
wenzelm@6664
   620
local
wenzelm@6664
   621
wenzelm@70068
   622
fun app int (tr as Transition {markers, trans, ...}) =
wenzelm@67932
   623
  setmp_thread_position tr
wenzelm@70068
   624
    (Timing.protocol (name_of tr) (pos_of tr) (apply_trans int trans markers)
wenzelm@67932
   625
      ##> Option.map (fn UNDEF => ERROR (type_error tr) | exn => exn));
wenzelm@6664
   626
wenzelm@6664
   627
in
wenzelm@5828
   628
wenzelm@26602
   629
fun transition int tr st =
wenzelm@28095
   630
  let
wenzelm@60895
   631
    val (st', opt_err) =
wenzelm@67642
   632
      Context.setmp_generic_context (try (Context.Proof o presentation_context0) st)
wenzelm@60895
   633
        (fn () => app int tr st) ();
wenzelm@59055
   634
    val opt_err' = opt_err |> Option.map
wenzelm@59055
   635
      (fn Runtime.EXCURSION_FAIL exn_info => exn_info
wenzelm@59055
   636
        | exn => (Runtime.exn_context (try context_of st) exn, at_command tr));
wenzelm@59055
   637
    val _ = get_hooks () |> List.app (fn f => (try (fn () => f tr st st') (); ()));
wenzelm@59055
   638
  in (st', opt_err') end;
wenzelm@6664
   639
wenzelm@6664
   640
end;
wenzelm@5828
   641
wenzelm@5828
   642
wenzelm@51284
   643
(* managed commands *)
wenzelm@5828
   644
wenzelm@51323
   645
fun command_errors int tr st =
wenzelm@51323
   646
  (case transition int tr st of
wenzelm@59055
   647
    (st', NONE) => ([], SOME st')
wenzelm@65948
   648
  | (_, SOME (exn, _)) => (Runtime.exn_messages exn, NONE));
wenzelm@51323
   649
wenzelm@51284
   650
fun command_exception int tr st =
wenzelm@51284
   651
  (case transition int tr st of
wenzelm@59055
   652
    (st', NONE) => st'
wenzelm@59055
   653
  | (_, SOME (exn, info)) =>
wenzelm@62505
   654
      if Exn.is_interrupt exn then Exn.reraise exn
wenzelm@59055
   655
      else raise Runtime.EXCURSION_FAIL (exn, info));
wenzelm@27576
   656
wenzelm@58848
   657
val command = command_exception false;
wenzelm@51284
   658
wenzelm@28433
   659
wenzelm@56937
   660
(* reset state *)
wenzelm@56937
   661
wenzelm@56937
   662
local
wenzelm@56937
   663
wenzelm@56937
   664
fun reset_state check trans st =
wenzelm@56937
   665
  if check st then NONE
wenzelm@56937
   666
  else #2 (command_errors false (trans empty) st);
wenzelm@56937
   667
wenzelm@56937
   668
in
wenzelm@56937
   669
wenzelm@68876
   670
val reset_theory = reset_state is_theory forget_proof;
wenzelm@56937
   671
wenzelm@56937
   672
val reset_proof =
wenzelm@56937
   673
  reset_state is_proof
wenzelm@70059
   674
    (transaction0 (fn _ =>
wenzelm@70059
   675
      (fn Theory gthy => Skipped_Proof (0, (gthy, gthy))
wenzelm@56937
   676
        | _ => raise UNDEF)));
wenzelm@56937
   677
wenzelm@68877
   678
val reset_notepad =
wenzelm@68877
   679
  reset_state
wenzelm@68877
   680
    (fn st =>
wenzelm@68877
   681
      (case try proof_of st of
wenzelm@68877
   682
        SOME state => not (Proof.is_notepad state) orelse can Proof.end_notepad state
wenzelm@68877
   683
      | NONE => true))
wenzelm@68878
   684
    (proof Proof.reset_notepad);
wenzelm@68877
   685
wenzelm@56937
   686
end;
wenzelm@56937
   687
wenzelm@56937
   688
wenzelm@46959
   689
(* scheduled proof result *)
wenzelm@28433
   690
wenzelm@51332
   691
datatype result =
wenzelm@51332
   692
  Result of transition * state |
wenzelm@51332
   693
  Result_List of result list |
wenzelm@51332
   694
  Result_Future of result future;
wenzelm@51332
   695
wenzelm@51332
   696
fun join_results (Result x) = [x]
wenzelm@51332
   697
  | join_results (Result_List xs) = maps join_results xs
wenzelm@51332
   698
  | join_results (Result_Future x) = join_results (Future.join x);
wenzelm@51332
   699
wenzelm@51323
   700
local
wenzelm@51323
   701
wenzelm@47417
   702
structure Result = Proof_Data
wenzelm@28974
   703
(
wenzelm@51332
   704
  type T = result;
wenzelm@59150
   705
  fun init _ = Result_List [];
wenzelm@28974
   706
);
wenzelm@28974
   707
wenzelm@51332
   708
val get_result = Result.get o Proof.context_of;
wenzelm@51332
   709
val put_result = Proof.map_context o Result.put;
wenzelm@51324
   710
wenzelm@66170
   711
fun timing_estimate elem =
wenzelm@68839
   712
  let val trs = tl (Thy_Element.flat_element elem)
wenzelm@70058
   713
  in fold (fn tr => fn t => timing_of tr + t) trs Time.zeroTime end;
wenzelm@51423
   714
wenzelm@68132
   715
fun future_proofs_enabled estimate st =
wenzelm@51324
   716
  (case try proof_of st of
wenzelm@51324
   717
    NONE => false
wenzelm@51324
   718
  | SOME state =>
wenzelm@51324
   719
      not (Proof.is_relevant state) andalso
wenzelm@51324
   720
       (if can (Proof.assert_bottom true) state
wenzelm@68132
   721
        then Future.proofs_enabled 1
wenzelm@68132
   722
        else Future.proofs_enabled 2 orelse Future.proofs_enabled_timing estimate));
wenzelm@51278
   723
wenzelm@70068
   724
val empty_markers = markers [];
wenzelm@70068
   725
val empty_trans = reset_trans #> keep (K ());
wenzelm@70068
   726
wenzelm@70068
   727
in
wenzelm@70068
   728
wenzelm@70068
   729
fun fork_presentation tr = (tr |> empty_markers, tr |> empty_trans);
wenzelm@70068
   730
wenzelm@58923
   731
fun atom_result keywords tr st =
wenzelm@51323
   732
  let
wenzelm@51323
   733
    val st' =
wenzelm@68132
   734
      if Future.proofs_enabled 1 andalso Keyword.is_diag keywords (name_of tr) then
wenzelm@70068
   735
        let
wenzelm@70068
   736
          val (tr1, tr2) = fork_presentation tr;
wenzelm@70068
   737
          val _ =
wenzelm@70068
   738
            Execution.fork {name = "Toplevel.diag", pos = pos_of tr, pri = ~1}
wenzelm@70068
   739
              (fn () => command tr1 st);
wenzelm@70068
   740
        in command tr2 st end
wenzelm@51323
   741
      else command tr st;
wenzelm@51332
   742
  in (Result (tr, st'), st') end;
wenzelm@51323
   743
wenzelm@68839
   744
fun element_result keywords (Thy_Element.Element (tr, NONE)) st = atom_result keywords tr st
wenzelm@68839
   745
  | element_result keywords (elem as Thy_Element.Element (head_tr, SOME element_rest)) st =
wenzelm@48633
   746
      let
wenzelm@58923
   747
        val (head_result, st') = atom_result keywords head_tr st;
wenzelm@51332
   748
        val (body_elems, end_tr) = element_rest;
wenzelm@66170
   749
        val estimate = timing_estimate elem;
wenzelm@51324
   750
      in
wenzelm@68132
   751
        if not (future_proofs_enabled estimate st')
wenzelm@51324
   752
        then
wenzelm@51332
   753
          let
wenzelm@68839
   754
            val proof_trs = maps Thy_Element.flat_element body_elems @ [end_tr];
wenzelm@58923
   755
            val (proof_results, st'') = fold_map (atom_result keywords) proof_trs st';
wenzelm@51332
   756
          in (Result_List (head_result :: proof_results), st'') end
wenzelm@51324
   757
        else
wenzelm@51324
   758
          let
wenzelm@70068
   759
            val (end_tr1, end_tr2) = fork_presentation end_tr;
wenzelm@70068
   760
wenzelm@51324
   761
            val finish = Context.Theory o Proof_Context.theory_of;
wenzelm@28974
   762
wenzelm@51605
   763
            val future_proof =
wenzelm@51605
   764
              Proof.future_proof (fn state =>
wenzelm@53192
   765
                Execution.fork
wenzelm@66170
   766
                  {name = "Toplevel.future_proof", pos = pos_of head_tr, pri = ~1}
wenzelm@51605
   767
                  (fn () =>
wenzelm@51605
   768
                    let
wenzelm@70064
   769
                      val State ((Proof (prf, (_, orig_gthy)), _), prev_thy) = st';
wenzelm@70059
   770
                      val node' = Proof (Proof_Node.apply (K state) prf, (finish, orig_gthy));
wenzelm@70068
   771
                      val (results, result_state) =
wenzelm@70067
   772
                        State (node_presentation node', prev_thy)
wenzelm@70068
   773
                        |> fold_map (element_result keywords) body_elems ||> command end_tr1;
wenzelm@70068
   774
                    in (Result_List results, presentation_context0 result_state) end))
wenzelm@51332
   775
              #> (fn (res, state') => state' |> put_result (Result_Future res));
wenzelm@51332
   776
wenzelm@51332
   777
            val forked_proof =
wenzelm@51332
   778
              proof (future_proof #>
wenzelm@51332
   779
                (fn state => state |> Proof.local_done_proof |> put_result (get_result state))) o
wenzelm@51332
   780
              end_proof (fn _ => future_proof #>
wenzelm@51332
   781
                (fn state => state |> Proof.global_done_proof |> Result.put (get_result state)));
wenzelm@28974
   782
wenzelm@70068
   783
            val st'' = st' |> command (head_tr |> reset_trans |> forked_proof);
wenzelm@70068
   784
            val end_st = st'' |> command end_tr2;
wenzelm@70068
   785
            val end_result = Result (end_tr, end_st);
wenzelm@51324
   786
            val result =
wenzelm@67642
   787
              Result_List [head_result, Result.get (presentation_context0 st''), end_result];
wenzelm@70068
   788
          in (result, end_st) end
wenzelm@51324
   789
      end;
wenzelm@28433
   790
wenzelm@6664
   791
end;
wenzelm@51323
   792
wenzelm@51323
   793
end;
wenzelm@69735
   794
haftmann@69724
   795
structure Local_Theory : LOCAL_THEORY = struct open Local_Theory; end;