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