Continuation Monad #
Monad encapsulating continuation passing programming style, similar to
Haskell's Cont
, ContT
and MonadCont
:
def
MonadCont.goto
{α : Type u_1}
{β : Type u}
{m : Type u → Type v}
(f : ContT.Label α m β)
(x : α)
:
m β
Equations
- ContT.goto f x = f.apply x
Instances For
- map_const : ∀ {α β : Type u}, Functor.mapConst = Functor.map ∘ Function.const β
- seqLeft_eq : ∀ {α β : Type u} (x : m α) (y : m β), (SeqLeft.seqLeft x fun (x : Unit) => y) = Seq.seq (Function.const β <$> x) fun (x : Unit) => y
- seqRight_eq : ∀ {α β : Type u} (x : m α) (y : m β), (SeqRight.seqRight x fun (x : Unit) => y) = Seq.seq (Function.const α id <$> x) fun (x : Unit) => y
- callCC_bind_right : ∀ {α ω γ : Type u} (cmd : m α) (next : ContT.Label ω m γ → α → m ω), (MonadCont.callCC fun (f : ContT.Label ω m γ) => cmd >>= next f) = do let x ← cmd MonadCont.callCC fun (f : ContT.Label ω m γ) => next f x
- callCC_bind_left : ∀ {α : Type u} (β : Type u) (x : α) (dead : ContT.Label α m β → β → m α), (MonadCont.callCC fun (f : ContT.Label α m β) => ContT.goto f x >>= dead f) = pure x
- callCC_dummy : ∀ {α β : Type u} (dummy : m α), (MonadCont.callCC fun (x : ContT.Label α m β) => dummy) = dummy
Instances
instance
ContT.instLawfulMonadContTInstMonadContT
{r : Type u}
{m : Type u → Type v}
:
LawfulMonad (ContT r m)
Equations
- (_ : LawfulMonad (ContT r m)) = (_ : LawfulMonad (ContT r m))
theorem
ContT.monadLift_bind
{r : Type u}
{m : Type u → Type v}
[Monad m]
[LawfulMonad m]
{α : Type u}
{β : Type u}
(x : m α)
(f : α → m β)
:
ContT.monadLift (x >>= f) = ContT.monadLift x >>= ContT.monadLift ∘ f
instance
ContT.instLawfulMonadContContTInstMonadContTInstMonadContContT
{r : Type u}
{m : Type u → Type v}
:
LawfulMonadCont (ContT r m)
Equations
- (_ : LawfulMonadCont (ContT r m)) = (_ : LawfulMonadCont (ContT r m))
instance
ContT.instMonadExceptContT
{r : Type u}
{m : Type u → Type v}
(ε : Type u_1)
[MonadExcept ε m]
:
MonadExcept ε (ContT r m)
Equations
- One or more equations did not get rendered due to their size.
def
ExceptT.mkLabel
{m : Type u → Type v}
[Monad m]
{α : Type u}
{β : Type u}
{ε : Type u}
:
ContT.Label (Except ε α) m β → ContT.Label α (ExceptT ε m) β
Equations
- ExceptT.mkLabel x = match x with | { apply := f } => { apply := fun (a : α) => monadLift (f (Except.ok a)) }
Instances For
theorem
ExceptT.goto_mkLabel
{m : Type u → Type v}
[Monad m]
{α : Type u}
{β : Type u}
{ε : Type u}
(x : ContT.Label (Except ε α) m β)
(i : α)
:
ContT.goto (ExceptT.mkLabel x) i = ExceptT.mk (Except.ok <$> ContT.goto x (Except.ok i))
def
ExceptT.callCC
{m : Type u → Type v}
[Monad m]
{ε : Type u}
[MonadCont m]
{α : Type u}
{β : Type u}
(f : ContT.Label α (ExceptT ε m) β → ExceptT ε m α)
:
ExceptT ε m α
Equations
- ExceptT.callCC f = ExceptT.mk (MonadCont.callCC fun (x : ContT.Label (Except ε α) m β) => ExceptT.run (f (ExceptT.mkLabel x)))
Instances For
instance
instLawfulMonadContExceptTInstMonadExceptTInstMonadContExceptT
{m : Type u → Type v}
[Monad m]
{ε : Type u}
[MonadCont m]
[LawfulMonadCont m]
:
LawfulMonadCont (ExceptT ε m)
Equations
- (_ : LawfulMonadCont (ExceptT ε m)) = (_ : LawfulMonadCont (ExceptT ε m))
def
OptionT.mkLabel
{m : Type u → Type v}
[Monad m]
{α : Type u}
{β : Type u}
:
ContT.Label (Option α) m β → ContT.Label α (OptionT m) β
Equations
- OptionT.mkLabel x = match x with | { apply := f } => { apply := fun (a : α) => monadLift (f (some a)) }
Instances For
theorem
OptionT.goto_mkLabel
{m : Type u → Type v}
[Monad m]
{α : Type u}
{β : Type u}
(x : ContT.Label (Option α) m β)
(i : α)
:
ContT.goto (OptionT.mkLabel x) i = OptionT.mk do
let a ← ContT.goto x (some i)
pure (some a)
def
OptionT.callCC
{m : Type u → Type v}
[Monad m]
[MonadCont m]
{α : Type u}
{β : Type u}
(f : ContT.Label α (OptionT m) β → OptionT m α)
:
OptionT m α
Equations
- OptionT.callCC f = OptionT.mk (MonadCont.callCC fun (x : ContT.Label (Option α) m β) => OptionT.run (f (OptionT.mkLabel x)))
Instances For
instance
instLawfulMonadContOptionTInstMonadOptionTInstMonadContOptionT
{m : Type u → Type v}
[Monad m]
[MonadCont m]
[LawfulMonadCont m]
:
Equations
- (_ : LawfulMonadCont (OptionT m)) = (_ : LawfulMonadCont (OptionT m))
def
WriterT.mkLabel
{m : Type u → Type v}
[Monad m]
{α : Type u_1}
{β : Type u}
{ω : Type u}
[EmptyCollection ω]
:
ContT.Label (α × ω) m β → ContT.Label α (WriterT ω m) β
Equations
- WriterT.mkLabel x = match x with | { apply := f } => { apply := fun (a : α) => monadLift (f (a, ∅)) }
Instances For
def
WriterT.mkLabel'
{m : Type u → Type v}
[Monad m]
{α : Type u_1}
{β : Type u}
{ω : Type u}
[Monoid ω]
:
ContT.Label (α × ω) m β → ContT.Label α (WriterT ω m) β
Equations
- WriterT.mkLabel' x = match x with | { apply := f } => { apply := fun (a : α) => monadLift (f (a, 1)) }
Instances For
theorem
WriterT.goto_mkLabel
{m : Type u → Type v}
[Monad m]
{α : Type u_1}
{β : Type u}
{ω : Type u}
[EmptyCollection ω]
(x : ContT.Label (α × ω) m β)
(i : α)
:
ContT.goto (WriterT.mkLabel x) i = monadLift (ContT.goto x (i, ∅))
theorem
WriterT.goto_mkLabel'
{m : Type u → Type v}
[Monad m]
{α : Type u_1}
{β : Type u}
{ω : Type u}
[Monoid ω]
(x : ContT.Label (α × ω) m β)
(i : α)
:
ContT.goto (WriterT.mkLabel' x) i = monadLift (ContT.goto x (i, 1))
def
WriterT.callCC
{m : Type u → Type v}
[Monad m]
[MonadCont m]
{α : Type u}
{β : Type u}
{ω : Type u}
[EmptyCollection ω]
(f : ContT.Label α (WriterT ω m) β → WriterT ω m α)
:
WriterT ω m α
Equations
- WriterT.callCC f = WriterT.mk (MonadCont.callCC (WriterT.run ∘ f ∘ WriterT.mkLabel))
Instances For
def
WriterT.callCC'
{m : Type u → Type v}
[Monad m]
[MonadCont m]
{α : Type u}
{β : Type u}
{ω : Type u}
[Monoid ω]
(f : ContT.Label α (WriterT ω m) β → WriterT ω m α)
:
WriterT ω m α
Equations
- WriterT.callCC' f = WriterT.mk (MonadCont.callCC (WriterT.run ∘ f ∘ WriterT.mkLabel'))
Instances For
instance
instMonadContWriterT
{m : Type u → Type v}
(ω : Type u)
[Monad m]
[EmptyCollection ω]
[MonadCont m]
:
Equations
- instMonadContWriterT ω = { callCC := fun {α β : Type u} => WriterT.callCC }
def
StateT.mkLabel
{m : Type u → Type v}
{α : Type u}
{β : Type u}
{σ : Type u}
:
ContT.Label (α × σ) m (β × σ) → ContT.Label α (StateT σ m) β
Equations
- StateT.mkLabel x = match x with | { apply := f } => { apply := fun (a : α) => StateT.mk fun (s : σ) => f (a, s) }
Instances For
theorem
StateT.goto_mkLabel
{m : Type u → Type v}
{α : Type u}
{β : Type u}
{σ : Type u}
(x : ContT.Label (α × σ) m (β × σ))
(i : α)
:
ContT.goto (StateT.mkLabel x) i = StateT.mk fun (s : σ) => ContT.goto x (i, s)
def
StateT.callCC
{m : Type u → Type v}
{σ : Type u}
[MonadCont m]
{α : Type u}
{β : Type u}
(f : ContT.Label α (StateT σ m) β → StateT σ m α)
:
StateT σ m α
Equations
- StateT.callCC f = StateT.mk fun (r : σ) => MonadCont.callCC fun (f' : ContT.Label (α × σ) m (β × σ)) => StateT.run (f (StateT.mkLabel f')) r
Instances For
instance
instLawfulMonadContStateTInstMonadStateTInstMonadContStateT
{m : Type u → Type v}
[Monad m]
{σ : Type u}
[MonadCont m]
[LawfulMonadCont m]
:
LawfulMonadCont (StateT σ m)
Equations
- (_ : LawfulMonadCont (StateT σ m)) = (_ : LawfulMonadCont (StateT σ m))
def
ReaderT.mkLabel
{m : Type u → Type v}
{α : Type u_1}
{β : Type u}
(ρ : Type u)
:
ContT.Label α m β → ContT.Label α (ReaderT ρ m) β
Equations
- ReaderT.mkLabel ρ x = match x with | { apply := f } => { apply := monadLift ∘ f }
Instances For
theorem
ReaderT.goto_mkLabel
{m : Type u → Type v}
{α : Type u_1}
{ρ : Type u}
{β : Type u}
(x : ContT.Label α m β)
(i : α)
:
ContT.goto (ReaderT.mkLabel ρ x) i = monadLift (ContT.goto x i)
def
ReaderT.callCC
{m : Type u → Type v}
{ε : Type u}
[MonadCont m]
{α : Type u}
{β : Type u}
(f : ContT.Label α (ReaderT ε m) β → ReaderT ε m α)
:
ReaderT ε m α
Equations
- ReaderT.callCC f = ReaderT.mk fun (r : ε) => MonadCont.callCC fun (f' : ContT.Label α m β) => ReaderT.run (f (ReaderT.mkLabel ε f')) r
Instances For
instance
instLawfulMonadContReaderTInstMonadReaderTInstMonadContReaderT
{m : Type u → Type v}
[Monad m]
{ρ : Type u}
[MonadCont m]
[LawfulMonadCont m]
:
LawfulMonadCont (ReaderT ρ m)
Equations
- (_ : LawfulMonadCont (ReaderT ρ m)) = (_ : LawfulMonadCont (ReaderT ρ m))
def
ContT.equiv
{m₁ : Type u₀ → Type v₀}
{m₂ : Type u₁ → Type v₁}
{α₁ : Type u₀}
{r₁ : Type u₀}
{α₂ : Type u₁}
{r₂ : Type u₁}
(F : m₁ r₁ ≃ m₂ r₂)
(G : α₁ ≃ α₂)
:
reduce the equivalence between two continuation passing monads to the equivalence between their underlying monad
Equations
- One or more equations did not get rendered due to their size.