| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- An effectful view of the Sum type (Left-biased) |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
open import Level |
| 10 |
|
| 11 |
module Data.Sum.Effectful.Left.Transformer {a} (A : Set a) (b : Level) where |
| 12 |
|
| 13 |
open import Data.Sum.Base using (map₂ ; inj₁ ; inj₂; _⊎_; [_,_]′) |
| 14 |
open import Effect.Choice using (RawChoice) |
| 15 |
open import Effect.Empty using (RawEmpty) |
| 16 |
open import Effect.Functor using (RawFunctor) |
| 17 |
open import Effect.Applicative |
| 18 |
using (RawApplicative; RawApplicativeZero; RawAlternative) |
| 19 |
open import Effect.Monad using (RawMonad; RawMonadT) |
| 20 |
open import Function.Base using (const; flip; _∘_; _∘′_; _$_) |
| 21 |
|
| 22 |
private |
| 23 |
variable |
| 24 |
M : Set (a ⊔ b) → Set (a ⊔ b) |
| 25 |
|
| 26 |
-- To minimize the universe level of the RawFunctor, we require that |
| 27 |
-- elements of B are "lifted" to a copy of B at a higher universe level |
| 28 |
-- (a ⊔ b). See the examples for how this is done. |
| 29 |
|
| 30 |
open import Data.Sum.Effectful.Left A b using (Sumₗ) |
| 31 |
|
| 32 |
------------------------------------------------------------------------ |
| 33 |
-- Left-biased monad transformer instance for _⊎_ |
| 34 |
|
| 35 |
record SumₗT |
| 36 |
(M : Set (a ⊔ b) → Set (a ⊔ b)) |
| 37 |
(B : Set (a ⊔ b)) : Set (a ⊔ b) where |
| 38 |
constructor mkSumₗT |
| 39 |
field runSumₗT : M (Sumₗ B) |
| 40 |
open SumₗT public |
| 41 |
|
| 42 |
------------------------------------------------------------------------ |
| 43 |
-- Structure |
| 44 |
|
| 45 |
functor : RawFunctor M → RawFunctor (SumₗT M) |
| 46 |
functor M = record |
| 47 |
{ _<$>_ = λ f → mkSumₗT ∘′ (map₂ f <$>_) ∘′ runSumₗT |
| 48 |
} where open RawFunctor M |
| 49 |
|
| 50 |
applicative : RawApplicative M → RawApplicative (SumₗT M) |
| 51 |
applicative M = record |
| 52 |
{ rawFunctor = functor rawFunctor |
| 53 |
; pure = mkSumₗT ∘′ pure ∘′ inj₂ |
| 54 |
; _<*>_ = λ mf mx → mkSumₗT ([ const ∘′ inj₁ , map₂ ]′ <$> runSumₗT mf <*> runSumₗT mx) |
| 55 |
} where open RawApplicative M |
| 56 |
|
| 57 |
empty : RawApplicative M → A → RawEmpty (SumₗT M) |
| 58 |
empty M a = record |
| 59 |
{ empty = mkSumₗT (pure (inj₁ a)) |
| 60 |
} where open RawApplicative M |
| 61 |
|
| 62 |
choice : RawApplicative M → RawChoice (SumₗT M) |
| 63 |
choice M = record |
| 64 |
{ _<|>_ = λ ma₁ ma₂ → mkSumₗT ([ flip const , const ∘ inj₂ ]′ <$> runSumₗT ma₁ <*> runSumₗT ma₂) |
| 65 |
} where open RawApplicative M |
| 66 |
|
| 67 |
applicativeZero : RawApplicative M → A → RawApplicativeZero (SumₗT M) |
| 68 |
applicativeZero M a = record |
| 69 |
{ rawApplicative = applicative M |
| 70 |
; rawEmpty = empty M a |
| 71 |
} |
| 72 |
|
| 73 |
alternative : RawApplicative M → A → RawAlternative (SumₗT M) |
| 74 |
alternative M a = record |
| 75 |
{ rawApplicativeZero = applicativeZero M a |
| 76 |
; rawChoice = choice M |
| 77 |
} |
| 78 |
|
| 79 |
monad : RawMonad M → RawMonad (SumₗT M) |
| 80 |
monad M = record |
| 81 |
{ rawApplicative = applicative rawApplicative |
| 82 |
; _>>=_ = λ ma f → mkSumₗT $ do |
| 83 |
a ← runSumₗT ma |
| 84 |
[ pure ∘′ inj₁ , runSumₗT ∘′ f ]′ a |
| 85 |
} where open RawMonad M |
| 86 |
|
| 87 |
monadT : RawMonadT SumₗT |
| 88 |
monadT M = record |
| 89 |
{ lift = mkSumₗT ∘′ (inj₂ <$>_) |
| 90 |
; rawMonad = monad M |
| 91 |
} where open RawMonad M |