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