on master help sign in
Use monadic action to get the initial state
1639cfdHarendra Kumar
benchmark/Streamly/Benchmark/Prelude/Serial.hs+4−2
@@ -1688,12 +1688,14 @@ sourceUnfoldrState value n = S.unfoldrM step n
1688
1688
1689{-# INLINE evalStateT #-}
1689{-# INLINE evalStateT #-}
1690evalStateT :: S.MonadAsync m => Int -> Int -> SerialT m Int
1690evalStateT :: S.MonadAsync m => Int -> Int -> SerialT m Int
1691+evalStateT value n =
1691−evalStateT value n = Internal.evalStateT 0 (sourceUnfoldrState value n)
1692+ Internal.evalStateT (return 0) (sourceUnfoldrState value n)
1692
1693
1693{-# INLINE withState #-}
1694{-# INLINE withState #-}
1694withState :: S.MonadAsync m => Int -> Int -> SerialT m Int
1695withState :: S.MonadAsync m => Int -> Int -> SerialT m Int
1695withState value n =
1696withState value n =
1697+ Internal.evalStateT
1696− Internal.evalStateT (0 :: Int) (Internal.liftInner (sourceUnfoldrM value n))
1698+ (return (0 :: Int)) (Internal.liftInner (sourceUnfoldrM value n))
1697
1699
1698o_1_space_hoisting :: Int -> [Benchmark]
1700o_1_space_hoisting :: Int -> [Benchmark]
1699o_1_space_hoisting value =
1701o_1_space_hoisting value =
examples/Split.hs+1−1
@@ -28,7 +28,7 @@ splitFile inHandle =
28 S.unfold FH.read inHandle
28 S.unfold FH.read inHandle
29 & S.liftInner
29 & S.liftInner
30 & S.chunksOf2 (180 * 1024 * 1024) newHandle IFH.write2
30 & S.chunksOf2 (180 * 1024 * 1024) newHandle IFH.write2
31− & S.evalStateT Nothing -- generate new handle for each iteration
31+ & S.evalStateT (return Nothing) -- generate new handle for each iteration
32 & S.drain
32 & S.drain
33
33
34main :: IO ()
34main :: IO ()
src/Streamly/Internal/Data/Stream/StreamD.hs+19−16
@@ -839,39 +839,42 @@ liftInner (Stream step state) = Stream step' state
839 Stop -> Stop
839 Stop -> Stop
840
840
841{-# INLINE_NORMAL runReaderT #-}
841{-# INLINE_NORMAL runReaderT #-}
842−runReaderT :: Monad m => s -> Stream (ReaderT s m) a -> Stream m a
842+runReaderT :: Monad m => m s -> Stream (ReaderT s m) a -> Stream m a
843−runReaderT sval (Stream step state) = Stream step' state
843+runReaderT env (Stream step state) = Stream step' (state, env)
844 where
844 where
845 {-# INLINE_LATE step' #-}
845 {-# INLINE_LATE step' #-}
846− step' gst st = do
846+ step' gst (st, action) = do
847+ sv <- action
847− r <- Reader.runReaderT (step (adaptState gst) st) sval
848+ r <- Reader.runReaderT (step (adaptState gst) st) sv
848 return $ case r of
849 return $ case r of
849− Yield x s -> Yield x s
850+ Yield x s -> Yield x (s, return sv)
850− Skip s -> Skip s
851+ Skip s -> Skip (s, return sv)
851 Stop -> Stop
852 Stop -> Stop
852
853
853{-# INLINE_NORMAL evalStateT #-}
854{-# INLINE_NORMAL evalStateT #-}
854−evalStateT :: Monad m => s -> Stream (StateT s m) a -> Stream m a
855+evalStateT :: Monad m => m s -> Stream (StateT s m) a -> Stream m a
855−evalStateT sval (Stream step state) = Stream step' (state, sval)
856+evalStateT initial (Stream step state) = Stream step' (state, initial)
856 where
857 where
857 {-# INLINE_LATE step' #-}
858 {-# INLINE_LATE step' #-}
858− step' gst (st, sv) = do
859+ step' gst (st, action) = do
860+ sv <- action
859 (r, sv') <- State.runStateT (step (adaptState gst) st) sv
861 (r, sv') <- State.runStateT (step (adaptState gst) st) sv
860 return $ case r of
862 return $ case r of
861− Yield x s -> Yield x (s, sv')
863+ Yield x s -> Yield x (s, return sv')
862− Skip s -> Skip (s, sv')
864+ Skip s -> Skip (s, return sv')
863 Stop -> Stop
865 Stop -> Stop
864
866
865{-# INLINE_NORMAL runStateT #-}
867{-# INLINE_NORMAL runStateT #-}
866−runStateT :: Monad m => s -> Stream (StateT s m) a -> Stream m (s, a)
868+runStateT :: Monad m => m s -> Stream (StateT s m) a -> Stream m (s, a)
867−runStateT sval (Stream step state) = Stream step' (state, sval)
869+runStateT initial (Stream step state) = Stream step' (state, initial)
868 where
870 where
869 {-# INLINE_LATE step' #-}
871 {-# INLINE_LATE step' #-}
870− step' gst (st, sv) = do
872+ step' gst (st, action) = do
873+ sv <- action
871 (r, sv') <- State.runStateT (step (adaptState gst) st) sv
874 (r, sv') <- State.runStateT (step (adaptState gst) st) sv
872 return $ case r of
875 return $ case r of
873− Yield x s -> Yield (sv', x) (s, sv')
876+ Yield x s -> Yield (sv', x) (s, return sv')
874− Skip s -> Skip (s, sv')
877+ Skip s -> Skip (s, return sv')
875 Stop -> Stop
878 Stop -> Stop
876
879
877------------------------------------------------------------------------------
880------------------------------------------------------------------------------
src/Streamly/Internal/Prelude.hs+5−5
@@ -4955,7 +4955,7 @@ liftInner xs = fromStreamD $ D.liftInner (toStreamD xs)
4955-- / Internal/
4955-- / Internal/
4956--
4956--
4957{-# INLINE runReaderT #-}
4957{-# INLINE runReaderT #-}
4958−runReaderT :: (IsStream t, Monad m) => s -> t (ReaderT s m) a -> t m a
4958+runReaderT :: (IsStream t, Monad m) => m s -> t (ReaderT s m) a -> t m a
4959runReaderT s xs = fromStreamD $ D.runReaderT s (toStreamD xs)
4959runReaderT s xs = fromStreamD $ D.runReaderT s (toStreamD xs)
4960
4960
4961-- | Evaluate the inner monad of a stream as 'StateT'.
4961-- | Evaluate the inner monad of a stream as 'StateT'.
@@ -4966,7 +4966,7 @@ runReaderT s xs = fromStreamD $ D.runReaderT s (toStreamD xs)
4966-- / Internal/
4966-- / Internal/
4967--
4967--
4968{-# INLINE evalStateT #-}
4968{-# INLINE evalStateT #-}
4969−evalStateT :: Monad m => s -> SerialT (StateT s m) a -> SerialT m a
4969+evalStateT :: Monad m => m s -> SerialT (StateT s m) a -> SerialT m a
4970evalStateT s xs = fromStreamD $ D.evalStateT s (toStreamD xs)
4970evalStateT s xs = fromStreamD $ D.evalStateT s (toStreamD xs)
4971
4971
4972-- | Run a stateful (StateT) stream transformation using a given state.
4972-- | Run a stateful (StateT) stream transformation using a given state.
@@ -4979,7 +4979,7 @@ evalStateT s xs = fromStreamD $ D.evalStateT s (toStreamD xs)
4979{-# INLINE usingStateT #-}
4979{-# INLINE usingStateT #-}
4980usingStateT
4980usingStateT
4981 :: Monad m
4981 :: Monad m
4982− => s
4982+ => m s
4983 -> (SerialT (StateT s m) a -> SerialT (StateT s m) a)
4983 -> (SerialT (StateT s m) a -> SerialT (StateT s m) a)
4984 -> SerialT m a
4984 -> SerialT m a
4985 -> SerialT m a
4985 -> SerialT m a
@@ -4994,7 +4994,7 @@ usingStateT s f xs = evalStateT s $ f $ liftInner xs
4994-- / Internal/
4994-- / Internal/
4995--
4995--
4996{-# INLINE runStateT #-}
4996{-# INLINE runStateT #-}
4997−runStateT :: Monad m => s -> SerialT (StateT s m) a -> SerialT m (s, a)
4997+runStateT :: Monad m => m s -> SerialT (StateT s m) a -> SerialT m (s, a)
4998runStateT s xs = fromStreamD $ D.runStateT s (toStreamD xs)
4998runStateT s xs = fromStreamD $ D.runStateT s (toStreamD xs)
4999
4999
5000-- | Run a stream transformation using a given environment.
5000-- | Run a stream transformation using a given environment.
@@ -5004,7 +5004,7 @@ runStateT s xs = fromStreamD $ D.runStateT s (toStreamD xs)
5004{-# INLINE usingReaderT #-}
5004{-# INLINE usingReaderT #-}
5005usingReaderT
5005usingReaderT
5006 :: (Monad m, IsStream t)
5006 :: (Monad m, IsStream t)
5007− => r
5007+ => m r
5008 -> (t (ReaderT r m) a -> t (ReaderT r m) a)
5008 -> (t (ReaderT r m) a -> t (ReaderT r m) a)
5009 -> t m a
5009 -> t m a
5010 -> t m a
5010 -> t m a