on master help sign in
Change elimination ops to use monomoprhic SerialT
c463d16Harendra Kumar
Changelog.md+7−5
@@ -16,14 +16,16 @@
16 different behavior for each type. See the documentation for more details. To
16 different behavior for each type. See the documentation for more details. To
17 adapt to this change replace any usage of `<|>` with `parallel` and
17 adapt to this change replace any usage of `<|>` with `parallel` and
18 `empty` with `nil`.
18 `empty` with `nil`.
19−* Streams now default to the `SerialT` type unless explicitly specified using a
19+* Stream type now defaults to the `SerialT` type unless explicitly specified
20− type combinator. This also means that you do not have to explicitly specify a
20+ using a type combinator or a monomorphic type. This change reduces puzzling
21− stream type now, the default will be used. This changes reduces puzzling type
22− errors for beginners. It includes the following two changes:
21+ type errors for beginners. It includes the following two changes:
23 * Change the type of all stream elimination functions to `SerialT`. This
22 * Change the type of all stream elimination functions to `SerialT`. This
24 makes sure that the stream type is always fixed at all exits.
23 makes sure that the stream type is always fixed at all exits.
25 * Change the type combinators to only fix the argument stream type and
24 * Change the type combinators to only fix the argument stream type and
26 the resulting stream type remains polymorphic.
25 the resulting stream type remains polymorphic.
26+
27+ Stream types may have to be changed or type combinators may have to added or
28+ removed to adapt to this change.
27* Change the type of `foldrM` to make it consistent with `foldrM` in base.
29* Change the type of `foldrM` to make it consistent with `foldrM` in base.
28
30
29### Deprecations
31### Deprecations
@@ -50,7 +52,7 @@
50 * `foldl'` strict left fold
52 * `foldl'` strict left fold
51 * `foldlM'` strict left fold with a monadic fold function
53 * `foldlM'` strict left fold with a monadic fold function
52 * `append` run two streams serially one after the other
54 * `append` run two streams serially one after the other
53− * `parallel` run two streams in parallel
55+ * `parallel` run two streams in parallel (replaces `<|>`)
54
56
55## 0.1.2
57## 0.1.2
56
58
src/Streamly/Prelude.hs+24−28
@@ -174,7 +174,7 @@ fromHandle h = fromStream go
174-- >> runIdentity $ foldr (:) [] (serially $ fromFoldable [1,2,3])
174-- >> runIdentity $ foldr (:) [] (serially $ fromFoldable [1,2,3])
175-- [1,2,3]
175-- [1,2,3]
176-- @
176-- @
177−foldr :: (IsStream t, Monad m) => (a -> b -> b) -> b -> t m a -> m b
177+foldr :: Monad m => (a -> b -> b) -> b -> SerialT m a -> m b
178foldr step acc m = go (toStream m)
178foldr step acc m = go (toStream m)
179 where
179 where
180 go m1 =
180 go m1 =
@@ -191,7 +191,7 @@ foldr step acc m = go (toStream m)
191-- [1,2,3]
191-- [1,2,3]
192-- @
192-- @
193{-# INLINE foldrM #-}
193{-# INLINE foldrM #-}
194−foldrM :: (IsStream t, Monad m) => (a -> b -> m b) -> b -> t m a -> m b
194+foldrM :: Monad m => (a -> b -> m b) -> b -> SerialT m a -> m b
195foldrM step acc m = go (toStream m)
195foldrM step acc m = go (toStream m)
196 where
196 where
197 go m1 =
197 go m1 =
@@ -233,8 +233,7 @@ scanl' step begin m = scanx step begin id m
233-- argument) to the folded value at the end. This is designed to work with the
233-- argument) to the folded value at the end. This is designed to work with the
234-- @foldl@ library. The suffix @x@ is a mnemonic for extraction.
234-- @foldl@ library. The suffix @x@ is a mnemonic for extraction.
235{-# INLINE foldx #-}
235{-# INLINE foldx #-}
236−foldx :: (IsStream t, Monad m)
237− => (x -> a -> x) -> x -> (x -> b) -> t m a -> m b
236+foldx :: Monad m => (x -> a -> x) -> x -> (x -> b) -> SerialT m a -> m b
238foldx step begin done m = get $ go (toStream m) begin
237foldx step begin done m = get $ go (toStream m) begin
239 where
238 where
240 {-# NOINLINE get #-}
239 {-# NOINLINE get #-}
@@ -257,19 +256,17 @@ foldx step begin done m = get $ go (toStream m) begin
257 in (S.runStream m1) Nothing stop yield
256 in (S.runStream m1) Nothing stop yield
258
257
259{-# DEPRECATED foldl "Please use foldx instead." #-}
258{-# DEPRECATED foldl "Please use foldx instead." #-}
260−foldl :: (IsStream t, Monad m)
261− => (x -> a -> x) -> x -> (x -> b) -> t m a -> m b
259+foldl :: Monad m => (x -> a -> x) -> x -> (x -> b) -> SerialT m a -> m b
262foldl = foldx
260foldl = foldx
263
261
264-- | Strict left associative fold.
262-- | Strict left associative fold.
265{-# INLINE foldl' #-}
263{-# INLINE foldl' #-}
266−foldl' :: (IsStream t, Monad m) => (b -> a -> b) -> b -> t m a -> m b
264+foldl' :: Monad m => (b -> a -> b) -> b -> SerialT m a -> m b
267foldl' step begin m = foldx step begin id m
265foldl' step begin m = foldx step begin id m
268
266
269-- XXX replace the recursive "go" with explicit continuations.
267-- XXX replace the recursive "go" with explicit continuations.
270-- | Like 'foldx', but with a monadic step function.
268-- | Like 'foldx', but with a monadic step function.
271−foldxM :: (IsStream t, Monad m)
272− => (x -> a -> m x) -> m x -> (x -> m b) -> t m a -> m b
269+foldxM :: Monad m => (x -> a -> m x) -> m x -> (x -> m b) -> SerialT m a -> m b
273foldxM step begin done m = go begin (toStream m)
270foldxM step begin done m = go begin (toStream m)
274 where
271 where
275 go !acc m1 =
272 go !acc m1 =
@@ -279,18 +276,17 @@ foldxM step begin done m = go begin (toStream m)
279 in (S.runStream m1) Nothing stop yield
276 in (S.runStream m1) Nothing stop yield
280
277
281{-# DEPRECATED foldlM "Please use foldxM instead." #-}
278{-# DEPRECATED foldlM "Please use foldxM instead." #-}
282−foldlM :: (IsStream t, Monad m)
283− => (x -> a -> m x) -> m x -> (x -> m b) -> t m a -> m b
279+foldlM :: Monad m => (x -> a -> m x) -> m x -> (x -> m b) -> SerialT m a -> m b
284foldlM = foldxM
280foldlM = foldxM
285
281
286-- | Like 'foldl'' but with a monadic step function.
282-- | Like 'foldl'' but with a monadic step function.
287−foldlM' :: (IsStream t, Monad m) => (b -> a -> m b) -> b -> t m a -> m b
283+foldlM' :: Monad m => (b -> a -> m b) -> b -> SerialT m a -> m b
288foldlM' step begin m = foldxM step (return begin) return m
284foldlM' step begin m = foldxM step (return begin) return m
289
285
290-- | Decompose a stream into its head and tail. If the stream is empty, returns
286-- | Decompose a stream into its head and tail. If the stream is empty, returns
291-- 'Nothing'. If the stream is non-empty, returns 'Just (a, ma)', where 'a' is
287-- 'Nothing'. If the stream is non-empty, returns 'Just (a, ma)', where 'a' is
292-- the head of the stream and 'ma' its tail.
288-- the head of the stream and 'ma' its tail.
293−uncons :: (IsStream t, Monad m) => t m a -> m (Maybe (a, t m a))
289+uncons :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (a, t m a))
294uncons m =
290uncons m =
295 let stop = return Nothing
291 let stop = return Nothing
296 yield a Nothing = return (Just (a, nil))
292 yield a Nothing = return (Just (a, nil))
@@ -298,7 +294,7 @@ uncons m =
298 in (S.runStream (toStream m)) Nothing stop yield
294 in (S.runStream (toStream m)) Nothing stop yield
299
295
300-- | Write a stream of Strings to an IO Handle.
296-- | Write a stream of Strings to an IO Handle.
301−toHandle :: (IsStream t, MonadIO m) => IO.Handle -> t m String -> m ()
297+toHandle :: MonadIO m => IO.Handle -> SerialT m String -> m ()
302toHandle h m = go (toStream m)
298toHandle h m = go (toStream m)
303 where
299 where
304 go m1 =
300 go m1 =
@@ -376,7 +372,7 @@ dropWhile p m = fromStream $ go (toStream m)
376 in (S.runStream m1) ctx stp yield
372 in (S.runStream m1) ctx stp yield
377
373
378-- | Determine whether all elements of a stream satisfy a predicate.
374-- | Determine whether all elements of a stream satisfy a predicate.
379−all :: (IsStream t, Monad m) => (a -> Bool) -> t m a -> m Bool
375+all :: Monad m => (a -> Bool) -> SerialT m a -> m Bool
380all p m = go (toStream m)
376all p m = go (toStream m)
381 where
377 where
382 go m1 =
378 go m1 =
@@ -387,7 +383,7 @@ all p m = go (toStream m)
387 in (S.runStream m1) Nothing (return True) yield
383 in (S.runStream m1) Nothing (return True) yield
388
384
389-- | Determine whether any of the elements of a stream satisfy a predicate.
385-- | Determine whether any of the elements of a stream satisfy a predicate.
390−any :: (IsStream t, Monad m) => (a -> Bool) -> t m a -> m Bool
386+any :: Monad m => (a -> Bool) -> SerialT m a -> m Bool
391any p m = go (toStream m)
387any p m = go (toStream m)
392 where
388 where
393 go m1 =
389 go m1 =
@@ -398,22 +394,22 @@ any p m = go (toStream m)
398 in (S.runStream m1) Nothing (return False) yield
394 in (S.runStream m1) Nothing (return False) yield
399
395
400-- | Determine the sum of all elements of a stream of numbers
396-- | Determine the sum of all elements of a stream of numbers
401−sum :: (IsStream t, Monad m, Num a) => t m a -> m a
397+sum :: (Monad m, Num a) => SerialT m a -> m a
402sum = foldl (+) 0 id
398sum = foldl (+) 0 id
403
399
404-- | Determine the product of all elements of a stream of numbers
400-- | Determine the product of all elements of a stream of numbers
405−product :: (IsStream t, Monad m, Num a) => t m a -> m a
401+product :: (Monad m, Num a) => SerialT m a -> m a
406product = foldl (*) 1 id
402product = foldl (*) 1 id
407
403
408-- | Extract the first element of the stream, if any.
404-- | Extract the first element of the stream, if any.
409−head :: (IsStream t, Monad m) => t m a -> m (Maybe a)
405+head :: Monad m => SerialT m a -> m (Maybe a)
410head m =
406head m =
411 let stop = return Nothing
407 let stop = return Nothing
412 yield a _ = return (Just a)
408 yield a _ = return (Just a)
413 in (S.runStream (toStream m)) Nothing stop yield
409 in (S.runStream (toStream m)) Nothing stop yield
414
410
415-- | Extract all but the first element of the stream, if any.
411-- | Extract all but the first element of the stream, if any.
416−tail :: (IsStream t, Monad m) => t m a -> m (Maybe (t m a))
412+tail :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (t m a))
417tail m =
413tail m =
418 let stop = return Nothing
414 let stop = return Nothing
419 yield _ Nothing = return $ Just nil
415 yield _ Nothing = return $ Just nil
@@ -422,18 +418,18 @@ tail m =
422
418
423-- | Extract the last element of the stream, if any.
419-- | Extract the last element of the stream, if any.
424{-# INLINE last #-}
420{-# INLINE last #-}
425−last :: (IsStream t, Monad m) => t m a -> m (Maybe a)
421+last :: Monad m => SerialT m a -> m (Maybe a)
426last = foldl (\_ y -> Just y) Nothing id
422last = foldl (\_ y -> Just y) Nothing id
427
423
428-- | Determine whether the stream is empty.
424-- | Determine whether the stream is empty.
429−null :: (IsStream t, Monad m) => t m a -> m Bool
425+null :: Monad m => SerialT m a -> m Bool
430null m =
426null m =
431 let stop = return True
427 let stop = return True
432 yield _ _ = return False
428 yield _ _ = return False
433 in (S.runStream (toStream m)) Nothing stop yield
429 in (S.runStream (toStream m)) Nothing stop yield
434
430
435-- | Determine whether an element is present in the stream.
431-- | Determine whether an element is present in the stream.
436−elem :: (IsStream t, Monad m, Eq a) => a -> t m a -> m Bool
432+elem :: (Monad m, Eq a) => a -> SerialT m a -> m Bool
437elem e m = go (toStream m)
433elem e m = go (toStream m)
438 where
434 where
439 go m1 =
435 go m1 =
@@ -443,7 +439,7 @@ elem e m = go (toStream m)
443 in (S.runStream m1) Nothing stop yield
439 in (S.runStream m1) Nothing stop yield
444
440
445-- | Determine whether an element is not present in the stream.
441-- | Determine whether an element is not present in the stream.
446−notElem :: (IsStream t, Monad m, Eq a) => a -> t m a -> m Bool
442+notElem :: (Monad m, Eq a) => a -> SerialT m a -> m Bool
447notElem e m = go (toStream m)
443notElem e m = go (toStream m)
448 where
444 where
449 go m1 =
445 go m1 =
@@ -453,7 +449,7 @@ notElem e m = go (toStream m)
453 in (S.runStream m1) Nothing stop yield
449 in (S.runStream m1) Nothing stop yield
454
450
455-- | Determine the length of the stream.
451-- | Determine the length of the stream.
456−length :: (IsStream t, Monad m) => t m a -> m Int
452+length :: Monad m => SerialT m a -> m Int
457length = foldl (\n _ -> n + 1) 0 id
453length = foldl (\n _ -> n + 1) 0 id
458
454
459-- | Returns the elements of the stream in reverse order.
455-- | Returns the elements of the stream in reverse order.
@@ -471,7 +467,7 @@ reverse m = fromStream $ go Nothing (toStream m)
471
467
472-- XXX replace the recursive "go" with continuation
468-- XXX replace the recursive "go" with continuation
473-- | Determine the minimum element in a stream.
469-- | Determine the minimum element in a stream.
474−minimum :: (IsStream t, Monad m, Ord a) => t m a -> m (Maybe a)
470+minimum :: (Monad m, Ord a) => SerialT m a -> m (Maybe a)
475minimum m = go Nothing (toStream m)
471minimum m = go Nothing (toStream m)
476 where
472 where
477 go r m1 =
473 go r m1 =
@@ -486,7 +482,7 @@ minimum m = go Nothing (toStream m)
486
482
487-- XXX replace the recursive "go" with continuation
483-- XXX replace the recursive "go" with continuation
488-- | Determine the maximum element in a stream.
484-- | Determine the maximum element in a stream.
489−maximum :: (IsStream t, Monad m, Ord a) => t m a -> m (Maybe a)
485+maximum :: (Monad m, Ord a) => SerialT m a -> m (Maybe a)
490maximum m = go Nothing (toStream m)
486maximum m = go Nothing (toStream m)
491 where
487 where
492 go r m1 =
488 go r m1 =
@@ -519,7 +515,7 @@ mapM f m = fromStream $ go (toStream m)
519
515
520-- | Apply a monadic action to each element of the stream and discard the
516-- | Apply a monadic action to each element of the stream and discard the
521-- output of the action.
517-- output of the action.
522−mapM_ :: (IsStream t, Monad m) => (a -> m b) -> t m a -> m ()
518+mapM_ :: Monad m => (a -> m b) -> SerialT m a -> m ()
523mapM_ f m = go (toStream m)
519mapM_ f m = go (toStream m)
524 where
520 where
525 go m1 =
521 go m1 =
test/Prop.hs+4−4
@@ -82,8 +82,8 @@ eliminateOp constr listOp op a =
82
82
83elemOp
83elemOp
84 :: ([Word8] -> t IO Word8)
84 :: ([Word8] -> t IO Word8)
85− -> (t IO Word8 -> t IO Word8)
85+ -> (t IO Word8 -> SerialT IO Word8)
86− -> (Word8 -> t IO Word8 -> IO Bool)
86+ -> (Word8 -> SerialT IO Word8 -> IO Bool)
87 -> (Word8 -> [Word8] -> Bool)
87 -> (Word8 -> [Word8] -> Bool)
88 -> (Word8, [Word8])
88 -> (Word8, [Word8])
89 -> Property
89 -> Property
@@ -160,7 +160,7 @@ eliminationOps
160 :: IsStream t
160 :: IsStream t
161 => ([Int] -> t IO Int)
161 => ([Int] -> t IO Int)
162 -> String
162 -> String
163− -> (t IO Int -> t IO Int)
163+ -> (t IO Int -> SerialT IO Int)
164 -> Spec
164 -> Spec
165eliminationOps constr desc t = do
165eliminationOps constr desc t = do
166 -- Elimination
166 -- Elimination
@@ -196,7 +196,7 @@ transformOpsWord8
196 :: IsStream t
196 :: IsStream t
197 => ([Word8] -> t IO Word8)
197 => ([Word8] -> t IO Word8)
198 -> String
198 -> String
199− -> (t IO Word8 -> t IO Word8)
199+ -> (t IO Word8 -> SerialT IO Word8)
200 -> Spec
200 -> Spec
201transformOpsWord8 constr desc t = do
201transformOpsWord8 constr desc t = do
202 prop (desc ++ " elem") $ elemOp constr t A.elem elem
202 prop (desc ++ " elem") $ elemOp constr t A.elem elem