| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- An effectful view of Vec |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Vec.Effectful.Transformer where |
| 10 |
|
| 11 |
open import Data.Nat.Base using (ℕ) |
| 12 |
open import Data.Vec.Base as Vec using (Vec; []; _∷_) |
| 13 |
open import Effect.Functor using (RawFunctor) |
| 14 |
open import Effect.Applicative using (RawApplicative) |
| 15 |
open import Effect.Monad using (RawMonad; RawMonadT) |
| 16 |
open import Function.Base using (_∘′_; _$_ ) |
| 17 |
open import Level using (Level) |
| 18 |
|
| 19 |
import Data.Vec.Effectful as Vec |
| 20 |
|
| 21 |
private |
| 22 |
variable |
| 23 |
f g : Level |
| 24 |
n : ℕ |
| 25 |
M : Set f → Set g |
| 26 |
|
| 27 |
------------------------------------------------------------------------ |
| 28 |
-- Vec monad transformer |
| 29 |
|
| 30 |
record VecT (n : ℕ) (M : Set f → Set g) (A : Set f) : Set g where |
| 31 |
constructor mkVecT |
| 32 |
field runVecT : M (Vec A n) |
| 33 |
open VecT public |
| 34 |
|
| 35 |
functor : RawFunctor M → RawFunctor {f} (VecT n M) |
| 36 |
functor M = record |
| 37 |
{ _<$>_ = λ f → mkVecT ∘′ (Vec.map f <$>_) ∘′ runVecT |
| 38 |
} where open RawFunctor M |
| 39 |
|
| 40 |
applicative : RawApplicative M → RawApplicative {f} (VecT n M) |
| 41 |
applicative M = record |
| 42 |
{ rawFunctor = functor rawFunctor |
| 43 |
; pure = mkVecT ∘′ pure ∘′ Vec.replicate _ |
| 44 |
; _<*>_ = λ mf ma → mkVecT (Vec.zipWith _$_ <$> runVecT mf <*> runVecT ma) |
| 45 |
} where open RawApplicative M |
| 46 |
|
| 47 |
monad : {M : Set f → Set g} → RawMonad M → RawMonad (VecT n M) |
| 48 |
monad {f} {g} M = record |
| 49 |
{ rawApplicative = applicative rawApplicative |
| 50 |
; _>>=_ = λ ma k → mkVecT $ do |
| 51 |
a ← runVecT ma |
| 52 |
bs ← mapM {a = f} (runVecT ∘′ k) a |
| 53 |
pure (Vec.diagonal bs) |
| 54 |
} where open RawMonad M; open Vec.TraversableM {m = f} {n = g} M |
| 55 |
|
| 56 |
monadT : RawMonadT {f} {g} (VecT n) |
| 57 |
monadT M = record |
| 58 |
{ lift = mkVecT ∘′ (Vec.replicate _ <$>_) |
| 59 |
; rawMonad = monad M |
| 60 |
} where open RawMonad M |