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