Writer monads #
A computation of the writer monad Writer ω yields a value and logs output in ω: pure logs
nothing and bind appends the logs. Forgetting the log is a monad morphism to Id,
and bind only extends the log, so a value can feed later logging while the
log never feeds a value.
Main definitions #
Writer.val,Writer.log: the value and the log of a computation.WriterT.instLawfulMonadList: the monad laws for list logs, over any lawful monad.
Implementation notes #
- Mathlib's two
Monadinstances onWriter ω, for[Monoid ω]and for[EmptyCollection ω] [Append ω], are bothWriterT.monad empty append; the projection lemmas are stated for that monad, as mathlib statesWriterT.run_bind, and apply under either. - Mathlib proves the monad laws for monoid logs only, and lists are not a monoid there.
instance
WriterT.instLawfulMonadList
{M : Type u → Type v}
{P : Type u}
[Monad M]
[LawfulMonad M]
:
LawfulMonad (WriterT (List P) M)
A writer monad with list logs is lawful over any lawful monad.
The value of a computation is the first component of its run.
Instances For
The log of a computation is the second component of its run.
Instances For
@[simp]
@[simp]
theorem
Writer.log_pure
{ω A : Type u}
(empty : ω)
(append : ω → ω → ω)
(a : A)
:
(pure a).log = empty
@[simp]
theorem
Writer.val_map
{ω A B : Type u}
(empty : ω)
(append : ω → ω → ω)
(f : A → B)
(m : Writer ω A)
:
@[simp]
theorem
Writer.log_map
{ω A B : Type u}
(empty : ω)
(append : ω → ω → ω)
(f : A → B)
(m : Writer ω A)
:
theorem
Writer.log_prefix_bind
{A B P : Type u}
(m : Writer (List P) A)
(f : A → Writer (List P) B)
:
The log of m >>= f extends the log of m, so composition is isotone for the prefix order
on list logs.