author | wenzelm |
Sun, 11 Sep 2005 20:02:51 +0200 | |
changeset 17320 | e72f43c659f7 |
parent 17266 | 31c23e8f8111 |
child 17321 | 227f1f5c3959 |
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 |
|
16815 | 10 |
datatype node = |
17076 | 11 |
Theory of theory * Proof.context option | |
12 |
Proof of ProofHistory.T | |
|
13 |
SkipProof of int History.T * theory |
|
5828 | 14 |
type state |
15 |
val toplevel: state |
|
7732 | 16 |
val is_toplevel: state -> bool |
17076 | 17 |
val level: state -> int |
6689 | 18 |
exception UNDEF |
17266 | 19 |
val assert: bool -> unit |
6689 | 20 |
val node_history_of: state -> node History.T |
5828 | 21 |
val node_of: state -> node |
6664 | 22 |
val node_case: (theory -> 'a) -> (Proof.state -> 'a) -> state -> 'a |
5828 | 23 |
val theory_of: state -> theory |
16452 | 24 |
val sign_of: state -> theory (*obsolete*) |
17076 | 25 |
val body_context_of: state -> Proof.context |
17208 | 26 |
val no_body_context: state -> state |
5828 | 27 |
val proof_of: state -> Proof.state |
17076 | 28 |
val is_proof: state -> bool |
9453 | 29 |
val enter_forward_proof: state -> Proof.state |
16815 | 30 |
val prompt_state_default: state -> string |
31 |
val prompt_state_fn: (state -> string) ref |
|
32 |
val print_state_context: state -> unit |
|
33 |
val print_state_default: bool -> state -> unit |
|
34 |
val print_state_hook: (bool -> state -> unit) -> unit |
|
35 |
val print_state_fn: (bool -> state -> unit) ref |
|
36 |
val print_state: bool -> state -> unit |
|
37 |
val pretty_state: bool -> state -> Pretty.T list |
|
16682 | 38 |
val quiet: bool ref |
39 |
val debug: bool ref |
|
40 |
val timing: bool ref |
|
41 |
val profiling: int ref |
|
16815 | 42 |
val skip_proofs: bool ref |
5828 | 43 |
exception TERMINATE |
5990 | 44 |
exception RESTART |
16682 | 45 |
type transition |
6689 | 46 |
val undo_limit: bool -> int option |
5828 | 47 |
val empty: transition |
14923 | 48 |
val name_of: transition -> string |
49 |
val source_of: transition -> OuterLex.token list option |
|
5828 | 50 |
val name: string -> transition -> transition |
51 |
val position: Position.T -> transition -> transition |
|
14923 | 52 |
val source: OuterLex.token list -> transition -> transition |
5828 | 53 |
val interactive: bool -> transition -> transition |
54 |
val print: transition -> transition |
|
16607 | 55 |
val print': string -> transition -> transition |
9010 | 56 |
val no_timing: transition -> transition |
5828 | 57 |
val reset: transition -> transition |
8465 | 58 |
val init: (bool -> node) -> (node -> unit) -> (node -> unit) -> transition -> transition |
6689 | 59 |
val exit: transition -> transition |
60 |
val kill: transition -> transition |
|
5828 | 61 |
val keep: (state -> unit) -> transition -> transition |
7612 | 62 |
val keep': (bool -> state -> unit) -> transition -> transition |
6689 | 63 |
val history: (node History.T -> node History.T) -> transition -> transition |
5828 | 64 |
val imperative: (unit -> unit) -> transition -> transition |
7105 | 65 |
val init_theory: (bool -> theory) -> (theory -> unit) -> (theory -> unit) |
6689 | 66 |
-> transition -> transition |
5828 | 67 |
val theory: (theory -> theory) -> transition -> transition |
7612 | 68 |
val theory': (bool -> theory -> theory) -> transition -> transition |
17076 | 69 |
val theory_context: (theory -> theory * Proof.context) -> transition -> transition |
6689 | 70 |
val theory_to_proof: (bool -> theory -> ProofHistory.T) -> transition -> transition |
5828 | 71 |
val proof: (ProofHistory.T -> ProofHistory.T) -> transition -> transition |
6689 | 72 |
val proof': (bool -> ProofHistory.T -> ProofHistory.T) -> transition -> transition |
16815 | 73 |
val actual_proof: (ProofHistory.T -> ProofHistory.T) -> transition -> transition |
74 |
val skip_proof: (int History.T -> int History.T) -> transition -> transition |
|
5828 | 75 |
val proof_to_theory: (ProofHistory.T -> theory) -> transition -> transition |
12315 | 76 |
val proof_to_theory': (bool -> ProofHistory.T -> theory) -> transition -> transition |
17115 | 77 |
val proof_to_theory_context: (bool -> ProofHistory.T -> theory * Proof.context) |
17076 | 78 |
-> transition -> transition |
15237
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
79 |
val skip_proof_to_theory: (int History.T -> bool) -> transition -> transition |
9512 | 80 |
val unknown_theory: transition -> transition |
81 |
val unknown_proof: transition -> transition |
|
82 |
val unknown_context: transition -> transition |
|
5922 | 83 |
val exn_message: exn -> string |
5828 | 84 |
val apply: bool -> transition -> state -> (state * (exn * string) option) option |
17266 | 85 |
val command: transition -> state -> state |
17076 | 86 |
val present_excursion: (transition * (state -> state -> 'a -> 'a)) list -> 'a -> 'a |
5828 | 87 |
val excursion: transition list -> unit |
88 |
val set_state: state -> unit |
|
89 |
val get_state: unit -> state |
|
90 |
val exn: unit -> (exn * string) option |
|
91 |
val >> : transition -> bool |
|
14985 | 92 |
val >>> : transition list -> unit |
14091 | 93 |
type 'a isar |
94 |
val loop: 'a isar -> unit |
|
5828 | 95 |
end; |
96 |
||
6965 | 97 |
structure Toplevel: TOPLEVEL = |
5828 | 98 |
struct |
99 |
||
100 |
||
101 |
(** toplevel state **) |
|
102 |
||
16815 | 103 |
(* datatype state *) |
5828 | 104 |
|
105 |
datatype node = |
|
17076 | 106 |
Theory of theory * Proof.context option | (*theory with optional body context*) |
107 |
Proof of ProofHistory.T | (*history of proof states*) |
|
108 |
SkipProof of int History.T * theory; (*history of proof depths*) |
|
5828 | 109 |
|
8465 | 110 |
datatype state = State of (node History.T * ((node -> unit) * (node -> unit))) option; |
5828 | 111 |
|
15531 | 112 |
val toplevel = State NONE; |
5828 | 113 |
|
15531 | 114 |
fun is_toplevel (State NONE) = true |
7732 | 115 |
| is_toplevel _ = false; |
116 |
||
17076 | 117 |
fun level (State NONE) = 0 |
118 |
| level (State (SOME (node, _))) = |
|
119 |
(case History.current node of |
|
120 |
Theory _ => 0 |
|
121 |
| Proof prf => Proof.level (ProofHistory.current prf) |
|
122 |
| SkipProof (h, _) => History.current h + 1); (*different notion of proof depth!*) |
|
123 |
||
15531 | 124 |
fun str_of_state (State NONE) = "at top level" |
16815 | 125 |
| str_of_state (State (SOME (node, _))) = |
126 |
(case History.current node of |
|
127 |
Theory _ => "in theory mode" |
|
128 |
| Proof _ => "in proof mode" |
|
129 |
| SkipProof _ => "in skipped proof mode"); |
|
5946
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
130 |
|
a4600d21b59b
print_state hook, obeys Goals.current_goals_markers by default;
wenzelm
parents:
5939
diff
changeset
|
131 |
|
5828 | 132 |
(* top node *) |
133 |
||
6689 | 134 |
exception UNDEF; |
135 |
||
17266 | 136 |
fun assert true = () |
137 |
| assert false = raise UNDEF; |
|
138 |
||
15531 | 139 |
fun node_history_of (State NONE) = raise UNDEF |
140 |
| node_history_of (State (SOME (node, _))) = node; |
|
6689 | 141 |
|
142 |
val node_of = History.current o node_history_of; |
|
5828 | 143 |
|
6664 | 144 |
fun node_case f g state = |
5828 | 145 |
(case node_of state of |
17076 | 146 |
Theory (thy, _) => f thy |
16815 | 147 |
| Proof prf => g (ProofHistory.current prf) |
148 |
| SkipProof (_, thy) => f thy); |
|
5828 | 149 |
|
6664 | 150 |
val theory_of = node_case I Proof.theory_of; |
16452 | 151 |
val sign_of = theory_of; |
17076 | 152 |
|
153 |
fun body_context_of state = |
|
154 |
(case node_of state of |
|
155 |
Theory (_, SOME ctxt) => ctxt |
|
156 |
| _ => node_case ProofContext.init Proof.context_of state); |
|
157 |
||
17208 | 158 |
fun no_body_context (State NONE) = State NONE |
159 |
| no_body_context (State (SOME (node, x))) = |
|
160 |
State (SOME (History.apply (fn Theory (thy, _) => Theory (thy, NONE) | nd => nd) node, x)); |
|
161 |
||
6664 | 162 |
val proof_of = node_case (fn _ => raise UNDEF) I; |
17076 | 163 |
val is_proof = can proof_of; |
6664 | 164 |
|
17115 | 165 |
val enter_forward_proof = node_case Proof.init Proof.enter_forward; |
5828 | 166 |
|
167 |
||
16815 | 168 |
(* prompt state *) |
169 |
||
170 |
fun prompt_state_default (State _) = Source.default_prompt; |
|
171 |
||
172 |
val prompt_state_fn = ref prompt_state_default; |
|
173 |
fun prompt_state state = ! prompt_state_fn state; |
|
174 |
||
175 |
||
176 |
(* print state *) |
|
177 |
||
178 |
fun pretty_context thy = [Pretty.block |
|
179 |
[Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy), |
|
180 |
Pretty.str " =", Pretty.brk 1, ThyInfo.pretty_theory thy]]; |
|
181 |
||
182 |
fun pretty_state_context state = |
|
183 |
(case try theory_of state of NONE => [] |
|
184 |
| SOME thy => pretty_context thy); |
|
185 |
||
17076 | 186 |
fun pretty_node prf_only (Theory (thy, _)) = if prf_only then [] else pretty_context thy |
16815 | 187 |
| pretty_node _ (Proof prf) = |
188 |
Proof.pretty_state (ProofHistory.position prf) (ProofHistory.current prf) |
|
189 |
| pretty_node _ (SkipProof (h, _)) = |
|
190 |
[Pretty.str ("skipped proof: depth " ^ string_of_int (History.current h))]; |
|
191 |
||
192 |
fun pretty_state prf_only state = |
|
193 |
let val ref (begin_state, end_state, _) = Display.current_goals_markers in |
|
194 |
(case try node_of state of NONE => [] |
|
195 |
| SOME node => |
|
196 |
(if begin_state = "" then [] else [Pretty.str begin_state]) @ |
|
197 |
pretty_node prf_only node @ |
|
198 |
(if end_state = "" then [] else [Pretty.str end_state])) |
|
199 |
end; |
|
200 |
||
201 |
val print_state_context = Pretty.writelns o pretty_state_context; |
|
202 |
fun print_state_default prf_only state = Pretty.writelns (pretty_state prf_only state); |
|
203 |
||
204 |
val print_state_hooks = ref ([]: (bool -> state -> unit) list); |
|
205 |
fun print_state_hook f = change print_state_hooks (cons f); |
|
206 |
val print_state_fn = ref print_state_default; |
|
207 |
||
208 |
fun print_state prf_only state = |
|
209 |
(List.app (fn f => (try (fn () => f prf_only state) (); ())) (! print_state_hooks); |
|
210 |
! print_state_fn prf_only state); |
|
211 |
||
212 |
||
15668 | 213 |
|
5828 | 214 |
(** toplevel transitions **) |
215 |
||
16682 | 216 |
val quiet = ref false; |
217 |
val debug = ref false; |
|
218 |
val timing = Output.timing; |
|
219 |
val profiling = ref 0; |
|
16815 | 220 |
val skip_proofs = ref false; |
16682 | 221 |
|
5828 | 222 |
exception TERMINATE; |
5990 | 223 |
exception RESTART; |
7022 | 224 |
exception EXCURSION_FAIL of exn * string; |
6689 | 225 |
exception FAILURE of state * exn; |
5828 | 226 |
|
227 |
||
16815 | 228 |
(* node transactions and recovery from stale theories *) |
6689 | 229 |
|
16815 | 230 |
(*NB: proof commands should be non-destructive!*) |
7022 | 231 |
|
6689 | 232 |
local |
233 |
||
16452 | 234 |
fun is_stale state = Context.is_stale (theory_of state) handle UNDEF => false; |
6689 | 235 |
|
16815 | 236 |
val stale_theory = ERROR_MESSAGE "Stale theory encountered after succesful execution!"; |
237 |
||
17076 | 238 |
fun checkpoint_node true (Theory (thy, ctxt)) = Theory (Theory.checkpoint thy, ctxt) |
6689 | 239 |
| checkpoint_node _ node = node; |
240 |
||
17076 | 241 |
fun copy_node true (Theory (thy, ctxt)) = Theory (Theory.copy thy, ctxt) |
6689 | 242 |
| copy_node _ node = node; |
243 |
||
15531 | 244 |
fun return (result, NONE) = result |
245 |
| return (result, SOME exn) = raise FAILURE (result, exn); |
|
7022 | 246 |
|
16729 | 247 |
fun debug_trans f x = |
248 |
if ! debug then |
|
16815 | 249 |
setmp Output.transform_exceptions false |
250 |
exception_trace (fn () => f x) |
|
16729 | 251 |
else f x; |
252 |
||
253 |
fun interruptible f x = |
|
254 |
let val y = ref x |
|
255 |
in raise_interrupt (fn () => y := f x) (); ! y end; |
|
256 |
||
6689 | 257 |
in |
258 |
||
16815 | 259 |
fun transaction _ _ _ (State NONE) = raise UNDEF |
260 |
| transaction int hist f (State (SOME (node, term))) = |
|
6689 | 261 |
let |
16046
7d68cd1b1b8b
node_trans: revert to original transaction code (pre 1.54);
wenzelm
parents:
15973
diff
changeset
|
262 |
val cont_node = History.map (checkpoint_node int) node; |
7d68cd1b1b8b
node_trans: revert to original transaction code (pre 1.54);
wenzelm
parents:
15973
diff
changeset
|
263 |
val back_node = History.map (copy_node int) cont_node; |
16729 | 264 |
fun state nd = State (SOME (nd, term)); |
265 |
fun normal_state nd = (state nd, NONE); |
|
266 |
fun error_state nd exn = (state nd, SOME exn); |
|
6689 | 267 |
|
16729 | 268 |
val (result, err) = |
269 |
cont_node |
|
270 |
|> ((fn nd => f int nd) |
|
271 |
|> (if hist then History.apply_copy (copy_node int) else History.map) |
|
272 |
|> debug_trans |
|
273 |
|> interruptible |
|
274 |
|> transform_error) |
|
275 |
|> normal_state |
|
276 |
handle exn => error_state cont_node exn; |
|
6689 | 277 |
in |
16729 | 278 |
if is_stale result |
279 |
then return (error_state back_node (if_none err stale_theory)) |
|
280 |
else return (result, err) |
|
6689 | 281 |
end; |
282 |
||
283 |
end; |
|
284 |
||
285 |
||
286 |
(* primitive transitions *) |
|
287 |
||
16815 | 288 |
(*NB: recovery from stale theories is provided only for theory-level |
289 |
operations via MapCurrent and AppCurrent. Other node or state |
|
290 |
operations should not touch theories at all. |
|
6965 | 291 |
|
16815 | 292 |
Interrupts are enabled only for Keep, MapCurrent, and AppCurrent.*) |
5828 | 293 |
|
294 |
datatype trans = |
|
6689 | 295 |
Reset | (*empty toplevel*) |
8465 | 296 |
Init of (bool -> node) * ((node -> unit) * (node -> unit)) | |
297 |
(*init node; provide exit/kill operation*) |
|
298 |
Exit | (*conclude node*) |
|
6689 | 299 |
Kill | (*abort node*) |
7612 | 300 |
Keep of bool -> state -> unit | (*peek at state*) |
6689 | 301 |
History of node History.T -> node History.T | (*history operation (undo etc.)*) |
302 |
MapCurrent of bool -> node -> node | (*change node, bypassing history*) |
|
303 |
AppCurrent of bool -> node -> node; (*change node, recording history*) |
|
304 |
||
15531 | 305 |
fun undo_limit int = if int then NONE else SOME 0; |
6689 | 306 |
|
307 |
local |
|
5828 | 308 |
|
6689 | 309 |
fun apply_tr _ Reset _ = toplevel |
15531 | 310 |
| apply_tr int (Init (f, term)) (State NONE) = |
311 |
State (SOME (History.init (undo_limit int) (f int), term)) |
|
312 |
| apply_tr _ (Init _ ) (State (SOME _)) = raise UNDEF |
|
313 |
| apply_tr _ Exit (State NONE) = raise UNDEF |
|
314 |
| apply_tr _ Exit (State (SOME (node, (exit, _)))) = |
|
315 |
(exit (History.current node); State NONE) |
|
316 |
| apply_tr _ Kill (State NONE) = raise UNDEF |
|
317 |
| apply_tr _ Kill (State (SOME (node, (_, kill)))) = |
|
318 |
(kill (History.current node); State NONE) |
|
12987 | 319 |
| apply_tr int (Keep f) state = (raise_interrupt (f int) state; state) |
15531 | 320 |
| apply_tr _ (History _) (State NONE) = raise UNDEF |
321 |
| apply_tr _ (History f) (State (SOME (node, term))) = State (SOME (f node, term)) |
|
16815 | 322 |
| apply_tr int (MapCurrent f) state = transaction int false f state |
323 |
| apply_tr int (AppCurrent f) state = transaction int true f state; |
|
5828 | 324 |
|
6689 | 325 |
fun apply_union _ [] state = raise FAILURE (state, UNDEF) |
326 |
| apply_union int (tr :: trs) state = |
|
327 |
transform_error (apply_tr int tr) state |
|
328 |
handle UNDEF => apply_union int trs state |
|
329 |
| FAILURE (alt_state, UNDEF) => apply_union int trs alt_state |
|
330 |
| exn as FAILURE _ => raise exn |
|
331 |
| exn => raise FAILURE (state, exn); |
|
332 |
||
333 |
in |
|
334 |
||
15531 | 335 |
fun apply_trans int trs state = (apply_union int trs state, NONE) |
336 |
handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn); |
|
6689 | 337 |
|
338 |
end; |
|
5828 | 339 |
|
340 |
||
341 |
(* datatype transition *) |
|
342 |
||
343 |
datatype transition = Transition of |
|
16815 | 344 |
{name: string, (*command name*) |
345 |
pos: Position.T, (*source position*) |
|
346 |
source: OuterLex.token list option, (*source text*) |
|
347 |
int_only: bool, (*interactive-only*) |
|
348 |
print: string list, (*print modes (union)*) |
|
349 |
no_timing: bool, (*suppress timing*) |
|
350 |
trans: trans list}; (*primitive transitions (union)*) |
|
5828 | 351 |
|
14923 | 352 |
fun make_transition (name, pos, source, int_only, print, no_timing, trans) = |
353 |
Transition {name = name, pos = pos, source = source, |
|
354 |
int_only = int_only, print = print, no_timing = no_timing, trans = trans}; |
|
5828 | 355 |
|
14923 | 356 |
fun map_transition f (Transition {name, pos, source, int_only, print, no_timing, trans}) = |
357 |
make_transition (f (name, pos, source, int_only, print, no_timing, trans)); |
|
5828 | 358 |
|
16607 | 359 |
val empty = make_transition ("<unknown>", Position.none, NONE, false, [], false, []); |
14923 | 360 |
|
361 |
fun name_of (Transition {name, ...}) = name; |
|
362 |
fun source_of (Transition {source, ...}) = source; |
|
5828 | 363 |
|
364 |
||
365 |
(* diagnostics *) |
|
366 |
||
367 |
fun str_of_transition (Transition {name, pos, ...}) = quote name ^ Position.str_of pos; |
|
368 |
||
369 |
fun command_msg msg tr = msg ^ "command " ^ str_of_transition tr; |
|
370 |
fun at_command tr = command_msg "At " tr ^ "."; |
|
371 |
||
372 |
fun type_error tr state = |
|
6689 | 373 |
ERROR_MESSAGE (command_msg "Illegal application of " tr ^ " " ^ str_of_state state); |
5828 | 374 |
|
375 |
||
376 |
(* modify transitions *) |
|
377 |
||
14923 | 378 |
fun name nm = map_transition (fn (_, pos, source, int_only, print, no_timing, trans) => |
379 |
(nm, pos, source, int_only, print, no_timing, trans)); |
|
5828 | 380 |
|
14923 | 381 |
fun position pos = map_transition (fn (name, _, source, int_only, print, no_timing, trans) => |
382 |
(name, pos, source, int_only, print, no_timing, trans)); |
|
9010 | 383 |
|
14923 | 384 |
fun source src = map_transition (fn (name, pos, _, int_only, print, no_timing, trans) => |
15531 | 385 |
(name, pos, SOME src, int_only, print, no_timing, trans)); |
5828 | 386 |
|
14923 | 387 |
fun interactive int_only = map_transition (fn (name, pos, source, _, print, no_timing, trans) => |
388 |
(name, pos, source, int_only, print, no_timing, trans)); |
|
389 |
||
16607 | 390 |
fun print' mode = map_transition (fn (name, pos, source, int_only, print, no_timing, trans) => |
391 |
(name, pos, source, int_only, insert (op =) mode print, no_timing, trans)); |
|
392 |
||
393 |
val print = print' ""; |
|
5828 | 394 |
|
14923 | 395 |
val no_timing = map_transition (fn (name, pos, source, int_only, print, _, trans) => |
396 |
(name, pos, source, int_only, print, true, trans)); |
|
5828 | 397 |
|
14923 | 398 |
fun add_trans tr = map_transition (fn (name, pos, source, int_only, print, no_timing, trans) => |
399 |
(name, pos, source, int_only, print, no_timing, trans @ [tr])); |
|
5828 | 400 |
|
401 |
||
402 |
(* build transitions *) |
|
403 |
||
404 |
val reset = add_trans Reset; |
|
6689 | 405 |
fun init f exit kill = add_trans (Init (f, (exit, kill))); |
406 |
val exit = add_trans Exit; |
|
407 |
val kill = add_trans Kill; |
|
7612 | 408 |
val keep' = add_trans o Keep; |
6689 | 409 |
val history = add_trans o History; |
5828 | 410 |
val map_current = add_trans o MapCurrent; |
6689 | 411 |
val app_current = add_trans o AppCurrent; |
5828 | 412 |
|
7612 | 413 |
fun keep f = add_trans (Keep (fn _ => f)); |
5828 | 414 |
fun imperative f = keep (fn _ => f ()); |
415 |
||
6689 | 416 |
fun init_theory f exit kill = |
17076 | 417 |
init (fn int => Theory (f int, NONE)) |
418 |
(fn Theory (thy, _) => exit thy | _ => raise UNDEF) |
|
419 |
(fn Theory (thy, _) => kill thy | _ => raise UNDEF); |
|
5828 | 420 |
|
15668 | 421 |
|
422 |
(* typed transitions *) |
|
423 |
||
17076 | 424 |
fun theory f = app_current (K (fn Theory (thy, _) => Theory (f thy, NONE) | _ => raise UNDEF)); |
425 |
fun theory' f = app_current (fn int => |
|
426 |
(fn Theory (thy, _) => Theory (f int thy, NONE) | _ => raise UNDEF)); |
|
427 |
||
428 |
fun theory_context f = |
|
429 |
app_current (K (fn Theory (thy, _) => Theory (apsnd SOME (f thy)) | _ => raise UNDEF)); |
|
15668 | 430 |
|
15237
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
431 |
fun theory_to_proof f = app_current (fn int => |
17076 | 432 |
(fn Theory (thy, _) => |
15668 | 433 |
if ! skip_proofs then SkipProof (History.init (undo_limit int) 0, |
17115 | 434 |
#1 (#1 (SkipProof.global_skip_proof int (ProofHistory.current (f int thy))))) |
15237
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
435 |
else Proof (f int thy) |
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
436 |
| _ => raise UNDEF)); |
15668 | 437 |
|
16815 | 438 |
fun proof' f = map_current (fn int => |
439 |
(fn Proof prf => Proof (f int prf) |
|
440 |
| SkipProof (h, thy) => SkipProof (History.apply I h, thy) (*approximate f*) |
|
441 |
| _ => raise UNDEF)); |
|
15668 | 442 |
|
6689 | 443 |
val proof = proof' o K; |
16815 | 444 |
|
445 |
fun actual_proof f = map_current (fn _ => |
|
446 |
(fn Proof prf => Proof (f prf) | _ => raise UNDEF)); |
|
447 |
||
448 |
fun skip_proof f = map_current (fn _ => |
|
449 |
(fn SkipProof (h, thy) => SkipProof (f h, thy) | _ => raise UNDEF)); |
|
15668 | 450 |
|
17076 | 451 |
fun end_proof f = |
15237
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
452 |
map_current (fn int => (fn Proof prf => Theory (f int prf) |
17076 | 453 |
| SkipProof (h, thy) => if History.current h = 0 then Theory (thy, NONE) else raise UNDEF |
15237
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
454 |
| _ => raise UNDEF)); |
15668 | 455 |
|
17076 | 456 |
fun proof_to_theory' f = end_proof (rpair NONE oo f); |
457 |
fun proof_to_theory f = proof_to_theory' (K f); |
|
17115 | 458 |
fun proof_to_theory_context f = end_proof (apsnd SOME oo f); |
15668 | 459 |
|
16815 | 460 |
fun skip_proof_to_theory p = map_current (fn _ => |
17076 | 461 |
(fn SkipProof (h, thy) => if p h then Theory (thy, NONE) else raise UNDEF | _ => raise UNDEF)); |
5828 | 462 |
|
9512 | 463 |
val unknown_theory = imperative (fn () => warning "Unknown theory context"); |
464 |
val unknown_proof = imperative (fn () => warning "Unknown proof context"); |
|
465 |
val unknown_context = imperative (fn () => warning "Unknown context"); |
|
466 |
||
5828 | 467 |
|
468 |
||
469 |
(** toplevel transactions **) |
|
470 |
||
471 |
(* print exceptions *) |
|
472 |
||
15668 | 473 |
local |
474 |
||
475 |
fun with_context f xs = |
|
476 |
(case Context.get_context () of NONE => [] |
|
16452 | 477 |
| SOME thy => map (f thy) xs); |
15668 | 478 |
|
479 |
fun raised name [] = "exception " ^ name ^ " raised" |
|
480 |
| raised name [msg] = "exception " ^ name ^ " raised: " ^ msg |
|
481 |
| raised name msgs = cat_lines (("exception " ^ name ^ " raised:") :: msgs); |
|
5828 | 482 |
|
15668 | 483 |
fun exn_msg _ TERMINATE = "Exit." |
484 |
| exn_msg _ RESTART = "Restart." |
|
485 |
| exn_msg _ Interrupt = "Interrupt." |
|
486 |
| exn_msg _ ERROR = "ERROR." |
|
487 |
| exn_msg _ (ERROR_MESSAGE msg) = msg |
|
16452 | 488 |
| exn_msg detailed (EXCURSION_FAIL (exn, msg)) = cat_lines [exn_msg detailed exn, msg] |
489 |
| exn_msg detailed (Context.DATA_FAIL (exn, msg)) = cat_lines [exn_msg detailed exn, msg] |
|
16607 | 490 |
| exn_msg detailed (Syntax.TRANSLATION_FAIL (exn, msg)) = cat_lines [exn_msg detailed exn, msg] |
15668 | 491 |
| exn_msg false (THEORY (msg, _)) = msg |
16452 | 492 |
| exn_msg true (THEORY (msg, thys)) = raised "THEORY" (msg :: map Context.str_of_thy thys) |
15668 | 493 |
| exn_msg _ (ProofContext.CONTEXT (msg, _)) = msg |
494 |
| exn_msg _ (Proof.STATE (msg, _)) = msg |
|
495 |
| exn_msg _ (ProofHistory.FAIL msg) = msg |
|
496 |
| exn_msg detailed (MetaSimplifier.SIMPROC_FAIL (name, exn)) = |
|
497 |
fail_msg detailed "simproc" ((name, Position.none), exn) |
|
498 |
| exn_msg detailed (Attrib.ATTRIB_FAIL info) = fail_msg detailed "attribute" info |
|
499 |
| exn_msg detailed (Method.METHOD_FAIL info) = fail_msg detailed "method" info |
|
500 |
| exn_msg detailed (Antiquote.ANTIQUOTE_FAIL info) = fail_msg detailed "antiquotation" info |
|
501 |
| exn_msg false (Syntax.AST (msg, _)) = raised "AST" [msg] |
|
502 |
| exn_msg true (Syntax.AST (msg, asts)) = |
|
503 |
raised "AST" (msg :: map (Pretty.string_of o Syntax.pretty_ast) asts) |
|
504 |
| exn_msg false (TYPE (msg, _, _)) = raised "TYPE" [msg] |
|
505 |
| exn_msg true (TYPE (msg, Ts, ts)) = raised "TYPE" (msg :: |
|
506 |
with_context Sign.string_of_typ Ts @ with_context Sign.string_of_term ts) |
|
507 |
| exn_msg false (TERM (msg, _)) = raised "TERM" [msg] |
|
508 |
| exn_msg true (TERM (msg, ts)) = raised "TERM" (msg :: with_context Sign.string_of_term ts) |
|
509 |
| exn_msg false (THM (msg, _, _)) = raised "THM" [msg] |
|
510 |
| exn_msg true (THM (msg, i, thms)) = |
|
511 |
raised ("THM " ^ string_of_int i) (msg :: map Display.string_of_thm thms) |
|
512 |
| exn_msg _ Option = raised "Option" [] |
|
513 |
| exn_msg _ UnequalLengths = raised "UnequalLengths" [] |
|
514 |
| exn_msg _ Empty = raised "Empty" [] |
|
515 |
| exn_msg _ Subscript = raised "Subscript" [] |
|
16197 | 516 |
| exn_msg _ (Fail msg) = raised "Fail" [msg] |
15668 | 517 |
| exn_msg _ exn = General.exnMessage exn |
518 |
and fail_msg detailed kind ((name, pos), exn) = |
|
519 |
"Error in " ^ kind ^ " " ^ quote name ^ Position.str_of pos ^ ":\n" ^ exn_msg detailed exn; |
|
15519 | 520 |
|
15668 | 521 |
in |
522 |
||
523 |
fun exn_message exn = exn_msg (! debug) exn; |
|
5828 | 524 |
|
15531 | 525 |
fun print_exn NONE = () |
526 |
| print_exn (SOME (exn, s)) = error_msg (cat_lines [exn_message exn, s]); |
|
5828 | 527 |
|
15668 | 528 |
end; |
529 |
||
5828 | 530 |
|
531 |
(* apply transitions *) |
|
532 |
||
6664 | 533 |
local |
534 |
||
9010 | 535 |
fun app int (tr as Transition {trans, int_only, print, no_timing, ...}) state = |
5828 | 536 |
let |
16682 | 537 |
val _ = conditional (not int andalso int_only) (fn () => |
538 |
warning (command_msg "Interactive-only " tr)); |
|
539 |
||
540 |
fun do_timing f x = (info (command_msg "" tr); timeap f x); |
|
541 |
fun do_profiling f x = profile (! profiling) f x; |
|
542 |
||
6689 | 543 |
val (result, opt_exn) = |
16729 | 544 |
state |> (apply_trans int trans |
545 |
|> (if ! profiling > 0 then do_profiling else I) |
|
546 |
|> (if ! profiling > 0 orelse ! timing andalso not no_timing then do_timing else I)); |
|
16607 | 547 |
val _ = conditional (int andalso not (! quiet) andalso |
548 |
exists (fn m => m mem_string print) ("" :: ! print_mode)) |
|
549 |
(fn () => print_state false result); |
|
15570 | 550 |
in (result, Option.map (fn UNDEF => type_error tr state | exn => exn) opt_exn) end; |
6664 | 551 |
|
552 |
in |
|
5828 | 553 |
|
6689 | 554 |
fun apply int tr st = |
6965 | 555 |
(case app int tr st of |
15531 | 556 |
(_, SOME TERMINATE) => NONE |
557 |
| (_, SOME RESTART) => SOME (toplevel, NONE) |
|
558 |
| (state', SOME (EXCURSION_FAIL exn_info)) => SOME (state', SOME exn_info) |
|
559 |
| (state', SOME exn) => SOME (state', SOME (exn, at_command tr)) |
|
560 |
| (state', NONE) => SOME (state', NONE)); |
|
6664 | 561 |
|
17266 | 562 |
fun command tr st = |
563 |
(case apply false tr st of |
|
564 |
SOME (st', NONE) => st' |
|
565 |
| SOME (st', SOME exn_info) => raise EXCURSION_FAIL exn_info |
|
566 |
| NONE => raise EXCURSION_FAIL (TERMINATE, at_command tr)); |
|
567 |
||
6664 | 568 |
end; |
5828 | 569 |
|
570 |
||
17076 | 571 |
(* excursion: toplevel -- apply transformers/presentation -- toplevel *) |
5828 | 572 |
|
6664 | 573 |
local |
574 |
||
5828 | 575 |
fun excur [] x = x |
17076 | 576 |
| excur ((tr, pr) :: trs) (st, res) = |
17320 | 577 |
(case apply (! debug) tr st of |
15531 | 578 |
SOME (st', NONE) => |
17076 | 579 |
excur trs (st', transform_error (fn () => pr st st' res) () handle exn => |
10324 | 580 |
raise EXCURSION_FAIL (exn, "Presentation failed\n" ^ at_command tr)) |
15531 | 581 |
| SOME (st', SOME exn_info) => raise EXCURSION_FAIL exn_info |
582 |
| NONE => raise EXCURSION_FAIL (TERMINATE, at_command tr)); |
|
5828 | 583 |
|
17076 | 584 |
fun no_pr _ _ _ = (); |
585 |
||
6664 | 586 |
in |
587 |
||
17076 | 588 |
fun present_excursion trs res = |
15531 | 589 |
(case excur trs (State NONE, res) of |
590 |
(State NONE, res') => res' |
|
9134 | 591 |
| _ => raise ERROR_MESSAGE "Unfinished development at end of input") |
592 |
handle exn => error (exn_message exn); |
|
593 |
||
17076 | 594 |
fun excursion trs = present_excursion (map (rpair no_pr) trs) (); |
7062 | 595 |
|
6664 | 596 |
end; |
597 |
||
5828 | 598 |
|
599 |
||
600 |
(** interactive transformations **) |
|
601 |
||
602 |
(* the global state reference *) |
|
603 |
||
15531 | 604 |
val global_state = ref (toplevel, NONE: (exn * string) option); |
5828 | 605 |
|
15531 | 606 |
fun set_state state = global_state := (state, NONE); |
5828 | 607 |
fun get_state () = fst (! global_state); |
608 |
fun exn () = snd (! global_state); |
|
609 |
||
610 |
||
6965 | 611 |
(* the Isar source of transitions *) |
612 |
||
14091 | 613 |
type 'a isar = |
6965 | 614 |
(transition, (transition option, |
12881 | 615 |
(OuterLex.token, (OuterLex.token option, (OuterLex.token, (OuterLex.token, |
14091 | 616 |
Position.T * (Symbol.symbol, (string, 'a) Source.source) Source.source) |
12881 | 617 |
Source.source) Source.source) Source.source) Source.source) Source.source) Source.source; |
6965 | 618 |
|
619 |
||
5828 | 620 |
(* apply transformers to global state *) |
621 |
||
14985 | 622 |
nonfix >> >>>; |
5828 | 623 |
|
624 |
fun >> tr = |
|
625 |
(case apply true tr (get_state ()) of |
|
15531 | 626 |
NONE => false |
627 |
| SOME (state', exn_info) => |
|
5828 | 628 |
(global_state := (state', exn_info); |
16729 | 629 |
print_exn exn_info; |
5828 | 630 |
true)); |
631 |
||
14985 | 632 |
fun >>> [] = () |
633 |
| >>> (tr :: trs) = if >> tr then >>> trs else (); |
|
634 |
||
7602 | 635 |
(*Note: this is for Poly/ML only, we really do not intend to exhibit |
636 |
interrupts here*) |
|
15531 | 637 |
fun get_interrupt src = SOME (Source.get_single src) handle Interrupt => NONE; |
7602 | 638 |
|
5828 | 639 |
fun raw_loop src = |
7602 | 640 |
(case get_interrupt (Source.set_prompt (prompt_state (get_state ())) src) of |
15531 | 641 |
NONE => (writeln "\nInterrupt."; raw_loop src) |
642 |
| SOME NONE => () |
|
643 |
| SOME (SOME (tr, src')) => if >> tr then raw_loop src' else ()); |
|
5828 | 644 |
|
12987 | 645 |
fun loop src = ignore_interrupt raw_loop src; |
5828 | 646 |
|
647 |
end; |