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