Question

In Haskell, what does the monad instance of functions give over just applicative? Looking at their implementations, they seem almost identical:

(<*>) f g x = f x (g x)
(>>=) f g x = g (f x) x

Is there anything you can do with >>= that you can't do with just <*>?

Was it helpful?

Solution

They are equivalent in power for the function instance: flip f <*> g == g >>= f. This is not true for most types that are instances of Monad though.

It's a little more clear if we compare <*> and =<< (which is flip (>>=)) specialized to the ((->) r) instance:

(<*>) :: Applicative f => f (a -> b) -> f a -> f b
   -- Specialized to ((->) r):
(<*>) :: (r -> a -> b) -> (r -> a) -> r -> b

(=<<) :: Monad m => (a -> m b) -> m a -> m b
   -- Specialized to ((->) r):
(=<<) :: (a -> r -> b) -> (r -> a) -> r -> b
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top