[Haskell-cafe] Rank-2 types in classes

Yves Parès limestrael at gmail.com
Wed Mar 2 13:30:43 CET 2011


Thank you !

Is what I'm trying to do a common technique to type-ensure contexts or are
there simpler methods?


2011/3/2 Max Bolingbroke <batterseapower at hotmail.com>

> On 2 March 2011 09:11, Yves Parès <limestrael at gmail.com> wrote:
> > class (forall x. Monad (IM i x)) => Impl i where
> >     data IM i :: * -> * -> *
> >
> > But GHC forbids me to do so.
>
> The way I usually work around this is by doing something like the
> following pattern:
>
> {{{
> class Monad1 m where
>    return1 :: a -> m x a
>    bind1 :: m x a -> (a -> m x b) -> m x b
>
> instance Monad1 (IM MyI) where
>    return1 = ...
>    bind1 = ...
>
> instance Monad1 m => Monad (m x) where
>    return = return1
>    (>>=) = bind1
> }}}
>
> Your class can now have a (Monad1 (IM i)) superclass context. You will
> have to enable a few extensions to get this through - most likely
> FlexibleInstances and OverlappingInstances.
>
> Cheers,
> Max
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110302/dca17890/attachment.htm>


More information about the Haskell-Cafe mailing list