Documentation

Mathlib.Control.Monad.Cont

Continuation Monad #

Monad encapsulating continuation passing programming style, similar to Haskell's Cont, ContT and MonadCont: https://hackage.haskell.org/package/mtl-2.2.2/docs/Control-Monad-Cont.html https://hackage.haskell.org/package/transformers-0.6.2.0/docs/Control-Monad-Trans-Cont.html

structure MonadCont.Label (α : Type w) (m : Type u → Type v) (β : Type u) :
Type (max v w)
  • apply : αm β
Instances For
    def MonadCont.goto {α : Type u_1} {β : Type u} {m : Type u → Type v} (f : Label α m β) (x : α) :
    m β
    Equations
      Instances For
        class MonadCont (m : Type u → Type v) :
        Type (max (u + 1) v)
        • callCC {α β : Type u} : (Label α m βm α)m α
        Instances
          class LawfulMonadCont (m : Type u → Type v) [Monad m] [MonadCont m] extends LawfulMonad m :
          Instances
            def ContT (r : Type u) (m : Type u → Type v) (α : Type w) :
            Type (max v w)
            Equations
              Instances For
                @[reducible, inline]
                abbrev Cont (r : Type u) (α : Type w) :
                Type (max u w)
                Equations
                  Instances For
                    def ContT.run {r : Type u} {m : Type u → Type v} {α : Type w} :
                    ContT r m α(αm r)m r
                    Equations
                      Instances For
                        def ContT.map {r : Type u} {m : Type u → Type v} {α : Type w} (f : m rm r) (x : ContT r m α) :
                        ContT r m α
                        Equations
                          Instances For
                            theorem ContT.run_contT_map_contT {r : Type u} {m : Type u → Type v} {α : Type w} (f : m rm r) (x : ContT r m α) :
                            (map f x).run = f x.run
                            def ContT.withContT {r : Type u} {m : Type u → Type v} {α β : Type w} (f : (βm r)αm r) (x : ContT r m α) :
                            ContT r m β
                            Equations
                              Instances For
                                theorem ContT.run_withContT {r : Type u} {m : Type u → Type v} {α β : Type w} (f : (βm r)αm r) (x : ContT r m α) :
                                (withContT f x).run = x.run f
                                theorem ContT.ext {r : Type u} {m : Type u → Type v} {α : Type w} {x y : ContT r m α} (h : ∀ (f : αm r), x.run f = y.run f) :
                                x = y
                                theorem ContT.ext_iff {r : Type u} {m : Type u → Type v} {α : Type w} {x y : ContT r m α} :
                                x = y ∀ (f : αm r), x.run f = y.run f
                                instance ContT.instMonad {r : Type u} {m : Type u → Type v} :
                                Monad (ContT r m)
                                Equations
                                  instance ContT.instLawfulMonad {r : Type u} {m : Type u → Type v} :
                                  def ContT.monadLift {r : Type u} {m : Type u → Type v} [Monad m] {α : Type u} :
                                  m αContT r m α
                                  Equations
                                    Instances For
                                      instance ContT.instMonadLiftOfMonad {r : Type u} {m : Type u → Type v} [Monad m] :
                                      Equations
                                        theorem ContT.monadLift_bind {r : Type u} {m : Type u → Type v} [Monad m] [LawfulMonad m] {α β : Type u} (x : m α) (f : αm β) :
                                        instance ContT.instMonadCont {r : Type u} {m : Type u → Type v} :
                                        Equations
                                          instance ContT.instLawfulMonadCont {r : Type u} {m : Type u → Type v} :
                                          instance ContT.instMonadExcept {r : Type u} {m : Type u → Type v} (ε : Type u_1) [MonadExcept ε m] :

                                          Note that tryCatch does not have correct behavior in this monad:

                                          def foo : ContT Bool (Except String) Bool := do
                                            let x ← try
                                              pure true
                                            catch _ =>
                                              return false
                                            throw s!"oh no {x}"
                                          #eval foo.run pure
                                          -- `Except.ok false`, no error
                                          

                                          Here, the throwError is being run inside the try. See Zulip for further discussion.

                                          Equations
                                            def ExceptT.mkLabel {m : Type u → Type v} [Monad m] {α β ε : Type u} :
                                            MonadCont.Label (Except ε α) m βMonadCont.Label α (ExceptT ε m) β
                                            Equations
                                              Instances For
                                                theorem ExceptT.goto_mkLabel {m : Type u → Type v} [Monad m] {α β ε : Type u} (x : MonadCont.Label (Except ε α) m β) (i : α) :
                                                def ExceptT.callCC {m : Type u → Type v} [Monad m] {ε : Type u} [MonadCont m] {α β : Type u} (f : MonadCont.Label α (ExceptT ε m) βExceptT ε m α) :
                                                ExceptT ε m α
                                                Equations
                                                  Instances For
                                                    instance instMonadContExceptT {m : Type u → Type v} [Monad m] {ε : Type u} [MonadCont m] :
                                                    Equations
                                                      def OptionT.mkLabel {m : Type u → Type v} [Monad m] {α β : Type u} :
                                                      Equations
                                                        Instances For
                                                          theorem OptionT.goto_mkLabel {m : Type u → Type v} [Monad m] {α β : Type u} (x : MonadCont.Label (Option α) m β) (i : α) :
                                                          def OptionT.callCC {m : Type u → Type v} [Monad m] [MonadCont m] {α β : Type u} (f : MonadCont.Label α (OptionT m) βOptionT m α) :
                                                          OptionT m α
                                                          Equations
                                                            Instances For
                                                              @[simp]
                                                              theorem run_callCC {m : Type u → Type v} [Monad m] [MonadCont m] {α β : Type u} (f : MonadCont.Label α (OptionT m) βOptionT m α) :
                                                              instance instMonadContOptionT {m : Type u → Type v} [Monad m] [MonadCont m] :
                                                              Equations
                                                                def WriterT.mkLabel {m : Type u → Type v} [Monad m] {α : Type u_1} {β ω : Type u} [EmptyCollection ω] :
                                                                MonadCont.Label (α × ω) m βMonadCont.Label α (WriterT ω m) β
                                                                Equations
                                                                  Instances For
                                                                    def WriterT.mkLabel' {m : Type u → Type v} [Monad m] {α : Type u_1} {β ω : Type u} [Monoid ω] :
                                                                    MonadCont.Label (α × ω) m βMonadCont.Label α (WriterT ω m) β
                                                                    Equations
                                                                      Instances For
                                                                        theorem WriterT.goto_mkLabel {m : Type u → Type v} [Monad m] {α : Type u_1} {β ω : Type u} [EmptyCollection ω] (x : MonadCont.Label (α × ω) m β) (i : α) :
                                                                        theorem WriterT.goto_mkLabel' {m : Type u → Type v} [Monad m] {α : Type u_1} {β ω : Type u} [Monoid ω] (x : MonadCont.Label (α × ω) m β) (i : α) :
                                                                        def WriterT.callCC {m : Type u → Type v} [Monad m] [MonadCont m] {α β ω : Type u} [EmptyCollection ω] (f : MonadCont.Label α (WriterT ω m) βWriterT ω m α) :
                                                                        WriterT ω m α
                                                                        Equations
                                                                          Instances For
                                                                            def WriterT.callCC' {m : Type u → Type v} [Monad m] [MonadCont m] {α β ω : Type u} [Monoid ω] (f : MonadCont.Label α (WriterT ω m) βWriterT ω m α) :
                                                                            WriterT ω m α
                                                                            Equations
                                                                              Instances For
                                                                                instance instMonadContWriterTOfMonadOfMonoid {m : Type u → Type v} (ω : Type u) [Monad m] [Monoid ω] [MonadCont m] :
                                                                                Equations
                                                                                  def StateT.mkLabel {m : Type u → Type v} {α β σ : Type u} :
                                                                                  MonadCont.Label (α × σ) m (β × σ)MonadCont.Label α (StateT σ m) β
                                                                                  Equations
                                                                                    Instances For
                                                                                      theorem StateT.goto_mkLabel {m : Type u → Type v} {α β σ : Type u} (x : MonadCont.Label (α × σ) m (β × σ)) (i : α) :
                                                                                      MonadCont.goto (mkLabel x) i = StateT.mk fun (s : σ) => MonadCont.goto x (i, s)
                                                                                      def StateT.callCC {m : Type u → Type v} {σ : Type u} [MonadCont m] {α β : Type u} (f : MonadCont.Label α (StateT σ m) βStateT σ m α) :
                                                                                      StateT σ m α
                                                                                      Equations
                                                                                        Instances For
                                                                                          instance instMonadContStateT {m : Type u → Type v} {σ : Type u} [MonadCont m] :
                                                                                          Equations
                                                                                            def ReaderT.mkLabel {m : Type u → Type v} {α : Type u_1} {β : Type u} (ρ : Type u) :
                                                                                            MonadCont.Label α m βMonadCont.Label α (ReaderT ρ m) β
                                                                                            Equations
                                                                                              Instances For
                                                                                                theorem ReaderT.goto_mkLabel {m : Type u → Type v} {α : Type u_1} {ρ β : Type u} (x : MonadCont.Label α m β) (i : α) :
                                                                                                def ReaderT.callCC {m : Type u → Type v} {ε : Type u} [MonadCont m] {α β : Type u} (f : MonadCont.Label α (ReaderT ε m) βReaderT ε m α) :
                                                                                                ReaderT ε m α
                                                                                                Equations
                                                                                                  Instances For
                                                                                                    instance instMonadContReaderT {m : Type u → Type v} {ρ : Type u} [MonadCont m] :
                                                                                                    Equations
                                                                                                      def ContT.equiv {m₁ : Type u₀ → Type v₀} {m₂ : Type u₁ → Type v₁} {α₁ r₁ : Type u₀} {α₂ r₂ : Type u₁} (F : m₁ r₁ m₂ r₂) (G : α₁ α₂) :
                                                                                                      ContT r₁ m₁ α₁ ContT r₂ m₂ α₂

                                                                                                      reduce the equivalence between two continuation passing monads to the equivalence between their underlying monad

                                                                                                      Equations
                                                                                                        Instances For