| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Basic definition and functions on the state monad transformer |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
|
| 10 |
module Effect.Monad.State.Transformer.Base where |
| 11 |
|
| 12 |
open import Data.Product.Base using (_×_; proj₁; proj₂) |
| 13 |
open import Data.Unit.Polymorphic.Base using (⊤) |
| 14 |
open import Effect.Functor using (RawFunctor) |
| 15 |
open import Function.Base using (_∘′_; const; id) |
| 16 |
open import Level using (Level; suc; _⊔_) |
| 17 |
|
| 18 |
private |
| 19 |
variable |
| 20 |
f s : Level |
| 21 |
A : Set s |
| 22 |
S : Set s |
| 23 |
M : Set s → Set f |
| 24 |
|
| 25 |
------------------------------------------------------------------------ |
| 26 |
-- State monad operations |
| 27 |
|
| 28 |
record RawMonadState |
| 29 |
(S : Set s) |
| 30 |
(M : Set s → Set f) |
| 31 |
: Set (suc s ⊔ f) where |
| 32 |
field |
| 33 |
gets : (S → A) → M A |
| 34 |
modify : (S → S) → M ⊤ |
| 35 |
|
| 36 |
put = modify ∘′ const |
| 37 |
get = gets id |
| 38 |
|
| 39 |
------------------------------------------------------------------------ |
| 40 |
-- State monad transformer |
| 41 |
|
| 42 |
record StateT |
| 43 |
(S : Set s) |
| 44 |
(M : Set s → Set f) |
| 45 |
(A : Set s) |
| 46 |
: Set (s ⊔ f) where |
| 47 |
constructor mkStateT |
| 48 |
field runStateT : S → M (S × A) |
| 49 |
open StateT public |
| 50 |
|
| 51 |
evalStateT : RawFunctor M → StateT S M A → S → M A |
| 52 |
evalStateT M ma s = let open RawFunctor M in proj₂ <$> runStateT ma s |
| 53 |
|
| 54 |
execStateT : RawFunctor M → StateT S M A → S → M S |
| 55 |
execStateT M ma s = let open RawFunctor M in proj₁ <$> runStateT ma s |