@@ -93,13 +93,14 @@ import qualified Prelude
9393import qualified System.IO as IO
9494
9595import Streamly.Core
96−import Streamly.Streams
96+import Streamly.Streams hiding (runStream)
97+
9798------------------------------------------------------------------------------
9899-- Construction
99100------------------------------------------------------------------------------
100101
101102-- | Build a Stream by unfolding pure steps starting from a seed.
102−unfoldr :: Streaming t => (b -> Maybe (a, b)) -> b -> t m a
103+unfoldr :: IsStream t => (b -> Maybe (a, b)) -> b -> t m a
103104unfoldr step = fromStream . go
104105 where
105106 go s = Stream $ \_ stp yld ->
@@ -108,7 +109,7 @@ unfoldr step = fromStream . go
108109 Just (a, b) -> yld a (Just (go b))
109110
110111-- | Build a Stream by unfolding monadic steps starting from a seed.
111−unfoldrM :: (Streaming t, Monad m) => (b -> m (Maybe (a, b))) -> b -> t m a
112+unfoldrM :: (IsStream t, Monad m) => (b -> m (Maybe (a, b))) -> b -> t m a
112113unfoldrM step = fromStream . go
113114 where
114115 go s = Stream $ \_ stp yld -> do
@@ -120,17 +121,17 @@ unfoldrM step = fromStream . go
120121-- XXX need eachInterleaved, eachAsync, eachParallel
121122-- | Same as @foldWith (<>)@ but more efficient.
122123{-# INLINE each #-}
123−each :: (Streaming t, Foldable f) => f a -> t m a
124+each :: (IsStream t, Foldable f) => f a -> t m a
124125each = Prelude.foldr cons nil
125126
126127-- | Iterate a pure function from a seed value, streaming the results forever
127−iterate :: Streaming t => (a -> a) -> a -> t m a
128+iterate :: IsStream t => (a -> a) -> a -> t m a
128129iterate step = fromStream . go
129130 where
130131 go s = scons s (Just (go (step s)))
131132
132133-- | Iterate a monadic function from a seed value, streaming the results forever
133−iterateM :: (Streaming t, Monad m) => (a -> m a) -> a -> t m a
134+iterateM :: (IsStream t, Monad m) => (a -> m a) -> a -> t m a
134135iterateM step = fromStream . go
135136 where
136137 go s = Stream $ \_ _ yld -> do
@@ -138,7 +139,7 @@ iterateM step = fromStream . go
138139 yld s (Just (go a))
139140
140141-- | Read lines from an IO Handle into a stream of Strings.
141−fromHandle :: (Streaming t, MonadIO m) => IO.Handle -> t m String
142+fromHandle :: (IsStream t, MonadIO m) => IO.Handle -> t m String
142143fromHandle h = fromStream go
143144 where
144145 go = Stream $ \_ stp yld -> do
@@ -156,7 +157,7 @@ fromHandle h = fromStream go
156157-- Parallel variants of folds?
157158
158159-- | Right fold.
159−foldr :: (Streaming t, Monad m) => (a -> b -> b) -> b -> t m a -> m b
160+foldr :: (IsStream t, Monad m) => (a -> b -> b) -> b -> t m a -> m b
160161foldr step acc m = go (toStream m)
161162 where
162163 go m1 =
@@ -167,7 +168,7 @@ foldr step acc m = go (toStream m)
167168
168169-- | Right fold with a monadic step function. See 'toList' for an example use.
169170{-# INLINE foldrM #-}
170−foldrM :: (Streaming t, Monad m) => (a -> b -> m b) -> b -> t m a -> m b
171+foldrM :: (IsStream t, Monad m) => (a -> b -> m b) -> b -> t m a -> m b
171172foldrM step acc m = go (toStream m)
172173 where
173174 go m1 =
@@ -178,7 +179,7 @@ foldrM step acc m = go (toStream m)
178179
179180-- | Scan left. A strict left fold which accumulates the result of its reduction steps inside a stream, from left.
180181{-# INLINE scan #-}
181−scan :: Streaming t => (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
182+scan :: IsStream t => (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
182183scan step begin done m = cons (done begin) $ fromStream $ go (toStream m) begin
183184 where
184185 go m1 !acc = Stream $ \_ stp yld ->
@@ -192,7 +193,7 @@ scan step begin done m = cons (done begin) $ fromStream $ go (toStream m) begin
192193-- | Strict left fold. This is typed to work with the foldl package. To use
193194-- it normally just pass 'id' as the third argument.
194195{-# INLINE foldl #-}
195−foldl :: (Streaming t, Monad m)
196+foldl :: (IsStream t, Monad m)
196197 => (x -> a -> x) -> x -> (x -> b) -> t m a -> m b
197198foldl step begin done m = get $ go (toStream m) begin
198199 where
@@ -218,7 +219,7 @@ foldl step begin done m = get $ go (toStream m) begin
218219-- XXX replace the recursive "go" with explicit continuations.
219220-- | Strict left fold, with monadic step function. This is typed to work
220221-- with the foldl package. To use directly pass 'id' as the third argument.
221−foldlM :: (Streaming t, Monad m)
222+foldlM :: (IsStream t, Monad m)
222223 => (x -> a -> m x) -> m x -> (x -> m b) -> t m a -> m b
223224foldlM step begin done m = go begin (toStream m)
224225 where
@@ -231,7 +232,7 @@ foldlM step begin done m = go begin (toStream m)
231232-- | Decompose a stream into its head and tail. If the stream is empty, returns
232233-- 'Nothing'. If the stream is non-empty, returns 'Just (a, ma)', where 'a' is
233234-- the head of the stream and 'ma' its tail.
234−uncons :: (Streaming t, Monad m) => t m a -> m (Maybe (a, t m a))
235+uncons :: (IsStream t, Monad m) => t m a -> m (Maybe (a, t m a))
235236uncons m =
236237 let stop = return Nothing
237238 yield a Nothing = return (Just (a, nil))
@@ -239,7 +240,7 @@ uncons m =
239240 in (runStream (toStream m)) Nothing stop yield
240241
241242-- | Write a stream of Strings to an IO Handle.
242−toHandle :: (Streaming t, MonadIO m) => IO.Handle -> t m String -> m ()
243+toHandle :: (IsStream t, MonadIO m) => IO.Handle -> t m String -> m ()
243244toHandle h m = go (toStream m)
244245 where
245246 go m1 =
@@ -254,12 +255,12 @@ toHandle h m = go (toStream m)
254255
255256-- | Convert a stream into a list in the underlying monad.
256257{-# INLINABLE toList #-}
257−toList :: (Streaming t, Monad m) => t m a -> m [a]
258+toList :: (IsStream t, Monad m) => t m a -> m [a]
258259toList = foldrM (\a xs -> return (a : xs)) []
259260
260261-- | Take first 'n' elements from the stream and discard the rest.
261262{-# INLINE take #-}
262−take :: Streaming t => Int -> t m a -> t m a
263+take :: IsStream t => Int -> t m a -> t m a
263264take n m = fromStream $ go n (toStream m)
264265 where
265266 go n1 m1 = Stream $ \ctx stp yld ->
@@ -269,7 +270,7 @@ take n m = fromStream $ go n (toStream m)
269270
270271-- | Include only those elements that pass a predicate.
271272{-# INLINE filter #-}
272−filter :: Streaming t => (a -> Bool) -> t m a -> t m a
273+filter :: IsStream t => (a -> Bool) -> t m a -> t m a
273274filter p m = fromStream $ go (toStream m)
274275 where
275276 go m1 = Stream $ \ctx stp yld ->
@@ -281,7 +282,7 @@ filter p m = fromStream $ go (toStream m)
281282
282283-- | End the stream as soon as the predicate fails on an element.
283284{-# INLINE takeWhile #-}
284−takeWhile :: Streaming t => (a -> Bool) -> t m a -> t m a
285+takeWhile :: IsStream t => (a -> Bool) -> t m a -> t m a
285286takeWhile p m = fromStream $ go (toStream m)
286287 where
287288 go m1 = Stream $ \ctx stp yld ->
@@ -292,7 +293,7 @@ takeWhile p m = fromStream $ go (toStream m)
292293 in (runStream m1) ctx stp yield
293294
294295-- | Discard first 'n' elements from the stream and take the rest.
295−drop :: Streaming t => Int -> t m a -> t m a
296+drop :: IsStream t => Int -> t m a -> t m a
296297drop n m = fromStream $ go n (toStream m)
297298 where
298299 go n1 m1 = Stream $ \ctx stp yld ->
@@ -306,7 +307,7 @@ drop n m = fromStream $ go n (toStream m)
306307-- | Drop elements in the stream as long as the predicate succeeds and then
307308-- take the rest of the stream.
308309{-# INLINE dropWhile #-}
309−dropWhile :: Streaming t => (a -> Bool) -> t m a -> t m a
310+dropWhile :: IsStream t => (a -> Bool) -> t m a -> t m a
310311dropWhile p m = fromStream $ go (toStream m)
311312 where
312313 go m1 = Stream $ \ctx stp yld ->
@@ -317,7 +318,7 @@ dropWhile p m = fromStream $ go (toStream m)
317318 in (runStream m1) ctx stp yield
318319
319320-- | Determine whether all elements of a stream satisfy a predicate.
320−all :: (Streaming t, Monad m) => (a -> Bool) -> t m a -> m Bool
321+all :: (IsStream t, Monad m) => (a -> Bool) -> t m a -> m Bool
321322all p m = go (toStream m)
322323 where
323324 go m1 =
@@ -328,7 +329,7 @@ all p m = go (toStream m)
328329 in (runStream m1) Nothing (return True) yield
329330
330331-- | Determine whether any of the elements of a stream satisfy a predicate.
331−any :: (Streaming t, Monad m) => (a -> Bool) -> t m a -> m Bool
332+any :: (IsStream t, Monad m) => (a -> Bool) -> t m a -> m Bool
332333any p m = go (toStream m)
333334 where
334335 go m1 =
@@ -339,22 +340,22 @@ any p m = go (toStream m)
339340 in (runStream m1) Nothing (return False) yield
340341
341342-- | Determine the sum of all elements of a stream of numbers
342−sum :: (Streaming t, Monad m, Num a) => t m a -> m a
343+sum :: (IsStream t, Monad m, Num a) => t m a -> m a
343344sum = foldl (+) 0 id
344345
345346-- | Determine the product of all elements of a stream of numbers
346−product :: (Streaming t, Monad m, Num a) => t m a -> m a
347+product :: (IsStream t, Monad m, Num a) => t m a -> m a
347348product = foldl (*) 1 id
348349
349350-- | Extract the first element of the stream, if any.
350−head :: (Streaming t, Monad m) => t m a -> m (Maybe a)
351+head :: (IsStream t, Monad m) => t m a -> m (Maybe a)
351352head m =
352353 let stop = return Nothing
353354 yield a _ = return (Just a)
354355 in (runStream (toStream m)) Nothing stop yield
355356
356357-- | Extract all but the first element of the stream, if any.
357−tail :: (Streaming t, Monad m) => t m a -> m (Maybe (t m a))
358+tail :: (IsStream t, Monad m) => t m a -> m (Maybe (t m a))
358359tail m =
359360 let stop = return Nothing
360361 yield _ Nothing = return $ Just nil
@@ -363,18 +364,18 @@ tail m =
363364
364365-- | Extract the last element of the stream, if any.
365366{-# INLINE last #-}
366−last :: (Streaming t, Monad m) => t m a -> m (Maybe a)
367+last :: (IsStream t, Monad m) => t m a -> m (Maybe a)
367368last = foldl (\_ y -> Just y) Nothing id
368369
369370-- | Determine whether the stream is empty.
370−null :: (Streaming t, Monad m) => t m a -> m Bool
371+null :: (IsStream t, Monad m) => t m a -> m Bool
371372null m =
372373 let stop = return True
373374 yield _ _ = return False
374375 in (runStream (toStream m)) Nothing stop yield
375376
376377-- | Determine whether an element is present in the stream.
377−elem :: (Streaming t, Monad m, Eq a) => a -> t m a -> m Bool
378+elem :: (IsStream t, Monad m, Eq a) => a -> t m a -> m Bool
378379elem e m = go (toStream m)
379380 where
380381 go m1 =
@@ -384,7 +385,7 @@ elem e m = go (toStream m)
384385 in (runStream m1) Nothing stop yield
385386
386387-- | Determine whether an element is not present in the stream.
387−notElem :: (Streaming t, Monad m, Eq a) => a -> t m a -> m Bool
388+notElem :: (IsStream t, Monad m, Eq a) => a -> t m a -> m Bool
388389notElem e m = go (toStream m)
389390 where
390391 go m1 =
@@ -394,12 +395,12 @@ notElem e m = go (toStream m)
394395 in (runStream m1) Nothing stop yield
395396
396397-- | Determine the length of the stream.
397−length :: (Streaming t, Monad m) => t m a -> m Int
398+length :: (IsStream t, Monad m) => t m a -> m Int
398399length = foldl (\n _ -> n + 1) 0 id
399400
400401-- | Returns the elements of the stream in reverse order.
401402-- The stream must be finite.
402−reverse :: (Streaming t) => t m a -> t m a
403+reverse :: (IsStream t) => t m a -> t m a
403404reverse m = fromStream $ go Nothing (toStream m)
404405 where
405406 go rev rest = Stream $ \svr stp yld ->
@@ -412,7 +413,7 @@ reverse m = fromStream $ go Nothing (toStream m)
412413
413414-- XXX replace the recursive "go" with continuation
414415-- | Determine the minimum element in a stream.
415−minimum :: (Streaming t, Monad m, Ord a) => t m a -> m (Maybe a)
416+minimum :: (IsStream t, Monad m, Ord a) => t m a -> m (Maybe a)
416417minimum m = go Nothing (toStream m)
417418 where
418419 go r m1 =
@@ -427,7 +428,7 @@ minimum m = go Nothing (toStream m)
427428
428429-- XXX replace the recursive "go" with continuation
429430-- | Determine the maximum element in a stream.
430−maximum :: (Streaming t, Monad m, Ord a) => t m a -> m (Maybe a)
431+maximum :: (IsStream t, Monad m, Ord a) => t m a -> m (Maybe a)
431432maximum m = go Nothing (toStream m)
432433 where
433434 go r m1 =
@@ -449,7 +450,7 @@ maximum m = go Nothing (toStream m)
449450-- | Replace each element of the stream with the result of a monadic action
450451-- applied on the element.
451452{-# INLINE mapM #-}
452−mapM :: (Streaming t, Monad m) => (a -> m b) -> t m a -> t m b
453+mapM :: (IsStream t, Monad m) => (a -> m b) -> t m a -> t m b
453454mapM f m = fromStream $ go (toStream m)
454455 where
455456 go m1 = Stream $ \_ stp yld ->
@@ -460,7 +461,7 @@ mapM f m = fromStream $ go (toStream m)
460461
461462-- | Apply a monadic action to each element of the stream and discard the
462463-- output of the action.
463−mapM_ :: (Streaming t, Monad m) => (a -> m b) -> t m a -> m ()
464+mapM_ :: (IsStream t, Monad m) => (a -> m b) -> t m a -> m ()
464465mapM_ f m = go (toStream m)
465466 where
466467 go m1 =
@@ -471,7 +472,7 @@ mapM_ f m = go (toStream m)
471472
472473-- | Reduce a stream of monadic actions to a stream of the output of those
473474-- actions.
474−sequence :: (Streaming t, Monad m) => t m (m a) -> t m a
475+sequence :: (IsStream t, Monad m) => t m (m a) -> t m a
475476sequence m = fromStream $ go (toStream m)
476477 where
477478 go m1 = Stream $ \_ stp yld ->
@@ -481,7 +482,7 @@ sequence m = fromStream $ go (toStream m)
481482 in (runStream m1) Nothing stop yield
482483
483484-- | Generate a stream by performing an action @n@ times.
484−replicateM :: (Streaming t, Monad m) => Int -> m a -> t m a
485+replicateM :: (IsStream t, Monad m) => Int -> m a -> t m a
485486replicateM n m = fromStream $ go n
486487 where
487488 go cnt = Stream $ \_ stp yld ->
@@ -494,7 +495,7 @@ replicateM n m = fromStream $ go n
494495------------------------------------------------------------------------------
495496
496497-- | Zip two streams serially using a monadic zipping function.
497−zipWithM :: Streaming t => (a -> b -> t m c) -> t m a -> t m b -> t m c
498+zipWithM :: IsStream t => (a -> b -> t m c) -> t m a -> t m b -> t m c
498499zipWithM f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
499500 where
500501 go mx my = Stream $ \_ stp yld -> do
@@ -514,7 +515,7 @@ zipWithM f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
514515
515516-- | Zip two streams asyncly (i.e. both the elements being zipped are generated
516517-- concurrently) using a monadic zipping function.
517−zipAsyncWithM :: (Streaming t, MonadAsync m)
518+zipAsyncWithM :: (IsStream t, MonadAsync m)
518519 => (a -> b -> t m c) -> t m a -> t m b -> t m c
519520zipAsyncWithM f m1 m2 = fromStream $ Stream $ \_ stp yld -> do
520521 ma <- async m1