Debian Codegraph @

src/Data/List/NonEmpty/Effectful.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 where
10
11 open import Data.List.Base using (List; []; _∷_)
12 import Data.List.Effectful as List
13 open import Data.List.NonEmpty.Base
14 open import Data.Product.Base using (uncurry)
15 open import Effect.Functor using (RawFunctor)
16 open import Effect.Applicative using (RawApplicative)
17 open import Effect.Monad using (RawMonad)
18 open import Effect.Comonad using (RawComonad)
19 open import Function.Base using (flip; _∘′_; _∘_)
20
21 ------------------------------------------------------------------------
22 -- List⁺ applicative functor
23
24 functor : ∀ {f} → RawFunctor {f} List⁺
25 functor = record
26 { _<$>_ = map
27 }
28
29 applicative : ∀ {f} → RawApplicative {f} List⁺
30 applicative = record
31 { rawFunctor = functor
32 ; pure = [_]
33 ; _<*>_ = ap
34 }
35
36 ------------------------------------------------------------------------
37 -- List⁺ monad
38
39 monad : ∀ {f} → RawMonad {f} List⁺
40 monad = record
41 { rawApplicative = applicative
42 ; _>>=_ = flip concatMap
43 }
44
45 ------------------------------------------------------------------------
46 -- List⁺ comonad
47
48 comonad : ∀ {f} → RawComonad {f} List⁺
49 comonad = record
50 { extract = head
51 ; extend = λ f → uncurry (extend f) ∘′ uncons
52 } where
53
54 extend : ∀ {A B} → (List⁺ A → B) → A → List A → List⁺ B
55 extend f x xs@[] = f (x ∷ xs) ∷ []
56 extend f x xs@(y ∷ ys) = f (x ∷ xs) ∷⁺ extend f y ys
57
58
59 ------------------------------------------------------------------------
60 -- Get access to other monadic functions
61
62 module TraversableA {f g F} (App : RawApplicative {f} {g} F) where
63
64 open RawApplicative App
65
66 sequenceA : ∀ {A} → List⁺ (F A) → F (List⁺ A)
67 sequenceA (x ∷ xs) = _∷_ <$> x ⊛ List.TraversableA.sequenceA App xs
68
69 mapA : ∀ {a} {A : Set a} {B} → (A → F B) → List⁺ A → F (List⁺ B)
70 mapA f = sequenceA ∘ map f
71
72 forA : ∀ {a} {A : Set a} {B} → List⁺ A → (A → F B) → F (List⁺ B)
73 forA = flip mapA
74
75 module TraversableM {m n M} (Mon : RawMonad {m} {n} 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 )