on master help sign in
Implement SerialT in terms of Stream
eb586c0Ranjeet Kumar Ranjan
benchmark/Streamly/Benchmark/Data/Array/Foreign/Mut.hs+3−4
@@ -21,7 +21,6 @@ import Control.DeepSeq (NFData(..))
21import Control.Monad.IO.Class (MonadIO)
21import Control.Monad.IO.Class (MonadIO)
22import Data.Functor ((<&>))
22import Data.Functor ((<&>))
23import Streamly.Prelude (MonadAsync, SerialT, IsStream)
23import Streamly.Prelude (MonadAsync, SerialT, IsStream)
24−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
25import System.Random (randomRIO)
24import System.Random (randomRIO)
26import Prelude
25import Prelude
27 ( IO
26 ( IO
@@ -43,7 +42,7 @@ import Prelude
43
42
44import qualified Streamly.Internal.Data.Array.Foreign as Array
43import qualified Streamly.Internal.Data.Array.Foreign as Array
45import qualified Streamly.Internal.Data.Array.Foreign.Mut as MArray
44import 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
47import qualified Streamly.Prelude as Stream
46import qualified Streamly.Prelude as Stream
48
47
49import Gauge
48import Gauge
@@ -172,11 +171,11 @@ unfoldReadRevDrain = Stream.drain . Stream.unfold MArray.readRev
172{-# INLINE toStreamDRevDrain #-}
171{-# INLINE toStreamDRevDrain #-}
173toStreamDRevDrain :: MonadIO m => Stream Int -> m ()
172toStreamDRevDrain :: MonadIO m => Stream Int -> m ()
174toStreamDRevDrain =
173toStreamDRevDrain =
175− Stream.drain . SerialT . StreamD.toStreamK . MArray.toStreamDRev
174+ Stream.drain . Stream.fromStreamD . MArray.toStreamDRev
176
175
177{-# INLINE toStreamDDrain #-}
176{-# INLINE toStreamDDrain #-}
178toStreamDDrain :: MonadIO m => Stream Int -> m ()
177toStreamDDrain :: MonadIO m => Stream Int -> m ()
179−toStreamDDrain = Stream.drain . SerialT . StreamD.toStreamK . MArray.toStreamD
178+toStreamDDrain = Stream.drain . Stream.fromStreamD . MArray.toStreamD
180
179
181{-# INLINE unfoldFold #-}
180{-# INLINE unfoldFold #-}
182unfoldFold :: MonadIO m => Stream Int -> m Int
181unfoldFold :: MonadIO m => Stream Int -> m Int
benchmark/Streamly/Benchmark/Prelude/Serial/Transformation.hs+2−2
@@ -30,7 +30,7 @@ import Test.Inspection
30#endif
30#endif
31
31
32import qualified Streamly.Prelude as S
32import qualified Streamly.Prelude as S
33−import qualified Streamly.Internal.Data.Stream.Serial as Serial
33+import qualified Streamly.Internal.Data.Stream as Stream
34import qualified Streamly.Internal.Data.Stream.IsStream as Internal
34import qualified Streamly.Internal.Data.Stream.IsStream as Internal
35import qualified Streamly.Internal.Data.Fold as FL
35import qualified Streamly.Internal.Data.Fold as FL
36import qualified Streamly.Internal.Data.Unfold as Unfold
36import qualified Streamly.Internal.Data.Unfold as Unfold
@@ -283,7 +283,7 @@ filterMAllIn value n = composeN n $ S.filterM (\x -> return $ x <= (value + 1))
283
283
284{-# INLINE foldFilterEven #-}
284{-# INLINE foldFilterEven #-}
285foldFilterEven :: MonadIO m => Int -> SerialT m Int -> m ()
285foldFilterEven :: 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)
287
287
288{-# INLINE _takeOne #-}
288{-# INLINE _takeOne #-}
289_takeOne :: MonadIO m => Int -> SerialT m Int -> m ()
289_takeOne :: MonadIO m => Int -> SerialT m Int -> m ()
benchmark/lib/Streamly/Benchmark/Common.hs+8−3
@@ -53,7 +53,8 @@ import Control.DeepSeq (NFData(..))
53import Data.Functor.Identity (Identity, runIdentity)
53import Data.Functor.Identity (Identity, runIdentity)
54import System.Random (randomRIO)
54import System.Random (randomRIO)
55
55
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
57
58
58import Gauge
59import Gauge
59
60
@@ -106,8 +107,12 @@ benchPureSink1 name f =
106 bench name $ nfIO $ randomRIO (1,1) >>= return . runIdentity . f
107 bench name $ nfIO $ randomRIO (1,1) >>= return . runIdentity . f
107
108
108{-# INLINE benchPureSrc #-}
109{-# INLINE benchPureSrc #-}
109−benchPureSrc :: String -> (Int -> S.SerialT Identity a) -> Benchmark
110+benchPureSrc :: String -> (Int -> S.Stream Identity a) -> Benchmark
110−benchPureSrc name src = benchPure name src (runIdentity . S.drain)
111+benchPureSrc name src = benchPure name src (runIdentity . drain)
112+
113+ where
114+
115+ drain = S.fold Fold.drain
111
116
112-------------------------------------------------------------------------------
117-------------------------------------------------------------------------------
113-- String/List generation for read instances
118-- String/List generation for read instances
core/src/Streamly/Internal/Data/Array.hs+7−6
@@ -62,7 +62,7 @@ import GHC.Base (Int(..))
62import GHC.IO (unsafePerformIO)
62import GHC.IO (unsafePerformIO)
63
63
64import Streamly.Internal.Data.Fold.Type (Fold(..))
64import Streamly.Internal.Data.Fold.Type (Fold(..))
65−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
65+import Streamly.Internal.Data.Stream.Serial (SerialT)
66import Streamly.Internal.Data.Tuple.Strict (Tuple'(..), Tuple3'(..))
66import Streamly.Internal.Data.Tuple.Strict (Tuple'(..), Tuple3'(..))
67import Streamly.Internal.Data.Unfold.Type (Unfold(..))
67import Streamly.Internal.Data.Unfold.Type (Unfold(..))
68
68
@@ -70,6 +70,7 @@ import qualified GHC.Exts as Exts
70import qualified Streamly.Internal.Data.Fold.Type as FL
70import qualified Streamly.Internal.Data.Fold.Type as FL
71import qualified Streamly.Internal.Data.Ring as RB
71import qualified Streamly.Internal.Data.Ring as RB
72import qualified Streamly.Internal.Data.Stream.StreamD as D
72import qualified Streamly.Internal.Data.Stream.StreamD as D
73+import qualified Streamly.Internal.Data.Stream.Type as Stream
73
74
74import Data.Primitive.Array hiding (fromList, fromListN)
75import Data.Primitive.Array hiding (fromList, fromListN)
75import Prelude hiding (foldr, length, read)
76import Prelude hiding (foldr, length, read)
@@ -152,13 +153,13 @@ fromStreamD = D.fold write
152
153
153{-# INLINE fromStreamN #-}
154{-# INLINE fromStreamN #-}
154fromStreamN :: MonadIO m => Int -> SerialT m a -> m (Array a)
155fromStreamN :: MonadIO m => Int -> SerialT m a -> m (Array a)
155−fromStreamN n (SerialT m) = do
156+fromStreamN n m = do
156 when (n < 0) $ error "fromStreamN: negative write count specified"
157 when (n < 0) $ error "fromStreamN: negative write count specified"
157− fromStreamDN n $ D.fromStreamK m
158+ fromStreamDN n $ Stream.toStreamD m
158
159
159{-# INLINE fromStream #-}
160{-# INLINE fromStream #-}
160fromStream :: MonadIO m => SerialT m a -> m (Array a)
161fromStream :: MonadIO m => SerialT m a -> m (Array a)
161−fromStream (SerialT m) = fromStreamD $ D.fromStreamK m
162+fromStream = fromStreamD . Stream.toStreamD
162
163
163{-# INLINABLE fromListN #-}
164{-# INLINABLE fromListN #-}
164fromListN :: Int -> [a] -> Array a
165fromListN :: Int -> [a] -> Array a
@@ -218,11 +219,11 @@ toStreamDRev arr = D.Stream step (length arr - 1)
218
219
219{-# INLINE_EARLY toStream #-}
220{-# INLINE_EARLY toStream #-}
220toStream :: Monad m => Array a -> SerialT m a
221toStream :: Monad m => Array a -> SerialT m a
221−toStream = SerialT . D.toStreamK . toStreamD
222+toStream = Stream.fromStreamD . toStreamD
222
223
223{-# INLINE_EARLY toStreamRev #-}
224{-# INLINE_EARLY toStreamRev #-}
224toStreamRev :: Monad m => Array a -> SerialT m a
225toStreamRev :: Monad m => Array a -> SerialT m a
225−toStreamRev = SerialT . D.toStreamK . toStreamDRev
226+toStreamRev = Stream.fromStreamD . toStreamDRev
226
227
227-------------------------------------------------------------------------------
228-------------------------------------------------------------------------------
228-- Elimination - using Folds
229-- Elimination - using Folds
core/src/Streamly/Internal/Data/Array/Foreign.hs+11−10
@@ -131,7 +131,7 @@ import Streamly.Internal.Data.Array.Foreign.Type
131 (Array(..), length, asPtrUnsafe)
131 (Array(..), length, asPtrUnsafe)
132import Streamly.Internal.Data.Fold.Type (Fold(..))
132import Streamly.Internal.Data.Fold.Type (Fold(..))
133import Streamly.Internal.Data.Producer.Type (Producer(..))
133import Streamly.Internal.Data.Producer.Type (Producer(..))
134−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
134+import Streamly.Internal.Data.Stream.Serial (SerialT)
135import Streamly.Internal.Data.Tuple.Strict (Tuple3Fused'(..))
135import Streamly.Internal.Data.Tuple.Strict (Tuple3Fused'(..))
136import Streamly.Internal.Data.Unfold.Type (Unfold(..))
136import Streamly.Internal.Data.Unfold.Type (Unfold(..))
137import Streamly.Internal.System.IO (unsafeInlineIO)
137import Streamly.Internal.System.IO (unsafeInlineIO)
@@ -142,10 +142,11 @@ import qualified Streamly.Internal.Data.Array.Foreign.Type as A
142import qualified Streamly.Internal.Data.Fold as FL
142import qualified Streamly.Internal.Data.Fold as FL
143import qualified Streamly.Internal.Data.Producer.Type as Producer
143import qualified Streamly.Internal.Data.Producer.Type as Producer
144import qualified Streamly.Internal.Data.Producer as Producer
144import qualified Streamly.Internal.Data.Producer as Producer
145+import qualified Streamly.Internal.Data.Ring.Foreign as RB
145import qualified Streamly.Internal.Data.Stream.Common as P
146import qualified Streamly.Internal.Data.Stream.Common as P
146import qualified Streamly.Internal.Data.Stream.StreamD as D
147import qualified Streamly.Internal.Data.Stream.StreamD as D
148+import qualified Streamly.Internal.Data.Stream.Type as Stream
147import qualified Streamly.Internal.Data.Unfold as Unfold
149import qualified Streamly.Internal.Data.Unfold as Unfold
148−import qualified Streamly.Internal.Data.Ring.Foreign as RB
149
150
150-------------------------------------------------------------------------------
151-------------------------------------------------------------------------------
151-- Construction
152-- Construction
@@ -158,9 +159,9 @@ import qualified Streamly.Internal.Data.Ring.Foreign as RB
158-- /Pre-release/
159-- /Pre-release/
159{-# INLINE fromStreamN #-}
160{-# INLINE fromStreamN #-}
160fromStreamN :: (MonadIO m, Storable a) => Int -> SerialT m a -> m (Array a)
161fromStreamN :: (MonadIO m, Storable a) => Int -> SerialT m a -> m (Array a)
161−fromStreamN n (SerialT m) = do
162+fromStreamN n m = do
162 when (n < 0) $ error "writeN: negative write count specified"
163 when (n < 0) $ error "writeN: negative write count specified"
163− A.fromStreamDN n $ D.fromStreamK m
164+ A.fromStreamDN n $ Stream.toStreamD m
164
165
165-- | Create an 'Array' from a stream. This is useful when we want to create a
166-- | Create an 'Array' from a stream. This is useful when we want to create a
166-- single array from a stream of unknown size. 'writeN' is at least twice
167-- single array from a stream of unknown size. 'writeN' is at least twice
@@ -173,7 +174,7 @@ fromStreamN n (SerialT m) = do
173-- /Pre-release/
174-- /Pre-release/
174{-# INLINE fromStream #-}
175{-# INLINE fromStream #-}
175fromStream :: (MonadIO m, Storable a) => SerialT m a -> m (Array a)
176fromStream :: (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
177-- write m = A.fromStreamD $ D.fromStreamK m
178-- write m = A.fromStreamD $ D.fromStreamK m
178
179
179-------------------------------------------------------------------------------
180-------------------------------------------------------------------------------
@@ -371,7 +372,7 @@ getSliceUnsafe index len (Array contents start e) =
371splitOn :: (Monad m, Storable a) =>
372splitOn :: (Monad m, Storable a) =>
372 (a -> Bool) -> Array a -> SerialT m (Array a)
373 (a -> Bool) -> Array a -> SerialT m (Array a)
373splitOn predicate arr =
374splitOn predicate arr =
374− SerialT $ D.toStreamK
375+ Stream.fromStreamD
375 $ fmap (\(i, len) -> getSliceUnsafe i len arr)
376 $ fmap (\(i, len) -> getSliceUnsafe i len arr)
376 $ D.sliceOnSuffix predicate (A.toStreamD arr)
377 $ D.sliceOnSuffix predicate (A.toStreamD arr)
377
378
@@ -437,8 +438,8 @@ getIndex i arr =
437-- /Pre-release/
438-- /Pre-release/
438{-# INLINE getIndices #-}
439{-# INLINE getIndices #-}
439getIndices :: (Monad m, Storable a) => SerialT m Int -> Unfold m (Array a) a
440getIndices :: (Monad m, Storable a) => SerialT m Int -> Unfold m (Array a) a
440−getIndices (SerialT stream) =
441+getIndices m =
441− let unf = MA.getIndicesD (return . unsafeInlineIO) $ D.fromStreamK stream
442+ let unf = MA.getIndicesD (return . unsafeInlineIO) $ D.fromStreamK $ Stream.toStreamK m
442 in Unfold.lmap A.unsafeThaw unf
443 in Unfold.lmap A.unsafeThaw unf
443
444
444-- | Unfolds @(from, then, to, array)@ generating a finite stream whose first
445-- | 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)
489streamTransform :: forall m a b. (MonadIO m, Storable a, Storable b)
490streamTransform :: forall m a b. (MonadIO m, Storable a, Storable b)
490 => (SerialT m a -> SerialT m b) -> Array a -> m (Array b)
491 => (SerialT m a -> SerialT m b) -> Array a -> m (Array b)
491streamTransform f arr =
492streamTransform 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)
493
494
494-------------------------------------------------------------------------------
495-------------------------------------------------------------------------------
495-- Casts
496-- Casts
@@ -553,7 +554,7 @@ asCStringUnsafe arr act = do
553-- /Pre-release/
554-- /Pre-release/
554{-# INLINE fold #-}
555{-# INLINE fold #-}
555fold :: forall m a b. (Monad m, Storable a) => Fold m a b -> Array a -> m b
556fold :: 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))
557
558
558-- | Fold an array using a stream fold operation.
559-- | Fold an array using a stream fold operation.
559--
560--
core/src/Streamly/Internal/Data/Array/Foreign/Mut.hs+4−4
@@ -34,10 +34,11 @@ where
34
34
35import Control.Monad.IO.Class (MonadIO(..))
35import Control.Monad.IO.Class (MonadIO(..))
36import Streamly.Internal.Data.Unboxed (Storable)
36import Streamly.Internal.Data.Unboxed (Storable)
37−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
37+import Streamly.Internal.Data.Stream.Serial (SerialT)
38import Streamly.Internal.Data.Unfold.Type (Unfold(..))
38import Streamly.Internal.Data.Unfold.Type (Unfold(..))
39
39
40import qualified Streamly.Internal.Data.Stream.StreamD as D
40import qualified Streamly.Internal.Data.Stream.StreamD as D
41+import qualified Streamly.Internal.Data.Stream.Type as Stream
41import qualified Streamly.Internal.Data.Unfold as Unfold
42import qualified Streamly.Internal.Data.Unfold as Unfold
42-- import qualified Streamly.Internal.Data.Stream.Common as P
43-- import qualified Streamly.Internal.Data.Stream.Common as P
43
44
@@ -52,8 +53,7 @@ import Streamly.Internal.Data.Array.Foreign.Mut.Type
52splitOn :: (MonadIO m, Storable a) =>
53splitOn :: (MonadIO m, Storable a) =>
53 (a -> Bool) -> Array a -> SerialT m (Array a)
54 (a -> Bool) -> Array a -> SerialT m (Array a)
54splitOn predicate arr =
55splitOn predicate arr =
55− SerialT
56+ Stream.fromStreamD
56− $ D.toStreamK
57 $ fmap (\(i, len) -> getSliceUnsafe i len arr)
57 $ fmap (\(i, len) -> getSliceUnsafe i len arr)
58 $ D.sliceOnSuffix predicate (toStreamD arr)
58 $ D.sliceOnSuffix predicate (toStreamD arr)
59
59
@@ -99,5 +99,5 @@ getSlicesFromLen from len =
99-- /Pre-release/
99-- /Pre-release/
100{-# INLINE fromStream #-}
100{-# INLINE fromStream #-}
101fromStream :: (MonadIO m, Storable a) => SerialT m a -> m (Array a)
101fromStream :: (MonadIO m, Storable a) => SerialT m a -> m (Array a)
102−fromStream (SerialT m) = fromStreamD $ D.fromStreamK m
102+fromStream = fromStreamD . Stream.toStreamD
103-- fromStream (SerialT m) = P.fold write m
103-- 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(..))
253
253
254import Streamly.Internal.Data.Fold.Type (Fold(..))
254import Streamly.Internal.Data.Fold.Type (Fold(..))
255import Streamly.Internal.Data.Producer.Type (Producer (..))
255import Streamly.Internal.Data.Producer.Type (Producer (..))
256−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
256+import Streamly.Internal.Data.Stream.Type (Stream)
257import Streamly.Internal.Data.SVar.Type (adaptState, defState)
257import Streamly.Internal.Data.SVar.Type (adaptState, defState)
258import Streamly.Internal.Data.Unfold.Type (Unfold(..))
258import Streamly.Internal.Data.Unfold.Type (Unfold(..))
259import Streamly.Internal.System.IO (arrayPayloadSize, defaultChunkSize)
259import Streamly.Internal.System.IO (arrayPayloadSize, defaultChunkSize)
@@ -266,6 +266,7 @@ import qualified Streamly.Internal.Data.Fold.Type as FL
266import qualified Streamly.Internal.Data.Producer as Producer
266import qualified Streamly.Internal.Data.Producer as Producer
267import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
267import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
268import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
268import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
269+import qualified Streamly.Internal.Data.Stream.Type as Stream
269#ifdef USE_FOREIGN_PTR
270#ifdef USE_FOREIGN_PTR
270import qualified Streamly.Internal.Foreign.Malloc as Malloc
271import qualified Streamly.Internal.Foreign.Malloc as Malloc
271#endif
272#endif
@@ -1127,8 +1128,8 @@ getIndicesD liftio (D.Stream stepi sti) = Unfold step inject
1127 return D.Stop
1128 return D.Stop
1128
1129
1129{-# INLINE getIndices #-}
1130{-# INLINE getIndices #-}
1130−getIndices :: (MonadIO m, Storable a) => SerialT m Int -> Unfold m (Array a) a
1131+getIndices :: (MonadIO m, Storable a) => Stream m Int -> Unfold m (Array a) a
1131−getIndices (SerialT stream) = getIndicesD liftIO $ D.fromStreamK stream
1132+getIndices = getIndicesD liftIO . Stream.toStreamD
1132
1133
1133-------------------------------------------------------------------------------
1134-------------------------------------------------------------------------------
1134-- Subarrays
1135-- Subarrays
core/src/Streamly/Internal/Data/Array/Foreign/Type.hs+6−5
@@ -94,7 +94,7 @@ import GHC.Ptr (Ptr(..))
94import Streamly.Internal.Data.Array.Foreign.Mut.Type
94import Streamly.Internal.Data.Array.Foreign.Mut.Type
95 (ArrayContents, touch)
95 (ArrayContents, touch)
96import Streamly.Internal.Data.Fold.Type (Fold(..))
96import Streamly.Internal.Data.Fold.Type (Fold(..))
97−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
97+import Streamly.Internal.Data.Stream.Type (Stream)
98import Streamly.Internal.Data.Unfold.Type (Unfold(..))
98import Streamly.Internal.Data.Unfold.Type (Unfold(..))
99import Text.Read (readPrec, readListPrec, readListPrecDefault)
99import Text.Read (readPrec, readListPrec, readListPrecDefault)
100
100
@@ -103,6 +103,7 @@ import Prelude hiding (length, foldr, read, unlines, splitAt)
103import qualified Streamly.Internal.Data.Array.Foreign.Mut.Type as MA
103import qualified Streamly.Internal.Data.Array.Foreign.Mut.Type as MA
104import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
104import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
105import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
105import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
106+import qualified Streamly.Internal.Data.Stream.Type as Stream
106import qualified Streamly.Internal.Data.Unfold.Type as Unfold
107import qualified Streamly.Internal.Data.Unfold.Type as Unfold
107import qualified GHC.Exts as Exts
108import qualified GHC.Exts as Exts
108
109
@@ -493,8 +494,8 @@ toStreamKRev arr =
493--
494--
494-- /Pre-release/
495-- /Pre-release/
495{-# INLINE_EARLY toStream #-}
496{-# INLINE_EARLY toStream #-}
496−toStream :: (Monad m, Storable a) => Array a -> SerialT m a
497+toStream :: (Monad m, Storable a) => Array a -> Stream m a
497−toStream = SerialT . D.toStreamK . toStreamD
498+toStream = Stream.fromStreamD . toStreamD
498-- XXX add fallback to StreamK rule
499-- XXX add fallback to StreamK rule
499-- {-# RULES "Streamly.Array.read fallback to StreamK" [1]
500-- {-# RULES "Streamly.Array.read fallback to StreamK" [1]
500-- forall a. S.readK (read a) = K.fromArray a #-}
501-- forall a. S.readK (read a) = K.fromArray a #-}
@@ -503,8 +504,8 @@ toStream = SerialT . D.toStreamK . toStreamD
503--
504--
504-- /Pre-release/
505-- /Pre-release/
505{-# INLINE_EARLY toStreamRev #-}
506{-# INLINE_EARLY toStreamRev #-}
506−toStreamRev :: (Monad m, Storable a) => Array a -> SerialT m a
507+toStreamRev :: (Monad m, Storable a) => Array a -> Stream m a
507−toStreamRev = SerialT . D.toStreamK . toStreamDRev
508+toStreamRev = Stream.fromStreamD . toStreamDRev
508
509
509-- XXX add fallback to StreamK rule
510-- XXX add fallback to StreamK rule
510-- {-# RULES "Streamly.Array.readRev fallback to StreamK" [1]
511-- {-# 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)
34import Streamly.Internal.Data.Unboxed (Storable, sizeOf)
34import Streamly.Internal.Data.Unboxed (Storable, sizeOf)
35import Streamly.Internal.Data.Array.Foreign.Mut.Type (Array(..))
35import Streamly.Internal.Data.Array.Foreign.Mut.Type (Array(..))
36import Streamly.Internal.Data.Fold.Type (Fold(..))
36import Streamly.Internal.Data.Fold.Type (Fold(..))
37−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
37+import Streamly.Internal.Data.Stream.Serial (SerialT)
38import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
38import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
39
39
40import qualified Streamly.Internal.Data.Array.Foreign.Mut.Type as MArray
40import qualified Streamly.Internal.Data.Array.Foreign.Mut.Type as MArray
41import qualified Streamly.Internal.Data.Fold.Type as FL
41import qualified Streamly.Internal.Data.Fold.Type as FL
42import qualified Streamly.Internal.Data.Stream.StreamD as D
42import qualified Streamly.Internal.Data.Stream.StreamD as D
43import qualified Streamly.Internal.Data.Parser.ParserD as ParserD
43import qualified Streamly.Internal.Data.Parser.ParserD as ParserD
44+import qualified Streamly.Internal.Data.Stream.Type as Stream
44
45
45-- | @arraysOf n stream@ groups the elements in the input stream into arrays of
46-- | @arraysOf n stream@ groups the elements in the input stream into arrays of
46-- @n@ elements each.
47-- @n@ elements each.
@@ -53,8 +54,7 @@ import qualified Streamly.Internal.Data.Parser.ParserD as ParserD
53{-# INLINE arraysOf #-}
54{-# INLINE arraysOf #-}
54arraysOf :: (MonadIO m, Storable a)
55arraysOf :: (MonadIO m, Storable a)
55 => Int -> SerialT m a -> SerialT m (Array a)
56 => Int -> SerialT m a -> SerialT m (Array a)
56−arraysOf n (SerialT xs) =
57+arraysOf n = Stream.fromStreamD . MArray.arraysOf n . Stream.toStreamD
57− SerialT $ D.toStreamK $ MArray.arraysOf n $ D.fromStreamK xs
58
58
59-------------------------------------------------------------------------------
59-------------------------------------------------------------------------------
60-- Compact
60-- Compact
@@ -193,8 +193,7 @@ lpackArraysChunksOf n (Fold step1 initial1 extract1) =
193{-# INLINE compact #-}
193{-# INLINE compact #-}
194compact :: (MonadIO m, Storable a)
194compact :: (MonadIO m, Storable a)
195 => Int -> SerialT m (Array a) -> SerialT m (Array a)
195 => Int -> SerialT m (Array a) -> SerialT m (Array a)
196−compact n (SerialT xs) =
196+compact n = Stream.fromStreamD . packArraysChunksOf n . Stream.toStreamD
197− SerialT $ D.toStreamK $ packArraysChunksOf n (D.fromStreamK xs)
198
197
199-- | Coalesce adjacent arrays in incoming stream to form bigger arrays of a
198-- | Coalesce adjacent arrays in incoming stream to form bigger arrays of a
200-- maximum specified size. Note that if a single array is bigger than the
199-- maximum specified size. Note that if a single array is bigger than the
@@ -301,8 +300,8 @@ compactGEFold n = Fold step initial extract
301-- /Internal/
300-- /Internal/
302compactLE :: (MonadThrow m, MonadIO m, Storable a) =>
301compactLE :: (MonadThrow m, MonadIO m, Storable a) =>
303 Int -> SerialT m (Array a) -> SerialT m (Array a)
302 Int -> SerialT m (Array a) -> SerialT m (Array a)
304−compactLE n (SerialT xs) =
303+compactLE n =
305− SerialT $ D.toStreamK $ D.parseMany (compactLEParserD n) (D.fromStreamK xs)
304+ Stream.fromStreamD . D.parseMany (compactLEParserD n) . Stream.toStreamD
306
305
307-- | Like 'compactLE' but generates arrays of exactly equal to the size
306-- | Like 'compactLE' but generates arrays of exactly equal to the size
308-- specified except for the last array in the stream which could be shorter.
307-- specified except for the last array in the stream which could be shorter.
@@ -322,5 +321,5 @@ compactEQ _n _xs = undefined
322compactGE ::
321compactGE ::
323 (MonadIO m, Storable a)
322 (MonadIO m, Storable a)
324 => Int -> SerialT m (Array a) -> SerialT m (Array a)
323 => Int -> SerialT m (Array a) -> SerialT m (Array a)
325−compactGE n (SerialT xs) =
324+compactGE n =
326− SerialT $ D.toStreamK $ D.foldMany (compactGEFold n) (D.fromStreamK xs)
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(..))
311import Streamly.Internal.Data.Pipe.Type (Pipe (..), PipeState(..))
311import Streamly.Internal.Data.Pipe.Type (Pipe (..), PipeState(..))
312import Streamly.Internal.Data.Unfold.Type (Unfold(..))
312import Streamly.Internal.Data.Unfold.Type (Unfold(..))
313import Streamly.Internal.Data.Tuple.Strict (Tuple'(..), Tuple3'(..))
313import Streamly.Internal.Data.Tuple.Strict (Tuple'(..), Tuple3'(..))
314−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
314+import Streamly.Internal.Data.Stream.Type (Stream)
315
315
316import qualified Data.IntSet as IntSet
316import qualified Data.IntSet as IntSet
317import qualified Data.Set as Set
317import qualified Data.Set as Set
@@ -323,6 +323,7 @@ import qualified Streamly.Internal.Data.IsMap as IsMap
323import qualified Streamly.Internal.Data.Pipe.Type as Pipe
323import qualified Streamly.Internal.Data.Pipe.Type as Pipe
324import qualified Streamly.Internal.Data.Ring.Foreign as Ring
324import qualified Streamly.Internal.Data.Ring.Foreign as Ring
325import qualified Streamly.Internal.Data.Stream.StreamD.Type as StreamD
325import qualified Streamly.Internal.Data.Stream.StreamD.Type as StreamD
326+import qualified Streamly.Internal.Data.Stream.Type as Stream
326
327
327import Prelude hiding
328import Prelude hiding
328 ( filter, foldl1, drop, dropWhile, take, takeWhile, zipWith
329 ( filter, foldl1, drop, dropWhile, take, takeWhile, zipWith
@@ -345,7 +346,8 @@ import Streamly.Internal.Data.Fold.Type
345-- >>> import qualified Streamly.Internal.Data.Fold.Window as FoldW
346-- >>> import qualified Streamly.Internal.Data.Fold.Window as FoldW
346-- >>> import qualified Streamly.Data.Array.Foreign as Array
347-- >>> import qualified Streamly.Data.Array.Foreign as Array
347-- >>> import qualified Streamly.Internal.Data.Parser as Parser
348-- >>> 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)
349-- >>> import Data.IORef (newIORef, readIORef, writeIORef)
351-- >>> import Data.IORef (newIORef, readIORef, writeIORef)
350-- >>> import qualified Streamly.Internal.Data.Array.Foreign.Mut.Type as MA
352-- >>> import qualified Streamly.Internal.Data.Array.Foreign.Mut.Type as MA
351
353
@@ -2537,12 +2539,12 @@ chunksBetween _low _high _f1 _f2 = undefined
2537-- /Warning!/ working on large streams accumulated as buffers in memory could
2539-- /Warning!/ working on large streams accumulated as buffers in memory could
2538-- be very inefficient, consider using "Streamly.Data.Array" instead.
2540-- be very inefficient, consider using "Streamly.Data.Array" instead.
2539--
2541--
2540−-- >>> toStream = fmap SerialT Fold.toStreamK
2542+-- >>> toStream = fmap Stream.fromStreamK Fold.toStreamK
2541--
2543--
2542-- /Pre-release/
2544-- /Pre-release/
2543{-# INLINE toStream #-}
2545{-# INLINE toStream #-}
2544−toStream :: Monad m => Fold m a (SerialT n a)
2546+toStream :: Monad m => Fold m a (Stream n a)
2545−toStream = fmap SerialT toStreamK
2547+toStream = fmap Stream.fromStreamK toStreamK
2546
2548
2547-- This is more efficient than 'toStream'. toStream is exactly the same as
2549-- This is more efficient than 'toStream'. toStream is exactly the same as
2548-- reversing the stream after toStreamRev.
2550-- reversing the stream after toStreamRev.
@@ -2550,7 +2552,7 @@ toStream = fmap SerialT toStreamK
2550-- | Buffers the input stream to a pure stream in the reverse order of the
2552-- | Buffers the input stream to a pure stream in the reverse order of the
2551-- input.
2553-- input.
2552--
2554--
2553−-- >>> toStreamRev = fmap SerialT Fold.toStreamKRev
2555+-- >>> toStreamRev = fmap Stream.fromStreamK Fold.toStreamKRev
2554--
2556--
2555-- /Warning!/ working on large streams accumulated as buffers in memory could
2557-- /Warning!/ working on large streams accumulated as buffers in memory could
2556-- be very inefficient, consider using "Streamly.Data.Array" instead.
2558-- be very inefficient, consider using "Streamly.Data.Array" instead.
@@ -2559,8 +2561,8 @@ toStream = fmap SerialT toStreamK
2559
2561
2560-- xn : ... : x2 : x1 : []
2562-- xn : ... : x2 : x1 : []
2561{-# INLINE toStreamRev #-}
2563{-# INLINE toStreamRev #-}
2562−toStreamRev :: Monad m => Fold m a (SerialT n a)
2564+toStreamRev :: Monad m => Fold m a (Stream n a)
2563−toStreamRev = fmap SerialT toStreamKRev
2565+toStreamRev = fmap Stream.fromStreamK toStreamKRev
2564
2566
2565-- XXX This does not fuse. It contains a recursive step function. We will need
2567-- XXX This does not fuse. It contains a recursive step function. We will need
2566-- a Skip input constructor in the fold type to make it fuse.
2568-- 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(..))
69#if __GLASGOW_HASKELL__ < 808
69#if __GLASGOW_HASKELL__ < 808
70import Data.Semigroup (Semigroup(..))
70import Data.Semigroup (Semigroup(..))
71#endif
71#endif
72−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
72+import Streamly.Internal.Data.Stream.Serial (SerialT)
73import Streamly.Internal.Data.Stream.Zip (ZipSerialM(..))
73import Streamly.Internal.Data.Stream.Zip (ZipSerialM(..))
74
74
75import qualified Streamly.Internal.Data.Stream.Serial as Serial
75import qualified Streamly.Internal.Data.Stream.Serial as Serial
76import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
76import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
77+import qualified Streamly.Internal.Data.Stream.Type as Stream
77
78
78-- We implement list as a newtype instead of a type synonym to make type
79-- We implement list as a newtype instead of a type synonym to make type
79-- inference easier when using -XOverloadedLists and -XOverloadedStrings. When
80-- inference easier when using -XOverloadedLists and -XOverloadedStrings. When
@@ -123,8 +124,8 @@ instance IsList (List a) where
123--
124--
124-- @since 0.6.0
125-- @since 0.6.0
125pattern Nil :: List a
126pattern Nil :: List a
126−pattern Nil <- (runIdentity . K.null . getSerialT . toSerial -> True) where
127+pattern Nil <- (runIdentity . K.null . Stream.toStreamK . toSerial -> True) where
127− Nil = List $ SerialT K.nil
128+ Nil = List $ Stream.fromStreamK K.nil
128
129
129infixr 5 `Cons`
130infixr 5 `Cons`
130
131
@@ -134,8 +135,8 @@ infixr 5 `Cons`
134-- @since 0.6.0
135-- @since 0.6.0
135pattern Cons :: a -> List a -> List a
136pattern Cons :: a -> List a -> List a
136pattern Cons x xs <-
137pattern Cons x xs <-
137− (fmap (second (List . SerialT))
138+ (fmap (second (List . Stream.fromStreamK))
138− . runIdentity . K.uncons . getSerialT . toSerial
139+ . runIdentity . K.uncons . Stream.toStreamK . toSerial
139 -> Just (x, xs)
140 -> Just (x, xs)
140 )
141 )
141
142
@@ -176,10 +177,10 @@ instance IsList (ZipList a) where
176--
177--
177-- @since 0.6.0
178-- @since 0.6.0
178fromZipList :: ZipList a -> List a
179fromZipList :: ZipList a -> List a
179−fromZipList (ZipList zs) = List $ SerialT $ getZipSerialM zs
180+fromZipList (ZipList zs) = List $ Stream.fromStreamK $ getZipSerialM zs
180
181
181-- | Convert a regular 'List' to a 'ZipList'
182-- | Convert a regular 'List' to a 'ZipList'
182--
183--
183-- @since 0.6.0
184-- @since 0.6.0
184toZipList :: List a -> ZipList a
185toZipList :: 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
248
248
249import Streamly.Internal.Data.Fold.Type (Fold(..))
249import Streamly.Internal.Data.Fold.Type (Fold(..))
250import Streamly.Internal.Data.Parser.ParserK.Type (Parser)
250import Streamly.Internal.Data.Parser.ParserK.Type (Parser)
251−import Streamly.Internal.Data.Stream.Serial (SerialT(getSerialT))
251+import Streamly.Internal.Data.Stream.Type (Stream)
252
252
253import qualified Streamly.Internal.Data.Fold.Type as FL
253import qualified Streamly.Internal.Data.Fold.Type as FL
254import qualified Streamly.Internal.Data.Parser.ParserD as D
254import qualified Streamly.Internal.Data.Parser.ParserD as D
255import qualified Streamly.Internal.Data.Parser.ParserK.Type as K
255import 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
257
257
258--
258--
259-- $setup
259-- $setup
@@ -1227,9 +1227,9 @@ takeP i p = D.toParserK $ D.takeP i $ D.fromParserK p
1227{-# INLINE concatSequence #-}
1227{-# INLINE concatSequence #-}
1228concatSequence ::
1228concatSequence ::
1229 MonadCatch m =>
1229 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
1231concatSequence f p =
1231concatSequence f p =
1232− let sp = fmap D.fromParserK $ SD.fromStreamK $ getSerialT p
1232+ let sp = fmap D.fromParserK $ Stream.toStreamD p
1233 in D.toParserK $ D.sequence f sp
1233 in D.toParserK $ D.sequence f sp
1234
1234
1235-- | Map a 'Parser' returning function on the result of a 'Parser'.
1235-- | 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)
85import GHC.Ptr (Ptr(..))
85import GHC.Ptr (Ptr(..))
86import Streamly.Internal.Data.Array.Foreign.Mut.Type (Array, memcmp)
86import Streamly.Internal.Data.Array.Foreign.Mut.Type (Array, memcmp)
87import Streamly.Internal.Data.Fold.Type (Fold(..), Step(..), lmap)
87import Streamly.Internal.Data.Fold.Type (Fold(..), Step(..), lmap)
88−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
88+import Streamly.Internal.Data.Stream.Type (Stream)
89import Streamly.Internal.Data.Stream.StreamD.Step (Step(..))
89import Streamly.Internal.Data.Stream.StreamD.Step (Step(..))
90import Streamly.Internal.Data.Unfold.Type (Unfold(..))
90import Streamly.Internal.Data.Unfold.Type (Unfold(..))
91import Streamly.Internal.System.IO (unsafeInlineIO)
91import Streamly.Internal.System.IO (unsafeInlineIO)
@@ -354,7 +354,7 @@ readRev = undefined
354-- /Unimplemented/
354-- /Unimplemented/
355{-# INLINE_NORMAL ringsOf #-}
355{-# INLINE_NORMAL ringsOf #-}
356ringsOf :: -- forall m a. (MonadIO m, Storable a) =>
356ringsOf :: -- 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)
358ringsOf = undefined -- Stream.scan (writeN n)
358ringsOf = undefined -- Stream.scan (writeN n)
359
359
360-------------------------------------------------------------------------------
360-------------------------------------------------------------------------------
core/src/Streamly/Internal/Data/Stream/Bottom.hs+1
@@ -122,6 +122,7 @@ foldContinue f s = D.foldContinue f $ toStreamD s
122--
122--
123-- >>> fold f = Stream.parse (Parser.fromFold f)
123-- >>> fold f = Stream.parse (Parser.fromFold f)
124--
124--
125+-- @since 0.9.0
125{-# INLINE fold #-}
126{-# INLINE fold #-}
126fold :: Monad m => Fold m a b -> Stream m a -> m b
127fold :: Monad m => Fold m a b -> Stream m a -> m b
127fold fl strm = D.fold fl $ D.fromStreamK $ toStreamK strm
128fold 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−
4-- |
1-- |
5-- Module : Streamly.Internal.Data.Stream.Serial
2-- Module : Streamly.Internal.Data.Stream.Serial
6-- Copyright : (c) 2017 Composewell Technologies
3-- Copyright : (c) 2017 Composewell Technologies
@@ -17,64 +14,36 @@
17module Streamly.Internal.Data.Stream.Serial
14module Streamly.Internal.Data.Stream.Serial
18 (
15 (
19 -- * Serial appending stream
16 -- * Serial appending stream
20− SerialT(..)
17+ SerialT
21 , Serial
18 , Serial
22 , serial
19 , serial
23
20
24 -- * Construction
21 -- * Construction
25− , cons
22+ , Stream.cons
26− , consM
23+ , Stream.consM
27 , repeat
24 , repeat
28 , unfoldrM
25 , unfoldrM
29 , fromList
26 , fromList
30− , list
31
27
32 -- * Elimination
28 -- * Elimination
33 , toList
29 , toList
34− , foldWith
35− , drain
36
30
37 -- * Transformation
31 -- * Transformation
38 , map
32 , map
39− , mapM
33+ , Stream.mapM
40− , filter
41− , foldFilter
42 )
34 )
43where
35where
44
36
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(..))
57#if __GLASGOW_HASKELL__ < 808
37#if __GLASGOW_HASKELL__ < 808
58import Data.Semigroup (Semigroup(..))
38import Data.Semigroup (Semigroup(..))
59#endif
39#endif
60−import GHC.Exts (IsList(..), IsString(..), oneShot)
40+import GHC.Exts (IsList(..))
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)
68
41
69−import qualified Streamly.Internal.Data.Stream.Common as P
70import qualified Streamly.Internal.Data.Stream.StreamD.Generate as D
42import qualified Streamly.Internal.Data.Stream.StreamD.Generate as D
71−import qualified Streamly.Internal.Data.Stream.StreamD.Transform as D
43+import qualified Streamly.Internal.Data.Stream as Stream
72−import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
73−import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
74
44
75−import Prelude hiding (map, mapM, repeat, filter)
45+import Prelude hiding (map, mapM, repeat)
76
46
77−#include "Instances.hs"
78#include "inline.hs"
47#include "inline.hs"
79
48
80-- $setup
49-- $setup
@@ -84,38 +53,7 @@ import Prelude hiding (map, mapM, repeat, filter)
84-- SerialT
53-- SerialT
85------------------------------------------------------------------------------
54------------------------------------------------------------------------------
86
55
87−-- | For 'SerialT' streams:
56+type SerialT = Stream.Stream
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)
119
57
120-- | A serial IO stream of elements of type @a@. See 'SerialT' documentation
58-- | A serial IO stream of elements of type @a@. See 'SerialT' documentation
121-- for more details.
59-- for more details.
@@ -129,21 +67,12 @@ type Serial = SerialT IO
129-- Generation
67-- Generation
130------------------------------------------------------------------------------
68------------------------------------------------------------------------------
131
69
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−
141-- |
70-- |
142-- Generate an infinite stream by repeating a pure value.
71-- Generate an infinite stream by repeating a pure value.
143--
72--
144{-# INLINE_NORMAL repeat #-}
73{-# INLINE_NORMAL repeat #-}
145repeat :: Monad m => a -> SerialT m a
74repeat :: Monad m => a -> SerialT m a
146−repeat = SerialT . D.toStreamK . D.repeat
75+repeat = Stream.fromStreamD . D.repeat
147
76
148------------------------------------------------------------------------------
77------------------------------------------------------------------------------
149-- Combining
78-- Combining
@@ -153,37 +82,6 @@ repeat = SerialT . D.toStreamK . D.repeat
153serial :: SerialT m a -> SerialT m a -> SerialT m a
82serial :: SerialT m a -> SerialT m a -> SerialT m a
154serial = (<>)
83serial = (<>)
155
84
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−
187-- |
85-- |
188-- @
86-- @
189-- map = fmap
87-- map = fmap
@@ -199,120 +97,7 @@ mapM f (SerialT m) = SerialT $ D.toStreamK $ D.mapM f $ D.fromStreamK m
199-- @since 0.4.0
97-- @since 0.4.0
200{-# INLINE map #-}
98{-# INLINE map #-}
201map :: Monad m => (a -> b) -> SerialT m a -> SerialT m b
99map :: Monad m => (a -> b) -> SerialT m a -> SerialT m b
202−map f = mapM (return . f)
100+map f = Stream.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 #-}
316
101
317------------------------------------------------------------------------------
102------------------------------------------------------------------------------
318-- Construction
103-- Construction
@@ -341,10 +126,4 @@ list = fromStreamD . D.fromList
341--
126--
342{-# INLINE unfoldrM #-}
127{-# INLINE unfoldrM #-}
343unfoldrM :: Monad m => (b -> m (Maybe (a, b))) -> b -> SerialT m a
128unfoldrM :: Monad m => (b -> m (Maybe (a, b))) -> b -> SerialT m a
344−unfoldrM step seed = SerialT $ D.toStreamK (D.unfoldrM step seed)
129+unfoldrM step seed = Stream.fromStreamD (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 #-}
core/src/Streamly/Internal/Data/Stream/Type.hs+1
@@ -212,6 +212,7 @@ instance Monad m => Applicative (Stream m) where
212 (<*) = apDiscardSnd
212 (<*) = apDiscardSnd
213 -- (<*) = K.apSerialDiscardSnd
213 -- (<*) = K.apSerialDiscardSnd
214
214
215+-- XXX Need to remove the MonadBase instance
215MONAD_COMMON_INSTANCES(Stream,)
216MONAD_COMMON_INSTANCES(Stream,)
216LIST_INSTANCES(Stream)
217LIST_INSTANCES(Stream)
217NFDATA1_INSTANCE(Stream)
218NFDATA1_INSTANCE(Stream)
core/src/Streamly/Internal/Data/Stream/Zip.hs+3−3
@@ -38,7 +38,6 @@ import Text.Read
38 , readListPrecDefault)
38 , readListPrecDefault)
39import Streamly.Internal.BaseCompat ((#.))
39import Streamly.Internal.BaseCompat ((#.))
40import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
40import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
41−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
42import Streamly.Internal.Data.Stream.StreamK.Type (Stream)
41import Streamly.Internal.Data.Stream.StreamK.Type (Stream)
43
42
44import qualified Streamly.Internal.Data.Stream.Common as P
43import qualified Streamly.Internal.Data.Stream.Common as P
@@ -46,6 +45,7 @@ import qualified Streamly.Internal.Data.Stream.Common as P
46import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
45import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
47import qualified Streamly.Internal.Data.Stream.StreamD as D
46import qualified Streamly.Internal.Data.Stream.StreamD as D
48import qualified Streamly.Internal.Data.Stream.Serial as Serial
47import qualified Streamly.Internal.Data.Stream.Serial as Serial
48+import qualified Streamly.Internal.Data.Stream.Type as Stream
49
49
50import Prelude hiding (map, repeat, zipWith)
50import Prelude hiding (map, repeat, zipWith)
51
51
@@ -105,10 +105,10 @@ NFDATA1_INSTANCE(ZipSerialM)
105
105
106instance Monad m => Functor (ZipSerialM m) where
106instance Monad m => Functor (ZipSerialM m) where
107 {-# INLINE fmap #-}
107 {-# 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)
109
109
110instance Monad m => Applicative (ZipSerialM m) where
110instance Monad m => Applicative (ZipSerialM m) where
111− pure = ZipSerialM . getSerialT . Serial.repeat
111+ pure = ZipSerialM . Stream.toStreamK . Serial.repeat
112
112
113 {-# INLINE (<*>) #-}
113 {-# INLINE (<*>) #-}
114 ZipSerialM m1 <*> ZipSerialM m2 = ZipSerialM $ zipWithK id m1 m2
114 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(..))
9import Data.Primitive.Types (Prim(..))
9import Data.Primitive.Types (Prim(..))
10import Streamly.Internal.Data.Fold.Type (Fold(..))
10import Streamly.Internal.Data.Fold.Type (Fold(..))
11import Streamly.Internal.Data.Unfold.Type (Unfold(..))
11import Streamly.Internal.Data.Unfold.Type (Unfold(..))
12−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
12+import Streamly.Internal.Data.Stream.Serial (SerialT)
13
13
14import qualified Streamly.Internal.Data.Stream.Common as P
14import qualified Streamly.Internal.Data.Stream.Common as P
15import qualified Streamly.Internal.Data.Stream.StreamD as D
15import qualified Streamly.Internal.Data.Stream.StreamD as D
16
16
17import Prelude hiding (length, null, last, map, (!!), read, concat)
17import Prelude hiding (length, null, last, map, (!!), read, concat)
18+import qualified Streamly.Internal.Data.Stream.Type as Stream
18
19
19-------------------------------------------------------------------------------
20-------------------------------------------------------------------------------
20-- Construction
21-- Construction
@@ -27,9 +28,9 @@ import Prelude hiding (length, null, last, map, (!!), read, concat)
27-- /Pre-release/
28-- /Pre-release/
28{-# INLINE fromStreamN #-}
29{-# INLINE fromStreamN #-}
29fromStreamN :: (MonadIO m, Prim a) => Int -> SerialT m a -> m (Array a)
30fromStreamN :: (MonadIO m, Prim a) => Int -> SerialT m a -> m (Array a)
30−fromStreamN n (SerialT m) = do
31+fromStreamN n m = do
31 when (n < 0) $ error "writeN: negative write count specified"
32 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
33
34
34-- | Create an 'Array' from a stream. This is useful when we want to create a
35-- | Create an 'Array' from a stream. This is useful when we want to create a
35-- single array from a stream of unknown size. 'writeN' is at least twice
36-- single array from a stream of unknown size. 'writeN' is at least twice
@@ -42,7 +43,7 @@ fromStreamN n (SerialT m) = do
42-- /Pre-release/
43-- /Pre-release/
43{-# INLINE fromStream #-}
44{-# INLINE fromStream #-}
44fromStream :: (MonadIO m, Prim a) => SerialT m a -> m (Array a)
45fromStream :: (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
46-- write m = A.fromStreamD $ D.toStreamD m
47-- write m = A.fromStreamD $ D.toStreamD m
47
48
48-------------------------------------------------------------------------------
49-------------------------------------------------------------------------------
@@ -54,7 +55,7 @@ fromStream (SerialT m) = P.fold A.write m
54-- /Pre-release/
55-- /Pre-release/
55{-# INLINE_EARLY toStream #-}
56{-# INLINE_EARLY toStream #-}
56toStream :: (MonadIO m, Prim a) => Array a -> SerialT m a
57toStream :: (MonadIO m, Prim a) => Array a -> SerialT m a
57−toStream = SerialT . D.toStreamK . A.toStreamD
58+toStream = Stream.fromStreamK . D.toStreamK . A.toStreamD
58-- XXX add fallback to StreamK rule
59-- XXX add fallback to StreamK rule
59-- {-# RULES "Streamly.Array.read fallback to StreamK" [1]
60-- {-# RULES "Streamly.Array.read fallback to StreamK" [1]
60-- forall a. S.readK (read a) = K.fromArray a #-}
61-- forall a. S.readK (read a) = K.fromArray a #-}
@@ -64,7 +65,7 @@ toStream = SerialT . D.toStreamK . A.toStreamD
64-- /Pre-release/
65-- /Pre-release/
65{-# INLINE_EARLY toStreamRev #-}
66{-# INLINE_EARLY toStreamRev #-}
66toStreamRev :: (MonadIO m, Prim a) => Array a -> SerialT m a
67toStreamRev :: (MonadIO m, Prim a) => Array a -> SerialT m a
67−toStreamRev = SerialT . D.toStreamK . A.toStreamDRev
68+toStreamRev = Stream.fromStreamK . D.toStreamK . A.toStreamDRev
68-- XXX add fallback to StreamK rule
69-- XXX add fallback to StreamK rule
69-- {-# RULES "Streamly.Array.readRev fallback to StreamK" [1]
70-- {-# RULES "Streamly.Array.readRev fallback to StreamK" [1]
70-- forall a. S.toStreamK (readRev a) = K.revFromArray a #-}
71-- forall a. S.toStreamK (readRev a) = K.revFromArray a #-}
@@ -125,7 +126,7 @@ null arr = length arr == 0
125-- /Pre-release/
126-- /Pre-release/
126{-# INLINE fold #-}
127{-# INLINE fold #-}
127fold :: forall m a b. (MonadIO m, Prim a) => Fold m a b -> Array a -> m b
128fold :: 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))
129
130
130-- | Fold an array using a stream fold operation.
131-- | Fold an array using a stream fold operation.
131--
132--
@@ -170,8 +171,8 @@ last arr = readIndex arr (length arr - 1)
170concat :: (MonadIO m, Prim a) => SerialT m (Array a) -> SerialT m a
171concat :: (MonadIO m, Prim a) => SerialT m (Array a) -> SerialT m a
171-- concat m = D.fromStreamD $ A.flattenArrays (D.toStreamD m)
172-- concat m = D.fromStreamD $ A.flattenArrays (D.toStreamD m)
172-- concat m = D.fromStreamD $ D.concatMap A.toStreamD (D.toStreamD m)
173-- concat m = D.fromStreamD $ D.concatMap A.toStreamD (D.toStreamD m)
173−concat (SerialT m) =
174+concat m =
174− SerialT $ D.toStreamK $ D.unfoldMany read (D.fromStreamK m)
175+ Stream.fromStreamK $ D.toStreamK $ D.unfoldMany read (D.fromStreamK $ Stream.toStreamK m)
175
176
176-- | Coalesce adjacent arrays in incoming stream to form bigger arrays of a
177-- | Coalesce adjacent arrays in incoming stream to form bigger arrays of a
177-- maximum specified size in bytes.
178-- maximum specified size in bytes.
@@ -180,5 +181,5 @@ concat (SerialT m) =
180{-# INLINE compact #-}
181{-# INLINE compact #-}
181compact ::
182compact ::
182 (MonadIO m, Prim a) => Int -> SerialT m (Array a) -> SerialT m (Array a)
183 (MonadIO m, Prim a) => Int -> SerialT m (Array a) -> SerialT m (Array a)
183−compact n (SerialT xs) =
184+compact n m =
184− SerialT $ D.toStreamK $ A.packArraysChunksOf n (D.fromStreamK xs)
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(..))
74import Streamly.Internal.Data.Array.Stream.Fold.Foreign (ArrayFold(..))
74import Streamly.Internal.Data.Array.Stream.Fold.Foreign (ArrayFold(..))
75import Streamly.Internal.Data.Fold.Type (Fold(..))
75import Streamly.Internal.Data.Fold.Type (Fold(..))
76import Streamly.Internal.Data.Parser (ParseError(..))
76import Streamly.Internal.Data.Parser (ParseError(..))
77−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
77+import Streamly.Internal.Data.Stream.Serial (SerialT)
78import Streamly.Internal.Data.Stream.IsStream.Type
78import Streamly.Internal.Data.Stream.IsStream.Type
79 (IsStream, fromStreamD, toStreamD)
79 (IsStream, fromStreamD, toStreamD)
80import Streamly.Internal.Data.SVar (adaptState, defState)
80import Streamly.Internal.Data.SVar (adaptState, defState)
@@ -94,6 +94,7 @@ import qualified Streamly.Internal.Data.Parser.ParserD as PRD
94import qualified Streamly.Internal.Data.Stream.IsStream as S
94import qualified Streamly.Internal.Data.Stream.IsStream as S
95import qualified Streamly.Internal.Data.Stream.StreamD as D
95import qualified Streamly.Internal.Data.Stream.StreamD as D
96import qualified Streamly.Internal.Data.Stream.StreamK as K
96import qualified Streamly.Internal.Data.Stream.StreamK as K
97+import qualified Streamly.Internal.Data.Stream.Type as Stream
97
98
98-- XXX Since these are immutable arrays MonadIO constraint can be removed from
99-- XXX Since these are immutable arrays MonadIO constraint can be removed from
99-- most places.
100-- most places.
@@ -408,7 +409,10 @@ foldBreak ::
408 -> SerialT m (A.Array a)
409 -> SerialT m (A.Array a)
409 -> m (b, SerialT m (A.Array a))
410 -> m (b, SerialT m (A.Array a))
410-- foldBreak f s = fmap fromStreamD <$> foldBreakD f (toStreamD s)
411-- 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
412-- If foldBreak performs better than runArrayFoldBreak we can use a rewrite
416-- If foldBreak performs better than runArrayFoldBreak we can use a rewrite
413-- rule to rewrite runArrayFoldBreak to fold.
417-- rule to rewrite runArrayFoldBreak to fold.
414-- foldBreak f = runArrayFoldBreak (ArrayFold.fromFold f)
418-- foldBreak f = runArrayFoldBreak (ArrayFold.fromFold f)
@@ -739,8 +743,10 @@ parseBreak ::
739parseBreak p s =
743parseBreak p s =
740 fmap fromStreamD <$> parseBreakD (PRD.fromParserK p) (toStreamD s)
744 fmap fromStreamD <$> parseBreakD (PRD.fromParserK p) (toStreamD s)
741-}
745-}
742−parseBreak p (SerialT s) =
746+parseBreak p =
747+ fmap (fmap Stream.fromStreamK)
743− fmap (fmap SerialT) $ parseBreakK (PRD.fromParserK p) s
748+ . parseBreakK (PRD.fromParserK p)
749+ . Stream.toStreamK
744
750
745-------------------------------------------------------------------------------
751-------------------------------------------------------------------------------
746-- Elimination - Running Array Folds and parsers
752-- Elimination - Running Array Folds and parsers
src/Streamly/Internal/Data/SmallArray.hs+6−5
@@ -53,10 +53,11 @@ import Streamly.Internal.Data.SmallArray.Type
53import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
53import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
54import Streamly.Internal.Data.Unfold.Type (Unfold(..))
54import Streamly.Internal.Data.Unfold.Type (Unfold(..))
55import Streamly.Internal.Data.Fold.Type (Fold(..))
55import Streamly.Internal.Data.Fold.Type (Fold(..))
56−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
56+import Streamly.Internal.Data.Stream.Serial (SerialT)
57
57
58import qualified Streamly.Internal.Data.Stream.StreamD as D
58import qualified Streamly.Internal.Data.Stream.StreamD as D
59import qualified Streamly.Internal.Data.Fold.Type as FL
59import qualified Streamly.Internal.Data.Fold.Type as FL
60+import qualified Streamly.Internal.Data.Stream.Type as Stream
60
61
61{-# NOINLINE bottomElement #-}
62{-# NOINLINE bottomElement #-}
62bottomElement :: a
63bottomElement :: a
@@ -161,17 +162,17 @@ instance NFData a => NFData (SmallArray a) where
161-- For optimal performance use this with @n@ <= 128.
162-- For optimal performance use this with @n@ <= 128.
162{-# INLINE fromStreamN #-}
163{-# INLINE fromStreamN #-}
163fromStreamN :: MonadIO m => Int -> SerialT m a -> m (SmallArray a)
164fromStreamN :: MonadIO m => Int -> SerialT m a -> m (SmallArray a)
164−fromStreamN n (SerialT m) = do
165+fromStreamN n m = do
165 when (n < 0) $ error "fromStreamN: negative write count specified"
166 when (n < 0) $ error "fromStreamN: negative write count specified"
166− fromStreamDN n $ D.fromStreamK m
167+ fromStreamDN n $ D.fromStreamK $ Stream.toStreamK m
167
168
168{-# INLINE_EARLY toStream #-}
169{-# INLINE_EARLY toStream #-}
169toStream :: Monad m => SmallArray a -> SerialT m a
170toStream :: Monad m => SmallArray a -> SerialT m a
170−toStream = SerialT . D.toStreamK . toStreamD
171+toStream = Stream.fromStreamK . D.toStreamK . toStreamD
171
172
172{-# INLINE_EARLY toStreamRev #-}
173{-# INLINE_EARLY toStreamRev #-}
173toStreamRev :: Monad m => SmallArray a -> SerialT m a
174toStreamRev :: Monad m => SmallArray a -> SerialT m a
174−toStreamRev = SerialT . D.toStreamK . toStreamDRev
175+toStreamRev = Stream.fromStreamK . D.toStreamK . toStreamDRev
175
176
176{-# INLINE fold #-}
177{-# INLINE fold #-}
177fold :: Monad m => Fold m a b -> SmallArray a -> m b
178fold :: 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
51
51
52import Streamly.Internal.Control.Concurrent
52import Streamly.Internal.Control.Concurrent
53 (MonadRunInIO, MonadAsync, RunInIO(..), askRunInIO, restoreM)
53 (MonadRunInIO, MonadAsync, RunInIO(..), askRunInIO, restoreM)
54−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
55import Streamly.Internal.Data.Stream.StreamK.Type (Stream)
54import Streamly.Internal.Data.Stream.StreamK.Type (Stream)
56
55
57import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
56import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
58import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
57import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
58+import qualified Streamly.Internal.Data.Stream.Type as Stream
59
59
60import Streamly.Internal.Data.Stream.SVar.Generate
60import Streamly.Internal.Data.Stream.SVar.Generate
61import Streamly.Internal.Data.SVar
61import Streamly.Internal.Data.SVar
@@ -606,7 +606,7 @@ forkSVarAhead :: MonadAsync m => Stream m a -> Stream m a -> Stream m a
606forkSVarAhead m1 m2 = K.mkStream $ \st yld sng stp -> do
606forkSVarAhead m1 m2 = K.mkStream $ \st yld sng stp -> do
607 sv <- newAheadVar st (concurrently m1 m2)
607 sv <- newAheadVar st (concurrently m1 m2)
608 workLoopAhead
608 workLoopAhead
609− K.foldStream st yld sng stp $ getSerialT (fromSVar sv)
609+ K.foldStream st yld sng stp $ Stream.toStreamK (fromSVar sv)
610 where
610 where
611 concurrently ma mb = K.mkStream $ \st yld sng stp -> do
611 concurrently ma mb = K.mkStream $ \st yld sng stp -> do
612 runInIO <- askRunInIO
612 runInIO <- askRunInIO
src/Streamly/Internal/Data/Stream/Async.hs+3−3
@@ -61,12 +61,12 @@ import Streamly.Internal.Control.Concurrent
61 (MonadRunInIO, MonadAsync, RunInIO(..), askRunInIO, restoreM)
61 (MonadRunInIO, MonadAsync, RunInIO(..), askRunInIO, restoreM)
62import Streamly.Internal.Data.Atomics
62import Streamly.Internal.Data.Atomics
63 (atomicModifyIORefCAS, atomicModifyIORefCAS_)
63 (atomicModifyIORefCAS, atomicModifyIORefCAS_)
64−import Streamly.Internal.Data.Stream.Serial (SerialT (..))
65import Streamly.Internal.Data.Stream.StreamK.Type (Stream)
64import Streamly.Internal.Data.Stream.StreamK.Type (Stream)
66import Streamly.Internal.Data.Stream.SVar.Generate (fromSVar, fromSVarD)
65import Streamly.Internal.Data.Stream.SVar.Generate (fromSVar, fromSVarD)
67
66
68import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
67import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
69import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
68import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
69+import qualified Streamly.Internal.Data.Stream.Type as Stream
70
70
71import Streamly.Internal.Data.SVar.Type
71import Streamly.Internal.Data.SVar.Type
72import Streamly.Internal.Data.SVar
72import Streamly.Internal.Data.SVar
@@ -545,7 +545,7 @@ newAsyncVar st m = do
545mkAsyncK :: MonadAsync m => Stream m a -> Stream m a
545mkAsyncK :: MonadAsync m => Stream m a -> Stream m a
546mkAsyncK m = K.mkStream $ \st yld sng stp -> do
546mkAsyncK m = K.mkStream $ \st yld sng stp -> do
547 sv <- newAsyncVar (adaptState st) m
547 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
549
549
550--
550--
551-- This is slightly faster than the CPS version above
551-- This is slightly faster than the CPS version above
@@ -649,7 +649,7 @@ forkSVarAsync style m1 m2 = K.mkStream $ \st yld sng stp -> do
649 AsyncVar -> newAsyncVar st (concurrently m1 m2)
649 AsyncVar -> newAsyncVar st (concurrently m1 m2)
650 WAsyncVar -> newWAsyncVar st (concurrently m1 m2)
650 WAsyncVar -> newWAsyncVar st (concurrently m1 m2)
651 _ -> error "illegal svar type"
651 _ -> 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
653 where
653 where
654 concurrently ma mb = K.mkStream $ \st yld sng stp -> do
654 concurrently ma mb = K.mkStream $ \st yld sng stp -> do
655 runInIO <- askRunInIO
655 runInIO <- askRunInIO
src/Streamly/Internal/Data/Stream/IsStream/Common.hs+5−52
@@ -23,8 +23,8 @@ module Streamly.Internal.Data.Stream.IsStream.Common
23
23
24 -- * Elimination
24 -- * Elimination
25 , foldContinue
25 , foldContinue
26− , fold
26+ , Stream.fold
27− , foldBreak
27+ , Stream.foldBreak
28
28
29 -- * Transformation
29 -- * Transformation
30 , map
30 , map
@@ -76,22 +76,22 @@ import Streamly.Internal.Data.Fold.Type (Fold (..))
76import Streamly.Internal.Data.Stream.IsStream.Combinators (maxYields)
76import Streamly.Internal.Data.Stream.IsStream.Combinators (maxYields)
77import Streamly.Internal.Data.Stream.IsStream.Type
77import Streamly.Internal.Data.Stream.IsStream.Type
78 (IsStream(..), fromStreamD, toStreamD, fromStreamS, toStreamS)
78 (IsStream(..), fromStreamD, toStreamD, fromStreamS, toStreamS)
79−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
79+import Streamly.Internal.Data.Stream.Serial (SerialT)
80import Streamly.Internal.Data.Time.Units (AbsTime, RelTime64, addToAbsTime64)
80import Streamly.Internal.Data.Time.Units (AbsTime, RelTime64, addToAbsTime64)
81+import Streamly.Internal.System.IO (defaultChunkSize)
81
82
82import qualified Streamly.Internal.Data.Array.Foreign.Type as A
83import qualified Streamly.Internal.Data.Array.Foreign.Type as A
83import qualified Streamly.Internal.Data.Stream.Async as Async
84import qualified Streamly.Internal.Data.Stream.Async as Async
84import qualified Streamly.Internal.Data.Stream.IsStream.Type as IsStream
85import qualified Streamly.Internal.Data.Stream.IsStream.Type as IsStream
85import qualified Streamly.Internal.Data.Stream.Parallel as Par
86import qualified Streamly.Internal.Data.Stream.Parallel as Par
86import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
87import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
87−import qualified Streamly.Internal.Data.Stream.StreamK as K
88import qualified Streamly.Internal.Data.Stream.StreamD as D
88import qualified Streamly.Internal.Data.Stream.StreamD as D
89#ifdef USE_STREAMK_ONLY
89#ifdef USE_STREAMK_ONLY
90import qualified Streamly.Internal.Data.Stream.StreamK as S
90import qualified Streamly.Internal.Data.Stream.StreamK as S
91#else
91#else
92import qualified Streamly.Internal.Data.Stream.StreamD as S
92import qualified Streamly.Internal.Data.Stream.StreamD as S
93#endif
93#endif
94−import Streamly.Internal.System.IO (defaultChunkSize)
94+import qualified Streamly.Internal.Data.Stream as Stream
95
95
96import Prelude hiding (take, takeWhile, drop, reverse, concatMap, map, zipWith)
96import Prelude hiding (take, takeWhile, drop, reverse, concatMap, map, zipWith)
97
97
@@ -285,53 +285,6 @@ relTimesWith = fmap snd . timesWith
285foldContinue :: Monad m => Fold m a b -> SerialT m a -> Fold m a b
285foldContinue :: Monad m => Fold m a b -> SerialT m a -> Fold m a b
286foldContinue f s = D.foldContinue f $ IsStream.toStreamD s
286foldContinue f s = D.foldContinue f $ IsStream.toStreamD s
287
287
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−
335------------------------------------------------------------------------------
288------------------------------------------------------------------------------
336-- Transformation
289-- Transformation
337------------------------------------------------------------------------------
290------------------------------------------------------------------------------
src/Streamly/Internal/Data/Stream/IsStream/Eliminate.hs+9−22
@@ -31,7 +31,7 @@ module Streamly.Internal.Data.Stream.IsStream.Eliminate
31 , parse
31 , parse
32 , parseK
32 , parseK
33 , parseD
33 , parseD
34− , parseBreak
34+ , Stream.parseBreak
35 , parseBreakD
35 , parseBreakD
36
36
37 -- * Stream Deconstruction
37 -- * Stream Deconstruction
@@ -166,22 +166,22 @@ import Streamly.Internal.Data.Stream.IsStream.Common
166 , take , takeWhile, mkParallel)
166 , take , takeWhile, mkParallel)
167import Streamly.Internal.Data.Stream.IsStream.Type
167import Streamly.Internal.Data.Stream.IsStream.Type
168 (IsStream, toStreamS, fromStreamD, toStreamD)
168 (IsStream, toStreamS, fromStreamD, toStreamD)
169−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
169+import Streamly.Internal.Data.Stream.Serial (SerialT)
170
170
171import qualified Streamly.Internal.Data.Array.Foreign.Type as A
171import qualified Streamly.Internal.Data.Array.Foreign.Type as A
172import qualified Streamly.Internal.Data.Fold as FL
172import qualified Streamly.Internal.Data.Fold as FL
173import qualified Streamly.Internal.Data.Stream.IsStream.Type as IsStream
173import qualified Streamly.Internal.Data.Stream.IsStream.Type as IsStream
174import qualified Streamly.Internal.Data.Stream.StreamD as D
174import qualified Streamly.Internal.Data.Stream.StreamD as D
175import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
175import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
176−import qualified Streamly.Internal.Data.Stream.StreamK as K
177import qualified Streamly.Internal.Data.Parser.ParserD as PRD
176import qualified Streamly.Internal.Data.Parser.ParserD as PRD
178import qualified Streamly.Internal.Data.Parser.ParserK.Type as PRK
177import qualified Streamly.Internal.Data.Parser.ParserK.Type as PRK
179−import qualified System.IO as IO
180#ifdef USE_STREAMK_ONLY
178#ifdef USE_STREAMK_ONLY
181import qualified Streamly.Internal.Data.Stream.StreamK as S
179import qualified Streamly.Internal.Data.Stream.StreamK as S
182#else
180#else
183import qualified Streamly.Internal.Data.Stream.StreamD as S
181import qualified Streamly.Internal.Data.Stream.StreamD as S
184#endif
182#endif
183+import qualified Streamly.Internal.Data.Stream as Stream
184+import qualified System.IO as IO
185
185
186import Prelude hiding
186import Prelude hiding
187 ( drop, take, takeWhile, foldr , foldl, mapM_, sequence, all, any, sum
187 ( drop, take, takeWhile, foldr , foldl, mapM_, sequence, all, any, sum
@@ -228,7 +228,7 @@ import Prelude hiding
228-- @since 0.1.0
228-- @since 0.1.0
229{-# INLINE uncons #-}
229{-# INLINE uncons #-}
230uncons :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (a, t m a))
230uncons :: (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
232
232
233------------------------------------------------------------------------------
233------------------------------------------------------------------------------
234-- Right Folds
234-- Right Folds
@@ -363,7 +363,7 @@ foldxM = IsStream.foldlMx'
363-- /Since: 0.8.0 (signature change)/
363-- /Since: 0.8.0 (signature change)/
364{-# INLINE foldlM' #-}
364{-# INLINE foldlM' #-}
365foldlM' :: Monad m => (b -> a -> m b) -> m b -> SerialT m a -> m b
365foldlM' :: 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
367
367
368------------------------------------------------------------------------------
368------------------------------------------------------------------------------
369-- Running a sink
369-- Running a sink
@@ -423,19 +423,6 @@ parseBreakD parser strm = do
423 (b, strmD) <- D.parseBreak parser (toStreamD strm)
423 (b, strmD) <- D.parseBreak parser (toStreamD strm)
424 return $! (b, fromStreamD strmD)
424 return $! (b, fromStreamD strmD)
425
425
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−
439------------------------------------------------------------------------------
426------------------------------------------------------------------------------
440-- Specific Fold Functions
427-- Specific Fold Functions
441------------------------------------------------------------------------------
428------------------------------------------------------------------------------
@@ -451,7 +438,7 @@ parseBreak p (SerialT strm) = fmap f $ K.parseBreak (PRD.fromParserK p) strm
451-- @since 0.1.0
438-- @since 0.1.0
452{-# INLINE mapM_ #-}
439{-# INLINE mapM_ #-}
453mapM_ :: Monad m => (a -> m b) -> SerialT m a -> m ()
440mapM_ :: 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
455
442
456-- |
443-- |
457-- > drain = mapM_ (\_ -> return ())
444-- > drain = mapM_ (\_ -> return ())
@@ -555,14 +542,14 @@ headElse x = D.headElse x . toStreamD
555-- @since 0.1.1
542-- @since 0.1.1
556{-# INLINE tail #-}
543{-# INLINE tail #-}
557tail :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (t m a))
544tail :: (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
559
546
560-- | Extract all but the last element of the stream, if any.
547-- | Extract all but the last element of the stream, if any.
561--
548--
562-- @since 0.5.0
549-- @since 0.5.0
563{-# INLINE init #-}
550{-# INLINE init #-}
564init :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (t m a))
551init :: (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
566
553
567-- | Extract the last element of the stream, if any.
554-- | Extract the last element of the stream, if any.
568--
555--
src/Streamly/Internal/Data/Stream/IsStream/Generate.hs+3−2
@@ -101,7 +101,7 @@ import Streamly.Internal.Data.Stream.IsStream.Common
101 , yield, yieldM, repeatM)
101 , yield, yieldM, repeatM)
102import Streamly.Internal.Data.Stream.IsStream.Type
102import Streamly.Internal.Data.Stream.IsStream.Type
103 (IsStream (..), fromSerial, consM, fromStreamD, fromStreamS)
103 (IsStream (..), fromSerial, consM, fromStreamD, fromStreamS)
104−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
104+import Streamly.Internal.Data.Stream.Serial (SerialT)
105import Streamly.Internal.Data.Stream.WSerial (WSerialT)
105import Streamly.Internal.Data.Stream.WSerial (WSerialT)
106import Streamly.Internal.Data.Stream.Zip (ZipSerialM)
106import Streamly.Internal.Data.Stream.Zip (ZipSerialM)
107import Streamly.Internal.Data.Time.Units (AbsTime , RelTime64, addToAbsTime64)
107import Streamly.Internal.Data.Time.Units (AbsTime , RelTime64, addToAbsTime64)
@@ -118,6 +118,7 @@ import qualified Streamly.Internal.Data.Stream.StreamK.Type as S
118#else
118#else
119import qualified Streamly.Internal.Data.Stream.StreamD.Generate as S
119import qualified Streamly.Internal.Data.Stream.StreamD.Generate as S
120#endif
120#endif
121+import qualified Streamly.Internal.Data.Stream.Type as Stream
121import qualified System.IO as IO
122import qualified System.IO as IO
122
123
123import Prelude hiding (iterate, replicate, repeat)
124import Prelude hiding (iterate, replicate, repeat)
@@ -652,7 +653,7 @@ fromCallback :: MonadAsync m => ((a -> m ()) -> m ()) -> SerialT m a
652fromCallback setCallback = concatM $ do
653fromCallback setCallback = concatM $ do
653 (callback, stream) <- Par.newCallbackStream
654 (callback, stream) <- Par.newCallbackStream
654 setCallback callback
655 setCallback callback
655− return $ SerialT stream
656+ return $ Stream.fromStreamK stream
656
657
657-- | Construct a stream by reading a 'Prim' 'IORef' repeatedly.
658-- | Construct a stream by reading a 'Prim' 'IORef' repeatedly.
658--
659--
src/Streamly/Internal/Data/Stream/IsStream/Type.hs+5−4
@@ -105,7 +105,7 @@ where
105
105
106import Streamly.Internal.Control.Concurrent (MonadAsync)
106import Streamly.Internal.Control.Concurrent (MonadAsync)
107import Streamly.Internal.Data.Fold.Type (Fold (..))
107import Streamly.Internal.Data.Fold.Type (Fold (..))
108−import Streamly.Internal.Data.Stream.Serial (SerialT(..), Serial)
108+import Streamly.Internal.Data.Stream.Serial (SerialT, Serial)
109import Streamly.Internal.Data.Stream.WSerial (WSerialT(..), WSerial)
109import Streamly.Internal.Data.Stream.WSerial (WSerialT(..), WSerial)
110import Streamly.Internal.Data.Stream.Async
110import Streamly.Internal.Data.Stream.Async
111 (AsyncT(..), Async, WAsyncT(..), WAsync)
111 (AsyncT(..), Async, WAsyncT(..), WAsync)
@@ -129,6 +129,7 @@ import qualified Streamly.Internal.Data.Stream.StreamK.Type as S
129#else
129#else
130import qualified Streamly.Internal.Data.Stream.StreamD.Type as S
130import qualified Streamly.Internal.Data.Stream.StreamD.Type as S
131#endif
131#endif
132+import qualified Streamly.Internal.Data.Stream.Type as Stream
132import qualified Streamly.Internal.Data.Stream.Zip as Zip
133import qualified Streamly.Internal.Data.Stream.Zip as Zip
133import qualified Streamly.Internal.Data.Stream.ZipAsync as ZipAsync
134import qualified Streamly.Internal.Data.Stream.ZipAsync as ZipAsync
134
135
@@ -463,9 +464,9 @@ foldStream st yld sng stp m =
463fromSerial :: IsStream t => SerialT m a -> t m a
464fromSerial :: IsStream t => SerialT m a -> t m a
464fromSerial = adapt
465fromSerial = adapt
465
466
466−instance IsStream SerialT where
467+instance IsStream Stream.Stream where
467− toStream = getSerialT
468+ toStream = Stream.toStreamK
468− fromStream = SerialT
469+ fromStream = Stream.fromStreamK
469
470
470 {-# INLINE consM #-}
471 {-# INLINE consM #-}
471 {-# SPECIALIZE consM :: IO a -> SerialT IO a -> SerialT IO a #-}
472 {-# 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
66
66
67import Streamly.Internal.Control.Concurrent (MonadAsync)
67import Streamly.Internal.Control.Concurrent (MonadAsync)
68import Streamly.Internal.Data.Fold.Type (Fold)
68import Streamly.Internal.Data.Fold.Type (Fold)
69−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
70import Streamly.Internal.Data.Stream.StreamD.Type (Step(..))
69import Streamly.Internal.Data.Stream.StreamD.Type (Step(..))
71import Streamly.Internal.Data.Stream.StreamK.Type (Stream)
70import Streamly.Internal.Data.Stream.StreamK.Type (Stream)
72
71
@@ -74,6 +73,7 @@ import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
74import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
73import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
75import qualified Streamly.Internal.Data.Stream.SVar.Generate as SVar
74import qualified Streamly.Internal.Data.Stream.SVar.Generate as SVar
76import qualified Streamly.Internal.Data.Stream.SVar.Eliminate as SVar
75import qualified Streamly.Internal.Data.Stream.SVar.Eliminate as SVar
76+import qualified Streamly.Internal.Data.Stream.Type as Stream
77
77
78import Streamly.Internal.Data.SVar
78import Streamly.Internal.Data.SVar
79
79
@@ -172,7 +172,7 @@ forkSVarPar ss m r = K.mkStream $ \st yld sng stp -> do
172 writeIORef (svarStopBy sv) $ Set.elemAt 0 set
172 writeIORef (svarStopBy sv) $ Set.elemAt 0 set
173 _ -> return ()
173 _ -> return ()
174 pushWorkerPar sv (runOne st{streamVar = Just sv} r)
174 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)
176
176
177{-# INLINE joinStreamVarPar #-}
177{-# INLINE joinStreamVarPar #-}
178joinStreamVarPar :: MonadAsync m
178joinStreamVarPar :: MonadAsync m
@@ -270,7 +270,7 @@ mkParallelK m = K.mkStream $ \st yld sng stp -> do
270 sv <- newParallelVar StopNone (adaptState st)
270 sv <- newParallelVar StopNone (adaptState st)
271 -- pushWorkerPar sv (runOne st{streamVar = Just sv} $ toStream m)
271 -- pushWorkerPar sv (runOne st{streamVar = Just sv} $ toStream m)
272 SVar.toSVarParallel st sv $ D.fromStreamK m
272 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
274
274
275-- | Same as 'mkParallel' but for StreamD stream.
275-- | Same as 'mkParallel' but for StreamD stream.
276--
276--
@@ -340,9 +340,9 @@ mkParallelD m = D.Stream step Nothing
340{-# INLINE tapAsyncK #-}
340{-# INLINE tapAsyncK #-}
341tapAsyncK :: MonadAsync m => (Stream m a -> m b) -> Stream m a -> Stream m a
341tapAsyncK :: MonadAsync m => (Stream m a -> m b) -> Stream m a -> Stream m a
342tapAsyncK f m = K.mkStream $ \st yld sng stp -> do
342tapAsyncK f m = K.mkStream $ \st yld sng stp -> do
343− sv <- SVar.newFoldSVar st (f . getSerialT)
343+ sv <- SVar.newFoldSVar st (f . Stream.toStreamK)
344 K.foldStreamShared st yld sng stp
344 K.foldStreamShared st yld sng stp
345− $ getSerialT (SVar.teeToSVar sv $ SerialT m)
345+ $ Stream.toStreamK (SVar.teeToSVar sv $ Stream.fromStreamK m)
346
346
347data TapState fs st a = TapInit | Tapping !fs st | TapDone st
347data TapState fs st a = TapInit | Tapping !fs st | TapDone st
348
348
src/Streamly/Internal/Data/Stream/SVar/Eliminate.hs+5−4
@@ -36,11 +36,12 @@ import Streamly.Internal.Control.ForkLifted (doFork)
36import Streamly.Internal.Data.Atomics (atomicModifyIORefCAS_)
36import Streamly.Internal.Data.Atomics (atomicModifyIORefCAS_)
37import Streamly.Internal.Data.Fold.SVar (write, writeLimited)
37import Streamly.Internal.Data.Fold.SVar (write, writeLimited)
38import Streamly.Internal.Data.Fold.Type (Fold(..))
38import Streamly.Internal.Data.Fold.Type (Fold(..))
39−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
39+import Streamly.Internal.Data.Stream.Serial (SerialT)
40import Streamly.Internal.Data.Time.Clock (Clock(Monotonic), getTime)
40import Streamly.Internal.Data.Time.Clock (Clock(Monotonic), getTime)
41
41
42import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
42import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
43import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
43import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
44+import qualified Streamly.Internal.Data.Stream.Type as Stream
44
45
45import Streamly.Internal.Data.SVar
46import Streamly.Internal.Data.SVar
46
47
@@ -214,7 +215,7 @@ newFoldSVar stt f = do
214 -- Add the producer thread-id to the SVar.
215 -- Add the producer thread-id to the SVar.
215 liftIO myThreadId >>= modifyThread sv
216 liftIO myThreadId >>= modifyThread sv
216
217
217− void $ doFork (void $ f $ SerialT $ fromProducer sv)
218+ void $ doFork (void $ f $ Stream.fromStreamK $ fromProducer sv)
218 (svarMrun sv)
219 (svarMrun sv)
219 (handleFoldException sv)
220 (handleFoldException sv)
220 return sv
221 return sv
@@ -369,8 +370,8 @@ pushToFold sv a = do
369{-# INLINE teeToSVar #-}
370{-# INLINE teeToSVar #-}
370teeToSVar :: MonadAsync m =>
371teeToSVar :: MonadAsync m =>
371 SVar K.Stream m a -> SerialT m a -> SerialT m a
372 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+teeToSVar svr m = Stream.fromStreamK $ K.mkStream $ \st yld sng stp -> do
373− K.foldStreamShared st yld sng stp (go False m)
374+ K.foldStreamShared st yld sng stp (go False $ Stream.toStreamK m)
374
375
375 where
376 where
376
377
src/Streamly/Internal/Data/Stream/SVar/Generate.hs+3−2
@@ -38,12 +38,13 @@ import Control.Monad.IO.Class (MonadIO(liftIO))
38import Data.IORef (newIORef, readIORef, mkWeakIORef, writeIORef)
38import Data.IORef (newIORef, readIORef, mkWeakIORef, writeIORef)
39import Data.Maybe (isNothing)
39import Data.Maybe (isNothing)
40import Streamly.Internal.Control.Concurrent (MonadAsync, askRunInIO)
40import Streamly.Internal.Control.Concurrent (MonadAsync, askRunInIO)
41−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
41+import Streamly.Internal.Data.Stream.Serial (SerialT)
42import Streamly.Internal.Data.Time.Clock (Clock(Monotonic), getTime)
42import Streamly.Internal.Data.Time.Clock (Clock(Monotonic), getTime)
43import System.Mem (performMajorGC)
43import System.Mem (performMajorGC)
44
44
45import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
45import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
46import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
46import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
47+import qualified Streamly.Internal.Data.Stream.Type as Stream
47
48
48import Streamly.Internal.Data.SVar
49import Streamly.Internal.Data.SVar
49
50
@@ -187,7 +188,7 @@ inspect $ hasNoTypeClassesExcept 'fromStreamVar
187{-# INLINE fromSVar #-}
188{-# INLINE fromSVar #-}
188fromSVar :: MonadAsync m => SVar K.Stream m a -> SerialT m a
189fromSVar :: MonadAsync m => SVar K.Stream m a -> SerialT m a
189fromSVar sv =
190fromSVar sv =
190− SerialT $ K.mkStream $ \st yld sng stp -> do
191+ Stream.fromStreamK $ K.mkStream $ \st yld sng stp -> do
191 ref <- liftIO $ newIORef ()
192 ref <- liftIO $ newIORef ()
192 _ <- liftIO $ mkWeakIORef ref hook
193 _ <- liftIO $ mkWeakIORef ref hook
193 -- We pass a copy of sv to fromStreamVar, so that we know that it has
194 -- 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
25import Data.Semigroup (Semigroup(..))
25import Data.Semigroup (Semigroup(..))
26#endif
26#endif
27import Streamly.Internal.Control.Concurrent (MonadAsync)
27import Streamly.Internal.Control.Concurrent (MonadAsync)
28−import Streamly.Internal.Data.Stream.Serial (SerialT(..))
29import Streamly.Internal.Data.Stream.StreamK.Type (Stream)
28import Streamly.Internal.Data.Stream.StreamK.Type (Stream)
30
29
31import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
30import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
@@ -34,6 +33,7 @@ import qualified Streamly.Internal.Data.Stream.StreamD as D
34import qualified Streamly.Internal.Data.Stream.Serial as Serial
33import qualified Streamly.Internal.Data.Stream.Serial as Serial
35import qualified Streamly.Internal.Data.Stream.SVar.Eliminate as SVar
34import qualified Streamly.Internal.Data.Stream.SVar.Eliminate as SVar
36import qualified Streamly.Internal.Data.Stream.SVar.Generate as SVar
35import qualified Streamly.Internal.Data.Stream.SVar.Generate as SVar
36+import qualified Streamly.Internal.Data.Stream.Type as Stream
37import Streamly.Internal.Data.SVar
37import Streamly.Internal.Data.SVar
38
38
39import Prelude hiding (map, repeat, zipWith, errorWithoutStackTrace)
39import Prelude hiding (map, repeat, zipWith, errorWithoutStackTrace)
@@ -63,7 +63,8 @@ zipAsyncWithMK :: MonadAsync m
63zipAsyncWithMK f m1 m2 = K.mkStream $ \st yld sng stp -> do
63zipAsyncWithMK f m1 m2 = K.mkStream $ \st yld sng stp -> do
64 sv <- newParallelVar StopNone (adaptState st)
64 sv <- newParallelVar StopNone (adaptState st)
65 SVar.toSVarParallel (adaptState st) sv $ D.fromStreamK m2
65 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))
67
68
68-- XXX Should we rename this to zipParWith or zipParallelWith? This can happen
69-- XXX Should we rename this to zipParWith or zipParallelWith? This can happen
69-- along with the change of behvaior to end the stream concurrently.
70-- along with the change of behvaior to end the stream concurrently.
@@ -122,10 +123,11 @@ consMZipAsync m (ZipAsyncM r) = ZipAsyncM $ K.consM m r
122
123
123instance Monad m => Functor (ZipAsyncM m) where
124instance Monad m => Functor (ZipAsyncM m) where
124 {-# INLINE fmap #-}
125 {-# 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)
126
128
127instance MonadAsync m => Applicative (ZipAsyncM m) where
129instance MonadAsync m => Applicative (ZipAsyncM m) where
128− pure = ZipAsyncM . getSerialT . Serial.repeat
130+ pure = ZipAsyncM . Stream.toStreamK . Serial.repeat
129
131
130 {-# INLINE (<*>) #-}
132 {-# INLINE (<*>) #-}
131 ZipAsyncM m1 <*> ZipAsyncM m2 = ZipAsyncM $ zipAsyncWithK id m1 m2
133 ZipAsyncM m1 <*> ZipAsyncM m2 = ZipAsyncM $ zipAsyncWithK id m1 m2