[Haskell-beginners] Re: Thinking about monads (Brent Yorgey)
Brent Yorgey
byorgey at seas.upenn.edu
Thu Apr 16 09:08:53 EDT 2009
On Tue, Apr 14, 2009 at 03:08:05PM -0700, 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?
Actually, it's already possible to do this, in a way. If you have a
Monad, then fmap is liftM, pure is return, and (<*>) is ap. So you
already have implementations of Functor and Applicative. In fact, I
routinely do this:
import Text.ParserCombinators.Parsec
instance Applicative (GenParser tok st) where
pure = return
(<*>) = ap
There are also various proposals which would help in automating this
sort of process, like "class aliases". But in general, having a nicer
class hierarchy as Bob suggests would be much better.
-Brent
More information about the Beginners
mailing list