-
Notifications
You must be signed in to change notification settings - Fork 0
/
State.hs
95 lines (78 loc) · 1.88 KB
/
State.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
{-|
The `MTL.MonadState`-like effect.
-}
module Effect.State
( -- * Interface
State
, get
, gets
, put
, modify
-- * Implementations
, storeViaRIO
, mergeState
, asState
-- * Re-exporting core
, module Core
)
where
import Control.Monad.IO.Class
import qualified Control.Monad.State as MTL
import Data.IORef
import Data.Coerce
import Data.Kind (Type)
import Core
import Effect.Final
import Effect.Lift
import Effect.Reader
import Product
-- | Ability to have `MTL.State` @s@.
data State s (m :: Type -> Type) a where
Get :: State s m s
Put :: s -> State s m ()
deriving anyclass Effect
-- | Get current state.
get :: forall s fs. Members '[State s] fs => Eff fs s
get = send Get
-- | Get view on the current state.
gets :: forall s a fs. Members '[State s] fs => (s -> a) -> Eff fs a
gets f = f <$> get
-- | Replace the state.
put :: forall s fs. Members '[State s] fs => s -> Eff fs ()
put s = send (Put s)
-- | Modify the state.
modify :: forall s fs. Members '[State s] fs => (s -> s) -> Eff fs ()
modify f = put . f =<< get
-- | Implement via `IORef`.
storeViaRIO
:: forall e m fs
. (MonadIO m, Members [Reader (IORef e), Lift m] fs)
=> Eff (State e : fs)
~> Eff fs
storeViaRIO = plug \case
Get -> do
ref <- ask
lift @m $ liftIO $ readIORef ref
Put s -> do
ref <- ask
lift @m $ liftIO $ writeIORef ref s
-- | Implement like `mergeEnv`.
mergeState
:: forall x xs fs
. (Contains x xs, Members '[State (Product xs)] fs)
=> Eff (State x : fs)
~> Eff fs
mergeState = plug \case
Get -> do
gets @(Product xs) getElem
Put s -> do
modify @(Product xs) $ modElem $ const s
-- | Delegate to final monad.
asState
:: forall e m fs
. (Members '[Lift m] fs, MTL.MonadState e m)
=> Eff (State e : fs)
~> Eff fs
asState = plug \case
Get -> lift @m MTL.get
Put s -> lift @m (MTL.put s)