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