|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file:
-- http://angg.twu.net/HASKELL/Applicative1.hs.html
-- http://angg.twu.net/HASKELL/Applicative1.hs
-- (find-angg "HASKELL/Applicative1.hs")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- (defun e () (interactive) (find-angg "HASKELL/Applicative1.hs"))
-- (find-es "haskell" "applicative")
-- (find-es "haskell" "Prelude")
-- (find-es "haskell" "InstanceSigns")
-- (find-books "__comp/__comp.el" "haskell-hutton")
-- (find-books "__comp/__comp.el" "haskell-hutton" "Applicatives")
-- https://www.staff.city.ac.uk/~ross/papers/Applicative.pdf
-- https://www.staff.city.ac.uk/~ross/papers/Applicative.html
-- (find-huttonbookpage 182 "12.1 Functors")
-- (find-huttonbooktext 182 "12.1 Functors")
-- (find-ghcbasefile "Data/Functor.hs")
-- (find-ghcbasefile "Data/Functor.hs" "An infix synonym for 'fmap'")
--
-- class Functor f where
-- fmap :: (a -> b) -> f a -> f b
-- (find-huttonbookpage 187 "12.2 Applicatives")
-- (find-huttonbooktext 187 "12.2 Applicatives")
-- (find-ghcbasefile "Control/Applicative.hs")
--
-- class Functor f => Applicative f where
-- pure :: a -> f a
-- (<*>) :: f (a -> b) -> f a -> f b
--
-- fmap0 :: a -> f a
-- fmap0 = pure
--
-- fmap1 :: (a -> b) -> f a -> f b
-- fmap1 g x = pure g <*> x
--
-- fmap1 g x = pure g <*> x
-- ==== === ==== ===
-- a->b f a a->b f a
-- ========== =========
-- f a->f b f(a->b)
-- =============== =================
-- f b f b
--
-- fmap2 :: (a -> b -> c) -> f a -> f b -> f c
-- fmap2 g x y = pure g <*> x <*> y
--
-- fmap2 g x y = pure g <*> x <*> y
-- ======= === === ======= === ===
-- a->b->c f a f b a->b->c f a f b
-- ============= ============
-- f a->f b->f c f(a->b->c)
-- ================= ====================
-- f b->f c f(b->c)
-- ===================== ============================
-- f c f c
--
-- fmap3 :: (a -> b -> c -> d) -> f a -> f b -> f c -> f d
-- fmap3 g x y z = pure g <*> x <*> y <*> z
--
-- fmap3 g x y z = pure g <*> x <*> y <*> z
-- ========== === === === ========== === === ===
-- a->b->c->d f a f b f c a->b->c->d f a f b f c
-- ================== ===============
-- f a->f b->f c->f d f(a->b->c->d)
-- ====================== =======================
-- f b ->f c-> f d f(b->c->d)
-- ========================== ==============================
-- f c->f d f(c->d)
-- ============================== =====================================
-- f d f d
-- (find-huttonbookpage 193 "12.3 Monads")
-- (find-huttonbooktext 193 "12.3 Monads")
--
-- class Applicative m => Monad m where
-- return :: a -> m a
-- (>>=) :: m a -> (a -> m b) -> m b
-- return = pure
-- liftM f mx == mx >>= \x -> return ( f x)
-- ==== === === = ==== =
-- a->b m a m a a a->b a
-- ======
-- b
-- ===============
-- m b
-- =====================
-- a->m b
-- =============================
-- m b
f <$> mx ={synonym}=
fmap f mx ={law}=
liftM f mx ={definition}=
mx >>= \x -> return (f x)
to
simplify the `m1 >>= \x1 -> return (f x0 x1)' to `f x0 <$>
m1')
f <$> mx
= fmap f mx
= liftM f mx
= mx >>= \x -> return (f x)
= m1 >>= \x1 -> return (f x0 x1)
fmap :: (a -> b) -> (f a -> f b)
(<$>) = fmap
(<$> mx) = \ f -> fmap f mx
==== ==== ===
a->b a->b ma
=============
m b
<ski> mf `ap` mx = mf >>= \f -> f <$> mx = mf >>= (<$> mx)
mf `ap` mx
= ap mf mx
= (<*>) mf mx
= mf >>= \f -> f <$> mx
= mf >>= (<$> mx)
(find-ghcbasefile "GHC/Base.hs" "Promote a function to a monad")
(find-ghcbasefile "GHC/Base.hs" "Promote a function to a monad" "liftM2")
(find-ghcbasefile "GHC/Base.hs" "(<*>) = liftA2 id")
<lambdabot> ap = liftM2 id
<hpc> bah
<ski> well, i pronounce it `ap' .. for the other question, see ^
<hpc> anyhoo, ap = (<*>), so see how that's defined
<ski> @src liftM2
<lambdabot> liftM2 f m1 m2 = do
<lambdabot> x1 <- m1
<lambdabot> x2 <- m2
<lambdabot> return (f x1 x2)
<ski> liftM2 f m0 m1 = m0 >>= \x0 -> m1 >>= \x1 -> return (f x0 x1)
<ski> = m0 >>= \x0 -> (return . f x0 <$> m1)
<ski> so
<edrx> so that's a definition for <*>?
*** chexum JOIN
<edrx> I'll need some minutes to type each subexpression of that
<geekosaur> use `id` for `f`
<ski> er, sorry, no `return' in the last line
<ski> = m0 >>= \x0 -> (f x0 <$> m1)
<ski> mf `ap` mx = mf >>= \f -> f <$> mx
<ski> = mf >>= (<$> mx)
<ski> (i used here that `f <$> mx ={synonym}= fmap f mx ={law}=
liftM f mx ={definition}= mx >>= \x -> return (f x)' to
simplify the `m1 >>= \x1 -> return (f x0 x1)' to `f x0 <$>
m1')
-- (<*>) mf mx = mf >>= \ f -> fmap f mx
-- ======= === ======= ==== ==== ===
-- m(a->b) m a m(a->b) a->b a->b m a
-- ================ ==============
-- m b m b
-- =======================
-- (a->b)->m b
-- ==================================
-- m b
-- Local Variables:
-- coding: utf-8-unix
-- End: