Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monad vs Applicative in the case of Reader resp. ((->) a)

Tags:

haskell

monads

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 <*>?

like image 434
user1535823 Avatar asked Mar 05 '14 23:03

user1535823


1 Answers

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
like image 184
David Young Avatar answered Nov 11 '22 04:11

David Young