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