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