@@ -21,6 +21,8 @@ module Control.Functor.Linear.Internal.State
21
21
runState ,
22
22
mapStateT ,
23
23
mapState ,
24
+ evalStateT ,
25
+ evalState ,
24
26
execStateT ,
25
27
execState ,
26
28
withStateT ,
@@ -36,6 +38,7 @@ import qualified Control.Monad.Trans.State.Strict as NonLinear
36
38
import Data.Functor.Identity
37
39
import qualified Data.Functor.Linear.Internal.Applicative as Data
38
40
import qualified Data.Functor.Linear.Internal.Functor as Data
41
+ import qualified Data.Tuple.Linear as Linear
39
42
import Data.Unrestricted.Linear.Internal.Consumable
40
43
import Data.Unrestricted.Linear.Internal.Dupable
41
44
import Prelude.Linear.Internal
@@ -80,6 +83,11 @@ withStateT r (StateT f) = StateT (f . r)
80
83
execStateT :: Functor m => StateT s m () % 1 -> s % 1 -> m s
81
84
execStateT f = fmap (\ (() , s) -> s) . (runStateT f)
82
85
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
+
83
91
mapState :: ((a , s ) % 1 -> (b , s )) % 1 -> State s a % 1 -> State s b
84
92
mapState f = mapStateT (Identity . f . runIdentity')
85
93
@@ -89,6 +97,11 @@ withState = withStateT
89
97
execState :: State s () % 1 -> s % 1 -> s
90
98
execState f = runIdentity' . execStateT f
91
99
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
+
92
105
modify :: Applicative m => (s % 1 -> s ) % 1 -> StateT s m ()
93
106
modify f = state $ \ s -> (() , f s)
94
107
0 commit comments