Right now Applicative
isn't a superclass of Monad
instance Monad m where ... -- this is how it is today, instead of
instance Applicative m => Monad m where ...
but it is planned so that in GHC 7.10 this will be changed so that Applicative
is a superclass of Monad
. In order to help the transition, in GHC 7.7 and 7.8 there will be the warning you saw issued whenever GHC encounters a Monad
without an Applicative
instance.
Now the slightly confusing bit is that all valid Monad
s are applicative functors, even if they're not instance
s of Applicative
. We can write
fmapM :: Monad m => (a -> b) -> m a -> m b
fmapM f ma = ma >>= return . f -- a.k.a. `liftM`
pureM :: Monad m => a -> m a
pureM = return
ap :: Monad m => m (a -> b) -> m a -> m b
ap mf ma = do { f <- mf; a <- ma; return (f a) } -- a.k.a. `ap`
which together satisfy the signature and laws of Functor
and Applicative
. This is why the superclass constraint makes sense to add and it's purely historical accident that it wasn't there in the first case—Applicative
s were discovered and popularized far after Monad
s were.
newtype WrappedMonad m a = WM (m a)
instance Monad m => Functor (WrappedMonad m) where
fmap f (WM m) = WM (liftM f m)
instance Monad m => Applicative (WrappedMonad m) where
pure = WM . return
WM mf <*> WM ma = WM $ mf `ap` ma
For more information on how Applicative
and Monad
relate, take a look at an answer I wrote previously here: Is it better to define Functor in terms of Applicative in terms of Monad, or vice versa?
Monad
s can be made an instance ofApplicative
, but not all of them necessarily have been (at least not by GHC 7.6). – Tom Ellis