Monadic join: flattens a nested monadic value.
$$\text{join} : m\,(m\;\alpha) \to m\;\alpha$$ $$\text{join}\;mma = mma \mathbin{>>=} \text{id}$$
Equations
- Control.Monad.join mma = mma >>= id
Instances For
Discard the result of a functor action, keeping only the effect.
$$\text{void} : f\;\alpha \to f\;\text{Unit}$$ $$\text{void}\;fa = (\lambda\;\_ \Rightarrow ()) \mathbin{<\$>} fa$$
Equations
- Control.Monad.void fa = (fun (x : α) => ()) <$> fa
Instances For
Conditional execution: run the action only when the boolean is true.
$$\text{«when»}\;b\;a = \begin{cases} a & \text{if } b \\ \text{pure}\;() & \text{otherwise} \end{cases}$$
Instances For
Conditional execution: run the action only when the boolean is false.
$$\text{unless}\;b\;a = \text{when}\;(\lnot b)\;a$$
Equations
- Control.Monad.unless b action = Control.Monad.when (!b) action
Instances For
Map a monadic function over a list, discarding the results.
$$\text{mapM\_}\;f\;[x_1, \ldots, x_n] = f\;x_1 \mathbin{>>} \cdots \mathbin{>>} f\;x_n \mathbin{>>} \text{pure}\;()$$
Equations
- Control.Monad.mapM_ f [] = pure ()
- Control.Monad.mapM_ f (x_1 :: xs) = do let _ ← f x_1 Control.Monad.mapM_ f xs
Instances For
Flipped mapM_: iterate over a list with a monadic action, discarding results.
$$\text{forM\_}\;xs\;f = \text{mapM\_}\;f\;xs$$
Equations
- Control.Monad.forM_ xs f = Control.Monad.mapM_ f xs
Instances For
Monadic left fold over a list.
$$\text{foldM}\;f\;z\;[x_1, \ldots, x_n] = f\;z\;x_1 \mathbin{>>=} \lambda z_1 \Rightarrow f\;z_1\;x_2 \mathbin{>>=} \cdots$$
Equations
- Control.Monad.foldM f init [] = pure init
- Control.Monad.foldM f init (x_1 :: xs) = do let acc ← f init x_1 Control.Monad.foldM f acc xs
Instances For
Repeat a monadic action n times, collecting the results.
$$\text{replicateM}\;n\;ma = [ma, ma, \ldots]\text{ (n times)}$$
Equations
- Control.Monad.replicateM 0 ma = pure []
- Control.Monad.replicateM n_2.succ ma = do let a ← ma let as ← Control.Monad.replicateM n_2 ma pure (a :: as)
Instances For
Repeat a monadic action n times, discarding the results.
$$\text{replicateM\_}\;n\;ma = ma \mathbin{>>} \cdots \mathbin{>>} ma \mathbin{>>} \text{pure}\;()$$
Equations
- Control.Monad.replicateM_ 0 ma = pure ()
- Control.Monad.replicateM_ n_2.succ ma = do let _ ← ma Control.Monad.replicateM_ n_2 ma
Instances For
Kleisli composition (left-to-right): $(f \mathbin{>=>} g)\;a = f\;a \mathbin{>>=} g$.
$$\text{fish} : (\alpha \to m\;\beta) \to (\beta \to m\;\gamma) \to \alpha \to m\;\gamma$$
Equations
- Control.Monad.fish f g a = f a >>= g
Instances For
Kleisli composition (right-to-left): $(g \mathbin{<=<} f) = f \mathbin{>=>} g$.
$$\text{fishBack} : (\beta \to m\;\gamma) \to (\alpha \to m\;\beta) \to \alpha \to m\;\gamma$$
Equations
Instances For
Join-pure law: joining a pure value is the identity.
$$\text{join}\;(\text{pure}\;x) = x$$