Monad combinators, as in Haskell's Control.Monad. #
Executes t
conditional on c
holding true, doing nothing otherwise.
Instances For
Applies the monadic action f
to every element in the list, left-to-right, and returns the list of
results.
This implementation is tail recursive. List.mapM'
is a a non-tail-recursive variant that may be
more convenient to reason about. List.forM
is the variant that discards the results and
List.mapA
is the variant that works with Applicative
.
Equations
Instances For
Applies the monadic action f
on every element in the list, left-to-right, and returns the list of
results.
This is a non-tail-recursive variant of List.mapM
that's easier to reason about. It cannot be used
as the main definition and replaced by the tail-recursive version because they can only be proved
equal when m
is a LawfulMonad
.
Equations
Instances For
Collapses two layers of monadic structure into one, passing the effects of the inner monad through the outer.
Equations
- @Monad.join = @joinM
Instances For
Applies the monadic predicate p
to every element in the list, in order from left to right, and
returns the list of elements for which p
returns true
.
O(|l|)
.
Example:
#eval [1, 2, 5, 2, 7, 7].filterM fun x => do
IO.println s!"Checking {x}"
return x < 3
Checking 1
Checking 2
Checking 5
Checking 2
Checking 7
Checking 7
[1, 2, 2]
Equations
Instances For
Folds a monadic function over a list from the left, accumulating a value starting with init
. The
accumulated value is combined with the each element of the list in order, using f
.
Example:
example [Monad m] (f : α → β → m α) :
List.foldlM (m := m) f x₀ [a, b, c] = (do
let x₁ ← f x₀ a
let x₂ ← f x₁ b
let x₃ ← f x₂ c
pure x₃)
:= by rfl
Equations
Instances For
Executes tm
or fm
depending on whether the result of mbool
is true
or false
respectively.
Equations
- @Monad.cond = @condM
Instances For
Executes a list of monadic actions in sequence, collecting the results.
Equations
- Monad.sequence [] = pure []
- Monad.sequence (h :: t) = do let h' ← h let t' ← Monad.sequence t pure (h' :: t')
Instances For
Executes a list of monadic actions in sequence, discarding the results.
Equations
- Monad.sequence' [] = pure ()
- Monad.sequence' (h :: t) = h *> Monad.sequence' t
Instances For
Executes t
if b
is true
, doing nothing otherwise.
Equations
- Monad.whenb b t = bif b then t else pure ()