on master help sign in
Inline PreludeCommon into IsStream.Type
cbe6eb1Adithya Kumar
src/Streamly/Internal/Data/Stream/IsStream/Type.hs+126−4
@@ -140,8 +140,6 @@ import qualified Streamly.Internal.Data.Stream.ZipAsync as ZipAsync
140import Prelude hiding (foldr, repeat)
140import Prelude hiding (foldr, repeat)
141
141
142#include "inline.hs"
142#include "inline.hs"
143−#define USE_IS_STREAM
144−#include "PreludeCommon.hs"
145
143
146------------------------------------------------------------------------------
144------------------------------------------------------------------------------
147-- Types that can behave as a Stream
145-- Types that can behave as a Stream
@@ -231,8 +229,8 @@ adapt :: (IsStream t1, IsStream t2) => t1 m a -> t2 m a
231adapt = fromStream . toStream
229adapt = fromStream . toStream
232
230
233{-# INLINE fromStreamD #-}
231{-# INLINE fromStreamD #-}
234−fromStreamD :: (IS_STREAM Monad m) => D.Stream m a -> STREAM m a
232+fromStreamD :: (IsStream t, Monad m) => D.Stream m a -> t m a
235−fromStreamD = FROM_STREAM . D.toStreamK
233+fromStreamD = fromStream . D.toStreamK
236
234
237-- | Adapt a polymorphic consM operation to a StreamK cons operation
235-- | Adapt a polymorphic consM operation to a StreamK cons operation
238{-# INLINE toConsK #-}
236{-# INLINE toConsK #-}
@@ -240,6 +238,80 @@ toConsK :: IsStream t =>
240 (m a -> t m a -> t m a) -> m a -> K.Stream m a -> K.Stream m a
238 (m a -> t m a -> t m a) -> m a -> K.Stream m a -> K.Stream m a
241toConsK cns x xs = toStream $ x `cns` fromStream xs
239toConsK cns x xs = toStream $ x `cns` fromStream xs
242
240
241+------------------------------------------------------------------------------
242+-- Conversion to and from direct style stream
243+------------------------------------------------------------------------------
244+
245+-- These definitions are dependent on what is imported as S
246+{-# INLINE fromStreamS #-}
247+fromStreamS :: (IsStream t, Monad m) => S.Stream m a -> t m a
248+fromStreamS = fromStream . S.toStreamK
249+
250+{-# INLINE toStreamS #-}
251+toStreamS :: (IsStream t, Monad m) => t m a -> S.Stream m a
252+toStreamS = S.fromStreamK . toStream
253+
254+{-# INLINE toStreamD #-}
255+toStreamD :: (IsStream t, Monad m) => t m a -> D.Stream m a
256+toStreamD = D.fromStreamK . toStream
257+
258+------------------------------------------------------------------------------
259+-- Elimination
260+------------------------------------------------------------------------------
261+
262+{-# INLINE_EARLY drain #-}
263+drain :: (IsStream t, Monad m) => t m a -> m ()
264+drain m = D.drain $ D.fromStreamK (toStream m)
265+{-# RULES "drain fallback to CPS" [1]
266+ forall a. D.drain (D.fromStreamK a) = K.drain a #-}
267+
268+------------------------------------------------------------------------------
269+-- Comparison
270+------------------------------------------------------------------------------
271+
272+-- | Compare two streams for equality
273+--
274+-- @since 0.5.3
275+{-# INLINE eqBy #-}
276+eqBy :: (IsStream t, Monad m) =>
277+ (a -> b -> Bool) -> t m a -> t m b -> m Bool
278+eqBy f m1 m2 = D.eqBy f (toStreamD m1) (toStreamD m2)
279+
280+-- | Compare two streams
281+--
282+-- @since 0.5.3
283+{-# INLINE cmpBy #-}
284+cmpBy
285+ :: (IsStream t, Monad m)
286+ => (a -> b -> Ordering) -> t m a -> t m b -> m Ordering
287+cmpBy f m1 m2 = D.cmpBy f (toStreamD m1) (toStreamD m2)
288+
289+------------------------------------------------------------------------------
290+-- List Conversions
291+------------------------------------------------------------------------------
292+
293+-- |
294+-- @
295+-- fromList = 'Prelude.foldr' 'K.cons' 'K.nil'
296+-- @
297+--
298+-- Construct a stream from a list of pure values. This is more efficient than
299+-- 'K.fromFoldable' for serial streams.
300+--
301+-- @since 0.4.0
302+{-# INLINE_EARLY fromList #-}
303+fromList :: (Monad m, IsStream t) => [a] -> t m a
304+fromList = fromStreamS . S.fromList
305+{-# RULES "fromList fallback to StreamK" [1]
306+ forall a. S.toStreamK (S.fromList a) = K.fromFoldable a #-}
307+
308+-- | Convert a stream into a list in the underlying monad.
309+--
310+-- @since 0.1.0
311+{-# INLINE toList #-}
312+toList :: (IsStream t, Monad m) => t m a -> m [a]
313+toList m = S.toList $ toStreamS m
314+
243------------------------------------------------------------------------------
315------------------------------------------------------------------------------
244-- Building a stream
316-- Building a stream
245------------------------------------------------------------------------------
317------------------------------------------------------------------------------
@@ -288,6 +360,56 @@ mkStreamStream
288 -> K.Stream m a
360 -> K.Stream m a
289mkStreamStream = K.MkStream
361mkStreamStream = K.MkStream
290
362
363+------------------------------------------------------------------------------
364+-- Folds
365+------------------------------------------------------------------------------
366+
367+{-# INLINE foldrM #-}
368+foldrM :: (IsStream t, Monad m) => (a -> m b -> m b) -> m b -> t m a -> m b
369+foldrM step acc m = S.foldrM step acc $ toStreamS m
370+
371+{-# INLINE foldrMx #-}
372+foldrMx :: (IsStream t, Monad m)
373+ => (a -> m x -> m x) -> m x -> (m x -> m b) -> t m a -> m b
374+foldrMx step final project m = D.foldrMx step final project $ toStreamD m
375+
376+{-# INLINE foldr #-}
377+foldr :: (IsStream t, Monad m) => (a -> b -> b) -> b -> t m a -> m b
378+foldr f z = foldrM (\a b -> f a <$> b) (return z)
379+
380+-- | Like 'foldlx'', but with a monadic step function.
381+--
382+-- @since 0.7.0
383+{-# INLINE foldlMx' #-}
384+foldlMx' ::
385+ (IsStream t, Monad m)
386+ => (x -> a -> m x) -> m x -> (x -> m b) -> t m a -> m b
387+foldlMx' step begin done m = S.foldlMx' step begin done $ toStreamS m
388+
389+-- | Strict left fold with an extraction function. Like the standard strict
390+-- left fold, but applies a user supplied extraction function (the third
391+-- argument) to the folded value at the end. This is designed to work with the
392+-- @foldl@ library. The suffix @x@ is a mnemonic for extraction.
393+--
394+-- @since 0.7.0
395+{-# INLINE foldlx' #-}
396+foldlx' ::
397+ (IsStream t, Monad m) => (x -> a -> x) -> x -> (x -> b) -> t m a -> m b
398+foldlx' step begin done m = S.foldlx' step begin done $ toStreamS m
399+
400+-- | Strict left associative fold.
401+--
402+-- @since 0.2.0
403+{-# INLINE foldl' #-}
404+foldl' ::
405+ (IsStream t, Monad m) => (b -> a -> b) -> b -> t m a -> m b
406+foldl' step begin m = S.foldl' step begin $ toStreamS m
407+
408+
409+{-# INLINE fold #-}
410+fold :: (IsStream t, Monad m) => Fold m a b -> t m a -> m b
411+fold fld m = S.fold fld $ toStreamS m
412+
291------------------------------------------------------------------------------
413------------------------------------------------------------------------------
292-- Folding a stream
414-- Folding a stream
293------------------------------------------------------------------------------
415------------------------------------------------------------------------------
src/Streamly/Internal/Data/Stream/PreludeCommon.hsdeleted−139
@@ -1,139 +0,0 @@
1−------------------------------------------------------------------------------
2−-- Conversion to and from direct style stream
3−------------------------------------------------------------------------------
4−
5−#ifdef USE_IS_STREAM
6−#define IS_STREAM IsStream t,
7−#define FROM_STREAM fromStream
8−#define TO_STREAM toStream
9−#define STREAM t
10−#else
11−#define IS_STREAM
12−#define FROM_STREAM id
13−#define TO_STREAM id
14−#define STREAM K.Stream
15−#endif
16−
17−-- These definitions are dependent on what is imported as S
18−{-# INLINE fromStreamS #-}
19−fromStreamS :: (IS_STREAM Monad m) => S.Stream m a -> STREAM m a
20−fromStreamS = FROM_STREAM . S.toStreamK
21−
22−{-# INLINE toStreamS #-}
23−toStreamS :: (IS_STREAM Monad m) => STREAM m a -> S.Stream m a
24−toStreamS = S.fromStreamK . TO_STREAM
25−
26−{-# INLINE toStreamD #-}
27−toStreamD :: (IS_STREAM Monad m) => STREAM m a -> D.Stream m a
28−toStreamD = D.fromStreamK . TO_STREAM
29−
30−------------------------------------------------------------------------------
31−-- Conversions
32−------------------------------------------------------------------------------
33−
34−{-# INLINE_EARLY drain #-}
35−drain :: (IS_STREAM Monad m) => STREAM m a -> m ()
36−drain m = D.drain $ D.fromStreamK (TO_STREAM m)
37−{-# RULES "drain fallback to CPS" [1]
38− forall a. D.drain (D.fromStreamK a) = K.drain a #-}
39−
40−------------------------------------------------------------------------------
41−-- Conversions
42−------------------------------------------------------------------------------
43−
44−-- |
45−-- @
46−-- fromList = 'Prelude.foldr' 'K.cons' 'K.nil'
47−-- @
48−--
49−-- Construct a stream from a list of pure values. This is more efficient than
50−-- 'K.fromFoldable' for serial streams.
51−--
52−-- @since 0.4.0
53−{-# INLINE_EARLY fromList #-}
54−#ifdef USE_IS_STREAM
55−fromList :: (Monad m, IsStream t) => [a] -> STREAM m a
56−#else
57−fromList :: Monad m => [a] -> STREAM m a
58−#endif
59−fromList = fromStreamS . S.fromList
60−{-# RULES "fromList fallback to StreamK" [1]
61− forall a. S.toStreamK (S.fromList a) = K.fromFoldable a #-}
62−
63−-- | Convert a stream into a list in the underlying monad.
64−--
65−-- @since 0.1.0
66−{-# INLINE toList #-}
67−toList :: (IS_STREAM Monad m) => STREAM m a -> m [a]
68−toList m = S.toList $ toStreamS m
69−
70−------------------------------------------------------------------------------
71−-- Folds
72−------------------------------------------------------------------------------
73−
74−{-# INLINE foldrM #-}
75−foldrM :: (IS_STREAM Monad m) => (a -> m b -> m b) -> m b -> STREAM m a -> m b
76−foldrM step acc m = S.foldrM step acc $ toStreamS m
77−
78−{-# INLINE foldrMx #-}
79−foldrMx :: (IS_STREAM Monad m)
80− => (a -> m x -> m x) -> m x -> (m x -> m b) -> STREAM m a -> m b
81−foldrMx step final project m = D.foldrMx step final project $ toStreamD m
82−
83−{-# INLINE foldr #-}
84−foldr :: (IS_STREAM Monad m) => (a -> b -> b) -> b -> STREAM m a -> m b
85−foldr f z = foldrM (\a b -> f a <$> b) (return z)
86−
87−-- | Like 'foldlx'', but with a monadic step function.
88−--
89−-- @since 0.7.0
90−{-# INLINE foldlMx' #-}
91−foldlMx' ::
92− (IS_STREAM Monad m)
93− => (x -> a -> m x) -> m x -> (x -> m b) -> STREAM m a -> m b
94−foldlMx' step begin done m = S.foldlMx' step begin done $ toStreamS m
95−
96−-- | Strict left fold with an extraction function. Like the standard strict
97−-- left fold, but applies a user supplied extraction function (the third
98−-- argument) to the folded value at the end. This is designed to work with the
99−-- @foldl@ library. The suffix @x@ is a mnemonic for extraction.
100−--
101−-- @since 0.7.0
102−{-# INLINE foldlx' #-}
103−foldlx' ::
104− (IS_STREAM Monad m) => (x -> a -> x) -> x -> (x -> b) -> STREAM m a -> m b
105−foldlx' step begin done m = S.foldlx' step begin done $ toStreamS m
106−
107−-- | Strict left associative fold.
108−--
109−-- @since 0.2.0
110−{-# INLINE foldl' #-}
111−foldl' ::
112− (IS_STREAM Monad m) => (b -> a -> b) -> b -> STREAM m a -> m b
113−foldl' step begin m = S.foldl' step begin $ toStreamS m
114−
115−
116−{-# INLINE fold #-}
117−fold :: (IS_STREAM Monad m) => Fold m a b -> STREAM m a -> m b
118−fold fld m = S.fold fld $ toStreamS m
119−
120−------------------------------------------------------------------------------
121−-- Comparison
122−------------------------------------------------------------------------------
123−
124−-- | Compare two streams for equality
125−--
126−-- @since 0.5.3
127−{-# INLINE eqBy #-}
128−eqBy :: (IS_STREAM Monad m) =>
129− (a -> b -> Bool) -> STREAM m a -> STREAM m b -> m Bool
130−eqBy f m1 m2 = D.eqBy f (toStreamD m1) (toStreamD m2)
131−
132−-- | Compare two streams
133−--
134−-- @since 0.5.3
135−{-# INLINE cmpBy #-}
136−cmpBy
137− :: (IS_STREAM Monad m)
138− => (a -> b -> Ordering) -> STREAM m a -> STREAM m b -> m Ordering
139−cmpBy f m1 m2 = D.cmpBy f (toStreamD m1) (toStreamD m2)
streamly.cabal−1
@@ -87,7 +87,6 @@ extra-source-files:
87
87
88 -- This is duplicated
88 -- This is duplicated
89 src/Streamly/Internal/Data/Stream/Instances.hs
89 src/Streamly/Internal/Data/Stream/Instances.hs
90− src/Streamly/Internal/Data/Stream/PreludeCommon.hs
91 src/Streamly/Internal/Data/Array/ArrayMacros.h
90 src/Streamly/Internal/Data/Array/ArrayMacros.h
92
91
93 src/Streamly/Internal/Data/Array/PrimInclude.hs
92 src/Streamly/Internal/Data/Array/PrimInclude.hs