@@ -28,40 +28,83 @@ import Effectful.Internal.Monad
2828newtype State s :: Effect where
2929 State :: s -> State s m r
3030
31−runState :: s -> Eff (State s : es) a -> Eff es (a, s)
31+-- | Run a 'State' effect with the given initial state and return the final
32+-- value along with the final state.
33+runState
34+ :: s -- ^ An initial state.
35+ -> Eff (State s : es) a
36+ -> Eff es (a, s)
3237runState s0 m = do
3338 (a, IdE (State s)) <- runEffect (IdE (State s0)) m
3439 pure (a, s)
3540
36−evalState :: s -> Eff (State s : es) a -> Eff es a
41+-- | Run a 'State' effect with the given initial state and return the final
42+-- value, discarding the final state.
43+evalState
44+ :: s -- ^ An initial state.
45+ -> Eff (State s : es) a
46+ -> Eff es a -- ^ A return value.
3747evalState s = evalEffect (IdE (State s))
3848
39−execState :: s -> Eff (State s : es) a -> Eff es s
49+-- | Run a 'State' effect with the given initial state and return the final
50+-- state, discarding the final value.
51+execState
52+ :: s -- ^ An initial state.
53+ -> Eff (State s : es) a
54+ -> Eff es s
4055execState s0 m = do
4156 IdE (State s) <- execEffect (IdE (State s0)) m
4257 pure s
4358
59+-- | Fetch the current value of the state.
4460get :: State s :> es => Eff es s
4561get = do
4662 IdE (State s) <- getEffect
4763 pure s
4864
49−gets :: State s :> es => (s -> a) -> Eff es a
65+-- | Get a function of the current state.
66+--
67+-- @'gets' f ≡ f '<$>' 'get'@
68+gets
69+ :: State s :> es
70+ => (s -> a) -- ^ The function to apply to the state.
71+ -> Eff es a
5072gets f = f <$> get
5173
74+-- | Set the current state to the given value.
5275put :: State s :> es => s -> Eff es ()
5376put s = putEffect (IdE (State s))
5477
55−state :: State s :> es => (s -> (a, s)) -> Eff es a
78+-- | Apply the function to the current state and return a value.
79+state
80+ :: State s :> es
81+ => (s -> (a, s)) -- ^ The function to modify the state.
82+ -> Eff es a
5683state f = stateEffect $ \(IdE (State s0)) -> let (a, s) = f s0 in (a, IdE (State s))
5784
58−modify :: State s :> es => (s -> s) -> Eff es ()
59−modify f = state (\s -> ((), f s))
85+-- | Apply the function to the current state.
86+--
87+-- @'modify' f ≡ 'state' (\\s -> ((), f s))@
88+modify
89+ :: State s :> es
90+ => (s -> s) -- ^ The function to modify the state.
91+ -> Eff es ()
92+modify f = state $ \s -> ((), f s)
6093
61−stateM :: State s :> es => (s -> Eff es (a, s)) -> Eff es a
94+-- | Apply the monadic function to the current state and return a value.
95+stateM
96+ :: State s :> es
97+ => (s -> Eff es (a, s)) -- ^ The function to modify the state.
98+ -> Eff es a
6299stateM f = stateEffectM $ \(IdE (State s0)) -> do
63100 (a, s) <- f s0
64101 pure (a, IdE (State s))
65102
66−modifyM :: State s :> es => (s -> Eff es s) -> Eff es ()
103+-- | Apply the monadic function to the current state.
104+--
105+-- @'modifyM' f ≡ 'stateM' (\\s -> ((), ) '<$>' f s)@
106+modifyM
107+ :: State s :> es
108+ => (s -> Eff es s) -- ^ The monadic function to modify the state.
109+ -> Eff es ()
67110modifyM f = stateM (\s -> ((), ) <$> f s)