author | paulson |
Sat, 26 Mar 2005 18:20:29 +0100 | |
changeset 15633 | 741deccec4e3 |
parent 15598 | 4ab52355bb53 |
child 15668 | 53c049a365cf |
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 |
||
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 |
||
6689 | 187 |
(* recovery from stale signatures *) |
188 |
||
7022 | 189 |
(*note: proof commands should be non-destructive!*) |
190 |
||
6689 | 191 |
local |
192 |
||
193 |
fun is_stale state = Sign.is_stale (sign_of state) handle UNDEF => false; |
|
194 |
||
195 |
fun checkpoint_node true (Theory thy) = Theory (PureThy.checkpoint thy) |
|
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); |
15570 | 203 |
in raise_interrupt (fn () => y := SOME (f x)) (); valOf (! y) end; |
6965 | 204 |
|
7022 | 205 |
val rollback = ERROR_MESSAGE "Stale signature encountered after succesful execution!"; |
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 |
|
15549
14d167259812
Modified node_trans to avoid duplication of signature stamps
berghofe
parents:
15531
diff
changeset
|
217 |
val f' = checkpoint_node int o f int o copy_node int; |
6689 | 218 |
|
15549
14d167259812
Modified node_trans to avoid duplication of signature stamps
berghofe
parents:
15531
diff
changeset
|
219 |
val trans = if hist then History.apply else History.map; |
6965 | 220 |
val (result, opt_exn) = |
15549
14d167259812
Modified node_trans to avoid duplication of signature stamps
berghofe
parents:
15531
diff
changeset
|
221 |
(mk_state (transform_error (interruptible (trans f')) node), NONE) |
14d167259812
Modified node_trans to avoid duplication of signature stamps
berghofe
parents:
15531
diff
changeset
|
222 |
handle exn => (mk_state node, SOME exn); |
6689 | 223 |
in |
15570 | 224 |
if is_stale result then return (mk_state node, SOME (getOpt (opt_exn, rollback))) |
7022 | 225 |
else return (result, opt_exn) |
6689 | 226 |
end; |
227 |
||
228 |
fun check_stale state = |
|
229 |
if not (is_stale state) then () |
|
7022 | 230 |
else warning "Stale signature encountered! Should restart current theory."; |
6689 | 231 |
|
232 |
end; |
|
233 |
||
234 |
||
235 |
(* primitive transitions *) |
|
236 |
||
237 |
(*Important note: recovery from stale signatures is provided only for |
|
238 |
theory-level operations via MapCurrent and AppCurrent. Other node |
|
6965 | 239 |
or state operations should not touch signatures at all. |
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 |
|
15237
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
369 |
(* |
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
370 |
The skip_proofs flag allows proof scripts to be skipped during interactive |
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
371 |
proof in order to speed up processing of large theories. While in skipping |
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
372 |
mode, states are represented as SkipProof (h, thy), where h contains a |
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
373 |
counter for the number of open proof blocks. |
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
374 |
*) |
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
375 |
|
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
376 |
val skip_proofs = ref false; |
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
377 |
|
6689 | 378 |
fun theory f = app_current (fn _ => (fn Theory thy => Theory (f thy) | _ => raise UNDEF)); |
7612 | 379 |
fun theory' f = app_current (fn int => (fn Theory thy => Theory (f int thy) | _ => raise UNDEF)); |
15237
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
380 |
fun theory_to_proof f = app_current (fn int => |
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
381 |
(fn Theory thy => |
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
382 |
if !skip_proofs then SkipProof (History.init (undo_limit int) 0, |
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
383 |
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
|
384 |
else Proof (f int thy) |
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
385 |
| _ => raise UNDEF)); |
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
386 |
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
|
387 |
| 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
|
388 |
| _ => raise UNDEF)); |
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
389 |
val proof' = proof''' true; |
6689 | 390 |
val proof = proof' o K; |
15237
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
391 |
val proof'' = proof''' false o K; |
12315 | 392 |
fun proof_to_theory' f = |
15237
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
393 |
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
|
394 |
| 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
|
395 |
| _ => raise UNDEF)); |
12315 | 396 |
val proof_to_theory = proof_to_theory' o K; |
15237
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
397 |
fun skip_proof f = map_current (fn int => |
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
398 |
(fn SkipProof (h, thy) => SkipProof (f h, thy) | _ => raise UNDEF)); |
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
399 |
fun skip_proof_to_theory p = map_current (fn int => |
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
14985
diff
changeset
|
400 |
(fn SkipProof (h, thy) => if p h then Theory thy else raise UNDEF | _ => raise UNDEF)); |
5828 | 401 |
|
9512 | 402 |
val unknown_theory = imperative (fn () => warning "Unknown theory context"); |
403 |
val unknown_proof = imperative (fn () => warning "Unknown proof context"); |
|
404 |
val unknown_context = imperative (fn () => warning "Unknown context"); |
|
405 |
||
5828 | 406 |
|
407 |
||
408 |
(** toplevel transactions **) |
|
409 |
||
7198 | 410 |
val quiet = ref false; |
15519 | 411 |
val debug = ref false; (* Verbose messages for core exceptions. *) |
5828 | 412 |
|
413 |
(* print exceptions *) |
|
414 |
||
415 |
fun raised name = "exception " ^ name ^ " raised"; |
|
416 |
fun raised_msg name msg = raised name ^ ": " ^ msg; |
|
417 |
||
15519 | 418 |
fun msg_on_debug (THM (msg, i, thms)) = |
419 |
if !debug |
|
420 |
then raised_msg ("THM " ^ string_of_int i) |
|
421 |
(cat_lines ("" :: msg :: map Display.string_of_thm thms)) |
|
422 |
else raised_msg "THM" msg |
|
423 |
| msg_on_debug (THEORY (msg, thys)) = |
|
424 |
if !debug |
|
425 |
then raised_msg "THEORY" (cat_lines ("" :: msg :: |
|
426 |
map (Pretty.string_of o Display.pretty_theory) thys)) |
|
427 |
else msg |
|
15596 | 428 |
| msg_on_debug (AST (msg, asts)) = |
429 |
if !debug |
|
430 |
then raised_msg "AST" (cat_lines ("" :: msg :: |
|
431 |
map (Pretty.string_of o Syntax.pretty_ast) asts)) |
|
432 |
else msg |
|
15519 | 433 |
| msg_on_debug (TERM (msg, ts)) = |
434 |
(case (!debug, Context.get_context ()) of |
|
15531 | 435 |
(true, SOME thy) => |
15519 | 436 |
let val sg = Theory.sign_of thy in |
437 |
raised_msg "TERM" |
|
438 |
(cat_lines |
|
439 |
("" :: msg :: map (Sign.string_of_term sg) ts)) |
|
440 |
end |
|
441 |
| _ => raised_msg "TERM" msg) |
|
442 |
| msg_on_debug (TYPE (msg, Ts, ts)) = |
|
443 |
(case (!debug, Context.get_context ()) of |
|
15531 | 444 |
(true, SOME thy) => |
15519 | 445 |
let val sg = Theory.sign_of thy in |
446 |
raised_msg "TYPE" |
|
447 |
(cat_lines |
|
448 |
("" :: msg :: map (Sign.string_of_typ sg) Ts @ |
|
449 |
map (Sign.string_of_term sg) ts)) |
|
450 |
end |
|
451 |
| _ => raised_msg "TYPE" msg) |
|
452 |
||
5828 | 453 |
fun exn_message TERMINATE = "Exit." |
5990 | 454 |
| exn_message RESTART = "Restart." |
7022 | 455 |
| exn_message (EXCURSION_FAIL (exn, msg)) = cat_lines [exn_message exn, msg] |
6971 | 456 |
| exn_message Interrupt = "Interrupt." |
6002 | 457 |
| exn_message ERROR = "ERROR." |
5828 | 458 |
| exn_message (ERROR_MESSAGE msg) = msg |
15519 | 459 |
| exn_message (THEORY (msg, thys)) = msg_on_debug (THEORY (msg, thys)) |
5828 | 460 |
| exn_message (ProofContext.CONTEXT (msg, _)) = msg |
461 |
| exn_message (Proof.STATE (msg, _)) = msg |
|
462 |
| exn_message (ProofHistory.FAIL msg) = msg |
|
13486
54464ea94d6f
exception SIMPROC_FAIL: solid error reporting of simprocs;
wenzelm
parents:
12987
diff
changeset
|
463 |
| exn_message (MetaSimplifier.SIMPROC_FAIL (name, exn)) = |
54464ea94d6f
exception SIMPROC_FAIL: solid error reporting of simprocs;
wenzelm
parents:
12987
diff
changeset
|
464 |
fail_message "simproc" ((name, Position.none), exn) |
5920 | 465 |
| exn_message (Attrib.ATTRIB_FAIL info) = fail_message "attribute" info |
466 |
| exn_message (Method.METHOD_FAIL info) = fail_message "method" info |
|
12881 | 467 |
| exn_message (Antiquote.ANTIQUOTE_FAIL info) = fail_message "antiquotation" info |
15596 | 468 |
| exn_message (Syntax.AST (msg, asts)) = msg_on_debug (AST (msg, asts)) |
15519 | 469 |
| exn_message (TYPE (msg, Ts, ts)) = msg_on_debug (TYPE (msg, Ts, ts)) |
470 |
| exn_message (TERM (msg, ts)) = msg_on_debug (TERM (msg, ts)) |
|
471 |
| exn_message (THM (msg, i, thms)) = msg_on_debug (THM (msg, i, thms)) |
|
15531 | 472 |
| exn_message Option = raised "Option" |
15570 | 473 |
| exn_message UnequalLengths = raised "UnequalLengths" |
15598
4ab52355bb53
Registrations of global locale interpretations: improved, better naming.
ballarin
parents:
15596
diff
changeset
|
474 |
| exn_message Empty = raised "Empty" |
4ab52355bb53
Registrations of global locale interpretations: improved, better naming.
ballarin
parents:
15596
diff
changeset
|
475 |
| exn_message Subscript = raised "Subscript" |
5920 | 476 |
| exn_message exn = General.exnMessage exn |
477 |
and fail_message kind ((name, pos), exn) = |
|
9152 | 478 |
"Error in " ^ kind ^ " " ^ quote name ^ Position.str_of pos ^ ":\n" ^ exn_message exn; |
5828 | 479 |
|
15531 | 480 |
fun print_exn NONE = () |
481 |
| print_exn (SOME (exn, s)) = error_msg (cat_lines [exn_message exn, s]); |
|
5828 | 482 |
|
483 |
||
484 |
(* apply transitions *) |
|
485 |
||
6664 | 486 |
local |
487 |
||
9010 | 488 |
fun app int (tr as Transition {trans, int_only, print, no_timing, ...}) state = |
5828 | 489 |
let |
490 |
val _ = |
|
491 |
if int orelse not int_only then () |
|
8722 | 492 |
else warning (command_msg "Interactive-only " tr); |
6689 | 493 |
val (result, opt_exn) = |
14825 | 494 |
(if ! Output.timing andalso not no_timing then (warning (command_msg "" tr); timeap) else I) |
9010 | 495 |
(apply_trans int trans) state; |
8886 | 496 |
val _ = if int andalso print andalso not (! quiet) then print_state false result else (); |
15570 | 497 |
in (result, Option.map (fn UNDEF => type_error tr state | exn => exn) opt_exn) end; |
6664 | 498 |
|
499 |
in |
|
5828 | 500 |
|
6689 | 501 |
fun apply int tr st = |
6965 | 502 |
(case app int tr st of |
15531 | 503 |
(_, SOME TERMINATE) => NONE |
504 |
| (_, SOME RESTART) => SOME (toplevel, NONE) |
|
505 |
| (state', SOME (EXCURSION_FAIL exn_info)) => SOME (state', SOME exn_info) |
|
506 |
| (state', SOME exn) => SOME (state', SOME (exn, at_command tr)) |
|
507 |
| (state', NONE) => SOME (state', NONE)); |
|
6664 | 508 |
|
509 |
end; |
|
5828 | 510 |
|
511 |
||
512 |
(* excursion: toplevel -- apply transformers -- toplevel *) |
|
513 |
||
6664 | 514 |
local |
515 |
||
5828 | 516 |
fun excur [] x = x |
9134 | 517 |
| excur ((tr, f) :: trs) (st, res) = |
518 |
(case apply false tr st of |
|
15531 | 519 |
SOME (st', NONE) => |
15431
6f4959ba7664
excursion_result now also passes previous state to presentation functions.
berghofe
parents:
15237
diff
changeset
|
520 |
excur trs (st', transform_error (fn () => f st st' res) () handle exn => |
10324 | 521 |
raise EXCURSION_FAIL (exn, "Presentation failed\n" ^ at_command tr)) |
15531 | 522 |
| SOME (st', SOME exn_info) => raise EXCURSION_FAIL exn_info |
523 |
| NONE => raise EXCURSION_FAIL (TERMINATE, at_command tr)); |
|
5828 | 524 |
|
6664 | 525 |
in |
526 |
||
9134 | 527 |
fun excursion_result (trs, res) = |
15531 | 528 |
(case excur trs (State NONE, res) of |
529 |
(State NONE, res') => res' |
|
9134 | 530 |
| _ => raise ERROR_MESSAGE "Unfinished development at end of input") |
531 |
handle exn => error (exn_message exn); |
|
532 |
||
5828 | 533 |
fun excursion trs = |
15431
6f4959ba7664
excursion_result now also passes previous state to presentation functions.
berghofe
parents:
15237
diff
changeset
|
534 |
excursion_result (map (fn tr => (tr, fn _ => fn _ => fn _ => ())) trs, ()); |
7062 | 535 |
|
6664 | 536 |
end; |
537 |
||
5828 | 538 |
|
539 |
||
540 |
(** interactive transformations **) |
|
541 |
||
542 |
(* the global state reference *) |
|
543 |
||
15531 | 544 |
val global_state = ref (toplevel, NONE: (exn * string) option); |
5828 | 545 |
|
15531 | 546 |
fun set_state state = global_state := (state, NONE); |
5828 | 547 |
fun get_state () = fst (! global_state); |
548 |
fun exn () = snd (! global_state); |
|
549 |
||
550 |
||
6965 | 551 |
(* the Isar source of transitions *) |
552 |
||
14091 | 553 |
type 'a isar = |
6965 | 554 |
(transition, (transition option, |
12881 | 555 |
(OuterLex.token, (OuterLex.token option, (OuterLex.token, (OuterLex.token, |
14091 | 556 |
Position.T * (Symbol.symbol, (string, 'a) Source.source) Source.source) |
12881 | 557 |
Source.source) Source.source) Source.source) Source.source) Source.source) Source.source; |
6965 | 558 |
|
559 |
||
5828 | 560 |
(* apply transformers to global state *) |
561 |
||
14985 | 562 |
nonfix >> >>>; |
5828 | 563 |
|
564 |
fun >> tr = |
|
565 |
(case apply true tr (get_state ()) of |
|
15531 | 566 |
NONE => false |
567 |
| SOME (state', exn_info) => |
|
5828 | 568 |
(global_state := (state', exn_info); |
569 |
check_stale state'; print_exn exn_info; |
|
570 |
true)); |
|
571 |
||
14985 | 572 |
fun >>> [] = () |
573 |
| >>> (tr :: trs) = if >> tr then >>> trs else (); |
|
574 |
||
7602 | 575 |
(*Note: this is for Poly/ML only, we really do not intend to exhibit |
576 |
interrupts here*) |
|
15531 | 577 |
fun get_interrupt src = SOME (Source.get_single src) handle Interrupt => NONE; |
7602 | 578 |
|
5828 | 579 |
fun raw_loop src = |
7602 | 580 |
(case get_interrupt (Source.set_prompt (prompt_state (get_state ())) src) of |
15531 | 581 |
NONE => (writeln "\nInterrupt."; raw_loop src) |
582 |
| SOME NONE => () |
|
583 |
| SOME (SOME (tr, src')) => if >> tr then raw_loop src' else ()); |
|
5828 | 584 |
|
585 |
||
12987 | 586 |
fun loop src = ignore_interrupt raw_loop src; |
5828 | 587 |
|
588 |
||
589 |
end; |