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