1-- | The 'State' effect with dynamic dispatch.
3-- It's not clear in which situation it's beneficial to use this instead of
4-- "Effectful.State" or "Effectful.State.MVar" as you either:
6-- - Share state between threads and need the synchonized version.
8-- - Don't share state between threads (or want it to be thread local) and are
9-- free to use the faster, pure version.
11-- However, let's include this for now.
13module Effectful.State.Dynamic
36import Effectful.Interpreter
38import qualified Effectful.State as SP
39import qualified Effectful.State.MVar as SM
41-- | Provide access to a mutable state of type @s@.
43-- Whether the state is represented as a pure value or an 'MVar' underneath
44-- depends on the interpretation.
45data State s :: Effect where
47 Put :: ~s -> State s m ()
48 State :: (s -> (a, s)) -> State s m a
49 StateM :: (s -> m (a, s)) -> State s m a
51----------------------------------------
54runState :: s -> Eff (State s : es) a -> Eff es (a, s)
55runState s0 = reinterpretM (SP.runState s0) statePure
57evalState :: s -> Eff (State s : es) a -> Eff es a
58evalState s0 = reinterpretM (SP.evalState s0) statePure
60execState :: s -> Eff (State s : es) a -> Eff es s
61execState s0 = reinterpretM (SP.execState s0) statePure
66 -> State s (Eff localEs) a
72 StateM f -> localSeqUnlift env $ \run -> SP.stateM (run . f)
74----------------------------------------
77runStateMVar :: s -> Eff (State s : es) a -> Eff es (a, s)
78runStateMVar s0 = reinterpretM (SM.runState s0) stateMVar
80evalStateMVar :: s -> Eff (State s : es) a -> Eff es a
81evalStateMVar s0 = reinterpretM (SM.evalState s0) stateMVar
83execStateMVar :: s -> Eff (State s : es) a -> Eff es s
84execStateMVar s0 = reinterpretM (SM.execState s0) stateMVar
89 -> State s (Eff localEs) a
95 StateM f -> localSeqUnlift env $ \run -> SM.stateM (run . f)
97----------------------------------------
100get :: State s :> es => Eff es s
103gets :: State s :> es => (s -> a) -> Eff es a
106put :: State s :> es => s -> Eff es ()
109state :: (State s :> es) => (s -> (a, s)) -> Eff es a
112modify :: State s :> es => (s -> s) -> Eff es ()
113modify f = state (\s -> ((), f s))
115stateM :: State s :> es => (s -> Eff es (a, s)) -> Eff es a
116stateM = send . StateM
118modifyM :: State s :> es => (s -> Eff es s) -> Eff es ()
119modifyM f = stateM (\s -> ((), ) <$> f s)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list