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