on master help sign in
rename InterleavedT=>CoserialT, AParallelT=>CoparallelT
commitacd550c731dc19a119efe8120b6f5987fca13cdf authorHarendra Kumar <harendra.kumar@gmail.com> dateWed 18 Apr 2018, 17:32 (8 years ago) parent3f638d9
5 files changed+110−92
Changelog.md+22−16 benchmark/Main.hs+2−2 src/Streamly.hs+12−10 src/Streamly/Streams.hs+72−62 test/Main.hs+2−2
Changelog.md+22−16
@@ -9,8 +9,8 @@
99 * For `ParallelT`, the `<>` operation now concurrently meges the two streams
1010 in a fairly parallel manner.
1111
12− To adapt to the new changes, replace `<>` with `append` wherever it is used
13− for streams other than `StreamT`.
12+ To adapt to the new changes, replace `<>` with `serial` wherever it is used
13+ for stream types other than `StreamT`.
1414
1515* Change the semantics of `Alternative` instance. The `<|>` operator now has a
1616 different behavior for each type. See the documentation for more details. To
@@ -19,39 +19,45 @@
1919* Stream type now defaults to the `SerialT` type unless explicitly specified
2020 using a type combinator or a monomorphic type. This change reduces puzzling
2121 type errors for beginners. It includes the following two changes:
22− * Change the type of all stream elimination functions to `SerialT`. This
23− makes sure that the stream type is always fixed at all exits.
24− * Change the type combinators to only fix the argument stream type and
25− the resulting stream type remains polymorphic.
22+ * Change the type of all stream elimination functions to use `SerialT`
23+ instead of a polymorphic type. This makes sure that the stream type is
24+ always fixed at all exits.
25+ * Change the type combinators (e.g. `parallely`) to only fix the argument
26+ stream type and the output stream type remains polymorphic.
2627
27− Stream types may have to be changed or type combinators may have to added or
28− removed to adapt to this change.
28+ Stream types may have to be changed or type combinators may have to be added
29+ or removed to adapt to this change.
2930* Change the type of `foldrM` to make it consistent with `foldrM` in base.
3031
3132### Deprecations
3233* Deprecate and rename the following symbols:
3334 * `StreamT` to `SerialT`
34− * `runStreamT` to `runSerialT`
3535 * `ZipStream` to `ZipSerial`
36− * `runZipStream` to `runZipSerial`
37− * `AsyncT` to `AParallelT`
38− * `runAsyncT` to `runAParallelT`
39− * `asyncly` to `aparallely`
36+ * `AsyncT` to `CoparallelT`
37+ * `interleaving` to `coserially`
38+ * `asyncly` to `coparallely`
4039 * `Streaming` to `IsStream`
4140 * `runStreaming` to `runStream`
42− * `<=>` to `interleave`
43− * `<|` to `aparallel`
41+ * `<=>` to `coserial`
42+ * `<|` to `coparallel`
4443 * `each` to `fromFoldable`
4544 * `scan` to `scanx`
4645 * `foldl` to `foldx`
4746 * `foldlM` to `foldxM`
47+* Deprecate and remove the following symbols:
48+ * `runStreamT`
49+ * `runInterleavedT`
50+ * `runAsyncT`
51+ * `runParallelT`
52+ * `runZipStream`
53+ * `runZipAsync`
4854
4955### Enhancements
5056* Add the following functions:
5157 * `scanl'` strict left scan
5258 * `foldl'` strict left fold
5359 * `foldlM'` strict left fold with a monadic fold function
54− * `append` run two streams serially one after the other
60+ * `serial` run two streams serially one after the other
5561 * `parallel` run two streams in parallel (replaces `<|>`)
5662
5763## 0.1.2
benchmark/Main.hs+2−2
@@ -141,13 +141,13 @@ streamly_serial = streamly_basic (A.toList . A.serially)
141141
142142{-# INLINE streamly_interleaved #-}
143143streamly_interleaved
144− :: (A.InterleavedT IO Int -> A.InterleavedT IO Int -> A.InterleavedT IO Int)
144+ :: (A.CoserialT IO Int -> A.CoserialT IO Int -> A.CoserialT IO Int)
145145 -> IO Int
146146streamly_interleaved = streamly_basic (A.toList . A.coserially)
147147
148148{-# INLINE streamly_async #-}
149149streamly_async
150− :: (A.AParallelT IO Int -> A.AParallelT IO Int -> A.AParallelT IO Int)
150+ :: (A.CoparallelT IO Int -> A.CoparallelT IO Int -> A.CoparallelT IO Int)
151151 -> IO Int
152152streamly_async = streamly_basic (A.toList . A.coparallely)
153153
src/Streamly.hs+12−10
@@ -21,8 +21,8 @@ module Streamly
2121 -- * General Stream Styles
2222 -- $product
2323 , SerialT
24− , InterleavedT
25− , AParallelT
24+ , CoserialT
25+ , CoparallelT
2626 , ParallelT
2727
2828 -- * Zip Style Streams
@@ -77,8 +77,10 @@ module Streamly
7777 , runAsyncT
7878 , runZipStream
7979 , StreamT
80+ , InterleavedT
8081 , AsyncT
8182 , ZipStream
83+ , interleaving
8284 , asyncly
8385 , (<=>)
8486 , (<|)
@@ -126,22 +128,22 @@ import Control.Monad.Trans.Class (MonadTrans (..))
126128
127129-- $overview
128130--
129−-- Streamly provides six distinct stream types i.e. 'SerialT', 'InterleavedT',
130−-- 'AParallelT' and 'ParallelT', 'ZipStream' and 'ZipAsync', each representing a
131+-- Streamly provides six distinct stream types i.e. 'SerialT', 'CoserialT',
132+-- 'CoparallelT' and 'ParallelT', 'ZipStream' and 'ZipAsync', each representing a
131133-- stream of elements. All these types have the same underlying representation
132134-- and can be adapted from one to another using type adaptor combinators
133135-- described later. Each of these types belongs to the 'IsStream' type class
134136-- which helps converting the specific type to and from the underlying generic
135137-- stream type.
136138--
137−-- The types 'SerialT', 'InterleavedT', 'AParallelT' and 'ParallelT' are 'Monad'
139+-- The types 'SerialT', 'CoserialT', 'CoparallelT' and 'ParallelT' are 'Monad'
138140-- transformers with the monadic bind operation combining streams in a product
139141-- style in much the same way as a list monad or a list transformer i.e. each
140142-- element from one stream is combined with every element of the other stream.
141143-- However, the applicative and monadic composition of these types differ in
142144-- terms of the ordering and time sequence in which the elements from two
143−-- streams are combined. 'SerialT' and 'InterleavedT' compose streams serially
144−-- whereas 'AParallelT' and 'ParallelT' are their concurrent counterparts. See the
145+-- streams are combined. 'SerialT' and 'CoserialT' compose streams serially
146+-- whereas 'CoparallelT' and 'ParallelT' are their concurrent counterparts. See the
145147-- documentation of the respective types for more details.
146148--
147149-- The types 'ZipStream' and 'ZipAsync' provide 'Applicative' instances to zip
@@ -156,7 +158,7 @@ import Control.Monad.Trans.Class (MonadTrans (..))
156158-- (concurrent or serial) in which the elements in the resulting stream are
157159-- produced depends on the type of the stream.
158160--
159−-- Concurrent composition types 'AParallelT', 'ParallelT', 'ZipAsync' require the
161+-- Concurrent composition types 'CoparallelT', 'ParallelT', 'ZipAsync' require the
160162-- underlying monad of the streaming monad transformer to be 'MonadAsync'.
161163--
162164-- For more details please see the "Streamly.Tutorial" and "Streamly.Examples"
@@ -168,8 +170,8 @@ import Control.Monad.Trans.Class (MonadTrans (..))
168170-- $product
169171--
170172-- Streams that compose serially or non-concurrently come in two flavors i.e.
171−-- 'SerialT' and 'InterleavedT'. Both of these serial flavors have
172−-- corresponding concurrent equivalents, those are 'AParallelT' and 'ParallelT'
173+-- 'SerialT' and 'CoserialT'. Both of these serial flavors have
174+-- corresponding concurrent equivalents, those are 'CoparallelT' and 'ParallelT'
173175-- respectively.
174176
175177-- $zipping
src/Streamly/Streams.hs+72−62
@@ -58,8 +58,9 @@ module Streamly.Streams
5858 -- * Stream Styles
5959 , SerialT
6060 , StreamT -- deprecated
61− , InterleavedT
62− , AParallelT
61+ , CoserialT
62+ , InterleavedT -- deprecated
63+ , CoparallelT
6364 , AsyncT -- deprecated
6465 , ParallelT
6566 , ZipSerial
@@ -69,6 +70,7 @@ module Streamly.Streams
6970 -- * Type Adapters
7071 , serially
7172 , coserially
73+ , interleaving -- deprecated
7274 , coparallely
7375 , asyncly -- deprecated
7476 , parallely
@@ -416,10 +418,10 @@ instance (Monad m, Floating a) => Floating (SerialT m a) where
416418 logBase = liftA2 logBase
417419
418420------------------------------------------------------------------------------
419−-- InterleavedT
421+-- CoserialT
420422------------------------------------------------------------------------------
421423
422−-- | The 'Semigroup' instance of 'InterleavedT' interleaves two streams,
424+-- | The 'Semigroup' instance of 'CoserialT' interleaves two streams,
423425-- yielding one element from each stream alternately.
424426--
425427-- @
@@ -449,31 +451,34 @@ instance (Monad m, Floating a) => Floating (SerialT m a) where
449451-- Note that interleaving composition can only combine a finite number of
450452-- streams as it needs to retain state for each unfinished stream.
451453--
452−newtype InterleavedT m a = InterleavedT {getInterleavedT :: Stream m a}
454+newtype CoserialT m a = CoserialT {getCoserialT :: Stream m a}
453455 deriving (Monoid, MonadTrans, MonadIO, MonadThrow)
454456
455−deriving instance MonadAsync m => Alternative (InterleavedT m)
456−deriving instance MonadAsync m => MonadPlus (InterleavedT m)
457−deriving instance (MonadBase b m, Monad m) => MonadBase b (InterleavedT m)
458−deriving instance MonadError e m => MonadError e (InterleavedT m)
459−deriving instance MonadReader r m => MonadReader r (InterleavedT m)
460−deriving instance MonadState s m => MonadState s (InterleavedT m)
457+deriving instance MonadAsync m => Alternative (CoserialT m)
458+deriving instance MonadAsync m => MonadPlus (CoserialT m)
459+deriving instance (MonadBase b m, Monad m) => MonadBase b (CoserialT m)
460+deriving instance MonadError e m => MonadError e (CoserialT m)
461+deriving instance MonadReader r m => MonadReader r (CoserialT m)
462+deriving instance MonadState s m => MonadState s (CoserialT m)
461463
462−instance IsStream InterleavedT where
463− toStream = getInterleavedT
464− fromStream = InterleavedT
464+{-# DEPRECATED InterleavedT "Please use 'CoserialT' instead." #-}
465+type InterleavedT = CoserialT
466+
467+instance IsStream CoserialT where
468+ toStream = getCoserialT
469+ fromStream = CoserialT
465470
466471------------------------------------------------------------------------------
467472-- Semigroup
468473------------------------------------------------------------------------------
469474
470−-- | Polymorphic version of the 'Semigroup' operation '<>' of 'InterleavedT'.
475+-- | Polymorphic version of the 'Semigroup' operation '<>' of 'CoserialT'.
471476-- Interleaves two streams, yielding one element from each stream alternately.
472477{-# INLINE coserial #-}
473478coserial :: IsStream t => t m a -> t m a -> t m a
474479coserial m1 m2 = fromStream $ S.coserial (toStream m1) (toStream m2)
475480
476−instance Semigroup (InterleavedT m a) where
481+instance Semigroup (CoserialT m a) where
477482 (<>) = coserial
478483
479484infixr 5 <=>
@@ -488,9 +493,9 @@ infixr 5 <=>
488493-- Monad
489494------------------------------------------------------------------------------
490495
491−instance Monad m => Monad (InterleavedT m) where
496+instance Monad m => Monad (CoserialT m) where
492497 return = pure
493− (InterleavedT (Stream m)) >>= f = InterleavedT $ Stream $ \_ stp yld ->
498+ (CoserialT (Stream m)) >>= f = CoserialT $ Stream $ \_ stp yld ->
494499 let run x = (S.runStream x) Nothing stp yld
495500 yield a Nothing = run $ toStream (f a)
496501 yield a (Just r) = run $ toStream $ f a <> (fromStream r >>= f)
@@ -500,26 +505,26 @@ instance Monad m => Monad (InterleavedT m) where
500505-- Applicative
501506------------------------------------------------------------------------------
502507
503−instance Monad m => Applicative (InterleavedT m) where
504− pure a = InterleavedT $ S.cons a Nothing
508+instance Monad m => Applicative (CoserialT m) where
509+ pure a = CoserialT $ S.cons a Nothing
505510 (<*>) = ap
506511
507512------------------------------------------------------------------------------
508513-- Functor
509514------------------------------------------------------------------------------
510515
511−instance Monad m => Functor (InterleavedT m) where
512− fmap f (InterleavedT (Stream m)) = InterleavedT $ Stream $ \_ stp yld ->
516+instance Monad m => Functor (CoserialT m) where
517+ fmap f (CoserialT (Stream m)) = CoserialT $ Stream $ \_ stp yld ->
513518 let yield a Nothing = yld (f a) Nothing
514519 yield a (Just r) =
515− yld (f a) (Just (getInterleavedT (fmap f (InterleavedT r))))
520+ yld (f a) (Just (getCoserialT (fmap f (CoserialT r))))
516521 in m Nothing stp yield
517522
518523------------------------------------------------------------------------------
519524-- Num
520525------------------------------------------------------------------------------
521526
522−instance (Monad m, Num a) => Num (InterleavedT m a) where
527+instance (Monad m, Num a) => Num (CoserialT m a) where
523528 fromInteger n = pure (fromInteger n)
524529
525530 negate = fmap negate
@@ -530,14 +535,14 @@ instance (Monad m, Num a) => Num (InterleavedT m a) where
530535 (*) = liftA2 (*)
531536 (-) = liftA2 (-)
532537
533−instance (Monad m, Fractional a) => Fractional (InterleavedT m a) where
538+instance (Monad m, Fractional a) => Fractional (CoserialT m a) where
534539 fromRational n = pure (fromRational n)
535540
536541 recip = fmap recip
537542
538543 (/) = liftA2 (/)
539544
540−instance (Monad m, Floating a) => Floating (InterleavedT m a) where
545+instance (Monad m, Floating a) => Floating (CoserialT m a) where
541546 pi = pure pi
542547
543548 exp = fmap exp
@@ -560,7 +565,7 @@ instance (Monad m, Floating a) => Floating (InterleavedT m a) where
560565 logBase = liftA2 logBase
561566
562567------------------------------------------------------------------------------
563−-- AParallelT
568+-- CoparallelT
564569------------------------------------------------------------------------------
565570
566571-- | Adaptive parallel, in a left to right 'Semigroup' composition it tries to
@@ -602,38 +607,38 @@ instance (Monad m, Floating a) => Floating (InterleavedT m a) where
602607-- Note that this composition can be used to combine infinite number of streams
603608-- as it explores only a bounded number of streams at a time.
604609--
605−newtype AParallelT m a = AParallelT {getAParallelT :: Stream m a}
610+newtype CoparallelT m a = CoparallelT {getCoparallelT :: Stream m a}
606611 deriving (MonadTrans)
607612
608−deriving instance MonadAsync m => Monoid (AParallelT m a)
609−deriving instance MonadAsync m => Alternative (AParallelT m)
610−deriving instance MonadAsync m => MonadPlus (AParallelT m)
611−deriving instance MonadAsync m => MonadIO (AParallelT m)
612−deriving instance MonadAsync m => MonadThrow (AParallelT m)
613−deriving instance (MonadBase b m, MonadAsync m) => MonadBase b (AParallelT m)
614−deriving instance (MonadError e m, MonadAsync m) => MonadError e (AParallelT m)
615−deriving instance (MonadReader r m, MonadAsync m) => MonadReader r (AParallelT m)
616−deriving instance (MonadState s m, MonadAsync m) => MonadState s (AParallelT m)
613+deriving instance MonadAsync m => Monoid (CoparallelT m a)
614+deriving instance MonadAsync m => Alternative (CoparallelT m)
615+deriving instance MonadAsync m => MonadPlus (CoparallelT m)
616+deriving instance MonadAsync m => MonadIO (CoparallelT m)
617+deriving instance MonadAsync m => MonadThrow (CoparallelT m)
618+deriving instance (MonadBase b m, MonadAsync m) => MonadBase b (CoparallelT m)
619+deriving instance (MonadError e m, MonadAsync m) => MonadError e (CoparallelT m)
620+deriving instance (MonadReader r m, MonadAsync m) => MonadReader r (CoparallelT m)
621+deriving instance (MonadState s m, MonadAsync m) => MonadState s (CoparallelT m)
617622
618−{-# DEPRECATED AsyncT "Please use 'AParallelT' instead." #-}
619−type AsyncT = AParallelT
623+{-# DEPRECATED AsyncT "Please use 'CoparallelT' instead." #-}
624+type AsyncT = CoparallelT
620625
621−instance IsStream AParallelT where
622− toStream = getAParallelT
623− fromStream = AParallelT
626+instance IsStream CoparallelT where
627+ toStream = getCoparallelT
628+ fromStream = CoparallelT
624629
625630------------------------------------------------------------------------------
626631-- Semigroup
627632------------------------------------------------------------------------------
628633
629−-- | Polymorphic version of the 'Semigroup' operation '<>' of 'AParallelT', but
634+-- | Polymorphic version of the 'Semigroup' operation '<>' of 'CoparallelT', but
630635-- polymorphic. Merges two streams possibly concurrently, preferring the
631636-- elements from the left one when available.
632637{-# INLINE coparallel #-}
633638coparallel :: (IsStream t, MonadAsync m) => t m a -> t m a -> t m a
634639coparallel m1 m2 = fromStream $ S.coparallel (toStream m1) (toStream m2)
635640
636−instance MonadAsync m => Semigroup (AParallelT m a) where
641+instance MonadAsync m => Semigroup (CoparallelT m a) where
637642 (<>) = coparallel
638643
639644-- | Same as 'coparallel'.
@@ -661,35 +666,35 @@ parbind par m f = go m
661666 yield a (Just r) = run $ f a `par` go r
662667 in g Nothing stp yield
663668
664−instance MonadAsync m => Monad (AParallelT m) where
669+instance MonadAsync m => Monad (CoparallelT m) where
665670 return = pure
666− (AParallelT m) >>= f = AParallelT $ parbind par m g
667− where g x = getAParallelT (f x)
671+ (CoparallelT m) >>= f = CoparallelT $ parbind par m g
672+ where g x = getCoparallelT (f x)
668673 par = S.joinStreamVar2 (SVarStyle Conjunction LIFO)
669674
670675------------------------------------------------------------------------------
671676-- Applicative
672677------------------------------------------------------------------------------
673678
674−instance MonadAsync m => Applicative (AParallelT m) where
675− pure a = AParallelT $ S.cons a Nothing
679+instance MonadAsync m => Applicative (CoparallelT m) where
680+ pure a = CoparallelT $ S.cons a Nothing
676681 (<*>) = ap
677682
678683------------------------------------------------------------------------------
679684-- Functor
680685------------------------------------------------------------------------------
681686
682−instance Monad m => Functor (AParallelT m) where
683− fmap f (AParallelT (Stream m)) = AParallelT $ Stream $ \_ stp yld ->
687+instance Monad m => Functor (CoparallelT m) where
688+ fmap f (CoparallelT (Stream m)) = CoparallelT $ Stream $ \_ stp yld ->
684689 let yield a Nothing = yld (f a) Nothing
685− yield a (Just r) = yld (f a) (Just (getAParallelT (fmap f (AParallelT r))))
690+ yield a (Just r) = yld (f a) (Just (getCoparallelT (fmap f (CoparallelT r))))
686691 in m Nothing stp yield
687692
688693------------------------------------------------------------------------------
689694-- Num
690695------------------------------------------------------------------------------
691696
692−instance (MonadAsync m, Num a) => Num (AParallelT m a) where
697+instance (MonadAsync m, Num a) => Num (CoparallelT m a) where
693698 fromInteger n = pure (fromInteger n)
694699
695700 negate = fmap negate
@@ -700,14 +705,14 @@ instance (MonadAsync m, Num a) => Num (AParallelT m a) where
700705 (*) = liftA2 (*)
701706 (-) = liftA2 (-)
702707
703−instance (MonadAsync m, Fractional a) => Fractional (AParallelT m a) where
708+instance (MonadAsync m, Fractional a) => Fractional (CoparallelT m a) where
704709 fromRational n = pure (fromRational n)
705710
706711 recip = fmap recip
707712
708713 (/) = liftA2 (/)
709714
710−instance (MonadAsync m, Floating a) => Floating (AParallelT m a) where
715+instance (MonadAsync m, Floating a) => Floating (CoparallelT m a) where
711716 pi = pure pi
712717
713718 exp = fmap exp
@@ -764,7 +769,7 @@ instance (MonadAsync m, Floating a) => Floating (AParallelT m a) where
764769-- ThreadId 38: Delay 3
765770-- @
766771--
767−-- Unlike 'AParallelT' all iterations are guaranteed to run fairly
772+-- Unlike 'CoparallelT' all iterations are guaranteed to run fairly
768773-- concurrently, unconditionally.
769774--
770775-- Note that round robin composition can only combine a finite number of
@@ -1073,17 +1078,22 @@ adapt = fromStream . toStream
10731078serially :: IsStream t => SerialT m a -> t m a
10741079serially = adapt
10751080
1076−-- | Fix the type of a polymorphic stream as 'InterleavedT'.
1077−coserially :: IsStream t => InterleavedT m a -> t m a
1081+-- | Fix the type of a polymorphic stream as 'CoserialT'.
1082+coserially :: IsStream t => CoserialT m a -> t m a
10781083coserially = adapt
10791084
1080−-- | Fix the type of a polymorphic stream as 'AParallelT'.
1081−coparallely :: IsStream t => AParallelT m a -> t m a
1085+-- | Same as 'coserially'.
1086+{-# DEPRECATED interleaving "Please use coserially instead." #-}
1087+interleaving :: IsStream t => CoserialT m a -> t m a
1088+interleaving = coserially
1089+
1090+-- | Fix the type of a polymorphic stream as 'CoparallelT'.
1091+coparallely :: IsStream t => CoparallelT m a -> t m a
10821092coparallely = adapt
10831093
10841094-- | Same as 'coparallely'.
10851095{-# DEPRECATED asyncly "Please use coparallely instead." #-}
1086−asyncly :: IsStream t => AParallelT m a -> t m a
1096+asyncly :: IsStream t => CoparallelT m a -> t m a
10871097asyncly = coparallely
10881098
10891099-- | Fix the type of a polymorphic stream as 'ParallelT'.
@@ -1114,7 +1124,7 @@ runInterleavedT = runStream . coserially
11141124
11151125-- | Same as @runStream . coparallely@.
11161126{-# DEPRECATED runAsyncT "Please use 'runStream . coparallely' instead." #-}
1117−runAsyncT :: Monad m => AParallelT m a -> m ()
1127+runAsyncT :: Monad m => CoparallelT m a -> m ()
11181128runAsyncT = runStream . coparallely
11191129
11201130-- | Same as @runStream . parallely@.
test/Main.hs+2−2
@@ -19,10 +19,10 @@ singleton a = a .: nil
1919toListSerial :: SerialT IO a -> IO [a]
2020toListSerial = A.toList . serially
2121
22−toListInterleaved :: InterleavedT IO a -> IO [a]
22+toListInterleaved :: CoserialT IO a -> IO [a]
2323toListInterleaved = A.toList . coserially
2424
25−toListAsync :: AParallelT IO a -> IO [a]
25+toListAsync :: CoparallelT IO a -> IO [a]
2626toListAsync = A.toList . coparallely
2727
2828toListParallel :: ParallelT IO a -> IO [a]