2
votes

In LYHFGG the author states that "Monads are just applicative functors that support >>=" (see image below). I don't see how this statement can be true if I look at the definition of Monad type class.

The Monad type class seems to have no relation whatsoever to the Control.Applicative type class, for example Monad type classes are not subtypes of Applicative. So it is clear that, technically, in Haskell, Monads and Applicative functors are completely independent type classes. So if the author's statement is true then it must be true in a different context.

Could someone please explain what the book author means by this seemingly untrue statement?

How should his statement be interpreted ? In what context? In the context of category theory perhaps?

In other words : I don't see how it is possible to turn any given Monad into an Applicative functor. Because if the author's statement is true then every Monad can be turned mechanically (by using an algorithm) into an Applicative functor. But is it really possible to do that? If yes, how?

enter image description here

2
Yes, it is really possible, as described in the answers to this question: stackoverflow.com/questions/13533769/….MvanGeest

2 Answers

14
votes

You're right that the statement means that you can write an Applicative instance when all you know is that your type constructor is a monad. If M is a monad, then you can write:

instance Applicative M where
  pure = return
  mf <*> mx =
    mf >>= \f ->
      mx >>= \x ->
        return (f x)

And actually, starting with GHC 7.10, Applicative will be a superclass of Monad, meaning that the concept that "Monad is Applicative plus ..." will be baked in the standard library.

9
votes

The author is perfectly right.

So it is clear that, technically, in Haskell, Monads and Applicative functors are completely independent type classes.

In fact, Monad should be a "subclass" of Applicative. It has been proposed and will probably be standardized in Haskell 2014. Everybody agrees that it was a mistake not to do it this way from the start.

We know that a Monad is a monad if it defines:

  • return
  • >>=
  • >> (can be derived from >>= and return)

I'll leave fail aside intentionally.

You can see that Applicative defines, amongst other things, pure, which is the same as return, and *> which is the same as >>. Therefore the only remaining difference is the definition of >>=.