| 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.Transformer where |
| 10 |
|
| 11 |
open import Data.Maybe.Base as Maybe using (Maybe; just; nothing; maybe) |
| 12 |
open import Effect.Choice using (RawChoice) |
| 13 |
open import Effect.Empty using (RawEmpty) |
| 14 |
open import Effect.Functor using (RawFunctor) |
| 15 |
open import Effect.Applicative using (RawApplicative) |
| 16 |
open import Effect.Monad using (RawMonad; RawMonadT) |
| 17 |
open import Function.Base using (_∘′_; _$_) |
| 18 |
open import Level using (Level; _⊔_) |
| 19 |
|
| 20 |
|
| 21 |
private |
| 22 |
variable |
| 23 |
f g : Level |
| 24 |
M : Set f → Set g |
| 25 |
|
| 26 |
------------------------------------------------------------------------ |
| 27 |
-- Maybe monad transformer |
| 28 |
|
| 29 |
record MaybeT (M : Set f → Set g) (A : Set f) : Set g where |
| 30 |
constructor mkMaybeT |
| 31 |
field runMaybeT : M (Maybe A) |
| 32 |
open MaybeT public |
| 33 |
|
| 34 |
------------------------------------------------------------------------ |
| 35 |
-- Structure |
| 36 |
|
| 37 |
functor : RawFunctor M → RawFunctor {f} (MaybeT M) |
| 38 |
functor M = record |
| 39 |
{ _<$>_ = λ f → mkMaybeT ∘′ (Maybe.map f <$>_) ∘′ runMaybeT |
| 40 |
} where open RawFunctor M |
| 41 |
|
| 42 |
applicative : RawApplicative M → RawApplicative {f} (MaybeT M) |
| 43 |
applicative M = record |
| 44 |
{ rawFunctor = functor rawFunctor |
| 45 |
; pure = mkMaybeT ∘′ pure ∘′ just |
| 46 |
; _<*>_ = λ mf ma → mkMaybeT (Maybe.ap <$> runMaybeT mf <*> runMaybeT ma) |
| 47 |
} where open RawApplicative M |
| 48 |
|
| 49 |
monad : RawMonad M → RawMonad {f} (MaybeT M) |
| 50 |
monad M = record |
| 51 |
{ rawApplicative = applicative rawApplicative |
| 52 |
; _>>=_ = λ ma f → mkMaybeT $ do |
| 53 |
a ← runMaybeT ma |
| 54 |
maybe (runMaybeT ∘′ f) (pure nothing) a |
| 55 |
} where open RawMonad M |
| 56 |
|
| 57 |
monadT : RawMonadT {f} {g} MaybeT |
| 58 |
monadT {M = F} M = record |
| 59 |
{ lift = mkMaybeT ∘′ (just <$>_) |
| 60 |
; rawMonad = monad M |
| 61 |
} where open RawMonad M |