7

I'm trying to implement a Monad instance. As a simpler example, assume the following:

data Maybee a = Notheeng | Juust a 

instance Monad Maybee where
   return x = Juust x
   Notheeng >>= f = Notheeng
   Juust x >>= f = f x
   fail _ = Notheeng 

This should be the standard implementation of Maybe as far as I know. However, this doesn't compile, because the compiler complains:

No instance for (Applicative Maybee)

and similarly he wants a Functor instance once the Applicative is given.

So: Simple question: Must I always implement Functor and Applicative before I can implement a Monad?

1
  • 5
    I anticipate a lot of questions about this, in the wake of GHC 7.10's release. Should we create a Functor-Applicative-Monad proposal tag?
    – jub0bs
    Apr 24, 2015 at 10:43

3 Answers 3

11

Yes, it wasn't the case before, it's a change that was introduced in ghc7.10 under the name of Functor-Applicative-Monad Proposal.

8

It is compulsory to define instances for Functor and Applicative (the second one is a new requirement in newer versions of Haskell), but it's actually no big deal because if you don't want to hand-write your own instances you can just use these ones:

import Control.Applicative (Applicative(..))
import Control.Monad       (liftM, ap)

-- Monad m

instance Functor m where
    fmap = liftM

instance Applicative m where
    pure  = return
    (<*>) = ap
6

With GHC 7.10 and above, you must implement Functor and Applicative. The class definitions for Monad mandate the superclass instances:

class Functor f => Applicative f where ...
class Applicative m => Monad m where ...

Note that once you have a Monad instance, the Functor and Applicative instances can be defined generically with no additional effort:

import Control.Monad

-- suppose we defined a Monad instance:
instance Monad m where ...

instance Functor m where
    fmap = liftM

instance Applicative m where
    pure = return
    (<*>) = ap
3
  • 2
    Actually not true: In 7.8 and older, Monad had no superclass constraint whatsoever. Apr 24, 2015 at 11:55
  • Duh, that's quite a heavy mistake from me. Apr 24, 2015 at 12:58
  • It's not a great idea to implement pure = return, I don't think. It's possible return will eventually go away, and IIRC there's even a warning available to discover when return is implemented as anything but pure.
    – dfeuer
    Jun 30, 2017 at 23:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.