Composition of Type Constructors

Hi! I would like to define a type class for a data structure that supports a lookup and an update function like the following one. class Mapping map key where lookup :: key -> map v -> v update :: key -> (v -> v) -> map v -> map v Now I want to lift this to a class instance for tuples of keys. This should be done by just using a map that contains another map as values. The problem is that I don't know how to express the composition of type constructors like the function (.) for ordinary functions. I know that (map1 . map2) is wrong but it states what I want to express, namely the composition of two type constructors. With this I would like to define an instance definition like the following. instance (Mapping map1 key1, Mapping map2 key2) => Mapping (map1 . map2) (key1, key2) where lookup (key1, key2) = lookup key2 . lookup key1 update (key1, key2) f = update key1 (update key2 f) I am not sure about the practical pertinence of this example cause I will probably only use one instance where the type of the keys is Int but I am curious if this is possible at all. thanks in advance, Jan

Jan Christiansen
The problem is that I don't know how to express the composition of type constructors like the function (.) for ordinary functions. I know that (map1 . map2) is wrong but it states what I want to express, namely the composition of two type constructors. With this I would like to define an instance definition like the following.
instance (Mapping map1 key1, Mapping map2 key2) => Mapping (map1 . map2) (key1, key2) where lookup (key1, key2) = lookup key2 . lookup key1 update (key1, key2) f = update key1 (update key2 f)
You can define the type-level composition yourself. Something like (untested): newtype Comp map1 map2 v = Comp (map1 (map2 v)) instance (Mapping map1 key1, Mapping map2 key2) => Mapping (Comp map1 map2) (key1, key2) where lookup (key1, key2) (Comp map) = lookup key2 (lookup key1 map) update (key1, key2) f (Comp map) = Comp (update key1 (update key2 f) map) -- Edit this signature at http://www.digitas.harvard.edu/cgi-bin/ken/sig A responsibility shared is a responsibility shirked. -- Bill Tucker
participants (2)
-
Chung-chieh Shan
-
Jan Christiansen