src/Pure/Isar/toplevel.ML
author wenzelm
Fri Sep 10 23:11:58 2010 +0200 (2010-09-10)
changeset 39285 85728a4b5620
parent 39237 be1acdcd55dc
child 39513 fce2202892c4
permissions -rw-r--r--
avoid extra wrapping for interrupts;
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@17076
    15
  val level: state -> int
wenzelm@30398
    16
  val presentation_context_of: state -> Proof.context
wenzelm@30801
    17
  val previous_context_of: state -> Proof.context option
wenzelm@21506
    18
  val context_of: state -> Proof.context
wenzelm@22089
    19
  val generic_theory_of: state -> generic_theory
wenzelm@5828
    20
  val theory_of: state -> theory
wenzelm@5828
    21
  val proof_of: state -> Proof.state
wenzelm@18589
    22
  val proof_position_of: state -> int
wenzelm@37953
    23
  val end_theory: Position.T -> state -> theory
wenzelm@16815
    24
  val print_state_context: state -> unit
wenzelm@16815
    25
  val print_state: bool -> state -> unit
wenzelm@37858
    26
  val pretty_abstract: state -> Pretty.T
wenzelm@32738
    27
  val quiet: bool Unsynchronized.ref
wenzelm@32738
    28
  val debug: bool Unsynchronized.ref
wenzelm@32738
    29
  val interact: bool Unsynchronized.ref
wenzelm@32738
    30
  val timing: bool Unsynchronized.ref
wenzelm@32738
    31
  val profiling: int Unsynchronized.ref
wenzelm@32738
    32
  val skip_proofs: bool Unsynchronized.ref
wenzelm@20128
    33
  val program: (unit -> 'a) -> 'a
wenzelm@33604
    34
  val thread: bool -> (unit -> unit) -> Thread.thread
wenzelm@16682
    35
  type transition
wenzelm@5828
    36
  val empty: transition
wenzelm@27441
    37
  val init_of: transition -> string option
wenzelm@38888
    38
  val print_of: transition -> bool
wenzelm@27427
    39
  val name_of: transition -> string
wenzelm@28105
    40
  val pos_of: transition -> Position.T
wenzelm@27500
    41
  val str_of: transition -> string
wenzelm@5828
    42
  val name: string -> transition -> transition
wenzelm@5828
    43
  val position: Position.T -> transition -> transition
wenzelm@5828
    44
  val interactive: bool -> transition -> transition
wenzelm@38888
    45
  val set_print: bool -> transition -> transition
wenzelm@5828
    46
  val print: transition -> transition
wenzelm@9010
    47
  val no_timing: transition -> transition
wenzelm@37977
    48
  val init_theory: string -> (unit -> theory) -> transition -> transition
wenzelm@37977
    49
  val modify_init: (unit -> theory) -> transition -> transition
wenzelm@6689
    50
  val exit: transition -> transition
wenzelm@5828
    51
  val keep: (state -> unit) -> transition -> transition
wenzelm@7612
    52
  val keep': (bool -> state -> unit) -> transition -> transition
wenzelm@5828
    53
  val imperative: (unit -> unit) -> transition -> transition
wenzelm@27840
    54
  val ignored: Position.T -> transition
wenzelm@27840
    55
  val malformed: Position.T -> string -> transition
wenzelm@5828
    56
  val theory: (theory -> theory) -> transition -> transition
wenzelm@26491
    57
  val generic_theory: (generic_theory -> generic_theory) -> transition -> transition
wenzelm@7612
    58
  val theory': (bool -> theory -> theory) -> transition -> transition
wenzelm@20985
    59
  val begin_local_theory: bool -> (theory -> local_theory) -> transition -> transition
wenzelm@21007
    60
  val end_local_theory: transition -> transition
wenzelm@29380
    61
  val local_theory': xstring option -> (bool -> local_theory -> local_theory) ->
wenzelm@29380
    62
    transition -> transition
wenzelm@20963
    63
  val local_theory: xstring option -> (local_theory -> local_theory) -> transition -> transition
wenzelm@30366
    64
  val present_local_theory: xstring option -> (state -> unit) -> transition -> transition
berghofe@24453
    65
  val local_theory_to_proof': xstring option -> (bool -> local_theory -> Proof.state) ->
berghofe@24453
    66
    transition -> transition
wenzelm@21007
    67
  val local_theory_to_proof: xstring option -> (local_theory -> Proof.state) ->
wenzelm@21007
    68
    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@21007
    71
  val forget_proof: transition -> transition
wenzelm@30366
    72
  val present_proof: (state -> unit) -> transition -> transition
wenzelm@21177
    73
  val proofs': (bool -> Proof.state -> Proof.state Seq.seq) -> transition -> transition
wenzelm@17904
    74
  val proof': (bool -> Proof.state -> Proof.state) -> transition -> transition
wenzelm@21177
    75
  val proofs: (Proof.state -> Proof.state 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@27427
    80
  val get_id: transition -> string option
wenzelm@27427
    81
  val put_id: string -> transition -> transition
wenzelm@9512
    82
  val unknown_theory: transition -> transition
wenzelm@9512
    83
  val unknown_proof: transition -> transition
wenzelm@9512
    84
  val unknown_context: transition -> transition
wenzelm@28425
    85
  val setmp_thread_position: transition -> ('a -> 'b) -> 'a -> 'b
wenzelm@27606
    86
  val status: transition -> Markup.T -> unit
wenzelm@38876
    87
  val error_msg: transition -> string -> unit
wenzelm@28103
    88
  val add_hook: (transition -> state -> state -> unit) -> unit
wenzelm@26602
    89
  val transition: bool -> transition -> state -> (state * (exn * string) option) option
wenzelm@28425
    90
  val command: transition -> state -> state
wenzelm@37953
    91
  val excursion: (transition * transition list) list -> (transition * state) list lazy * theory
wenzelm@5828
    92
end;
wenzelm@5828
    93
wenzelm@6965
    94
structure Toplevel: TOPLEVEL =
wenzelm@5828
    95
struct
wenzelm@5828
    96
wenzelm@5828
    97
(** toplevel state **)
wenzelm@5828
    98
wenzelm@31476
    99
exception UNDEF = Runtime.UNDEF;
wenzelm@19063
   100
wenzelm@19063
   101
wenzelm@21294
   102
(* local theory wrappers *)
wenzelm@5828
   103
haftmann@38350
   104
val loc_init = Named_Target.context_cmd;
wenzelm@33671
   105
val loc_exit = Local_Theory.exit_global;
wenzelm@21294
   106
wenzelm@25292
   107
fun loc_begin loc (Context.Theory thy) = loc_init (the_default "-" loc) thy
wenzelm@21294
   108
  | loc_begin NONE (Context.Proof lthy) = lthy
haftmann@38391
   109
  | loc_begin (SOME loc) (Context.Proof lthy) = (loc_init loc o loc_exit) lthy;
wenzelm@21294
   110
wenzelm@21294
   111
fun loc_finish _ (Context.Theory _) = Context.Theory o loc_exit
wenzelm@33671
   112
  | loc_finish NONE (Context.Proof _) = Context.Proof o Local_Theory.restore
haftmann@38391
   113
  | loc_finish (SOME _) (Context.Proof lthy) = Context.Proof o Named_Target.reinit lthy;
wenzelm@21294
   114
wenzelm@21294
   115
wenzelm@21958
   116
(* datatype node *)
wenzelm@21294
   117
wenzelm@5828
   118
datatype node =
wenzelm@27576
   119
  Theory of generic_theory * Proof.context option
wenzelm@27576
   120
    (*theory with presentation context*) |
wenzelm@33390
   121
  Proof of Proof_Node.T * ((Proof.context -> generic_theory) * generic_theory)
wenzelm@27576
   122
    (*proof node, finish, original theory*) |
wenzelm@27564
   123
  SkipProof of int * (generic_theory * generic_theory);
wenzelm@27564
   124
    (*proof depth, resulting theory, original theory*)
wenzelm@5828
   125
wenzelm@20963
   126
val theory_node = fn Theory (gthy, _) => SOME gthy | _ => NONE;
wenzelm@18589
   127
val proof_node = fn Proof (prf, _) => SOME prf | _ => NONE;
wenzelm@18589
   128
wenzelm@20963
   129
fun cases_node f _ (Theory (gthy, _)) = f gthy
wenzelm@33390
   130
  | cases_node _ g (Proof (prf, _)) = g (Proof_Node.current prf)
wenzelm@21007
   131
  | cases_node f _ (SkipProof (_, (gthy, _))) = f gthy;
wenzelm@19063
   132
wenzelm@29066
   133
val context_node = cases_node Context.proof_of Proof.context_of;
wenzelm@29066
   134
wenzelm@21958
   135
wenzelm@21958
   136
(* datatype state *)
wenzelm@21958
   137
wenzelm@37953
   138
datatype state = State of node option * node option;  (*current, previous*)
wenzelm@5828
   139
wenzelm@27576
   140
val toplevel = State (NONE, NONE);
wenzelm@5828
   141
wenzelm@27576
   142
fun is_toplevel (State (NONE, _)) = true
wenzelm@7732
   143
  | is_toplevel _ = false;
wenzelm@7732
   144
wenzelm@27576
   145
fun level (State (NONE, _)) = 0
wenzelm@37953
   146
  | level (State (SOME (Theory _), _)) = 0
wenzelm@37953
   147
  | level (State (SOME (Proof (prf, _)), _)) = Proof.level (Proof_Node.current prf)
wenzelm@37953
   148
  | level (State (SOME (SkipProof (d, _)), _)) = d + 1;   (*different notion of proof depth!*)
wenzelm@17076
   149
wenzelm@27576
   150
fun str_of_state (State (NONE, _)) = "at top level"
wenzelm@37953
   151
  | str_of_state (State (SOME (Theory (Context.Theory _, _)), _)) = "in theory mode"
wenzelm@37953
   152
  | str_of_state (State (SOME (Theory (Context.Proof _, _)), _)) = "in local theory mode"
wenzelm@37953
   153
  | str_of_state (State (SOME (Proof _), _)) = "in proof mode"
wenzelm@37953
   154
  | str_of_state (State (SOME (SkipProof _), _)) = "in skipped proof mode";
wenzelm@5946
   155
wenzelm@5946
   156
wenzelm@27576
   157
(* current node *)
wenzelm@5828
   158
wenzelm@27576
   159
fun node_of (State (NONE, _)) = raise UNDEF
wenzelm@37953
   160
  | node_of (State (SOME node, _)) = node;
wenzelm@5828
   161
wenzelm@18589
   162
fun is_theory state = not (is_toplevel state) andalso is_some (theory_node (node_of state));
wenzelm@18589
   163
fun is_proof state = not (is_toplevel state) andalso is_some (proof_node (node_of state));
wenzelm@18589
   164
wenzelm@19063
   165
fun node_case f g state = cases_node f g (node_of state);
wenzelm@5828
   166
wenzelm@30398
   167
fun presentation_context_of state =
wenzelm@30398
   168
  (case try node_of state of
wenzelm@30398
   169
    SOME (Theory (_, SOME ctxt)) => ctxt
wenzelm@30398
   170
  | SOME node => context_node node
wenzelm@30398
   171
  | NONE => raise UNDEF);
wenzelm@30366
   172
wenzelm@30801
   173
fun previous_context_of (State (_, NONE)) = NONE
wenzelm@37953
   174
  | previous_context_of (State (_, SOME prev)) = SOME (context_node prev);
wenzelm@30801
   175
wenzelm@21506
   176
val context_of = node_case Context.proof_of Proof.context_of;
wenzelm@22089
   177
val generic_theory_of = node_case I (Context.Proof o Proof.context_of);
wenzelm@20963
   178
val theory_of = node_case Context.theory_of Proof.theory_of;
wenzelm@18589
   179
val proof_of = node_case (fn _ => raise UNDEF) I;
wenzelm@17208
   180
wenzelm@18589
   181
fun proof_position_of state =
wenzelm@18589
   182
  (case node_of state of
wenzelm@33390
   183
    Proof (prf, _) => Proof_Node.position prf
wenzelm@18589
   184
  | _ => raise UNDEF);
wenzelm@6664
   185
wenzelm@37953
   186
fun end_theory _ (State (NONE, SOME (Theory (Context.Theory thy, _)))) = Theory.end_theory thy
wenzelm@37953
   187
  | end_theory pos _ = error ("Unfinished theory at end of input" ^ Position.str_of pos);
wenzelm@37953
   188
wenzelm@5828
   189
wenzelm@16815
   190
(* print state *)
wenzelm@16815
   191
haftmann@38388
   192
val pretty_context = Local_Theory.pretty o Context.cases (Named_Target.theory_init) I;
wenzelm@16815
   193
wenzelm@23640
   194
fun print_state_context state =
wenzelm@24795
   195
  (case try node_of state of
wenzelm@21506
   196
    NONE => []
wenzelm@24795
   197
  | SOME (Theory (gthy, _)) => pretty_context gthy
wenzelm@24795
   198
  | SOME (Proof (_, (_, gthy))) => pretty_context gthy
wenzelm@24795
   199
  | SOME (SkipProof (_, (gthy, _))) => pretty_context gthy)
wenzelm@23640
   200
  |> Pretty.chunks |> Pretty.writeln;
wenzelm@16815
   201
wenzelm@23640
   202
fun print_state prf_only state =
wenzelm@23701
   203
  (case try node_of state of
wenzelm@23701
   204
    NONE => []
wenzelm@23701
   205
  | SOME (Theory (gthy, _)) => if prf_only then [] else pretty_context gthy
wenzelm@23701
   206
  | SOME (Proof (prf, _)) =>
wenzelm@33390
   207
      Proof.pretty_state (Proof_Node.position prf) (Proof_Node.current prf)
wenzelm@27564
   208
  | SOME (SkipProof (d, _)) => [Pretty.str ("skipped proof: depth " ^ string_of_int d)])
wenzelm@23701
   209
  |> Pretty.markup_chunks Markup.state |> Pretty.writeln;
wenzelm@16815
   210
wenzelm@37858
   211
fun pretty_abstract state = Pretty.str ("<Isar " ^ str_of_state state ^ ">");
wenzelm@37858
   212
wenzelm@16815
   213
wenzelm@15668
   214
wenzelm@5828
   215
(** toplevel transitions **)
wenzelm@5828
   216
wenzelm@32738
   217
val quiet = Unsynchronized.ref false;
wenzelm@22135
   218
val debug = Output.debugging;
wenzelm@32738
   219
val interact = Unsynchronized.ref false;
wenzelm@16682
   220
val timing = Output.timing;
wenzelm@32738
   221
val profiling = Unsynchronized.ref 0;
wenzelm@32738
   222
val skip_proofs = Unsynchronized.ref false;
wenzelm@16682
   223
wenzelm@33604
   224
fun program body =
wenzelm@33604
   225
 (body
wenzelm@31476
   226
  |> Runtime.controlled_execution
wenzelm@33604
   227
  |> Runtime.toplevel_error (Output.error_msg o ML_Compiler.exn_message)) ();
wenzelm@33604
   228
wenzelm@33604
   229
fun thread interrupts body =
wenzelm@33604
   230
  Thread.fork
wenzelm@39232
   231
    (((fn () => body () handle exn => if Exn.is_interrupt exn then () else reraise exn)
wenzelm@33604
   232
        |> Runtime.debugging
wenzelm@33604
   233
        |> Runtime.toplevel_error
wenzelm@33604
   234
          (fn exn => priority ("## INTERNAL ERROR ##\n" ^ ML_Compiler.exn_message exn))),
wenzelm@37216
   235
      Simple_Thread.attributes interrupts);
wenzelm@20128
   236
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@6689
   242
local
wenzelm@6689
   243
wenzelm@30366
   244
fun reset_presentation (Theory (gthy, _)) = Theory (gthy, NONE)
wenzelm@30366
   245
  | reset_presentation node = node;
wenzelm@30366
   246
wenzelm@26624
   247
fun is_draft_theory (Theory (gthy, _)) = Context.is_draft (Context.theory_of gthy)
wenzelm@26624
   248
  | is_draft_theory _ = false;
wenzelm@26624
   249
wenzelm@31476
   250
fun is_stale state = Context.is_stale (theory_of state) handle Runtime.UNDEF => false;
wenzelm@27601
   251
wenzelm@26624
   252
fun stale_error NONE = SOME (ERROR "Stale theory encountered after successful execution!")
wenzelm@26624
   253
  | stale_error some = some;
wenzelm@16815
   254
wenzelm@27576
   255
fun map_theory f (Theory (gthy, ctxt)) =
wenzelm@33671
   256
      Theory (Context.mapping f (Local_Theory.raw_theory f) gthy, ctxt)
wenzelm@27576
   257
  | map_theory _ node = node;
wenzelm@6689
   258
wenzelm@6689
   259
in
wenzelm@6689
   260
wenzelm@37953
   261
fun apply_transaction f g node =
wenzelm@20128
   262
  let
wenzelm@27576
   263
    val _ = is_draft_theory node andalso error "Illegal draft theory in toplevel state";
wenzelm@27576
   264
    val cont_node = reset_presentation node;
wenzelm@27576
   265
    val back_node = map_theory (Theory.checkpoint o Theory.copy) cont_node;
wenzelm@37953
   266
    fun state_error e nd = (State (SOME nd, SOME node), e);
wenzelm@6689
   267
wenzelm@20128
   268
    val (result, err) =
wenzelm@20128
   269
      cont_node
wenzelm@31476
   270
      |> Runtime.controlled_execution f
wenzelm@26624
   271
      |> map_theory Theory.checkpoint
wenzelm@26624
   272
      |> state_error NONE
wenzelm@26624
   273
      handle exn => state_error (SOME exn) cont_node;
wenzelm@26624
   274
wenzelm@26624
   275
    val (result', err') =
wenzelm@26624
   276
      if is_stale result then state_error (stale_error err) back_node
wenzelm@26624
   277
      else (result, err);
wenzelm@20128
   278
  in
wenzelm@26624
   279
    (case err' of
wenzelm@30366
   280
      NONE => tap g result'
wenzelm@26624
   281
    | SOME exn => raise FAILURE (result', exn))
wenzelm@20128
   282
  end;
wenzelm@6689
   283
wenzelm@6689
   284
end;
wenzelm@6689
   285
wenzelm@6689
   286
wenzelm@6689
   287
(* primitive transitions *)
wenzelm@6689
   288
wenzelm@5828
   289
datatype trans =
wenzelm@37977
   290
  Init of string * (unit -> theory) |    (*theory name, init*)
wenzelm@37953
   291
  Exit |                                 (*formal exit of theory*)
wenzelm@37953
   292
  Keep of bool -> state -> unit |        (*peek at state*)
wenzelm@30366
   293
  Transaction of (bool -> node -> node) * (state -> unit);  (*node transaction and presentation*)
wenzelm@21958
   294
wenzelm@6689
   295
local
wenzelm@5828
   296
wenzelm@37977
   297
fun apply_tr _ (Init (_, f)) (State (NONE, _)) =
wenzelm@33727
   298
      State (SOME (Theory (Context.Theory
wenzelm@37977
   299
          (Theory.checkpoint (Runtime.controlled_execution f ())), NONE)), NONE)
wenzelm@37953
   300
  | apply_tr _ Exit (State (prev as SOME (Theory (Context.Theory _, _)), _)) =
wenzelm@27603
   301
      State (NONE, prev)
wenzelm@32792
   302
  | apply_tr int (Keep f) state =
wenzelm@31476
   303
      Runtime.controlled_execution (fn x => tap (f int) x) state
wenzelm@32792
   304
  | apply_tr int (Transaction (f, g)) (State (SOME state, _)) =
wenzelm@32792
   305
      apply_transaction (fn x => f int x) g state
wenzelm@32792
   306
  | apply_tr _ _ _ = raise UNDEF;
wenzelm@5828
   307
wenzelm@32792
   308
fun apply_union _ [] state = raise FAILURE (state, UNDEF)
wenzelm@32792
   309
  | apply_union int (tr :: trs) state =
wenzelm@32792
   310
      apply_union int trs state
wenzelm@32792
   311
        handle Runtime.UNDEF => apply_tr int tr state
wenzelm@32792
   312
          | FAILURE (alt_state, UNDEF) => apply_tr int tr alt_state
wenzelm@6689
   313
          | exn as FAILURE _ => raise exn
wenzelm@6689
   314
          | exn => raise FAILURE (state, exn);
wenzelm@6689
   315
wenzelm@6689
   316
in
wenzelm@6689
   317
wenzelm@32792
   318
fun apply_trans int trs state = (apply_union int trs state, NONE)
skalberg@15531
   319
  handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn);
wenzelm@6689
   320
wenzelm@6689
   321
end;
wenzelm@5828
   322
wenzelm@5828
   323
wenzelm@5828
   324
(* datatype transition *)
wenzelm@5828
   325
wenzelm@5828
   326
datatype transition = Transition of
wenzelm@26621
   327
 {name: string,              (*command name*)
wenzelm@26621
   328
  pos: Position.T,           (*source position*)
wenzelm@26621
   329
  int_only: bool,            (*interactive-only*)
wenzelm@26621
   330
  print: bool,               (*print result state*)
wenzelm@26621
   331
  no_timing: bool,           (*suppress timing*)
wenzelm@26621
   332
  trans: trans list};        (*primitive transitions (union)*)
wenzelm@5828
   333
wenzelm@26621
   334
fun make_transition (name, pos, int_only, print, no_timing, trans) =
wenzelm@26621
   335
  Transition {name = name, pos = pos, int_only = int_only, print = print, no_timing = no_timing,
wenzelm@26621
   336
    trans = trans};
wenzelm@5828
   337
wenzelm@26621
   338
fun map_transition f (Transition {name, pos, int_only, print, no_timing, trans}) =
wenzelm@26621
   339
  make_transition (f (name, pos, int_only, print, no_timing, trans));
wenzelm@5828
   340
wenzelm@27441
   341
val empty = make_transition ("", Position.none, false, false, false, []);
wenzelm@5828
   342
wenzelm@5828
   343
wenzelm@5828
   344
(* diagnostics *)
wenzelm@5828
   345
wenzelm@37953
   346
fun init_of (Transition {trans = [Init (name, _)], ...}) = SOME name
wenzelm@27441
   347
  | init_of _ = NONE;
wenzelm@27441
   348
wenzelm@38888
   349
fun print_of (Transition {print, ...}) = print;
wenzelm@27427
   350
fun name_of (Transition {name, ...}) = name;
wenzelm@28105
   351
fun pos_of (Transition {pos, ...}) = pos;
wenzelm@28105
   352
fun str_of tr = quote (name_of tr) ^ Position.str_of (pos_of tr);
wenzelm@5828
   353
wenzelm@27427
   354
fun command_msg msg tr = msg ^ "command " ^ str_of tr;
wenzelm@38875
   355
fun at_command tr = command_msg "At " tr;
wenzelm@5828
   356
wenzelm@5828
   357
fun type_error tr state =
wenzelm@18685
   358
  ERROR (command_msg "Illegal application of " tr ^ " " ^ str_of_state state);
wenzelm@5828
   359
wenzelm@5828
   360
wenzelm@5828
   361
(* modify transitions *)
wenzelm@5828
   362
wenzelm@28451
   363
fun name name = map_transition (fn (_, pos, int_only, print, no_timing, trans) =>
wenzelm@28451
   364
  (name, pos, int_only, print, no_timing, trans));
wenzelm@9010
   365
wenzelm@26621
   366
fun position pos = map_transition (fn (name, _, int_only, print, no_timing, trans) =>
wenzelm@26621
   367
  (name, pos, int_only, print, no_timing, trans));
wenzelm@5828
   368
wenzelm@26621
   369
fun interactive int_only = map_transition (fn (name, pos, _, print, no_timing, trans) =>
wenzelm@26621
   370
  (name, pos, int_only, print, no_timing, trans));
wenzelm@14923
   371
wenzelm@26621
   372
val no_timing = map_transition (fn (name, pos, int_only, print, _, trans) =>
wenzelm@26621
   373
  (name, pos, int_only, print, true, trans));
wenzelm@17363
   374
wenzelm@26621
   375
fun add_trans tr = map_transition (fn (name, pos, int_only, print, no_timing, trans) =>
wenzelm@28451
   376
  (name, pos, int_only, print, no_timing, tr :: trans));
wenzelm@16607
   377
wenzelm@28433
   378
val reset_trans = map_transition (fn (name, pos, int_only, print, no_timing, _) =>
wenzelm@28433
   379
  (name, pos, int_only, print, no_timing, []));
wenzelm@28433
   380
wenzelm@28453
   381
fun set_print print = map_transition (fn (name, pos, int_only, _, no_timing, trans) =>
wenzelm@28453
   382
  (name, pos, int_only, print, no_timing, trans));
wenzelm@28453
   383
wenzelm@28453
   384
val print = set_print true;
wenzelm@5828
   385
wenzelm@5828
   386
wenzelm@21007
   387
(* basic transitions *)
wenzelm@5828
   388
wenzelm@37953
   389
fun init_theory name f = add_trans (Init (name, f));
wenzelm@37977
   390
wenzelm@37977
   391
fun modify_init f tr =
wenzelm@37977
   392
  (case init_of tr of
wenzelm@37977
   393
    SOME name => init_theory name f (reset_trans tr)
wenzelm@37977
   394
  | NONE => tr);
wenzelm@37977
   395
wenzelm@6689
   396
val exit = add_trans Exit;
wenzelm@7612
   397
val keep' = add_trans o Keep;
wenzelm@30366
   398
wenzelm@30366
   399
fun present_transaction f g = add_trans (Transaction (f, g));
wenzelm@30366
   400
fun transaction f = present_transaction f (K ());
wenzelm@5828
   401
wenzelm@7612
   402
fun keep f = add_trans (Keep (fn _ => f));
wenzelm@5828
   403
fun imperative f = keep (fn _ => f ());
wenzelm@5828
   404
wenzelm@27840
   405
fun ignored pos = empty |> name "<ignored>" |> position pos |> imperative I;
wenzelm@27840
   406
fun malformed pos msg =
wenzelm@27840
   407
  empty |> name "<malformed>" |> position pos |> imperative (fn () => error msg);
wenzelm@27840
   408
wenzelm@21007
   409
val unknown_theory = imperative (fn () => warning "Unknown theory context");
wenzelm@21007
   410
val unknown_proof = imperative (fn () => warning "Unknown proof context");
wenzelm@21007
   411
val unknown_context = imperative (fn () => warning "Unknown context");
wenzelm@15668
   412
wenzelm@21007
   413
wenzelm@21007
   414
(* theory transitions *)
wenzelm@15668
   415
wenzelm@27601
   416
fun generic_theory f = transaction (fn _ =>
wenzelm@26491
   417
  (fn Theory (gthy, _) => Theory (f gthy, NONE)
wenzelm@26491
   418
    | _ => raise UNDEF));
wenzelm@26491
   419
wenzelm@27601
   420
fun theory' f = transaction (fn int =>
wenzelm@33725
   421
  (fn Theory (Context.Theory thy, _) =>
wenzelm@33725
   422
      let val thy' = thy
wenzelm@33725
   423
        |> Sign.new_group
wenzelm@33725
   424
        |> Theory.checkpoint
wenzelm@33725
   425
        |> f int
wenzelm@33725
   426
        |> Sign.reset_group;
wenzelm@33725
   427
      in Theory (Context.Theory thy', NONE) end
wenzelm@20963
   428
    | _ => raise UNDEF));
wenzelm@20963
   429
wenzelm@20963
   430
fun theory f = theory' (K f);
wenzelm@20963
   431
wenzelm@27601
   432
fun begin_local_theory begin f = transaction (fn _ =>
wenzelm@20963
   433
  (fn Theory (Context.Theory thy, _) =>
wenzelm@20963
   434
        let
wenzelm@20985
   435
          val lthy = f thy;
wenzelm@21294
   436
          val gthy = if begin then Context.Proof lthy else Context.Theory (loc_exit lthy);
wenzelm@21294
   437
        in Theory (gthy, SOME lthy) end
wenzelm@20963
   438
    | _ => raise UNDEF));
wenzelm@17076
   439
wenzelm@27601
   440
val end_local_theory = transaction (fn _ =>
wenzelm@21294
   441
  (fn Theory (Context.Proof lthy, _) => Theory (Context.Theory (loc_exit lthy), SOME lthy)
wenzelm@21007
   442
    | _ => raise UNDEF));
wenzelm@21007
   443
wenzelm@21007
   444
local
wenzelm@21007
   445
wenzelm@30366
   446
fun local_theory_presentation loc f = present_transaction (fn int =>
wenzelm@21294
   447
  (fn Theory (gthy, _) =>
wenzelm@21294
   448
        let
wenzelm@21294
   449
          val finish = loc_finish loc gthy;
wenzelm@33725
   450
          val lthy' = loc_begin loc gthy
wenzelm@33725
   451
            |> Local_Theory.new_group
wenzelm@33725
   452
            |> f int
wenzelm@33725
   453
            |> Local_Theory.reset_group;
wenzelm@21294
   454
        in Theory (finish lthy', SOME lthy') end
wenzelm@30366
   455
    | _ => raise UNDEF));
wenzelm@15668
   456
wenzelm@21007
   457
in
wenzelm@21007
   458
wenzelm@30366
   459
fun local_theory' loc f = local_theory_presentation loc f (K ());
wenzelm@29380
   460
fun local_theory loc f = local_theory' loc (K f);
wenzelm@30366
   461
fun present_local_theory loc = local_theory_presentation loc (K I);
wenzelm@18955
   462
wenzelm@21007
   463
end;
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@21007
   475
            in Theory (gthy', SOME ctxt') end
wenzelm@21007
   476
          else raise UNDEF
wenzelm@21007
   477
        end
wenzelm@27564
   478
    | SkipProof (0, (gthy, _)) => Theory (gthy, NONE)
wenzelm@21007
   479
    | _ => raise UNDEF));
wenzelm@21007
   480
wenzelm@21294
   481
local
wenzelm@21294
   482
wenzelm@27601
   483
fun begin_proof init finish = transaction (fn int =>
wenzelm@21294
   484
  (fn Theory (gthy, _) =>
wenzelm@21294
   485
    let
berghofe@24453
   486
      val prf = init int gthy;
wenzelm@28433
   487
      val skip = ! skip_proofs;
wenzelm@21294
   488
      val schematic = Proof.schematic_goal prf;
wenzelm@21294
   489
    in
wenzelm@28433
   490
      if skip andalso schematic then
wenzelm@21294
   491
        warning "Cannot skip proof of schematic goal statement"
wenzelm@21294
   492
      else ();
wenzelm@28433
   493
      if skip andalso not schematic then
wenzelm@27564
   494
        SkipProof (0, (finish gthy (Proof.global_skip_proof int prf), gthy))
wenzelm@33390
   495
      else Proof (Proof_Node.init prf, (finish gthy, gthy))
wenzelm@21294
   496
    end
wenzelm@21294
   497
  | _ => raise UNDEF));
wenzelm@21294
   498
wenzelm@21294
   499
in
wenzelm@21294
   500
wenzelm@24780
   501
fun local_theory_to_proof' loc f = begin_proof
wenzelm@33725
   502
  (fn int => fn gthy => f int (Local_Theory.new_group (loc_begin loc gthy)))
wenzelm@33725
   503
  (fn gthy => loc_finish loc gthy o Local_Theory.reset_group);
wenzelm@24780
   504
berghofe@24453
   505
fun local_theory_to_proof loc f = local_theory_to_proof' loc (K f);
wenzelm@21294
   506
wenzelm@21294
   507
fun theory_to_proof f = begin_proof
wenzelm@33725
   508
  (K (fn Context.Theory thy => f (Theory.checkpoint (Sign.new_group thy)) | _ => raise UNDEF))
wenzelm@33725
   509
  (K (Context.Theory o Sign.reset_group o ProofContext.theory_of));
wenzelm@21294
   510
wenzelm@21294
   511
end;
wenzelm@21294
   512
wenzelm@27601
   513
val forget_proof = transaction (fn _ =>
wenzelm@21007
   514
  (fn Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
wenzelm@21007
   515
    | SkipProof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
wenzelm@21007
   516
    | _ => raise UNDEF));
wenzelm@21007
   517
wenzelm@30366
   518
val present_proof = present_transaction (fn _ =>
wenzelm@33390
   519
  (fn Proof (prf, x) => Proof (Proof_Node.apply I prf, x)
wenzelm@27564
   520
    | skip as SkipProof _ => skip
wenzelm@30366
   521
    | _ => raise UNDEF));
wenzelm@21177
   522
wenzelm@27601
   523
fun proofs' f = transaction (fn int =>
wenzelm@33390
   524
  (fn Proof (prf, x) => Proof (Proof_Node.applys (f int) prf, x)
wenzelm@27564
   525
    | skip as SkipProof _ => skip
wenzelm@16815
   526
    | _ => raise UNDEF));
wenzelm@15668
   527
wenzelm@17904
   528
fun proof' f = proofs' (Seq.single oo f);
wenzelm@17904
   529
val proofs = proofs' o K;
wenzelm@6689
   530
val proof = proof' o K;
wenzelm@16815
   531
wenzelm@27601
   532
fun actual_proof f = transaction (fn _ =>
wenzelm@21007
   533
  (fn Proof (prf, x) => Proof (f prf, x)
wenzelm@20963
   534
    | _ => raise UNDEF));
wenzelm@16815
   535
wenzelm@27601
   536
fun skip_proof f = transaction (fn _ =>
wenzelm@21007
   537
  (fn SkipProof (h, x) => SkipProof (f h, x)
wenzelm@18563
   538
    | _ => raise UNDEF));
wenzelm@18563
   539
wenzelm@27601
   540
fun skip_proof_to_theory pred = transaction (fn _ =>
wenzelm@27564
   541
  (fn SkipProof (d, (gthy, _)) => if pred d then Theory (gthy, NONE) else raise UNDEF
wenzelm@33725
   542
    | _ => raise UNDEF));
wenzelm@5828
   543
wenzelm@5828
   544
wenzelm@5828
   545
wenzelm@5828
   546
(** toplevel transactions **)
wenzelm@5828
   547
wenzelm@27427
   548
(* identification *)
wenzelm@27427
   549
wenzelm@27427
   550
fun get_id (Transition {pos, ...}) = Position.get_id pos;
wenzelm@27427
   551
fun put_id id (tr as Transition {pos, ...}) = position (Position.put_id id pos) tr;
wenzelm@27427
   552
wenzelm@27427
   553
wenzelm@25960
   554
(* thread position *)
wenzelm@25799
   555
wenzelm@25960
   556
fun setmp_thread_position (Transition {pos, ...}) f x =
wenzelm@25819
   557
  Position.setmp_thread_data pos f x;
wenzelm@25799
   558
wenzelm@27606
   559
fun status tr m =
wenzelm@27606
   560
  setmp_thread_position tr (fn () => Output.status (Markup.markup m "")) ();
wenzelm@27606
   561
wenzelm@38876
   562
fun error_msg tr msg =
wenzelm@38876
   563
  setmp_thread_position tr (fn () => Output.error_msg msg) ();
wenzelm@26602
   564
wenzelm@25799
   565
wenzelm@28095
   566
(* post-transition hooks *)
wenzelm@28095
   567
wenzelm@37905
   568
local
wenzelm@37905
   569
  val hooks = Unsynchronized.ref ([]: (transition -> state -> state -> unit) list);
wenzelm@37905
   570
in
wenzelm@28095
   571
wenzelm@32738
   572
fun add_hook f = CRITICAL (fn () => Unsynchronized.change hooks (cons f));
wenzelm@33223
   573
fun get_hooks () = ! hooks;
wenzelm@28095
   574
wenzelm@28095
   575
end;
wenzelm@28095
   576
wenzelm@28095
   577
wenzelm@5828
   578
(* apply transitions *)
wenzelm@5828
   579
wenzelm@6664
   580
local
wenzelm@6664
   581
wenzelm@32792
   582
fun app int (tr as Transition {trans, print, no_timing, ...}) =
wenzelm@25819
   583
  setmp_thread_position tr (fn state =>
wenzelm@25799
   584
    let
wenzelm@25799
   585
      fun do_timing f x = (warning (command_msg "" tr); timeap f x);
wenzelm@25799
   586
      fun do_profiling f x = profile (! profiling) f x;
wenzelm@25799
   587
wenzelm@26256
   588
      val (result, status) =
wenzelm@37905
   589
         state |>
wenzelm@37905
   590
          (apply_trans int trans
wenzelm@37905
   591
            |> (! profiling > 0 andalso not no_timing) ? do_profiling
wenzelm@37905
   592
            |> (! profiling > 0 orelse ! timing andalso not no_timing) ? do_timing);
wenzelm@26256
   593
wenzelm@26621
   594
      val _ = if int andalso not (! quiet) andalso print then print_state false result else ();
wenzelm@26256
   595
    in (result, Option.map (fn UNDEF => type_error tr state | exn => exn) status) end);
wenzelm@6664
   596
wenzelm@6664
   597
in
wenzelm@5828
   598
wenzelm@26602
   599
fun transition int tr st =
wenzelm@28095
   600
  let
wenzelm@28095
   601
    val hooks = get_hooks ();
wenzelm@28103
   602
    fun apply_hooks st' = hooks |> List.app (fn f => (try (fn () => f tr st st') (); ()));
wenzelm@28095
   603
wenzelm@28095
   604
    val ctxt = try context_of st;
wenzelm@28095
   605
    val res =
wenzelm@28095
   606
      (case app int tr st of
wenzelm@38888
   607
        (_, SOME Runtime.TERMINATE) => NONE
wenzelm@38888
   608
      | (st', SOME (Runtime.EXCURSION_FAIL exn_info)) => SOME (st', SOME exn_info)
wenzelm@31476
   609
      | (st', SOME exn) => SOME (st', SOME (Runtime.exn_context ctxt exn, at_command tr))
wenzelm@28103
   610
      | (st', NONE) => SOME (st', NONE));
wenzelm@28103
   611
    val _ = (case res of SOME (st', NONE) => apply_hooks st' | _ => ());
wenzelm@28095
   612
  in res end;
wenzelm@6664
   613
wenzelm@6664
   614
end;
wenzelm@5828
   615
wenzelm@5828
   616
wenzelm@28425
   617
(* nested commands *)
wenzelm@5828
   618
wenzelm@28425
   619
fun command tr st =
wenzelm@27576
   620
  (case transition (! interact) tr st of
wenzelm@28425
   621
    SOME (st', NONE) => st'
wenzelm@39285
   622
  | SOME (_, SOME (exn, info)) =>
wenzelm@39285
   623
      if Exn.is_interrupt exn then reraise exn else raise Runtime.EXCURSION_FAIL (exn, info)
wenzelm@38888
   624
  | NONE => raise Runtime.EXCURSION_FAIL (Runtime.TERMINATE, at_command tr));
wenzelm@27576
   625
wenzelm@29483
   626
fun command_result tr st =
wenzelm@29483
   627
  let val st' = command tr st
wenzelm@29483
   628
  in (st', st') end;
wenzelm@29483
   629
wenzelm@28433
   630
wenzelm@28433
   631
(* excursion of units, consisting of commands with proof *)
wenzelm@28433
   632
wenzelm@33519
   633
structure States = Proof_Data
wenzelm@28974
   634
(
wenzelm@28974
   635
  type T = state list future option;
wenzelm@28974
   636
  fun init _ = NONE;
wenzelm@28974
   637
);
wenzelm@28974
   638
wenzelm@28974
   639
fun proof_result immediate (tr, proof_trs) st =
wenzelm@28433
   640
  let val st' = command tr st in
wenzelm@36315
   641
    if immediate orelse
wenzelm@36315
   642
      null proof_trs orelse
wenzelm@36950
   643
      Keyword.is_schematic_goal (name_of tr) orelse
wenzelm@36950
   644
      exists (Keyword.is_qed_global o name_of) proof_trs orelse
wenzelm@36315
   645
      not (can proof_of st') orelse
wenzelm@36315
   646
      Proof.is_relevant (proof_of st')
wenzelm@28433
   647
    then
wenzelm@28453
   648
      let val (states, st'') = fold_map command_result proof_trs st'
wenzelm@28974
   649
      in (Lazy.value ((tr, st') :: (proof_trs ~~ states)), st'') end
wenzelm@28453
   650
    else
wenzelm@28433
   651
      let
wenzelm@28433
   652
        val (body_trs, end_tr) = split_last proof_trs;
wenzelm@28453
   653
        val finish = Context.Theory o ProofContext.theory_of;
wenzelm@28974
   654
wenzelm@29386
   655
        val future_proof = Proof.global_future_proof
wenzelm@28974
   656
          (fn prf =>
wenzelm@32062
   657
            Future.fork_pri ~1 (fn () =>
wenzelm@30366
   658
              let val (states, result_state) =
wenzelm@37953
   659
                (case st' of State (SOME (Proof (_, (_, orig_gthy))), prev)
wenzelm@37953
   660
                  => State (SOME (Proof (Proof_Node.init prf, (finish, orig_gthy))), prev))
wenzelm@28974
   661
                |> fold_map command_result body_trs
wenzelm@28974
   662
                ||> command (end_tr |> set_print false);
wenzelm@30398
   663
              in (states, presentation_context_of result_state) end))
wenzelm@28974
   664
          #> (fn (states, ctxt) => States.put (SOME states) ctxt);
wenzelm@28974
   665
wenzelm@28974
   666
        val st'' = st' |> command (end_tr |> reset_trans |> end_proof (K future_proof));
wenzelm@28974
   667
wenzelm@28974
   668
        val states =
wenzelm@30398
   669
          (case States.get (presentation_context_of st'') of
wenzelm@37852
   670
            NONE => raise Fail ("No future states for " ^ name_of tr ^ Position.str_of (pos_of tr))
wenzelm@28974
   671
          | SOME states => states);
wenzelm@28974
   672
        val result = Lazy.lazy
wenzelm@28974
   673
          (fn () => (tr, st') :: (body_trs ~~ Future.join states) @ [(end_tr, st'')]);
wenzelm@28974
   674
wenzelm@28974
   675
      in (result, st'') end
wenzelm@28433
   676
  end;
wenzelm@28433
   677
wenzelm@29068
   678
fun excursion input =
wenzelm@28425
   679
  let
wenzelm@28433
   680
    val end_pos = if null input then error "No input" else pos_of (fst (List.last input));
wenzelm@29448
   681
    val immediate = not (Goal.future_enabled ());
wenzelm@29427
   682
    val (results, end_state) = fold_map (proof_result immediate) input toplevel;
wenzelm@37953
   683
    val thy = end_theory end_pos end_state;
wenzelm@37953
   684
  in (Lazy.lazy (fn () => maps Lazy.force results), thy) end;
wenzelm@7062
   685
wenzelm@6664
   686
end;