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