@@ -21,6 +21,8 @@ module Control.Functor.Linear.Internal.State
2121 runState ,
2222 mapStateT ,
2323 mapState ,
24+ evalStateT ,
25+ evalState ,
2426 execStateT ,
2527 execState ,
2628 withStateT ,
@@ -36,6 +38,7 @@ import qualified Control.Monad.Trans.State.Strict as NonLinear
3638import Data.Functor.Identity
3739import qualified Data.Functor.Linear.Internal.Applicative as Data
3840import qualified Data.Functor.Linear.Internal.Functor as Data
41+ import qualified Data.Tuple.Linear as Linear
3942import Data.Unrestricted.Linear.Internal.Consumable
4043import Data.Unrestricted.Linear.Internal.Dupable
4144import Prelude.Linear.Internal
@@ -80,6 +83,11 @@ withStateT r (StateT f) = StateT (f . r)
8083execStateT :: Functor m => StateT s m () % 1 -> s % 1 -> m s
8184execStateT f = fmap (\ (() , s) -> s) . (runStateT f)
8285
86+ -- | Use with care!
87+ -- This consumes the final state, so might be costly at runtime.
88+ evalStateT :: (Functor m , Consumable s ) => StateT s m a % 1 -> s % 1 -> m a
89+ evalStateT f = fmap Linear. fst . runStateT f
90+
8391mapState :: ((a , s ) % 1 -> (b , s )) % 1 -> State s a % 1 -> State s b
8492mapState f = mapStateT (Identity . f . runIdentity')
8593
@@ -89,6 +97,11 @@ withState = withStateT
8997execState :: State s () % 1 -> s % 1 -> s
9098execState f = runIdentity' . execStateT f
9199
100+ -- | Use with care!
101+ -- This consumes the final state, so might be costly at runtime.
102+ evalState :: Consumable s => State s a % 1 -> s % 1 -> a
103+ evalState f = runIdentity' . evalStateT f
104+
92105modify :: Applicative m => (s % 1 -> s ) % 1 -> StateT s m ()
93106modify f = state $ \ s -> (() , f s)
94107
0 commit comments