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