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