src/Pure/Isar/toplevel.ML
author wenzelm
Wed Nov 18 10:56:53 1998 +0100 (1998-11-18 ago)
changeset 5922 85d62ecb950d
parent 5920 d7e35f45b72c
child 5930 41aa67a045f7
permissions -rw-r--r--
export exn_message;
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
TODO:
wenzelm@5828
     8
  - cleanup this file:
wenzelm@5828
     9
      . more internal locality;
wenzelm@5828
    10
      . clearer structure of control flow (exceptions, interrupts);
wenzelm@5828
    11
      . clear division with outer_syntax.ML (!?);
wenzelm@5828
    12
  - improve transactions; only in interactive mode!
wenzelm@5828
    13
  - init / exit proof;
wenzelm@5828
    14
  - display stack size in prompt;
wenzelm@5828
    15
*)
wenzelm@5828
    16
wenzelm@5828
    17
signature TOPLEVEL =
wenzelm@5828
    18
sig
wenzelm@5828
    19
  datatype node = Theory of theory | Proof of ProofHistory.T
wenzelm@5828
    20
  type state
wenzelm@5828
    21
  exception UNDEF
wenzelm@5828
    22
  val toplevel: state
wenzelm@5828
    23
  val print_state: state -> unit
wenzelm@5828
    24
  val node_of: state -> node
wenzelm@5828
    25
  val node_cases: (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@5828
    28
  val proof_of: state -> Proof.state
wenzelm@5828
    29
  type transition
wenzelm@5828
    30
  exception TERMINATE
wenzelm@5920
    31
  exception BREAK of state
wenzelm@5828
    32
  val empty: transition
wenzelm@5828
    33
  val name: string -> transition -> transition
wenzelm@5828
    34
  val position: Position.T -> transition -> transition
wenzelm@5828
    35
  val interactive: bool -> transition -> transition
wenzelm@5828
    36
  val print: transition -> transition
wenzelm@5828
    37
  val reset: transition -> transition
wenzelm@5828
    38
  val init: (state -> node) -> (node -> unit) -> transition -> transition
wenzelm@5828
    39
  val keep: (state -> unit) -> transition -> transition
wenzelm@5828
    40
  val exit: transition -> transition
wenzelm@5828
    41
  val imperative: (unit -> unit) -> transition -> transition
wenzelm@5828
    42
  val init_theory: (unit -> theory) -> (theory -> unit) -> transition -> transition
wenzelm@5828
    43
(* FIXME
wenzelm@5828
    44
  val init_proof: (theory -> ProofHistory.T) -> transition -> transition
wenzelm@5828
    45
  val exit_proof: (ProofHistory.T -> unit) -> transition -> transition
wenzelm@5828
    46
*)
wenzelm@5828
    47
  val theory: (theory -> theory) -> transition -> transition
wenzelm@5828
    48
  val proof: (ProofHistory.T -> ProofHistory.T) -> transition -> transition
wenzelm@5828
    49
  val theory_to_proof: (theory -> ProofHistory.T) -> transition -> transition
wenzelm@5828
    50
  val proof_to_theory: (ProofHistory.T -> theory) -> transition -> transition
wenzelm@5828
    51
  type isar
wenzelm@5828
    52
  val trace: bool ref
wenzelm@5922
    53
  val exn_message: exn -> string
wenzelm@5828
    54
  val apply: bool -> transition -> state -> (state * (exn * string) option) option
wenzelm@5828
    55
  val excursion: transition list -> unit
wenzelm@5828
    56
  val set_state: state -> unit
wenzelm@5828
    57
  val get_state: unit -> state
wenzelm@5828
    58
  val exn: unit -> (exn * string) option
wenzelm@5828
    59
  val >> : transition -> bool
wenzelm@5828
    60
  val loop: isar -> unit
wenzelm@5828
    61
end;
wenzelm@5828
    62
wenzelm@5828
    63
structure Toplevel: TOPLEVEL =
wenzelm@5828
    64
struct
wenzelm@5828
    65
wenzelm@5828
    66
wenzelm@5828
    67
(** toplevel state **)
wenzelm@5828
    68
wenzelm@5828
    69
(* datatype node *)
wenzelm@5828
    70
wenzelm@5828
    71
datatype node =
wenzelm@5828
    72
  Theory of theory |
wenzelm@5828
    73
  Proof of ProofHistory.T;
wenzelm@5828
    74
wenzelm@5828
    75
fun print_node (Theory thy) = Pretty.writeln (Pretty.block
wenzelm@5828
    76
      [Pretty.str "Theory:", Pretty.brk 1, Pretty.str (PureThy.get_name thy),
wenzelm@5828
    77
        Pretty.str " =", Pretty.brk 1, Display.pretty_theory thy])
wenzelm@5828
    78
  | print_node (Proof prf) =
wenzelm@5828
    79
      (writeln "Proof:"; writeln ("Step #" ^ string_of_int (ProofHistory.position prf));
wenzelm@5828
    80
        Proof.print_state (ProofHistory.current prf); writeln "Proof.");
wenzelm@5828
    81
wenzelm@5828
    82
wenzelm@5828
    83
(* datatype state *)
wenzelm@5828
    84
wenzelm@5828
    85
datatype state = State of (node * (node -> unit)) list;
wenzelm@5828
    86
wenzelm@5828
    87
exception UNDEF;
wenzelm@5828
    88
wenzelm@5828
    89
val toplevel = State [];
wenzelm@5828
    90
wenzelm@5828
    91
fun print_state (State []) = ()
wenzelm@5828
    92
  | print_state (State [(node, _)]) = print_node node
wenzelm@5828
    93
  | print_state (State ((node, _) :: nodes)) =
wenzelm@5828
    94
      (writeln ("Stack size: " ^ string_of_int (length nodes)); print_node node);
wenzelm@5828
    95
wenzelm@5828
    96
fun str_of_state (State []) = "at top level"
wenzelm@5828
    97
  | str_of_state (State ((Theory _, _) :: _)) = "in theory mode"
wenzelm@5828
    98
  | str_of_state (State ((Proof _, _) :: _)) = "in proof mode";
wenzelm@5828
    99
wenzelm@5828
   100
wenzelm@5828
   101
(* top node *)
wenzelm@5828
   102
wenzelm@5828
   103
fun node_of (State []) = raise UNDEF
wenzelm@5828
   104
  | node_of (State ((node, _) :: _)) = node;
wenzelm@5828
   105
wenzelm@5828
   106
fun node_cases f g state =
wenzelm@5828
   107
  (case node_of state of
wenzelm@5828
   108
    Theory thy => f thy
wenzelm@5828
   109
  | Proof prf => g (ProofHistory.current prf));
wenzelm@5828
   110
wenzelm@5828
   111
val theory_of = node_cases I Proof.theory_of;
wenzelm@5828
   112
val sign_of = Theory.sign_of o theory_of;
wenzelm@5828
   113
val proof_of = node_cases (fn _ => raise UNDEF) I;
wenzelm@5828
   114
wenzelm@5828
   115
wenzelm@5828
   116
wenzelm@5828
   117
(** toplevel transitions **)
wenzelm@5828
   118
wenzelm@5828
   119
exception TERMINATE;
wenzelm@5920
   120
exception BREAK of state;
wenzelm@5828
   121
exception FAIL of exn * string;
wenzelm@5828
   122
wenzelm@5828
   123
wenzelm@5828
   124
(* datatype trans *)
wenzelm@5828
   125
wenzelm@5828
   126
datatype trans =
wenzelm@5828
   127
  Reset |
wenzelm@5828
   128
  Init of (state -> node) * (node -> unit) |
wenzelm@5828
   129
  Keep of (state -> unit) |
wenzelm@5828
   130
  Exit |
wenzelm@5828
   131
  MapCurrent of node -> node;
wenzelm@5828
   132
wenzelm@5828
   133
fun apply_trans Reset _ = State []
wenzelm@5828
   134
  | apply_trans (Init (f, g)) (state as State nodes) = State ((f state, g) :: nodes)
wenzelm@5828
   135
  | apply_trans (Keep f) state = (f state; state)
wenzelm@5828
   136
  | apply_trans Exit (State []) = raise UNDEF
wenzelm@5828
   137
  | apply_trans Exit (State ((node, g) :: nodes)) = (g node; State nodes)
wenzelm@5828
   138
  | apply_trans (MapCurrent _) (State []) = raise UNDEF
wenzelm@5828
   139
  | apply_trans (MapCurrent f) (State ((node, g) :: nodes)) = State ((f node, g) :: nodes);
wenzelm@5828
   140
wenzelm@5828
   141
fun apply_union [] _ = raise UNDEF
wenzelm@5828
   142
  | apply_union (tr :: trs) state =
wenzelm@5828
   143
      apply_trans tr state handle UNDEF => apply_union trs state;
wenzelm@5828
   144
wenzelm@5828
   145
wenzelm@5828
   146
(* datatype transition *)
wenzelm@5828
   147
wenzelm@5828
   148
datatype transition = Transition of
wenzelm@5828
   149
 {name: string,
wenzelm@5828
   150
  pos: Position.T,
wenzelm@5828
   151
  int_only: bool,
wenzelm@5828
   152
  print: bool,
wenzelm@5828
   153
  trans: trans list};
wenzelm@5828
   154
wenzelm@5828
   155
fun make_transition (name, pos, int_only, print, trans) =
wenzelm@5828
   156
  Transition {name = name, pos = pos, int_only = int_only, print = print, trans = trans};
wenzelm@5828
   157
wenzelm@5828
   158
fun map_transition f (Transition {name, pos, int_only, print, trans}) =
wenzelm@5828
   159
  make_transition (f (name, pos, int_only, print, trans));
wenzelm@5828
   160
wenzelm@5828
   161
val empty = make_transition ("<unknown>", Position.none, false, false, []);
wenzelm@5828
   162
wenzelm@5828
   163
wenzelm@5828
   164
(* diagnostics *)
wenzelm@5828
   165
wenzelm@5828
   166
fun str_of_transition (Transition {name, pos, ...}) = quote name ^ Position.str_of pos;
wenzelm@5828
   167
wenzelm@5828
   168
fun command_msg msg tr = msg ^ "command " ^ str_of_transition tr;
wenzelm@5828
   169
fun at_command tr = command_msg "At " tr ^ ".";
wenzelm@5828
   170
wenzelm@5828
   171
fun type_error tr state =
wenzelm@5828
   172
  error (command_msg "Illegal application of " tr ^ " " ^ str_of_state state);
wenzelm@5828
   173
wenzelm@5828
   174
wenzelm@5828
   175
(* modify transitions *)
wenzelm@5828
   176
wenzelm@5828
   177
fun name nm = map_transition
wenzelm@5828
   178
  (fn (_, pos, int_only, print, trans) => (nm, pos, int_only, print, trans));
wenzelm@5828
   179
wenzelm@5828
   180
fun position pos = map_transition
wenzelm@5828
   181
  (fn (name, _, int_only, print, trans) => (name, pos, int_only, print, trans));
wenzelm@5828
   182
wenzelm@5828
   183
fun interactive int_only = map_transition
wenzelm@5828
   184
  (fn (name, pos, _, print, trans) => (name, pos, int_only, print, trans));
wenzelm@5828
   185
wenzelm@5828
   186
val print = map_transition
wenzelm@5828
   187
  (fn (name, pos, int_only, _, trans) => (name, pos, int_only, true, trans));
wenzelm@5828
   188
wenzelm@5828
   189
fun add_trans tr = map_transition
wenzelm@5828
   190
  (fn (name, pos, int_only, print, trans) => (name, pos, int_only, print, trans @ [tr]));
wenzelm@5828
   191
wenzelm@5828
   192
wenzelm@5828
   193
(* build transitions *)
wenzelm@5828
   194
wenzelm@5828
   195
val reset = add_trans Reset;
wenzelm@5828
   196
fun init f g = add_trans (Init (f, g));
wenzelm@5828
   197
val keep = add_trans o Keep;
wenzelm@5828
   198
val exit = add_trans Exit;
wenzelm@5828
   199
val map_current = add_trans o MapCurrent;
wenzelm@5828
   200
wenzelm@5828
   201
fun imperative f = keep (fn _ => f ());
wenzelm@5828
   202
wenzelm@5828
   203
fun init_theory f g =
wenzelm@5828
   204
  init (fn _ => Theory (f ())) (fn Theory thy => g thy | _ => raise UNDEF);
wenzelm@5828
   205
wenzelm@5828
   206
fun theory f = map_current
wenzelm@5828
   207
  (fn Theory thy => Theory (PureThy.transaction f thy) | _ => raise UNDEF);
wenzelm@5828
   208
fun proof f = map_current (fn Proof prf => Proof (f prf) | _ => raise UNDEF);
wenzelm@5828
   209
wenzelm@5828
   210
fun theory_to_proof f = map_current (fn Theory thy => Proof (f thy) | _ => raise UNDEF);
wenzelm@5828
   211
fun proof_to_theory f = map_current (fn Proof prf => Theory (f prf) | _ => raise UNDEF);
wenzelm@5828
   212
wenzelm@5828
   213
wenzelm@5828
   214
(* the Isar source of transitions *)
wenzelm@5828
   215
wenzelm@5828
   216
type isar =
wenzelm@5828
   217
  (transition, (transition option,
wenzelm@5828
   218
    (OuterLex.token, (OuterLex.token,
wenzelm@5828
   219
      Position.T * (Symbol.symbol, (string, unit) Source.source) Source.source)
wenzelm@5828
   220
          Source.source) Source.source) Source.source) Source.source;
wenzelm@5828
   221
wenzelm@5828
   222
wenzelm@5828
   223
wenzelm@5828
   224
(** toplevel transactions **)
wenzelm@5828
   225
wenzelm@5828
   226
val trace = ref false;
wenzelm@5828
   227
wenzelm@5828
   228
wenzelm@5828
   229
(* print exceptions *)
wenzelm@5828
   230
wenzelm@5828
   231
fun raised name = "exception " ^ name ^ " raised";
wenzelm@5828
   232
fun raised_msg name msg = raised name ^ ": " ^ msg;
wenzelm@5828
   233
wenzelm@5828
   234
fun exn_message TERMINATE = "Exit."
wenzelm@5920
   235
  | exn_message (BREAK _) = "Break."
wenzelm@5920
   236
  | exn_message Interrupt = "Interrupt (exec)."
wenzelm@5828
   237
  | exn_message (ERROR_MESSAGE msg) = msg
wenzelm@5828
   238
  | exn_message (THEORY (msg, _)) = msg
wenzelm@5828
   239
  | exn_message (ProofContext.CONTEXT (msg, _)) = msg
wenzelm@5828
   240
  | exn_message (Proof.STATE (msg, _)) = msg
wenzelm@5828
   241
  | exn_message (ProofHistory.FAIL msg) = msg
wenzelm@5920
   242
  | exn_message (Attrib.ATTRIB_FAIL info) = fail_message "attribute" info
wenzelm@5920
   243
  | exn_message (Method.METHOD_FAIL info) = fail_message "method" info
wenzelm@5828
   244
  | exn_message (Syntax.AST (msg, _)) = raised_msg "AST" msg
wenzelm@5828
   245
  | exn_message (TYPE (msg, _, _)) = raised_msg "TYPE" msg
wenzelm@5828
   246
  | exn_message (TERM (msg, _)) = raised_msg "TERM" msg
wenzelm@5828
   247
  | exn_message (THM (msg, _, _)) = raised_msg "THM" msg
wenzelm@5828
   248
  | exn_message Library.OPTION = raised "Library.OPTION"
wenzelm@5828
   249
  | exn_message (Library.LIST msg) = raised_msg "Library.LIST" msg
wenzelm@5920
   250
  | exn_message exn = General.exnMessage exn
wenzelm@5920
   251
and fail_message kind ((name, pos), exn) =
wenzelm@5920
   252
  "Error in " ^ kind ^ " " ^ name ^ Position.str_of pos ^ ":\n" ^ exn_message exn;
wenzelm@5828
   253
wenzelm@5828
   254
fun print_exn None = ()
wenzelm@5828
   255
  | print_exn (Some (exn, s)) = error_msg (cat_lines [exn_message exn, s]);
wenzelm@5828
   256
wenzelm@5828
   257
wenzelm@5828
   258
(* transform interrupt (non-polymorphic) *)
wenzelm@5828
   259
wenzelm@5828
   260
fun transform_interrupt_state f x =
wenzelm@5828
   261
  let val y = ref (None: state option);
wenzelm@5828
   262
  in exhibit_interrupt (fn () => y := Some (f x)) (); the (! y) end;
wenzelm@5828
   263
wenzelm@5828
   264
fun transform_interrupt_isar f x =
wenzelm@5828
   265
  let val y = ref (None: (transition * isar) option option);
wenzelm@5828
   266
  in exhibit_interrupt (fn () => y := Some (f x)) (); the (! y) end;
wenzelm@5828
   267
wenzelm@5828
   268
wenzelm@5828
   269
(* the dreaded stale signatures *)
wenzelm@5828
   270
wenzelm@5828
   271
fun check_stale state =
wenzelm@5828
   272
  (case try theory_of state of
wenzelm@5828
   273
    None => ()
wenzelm@5828
   274
  | Some thy =>
wenzelm@5828
   275
      if Sign.is_stale (Theory.sign_of thy) then
wenzelm@5828
   276
        warning "Stale signature encountered!  Should redo current theory from start."
wenzelm@5828
   277
      else ());
wenzelm@5828
   278
wenzelm@5828
   279
wenzelm@5828
   280
(* apply transitions *)
wenzelm@5828
   281
wenzelm@5828
   282
fun app int (tr as Transition {trans, int_only, print, ...}) state =
wenzelm@5828
   283
  let
wenzelm@5828
   284
    val _ =
wenzelm@5828
   285
      if int orelse not int_only then ()
wenzelm@5828
   286
      else warning (command_msg "Executing interactive-only " tr);
wenzelm@5828
   287
    val state' =
wenzelm@5828
   288
      (if ! trace then (writeln (command_msg "" tr); timeap (apply_union trans) state)
wenzelm@5828
   289
        else apply_union trans state)
wenzelm@5828
   290
      handle UNDEF => type_error tr state;
wenzelm@5828
   291
    val _ = if int andalso print then print_state state' else ();
wenzelm@5828
   292
  in state' end;
wenzelm@5828
   293
wenzelm@5828
   294
fun rollback tr copy_thy (State ((Theory _, g) :: nodes)) =
wenzelm@5828
   295
      (warning (command_msg "Rollback after " tr); State ((Theory copy_thy, g) :: nodes))
wenzelm@5828
   296
  | rollback tr _ state =
wenzelm@5828
   297
      (warning (command_msg "Ignoring rollback theory copy from " tr); state);
wenzelm@5828
   298
wenzelm@5828
   299
fun apply int tr state =
wenzelm@5828
   300
  Some (transform_interrupt_state (transform_error (app int tr)) state, None)
wenzelm@5828
   301
    handle
wenzelm@5828
   302
      TERMINATE => None
wenzelm@5828
   303
    | FAIL exn_info => Some (state, Some exn_info)
wenzelm@5828
   304
    | PureThy.ROLLBACK (copy_thy, opt_exn) =>
wenzelm@5828
   305
        Some (rollback tr copy_thy state, apsome (fn exn => (exn, at_command tr)) opt_exn)
wenzelm@5920
   306
    | exn as BREAK break_state => Some (break_state, Some (exn, at_command tr))
wenzelm@5828
   307
    | exn => Some (state, Some (exn, at_command tr));
wenzelm@5828
   308
wenzelm@5828
   309
wenzelm@5828
   310
(* excursion: toplevel -- apply transformers -- toplevel *)
wenzelm@5828
   311
wenzelm@5828
   312
fun excur [] x = x
wenzelm@5828
   313
  | excur (tr :: trs) x =
wenzelm@5828
   314
      (case apply false tr x of
wenzelm@5828
   315
        Some (x', None) => excur trs x'
wenzelm@5828
   316
      | Some (x', Some exn_info) => raise FAIL exn_info
wenzelm@5828
   317
      | None => raise FAIL (TERMINATE, at_command tr));
wenzelm@5828
   318
wenzelm@5828
   319
fun excursion trs =
wenzelm@5828
   320
  (case excur trs (State []) of
wenzelm@5828
   321
    State [] => ()
wenzelm@5828
   322
  | _ => error "Pending blocks at end of excursion");
wenzelm@5828
   323
wenzelm@5828
   324
wenzelm@5828
   325
wenzelm@5828
   326
(** interactive transformations **)
wenzelm@5828
   327
wenzelm@5828
   328
(* the global state reference *)
wenzelm@5828
   329
wenzelm@5828
   330
val global_state = ref (State [], None: (exn * string) option);
wenzelm@5828
   331
wenzelm@5828
   332
fun set_state state = global_state := (state, None);
wenzelm@5828
   333
fun get_state () = fst (! global_state);
wenzelm@5828
   334
fun exn () = snd (! global_state);
wenzelm@5828
   335
wenzelm@5828
   336
wenzelm@5828
   337
(* apply transformers to global state *)
wenzelm@5828
   338
wenzelm@5828
   339
nonfix >>;
wenzelm@5828
   340
wenzelm@5828
   341
fun >> tr =
wenzelm@5828
   342
  (case apply true tr (get_state ()) of
wenzelm@5828
   343
    None => false
wenzelm@5828
   344
  | Some (state', exn_info) =>
wenzelm@5828
   345
      (global_state := (state', exn_info);
wenzelm@5828
   346
        check_stale state'; print_exn exn_info;
wenzelm@5828
   347
        true));
wenzelm@5828
   348
wenzelm@5828
   349
fun get_interruptible src =
wenzelm@5828
   350
  Some (transform_interrupt_isar Source.get_single src)
wenzelm@5828
   351
    handle Interrupt => None;
wenzelm@5828
   352
wenzelm@5828
   353
fun raw_loop src =
wenzelm@5828
   354
  (case get_interruptible src of
wenzelm@5920
   355
    None => (writeln "\nInterrupt (read)."; raw_loop src)
wenzelm@5828
   356
  | Some None => ()
wenzelm@5828
   357
  | Some (Some (tr, src')) => if >> tr then raw_loop src' else ());
wenzelm@5828
   358
wenzelm@5828
   359
wenzelm@5828
   360
fun loop src = mask_interrupt raw_loop src;
wenzelm@5828
   361
wenzelm@5828
   362
wenzelm@5828
   363
end;