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