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