@@ -140,8 +140,6 @@ import qualified Streamly.Internal.Data.Stream.ZipAsync as ZipAsync
140140import Prelude hiding (foldr, repeat)
141141
142142#include "inline.hs"
143−#define USE_IS_STREAM
144−#include "PreludeCommon.hs"
145143
146144------------------------------------------------------------------------------
147145-- Types that can behave as a Stream
@@ -231,8 +229,8 @@ adapt :: (IsStream t1, IsStream t2) => t1 m a -> t2 m a
231229adapt = fromStream . toStream
232230
233231{-# INLINE fromStreamD #-}
234−fromStreamD :: (IS_STREAM Monad m) => D.Stream m a -> STREAM m a
235−fromStreamD = FROM_STREAM . D.toStreamK
232+fromStreamD :: (IsStream t, Monad m) => D.Stream m a -> t m a
233+fromStreamD = fromStream . D.toStreamK
236234
237235-- | Adapt a polymorphic consM operation to a StreamK cons operation
238236{-# INLINE toConsK #-}
@@ -240,6 +238,80 @@ toConsK :: IsStream t =>
240238 (m a -> t m a -> t m a) -> m a -> K.Stream m a -> K.Stream m a
241239toConsK cns x xs = toStream $ x `cns` fromStream xs
242240
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+
243315------------------------------------------------------------------------------
244316-- Building a stream
245317------------------------------------------------------------------------------
@@ -288,6 +360,56 @@ mkStreamStream
288360 -> K.Stream m a
289361mkStreamStream = K.MkStream
290362
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+
291413------------------------------------------------------------------------------
292414-- Folding a stream
293415------------------------------------------------------------------------------