on master help sign in
Use monadic action to get the initial state

In ReaderT/StateT evaluation.

commit1639cfde985e366b97377f1125079b97a23a07b1 authorHarendra Kumar <harendra@composewell.com> dateWed 19 Aug 2020, 04:32 (6 years ago) parentd162e6d
4 files changed+29−24
benchmark/Streamly/Benchmark/Prelude/Serial.hs+4−2 examples/Split.hs+1−1 src/Streamly/Internal/Data/Stream/StreamD.hs+19−16 src/Streamly/Internal/Prelude.hs+5−5
benchmark/Streamly/Benchmark/Prelude/Serial.hs+4−2
@@ -1688,12 +1688,14 @@ sourceUnfoldrState value n = S.unfoldrM step n
16881688
16891689{-# INLINE evalStateT #-}
16901690evalStateT :: S.MonadAsync m => Int -> Int -> SerialT m Int
1691−evalStateT value n = Internal.evalStateT 0 (sourceUnfoldrState value n)
1691+evalStateT value n =
1692+ Internal.evalStateT (return 0) (sourceUnfoldrState value n)
16921693
16931694{-# INLINE withState #-}
16941695withState :: S.MonadAsync m => Int -> Int -> SerialT m Int
16951696withState value n =
1696− Internal.evalStateT (0 :: Int) (Internal.liftInner (sourceUnfoldrM value n))
1697+ Internal.evalStateT
1698+ (return (0 :: Int)) (Internal.liftInner (sourceUnfoldrM value n))
16971699
16981700o_1_space_hoisting :: Int -> [Benchmark]
16991701o_1_space_hoisting value =
examples/Split.hs+1−1
@@ -28,7 +28,7 @@ splitFile inHandle =
2828 S.unfold FH.read inHandle
2929 & S.liftInner
3030 & 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
3232 & S.drain
3333
3434main :: IO ()
src/Streamly/Internal/Data/Stream/StreamD.hs+19−16
@@ -839,39 +839,42 @@ liftInner (Stream step state) = Stream step' state
839839 Stop -> Stop
840840
841841{-# INLINE_NORMAL runReaderT #-}
842−runReaderT :: Monad m => s -> Stream (ReaderT s m) a -> Stream m a
843−runReaderT sval (Stream step state) = Stream step' state
842+runReaderT :: Monad m => m s -> Stream (ReaderT s m) a -> Stream m a
843+runReaderT env (Stream step state) = Stream step' (state, env)
844844 where
845845 {-# INLINE_LATE step' #-}
846− step' gst st = do
847− r <- Reader.runReaderT (step (adaptState gst) st) sval
846+ step' gst (st, action) = do
847+ sv <- action
848+ r <- Reader.runReaderT (step (adaptState gst) st) sv
848849 return $ case r of
849− Yield x s -> Yield x s
850− Skip s -> Skip s
850+ Yield x s -> Yield x (s, return sv)
851+ Skip s -> Skip (s, return sv)
851852 Stop -> Stop
852853
853854{-# INLINE_NORMAL evalStateT #-}
854−evalStateT :: Monad m => s -> Stream (StateT s m) a -> Stream m a
855−evalStateT sval (Stream step state) = Stream step' (state, sval)
855+evalStateT :: Monad m => m s -> Stream (StateT s m) a -> Stream m a
856+evalStateT initial (Stream step state) = Stream step' (state, initial)
856857 where
857858 {-# INLINE_LATE step' #-}
858− step' gst (st, sv) = do
859+ step' gst (st, action) = do
860+ sv <- action
859861 (r, sv') <- State.runStateT (step (adaptState gst) st) sv
860862 return $ case r of
861− Yield x s -> Yield x (s, sv')
862− Skip s -> Skip (s, sv')
863+ Yield x s -> Yield x (s, return sv')
864+ Skip s -> Skip (s, return sv')
863865 Stop -> Stop
864866
865867{-# INLINE_NORMAL runStateT #-}
866−runStateT :: Monad m => s -> Stream (StateT s m) a -> Stream m (s, a)
867−runStateT sval (Stream step state) = Stream step' (state, sval)
868+runStateT :: Monad m => m s -> Stream (StateT s m) a -> Stream m (s, a)
869+runStateT initial (Stream step state) = Stream step' (state, initial)
868870 where
869871 {-# INLINE_LATE step' #-}
870− step' gst (st, sv) = do
872+ step' gst (st, action) = do
873+ sv <- action
871874 (r, sv') <- State.runStateT (step (adaptState gst) st) sv
872875 return $ case r of
873− Yield x s -> Yield (sv', x) (s, sv')
874− Skip s -> Skip (s, sv')
876+ Yield x s -> Yield (sv', x) (s, return sv')
877+ Skip s -> Skip (s, return sv')
875878 Stop -> Stop
876879
877880------------------------------------------------------------------------------
src/Streamly/Internal/Prelude.hs+5−5
@@ -4955,7 +4955,7 @@ liftInner xs = fromStreamD $ D.liftInner (toStreamD xs)
49554955-- / Internal/
49564956--
49574957{-# 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
49594959runReaderT s xs = fromStreamD $ D.runReaderT s (toStreamD xs)
49604960
49614961-- | Evaluate the inner monad of a stream as 'StateT'.
@@ -4966,7 +4966,7 @@ runReaderT s xs = fromStreamD $ D.runReaderT s (toStreamD xs)
49664966-- / Internal/
49674967--
49684968{-# 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
49704970evalStateT s xs = fromStreamD $ D.evalStateT s (toStreamD xs)
49714971
49724972-- | Run a stateful (StateT) stream transformation using a given state.
@@ -4979,7 +4979,7 @@ evalStateT s xs = fromStreamD $ D.evalStateT s (toStreamD xs)
49794979{-# INLINE usingStateT #-}
49804980usingStateT
49814981 :: Monad m
4982− => s
4982+ => m s
49834983 -> (SerialT (StateT s m) a -> SerialT (StateT s m) a)
49844984 -> SerialT m a
49854985 -> SerialT m a
@@ -4994,7 +4994,7 @@ usingStateT s f xs = evalStateT s $ f $ liftInner xs
49944994-- / Internal/
49954995--
49964996{-# 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)
49984998runStateT s xs = fromStreamD $ D.runStateT s (toStreamD xs)
49994999
50005000-- | Run a stream transformation using a given environment.
@@ -5004,7 +5004,7 @@ runStateT s xs = fromStreamD $ D.runStateT s (toStreamD xs)
50045004{-# INLINE usingReaderT #-}
50055005usingReaderT
50065006 :: (Monad m, IsStream t)
5007− => r
5007+ => m r
50085008 -> (t (ReaderT r m) a -> t (ReaderT r m) a)
50095009 -> t m a
50105010 -> t m a