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