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