author | wenzelm |
Fri, 21 May 1999 11:46:42 +0200 | |
changeset 6689 | 169e5b07ec06 |
parent 6664 | f679ddd1ddd8 |
child 6965 | a766de752996 |
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 |
6689 | 51 |
type isar |
5828 | 52 |
val apply: bool -> transition -> state -> (state * (exn * string) option) option |
53 |
val excursion: transition list -> unit |
|
54 |
val set_state: state -> unit |
|
55 |
val get_state: unit -> state |
|
56 |
val exn: unit -> (exn * string) option |
|
57 |
val >> : transition -> bool |
|
58 |
val loop: isar -> unit |
|
59 |
end; |
|
60 |
||
6689 | 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 |
||
163 |
in |
|
164 |
||
165 |
fun node_trans _ _ _ (State []) = raise UNDEF |
|
166 |
| node_trans int hist f (State ((node, term) :: nodes)) = |
|
167 |
let |
|
168 |
fun mk_state nd = State ((nd, term) :: nodes); |
|
169 |
||
170 |
val cont_node = History.map (checkpoint_node int) node; |
|
171 |
val back_node = History.map (copy_node int) cont_node; |
|
172 |
||
173 |
val trans = if hist then History.apply_copy (copy_node int) else History.map; |
|
174 |
val (result, opt_exn) = (mk_state (transform_error (trans (f int)) cont_node), None) |
|
175 |
handle exn => (mk_state cont_node, Some exn); |
|
176 |
in |
|
177 |
if is_stale result then raise ROLLBACK (mk_state back_node, opt_exn) |
|
178 |
else (case opt_exn of None => result | Some exn => raise FAILURE (result, exn)) |
|
179 |
end; |
|
180 |
||
181 |
fun check_stale state = |
|
182 |
if not (is_stale state) then () |
|
183 |
else warning "Stale signature encountered! Should redo current theory from start."; |
|
184 |
||
185 |
end; |
|
186 |
||
187 |
||
188 |
(* primitive transitions *) |
|
189 |
||
190 |
(*Important note: recovery from stale signatures is provided only for |
|
191 |
theory-level operations via MapCurrent and AppCurrent. Other node |
|
192 |
or state operations should not touch signatures at all.*) |
|
5828 | 193 |
|
194 |
datatype trans = |
|
6689 | 195 |
Reset | (*empty toplevel*) |
196 |
Init of (state -> node) * ((node -> unit) * (node -> unit)) | |
|
197 |
(*push node; provide exit/kill operation*) |
|
198 |
Exit | (*pop node*) |
|
199 |
Kill | (*abort node*) |
|
200 |
Keep of state -> unit | (*peek at state*) |
|
201 |
History of node History.T -> node History.T | (*history operation (undo etc.)*) |
|
202 |
MapCurrent of bool -> node -> node | (*change node, bypassing history*) |
|
203 |
AppCurrent of bool -> node -> node; (*change node, recording history*) |
|
204 |
||
205 |
fun undo_limit int = if int then None else Some 0; |
|
206 |
||
207 |
local |
|
5828 | 208 |
|
6689 | 209 |
fun apply_tr _ Reset _ = toplevel |
210 |
| apply_tr int (Init (f, term)) (state as State nodes) = |
|
211 |
State ((History.init (undo_limit int) (f state), term) :: nodes) |
|
212 |
| apply_tr _ Exit (State []) = raise UNDEF |
|
213 |
| apply_tr _ Exit (State ((node, (exit, _)):: nodes)) = |
|
214 |
(exit (History.current node); State nodes) |
|
215 |
| apply_tr _ Kill (State []) = raise UNDEF |
|
216 |
| apply_tr _ Kill (State ((node, (_, kill)) :: nodes)) = |
|
217 |
(kill (History.current node); State nodes) |
|
218 |
| apply_tr _ (Keep f) state = (f state; state) |
|
219 |
| apply_tr _ (History _) (State []) = raise UNDEF |
|
220 |
| apply_tr _ (History f) (State ((node, term) :: nodes)) = State ((f node, term) :: nodes) |
|
221 |
| apply_tr int (MapCurrent f) state = node_trans int false f state |
|
222 |
| apply_tr int (AppCurrent f) state = node_trans int true f state; |
|
5828 | 223 |
|
6689 | 224 |
fun apply_union _ [] state = raise FAILURE (state, UNDEF) |
225 |
| apply_union int (tr :: trs) state = |
|
226 |
transform_error (apply_tr int tr) state |
|
227 |
handle UNDEF => apply_union int trs state |
|
228 |
| FAILURE (alt_state, UNDEF) => apply_union int trs alt_state |
|
229 |
| exn as FAILURE _ => raise exn |
|
230 |
| exn => raise FAILURE (state, exn); |
|
231 |
||
232 |
in |
|
233 |
||
234 |
fun apply_trans int trs state = (apply_union int trs state, None) |
|
235 |
handle FAILURE (alt_state, exn) => (alt_state, Some exn) | exn => (state, Some exn); |
|
236 |
||
237 |
end; |
|
5828 | 238 |
|
239 |
||
240 |
(* datatype transition *) |
|
241 |
||
242 |
datatype transition = Transition of |
|
243 |
{name: string, |
|
244 |
pos: Position.T, |
|
245 |
int_only: bool, |
|
246 |
print: bool, |
|
247 |
trans: trans list}; |
|
248 |
||
249 |
fun make_transition (name, pos, int_only, print, trans) = |
|
250 |
Transition {name = name, pos = pos, int_only = int_only, print = print, trans = trans}; |
|
251 |
||
252 |
fun map_transition f (Transition {name, pos, int_only, print, trans}) = |
|
253 |
make_transition (f (name, pos, int_only, print, trans)); |
|
254 |
||
255 |
val empty = make_transition ("<unknown>", Position.none, false, false, []); |
|
256 |
||
257 |
||
258 |
(* diagnostics *) |
|
259 |
||
260 |
fun str_of_transition (Transition {name, pos, ...}) = quote name ^ Position.str_of pos; |
|
261 |
||
262 |
fun command_msg msg tr = msg ^ "command " ^ str_of_transition tr; |
|
263 |
fun at_command tr = command_msg "At " tr ^ "."; |
|
264 |
||
265 |
fun type_error tr state = |
|
6689 | 266 |
ERROR_MESSAGE (command_msg "Illegal application of " tr ^ " " ^ str_of_state state); |
5828 | 267 |
|
268 |
||
269 |
(* modify transitions *) |
|
270 |
||
271 |
fun name nm = map_transition |
|
272 |
(fn (_, pos, int_only, print, trans) => (nm, pos, int_only, print, trans)); |
|
273 |
||
274 |
fun position pos = map_transition |
|
275 |
(fn (name, _, int_only, print, trans) => (name, pos, int_only, print, trans)); |
|
276 |
||
277 |
fun interactive int_only = map_transition |
|
278 |
(fn (name, pos, _, print, trans) => (name, pos, int_only, print, trans)); |
|
279 |
||
280 |
val print = map_transition |
|
281 |
(fn (name, pos, int_only, _, trans) => (name, pos, int_only, true, trans)); |
|
282 |
||
283 |
fun add_trans tr = map_transition |
|
284 |
(fn (name, pos, int_only, print, trans) => (name, pos, int_only, print, trans @ [tr])); |
|
285 |
||
286 |
||
287 |
(* build transitions *) |
|
288 |
||
289 |
val reset = add_trans Reset; |
|
6689 | 290 |
fun init f exit kill = add_trans (Init (f, (exit, kill))); |
291 |
val exit = add_trans Exit; |
|
292 |
val kill = add_trans Kill; |
|
5828 | 293 |
val keep = add_trans o Keep; |
6689 | 294 |
val history = add_trans o History; |
5828 | 295 |
val map_current = add_trans o MapCurrent; |
6689 | 296 |
val app_current = add_trans o AppCurrent; |
5828 | 297 |
|
298 |
fun imperative f = keep (fn _ => f ()); |
|
299 |
||
6689 | 300 |
fun init_theory f exit kill = |
301 |
init (fn _ => Theory (f ())) |
|
302 |
(fn Theory thy => exit thy | _ => raise UNDEF) |
|
303 |
(fn Theory thy => kill thy | _ => raise UNDEF); |
|
5828 | 304 |
|
6689 | 305 |
fun theory f = app_current (fn _ => (fn Theory thy => Theory (f thy) | _ => raise UNDEF)); |
306 |
fun theory_to_proof f = |
|
307 |
app_current (fn int => (fn Theory thy => Proof (f int thy) | _ => raise UNDEF)); |
|
308 |
fun proof' f = map_current (fn int => (fn Proof prf => Proof (f int prf) | _ => raise UNDEF)); |
|
309 |
val proof = proof' o K; |
|
310 |
fun proof_to_theory f = map_current (fn _ => (fn Proof prf => Theory (f prf) | _ => raise UNDEF)); |
|
5828 | 311 |
|
312 |
||
313 |
||
314 |
(** toplevel transactions **) |
|
315 |
||
316 |
val trace = ref false; |
|
317 |
||
318 |
||
319 |
(* print exceptions *) |
|
320 |
||
321 |
fun raised name = "exception " ^ name ^ " raised"; |
|
322 |
fun raised_msg name msg = raised name ^ ": " ^ msg; |
|
323 |
||
324 |
fun exn_message TERMINATE = "Exit." |
|
5990 | 325 |
| exn_message RESTART = "Restart." |
5920 | 326 |
| exn_message (BREAK _) = "Break." |
5930 | 327 |
| exn_message (FAIL (exn, msg)) = cat_lines [exn_message exn, msg] |
5920 | 328 |
| exn_message Interrupt = "Interrupt (exec)." |
6002 | 329 |
| exn_message ERROR = "ERROR." |
5828 | 330 |
| exn_message (ERROR_MESSAGE msg) = msg |
331 |
| exn_message (THEORY (msg, _)) = msg |
|
332 |
| exn_message (ProofContext.CONTEXT (msg, _)) = msg |
|
333 |
| exn_message (Proof.STATE (msg, _)) = msg |
|
334 |
| exn_message (ProofHistory.FAIL msg) = msg |
|
5920 | 335 |
| exn_message (Attrib.ATTRIB_FAIL info) = fail_message "attribute" info |
336 |
| exn_message (Method.METHOD_FAIL info) = fail_message "method" info |
|
5828 | 337 |
| exn_message (Syntax.AST (msg, _)) = raised_msg "AST" msg |
338 |
| exn_message (TYPE (msg, _, _)) = raised_msg "TYPE" msg |
|
339 |
| exn_message (TERM (msg, _)) = raised_msg "TERM" msg |
|
340 |
| exn_message (THM (msg, _, _)) = raised_msg "THM" msg |
|
341 |
| exn_message Library.OPTION = raised "Library.OPTION" |
|
342 |
| exn_message (Library.LIST msg) = raised_msg "Library.LIST" msg |
|
5920 | 343 |
| exn_message exn = General.exnMessage exn |
344 |
and fail_message kind ((name, pos), exn) = |
|
345 |
"Error in " ^ kind ^ " " ^ name ^ Position.str_of pos ^ ":\n" ^ exn_message exn; |
|
5828 | 346 |
|
347 |
fun print_exn None = () |
|
348 |
| print_exn (Some (exn, s)) = error_msg (cat_lines [exn_message exn, s]); |
|
349 |
||
350 |
||
6689 | 351 |
(* the Isar source of transitions *) |
352 |
||
353 |
type isar = |
|
354 |
(transition, (transition option, |
|
355 |
(OuterLex.token, (OuterLex.token, |
|
356 |
Position.T * (Symbol.symbol, (string, unit) Source.source) Source.source) |
|
357 |
Source.source) Source.source) Source.source) Source.source; |
|
358 |
||
359 |
||
5828 | 360 |
(* transform interrupt (non-polymorphic) *) |
361 |
||
362 |
fun transform_interrupt_state f x = |
|
6689 | 363 |
let val y = ref (None: (state * exn option) option); |
5828 | 364 |
in exhibit_interrupt (fn () => y := Some (f x)) (); the (! y) end; |
365 |
||
366 |
fun transform_interrupt_isar f x = |
|
367 |
let val y = ref (None: (transition * isar) option option); |
|
368 |
in exhibit_interrupt (fn () => y := Some (f x)) (); the (! y) end; |
|
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; |
|
382 |
val _ = if int andalso print then print_state result else (); |
|
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 = |
388 |
(case transform_interrupt_state (app int tr) st of |
|
389 |
(_, Some TERMINATE) => None |
|
6664 | 390 |
| (_, Some RESTART) => Some (toplevel, None) |
391 |
| (state', Some (FAIL (exn_info as (BREAK break_state, _)))) => |
|
392 |
Some (append_states break_state state', Some exn_info) |
|
393 |
| (state', Some (FAIL exn_info)) => Some (state', Some exn_info) |
|
6689 | 394 |
| (_, Some (ROLLBACK (back_state, opt_exn))) => |
395 |
(warning (command_msg "Rollback after " tr); |
|
396 |
Some (back_state, apsome (fn exn => (exn, at_command tr)) opt_exn)) |
|
397 |
| (state', Some exn) => Some (state', Some (exn, at_command tr)) |
|
398 |
| (state', None) => Some (state', None)); |
|
6664 | 399 |
|
400 |
end; |
|
5828 | 401 |
|
402 |
||
403 |
(* excursion: toplevel -- apply transformers -- toplevel *) |
|
404 |
||
6664 | 405 |
local |
406 |
||
5828 | 407 |
fun excur [] x = x |
408 |
| excur (tr :: trs) x = |
|
409 |
(case apply false tr x of |
|
410 |
Some (x', None) => excur trs x' |
|
411 |
| Some (x', Some exn_info) => raise FAIL exn_info |
|
412 |
| None => raise FAIL (TERMINATE, at_command tr)); |
|
413 |
||
6664 | 414 |
in |
415 |
||
5828 | 416 |
fun excursion trs = |
417 |
(case excur trs (State []) of |
|
418 |
State [] => () |
|
6244 | 419 |
| _ => raise ERROR_MESSAGE "Open block(s) pending at end of input"); |
5828 | 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 |
||
436 |
(* apply transformers to global state *) |
|
437 |
||
438 |
nonfix >>; |
|
439 |
||
440 |
fun >> tr = |
|
441 |
(case apply true tr (get_state ()) of |
|
442 |
None => false |
|
443 |
| Some (state', exn_info) => |
|
444 |
(global_state := (state', exn_info); |
|
445 |
check_stale state'; print_exn exn_info; |
|
446 |
true)); |
|
447 |
||
448 |
fun get_interruptible src = |
|
449 |
Some (transform_interrupt_isar Source.get_single src) |
|
450 |
handle Interrupt => None; |
|
451 |
||
452 |
fun raw_loop src = |
|
6689 | 453 |
(case get_interruptible (Source.set_prompt (prompt_state (get_state ())) src) of |
5920 | 454 |
None => (writeln "\nInterrupt (read)."; raw_loop src) |
5828 | 455 |
| Some None => () |
456 |
| Some (Some (tr, src')) => if >> tr then raw_loop src' else ()); |
|
457 |
||
458 |
||
459 |
fun loop src = mask_interrupt raw_loop src; |
|
460 |
||
461 |
||
462 |
end; |