[Haskell-cafe] Rank-2 types in classes

Max Bolingbroke batterseapower at hotmail.com
Wed Mar 2 10:29:30 CET 2011


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



More information about the Haskell-Cafe mailing list