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
Equations
- One or more equations did not get rendered due to their size.
Creates an instance of Monad, with an explicitly given empty and append operation.
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 ω]
.
Equations
- WriterT.monad empty append = Monad.mk
Instances For
Lift an M
to a WriterT ω M
, using the given empty
as the monoid unit.
Equations
- WriterT.liftTell empty = { monadLift := fun {α} cmd => WriterT.mk ((fun a => (a, empty)) <$> cmd) }
Instances For
Equations
- WriterT.instMonadWriterT = WriterT.monad ∅ fun x x_1 => x ++ x_1
Equations
- WriterT.instMonadLiftWriterT = WriterT.liftTell ∅
Equations
- One or more equations did not get rendered due to their size.
Equations
- WriterT.instMonadExceptWriterT = { throw := fun {α} e => WriterT.mk (throw e), tryCatch := fun {α} cmd c => WriterT.mk (tryCatch cmd fun e => WriterT.run (c e)) }
Equations
- WriterT.instMonadControlWriterT = { stM := fun α => α × ω, liftWith := fun {α} f => liftM (f fun {β} x => WriterT.run x), restoreM := fun {α} => WriterT.mk }
Equations
- WriterT.instMonadFunctorWriterT = { monadMap := fun {α} k w => WriterT.mk (k (α × ω) w) }
Equations
- WriterT.adapt f cmd = WriterT.mk (Prod.map id f <$> cmd)
Instances For
- adaptWriter : {α : Type u} → (ω → ω) → m α → m α
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
.
Instances
Transitivity.
see Note [lower instance priority]
Equations
- monadWriterAdapterTrans = { adaptWriter := fun {α} f => monadMap fun {α} => adaptWriter f }
Equations
- instMonadWriterAdapterWriterT = { adaptWriter := fun {α} => WriterT.adapt }
reduce the equivalence between two writer monads to the equivalence between their underlying monad
Equations
- One or more equations did not get rendered due to their size.