author | wenzelm |
Sun, 29 Nov 1998 13:17:21 +0100 | |
changeset 5990 | 8b6de9bd7d72 |
parent 5946 | a4600d21b59b |
child 6002 | c1f28f8ec72c |
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 |
TODO: |
|
8 |
- cleanup this file: |
|
9 |
. more internal locality; |
|
10 |
. clearer structure of control flow (exceptions, interrupts); |
|
11 |
. clear division with outer_syntax.ML (!?); |
|
12 |
- improve transactions; only in interactive mode! |
|
13 |
- init / exit proof; |
|
5946
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
14 |
- display stack size in prompt (prompt: state -> string (hook)); |
5828 | 15 |
*) |
16 |
||
17 |
signature TOPLEVEL = |
|
18 |
sig |
|
19 |
datatype node = Theory of theory | Proof of ProofHistory.T |
|
20 |
type state |
|
21 |
exception UNDEF |
|
22 |
val toplevel: state |
|
5946
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
23 |
val print_state_default: state -> unit |
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
24 |
val print_state_fn: (state -> unit) ref |
5828 | 25 |
val print_state: state -> unit |
26 |
val node_of: state -> node |
|
27 |
val node_cases: (theory -> 'a) -> (Proof.state -> 'a) -> state -> 'a |
|
28 |
val theory_of: state -> theory |
|
29 |
val sign_of: state -> Sign.sg |
|
30 |
val proof_of: state -> Proof.state |
|
31 |
type transition |
|
32 |
exception TERMINATE |
|
5990 | 33 |
exception RESTART |
5920 | 34 |
exception BREAK of state |
5828 | 35 |
val empty: transition |
36 |
val name: string -> transition -> transition |
|
37 |
val position: Position.T -> transition -> transition |
|
38 |
val interactive: bool -> transition -> transition |
|
39 |
val print: transition -> transition |
|
40 |
val reset: transition -> transition |
|
41 |
val init: (state -> node) -> (node -> unit) -> transition -> transition |
|
42 |
val keep: (state -> unit) -> transition -> transition |
|
43 |
val exit: transition -> transition |
|
44 |
val imperative: (unit -> unit) -> transition -> transition |
|
45 |
val init_theory: (unit -> theory) -> (theory -> unit) -> transition -> transition |
|
46 |
(* FIXME |
|
47 |
val init_proof: (theory -> ProofHistory.T) -> transition -> transition |
|
48 |
val exit_proof: (ProofHistory.T -> unit) -> transition -> transition |
|
49 |
*) |
|
50 |
val theory: (theory -> theory) -> transition -> transition |
|
51 |
val proof: (ProofHistory.T -> ProofHistory.T) -> transition -> transition |
|
52 |
val theory_to_proof: (theory -> ProofHistory.T) -> transition -> transition |
|
53 |
val proof_to_theory: (ProofHistory.T -> theory) -> transition -> transition |
|
54 |
type isar |
|
55 |
val trace: bool ref |
|
5922 | 56 |
val exn_message: exn -> string |
5828 | 57 |
val apply: bool -> transition -> state -> (state * (exn * string) option) option |
58 |
val excursion: transition list -> unit |
|
59 |
val set_state: state -> unit |
|
60 |
val get_state: unit -> state |
|
61 |
val exn: unit -> (exn * string) option |
|
62 |
val >> : transition -> bool |
|
63 |
val loop: isar -> unit |
|
64 |
end; |
|
65 |
||
66 |
structure Toplevel: TOPLEVEL = |
|
67 |
struct |
|
68 |
||
69 |
||
70 |
(** toplevel state **) |
|
71 |
||
72 |
(* datatype node *) |
|
73 |
||
74 |
datatype node = |
|
75 |
Theory of theory | |
|
76 |
Proof of ProofHistory.T; |
|
77 |
||
78 |
fun print_node (Theory 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 |
| print_node (Proof prf) = |
|
5946
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
82 |
(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
|
83 |
Proof.print_state (ProofHistory.current prf)); |
5828 | 84 |
|
85 |
||
86 |
(* datatype state *) |
|
87 |
||
88 |
datatype state = State of (node * (node -> unit)) list; |
|
89 |
||
90 |
exception UNDEF; |
|
91 |
||
92 |
val toplevel = State []; |
|
5939 | 93 |
fun append_states (State ns) (State ms) = State (ns @ ms); |
5828 | 94 |
|
95 |
fun str_of_state (State []) = "at top level" |
|
96 |
| str_of_state (State ((Theory _, _) :: _)) = "in theory mode" |
|
97 |
| str_of_state (State ((Proof _, _) :: _)) = "in proof mode"; |
|
98 |
||
99 |
||
5946
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
100 |
(* print_state hook *) |
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
101 |
|
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
102 |
fun print_topnode (State []) = () |
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
103 |
| print_topnode (State [(node, _)]) = print_node node |
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
104 |
| print_topnode (State ((node, _) :: nodes)) = |
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
105 |
(writeln ("Stack size: " ^ string_of_int (length nodes)); print_node node); |
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_state_default state = |
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
108 |
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
|
109 |
if begin_state = "" then () else writeln begin_state; |
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
110 |
print_topnode state; |
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
111 |
if end_state = "" then () else writeln end_state |
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
112 |
end; |
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
113 |
|
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
114 |
val print_state_fn = ref print_state_default; |
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
115 |
fun print_state state = ! print_state_fn state; |
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 |
|
5828 | 118 |
(* top node *) |
119 |
||
120 |
fun node_of (State []) = raise UNDEF |
|
121 |
| node_of (State ((node, _) :: _)) = node; |
|
122 |
||
123 |
fun node_cases f g state = |
|
124 |
(case node_of state of |
|
125 |
Theory thy => f thy |
|
126 |
| Proof prf => g (ProofHistory.current prf)); |
|
127 |
||
128 |
val theory_of = node_cases I Proof.theory_of; |
|
129 |
val sign_of = Theory.sign_of o theory_of; |
|
130 |
val proof_of = node_cases (fn _ => raise UNDEF) I; |
|
131 |
||
132 |
||
133 |
||
134 |
(** toplevel transitions **) |
|
135 |
||
136 |
exception TERMINATE; |
|
5990 | 137 |
exception RESTART; |
5920 | 138 |
exception BREAK of state; |
5828 | 139 |
exception FAIL of exn * string; |
140 |
||
141 |
||
142 |
(* datatype trans *) |
|
143 |
||
144 |
datatype trans = |
|
145 |
Reset | |
|
146 |
Init of (state -> node) * (node -> unit) | |
|
147 |
Keep of (state -> unit) | |
|
148 |
Exit | |
|
149 |
MapCurrent of node -> node; |
|
150 |
||
151 |
fun apply_trans Reset _ = State [] |
|
152 |
| apply_trans (Init (f, g)) (state as State nodes) = State ((f state, g) :: nodes) |
|
153 |
| apply_trans (Keep f) state = (f state; state) |
|
154 |
| apply_trans Exit (State []) = raise UNDEF |
|
155 |
| apply_trans Exit (State ((node, g) :: nodes)) = (g node; State nodes) |
|
156 |
| apply_trans (MapCurrent _) (State []) = raise UNDEF |
|
157 |
| apply_trans (MapCurrent f) (State ((node, g) :: nodes)) = State ((f node, g) :: nodes); |
|
158 |
||
159 |
fun apply_union [] _ = raise UNDEF |
|
160 |
| apply_union (tr :: trs) state = |
|
161 |
apply_trans tr state handle UNDEF => apply_union trs state; |
|
162 |
||
163 |
||
164 |
(* datatype transition *) |
|
165 |
||
166 |
datatype transition = Transition of |
|
167 |
{name: string, |
|
168 |
pos: Position.T, |
|
169 |
int_only: bool, |
|
170 |
print: bool, |
|
171 |
trans: trans list}; |
|
172 |
||
173 |
fun make_transition (name, pos, int_only, print, trans) = |
|
174 |
Transition {name = name, pos = pos, int_only = int_only, print = print, trans = trans}; |
|
175 |
||
176 |
fun map_transition f (Transition {name, pos, int_only, print, trans}) = |
|
177 |
make_transition (f (name, pos, int_only, print, trans)); |
|
178 |
||
179 |
val empty = make_transition ("<unknown>", Position.none, false, false, []); |
|
180 |
||
181 |
||
182 |
(* diagnostics *) |
|
183 |
||
184 |
fun str_of_transition (Transition {name, pos, ...}) = quote name ^ Position.str_of pos; |
|
185 |
||
186 |
fun command_msg msg tr = msg ^ "command " ^ str_of_transition tr; |
|
187 |
fun at_command tr = command_msg "At " tr ^ "."; |
|
188 |
||
189 |
fun type_error tr state = |
|
190 |
error (command_msg "Illegal application of " tr ^ " " ^ str_of_state state); |
|
191 |
||
192 |
||
193 |
(* modify transitions *) |
|
194 |
||
195 |
fun name nm = map_transition |
|
196 |
(fn (_, pos, int_only, print, trans) => (nm, pos, int_only, print, trans)); |
|
197 |
||
198 |
fun position pos = map_transition |
|
199 |
(fn (name, _, int_only, print, trans) => (name, pos, int_only, print, trans)); |
|
200 |
||
201 |
fun interactive int_only = map_transition |
|
202 |
(fn (name, pos, _, print, trans) => (name, pos, int_only, print, trans)); |
|
203 |
||
204 |
val print = map_transition |
|
205 |
(fn (name, pos, int_only, _, trans) => (name, pos, int_only, true, trans)); |
|
206 |
||
207 |
fun add_trans tr = map_transition |
|
208 |
(fn (name, pos, int_only, print, trans) => (name, pos, int_only, print, trans @ [tr])); |
|
209 |
||
210 |
||
211 |
(* build transitions *) |
|
212 |
||
213 |
val reset = add_trans Reset; |
|
214 |
fun init f g = add_trans (Init (f, g)); |
|
215 |
val keep = add_trans o Keep; |
|
216 |
val exit = add_trans Exit; |
|
217 |
val map_current = add_trans o MapCurrent; |
|
218 |
||
219 |
fun imperative f = keep (fn _ => f ()); |
|
220 |
||
221 |
fun init_theory f g = |
|
222 |
init (fn _ => Theory (f ())) (fn Theory thy => g thy | _ => raise UNDEF); |
|
223 |
||
224 |
fun theory f = map_current |
|
225 |
(fn Theory thy => Theory (PureThy.transaction f thy) | _ => raise UNDEF); |
|
226 |
fun proof f = map_current (fn Proof prf => Proof (f prf) | _ => raise UNDEF); |
|
227 |
||
228 |
fun theory_to_proof f = map_current (fn Theory thy => Proof (f thy) | _ => raise UNDEF); |
|
229 |
fun proof_to_theory f = map_current (fn Proof prf => Theory (f prf) | _ => raise UNDEF); |
|
230 |
||
231 |
||
232 |
(* the Isar source of transitions *) |
|
233 |
||
234 |
type isar = |
|
235 |
(transition, (transition option, |
|
236 |
(OuterLex.token, (OuterLex.token, |
|
237 |
Position.T * (Symbol.symbol, (string, unit) Source.source) Source.source) |
|
238 |
Source.source) Source.source) Source.source) Source.source; |
|
239 |
||
240 |
||
241 |
||
242 |
(** toplevel transactions **) |
|
243 |
||
244 |
val trace = ref false; |
|
245 |
||
246 |
||
247 |
(* print exceptions *) |
|
248 |
||
249 |
fun raised name = "exception " ^ name ^ " raised"; |
|
250 |
fun raised_msg name msg = raised name ^ ": " ^ msg; |
|
251 |
||
252 |
fun exn_message TERMINATE = "Exit." |
|
5990 | 253 |
| exn_message RESTART = "Restart." |
5920 | 254 |
| exn_message (BREAK _) = "Break." |
5930 | 255 |
| exn_message (FAIL (exn, msg)) = cat_lines [exn_message exn, msg] |
5920 | 256 |
| exn_message Interrupt = "Interrupt (exec)." |
5828 | 257 |
| exn_message (ERROR_MESSAGE msg) = msg |
258 |
| exn_message (THEORY (msg, _)) = msg |
|
259 |
| exn_message (ProofContext.CONTEXT (msg, _)) = msg |
|
260 |
| exn_message (Proof.STATE (msg, _)) = msg |
|
261 |
| exn_message (ProofHistory.FAIL msg) = msg |
|
5920 | 262 |
| exn_message (Attrib.ATTRIB_FAIL info) = fail_message "attribute" info |
263 |
| exn_message (Method.METHOD_FAIL info) = fail_message "method" info |
|
5828 | 264 |
| exn_message (Syntax.AST (msg, _)) = raised_msg "AST" msg |
265 |
| exn_message (TYPE (msg, _, _)) = raised_msg "TYPE" msg |
|
266 |
| exn_message (TERM (msg, _)) = raised_msg "TERM" msg |
|
267 |
| exn_message (THM (msg, _, _)) = raised_msg "THM" msg |
|
268 |
| exn_message Library.OPTION = raised "Library.OPTION" |
|
269 |
| exn_message (Library.LIST msg) = raised_msg "Library.LIST" msg |
|
5920 | 270 |
| exn_message exn = General.exnMessage exn |
271 |
and fail_message kind ((name, pos), exn) = |
|
272 |
"Error in " ^ kind ^ " " ^ name ^ Position.str_of pos ^ ":\n" ^ exn_message exn; |
|
5828 | 273 |
|
274 |
fun print_exn None = () |
|
275 |
| print_exn (Some (exn, s)) = error_msg (cat_lines [exn_message exn, s]); |
|
276 |
||
277 |
||
278 |
(* transform interrupt (non-polymorphic) *) |
|
279 |
||
280 |
fun transform_interrupt_state f x = |
|
281 |
let val y = ref (None: state option); |
|
282 |
in exhibit_interrupt (fn () => y := Some (f x)) (); the (! y) end; |
|
283 |
||
284 |
fun transform_interrupt_isar f x = |
|
285 |
let val y = ref (None: (transition * isar) option option); |
|
286 |
in exhibit_interrupt (fn () => y := Some (f x)) (); the (! y) end; |
|
287 |
||
288 |
||
289 |
(* the dreaded stale signatures *) |
|
290 |
||
291 |
fun check_stale state = |
|
292 |
(case try theory_of state of |
|
293 |
None => () |
|
294 |
| Some thy => |
|
295 |
if Sign.is_stale (Theory.sign_of thy) then |
|
296 |
warning "Stale signature encountered! Should redo current theory from start." |
|
297 |
else ()); |
|
298 |
||
299 |
||
300 |
(* apply transitions *) |
|
301 |
||
302 |
fun app int (tr as Transition {trans, int_only, print, ...}) state = |
|
303 |
let |
|
304 |
val _ = |
|
305 |
if int orelse not int_only then () |
|
306 |
else warning (command_msg "Executing interactive-only " tr); |
|
307 |
val state' = |
|
308 |
(if ! trace then (writeln (command_msg "" tr); timeap (apply_union trans) state) |
|
309 |
else apply_union trans state) |
|
310 |
handle UNDEF => type_error tr state; |
|
311 |
val _ = if int andalso print then print_state state' else (); |
|
312 |
in state' end; |
|
313 |
||
314 |
fun rollback tr copy_thy (State ((Theory _, g) :: nodes)) = |
|
315 |
(warning (command_msg "Rollback after " tr); State ((Theory copy_thy, g) :: nodes)) |
|
316 |
| rollback tr _ state = |
|
317 |
(warning (command_msg "Ignoring rollback theory copy from " tr); state); |
|
318 |
||
319 |
fun apply int tr state = |
|
320 |
Some (transform_interrupt_state (transform_error (app int tr)) state, None) |
|
321 |
handle |
|
322 |
TERMINATE => None |
|
5990 | 323 |
| RESTART => Some (toplevel, None) |
5939 | 324 |
| FAIL (exn_info as (BREAK break_state, _)) => |
325 |
Some (append_states break_state state, Some exn_info) |
|
5828 | 326 |
| FAIL exn_info => Some (state, Some exn_info) |
327 |
| PureThy.ROLLBACK (copy_thy, opt_exn) => |
|
328 |
Some (rollback tr copy_thy state, apsome (fn exn => (exn, at_command tr)) opt_exn) |
|
329 |
| exn => Some (state, Some (exn, at_command tr)); |
|
330 |
||
331 |
||
332 |
(* excursion: toplevel -- apply transformers -- toplevel *) |
|
333 |
||
334 |
fun excur [] x = x |
|
335 |
| excur (tr :: trs) x = |
|
336 |
(case apply false tr x of |
|
337 |
Some (x', None) => excur trs x' |
|
338 |
| Some (x', Some exn_info) => raise FAIL exn_info |
|
339 |
| None => raise FAIL (TERMINATE, at_command tr)); |
|
340 |
||
341 |
fun excursion trs = |
|
342 |
(case excur trs (State []) of |
|
343 |
State [] => () |
|
344 |
| _ => error "Pending blocks at end of excursion"); |
|
345 |
||
346 |
||
347 |
||
348 |
(** interactive transformations **) |
|
349 |
||
350 |
(* the global state reference *) |
|
351 |
||
352 |
val global_state = ref (State [], None: (exn * string) option); |
|
353 |
||
354 |
fun set_state state = global_state := (state, None); |
|
355 |
fun get_state () = fst (! global_state); |
|
356 |
fun exn () = snd (! global_state); |
|
357 |
||
358 |
||
359 |
(* apply transformers to global state *) |
|
360 |
||
361 |
nonfix >>; |
|
362 |
||
363 |
fun >> tr = |
|
364 |
(case apply true tr (get_state ()) of |
|
365 |
None => false |
|
366 |
| Some (state', exn_info) => |
|
367 |
(global_state := (state', exn_info); |
|
368 |
check_stale state'; print_exn exn_info; |
|
369 |
true)); |
|
370 |
||
371 |
fun get_interruptible src = |
|
372 |
Some (transform_interrupt_isar Source.get_single src) |
|
373 |
handle Interrupt => None; |
|
374 |
||
375 |
fun raw_loop src = |
|
376 |
(case get_interruptible src of |
|
5920 | 377 |
None => (writeln "\nInterrupt (read)."; raw_loop src) |
5828 | 378 |
| Some None => () |
379 |
| Some (Some (tr, src')) => if >> tr then raw_loop src' else ()); |
|
380 |
||
381 |
||
382 |
fun loop src = mask_interrupt raw_loop src; |
|
383 |
||
384 |
||
385 |
end; |