[Haskell-cafe] Re: kinds question

David Roundy droundy at abridgegame.org
Fri Dec 23 09:39:51 EST 2005


On Thu, Dec 22, 2005 at 06:27:41PM -0800, Ashley Yakeley wrote:
> David Roundy wrote:
> >Hello all,
> >
> >I have a question about how to create the right kind to declare lists to be
> >a class.  I have a class Foo
> >
> >class Foo f where
> >  foo :: f a -> Foo
> >
> >and I want to define that a list of Foos is also a Foo, but can't see how
> >to do it.  I imagine something like
> >
> >instance Foo f => Foo [f] where
> >  foo xs = map foo xs
> >
> >but of course [f] isn't a valid type.
> 
> [] and f both have * -> *, and you want to compose them. You can do this 
> like this:
> 
>   newtype Compose p q a = MkCompose p (q a)
> 
> and then
> 
>   instance Foo f => instance (Compose [] f) where
>     foo (MkCompose fs) = ...

Given:

instance Foo f => Foo (Compose [] f) where
    foo _ = undefined

ghc gives me the error:

test.hs:24:0:
    Illegal instance declaration for `Foo (Compose [] f)'
        (The instance type must be of form (T a b c)
         where T is not a synonym, and a,b,c are distinct type variables)
    In the instance declaration for `Foo (Compose ([]) f)'

I can, however, define

instance (Functor p, Foo q) => Foo (Compose p q) where
    foo (MkCompose fs) = MkCompose (fmap foo fs)

but in any case, this doesn't make a list itself a Foo, so you'd still need
wrappers, which would defeat the point of putting lists into the class.  :(
-- 
David Roundy
http://www.darcs.net


More information about the Haskell-Cafe mailing list