The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V33. Data structures and computation

Mathlib.Control.Monad.Cont

75 declarations. Mathlib docs · Source

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

def MonadCont.Label.ctorIdx E1 #

{α : Type w} → {m : Type u → Type v} → {β : Type u} → MonadCont.Label α m β → ℕ

def MonadCont.ctorIdx E1 #

{m : Type u → Type v} → MonadCont m → ℕ

theorem MonadCont.Label.mk.injEq E0 #

∀ {α : Type w} {m : Type u → Type v} {β : Type u} (apply apply_1 : α → m β),
  ({ apply := apply } = { apply := apply_1 }) = (apply = apply_1)

theorem MonadCont.Label.mk.inj E1 #

∀ {α : Type w} {m : Type u → Type v} {β : Type u} {apply apply_1 : α → m β},
  { apply := apply } = { apply := apply_1 } → apply = apply_1

structure MonadCont.Label E1 #

Type w → (Type u → Type v) → Type u → Type (max v w)

A Label α m β is a jump target for MonadCont.callCC: it wraps a continuation α → m β, which represents the rest of the computation surrounding the callCC block, pending the value of type α. Jumping to a label with MonadCont.goto abandons the rest of the block and finishes it immediately.

The result type β is arbitrary: a jump never returns control to the jump site, so the m β it "returns" is never consumed (compare throw).

ctor MonadCont.Label.mk E0 #

{α : Type w} → {m : Type u → Type v} → {β : Type u} → (α → m β) → MonadCont.Label α m β

abbrev MonadCont.Label.apply E1 #

{α : Type w} → {m : Type u → Type v} → {β : Type u} → MonadCont.Label α m β → α → m β

The continuation to jump to. Use MonadCont.goto rather than calling this directly.

abbrev MonadCont.goto E1 #

{α : Type u_1} → {β : Type u} → {m : Type u → Type v} → MonadCont.Label α m β → α → m β

Jump to a label: abandon the rest of the enclosing MonadCont.callCC block and finish the block immediately, with x as its result.

class MonadCont E1 #

(Type u → Type v) → Type (max (u + 1) v)

The class of monads equipped with callCC ("call with current continuation").

At any point in a monadic computation, the "current continuation" is the rest of the computation: everything that is waiting to consume the value produced so far. callCC f captures the current continuation, packages it as a first-class value (a MonadCont.Label), and hands it to f. The label then acts as an escape hatch out of f, allowing possibilities like early termination of the computation.

The canonical instance is the continuation monad transformer ContT.

ctor MonadCont.mk E0 #

{m : Type u → Type v} → ({α β : Type u} → (MonadCont.Label α m β → m α) → m α) → MonadCont m

abbrev MonadCont.callCC E1 #

{m : Type u → Type v} → [self : MonadCont m] → {α β : Type u} → (MonadCont.Label α m β → m α) → m α

Capture the current continuation as a Label and pass it to the given block.

class LawfulMonadCont E1 #

(m : Type u → Type v) → [Monad m] → [MonadCont m] → Prop

The laws a well-behaved callCC must satisfy.

ctor LawfulMonadCont.mk E0 #

∀ {m : Type u → Type v} [inst : Monad m] [inst_1 : MonadCont m] [toLawfulMonad : LawfulMonad m],
  (∀ {α ω γ : Type u} (cmd : m α) (next : MonadCont.Label ω m γ → α → m ω),
      (MonadCont.callCC fun f ↦ cmd >>= next f) = do
        let x ← cmd
        MonadCont.callCC fun f ↦ next f x) →
    (∀ {α : Type u} (β : Type u) (x : α) (dead : MonadCont.Label α m β → β → m α),
        (MonadCont.callCC fun f ↦ MonadCont.goto f x >>= dead f) = pure x) →
      (∀ {α β : Type u} (dummy : m α), (MonadCont.callCC fun x ↦ dummy) = dummy) → LawfulMonadCont m

instance LawfulMonadCont.toLawfulMonad E0 #

∀ {m : Type u → Type v} {inst : Monad m} {inst_1 : MonadCont m} [self : LawfulMonadCont m], LawfulMonad m

theorem LawfulMonadCont.callCC_bind_right E1 #

∀ {m : Type u → Type v} {inst : Monad m} {inst_1 : MonadCont m} [self : LawfulMonadCont m] {α ω γ : Type u} (cmd : m α)
  (next : MonadCont.Label ω m γ → α → m ω),
  (MonadCont.callCC fun f ↦ cmd >>= next f) = do
    let x ← cmd
    MonadCont.callCC fun f ↦ next f x

Actions performed before the label is available can be moved out of the block.

theorem LawfulMonadCont.callCC_bind_left E1 #

∀ {m : Type u → Type v} {inst : Monad m} {inst_1 : MonadCont m} [self : LawfulMonadCont m] {α : Type u} (β : Type u)
  (x : α) (dead : MonadCont.Label α m β → β → m α), (MonadCont.callCC fun f ↦ MonadCont.goto f x >>= dead f) = pure x

A jump discards everything sequenced after it.

theorem LawfulMonadCont.callCC_dummy E1 #

∀ {m : Type u → Type v} {inst : Monad m} {inst_1 : MonadCont m} [self : LawfulMonadCont m] {α β : Type u} (dummy : m α),
  (MonadCont.callCC fun x ↦ dummy) = dummy

A block that never uses its label is just the underlying computation.

def ContT E1 #

Type u → (Type u → Type v) → Type w → Type (max v w)

The continuation transformer, the canonical instance of the MonadCont class.

Given a return type r, a type transformer (typically a monad) m, and a type α, ContT r m α represents computations that take a continuation function from α to m r and return an m r.

This allows for continuation-passing style programming, where control flow can be manipulated by capturing and invoking continuations.

abbrev Cont E1 #

Type u → Type w → Type (max u w)

The continuation monad: ContT over the identity monad.

def ContT.mk E1 #

{r : Type u} → {m : Type u → Type v} → {α : Type w} → ((α → m r) → m r) → ContT r m α

Build a ContT from a function taking a continuation callback.

def ContT.run E1 #

{r : Type u} → {m : Type u → Type v} → {α : Type w} → ContT r m α → (α → m r) → m r

Run a ContT with a provided callback.

def ContT.map E1 #

{r : Type u} → {m : Type u → Type v} → {α : Type w} → (m r → m r) → ContT r m α → ContT r m α

Compose a given function with the continuation computation.

theorem ContT.run_contT_map_contT E1 #

∀ {r : Type u} {m : Type u → Type v} {α : Type w} (f : m r → m r) (x : ContT r m α), (ContT.map f x).run = f ∘ x.run

def ContT.withContT E1 #

{r : Type u} → {m : Type u → Type v} → {α β : Type w} → ((β → m r) → α → m r) → ContT r m α → ContT r m β

Transform the continuation of a computation.

theorem ContT.run_withContT E1 #

∀ {r : Type u} {m : Type u → Type v} {α β : Type w} (f : (β → m r) → α → m r) (x : ContT r m α),
  (ContT.withContT f x).run = x.run ∘ f

theorem ContT.ext E1 #

∀ {r : Type u} {m : Type u → Type v} {α : Type w} {x y : ContT r m α}, (∀ (f : α → m r), x.run f = y.run f) → x = y

theorem ContT.ext_iff E0 #

∀ {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 E0 #

{r : Type u} → {m : Type u → Type v} → Monad (ContT r m)

theorem ContT.run_mk E0 #

∀ {r : Type u} {m : Type u → Type v} {α : Type w} (f : (α → m r) → m r) (k : α → m r), (ContT.mk f).run k = f k

theorem ContT.run_pure E0 #

∀ {r : Type u} {m : Type u → Type v} {α : Type w} (a : α) (k : α → m r), (pure a).run k = k a

theorem ContT.run_bind E0 #

∀ {r : Type u} {m : Type u → Type v} {α β : Type w} (x : ContT r m α) (f : α → ContT r m β) (k : β → m r),
  (x >>= f).run k = x.run fun x ↦ (f x).run k

theorem ContT.run_map E0 #

∀ {r : Type u} {m : Type u → Type v} {α β : Type w} (f : α → β) (x : ContT r m α) (k : β → m r),
  (f <$> x).run k = x.run (k ∘ f)

theorem ContT.run_seq E0 #

∀ {r : Type u} {m : Type u → Type v} {α β : Type w} (f : ContT r m (α → β)) (x : ContT r m α) (k : β → m r),
  (f <*> x).run k = f.run fun f ↦ x.run (k ∘ f)

theorem ContT.run_seqLeft E0 #

∀ {r : Type u} {m : Type u → Type v} {α β : Type w} (x : ContT r m α) (y : ContT r m β) (k : α → m r),
  (x <* y).run k = x.run fun x ↦ y.run fun x_1 ↦ k x

theorem ContT.run_seqRight E0 #

∀ {r : Type u} {m : Type u → Type v} {α β : Type w} (x : ContT r m α) (y : ContT r m β) (k : β → m r),
  (x *> y).run k = x.run fun x ↦ y.run k

instance ContT.instLawfulMonad E0 #

∀ {r : Type u} {m : Type u → Type v}, LawfulMonad (ContT r m)

instance ContT.instMonadLiftOfMonad E0 #

{r : Type u} → {m : Type u → Type v} → [Monad m] → MonadLift m (ContT r m)

theorem ContT.run_monadLift E0 #

∀ {r : Type u} {m : Type u → Type v} [inst : Monad m] {α : Type u} (x : m α) (k : α → m r),
  (monadLift x).run k = x >>= k

theorem ContT.monadLift_bind E1 #

∀ {r : Type u} {m : Type u → Type v} [inst : Monad m] [LawfulMonad m] {α β : Type u} (x : m α) (f : α → m β),
  monadLift (x >>= f) = monadLift x >>= monadLift ∘ f

instance ContT.instMonadCont E0 #

{r : Type u} → {m : Type u → Type v} → MonadCont (ContT r m)

theorem ContT.run_callCC E0 #

∀ {r : Type u} {m : Type u → Type v} {α β : Type w} (f : MonadCont.Label α (ContT r m) β → ContT r m α) (k : α → m r),
  (MonadCont.callCC f).run k = (f { apply := fun x ↦ ContT.mk fun x_1 ↦ k x }).run k

instance ContT.instLawfulMonadCont E0 #

∀ {r : Type u} {m : Type u → Type v}, LawfulMonadCont (ContT r m)

instance ContT.instMonadExceptOf E0 #

{r : Type u} → {m : Type u → Type v} → (ε : Type u_1) → [MonadExceptOf ε m] → MonadExceptOf ε (ContT r 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.

theorem ContT.run_throw E0 #

∀ {r : Type u} {m : Type u → Type v} {α : Type w} {ε : Type u_1} [inst : MonadExceptOf ε m] (e : ε) (f : α → m r),
  (throw e).run f = throw e

theorem ContT.run_tryCatch E0 #

∀ {r : Type u} {m : Type u → Type v} {α : Type w} {ε : Type u_1} [inst : MonadExceptOf ε m] (act : ContT r m α)
  (h : ε → ContT r m α) (f : α → m r), (tryCatch act h).run f = tryCatch (act.run f) fun e ↦ (h e).run f

def ExceptT.mkLabel E1 #

{m : Type u → Type v} →
  [Monad m] → {α β ε : Type u} → MonadCont.Label (Except ε α) m β → MonadCont.Label α (ExceptT ε m) β

Lift a jump target along ExceptT: jumping with a becomes a jump in the base monad with Except.ok a.

theorem ExceptT.goto_mkLabel E1 #

∀ {m : Type u → Type v} [inst : Monad m] {α β ε : Type u} (x : MonadCont.Label (Except ε α) m β) (i : α),
  MonadCont.goto (ExceptT.mkLabel x) i = ExceptT.mk (Except.ok <$> MonadCont.goto x (Except.ok i))

def ExceptT.callCC E1 #

{m : Type u → Type v} →
  [Monad m] →
    {ε : Type u} → [MonadCont m] → {α β : Type u} → (MonadCont.Label α (ExceptT ε m) β → ExceptT ε m α) → ExceptT ε m α

The callCC operation of ExceptT ε m, delegating to callCC in the base monad.

instance instMonadContExceptT E0 #

{m : Type u → Type v} → [Monad m] → {ε : Type u} → [MonadCont m] → MonadCont (ExceptT ε m)

instance instLawfulMonadContExceptT E0 #

∀ {m : Type u → Type v} [inst : Monad m] {ε : Type u} [inst_1 : MonadCont m] [LawfulMonadCont m],
  LawfulMonadCont (ExceptT ε m)

def OptionT.mkLabel E1 #

{m : Type u → Type v} → [Monad m] → {α β : Type u} → MonadCont.Label (Option α) m β → MonadCont.Label α (OptionT m) β

Lift a jump target along OptionT: jumping with a becomes a jump in the base monad with some a.

theorem OptionT.goto_mkLabel E1 #

∀ {m : Type u → Type v} [inst : Monad m] {α β : Type u} (x : MonadCont.Label (Option α) m β) (i : α),
  MonadCont.goto (OptionT.mkLabel x) i =
    OptionT.mk do
      let a ← MonadCont.goto x (some i)
      pure (some a)

def OptionT.callCC E1 #

{m : Type u → Type v} →
  [Monad m] → [MonadCont m] → {α β : Type u} → (MonadCont.Label α (OptionT m) β → OptionT m α) → OptionT m α

The callCC operation of OptionT m, delegating to callCC in the base monad.

theorem run_callCC E0 #

∀ {m : Type u → Type v} [inst : Monad m] [inst_1 : MonadCont m] {α β : Type u}
  (f : MonadCont.Label α (OptionT m) β → OptionT m α),
  (OptionT.callCC f).run = MonadCont.callCC fun x ↦ (f (OptionT.mkLabel x)).run

instance instMonadContOptionT E0 #

{m : Type u → Type v} → [Monad m] → [MonadCont m] → MonadCont (OptionT m)

instance instLawfulMonadContOptionT E0 #

∀ {m : Type u → Type v} [inst : Monad m] [inst_1 : MonadCont m] [LawfulMonadCont m], LawfulMonadCont (OptionT m)

def WriterT.mkLabel E1 #

{m : Type u → Type v} →
  [Monad m] →
    {α : Type u_1} →
      {β ω : Type u} → [EmptyCollection ω] → MonadCont.Label (α × ω) m β → MonadCont.Label α (WriterT ω m) β

Lift a jump target along WriterT (with EmptyCollection log): jumping with a becomes a jump in the base monad with (a, ∅), resetting the accumulated log to the start of the block.

def WriterT.mkLabel' E1 #

{m : Type u → Type v} →
  [Monad m] →
    {α : Type u_1} → {β ω : Type u} → [Monoid ω] → MonadCont.Label (α × ω) m β → MonadCont.Label α (WriterT ω m) β

Lift a jump target along WriterT (with Monoid log): jumping with a becomes a jump in the base monad with (a, 1), resetting the accumulated log to the start of the block.

theorem WriterT.goto_mkLabel E1 #

∀ {m : Type u → Type v} [inst : Monad m] {α : Type u_1} {β ω : Type u} [inst_1 : EmptyCollection ω]
  (x : MonadCont.Label (α × ω) m β) (i : α), MonadCont.goto (WriterT.mkLabel x) i = monadLift (MonadCont.goto x (i, ∅))

theorem WriterT.goto_mkLabel' E1 #

∀ {m : Type u → Type v} [inst : Monad m] {α : Type u_1} {β ω : Type u} [inst_1 : Monoid ω]
  (x : MonadCont.Label (α × ω) m β) (i : α), MonadCont.goto (WriterT.mkLabel' x) i = monadLift (MonadCont.goto x (i, 1))

def WriterT.callCC E1 #

{m : Type u → Type v} →
  [Monad m] →
    [MonadCont m] →
      {α β ω : Type u} → [EmptyCollection ω] → (MonadCont.Label α (WriterT ω m) β → WriterT ω m α) → WriterT ω m α

The callCC operation of WriterT ω m (with EmptyCollection log), delegating to callCC in the base monad.

def WriterT.callCC' E1 #

{m : Type u → Type v} →
  [Monad m] →
    [MonadCont m] → {α β ω : Type u} → [Monoid ω] → (MonadCont.Label α (WriterT ω m) β → WriterT ω m α) → WriterT ω m α

The callCC operation of WriterT ω m (with Monoid log), delegating to callCC in the base monad.

instance instMonadContWriterTOfMonadOfEmptyCollection E0 #

{m : Type u → Type v} → (ω : Type u) → [Monad m] → [EmptyCollection ω] → [MonadCont m] → MonadCont (WriterT ω m)

instance instMonadContWriterTOfMonadOfMonoid E0 #

{m : Type u → Type v} → (ω : Type u) → [Monad m] → [Monoid ω] → [MonadCont m] → MonadCont (WriterT ω m)

def StateT.mkLabel E1 #

{m : Type u → Type v} → {α β σ : Type u} → MonadCont.Label (α × σ) m (β × σ) → MonadCont.Label α (StateT σ m) β

Lift a jump target along StateT: jumping with a becomes a jump in the base monad with (a, s), where s is the state at the jump site.

theorem StateT.goto_mkLabel E1 #

∀ {m : Type u → Type v} {α β σ : Type u} (x : MonadCont.Label (α × σ) m (β × σ)) (i : α),
  MonadCont.goto (StateT.mkLabel x) i = StateT.mk fun s ↦ MonadCont.goto x (i, s)

def StateT.callCC E1 #

{m : Type u → Type v} →
  {σ : Type u} → [MonadCont m] → {α β : Type u} → (MonadCont.Label α (StateT σ m) β → StateT σ m α) → StateT σ m α

The callCC operation of StateT σ m, delegating to callCC in the base monad; the state at the jump site is carried through the jump.

instance instMonadContStateT E0 #

{m : Type u → Type v} → {σ : Type u} → [MonadCont m] → MonadCont (StateT σ m)

instance instLawfulMonadContStateT E0 #

∀ {m : Type u → Type v} {σ : Type u} [inst : Monad m] [inst_1 : MonadCont m] [LawfulMonadCont m],
  LawfulMonadCont (StateT σ m)

def ReaderT.mkLabel E1 #

{m : Type u → Type v} →
  {α : Type u_1} → {β : Type u} → (ρ : Type u) → MonadCont.Label α m β → MonadCont.Label α (ReaderT ρ m) β

Lift a jump target along ReaderT; the jump does not depend on the environment.

theorem ReaderT.goto_mkLabel E1 #

∀ {m : Type u → Type v} {α : Type u_1} {ρ β : Type u} (x : MonadCont.Label α m β) (i : α),
  MonadCont.goto (ReaderT.mkLabel ρ x) i = monadLift (MonadCont.goto x i)

def ReaderT.callCC E1 #

{m : Type u → Type v} →
  {ε : Type u} → [MonadCont m] → {α β : Type u} → (MonadCont.Label α (ReaderT ε m) β → ReaderT ε m α) → ReaderT ε m α

The callCC operation of ReaderT ε m, delegating to callCC in the base monad.

instance instMonadContReaderT E0 #

{m : Type u → Type v} → {ρ : Type u} → [MonadCont m] → MonadCont (ReaderT ρ m)

instance instLawfulMonadContReaderT E0 #

∀ {m : Type u → Type v} {ρ : Type u} [inst : Monad m] [inst_1 : MonadCont m] [LawfulMonadCont m],
  LawfulMonadCont (ReaderT ρ m)

def ContT.equiv E1 #

{m₁ : Type u₀ → Type v₀} →
  {m₂ : Type u₁ → Type v₁} →
    {α₁ r₁ : Type u₀} → {α₂ r₂ : Type u₁} → m₁ r₁ ≃ m₂ r₂ → α₁ ≃ α₂ → ContT r₁ m₁ α₁ ≃ ContT r₂ m₂ α₂

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