| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Left-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.Left |
| 19 |
{a e} (A : RawMonoid a e) (b : Level) where |
| 20 |
|
| 21 |
open import Data.Product.Base using (_,_; map₁; map₂; zip; uncurry) |
| 22 |
import Data.Product.Effectful.Left.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 A |
| 28 |
|
| 29 |
------------------------------------------------------------------------ |
| 30 |
-- Re-export the base contents publically |
| 31 |
|
| 32 |
open Base Carrier b 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 λ w₁ a f → map₁ (w₁ ∙_) (f a) |
| 48 |
} |
| 49 |
|
| 50 |
-- The monad instance also requires some mucking about with universe levels. |
| 51 |
monadT : ∀ {ℓ} → RawMonadT {g₁ = ℓ} (_∘′ Productₗ) |
| 52 |
monadT M = record |
| 53 |
{ lift = (ε ,_) <$>_ |
| 54 |
; rawMonad = mkRawMonad _ |
| 55 |
(pure ∘′ (ε ,_)) |
| 56 |
(λ ma f → ma >>= uncurry λ a x → map₁ (a ∙_) <$> f x) |
| 57 |
} where open RawMonad M |
| 58 |
|
| 59 |
------------------------------------------------------------------------ |
| 60 |
-- Get access to other monadic functions |
| 61 |
|
| 62 |
module TraversableA {F} (App : RawApplicative {a ⊔ b} {a ⊔ b} F) where |
| 63 |
|
| 64 |
open RawApplicative App |
| 65 |
|
| 66 |
sequenceA : ∀ {A} → Productₗ (F A) → F (Productₗ A) |
| 67 |
sequenceA (x , fa) = (x ,_) <$> fa |
| 68 |
|
| 69 |
mapA : ∀ {A B} → (A → F B) → Productₗ A → F (Productₗ B) |
| 70 |
mapA f = sequenceA ∘ map₂ f |
| 71 |
|
| 72 |
forA : ∀ {A B} → Productₗ A → (A → F B) → F (Productₗ B) |
| 73 |
forA = flip mapA |
| 74 |
|
| 75 |
module TraversableM {M} (Mon : RawMonad {a ⊔ b} {a ⊔ b} M) where |
| 76 |
|
| 77 |
open RawMonad Mon |
| 78 |
|
| 79 |
open TraversableA rawApplicative public |
| 80 |
renaming |
| 81 |
( sequenceA to sequenceM |
| 82 |
; mapA to mapM |
| 83 |
; forA to forM |
| 84 |
) |