module Data.Accessor.MonadStatePrivate where

import qualified Data.Accessor.Basic as Accessor
import qualified Control.Monad.Trans.State as State
import qualified Control.Monad.Trans.Class as Trans
import Control.Monad.Trans.State (State, runState, StateT(runStateT), )

-- * accessors in the form of actions in the state monad

set :: Monad m => Accessor.T r a -> a -> StateT r m ()
set :: forall (m :: * -> *) r a. Monad m => T r a -> a -> StateT r m ()
set T r a
f a
x = forall (m :: * -> *) s. Monad m => (s -> s) -> StateT s m ()
State.modify (forall r a. T r a -> a -> r -> r
Accessor.set T r a
f a
x)

get :: Monad m => Accessor.T r a -> StateT r m a
get :: forall (m :: * -> *) r a. Monad m => T r a -> StateT r m a
get T r a
f = forall (m :: * -> *) s a. Monad m => (s -> a) -> StateT s m a
State.gets (forall r a. T r a -> r -> a
Accessor.get T r a
f)

modify :: Monad m => Accessor.T r a -> (a -> a) -> StateT r m ()
modify :: forall (m :: * -> *) r a.
Monad m =>
T r a -> (a -> a) -> StateT r m ()
modify T r a
f a -> a
g = forall (m :: * -> *) s. Monad m => (s -> s) -> StateT s m ()
State.modify (forall r a. T r a -> (a -> a) -> r -> r
Accessor.modify T r a
f a -> a
g)

{- |
Modify a record element and return its old value.
-}
getAndModify :: Monad m => Accessor.T r a -> (a -> a) -> StateT r m a
getAndModify :: forall (m :: * -> *) r a.
Monad m =>
T r a -> (a -> a) -> StateT r m a
getAndModify T r a
f a -> a
g =
   do a
x <- forall (m :: * -> *) r a. Monad m => T r a -> StateT r m a
get T r a
f
      forall (m :: * -> *) r a.
Monad m =>
T r a -> (a -> a) -> StateT r m ()
modify T r a
f a -> a
g
      forall (m :: * -> *) a. Monad m => a -> m a
return a
x

{- |
Modify a record element and return its new value.
-}
modifyAndGet :: Monad m => Accessor.T r a -> (a -> a) -> StateT r m a
modifyAndGet :: forall (m :: * -> *) r a.
Monad m =>
T r a -> (a -> a) -> StateT r m a
modifyAndGet T r a
f a -> a
g =
   do forall (m :: * -> *) r a.
Monad m =>
T r a -> (a -> a) -> StateT r m ()
modify T r a
f a -> a
g
      forall (m :: * -> *) r a. Monad m => T r a -> StateT r m a
get T r a
f



infix 1 %=, %:

{- |
Infix variant of 'set'.
-}
(%=) :: Monad m => Accessor.T r a -> a -> StateT r m ()
%= :: forall (m :: * -> *) r a. Monad m => T r a -> a -> StateT r m ()
(%=) = forall (m :: * -> *) r a. Monad m => T r a -> a -> StateT r m ()
set

{- |
Infix variant of 'modify'.
-}
(%:) :: Monad m => Accessor.T r a -> (a -> a) -> StateT r m ()
%: :: forall (m :: * -> *) r a.
Monad m =>
T r a -> (a -> a) -> StateT r m ()
(%:) = forall (m :: * -> *) r a.
Monad m =>
T r a -> (a -> a) -> StateT r m ()
modify



-- * lift a state monadic accessor to an accessor of a parent record

lift :: Monad m => Accessor.T r s -> State s a -> StateT r m a
lift :: forall (m :: * -> *) r s a.
Monad m =>
T r s -> State s a -> StateT r m a
lift T r s
f State s a
m =
   do s
s0 <- forall (m :: * -> *) r a. Monad m => T r a -> StateT r m a
get T r s
f
      let (a
a,s
s1) = forall s a. State s a -> s -> (a, s)
runState State s a
m s
s0
      forall (m :: * -> *) r a. Monad m => T r a -> a -> StateT r m ()
set T r s
f s
s1
      forall (m :: * -> *) a. Monad m => a -> m a
return a
a

liftT :: (Monad m) =>
   Accessor.T r s -> StateT s m a -> StateT r m a
liftT :: forall (m :: * -> *) r s a.
Monad m =>
T r s -> StateT s m a -> StateT r m a
liftT T r s
f StateT s m a
m =
   do s
s0 <- forall (m :: * -> *) r a. Monad m => T r a -> StateT r m a
get T r s
f
      (a
a,s
s1) <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
Trans.lift forall a b. (a -> b) -> a -> b
$ forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT StateT s m a
m s
s0
      forall (m :: * -> *) r a. Monad m => T r a -> a -> StateT r m ()
set T r s
f s
s1
      forall (m :: * -> *) a. Monad m => a -> m a
return a
a