src/Pure/Isar/toplevel.ML
author wenzelm
Sat Nov 22 15:27:48 2014 +0100 (2014-11-22 ago)
changeset 59032 f36496364ce1
parent 59000 ed09ae4ea2d8
child 59055 5a7157b8e870
permissions -rw-r--r--
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@56893
    25
  val pretty_context: state -> Pretty.T list
wenzelm@56887
    26
  val pretty_state: state -> Pretty.T list
wenzelm@56887
    27
  val print_state: state -> unit
wenzelm@37858
    28
  val pretty_abstract: state -> Pretty.T
wenzelm@32738
    29
  val profiling: int Unsynchronized.ref
wenzelm@16682
    30
  type transition
wenzelm@5828
    31
  val empty: transition
wenzelm@27427
    32
  val name_of: transition -> string
wenzelm@28105
    33
  val pos_of: transition -> Position.T
wenzelm@56937
    34
  val type_error: transition -> state -> string
wenzelm@5828
    35
  val name: string -> transition -> transition
wenzelm@5828
    36
  val position: Position.T -> transition -> transition
wenzelm@44187
    37
  val init_theory: (unit -> theory) -> transition -> transition
wenzelm@44187
    38
  val is_init: transition -> bool
wenzelm@44186
    39
  val modify_init: (unit -> theory) -> transition -> transition
wenzelm@6689
    40
  val exit: transition -> transition
wenzelm@5828
    41
  val keep: (state -> unit) -> transition -> transition
wenzelm@7612
    42
  val keep': (bool -> state -> unit) -> transition -> transition
wenzelm@5828
    43
  val imperative: (unit -> unit) -> transition -> transition
wenzelm@27840
    44
  val ignored: Position.T -> transition
wenzelm@51268
    45
  val is_ignored: transition -> bool
wenzelm@27840
    46
  val malformed: Position.T -> string -> transition
wenzelm@48772
    47
  val is_malformed: transition -> bool
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@45488
    55
  val local_theory': (xstring * Position.T) option -> (bool -> local_theory -> local_theory) ->
wenzelm@45488
    56
    transition -> transition
wenzelm@45488
    57
  val local_theory: (xstring * Position.T) option -> (local_theory -> local_theory) ->
wenzelm@29380
    58
    transition -> transition
wenzelm@45488
    59
  val present_local_theory: (xstring * Position.T) option -> (state -> unit) ->
berghofe@24453
    60
    transition -> transition
wenzelm@45488
    61
  val local_theory_to_proof': (xstring * Position.T) option ->
wenzelm@45488
    62
    (bool -> local_theory -> Proof.state) -> transition -> transition
wenzelm@45488
    63
  val local_theory_to_proof: (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@27564
    73
  val skip_proof: (int -> int) -> transition -> transition
wenzelm@17904
    74
  val skip_proof_to_theory: (int -> bool) -> transition -> transition
wenzelm@52536
    75
  val exec_id: Document_ID.exec -> transition -> transition
wenzelm@9512
    76
  val unknown_theory: transition -> transition
wenzelm@9512
    77
  val unknown_proof: transition -> transition
wenzelm@9512
    78
  val unknown_context: transition -> transition
wenzelm@28425
    79
  val setmp_thread_position: transition -> ('a -> 'b) -> 'a -> 'b
wenzelm@28103
    80
  val add_hook: (transition -> state -> state -> unit) -> unit
wenzelm@51423
    81
  val get_timing: transition -> Time.time option
wenzelm@51423
    82
  val put_timing: Time.time option -> transition -> transition
wenzelm@26602
    83
  val transition: bool -> transition -> state -> (state * (exn * string) option) option
wenzelm@51323
    84
  val command_errors: bool -> transition -> state -> Runtime.error list * state option
wenzelm@51284
    85
  val command_exception: bool -> transition -> state -> state
wenzelm@56937
    86
  val reset_theory: state -> state option
wenzelm@56937
    87
  val reset_proof: state -> state option
wenzelm@51332
    88
  type result
wenzelm@51332
    89
  val join_results: result -> (transition * state) list
wenzelm@58923
    90
  val element_result: Keyword.keywords -> transition Thy_Syntax.element -> state -> result * state
wenzelm@5828
    91
end;
wenzelm@5828
    92
wenzelm@6965
    93
structure Toplevel: TOPLEVEL =
wenzelm@5828
    94
struct
wenzelm@5828
    95
wenzelm@5828
    96
(** toplevel state **)
wenzelm@5828
    97
wenzelm@31476
    98
exception UNDEF = Runtime.UNDEF;
wenzelm@19063
    99
wenzelm@19063
   100
wenzelm@21958
   101
(* datatype node *)
wenzelm@21294
   102
wenzelm@5828
   103
datatype node =
wenzelm@27576
   104
  Theory of generic_theory * Proof.context option
wenzelm@27576
   105
    (*theory with presentation context*) |
wenzelm@33390
   106
  Proof of Proof_Node.T * ((Proof.context -> generic_theory) * generic_theory)
wenzelm@27576
   107
    (*proof node, finish, original theory*) |
wenzelm@51555
   108
  Skipped_Proof of int * (generic_theory * generic_theory);
wenzelm@27564
   109
    (*proof depth, resulting theory, original theory*)
wenzelm@5828
   110
wenzelm@20963
   111
val theory_node = fn Theory (gthy, _) => SOME gthy | _ => NONE;
wenzelm@18589
   112
val proof_node = fn Proof (prf, _) => SOME prf | _ => NONE;
wenzelm@51555
   113
val skipped_proof_node = fn Skipped_Proof _ => true | _ => false;
wenzelm@18589
   114
wenzelm@20963
   115
fun cases_node f _ (Theory (gthy, _)) = f gthy
wenzelm@33390
   116
  | cases_node _ g (Proof (prf, _)) = g (Proof_Node.current prf)
wenzelm@51555
   117
  | cases_node f _ (Skipped_Proof (_, (gthy, _))) = f gthy;
wenzelm@19063
   118
wenzelm@29066
   119
val context_node = cases_node Context.proof_of Proof.context_of;
wenzelm@29066
   120
wenzelm@21958
   121
wenzelm@21958
   122
(* datatype state *)
wenzelm@21958
   123
wenzelm@37953
   124
datatype state = State of node option * node option;  (*current, previous*)
wenzelm@5828
   125
wenzelm@27576
   126
val toplevel = State (NONE, NONE);
wenzelm@5828
   127
wenzelm@27576
   128
fun is_toplevel (State (NONE, _)) = true
wenzelm@7732
   129
  | is_toplevel _ = false;
wenzelm@7732
   130
wenzelm@27576
   131
fun level (State (NONE, _)) = 0
wenzelm@37953
   132
  | level (State (SOME (Theory _), _)) = 0
wenzelm@37953
   133
  | level (State (SOME (Proof (prf, _)), _)) = Proof.level (Proof_Node.current prf)
wenzelm@51555
   134
  | level (State (SOME (Skipped_Proof (d, _)), _)) = d + 1;   (*different notion of proof depth!*)
wenzelm@17076
   135
wenzelm@52565
   136
fun str_of_state (State (NONE, SOME (Theory (Context.Theory thy, _)))) =
wenzelm@52565
   137
      "at top level, result theory " ^ quote (Context.theory_name thy)
wenzelm@52565
   138
  | str_of_state (State (NONE, _)) = "at top level"
wenzelm@37953
   139
  | str_of_state (State (SOME (Theory (Context.Theory _, _)), _)) = "in theory mode"
wenzelm@37953
   140
  | str_of_state (State (SOME (Theory (Context.Proof _, _)), _)) = "in local theory mode"
wenzelm@37953
   141
  | str_of_state (State (SOME (Proof _), _)) = "in proof mode"
wenzelm@51555
   142
  | str_of_state (State (SOME (Skipped_Proof _), _)) = "in skipped proof mode";
wenzelm@5946
   143
wenzelm@5946
   144
wenzelm@27576
   145
(* current node *)
wenzelm@5828
   146
wenzelm@27576
   147
fun node_of (State (NONE, _)) = raise UNDEF
wenzelm@37953
   148
  | node_of (State (SOME node, _)) = node;
wenzelm@5828
   149
wenzelm@18589
   150
fun is_theory state = not (is_toplevel state) andalso is_some (theory_node (node_of state));
wenzelm@18589
   151
fun is_proof state = not (is_toplevel state) andalso is_some (proof_node (node_of state));
wenzelm@51555
   152
fun is_skipped_proof state = not (is_toplevel state) andalso skipped_proof_node (node_of state);
wenzelm@18589
   153
wenzelm@19063
   154
fun node_case f g state = cases_node f g (node_of state);
wenzelm@5828
   155
wenzelm@30398
   156
fun presentation_context_of state =
wenzelm@30398
   157
  (case try node_of state of
wenzelm@30398
   158
    SOME (Theory (_, SOME ctxt)) => ctxt
wenzelm@30398
   159
  | SOME node => context_node node
wenzelm@30398
   160
  | NONE => raise UNDEF);
wenzelm@30366
   161
wenzelm@30801
   162
fun previous_context_of (State (_, NONE)) = NONE
wenzelm@37953
   163
  | previous_context_of (State (_, SOME prev)) = SOME (context_node prev);
wenzelm@30801
   164
wenzelm@21506
   165
val context_of = node_case Context.proof_of Proof.context_of;
wenzelm@22089
   166
val generic_theory_of = node_case I (Context.Proof o Proof.context_of);
wenzelm@20963
   167
val theory_of = node_case Context.theory_of Proof.theory_of;
wenzelm@18589
   168
val proof_of = node_case (fn _ => raise UNDEF) I;
wenzelm@17208
   169
wenzelm@18589
   170
fun proof_position_of state =
wenzelm@18589
   171
  (case node_of state of
wenzelm@33390
   172
    Proof (prf, _) => Proof_Node.position prf
wenzelm@18589
   173
  | _ => raise UNDEF);
wenzelm@6664
   174
wenzelm@43667
   175
fun end_theory _ (State (NONE, SOME (Theory (Context.Theory thy, _)))) = thy
wenzelm@48992
   176
  | end_theory pos (State (NONE, _)) = error ("Bad theory" ^ Position.here pos)
wenzelm@48992
   177
  | end_theory pos (State (SOME _, _)) = error ("Unfinished theory" ^ Position.here pos);
wenzelm@37953
   178
wenzelm@5828
   179
wenzelm@16815
   180
(* print state *)
wenzelm@16815
   181
wenzelm@56893
   182
fun pretty_context state =
wenzelm@24795
   183
  (case try node_of state of
wenzelm@21506
   184
    NONE => []
wenzelm@56893
   185
  | SOME node =>
wenzelm@56893
   186
      let
wenzelm@56893
   187
        val gthy =
wenzelm@56893
   188
          (case node of
wenzelm@56893
   189
            Theory (gthy, _) => gthy
wenzelm@56893
   190
          | Proof (_, (_, gthy)) => gthy
wenzelm@56893
   191
          | Skipped_Proof (_, (gthy, _)) => gthy);
wenzelm@56893
   192
        val lthy = Context.cases (Named_Target.theory_init) I gthy;
wenzelm@56893
   193
      in Local_Theory.pretty lthy end);
wenzelm@16815
   194
wenzelm@56887
   195
fun pretty_state state =
wenzelm@23701
   196
  (case try node_of state of
wenzelm@23701
   197
    NONE => []
wenzelm@56887
   198
  | SOME (Theory _) => []
wenzelm@23701
   199
  | SOME (Proof (prf, _)) =>
wenzelm@33390
   200
      Proof.pretty_state (Proof_Node.position prf) (Proof_Node.current prf)
wenzelm@56867
   201
  | SOME (Skipped_Proof (d, _)) => [Pretty.str ("skipped proof: depth " ^ string_of_int d)]);
wenzelm@56867
   202
wenzelm@56887
   203
val print_state = pretty_state #> Pretty.markup_chunks Markup.state #> Pretty.writeln;
wenzelm@16815
   204
wenzelm@37858
   205
fun pretty_abstract state = Pretty.str ("<Isar " ^ str_of_state state ^ ">");
wenzelm@37858
   206
wenzelm@16815
   207
wenzelm@15668
   208
wenzelm@5828
   209
(** toplevel transitions **)
wenzelm@5828
   210
wenzelm@32738
   211
val profiling = Unsynchronized.ref 0;
wenzelm@16682
   212
wenzelm@53709
   213
wenzelm@27601
   214
(* node transactions -- maintaining stable checkpoints *)
wenzelm@7022
   215
wenzelm@31476
   216
exception FAILURE of state * exn;
wenzelm@31476
   217
wenzelm@6689
   218
local
wenzelm@6689
   219
wenzelm@30366
   220
fun reset_presentation (Theory (gthy, _)) = Theory (gthy, NONE)
wenzelm@30366
   221
  | reset_presentation node = node;
wenzelm@30366
   222
wenzelm@27576
   223
fun map_theory f (Theory (gthy, ctxt)) =
wenzelm@33671
   224
      Theory (Context.mapping f (Local_Theory.raw_theory f) gthy, ctxt)
wenzelm@27576
   225
  | map_theory _ node = node;
wenzelm@6689
   226
wenzelm@6689
   227
in
wenzelm@6689
   228
wenzelm@37953
   229
fun apply_transaction f g node =
wenzelm@20128
   230
  let
wenzelm@27576
   231
    val cont_node = reset_presentation node;
wenzelm@56265
   232
    val context = cases_node I (Context.Proof o Proof.context_of) cont_node;
wenzelm@37953
   233
    fun state_error e nd = (State (SOME nd, SOME node), e);
wenzelm@6689
   234
wenzelm@20128
   235
    val (result, err) =
wenzelm@20128
   236
      cont_node
wenzelm@56303
   237
      |> Runtime.controlled_execution (SOME context) f
wenzelm@26624
   238
      |> state_error NONE
wenzelm@26624
   239
      handle exn => state_error (SOME exn) cont_node;
wenzelm@20128
   240
  in
wenzelm@52696
   241
    (case err of
wenzelm@52696
   242
      NONE => tap g result
wenzelm@52696
   243
    | SOME exn => raise FAILURE (result, exn))
wenzelm@20128
   244
  end;
wenzelm@6689
   245
wenzelm@43667
   246
val exit_transaction =
wenzelm@43667
   247
  apply_transaction
wenzelm@43667
   248
    (fn Theory (Context.Theory thy, _) => Theory (Context.Theory (Theory.end_theory thy), NONE)
wenzelm@43667
   249
      | node => node) (K ())
wenzelm@43667
   250
  #> (fn State (node', _) => State (NONE, node'));
wenzelm@43667
   251
wenzelm@6689
   252
end;
wenzelm@6689
   253
wenzelm@6689
   254
wenzelm@6689
   255
(* primitive transitions *)
wenzelm@6689
   256
wenzelm@5828
   257
datatype trans =
wenzelm@44187
   258
  Init of unit -> theory |               (*init theory*)
wenzelm@37953
   259
  Exit |                                 (*formal exit of theory*)
wenzelm@37953
   260
  Keep of bool -> state -> unit |        (*peek at state*)
wenzelm@30366
   261
  Transaction of (bool -> node -> node) * (state -> unit);  (*node transaction and presentation*)
wenzelm@21958
   262
wenzelm@6689
   263
local
wenzelm@5828
   264
wenzelm@44187
   265
fun apply_tr _ (Init f) (State (NONE, _)) =
wenzelm@56303
   266
      State (SOME (Theory (Context.Theory (Runtime.controlled_execution NONE f ()), NONE)), NONE)
wenzelm@43667
   267
  | apply_tr _ Exit (State (SOME (state as Theory (Context.Theory _, _)), _)) =
wenzelm@43667
   268
      exit_transaction state
wenzelm@32792
   269
  | apply_tr int (Keep f) state =
wenzelm@56303
   270
      Runtime.controlled_execution (try generic_theory_of state) (fn x => tap (f int) x) state
wenzelm@32792
   271
  | apply_tr int (Transaction (f, g)) (State (SOME state, _)) =
wenzelm@32792
   272
      apply_transaction (fn x => f int x) g state
wenzelm@32792
   273
  | apply_tr _ _ _ = raise UNDEF;
wenzelm@5828
   274
wenzelm@32792
   275
fun apply_union _ [] state = raise FAILURE (state, UNDEF)
wenzelm@32792
   276
  | apply_union int (tr :: trs) state =
wenzelm@32792
   277
      apply_union int trs state
wenzelm@32792
   278
        handle Runtime.UNDEF => apply_tr int tr state
wenzelm@32792
   279
          | FAILURE (alt_state, UNDEF) => apply_tr int tr alt_state
wenzelm@6689
   280
          | exn as FAILURE _ => raise exn
wenzelm@6689
   281
          | exn => raise FAILURE (state, exn);
wenzelm@6689
   282
wenzelm@6689
   283
in
wenzelm@6689
   284
wenzelm@32792
   285
fun apply_trans int trs state = (apply_union int trs state, NONE)
skalberg@15531
   286
  handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn);
wenzelm@6689
   287
wenzelm@6689
   288
end;
wenzelm@5828
   289
wenzelm@5828
   290
wenzelm@5828
   291
(* datatype transition *)
wenzelm@5828
   292
wenzelm@5828
   293
datatype transition = Transition of
wenzelm@26621
   294
 {name: string,              (*command name*)
wenzelm@26621
   295
  pos: Position.T,           (*source position*)
wenzelm@51423
   296
  timing: Time.time option,  (*prescient timing information*)
wenzelm@26621
   297
  trans: trans list};        (*primitive transitions (union)*)
wenzelm@5828
   298
wenzelm@58893
   299
fun make_transition (name, pos, timing, trans) =
wenzelm@58893
   300
  Transition {name = name, pos = pos, timing = timing, trans = trans};
wenzelm@5828
   301
wenzelm@58893
   302
fun map_transition f (Transition {name, pos, timing, trans}) =
wenzelm@58893
   303
  make_transition (f (name, pos, timing, trans));
wenzelm@5828
   304
wenzelm@58893
   305
val empty = make_transition ("", Position.none, NONE, []);
wenzelm@5828
   306
wenzelm@5828
   307
wenzelm@5828
   308
(* diagnostics *)
wenzelm@5828
   309
wenzelm@27427
   310
fun name_of (Transition {name, ...}) = name;
wenzelm@28105
   311
fun pos_of (Transition {pos, ...}) = pos;
wenzelm@5828
   312
wenzelm@48993
   313
fun command_msg msg tr = msg ^ "command " ^ quote (name_of tr) ^ Position.here (pos_of tr);
wenzelm@38875
   314
fun at_command tr = command_msg "At " tr;
wenzelm@5828
   315
wenzelm@5828
   316
fun type_error tr state =
wenzelm@56937
   317
  command_msg "Illegal application of " tr ^ " " ^ str_of_state state;
wenzelm@5828
   318
wenzelm@5828
   319
wenzelm@5828
   320
(* modify transitions *)
wenzelm@5828
   321
wenzelm@58893
   322
fun name name = map_transition (fn (_, pos, timing, trans) =>
wenzelm@58893
   323
  (name, pos, timing, trans));
wenzelm@9010
   324
wenzelm@58893
   325
fun position pos = map_transition (fn (name, _, timing, trans) =>
wenzelm@58893
   326
  (name, pos, timing, trans));
wenzelm@14923
   327
wenzelm@58893
   328
fun add_trans tr = map_transition (fn (name, pos, timing, trans) =>
wenzelm@58893
   329
  (name, pos, timing, tr :: trans));
wenzelm@16607
   330
wenzelm@58893
   331
val reset_trans = map_transition (fn (name, pos, timing, _) =>
wenzelm@58893
   332
  (name, pos, timing, []));
wenzelm@5828
   333
wenzelm@5828
   334
wenzelm@21007
   335
(* basic transitions *)
wenzelm@5828
   336
wenzelm@44187
   337
fun init_theory f = add_trans (Init f);
wenzelm@37977
   338
wenzelm@44187
   339
fun is_init (Transition {trans = [Init _], ...}) = true
wenzelm@44187
   340
  | is_init _ = false;
wenzelm@44187
   341
wenzelm@44187
   342
fun modify_init f tr = if is_init tr then init_theory f (reset_trans tr) else tr;
wenzelm@37977
   343
wenzelm@6689
   344
val exit = add_trans Exit;
wenzelm@7612
   345
val keep' = add_trans o Keep;
wenzelm@30366
   346
wenzelm@30366
   347
fun present_transaction f g = add_trans (Transaction (f, g));
wenzelm@30366
   348
fun transaction f = present_transaction f (K ());
wenzelm@5828
   349
wenzelm@7612
   350
fun keep f = add_trans (Keep (fn _ => f));
wenzelm@5828
   351
fun imperative f = keep (fn _ => f ());
wenzelm@5828
   352
wenzelm@27840
   353
fun ignored pos = empty |> name "<ignored>" |> position pos |> imperative I;
wenzelm@51268
   354
fun is_ignored tr = name_of tr = "<ignored>";
wenzelm@48772
   355
wenzelm@48772
   356
val malformed_name = "<malformed>";
wenzelm@27840
   357
fun malformed pos msg =
wenzelm@48772
   358
  empty |> name malformed_name |> position pos |> imperative (fn () => error msg);
wenzelm@48772
   359
fun is_malformed tr = name_of tr = malformed_name;
wenzelm@27840
   360
wenzelm@21007
   361
val unknown_theory = imperative (fn () => warning "Unknown theory context");
wenzelm@21007
   362
val unknown_proof = imperative (fn () => warning "Unknown proof context");
wenzelm@21007
   363
val unknown_context = imperative (fn () => warning "Unknown context");
wenzelm@15668
   364
wenzelm@21007
   365
wenzelm@49012
   366
(* theory transitions *)
wenzelm@44304
   367
wenzelm@27601
   368
fun generic_theory f = transaction (fn _ =>
wenzelm@26491
   369
  (fn Theory (gthy, _) => Theory (f gthy, NONE)
wenzelm@26491
   370
    | _ => raise UNDEF));
wenzelm@26491
   371
wenzelm@27601
   372
fun theory' f = transaction (fn int =>
wenzelm@33725
   373
  (fn Theory (Context.Theory thy, _) =>
wenzelm@33725
   374
      let val thy' = thy
wenzelm@49012
   375
        |> Sign.new_group
wenzelm@33725
   376
        |> f int
wenzelm@33725
   377
        |> Sign.reset_group;
wenzelm@33725
   378
      in Theory (Context.Theory thy', NONE) end
wenzelm@20963
   379
    | _ => raise UNDEF));
wenzelm@20963
   380
wenzelm@20963
   381
fun theory f = theory' (K f);
wenzelm@20963
   382
wenzelm@27601
   383
fun begin_local_theory begin f = transaction (fn _ =>
wenzelm@20963
   384
  (fn Theory (Context.Theory thy, _) =>
wenzelm@20963
   385
        let
wenzelm@20985
   386
          val lthy = f thy;
haftmann@57483
   387
          val gthy = if begin then Context.Proof lthy else Context.Theory (Named_Target.exit lthy);
wenzelm@56897
   388
          val _ =
wenzelm@56897
   389
            if begin then
wenzelm@56897
   390
              Pretty.writeln (Pretty.mark Markup.state (Pretty.chunks (Local_Theory.pretty lthy)))
wenzelm@56897
   391
            else ();
wenzelm@21294
   392
        in Theory (gthy, SOME lthy) end
wenzelm@20963
   393
    | _ => raise UNDEF));
wenzelm@17076
   394
wenzelm@27601
   395
val end_local_theory = transaction (fn _ =>
haftmann@57483
   396
  (fn Theory (Context.Proof lthy, _) => Theory (Context.Theory (Named_Target.exit lthy), SOME lthy)
wenzelm@21007
   397
    | _ => raise UNDEF));
wenzelm@21007
   398
wenzelm@47069
   399
fun open_target f = transaction (fn _ =>
wenzelm@47069
   400
  (fn Theory (gthy, _) =>
wenzelm@47069
   401
        let val lthy = f gthy
wenzelm@47069
   402
        in Theory (Context.Proof lthy, SOME lthy) end
wenzelm@47069
   403
    | _ => raise UNDEF));
wenzelm@47069
   404
wenzelm@47069
   405
val close_target = transaction (fn _ =>
wenzelm@47069
   406
  (fn Theory (Context.Proof lthy, _) =>
wenzelm@47069
   407
        (case try Local_Theory.close_target lthy of
wenzelm@50739
   408
          SOME ctxt' =>
wenzelm@50739
   409
            let
wenzelm@50739
   410
              val gthy' =
wenzelm@50739
   411
                if can Local_Theory.assert ctxt'
wenzelm@50739
   412
                then Context.Proof ctxt'
wenzelm@50739
   413
                else Context.Theory (Proof_Context.theory_of ctxt');
wenzelm@50739
   414
            in Theory (gthy', SOME lthy) end
wenzelm@47069
   415
        | NONE => raise UNDEF)
wenzelm@47069
   416
    | _ => raise UNDEF));
wenzelm@47069
   417
wenzelm@59032
   418
fun local_theory' loc f = present_transaction (fn int =>
wenzelm@21294
   419
  (fn Theory (gthy, _) =>
wenzelm@21294
   420
        let
haftmann@57483
   421
          val (finish, lthy) = Named_Target.switch loc gthy;
wenzelm@47274
   422
          val lthy' = lthy
wenzelm@49012
   423
            |> Local_Theory.new_group
wenzelm@33725
   424
            |> f int
wenzelm@33725
   425
            |> Local_Theory.reset_group;
wenzelm@21294
   426
        in Theory (finish lthy', SOME lthy') end
wenzelm@59032
   427
    | _ => raise UNDEF))
wenzelm@59032
   428
  (K ());
wenzelm@15668
   429
wenzelm@59032
   430
fun local_theory loc f = local_theory' loc (K f);
wenzelm@21007
   431
wenzelm@59032
   432
fun present_local_theory loc = present_transaction (fn int =>
wenzelm@59032
   433
  (fn Theory (gthy, _) =>
wenzelm@59032
   434
        let val (finish, lthy) = Named_Target.switch loc gthy;
wenzelm@59032
   435
        in Theory (finish lthy, SOME lthy) end
wenzelm@59032
   436
    | _ => raise UNDEF));
wenzelm@21007
   437
wenzelm@21007
   438
wenzelm@21007
   439
(* proof transitions *)
wenzelm@21007
   440
wenzelm@27601
   441
fun end_proof f = transaction (fn int =>
wenzelm@24795
   442
  (fn Proof (prf, (finish, _)) =>
wenzelm@33390
   443
        let val state = Proof_Node.current prf in
wenzelm@21007
   444
          if can (Proof.assert_bottom true) state then
wenzelm@21007
   445
            let
wenzelm@21007
   446
              val ctxt' = f int state;
wenzelm@21007
   447
              val gthy' = finish ctxt';
wenzelm@21007
   448
            in Theory (gthy', SOME ctxt') end
wenzelm@21007
   449
          else raise UNDEF
wenzelm@21007
   450
        end
wenzelm@51555
   451
    | Skipped_Proof (0, (gthy, _)) => Theory (gthy, NONE)
wenzelm@21007
   452
    | _ => raise UNDEF));
wenzelm@21007
   453
wenzelm@21294
   454
local
wenzelm@21294
   455
wenzelm@47274
   456
fun begin_proof init = transaction (fn int =>
wenzelm@21294
   457
  (fn Theory (gthy, _) =>
wenzelm@21294
   458
    let
wenzelm@47274
   459
      val (finish, prf) = init int gthy;
wenzelm@52499
   460
      val skip = Goal.skip_proofs_enabled ();
wenzelm@58795
   461
      val schematic_goal = try Proof.schematic_goal prf;
wenzelm@47274
   462
      val _ =
wenzelm@58795
   463
        if skip andalso schematic_goal = SOME true then
wenzelm@47274
   464
          warning "Cannot skip proof of schematic goal statement"
wenzelm@47274
   465
        else ();
wenzelm@21294
   466
    in
wenzelm@58795
   467
      if skip andalso schematic_goal = SOME false then
wenzelm@51555
   468
        Skipped_Proof (0, (finish (Proof.global_skip_proof true prf), gthy))
wenzelm@47274
   469
      else Proof (Proof_Node.init prf, (finish, gthy))
wenzelm@21294
   470
    end
wenzelm@21294
   471
  | _ => raise UNDEF));
wenzelm@21294
   472
wenzelm@21294
   473
in
wenzelm@21294
   474
wenzelm@24780
   475
fun local_theory_to_proof' loc f = begin_proof
wenzelm@47274
   476
  (fn int => fn gthy =>
haftmann@57483
   477
    let val (finish, lthy) = Named_Target.switch loc gthy
wenzelm@49012
   478
    in (finish o Local_Theory.reset_group, f int (Local_Theory.new_group lthy)) end);
wenzelm@24780
   479
berghofe@24453
   480
fun local_theory_to_proof loc f = local_theory_to_proof' loc (K f);
wenzelm@21294
   481
wenzelm@21294
   482
fun theory_to_proof f = begin_proof
wenzelm@47274
   483
  (fn _ => fn gthy =>
wenzelm@56057
   484
    (Context.Theory o Sign.reset_group o Sign.change_check o Proof_Context.theory_of,
wenzelm@49062
   485
      (case gthy of
wenzelm@52788
   486
        Context.Theory thy => f (Sign.new_group thy)
wenzelm@49012
   487
      | _ => raise UNDEF)));
wenzelm@21294
   488
wenzelm@21294
   489
end;
wenzelm@21294
   490
wenzelm@58798
   491
fun forget_proof strict = transaction (fn _ =>
wenzelm@58798
   492
  (fn Proof (prf, (_, orig_gthy)) =>
wenzelm@58798
   493
        if strict andalso not (Proof.has_bottom_goal (Proof_Node.current prf))
wenzelm@58798
   494
        then raise UNDEF else Theory (orig_gthy, NONE)
wenzelm@51555
   495
    | Skipped_Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
wenzelm@21007
   496
    | _ => raise UNDEF));
wenzelm@21007
   497
wenzelm@27601
   498
fun proofs' f = transaction (fn int =>
wenzelm@49062
   499
  (fn Proof (prf, x) => Proof (Proof_Node.applys (f int) prf, x)
wenzelm@51555
   500
    | skip as Skipped_Proof _ => skip
wenzelm@16815
   501
    | _ => raise UNDEF));
wenzelm@15668
   502
wenzelm@49863
   503
fun proof' f = proofs' ((Seq.single o Seq.Result) oo f);
wenzelm@17904
   504
val proofs = proofs' o K;
wenzelm@6689
   505
val proof = proof' o K;
wenzelm@16815
   506
wenzelm@27601
   507
fun actual_proof f = transaction (fn _ =>
wenzelm@21007
   508
  (fn Proof (prf, x) => Proof (f prf, x)
wenzelm@20963
   509
    | _ => raise UNDEF));
wenzelm@16815
   510
wenzelm@27601
   511
fun skip_proof f = transaction (fn _ =>
wenzelm@51555
   512
  (fn Skipped_Proof (h, x) => Skipped_Proof (f h, x)
wenzelm@18563
   513
    | _ => raise UNDEF));
wenzelm@18563
   514
wenzelm@27601
   515
fun skip_proof_to_theory pred = transaction (fn _ =>
wenzelm@51555
   516
  (fn Skipped_Proof (d, (gthy, _)) => if pred d then Theory (gthy, NONE) else raise UNDEF
wenzelm@33725
   517
    | _ => raise UNDEF));
wenzelm@5828
   518
wenzelm@5828
   519
wenzelm@5828
   520
wenzelm@5828
   521
(** toplevel transactions **)
wenzelm@5828
   522
wenzelm@52527
   523
(* runtime position *)
wenzelm@27427
   524
wenzelm@52536
   525
fun exec_id id (tr as Transition {pos, ...}) =
wenzelm@52536
   526
  position (Position.put_id (Document_ID.print id) pos) tr;
wenzelm@25799
   527
wenzelm@25960
   528
fun setmp_thread_position (Transition {pos, ...}) f x =
wenzelm@25819
   529
  Position.setmp_thread_data pos f x;
wenzelm@25799
   530
wenzelm@25799
   531
wenzelm@28095
   532
(* post-transition hooks *)
wenzelm@28095
   533
wenzelm@37905
   534
local
wenzelm@56147
   535
  val hooks =
wenzelm@56147
   536
    Synchronized.var "Toplevel.hooks" ([]: (transition -> state -> state -> unit) list);
wenzelm@37905
   537
in
wenzelm@28095
   538
wenzelm@56147
   539
fun add_hook hook = Synchronized.change hooks (cons hook);
wenzelm@56147
   540
fun get_hooks () = Synchronized.value hooks;
wenzelm@28095
   541
wenzelm@28095
   542
end;
wenzelm@28095
   543
wenzelm@28095
   544
wenzelm@5828
   545
(* apply transitions *)
wenzelm@5828
   546
wenzelm@51217
   547
fun get_timing (Transition {timing, ...}) = timing;
wenzelm@58893
   548
fun put_timing timing = map_transition (fn (name, pos, _, trans) => (name, pos, timing, trans));
wenzelm@51217
   549
wenzelm@6664
   550
local
wenzelm@6664
   551
wenzelm@56895
   552
fun app int (tr as Transition {name, trans, ...}) =
wenzelm@25819
   553
  setmp_thread_position tr (fn state =>
wenzelm@25799
   554
    let
wenzelm@51595
   555
      val timing_start = Timing.start ();
wenzelm@25799
   556
wenzelm@51595
   557
      val (result, opt_err) =
wenzelm@51658
   558
         state |> (apply_trans int trans |> ! profiling > 0 ? profile (! profiling));
wenzelm@56895
   559
wenzelm@51595
   560
      val timing_result = Timing.result timing_start;
wenzelm@51662
   561
      val timing_props =
wenzelm@51662
   562
        Markup.command_timing :: (Markup.nameN, name_of tr) :: Position.properties_of (pos_of tr);
wenzelm@51662
   563
      val _ = Timing.protocol_message timing_props timing_result;
wenzelm@51595
   564
    in
wenzelm@56937
   565
      (result, Option.map (fn UNDEF => ERROR (type_error tr state) | exn => exn) opt_err)
wenzelm@51595
   566
    end);
wenzelm@6664
   567
wenzelm@6664
   568
in
wenzelm@5828
   569
wenzelm@26602
   570
fun transition int tr st =
wenzelm@28095
   571
  let
wenzelm@28095
   572
    val hooks = get_hooks ();
wenzelm@28103
   573
    fun apply_hooks st' = hooks |> List.app (fn f => (try (fn () => f tr st st') (); ()));
wenzelm@28095
   574
wenzelm@28095
   575
    val ctxt = try context_of st;
wenzelm@28095
   576
    val res =
wenzelm@28095
   577
      (case app int tr st of
wenzelm@38888
   578
        (_, SOME Runtime.TERMINATE) => NONE
wenzelm@38888
   579
      | (st', SOME (Runtime.EXCURSION_FAIL exn_info)) => SOME (st', SOME exn_info)
wenzelm@31476
   580
      | (st', SOME exn) => SOME (st', SOME (Runtime.exn_context ctxt exn, at_command tr))
wenzelm@28103
   581
      | (st', NONE) => SOME (st', NONE));
wenzelm@28103
   582
    val _ = (case res of SOME (st', NONE) => apply_hooks st' | _ => ());
wenzelm@28095
   583
  in res end;
wenzelm@6664
   584
wenzelm@6664
   585
end;
wenzelm@5828
   586
wenzelm@5828
   587
wenzelm@51284
   588
(* managed commands *)
wenzelm@5828
   589
wenzelm@51323
   590
fun command_errors int tr st =
wenzelm@51323
   591
  (case transition int tr st of
wenzelm@51323
   592
    SOME (st', NONE) => ([], SOME st')
wenzelm@56303
   593
  | SOME (_, SOME (exn, _)) => (Runtime.exn_messages_ids exn, NONE)
wenzelm@56303
   594
  | NONE => (Runtime.exn_messages_ids Runtime.TERMINATE, NONE));
wenzelm@51323
   595
wenzelm@51284
   596
fun command_exception int tr st =
wenzelm@51284
   597
  (case transition int tr st of
wenzelm@28425
   598
    SOME (st', NONE) => st'
wenzelm@39285
   599
  | SOME (_, SOME (exn, info)) =>
wenzelm@39285
   600
      if Exn.is_interrupt exn then reraise exn else raise Runtime.EXCURSION_FAIL (exn, info)
wenzelm@38888
   601
  | NONE => raise Runtime.EXCURSION_FAIL (Runtime.TERMINATE, at_command tr));
wenzelm@27576
   602
wenzelm@58848
   603
val command = command_exception false;
wenzelm@51284
   604
wenzelm@28433
   605
wenzelm@56937
   606
(* reset state *)
wenzelm@56937
   607
wenzelm@56937
   608
local
wenzelm@56937
   609
wenzelm@56937
   610
fun reset_state check trans st =
wenzelm@56937
   611
  if check st then NONE
wenzelm@56937
   612
  else #2 (command_errors false (trans empty) st);
wenzelm@56937
   613
wenzelm@56937
   614
in
wenzelm@56937
   615
wenzelm@58798
   616
val reset_theory = reset_state is_theory (forget_proof false);
wenzelm@56937
   617
wenzelm@56937
   618
val reset_proof =
wenzelm@56937
   619
  reset_state is_proof
wenzelm@56937
   620
    (transaction (fn _ =>
wenzelm@56937
   621
      (fn Theory (gthy, _) => Skipped_Proof (0, (gthy, gthy))
wenzelm@56937
   622
        | _ => raise UNDEF)));
wenzelm@56937
   623
wenzelm@56937
   624
end;
wenzelm@56937
   625
wenzelm@56937
   626
wenzelm@46959
   627
(* scheduled proof result *)
wenzelm@28433
   628
wenzelm@51332
   629
datatype result =
wenzelm@51332
   630
  Result of transition * state |
wenzelm@51332
   631
  Result_List of result list |
wenzelm@51332
   632
  Result_Future of result future;
wenzelm@51332
   633
wenzelm@51332
   634
fun join_results (Result x) = [x]
wenzelm@51332
   635
  | join_results (Result_List xs) = maps join_results xs
wenzelm@51332
   636
  | join_results (Result_Future x) = join_results (Future.join x);
wenzelm@51332
   637
wenzelm@51323
   638
local
wenzelm@51323
   639
wenzelm@47417
   640
structure Result = Proof_Data
wenzelm@28974
   641
(
wenzelm@51332
   642
  type T = result;
wenzelm@51332
   643
  val empty: T = Result_List [];
wenzelm@47417
   644
  fun init _ = empty;
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;