module Data.Accessor.Monad.FD.State where
import qualified Data.Accessor.Basic as Accessor
import qualified Control.Monad.State as State
import qualified Control.Monad.Trans as Trans
import Control.Monad.Trans.State (State, runState, StateT, runStateT, )
import Control.Monad.Trans (MonadTrans, )
import Control.Monad.State (MonadState, )
set :: MonadState r m => Accessor.T r a -> a -> m ()
set f x = State.modify (Accessor.set f x)
get :: MonadState r m => Accessor.T r a -> m a
get f = State.gets (Accessor.get f)
modify :: MonadState r m => Accessor.T r a -> (a -> a) -> m ()
modify f g = State.modify (Accessor.modify f g)
getAndModify :: MonadState r m => Accessor.T r a -> (a -> a) -> m a
getAndModify f g =
do x <- get f
modify f g
return x
modifyAndGet :: MonadState r m => Accessor.T r a -> (a -> a) -> m a
modifyAndGet f g =
do modify f g
get f
infix 1 %=, %:
(%=) :: MonadState r m => Accessor.T r a -> a -> m ()
(%=) = set
(%:) :: MonadState r m => Accessor.T r a -> (a -> a) -> m ()
(%:) = modify
lift :: (MonadState r mr) =>
Accessor.T r s -> State s a -> mr a
lift f m =
do s0 <- get f
let (a,s1) = runState m s0
set f s1
return a
liftT :: (Monad m, MonadTrans t, MonadState r (t m)) =>
Accessor.T r s -> StateT s m a -> t m a
liftT f m =
do s0 <- get f
(a,s1) <- Trans.lift $ runStateT m s0
set f s1
return a