on master help sign in
Implement SerialT in terms of Stream
commiteb586c081c40b345b07b8755ea10bd695e2c4354 authorRanjeet Kumar Ranjan <ranjeetr@composewell.com> dateWed 20 Jul 2022, 15:33 (4 years ago) parent0706ac0
30 files changed+167−422
benchmark/Streamly/Benchmark/Data/Array/Foreign/Mut.hs+3−4 benchmark/Streamly/Benchmark/Prelude/Serial/Transformation.hs+2−2 benchmark/lib/Streamly/Benchmark/Common.hs+8−3 core/src/Streamly/Internal/Data/Array.hs+7−6 core/src/Streamly/Internal/Data/Array/Foreign.hs+11−10 core/src/Streamly/Internal/Data/Array/Foreign/Mut.hs+4−4 core/src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+4−3 core/src/Streamly/Internal/Data/Array/Foreign/Type.hs+6−5 core/src/Streamly/Internal/Data/Array/Stream/Mut/Foreign.hs+8−9 core/src/Streamly/Internal/Data/Fold.hs+10−8 core/src/Streamly/Internal/Data/List.hs+8−7 core/src/Streamly/Internal/Data/Parser.hs+4−4 core/src/Streamly/Internal/Data/Ring/Foreign.hs+2−2 core/src/Streamly/Internal/Data/Stream/Bottom.hs+1 core/src/Streamly/Internal/Data/Stream/Serial.hs+11−232 core/src/Streamly/Internal/Data/Stream/Type.hs+1 core/src/Streamly/Internal/Data/Stream/Zip.hs+3−3 src/Streamly/Internal/Data/Array/PrimInclude.hs+12−11 src/Streamly/Internal/Data/Array/Stream/Foreign.hs+10−4 src/Streamly/Internal/Data/SmallArray.hs+6−5 src/Streamly/Internal/Data/Stream/Ahead.hs+2−2 src/Streamly/Internal/Data/Stream/Async.hs+3−3 src/Streamly/Internal/Data/Stream/IsStream/Common.hs+5−52 src/Streamly/Internal/Data/Stream/IsStream/Eliminate.hs+9−22 src/Streamly/Internal/Data/Stream/IsStream/Generate.hs+3−2 src/Streamly/Internal/Data/Stream/IsStream/Type.hs+5−4 src/Streamly/Internal/Data/Stream/Parallel.hs+5−5 src/Streamly/Internal/Data/Stream/SVar/Eliminate.hs+5−4 src/Streamly/Internal/Data/Stream/SVar/Generate.hs+3−2 src/Streamly/Internal/Data/Stream/ZipAsync.hs+6−4
benchmark/Streamly/Benchmark/Data/Array/Foreign/Mut.hs+3−4
@@ -21,7 +21,6 @@ import Control.DeepSeq (NFData(..))
2121import Control.Monad.IO.Class (MonadIO)
2222import Data.Functor ((<&>))
2323import Streamly.Prelude (MonadAsync, SerialT, IsStream)
24−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
2524import System.Random (randomRIO)
2625import Prelude
2726 ( IO
@@ -43,7 +42,7 @@ import Prelude
4342
4443import qualified Streamly.Internal.Data.Array.Foreign as Array
4544import qualified Streamly.Internal.Data.Array.Foreign.Mut as MArray
46−import qualified Streamly.Internal.Data.Stream.StreamD as StreamD
45+import qualified Streamly.Internal.Data.Stream.Type as Stream
4746import qualified Streamly.Prelude as Stream
4847
4948import Gauge
@@ -172,11 +171,11 @@ unfoldReadRevDrain = Stream.drain . Stream.unfold MArray.readRev
172171{-# INLINE toStreamDRevDrain #-}
173172toStreamDRevDrain :: MonadIO m => Stream Int -> m ()
174173toStreamDRevDrain =
175− Stream.drain . SerialT . StreamD.toStreamK . MArray.toStreamDRev
174+ Stream.drain . Stream.fromStreamD . MArray.toStreamDRev
176175
177176{-# INLINE toStreamDDrain #-}
178177toStreamDDrain :: MonadIO m => Stream Int -> m ()
179−toStreamDDrain = Stream.drain . SerialT . StreamD.toStreamK . MArray.toStreamD
178+toStreamDDrain = Stream.drain . Stream.fromStreamD . MArray.toStreamD
180179
181180{-# INLINE unfoldFold #-}
182181unfoldFold :: MonadIO m => Stream Int -> m Int
benchmark/Streamly/Benchmark/Prelude/Serial/Transformation.hs+2−2
@@ -30,7 +30,7 @@ import Test.Inspection
3030#endif
3131
3232import qualified Streamly.Prelude as S
33−import qualified Streamly.Internal.Data.Stream.Serial as Serial
33+import qualified Streamly.Internal.Data.Stream as Stream
3434import qualified Streamly.Internal.Data.Stream.IsStream as Internal
3535import qualified Streamly.Internal.Data.Fold as FL
3636import qualified Streamly.Internal.Data.Unfold as Unfold
@@ -283,7 +283,7 @@ filterMAllIn value n = composeN n $ S.filterM (\x -> return $ x <= (value + 1))
283283
284284{-# INLINE foldFilterEven #-}
285285foldFilterEven :: MonadIO m => Int -> SerialT m Int -> m ()
286−foldFilterEven n = composeN n $ Serial.foldFilter (FL.satisfy even)
286+foldFilterEven n = composeN n $ Stream.foldFilter (FL.satisfy even)
287287
288288{-# INLINE _takeOne #-}
289289_takeOne :: MonadIO m => Int -> SerialT m Int -> m ()
benchmark/lib/Streamly/Benchmark/Common.hs+8−3
@@ -53,7 +53,8 @@ import Control.DeepSeq (NFData(..))
5353import Data.Functor.Identity (Identity, runIdentity)
5454import System.Random (randomRIO)
5555
56−import qualified Streamly.Internal.Data.Stream.Serial as S
56+import qualified Streamly.Internal.Data.Fold as Fold
57+import qualified Streamly.Internal.Data.Stream as S
5758
5859import Gauge
5960
@@ -106,8 +107,12 @@ benchPureSink1 name f =
106107 bench name $ nfIO $ randomRIO (1,1) >>= return . runIdentity . f
107108
108109{-# INLINE benchPureSrc #-}
109−benchPureSrc :: String -> (Int -> S.SerialT Identity a) -> Benchmark
110−benchPureSrc name src = benchPure name src (runIdentity . S.drain)
110+benchPureSrc :: String -> (Int -> S.Stream Identity a) -> Benchmark
111+benchPureSrc name src = benchPure name src (runIdentity . drain)
112+
113+ where
114+
115+ drain = S.fold Fold.drain
111116
112117-------------------------------------------------------------------------------
113118-- String/List generation for read instances
core/src/Streamly/Internal/Data/Array.hs+7−6
@@ -62,7 +62,7 @@ import GHC.Base (Int(..))
6262import GHC.IO (unsafePerformIO)
6363
6464import Streamly.Internal.Data.Fold.Type (Fold(..))
65−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
65+import Streamly.Internal.Data.Stream.Serial (SerialT)
6666import Streamly.Internal.Data.Tuple.Strict (Tuple'(..), Tuple3'(..))
6767import Streamly.Internal.Data.Unfold.Type (Unfold(..))
6868
@@ -70,6 +70,7 @@ import qualified GHC.Exts as Exts
7070import qualified Streamly.Internal.Data.Fold.Type as FL
7171import qualified Streamly.Internal.Data.Ring as RB
7272import qualified Streamly.Internal.Data.Stream.StreamD as D
73+import qualified Streamly.Internal.Data.Stream.Type as Stream
7374
7475import Data.Primitive.Array hiding (fromList, fromListN)
7576import Prelude hiding (foldr, length, read)
@@ -152,13 +153,13 @@ fromStreamD = D.fold write
152153
153154{-# INLINE fromStreamN #-}
154155fromStreamN :: MonadIO m => Int -> SerialT m a -> m (Array a)
155−fromStreamN n (SerialT m) = do
156+fromStreamN n m = do
156157 when (n < 0) $ error "fromStreamN: negative write count specified"
157− fromStreamDN n $ D.fromStreamK m
158+ fromStreamDN n $ Stream.toStreamD m
158159
159160{-# INLINE fromStream #-}
160161fromStream :: MonadIO m => SerialT m a -> m (Array a)
161−fromStream (SerialT m) = fromStreamD $ D.fromStreamK m
162+fromStream = fromStreamD . Stream.toStreamD
162163
163164{-# INLINABLE fromListN #-}
164165fromListN :: Int -> [a] -> Array a
@@ -218,11 +219,11 @@ toStreamDRev arr = D.Stream step (length arr - 1)
218219
219220{-# INLINE_EARLY toStream #-}
220221toStream :: Monad m => Array a -> SerialT m a
221−toStream = SerialT . D.toStreamK . toStreamD
222+toStream = Stream.fromStreamD . toStreamD
222223
223224{-# INLINE_EARLY toStreamRev #-}
224225toStreamRev :: Monad m => Array a -> SerialT m a
225−toStreamRev = SerialT . D.toStreamK . toStreamDRev
226+toStreamRev = Stream.fromStreamD . toStreamDRev
226227
227228-------------------------------------------------------------------------------
228229-- Elimination - using Folds
core/src/Streamly/Internal/Data/Array/Foreign.hs+11−10
@@ -131,7 +131,7 @@ import Streamly.Internal.Data.Array.Foreign.Type
131131 (Array(..), length, asPtrUnsafe)
132132import Streamly.Internal.Data.Fold.Type (Fold(..))
133133import Streamly.Internal.Data.Producer.Type (Producer(..))
134−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
134+import Streamly.Internal.Data.Stream.Serial (SerialT)
135135import Streamly.Internal.Data.Tuple.Strict (Tuple3Fused'(..))
136136import Streamly.Internal.Data.Unfold.Type (Unfold(..))
137137import Streamly.Internal.System.IO (unsafeInlineIO)
@@ -142,10 +142,11 @@ import qualified Streamly.Internal.Data.Array.Foreign.Type as A
142142import qualified Streamly.Internal.Data.Fold as FL
143143import qualified Streamly.Internal.Data.Producer.Type as Producer
144144import qualified Streamly.Internal.Data.Producer as Producer
145+import qualified Streamly.Internal.Data.Ring.Foreign as RB
145146import qualified Streamly.Internal.Data.Stream.Common as P
146147import qualified Streamly.Internal.Data.Stream.StreamD as D
148+import qualified Streamly.Internal.Data.Stream.Type as Stream
147149import qualified Streamly.Internal.Data.Unfold as Unfold
148−import qualified Streamly.Internal.Data.Ring.Foreign as RB
149150
150151-------------------------------------------------------------------------------
151152-- Construction
@@ -158,9 +159,9 @@ import qualified Streamly.Internal.Data.Ring.Foreign as RB
158159-- /Pre-release/
159160{-# INLINE fromStreamN #-}
160161fromStreamN :: (MonadIO m, Storable a) => Int -> SerialT m a -> m (Array a)
161−fromStreamN n (SerialT m) = do
162+fromStreamN n m = do
162163 when (n < 0) $ error "writeN: negative write count specified"
163− A.fromStreamDN n $ D.fromStreamK m
164+ A.fromStreamDN n $ Stream.toStreamD m
164165
165166-- | Create an 'Array' from a stream. This is useful when we want to create a
166167-- single array from a stream of unknown size. 'writeN' is at least twice
@@ -173,7 +174,7 @@ fromStreamN n (SerialT m) = do
173174-- /Pre-release/
174175{-# INLINE fromStream #-}
175176fromStream :: (MonadIO m, Storable a) => SerialT m a -> m (Array a)
176−fromStream (SerialT m) = P.fold A.write m
177+fromStream m = P.fold A.write $ Stream.toStreamK m
177178-- write m = A.fromStreamD $ D.fromStreamK m
178179
179180-------------------------------------------------------------------------------
@@ -371,7 +372,7 @@ getSliceUnsafe index len (Array contents start e) =
371372splitOn :: (Monad m, Storable a) =>
372373 (a -> Bool) -> Array a -> SerialT m (Array a)
373374splitOn predicate arr =
374− SerialT $ D.toStreamK
375+ Stream.fromStreamD
375376 $ fmap (\(i, len) -> getSliceUnsafe i len arr)
376377 $ D.sliceOnSuffix predicate (A.toStreamD arr)
377378
@@ -437,8 +438,8 @@ getIndex i arr =
437438-- /Pre-release/
438439{-# INLINE getIndices #-}
439440getIndices :: (Monad m, Storable a) => SerialT m Int -> Unfold m (Array a) a
440−getIndices (SerialT stream) =
441− let unf = MA.getIndicesD (return . unsafeInlineIO) $ D.fromStreamK stream
441+getIndices m =
442+ let unf = MA.getIndicesD (return . unsafeInlineIO) $ D.fromStreamK $ Stream.toStreamK m
442443 in Unfold.lmap A.unsafeThaw unf
443444
444445-- | Unfolds @(from, then, to, array)@ generating a finite stream whose first
@@ -489,7 +490,7 @@ runPipe f arr = P.runPipe (toArrayMinChunk (length arr)) $ f (A.read arr)
489490streamTransform :: forall m a b. (MonadIO m, Storable a, Storable b)
490491 => (SerialT m a -> SerialT m b) -> Array a -> m (Array b)
491492streamTransform f arr =
492− P.fold (A.writeWith (length arr)) $ getSerialT $ f (A.toStream arr)
493+ P.fold (A.writeWith (length arr)) $ Stream.toStreamK $ f (A.toStream arr)
493494
494495-------------------------------------------------------------------------------
495496-- Casts
@@ -553,7 +554,7 @@ asCStringUnsafe arr act = do
553554-- /Pre-release/
554555{-# INLINE fold #-}
555556fold :: forall m a b. (Monad m, Storable a) => Fold m a b -> Array a -> m b
556−fold f arr = P.fold f (getSerialT (A.toStream arr))
557+fold f arr = P.fold f (Stream.toStreamK (A.toStream arr))
557558
558559-- | Fold an array using a stream fold operation.
559560--
core/src/Streamly/Internal/Data/Array/Foreign/Mut.hs+4−4
@@ -34,10 +34,11 @@ where
3434
3535import Control.Monad.IO.Class (MonadIO(..))
3636import Streamly.Internal.Data.Unboxed (Storable)
37−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
37+import Streamly.Internal.Data.Stream.Serial (SerialT)
3838import Streamly.Internal.Data.Unfold.Type (Unfold(..))
3939
4040import qualified Streamly.Internal.Data.Stream.StreamD as D
41+import qualified Streamly.Internal.Data.Stream.Type as Stream
4142import qualified Streamly.Internal.Data.Unfold as Unfold
4243-- import qualified Streamly.Internal.Data.Stream.Common as P
4344
@@ -52,8 +53,7 @@ import Streamly.Internal.Data.Array.Foreign.Mut.Type
5253splitOn :: (MonadIO m, Storable a) =>
5354 (a -> Bool) -> Array a -> SerialT m (Array a)
5455splitOn predicate arr =
55− SerialT
56− $ D.toStreamK
56+ Stream.fromStreamD
5757 $ fmap (\(i, len) -> getSliceUnsafe i len arr)
5858 $ D.sliceOnSuffix predicate (toStreamD arr)
5959
@@ -99,5 +99,5 @@ getSlicesFromLen from len =
9999-- /Pre-release/
100100{-# INLINE fromStream #-}
101101fromStream :: (MonadIO m, Storable a) => SerialT m a -> m (Array a)
102−fromStream (SerialT m) = fromStreamD $ D.fromStreamK m
102+fromStream = fromStreamD . Stream.toStreamD
103103-- fromStream (SerialT m) = P.fold write m
core/src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+4−3
@@ -253,7 +253,7 @@ import GHC.Ptr (Ptr(..))
253253
254254import Streamly.Internal.Data.Fold.Type (Fold(..))
255255import Streamly.Internal.Data.Producer.Type (Producer (..))
256−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
256+import Streamly.Internal.Data.Stream.Type (Stream)
257257import Streamly.Internal.Data.SVar.Type (adaptState, defState)
258258import Streamly.Internal.Data.Unfold.Type (Unfold(..))
259259import Streamly.Internal.System.IO (arrayPayloadSize, defaultChunkSize)
@@ -266,6 +266,7 @@ import qualified Streamly.Internal.Data.Fold.Type as FL
266266import qualified Streamly.Internal.Data.Producer as Producer
267267import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
268268import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
269+import qualified Streamly.Internal.Data.Stream.Type as Stream
269270#ifdef USE_FOREIGN_PTR
270271import qualified Streamly.Internal.Foreign.Malloc as Malloc
271272#endif
@@ -1127,8 +1128,8 @@ getIndicesD liftio (D.Stream stepi sti) = Unfold step inject
11271128 return D.Stop
11281129
11291130{-# INLINE getIndices #-}
1130−getIndices :: (MonadIO m, Storable a) => SerialT m Int -> Unfold m (Array a) a
1131−getIndices (SerialT stream) = getIndicesD liftIO $ D.fromStreamK stream
1131+getIndices :: (MonadIO m, Storable a) => Stream m Int -> Unfold m (Array a) a
1132+getIndices = getIndicesD liftIO . Stream.toStreamD
11321133
11331134-------------------------------------------------------------------------------
11341135-- Subarrays
core/src/Streamly/Internal/Data/Array/Foreign/Type.hs+6−5
@@ -94,7 +94,7 @@ import GHC.Ptr (Ptr(..))
9494import Streamly.Internal.Data.Array.Foreign.Mut.Type
9595 (ArrayContents, touch)
9696import Streamly.Internal.Data.Fold.Type (Fold(..))
97−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
97+import Streamly.Internal.Data.Stream.Type (Stream)
9898import Streamly.Internal.Data.Unfold.Type (Unfold(..))
9999import Text.Read (readPrec, readListPrec, readListPrecDefault)
100100
@@ -103,6 +103,7 @@ import Prelude hiding (length, foldr, read, unlines, splitAt)
103103import qualified Streamly.Internal.Data.Array.Foreign.Mut.Type as MA
104104import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
105105import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
106+import qualified Streamly.Internal.Data.Stream.Type as Stream
106107import qualified Streamly.Internal.Data.Unfold.Type as Unfold
107108import qualified GHC.Exts as Exts
108109
@@ -493,8 +494,8 @@ toStreamKRev arr =
493494--
494495-- /Pre-release/
495496{-# INLINE_EARLY toStream #-}
496−toStream :: (Monad m, Storable a) => Array a -> SerialT m a
497−toStream = SerialT . D.toStreamK . toStreamD
497+toStream :: (Monad m, Storable a) => Array a -> Stream m a
498+toStream = Stream.fromStreamD . toStreamD
498499-- XXX add fallback to StreamK rule
499500-- {-# RULES "Streamly.Array.read fallback to StreamK" [1]
500501-- forall a. S.readK (read a) = K.fromArray a #-}
@@ -503,8 +504,8 @@ toStream = SerialT . D.toStreamK . toStreamD
503504--
504505-- /Pre-release/
505506{-# INLINE_EARLY toStreamRev #-}
506−toStreamRev :: (Monad m, Storable a) => Array a -> SerialT m a
507−toStreamRev = SerialT . D.toStreamK . toStreamDRev
507+toStreamRev :: (Monad m, Storable a) => Array a -> Stream m a
508+toStreamRev = Stream.fromStreamD . toStreamDRev
508509
509510-- XXX add fallback to StreamK rule
510511-- {-# RULES "Streamly.Array.readRev fallback to StreamK" [1]
core/src/Streamly/Internal/Data/Array/Stream/Mut/Foreign.hs+8−9
@@ -34,13 +34,14 @@ import Data.Bifunctor (first)
3434import Streamly.Internal.Data.Unboxed (Storable, sizeOf)
3535import Streamly.Internal.Data.Array.Foreign.Mut.Type (Array(..))
3636import Streamly.Internal.Data.Fold.Type (Fold(..))
37−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
37+import Streamly.Internal.Data.Stream.Serial (SerialT)
3838import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
3939
4040import qualified Streamly.Internal.Data.Array.Foreign.Mut.Type as MArray
4141import qualified Streamly.Internal.Data.Fold.Type as FL
4242import qualified Streamly.Internal.Data.Stream.StreamD as D
4343import qualified Streamly.Internal.Data.Parser.ParserD as ParserD
44+import qualified Streamly.Internal.Data.Stream.Type as Stream
4445
4546-- | @arraysOf n stream@ groups the elements in the input stream into arrays of
4647-- @n@ elements each.
@@ -53,8 +54,7 @@ import qualified Streamly.Internal.Data.Parser.ParserD as ParserD
5354{-# INLINE arraysOf #-}
5455arraysOf :: (MonadIO m, Storable a)
5556 => Int -> SerialT m a -> SerialT m (Array a)
56−arraysOf n (SerialT xs) =
57− SerialT $ D.toStreamK $ MArray.arraysOf n $ D.fromStreamK xs
57+arraysOf n = Stream.fromStreamD . MArray.arraysOf n . Stream.toStreamD
5858
5959-------------------------------------------------------------------------------
6060-- Compact
@@ -193,8 +193,7 @@ lpackArraysChunksOf n (Fold step1 initial1 extract1) =
193193{-# INLINE compact #-}
194194compact :: (MonadIO m, Storable a)
195195 => Int -> SerialT m (Array a) -> SerialT m (Array a)
196−compact n (SerialT xs) =
197− SerialT $ D.toStreamK $ packArraysChunksOf n (D.fromStreamK xs)
196+compact n = Stream.fromStreamD . packArraysChunksOf n . Stream.toStreamD
198197
199198-- | Coalesce adjacent arrays in incoming stream to form bigger arrays of a
200199-- maximum specified size. Note that if a single array is bigger than the
@@ -301,8 +300,8 @@ compactGEFold n = Fold step initial extract
301300-- /Internal/
302301compactLE :: (MonadThrow m, MonadIO m, Storable a) =>
303302 Int -> SerialT m (Array a) -> SerialT m (Array a)
304−compactLE n (SerialT xs) =
305− SerialT $ D.toStreamK $ D.parseMany (compactLEParserD n) (D.fromStreamK xs)
303+compactLE n =
304+ Stream.fromStreamD . D.parseMany (compactLEParserD n) . Stream.toStreamD
306305
307306-- | Like 'compactLE' but generates arrays of exactly equal to the size
308307-- specified except for the last array in the stream which could be shorter.
@@ -322,5 +321,5 @@ compactEQ _n _xs = undefined
322321compactGE ::
323322 (MonadIO m, Storable a)
324323 => Int -> SerialT m (Array a) -> SerialT m (Array a)
325−compactGE n (SerialT xs) =
326− SerialT $ D.toStreamK $ D.foldMany (compactGEFold n) (D.fromStreamK xs)
324+compactGE n =
325+ Stream.fromStreamD . D.foldMany (compactGEFold n) . Stream.toStreamD
core/src/Streamly/Internal/Data/Fold.hs+10−8
@@ -311,7 +311,7 @@ import Streamly.Internal.Data.IsMap (IsMap(..))
311311import Streamly.Internal.Data.Pipe.Type (Pipe (..), PipeState(..))
312312import Streamly.Internal.Data.Unfold.Type (Unfold(..))
313313import Streamly.Internal.Data.Tuple.Strict (Tuple'(..), Tuple3'(..))
314−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
314+import Streamly.Internal.Data.Stream.Type (Stream)
315315
316316import qualified Data.IntSet as IntSet
317317import qualified Data.Set as Set
@@ -323,6 +323,7 @@ import qualified Streamly.Internal.Data.IsMap as IsMap
323323import qualified Streamly.Internal.Data.Pipe.Type as Pipe
324324import qualified Streamly.Internal.Data.Ring.Foreign as Ring
325325import qualified Streamly.Internal.Data.Stream.StreamD.Type as StreamD
326+import qualified Streamly.Internal.Data.Stream.Type as Stream
326327
327328import Prelude hiding
328329 ( filter, foldl1, drop, dropWhile, take, takeWhile, zipWith
@@ -345,7 +346,8 @@ import Streamly.Internal.Data.Fold.Type
345346-- >>> import qualified Streamly.Internal.Data.Fold.Window as FoldW
346347-- >>> import qualified Streamly.Data.Array.Foreign as Array
347348-- >>> import qualified Streamly.Internal.Data.Parser as Parser
348−-- >>> import Streamly.Internal.Data.Stream.Serial (SerialT(..))
349+-- >>> import qualified Streamly.Internal.Data.Stream.Type as Stream
350+-- >>> import Streamly.Internal.Data.Stream.Type (Stream)
349351-- >>> import Data.IORef (newIORef, readIORef, writeIORef)
350352-- >>> import qualified Streamly.Internal.Data.Array.Foreign.Mut.Type as MA
351353
@@ -2537,12 +2539,12 @@ chunksBetween _low _high _f1 _f2 = undefined
25372539-- /Warning!/ working on large streams accumulated as buffers in memory could
25382540-- be very inefficient, consider using "Streamly.Data.Array" instead.
25392541--
2540−-- >>> toStream = fmap SerialT Fold.toStreamK
2542+-- >>> toStream = fmap Stream.fromStreamK Fold.toStreamK
25412543--
25422544-- /Pre-release/
25432545{-# INLINE toStream #-}
2544−toStream :: Monad m => Fold m a (SerialT n a)
2545−toStream = fmap SerialT toStreamK
2546+toStream :: Monad m => Fold m a (Stream n a)
2547+toStream = fmap Stream.fromStreamK toStreamK
25462548
25472549-- This is more efficient than 'toStream'. toStream is exactly the same as
25482550-- reversing the stream after toStreamRev.
@@ -2550,7 +2552,7 @@ toStream = fmap SerialT toStreamK
25502552-- | Buffers the input stream to a pure stream in the reverse order of the
25512553-- input.
25522554--
2553−-- >>> toStreamRev = fmap SerialT Fold.toStreamKRev
2555+-- >>> toStreamRev = fmap Stream.fromStreamK Fold.toStreamKRev
25542556--
25552557-- /Warning!/ working on large streams accumulated as buffers in memory could
25562558-- be very inefficient, consider using "Streamly.Data.Array" instead.
@@ -2559,8 +2561,8 @@ toStream = fmap SerialT toStreamK
25592561
25602562-- xn : ... : x2 : x1 : []
25612563{-# INLINE toStreamRev #-}
2562−toStreamRev :: Monad m => Fold m a (SerialT n a)
2563−toStreamRev = fmap SerialT toStreamKRev
2564+toStreamRev :: Monad m => Fold m a (Stream n a)
2565+toStreamRev = fmap Stream.fromStreamK toStreamKRev
25642566
25652567-- XXX This does not fuse. It contains a recursive step function. We will need
25662568-- a Skip input constructor in the fold type to make it fuse.
core/src/Streamly/Internal/Data/List.hs+8−7
@@ -69,11 +69,12 @@ import GHC.Exts (IsList(..), IsString(..))
6969#if __GLASGOW_HASKELL__ < 808
7070import Data.Semigroup (Semigroup(..))
7171#endif
72−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
72+import Streamly.Internal.Data.Stream.Serial (SerialT)
7373import Streamly.Internal.Data.Stream.Zip (ZipSerialM(..))
7474
7575import qualified Streamly.Internal.Data.Stream.Serial as Serial
7676import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
77+import qualified Streamly.Internal.Data.Stream.Type as Stream
7778
7879-- We implement list as a newtype instead of a type synonym to make type
7980-- inference easier when using -XOverloadedLists and -XOverloadedStrings. When
@@ -123,8 +124,8 @@ instance IsList (List a) where
123124--
124125-- @since 0.6.0
125126pattern Nil :: List a
126−pattern Nil <- (runIdentity . K.null . getSerialT . toSerial -> True) where
127− Nil = List $ SerialT K.nil
127+pattern Nil <- (runIdentity . K.null . Stream.toStreamK . toSerial -> True) where
128+ Nil = List $ Stream.fromStreamK K.nil
128129
129130infixr 5 `Cons`
130131
@@ -134,8 +135,8 @@ infixr 5 `Cons`
134135-- @since 0.6.0
135136pattern Cons :: a -> List a -> List a
136137pattern Cons x xs <-
137− (fmap (second (List . SerialT))
138− . runIdentity . K.uncons . getSerialT . toSerial
138+ (fmap (second (List . Stream.fromStreamK))
139+ . runIdentity . K.uncons . Stream.toStreamK . toSerial
139140 -> Just (x, xs)
140141 )
141142
@@ -176,10 +177,10 @@ instance IsList (ZipList a) where
176177--
177178-- @since 0.6.0
178179fromZipList :: ZipList a -> List a
179−fromZipList (ZipList zs) = List $ SerialT $ getZipSerialM zs
180+fromZipList (ZipList zs) = List $ Stream.fromStreamK $ getZipSerialM zs
180181
181182-- | Convert a regular 'List' to a 'ZipList'
182183--
183184-- @since 0.6.0
184185toZipList :: List a -> ZipList a
185−toZipList = ZipList . ZipSerialM . getSerialT . toSerial
186+toZipList = ZipList . ZipSerialM . Stream.toStreamK . toSerial
core/src/Streamly/Internal/Data/Parser.hs+4−4
@@ -248,12 +248,12 @@ import Prelude hiding
248248
249249import Streamly.Internal.Data.Fold.Type (Fold(..))
250250import Streamly.Internal.Data.Parser.ParserK.Type (Parser)
251−import Streamly.Internal.Data.Stream.Serial (SerialT(getSerialT))
251+import Streamly.Internal.Data.Stream.Type (Stream)
252252
253253import qualified Streamly.Internal.Data.Fold.Type as FL
254254import qualified Streamly.Internal.Data.Parser.ParserD as D
255255import qualified Streamly.Internal.Data.Parser.ParserK.Type as K
256−import qualified Streamly.Internal.Data.Stream.StreamD.Type as SD
256+import qualified Streamly.Internal.Data.Stream.Type as Stream
257257
258258--
259259-- $setup
@@ -1227,9 +1227,9 @@ takeP i p = D.toParserK $ D.takeP i $ D.fromParserK p
12271227{-# INLINE concatSequence #-}
12281228concatSequence ::
12291229 MonadCatch m =>
1230− Fold m b c -> SerialT m (Parser m a b) -> Parser m a c
1230+ Fold m b c -> Stream m (Parser m a b) -> Parser m a c
12311231concatSequence f p =
1232− let sp = fmap D.fromParserK $ SD.fromStreamK $ getSerialT p
1232+ let sp = fmap D.fromParserK $ Stream.toStreamD p
12331233 in D.toParserK $ D.sequence f sp
12341234
12351235-- | Map a 'Parser' returning function on the result of a 'Parser'.
core/src/Streamly/Internal/Data/Ring/Foreign.hs+2−2
@@ -85,7 +85,7 @@ import GHC.ForeignPtr (mallocPlainForeignPtrAlignedBytes)
8585import GHC.Ptr (Ptr(..))
8686import Streamly.Internal.Data.Array.Foreign.Mut.Type (Array, memcmp)
8787import Streamly.Internal.Data.Fold.Type (Fold(..), Step(..), lmap)
88−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
88+import Streamly.Internal.Data.Stream.Type (Stream)
8989import Streamly.Internal.Data.Stream.StreamD.Step (Step(..))
9090import Streamly.Internal.Data.Unfold.Type (Unfold(..))
9191import Streamly.Internal.System.IO (unsafeInlineIO)
@@ -354,7 +354,7 @@ readRev = undefined
354354-- /Unimplemented/
355355{-# INLINE_NORMAL ringsOf #-}
356356ringsOf :: -- forall m a. (MonadIO m, Storable a) =>
357− Int -> SerialT m a -> SerialT m (Array a)
357+ Int -> Stream m a -> Stream m (Array a)
358358ringsOf = undefined -- Stream.scan (writeN n)
359359
360360-------------------------------------------------------------------------------
core/src/Streamly/Internal/Data/Stream/Bottom.hs+1
@@ -122,6 +122,7 @@ foldContinue f s = D.foldContinue f $ toStreamD s
122122--
123123-- >>> fold f = Stream.parse (Parser.fromFold f)
124124--
125+-- @since 0.9.0
125126{-# INLINE fold #-}
126127fold :: Monad m => Fold m a b -> Stream m a -> m b
127128fold fl strm = D.fold fl $ D.fromStreamK $ toStreamK strm
core/src/Streamly/Internal/Data/Stream/Serial.hs+11−232
@@ -1,6 +1,3 @@
1−{-# LANGUAGE UndecidableInstances #-}
2−{-# OPTIONS_GHC -Wno-orphans #-}
3−
41-- |
52-- Module : Streamly.Internal.Data.Stream.Serial
63-- Copyright : (c) 2017 Composewell Technologies
@@ -17,64 +14,36 @@
1714module Streamly.Internal.Data.Stream.Serial
1815 (
1916 -- * Serial appending stream
20− SerialT(..)
17+ SerialT
2118 , Serial
2219 , serial
2320
2421 -- * Construction
25− , cons
26− , consM
22+ , Stream.cons
23+ , Stream.consM
2724 , repeat
2825 , unfoldrM
2926 , fromList
30− , list
3127
3228 -- * Elimination
3329 , toList
34− , foldWith
35− , drain
3630
3731 -- * Transformation
3832 , map
39− , mapM
40− , filter
41− , foldFilter
33+ , Stream.mapM
4234 )
4335where
4436
45−import Control.Applicative (liftA2)
46−import Control.DeepSeq (NFData(..), NFData1(..))
47−import Control.Monad.Base (MonadBase(..), liftBaseDefault)
48−import Control.Monad.Catch (MonadThrow, throwM)
49−import Control.Monad.IO.Class (MonadIO(..))
50−import Control.Monad.Reader.Class (MonadReader(..))
51−import Control.Monad.State.Class (MonadState(..))
52−import Control.Monad.Trans.Class (MonadTrans(lift))
53−import Data.Foldable (Foldable(foldl'), fold)
54−import Data.Functor.Identity (Identity(..), runIdentity)
55−import Data.Maybe (fromMaybe)
56−import Data.Semigroup (Endo(..))
5737#if __GLASGOW_HASKELL__ < 808
5838import Data.Semigroup (Semigroup(..))
5939#endif
60−import GHC.Exts (IsList(..), IsString(..), oneShot)
61−import Text.Read
62− ( Lexeme(Ident), lexP, parens, prec, readPrec, readListPrec
63− , readListPrecDefault)
64−import Streamly.Internal.BaseCompat ((#.))
65−import Streamly.Internal.Data.Fold.Type (Fold)
66−import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
67−import Streamly.Internal.Data.Stream.StreamK.Type (Stream)
40+import GHC.Exts (IsList(..))
6841
69−import qualified Streamly.Internal.Data.Stream.Common as P
7042import qualified Streamly.Internal.Data.Stream.StreamD.Generate as D
71−import qualified Streamly.Internal.Data.Stream.StreamD.Transform as D
72−import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
73−import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
43+import qualified Streamly.Internal.Data.Stream as Stream
7444
75−import Prelude hiding (map, mapM, repeat, filter)
45+import Prelude hiding (map, mapM, repeat)
7646
77−#include "Instances.hs"
7847#include "inline.hs"
7948
8049-- $setup
@@ -84,38 +53,7 @@ import Prelude hiding (map, mapM, repeat, filter)
8453-- SerialT
8554------------------------------------------------------------------------------
8655
87−-- | For 'SerialT' streams:
88−--
89−-- @
90−-- (<>) = 'Streamly.Prelude.serial' -- 'Semigroup'
91−-- (>>=) = flip . 'Streamly.Prelude.concatMapWith' 'Streamly.Prelude.serial' -- 'Monad'
92−-- @
93−--
94−-- A single 'Monad' bind behaves like a @for@ loop:
95−--
96−-- >>> :{
97−-- Stream.toList $ do
98−-- x <- Stream.fromList [1,2] -- foreach x in stream
99−-- return x
100−-- :}
101−-- [1,2]
102−--
103−-- Nested monad binds behave like nested @for@ loops:
104−--
105−-- >>> :{
106−-- Stream.toList $ do
107−-- x <- Stream.fromList [1,2] -- foreach x in stream
108−-- y <- Stream.fromList [3,4] -- foreach y in stream
109−-- return (x, y)
110−-- :}
111−-- [(1,3),(1,4),(2,3),(2,4)]
112−--
113−-- /Since: 0.2.0 ("Streamly")/
114−--
115−-- @since 0.8.0
116−newtype SerialT m a = SerialT {getSerialT :: Stream m a}
117− -- XXX when deriving do we inherit an INLINE?
118− deriving (Semigroup, Monoid, MonadTrans)
56+type SerialT = Stream.Stream
11957
12058-- | A serial IO stream of elements of type @a@. See 'SerialT' documentation
12159-- for more details.
@@ -129,21 +67,12 @@ type Serial = SerialT IO
12967-- Generation
13068------------------------------------------------------------------------------
13169
132−{-# INLINE cons #-}
133−cons :: a -> SerialT m a -> SerialT m a
134−cons x (SerialT ms) = SerialT $ K.cons x ms
135−
136−{-# INLINE consM #-}
137−{-# SPECIALIZE consM :: IO a -> SerialT IO a -> SerialT IO a #-}
138−consM :: Monad m => m a -> SerialT m a -> SerialT m a
139−consM m (SerialT ms) = SerialT $ K.consM m ms
140−
14170-- |
14271-- Generate an infinite stream by repeating a pure value.
14372--
14473{-# INLINE_NORMAL repeat #-}
14574repeat :: Monad m => a -> SerialT m a
146−repeat = SerialT . D.toStreamK . D.repeat
75+repeat = Stream.fromStreamD . D.repeat
14776
14877------------------------------------------------------------------------------
14978-- Combining
@@ -153,37 +82,6 @@ repeat = SerialT . D.toStreamK . D.repeat
15382serial :: SerialT m a -> SerialT m a -> SerialT m a
15483serial = (<>)
15584
156−------------------------------------------------------------------------------
157−-- Monad
158−------------------------------------------------------------------------------
159−
160−instance Monad m => Monad (SerialT m) where
161− return = pure
162−
163− -- Benchmarks better with StreamD bind and pure:
164− -- toList, filterAllout, *>, *<, >> (~2x)
165− --
166− -- pure = SerialT . D.fromStreamD . D.fromPure
167− -- m >>= f = D.fromStreamD $ D.concatMap (D.toStreamD . f) (D.toStreamD m)
168−
169− -- Benchmarks better with CPS bind and pure:
170− -- Prime sieve (25x)
171− -- n binds, breakAfterSome, filterAllIn, state transformer (~2x)
172− --
173− {-# INLINE (>>=) #-}
174− (>>=) (SerialT m) f = SerialT $ K.bindWith K.serial m (getSerialT . f)
175−
176− {-# INLINE (>>) #-}
177− (>>) = (*>)
178−
179−------------------------------------------------------------------------------
180−-- Other instances
181−------------------------------------------------------------------------------
182−
183−{-# INLINE mapM #-}
184−mapM :: Monad m => (a -> m b) -> SerialT m a -> SerialT m b
185−mapM f (SerialT m) = SerialT $ D.toStreamK $ D.mapM f $ D.fromStreamK m
186−
18785-- |
18886-- @
18987-- map = fmap
@@ -199,120 +97,7 @@ mapM f (SerialT m) = SerialT $ D.toStreamK $ D.mapM f $ D.fromStreamK m
19997-- @since 0.4.0
20098{-# INLINE map #-}
20199map :: Monad m => (a -> b) -> SerialT m a -> SerialT m b
202−map f = mapM (return . f)
203−
204−{-# INLINE apSerial #-}
205−apSerial :: Monad m => SerialT m (a -> b) -> SerialT m a -> SerialT m b
206−apSerial (SerialT m1) (SerialT m2) =
207− SerialT $ D.toStreamK $ D.fromStreamK m1 <*> D.fromStreamK m2
208−
209−{-# INLINE apSequence #-}
210−apSequence :: Monad m => SerialT m a -> SerialT m b -> SerialT m b
211−apSequence (SerialT m1) (SerialT m2) =
212− SerialT $ D.toStreamK $ D.fromStreamK m1 *> D.fromStreamK m2
213−
214−{-# INLINE apDiscardSnd #-}
215−apDiscardSnd :: Monad m => SerialT m a -> SerialT m b -> SerialT m a
216−apDiscardSnd (SerialT m1) (SerialT m2) =
217− SerialT $ D.toStreamK $ D.fromStreamK m1 <* D.fromStreamK m2
218−
219−-- Note: we need to define all the typeclass operations because we want to
220−-- INLINE them.
221−instance Monad m => Applicative (SerialT m) where
222− {-# INLINE pure #-}
223− pure = SerialT . K.fromPure
224−
225− {-# INLINE (<*>) #-}
226− (<*>) = apSerial
227− -- (<*>) = K.apSerial
228−
229− {-# INLINE liftA2 #-}
230− liftA2 f x = (<*>) (fmap f x)
231−
232− {-# INLINE (*>) #-}
233− (*>) = apSequence
234− -- (*>) = K.apSerialDiscardFst
235−
236− {-# INLINE (<*) #-}
237− (<*) = apDiscardSnd
238− -- (<*) = K.apSerialDiscardSnd
239−
240−MONAD_COMMON_INSTANCES(SerialT,)
241−LIST_INSTANCES(SerialT)
242−NFDATA1_INSTANCE(SerialT)
243−FOLDABLE_INSTANCE(SerialT)
244−TRAVERSABLE_INSTANCE(SerialT)
245−
246−{-# INLINE toStreamD #-}
247−toStreamD :: Applicative m => SerialT m a -> D.Stream m a
248−toStreamD (SerialT m) = D.fromStreamK m
249−
250−{-# INLINE fromStreamD #-}
251−fromStreamD :: Monad m => D.Stream m a -> SerialT m a
252−fromStreamD m = SerialT $ D.toStreamK m
253−
254−-- XXX We should only export generation and combinators from this module.
255−--
256−-- | Include only those elements that pass a predicate.
257−--
258−{-# INLINE filter #-}
259−filter :: Monad m => (a -> Bool) -> SerialT m a -> SerialT m a
260−filter p = fromStreamD . D.filter p . toStreamD
261−
262−-- | Use a filtering fold on a stream.
263−--
264−-- > Stream.sum $ Stream.foldFilter (Fold.satisfy (> 5)) $ Stream.fromList [1..10]
265−-- 40
266−--
267−{-# INLINE foldFilter #-}
268−foldFilter :: Monad m => Fold m a (Maybe b) -> SerialT m a -> SerialT m b
269−foldFilter p = fromStreamD . D.foldFilter p . toStreamD
270−
271−-- XXX Renamed to foldWith because SerialT has a Foldable instance having
272−-- method fold.
273−--
274−-- | Fold a stream using the supplied left 'Fold' and reducing the resulting
275−-- expression strictly at each step. The behavior is similar to 'foldl''. A
276−-- 'Fold' can terminate early without consuming the full stream. See the
277−-- documentation of individual 'Fold's for termination behavior.
278−--
279−-- >>> Stream.foldWith Fold.sum (Stream.enumerateFromTo 1 100)
280−-- 5050
281−--
282−-- Folds never fail, therefore, they produce a default value even when no input
283−-- is provided. It means we can always fold an empty stream and get a valid
284−-- result. For example:
285−--
286−-- >>> Stream.foldWith Fold.sum Stream.nil
287−-- 0
288−--
289−-- However, 'foldMany' on an empty stream results in an empty stream.
290−-- Therefore, @Stream.foldWith f@ is not the same as @Stream.head . Stream.foldMany
291−-- f@.
292−--
293−-- @foldWith f = Stream.parse (Parser.fromFold f)@
294−--
295−-- /Pre-release/
296−{-# INLINE foldWith #-}
297−foldWith :: Monad m => Fold m a b -> SerialT m a -> m b
298−foldWith fld m = D.fold fld $ toStreamD m
299−
300−-- XXX Renamed to "list" because fromList is present in IsList instance.
301−--
302−-- |
303−-- @
304−-- fromList = 'Prelude.foldr' 'K.cons' 'K.nil'
305−-- @
306−--
307−-- Construct a stream from a list of pure values. This is more efficient than
308−-- 'K.fromFoldable' for serial streams.
309−--
310−-- @since 0.4.0
311−{-# INLINE_EARLY list #-}
312−list :: Monad m => [a] -> SerialT m a
313−list = fromStreamD . D.fromList
314−{-# RULES "list fallback to StreamK" [1]
315− forall a. D.toStreamK (D.fromList a) = K.fromFoldable a #-}
100+map f = Stream.mapM (return . f)
316101
317102------------------------------------------------------------------------------
318103-- Construction
@@ -341,10 +126,4 @@ list = fromStreamD . D.fromList
341126--
342127{-# INLINE unfoldrM #-}
343128unfoldrM :: Monad m => (b -> m (Maybe (a, b))) -> b -> SerialT m a
344−unfoldrM step seed = SerialT $ D.toStreamK (D.unfoldrM step seed)
345−
346−{-# INLINE_EARLY drain #-}
347−drain :: Monad m => SerialT m a -> m ()
348−drain (SerialT m) = D.drain $ D.fromStreamK m
349−{-# RULES "drain fallback to CPS" [1]
350− forall a. D.drain (D.fromStreamK a) = K.drain a #-}
129+unfoldrM step seed = Stream.fromStreamD (D.unfoldrM step seed)
core/src/Streamly/Internal/Data/Stream/Type.hs+1
@@ -212,6 +212,7 @@ instance Monad m => Applicative (Stream m) where
212212 (<*) = apDiscardSnd
213213 -- (<*) = K.apSerialDiscardSnd
214214
215+-- XXX Need to remove the MonadBase instance
215216MONAD_COMMON_INSTANCES(Stream,)
216217LIST_INSTANCES(Stream)
217218NFDATA1_INSTANCE(Stream)
core/src/Streamly/Internal/Data/Stream/Zip.hs+3−3
@@ -38,7 +38,6 @@ import Text.Read
3838 , readListPrecDefault)
3939import Streamly.Internal.BaseCompat ((#.))
4040import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
41−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
4241import Streamly.Internal.Data.Stream.StreamK.Type (Stream)
4342
4443import qualified Streamly.Internal.Data.Stream.Common as P
@@ -46,6 +45,7 @@ import qualified Streamly.Internal.Data.Stream.Common as P
4645import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
4746import qualified Streamly.Internal.Data.Stream.StreamD as D
4847import qualified Streamly.Internal.Data.Stream.Serial as Serial
48+import qualified Streamly.Internal.Data.Stream.Type as Stream
4949
5050import Prelude hiding (map, repeat, zipWith)
5151
@@ -105,10 +105,10 @@ NFDATA1_INSTANCE(ZipSerialM)
105105
106106instance Monad m => Functor (ZipSerialM m) where
107107 {-# INLINE fmap #-}
108− fmap f (ZipSerialM m) = ZipSerialM $ getSerialT $ fmap f (SerialT m)
108+ fmap f (ZipSerialM m) = ZipSerialM $ Stream.toStreamK $ fmap f (Stream.fromStreamK m)
109109
110110instance Monad m => Applicative (ZipSerialM m) where
111− pure = ZipSerialM . getSerialT . Serial.repeat
111+ pure = ZipSerialM . Stream.toStreamK . Serial.repeat
112112
113113 {-# INLINE (<*>) #-}
114114 ZipSerialM m1 <*> ZipSerialM m2 = ZipSerialM $ zipWithK id m1 m2
src/Streamly/Internal/Data/Array/PrimInclude.hs+12−11
@@ -9,12 +9,13 @@ import Control.Monad.IO.Class (MonadIO(..))
99import Data.Primitive.Types (Prim(..))
1010import Streamly.Internal.Data.Fold.Type (Fold(..))
1111import Streamly.Internal.Data.Unfold.Type (Unfold(..))
12−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
12+import Streamly.Internal.Data.Stream.Serial (SerialT)
1313
1414import qualified Streamly.Internal.Data.Stream.Common as P
1515import qualified Streamly.Internal.Data.Stream.StreamD as D
1616
1717import Prelude hiding (length, null, last, map, (!!), read, concat)
18+import qualified Streamly.Internal.Data.Stream.Type as Stream
1819
1920-------------------------------------------------------------------------------
2021-- Construction
@@ -27,9 +28,9 @@ import Prelude hiding (length, null, last, map, (!!), read, concat)
2728-- /Pre-release/
2829{-# INLINE fromStreamN #-}
2930fromStreamN :: (MonadIO m, Prim a) => Int -> SerialT m a -> m (Array a)
30−fromStreamN n (SerialT m) = do
31+fromStreamN n m = do
3132 when (n < 0) $ error "writeN: negative write count specified"
32− A.fromStreamDN n $ D.fromStreamK m
33+ A.fromStreamDN n $ D.fromStreamK $ Stream.toStreamK m
3334
3435-- | Create an 'Array' from a stream. This is useful when we want to create a
3536-- single array from a stream of unknown size. 'writeN' is at least twice
@@ -42,7 +43,7 @@ fromStreamN n (SerialT m) = do
4243-- /Pre-release/
4344{-# INLINE fromStream #-}
4445fromStream :: (MonadIO m, Prim a) => SerialT m a -> m (Array a)
45−fromStream (SerialT m) = P.fold A.write m
46+fromStream m = P.fold A.write $ Stream.toStreamK m
4647-- write m = A.fromStreamD $ D.toStreamD m
4748
4849-------------------------------------------------------------------------------
@@ -54,7 +55,7 @@ fromStream (SerialT m) = P.fold A.write m
5455-- /Pre-release/
5556{-# INLINE_EARLY toStream #-}
5657toStream :: (MonadIO m, Prim a) => Array a -> SerialT m a
57−toStream = SerialT . D.toStreamK . A.toStreamD
58+toStream = Stream.fromStreamK . D.toStreamK . A.toStreamD
5859-- XXX add fallback to StreamK rule
5960-- {-# RULES "Streamly.Array.read fallback to StreamK" [1]
6061-- forall a. S.readK (read a) = K.fromArray a #-}
@@ -64,7 +65,7 @@ toStream = SerialT . D.toStreamK . A.toStreamD
6465-- /Pre-release/
6566{-# INLINE_EARLY toStreamRev #-}
6667toStreamRev :: (MonadIO m, Prim a) => Array a -> SerialT m a
67−toStreamRev = SerialT . D.toStreamK . A.toStreamDRev
68+toStreamRev = Stream.fromStreamK . D.toStreamK . A.toStreamDRev
6869-- XXX add fallback to StreamK rule
6970-- {-# RULES "Streamly.Array.readRev fallback to StreamK" [1]
7071-- forall a. S.toStreamK (readRev a) = K.revFromArray a #-}
@@ -125,7 +126,7 @@ null arr = length arr == 0
125126-- /Pre-release/
126127{-# INLINE fold #-}
127128fold :: forall m a b. (MonadIO m, Prim a) => Fold m a b -> Array a -> m b
128−fold f arr = P.fold f (getSerialT (toStream arr))
129+fold f arr = P.fold f (Stream.toStreamK (toStream arr))
129130
130131-- | Fold an array using a stream fold operation.
131132--
@@ -170,8 +171,8 @@ last arr = readIndex arr (length arr - 1)
170171concat :: (MonadIO m, Prim a) => SerialT m (Array a) -> SerialT m a
171172-- concat m = D.fromStreamD $ A.flattenArrays (D.toStreamD m)
172173-- concat m = D.fromStreamD $ D.concatMap A.toStreamD (D.toStreamD m)
173−concat (SerialT m) =
174− SerialT $ D.toStreamK $ D.unfoldMany read (D.fromStreamK m)
174+concat m =
175+ Stream.fromStreamK $ D.toStreamK $ D.unfoldMany read (D.fromStreamK $ Stream.toStreamK m)
175176
176177-- | Coalesce adjacent arrays in incoming stream to form bigger arrays of a
177178-- maximum specified size in bytes.
@@ -180,5 +181,5 @@ concat (SerialT m) =
180181{-# INLINE compact #-}
181182compact ::
182183 (MonadIO m, Prim a) => Int -> SerialT m (Array a) -> SerialT m (Array a)
183−compact n (SerialT xs) =
184− SerialT $ D.toStreamK $ A.packArraysChunksOf n (D.fromStreamK xs)
184+compact n m =
185+ Stream.fromStreamK $ D.toStreamK $ A.packArraysChunksOf n (D.fromStreamK $ Stream.toStreamK m)
src/Streamly/Internal/Data/Array/Stream/Foreign.hs+10−4
@@ -74,7 +74,7 @@ import Streamly.Internal.Data.Array.Foreign.Type (Array(..))
7474import Streamly.Internal.Data.Array.Stream.Fold.Foreign (ArrayFold(..))
7575import Streamly.Internal.Data.Fold.Type (Fold(..))
7676import Streamly.Internal.Data.Parser (ParseError(..))
77−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
77+import Streamly.Internal.Data.Stream.Serial (SerialT)
7878import Streamly.Internal.Data.Stream.IsStream.Type
7979 (IsStream, fromStreamD, toStreamD)
8080import Streamly.Internal.Data.SVar (adaptState, defState)
@@ -94,6 +94,7 @@ import qualified Streamly.Internal.Data.Parser.ParserD as PRD
9494import qualified Streamly.Internal.Data.Stream.IsStream as S
9595import qualified Streamly.Internal.Data.Stream.StreamD as D
9696import qualified Streamly.Internal.Data.Stream.StreamK as K
97+import qualified Streamly.Internal.Data.Stream.Type as Stream
9798
9899-- XXX Since these are immutable arrays MonadIO constraint can be removed from
99100-- most places.
@@ -408,7 +409,10 @@ foldBreak ::
408409 -> SerialT m (A.Array a)
409410 -> m (b, SerialT m (A.Array a))
410411-- foldBreak f s = fmap fromStreamD <$> foldBreakD f (toStreamD s)
411−foldBreak f (SerialT s) = fmap (fmap SerialT) $ foldBreakK f s
412+foldBreak f =
413+ fmap (fmap Stream.fromStreamK)
414+ . foldBreakK f
415+ . Stream.toStreamK
412416-- If foldBreak performs better than runArrayFoldBreak we can use a rewrite
413417-- rule to rewrite runArrayFoldBreak to fold.
414418-- foldBreak f = runArrayFoldBreak (ArrayFold.fromFold f)
@@ -739,8 +743,10 @@ parseBreak ::
739743parseBreak p s =
740744 fmap fromStreamD <$> parseBreakD (PRD.fromParserK p) (toStreamD s)
741745-}
742−parseBreak p (SerialT s) =
743− fmap (fmap SerialT) $ parseBreakK (PRD.fromParserK p) s
746+parseBreak p =
747+ fmap (fmap Stream.fromStreamK)
748+ . parseBreakK (PRD.fromParserK p)
749+ . Stream.toStreamK
744750
745751-------------------------------------------------------------------------------
746752-- Elimination - Running Array Folds and parsers
src/Streamly/Internal/Data/SmallArray.hs+6−5
@@ -53,10 +53,11 @@ import Streamly.Internal.Data.SmallArray.Type
5353import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
5454import Streamly.Internal.Data.Unfold.Type (Unfold(..))
5555import Streamly.Internal.Data.Fold.Type (Fold(..))
56−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
56+import Streamly.Internal.Data.Stream.Serial (SerialT)
5757
5858import qualified Streamly.Internal.Data.Stream.StreamD as D
5959import qualified Streamly.Internal.Data.Fold.Type as FL
60+import qualified Streamly.Internal.Data.Stream.Type as Stream
6061
6162{-# NOINLINE bottomElement #-}
6263bottomElement :: a
@@ -161,17 +162,17 @@ instance NFData a => NFData (SmallArray a) where
161162-- For optimal performance use this with @n@ <= 128.
162163{-# INLINE fromStreamN #-}
163164fromStreamN :: MonadIO m => Int -> SerialT m a -> m (SmallArray a)
164−fromStreamN n (SerialT m) = do
165+fromStreamN n m = do
165166 when (n < 0) $ error "fromStreamN: negative write count specified"
166− fromStreamDN n $ D.fromStreamK m
167+ fromStreamDN n $ D.fromStreamK $ Stream.toStreamK m
167168
168169{-# INLINE_EARLY toStream #-}
169170toStream :: Monad m => SmallArray a -> SerialT m a
170−toStream = SerialT . D.toStreamK . toStreamD
171+toStream = Stream.fromStreamK . D.toStreamK . toStreamD
171172
172173{-# INLINE_EARLY toStreamRev #-}
173174toStreamRev :: Monad m => SmallArray a -> SerialT m a
174−toStreamRev = SerialT . D.toStreamK . toStreamDRev
175+toStreamRev = Stream.fromStreamK . D.toStreamK . toStreamDRev
175176
176177{-# INLINE fold #-}
177178fold :: Monad m => Fold m a b -> SmallArray a -> m b
src/Streamly/Internal/Data/Stream/Ahead.hs+2−2
@@ -51,11 +51,11 @@ import qualified Data.Heap as H
5151
5252import Streamly.Internal.Control.Concurrent
5353 (MonadRunInIO, MonadAsync, RunInIO(..), askRunInIO, restoreM)
54−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
5554import Streamly.Internal.Data.Stream.StreamK.Type (Stream)
5655
5756import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
5857import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
58+import qualified Streamly.Internal.Data.Stream.Type as Stream
5959
6060import Streamly.Internal.Data.Stream.SVar.Generate
6161import Streamly.Internal.Data.SVar
@@ -606,7 +606,7 @@ forkSVarAhead :: MonadAsync m => Stream m a -> Stream m a -> Stream m a
606606forkSVarAhead m1 m2 = K.mkStream $ \st yld sng stp -> do
607607 sv <- newAheadVar st (concurrently m1 m2)
608608 workLoopAhead
609− K.foldStream st yld sng stp $ getSerialT (fromSVar sv)
609+ K.foldStream st yld sng stp $ Stream.toStreamK (fromSVar sv)
610610 where
611611 concurrently ma mb = K.mkStream $ \st yld sng stp -> do
612612 runInIO <- askRunInIO
src/Streamly/Internal/Data/Stream/Async.hs+3−3
@@ -61,12 +61,12 @@ import Streamly.Internal.Control.Concurrent
6161 (MonadRunInIO, MonadAsync, RunInIO(..), askRunInIO, restoreM)
6262import Streamly.Internal.Data.Atomics
6363 (atomicModifyIORefCAS, atomicModifyIORefCAS_)
64−import Streamly.Internal.Data.Stream.Serial (SerialT (..))
6564import Streamly.Internal.Data.Stream.StreamK.Type (Stream)
6665import Streamly.Internal.Data.Stream.SVar.Generate (fromSVar, fromSVarD)
6766
6867import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
6968import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
69+import qualified Streamly.Internal.Data.Stream.Type as Stream
7070
7171import Streamly.Internal.Data.SVar.Type
7272import Streamly.Internal.Data.SVar
@@ -545,7 +545,7 @@ newAsyncVar st m = do
545545mkAsyncK :: MonadAsync m => Stream m a -> Stream m a
546546mkAsyncK m = K.mkStream $ \st yld sng stp -> do
547547 sv <- newAsyncVar (adaptState st) m
548− K.foldStream st yld sng stp $ getSerialT $ fromSVar sv
548+ K.foldStream st yld sng stp $ Stream.toStreamK $ fromSVar sv
549549
550550--
551551-- This is slightly faster than the CPS version above
@@ -649,7 +649,7 @@ forkSVarAsync style m1 m2 = K.mkStream $ \st yld sng stp -> do
649649 AsyncVar -> newAsyncVar st (concurrently m1 m2)
650650 WAsyncVar -> newWAsyncVar st (concurrently m1 m2)
651651 _ -> error "illegal svar type"
652− K.foldStream st yld sng stp $ getSerialT $ fromSVar sv
652+ K.foldStream st yld sng stp $ Stream.toStreamK $ fromSVar sv
653653 where
654654 concurrently ma mb = K.mkStream $ \st yld sng stp -> do
655655 runInIO <- askRunInIO
src/Streamly/Internal/Data/Stream/IsStream/Common.hs+5−52
@@ -23,8 +23,8 @@ module Streamly.Internal.Data.Stream.IsStream.Common
2323
2424 -- * Elimination
2525 , foldContinue
26− , fold
27− , foldBreak
26+ , Stream.fold
27+ , Stream.foldBreak
2828
2929 -- * Transformation
3030 , map
@@ -76,22 +76,22 @@ import Streamly.Internal.Data.Fold.Type (Fold (..))
7676import Streamly.Internal.Data.Stream.IsStream.Combinators (maxYields)
7777import Streamly.Internal.Data.Stream.IsStream.Type
7878 (IsStream(..), fromStreamD, toStreamD, fromStreamS, toStreamS)
79−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
79+import Streamly.Internal.Data.Stream.Serial (SerialT)
8080import Streamly.Internal.Data.Time.Units (AbsTime, RelTime64, addToAbsTime64)
81+import Streamly.Internal.System.IO (defaultChunkSize)
8182
8283import qualified Streamly.Internal.Data.Array.Foreign.Type as A
8384import qualified Streamly.Internal.Data.Stream.Async as Async
8485import qualified Streamly.Internal.Data.Stream.IsStream.Type as IsStream
8586import qualified Streamly.Internal.Data.Stream.Parallel as Par
8687import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
87−import qualified Streamly.Internal.Data.Stream.StreamK as K
8888import qualified Streamly.Internal.Data.Stream.StreamD as D
8989#ifdef USE_STREAMK_ONLY
9090import qualified Streamly.Internal.Data.Stream.StreamK as S
9191#else
9292import qualified Streamly.Internal.Data.Stream.StreamD as S
9393#endif
94−import Streamly.Internal.System.IO (defaultChunkSize)
94+import qualified Streamly.Internal.Data.Stream as Stream
9595
9696import Prelude hiding (take, takeWhile, drop, reverse, concatMap, map, zipWith)
9797
@@ -285,53 +285,6 @@ relTimesWith = fmap snd . timesWith
285285foldContinue :: Monad m => Fold m a b -> SerialT m a -> Fold m a b
286286foldContinue f s = D.foldContinue f $ IsStream.toStreamD s
287287
288−-- | Fold a stream using the supplied left 'Fold' and reducing the resulting
289−-- expression strictly at each step. The behavior is similar to 'foldl''. A
290−-- 'Fold' can terminate early without consuming the full stream. See the
291−-- documentation of individual 'Fold's for termination behavior.
292−--
293−-- >>> Stream.fold Fold.sum (Stream.enumerateFromTo 1 100)
294−-- 5050
295−--
296−-- Folds never fail, therefore, they produce a default value even when no input
297−-- is provided. It means we can always fold an empty stream and get a valid
298−-- result. For example:
299−--
300−-- >>> Stream.fold Fold.sum Stream.nil
301−-- 0
302−--
303−-- However, 'foldMany' on an empty stream results in an empty stream.
304−-- Therefore, @Stream.fold f@ is not the same as @Stream.head . Stream.foldMany
305−-- f@.
306−--
307−-- @fold f = Stream.parse (Parser.fromFold f)@
308−--
309−-- @since 0.7.0
310−{-# INLINE fold #-}
311−fold :: Monad m => Fold m a b -> SerialT m a -> m b
312−fold fl (SerialT strm) = D.fold fl $ D.fromStreamK strm
313−
314−-- | Like 'fold' but also returns the remaining stream.
315−--
316−-- /Inhibits stream fusion/
317−--
318−{-# INLINE foldBreak #-}
319−foldBreak :: Monad m => Fold m a b -> SerialT m a -> m (b, SerialT m a)
320−{-
321−-- XXX This shows quadratic performance when used recursively perhaps because
322−-- of StreamK to StreamD conversions not getting eliminated sue to recursion.
323−foldBreak fl (SerialT strm) = fmap f $ D.foldBreak fl $ D.fromStreamK strm
324−
325− where
326−
327− f (b, str) = (b, SerialT (D.toStreamK str))
328−-}
329−foldBreak fl (SerialT strm) = fmap f $ K.foldBreak fl strm
330−
331− where
332−
333− f (b, str) = (b, SerialT str)
334−
335288------------------------------------------------------------------------------
336289-- Transformation
337290------------------------------------------------------------------------------
src/Streamly/Internal/Data/Stream/IsStream/Eliminate.hs+9−22
@@ -31,7 +31,7 @@ module Streamly.Internal.Data.Stream.IsStream.Eliminate
3131 , parse
3232 , parseK
3333 , parseD
34− , parseBreak
34+ , Stream.parseBreak
3535 , parseBreakD
3636
3737 -- * Stream Deconstruction
@@ -166,22 +166,22 @@ import Streamly.Internal.Data.Stream.IsStream.Common
166166 , take , takeWhile, mkParallel)
167167import Streamly.Internal.Data.Stream.IsStream.Type
168168 (IsStream, toStreamS, fromStreamD, toStreamD)
169−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
169+import Streamly.Internal.Data.Stream.Serial (SerialT)
170170
171171import qualified Streamly.Internal.Data.Array.Foreign.Type as A
172172import qualified Streamly.Internal.Data.Fold as FL
173173import qualified Streamly.Internal.Data.Stream.IsStream.Type as IsStream
174174import qualified Streamly.Internal.Data.Stream.StreamD as D
175175import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
176−import qualified Streamly.Internal.Data.Stream.StreamK as K
177176import qualified Streamly.Internal.Data.Parser.ParserD as PRD
178177import qualified Streamly.Internal.Data.Parser.ParserK.Type as PRK
179−import qualified System.IO as IO
180178#ifdef USE_STREAMK_ONLY
181179import qualified Streamly.Internal.Data.Stream.StreamK as S
182180#else
183181import qualified Streamly.Internal.Data.Stream.StreamD as S
184182#endif
183+import qualified Streamly.Internal.Data.Stream as Stream
184+import qualified System.IO as IO
185185
186186import Prelude hiding
187187 ( drop, take, takeWhile, foldr , foldl, mapM_, sequence, all, any, sum
@@ -228,7 +228,7 @@ import Prelude hiding
228228-- @since 0.1.0
229229{-# INLINE uncons #-}
230230uncons :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (a, t m a))
231−uncons (SerialT m) = fmap (fmap (fmap IsStream.fromStream)) $ K.uncons m
231+uncons = fmap (fmap (fmap IsStream.fromStream)) . K.uncons . Stream.toStreamK
232232
233233------------------------------------------------------------------------------
234234-- Right Folds
@@ -363,7 +363,7 @@ foldxM = IsStream.foldlMx'
363363-- /Since: 0.8.0 (signature change)/
364364{-# INLINE foldlM' #-}
365365foldlM' :: Monad m => (b -> a -> m b) -> m b -> SerialT m a -> m b
366−foldlM' step begin m = S.foldlM' step begin $ toStreamS m
366+foldlM' step begin m = S.foldlM' step begin $ IsStream.toStreamS m
367367
368368------------------------------------------------------------------------------
369369-- Running a sink
@@ -423,19 +423,6 @@ parseBreakD parser strm = do
423423 (b, strmD) <- D.parseBreak parser (toStreamD strm)
424424 return $! (b, fromStreamD strmD)
425425
426−-- | Parse a stream using the supplied 'Parser'.
427−--
428−-- /Internal/
429−--
430−{-# INLINE parseBreak #-}
431−parseBreak :: MonadThrow m => Parser m a b -> SerialT m a -> m (b, SerialT m a)
432−parseBreak p (SerialT strm) = fmap f $ K.parseBreak (PRD.fromParserK p) strm
433−
434− where
435−
436− f (b, str) = (b, SerialT str)
437−
438−
439426------------------------------------------------------------------------------
440427-- Specific Fold Functions
441428------------------------------------------------------------------------------
@@ -451,7 +438,7 @@ parseBreak p (SerialT strm) = fmap f $ K.parseBreak (PRD.fromParserK p) strm
451438-- @since 0.1.0
452439{-# INLINE mapM_ #-}
453440mapM_ :: Monad m => (a -> m b) -> SerialT m a -> m ()
454−mapM_ f m = S.mapM_ f $ toStreamS m
441+mapM_ f = S.mapM_ f . IsStream.toStreamS
455442
456443-- |
457444-- > drain = mapM_ (\_ -> return ())
@@ -555,14 +542,14 @@ headElse x = D.headElse x . toStreamD
555542-- @since 0.1.1
556543{-# INLINE tail #-}
557544tail :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (t m a))
558−tail (SerialT m) = fmap (fmap IsStream.fromStream) $ K.tail m
545+tail m = fmap (fmap IsStream.fromStream) $ K.tail $ Stream.toStreamK m
559546
560547-- | Extract all but the last element of the stream, if any.
561548--
562549-- @since 0.5.0
563550{-# INLINE init #-}
564551init :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (t m a))
565−init (SerialT m) = fmap (fmap IsStream.fromStream) $ K.init m
552+init m = fmap (fmap IsStream.fromStream) $ K.init $ Stream.toStreamK m
566553
567554-- | Extract the last element of the stream, if any.
568555--
src/Streamly/Internal/Data/Stream/IsStream/Generate.hs+3−2
@@ -101,7 +101,7 @@ import Streamly.Internal.Data.Stream.IsStream.Common
101101 , yield, yieldM, repeatM)
102102import Streamly.Internal.Data.Stream.IsStream.Type
103103 (IsStream (..), fromSerial, consM, fromStreamD, fromStreamS)
104−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
104+import Streamly.Internal.Data.Stream.Serial (SerialT)
105105import Streamly.Internal.Data.Stream.WSerial (WSerialT)
106106import Streamly.Internal.Data.Stream.Zip (ZipSerialM)
107107import Streamly.Internal.Data.Time.Units (AbsTime , RelTime64, addToAbsTime64)
@@ -118,6 +118,7 @@ import qualified Streamly.Internal.Data.Stream.StreamK.Type as S
118118#else
119119import qualified Streamly.Internal.Data.Stream.StreamD.Generate as S
120120#endif
121+import qualified Streamly.Internal.Data.Stream.Type as Stream
121122import qualified System.IO as IO
122123
123124import Prelude hiding (iterate, replicate, repeat)
@@ -652,7 +653,7 @@ fromCallback :: MonadAsync m => ((a -> m ()) -> m ()) -> SerialT m a
652653fromCallback setCallback = concatM $ do
653654 (callback, stream) <- Par.newCallbackStream
654655 setCallback callback
655− return $ SerialT stream
656+ return $ Stream.fromStreamK stream
656657
657658-- | Construct a stream by reading a 'Prim' 'IORef' repeatedly.
658659--
src/Streamly/Internal/Data/Stream/IsStream/Type.hs+5−4
@@ -105,7 +105,7 @@ where
105105
106106import Streamly.Internal.Control.Concurrent (MonadAsync)
107107import Streamly.Internal.Data.Fold.Type (Fold (..))
108−import Streamly.Internal.Data.Stream.Serial (SerialT(..), Serial)
108+import Streamly.Internal.Data.Stream.Serial (SerialT, Serial)
109109import Streamly.Internal.Data.Stream.WSerial (WSerialT(..), WSerial)
110110import Streamly.Internal.Data.Stream.Async
111111 (AsyncT(..), Async, WAsyncT(..), WAsync)
@@ -129,6 +129,7 @@ import qualified Streamly.Internal.Data.Stream.StreamK.Type as S
129129#else
130130import qualified Streamly.Internal.Data.Stream.StreamD.Type as S
131131#endif
132+import qualified Streamly.Internal.Data.Stream.Type as Stream
132133import qualified Streamly.Internal.Data.Stream.Zip as Zip
133134import qualified Streamly.Internal.Data.Stream.ZipAsync as ZipAsync
134135
@@ -463,9 +464,9 @@ foldStream st yld sng stp m =
463464fromSerial :: IsStream t => SerialT m a -> t m a
464465fromSerial = adapt
465466
466−instance IsStream SerialT where
467− toStream = getSerialT
468− fromStream = SerialT
467+instance IsStream Stream.Stream where
468+ toStream = Stream.toStreamK
469+ fromStream = Stream.fromStreamK
469470
470471 {-# INLINE consM #-}
471472 {-# SPECIALIZE consM :: IO a -> SerialT IO a -> SerialT IO a #-}
src/Streamly/Internal/Data/Stream/Parallel.hs+5−5
@@ -66,7 +66,6 @@ import qualified Data.Set as Set
6666
6767import Streamly.Internal.Control.Concurrent (MonadAsync)
6868import Streamly.Internal.Data.Fold.Type (Fold)
69−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
7069import Streamly.Internal.Data.Stream.StreamD.Type (Step(..))
7170import Streamly.Internal.Data.Stream.StreamK.Type (Stream)
7271
@@ -74,6 +73,7 @@ import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
7473import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
7574import qualified Streamly.Internal.Data.Stream.SVar.Generate as SVar
7675import qualified Streamly.Internal.Data.Stream.SVar.Eliminate as SVar
76+import qualified Streamly.Internal.Data.Stream.Type as Stream
7777
7878import Streamly.Internal.Data.SVar
7979
@@ -172,7 +172,7 @@ forkSVarPar ss m r = K.mkStream $ \st yld sng stp -> do
172172 writeIORef (svarStopBy sv) $ Set.elemAt 0 set
173173 _ -> return ()
174174 pushWorkerPar sv (runOne st{streamVar = Just sv} r)
175− K.foldStream st yld sng stp $ getSerialT (SVar.fromSVar sv)
175+ K.foldStream st yld sng stp $ Stream.toStreamK (SVar.fromSVar sv)
176176
177177{-# INLINE joinStreamVarPar #-}
178178joinStreamVarPar :: MonadAsync m
@@ -270,7 +270,7 @@ mkParallelK m = K.mkStream $ \st yld sng stp -> do
270270 sv <- newParallelVar StopNone (adaptState st)
271271 -- pushWorkerPar sv (runOne st{streamVar = Just sv} $ toStream m)
272272 SVar.toSVarParallel st sv $ D.fromStreamK m
273− K.foldStream st yld sng stp $ getSerialT $ SVar.fromSVar sv
273+ K.foldStream st yld sng stp $ Stream.toStreamK $ SVar.fromSVar sv
274274
275275-- | Same as 'mkParallel' but for StreamD stream.
276276--
@@ -340,9 +340,9 @@ mkParallelD m = D.Stream step Nothing
340340{-# INLINE tapAsyncK #-}
341341tapAsyncK :: MonadAsync m => (Stream m a -> m b) -> Stream m a -> Stream m a
342342tapAsyncK f m = K.mkStream $ \st yld sng stp -> do
343− sv <- SVar.newFoldSVar st (f . getSerialT)
343+ sv <- SVar.newFoldSVar st (f . Stream.toStreamK)
344344 K.foldStreamShared st yld sng stp
345− $ getSerialT (SVar.teeToSVar sv $ SerialT m)
345+ $ Stream.toStreamK (SVar.teeToSVar sv $ Stream.fromStreamK m)
346346
347347data TapState fs st a = TapInit | Tapping !fs st | TapDone st
348348
src/Streamly/Internal/Data/Stream/SVar/Eliminate.hs+5−4
@@ -36,11 +36,12 @@ import Streamly.Internal.Control.ForkLifted (doFork)
3636import Streamly.Internal.Data.Atomics (atomicModifyIORefCAS_)
3737import Streamly.Internal.Data.Fold.SVar (write, writeLimited)
3838import Streamly.Internal.Data.Fold.Type (Fold(..))
39−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
39+import Streamly.Internal.Data.Stream.Serial (SerialT)
4040import Streamly.Internal.Data.Time.Clock (Clock(Monotonic), getTime)
4141
4242import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
4343import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
44+import qualified Streamly.Internal.Data.Stream.Type as Stream
4445
4546import Streamly.Internal.Data.SVar
4647
@@ -214,7 +215,7 @@ newFoldSVar stt f = do
214215 -- Add the producer thread-id to the SVar.
215216 liftIO myThreadId >>= modifyThread sv
216217
217− void $ doFork (void $ f $ SerialT $ fromProducer sv)
218+ void $ doFork (void $ f $ Stream.fromStreamK $ fromProducer sv)
218219 (svarMrun sv)
219220 (handleFoldException sv)
220221 return sv
@@ -369,8 +370,8 @@ pushToFold sv a = do
369370{-# INLINE teeToSVar #-}
370371teeToSVar :: MonadAsync m =>
371372 SVar K.Stream m a -> SerialT m a -> SerialT m a
372−teeToSVar svr (SerialT m) = SerialT $ K.mkStream $ \st yld sng stp -> do
373− K.foldStreamShared st yld sng stp (go False m)
373+teeToSVar svr m = Stream.fromStreamK $ K.mkStream $ \st yld sng stp -> do
374+ K.foldStreamShared st yld sng stp (go False $ Stream.toStreamK m)
374375
375376 where
376377
src/Streamly/Internal/Data/Stream/SVar/Generate.hs+3−2
@@ -38,12 +38,13 @@ import Control.Monad.IO.Class (MonadIO(liftIO))
3838import Data.IORef (newIORef, readIORef, mkWeakIORef, writeIORef)
3939import Data.Maybe (isNothing)
4040import Streamly.Internal.Control.Concurrent (MonadAsync, askRunInIO)
41−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
41+import Streamly.Internal.Data.Stream.Serial (SerialT)
4242import Streamly.Internal.Data.Time.Clock (Clock(Monotonic), getTime)
4343import System.Mem (performMajorGC)
4444
4545import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
4646import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
47+import qualified Streamly.Internal.Data.Stream.Type as Stream
4748
4849import Streamly.Internal.Data.SVar
4950
@@ -187,7 +188,7 @@ inspect $ hasNoTypeClassesExcept 'fromStreamVar
187188{-# INLINE fromSVar #-}
188189fromSVar :: MonadAsync m => SVar K.Stream m a -> SerialT m a
189190fromSVar sv =
190− SerialT $ K.mkStream $ \st yld sng stp -> do
191+ Stream.fromStreamK $ K.mkStream $ \st yld sng stp -> do
191192 ref <- liftIO $ newIORef ()
192193 _ <- liftIO $ mkWeakIORef ref hook
193194 -- We pass a copy of sv to fromStreamVar, so that we know that it has
src/Streamly/Internal/Data/Stream/ZipAsync.hs+6−4
@@ -25,7 +25,6 @@ where
2525import Data.Semigroup (Semigroup(..))
2626#endif
2727import Streamly.Internal.Control.Concurrent (MonadAsync)
28−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
2928import Streamly.Internal.Data.Stream.StreamK.Type (Stream)
3029
3130import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
@@ -34,6 +33,7 @@ import qualified Streamly.Internal.Data.Stream.StreamD as D
3433import qualified Streamly.Internal.Data.Stream.Serial as Serial
3534import qualified Streamly.Internal.Data.Stream.SVar.Eliminate as SVar
3635import qualified Streamly.Internal.Data.Stream.SVar.Generate as SVar
36+import qualified Streamly.Internal.Data.Stream.Type as Stream
3737import Streamly.Internal.Data.SVar
3838
3939import Prelude hiding (map, repeat, zipWith, errorWithoutStackTrace)
@@ -63,7 +63,8 @@ zipAsyncWithMK :: MonadAsync m
6363zipAsyncWithMK f m1 m2 = K.mkStream $ \st yld sng stp -> do
6464 sv <- newParallelVar StopNone (adaptState st)
6565 SVar.toSVarParallel (adaptState st) sv $ D.fromStreamK m2
66− K.foldStream st yld sng stp $ K.zipWithM f m1 (getSerialT (SVar.fromSVar sv))
66+ K.foldStream st yld sng stp
67+ $ K.zipWithM f m1 (Stream.toStreamK (SVar.fromSVar sv))
6768
6869-- XXX Should we rename this to zipParWith or zipParallelWith? This can happen
6970-- along with the change of behvaior to end the stream concurrently.
@@ -122,10 +123,11 @@ consMZipAsync m (ZipAsyncM r) = ZipAsyncM $ K.consM m r
122123
123124instance Monad m => Functor (ZipAsyncM m) where
124125 {-# INLINE fmap #-}
125− fmap f (ZipAsyncM m) = ZipAsyncM $ getSerialT $ fmap f (SerialT m)
126+ fmap f (ZipAsyncM m) =
127+ ZipAsyncM $ Stream.toStreamK $ fmap f (Stream.fromStreamK m)
126128
127129instance MonadAsync m => Applicative (ZipAsyncM m) where
128− pure = ZipAsyncM . getSerialT . Serial.repeat
130+ pure = ZipAsyncM . Stream.toStreamK . Serial.repeat
129131
130132 {-# INLINE (<*>) #-}
131133 ZipAsyncM m1 <*> ZipAsyncM m2 = ZipAsyncM $ zipAsyncWithK id m1 m2