Debian Codegraph @

src/Effect/Monad/Writer/Transformer/Base.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Basic type and definition of the writer monad transformer
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Effect.Monad.Writer.Transformer.Base where
10
11 open import Algebra using (RawMonoid)
12 open import Data.Product.Base using (_×_; _,_; proj₁; proj₂)
13 open import Data.Unit.Polymorphic using (⊤; tt)
14 open import Effect.Functor using (RawFunctor)
15 open import Function.Base using (id; _∘′_)
16 open import Level using (Level; suc; _⊔_)
17
18 private
19 variable
20 w f g : Level
21 A : Set g
22 M : Set w → Set g
23 𝕎 : RawMonoid w g
24
25 ------------------------------------------------------------------------
26 -- Writer monad operations
27
28 record RawMonadWriter
29 (𝕎 : RawMonoid w f)
30 (M : Set w → Set g)
31 : Set (suc w ⊔ g) where
32 open RawMonoid 𝕎 renaming (Carrier to W)
33 field
34 writer : W × A → M A
35 listen : M A → M (W × A)
36 pass : M ((W → W) × A) → M A
37
38 tell : W → M ⊤
39 tell w = writer (w , tt)
40
41 ------------------------------------------------------------------------
42 -- Writer monad transformer (CPS-encoded)
43
44 record WriterT
45 (𝕎 : RawMonoid w f)
46 (M : Set w → Set g)
47 (A : Set w)
48 : Set (w ⊔ g) where
49 constructor mkWriterT
50 open RawMonoid 𝕎 renaming (Carrier to W)
51 field runWriterT : W → M (W × A)
52 open WriterT public
53
54 module _ {𝕎 : RawMonoid w f} where
55
56 open RawMonoid 𝕎 renaming (Carrier to W)
57
58 evalWriterT : RawFunctor M → WriterT 𝕎 M A → M A
59 evalWriterT M ma = proj₂ <$> runWriterT ma ε
60 where open RawFunctor M
61
62 execWriterT : RawFunctor M → WriterT 𝕎 M A → M W
63 execWriterT M ma = proj₁ <$> runWriterT ma ε
64 where open RawFunctor M