Continuation Monad #
Monad encapsulating continuation passing programming style, similar to
Haskell's Cont
, ContT
and MonadCont
:
http://hackage.haskell.org/package/mtl-2.2.2/docs/Control-Monad-Cont.html
def
MonadCont.goto
{α : Type u_1}
{β : Type u}
{m : Type u → Type v}
(f : ContT.Label α m β)
(x : α)
:
m β
Equations
- ContT.goto f x = MonadCont.Label.apply f 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 => y) = Seq.seq (Function.const β <$> x) fun x => y
- seqRight_eq : ∀ {α β : Type u} (x : m α) (y : m β), (SeqRight.seqRight x fun x => y) = Seq.seq (Function.const α id <$> x) fun x => y
- callCC_bind_right : ∀ {α ω γ : Type u} (cmd : m α) (next : ContT.Label ω m γ → α → m ω), (MonadCont.callCC fun f => cmd >>= next f) = do let x ← cmd MonadCont.callCC fun f => next f x
- callCC_bind_left : ∀ {α : Type u} (β : Type u) (x : α) (dead : ContT.Label α m β → β → m α), (MonadCont.callCC fun f => ContT.goto f x >>= dead f) = pure x
- callCC_dummy : ∀ {α β : Type u} (dummy : m α), (MonadCont.callCC fun x => dummy) = dummy
Instances
instance
ContT.instLawfulMonadContTInstMonadContT
{r : Type u}
{m : Type u → Type v}
:
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.instMonadExceptContT
{r : Type u}
{m : Type u → Type v}
(ε : Type u_1)
[MonadExcept ε m]
:
MonadExcept ε (ContT r m)
Equations
- ContT.instMonadExceptContT ε = { throw := fun {α} e x => throw e, tryCatch := fun {α} act h f => tryCatch (act f) fun e => h e f }
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 => 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)
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 => OptionT.run (f (OptionT.mkLabel x)))
Instances For
instance
instLawfulMonadContOptionTInstMonadOptionTInstMonadContOptionT
{m : Type u → Type v}
[Monad m]
[MonadCont m]
[LawfulMonadCont 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 {α β} => 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' => 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)
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' => 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)
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.