- 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 f xs = xs >>= return . f
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.
m >>= return = m
return x >> = f = f x
(m >>= f) >>= g = m >>= (\x -> f x >>= g)
(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c