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