author | wenzelm |
Fri, 16 Jul 1999 22:22:02 +0200 | |
changeset 7021 | 0073aa571502 |
parent 6971 | 4a13e098ee86 |
child 7022 | abf9d5e2fb6e |
permissions | -rw-r--r-- |
5828 | 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 |
val toplevel: state |
|
6689 | 13 |
val prompt_state_default: state -> string |
14 |
val prompt_state_fn: (state -> string) ref |
|
5946
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
15 |
val print_state_default: state -> unit |
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
16 |
val print_state_fn: (state -> unit) ref |
5828 | 17 |
val print_state: state -> unit |
6689 | 18 |
exception UNDEF |
19 |
val node_history_of: state -> node History.T |
|
5828 | 20 |
val node_of: state -> node |
6664 | 21 |
val node_case: (theory -> 'a) -> (Proof.state -> 'a) -> state -> 'a |
5828 | 22 |
val theory_of: state -> theory |
23 |
val sign_of: state -> Sign.sg |
|
24 |
val proof_of: state -> Proof.state |
|
25 |
type transition |
|
26 |
exception TERMINATE |
|
5990 | 27 |
exception RESTART |
5920 | 28 |
exception BREAK of state |
6689 | 29 |
val undo_limit: bool -> int option |
5828 | 30 |
val empty: transition |
31 |
val name: string -> transition -> transition |
|
32 |
val position: Position.T -> transition -> transition |
|
33 |
val interactive: bool -> transition -> transition |
|
34 |
val print: transition -> transition |
|
35 |
val reset: transition -> transition |
|
6689 | 36 |
val init: (state -> node) -> (node -> unit) -> (node -> unit) -> transition -> transition |
37 |
val exit: transition -> transition |
|
38 |
val kill: transition -> transition |
|
5828 | 39 |
val keep: (state -> unit) -> transition -> transition |
6689 | 40 |
val history: (node History.T -> node History.T) -> transition -> transition |
5828 | 41 |
val imperative: (unit -> unit) -> transition -> transition |
6689 | 42 |
val init_theory: (unit -> theory) -> (theory -> unit) -> (theory -> unit) |
43 |
-> transition -> transition |
|
5828 | 44 |
val theory: (theory -> theory) -> transition -> transition |
6689 | 45 |
val theory_to_proof: (bool -> theory -> ProofHistory.T) -> transition -> transition |
5828 | 46 |
val proof: (ProofHistory.T -> ProofHistory.T) -> transition -> transition |
6689 | 47 |
val proof': (bool -> ProofHistory.T -> ProofHistory.T) -> transition -> transition |
5828 | 48 |
val proof_to_theory: (ProofHistory.T -> theory) -> transition -> transition |
49 |
val trace: bool ref |
|
5922 | 50 |
val exn_message: exn -> string |
5828 | 51 |
val apply: bool -> transition -> state -> (state * (exn * string) option) option |
52 |
val excursion: transition list -> unit |
|
53 |
val set_state: state -> unit |
|
54 |
val get_state: unit -> state |
|
55 |
val exn: unit -> (exn * string) option |
|
56 |
val >> : transition -> bool |
|
6965 | 57 |
type isar |
5828 | 58 |
val loop: isar -> unit |
59 |
end; |
|
60 |
||
6965 | 61 |
structure Toplevel: TOPLEVEL = |
5828 | 62 |
struct |
63 |
||
64 |
||
65 |
(** toplevel state **) |
|
66 |
||
67 |
(* datatype node *) |
|
68 |
||
69 |
datatype node = |
|
70 |
Theory of theory | |
|
71 |
Proof of ProofHistory.T; |
|
72 |
||
6689 | 73 |
fun str_of_node (Theory _) = "in theory mode" |
74 |
| str_of_node (Proof _) = "in proof mode"; |
|
75 |
||
5828 | 76 |
fun print_node (Theory thy) = Pretty.writeln (Pretty.block |
77 |
[Pretty.str "Theory:", Pretty.brk 1, Pretty.str (PureThy.get_name thy), |
|
78 |
Pretty.str " =", Pretty.brk 1, Display.pretty_theory thy]) |
|
79 |
| print_node (Proof prf) = |
|
5946
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
80 |
(writeln ("Proof: step #" ^ string_of_int (ProofHistory.position prf)); |
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
81 |
Proof.print_state (ProofHistory.current prf)); |
5828 | 82 |
|
83 |
||
84 |
(* datatype state *) |
|
85 |
||
6689 | 86 |
datatype state = State of (node History.T * ((node -> unit) * (node -> unit))) list; |
5828 | 87 |
|
88 |
val toplevel = State []; |
|
5939 | 89 |
fun append_states (State ns) (State ms) = State (ns @ ms); |
5828 | 90 |
|
91 |
fun str_of_state (State []) = "at top level" |
|
6689 | 92 |
| str_of_state (State ((node, _) :: _)) = str_of_node (History.current node); |
93 |
||
94 |
||
95 |
(* prompt_state hook *) |
|
96 |
||
97 |
fun prompt_state_default (State nodes) = |
|
98 |
let val len = length nodes |
|
99 |
in (if len < 2 then "" else string_of_int len) ^ Source.default_prompt end; |
|
100 |
||
101 |
val prompt_state_fn = ref prompt_state_default; |
|
102 |
fun prompt_state state = ! prompt_state_fn state; |
|
5828 | 103 |
|
104 |
||
5946
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
105 |
(* print_state hook *) |
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
106 |
|
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
107 |
fun print_topnode (State []) = () |
6689 | 108 |
| print_topnode (State ((node, _) :: _)) = print_node (History.current node); |
5946
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
109 |
|
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
110 |
fun print_state_default state = |
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
111 |
let val ref (begin_state, end_state, _) = Goals.current_goals_markers in |
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
112 |
if begin_state = "" then () else writeln begin_state; |
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
113 |
print_topnode state; |
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
114 |
if end_state = "" then () else writeln end_state |
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
115 |
end; |
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
116 |
|
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
117 |
val print_state_fn = ref print_state_default; |
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
118 |
fun print_state state = ! print_state_fn state; |
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
119 |
|
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
120 |
|
5828 | 121 |
(* top node *) |
122 |
||
6689 | 123 |
exception UNDEF; |
124 |
||
125 |
fun node_history_of (State []) = raise UNDEF |
|
126 |
| node_history_of (State ((node, _) :: _)) = node; |
|
127 |
||
128 |
val node_of = History.current o node_history_of; |
|
5828 | 129 |
|
6664 | 130 |
fun node_case f g state = |
5828 | 131 |
(case node_of state of |
132 |
Theory thy => f thy |
|
133 |
| Proof prf => g (ProofHistory.current prf)); |
|
134 |
||
6664 | 135 |
val theory_of = node_case I Proof.theory_of; |
5828 | 136 |
val sign_of = Theory.sign_of o theory_of; |
6664 | 137 |
val proof_of = node_case (fn _ => raise UNDEF) I; |
138 |
||
5828 | 139 |
|
140 |
||
141 |
(** toplevel transitions **) |
|
142 |
||
143 |
exception TERMINATE; |
|
5990 | 144 |
exception RESTART; |
5920 | 145 |
exception BREAK of state; |
5828 | 146 |
exception FAIL of exn * string; |
6689 | 147 |
exception ROLLBACK of state * exn option; |
148 |
exception FAILURE of state * exn; |
|
5828 | 149 |
|
150 |
||
6689 | 151 |
(* recovery from stale signatures *) |
152 |
||
153 |
local |
|
154 |
||
155 |
fun is_stale state = Sign.is_stale (sign_of state) handle UNDEF => false; |
|
156 |
||
157 |
fun checkpoint_node true (Theory thy) = Theory (PureThy.checkpoint thy) |
|
158 |
| checkpoint_node _ node = node; |
|
159 |
||
160 |
fun copy_node true (Theory thy) = Theory (Theory.copy thy) |
|
161 |
| copy_node _ node = node; |
|
162 |
||
6965 | 163 |
fun interruptible f x = |
164 |
let val y = ref (None: node History.T option); |
|
165 |
in exhibit_interrupt (fn () => y := Some (f x)) (); the (! y) end; |
|
166 |
||
6689 | 167 |
in |
168 |
||
169 |
fun node_trans _ _ _ (State []) = raise UNDEF |
|
170 |
| node_trans int hist f (State ((node, term) :: nodes)) = |
|
171 |
let |
|
172 |
fun mk_state nd = State ((nd, term) :: nodes); |
|
173 |
||
174 |
val cont_node = History.map (checkpoint_node int) node; |
|
175 |
val back_node = History.map (copy_node int) cont_node; |
|
176 |
||
177 |
val trans = if hist then History.apply_copy (copy_node int) else History.map; |
|
6965 | 178 |
val (result, opt_exn) = |
179 |
(mk_state (transform_error (interruptible (trans (f int))) cont_node), None) |
|
180 |
handle exn => (mk_state cont_node, Some exn); |
|
6689 | 181 |
in |
182 |
if is_stale result then raise ROLLBACK (mk_state back_node, opt_exn) |
|
183 |
else (case opt_exn of None => result | Some exn => raise FAILURE (result, exn)) |
|
184 |
end; |
|
185 |
||
186 |
fun check_stale state = |
|
187 |
if not (is_stale state) then () |
|
188 |
else warning "Stale signature encountered! Should redo current theory from start."; |
|
189 |
||
190 |
end; |
|
191 |
||
192 |
||
193 |
(* primitive transitions *) |
|
194 |
||
195 |
(*Important note: recovery from stale signatures is provided only for |
|
196 |
theory-level operations via MapCurrent and AppCurrent. Other node |
|
6965 | 197 |
or state operations should not touch signatures at all. |
198 |
||
199 |
Also note that interrupts are enabled for Keep, MapCurrent, and |
|
200 |
AppCurrent only.*) |
|
5828 | 201 |
|
202 |
datatype trans = |
|
6689 | 203 |
Reset | (*empty toplevel*) |
204 |
Init of (state -> node) * ((node -> unit) * (node -> unit)) | |
|
205 |
(*push node; provide exit/kill operation*) |
|
206 |
Exit | (*pop node*) |
|
207 |
Kill | (*abort node*) |
|
208 |
Keep of state -> unit | (*peek at state*) |
|
209 |
History of node History.T -> node History.T | (*history operation (undo etc.)*) |
|
210 |
MapCurrent of bool -> node -> node | (*change node, bypassing history*) |
|
211 |
AppCurrent of bool -> node -> node; (*change node, recording history*) |
|
212 |
||
213 |
fun undo_limit int = if int then None else Some 0; |
|
214 |
||
215 |
local |
|
5828 | 216 |
|
6689 | 217 |
fun apply_tr _ Reset _ = toplevel |
218 |
| apply_tr int (Init (f, term)) (state as State nodes) = |
|
219 |
State ((History.init (undo_limit int) (f state), term) :: nodes) |
|
220 |
| apply_tr _ Exit (State []) = raise UNDEF |
|
221 |
| apply_tr _ Exit (State ((node, (exit, _)):: nodes)) = |
|
222 |
(exit (History.current node); State nodes) |
|
223 |
| apply_tr _ Kill (State []) = raise UNDEF |
|
224 |
| apply_tr _ Kill (State ((node, (_, kill)) :: nodes)) = |
|
225 |
(kill (History.current node); State nodes) |
|
6965 | 226 |
| apply_tr _ (Keep f) state = (exhibit_interrupt f state; state) |
6689 | 227 |
| apply_tr _ (History _) (State []) = raise UNDEF |
228 |
| apply_tr _ (History f) (State ((node, term) :: nodes)) = State ((f node, term) :: nodes) |
|
229 |
| apply_tr int (MapCurrent f) state = node_trans int false f state |
|
230 |
| apply_tr int (AppCurrent f) state = node_trans int true f state; |
|
5828 | 231 |
|
6689 | 232 |
fun apply_union _ [] state = raise FAILURE (state, UNDEF) |
233 |
| apply_union int (tr :: trs) state = |
|
234 |
transform_error (apply_tr int tr) state |
|
235 |
handle UNDEF => apply_union int trs state |
|
236 |
| FAILURE (alt_state, UNDEF) => apply_union int trs alt_state |
|
237 |
| exn as FAILURE _ => raise exn |
|
238 |
| exn => raise FAILURE (state, exn); |
|
239 |
||
240 |
in |
|
241 |
||
242 |
fun apply_trans int trs state = (apply_union int trs state, None) |
|
243 |
handle FAILURE (alt_state, exn) => (alt_state, Some exn) | exn => (state, Some exn); |
|
244 |
||
245 |
end; |
|
5828 | 246 |
|
247 |
||
248 |
(* datatype transition *) |
|
249 |
||
250 |
datatype transition = Transition of |
|
251 |
{name: string, |
|
252 |
pos: Position.T, |
|
253 |
int_only: bool, |
|
254 |
print: bool, |
|
255 |
trans: trans list}; |
|
256 |
||
257 |
fun make_transition (name, pos, int_only, print, trans) = |
|
258 |
Transition {name = name, pos = pos, int_only = int_only, print = print, trans = trans}; |
|
259 |
||
260 |
fun map_transition f (Transition {name, pos, int_only, print, trans}) = |
|
261 |
make_transition (f (name, pos, int_only, print, trans)); |
|
262 |
||
263 |
val empty = make_transition ("<unknown>", Position.none, false, false, []); |
|
264 |
||
265 |
||
266 |
(* diagnostics *) |
|
267 |
||
268 |
fun str_of_transition (Transition {name, pos, ...}) = quote name ^ Position.str_of pos; |
|
269 |
||
270 |
fun command_msg msg tr = msg ^ "command " ^ str_of_transition tr; |
|
271 |
fun at_command tr = command_msg "At " tr ^ "."; |
|
272 |
||
273 |
fun type_error tr state = |
|
6689 | 274 |
ERROR_MESSAGE (command_msg "Illegal application of " tr ^ " " ^ str_of_state state); |
5828 | 275 |
|
276 |
||
277 |
(* modify transitions *) |
|
278 |
||
279 |
fun name nm = map_transition |
|
280 |
(fn (_, pos, int_only, print, trans) => (nm, pos, int_only, print, trans)); |
|
281 |
||
282 |
fun position pos = map_transition |
|
283 |
(fn (name, _, int_only, print, trans) => (name, pos, int_only, print, trans)); |
|
284 |
||
285 |
fun interactive int_only = map_transition |
|
286 |
(fn (name, pos, _, print, trans) => (name, pos, int_only, print, trans)); |
|
287 |
||
288 |
val print = map_transition |
|
289 |
(fn (name, pos, int_only, _, trans) => (name, pos, int_only, true, trans)); |
|
290 |
||
291 |
fun add_trans tr = map_transition |
|
292 |
(fn (name, pos, int_only, print, trans) => (name, pos, int_only, print, trans @ [tr])); |
|
293 |
||
294 |
||
295 |
(* build transitions *) |
|
296 |
||
297 |
val reset = add_trans Reset; |
|
6689 | 298 |
fun init f exit kill = add_trans (Init (f, (exit, kill))); |
299 |
val exit = add_trans Exit; |
|
300 |
val kill = add_trans Kill; |
|
5828 | 301 |
val keep = add_trans o Keep; |
6689 | 302 |
val history = add_trans o History; |
5828 | 303 |
val map_current = add_trans o MapCurrent; |
6689 | 304 |
val app_current = add_trans o AppCurrent; |
5828 | 305 |
|
306 |
fun imperative f = keep (fn _ => f ()); |
|
307 |
||
6689 | 308 |
fun init_theory f exit kill = |
309 |
init (fn _ => Theory (f ())) |
|
310 |
(fn Theory thy => exit thy | _ => raise UNDEF) |
|
311 |
(fn Theory thy => kill thy | _ => raise UNDEF); |
|
5828 | 312 |
|
6689 | 313 |
fun theory f = app_current (fn _ => (fn Theory thy => Theory (f thy) | _ => raise UNDEF)); |
314 |
fun theory_to_proof f = |
|
315 |
app_current (fn int => (fn Theory thy => Proof (f int thy) | _ => raise UNDEF)); |
|
316 |
fun proof' f = map_current (fn int => (fn Proof prf => Proof (f int prf) | _ => raise UNDEF)); |
|
317 |
val proof = proof' o K; |
|
318 |
fun proof_to_theory f = map_current (fn _ => (fn Proof prf => Theory (f prf) | _ => raise UNDEF)); |
|
5828 | 319 |
|
320 |
||
321 |
||
322 |
(** toplevel transactions **) |
|
323 |
||
324 |
val trace = ref false; |
|
325 |
||
326 |
||
327 |
(* print exceptions *) |
|
328 |
||
329 |
fun raised name = "exception " ^ name ^ " raised"; |
|
330 |
fun raised_msg name msg = raised name ^ ": " ^ msg; |
|
331 |
||
332 |
fun exn_message TERMINATE = "Exit." |
|
5990 | 333 |
| exn_message RESTART = "Restart." |
5920 | 334 |
| exn_message (BREAK _) = "Break." |
5930 | 335 |
| exn_message (FAIL (exn, msg)) = cat_lines [exn_message exn, msg] |
6971 | 336 |
| exn_message Interrupt = "Interrupt." |
6002 | 337 |
| exn_message ERROR = "ERROR." |
5828 | 338 |
| exn_message (ERROR_MESSAGE msg) = msg |
339 |
| exn_message (THEORY (msg, _)) = msg |
|
340 |
| exn_message (ProofContext.CONTEXT (msg, _)) = msg |
|
341 |
| exn_message (Proof.STATE (msg, _)) = msg |
|
342 |
| exn_message (ProofHistory.FAIL msg) = msg |
|
5920 | 343 |
| exn_message (Attrib.ATTRIB_FAIL info) = fail_message "attribute" info |
344 |
| exn_message (Method.METHOD_FAIL info) = fail_message "method" info |
|
5828 | 345 |
| exn_message (Syntax.AST (msg, _)) = raised_msg "AST" msg |
346 |
| exn_message (TYPE (msg, _, _)) = raised_msg "TYPE" msg |
|
347 |
| exn_message (TERM (msg, _)) = raised_msg "TERM" msg |
|
348 |
| exn_message (THM (msg, _, _)) = raised_msg "THM" msg |
|
349 |
| exn_message Library.OPTION = raised "Library.OPTION" |
|
350 |
| exn_message (Library.LIST msg) = raised_msg "Library.LIST" msg |
|
5920 | 351 |
| exn_message exn = General.exnMessage exn |
352 |
and fail_message kind ((name, pos), exn) = |
|
353 |
"Error in " ^ kind ^ " " ^ name ^ Position.str_of pos ^ ":\n" ^ exn_message exn; |
|
5828 | 354 |
|
355 |
fun print_exn None = () |
|
356 |
| print_exn (Some (exn, s)) = error_msg (cat_lines [exn_message exn, s]); |
|
357 |
||
358 |
||
359 |
(* apply transitions *) |
|
360 |
||
6664 | 361 |
local |
362 |
||
6689 | 363 |
fun app int (tr as Transition {trans, int_only, print, ...}) state = |
5828 | 364 |
let |
365 |
val _ = |
|
366 |
if int orelse not int_only then () |
|
367 |
else warning (command_msg "Executing interactive-only " tr); |
|
6689 | 368 |
val (result, opt_exn) = |
369 |
(if ! trace then (writeln (command_msg "" tr); timeap) else I) (apply_trans int trans) state; |
|
370 |
val _ = if int andalso print then print_state result else (); |
|
371 |
in (result, apsome (fn UNDEF => type_error tr state | exn => exn) opt_exn) end; |
|
6664 | 372 |
|
373 |
in |
|
5828 | 374 |
|
6689 | 375 |
fun apply int tr st = |
6965 | 376 |
(case app int tr st of |
6689 | 377 |
(_, Some TERMINATE) => None |
6664 | 378 |
| (_, Some RESTART) => Some (toplevel, None) |
379 |
| (state', Some (FAIL (exn_info as (BREAK break_state, _)))) => |
|
380 |
Some (append_states break_state state', Some exn_info) |
|
381 |
| (state', Some (FAIL exn_info)) => Some (state', Some exn_info) |
|
6689 | 382 |
| (_, Some (ROLLBACK (back_state, opt_exn))) => |
383 |
(warning (command_msg "Rollback after " tr); |
|
384 |
Some (back_state, apsome (fn exn => (exn, at_command tr)) opt_exn)) |
|
385 |
| (state', Some exn) => Some (state', Some (exn, at_command tr)) |
|
386 |
| (state', None) => Some (state', None)); |
|
6664 | 387 |
|
388 |
end; |
|
5828 | 389 |
|
390 |
||
391 |
(* excursion: toplevel -- apply transformers -- toplevel *) |
|
392 |
||
6664 | 393 |
local |
394 |
||
5828 | 395 |
fun excur [] x = x |
396 |
| excur (tr :: trs) x = |
|
397 |
(case apply false tr x of |
|
398 |
Some (x', None) => excur trs x' |
|
399 |
| Some (x', Some exn_info) => raise FAIL exn_info |
|
400 |
| None => raise FAIL (TERMINATE, at_command tr)); |
|
401 |
||
6664 | 402 |
in |
403 |
||
5828 | 404 |
fun excursion trs = |
405 |
(case excur trs (State []) of |
|
406 |
State [] => () |
|
6244 | 407 |
| _ => raise ERROR_MESSAGE "Open block(s) pending at end of input"); |
5828 | 408 |
|
6664 | 409 |
end; |
410 |
||
5828 | 411 |
|
412 |
||
413 |
(** interactive transformations **) |
|
414 |
||
415 |
(* the global state reference *) |
|
416 |
||
6689 | 417 |
val global_state = ref (toplevel, None: (exn * string) option); |
5828 | 418 |
|
419 |
fun set_state state = global_state := (state, None); |
|
420 |
fun get_state () = fst (! global_state); |
|
421 |
fun exn () = snd (! global_state); |
|
422 |
||
423 |
||
6965 | 424 |
(* the Isar source of transitions *) |
425 |
||
426 |
type isar = |
|
427 |
(transition, (transition option, |
|
428 |
(OuterLex.token, (OuterLex.token, |
|
429 |
Position.T * (Symbol.symbol, (string, unit) Source.source) Source.source) |
|
430 |
Source.source) Source.source) Source.source) Source.source; |
|
431 |
||
432 |
fun transform_interrupt_isar f x = |
|
433 |
let val y = ref (None: (transition * isar) option option); |
|
434 |
in exhibit_interrupt (fn () => y := Some (f x)) (); the (! y) end; |
|
435 |
||
436 |
fun get_interruptible src = |
|
437 |
Some (transform_interrupt_isar Source.get_single src) |
|
438 |
handle Interrupt => None; |
|
439 |
||
440 |
||
5828 | 441 |
(* apply transformers to global state *) |
442 |
||
443 |
nonfix >>; |
|
444 |
||
445 |
fun >> tr = |
|
446 |
(case apply true tr (get_state ()) of |
|
447 |
None => false |
|
448 |
| Some (state', exn_info) => |
|
449 |
(global_state := (state', exn_info); |
|
450 |
check_stale state'; print_exn exn_info; |
|
451 |
true)); |
|
452 |
||
453 |
fun raw_loop src = |
|
6689 | 454 |
(case get_interruptible (Source.set_prompt (prompt_state (get_state ())) src) of |
6971 | 455 |
None => (writeln "\nInterrupt."; raw_loop src) |
5828 | 456 |
| Some None => () |
457 |
| Some (Some (tr, src')) => if >> tr then raw_loop src' else ()); |
|
458 |
||
459 |
||
460 |
fun loop src = mask_interrupt raw_loop src; |
|
461 |
||
462 |
||
463 |
end; |