> (This is related to my previous post concerning monad transformers.)
>
> GHC complains about:
>
> > class (Monad m, {-, Monad (t m)-}) => MonadT t m where
> > lift :: m a -> (t m) a
> >
> > instance (Monad m) => Monad (EnvT env m) where
> > ...
> >
> > instance (Monad (EnvT env m)) => MonadT (EnvT env) m where
> > ...
>
> saying that it cannot deduce (Monad m) to satisfy the second instance
> declaration. But it should be able to pick up that fact from the first
> instance declaration, shouldn't it?
>
> Indeed, if I add (Monad m) to the context explicitly:
>
> > instance (Monad m, Monad (EnvT env m)) => MonadT (EnvT env) m where
> > ...
>
> then GHC gives a warning when I import this module because the assumption
> appears twice in a class context in the .hi file:
>
> Duplicated class assertion `Monad b' in the context:
> (Monad b, Monad Env.EnvT a b, Monad b)
OK, there are two things going on. First, GHC is meant to implement
the restriction that the context in an instance decl can constrain
only type variables (Choice 6b). So this should be illegal:
instance (Monad (EnvT env m)) => MonadT (EnvT env) m where ...
Instead, since Monad (EnvT env m) holds if Monad m holds, you should
write
instance Monad m => MonadT (EnvT env) m where ...
The complaint on import is because GHC didn't realise that it
hadn't checked for a decent instance context, and thereby generated
a bogus one in the interface file.
Choice 6b restricts instance decls so that context reduction is
guaranteed to terminate. As ever, I'm interested in cases where
this prevents useful programs. This time, there's no problem,
I think
Simon
Re: Instance declaration superclasses
Simon L Peyton Jones Mon, 27 Apr 1998 10:09:43 +0200 (MET DST)
- Instance declaration superclasses Frank A. Christoph
- RE: Instance declaration superclasses Simon L Peyton Jones
- RE: Instance declaration superclasses Frank A. Christoph
- Re: Instance declaration superclasses Simon L Peyton Jones
