Debian Codegraph @

src/Effect/Monad/State/Transformer.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- The state monad transformer
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9
10 module Effect.Monad.State.Transformer where
11
12 open import Algebra using (RawMonoid)
13 open import Data.Product.Base using (_×_; _,_; map₂; proj₁; proj₂)
14 open import Data.Unit.Polymorphic.Base using (tt)
15 open import Effect.Choice using (RawChoice)
16 open import Effect.Empty using (RawEmpty)
17 open import Effect.Functor using (RawFunctor)
18 open import Effect.Applicative using (RawApplicative; RawApplicativeZero; RawAlternative)
19 open import Effect.Monad using (RawMonad; RawMonadZero; RawMonadPlus; RawMonadT; RawMonadTd)
20 open import Function.Base using (_∘′_; _$_; const)
21 open import Level using (Level; _⊔_)
22
23
24 private
25 variable
26 f s : Level
27 A B : Set s
28 S S₁ S₂ : Set s
29 M : Set s → Set f
30
31 ------------------------------------------------------------------------
32 -- Re-export the basic type and definitions
33
34 open import Effect.Monad.State.Transformer.Base public
35 using ( RawMonadState
36 ; StateT
37 ; mkStateT
38 ; runStateT
39 ; evalStateT
40 ; execStateT
41 )
42
43 ------------------------------------------------------------------------
44 -- Structure
45
46 functor : RawFunctor M → RawFunctor (StateT S M)
47 functor M = record
48 { _<$>_ = λ f ma → mkStateT (λ s → map₂ f <$> StateT.runStateT ma s)
49 } where open RawFunctor M
50
51 applicative : RawMonad M → RawApplicative (StateT S M)
52 applicative M = record
53 { rawFunctor = functor rawFunctor
54 ; pure = λ a → mkStateT (pure ∘′ (_, a))
55 ; _<*>_ = λ mf mx → mkStateT $ λ s →
56 do (s , f) ← StateT.runStateT mf s
57 (s , x) ← StateT.runStateT mx s
58 pure (s , f x)
59 } where open RawMonad M
60
61 empty : RawEmpty M → RawEmpty (StateT S M)
62 empty M = record
63 { empty = mkStateT (const (RawEmpty.empty M))
64 }
65
66 choice : RawChoice M → RawChoice (StateT S M)
67 choice M = record
68 { _<|>_ = λ ma₁ ma₂ → mkStateT $ λ s →
69 StateT.runStateT ma₁ s
70 <|> StateT.runStateT ma₂ s
71 } where open RawChoice M
72
73 applicativeZero : RawMonadZero M → RawApplicativeZero (StateT S M)
74 applicativeZero M = record
75 { rawApplicative = applicative (RawMonadZero.rawMonad M)
76 ; rawEmpty = empty (RawMonadZero.rawEmpty M)
77 }
78
79 alternative : RawMonadPlus M → RawAlternative (StateT S M)
80 alternative M = record
81 { rawApplicativeZero = applicativeZero rawMonadZero
82 ; rawChoice = choice rawChoice
83 } where open RawMonadPlus M
84
85 monad : RawMonad M → RawMonad (StateT S M)
86 monad M = record
87 { rawApplicative = applicative M
88 ; _>>=_ = λ ma f → mkStateT $ λ s →
89 do (s , a) ← StateT.runStateT ma s
90 StateT.runStateT (f a) s
91 } where open RawMonad M
92
93 monadZero : RawMonadZero M → RawMonadZero (StateT S M)
94 monadZero M = record
95 { rawMonad = monad (RawMonadZero.rawMonad M)
96 ; rawEmpty = empty (RawMonadZero.rawEmpty M)
97 }
98
99 monadPlus : RawMonadPlus M → RawMonadPlus (StateT S M)
100 monadPlus M = record
101 { rawMonadZero = monadZero rawMonadZero
102 ; rawChoice = choice rawChoice
103 } where open RawMonadPlus M
104
105 ------------------------------------------------------------------------
106 -- State monad transformer specifics
107
108 monadT : RawMonadT {f = s} {g₁ = f} {g₂ = s ⊔ f} (StateT S)
109 monadT M = record
110 { lift = λ ma → mkStateT (λ s → (s ,_) <$> ma)
111 ; rawMonad = monad M
112 } where open RawMonad M
113
114 monadState : RawMonad M → RawMonadState S (StateT S M)
115 monadState M = record
116 { gets = λ f → mkStateT (λ s → pure (s , f s))
117 ; modify = λ f → mkStateT (λ s → pure (f s , _))
118 } where open RawMonad M
119
120 ------------------------------------------------------------------------
121 -- State monad transformer specifics
122
123 liftStateT : RawMonad M →
124 RawMonadState S₁ M →
125 RawMonadState S₁ (StateT S₂ M)
126 liftStateT M Mon = record
127 { gets = λ f₁ → lift (gets f₁)
128 ; modify = λ f₁ → lift (modify f₁)
129 } where open RawMonadTd (monadT M) using (lift); open RawMonadState Mon
130
131 open import Effect.Monad.Reader.Transformer.Base
132
133 liftReaderT : RawMonadState S₁ M →
134 RawMonadState S₁ (ReaderT S₂ M)
135 liftReaderT Mon = record
136 { gets = λ f → mkReaderT (const (gets f))
137 ; modify = λ f → mkReaderT (const (modify f))
138 } where open RawMonadState Mon
139
140 open import Effect.Monad.Writer.Transformer.Base
141
142 liftWriterT : (MS : RawMonoid s f) →
143 RawFunctor M →
144 RawMonadState S M →
145 RawMonadState S (WriterT MS M)
146 liftWriterT MS M Mon = record
147 { gets = λ f → mkWriterT λ w → (gets ((w ,_) ∘′ f))
148 ; modify = λ f → mkWriterT λ w → (const (w , tt) <$> modify f)
149 } where open RawMonadState Mon
150 open RawFunctor M