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