[Haskell-beginners] Re: Thinking about monads (Brent Yorgey)

Thomas Davie tom.davie at gmail.com
Wed Apr 15 02:19:48 EDT 2009


On 15 Apr 2009, at 00:08, Arthur Chan wrote:

> You know, I was wondering...  if Monads are a subset of Functors,  
> and Applicative is a subset of Functors, and Monads are a subset of  
> Applicative...  shouldn't it be possible to tack on the definitions  
> that automatically derive Functor and Applicative?  Isn't it the  
> case that there is really only one way to define Applicative for a  
> Monad anyway?  And isn't there only one way to define fmap for a  
> Monad that makes sense?

Yes, but at the same time no.

Firstly, it's possible that you want an applicative instance that  
disagrees with your monad one – not common, and usually ugly, but  
possible.

Secondly, it's often possible to implement the applicative/functor  
methods in a much more efficient way by dealing with them specifically.

In reality, what we want to see is this:

class Pointed f where
   pure :: a -> f a

class Functor f where
   fmap :: (a -> b) -> f a -> f b

class (Functor f, Pointed f) => Applicative f where
   (<*>) :: f (a -> b) -> f a -> f b

class (Applicative f) => Monad f where
   join :: f (f a) -> f a

Then we only need to define each behavior in one place, and the tree  
is neatly seperated out so that if we have something that isn't an  
Applicative, we can stop implementing after Functor, and if we have  
something that isn't a Monad, we can stop implementing after  
Applicative.

Bob


More information about the Beginners mailing list