]> git.rkrishnan.org Git - yorgey.git/blob - misc/applicatives.hs
abf92313c5caa63aeec3b132eadd3306496e2a77
[yorgey.git] / misc / applicatives.hs
1 liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c
2 liftA2 h fa fb = (h `fmap` fa) <*> fb
3
4 -- In fact, this pattern is so common that Control.Applicative defines (<$>) as a synonym for fmap,
5
6 (<$>) :: Functor f => (a -> b) -> f a -> f b
7 (<$>) = fmap
8
9 liftA2 h fa fb = h <$> fa <*> fb
10
11 {-|
12
13 h -> (a -> b -> c)
14
15 -- (a -> b -> c) -> f a -> f (b -> c)
16 h <$> fa = hfa
17
18 -- (<*>) :: f (a -> b) -> f a -> f b
19 -- hfa <*> f b
20 -- f (b -> c) -> f b -> f c
21
22 -- Applicative law
23 -- f `fmap` x === pure f <*> x
24
25 -- lhs = (a -> b) `fmap` f a gives a value of type f b
26 -- rhs = pure f.
27 --     f  has the type (a -> b), so pure f has the type f (a -> b)
28 --   pure f <*> x => f (a -> b> <*> f a => f b
29
30 -}