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