From: Ramakrishnan Muthukrishnan Date: Fri, 26 Dec 2014 16:43:59 +0000 (+0530) Subject: scratch pad notes on applicatives X-Git-Url: https://git.rkrishnan.org/pf/content/en/components/com_hotproperty/-?a=commitdiff_plain;h=80a5790a56043c8bf8dd3e01d5a9d6b776a13f55;p=yorgey.git scratch pad notes on applicatives --- diff --git a/misc/applicatives.hs b/misc/applicatives.hs new file mode 100644 index 0000000..abf9231 --- /dev/null +++ b/misc/applicatives.hs @@ -0,0 +1,30 @@ +liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c +liftA2 h fa fb = (h `fmap` fa) <*> fb + +-- In fact, this pattern is so common that Control.Applicative defines (<$>) as a synonym for fmap, + +(<$>) :: Functor f => (a -> b) -> f a -> f b +(<$>) = fmap + +liftA2 h fa fb = h <$> fa <*> fb + +{-| + +h -> (a -> b -> c) + +-- (a -> b -> c) -> f a -> f (b -> c) +h <$> fa = hfa + +-- (<*>) :: f (a -> b) -> f a -> f b +-- hfa <*> f b +-- f (b -> c) -> f b -> f c + +-- Applicative law +-- f `fmap` x === pure f <*> x + +-- lhs = (a -> b) `fmap` f a gives a value of type f b +-- rhs = pure f. +-- f has the type (a -> b), so pure f has the type f (a -> b) +-- pure f <*> x => f (a -> b> <*> f a => f b + +-} \ No newline at end of file