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