| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- A delimited continuation monad |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Effect.Monad.Continuation where |
| 10 |
|
| 11 |
open import Effect.Applicative.Indexed using (IFun) |
| 12 |
open import Effect.Monad using (RawMonad) |
| 13 |
open import Function.Identity.Effectful as Id using (Identity) |
| 14 |
open import Effect.Monad.Indexed using (RawIMonad) |
| 15 |
open import Function.Base using (flip) |
| 16 |
open import Level using (Level; _⊔_; suc) |
| 17 |
|
| 18 |
private |
| 19 |
variable |
| 20 |
i f : Level |
| 21 |
I : Set i |
| 22 |
|
| 23 |
------------------------------------------------------------------------ |
| 24 |
-- Delimited continuation monads |
| 25 |
|
| 26 |
DContT : (I → Set f) → (Set f → Set f) → IFun I f |
| 27 |
DContT K M r₂ r₁ a = (a → M (K r₁)) → M (K r₂) |
| 28 |
|
| 29 |
DCont : (I → Set f) → IFun I f |
| 30 |
DCont K = DContT K Identity |
| 31 |
|
| 32 |
DContTIMonad : ∀ (K : I → Set f) {M} → RawMonad M → RawIMonad (DContT K M) |
| 33 |
DContTIMonad K Mon = record |
| 34 |
{ return = λ a k → k a |
| 35 |
; _>>=_ = λ c f k → c (flip f k) |
| 36 |
} |
| 37 |
where open RawMonad Mon |
| 38 |
|
| 39 |
DContIMonad : (K : I → Set f) → RawIMonad (DCont K) |
| 40 |
DContIMonad K = DContTIMonad K Id.monad |
| 41 |
|
| 42 |
------------------------------------------------------------------------ |
| 43 |
-- Delimited continuation operations |
| 44 |
|
| 45 |
record RawIMonadDCont {I : Set i} (K : I → Set f) |
| 46 |
(M : IFun I f) : Set (i ⊔ suc f) where |
| 47 |
field |
| 48 |
monad : RawIMonad M |
| 49 |
reset : ∀ {r₁ r₂ r₃} → M r₁ r₂ (K r₂) → M r₃ r₃ (K r₁) |
| 50 |
shift : ∀ {a r₁ r₂ r₃ r₄} → |
| 51 |
((a → M r₁ r₁ (K r₂)) → M r₃ r₄ (K r₄)) → M r₃ r₂ a |
| 52 |
|
| 53 |
open RawIMonad monad public |
| 54 |
|
| 55 |
DContTIMonadDCont : ∀ (K : I → Set f) {M} → |
| 56 |
RawMonad M → RawIMonadDCont K (DContT K M) |
| 57 |
DContTIMonadDCont K Mon = record |
| 58 |
{ monad = DContTIMonad K Mon |
| 59 |
; reset = λ e k → e pure >>= k |
| 60 |
; shift = λ e k → e (λ a k′ → (k a) >>= k′) pure |
| 61 |
} |
| 62 |
where |
| 63 |
open RawMonad Mon |
| 64 |
|
| 65 |
DContIMonadDCont : (K : I → Set f) → RawIMonadDCont K (DCont K) |
| 66 |
DContIMonadDCont K = DContTIMonadDCont K Id.monad |