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