{-# LANGUAGE TemplateHaskell #-}
module Polysemy.State
(
State (..)
, get
, gets
, put
, modify
, runState
, runLazyState
, runStateInIORef
) where
import Data.IORef
import Polysemy
import Polysemy.Internal.Combinators
data State s m a where
Get :: State s m s
Put :: s -> State s m ()
makeSem ''State
gets :: Member (State s) r => (s -> a) -> Sem r a
gets f = fmap f get
{-# INLINABLE gets #-}
modify :: Member (State s) r => (s -> s) -> Sem r ()
modify f = do
s <- get
put $ f s
{-# INLINABLE modify #-}
runState :: s -> Sem (State s ': r) a -> Sem r (s, a)
runState = stateful $ \case
Get -> \s -> pure (s, s)
Put s -> const $ pure (s, ())
{-# INLINE[3] runState #-}
runLazyState :: s -> Sem (State s ': r) a -> Sem r (s, a)
runLazyState = lazilyStateful $ \case
Get -> \s -> pure (s, s)
Put s -> const $ pure (s, ())
{-# INLINE[3] runLazyState #-}
runStateInIORef
:: forall s r a
. Member (Lift IO) r
=> IORef s
-> Sem (State s ': r) a
-> Sem r a
runStateInIORef ref = interpret $ \case
Get -> sendM $ readIORef ref
Put s -> sendM $ writeIORef ref s
{-# INLINE runStateInIORef #-}
{-# RULES "runState/reinterpret"
forall s e (f :: forall m x. e m x -> Sem (State s ': r) x).
runState s (reinterpret f e) = stateful (\x s' -> runState s' $ f x) s e
#-}
{-# RULES "runLazyState/reinterpret"
forall s e (f :: forall m x. e m x -> Sem (State s ': r) x).
runLazyState s (reinterpret f e) = lazilyStateful (\x s' -> runLazyState s' $ f x) s e
#-}