The areas / V33. Data structures and computation
Mathlib.Control.Monad.Writer
44 declarations. Mathlib docs · Source
Writer monads
This file introduces monads for managing immutable, appendable state. Common applications are logging monads where the monad logs messages as the computation progresses.
References
- https://hackage.haskell.org/package/mtl-2.2.1/docs/Control-Monad-Writer-Class.html
- Original Mark P Jones article introducing
Writer
def MonadWriter.ctorIdx E1 #
{ω : outParam (Type u)} → {M : Type u → Type v} → MonadWriter ω M → ℕ
def MonadWriterAdapter.ctorIdx E1 #
{ω : outParam (Type u)} → {m : Type u → Type v} → MonadWriterAdapter ω m → ℕ
def WriterT E1 #
Type u → (Type u → Type v) → Type u → Type v
Adds a writable output of type ω to a monad.
The instances on this type assume that either [Monoid ω] or [EmptyCollection ω] [Append ω].
Use WriterT.run to obtain the final value of this output.
abbrev Writer E1 #
Type u_1 → Type u_1 → Type u_1
ctor MonadWriter.mk E0 #
{ω : outParam (Type u)} →
{M : Type u → Type v} →
(ω → M PUnit.{u + 1}) → ({α : Type u} → M α → M (α × ω)) → ({α : Type u} → M (α × (ω → ω)) → M α) → MonadWriter ω M
class MonadWriter E1 #
outParam (Type u) → (Type u → Type v) → Type (max (u + 1) v)
abbrev MonadWriter.tell E1 #
{ω : outParam (Type u)} → {M : Type u → Type v} → [self : MonadWriter ω M] → ω → M PUnit.{u + 1}
Emit an output w.
abbrev MonadWriter.listen E1 #
{ω : outParam (Type u)} → {M : Type u → Type v} → [self : MonadWriter ω M] → {α : Type u} → M α → M (α × ω)
Capture the output produced by f, without intercepting.
abbrev MonadWriter.pass E1 #
{ω : outParam (Type u)} → {M : Type u → Type v} → [self : MonadWriter ω M] → {α : Type u} → M (α × (ω → ω)) → M α
Buffer the output produced by f as w, then emit (← f).2 w in its place.
instance instMonadWriterReaderT E0 #
{M : Type u → Type v} → {ω ρ : Type u} → [MonadWriter ω M] → MonadWriter ω (ReaderT ρ M)
instance instMonadWriterStateTOfMonad E0 #
{M : Type u → Type v} → {ω σ : Type u} → [Monad M] → [MonadWriter ω M] → MonadWriter ω (StateT σ M)
def WriterT.mk E1 #
{M : Type u → Type v} → {α ω : Type u} → M (α × ω) → WriterT ω M α
def WriterT.run E1 #
{M : Type u → Type v} → {α ω : Type u} → WriterT ω M α → M (α × ω)
abbrev WriterT.runThe E1 #
{M : Type u → Type v} → {α : Type u} → (ω : Type u) → WriterT ω M α → M (α × ω)
theorem WriterT.run_mk E0 #
∀ {M : Type u → Type v} {α ω : Type u} (cmd : M (α × ω)), (WriterT.mk cmd).run = cmd
theorem WriterT.ext E1 #
∀ {M : Type u → Type v} {α ω : Type u} (x x' : WriterT ω M α), x.run = x'.run → x = x'
theorem WriterT.ext_iff E0 #
∀ {M : Type u → Type v} {α ω : Type u} {x x' : WriterT ω M α}, x = x' ↔ x.run = x'.run
def WriterT.monad E1 #
{M : Type u → Type v} → {ω : Type u} → [Monad M] → ω → (ω → ω → ω) → Monad (WriterT ω M)
Creates an instance of Monad, with explicitly given empty and append operations.
Previously, this would have used an instance of [Monoid ω] as input.
In practice, however, WriterT is used for logging and creating lists so restricting to
monoids with Mul and One can make WriterT cumbersome to use.
This is used to derive instances for both [EmptyCollection ω] [Append ω] and [Monoid ω].
theorem WriterT.run_pure E0 #
∀ {M : Type u → Type v} {α ω : Type u} [inst : Monad M] (empty : ω) (append : ω → ω → ω) (a : α),
(pure a).run = pure (a, empty)
theorem WriterT.run_map E0 #
∀ {M : Type u → Type v} {α β ω : Type u} [inst : Monad M] (empty : ω) (append : ω → ω → ω) (f : α → β)
(cmd : WriterT ω M α),
(f <$> cmd).run =
(fun x ↦
match x with
| (a, w) => (f a, w)) <$>
cmd.run
theorem WriterT.run_bind E0 #
∀ {M : Type u → Type v} {α β ω : Type u} [inst : Monad M] (empty : ω) (append : ω → ω → ω) (cmd : WriterT ω M α)
(f : α → WriterT ω M β),
(cmd >>= f).run = do
let x ← cmd.run
match x with
| (a, w₁) =>
(fun x ↦
match x with
| (b, w₂) => (b, append w₁ w₂)) <$>
(f a).run
def WriterT.liftTell E1 #
{M : Type u → Type v} → {ω : Type u} → [Monad M] → ω → MonadLift M (WriterT ω M)
Lift an M to a WriterT ω M, using the given empty as the monoid unit.
theorem WriterT.run_liftM E0 #
∀ {M : Type u → Type v} {α ω : Type u} [inst : Monad M] (empty : ω) (cmd : M α),
(liftM cmd).run = (fun a ↦ (a, empty)) <$> cmd
instance WriterT.instMonadOfEmptyCollectionOfAppend E0 #
{M : Type u → Type v} → {ω : Type u} → [Monad M] → [EmptyCollection ω] → [Append ω] → Monad (WriterT ω M)
instance WriterT.instMonadLiftOfEmptyCollection E0 #
{M : Type u → Type v} → {ω : Type u} → [Monad M] → [EmptyCollection ω] → MonadLift M (WriterT ω M)
instance WriterT.instMonadOfMonoid E0 #
{M : Type u → Type v} → {ω : Type u} → [Monad M] → [Monoid ω] → Monad (WriterT ω M)
instance WriterT.instMonadLiftOfMonoid E0 #
{M : Type u → Type v} → {ω : Type u} → [Monad M] → [Monoid ω] → MonadLift M (WriterT ω M)
instance WriterT.instLawfulMonad E0 #
∀ {M : Type u → Type v} {ω : Type u} [inst : Monad M] [inst_1 : Monoid ω] [LawfulMonad M], LawfulMonad (WriterT ω M)
instance WriterT.instMonadWriter E0 #
{M : Type u → Type v} → {ω : Type u} → [Monad M] → MonadWriter ω (WriterT ω M)
theorem WriterT.run_tell E0 #
∀ {M : Type u → Type v} {ω : Type u} [inst : Monad M] (w : ω), (tell w).run = pure (PUnit.unit, w)
theorem WriterT.run_listen E0 #
∀ {M : Type u → Type v} {α ω : Type u} [inst : Monad M] (cmd : WriterT ω M α),
(listen cmd).run =
(fun x ↦
match x with
| (a, w) => ((a, w), w)) <$>
cmd.run
theorem WriterT.run_pass E0 #
∀ {M : Type u → Type v} {α ω : Type u} [inst : Monad M] (cmd : WriterT ω M (α × (ω → ω))),
(pass cmd).run =
(fun x ↦
match x with
| ((a, f), w) => (a, f w)) <$>
cmd.run
instance WriterT.instMonadExceptOf E0 #
{M : Type u → Type v} → {ω : Type u} → {ε : Type u_1} → [MonadExceptOf ε M] → MonadExceptOf ε (WriterT ω M)
theorem WriterT.run_throw E0 #
∀ {α ω : Type u} {M : Type u → Type u_2} {ε : Type u_1} [inst : MonadExceptOf ε M] (e : ε), (throw e).run = throw e
theorem WriterT.run_tryCatch E0 #
∀ {α ω : Type u} {M : Type u → Type u_2} {ε : Type u_1} [inst : MonadExceptOf ε M] (cmd : WriterT ω M α)
(c : ε → WriterT ω M α), (tryCatch cmd c).run = tryCatch cmd.run fun e ↦ (c e).run
instance WriterT.instMonadControlOfMonadLiftT E0 #
{M : Type u → Type v} → {ω : Type u} → [MonadLiftT M (WriterT ω M)] → MonadControl M (WriterT ω M)
instance WriterT.instMonadFunctor E0 #
{M : Type u → Type v} → {ω : Type u} → MonadFunctor M (WriterT ω M)
def WriterT.adapt E1 #
{M : Type u → Type v} → {ω : Type u} → [Monad M] → {ω' α : Type u} → (ω → ω') → WriterT ω M α → WriterT ω' M α
class MonadWriterAdapter E1 #
outParam (Type u) → (Type u → Type v) → Type (max (u + 1) v)
Adapt a monad stack, changing the type of its top-most environment.
This class is comparable to Control.Lens.Magnify,
but does not use lenses (why would it), and is derived automatically for any transformer
implementing MonadFunctor.
ctor MonadWriterAdapter.mk E0 #
{ω : outParam (Type u)} → {m : Type u → Type v} → ({α : Type u} → (ω → ω) → m α → m α) → MonadWriterAdapter ω m
abbrev MonadWriterAdapter.adaptWriter E1 #
{ω : outParam (Type u)} → {m : Type u → Type v} → [self : MonadWriterAdapter ω m] → {α : Type u} → (ω → ω) → m α → m α
instance monadWriterAdapterTrans E0 #
{ω : Type u} →
{m : Type u → Type u_1} →
{n : Type u → Type v} → [MonadWriterAdapter ω m] → [MonadFunctor m n] → MonadWriterAdapter ω n
Transitivity.
see Note [lower instance priority]
instance instMonadWriterAdapterWriterTOfMonad E0 #
{ω : Type u} → {m : Type u → Type u_1} → [Monad m] → MonadWriterAdapter ω (WriterT ω m)
def WriterT.equiv E1 #
{m₁ : Type u₀ → Type v₀} →
{m₂ : Type u₁ → Type v₁} →
{α₁ ω₁ : Type u₀} → {α₂ ω₂ : Type u₁} → m₁ (α₁ × ω₁) ≃ m₂ (α₂ × ω₂) → WriterT ω₁ m₁ α₁ ≃ WriterT ω₂ m₂ α₂
reduce the equivalence between two writer monads to the equivalence between their underlying monad