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