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?
It is compulsory to define instances for
Functor
andApplicative
(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:With GHC 7.10 and above, you must implement
Functor
andApplicative
. The class definitions forMonad
mandate the superclass instances:Note that once you have a
Monad
instance, theFunctor
andApplicative
instances can be defined generically with no additional effort: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.