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