[Haskell-cafe] Applicative instances for Monads

wren ng thornton wren at freegeek.org
Sat Sep 25 19:43:24 EDT 2010

On 9/24/10 10:01 PM, Gregory Crosswhite wrote:
> Hey everyone,
> There is something that has been bugging me recently about the
> Applicative class and the Monad class.
> Any type constructor F that is a Monad has a natural Applicative instance,
> (<$>) :: F (a -> b) -> F a -> F b
> mf <$> ma = do
> f <- mf
> a <- ma
> return (f a)

Er, I'm pretty sure you mean (<*>) not (<$>) :)

> So it seems that defining something to be a Monad should automatically
> make it an instance of Applicative with this definition for (<$>). So
> far so good, but there are times when this implementation is too
> "sequential". The nature of Applicative is that later actions are not
> allowed to depend on earlier actions,

Yes and no. Later actions are not allowed to depend on the resulting 
value of earlier actions, but they are most certainly allowed to 
'depend' on the side-effects of earlier actions. Otherwise applicative 
parser combinators wouldn't make any sense because there's be no way to 
sequence their effects of consuming input.

It would be nice to have something like AIO in a standard library though.

Live well,

More information about the Haskell-Cafe mailing list