Debian Codegraph @

src/Data/These/Effectful/Left.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Left-biased universe-sensitive functor and monad instances for These.
5 --
6 ------------------------------------------------------------------------
7
8 -- To minimize the universe level of the RawFunctor, we require that
9 -- elements of B are "lifted" to a copy of B at a higher universe level
10 -- (a ⊔ b).
11 -- See the Data.Product.Effectful.Examples for how this is done in a
12 -- Product-based similar setting.
13
14 -- This functor can be understood as a notion of computation which can
15 -- either fail (this), succeed (that) or accumulate warnings whilst
16 -- delivering a successful computation (these).
17
18 -- It is a good alternative to Data.Product.Effectful when the notion
19 -- of warnings does not have a neutral element (e.g. List⁺).
20
21 {-# OPTIONS --cubical-compatible --safe #-}
22
23 open import Level using (Level; _⊔_)
24 open import Algebra.Bundles using (Semigroup)
25
26 module Data.These.Effectful.Left {c ℓ} (W : Semigroup c ℓ) (b : Level) where
27
28 open import Data.These.Base using (These; this; that; these; map₁; map₂; map)
29 open import Effect.Applicative using (RawApplicative)
30 open import Effect.Monad using (RawMonad)
31
32 open Semigroup W using (Carrier; _∙_)
33 open import Data.These.Effectful.Left.Base Carrier b public
34
35 module _ {a b} {A : Set a} {B : Set b} where
36
37 applicative : RawApplicative Theseₗ
38 applicative = record
39 { rawFunctor = functor
40 ; pure = that
41 ; _<*>_ = ap
42 } where
43
44 ap : ∀ {A B} → Theseₗ (A → B) → Theseₗ A → Theseₗ B
45 ap (this w) t = this w
46 ap (that f) t = map₂ f t
47 ap (these w f) t = map (w ∙_) f t
48
49 monad : RawMonad Theseₗ
50 monad = record
51 { rawApplicative = applicative
52 ; _>>=_ = bind
53 } where
54
55 bind : ∀ {A B} → Theseₗ A → (A → Theseₗ B) → Theseₗ B
56 bind (this w) f = this w
57 bind (that t) f = f t
58 bind (these w t) f = map₁ (w ∙_) (f t)