Debian Codegraph @

src/Data/List/NonEmpty/Effectful/Transformer.agda

raw
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.NonEmpty.Effectful.Transformer where
10
11 open import Data.List.NonEmpty.Base as List⁺ using (List⁺; _∷_)
12 open import Effect.Functor using (RawFunctor)
13 open import Effect.Applicative using (RawApplicative)
14 open import Effect.Monad using (RawMonad; RawMonadT)
15 open import Function.Base using (_∘′_; _∘_; _$_)
16 open import Level using (Level)
17
18 import Data.List.NonEmpty.Effectful as List⁺ using (module TraversableM)
19
20 private
21 variable
22 f g : Level
23 M : Set f → Set g
24
25 ------------------------------------------------------------------------
26 -- List⁺ monad transformer
27
28 record List⁺T (M : Set f → Set g) (A : Set f) : Set g where
29 constructor mkList⁺T
30 field runList⁺T : M (List⁺ A)
31 open List⁺T public
32
33 functor : RawFunctor M → RawFunctor {f} (List⁺T M)
34 functor M = record
35 { _<$>_ = λ f → mkList⁺T ∘′ (List⁺.map f <$>_) ∘′ runList⁺T
36 } where open RawFunctor M
37
38 applicative : RawApplicative M → RawApplicative {f} (List⁺T M)
39 applicative M = record
40 { rawFunctor = functor rawFunctor
41 ; pure = mkList⁺T ∘′ pure ∘′ List⁺.[_]
42 ; _<*>_ = λ mf ma → mkList⁺T (List⁺.ap <$> runList⁺T mf <*> runList⁺T ma)
43 } where open RawApplicative M
44
45 monad : RawMonad M → RawMonad (List⁺T M)
46 monad M = record
47 { rawApplicative = applicative rawApplicative
48 ; _>>=_ = λ mas f → mkList⁺T $ do
49 as ← runList⁺T mas
50 List⁺.concat <$> mapM (runList⁺T ∘′ f) as
51 } where open RawMonad M; open List⁺.TraversableM M
52
53 monadT : RawMonadT {f} {g} List⁺T
54 monadT M = record
55 { lift = mkList⁺T ∘′ (List⁺.[_] <$>_)
56 ; rawMonad = monad M
57 } where open RawMonad M