src/Pure/Isar/toplevel.ML
author wenzelm
Thu Nov 13 23:45:15 2014 +0100 (2014-11-13 ago)
changeset 59000 ed09ae4ea2d8
parent 58923 cb9b69cca999
child 59032 f36496364ce1
permissions -rw-r--r--
uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
discontinued obsolete 'txt_raw' (superseded by 'text_raw');
eliminated obsolete Outer_Syntax.markup (superseded by keyword kinds);
'text' and 'txt' no longer appear in Sidekick tree due to change of keyword kind;
changed tagging of diagnostic commands within proof;
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@47069
   418
wenzelm@21007
   419
local
wenzelm@21007
   420
wenzelm@30366
   421
fun local_theory_presentation loc f = present_transaction (fn int =>
wenzelm@21294
   422
  (fn Theory (gthy, _) =>
wenzelm@21294
   423
        let
haftmann@57483
   424
          val (finish, lthy) = Named_Target.switch loc gthy;
wenzelm@47274
   425
          val lthy' = lthy
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@30366
   430
    | _ => raise UNDEF));
wenzelm@15668
   431
wenzelm@21007
   432
in
wenzelm@21007
   433
wenzelm@30366
   434
fun local_theory' loc f = local_theory_presentation loc f (K ());
wenzelm@29380
   435
fun local_theory loc f = local_theory' loc (K f);
wenzelm@30366
   436
fun present_local_theory loc = local_theory_presentation loc (K I);
wenzelm@18955
   437
wenzelm@21007
   438
end;
wenzelm@21007
   439
wenzelm@21007
   440
wenzelm@21007
   441
(* proof transitions *)
wenzelm@21007
   442
wenzelm@27601
   443
fun end_proof f = transaction (fn int =>
wenzelm@24795
   444
  (fn Proof (prf, (finish, _)) =>
wenzelm@33390
   445
        let val state = Proof_Node.current prf in
wenzelm@21007
   446
          if can (Proof.assert_bottom true) state then
wenzelm@21007
   447
            let
wenzelm@21007
   448
              val ctxt' = f int state;
wenzelm@21007
   449
              val gthy' = finish ctxt';
wenzelm@21007
   450
            in Theory (gthy', SOME ctxt') end
wenzelm@21007
   451
          else raise UNDEF
wenzelm@21007
   452
        end
wenzelm@51555
   453
    | Skipped_Proof (0, (gthy, _)) => Theory (gthy, NONE)
wenzelm@21007
   454
    | _ => raise UNDEF));
wenzelm@21007
   455
wenzelm@21294
   456
local
wenzelm@21294
   457
wenzelm@47274
   458
fun begin_proof init = transaction (fn int =>
wenzelm@21294
   459
  (fn Theory (gthy, _) =>
wenzelm@21294
   460
    let
wenzelm@47274
   461
      val (finish, prf) = init int gthy;
wenzelm@52499
   462
      val skip = Goal.skip_proofs_enabled ();
wenzelm@58795
   463
      val schematic_goal = try Proof.schematic_goal prf;
wenzelm@47274
   464
      val _ =
wenzelm@58795
   465
        if skip andalso schematic_goal = SOME true then
wenzelm@47274
   466
          warning "Cannot skip proof of schematic goal statement"
wenzelm@47274
   467
        else ();
wenzelm@21294
   468
    in
wenzelm@58795
   469
      if skip andalso schematic_goal = SOME false then
wenzelm@51555
   470
        Skipped_Proof (0, (finish (Proof.global_skip_proof true prf), gthy))
wenzelm@47274
   471
      else Proof (Proof_Node.init prf, (finish, gthy))
wenzelm@21294
   472
    end
wenzelm@21294
   473
  | _ => raise UNDEF));
wenzelm@21294
   474
wenzelm@21294
   475
in
wenzelm@21294
   476
wenzelm@24780
   477
fun local_theory_to_proof' loc f = begin_proof
wenzelm@47274
   478
  (fn int => fn gthy =>
haftmann@57483
   479
    let val (finish, lthy) = Named_Target.switch loc gthy
wenzelm@49012
   480
    in (finish o Local_Theory.reset_group, f int (Local_Theory.new_group lthy)) end);
wenzelm@24780
   481
berghofe@24453
   482
fun local_theory_to_proof loc f = local_theory_to_proof' loc (K f);
wenzelm@21294
   483
wenzelm@21294
   484
fun theory_to_proof f = begin_proof
wenzelm@47274
   485
  (fn _ => fn gthy =>
wenzelm@56057
   486
    (Context.Theory o Sign.reset_group o Sign.change_check o Proof_Context.theory_of,
wenzelm@49062
   487
      (case gthy of
wenzelm@52788
   488
        Context.Theory thy => f (Sign.new_group thy)
wenzelm@49012
   489
      | _ => raise UNDEF)));
wenzelm@21294
   490
wenzelm@21294
   491
end;
wenzelm@21294
   492
wenzelm@58798
   493
fun forget_proof strict = transaction (fn _ =>
wenzelm@58798
   494
  (fn Proof (prf, (_, orig_gthy)) =>
wenzelm@58798
   495
        if strict andalso not (Proof.has_bottom_goal (Proof_Node.current prf))
wenzelm@58798
   496
        then raise UNDEF else Theory (orig_gthy, NONE)
wenzelm@51555
   497
    | Skipped_Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
wenzelm@21007
   498
    | _ => raise UNDEF));
wenzelm@21007
   499
wenzelm@27601
   500
fun proofs' f = transaction (fn int =>
wenzelm@49062
   501
  (fn Proof (prf, x) => Proof (Proof_Node.applys (f int) prf, x)
wenzelm@51555
   502
    | skip as Skipped_Proof _ => skip
wenzelm@16815
   503
    | _ => raise UNDEF));
wenzelm@15668
   504
wenzelm@49863
   505
fun proof' f = proofs' ((Seq.single o Seq.Result) oo f);
wenzelm@17904
   506
val proofs = proofs' o K;
wenzelm@6689
   507
val proof = proof' o K;
wenzelm@16815
   508
wenzelm@27601
   509
fun actual_proof f = transaction (fn _ =>
wenzelm@21007
   510
  (fn Proof (prf, x) => Proof (f prf, x)
wenzelm@20963
   511
    | _ => raise UNDEF));
wenzelm@16815
   512
wenzelm@27601
   513
fun skip_proof f = transaction (fn _ =>
wenzelm@51555
   514
  (fn Skipped_Proof (h, x) => Skipped_Proof (f h, x)
wenzelm@18563
   515
    | _ => raise UNDEF));
wenzelm@18563
   516
wenzelm@27601
   517
fun skip_proof_to_theory pred = transaction (fn _ =>
wenzelm@51555
   518
  (fn Skipped_Proof (d, (gthy, _)) => if pred d then Theory (gthy, NONE) else raise UNDEF
wenzelm@33725
   519
    | _ => raise UNDEF));
wenzelm@5828
   520
wenzelm@5828
   521
wenzelm@5828
   522
wenzelm@5828
   523
(** toplevel transactions **)
wenzelm@5828
   524
wenzelm@52527
   525
(* runtime position *)
wenzelm@27427
   526
wenzelm@52536
   527
fun exec_id id (tr as Transition {pos, ...}) =
wenzelm@52536
   528
  position (Position.put_id (Document_ID.print id) pos) tr;
wenzelm@25799
   529
wenzelm@25960
   530
fun setmp_thread_position (Transition {pos, ...}) f x =
wenzelm@25819
   531
  Position.setmp_thread_data pos f x;
wenzelm@25799
   532
wenzelm@25799
   533
wenzelm@28095
   534
(* post-transition hooks *)
wenzelm@28095
   535
wenzelm@37905
   536
local
wenzelm@56147
   537
  val hooks =
wenzelm@56147
   538
    Synchronized.var "Toplevel.hooks" ([]: (transition -> state -> state -> unit) list);
wenzelm@37905
   539
in
wenzelm@28095
   540
wenzelm@56147
   541
fun add_hook hook = Synchronized.change hooks (cons hook);
wenzelm@56147
   542
fun get_hooks () = Synchronized.value hooks;
wenzelm@28095
   543
wenzelm@28095
   544
end;
wenzelm@28095
   545
wenzelm@28095
   546
wenzelm@5828
   547
(* apply transitions *)
wenzelm@5828
   548
wenzelm@51217
   549
fun get_timing (Transition {timing, ...}) = timing;
wenzelm@58893
   550
fun put_timing timing = map_transition (fn (name, pos, _, trans) => (name, pos, timing, trans));
wenzelm@51217
   551
wenzelm@6664
   552
local
wenzelm@6664
   553
wenzelm@56895
   554
fun app int (tr as Transition {name, trans, ...}) =
wenzelm@25819
   555
  setmp_thread_position tr (fn state =>
wenzelm@25799
   556
    let
wenzelm@51595
   557
      val timing_start = Timing.start ();
wenzelm@25799
   558
wenzelm@51595
   559
      val (result, opt_err) =
wenzelm@51658
   560
         state |> (apply_trans int trans |> ! profiling > 0 ? profile (! profiling));
wenzelm@56895
   561
wenzelm@51595
   562
      val timing_result = Timing.result timing_start;
wenzelm@51662
   563
      val timing_props =
wenzelm@51662
   564
        Markup.command_timing :: (Markup.nameN, name_of tr) :: Position.properties_of (pos_of tr);
wenzelm@51662
   565
      val _ = Timing.protocol_message timing_props timing_result;
wenzelm@51595
   566
    in
wenzelm@56937
   567
      (result, Option.map (fn UNDEF => ERROR (type_error tr state) | exn => exn) opt_err)
wenzelm@51595
   568
    end);
wenzelm@6664
   569
wenzelm@6664
   570
in
wenzelm@5828
   571
wenzelm@26602
   572
fun transition int tr st =
wenzelm@28095
   573
  let
wenzelm@28095
   574
    val hooks = get_hooks ();
wenzelm@28103
   575
    fun apply_hooks st' = hooks |> List.app (fn f => (try (fn () => f tr st st') (); ()));
wenzelm@28095
   576
wenzelm@28095
   577
    val ctxt = try context_of st;
wenzelm@28095
   578
    val res =
wenzelm@28095
   579
      (case app int tr st of
wenzelm@38888
   580
        (_, SOME Runtime.TERMINATE) => NONE
wenzelm@38888
   581
      | (st', SOME (Runtime.EXCURSION_FAIL exn_info)) => SOME (st', SOME exn_info)
wenzelm@31476
   582
      | (st', SOME exn) => SOME (st', SOME (Runtime.exn_context ctxt exn, at_command tr))
wenzelm@28103
   583
      | (st', NONE) => SOME (st', NONE));
wenzelm@28103
   584
    val _ = (case res of SOME (st', NONE) => apply_hooks st' | _ => ());
wenzelm@28095
   585
  in res end;
wenzelm@6664
   586
wenzelm@6664
   587
end;
wenzelm@5828
   588
wenzelm@5828
   589
wenzelm@51284
   590
(* managed commands *)
wenzelm@5828
   591
wenzelm@51323
   592
fun command_errors int tr st =
wenzelm@51323
   593
  (case transition int tr st of
wenzelm@51323
   594
    SOME (st', NONE) => ([], SOME st')
wenzelm@56303
   595
  | SOME (_, SOME (exn, _)) => (Runtime.exn_messages_ids exn, NONE)
wenzelm@56303
   596
  | NONE => (Runtime.exn_messages_ids Runtime.TERMINATE, NONE));
wenzelm@51323
   597
wenzelm@51284
   598
fun command_exception int tr st =
wenzelm@51284
   599
  (case transition int tr st of
wenzelm@28425
   600
    SOME (st', NONE) => st'
wenzelm@39285
   601
  | SOME (_, SOME (exn, info)) =>
wenzelm@39285
   602
      if Exn.is_interrupt exn then reraise exn else raise Runtime.EXCURSION_FAIL (exn, info)
wenzelm@38888
   603
  | NONE => raise Runtime.EXCURSION_FAIL (Runtime.TERMINATE, at_command tr));
wenzelm@27576
   604
wenzelm@58848
   605
val command = command_exception false;
wenzelm@51284
   606
wenzelm@28433
   607
wenzelm@56937
   608
(* reset state *)
wenzelm@56937
   609
wenzelm@56937
   610
local
wenzelm@56937
   611
wenzelm@56937
   612
fun reset_state check trans st =
wenzelm@56937
   613
  if check st then NONE
wenzelm@56937
   614
  else #2 (command_errors false (trans empty) st);
wenzelm@56937
   615
wenzelm@56937
   616
in
wenzelm@56937
   617
wenzelm@58798
   618
val reset_theory = reset_state is_theory (forget_proof false);
wenzelm@56937
   619
wenzelm@56937
   620
val reset_proof =
wenzelm@56937
   621
  reset_state is_proof
wenzelm@56937
   622
    (transaction (fn _ =>
wenzelm@56937
   623
      (fn Theory (gthy, _) => Skipped_Proof (0, (gthy, gthy))
wenzelm@56937
   624
        | _ => raise UNDEF)));
wenzelm@56937
   625
wenzelm@56937
   626
end;
wenzelm@56937
   627
wenzelm@56937
   628
wenzelm@46959
   629
(* scheduled proof result *)
wenzelm@28433
   630
wenzelm@51332
   631
datatype result =
wenzelm@51332
   632
  Result of transition * state |
wenzelm@51332
   633
  Result_List of result list |
wenzelm@51332
   634
  Result_Future of result future;
wenzelm@51332
   635
wenzelm@51332
   636
fun join_results (Result x) = [x]
wenzelm@51332
   637
  | join_results (Result_List xs) = maps join_results xs
wenzelm@51332
   638
  | join_results (Result_Future x) = join_results (Future.join x);
wenzelm@51332
   639
wenzelm@51323
   640
local
wenzelm@51323
   641
wenzelm@47417
   642
structure Result = Proof_Data
wenzelm@28974
   643
(
wenzelm@51332
   644
  type T = result;
wenzelm@51332
   645
  val empty: T = Result_List [];
wenzelm@47417
   646
  fun init _ = empty;
wenzelm@28974
   647
);
wenzelm@28974
   648
wenzelm@51332
   649
val get_result = Result.get o Proof.context_of;
wenzelm@51332
   650
val put_result = Proof.map_context o Result.put;
wenzelm@51324
   651
wenzelm@51423
   652
fun timing_estimate include_head elem =
wenzelm@51423
   653
  let
wenzelm@51423
   654
    val trs = Thy_Syntax.flat_element elem |> not include_head ? tl;
wenzelm@51423
   655
    val timings = map get_timing trs;
wenzelm@51423
   656
  in
wenzelm@51423
   657
    if forall is_some timings then
wenzelm@51423
   658
      SOME (fold (curry Time.+ o the) timings Time.zeroTime)
wenzelm@51423
   659
    else NONE
wenzelm@51423
   660
  end;
wenzelm@51423
   661
wenzelm@51423
   662
fun priority NONE = ~1
wenzelm@51423
   663
  | priority (SOME estimate) =
wenzelm@51423
   664
      Int.min (Real.floor (Real.max (Math.log10 (Time.toReal estimate), ~3.0)) - 3, ~1);
wenzelm@51423
   665
wenzelm@51423
   666
fun proof_future_enabled estimate st =
wenzelm@51324
   667
  (case try proof_of st of
wenzelm@51324
   668
    NONE => false
wenzelm@51324
   669
  | SOME state =>
wenzelm@51324
   670
      not (Proof.is_relevant state) andalso
wenzelm@51324
   671
       (if can (Proof.assert_bottom true) state
wenzelm@53189
   672
        then Goal.future_enabled 1
wenzelm@51423
   673
        else
wenzelm@51423
   674
          (case estimate of
wenzelm@53189
   675
            NONE => Goal.future_enabled 2
wenzelm@51423
   676
          | SOME t => Goal.future_enabled_timing t)));
wenzelm@51278
   677
wenzelm@58923
   678
fun atom_result keywords tr st =
wenzelm@51323
   679
  let
wenzelm@51323
   680
    val st' =
wenzelm@58923
   681
      if Goal.future_enabled 1 andalso Keyword.is_diag keywords (name_of tr) then
wenzelm@53192
   682
        (Execution.fork
wenzelm@51605
   683
          {name = "Toplevel.diag", pos = pos_of tr,
wenzelm@51605
   684
            pri = priority (timing_estimate true (Thy_Syntax.atom tr))}
wenzelm@51605
   685
          (fn () => command tr st); st)
wenzelm@51323
   686
      else command tr st;
wenzelm@51332
   687
  in (Result (tr, st'), st') end;
wenzelm@51323
   688
wenzelm@51323
   689
in
wenzelm@51323
   690
wenzelm@58923
   691
fun element_result keywords (Thy_Syntax.Element (tr, NONE)) st = atom_result keywords tr st
wenzelm@58923
   692
  | element_result keywords (elem as Thy_Syntax.Element (head_tr, SOME element_rest)) st =
wenzelm@48633
   693
      let
wenzelm@58923
   694
        val (head_result, st') = atom_result keywords head_tr st;
wenzelm@51332
   695
        val (body_elems, end_tr) = element_rest;
wenzelm@51423
   696
        val estimate = timing_estimate false elem;
wenzelm@51324
   697
      in
wenzelm@51423
   698
        if not (proof_future_enabled estimate st')
wenzelm@51324
   699
        then
wenzelm@51332
   700
          let
wenzelm@51332
   701
            val proof_trs = maps Thy_Syntax.flat_element body_elems @ [end_tr];
wenzelm@58923
   702
            val (proof_results, st'') = fold_map (atom_result keywords) proof_trs st';
wenzelm@51332
   703
          in (Result_List (head_result :: proof_results), st'') end
wenzelm@51324
   704
        else
wenzelm@51324
   705
          let
wenzelm@51324
   706
            val finish = Context.Theory o Proof_Context.theory_of;
wenzelm@28974
   707
wenzelm@51605
   708
            val future_proof =
wenzelm@51605
   709
              Proof.future_proof (fn state =>
wenzelm@53192
   710
                Execution.fork
wenzelm@51605
   711
                  {name = "Toplevel.future_proof", pos = pos_of head_tr, pri = priority estimate}
wenzelm@51605
   712
                  (fn () =>
wenzelm@51605
   713
                    let
wenzelm@51605
   714
                      val State (SOME (Proof (prf, (_, orig_gthy))), prev) = st';
wenzelm@51605
   715
                      val prf' = Proof_Node.apply (K state) prf;
wenzelm@51605
   716
                      val (result, result_state) =
wenzelm@51605
   717
                        State (SOME (Proof (prf', (finish, orig_gthy))), prev)
wenzelm@58923
   718
                        |> fold_map (element_result keywords) body_elems ||> command end_tr;
wenzelm@51605
   719
                    in (Result_List result, presentation_context_of result_state) end))
wenzelm@51332
   720
              #> (fn (res, state') => state' |> put_result (Result_Future res));
wenzelm@51332
   721
wenzelm@51332
   722
            val forked_proof =
wenzelm@51332
   723
              proof (future_proof #>
wenzelm@51332
   724
                (fn state => state |> Proof.local_done_proof |> put_result (get_result state))) o
wenzelm@51332
   725
              end_proof (fn _ => future_proof #>
wenzelm@51332
   726
                (fn state => state |> Proof.global_done_proof |> Result.put (get_result state)));
wenzelm@28974
   727
wenzelm@51324
   728
            val st'' = st'
wenzelm@56895
   729
              |> command (head_tr |> reset_trans |> forked_proof);
wenzelm@51332
   730
            val end_result = Result (end_tr, st'');
wenzelm@51324
   731
            val result =
wenzelm@51332
   732
              Result_List [head_result, Result.get (presentation_context_of st''), end_result];
wenzelm@51324
   733
          in (result, st'') end
wenzelm@51324
   734
      end;
wenzelm@28433
   735
wenzelm@6664
   736
end;
wenzelm@51323
   737
wenzelm@51323
   738
end;