| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Right-biased universe-sensitive functor and monad instances for the |
| 5 |
-- Product type. |
| 6 |
-- |
| 7 |
-- To minimize the universe level of the RawFunctor, we require that |
| 8 |
-- elements of B are "lifted" to a copy of B at a higher universe level |
| 9 |
-- (a ⊔ b). See the Data.Product.Effectful.Examples for how this is |
| 10 |
-- done. |
| 11 |
------------------------------------------------------------------------ |
| 12 |
|
| 13 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 14 |
|
| 15 |
open import Algebra |
| 16 |
open import Level |
| 17 |
|
| 18 |
module Data.Product.Effectful.Right |
| 19 |
(a : Level) {b e} (B : RawMonoid b e) where |
| 20 |
|
| 21 |
open import Data.Product.Base using (_,_; map₁; map₂; zip; uncurry) |
| 22 |
import Data.Product.Effectful.Right.Base as Base using (Productᵣ; functor) |
| 23 |
open import Effect.Applicative using (RawApplicative) |
| 24 |
open import Effect.Monad using (RawMonad; RawMonadT; mkRawMonad) |
| 25 |
open import Function.Base using (id; flip; _∘_; _∘′_) |
| 26 |
|
| 27 |
open RawMonoid B |
| 28 |
|
| 29 |
------------------------------------------------------------------------ |
| 30 |
-- Re-export the base contents publically |
| 31 |
|
| 32 |
open Base Carrier a public |
| 33 |
|
| 34 |
------------------------------------------------------------------------ |
| 35 |
-- Basic records |
| 36 |
|
| 37 |
applicative : RawApplicative Productᵣ |
| 38 |
applicative = record |
| 39 |
{ rawFunctor = functor |
| 40 |
; pure = _, ε |
| 41 |
; _<*>_ = zip id _∙_ |
| 42 |
} |
| 43 |
|
| 44 |
monad : RawMonad Productᵣ |
| 45 |
monad = record |
| 46 |
{ rawApplicative = applicative |
| 47 |
; _>>=_ = uncurry λ a w₁ f → map₂ (w₁ ∙_) (f a) |
| 48 |
} |
| 49 |
|
| 50 |
monadT : ∀ {ℓ} → RawMonadT {g₁ = ℓ} (_∘′ Productᵣ) |
| 51 |
monadT M = record |
| 52 |
{ lift = (_, ε) <$>_ |
| 53 |
; rawMonad = mkRawMonad _ |
| 54 |
(pure ∘′ (_, ε)) |
| 55 |
(λ ma f → ma >>= uncurry λ x b → map₂ (b ∙_) <$> f x) |
| 56 |
} where open RawMonad M |
| 57 |
|
| 58 |
------------------------------------------------------------------------ |
| 59 |
-- Get access to other monadic functions |
| 60 |
|
| 61 |
module TraversableA {F} (App : RawApplicative {a ⊔ b} {a ⊔ b} F) where |
| 62 |
|
| 63 |
open RawApplicative App |
| 64 |
|
| 65 |
sequenceA : ∀ {A} → Productᵣ (F A) → F (Productᵣ A) |
| 66 |
sequenceA (fa , y) = (_, y) <$> fa |
| 67 |
|
| 68 |
mapA : ∀ {A B} → (A → F B) → Productᵣ A → F (Productᵣ B) |
| 69 |
mapA f = sequenceA ∘ map₁ f |
| 70 |
|
| 71 |
forA : ∀ {A B} → Productᵣ A → (A → F B) → F (Productᵣ B) |
| 72 |
forA = flip mapA |
| 73 |
|
| 74 |
module TraversableM {M} (Mon : RawMonad {a ⊔ b} {a ⊔ b} M) where |
| 75 |
|
| 76 |
open RawMonad Mon |
| 77 |
|
| 78 |
open TraversableA rawApplicative public |
| 79 |
renaming |
| 80 |
( sequenceA to sequenceM |
| 81 |
; mapA to mapM |
| 82 |
; forA to forM |
| 83 |
) |