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