module Control.Effect.State
(
State(..)
, get
, gets
, put
, modify
, modifyLazy
, state
, Algebra
, Effect
, Has
, run
) where
import Control.Algebra
import Control.Effect.State.Internal (State(..))
get :: Has (State s) sig m => m s
get :: m s
get = State s m s -> m s
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send ((s -> m s) -> State s m s
forall s (m :: * -> *) k. (s -> m k) -> State s m k
Get s -> m s
forall (f :: * -> *) a. Applicative f => a -> f a
pure)
{-# INLINEABLE get #-}
gets :: Has (State s) sig m => (s -> a) -> m a
gets :: (s -> a) -> m a
gets f :: s -> a
f = State s m a -> m a
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send ((s -> m a) -> State s m a
forall s (m :: * -> *) k. (s -> m k) -> State s m k
Get (a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> m a) -> (s -> a) -> s -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> a
f))
{-# INLINEABLE gets #-}
put :: Has (State s) sig m => s -> m ()
put :: s -> m ()
put s :: s
s = State s m () -> m ()
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (s -> m () -> State s m ()
forall s (m :: * -> *) k. s -> m k -> State s m k
Put s
s (() -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()))
{-# INLINEABLE put #-}
modify :: Has (State s) sig m => (s -> s) -> m ()
modify :: (s -> s) -> m ()
modify f :: s -> s
f = do
s
a <- m s
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
Has (State s) sig m =>
m s
get
s -> m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
Has (State s) sig m =>
s -> m ()
put (s -> m ()) -> s -> m ()
forall a b. (a -> b) -> a -> b
$! s -> s
f s
a
{-# INLINEABLE modify #-}
modifyLazy :: Has (State s) sig m => (s -> s) -> m ()
modifyLazy :: (s -> s) -> m ()
modifyLazy f :: s -> s
f = m s
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
Has (State s) sig m =>
m s
get m s -> (s -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
Has (State s) sig m =>
s -> m ()
put (s -> m ()) -> (s -> s) -> s -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> s
f
{-# INLINEABLE modifyLazy #-}
state :: Has (State s) sig m => (s -> (s, a)) -> m a
state :: (s -> (s, a)) -> m a
state f :: s -> (s, a)
f = do
(s' :: s
s', a :: a
a) <- (s -> (s, a)) -> m (s, a)
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Has (State s) sig m =>
(s -> a) -> m a
gets s -> (s, a)
f
a
a a -> m () -> m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ s -> m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
Has (State s) sig m =>
s -> m ()
put s
s'
{-# INLINEABLE state #-}