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