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