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