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