| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Usage examples of the effectful view of the Sum type |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Sum.Effectful.Examples where |
| 10 |
|
| 11 |
open import Level using (Level; _⊔_; lift; Lift) |
| 12 |
open import Data.Sum.Base using (_⊎_; inj₁; inj₂) |
| 13 |
import Data.Sum.Effectful.Left as Sumₗ using (Sumₗ; functor; monad) |
| 14 |
open import Effect.Functor using (RawFunctor) |
| 15 |
open import Effect.Monad using (RawMonad) |
| 16 |
|
| 17 |
-- Note that these examples are simple unit tests, because the type |
| 18 |
-- checker verifies them. |
| 19 |
|
| 20 |
private |
| 21 |
module Examplesₗ {a b} {A : Set a} {B : Set b} where |
| 22 |
|
| 23 |
open import Agda.Builtin.Equality |
| 24 |
open import Function.Base using (id) |
| 25 |
module Sₗ = Sumₗ A b |
| 26 |
|
| 27 |
open RawFunctor Sₗ.functor |
| 28 |
|
| 29 |
-- This type to the right of ⊎ needs to be a "lifted" version of |
| 30 |
-- (B : Set b) that lives in the universe (Set (a ⊔ b)). |
| 31 |
fmapId : (x : A ⊎ (Lift a B)) → (id <$> x) ≡ x |
| 32 |
fmapId (inj₁ x) = refl |
| 33 |
fmapId (inj₂ y) = refl |
| 34 |
|
| 35 |
|
| 36 |
open RawMonad Sₗ.monad |
| 37 |
|
| 38 |
-- Now, let's show that "pure" is a unit for >>=. We use Lift in |
| 39 |
-- exactly the same way as above. The data (x : B) then needs to be |
| 40 |
-- "lifted" to this new type (Lift B). |
| 41 |
pureUnitL : ∀ {x : B} {f : Lift a B → A ⊎ (Lift a B)} |
| 42 |
→ (pure (lift x) >>= f) ≡ f (lift x) |
| 43 |
pureUnitL = refl |
| 44 |
|
| 45 |
pureUnitR : (x : A ⊎ (Lift a B)) → (x >>= pure) ≡ x |
| 46 |
pureUnitR (inj₁ _) = refl |
| 47 |
pureUnitR (inj₂ _) = refl |
| 48 |
|
| 49 |
-- And another (limited version of a) monad law... |
| 50 |
bindCompose : ∀ {f g : Lift a B → A ⊎ (Lift a B)} |
| 51 |
→ (x : A ⊎ (Lift a B)) |
| 52 |
→ ((x >>= f) >>= g) ≡ (x >>= (λ y → (f y >>= g))) |
| 53 |
bindCompose (inj₁ x) = refl |
| 54 |
bindCompose (inj₂ y) = refl |