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