on master help sign in
Change elimination ops to use monomoprhic SerialT
commitc463d1634c550227eba5d513d7d605e4d0b6b436 authorHarendra Kumar <harendra.kumar@gmail.com> dateWed 18 Apr 2018, 11:18 (9 years ago) parent38d21d5
3 files changed+35−37
Changelog.md+7−5 src/Streamly/Prelude.hs+24−28 test/Prop.hs+4−4
Changelog.md+7−5
@@ -16,14 +16,16 @@
1616 different behavior for each type. See the documentation for more details. To
1717 adapt to this change replace any usage of `<|>` with `parallel` and
1818 `empty` with `nil`.
19−* Streams now default to the `SerialT` type unless explicitly specified using a
20− type combinator. This also means that you do not have to explicitly specify a
21− stream type now, the default will be used. This changes reduces puzzling type
22− errors for beginners. It includes the following two changes:
19+* Stream type now defaults to the `SerialT` type unless explicitly specified
20+ using a type combinator or a monomorphic type. This change reduces puzzling
21+ type errors for beginners. It includes the following two changes:
2322 * Change the type of all stream elimination functions to `SerialT`. This
2423 makes sure that the stream type is always fixed at all exits.
2524 * Change the type combinators to only fix the argument stream type and
2625 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.
2729* Change the type of `foldrM` to make it consistent with `foldrM` in base.
2830
2931### Deprecations
@@ -50,7 +52,7 @@
5052 * `foldl'` strict left fold
5153 * `foldlM'` strict left fold with a monadic fold function
5254 * `append` run two streams serially one after the other
53− * `parallel` run two streams in parallel
55+ * `parallel` run two streams in parallel (replaces `<|>`)
5456
5557## 0.1.2
5658
src/Streamly/Prelude.hs+24−28
@@ -174,7 +174,7 @@ fromHandle h = fromStream go
174174-- >> runIdentity $ foldr (:) [] (serially $ fromFoldable [1,2,3])
175175-- [1,2,3]
176176-- @
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
178178foldr step acc m = go (toStream m)
179179 where
180180 go m1 =
@@ -191,7 +191,7 @@ foldr step acc m = go (toStream m)
191191-- [1,2,3]
192192-- @
193193{-# 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
195195foldrM step acc m = go (toStream m)
196196 where
197197 go m1 =
@@ -233,8 +233,7 @@ scanl' step begin m = scanx step begin id m
233233-- argument) to the folded value at the end. This is designed to work with the
234234-- @foldl@ library. The suffix @x@ is a mnemonic for extraction.
235235{-# 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
238237foldx step begin done m = get $ go (toStream m) begin
239238 where
240239 {-# NOINLINE get #-}
@@ -257,19 +256,17 @@ foldx step begin done m = get $ go (toStream m) begin
257256 in (S.runStream m1) Nothing stop yield
258257
259258{-# 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
262260foldl = foldx
263261
264262-- | Strict left associative fold.
265263{-# 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
267265foldl' step begin m = foldx step begin id m
268266
269267-- XXX replace the recursive "go" with explicit continuations.
270268-- | 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
273270foldxM step begin done m = go begin (toStream m)
274271 where
275272 go !acc m1 =
@@ -279,18 +276,17 @@ foldxM step begin done m = go begin (toStream m)
279276 in (S.runStream m1) Nothing stop yield
280277
281278{-# 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
284280foldlM = foldxM
285281
286282-- | 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
288284foldlM' step begin m = foldxM step (return begin) return m
289285
290286-- | Decompose a stream into its head and tail. If the stream is empty, returns
291287-- 'Nothing'. If the stream is non-empty, returns 'Just (a, ma)', where 'a' is
292288-- 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))
294290uncons m =
295291 let stop = return Nothing
296292 yield a Nothing = return (Just (a, nil))
@@ -298,7 +294,7 @@ uncons m =
298294 in (S.runStream (toStream m)) Nothing stop yield
299295
300296-- | 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 ()
302298toHandle h m = go (toStream m)
303299 where
304300 go m1 =
@@ -376,7 +372,7 @@ dropWhile p m = fromStream $ go (toStream m)
376372 in (S.runStream m1) ctx stp yield
377373
378374-- | 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
380376all p m = go (toStream m)
381377 where
382378 go m1 =
@@ -387,7 +383,7 @@ all p m = go (toStream m)
387383 in (S.runStream m1) Nothing (return True) yield
388384
389385-- | 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
391387any p m = go (toStream m)
392388 where
393389 go m1 =
@@ -398,22 +394,22 @@ any p m = go (toStream m)
398394 in (S.runStream m1) Nothing (return False) yield
399395
400396-- | 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
402398sum = foldl (+) 0 id
403399
404400-- | 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
406402product = foldl (*) 1 id
407403
408404-- | 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)
410406head m =
411407 let stop = return Nothing
412408 yield a _ = return (Just a)
413409 in (S.runStream (toStream m)) Nothing stop yield
414410
415411-- | 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))
417413tail m =
418414 let stop = return Nothing
419415 yield _ Nothing = return $ Just nil
@@ -422,18 +418,18 @@ tail m =
422418
423419-- | Extract the last element of the stream, if any.
424420{-# INLINE last #-}
425−last :: (IsStream t, Monad m) => t m a -> m (Maybe a)
421+last :: Monad m => SerialT m a -> m (Maybe a)
426422last = foldl (\_ y -> Just y) Nothing id
427423
428424-- | 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
430426null m =
431427 let stop = return True
432428 yield _ _ = return False
433429 in (S.runStream (toStream m)) Nothing stop yield
434430
435431-- | 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
437433elem e m = go (toStream m)
438434 where
439435 go m1 =
@@ -443,7 +439,7 @@ elem e m = go (toStream m)
443439 in (S.runStream m1) Nothing stop yield
444440
445441-- | 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
447443notElem e m = go (toStream m)
448444 where
449445 go m1 =
@@ -453,7 +449,7 @@ notElem e m = go (toStream m)
453449 in (S.runStream m1) Nothing stop yield
454450
455451-- | 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
457453length = foldl (\n _ -> n + 1) 0 id
458454
459455-- | Returns the elements of the stream in reverse order.
@@ -471,7 +467,7 @@ reverse m = fromStream $ go Nothing (toStream m)
471467
472468-- XXX replace the recursive "go" with continuation
473469-- | 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)
475471minimum m = go Nothing (toStream m)
476472 where
477473 go r m1 =
@@ -486,7 +482,7 @@ minimum m = go Nothing (toStream m)
486482
487483-- XXX replace the recursive "go" with continuation
488484-- | 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)
490486maximum m = go Nothing (toStream m)
491487 where
492488 go r m1 =
@@ -519,7 +515,7 @@ mapM f m = fromStream $ go (toStream m)
519515
520516-- | Apply a monadic action to each element of the stream and discard the
521517-- 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 ()
523519mapM_ f m = go (toStream m)
524520 where
525521 go m1 =
test/Prop.hs+4−4
@@ -82,8 +82,8 @@ eliminateOp constr listOp op a =
8282
8383elemOp
8484 :: ([Word8] -> t IO Word8)
85− -> (t IO Word8 -> t IO Word8)
86− -> (Word8 -> t IO Word8 -> IO Bool)
85+ -> (t IO Word8 -> SerialT IO Word8)
86+ -> (Word8 -> SerialT IO Word8 -> IO Bool)
8787 -> (Word8 -> [Word8] -> Bool)
8888 -> (Word8, [Word8])
8989 -> Property
@@ -160,7 +160,7 @@ eliminationOps
160160 :: IsStream t
161161 => ([Int] -> t IO Int)
162162 -> String
163− -> (t IO Int -> t IO Int)
163+ -> (t IO Int -> SerialT IO Int)
164164 -> Spec
165165eliminationOps constr desc t = do
166166 -- Elimination
@@ -196,7 +196,7 @@ transformOpsWord8
196196 :: IsStream t
197197 => ([Word8] -> t IO Word8)
198198 -> String
199− -> (t IO Word8 -> t IO Word8)
199+ -> (t IO Word8 -> SerialT IO Word8)
200200 -> Spec
201201transformOpsWord8 constr desc t = do
202202 prop (desc ++ " elem") $ elemOp constr t A.elem elem