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