author | wenzelm |
Wed, 31 Oct 2001 22:02:11 +0100 | |
changeset 12008 | 078637472921 |
parent 11926 | e31f781611b3 |
child 12016 | 425289df84d3 |
permissions | -rw-r--r-- |
5819 | 1 |
(* Title: Pure/Isar/proof_context.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
8807 | 4 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
5819 | 5 |
|
6 |
Proof context information. |
|
7 |
*) |
|
8 |
||
9 |
signature PROOF_CONTEXT = |
|
10 |
sig |
|
11 |
type context |
|
9470 | 12 |
type exporter |
5819 | 13 |
exception CONTEXT of string * context |
14 |
val theory_of: context -> theory |
|
15 |
val sign_of: context -> Sign.sg |
|
7557 | 16 |
val prems_of: context -> thm list |
5819 | 17 |
val print_proof_data: theory -> unit |
5874 | 18 |
val init: theory -> context |
9470 | 19 |
val assumptions: context -> (cterm list * exporter) list |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
20 |
val fixed_names: context -> string list |
5819 | 21 |
val read_typ: context -> string -> typ |
9504 | 22 |
val read_typ_no_norm: context -> string -> typ |
5819 | 23 |
val cert_typ: context -> typ -> typ |
9504 | 24 |
val cert_typ_no_norm: context -> typ -> typ |
10583 | 25 |
val get_skolem: context -> string -> string |
9291
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
26 |
val intern_skolem: context -> term -> term |
9133 | 27 |
val extern_skolem: context -> term -> term |
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 |
|
11925 | 31 |
val read_prop_schematic: context -> string -> term |
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
32 |
val read_terms: context -> string list -> term list |
8096 | 33 |
val read_termT_pats: context -> (string * typ) list -> term list |
34 |
val read_term_pats: typ -> context -> string list -> term list |
|
35 |
val read_prop_pats: context -> string list -> term list |
|
5819 | 36 |
val cert_term: context -> term -> term |
37 |
val cert_prop: context -> term -> term |
|
8096 | 38 |
val cert_term_pats: typ -> context -> term list -> term list |
39 |
val cert_prop_pats: context -> term list -> term list |
|
5819 | 40 |
val declare_term: term -> context -> context |
41 |
val declare_terms: term list -> context -> context |
|
7925 | 42 |
val warn_extra_tfrees: context -> context -> context |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
43 |
val generalizeT: context -> context -> typ -> typ |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
44 |
val generalize: context -> context -> term -> term |
9470 | 45 |
val find_free: term -> string -> term option |
12008 | 46 |
val export: bool -> context -> context -> thm -> thm Seq.seq |
10810 | 47 |
val drop_schematic: indexname * term option -> indexname * term option |
48 |
val add_binds: (indexname * string option) list -> context -> context |
|
49 |
val add_binds_i: (indexname * term option) list -> context -> context |
|
7925 | 50 |
val auto_bind_goal: term -> context -> context |
51 |
val auto_bind_facts: string -> term list -> context -> context |
|
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
52 |
val match_bind: bool -> (string list * string) list -> context -> context |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
53 |
val match_bind_i: bool -> (term list * term) list -> context -> context |
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
54 |
val read_propp: context * (string * (string list * string list)) list list |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
55 |
-> context * (term * (term list * term list)) list list |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
56 |
val cert_propp: context * (term * (term list * term list)) list list |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
57 |
-> context * (term * (term list * term list)) list list |
10554 | 58 |
val read_propp_schematic: context * (string * (string list * string list)) list list |
59 |
-> context * (term * (term list * term list)) list list |
|
60 |
val cert_propp_schematic: context * (term * (term list * term list)) list list |
|
61 |
-> context * (term * (term list * term list)) list list |
|
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
62 |
val bind_propp: context * (string * (string list * string list)) list list |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
63 |
-> context * (term list list * (context -> context)) |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
64 |
val bind_propp_i: context * (term * (term list * term list)) list list |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
65 |
-> context * (term list list * (context -> context)) |
10554 | 66 |
val bind_propp_schematic: context * (string * (string list * string list)) list list |
67 |
-> context * (term list list * (context -> context)) |
|
68 |
val bind_propp_schematic_i: context * (term * (term list * term list)) list list |
|
69 |
-> context * (term list list * (context -> context)) |
|
6091 | 70 |
val get_thm: context -> string -> thm |
9566 | 71 |
val get_thm_closure: context -> string -> thm |
6091 | 72 |
val get_thms: context -> string -> thm list |
9566 | 73 |
val get_thms_closure: context -> string -> thm list |
6091 | 74 |
val put_thm: string * thm -> context -> context |
75 |
val put_thms: string * thm list -> context -> context |
|
76 |
val put_thmss: (string * thm list) list -> context -> context |
|
7606 | 77 |
val reset_thms: string -> context -> context |
9196 | 78 |
val have_thmss: |
79 |
((string * context attribute list) * (thm list * context attribute list) list) list -> |
|
80 |
context -> context * (string * thm list) list |
|
11918
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
81 |
val export_assume: exporter |
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
82 |
val export_presume: exporter |
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
83 |
val export_def: exporter |
9470 | 84 |
val assume: exporter |
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
85 |
-> ((string * context attribute list) * (string * (string list * string list)) list) list |
11925 | 86 |
-> context -> context * (string * thm list) list |
9470 | 87 |
val assume_i: exporter |
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
88 |
-> ((string * context attribute list) * (term * (term list * term list)) list) list |
11925 | 89 |
-> context -> context * (string * thm list) list |
8096 | 90 |
val read_vars: context * (string list * string option) -> context * (string list * typ option) |
91 |
val cert_vars: context * (string list * typ option) -> context * (string list * typ option) |
|
7411 | 92 |
val fix: (string list * string option) list -> context -> context |
7663 | 93 |
val fix_i: (string list * typ option) list -> context -> context |
11925 | 94 |
val fix_direct: (string list * typ option) list -> context -> context |
9291
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
95 |
val bind_skolem: context -> string list -> term -> term |
11793
5f0ab6f5c280
support impromptu terminology of cases parameters;
wenzelm
parents:
11764
diff
changeset
|
96 |
val get_case: context -> string -> string option list -> RuleCases.T |
8373
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
97 |
val add_cases: (string * RuleCases.T) list -> context -> context |
10830 | 98 |
val apply_case: RuleCases.T -> context -> context * ((indexname * term option) list * term list) |
10810 | 99 |
val show_hyps: bool ref |
100 |
val pretty_thm: thm -> Pretty.T |
|
101 |
val verbose: bool ref |
|
102 |
val setmp_verbose: ('a -> 'b) -> 'a -> 'b |
|
103 |
val print_binds: context -> unit |
|
104 |
val print_thms: context -> unit |
|
105 |
val print_cases: context -> unit |
|
106 |
val prems_limit: int ref |
|
107 |
val pretty_prems: context -> Pretty.T list |
|
108 |
val pretty_context: context -> Pretty.T list |
|
5819 | 109 |
val setup: (theory -> theory) list |
110 |
end; |
|
111 |
||
8151 | 112 |
signature PRIVATE_PROOF_CONTEXT = |
5819 | 113 |
sig |
114 |
include PROOF_CONTEXT |
|
115 |
val init_data: Object.kind -> (theory -> Object.T) * (context -> Object.T -> unit) |
|
116 |
-> theory -> theory |
|
117 |
val print_data: Object.kind -> context -> unit |
|
118 |
val get_data: Object.kind -> (Object.T -> 'a) -> context -> 'a |
|
119 |
val put_data: Object.kind -> ('a -> Object.T) -> 'a -> context -> context |
|
120 |
end; |
|
121 |
||
8151 | 122 |
structure ProofContext: PRIVATE_PROOF_CONTEXT = |
5819 | 123 |
struct |
124 |
||
125 |
||
126 |
(** datatype context **) |
|
127 |
||
11816 | 128 |
type exporter = bool -> cterm list -> thm -> thm Seq.seq; |
9470 | 129 |
|
5819 | 130 |
datatype context = |
131 |
Context of |
|
6847
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
wenzelm
parents:
6797
diff
changeset
|
132 |
{thy: theory, (*current theory*) |
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
wenzelm
parents:
6797
diff
changeset
|
133 |
data: Object.T Symtab.table, (*user data*) |
5819 | 134 |
asms: |
9470 | 135 |
((cterm list * exporter) list * (*assumes: A ==> _*) |
6931 | 136 |
(string * thm list) list) * |
6847
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
wenzelm
parents:
6797
diff
changeset
|
137 |
((string * string) list * string list), (*fixes: !!x. _*) |
7606 | 138 |
binds: (term * typ) option Vartab.table, (*term bindings*) |
139 |
thms: thm list option Symtab.table, (*local thms*) |
|
8426 | 140 |
cases: (string * RuleCases.T) list, (*local contexts*) |
5819 | 141 |
defs: |
6847
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
wenzelm
parents:
6797
diff
changeset
|
142 |
typ Vartab.table * (*type constraints*) |
f175f56c57a6
tuned output: print_context replaced by strings_of_context;
wenzelm
parents:
6797
diff
changeset
|
143 |
sort Vartab.table * (*default sorts*) |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
144 |
(string list * term list Symtab.table)}; (*used type variables*) |
5819 | 145 |
|
146 |
exception CONTEXT of string * context; |
|
147 |
||
148 |
||
8373
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
149 |
fun make_context (thy, data, asms, binds, thms, cases, defs) = |
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
150 |
Context {thy = thy, data = data, asms = asms, binds = binds, thms = thms, |
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
151 |
cases = cases, defs = defs}; |
5819 | 152 |
|
8373
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
153 |
fun map_context f (Context {thy, data, asms, binds, thms, cases, defs}) = |
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
154 |
make_context (f (thy, data, asms, binds, thms, cases, defs)); |
5819 | 155 |
|
156 |
fun theory_of (Context {thy, ...}) = thy; |
|
157 |
val sign_of = Theory.sign_of o theory_of; |
|
158 |
||
7270 | 159 |
fun prems_of (Context {asms = ((_, asms), _), ...}) = flat (map #2 asms); |
160 |
||
5819 | 161 |
|
162 |
(** user data **) |
|
163 |
||
164 |
(* errors *) |
|
165 |
||
166 |
fun of_theory thy = "\nof theory " ^ Sign.str_of_sg (Theory.sign_of thy); |
|
167 |
||
168 |
fun err_inconsistent kinds = |
|
169 |
error ("Attempt to merge different versions of " ^ commas_quote kinds ^ " proof data"); |
|
170 |
||
171 |
fun err_dup_init thy kind = |
|
172 |
error ("Duplicate initialization of " ^ quote kind ^ " proof data" ^ of_theory thy); |
|
173 |
||
174 |
fun err_undef ctxt kind = |
|
175 |
raise CONTEXT ("Tried to access undefined " ^ quote kind ^ " proof data", ctxt); |
|
176 |
||
177 |
fun err_uninit ctxt kind = |
|
178 |
raise CONTEXT ("Tried to access uninitialized " ^ quote kind ^ " proof data" ^ |
|
179 |
of_theory (theory_of ctxt), ctxt); |
|
180 |
||
181 |
fun err_access ctxt kind = |
|
182 |
raise CONTEXT ("Unauthorized access to " ^ quote kind ^ " proof data" ^ |
|
183 |
of_theory (theory_of ctxt), ctxt); |
|
184 |
||
185 |
||
186 |
(* data kind 'Isar/proof_data' *) |
|
187 |
||
188 |
structure ProofDataDataArgs = |
|
189 |
struct |
|
190 |
val name = "Isar/proof_data"; |
|
191 |
type T = (Object.kind * ((theory -> Object.T) * (context -> Object.T -> unit))) Symtab.table; |
|
192 |
||
193 |
val empty = Symtab.empty; |
|
6550 | 194 |
val copy = I; |
5819 | 195 |
val prep_ext = I; |
196 |
fun merge tabs = Symtab.merge (Object.eq_kind o pairself fst) tabs |
|
197 |
handle Symtab.DUPS kinds => err_inconsistent kinds; |
|
198 |
fun print _ tab = Pretty.writeln (Pretty.strs (map #1 (Symtab.dest tab))); |
|
199 |
end; |
|
200 |
||
201 |
structure ProofDataData = TheoryDataFun(ProofDataDataArgs); |
|
202 |
val print_proof_data = ProofDataData.print; |
|
203 |
||
204 |
||
205 |
(* init proof data *) |
|
206 |
||
207 |
fun init_data kind meths thy = |
|
208 |
let |
|
209 |
val name = Object.name_of_kind kind; |
|
210 |
val tab = Symtab.update_new ((name, (kind, meths)), ProofDataData.get thy) |
|
211 |
handle Symtab.DUP _ => err_dup_init thy name; |
|
212 |
in thy |> ProofDataData.put tab end; |
|
213 |
||
214 |
||
215 |
(* access data *) |
|
216 |
||
217 |
fun lookup_data (ctxt as Context {data, ...}) kind = |
|
218 |
let |
|
219 |
val thy = theory_of ctxt; |
|
220 |
val name = Object.name_of_kind kind; |
|
221 |
in |
|
222 |
(case Symtab.lookup (ProofDataData.get thy, name) of |
|
223 |
Some (k, meths) => |
|
224 |
if Object.eq_kind (kind, k) then |
|
225 |
(case Symtab.lookup (data, name) of |
|
226 |
Some x => (x, meths) |
|
227 |
| None => err_undef ctxt name) |
|
228 |
else err_access ctxt name |
|
229 |
| None => err_uninit ctxt name) |
|
230 |
end; |
|
231 |
||
232 |
fun get_data kind f ctxt = |
|
233 |
let val (x, _) = lookup_data ctxt kind |
|
234 |
in f x handle Match => Object.kind_error kind end; |
|
235 |
||
236 |
fun print_data kind ctxt = |
|
237 |
let val (x, (_, prt)) = lookup_data ctxt kind |
|
238 |
in prt ctxt x end; |
|
239 |
||
240 |
fun put_data kind f x ctxt = |
|
8373
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
241 |
(lookup_data ctxt kind; |
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
242 |
ctxt |> map_context (fn (thy, data, asms, binds, thms, cases, defs) => |
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
243 |
(thy, Symtab.update ((Object.name_of_kind kind, f x), data), asms, binds, thms, cases, defs))); |
5819 | 244 |
|
245 |
||
246 |
(* init context *) |
|
247 |
||
5874 | 248 |
fun init thy = |
249 |
let val data = Symtab.map (fn (_, (f, _)) => f thy) (ProofDataData.get thy) in |
|
8426 | 250 |
make_context (thy, data, (([], []), ([], [])), Vartab.empty, Symtab.empty, [], |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
251 |
(Vartab.empty, Vartab.empty, ([], Symtab.empty))) |
5819 | 252 |
end; |
253 |
||
254 |
||
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
255 |
(* get assumptions *) |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
256 |
|
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
257 |
fun assumptions (Context {asms = ((asms, _), _), ...}) = asms; |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
258 |
fun fixed_names (Context {asms = (_, (fixes, _)), ...}) = map #2 fixes; |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
259 |
fun used_table (Context {defs = (_, _, (_, tab)), ...}) = tab; |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
260 |
|
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
261 |
|
8096 | 262 |
|
7663 | 263 |
(** default sorts and types **) |
264 |
||
7670 | 265 |
fun def_sort (Context {defs = (_, sorts, _), ...}) xi = Vartab.lookup (sorts, xi); |
7663 | 266 |
|
7670 | 267 |
fun def_type (Context {binds, defs = (types, _, _), ...}) is_pat xi = |
7663 | 268 |
(case Vartab.lookup (types, xi) of |
269 |
None => |
|
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
270 |
if is_pat then None else |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
271 |
(case Vartab.lookup (binds, xi) of |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
272 |
Some (Some (_, T)) => Some (TypeInfer.polymorphicT T) |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
273 |
| _ => None) |
7663 | 274 |
| some => some); |
275 |
||
276 |
||
5819 | 277 |
|
278 |
(** prepare types **) |
|
279 |
||
9504 | 280 |
local |
281 |
||
282 |
fun read_typ_aux read ctxt s = |
|
283 |
transform_error (read (sign_of ctxt, def_sort ctxt)) s |
|
7663 | 284 |
handle ERROR_MESSAGE msg => raise CONTEXT (msg, ctxt); |
5819 | 285 |
|
10554 | 286 |
fun cert_typ_aux cert ctxt raw_T = |
287 |
cert (sign_of ctxt) raw_T |
|
288 |
handle TYPE (msg, _, _) => raise CONTEXT (msg, ctxt); |
|
9504 | 289 |
|
290 |
in |
|
291 |
||
11925 | 292 |
val read_typ = read_typ_aux Sign.read_typ; |
9504 | 293 |
val read_typ_no_norm = read_typ_aux Sign.read_typ_no_norm; |
11925 | 294 |
val cert_typ = cert_typ_aux Sign.certify_typ; |
9504 | 295 |
val cert_typ_no_norm = cert_typ_aux Sign.certify_typ_no_norm; |
296 |
||
297 |
end; |
|
298 |
||
5819 | 299 |
|
7679 | 300 |
(* internalize Skolem constants *) |
301 |
||
9133 | 302 |
fun fixes_of (Context {asms = (_, (fixes, _)), ...}) = fixes; |
10583 | 303 |
fun lookup_skolem ctxt x = assoc (fixes_of ctxt, x); |
304 |
fun get_skolem ctxt x = if_none (lookup_skolem ctxt x) x; |
|
7679 | 305 |
|
9325 | 306 |
fun no_skolem no_internal ctxt x = |
9291
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
307 |
if can Syntax.dest_skolem x then |
7679 | 308 |
raise CONTEXT ("Illegal reference to internal Skolem constant: " ^ quote x, ctxt) |
9325 | 309 |
else if no_internal andalso can Syntax.dest_internal x then |
9291
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
310 |
raise CONTEXT ("Illegal reference to internal variable: " ^ quote x, ctxt) |
7679 | 311 |
else x; |
312 |
||
9291
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
313 |
fun intern_skolem ctxt = |
7679 | 314 |
let |
315 |
fun intern (t as Free (x, T)) = |
|
10583 | 316 |
(case lookup_skolem ctxt (no_skolem true ctxt x) of |
7679 | 317 |
Some x' => Free (x', T) |
318 |
| None => t) |
|
319 |
| intern (t $ u) = intern t $ intern u |
|
320 |
| intern (Abs (x, T, t)) = Abs (x, T, intern t) |
|
321 |
| intern a = a; |
|
322 |
in intern end; |
|
323 |
||
324 |
||
9133 | 325 |
(* externalize Skolem constants -- for printing purposes only *) |
326 |
||
327 |
fun extern_skolem ctxt = |
|
328 |
let |
|
329 |
val rev_fixes = map Library.swap (fixes_of ctxt); |
|
330 |
||
331 |
fun extern (t as Free (x, T)) = |
|
332 |
(case assoc (rev_fixes, x) of |
|
10583 | 333 |
Some x' => Free (if lookup_skolem ctxt x' = Some x then x' else NameSpace.hidden x', T) |
9133 | 334 |
| None => t) |
335 |
| extern (t $ u) = extern t $ extern u |
|
336 |
| extern (Abs (x, T, t)) = Abs (x, T, extern t) |
|
337 |
| extern a = a; |
|
338 |
in extern end |
|
339 |
||
8096 | 340 |
|
5819 | 341 |
(** prepare terms and propositions **) |
342 |
||
343 |
(* |
|
344 |
(1) read / certify wrt. signature of context |
|
345 |
(2) intern Skolem constants |
|
346 |
(3) expand term bindings |
|
347 |
*) |
|
348 |
||
349 |
||
350 |
(* read / certify wrt. signature *) (*exception ERROR*) (*exception TERM*) |
|
351 |
||
5874 | 352 |
fun read_def_termTs freeze sg (types, sorts, used) sTs = |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
353 |
Sign.read_def_terms (sg, types, sorts) used freeze sTs; |
5874 | 354 |
|
355 |
fun read_def_termT freeze sg defs sT = apfst hd (read_def_termTs freeze sg defs [sT]); |
|
356 |
||
8096 | 357 |
fun read_term_sg freeze sg defs s = #1 (read_def_termT freeze sg defs (s, TypeInfer.logicT)); |
358 |
fun read_prop_sg freeze sg defs s = #1 (read_def_termT freeze sg defs (s, propT)); |
|
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
359 |
fun read_terms_sg freeze sg defs = |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
360 |
#1 o read_def_termTs freeze sg defs o map (rpair TypeInfer.logicT); |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
361 |
fun read_props_sg freeze sg defs = #1 o read_def_termTs freeze sg defs o map (rpair propT); |
5819 | 362 |
|
363 |
||
364 |
fun cert_term_sg sg t = Thm.term_of (Thm.cterm_of sg t); |
|
365 |
||
366 |
fun cert_prop_sg sg tm = |
|
367 |
let |
|
368 |
val ctm = Thm.cterm_of sg tm; |
|
369 |
val {t, T, ...} = Thm.rep_cterm ctm; |
|
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
370 |
in if T = propT then t else raise TERM ("Term not of type prop", [t]) end; |
5819 | 371 |
|
372 |
||
373 |
(* norm_term *) |
|
374 |
||
375 |
(*beta normal form for terms (not eta normal form), chase variables in |
|
376 |
bindings environment (code taken from Pure/envir.ML)*) |
|
377 |
||
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
378 |
fun unifyT ctxt (T, U) = |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
379 |
let val maxidx = Int.max (Term.maxidx_of_typ T, Term.maxidx_of_typ U) |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
380 |
in #1 (Type.unify (Sign.tsig_of (sign_of ctxt)) maxidx Vartab.empty (T, U)) end; |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
381 |
|
10554 | 382 |
fun norm_term (ctxt as Context {binds, ...}) schematic = |
5819 | 383 |
let |
384 |
(*raised when norm has no effect on a term, to do sharing instead of copying*) |
|
385 |
exception SAME; |
|
386 |
||
387 |
fun norm (t as Var (xi, T)) = |
|
388 |
(case Vartab.lookup (binds, xi) of |
|
7606 | 389 |
Some (Some (u, U)) => |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
390 |
let |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
391 |
val env = unifyT ctxt (T, U) handle Type.TUNIFY => |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
392 |
raise TYPE ("norm_term: ill-typed variable assigment", [T, U], [t, u]); |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
393 |
val u' = Term.subst_TVars_Vartab env u; |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
394 |
in norm u' handle SAME => u' end |
10554 | 395 |
| _ => |
396 |
if schematic then raise SAME |
|
397 |
else raise CONTEXT ("Unbound schematic variable: " ^ Syntax.string_of_vname xi, ctxt)) |
|
5819 | 398 |
| norm (Abs (a, T, body)) = Abs (a, T, norm body) |
399 |
| norm (Abs (_, _, body) $ t) = normh (subst_bound (t, body)) |
|
400 |
| norm (f $ t) = |
|
401 |
((case norm f of |
|
402 |
Abs (_, _, body) => normh (subst_bound (t, body)) |
|
403 |
| nf => nf $ (norm t handle SAME => t)) handle SAME => f $ norm t) |
|
404 |
| norm _ = raise SAME |
|
405 |
and normh t = norm t handle SAME => t |
|
406 |
in normh end; |
|
407 |
||
408 |
||
6550 | 409 |
(* dummy patterns *) |
410 |
||
9540 | 411 |
fun prepare_dummies t = #2 (Term.replace_dummy_patterns (1, t)); |
6762 | 412 |
|
9540 | 413 |
fun reject_dummies ctxt t = Term.no_dummy_patterns t |
414 |
handle TERM _ => raise CONTEXT ("Illegal dummy pattern(s) in term", ctxt); |
|
6550 | 415 |
|
416 |
||
5819 | 417 |
(* read terms *) |
418 |
||
10554 | 419 |
local |
420 |
||
421 |
fun gen_read read app is_pat schematic (ctxt as Context {defs = (_, _, (used, _)), ...}) s = |
|
7663 | 422 |
(transform_error (read (sign_of ctxt) (def_type ctxt is_pat, def_sort ctxt, used)) s |
423 |
handle TERM (msg, _) => raise CONTEXT (msg, ctxt) |
|
424 |
| ERROR_MESSAGE msg => raise CONTEXT (msg, ctxt)) |
|
9291
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
425 |
|> app (intern_skolem ctxt) |
10554 | 426 |
|> app (if is_pat then I else norm_term ctxt schematic) |
7663 | 427 |
|> app (if is_pat then prepare_dummies else (reject_dummies ctxt)); |
5819 | 428 |
|
10554 | 429 |
in |
430 |
||
431 |
val read_termTs = gen_read (read_def_termTs false) (apfst o map) false false; |
|
432 |
val read_termT_pats = #1 oo gen_read (read_def_termTs false) (apfst o map) true false; |
|
8096 | 433 |
|
434 |
fun read_term_pats T ctxt pats = read_termT_pats ctxt (map (rpair T) pats); |
|
435 |
val read_prop_pats = read_term_pats propT; |
|
436 |
||
10554 | 437 |
val read_term = gen_read (read_term_sg true) I false false; |
438 |
val read_prop = gen_read (read_prop_sg true) I false false; |
|
11925 | 439 |
val read_prop_schematic = gen_read (read_prop_sg true) I false true; |
10554 | 440 |
val read_terms = gen_read (read_terms_sg true) map false false; |
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
441 |
val read_props = gen_read (read_props_sg true) map false; |
5819 | 442 |
|
10554 | 443 |
end; |
444 |
||
5819 | 445 |
|
446 |
(* certify terms *) |
|
447 |
||
10554 | 448 |
local |
449 |
||
450 |
fun gen_cert cert is_pat schematic ctxt t = t |
|
451 |
|> (if is_pat then I else norm_term ctxt schematic) |
|
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
452 |
|> (fn t' => cert (sign_of ctxt) t' handle TERM (msg, _) => raise CONTEXT (msg, ctxt)); |
5819 | 453 |
|
10554 | 454 |
in |
8096 | 455 |
|
10554 | 456 |
val cert_term = gen_cert cert_term_sg false false; |
457 |
val cert_prop = gen_cert cert_prop_sg false false; |
|
458 |
val cert_props = map oo gen_cert cert_prop_sg false; |
|
459 |
||
460 |
fun cert_term_pats _ = map o gen_cert cert_term_sg true false; |
|
461 |
val cert_prop_pats = map o gen_cert cert_prop_sg true false; |
|
462 |
||
463 |
end; |
|
5819 | 464 |
|
465 |
||
466 |
(* declare terms *) |
|
467 |
||
10381 | 468 |
local |
469 |
||
5819 | 470 |
val ins_types = foldl_aterms |
471 |
(fn (types, Free (x, T)) => Vartab.update (((x, ~1), T), types) |
|
472 |
| (types, Var v) => Vartab.update (v, types) |
|
473 |
| (types, _) => types); |
|
474 |
||
475 |
val ins_sorts = foldl_types (foldl_atyps |
|
476 |
(fn (sorts, TFree (x, S)) => Vartab.update (((x, ~1), S), sorts) |
|
477 |
| (sorts, TVar v) => Vartab.update (v, sorts) |
|
478 |
| (sorts, _) => sorts)); |
|
479 |
||
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
480 |
val ins_used = foldl_term_types (fn t => foldl_atyps |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
481 |
(fn ((used, tab), TFree (x, _)) => (x ins used, Symtab.update_multi ((x, t), tab)) |
5819 | 482 |
| (used, _) => used)); |
483 |
||
7663 | 484 |
fun ins_skolem def_ty = foldr |
5994 | 485 |
(fn ((x, x'), types) => |
7663 | 486 |
(case def_ty x' of |
5994 | 487 |
Some T => Vartab.update (((x, ~1), T), types) |
488 |
| None => types)); |
|
489 |
||
8373
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
490 |
fun map_defaults f = map_context (fn (thy, data, asms, binds, thms, cases, defs) => |
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
491 |
(thy, data, asms, binds, thms, cases, f defs)); |
5819 | 492 |
|
10381 | 493 |
fun declare_syn (ctxt, t) = |
5819 | 494 |
ctxt |
7670 | 495 |
|> map_defaults (fn (types, sorts, used) => (ins_types (types, t), sorts, used)) |
10381 | 496 |
|> map_defaults (fn (types, sorts, used) => (types, ins_sorts (sorts, t), used)); |
497 |
||
498 |
fun declare_occ (ctxt as Context {asms = (_, (fixes, _)), ...}, t) = |
|
499 |
declare_syn (ctxt, t) |
|
7670 | 500 |
|> map_defaults (fn (types, sorts, used) => (types, sorts, ins_used (used, t))) |
501 |
|> map_defaults (fn (types, sorts, used) => |
|
502 |
(ins_skolem (fn x => Vartab.lookup (types, (x, ~1))) (fixes, types), sorts, used)); |
|
5819 | 503 |
|
10381 | 504 |
in |
5819 | 505 |
|
10381 | 506 |
fun declare_term t ctxt = declare_occ (ctxt, t); |
507 |
fun declare_terms ts ctxt = foldl declare_occ (ctxt, ts); |
|
508 |
fun predeclare_terms ts ctxt = foldl declare_syn (ctxt, ts); |
|
509 |
||
510 |
end; |
|
5819 | 511 |
|
512 |
||
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
513 |
|
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
514 |
(** Hindley-Milner polymorphism **) |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
515 |
|
7925 | 516 |
(* warn_extra_tfrees *) |
517 |
||
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
518 |
local |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
519 |
|
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
520 |
fun used_free (a, ts) = |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
521 |
(case mapfilter (fn Free (x, _) => Some x | _ => None) ts of |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
522 |
[] => None |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
523 |
| xs => Some (a, xs)); |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
524 |
|
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
525 |
fun warn_extra (names1: string list, tab1) (names2, tab2) = |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
526 |
if names1 = names2 then () |
7925 | 527 |
else |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
528 |
let |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
529 |
val extra = |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
530 |
Library.gen_rems Library.eq_fst (Symtab.dest tab2, Symtab.dest tab1) |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
531 |
|> mapfilter used_free; |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
532 |
val tfrees = map #1 extra; |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
533 |
val frees = Library.sort_strings (Library.distinct (flat (map #2 extra))); |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
534 |
in |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
535 |
if null extra then () |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
536 |
else warning ("Danger! Just introduced free type variable(s): " ^ commas tfrees ^ " in " ^ |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
537 |
space_implode " or " frees) |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
538 |
end; |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
539 |
|
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
540 |
in |
7925 | 541 |
|
542 |
fun warn_extra_tfrees (ctxt1 as Context {defs = (_, _, used1), ...}) |
|
543 |
(ctxt2 as Context {defs = (_, _, used2), ...}) = (warn_extra used1 used2; ctxt2); |
|
544 |
||
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
545 |
end; |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
546 |
|
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
547 |
|
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
548 |
(* generalize type variables *) |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
549 |
|
12008 | 550 |
fun gen_tfrees inner outer = |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
551 |
let |
12008 | 552 |
val extra_fixes = fixed_names inner \\ fixed_names outer; |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
553 |
fun still_fixed (Free (x, _)) = not (x mem_string extra_fixes) |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
554 |
| still_fixed _ = false; |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
555 |
fun add (gen, (a, xs)) = |
12008 | 556 |
if is_some (Symtab.lookup (used_table outer, a)) orelse exists still_fixed xs |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
557 |
then gen else a :: gen; |
12008 | 558 |
in Symtab.foldl add ([], used_table inner) end; |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
559 |
|
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
560 |
fun generalizeT inner outer = |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
561 |
let |
12008 | 562 |
val tfrees = gen_tfrees inner outer; |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
563 |
fun gen (x, S) = if x mem_string tfrees then TVar ((x, 0), S) else TFree (x, S); |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
564 |
in Term.map_type_tfree gen end; |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
565 |
|
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
566 |
val generalize = Term.map_term_types oo generalizeT; |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
567 |
|
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
568 |
|
9553 | 569 |
|
570 |
(** export theorems **) |
|
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
571 |
|
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
572 |
fun get_free x (None, t as Free (y, _)) = if x = y then Some t else None |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
573 |
| get_free _ (opt, _) = opt; |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
574 |
|
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
575 |
fun find_free t x = foldl_aterms (get_free x) (None, t); |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
576 |
|
12008 | 577 |
fun export is_goal inner outer = |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
578 |
let |
12008 | 579 |
val tfrees = gen_tfrees inner outer; |
580 |
val fixes = fixed_names inner \\ fixed_names outer; |
|
581 |
val asms = Library.drop (length (assumptions outer), assumptions inner); |
|
11816 | 582 |
val exp_asms = map (fn (cprops, exp) => exp is_goal cprops) asms; |
583 |
in fn thm => |
|
584 |
thm |
|
585 |
|> Tactic.norm_hhf |
|
586 |
|> Seq.EVERY (rev exp_asms) |
|
587 |
|> Seq.map (fn rule => |
|
588 |
let |
|
589 |
val {sign, prop, ...} = Thm.rep_thm rule; |
|
590 |
val frees = map (Thm.cterm_of sign) (mapfilter (find_free prop) fixes); |
|
591 |
in |
|
592 |
rule |
|
593 |
|> Drule.forall_intr_list frees |
|
594 |
|> Tactic.norm_hhf |
|
595 |
|> Drule.tvars_intr_list tfrees |
|
596 |
end) |
|
597 |
end; |
|
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
598 |
|
7925 | 599 |
|
5819 | 600 |
|
601 |
(** bindings **) |
|
602 |
||
603 |
(* update_binds *) |
|
604 |
||
7606 | 605 |
fun del_bind (ctxt, xi) = |
606 |
ctxt |
|
8373
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
607 |
|> map_context (fn (thy, data, asms, binds, thms, cases, defs) => |
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
608 |
(thy, data, asms, Vartab.update ((xi, None), binds), thms, cases, defs)); |
7606 | 609 |
|
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
610 |
fun upd_bind (ctxt, ((x, i), t)) = |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
611 |
let |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
612 |
val T = Term.fastype_of t; |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
613 |
val t' = |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
614 |
if null (Term.term_tvars t \\ Term.typ_tvars T) then t |
8637 | 615 |
else Var ((x ^ "_has_extra_type_vars_on_rhs", i), T); |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
616 |
in |
5819 | 617 |
ctxt |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
618 |
|> declare_term t' |
8373
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
619 |
|> map_context (fn (thy, data, asms, binds, thms, cases, defs) => |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
620 |
(thy, data, asms, Vartab.update (((x, i), Some (t', T)), binds), thms, cases, defs)) |
5819 | 621 |
end; |
622 |
||
7606 | 623 |
fun del_upd_bind (ctxt, (xi, None)) = del_bind (ctxt, xi) |
624 |
| del_upd_bind (ctxt, (xi, Some t)) = upd_bind (ctxt, (xi, t)); |
|
625 |
||
5819 | 626 |
fun update_binds bs ctxt = foldl upd_bind (ctxt, bs); |
7606 | 627 |
fun delete_update_binds bs ctxt = foldl del_upd_bind (ctxt, bs); |
628 |
||
5819 | 629 |
|
8096 | 630 |
(* simult_matches *) |
631 |
||
10554 | 632 |
local |
633 |
||
634 |
val add_vars = Term.foldl_aterms (fn (vs, Var (xi, _)) => xi ins vs | (vs, _) => vs); |
|
635 |
fun vars_of ts = foldl add_vars ([], ts); |
|
636 |
||
637 |
in |
|
638 |
||
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
639 |
fun simult_matches ctxt [] = [] |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
640 |
| simult_matches ctxt pairs = |
8096 | 641 |
let |
10554 | 642 |
fun fail () = raise CONTEXT ("Pattern match failed!", ctxt); |
643 |
||
8096 | 644 |
val maxidx = foldl (fn (i, (t1, t2)) => |
645 |
Int.max (i, Int.max (Term.maxidx_of_term t1, Term.maxidx_of_term t2))) (~1, pairs); |
|
10554 | 646 |
val envs = Unify.smash_unifiers (sign_of ctxt, Envir.empty maxidx, |
647 |
map swap pairs); (*prefer assignment of variables from patterns*) |
|
648 |
val env = |
|
8096 | 649 |
(case Seq.pull envs of |
10554 | 650 |
None => fail () |
651 |
| Some (env, _) => env); (*ignore further results*) |
|
652 |
val domain = filter_out Term.is_replaced_dummy_pattern (vars_of (map #1 pairs)); |
|
653 |
val _ = (*may not assign variables from text*) |
|
654 |
if null (map #1 (Envir.alist_of env) inter vars_of (map #2 pairs)) then () |
|
655 |
else fail (); |
|
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
656 |
fun norm_bind (xi, t) = if xi mem domain then Some (xi, Envir.norm_term env t) else None; |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
657 |
in mapfilter norm_bind (Envir.alist_of env) end; |
8096 | 658 |
|
10554 | 659 |
end; |
660 |
||
8096 | 661 |
|
662 |
(* add_binds(_i) *) |
|
5819 | 663 |
|
7925 | 664 |
local |
665 |
||
5819 | 666 |
fun gen_bind prep (ctxt, (xi as (x, _), raw_t)) = |
7606 | 667 |
ctxt |> delete_update_binds [(xi, apsome (prep ctxt) raw_t)]; |
5819 | 668 |
|
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
669 |
fun gen_binds prep binds ctxt = foldl (gen_bind prep) (ctxt, binds); |
7925 | 670 |
|
10810 | 671 |
in |
672 |
||
10554 | 673 |
fun drop_schematic (b as (xi, Some t)) = if null (Term.term_vars t) then b else (xi, None) |
674 |
| drop_schematic b = b; |
|
675 |
||
5819 | 676 |
val add_binds = gen_binds read_term; |
677 |
val add_binds_i = gen_binds cert_term; |
|
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
678 |
|
11764 | 679 |
fun auto_bind_goal t ctxt = |
680 |
ctxt |> add_binds_i (map drop_schematic (AutoBind.goal (sign_of ctxt) t)); |
|
681 |
||
682 |
fun auto_bind_facts name ts ctxt = |
|
683 |
ctxt |> add_binds_i (map drop_schematic (AutoBind.facts (sign_of ctxt) name ts)); |
|
7925 | 684 |
|
685 |
end; |
|
5819 | 686 |
|
687 |
||
8096 | 688 |
(* match_bind(_i) *) |
5819 | 689 |
|
8096 | 690 |
local |
691 |
||
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
692 |
fun prep_bind prep_pats (ctxt, (raw_pats, t)) = |
5819 | 693 |
let |
8096 | 694 |
val ctxt' = declare_term t ctxt; |
695 |
val pats = prep_pats (fastype_of t) ctxt' raw_pats; |
|
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
696 |
val binds = simult_matches ctxt' (map (rpair t) pats); |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
697 |
in (ctxt', binds) end; |
7670 | 698 |
|
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
699 |
fun gen_binds prep_terms prep_pats gen raw_binds ctxt = |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
700 |
let |
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
701 |
val ts = prep_terms ctxt (map snd raw_binds); |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
702 |
val (ctxt', binds) = |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
703 |
apsnd flat (foldl_map (prep_bind prep_pats) (ctxt, map fst raw_binds ~~ ts)); |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
704 |
val binds' = |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
705 |
if gen then map (apsnd (generalize ctxt' ctxt)) binds |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
706 |
else binds; |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
707 |
val binds'' = map (apsnd Some) binds'; |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
708 |
in |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
709 |
warn_extra_tfrees ctxt |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
710 |
(if gen then ctxt (*sic!*) |> declare_terms (map #2 binds') |> add_binds_i binds'' |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
711 |
else ctxt' |> add_binds_i binds'') |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
712 |
end; |
8096 | 713 |
|
714 |
in |
|
5935 | 715 |
|
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
716 |
val match_bind = gen_binds read_terms read_term_pats; |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
717 |
val match_bind_i = gen_binds (map o cert_term) cert_term_pats; |
8096 | 718 |
|
719 |
end; |
|
5935 | 720 |
|
721 |
||
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
722 |
(* propositions with patterns *) |
5935 | 723 |
|
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
724 |
local |
8096 | 725 |
|
10554 | 726 |
fun prep_propp schematic prep_props prep_pats (context, args) = |
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
727 |
let |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
728 |
fun prep ((ctxt, prop :: props), (_, (raw_pats1, raw_pats2))) = |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
729 |
let |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
730 |
val ctxt' = declare_term prop ctxt; |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
731 |
val pats = prep_pats ctxt' (raw_pats1 @ raw_pats2); (*simultaneous type inference!*) |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
732 |
val len1 = length raw_pats1; |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
733 |
in ((ctxt', props), (prop, (take (len1, pats), drop (len1, pats)))) end |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
734 |
| prep _ = sys_error "prep_propp"; |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
735 |
val ((context', _), propp) = foldl_map (foldl_map prep) |
10554 | 736 |
((context, prep_props schematic context (flat (map (map fst) args))), args); |
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
737 |
in (context', propp) end; |
5935 | 738 |
|
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
739 |
fun matches ctxt (prop, (pats1, pats2)) = |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
740 |
simult_matches ctxt (map (rpair prop) pats1 @ map (rpair (Logic.strip_imp_concl prop)) pats2); |
8096 | 741 |
|
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
742 |
fun gen_bind_propp prepp (ctxt, raw_args) = |
8096 | 743 |
let |
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
744 |
val (ctxt', args) = prepp (ctxt, raw_args); |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
745 |
val binds = flat (flat (map (map (matches ctxt')) args)); |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
746 |
val propss = map (map #1) args; |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
747 |
|
10554 | 748 |
(*generalize result: context evaluated now, binds added later*) |
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
749 |
val gen = generalize ctxt' ctxt; |
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
750 |
fun gen_binds c = c |> add_binds_i (map (apsnd (Some o gen)) binds); |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
751 |
in (ctxt' |> add_binds_i (map (apsnd Some) binds), (propss, gen_binds)) end; |
8096 | 752 |
|
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
753 |
in |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
754 |
|
11925 | 755 |
val read_propp = prep_propp false read_props read_prop_pats; |
756 |
val cert_propp = prep_propp false cert_props cert_prop_pats; |
|
10554 | 757 |
val read_propp_schematic = prep_propp true read_props read_prop_pats; |
758 |
val cert_propp_schematic = prep_propp true cert_props cert_prop_pats; |
|
759 |
||
11925 | 760 |
val bind_propp = gen_bind_propp read_propp; |
761 |
val bind_propp_i = gen_bind_propp cert_propp; |
|
762 |
val bind_propp_schematic = gen_bind_propp read_propp_schematic; |
|
10554 | 763 |
val bind_propp_schematic_i = gen_bind_propp cert_propp_schematic; |
6789 | 764 |
|
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
765 |
end; |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
766 |
|
6789 | 767 |
|
5819 | 768 |
|
769 |
(** theorems **) |
|
770 |
||
6091 | 771 |
(* get_thm(s) *) |
5819 | 772 |
|
9566 | 773 |
(*beware of proper order of evaluation!*) |
5819 | 774 |
|
9566 | 775 |
fun retrieve_thms f g (ctxt as Context {thy, thms, ...}) = |
776 |
let |
|
777 |
val sg_ref = Sign.self_ref (Theory.sign_of thy); |
|
778 |
val get_from_thy = f thy; |
|
779 |
in |
|
780 |
fn name => |
|
781 |
(case Symtab.lookup (thms, name) of |
|
782 |
Some (Some ths) => map (Thm.transfer_sg (Sign.deref sg_ref)) ths |
|
783 |
| _ => get_from_thy name) |> g name |
|
784 |
end; |
|
5819 | 785 |
|
9566 | 786 |
val get_thm = retrieve_thms PureThy.get_thms PureThy.single_thm; |
787 |
val get_thm_closure = retrieve_thms PureThy.get_thms_closure PureThy.single_thm; |
|
788 |
val get_thms = retrieve_thms PureThy.get_thms (K I); |
|
789 |
val get_thms_closure = retrieve_thms PureThy.get_thms_closure (K I); |
|
5819 | 790 |
|
791 |
||
6091 | 792 |
(* put_thm(s) *) |
5819 | 793 |
|
7479 | 794 |
fun put_thms ("", _) = I |
795 |
| put_thms (name, ths) = map_context |
|
8373
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
796 |
(fn (thy, data, asms, binds, thms, cases, defs) => |
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
797 |
(thy, data, asms, binds, Symtab.update ((name, Some ths), thms), cases, defs)); |
5819 | 798 |
|
6091 | 799 |
fun put_thm (name, th) = put_thms (name, [th]); |
5819 | 800 |
|
6091 | 801 |
fun put_thmss [] ctxt = ctxt |
802 |
| put_thmss (th :: ths) ctxt = ctxt |> put_thms th |> put_thmss ths; |
|
5819 | 803 |
|
804 |
||
7606 | 805 |
(* reset_thms *) |
806 |
||
8373
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
807 |
fun reset_thms name = map_context (fn (thy, data, asms, binds, thms, cases, defs) => |
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
808 |
(thy, data, asms, binds, Symtab.update ((name, None), thms), cases, defs)); |
7606 | 809 |
|
810 |
||
6091 | 811 |
(* have_thmss *) |
5819 | 812 |
|
9196 | 813 |
fun have_thss (ctxt, ((name, more_attrs), ths_attrs)) = |
5819 | 814 |
let |
815 |
fun app ((ct, ths), (th, attrs)) = |
|
6091 | 816 |
let val (ct', th') = Thm.applys_attributes ((ct, th), attrs @ more_attrs) |
5819 | 817 |
in (ct', th' :: ths) end |
818 |
val (ctxt', rev_thms) = foldl app ((ctxt, []), ths_attrs); |
|
9196 | 819 |
val thms = flat (rev rev_thms); |
6091 | 820 |
in (ctxt' |> put_thms (name, thms), (name, thms)) end; |
5819 | 821 |
|
9196 | 822 |
fun have_thmss args ctxt = foldl_map have_thss (ctxt, args); |
823 |
||
5819 | 824 |
|
825 |
||
826 |
(** assumptions **) |
|
827 |
||
11918
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
828 |
(* basic exporters *) |
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
829 |
|
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
830 |
fun export_assume true = Seq.single oo Drule.implies_intr_goals |
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
831 |
| export_assume false = Seq.single oo Drule.implies_intr_list; |
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
832 |
|
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
833 |
fun export_presume _ = export_assume false; |
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
834 |
|
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
835 |
|
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
836 |
fun dest_lhs cprop = |
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
837 |
let val x = #1 (Logic.dest_equals (Thm.term_of cprop)) |
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
838 |
in Term.dest_Free x; Thm.cterm_of (Thm.sign_of_cterm cprop) x end |
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
839 |
handle TERM _ => raise TERM ("Malformed definitional assumption encountered:\n" ^ |
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
840 |
quote (Display.string_of_cterm cprop), []); |
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
841 |
|
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
842 |
fun export_def _ cprops thm = |
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
843 |
thm |
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
844 |
|> Drule.implies_intr_list cprops |
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
845 |
|> Drule.forall_intr_list (map dest_lhs cprops) |
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
846 |
|> Drule.forall_elim_vars 0 |
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
847 |
|> RANGE (replicate (length cprops) (Tactic.rtac Drule.reflexive_thm)) 1; |
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
848 |
|
dfdf0798d7b8
added export_assume, export_presume, export_def (from proof.ML);
wenzelm
parents:
11915
diff
changeset
|
849 |
|
5819 | 850 |
(* assume *) |
851 |
||
7270 | 852 |
local |
6797 | 853 |
|
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
854 |
fun add_assm (ctxt, ((name, attrs), props)) = |
5819 | 855 |
let |
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
856 |
val cprops = map (Thm.cterm_of (sign_of ctxt)) props; |
11816 | 857 |
val asms = map (Tactic.norm_hhf o Thm.assume) cprops; |
5919 | 858 |
|
859 |
val ths = map (fn th => ([th], [])) asms; |
|
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
860 |
val (ctxt', [(_, thms)]) = |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
861 |
ctxt |
6797 | 862 |
|> auto_bind_facts name props |
11526 | 863 |
|> have_thmss [((name, attrs), ths)]; |
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
864 |
in (ctxt', (cprops, (name, asms), (name, thms))) end; |
5819 | 865 |
|
9470 | 866 |
fun gen_assms prepp exp args ctxt = |
867 |
let |
|
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
868 |
val (ctxt1, propss) = prepp (ctxt, map snd args); |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
869 |
val (ctxt2, results) = foldl_map add_assm (ctxt1, map fst args ~~ propss); |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
870 |
|
9470 | 871 |
val cprops = flat (map #1 results); |
872 |
val asmss = map #2 results; |
|
873 |
val thmss = map #3 results; |
|
10465
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
874 |
val ctxt3 = |
4aa6f8b5cdc4
added read_terms, read_props (simulataneous type-inference);
wenzelm
parents:
10381
diff
changeset
|
875 |
ctxt2 |
8373
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
876 |
|> map_context (fn (thy, data, ((asms_ct, asms_th), fixes), binds, thms, cases, defs) => |
9470 | 877 |
(thy, data, ((asms_ct @ [(cprops, exp)], asms_th @ asmss), fixes), binds, thms, |
878 |
cases, defs)); |
|
11925 | 879 |
val ctxt4 = ctxt3 |> put_thms ("prems", prems_of ctxt3); |
880 |
in (warn_extra_tfrees ctxt ctxt4, thmss) end; |
|
5819 | 881 |
|
7270 | 882 |
in |
883 |
||
8616
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
884 |
val assume = gen_assms (apsnd #1 o bind_propp); |
90d2fed59be1
support Hindley-Milner polymorphisms in binds and facts;
wenzelm
parents:
8462
diff
changeset
|
885 |
val assume_i = gen_assms (apsnd #1 o bind_propp_i); |
7270 | 886 |
|
887 |
end; |
|
5819 | 888 |
|
889 |
||
8096 | 890 |
(* variables *) |
891 |
||
10381 | 892 |
local |
893 |
||
11925 | 894 |
fun prep_vars prep_typ no_internal (ctxt, (xs, raw_T)) = |
8096 | 895 |
let |
11925 | 896 |
val _ = (case filter (not o Syntax.is_identifier) (map (no_skolem no_internal ctxt) xs) of |
8096 | 897 |
[] => () | bads => raise CONTEXT ("Bad variable name(s): " ^ commas_quote bads, ctxt)); |
898 |
||
899 |
val opt_T = apsome (prep_typ ctxt) raw_T; |
|
900 |
val T = if_none opt_T TypeInfer.logicT; |
|
10381 | 901 |
val ctxt' = ctxt |> predeclare_terms (map (fn x => Free (x, T)) xs); |
8096 | 902 |
in (ctxt', (xs, opt_T)) end; |
903 |
||
10381 | 904 |
in |
905 |
||
11925 | 906 |
val read_vars = prep_vars read_typ true; |
907 |
val cert_vars = prep_vars cert_typ false; |
|
8096 | 908 |
|
10381 | 909 |
end; |
910 |
||
8096 | 911 |
|
5819 | 912 |
(* fix *) |
913 |
||
8096 | 914 |
local |
915 |
||
8373
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
916 |
fun map_vars f = map_context (fn (thy, data, (assumes, vars), binds, thms, cases, defs) => |
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
917 |
(thy, data, (assumes, f vars), binds, thms, cases, defs)); |
5819 | 918 |
|
11925 | 919 |
fun err_dups ctxt xs = raise CONTEXT ("Duplicate variable(s): " ^ commas_quote xs, ctxt); |
920 |
||
921 |
fun add_vars _ xs (fixes, used) = |
|
922 |
let |
|
923 |
val xs' = variantlist (xs, used); |
|
924 |
val fixes' = (xs ~~ map Syntax.skolem xs') @ fixes; |
|
925 |
val used' = xs' @ used; |
|
926 |
in (fixes', used') end; |
|
927 |
||
928 |
fun add_vars_direct ctxt xs (fixes, used) = |
|
929 |
(case xs inter_string map #1 fixes of |
|
930 |
[] => ((xs ~~ xs) @ fixes, xs @ used) |
|
931 |
| dups => err_dups ctxt dups); |
|
932 |
||
933 |
fun gen_fix prep add raw_vars ctxt = |
|
8096 | 934 |
let |
935 |
val (ctxt', varss) = foldl_map prep (ctxt, raw_vars); |
|
936 |
val xs = flat (map fst varss); |
|
937 |
in |
|
11925 | 938 |
(case Library.duplicates xs of [] => () | dups => err_dups ctxt dups); |
939 |
ctxt' |> map_vars (add ctxt xs) |
|
8096 | 940 |
end; |
5819 | 941 |
|
8096 | 942 |
in |
7679 | 943 |
|
11925 | 944 |
val fix = gen_fix read_vars add_vars; |
945 |
val fix_i = gen_fix cert_vars add_vars; |
|
946 |
val fix_direct = gen_fix cert_vars add_vars_direct; |
|
8096 | 947 |
|
948 |
end; |
|
5819 | 949 |
|
6895 | 950 |
|
9291
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
951 |
(*Note: improper use may result in variable capture / dynamic scoping!*) |
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
952 |
fun bind_skolem ctxt xs = |
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
953 |
let |
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
954 |
val ctxt' = ctxt |> fix_i [(xs, None)]; |
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
955 |
fun bind (t as Free (x, T)) = |
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
956 |
if x mem_string xs then |
10583 | 957 |
(case lookup_skolem ctxt' x of Some x' => Free (x', T) | None => t) |
9291
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
958 |
else t |
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
959 |
| bind (t $ u) = bind t $ bind u |
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
960 |
| bind (Abs (x, T, t)) = Abs (x, T, bind t) |
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
961 |
| bind a = a; |
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
962 |
in bind end; |
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
963 |
|
23705d14be8f
"_i" arguments now expected to have skolems already internalized;
wenzelm
parents:
9274
diff
changeset
|
964 |
|
5819 | 965 |
|
8373
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
966 |
(** cases **) |
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
967 |
|
11793
5f0ab6f5c280
support impromptu terminology of cases parameters;
wenzelm
parents:
11764
diff
changeset
|
968 |
fun prep_case ctxt name xs {fixes, assumes, binds} = |
5f0ab6f5c280
support impromptu terminology of cases parameters;
wenzelm
parents:
11764
diff
changeset
|
969 |
let |
5f0ab6f5c280
support impromptu terminology of cases parameters;
wenzelm
parents:
11764
diff
changeset
|
970 |
fun replace (opt_x :: xs) ((y, T) :: ys) = (if_none opt_x y, T) :: replace xs ys |
5f0ab6f5c280
support impromptu terminology of cases parameters;
wenzelm
parents:
11764
diff
changeset
|
971 |
| replace [] ys = ys |
5f0ab6f5c280
support impromptu terminology of cases parameters;
wenzelm
parents:
11764
diff
changeset
|
972 |
| replace (_ :: _) [] = raise CONTEXT ("Too many parameters for case " ^ quote name, ctxt); |
5f0ab6f5c280
support impromptu terminology of cases parameters;
wenzelm
parents:
11764
diff
changeset
|
973 |
in |
5f0ab6f5c280
support impromptu terminology of cases parameters;
wenzelm
parents:
11764
diff
changeset
|
974 |
if null (foldr Term.add_typ_tvars (map snd fixes, [])) andalso |
5f0ab6f5c280
support impromptu terminology of cases parameters;
wenzelm
parents:
11764
diff
changeset
|
975 |
null (foldr Term.add_term_vars (assumes, [])) then |
5f0ab6f5c280
support impromptu terminology of cases parameters;
wenzelm
parents:
11764
diff
changeset
|
976 |
{fixes = replace xs fixes, assumes = assumes, binds = map drop_schematic binds} |
5f0ab6f5c280
support impromptu terminology of cases parameters;
wenzelm
parents:
11764
diff
changeset
|
977 |
else raise CONTEXT ("Illegal schematic variable(s) in case " ^ quote name, ctxt) |
5f0ab6f5c280
support impromptu terminology of cases parameters;
wenzelm
parents:
11764
diff
changeset
|
978 |
end; |
8403 | 979 |
|
11793
5f0ab6f5c280
support impromptu terminology of cases parameters;
wenzelm
parents:
11764
diff
changeset
|
980 |
fun get_case (ctxt as Context {cases, ...}) name xs = |
8426 | 981 |
(case assoc (cases, name) of |
8373
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
982 |
None => raise CONTEXT ("Unknown case: " ^ quote name, ctxt) |
11793
5f0ab6f5c280
support impromptu terminology of cases parameters;
wenzelm
parents:
11764
diff
changeset
|
983 |
| Some c => prep_case ctxt name xs c); |
8373
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
984 |
|
8384 | 985 |
|
8373
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
986 |
fun add_cases xs = map_context (fn (thy, data, asms, binds, thms, cases, defs) => |
8426 | 987 |
(thy, data, asms, binds, thms, rev (filter_out (equal "" o #1) xs) @ cases, defs)); |
8373
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
988 |
|
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
989 |
|
e7237c8fe29e
handling of local contexts: print_cases, get_case, add_cases;
wenzelm
parents:
8186
diff
changeset
|
990 |
|
10810 | 991 |
(** print context information **) |
992 |
||
993 |
val show_hyps = ref false; |
|
994 |
||
995 |
fun pretty_thm thm = |
|
996 |
if ! show_hyps then setmp Display.show_hyps true Display.pretty_thm_no_quote thm |
|
997 |
else Display.pretty_cterm (#prop (Thm.crep_thm thm)); |
|
998 |
||
999 |
val verbose = ref false; |
|
1000 |
fun verb f x = if ! verbose then f (x ()) else []; |
|
1001 |
fun verb_single x = verb Library.single x; |
|
1002 |
||
1003 |
fun setmp_verbose f x = Library.setmp verbose true f x; |
|
1004 |
||
1005 |
fun pretty_items prt name items = |
|
1006 |
let |
|
1007 |
fun prt_itms (name, [x]) = Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt x] |
|
1008 |
| prt_itms (name, xs) = Pretty.big_list (name ^ ":") (map prt xs); |
|
1009 |
in |
|
1010 |
if null items andalso not (! verbose) then [] |
|
1011 |
else [Pretty.big_list name (map prt_itms items)] |
|
1012 |
end; |
|
1013 |
||
1014 |
||
1015 |
(* term bindings *) |
|
1016 |
||
1017 |
val smash_option = fn (_, None) => None | (xi, Some b) => Some (xi, b); |
|
1018 |
||
1019 |
fun pretty_binds (ctxt as Context {binds, ...}) = |
|
1020 |
let |
|
1021 |
val prt_term = Sign.pretty_term (sign_of ctxt); |
|
1022 |
fun prt_bind (xi, (t, T)) = prt_term (Logic.mk_equals (Var (xi, T), t)); |
|
1023 |
val bs = mapfilter smash_option (Vartab.dest binds); |
|
1024 |
in |
|
1025 |
if null bs andalso not (! verbose) then [] |
|
1026 |
else [Pretty.big_list "term bindings:" (map prt_bind bs)] |
|
1027 |
end; |
|
1028 |
||
1029 |
val print_binds = Pretty.writeln o Pretty.chunks o pretty_binds; |
|
1030 |
||
1031 |
||
1032 |
(* local theorems *) |
|
1033 |
||
1034 |
fun pretty_thms (Context {thms, ...}) = |
|
1035 |
pretty_items pretty_thm "facts:" (mapfilter smash_option (Symtab.dest thms)); |
|
1036 |
||
1037 |
val print_thms = Pretty.writeln o Pretty.chunks o pretty_thms; |
|
1038 |
||
1039 |
||
1040 |
(* local contexts *) |
|
1041 |
||
10830 | 1042 |
fun apply_case ({fixes, assumes, binds}: RuleCases.T) ctxt = |
10810 | 1043 |
let |
10830 | 1044 |
fun bind (c, (x, T)) = (c |> fix_i [([x], Some T)], bind_skolem c [x] (Free (x, T))); |
1045 |
val (ctxt', xs) = foldl_map bind (ctxt, fixes); |
|
10810 | 1046 |
fun app t = foldl Term.betapply (t, xs); |
10830 | 1047 |
in (ctxt', (map (apsnd (apsome app)) binds, map app assumes)) end; |
10810 | 1048 |
|
1049 |
fun pretty_cases (ctxt as Context {cases, ...}) = |
|
1050 |
let |
|
1051 |
val prt_term = Sign.pretty_term (sign_of ctxt); |
|
1052 |
fun prt_let (xi, t) = Pretty.block |
|
10818 | 1053 |
[Pretty.quote (prt_term (Var (xi, Term.fastype_of t))), Pretty.str " =", Pretty.brk 1, |
10810 | 1054 |
Pretty.quote (prt_term t)]; |
1055 |
||
1056 |
fun prt_sect _ _ _ [] = [] |
|
1057 |
| prt_sect s sep prt xs = [Pretty.block (Pretty.breaks (Pretty.str s :: |
|
1058 |
flat (Library.separate sep (map (Library.single o prt) xs))))]; |
|
1059 |
||
10830 | 1060 |
fun prt_case (name, (fixes, (lets, asms))) = Pretty.block (Pretty.fbreaks |
10810 | 1061 |
(Pretty.str (name ^ ":") :: |
11915 | 1062 |
prt_sect "fix" [] (Pretty.str o fst) fixes @ |
10810 | 1063 |
prt_sect "let" [Pretty.str "and"] prt_let |
1064 |
(mapfilter (fn (xi, Some t) => Some (xi, t) | _ => None) lets) @ |
|
1065 |
prt_sect "assume" [] (Pretty.quote o prt_term) asms)); |
|
1066 |
||
1067 |
val cases' = rev (Library.gen_distinct Library.eq_fst cases); |
|
1068 |
in |
|
1069 |
if null cases andalso not (! verbose) then [] |
|
10830 | 1070 |
else [Pretty.big_list "cases:" |
1071 |
(map (prt_case o apsnd (fn c => (#fixes c, #2 (apply_case c ctxt)))) cases')] |
|
10810 | 1072 |
end; |
1073 |
||
1074 |
val print_cases = Pretty.writeln o Pretty.chunks o pretty_cases; |
|
1075 |
||
1076 |
||
1077 |
(* prems *) |
|
1078 |
||
1079 |
val prems_limit = ref 10; |
|
1080 |
||
1081 |
fun pretty_prems ctxt = |
|
1082 |
let |
|
1083 |
val limit = ! prems_limit; |
|
1084 |
val prems = prems_of ctxt; |
|
1085 |
val len = length prems; |
|
1086 |
val prt_prems = |
|
1087 |
(if len <= limit then [] else [Pretty.str "..."]) @ |
|
1088 |
map pretty_thm (Library.drop (len - limit, prems)); |
|
1089 |
in if null prems then [] else [Pretty.big_list "prems:" prt_prems] end; |
|
1090 |
||
1091 |
||
1092 |
(* main context *) |
|
1093 |
||
1094 |
fun pretty_context (ctxt as Context {asms = (_, (fixes, _)), cases, |
|
1095 |
defs = (types, sorts, (used, _)), ...}) = |
|
1096 |
let |
|
1097 |
val sign = sign_of ctxt; |
|
1098 |
||
1099 |
val prt_term = Sign.pretty_term sign; |
|
1100 |
val prt_typ = Sign.pretty_typ sign; |
|
1101 |
val prt_sort = Sign.pretty_sort sign; |
|
1102 |
||
1103 |
(*theory*) |
|
1104 |
val pretty_thy = Pretty.block [Pretty.str "Theory:", Pretty.brk 1, Sign.pretty_sg sign]; |
|
1105 |
||
1106 |
(*fixes*) |
|
12008 | 1107 |
fun prt_fix (x, x') = |
1108 |
if x = x' then Pretty.str x |
|
1109 |
else Pretty.block [Pretty.str x, Pretty.str " =", Pretty.brk 1, prt_term (Syntax.free x')]; |
|
10810 | 1110 |
|
1111 |
fun prt_fixes [] = [] |
|
1112 |
| prt_fixes xs = [Pretty.block (Pretty.str "fixed variables:" :: Pretty.brk 1 :: |
|
1113 |
Pretty.commas (map prt_fix xs))]; |
|
1114 |
||
1115 |
(*defaults*) |
|
1116 |
fun prt_atom prt prtT (x, X) = Pretty.block |
|
1117 |
[prt x, Pretty.str " ::", Pretty.brk 1, prtT X]; |
|
1118 |
||
1119 |
fun prt_var (x, ~1) = prt_term (Syntax.free x) |
|
1120 |
| prt_var xi = prt_term (Syntax.var xi); |
|
1121 |
||
1122 |
fun prt_varT (x, ~1) = prt_typ (TFree (x, [])) |
|
1123 |
| prt_varT xi = prt_typ (TVar (xi, [])); |
|
1124 |
||
1125 |
val prt_defT = prt_atom prt_var prt_typ; |
|
1126 |
val prt_defS = prt_atom prt_varT prt_sort; |
|
1127 |
in |
|
1128 |
verb_single (K pretty_thy) @ |
|
1129 |
prt_fixes (rev (filter_out (can Syntax.dest_internal o #1) fixes)) @ |
|
1130 |
pretty_prems ctxt @ |
|
1131 |
verb pretty_binds (K ctxt) @ |
|
1132 |
verb pretty_thms (K ctxt) @ |
|
1133 |
verb pretty_cases (K ctxt) @ |
|
1134 |
verb_single (fn () => Pretty.big_list "type constraints:" (map prt_defT (Vartab.dest types))) @ |
|
1135 |
verb_single (fn () => Pretty.big_list "default sorts:" (map prt_defS (Vartab.dest sorts))) @ |
|
1136 |
verb_single (fn () => Pretty.strs ("used type variable names:" :: used)) |
|
1137 |
end; |
|
1138 |
||
1139 |
||
1140 |
||
5819 | 1141 |
(** theory setup **) |
1142 |
||
1143 |
val setup = [ProofDataData.init]; |
|
1144 |
||
1145 |
end; |