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