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