Haskell - can you have a monad that is not an application functor?

Into this slide set of Jim Duyah on slide 13 - he suggests that all Monads be applicative functors.

enter image description here

In the output of the Haskell 7.7 compiler - I see the following (another example here ):

'Parser is an instance of Monad, but not applicative - this will become a bug in GHC 7.10 as proposed by Applicative-Monad.

Does this mean that the Haskell compiler is currently porting Monads, which are not applicative functors, but the plan should fix this?

+8
haskell monads fam-proposal
source share
1 answer

Applicative not a Monad superclass right now

 instance Monad m where ... -- this is how it is today, instead of instance Applicative m => Monad m where ... 

but it is planned that in GHC 7.10 this will be changed so that Applicative Monad superclass. To help with the transition, a warning will appear in GHC 7.7 and 7.8 that you saw when the GHC encounters Monad without an Applicative instance.


Now a bit confusing bit is that all valid Monad are applicative functors, even if they are not instance of Applicative . We can write

 fmapM :: Monad m => (a -> b) -> ma -> mb fmapM f ma = ma >>= return . f -- aka `liftM` pureM :: Monad m => a -> ma pureM = return ap :: Monad m => m (a -> b) -> ma -> mb ap mf ma = do { f <- mf; a <- ma; return (fa) } -- aka `ap` 

which together satisfy the signature and laws of Functor and Applicative . This is why the restriction of the superclass makes sense to add a purely historical catastrophe that it was not in the first case - Applicative was discovered and popularized far after Monad .

 newtype WrappedMonad ma = WM (ma) instance Monad m => Functor (WrappedMonad m) where fmap f (WM m) = WM (liftM fm) 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 the answer I wrote earlier here: Is it better to define Functor from the applicative point of view in terms of Monad or vice versa?

+10
source share

All Articles