| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- The state monad |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Effect.Monad.State where |
| 10 |
|
| 11 |
open import Data.Product.Base using (_×_) |
| 12 |
open import Effect.Functor using (RawFunctor) |
| 13 |
open import Effect.Applicative using (RawApplicative) |
| 14 |
open import Effect.Monad using (RawMonad; module Join) |
| 15 |
open import Effect.Monad.Identity as Id using (Identity; runIdentity) |
| 16 |
open import Level using (Level) |
| 17 |
|
| 18 |
import Effect.Monad.State.Transformer as Trans |
| 19 |
|
| 20 |
private |
| 21 |
variable |
| 22 |
s : Level |
| 23 |
S A : Set s |
| 24 |
|
| 25 |
------------------------------------------------------------------------ |
| 26 |
-- Re-export the state monad operations |
| 27 |
|
| 28 |
open Trans public |
| 29 |
using (RawMonadState) |
| 30 |
|
| 31 |
------------------------------------------------------------------------ |
| 32 |
-- State monad |
| 33 |
|
| 34 |
State : (S : Set s) (A : Set s) → Set s |
| 35 |
State S = Trans.StateT S Identity |
| 36 |
|
| 37 |
runState : State S A → S → S × A |
| 38 |
runState ma s = runIdentity (Trans.runStateT ma s) |
| 39 |
|
| 40 |
evalState : State S A → S → A |
| 41 |
evalState ma s = runIdentity (Trans.evalStateT Id.functor ma s) |
| 42 |
|
| 43 |
execState : State S A → S → S |
| 44 |
execState ma s = runIdentity (Trans.execStateT Id.functor ma s) |
| 45 |
|
| 46 |
------------------------------------------------------------------------ |
| 47 |
-- Structure |
| 48 |
|
| 49 |
functor : RawFunctor (State S) |
| 50 |
functor = Trans.functor Id.functor |
| 51 |
|
| 52 |
applicative : RawApplicative (State S) |
| 53 |
applicative = Trans.applicative Id.monad |
| 54 |
|
| 55 |
monad : RawMonad (State S) |
| 56 |
monad = Trans.monad Id.monad |
| 57 |
|
| 58 |
join : State S (State S A) → State S A |
| 59 |
join = Join.join monad |
| 60 |
|
| 61 |
------------------------------------------------------------------------ |
| 62 |
-- State monad specifics |
| 63 |
|
| 64 |
monadState : RawMonadState S (State S) |
| 65 |
monadState = Trans.monadState Id.monad |