src/Pure/Isar/toplevel.ML
author berghofe
Mon Oct 11 10:51:19 2004 +0200 (2004-10-11)
changeset 15237 250e9be7a09d
parent 14985 fefcf6cd858a
child 15431 6f4959ba7664
permissions -rw-r--r--
Some changes to allow skipping of proof scripts.
wenzelm@5828
     1
(*  Title:      Pure/Isar/toplevel.ML
wenzelm@5828
     2
    ID:         $Id$
wenzelm@5828
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5828
     4
wenzelm@5828
     5
The Isabelle/Isar toplevel.
wenzelm@5828
     6
*)
wenzelm@5828
     7
wenzelm@5828
     8
signature TOPLEVEL =
wenzelm@5828
     9
sig
wenzelm@5828
    10
  datatype node = Theory of theory | Proof of ProofHistory.T
berghofe@15237
    11
    | SkipProof of int History.T * theory
wenzelm@5828
    12
  type state
wenzelm@5828
    13
  val toplevel: state
wenzelm@7732
    14
  val is_toplevel: state -> bool
wenzelm@6689
    15
  val prompt_state_default: state -> string
wenzelm@6689
    16
  val prompt_state_fn: (state -> string) ref
wenzelm@7308
    17
  val print_state_context: state -> unit
wenzelm@8886
    18
  val print_state_default: bool -> state -> unit
wenzelm@8886
    19
  val print_state_fn: (bool -> state -> unit) ref
wenzelm@8886
    20
  val print_state: bool -> state -> unit
wenzelm@12423
    21
  val pretty_state: bool -> state -> Pretty.T list
wenzelm@6689
    22
  exception UNDEF
wenzelm@6689
    23
  val node_history_of: state -> node History.T
wenzelm@5828
    24
  val node_of: state -> node
wenzelm@6664
    25
  val node_case: (theory -> 'a) -> (Proof.state -> 'a) -> state -> 'a
wenzelm@5828
    26
  val theory_of: state -> theory
wenzelm@5828
    27
  val sign_of: state -> Sign.sg
wenzelm@7501
    28
  val context_of: state -> Proof.context
wenzelm@5828
    29
  val proof_of: state -> Proof.state
wenzelm@9453
    30
  val enter_forward_proof: state -> Proof.state
wenzelm@5828
    31
  type transition
wenzelm@5828
    32
  exception TERMINATE
wenzelm@5990
    33
  exception RESTART
wenzelm@6689
    34
  val undo_limit: bool -> int option
wenzelm@5828
    35
  val empty: transition
wenzelm@14923
    36
  val name_of: transition -> string
wenzelm@14923
    37
  val source_of: transition -> OuterLex.token list option
wenzelm@5828
    38
  val name: string -> transition -> transition
wenzelm@5828
    39
  val position: Position.T -> transition -> transition
wenzelm@14923
    40
  val source: OuterLex.token list -> transition -> transition
wenzelm@5828
    41
  val interactive: bool -> transition -> transition
wenzelm@5828
    42
  val print: transition -> transition
wenzelm@9010
    43
  val no_timing: transition -> transition
wenzelm@5828
    44
  val reset: transition -> transition
wenzelm@8465
    45
  val init: (bool -> node) -> (node -> unit) -> (node -> unit) -> transition -> transition
wenzelm@6689
    46
  val exit: transition -> transition
wenzelm@6689
    47
  val kill: transition -> transition
wenzelm@5828
    48
  val keep: (state -> unit) -> transition -> transition
wenzelm@7612
    49
  val keep': (bool -> state -> unit) -> transition -> transition
wenzelm@6689
    50
  val history: (node History.T -> node History.T) -> transition -> transition
wenzelm@5828
    51
  val imperative: (unit -> unit) -> transition -> transition
wenzelm@7105
    52
  val init_theory: (bool -> theory) -> (theory -> unit) -> (theory -> unit)
wenzelm@6689
    53
    -> transition -> transition
berghofe@15237
    54
  val skip_proofs: bool ref
wenzelm@5828
    55
  val theory: (theory -> theory) -> transition -> transition
wenzelm@7612
    56
  val theory': (bool -> theory -> theory) -> transition -> transition
wenzelm@6689
    57
  val theory_to_proof: (bool -> theory -> ProofHistory.T) -> transition -> transition
wenzelm@5828
    58
  val proof: (ProofHistory.T -> ProofHistory.T) -> transition -> transition
wenzelm@6689
    59
  val proof': (bool -> ProofHistory.T -> ProofHistory.T) -> transition -> transition
berghofe@15237
    60
  val proof'': (ProofHistory.T -> ProofHistory.T) -> transition -> transition
wenzelm@5828
    61
  val proof_to_theory: (ProofHistory.T -> theory) -> transition -> transition
wenzelm@12315
    62
  val proof_to_theory': (bool -> ProofHistory.T -> theory) -> transition -> transition
berghofe@15237
    63
  val skip_proof: (int History.T -> int History.T) -> transition -> transition
berghofe@15237
    64
  val skip_proof_to_theory: (int History.T -> bool) -> transition -> transition
wenzelm@9512
    65
  val unknown_theory: transition -> transition
wenzelm@9512
    66
  val unknown_proof: transition -> transition
wenzelm@9512
    67
  val unknown_context: transition -> transition
wenzelm@7198
    68
  val quiet: bool ref
wenzelm@5922
    69
  val exn_message: exn -> string
wenzelm@5828
    70
  val apply: bool -> transition -> state -> (state * (exn * string) option) option
wenzelm@9134
    71
  val excursion_result: ((transition * (state -> 'a -> 'a)) list * 'a) -> 'a
wenzelm@5828
    72
  val excursion: transition list -> unit
wenzelm@5828
    73
  val set_state: state -> unit
wenzelm@5828
    74
  val get_state: unit -> state
wenzelm@5828
    75
  val exn: unit -> (exn * string) option
wenzelm@5828
    76
  val >> : transition -> bool
wenzelm@14985
    77
  val >>> : transition list -> unit
nipkow@14091
    78
  type 'a isar
nipkow@14091
    79
  val loop: 'a isar -> unit
wenzelm@5828
    80
end;
wenzelm@5828
    81
wenzelm@6965
    82
structure Toplevel: TOPLEVEL =
wenzelm@5828
    83
struct
wenzelm@5828
    84
wenzelm@5828
    85
wenzelm@5828
    86
(** toplevel state **)
wenzelm@5828
    87
wenzelm@5828
    88
(* datatype node *)
wenzelm@5828
    89
wenzelm@5828
    90
datatype node =
wenzelm@5828
    91
  Theory of theory |
berghofe@15237
    92
  Proof of ProofHistory.T |
berghofe@15237
    93
  SkipProof of int History.T * theory;
wenzelm@5828
    94
wenzelm@6689
    95
fun str_of_node (Theory _) = "in theory mode"
berghofe@15237
    96
  | str_of_node _ = "in proof mode";
wenzelm@6689
    97
wenzelm@12423
    98
fun pretty_context thy = [Pretty.block
wenzelm@8561
    99
  [Pretty.str "theory", Pretty.brk 1, Pretty.str (PureThy.get_name thy),
wenzelm@12423
   100
    Pretty.str " =", Pretty.brk 1, Display.pretty_theory thy]];
wenzelm@7308
   101
wenzelm@12423
   102
fun pretty_prf prf = Proof.pretty_state (ProofHistory.position prf) (ProofHistory.current prf);
wenzelm@8886
   103
wenzelm@12423
   104
fun pretty_node_context (Theory thy) = pretty_context thy
berghofe@15237
   105
  | pretty_node_context (Proof prf) = pretty_context (Proof.theory_of (ProofHistory.current prf))
berghofe@15237
   106
  | pretty_node_context _ = [];
wenzelm@7308
   107
wenzelm@12423
   108
fun pretty_node prf_only (Theory thy) = if prf_only then [] else pretty_context thy
berghofe@15237
   109
  | pretty_node _ (Proof prf) = pretty_prf prf
berghofe@15237
   110
  | pretty_node _ _ = [];
wenzelm@5828
   111
wenzelm@5828
   112
wenzelm@5828
   113
(* datatype state *)
wenzelm@5828
   114
wenzelm@8465
   115
datatype state = State of (node History.T * ((node -> unit) * (node -> unit))) option;
wenzelm@5828
   116
wenzelm@8465
   117
val toplevel = State None;
wenzelm@5828
   118
wenzelm@8465
   119
fun is_toplevel (State None) = true
wenzelm@7732
   120
  | is_toplevel _ = false;
wenzelm@7732
   121
wenzelm@8465
   122
fun str_of_state (State None) = "at top level"
wenzelm@8465
   123
  | str_of_state (State (Some (node, _))) = str_of_node (History.current node);
wenzelm@6689
   124
wenzelm@6689
   125
wenzelm@6689
   126
(* prompt_state hook *)
wenzelm@6689
   127
wenzelm@8465
   128
fun prompt_state_default (State _) = Source.default_prompt;
wenzelm@6689
   129
wenzelm@6689
   130
val prompt_state_fn = ref prompt_state_default;
wenzelm@6689
   131
fun prompt_state state = ! prompt_state_fn state;
wenzelm@5828
   132
wenzelm@5828
   133
wenzelm@7308
   134
(* print state *)
wenzelm@5946
   135
wenzelm@12423
   136
fun pretty_current_node _ (State None) = []
wenzelm@12423
   137
  | pretty_current_node prt (State (Some (node, _))) = prt (History.current node);
wenzelm@7308
   138
wenzelm@12423
   139
val print_state_context =
wenzelm@12423
   140
  Pretty.writeln o Pretty.chunks o pretty_current_node pretty_node_context;
wenzelm@5946
   141
wenzelm@12423
   142
fun pretty_state prf_only state =
wenzelm@11894
   143
  let val ref (begin_state, end_state, _) = Display.current_goals_markers in
wenzelm@12423
   144
    (case pretty_current_node (pretty_node prf_only) state of [] => []
wenzelm@12423
   145
    | prts =>
wenzelm@12423
   146
        (if begin_state = "" then [] else [Pretty.str begin_state]) @ prts @
wenzelm@12423
   147
        (if end_state = "" then [] else [Pretty.str end_state]))
wenzelm@5946
   148
  end;
wenzelm@5946
   149
wenzelm@12423
   150
fun print_state_default prf_only state = Pretty.writelns (pretty_state prf_only state);
wenzelm@5946
   151
val print_state_fn = ref print_state_default;
wenzelm@5946
   152
fun print_state state = ! print_state_fn state;
wenzelm@5946
   153
wenzelm@5946
   154
wenzelm@5828
   155
(* top node *)
wenzelm@5828
   156
wenzelm@6689
   157
exception UNDEF;
wenzelm@6689
   158
wenzelm@8465
   159
fun node_history_of (State None) = raise UNDEF
wenzelm@8465
   160
  | node_history_of (State (Some (node, _))) = node;
wenzelm@6689
   161
wenzelm@6689
   162
val node_of = History.current o node_history_of;
wenzelm@5828
   163
wenzelm@6664
   164
fun node_case f g state =
wenzelm@5828
   165
  (case node_of state of
wenzelm@5828
   166
    Theory thy => f thy
berghofe@15237
   167
  | SkipProof (i, thy) => f thy
wenzelm@5828
   168
  | Proof prf => g (ProofHistory.current prf));
wenzelm@5828
   169
wenzelm@6664
   170
val theory_of = node_case I Proof.theory_of;
wenzelm@7501
   171
val context_of = node_case ProofContext.init Proof.context_of;
wenzelm@5828
   172
val sign_of = Theory.sign_of o theory_of;
wenzelm@6664
   173
val proof_of = node_case (fn _ => raise UNDEF) I;
wenzelm@6664
   174
wenzelm@12049
   175
val enter_forward_proof = node_case Proof.init_state Proof.enter_forward;
wenzelm@5828
   176
wenzelm@5828
   177
wenzelm@5828
   178
(** toplevel transitions **)
wenzelm@5828
   179
wenzelm@5828
   180
exception TERMINATE;
wenzelm@5990
   181
exception RESTART;
wenzelm@7022
   182
exception EXCURSION_FAIL of exn * string;
wenzelm@6689
   183
exception FAILURE of state * exn;
wenzelm@5828
   184
wenzelm@5828
   185
wenzelm@6689
   186
(* recovery from stale signatures *)
wenzelm@6689
   187
wenzelm@7022
   188
(*note: proof commands should be non-destructive!*)
wenzelm@7022
   189
wenzelm@6689
   190
local
wenzelm@6689
   191
wenzelm@6689
   192
fun is_stale state = Sign.is_stale (sign_of state) handle UNDEF => false;
wenzelm@6689
   193
wenzelm@6689
   194
fun checkpoint_node true (Theory thy) = Theory (PureThy.checkpoint thy)
wenzelm@6689
   195
  | checkpoint_node _ node = node;
wenzelm@6689
   196
wenzelm@6689
   197
fun copy_node true (Theory thy) = Theory (Theory.copy thy)
wenzelm@6689
   198
  | copy_node _ node = node;
wenzelm@6689
   199
wenzelm@6965
   200
fun interruptible f x =
wenzelm@6965
   201
  let val y = ref (None: node History.T option);
wenzelm@12987
   202
  in raise_interrupt (fn () => y := Some (f x)) (); the (! y) end;
wenzelm@6965
   203
wenzelm@7022
   204
val rollback = ERROR_MESSAGE "Stale signature encountered after succesful execution!";
wenzelm@7022
   205
wenzelm@7022
   206
fun return (result, None) = result
wenzelm@7022
   207
  | return (result, Some exn) = raise FAILURE (result, exn);
wenzelm@7022
   208
wenzelm@6689
   209
in
wenzelm@6689
   210
wenzelm@8465
   211
fun node_trans _ _ _ (State None) = raise UNDEF
wenzelm@8465
   212
  | node_trans int hist f (State (Some (node, term))) =
wenzelm@6689
   213
      let
wenzelm@8465
   214
        fun mk_state nd = State (Some (nd, term));
wenzelm@6689
   215
wenzelm@6689
   216
        val cont_node = History.map (checkpoint_node int) node;
wenzelm@6689
   217
        val back_node = History.map (copy_node int) cont_node;
wenzelm@6689
   218
wenzelm@6689
   219
        val trans = if hist then History.apply_copy (copy_node int) else History.map;
wenzelm@6965
   220
        val (result, opt_exn) =
wenzelm@6965
   221
          (mk_state (transform_error (interruptible (trans (f int))) cont_node), None)
wenzelm@6965
   222
            handle exn => (mk_state cont_node, Some exn);
wenzelm@6689
   223
      in
wenzelm@7022
   224
        if is_stale result then return (mk_state back_node, Some (if_none opt_exn rollback))
wenzelm@7022
   225
        else return (result, opt_exn)
wenzelm@6689
   226
      end;
wenzelm@6689
   227
wenzelm@6689
   228
fun check_stale state =
wenzelm@6689
   229
  if not (is_stale state) then ()
wenzelm@7022
   230
  else warning "Stale signature encountered!  Should restart current theory.";
wenzelm@6689
   231
wenzelm@6689
   232
end;
wenzelm@6689
   233
wenzelm@6689
   234
wenzelm@6689
   235
(* primitive transitions *)
wenzelm@6689
   236
wenzelm@6689
   237
(*Important note: recovery from stale signatures is provided only for
wenzelm@6689
   238
  theory-level operations via MapCurrent and AppCurrent.  Other node
wenzelm@6965
   239
  or state operations should not touch signatures at all.
wenzelm@6965
   240
wenzelm@6965
   241
  Also note that interrupts are enabled for Keep, MapCurrent, and
wenzelm@6965
   242
  AppCurrent only.*)
wenzelm@5828
   243
wenzelm@5828
   244
datatype trans =
wenzelm@6689
   245
  Reset |                                       (*empty toplevel*)
wenzelm@8465
   246
  Init of (bool -> node) * ((node -> unit) * (node -> unit)) |
wenzelm@8465
   247
    (*init node; provide exit/kill operation*)
wenzelm@8465
   248
  Exit |                                        (*conclude node*)
wenzelm@6689
   249
  Kill |                                        (*abort node*)
wenzelm@7612
   250
  Keep of bool -> state -> unit |               (*peek at state*)
wenzelm@6689
   251
  History of node History.T -> node History.T | (*history operation (undo etc.)*)
wenzelm@6689
   252
  MapCurrent of bool -> node -> node |          (*change node, bypassing history*)
wenzelm@6689
   253
  AppCurrent of bool -> node -> node;           (*change node, recording history*)
wenzelm@6689
   254
wenzelm@6689
   255
fun undo_limit int = if int then None else Some 0;
wenzelm@6689
   256
wenzelm@6689
   257
local
wenzelm@5828
   258
wenzelm@6689
   259
fun apply_tr _ Reset _ = toplevel
wenzelm@8465
   260
  | apply_tr int (Init (f, term)) (State None) =
wenzelm@8465
   261
      State (Some (History.init (undo_limit int) (f int), term))
wenzelm@8465
   262
  | apply_tr _ (Init _ ) (State (Some _)) = raise UNDEF
wenzelm@8465
   263
  | apply_tr _ Exit (State None) = raise UNDEF
wenzelm@8465
   264
  | apply_tr _ Exit (State (Some (node, (exit, _)))) =
wenzelm@8465
   265
      (exit (History.current node); State None)
wenzelm@8465
   266
  | apply_tr _ Kill (State None) = raise UNDEF
wenzelm@8465
   267
  | apply_tr _ Kill (State (Some (node, (_, kill)))) =
wenzelm@8465
   268
      (kill (History.current node); State None)
wenzelm@12987
   269
  | apply_tr int (Keep f) state = (raise_interrupt (f int) state; state)
wenzelm@8465
   270
  | apply_tr _ (History _) (State None) = raise UNDEF
wenzelm@8465
   271
  | apply_tr _ (History f) (State (Some (node, term))) = State (Some (f node, term))
wenzelm@6689
   272
  | apply_tr int (MapCurrent f) state = node_trans int false f state
wenzelm@6689
   273
  | apply_tr int (AppCurrent f) state = node_trans int true f state;
wenzelm@5828
   274
wenzelm@6689
   275
fun apply_union _ [] state = raise FAILURE (state, UNDEF)
wenzelm@6689
   276
  | apply_union int (tr :: trs) state =
wenzelm@6689
   277
      transform_error (apply_tr int tr) state
wenzelm@6689
   278
        handle UNDEF => apply_union int trs state
wenzelm@6689
   279
          | FAILURE (alt_state, UNDEF) => apply_union int trs alt_state
wenzelm@6689
   280
          | exn as FAILURE _ => raise exn
wenzelm@6689
   281
          | exn => raise FAILURE (state, exn);
wenzelm@6689
   282
wenzelm@6689
   283
in
wenzelm@6689
   284
wenzelm@6689
   285
fun apply_trans int trs state = (apply_union int trs state, None)
wenzelm@6689
   286
  handle FAILURE (alt_state, exn) => (alt_state, Some exn) | exn => (state, Some exn);
wenzelm@6689
   287
wenzelm@6689
   288
end;
wenzelm@5828
   289
wenzelm@5828
   290
wenzelm@5828
   291
(* datatype transition *)
wenzelm@5828
   292
wenzelm@5828
   293
datatype transition = Transition of
wenzelm@5828
   294
 {name: string,
wenzelm@5828
   295
  pos: Position.T,
wenzelm@14923
   296
  source: OuterLex.token list option,
wenzelm@5828
   297
  int_only: bool,
wenzelm@5828
   298
  print: bool,
wenzelm@9010
   299
  no_timing: bool,
wenzelm@5828
   300
  trans: trans list};
wenzelm@5828
   301
wenzelm@14923
   302
fun make_transition (name, pos, source, int_only, print, no_timing, trans) =
wenzelm@14923
   303
  Transition {name = name, pos = pos, source = source,
wenzelm@14923
   304
    int_only = int_only, print = print, no_timing = no_timing, trans = trans};
wenzelm@5828
   305
wenzelm@14923
   306
fun map_transition f (Transition {name, pos, source, int_only, print, no_timing, trans}) =
wenzelm@14923
   307
  make_transition (f (name, pos, source, int_only, print, no_timing, trans));
wenzelm@5828
   308
wenzelm@14923
   309
val empty = make_transition ("<unknown>", Position.none, None, false, false, false, []);
wenzelm@14923
   310
wenzelm@14923
   311
fun name_of (Transition {name, ...}) = name;
wenzelm@14923
   312
fun source_of (Transition {source, ...}) = source;
wenzelm@5828
   313
wenzelm@5828
   314
wenzelm@5828
   315
(* diagnostics *)
wenzelm@5828
   316
wenzelm@5828
   317
fun str_of_transition (Transition {name, pos, ...}) = quote name ^ Position.str_of pos;
wenzelm@5828
   318
wenzelm@5828
   319
fun command_msg msg tr = msg ^ "command " ^ str_of_transition tr;
wenzelm@5828
   320
fun at_command tr = command_msg "At " tr ^ ".";
wenzelm@5828
   321
wenzelm@5828
   322
fun type_error tr state =
wenzelm@6689
   323
  ERROR_MESSAGE (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@14923
   328
fun name nm = map_transition (fn (_, pos, source, int_only, print, no_timing, trans) =>
wenzelm@14923
   329
  (nm, pos, source, int_only, print, no_timing, trans));
wenzelm@5828
   330
wenzelm@14923
   331
fun position pos = map_transition (fn (name, _, source, int_only, print, no_timing, trans) =>
wenzelm@14923
   332
  (name, pos, source, int_only, print, no_timing, trans));
wenzelm@9010
   333
wenzelm@14923
   334
fun source src = map_transition (fn (name, pos, _, int_only, print, no_timing, trans) =>
wenzelm@14923
   335
  (name, pos, Some src, int_only, print, no_timing, trans));
wenzelm@5828
   336
wenzelm@14923
   337
fun interactive int_only = map_transition (fn (name, pos, source, _, print, no_timing, trans) =>
wenzelm@14923
   338
  (name, pos, source, int_only, print, no_timing, trans));
wenzelm@14923
   339
wenzelm@14923
   340
val print = map_transition (fn (name, pos, source, int_only, _, no_timing, trans) =>
wenzelm@14923
   341
  (name, pos, source, int_only, true, no_timing, trans));
wenzelm@5828
   342
wenzelm@14923
   343
val no_timing = map_transition (fn (name, pos, source, int_only, print, _, trans) =>
wenzelm@14923
   344
  (name, pos, source, int_only, print, true, trans));
wenzelm@5828
   345
wenzelm@14923
   346
fun add_trans tr = map_transition (fn (name, pos, source, int_only, print, no_timing, trans) =>
wenzelm@14923
   347
  (name, pos, source, int_only, print, no_timing, trans @ [tr]));
wenzelm@5828
   348
wenzelm@5828
   349
wenzelm@5828
   350
(* build transitions *)
wenzelm@5828
   351
wenzelm@5828
   352
val reset = add_trans Reset;
wenzelm@6689
   353
fun init f exit kill = add_trans (Init (f, (exit, kill)));
wenzelm@6689
   354
val exit = add_trans Exit;
wenzelm@6689
   355
val kill = add_trans Kill;
wenzelm@7612
   356
val keep' = add_trans o Keep;
wenzelm@6689
   357
val history = add_trans o History;
wenzelm@5828
   358
val map_current = add_trans o MapCurrent;
wenzelm@6689
   359
val app_current = add_trans o AppCurrent;
wenzelm@5828
   360
wenzelm@7612
   361
fun keep f = add_trans (Keep (fn _ => f));
wenzelm@5828
   362
fun imperative f = keep (fn _ => f ());
wenzelm@5828
   363
wenzelm@6689
   364
fun init_theory f exit kill =
wenzelm@8465
   365
  init (fn int => Theory (f int))
wenzelm@6689
   366
    (fn Theory thy => exit thy | _ => raise UNDEF)
wenzelm@6689
   367
    (fn Theory thy => kill thy | _ => raise UNDEF);
wenzelm@5828
   368
berghofe@15237
   369
(*
berghofe@15237
   370
The skip_proofs flag allows proof scripts to be skipped during interactive
berghofe@15237
   371
proof in order to speed up processing of large theories. While in skipping
berghofe@15237
   372
mode, states are represented as SkipProof (h, thy), where h contains a
berghofe@15237
   373
counter for the number of open proof blocks.
berghofe@15237
   374
*)
berghofe@15237
   375
berghofe@15237
   376
val skip_proofs = ref false;
berghofe@15237
   377
wenzelm@6689
   378
fun theory f = app_current (fn _ => (fn Theory thy => Theory (f thy) | _ => raise UNDEF));
wenzelm@7612
   379
fun theory' f = app_current (fn int => (fn Theory thy => Theory (f int thy) | _ => raise UNDEF));
berghofe@15237
   380
fun theory_to_proof f = app_current (fn int =>
berghofe@15237
   381
  (fn Theory thy =>
berghofe@15237
   382
        if !skip_proofs then SkipProof (History.init (undo_limit int) 0,
berghofe@15237
   383
          fst (SkipProof.global_skip_proof int (ProofHistory.current (f int thy))))
berghofe@15237
   384
        else Proof (f int thy)
berghofe@15237
   385
    | _ => raise UNDEF));
berghofe@15237
   386
fun proof''' b f = map_current (fn int => (fn Proof prf => Proof (f int prf)
berghofe@15237
   387
  | SkipProof (h, thy) => if b then SkipProof (History.apply I h, thy) else raise UNDEF
berghofe@15237
   388
  | _ => raise UNDEF));
berghofe@15237
   389
val proof' = proof''' true;
wenzelm@6689
   390
val proof = proof' o K;
berghofe@15237
   391
val proof'' = proof''' false o K;
wenzelm@12315
   392
fun proof_to_theory' f =
berghofe@15237
   393
  map_current (fn int => (fn Proof prf => Theory (f int prf)
berghofe@15237
   394
    | SkipProof (h, thy) => if History.current h = 0 then Theory thy else raise UNDEF
berghofe@15237
   395
    | _ => raise UNDEF));
wenzelm@12315
   396
val proof_to_theory = proof_to_theory' o K;
berghofe@15237
   397
fun skip_proof f = map_current (fn int =>
berghofe@15237
   398
  (fn SkipProof (h, thy) => SkipProof (f h, thy) | _ => raise UNDEF));
berghofe@15237
   399
fun skip_proof_to_theory p = map_current (fn int =>
berghofe@15237
   400
  (fn SkipProof (h, thy) => if p h then Theory thy else raise UNDEF | _ => raise UNDEF));
wenzelm@5828
   401
wenzelm@9512
   402
val unknown_theory = imperative (fn () => warning "Unknown theory context");
wenzelm@9512
   403
val unknown_proof = imperative (fn () => warning "Unknown proof context");
wenzelm@9512
   404
val unknown_context = imperative (fn () => warning "Unknown context");
wenzelm@9512
   405
wenzelm@5828
   406
wenzelm@5828
   407
wenzelm@5828
   408
(** toplevel transactions **)
wenzelm@5828
   409
wenzelm@7198
   410
val quiet = ref false;
wenzelm@5828
   411
wenzelm@5828
   412
wenzelm@5828
   413
(* print exceptions *)
wenzelm@5828
   414
wenzelm@5828
   415
fun raised name = "exception " ^ name ^ " raised";
wenzelm@5828
   416
fun raised_msg name msg = raised name ^ ": " ^ msg;
wenzelm@5828
   417
wenzelm@5828
   418
fun exn_message TERMINATE = "Exit."
wenzelm@5990
   419
  | exn_message RESTART = "Restart."
wenzelm@7022
   420
  | exn_message (EXCURSION_FAIL (exn, msg)) = cat_lines [exn_message exn, msg]
wenzelm@6971
   421
  | exn_message Interrupt = "Interrupt."
wenzelm@6002
   422
  | exn_message ERROR = "ERROR."
wenzelm@5828
   423
  | exn_message (ERROR_MESSAGE msg) = msg
wenzelm@5828
   424
  | exn_message (THEORY (msg, _)) = msg
wenzelm@5828
   425
  | exn_message (ProofContext.CONTEXT (msg, _)) = msg
wenzelm@5828
   426
  | exn_message (Proof.STATE (msg, _)) = msg
wenzelm@5828
   427
  | exn_message (ProofHistory.FAIL msg) = msg
wenzelm@13486
   428
  | exn_message (MetaSimplifier.SIMPROC_FAIL (name, exn)) =
wenzelm@13486
   429
      fail_message "simproc" ((name, Position.none), exn)
wenzelm@5920
   430
  | exn_message (Attrib.ATTRIB_FAIL info) = fail_message "attribute" info
wenzelm@5920
   431
  | exn_message (Method.METHOD_FAIL info) = fail_message "method" info
wenzelm@12881
   432
  | exn_message (Antiquote.ANTIQUOTE_FAIL info) = fail_message "antiquotation" info
wenzelm@5828
   433
  | exn_message (Syntax.AST (msg, _)) = raised_msg "AST" msg
wenzelm@5828
   434
  | exn_message (TYPE (msg, _, _)) = raised_msg "TYPE" msg
wenzelm@5828
   435
  | exn_message (TERM (msg, _)) = raised_msg "TERM" msg
wenzelm@5828
   436
  | exn_message (THM (msg, _, _)) = raised_msg "THM" msg
wenzelm@5828
   437
  | exn_message Library.OPTION = raised "Library.OPTION"
wenzelm@5828
   438
  | exn_message (Library.LIST msg) = raised_msg "Library.LIST" msg
wenzelm@5920
   439
  | exn_message exn = General.exnMessage exn
wenzelm@5920
   440
and fail_message kind ((name, pos), exn) =
wenzelm@9152
   441
  "Error in " ^ kind ^ " " ^ quote name ^ Position.str_of pos ^ ":\n" ^ exn_message exn;
wenzelm@5828
   442
wenzelm@5828
   443
fun print_exn None = ()
wenzelm@5828
   444
  | print_exn (Some (exn, s)) = error_msg (cat_lines [exn_message exn, s]);
wenzelm@5828
   445
wenzelm@5828
   446
wenzelm@5828
   447
(* apply transitions *)
wenzelm@5828
   448
wenzelm@6664
   449
local
wenzelm@6664
   450
wenzelm@9010
   451
fun app int (tr as Transition {trans, int_only, print, no_timing, ...}) state =
wenzelm@5828
   452
  let
wenzelm@5828
   453
    val _ =
wenzelm@5828
   454
      if int orelse not int_only then ()
wenzelm@8722
   455
      else warning (command_msg "Interactive-only " tr);
wenzelm@6689
   456
    val (result, opt_exn) =
wenzelm@14825
   457
      (if ! Output.timing andalso not no_timing then (warning (command_msg "" tr); timeap) else I)
wenzelm@9010
   458
        (apply_trans int trans) state;
wenzelm@8886
   459
    val _ = if int andalso print andalso not (! quiet) then print_state false result else ();
wenzelm@6689
   460
  in (result, apsome (fn UNDEF => type_error tr state | exn => exn) opt_exn) end;
wenzelm@6664
   461
wenzelm@6664
   462
in
wenzelm@5828
   463
wenzelm@6689
   464
fun apply int tr st =
wenzelm@6965
   465
  (case app int tr st of
wenzelm@6689
   466
    (_, Some TERMINATE) => None
wenzelm@6664
   467
  | (_, Some RESTART) => Some (toplevel, None)
wenzelm@7022
   468
  | (state', Some (EXCURSION_FAIL exn_info)) => Some (state', Some exn_info)
wenzelm@6689
   469
  | (state', Some exn) => Some (state', Some (exn, at_command tr))
wenzelm@6689
   470
  | (state', None) => Some (state', None));
wenzelm@6664
   471
wenzelm@6664
   472
end;
wenzelm@5828
   473
wenzelm@5828
   474
wenzelm@5828
   475
(* excursion: toplevel -- apply transformers -- toplevel *)
wenzelm@5828
   476
wenzelm@6664
   477
local
wenzelm@6664
   478
wenzelm@5828
   479
fun excur [] x = x
wenzelm@9134
   480
  | excur ((tr, f) :: trs) (st, res) =
wenzelm@9134
   481
      (case apply false tr st of
wenzelm@9134
   482
        Some (st', None) =>
wenzelm@9154
   483
          excur trs (st', transform_error (fn () => f st' res) () handle exn =>
wenzelm@10324
   484
            raise EXCURSION_FAIL (exn, "Presentation failed\n" ^ at_command tr))
wenzelm@9134
   485
      | Some (st', Some exn_info) => raise EXCURSION_FAIL exn_info
wenzelm@7022
   486
      | None => raise EXCURSION_FAIL (TERMINATE, at_command tr));
wenzelm@5828
   487
wenzelm@6664
   488
in
wenzelm@6664
   489
wenzelm@9134
   490
fun excursion_result (trs, res) =
wenzelm@9134
   491
  (case excur trs (State None, res) of
wenzelm@9134
   492
    (State None, res') => res'
wenzelm@9134
   493
  | _ => raise ERROR_MESSAGE "Unfinished development at end of input")
wenzelm@9134
   494
  handle exn => error (exn_message exn);
wenzelm@9134
   495
wenzelm@5828
   496
fun excursion trs =
wenzelm@9134
   497
  excursion_result (map (fn tr => (tr, fn _ => fn _ => ())) trs, ());
wenzelm@7062
   498
wenzelm@6664
   499
end;
wenzelm@6664
   500
wenzelm@5828
   501
wenzelm@5828
   502
wenzelm@5828
   503
(** interactive transformations **)
wenzelm@5828
   504
wenzelm@5828
   505
(* the global state reference *)
wenzelm@5828
   506
wenzelm@6689
   507
val global_state = ref (toplevel, None: (exn * string) option);
wenzelm@5828
   508
wenzelm@5828
   509
fun set_state state = global_state := (state, None);
wenzelm@5828
   510
fun get_state () = fst (! global_state);
wenzelm@5828
   511
fun exn () = snd (! global_state);
wenzelm@5828
   512
wenzelm@5828
   513
wenzelm@6965
   514
(* the Isar source of transitions *)
wenzelm@6965
   515
nipkow@14091
   516
type 'a isar =
wenzelm@6965
   517
  (transition, (transition option,
wenzelm@12881
   518
    (OuterLex.token, (OuterLex.token option, (OuterLex.token, (OuterLex.token,
nipkow@14091
   519
      Position.T * (Symbol.symbol, (string, 'a) Source.source) Source.source)
wenzelm@12881
   520
          Source.source) Source.source) Source.source) Source.source) Source.source) Source.source;
wenzelm@6965
   521
wenzelm@6965
   522
wenzelm@5828
   523
(* apply transformers to global state *)
wenzelm@5828
   524
wenzelm@14985
   525
nonfix >> >>>;
wenzelm@5828
   526
wenzelm@5828
   527
fun >> tr =
wenzelm@5828
   528
  (case apply true tr (get_state ()) of
wenzelm@5828
   529
    None => false
wenzelm@5828
   530
  | Some (state', exn_info) =>
wenzelm@5828
   531
      (global_state := (state', exn_info);
wenzelm@5828
   532
        check_stale state'; print_exn exn_info;
wenzelm@5828
   533
        true));
wenzelm@5828
   534
wenzelm@14985
   535
fun >>> [] = ()
wenzelm@14985
   536
  | >>> (tr :: trs) = if >> tr then >>> trs else ();
wenzelm@14985
   537
wenzelm@7602
   538
(*Note: this is for Poly/ML only, we really do not intend to exhibit
wenzelm@7602
   539
  interrupts here*)
wenzelm@7602
   540
fun get_interrupt src = Some (Source.get_single src) handle Interrupt => None;
wenzelm@7602
   541
wenzelm@5828
   542
fun raw_loop src =
wenzelm@7602
   543
  (case get_interrupt (Source.set_prompt (prompt_state (get_state ())) src) of
wenzelm@6971
   544
    None => (writeln "\nInterrupt."; raw_loop src)
wenzelm@5828
   545
  | Some None => ()
wenzelm@5828
   546
  | Some (Some (tr, src')) => if >> tr then raw_loop src' else ());
wenzelm@5828
   547
wenzelm@5828
   548
wenzelm@12987
   549
fun loop src = ignore_interrupt raw_loop src;
wenzelm@5828
   550
wenzelm@5828
   551
wenzelm@5828
   552
end;