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