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