Documentation

Mathlib.Control.Monad.Writer

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 #

def WriterT (ω : Type u) (M : Type u → Type v) (α : Type u) :
Equations
Instances For
    @[reducible]
    def Writer (ω : Type u_1) (α : Type u_1) :
    Type u_1
    Equations
    Instances For
      class MonadWriter (ω : outParam (Type u)) (M : Type u → Type v) :
      Type (max (u + 1) v)
      • tell : ωM PUnit.{u + 1}
      • listen : {α : Type u} → M αM (α × ω)
      • pass : {α : Type u} → M (α × (ωω))M α
      Instances
        instance instMonadWriterReaderT {M : Type u → Type v} {ω : Type u} {ρ : Type u} [MonadWriter ω M] :
        Equations
        • instMonadWriterReaderT = { tell := fun w => liftM (tell w), listen := fun {α} x r => listen (x r), pass := fun {α} x r => pass (x r) }
        instance instMonadWriterStateT {M : Type u → Type v} [Monad M] {ω : Type u} {σ : Type u} [MonadWriter ω M] :
        Equations
        • One or more equations did not get rendered due to their size.
        @[inline]
        def WriterT.mk {M : Type u → Type v} {α : Type u} {ω : Type u} (cmd : M (α × ω)) :
        WriterT ω M α
        Equations
        Instances For
          @[inline]
          def WriterT.run {M : Type u → Type v} {α : Type u} {ω : Type u} (cmd : WriterT ω M α) :
          M (α × ω)
          Equations
          Instances For
            @[inline]
            def WriterT.runThe {M : Type u → Type v} {α : Type u} (ω : Type u) (cmd : WriterT ω M α) :
            M (α × ω)
            Equations
            Instances For
              theorem WriterT.ext {M : Type u → Type v} {α : Type u} {ω : Type u} (x : WriterT ω M α) (x' : WriterT ω M α) (h : WriterT.run x = WriterT.run x') :
              x = x'
              @[inline, reducible]
              def WriterT.monad {M : Type u → Type v} [Monad M] {ω : Type u} (empty : ω) (append : ωωω) :
              Monad (WriterT ω M)

              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
              Instances For
                @[inline]
                def WriterT.liftTell {M : Type u → Type v} [Monad M] {ω : Type u} (empty : ω) :

                Lift an M to a WriterT ω M, using the given empty as the monoid unit.

                Equations
                Instances For
                  instance WriterT.instMonadWriterT {M : Type u → Type v} [Monad M] {ω : Type u} [EmptyCollection ω] [Append ω] :
                  Monad (WriterT ω M)
                  Equations
                  instance WriterT.instMonadLiftWriterT {M : Type u → Type v} [Monad M] {ω : Type u} [EmptyCollection ω] :
                  Equations
                  instance WriterT.instMonadWriterT_1 {M : Type u → Type v} [Monad M] {ω : Type u} [Monoid ω] :
                  Monad (WriterT ω M)
                  Equations
                  instance WriterT.instMonadLiftWriterT_1 {M : Type u → Type v} [Monad M] {ω : Type u} [Monoid ω] :
                  Equations
                  instance WriterT.instMonadWriterWriterT {M : Type u → Type v} [Monad M] {ω : Type u} :
                  Equations
                  • One or more equations did not get rendered due to their size.
                  instance WriterT.instMonadExceptWriterT {M : Type u → Type v} {ω : Type u} {ε : Type u_1} [MonadExcept ε M] :
                  Equations
                  instance WriterT.instMonadControlWriterT {M : Type u → Type v} {ω : Type u} [MonadLiftT M (WriterT ω M)] :
                  Equations
                  • WriterT.instMonadControlWriterT = { stM := fun α => α × ω, liftWith := fun {α} f => liftM (f fun {β} x => WriterT.run x), restoreM := fun {α} => WriterT.mk }
                  instance WriterT.instMonadFunctorWriterT {M : Type u → Type v} {ω : Type u} :
                  Equations
                  • WriterT.instMonadFunctorWriterT = { monadMap := fun {α} k w => WriterT.mk (k (α × ω) w) }
                  @[inline]
                  def WriterT.adapt {M : Type u → Type v} [Monad M] {ω : Type u} {ω' : Type u} {α : Type u} (f : ωω') :
                  WriterT ω M αWriterT ω' M α
                  Equations
                  Instances For
                    class MonadWriterAdapter (ω : outParam (Type u)) (m : Type u → Type v) :
                    Type (max (u + 1) v)
                    • 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
                      instance monadWriterAdapterTrans {ω : Type u} {m : Type u → Type u_1} {n : Type u → Type v} [MonadWriterAdapter ω m] [MonadFunctor m n] :

                      Transitivity.

                      see Note [lower instance priority]

                      Equations
                      instance instMonadWriterAdapterWriterT {m : Type u_1 → Type u_2} {ω : Type u_1} [Monad m] :
                      Equations
                      • instMonadWriterAdapterWriterT = { adaptWriter := fun {α} => WriterT.adapt }
                      def WriterT.equiv {m₁ : Type u₀ → Type v₀} {m₂ : Type u₁ → Type v₁} {α₁ : Type u₀} {ω₁ : Type u₀} {α₂ : Type u₁} {ω₂ : Type u₁} (F : m₁ (α₁ × ω₁) m₂ (α₂ × ω₂)) :
                      WriterT ω₁ m₁ α₁ WriterT ω₂ m₂ α₂

                      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.
                      Instances For