Haskell Language

Functors, Applicatives, Monads

Functors, Applicatives, And Monads In Pictures - adit.io

Functor

class Functor f where
    fmap :: (a -> b) -> f a -> f b

    -- Identity
    -- fmap id == id

    -- Composition
    -- fmap (f . g) == fmap f . fmap g

-- Utility functions
 ($)  ::              (a -> b) ->   a ->   b
(<$>) :: Functor f => (a -> b) -> f a -> f b

Applicative

class Functor f => Applicative f where
    pure :: a -> f a
    (<*>) :: f (a -> b) -> f a -> f b

    -- Identity
    -- pure id <*> v == v

    -- Composition
    -- pure (.) <*> u <*> v <*> w == u <*> (v <*> w)

    -- Homomorphism
    -- pure f <*> pure x == pure (f x)

    -- Interchange
    -- u <*> pure y == pure ($ y) <*> u

Monad

class Applicative m => Monad m where
    return :: a -> m a
    (>>=) :: forall a b. m a -> (a -> m b) -> m b

    -- Left Identity
    -- return a >>= k == k a

    -- Right Identity
    -- m >>= return == m

    -- Associativity
    -- m >>= (\\x -> k x >>= h) == (m >>= k) >>= h

stack