[Haskell-cafe] Re: Composition of Type Constructors

Chung-chieh Shan ccshan at post.harvard.edu
Wed May 25 11:20:12 EDT 2005


Jan Christiansen <jac at informatik.uni-kiel.de> wrote in article <Pine.GSO.4.63.0505251243020.11488 at arwen> in gmane.comp.lang.haskell.cafe:
> 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



More information about the Haskell-Cafe mailing list