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