| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- An effectful view of List |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.List.Effectful.Transformer where |
| 10 |
|
| 11 |
open import Data.List.Base as List using (List; []; _∷_) |
| 12 |
import Data.List.Effectful as List |
| 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 |
private |
| 20 |
variable |
| 21 |
f g : Level |
| 22 |
M : Set f → Set g |
| 23 |
|
| 24 |
------------------------------------------------------------------------ |
| 25 |
-- List monad transformer |
| 26 |
|
| 27 |
record ListT (M : Set f → Set g) (A : Set f) : Set g where |
| 28 |
constructor mkListT |
| 29 |
field runListT : M (List A) |
| 30 |
open ListT public |
| 31 |
|
| 32 |
functor : RawFunctor M → RawFunctor {f} (ListT M) |
| 33 |
functor M = record |
| 34 |
{ _<$>_ = λ f → mkListT ∘′ (List.map f <$>_) ∘′ runListT |
| 35 |
} where open RawFunctor M |
| 36 |
|
| 37 |
applicative : RawApplicative M → RawApplicative {f} (ListT M) |
| 38 |
applicative M = record |
| 39 |
{ rawFunctor = functor rawFunctor |
| 40 |
; pure = mkListT ∘′ pure ∘′ List.[_] |
| 41 |
; _<*>_ = λ mf ma → mkListT (List.ap <$> runListT mf <*> runListT ma) |
| 42 |
} where open RawApplicative M |
| 43 |
|
| 44 |
monad : RawMonad M → RawMonad (ListT M) |
| 45 |
monad M = record |
| 46 |
{ rawApplicative = applicative rawApplicative |
| 47 |
; _>>=_ = λ mas f → mkListT $ do |
| 48 |
as ← runListT mas |
| 49 |
List.concat <$> mapM (runListT ∘′ f) as |
| 50 |
} where open RawMonad M; open List.TraversableM M |
| 51 |
|
| 52 |
monadT : RawMonadT {f} {g} ListT |
| 53 |
monadT M = record |
| 54 |
{ lift = mkListT ∘′ (List.[_] <$>_) |
| 55 |
; rawMonad = monad M |
| 56 |
} where open RawMonad M |