@@ -3,10 +3,11 @@ module Class.Monad.Core where
33
44open import Class.Prelude
55open import Class.Core
6- open import Class.Functor
7- open import Class.Applicative
6+ open import Class.Functor.Core
7+ open import Class.Applicative.Core
88
9- record Monad (M : Type↑) : Typeω where
9+ record Monad (M : Type↑ ℓ↑) : Typeω where
10+ constructor mkMonad
1011 infixl 1 _>>=_ _>>_ _>=>_
1112 infixr 1 _=<<_ _<=<_
1213
@@ -32,9 +33,6 @@ record Monad (M : Type↑) : Typeω where
3233 join : M (M A) → M A
3334 join m = m >>= id
3435
35- Functor-M : Functor M
36- Functor-M = λ where ._<$>_ f x → return ∘ f =<< x
37-
3836open Monad ⦃...⦄ public
3937
4038module _ ⦃ _ : Monad M ⦄ where
@@ -67,7 +65,13 @@ module _ ⦃ _ : Monad M ⦄ where
6765 [] → return []
6866 (x ∷ xs) → ⦇ f x ∷ traverseM f xs ⦈
6967
70- record MonadLaws (M : Type↑) ⦃ _ : Monad M ⦄ : Typeω where
68+ whenM : Bool -> M ⊤ -> M ⊤
69+ whenM cond act =
70+ if cond
71+ then act >> return _
72+ else return _
73+
74+ record MonadLaws (M : Type↑ ℓ↑) ⦃ _ : Monad M ⦄ : Typeω where
7175 field
7276 >>=-identityˡ : ∀ {A : Type ℓ} {B : Type ℓ′} →
7377 ∀ {a : A} {h : A → M B} →
@@ -80,15 +84,31 @@ record MonadLaws (M : Type↑) ⦃ _ : Monad M ⦄ : Typeω where
8084 ((m >>= g) >>= h) ≡ (m >>= (λ x → g x >>= h))
8185open MonadLaws ⦃...⦄ public
8286
83- record Monad₀ (M : Type↑) : Typeω where
87+ module MkMonad
88+ (return : ∀ {ℓ} {A : Type ℓ} → A → M A)
89+ (_>>=_ : ∀ {ℓ ℓ′} {A : Type ℓ} {B : Type ℓ′} → M A → (A → M B) → M B)
90+ where instance
91+
92+ ⇒Functor : Functor M
93+ ⇒Functor ._<$>_ f mx = mx >>= (return ∘ f)
94+
95+ ⇒Applicative : Applicative M
96+ ⇒Applicative = λ where
97+ .pure → return
98+ ._<*>_ mf mx → mf >>= (_<$> mx)
99+
100+ ⇒Monad : Monad M
101+ ⇒Monad = mkMonad return _>>=_
102+
103+ record Monad₀ (M : Type↑ ℓ↑) : Typeω where
84104 field ⦃ isMonad ⦄ : Monad M
85105 ⦃ isApplicative₀ ⦄ : Applicative₀ M
86106open Monad₀ ⦃...⦄ using () public
87107instance
88108 mkMonad₀ : ⦃ Monad M ⦄ → ⦃ Applicative₀ M ⦄ → Monad₀ M
89109 mkMonad₀ = record {}
90110
91- record Monad⁺ (M : Type↑) : Typeω where
111+ record Monad⁺ (M : Type↑ ℓ↑ ) : Typeω where
92112 field ⦃ isMonad ⦄ : Monad M
93113 ⦃ isAlternative ⦄ : Alternative M
94114open Monad⁺ ⦃...⦄ using () public
0 commit comments