Skip to content

Commit 113a758

Browse files
authored
Merge pull request #411 from andreasabel/evalState
Add `evalState(T)`
2 parents 3037bf9 + 669cfe3 commit 113a758

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

linear-base.cabal

+2-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ test-suite examples
183183
base,
184184
linear-base,
185185
tasty,
186-
tasty-hedgehog,
186+
tasty-hedgehog < 1.2,
187+
-- tasty-hedgehog deprecates 'testProperty' in test/Test/Data/Destination.hs
187188
hedgehog,
188189
storable-tuple,
189190
vector,

src/Control/Functor/Linear.hs

+2
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ module Control.Functor.Linear
4848
State,
4949
state,
5050
runState,
51+
evalState,
5152
execState,
5253
mapState,
5354
withState,
5455
StateT (..),
5556
runStateT,
57+
evalStateT,
5658
execStateT,
5759
mapStateT,
5860
withStateT,

src/Control/Functor/Linear/Internal/State.hs

+13
Original file line numberDiff line numberDiff line change
@@ -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
3638
import Data.Functor.Identity
3739
import qualified Data.Functor.Linear.Internal.Applicative as Data
3840
import qualified Data.Functor.Linear.Internal.Functor as Data
41+
import qualified Data.Tuple.Linear as Linear
3942
import Data.Unrestricted.Linear.Internal.Consumable
4043
import Data.Unrestricted.Linear.Internal.Dupable
4144
import Prelude.Linear.Internal
@@ -80,6 +83,11 @@ withStateT r (StateT f) = StateT (f . r)
8083
execStateT :: Functor m => StateT s m () %1 -> s %1 -> m s
8184
execStateT 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+
8391
mapState :: ((a, s) %1 -> (b, s)) %1 -> State s a %1 -> State s b
8492
mapState f = mapStateT (Identity . f . runIdentity')
8593

@@ -89,6 +97,11 @@ withState = withStateT
8997
execState :: State s () %1 -> s %1 -> s
9098
execState 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+
92105
modify :: Applicative m => (s %1 -> s) %1 -> StateT s m ()
93106
modify f = state $ \s -> ((), f s)
94107

src/Data/Tuple/Linear.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{-# LANGUAGE LinearTypes #-}
22
{-# LANGUAGE NoImplicitPrelude #-}
33

4-
-- | This module provides linear functions commonly used on tuples
4+
-- | This module provides linear functions commonly used on tuples.
55
module Data.Tuple.Linear
66
( fst,
77
snd,
@@ -11,8 +11,8 @@ module Data.Tuple.Linear
1111
)
1212
where
1313

14-
import Data.Unrestricted.Linear
15-
import Prelude.Linear.Internal
14+
import Data.Unrestricted.Linear.Internal.Consumable
15+
import Prelude.Linear.Internal (curry, uncurry)
1616

1717
fst :: Consumable b => (a, b) %1 -> a
1818
fst (a, b) = lseq b a

src/Data/Unrestricted/Linear.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
-- If a type is 'Consumable', you can __consume__ it in a linear function that
2323
-- doesn't need that value to produce it's result:
2424
--
25-
-- > first :: Consumable b => (a,b) %1-> a
26-
-- > first (a,b) = withConsume (consume b) a
25+
-- > fst :: Consumable b => (a,b) %1-> a
26+
-- > fst (a,b) = withConsume (consume b) a
2727
-- > where
2828
-- > withConsume :: () %1-> a %1-> a
2929
-- > withConsume () x = x

0 commit comments

Comments
 (0)