author | wenzelm |
Mon, 07 Jun 1999 22:17:23 +0200 | |
changeset 6797 | f86b96a0f6fb |
parent 6789 | 0e5a965de17a |
child 6847 | f175f56c57a6 |
permissions | -rw-r--r-- |
5819 | 1 |
(* Title: Pure/Isar/proof_context.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
4 |
||
5 |
Proof context information. |
|
6 |
*) |
|
7 |
||
8 |
signature PROOF_CONTEXT = |
|
9 |
sig |
|
10 |
type context |
|
11 |
exception CONTEXT of string * context |
|
12 |
val theory_of: context -> theory |
|
13 |
val sign_of: context -> Sign.sg |
|
6528 | 14 |
val verbose: bool ref |
5819 | 15 |
val print_binds: context -> unit |
16 |
val print_thms: context -> unit |
|
17 |
val print_context: context -> unit |
|
18 |
val print_proof_data: theory -> unit |
|
5874 | 19 |
val init: theory -> context |
5819 | 20 |
val read_typ: context -> string -> typ |
21 |
val cert_typ: context -> typ -> typ |
|
5874 | 22 |
val read_termTs: context -> (string * typ) list -> term list * (indexname * typ) list |
5819 | 23 |
val read_term: context -> string -> term |
24 |
val read_prop: context -> string -> term |
|
5935 | 25 |
val read_term_pat: context -> string -> term |
26 |
val read_prop_pat: context -> string -> term |
|
5819 | 27 |
val cert_term: context -> term -> term |
28 |
val cert_prop: context -> term -> term |
|
29 |
val declare_term: term -> context -> context |
|
30 |
val declare_terms: term list -> context -> context |
|
5874 | 31 |
val declare_thm: thm -> context -> context |
5819 | 32 |
val add_binds: (indexname * string) list -> context -> context |
33 |
val add_binds_i: (indexname * term) list -> context -> context |
|
5935 | 34 |
val match_binds: (string list * string) list -> context -> context |
35 |
val match_binds_i: (term list * term) list -> context -> context |
|
36 |
val bind_propp: context * (string * string list) -> context * term |
|
37 |
val bind_propp_i: context * (term * term list) -> context * term |
|
6789 | 38 |
val auto_bind_goal: term -> context -> context |
6797 | 39 |
val auto_bind_facts: string -> term list -> context -> context |
6091 | 40 |
val thms_closure: context -> xstring -> thm list option |
41 |
val get_thm: context -> string -> thm |
|
42 |
val get_thms: context -> string -> thm list |
|
43 |
val get_thmss: context -> string list -> thm list |
|
44 |
val put_thm: string * thm -> context -> context |
|
45 |
val put_thms: string * thm list -> context -> context |
|
46 |
val put_thmss: (string * thm list) list -> context -> context |
|
47 |
val have_thmss: string -> context attribute list |
|
48 |
-> (thm list * context attribute list) list -> context -> context * (string * thm list) |
|
6797 | 49 |
val assumptions: context -> cterm list |
5819 | 50 |
val fixed_names: context -> string list |
5935 | 51 |
val assume: string -> context attribute list -> (string * string list) list -> context |
6797 | 52 |
-> context * ((string * thm list) * thm list) |
5935 | 53 |
val assume_i: string -> context attribute list -> (term * term list) list -> context |
6797 | 54 |
-> context * ((string * thm list) * thm list) |
5819 | 55 |
val fix: (string * string option) list -> context -> context |
56 |
val fix_i: (string * typ) list -> context -> context |
|
57 |
val setup: (theory -> theory) list |
|
58 |
end; |
|
59 |
||
60 |
signature PROOF_CONTEXT_PRIVATE = |
|
61 |
sig |
|
62 |
include PROOF_CONTEXT |
|
63 |
val init_data: Object.kind -> (theory -> Object.T) * (context -> Object.T -> unit) |
|
64 |
-> theory -> theory |
|
65 |
val print_data: Object.kind -> context -> unit |
|
66 |
val get_data: Object.kind -> (Object.T -> 'a) -> context -> 'a |
|
67 |
val put_data: Object.kind -> ('a -> Object.T) -> 'a -> context -> context |
|
68 |
end; |
|
69 |
||
70 |
structure ProofContext: PROOF_CONTEXT_PRIVATE = |
|
71 |
struct |
|
72 |
||
73 |
||
74 |
(** datatype context **) |
|
75 |
||
76 |
datatype context = |
|
77 |
Context of |
|
78 |
{thy: theory, (*current theory*) |
|
79 |
data: Object.T Symtab.table, (*user data*) |
|
80 |
asms: |
|
6797 | 81 |
(cterm list * (string * thm list) list) * (*assumes: A ==> _*) |
5819 | 82 |
((string * string) list * string list), (*fixes: !!x. _*) |
83 |
binds: (term * typ) Vartab.table, (*term bindings*) |
|
6091 | 84 |
thms: thm list Symtab.table, (*local thms*) |
5819 | 85 |
defs: |
86 |
typ Vartab.table * (*type constraints*) |
|
87 |
sort Vartab.table * (*default sorts*) |
|
88 |
int * (*maxidx*) |
|
89 |
string list}; (*used type variable names*) |
|
90 |
||
91 |
exception CONTEXT of string * context; |
|
92 |
||
93 |
||
94 |
fun make_context (thy, data, asms, binds, thms, defs) = |
|
95 |
Context {thy = thy, data = data, asms = asms, binds = binds, thms = thms, defs = defs}; |
|
96 |
||
97 |
fun map_context f (Context {thy, data, asms, binds, thms, defs}) = |
|
98 |
make_context (f (thy, data, asms, binds, thms, defs)); |
|
99 |
||
100 |
fun theory_of (Context {thy, ...}) = thy; |
|
101 |
val sign_of = Theory.sign_of o theory_of; |
|
102 |
||
103 |
||
104 |
||
105 |
(** print context information **) |
|
106 |
||
6528 | 107 |
val verbose = ref false; |
108 |
fun verb f x = if ! verbose then f x else (); |
|
5819 | 109 |
|
110 |
fun print_items prt name items = |
|
111 |
let |
|
112 |
fun pretty_itms (name, [x]) = Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt x] |
|
113 |
| pretty_itms (name, xs) = Pretty.big_list (name ^ ":") (map prt xs); |
|
6721 | 114 |
in |
115 |
if null items andalso not (! verbose) then () |
|
116 |
else Pretty.writeln (Pretty.big_list name (map pretty_itms items)) |
|
117 |
end; |
|
5819 | 118 |
|
119 |
||
120 |
(* term bindings *) |
|
121 |
||
122 |
fun print_binds (Context {thy, binds, ...}) = |
|
123 |
let |
|
124 |
val prt_term = Sign.pretty_term (Theory.sign_of thy); |
|
125 |
||
126 |
fun fix_var (x, i) = |
|
127 |
(case try Syntax.dest_binding x of |
|
128 |
None => Syntax.string_of_vname (x, i) |
|
129 |
| Some x' => if i = 0 then "??" ^ x' else Syntax.string_of_vname (x, i)); |
|
130 |
fun pretty_bind (xi, (t, T)) = Pretty.block |
|
131 |
[Pretty.str (fix_var xi), Pretty.str " ==", Pretty.brk 1, prt_term t]; |
|
6721 | 132 |
in |
133 |
if Vartab.is_empty binds andalso not (! verbose) then () |
|
134 |
else Pretty.writeln (Pretty.big_list "Term bindings:" (map pretty_bind (Vartab.dest binds))) |
|
135 |
end; |
|
5819 | 136 |
|
137 |
||
138 |
(* local theorems *) |
|
139 |
||
140 |
fun print_thms (Context {thms, ...}) = |
|
6091 | 141 |
print_items Display.pretty_thm "Local theorems:" (Symtab.dest thms); |
5819 | 142 |
|
143 |
||
144 |
(* main context *) |
|
145 |
||
6797 | 146 |
fun print_context (ctxt as Context {thy, data = _, asms = ((_, prems), (fixes, _)), binds = _, |
5819 | 147 |
thms = _, defs = (types, sorts, maxidx, used)}) = |
148 |
let |
|
149 |
val sign = Theory.sign_of thy; |
|
150 |
||
151 |
val prt_term = Sign.pretty_term sign; |
|
152 |
val prt_typ = Sign.pretty_typ sign; |
|
153 |
val prt_sort = Sign.pretty_sort sign; |
|
154 |
||
155 |
(*theory*) |
|
156 |
val pretty_thy = Pretty.block [Pretty.str "Theory:", Pretty.brk 1, Sign.pretty_sg sign]; |
|
157 |
||
158 |
(*fixes*) |
|
159 |
fun prt_fix (x, x') = Pretty.str (x ^ " = " ^ x'); |
|
160 |
||
161 |
(* defaults *) |
|
162 |
||
163 |
fun prt_atom prt prtT (x, X) = Pretty.block |
|
164 |
[prt x, Pretty.str " ::", Pretty.brk 1, prtT X]; |
|
165 |
||
166 |
fun prt_var (x, ~1) = prt_term (Syntax.free x) |
|
167 |
| prt_var xi = prt_term (Syntax.var xi); |
|
168 |
||
169 |
fun prt_varT (x, ~1) = prt_typ (TFree (x, [])) |
|
170 |
| prt_varT xi = prt_typ (TVar (xi, [])); |
|
171 |
||
172 |
val prt_defT = prt_atom prt_var prt_typ; |
|
173 |
val prt_defS = prt_atom prt_varT prt_sort; |
|
174 |
in |
|
6528 | 175 |
verb Pretty.writeln pretty_thy; |
6721 | 176 |
if null fixes andalso not (! verbose) then () |
177 |
else Pretty.writeln (Pretty.big_list "Fixed variables:" (map prt_fix (rev fixes))); |
|
6797 | 178 |
print_items (prt_term o #prop o Thm.rep_thm) "Assumptions:" prems; |
6528 | 179 |
verb print_binds ctxt; |
180 |
verb print_thms ctxt; |
|
181 |
verb Pretty.writeln (Pretty.big_list "Type constraints:" (map prt_defT (Vartab.dest types))); |
|
182 |
verb Pretty.writeln (Pretty.big_list "Default sorts:" (map prt_defS (Vartab.dest sorts))); |
|
183 |
verb writeln ("Maxidx: " ^ string_of_int maxidx); |
|
184 |
verb Pretty.writeln (Pretty.strs ("Used type variable names:" :: used)) |
|
5819 | 185 |
end; |
186 |
||
187 |
||
188 |
||
189 |
(** user data **) |
|
190 |
||
191 |
(* errors *) |
|
192 |
||
193 |
fun of_theory thy = "\nof theory " ^ Sign.str_of_sg (Theory.sign_of thy); |
|
194 |
||
195 |
fun err_inconsistent kinds = |
|
196 |
error ("Attempt to merge different versions of " ^ commas_quote kinds ^ " proof data"); |
|
197 |
||
198 |
fun err_dup_init thy kind = |
|
199 |
error ("Duplicate initialization of " ^ quote kind ^ " proof data" ^ of_theory thy); |
|
200 |
||
201 |
fun err_undef ctxt kind = |
|
202 |
raise CONTEXT ("Tried to access undefined " ^ quote kind ^ " proof data", ctxt); |
|
203 |
||
204 |
fun err_uninit ctxt kind = |
|
205 |
raise CONTEXT ("Tried to access uninitialized " ^ quote kind ^ " proof data" ^ |
|
206 |
of_theory (theory_of ctxt), ctxt); |
|
207 |
||
208 |
fun err_access ctxt kind = |
|
209 |
raise CONTEXT ("Unauthorized access to " ^ quote kind ^ " proof data" ^ |
|
210 |
of_theory (theory_of ctxt), ctxt); |
|
211 |
||
212 |
||
213 |
(* data kind 'Isar/proof_data' *) |
|
214 |
||
215 |
structure ProofDataDataArgs = |
|
216 |
struct |
|
217 |
val name = "Isar/proof_data"; |
|
218 |
type T = (Object.kind * ((theory -> Object.T) * (context -> Object.T -> unit))) Symtab.table; |
|
219 |
||
220 |
val empty = Symtab.empty; |
|
6550 | 221 |
val copy = I; |
5819 | 222 |
val prep_ext = I; |
223 |
fun merge tabs = Symtab.merge (Object.eq_kind o pairself fst) tabs |
|
224 |
handle Symtab.DUPS kinds => err_inconsistent kinds; |
|
225 |
fun print _ tab = Pretty.writeln (Pretty.strs (map #1 (Symtab.dest tab))); |
|
226 |
end; |
|
227 |
||
228 |
structure ProofDataData = TheoryDataFun(ProofDataDataArgs); |
|
229 |
val print_proof_data = ProofDataData.print; |
|
230 |
||
231 |
||
232 |
(* init proof data *) |
|
233 |
||
234 |
fun init_data kind meths thy = |
|
235 |
let |
|
236 |
val name = Object.name_of_kind kind; |
|
237 |
val tab = Symtab.update_new ((name, (kind, meths)), ProofDataData.get thy) |
|
238 |
handle Symtab.DUP _ => err_dup_init thy name; |
|
239 |
in thy |> ProofDataData.put tab end; |
|
240 |
||
241 |
||
242 |
(* access data *) |
|
243 |
||
244 |
fun lookup_data (ctxt as Context {data, ...}) kind = |
|
245 |
let |
|
246 |
val thy = theory_of ctxt; |
|
247 |
val name = Object.name_of_kind kind; |
|
248 |
in |
|
249 |
(case Symtab.lookup (ProofDataData.get thy, name) of |
|
250 |
Some (k, meths) => |
|
251 |
if Object.eq_kind (kind, k) then |
|
252 |
(case Symtab.lookup (data, name) of |
|
253 |
Some x => (x, meths) |
|
254 |
| None => err_undef ctxt name) |
|
255 |
else err_access ctxt name |
|
256 |
| None => err_uninit ctxt name) |
|
257 |
end; |
|
258 |
||
259 |
fun get_data kind f ctxt = |
|
260 |
let val (x, _) = lookup_data ctxt kind |
|
261 |
in f x handle Match => Object.kind_error kind end; |
|
262 |
||
263 |
fun print_data kind ctxt = |
|
264 |
let val (x, (_, prt)) = lookup_data ctxt kind |
|
265 |
in prt ctxt x end; |
|
266 |
||
267 |
fun put_data kind f x ctxt = |
|
268 |
(lookup_data ctxt kind; |
|
269 |
ctxt |> map_context (fn (thy, data, asms, binds, thms, defs) => |
|
270 |
(thy, Symtab.update ((Object.name_of_kind kind, f x), data), asms, binds, thms, defs))); |
|
271 |
||
272 |
||
273 |
(* init context *) |
|
274 |
||
5874 | 275 |
fun init thy = |
276 |
let val data = Symtab.map (fn (_, (f, _)) => f thy) (ProofDataData.get thy) in |
|
6797 | 277 |
make_context (thy, data, (([], []), ([], [])), Vartab.empty, Symtab.empty, |
5819 | 278 |
(Vartab.empty, Vartab.empty, ~1, [])) |
279 |
end; |
|
280 |
||
281 |
||
282 |
||
283 |
(** prepare types **) |
|
284 |
||
285 |
fun read_typ (ctxt as Context {defs = (_, sorts, _, _), ...}) s = |
|
286 |
let |
|
287 |
val sign = sign_of ctxt; |
|
288 |
fun def_sort xi = Vartab.lookup (sorts, xi); |
|
289 |
in |
|
290 |
transform_error (Sign.read_typ (sign, def_sort)) s |
|
291 |
handle ERROR_MESSAGE msg => raise CONTEXT (msg, ctxt) |
|
292 |
end; |
|
293 |
||
294 |
fun cert_typ ctxt raw_T = |
|
295 |
Sign.certify_typ (sign_of ctxt) raw_T |
|
296 |
handle TYPE (msg, _, _) => raise CONTEXT (msg, ctxt); |
|
297 |
||
298 |
||
299 |
||
300 |
(** prepare terms and propositions **) |
|
301 |
||
302 |
(* |
|
303 |
(1) read / certify wrt. signature of context |
|
304 |
(2) intern Skolem constants |
|
305 |
(3) expand term bindings |
|
306 |
*) |
|
307 |
||
308 |
||
309 |
(* read / certify wrt. signature *) (*exception ERROR*) (*exception TERM*) |
|
310 |
||
5874 | 311 |
fun read_def_termTs freeze sg (types, sorts, used) sTs = |
312 |
let val (cts, env) = Thm.read_def_cterms (sg, types, sorts) used freeze sTs |
|
313 |
in (map Thm.term_of cts, env) end; |
|
314 |
||
315 |
fun read_def_termT freeze sg defs sT = apfst hd (read_def_termTs freeze sg defs [sT]); |
|
316 |
||
5819 | 317 |
|
6762 | 318 |
fun read_term_sg freeze sg (defs as (_, _, used)) s = |
319 |
#1 (read_def_termT freeze sg defs (s, TVar ((variant used "'z", 0), logicS))); |
|
5819 | 320 |
|
6762 | 321 |
fun read_prop_sg freeze sg defs s = |
322 |
#1 (read_def_termT freeze sg defs (s, propT)); |
|
5819 | 323 |
|
324 |
||
325 |
fun cert_term_sg sg t = Thm.term_of (Thm.cterm_of sg t); |
|
326 |
||
327 |
fun cert_prop_sg sg tm = |
|
328 |
let |
|
329 |
val ctm = Thm.cterm_of sg tm; |
|
330 |
val {t, T, ...} = Thm.rep_cterm ctm; |
|
331 |
in |
|
332 |
if T = propT then t |
|
333 |
else raise TERM ("Term not of type prop", [t]) |
|
334 |
end; |
|
335 |
||
336 |
||
337 |
(* intern_skolem *) |
|
338 |
||
339 |
fun get_skolem (Context {asms = (_, (fixes, _)), ...}) x = assoc (fixes, x); |
|
340 |
||
341 |
fun check_skolem ctxt check x = |
|
342 |
if check andalso can Syntax.dest_skolem x then |
|
343 |
raise CONTEXT ("Illegal reference to internal Skolem constant: " ^ quote x, ctxt) |
|
344 |
else x; |
|
345 |
||
346 |
fun intern_skolem ctxt check = |
|
347 |
let |
|
348 |
fun intern (t as Free (x, T)) = |
|
349 |
(case get_skolem ctxt (check_skolem ctxt check x) of |
|
350 |
Some x' => Free (x', T) |
|
351 |
| None => t) |
|
352 |
| intern (t $ u) = intern t $ intern u |
|
353 |
| intern (Abs (x, T, t)) = Abs (x, T, intern t) |
|
354 |
| intern a = a; |
|
355 |
in intern end; |
|
356 |
||
357 |
||
358 |
(* norm_term *) |
|
359 |
||
360 |
(*beta normal form for terms (not eta normal form), chase variables in |
|
361 |
bindings environment (code taken from Pure/envir.ML)*) |
|
362 |
||
363 |
fun norm_term (ctxt as Context {binds, ...}) = |
|
364 |
let |
|
365 |
(*raised when norm has no effect on a term, to do sharing instead of copying*) |
|
366 |
exception SAME; |
|
367 |
||
368 |
fun norm (t as Var (xi, T)) = |
|
369 |
(case Vartab.lookup (binds, xi) of |
|
370 |
Some (u, U) => |
|
371 |
if T = U then (norm u handle SAME => u) |
|
372 |
else raise TYPE ("norm_term: ill-typed variable assigment", [T, U], [t, u]) |
|
373 |
| None => |
|
374 |
if can Syntax.dest_binding (#1 xi) then |
|
375 |
raise CONTEXT ("Unbound binding: " ^ Syntax.string_of_vname xi, ctxt) |
|
376 |
else raise SAME) |
|
377 |
| norm (Abs (a, T, body)) = Abs (a, T, norm body) |
|
378 |
| norm (Abs (_, _, body) $ t) = normh (subst_bound (t, body)) |
|
379 |
| norm (f $ t) = |
|
380 |
((case norm f of |
|
381 |
Abs (_, _, body) => normh (subst_bound (t, body)) |
|
382 |
| nf => nf $ (norm t handle SAME => t)) handle SAME => f $ norm t) |
|
383 |
| norm _ = raise SAME |
|
384 |
and normh t = norm t handle SAME => t |
|
385 |
in normh end; |
|
386 |
||
387 |
||
6550 | 388 |
(* dummy patterns *) |
389 |
||
6762 | 390 |
local |
391 |
||
6560
1436349f8b28
renamed 'dummy' to 'dummy_pattern' (less dangerous);
wenzelm
parents:
6550
diff
changeset
|
392 |
fun is_dummy (Const (c, _)) = c = PureThy.dummy_patternN |
6550 | 393 |
| is_dummy _ = false; |
394 |
||
6762 | 395 |
fun prep_dummy (i, t) = |
396 |
if is_dummy t then (i + 1, Var (("_dummy_", i), Term.fastype_of t)) else (i, t); |
|
397 |
||
398 |
in |
|
399 |
||
400 |
fun prepare_dummies tm = #2 (Term.foldl_map_aterms prep_dummy (1, tm)); |
|
401 |
||
6550 | 402 |
fun reject_dummies ctxt tm = |
403 |
if foldl_aterms (fn (ok, t) => ok andalso not (is_dummy t)) (true, tm) then tm |
|
404 |
else raise CONTEXT ("Illegal dummy pattern(s) in term", ctxt); |
|
405 |
||
6762 | 406 |
end; |
6550 | 407 |
|
408 |
||
5819 | 409 |
(* read terms *) |
410 |
||
5874 | 411 |
fun gen_read read app is_pat (ctxt as Context {binds, defs = (types, sorts, _, used), ...}) s = |
5819 | 412 |
let |
413 |
val sign = sign_of ctxt; |
|
414 |
||
415 |
fun def_type xi = |
|
416 |
(case Vartab.lookup (types, xi) of |
|
417 |
None => if is_pat then None else apsome #2 (Vartab.lookup (binds, xi)) |
|
418 |
| some => some); |
|
419 |
||
420 |
fun def_sort xi = Vartab.lookup (sorts, xi); |
|
421 |
in |
|
422 |
(transform_error (read sign (def_type, def_sort, used)) s |
|
423 |
handle TERM (msg, _) => raise CONTEXT (msg, ctxt) |
|
424 |
| ERROR_MESSAGE msg => raise CONTEXT (msg, ctxt)) |
|
5874 | 425 |
|> app (intern_skolem ctxt true) |
426 |
|> app (if is_pat then I else norm_term ctxt) |
|
6550 | 427 |
|> app (if is_pat then prepare_dummies else (reject_dummies ctxt)) |
5819 | 428 |
end; |
429 |
||
5874 | 430 |
val read_termTs = gen_read (read_def_termTs false) (apfst o map) false; |
6762 | 431 |
val read_term = gen_read (read_term_sg true) I false; |
432 |
val read_prop = gen_read (read_prop_sg true) I false; |
|
433 |
val read_term_pat = gen_read (read_term_sg false) I true; |
|
434 |
val read_prop_pat = gen_read (read_prop_sg false) I true; |
|
5819 | 435 |
|
436 |
||
437 |
(* certify terms *) |
|
438 |
||
439 |
fun gen_cert cert is_pat ctxt t = |
|
440 |
(cert (sign_of ctxt) t handle TERM (msg, _) => raise CONTEXT (msg, ctxt)) |
|
441 |
|> intern_skolem ctxt false |
|
442 |
|> (if is_pat then I else norm_term ctxt); |
|
443 |
||
444 |
val cert_term = gen_cert cert_term_sg false; |
|
445 |
val cert_prop = gen_cert cert_prop_sg false; |
|
5935 | 446 |
val cert_term_pat = gen_cert cert_term_sg true; |
447 |
val cert_prop_pat = gen_cert cert_prop_sg true; |
|
5819 | 448 |
|
449 |
||
450 |
(* declare terms *) |
|
451 |
||
452 |
val ins_types = foldl_aterms |
|
453 |
(fn (types, Free (x, T)) => Vartab.update (((x, ~1), T), types) |
|
454 |
| (types, Var v) => Vartab.update (v, types) |
|
455 |
| (types, _) => types); |
|
456 |
||
457 |
val ins_sorts = foldl_types (foldl_atyps |
|
458 |
(fn (sorts, TFree (x, S)) => Vartab.update (((x, ~1), S), sorts) |
|
459 |
| (sorts, TVar v) => Vartab.update (v, sorts) |
|
460 |
| (sorts, _) => sorts)); |
|
461 |
||
462 |
val ins_used = foldl_types (foldl_atyps |
|
463 |
(fn (used, TFree (x, _)) => x ins used |
|
464 |
| (used, TVar ((x, _), _)) => x ins used |
|
465 |
| (used, _) => used)); |
|
466 |
||
5994 | 467 |
fun ins_skolem def_type = foldr |
468 |
(fn ((x, x'), types) => |
|
469 |
(case def_type x' of |
|
470 |
Some T => Vartab.update (((x, ~1), T), types) |
|
471 |
| None => types)); |
|
472 |
||
5819 | 473 |
fun map_defaults f = map_context |
474 |
(fn (thy, data, asms, binds, thms, defs) => (thy, data, asms, binds, thms, f defs)); |
|
475 |
||
5994 | 476 |
fun declare (ctxt as Context {asms = (_, (fixes, _)), ...}, t) = |
5819 | 477 |
ctxt |
478 |
|> map_defaults (fn (types, sorts, maxidx, used) => (ins_types (types, t), sorts, maxidx, used)) |
|
479 |
|> map_defaults (fn (types, sorts, maxidx, used) => (types, ins_sorts (sorts, t), maxidx, used)) |
|
480 |
|> map_defaults (fn (types, sorts, maxidx, used) => (types, sorts, maxidx, ins_used (used, t))) |
|
481 |
|> map_defaults (fn (types, sorts, maxidx, used) => |
|
5994 | 482 |
(types, sorts, Int.max (maxidx, Term.maxidx_of_term t), used)) |
483 |
|> map_defaults (fn (types, sorts, maxidx, used) => |
|
484 |
(ins_skolem (fn x => Vartab.lookup (types, (x, ~1))) (fixes, types), sorts, maxidx, used)); |
|
5819 | 485 |
|
486 |
||
487 |
fun declare_term t ctxt = declare (ctxt, t); |
|
488 |
fun declare_terms ts ctxt = foldl declare (ctxt, ts); |
|
489 |
||
5874 | 490 |
fun declare_thm thm ctxt = |
491 |
let val {prop, hyps, ...} = Thm.rep_thm thm |
|
492 |
in ctxt |> declare_terms (prop :: hyps) end; |
|
493 |
||
5819 | 494 |
|
495 |
||
496 |
(** bindings **) |
|
497 |
||
498 |
(* update_binds *) |
|
499 |
||
500 |
fun upd_bind (ctxt, (xi, t)) = |
|
501 |
let val T = fastype_of t in |
|
502 |
ctxt |
|
503 |
|> declare_term t |
|
504 |
|> map_context (fn (thy, data, asms, binds, thms, defs) => |
|
505 |
(thy, data, asms, Vartab.update ((xi, (t, T)), binds), thms, defs)) |
|
506 |
end; |
|
507 |
||
508 |
fun update_binds bs ctxt = foldl upd_bind (ctxt, bs); |
|
509 |
fun update_binds_env env = update_binds (Envir.alist_of env); |
|
510 |
||
511 |
||
512 |
(* add_binds(_i) -- sequential *) |
|
513 |
||
514 |
fun gen_bind prep (ctxt, (xi as (x, _), raw_t)) = |
|
515 |
let val t = prep ctxt raw_t in |
|
516 |
if can Syntax.dest_binding x then ctxt |> update_binds [(xi, t)] |
|
517 |
else raise CONTEXT ("Illegal variable name for term binding: " ^ |
|
518 |
quote (Syntax.string_of_vname xi), ctxt) |
|
519 |
end; |
|
520 |
||
521 |
fun gen_binds prep binds ctxt = foldl (gen_bind prep) (ctxt, binds); |
|
522 |
||
523 |
val add_binds = gen_binds read_term; |
|
524 |
val add_binds_i = gen_binds cert_term; |
|
525 |
||
526 |
||
527 |
(* match_binds(_i) -- parallel *) |
|
528 |
||
5935 | 529 |
fun prep_declare_match (prep_pat, prep) (ctxt, (raw_pats, raw_t)) = |
5819 | 530 |
let |
5935 | 531 |
val t = prep ctxt raw_t; |
5994 | 532 |
val ctxt' = ctxt |> declare_term t; |
533 |
val pats = map (prep_pat ctxt') raw_pats; (* FIXME seq / par / simult (??) *) |
|
534 |
in (ctxt', (map (rpair t) pats, t)) end; |
|
5819 | 535 |
|
5935 | 536 |
fun gen_match_binds _ [] ctxt = ctxt |
537 |
| gen_match_binds prepp raw_pairs ctxt = |
|
538 |
let |
|
539 |
val (ctxt', matches) = foldl_map (prep_declare_match prepp) (ctxt, raw_pairs); |
|
540 |
val pairs = flat (map #1 matches); |
|
541 |
val Context {defs = (_, _, maxidx, _), ...} = ctxt'; |
|
542 |
val envs = Unify.smash_unifiers (sign_of ctxt', Envir.empty maxidx, pairs); |
|
543 |
val env = |
|
544 |
(case Seq.pull envs of |
|
545 |
None => raise CONTEXT ("Pattern match failed!", ctxt') |
|
546 |
| Some (env, _) => env); |
|
547 |
in ctxt' |> update_binds_env env end; |
|
548 |
||
549 |
val match_binds = gen_match_binds (read_term_pat, read_term); |
|
550 |
val match_binds_i = gen_match_binds (cert_term_pat, cert_term); |
|
551 |
||
552 |
||
553 |
(* bind proposition patterns *) |
|
554 |
||
555 |
fun gen_bind_propp prepp (ctxt, (raw_prop, raw_pats)) = |
|
556 |
let val (ctxt', (pairs, prop)) = prep_declare_match prepp (ctxt, (raw_pats, raw_prop)) |
|
557 |
in (ctxt' |> match_binds_i (map (apfst single) pairs), prop) end; |
|
558 |
||
559 |
val bind_propp = gen_bind_propp (read_prop_pat, read_prop); |
|
560 |
val bind_propp_i = gen_bind_propp (cert_prop_pat, cert_prop); |
|
5819 | 561 |
|
562 |
||
6789 | 563 |
(* auto binds *) |
564 |
||
565 |
val auto_bind_goal = add_binds_i o AutoBind.goal; |
|
6797 | 566 |
val auto_bind_facts = add_binds_i oo AutoBind.facts; |
6789 | 567 |
|
568 |
||
5819 | 569 |
|
570 |
(** theorems **) |
|
571 |
||
572 |
(* thms_closure *) |
|
573 |
||
574 |
fun thms_closure (Context {thy, thms, ...}) = |
|
575 |
let |
|
576 |
val global_closure = PureThy.thms_closure thy; |
|
577 |
fun get name = |
|
578 |
(case global_closure name of |
|
579 |
None => Symtab.lookup (thms, name) |
|
580 |
| some => some); |
|
581 |
in get end; |
|
582 |
||
583 |
||
6091 | 584 |
(* get_thm(s) *) |
5819 | 585 |
|
6091 | 586 |
fun get_thm (ctxt as Context {thy, thms, ...}) name = |
5819 | 587 |
(case Symtab.lookup (thms, name) of |
588 |
Some [th] => th |
|
589 |
| Some _ => raise CONTEXT ("Single theorem expected: " ^ quote name, ctxt) |
|
6091 | 590 |
| None => (PureThy.get_thm thy name handle THEORY (msg, _) => raise CONTEXT (msg, ctxt))); |
5819 | 591 |
|
6091 | 592 |
fun get_thms (ctxt as Context {thy, thms, ...}) name = |
5819 | 593 |
(case Symtab.lookup (thms, name) of |
594 |
Some ths => ths |
|
6091 | 595 |
| None => (PureThy.get_thms thy name handle THEORY (msg, _) => raise CONTEXT (msg, ctxt))); |
5819 | 596 |
|
6091 | 597 |
fun get_thmss ctxt names = flat (map (get_thms ctxt) names); |
5819 | 598 |
|
599 |
||
6091 | 600 |
(* put_thm(s) *) |
5819 | 601 |
|
6091 | 602 |
fun put_thms (name, ths) = map_context |
5819 | 603 |
(fn (thy, data, asms, binds, thms, defs) => |
604 |
(thy, data, asms, binds, Symtab.update ((name, ths), thms), defs)); |
|
605 |
||
6091 | 606 |
fun put_thm (name, th) = put_thms (name, [th]); |
5819 | 607 |
|
6091 | 608 |
fun put_thmss [] ctxt = ctxt |
609 |
| put_thmss (th :: ths) ctxt = ctxt |> put_thms th |> put_thmss ths; |
|
5819 | 610 |
|
611 |
||
6091 | 612 |
(* have_thmss *) |
5819 | 613 |
|
6091 | 614 |
fun have_thmss name more_attrs ths_attrs ctxt = |
5819 | 615 |
let |
616 |
fun app ((ct, ths), (th, attrs)) = |
|
6091 | 617 |
let val (ct', th') = Thm.applys_attributes ((ct, th), attrs @ more_attrs) |
5819 | 618 |
in (ct', th' :: ths) end |
619 |
val (ctxt', rev_thms) = foldl app ((ctxt, []), ths_attrs); |
|
5919 | 620 |
val thms = flat (rev rev_thms); |
6091 | 621 |
in (ctxt' |> put_thms (name, thms), (name, thms)) end; |
5819 | 622 |
|
623 |
||
624 |
||
625 |
(** assumptions **) |
|
626 |
||
627 |
(* get assumptions *) |
|
628 |
||
6797 | 629 |
fun assumptions (Context {asms = ((asms_ct, _), _), ...}) = asms_ct; |
5819 | 630 |
fun fixed_names (Context {asms = (_, (fixes, _)), ...}) = map #2 fixes; |
631 |
||
632 |
||
633 |
(* assume *) |
|
634 |
||
6797 | 635 |
fun prems (Context {asms = ((_, asms_th), _), ...}) = flat (map #2 asms_th); |
636 |
||
5935 | 637 |
fun gen_assume prepp name attrs raw_prop_pats ctxt = |
5819 | 638 |
let |
5935 | 639 |
val (ctxt', props) = foldl_map prepp (ctxt, raw_prop_pats); |
5819 | 640 |
val sign = sign_of ctxt'; |
6797 | 641 |
val Context {defs = (_, _, maxidx, _), ...} = ctxt'; |
5819 | 642 |
|
6797 | 643 |
val cprops = map (Thm.cterm_of sign) props; |
644 |
val asms = map (Drule.forall_elim_vars (maxidx + 1) o Thm.assume) cprops; |
|
5919 | 645 |
|
646 |
val ths = map (fn th => ([th], [])) asms; |
|
6091 | 647 |
val (ctxt'', (_, thms)) = |
5819 | 648 |
ctxt' |
6797 | 649 |
|> auto_bind_facts name props |
6091 | 650 |
|> have_thmss name (attrs @ [Drule.tag_assumption]) ths; |
5819 | 651 |
|
652 |
val ctxt''' = |
|
653 |
ctxt'' |
|
6797 | 654 |
|> map_context (fn (thy, data, ((asms_ct, asms_th), fixes), binds, thms, defs) => |
655 |
(thy, data, ((asms_ct @ cprops, asms_th @ [(name, asms)]), fixes), binds, thms, defs)); |
|
656 |
in (ctxt''', ((name, thms), prems ctxt')) end; |
|
5819 | 657 |
|
5935 | 658 |
val assume = gen_assume bind_propp; |
659 |
val assume_i = gen_assume bind_propp_i; |
|
5819 | 660 |
|
661 |
||
662 |
(* fix *) |
|
663 |
||
664 |
fun read_skolemT (Context {defs = (_, _, _, used), ...}) None = Type.param used ("'z", logicS) |
|
665 |
| read_skolemT ctxt (Some s) = read_typ ctxt s; |
|
666 |
||
667 |
fun gen_fix prep check (ctxt, (x, raw_T)) = |
|
668 |
ctxt |
|
669 |
|> declare_term (Free (check_skolem ctxt check x, prep ctxt raw_T)) |
|
670 |
|> map_context (fn (thy, data, (assumes, (fixes, names)), binds, thms, defs) => |
|
671 |
let val x' = variant names x in |
|
672 |
(thy, data, (assumes, ((x, Syntax.skolem x') :: fixes, x' :: names)), binds, thms, defs) |
|
673 |
end); |
|
674 |
||
675 |
fun gen_fixs prep check xs ctxt = foldl (gen_fix prep check) (ctxt, xs); |
|
676 |
||
677 |
||
678 |
val fix = gen_fixs read_skolemT true; |
|
679 |
val fix_i = gen_fixs cert_typ false; |
|
680 |
||
681 |
||
682 |
||
683 |
(** theory setup **) |
|
684 |
||
685 |
val setup = [ProofDataData.init]; |
|
686 |
||
687 |
||
688 |
end; |