Debian Codegraph @

src/Data/Sum/Effectful/Right/Transformer.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- An effectful view of the Sum type (Right-biased)
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 open import Level
10
11 module Data.Sum.Effectful.Right.Transformer (a : Level) {b} (B : Set b) where
12
13 open import Data.Sum.Base
14 open import Effect.Choice
15 open import Effect.Empty
16 open import Effect.Functor
17 open import Effect.Applicative
18 open import Effect.Monad
19 open import Function.Base
20
21 private
22 variable
23 M : Set (a ⊔ b) → Set (a ⊔ b)
24
25 -- To minimize the universe level of the RawFunctor, we require that
26 -- elements of B are "lifted" to a copy of B at a higher universe level
27 -- (a ⊔ b). See the examples for how this is done.
28
29 open import Data.Sum.Effectful.Right a B using (Sumᵣ)
30
31 ------------------------------------------------------------------------
32 -- Right-biased monad transformer instance for _⊎_
33
34 record SumᵣT
35 (M : Set (a ⊔ b) → Set (a ⊔ b))
36 (B : Set (a ⊔ b)) : Set (a ⊔ b) where
37 constructor mkSumᵣT
38 field runSumᵣT : M (Sumᵣ B)
39 open SumᵣT public
40
41 ------------------------------------------------------------------------
42 -- Structure
43
44 functor : RawFunctor M → RawFunctor (SumᵣT M)
45 functor M = record
46 { _<$>_ = λ f → mkSumᵣT ∘′ (map₁ f <$>_) ∘′ runSumᵣT
47 } where open RawFunctor M
48
49 applicative : RawApplicative M → RawApplicative (SumᵣT M)
50 applicative M = record
51 { rawFunctor = functor rawFunctor
52 ; pure = mkSumᵣT ∘′ pure ∘′ inj₁
53 ; _<*>_ = λ mf mx → mkSumᵣT ([ map₁ , const ∘′ inj₂ ]′ <$> runSumᵣT mf <*> runSumᵣT mx)
54 } where open RawApplicative M
55
56 empty : RawApplicative M → B → RawEmpty (SumᵣT M)
57 empty M a = record
58 { empty = mkSumᵣT (pure (inj₂ a))
59 } where open RawApplicative M
60
61 choice : RawApplicative M → RawChoice (SumᵣT M)
62 choice M = record
63 { _<|>_ = λ ma₁ ma₁ → mkSumᵣT ([ const ∘ inj₁ , flip const ]′ <$> runSumᵣT ma₁ <*> runSumᵣT ma₁)
64 } where open RawApplicative M
65
66 applicativeZero : RawApplicative M → B → RawApplicativeZero (SumᵣT M)
67 applicativeZero M a = record
68 { rawApplicative = applicative M
69 ; rawEmpty = empty M a
70 }
71
72 alternative : RawApplicative M → B → RawAlternative (SumᵣT M)
73 alternative M a = record
74 { rawApplicativeZero = applicativeZero M a
75 ; rawChoice = choice M
76 }
77
78 monad : RawMonad M → RawMonad (SumᵣT M)
79 monad M = record
80 { rawApplicative = applicative rawApplicative
81 ; _>>=_ = λ ma f → mkSumᵣT $ do
82 a ← runSumᵣT ma
83 [ runSumᵣT ∘′ f , pure ∘′ inj₂ ]′ a
84 } where open RawMonad M
85
86 monadT : RawMonadT SumᵣT
87 monadT M = record
88 { lift = mkSumᵣT ∘′ (inj₁ <$>_)
89 ; rawMonad = monad M
90 } where open RawMonad M