Skip to content

Latest commit

 

History

History
57 lines (44 loc) · 1.09 KB

README.md

File metadata and controls

57 lines (44 loc) · 1.09 KB

chapter18 - Monads

  1. Monads are applicative functors.
class Applicative m => Monad m where
    (>>=) :: m a -> (a -> m b) -> m b --Binding Operation
    (>>) :: m a -> m b -> m b  --Sequencing Operation
    return :: a -> m a

fmap using Monadic Operations

fmap f xs = xs >>= return . f

Dependency Chain

Functor -> Applicative -> Monad
  • Eventhough there are three monadic operations, we only (>>=) to be a minimally complete monad instance.

join :: Monad m => m (m a) => m a
bind :: Monad m => (a -> m b) -> m a -> m b
bind f xs =
     join $ fmap f xs
  • Monad is not impure.
  • Monad is not an embedded language for doing impure programming.
  • Monad is not a value. It's a typeclass just like functor, applicative.
  • Monad is not about strictness.

Identity Laws

m >>= return = m
return x >> = f = f x

Associativity Law

(m >>= f) >>= g = m >>= (\x -> f x >>= g)

Monadic Composition - Kleisli Composition

(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c