Debian Codegraph @

src/Data/Maybe/Effectful.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- An effectful view of Maybe
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.Maybe.Effectful where
10
11 open import Data.Maybe.Base using (Maybe; just; nothing; map; _>>=_; _<∣>_; maybe)
12 open import Effect.Applicative using (RawApplicative; RawApplicativeZero; RawAlternative)
13 open import Effect.Choice using (RawChoice)
14 open import Effect.Empty using (RawEmpty)
15 open import Effect.Functor using (RawFunctor)
16 open import Effect.Monad
17 open import Function.Base using (flip; const; _∘_)
18 open import Level using (Level; _⊔_)
19
20 private
21 variable
22 a b f g m n : Level
23 A : Set a
24 B : Set b
25
26 ------------------------------------------------------------------------
27 -- Maybe applicative functor
28
29 functor : RawFunctor {f} Maybe
30 functor = record
31 { _<$>_ = map
32 }
33
34 applicative : RawApplicative {f} Maybe
35 applicative = record
36 { rawFunctor = functor
37 ; pure = just
38 ; _<*>_ = maybe map (const nothing)
39 }
40
41 empty : RawEmpty {f} Maybe
42 empty = record { empty = nothing }
43
44 choice : RawChoice {f} Maybe
45 choice = record { _<|>_ = _<∣>_ }
46
47 applicativeZero : RawApplicativeZero {f} Maybe
48 applicativeZero = record
49 { rawApplicative = applicative
50 ; rawEmpty = empty
51 }
52
53 alternative : RawAlternative {f} Maybe
54 alternative = record
55 { rawApplicativeZero = applicativeZero
56 ; rawChoice = choice
57 }
58
59 ------------------------------------------------------------------------
60 -- Maybe monad
61
62 monad : RawMonad {f} Maybe
63 monad = record
64 { rawApplicative = applicative
65 ; _>>=_ = _>>=_
66 }
67
68 join : Maybe (Maybe A) → Maybe A
69 join = Join.join monad
70
71 monadZero : RawMonadZero {f} Maybe
72 monadZero = record
73 { rawMonad = monad
74 ; rawEmpty = empty
75 }
76
77 monadPlus : RawMonadPlus {f} Maybe
78 monadPlus {f} = record
79 { rawMonadZero = monadZero
80 ; rawChoice = choice
81 }
82
83 module TraversableA {F} (App : RawApplicative {f} {g} F) where
84
85 open RawApplicative App
86
87 sequenceA : Maybe (F A) → F (Maybe A)
88 sequenceA nothing = pure nothing
89 sequenceA (just x) = just <$> x
90
91 mapA : (A → F B) → Maybe A → F (Maybe B)
92 mapA f = sequenceA ∘ map f
93
94 forA : Maybe A → (A → F B) → F (Maybe B)
95 forA = flip mapA
96
97 module TraversableM {M} (Mon : RawMonad {m} {n} M) where
98
99 open RawMonad Mon
100
101 open TraversableA rawApplicative public
102 renaming
103 ( sequenceA to sequenceM
104 ; mapA to mapM
105 ; forA to forM
106 )