| author | paulson |
| Fri, 23 Dec 2005 17:34:46 +0100 | |
| changeset 18508 | c5861e128a95 |
| parent 18380 | 9668764224a7 |
| child 18515 | 1cad5c2b2a0b |
| permissions | -rw-r--r-- |
| 18168 | 1 |
(* Title: Pure/Tools/class_package.ML |
2 |
ID: $Id$ |
|
3 |
Author: Florian Haftmann, TU Muenchen |
|
4 |
||
5 |
Haskell98-like operational view on type classes. |
|
6 |
*) |
|
7 |
||
8 |
signature CLASS_PACKAGE = |
|
9 |
sig |
|
| 18335 | 10 |
val add_classentry: class -> string list -> string list -> theory -> theory |
| 18168 | 11 |
val the_consts: theory -> class -> string list |
12 |
val the_tycos: theory -> class -> (string * string) list |
|
13 |
||
| 18360 | 14 |
val syntactic_sort_of: theory -> sort -> sort |
| 18168 | 15 |
val get_arities: theory -> sort -> string -> sort list |
16 |
val get_superclasses: theory -> class -> class list |
|
| 18304 | 17 |
val get_const_sign: theory -> string -> string -> typ |
| 18168 | 18 |
val get_inst_consts_sign: theory -> string * class -> (string * typ) list |
19 |
val lookup_const_class: theory -> string -> class option |
|
20 |
val get_classtab: theory -> (string list * (string * string) list) Symtab.table |
|
21 |
||
22 |
type sortcontext = (string * sort) list |
|
23 |
datatype sortlookup = Instance of (class * string) * sortlookup list list |
|
24 |
| Lookup of class list * (string * int) |
|
25 |
val extract_sortctxt: theory -> typ -> sortcontext |
|
26 |
val extract_sortlookup: theory -> typ * typ -> sortlookup list list |
|
27 |
end; |
|
28 |
||
29 |
structure ClassPackage: CLASS_PACKAGE = |
|
30 |
struct |
|
31 |
||
32 |
||
33 |
(* data kind 'Pure/classes' *) |
|
34 |
||
35 |
type class_data = {
|
|
36 |
locale_name: string, |
|
37 |
axclass_name: string, |
|
38 |
consts: string list, |
|
39 |
tycos: (string * string) list |
|
40 |
}; |
|
41 |
||
42 |
structure ClassesData = TheoryDataFun ( |
|
43 |
struct |
|
44 |
val name = "Pure/classes"; |
|
45 |
type T = class_data Symtab.table * class Symtab.table; |
|
46 |
val empty = (Symtab.empty, Symtab.empty); |
|
47 |
val copy = I; |
|
48 |
val extend = I; |
|
49 |
fun merge _ ((t1, r1), (t2, r2))= |
|
50 |
(Symtab.merge (op =) (t1, t2), |
|
51 |
Symtab.merge (op =) (r1, r2)); |
|
52 |
fun print _ (tab, _) = (Pretty.writeln o Pretty.chunks) (map Pretty.str (Symtab.keys tab)); |
|
53 |
end |
|
54 |
); |
|
55 |
||
56 |
val lookup_class_data = Symtab.lookup o fst o ClassesData.get; |
|
57 |
val lookup_const_class = Symtab.lookup o snd o ClassesData.get; |
|
58 |
||
59 |
fun get_class_data thy class = |
|
60 |
case lookup_class_data thy class |
|
61 |
of NONE => error ("undeclared class " ^ quote class)
|
|
62 |
| SOME data => data; |
|
63 |
||
64 |
fun put_class_data class data = |
|
65 |
ClassesData.map (apfst (Symtab.update (class, data))); |
|
66 |
fun add_const class const = |
|
67 |
ClassesData.map (apsnd (Symtab.update (const, class))); |
|
68 |
||
69 |
||
70 |
(* name mangling *) |
|
71 |
||
72 |
fun get_locale_for_class thy class = |
|
73 |
#locale_name (get_class_data thy class); |
|
74 |
||
75 |
fun get_axclass_for_class thy class = |
|
76 |
#axclass_name (get_class_data thy class); |
|
77 |
||
78 |
||
79 |
(* assign consts to type classes *) |
|
80 |
||
81 |
local |
|
82 |
||
83 |
fun gen_add_consts prep_class prep_const (raw_class, raw_consts_new) thy = |
|
84 |
let |
|
85 |
val class = prep_class thy raw_class; |
|
86 |
val consts_new = map (prep_const thy) raw_consts_new; |
|
87 |
val {locale_name, axclass_name, consts, tycos} =
|
|
88 |
get_class_data thy class; |
|
89 |
in |
|
90 |
thy |
|
91 |
|> put_class_data class {
|
|
92 |
locale_name = locale_name, |
|
93 |
axclass_name = axclass_name, |
|
94 |
consts = consts @ consts_new, |
|
95 |
tycos = tycos |
|
96 |
} |
|
97 |
|> fold (add_const class) consts_new |
|
98 |
end; |
|
99 |
||
100 |
in |
|
101 |
||
102 |
val add_consts = gen_add_consts Sign.intern_class Sign.intern_const; |
|
103 |
val add_consts_i = gen_add_consts (K I) (K I); |
|
104 |
||
105 |
end; (* local *) |
|
106 |
||
107 |
val the_consts = #consts oo get_class_data; |
|
108 |
||
109 |
||
110 |
(* assign type constructors to type classes *) |
|
111 |
||
112 |
local |
|
113 |
||
114 |
fun gen_add_tycos prep_class prep_type (raw_class, raw_tycos_new) thy = |
|
115 |
let |
|
116 |
val class = prep_class thy raw_class |
|
117 |
val tycos_new = map (prep_type thy) raw_tycos_new |
|
118 |
val {locale_name, axclass_name, consts, tycos} =
|
|
119 |
get_class_data thy class |
|
120 |
in |
|
121 |
thy |
|
122 |
|> put_class_data class {
|
|
123 |
locale_name = locale_name, |
|
124 |
axclass_name = axclass_name, |
|
125 |
consts = consts, |
|
126 |
tycos = tycos @ tycos_new |
|
127 |
} |
|
128 |
end; |
|
129 |
||
130 |
in |
|
131 |
||
132 |
fun add_tycos xs thy = |
|
133 |
gen_add_tycos Sign.intern_class (rpair (Context.theory_name thy) oo Sign.intern_type) xs thy; |
|
134 |
val add_tycos_i = gen_add_tycos (K I) (K I); |
|
135 |
||
136 |
end; (* local *) |
|
137 |
||
138 |
val the_tycos = #tycos oo get_class_data; |
|
139 |
||
140 |
||
141 |
(* class queries *) |
|
142 |
||
| 18360 | 143 |
fun is_class thy cls = lookup_class_data thy cls |> Option.map (not o null o #consts) |> the_default false; |
| 18168 | 144 |
|
| 18360 | 145 |
fun syntactic_sort_of thy sort = |
146 |
let |
|
147 |
val classes = Sign.classes_of thy; |
|
148 |
fun get_sort cls = |
|
149 |
if is_class thy cls |
|
150 |
then [cls] |
|
151 |
else syntactic_sort_of thy (Sorts.superclasses classes cls); |
|
152 |
in |
|
153 |
map get_sort sort |
|
154 |
|> Library.flat |
|
155 |
|> Sorts.norm_sort classes |
|
156 |
end; |
|
| 18168 | 157 |
|
158 |
fun get_arities thy sort tycon = |
|
159 |
Sorts.mg_domain (Sign.classes_arities_of thy) tycon sort |
|
| 18360 | 160 |
|> map (syntactic_sort_of thy); |
| 18168 | 161 |
|
162 |
fun get_superclasses thy class = |
|
163 |
Sorts.superclasses (Sign.classes_of thy) class |
|
| 18360 | 164 |
|> syntactic_sort_of thy; |
| 18168 | 165 |
|
166 |
||
167 |
(* instance queries *) |
|
168 |
||
| 18304 | 169 |
fun get_const_sign thy tvar const = |
| 18168 | 170 |
let |
171 |
val class = (the o lookup_const_class thy) const; |
|
| 18335 | 172 |
val (ty, thaw) = (Type.freeze_thaw_type o Sign.the_const_constraint thy) const; |
| 18304 | 173 |
val tvars_used = Term.add_tfreesT ty []; |
174 |
val tvar_rename = hd (Term.invent_names (map fst tvars_used) tvar 1); |
|
175 |
in |
|
176 |
ty |
|
177 |
|> map_type_tfree (fn (tvar', sort) => |
|
178 |
if Sorts.sort_eq (Sign.classes_of thy) ([class], sort) |
|
179 |
then TFree (tvar, []) |
|
180 |
else if tvar' = tvar |
|
| 18335 | 181 |
then TVar ((tvar_rename, 0), sort) |
| 18304 | 182 |
else TFree (tvar', sort)) |
| 18335 | 183 |
|> thaw |
| 18304 | 184 |
end; |
| 18168 | 185 |
|
186 |
fun get_inst_consts_sign thy (tyco, class) = |
|
187 |
let |
|
188 |
val consts = the_consts thy class; |
|
189 |
val arities = get_arities thy [class] tyco; |
|
| 18304 | 190 |
val const_signs = map (get_const_sign thy "'a") consts; |
191 |
val vars_used = fold (fn ty => curry (gen_union (op =)) |
|
192 |
(map fst (typ_tfrees ty) |> remove (op =) "'a")) const_signs []; |
|
| 18168 | 193 |
val vars_new = Term.invent_names vars_used "'a" (length arities); |
| 18330 | 194 |
val typ_arity = Type (tyco, map2 (curry TFree) vars_new arities); |
| 18168 | 195 |
val instmem_signs = |
| 18335 | 196 |
map (typ_subst_TVars [(("'a", 0), typ_arity)]) const_signs;
|
| 18168 | 197 |
in consts ~~ instmem_signs end; |
198 |
||
199 |
fun get_classtab thy = |
|
200 |
Symtab.fold |
|
201 |
(fn (class, { consts = consts, tycos = tycos, ... }) =>
|
|
202 |
Symtab.update_new (class, (consts, tycos))) |
|
203 |
(fst (ClassesData.get thy)) Symtab.empty; |
|
204 |
||
205 |
||
206 |
(* extracting dictionary obligations from types *) |
|
207 |
||
208 |
type sortcontext = (string * sort) list; |
|
209 |
||
| 18335 | 210 |
fun extract_sortctxt thy ty = |
211 |
(typ_tfrees o Type.no_tvars) ty |
|
| 18360 | 212 |
|> map (apsnd (syntactic_sort_of thy)) |
| 18168 | 213 |
|> filter (not o null o snd); |
214 |
||
215 |
datatype sortlookup = Instance of (class * string) * sortlookup list list |
|
216 |
| Lookup of class list * (string * int) |
|
217 |
||
218 |
fun extract_sortlookup thy (raw_typ_def, raw_typ_use) = |
|
219 |
let |
|
220 |
val typ_def = Type.varifyT raw_typ_def; |
|
221 |
val typ_use = Type.varifyT raw_typ_use; |
|
222 |
val match_tab = Sign.typ_match thy (typ_def, typ_use) Vartab.empty; |
|
223 |
fun tab_lookup vname = (the o Vartab.lookup match_tab) (vname, 0); |
|
224 |
fun get_superclass_derivation (subclasses, superclass) = |
|
225 |
(the oo get_first) (fn subclass => |
|
226 |
Sorts.class_le_path (Sign.classes_of thy) (subclass, superclass) |
|
227 |
) subclasses; |
|
228 |
fun mk_class_deriv thy subclasses superclass = |
|
229 |
case get_superclass_derivation (subclasses, superclass) |
|
|
18380
9668764224a7
substantial improvements for class code generation
haftmann
parents:
18360
diff
changeset
|
230 |
of (subclass::deriv) => ((rev o filter (is_class thy)) deriv, find_index_eq subclass subclasses); |
| 18168 | 231 |
fun mk_lookup (sort_def, (Type (tycon, tys))) = |
232 |
let |
|
| 18330 | 233 |
val arity_lookup = map2 (curry mk_lookup) |
| 18360 | 234 |
(map (syntactic_sort_of thy) (Sorts.mg_domain (Sign.classes_arities_of thy) tycon sort_def)) tys |
| 18168 | 235 |
in map (fn class => Instance ((class, tycon), arity_lookup)) sort_def end |
236 |
| mk_lookup (sort_def, TVar ((vname, _), sort_use)) = |
|
237 |
let |
|
238 |
fun mk_look class = |
|
239 |
let val (deriv, classindex) = mk_class_deriv thy sort_use class |
|
240 |
in Lookup (deriv, (vname, classindex)) end; |
|
241 |
in map mk_look sort_def end; |
|
242 |
in |
|
| 18335 | 243 |
extract_sortctxt thy ((fst o Type.freeze_thaw_type) raw_typ_def) |
| 18168 | 244 |
|> map (tab_lookup o fst) |
| 18360 | 245 |
|> map (apfst (syntactic_sort_of thy)) |
| 18168 | 246 |
|> filter (not o null o fst) |
247 |
|> map mk_lookup |
|
248 |
end; |
|
249 |
||
250 |
||
| 18335 | 251 |
(* intermediate auxiliary *) |
| 18168 | 252 |
|
| 18335 | 253 |
fun add_classentry raw_class raw_consts raw_tycos thy = |
| 18168 | 254 |
let |
255 |
val class = Sign.intern_class thy raw_class; |
|
256 |
in |
|
257 |
thy |
|
258 |
|> put_class_data class {
|
|
259 |
locale_name = "", |
|
260 |
axclass_name = class, |
|
261 |
consts = [], |
|
262 |
tycos = [] |
|
263 |
} |
|
264 |
|> add_consts (class, raw_consts) |
|
265 |
|> add_tycos (class, raw_tycos) |
|
| 18335 | 266 |
end; |
267 |
||
| 18168 | 268 |
|
269 |
(* setup *) |
|
270 |
||
271 |
val _ = Context.add_setup [ClassesData.init]; |
|
272 |
||
273 |
end; (* struct *) |