Debian Codegraph @

src/Effect/Monad.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Monads
5 ------------------------------------------------------------------------
6
7 -- Note that currently the monad laws are not included here.
8
9 {-# OPTIONS --cubical-compatible --safe #-}
10
11 module Effect.Monad where
12
13 open import Data.Bool.Base using (Bool; true; false; not)
14 open import Data.Unit.Polymorphic.Base using (⊤)
15 open import Effect.Choice using (RawChoice)
16 open import Effect.Empty using (RawEmpty)
17 open import Effect.Applicative as App
18 using (RawApplicative; RawApplicativeZero; mkRawApplicative; RawAlternative)
19 open import Function.Base using (id; flip; _$′_; _∘′_)
20 open import Level using (Level; suc; _⊔_)
21
22 private
23 variable
24 f g g₁ g₂ : Level
25 A B C : Set f
26
27 ------------------------------------------------------------------------
28 -- The type of raw monads
29
30 record RawMonad (F : Set f → Set g) : Set (suc f ⊔ g) where
31 infixl 1 _>>=_ _>>_ _>=>_
32 infixr 1 _=<<_ _<=<_
33 field
34 rawApplicative : RawApplicative F
35 _>>=_ : F A → (A → F B) → F B
36
37 open RawApplicative rawApplicative public
38
39 _>>_ : F A → F B → F B
40 _>>_ = _*>_
41
42 _=<<_ : (A → F B) → F A → F B
43 _=<<_ = flip _>>=_
44
45 Kleisli : Set f → Set f → Set (f ⊔ g)
46 Kleisli A B = A → F B
47
48 _>=>_ : Kleisli A B → Kleisli B C → Kleisli A C
49 (f >=> g) a = f a >>= g
50
51 _<=<_ : Kleisli B C → Kleisli A B → Kleisli A C
52 _<=<_ = flip _>=>_
53
54 when : Bool → F ⊤ → F ⊤
55 when true m = m
56 when false m = pure _
57
58 unless : Bool → F ⊤ → F ⊤
59 unless = when ∘′ not
60
61 -- When level g=f, a join/μ operator is definable
62
63 module Join {F : Set f → Set f} (M : RawMonad F) where
64 open RawMonad M
65
66 join : F (F A) → F A
67 join = _>>= id
68
69 -- Smart constructor
70
71 module _ where
72
73 open RawMonad
74 open RawApplicative
75
76 mkRawMonad :
77 (F : Set f → Set g) →
78 (pure : ∀ {A} → A → F A) →
79 (bind : ∀ {A B} → F A → (A → F B) → F B) →
80 RawMonad F
81 mkRawMonad F pure _>>=_ .rawApplicative =
82 mkRawApplicative _ pure $′ λ mf mx → do
83 f ← mf
84 x ← mx
85 pure (f x)
86 mkRawMonad F pure _>>=_ ._>>=_ = _>>=_
87
88 ------------------------------------------------------------------------
89 -- The type of raw monads with a zero
90
91 record RawMonadZero (F : Set f → Set g) : Set (suc f ⊔ g) where
92 field
93 rawMonad : RawMonad F
94 rawEmpty : RawEmpty F
95
96 open RawMonad rawMonad public
97 open RawEmpty rawEmpty public
98
99 rawApplicativeZero : RawApplicativeZero F
100 rawApplicativeZero = record
101 { rawApplicative = rawApplicative
102 ; rawEmpty = rawEmpty
103 }
104
105 ------------------------------------------------------------------------
106 -- The type of raw monadplus
107
108 record RawMonadPlus (F : Set f → Set g) : Set (suc f ⊔ g) where
109 field
110 rawMonadZero : RawMonadZero F
111 rawChoice : RawChoice F
112
113 open RawMonadZero rawMonadZero public
114 open RawChoice rawChoice public
115
116 rawAlternative : RawAlternative F
117 rawAlternative = record
118 { rawApplicativeZero = rawApplicativeZero
119 ; rawChoice = rawChoice
120 }
121
122 ------------------------------------------------------------------------
123 -- The type of raw monad transformer
124
125 -- F has been RawMonadT'd as TF
126 record RawMonadTd (F : Set f → Set g₁) (TF : Set f → Set g₂) : Set (suc f ⊔ g₁ ⊔ g₂) where
127 field
128 lift : F A → TF A
129 rawMonad : RawMonad TF
130
131 open RawMonad rawMonad public
132
133 RawMonadT : (T : (Set f → Set g₁) → (Set f → Set g₂)) → Set (suc f ⊔ suc g₁ ⊔ g₂)
134 RawMonadT T = ∀ {M} → RawMonad M → RawMonadTd M (T M)