1{-# LANGUAGE UndecidableInstances #-}
4-- Module : Streamly.Internal.Data.Stream.Serial
5-- Copyright : (c) 2017 Composewell Technologies
8-- Maintainer : streamly@composewell.com
9-- Stability : experimental
13module Streamly.Internal.Data.Stream.Serial
15 -- * Serial appending stream
21 -- * Serial interleaving stream
44import Control.Applicative (liftA2)
45import Control.DeepSeq (NFData(..))
46#if MIN_VERSION_deepseq(1,4,3)
47import Control.DeepSeq (NFData1(..))
49import Control.Monad.Base (MonadBase(..), liftBaseDefault)
50import Control.Monad.Catch (MonadThrow, throwM)
51import Control.Monad.IO.Class (MonadIO(..))
52import Control.Monad.Reader.Class (MonadReader(..))
53import Control.Monad.State.Class (MonadState(..))
54import Control.Monad.Trans.Class (MonadTrans(lift))
55import Data.Foldable (Foldable(foldl'), fold)
56import Data.Functor.Identity (Identity(..), runIdentity)
57import Data.Maybe (fromMaybe)
58import Data.Semigroup (Endo(..))
59#if __GLASGOW_HASKELL__ < 808
60import Data.Semigroup (Semigroup(..))
62import GHC.Exts (IsList(..), IsString(..))
64 ( Lexeme(Ident), lexP, parens, prec, readPrec, readListPrec
65 , readListPrecDefault)
66import Streamly.Internal.BaseCompat ((#.), errorWithoutStackTrace)
67import Streamly.Internal.Data.Stream.StreamK.Type
68 (IsStream(..), adapt, Stream, mkStream, foldStream)
69import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
71import qualified Streamly.Internal.Data.Stream.Prelude as P
72 (cmpBy, foldl', foldr, eqBy, fromList, toList)
73import qualified Streamly.Internal.Data.Stream.StreamK as K (withLocal)
74import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
75import qualified Streamly.Internal.Data.Stream.StreamD.Generate as D (unfoldrM)
76import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
78import Prelude hiding (map, mapM, errorWithoutStackTrace)
80#include "Instances.hs"
83------------------------------------------------------------------------------
85------------------------------------------------------------------------------
87-- | The 'Semigroup' operation for 'SerialT' behaves like a regular append
88-- operation. Therefore, when @a <> b@ is evaluated, stream @a@ is evaluated
89-- first until it exhausts and then stream @b@ is evaluated. In other words,
90-- the elements of stream @b@ are appended to the elements of stream @a@. This
91-- operation can be used to fold an infinite lazy container of streams.
95-- import qualified "Streamly.Prelude" as S
97-- main = (S.toList . 'serially' $ (S.fromList [1,2]) \<\> (S.fromList [3,4])) >>= print
103-- The 'Monad' instance runs the /monadic continuation/ for each
104-- element of the stream, serially.
107-- main = S.drain . 'serially' $ do
108-- x <- return 1 \<\> return 2
116-- 'SerialT' nests streams serially in a depth first manner.
119-- main = S.drain . 'serially' $ do
120-- x <- return 1 \<\> return 2
121-- y <- return 3 \<\> return 4
122-- S.yieldM $ print (x, y)
131-- We call the monadic code being run for each element of the stream a monadic
132-- continuation. In imperative paradigm we can think of this composition as
133-- nested @for@ loops and the monadic continuation is the body of the loop. The
134-- loop iterates for all elements of the stream.
136-- Note that the behavior and semantics of 'SerialT', including 'Semigroup'
137-- and 'Monad' instances are exactly like Haskell lists except that 'SerialT'
138-- can contain effectful actions while lists are pure.
140-- In the code above, the 'serially' combinator can be omitted as the default
141-- stream type is 'SerialT'.
143-- /Since: 0.2.0 ("Streamly")/
146newtype SerialT m a = SerialT {getSerialT :: Stream m a}
147 -- XXX when deriving do we inherit an INLINE?
148 deriving (Semigroup, Monoid, MonadTrans)
150-- | A serial IO stream of elements of type @a@. See 'SerialT' documentation
153-- /Since: 0.2.0 ("Streamly")/
156type Serial = SerialT IO
160{-# DEPRECATED StreamT "Please use 'SerialT' instead." #-}
161type StreamT = SerialT
163-- | Fix the type of a polymorphic stream as 'SerialT'.
165-- /Since: 0.1.0 ("Streamly")/
168serially :: IsStream t => SerialT m a -> t m a
171{-# INLINE consMSerial #-}
172{-# SPECIALIZE consMSerial :: IO a -> SerialT IO a -> SerialT IO a #-}
173consMSerial :: Monad m => m a -> SerialT m a -> SerialT m a
174consMSerial m ms = fromStream $ K.consMStream m (toStream ms)
176instance IsStream SerialT where
177 toStream = getSerialT
182------------------------------------------------------------------------------
184------------------------------------------------------------------------------
186instance Monad m => Monad (SerialT m) where
190 (>>=) = K.bindWith K.serial
195 -- StreamD based implementation
196 -- return = SerialT . D.fromStreamD . D.yield
197 -- m >>= f = D.fromStreamD $ D.concatMap (\a -> D.toStreamD (f a)) (D.toStreamD m)
199------------------------------------------------------------------------------
201------------------------------------------------------------------------------
204mapM :: (IsStream t, Monad m) => (a -> m b) -> t m a -> t m b
205mapM f m = D.fromStreamD $ D.mapM f $ D.toStreamD m
215-- > S.toList $ S.map (+1) $ S.fromList [1,2,3]
221map :: (IsStream t, Monad m) => (a -> b) -> t m a -> t m b
222map f = mapM (return . f)
224{-# INLINE apSerial #-}
225apSerial :: Monad m => SerialT m (a -> b) -> SerialT m a -> SerialT m b
226apSerial (SerialT m1) (SerialT m2) =
227 D.fromStreamD $ D.toStreamD m1 <*> D.toStreamD m2
229{-# INLINE apSequence #-}
230apSequence :: Monad m => SerialT m a -> SerialT m b -> SerialT m b
231apSequence (SerialT m1) (SerialT m2) =
232 D.fromStreamD $ D.toStreamD m1 *> D.toStreamD m2
234{-# INLINE apDiscardSnd #-}
235apDiscardSnd :: Monad m => SerialT m a -> SerialT m b -> SerialT m a
236apDiscardSnd (SerialT m1) (SerialT m2) =
237 D.fromStreamD $ D.toStreamD m1 <* D.toStreamD m2
239-- Note: we need to define all the typeclass operations because we want to
241instance Monad m => Applicative (SerialT m) where
243 pure = SerialT . K.yield
247 -- (<*>) = K.apSerial
249#if MIN_VERSION_base(4,10,0)
250 {-# INLINE liftA2 #-}
251 liftA2 f x = (<*>) (fmap f x)
256 -- (*>) = K.apSerialDiscardFst
260 -- (<*) = K.apSerialDiscardSnd
262MONAD_COMMON_INSTANCES(SerialT,)
263LIST_INSTANCES(SerialT)
264NFDATA1_INSTANCE(SerialT)
265FOLDABLE_INSTANCE(SerialT)
266TRAVERSABLE_INSTANCE(SerialT)
268------------------------------------------------------------------------------
270------------------------------------------------------------------------------
272-- | The 'Semigroup' operation for 'WSerialT' interleaves the elements from the
273-- two streams. Therefore, when @a <> b@ is evaluated, stream @a@ is evaluated
274-- first to produce the first element of the combined stream and then stream
275-- @b@ is evaluated to produce the next element of the combined stream, and
276-- then we go back to evaluating stream @a@ and so on. In other words, the
277-- elements of stream @a@ are interleaved with the elements of stream @b@.
279-- Note that evaluation of @a <> b <> c@ does not schedule @a@, @b@ and @c@
280-- with equal priority. This expression is equivalent to @a <> (b <> c)@,
281-- therefore, it fairly interleaves @a@ with the result of @b <> c@. For
282-- example, @S.fromList [1,2] <> S.fromList [3,4] <> S.fromList [5,6] ::
283-- WSerialT Identity Int@ would result in [1,3,2,5,4,6]. In other words, the
284-- leftmost stream gets the same scheduling priority as the rest of the
285-- streams taken together. The same is true for each subexpression on the right.
287-- Note that this operation cannot be used to fold a container of infinite
288-- streams as the state that it needs to maintain is proportional to the number
291-- The @W@ in the name stands for @wide@ or breadth wise scheduling in
292-- contrast to the depth wise scheduling behavior of 'SerialT'.
296-- import qualified "Streamly.Prelude" as S
298-- main = (S.toList . 'wSerially' $ (S.fromList [1,2]) \<\> (S.fromList [3,4])) >>= print
304-- Similarly, the 'Monad' instance interleaves the iterations of the
305-- inner and the outer loop, nesting loops in a breadth first manner.
309-- main = S.drain . 'wSerially' $ do
310-- x <- return 1 \<\> return 2
311-- y <- return 3 \<\> return 4
312-- S.yieldM $ print (x, y)
321-- /Since: 0.2.0 ("Streamly")/
324newtype WSerialT m a = WSerialT {getWSerialT :: Stream m a}
325 deriving (MonadTrans)
327-- | An interleaving serial IO stream of elements of type @a@. See 'WSerialT'
328-- documentation for more details.
330-- /Since: 0.2.0 ("Streamly")/
333type WSerial = WSerialT IO
337{-# DEPRECATED InterleavedT "Please use 'WSerialT' instead." #-}
338type InterleavedT = WSerialT
340-- | Fix the type of a polymorphic stream as 'WSerialT'.
342-- /Since: 0.2.0 ("Streamly")/
345wSerially :: IsStream t => WSerialT m a -> t m a
348-- | Same as 'wSerially'.
351{-# DEPRECATED interleaving "Please use wSerially instead." #-}
352interleaving :: IsStream t => WSerialT m a -> t m a
353interleaving = wSerially
355consMWSerial :: Monad m => m a -> WSerialT m a -> WSerialT m a
356consMWSerial m ms = fromStream $ K.consMStream m (toStream ms)
358instance IsStream WSerialT where
359 toStream = getWSerialT
360 fromStream = WSerialT
363 {-# SPECIALIZE consM :: IO a -> WSerialT IO a -> WSerialT IO a #-}
364 consM :: Monad m => m a -> WSerialT m a -> WSerialT m a
368 {-# SPECIALIZE (|:) :: IO a -> WSerialT IO a -> WSerialT IO a #-}
369 (|:) :: Monad m => m a -> WSerialT m a -> WSerialT m a
372------------------------------------------------------------------------------
374------------------------------------------------------------------------------
378-- Additionally we can have m elements yield from the first stream and n
379-- elements yielding from the second stream. We can also have time slicing
380-- variants of positional interleaving, e.g. run first stream for m seconds and
381-- run the second stream for n seconds.
383-- Similar combinators can be implemented using WAhead style.
385-- | Polymorphic version of the 'Semigroup' operation '<>' of 'WSerialT'.
386-- Interleaves two streams, yielding one element from each stream alternately.
387-- When one stream stops the rest of the other stream is used in the output
392-- /Since: 0.2.0 ("Streamly")/
393{-# INLINE wSerial #-}
394wSerial :: IsStream t => t m a -> t m a -> t m a
395wSerial m1 m2 = mkStream $ \st yld sng stp -> do
396 let stop = foldStream st yld sng stp m2
398 yieldk a r = yld a (wSerial m2 r)
399 foldStream st yieldk single stop m1
401-- | Like `wSerial` but stops interleaving as soon as the first stream stops.
404{-# INLINE wSerialFst #-}
405wSerialFst :: IsStream t => t m a -> t m a -> t m a
406wSerialFst m1 m2 = mkStream $ \st yld sng stp -> do
407 let yieldFirst a r = yld a (yieldSecond r m2)
408 in foldStream st yieldFirst sng stp m1
412 yieldSecond s1 s2 = mkStream $ \st yld sng stp -> do
413 let stop = foldStream st yld sng stp s1
415 yieldk a r = yld a (wSerial s1 r)
416 in foldStream st yieldk single stop s2
418-- | Like `wSerial` but stops interleaving as soon as any of the two streams
422{-# INLINE wSerialMin #-}
423wSerialMin :: IsStream t => t m a -> t m a -> t m a
424wSerialMin m1 m2 = mkStream $ \st yld sng stp -> do
427 yieldk a r = yld a (wSerial m2 r)
428 foldStream st yieldk single stop m1
430instance Semigroup (WSerialT m a) where
435-- | Same as 'wSerial'.
438{-# DEPRECATED (<=>) "Please use 'wSerial' instead." #-}
440(<=>) :: IsStream t => t m a -> t m a -> t m a
443------------------------------------------------------------------------------
445------------------------------------------------------------------------------
447instance Monoid (WSerialT m a) where
451{-# INLINE apWSerial #-}
452apWSerial :: Monad m => WSerialT m (a -> b) -> WSerialT m a -> WSerialT m b
453apWSerial (WSerialT m1) (WSerialT m2) =
454 let f x1 = K.concatMapBy wSerial (pure . x1) m2
455 in WSerialT $ K.concatMapBy wSerial f m1
457instance Monad m => Applicative (WSerialT m) where
459 pure = WSerialT . K.yield
463------------------------------------------------------------------------------
465------------------------------------------------------------------------------
467instance Monad m => Monad (WSerialT m) where
470 (>>=) = K.bindWith wSerial
472------------------------------------------------------------------------------
474------------------------------------------------------------------------------
476MONAD_COMMON_INSTANCES(WSerialT,)
477LIST_INSTANCES(WSerialT)
478NFDATA1_INSTANCE(WSerialT)
479FOLDABLE_INSTANCE(WSerialT)
480TRAVERSABLE_INSTANCE(WSerialT)
482------------------------------------------------------------------------------
484------------------------------------------------------------------------------
486-- | Build a stream by unfolding a /monadic/ step function starting from a
487-- seed. The step function returns the next element in the stream and the next
488-- seed value. When it is done it returns 'Nothing' and the stream ends. For
494-- then return Nothing
495-- else print b >> return (Just (b, b + 1))
496-- in drain $ unfoldrM f 0
507{-# INLINE unfoldrM #-}
508unfoldrM :: (IsStream t, Monad m) => (b -> m (Maybe (a, b))) -> b -> t m a
509unfoldrM step seed = D.fromStreamD (D.unfoldrM step seed)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list