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