| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- The error monad transformer |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
open import Level using (Level; _⊔_; suc) |
| 10 |
|
| 11 |
module Effect.Monad.Error.Transformer {e} (E : Set e) (a : Level) where |
| 12 |
|
| 13 |
open import Effect.Monad using (RawMonad) |
| 14 |
open import Function.Base using (_∘′_; _$_) |
| 15 |
|
| 16 |
private |
| 17 |
variable |
| 18 |
f ℓ : Level |
| 19 |
A B : Set ℓ |
| 20 |
M : Set f → Set ℓ |
| 21 |
|
| 22 |
------------------------------------------------------------------------ |
| 23 |
-- Error monad operations |
| 24 |
|
| 25 |
record RawMonadError |
| 26 |
(M : Set (e ⊔ a) → Set ℓ) |
| 27 |
: Set (suc (e ⊔ a) ⊔ ℓ) where |
| 28 |
field |
| 29 |
throw : E → M A |
| 30 |
catch : M A → (E → M A) → M A |
| 31 |
|
| 32 |
during : (E → E) → M A → M A |
| 33 |
during f ma = catch ma (throw ∘′ f) |
| 34 |
|
| 35 |
------------------------------------------------------------------------ |
| 36 |
-- Monad error transformer specifics |
| 37 |
|
| 38 |
module Sumₗ where |
| 39 |
|
| 40 |
open import Data.Sum.Base using (inj₁; inj₂; [_,_]′) |
| 41 |
open import Data.Sum.Effectful.Left.Transformer E a |
| 42 |
|
| 43 |
monadError : RawMonad M → RawMonadError (SumₗT M) |
| 44 |
monadError M = record |
| 45 |
{ throw = mkSumₗT ∘′ pure ∘′ inj₁ |
| 46 |
; catch = λ ma k → mkSumₗT $ do |
| 47 |
a ← runSumₗT ma |
| 48 |
[ runSumₗT ∘′ k , pure ∘′ inj₂ ]′ a |
| 49 |
} where open RawMonad M |
| 50 |
|
| 51 |
module Sumᵣ where |
| 52 |
|
| 53 |
open import Data.Sum.Base using (inj₁; inj₂; [_,_]′) |
| 54 |
open import Data.Sum.Effectful.Right.Transformer a E |
| 55 |
|
| 56 |
monadError : RawMonad M → RawMonadError (SumᵣT M) |
| 57 |
monadError M = record |
| 58 |
{ throw = mkSumᵣT ∘′ pure ∘′ inj₂ |
| 59 |
; catch = λ ma k → mkSumᵣT $ do |
| 60 |
a ← runSumᵣT ma |
| 61 |
[ pure ∘′ inj₁ , runSumᵣT ∘′ k ]′ a |
| 62 |
} where open RawMonad M |