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