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