on ed030cc help sign in
509 lines 15 kB history raw
1{-# LANGUAGE UndecidableInstances #-}
3-- |
4-- Module : Streamly.Internal.Data.Stream.Serial
5-- Copyright : (c) 2017 Composewell Technologies
6--
7-- License : BSD3
8-- Maintainer : streamly@composewell.com
9-- Stability : experimental
10-- Portability : GHC
11--
12--
13module Streamly.Internal.Data.Stream.Serial
14 (
15 -- * Serial appending stream
16 SerialT
17 , Serial
18 , K.serial
19 , serially
21 -- * Serial interleaving stream
22 , WSerialT
23 , WSerial
24 , wSerial
25 , wSerialFst
26 , wSerialMin
27 , wSerially
29 -- * Construction
30 , unfoldrM
32 -- * Transformation
33 , map
34 , mapM
36 -- * Deprecated
37 , StreamT
38 , InterleavedT
39 , (<=>)
40 , interleaving
41 )
42where
44import Control.Applicative (liftA2)
45import Control.DeepSeq (NFData(..))
46#if MIN_VERSION_deepseq(1,4,3)
47import Control.DeepSeq (NFData1(..))
48#endif
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(..))
61#endif
62import GHC.Exts (IsList(..), IsString(..))
63import Text.Read
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"
81#include "inline.hs"
83------------------------------------------------------------------------------
84-- SerialT
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.
92--
93-- @
94-- import Streamly
95-- import qualified "Streamly.Prelude" as S
96--
97-- main = (S.toList . 'serially' $ (S.fromList [1,2]) \<\> (S.fromList [3,4])) >>= print
98-- @
99-- @
100-- [1,2,3,4]
101-- @
102--
103-- The 'Monad' instance runs the /monadic continuation/ for each
104-- element of the stream, serially.
105--
106-- @
107-- main = S.drain . 'serially' $ do
108-- x <- return 1 \<\> return 2
109-- S.yieldM $ print x
110-- @
111-- @
112-- 1
113-- 2
114-- @
115--
116-- 'SerialT' nests streams serially in a depth first manner.
117--
118-- @
119-- main = S.drain . 'serially' $ do
120-- x <- return 1 \<\> return 2
121-- y <- return 3 \<\> return 4
122-- S.yieldM $ print (x, y)
123-- @
124-- @
125-- (1,3)
126-- (1,4)
127-- (2,3)
128-- (2,4)
129-- @
130--
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.
135--
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.
139--
140-- In the code above, the 'serially' combinator can be omitted as the default
141-- stream type is 'SerialT'.
142--
143-- /Since: 0.2.0 ("Streamly")/
144--
145-- @since 0.8.0
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
151-- for more details.
152--
153-- /Since: 0.2.0 ("Streamly")/
154--
155-- @since 0.8.0
156type Serial = SerialT IO
158-- |
159-- @since 0.1.0
160{-# DEPRECATED StreamT "Please use 'SerialT' instead." #-}
161type StreamT = SerialT
163-- | Fix the type of a polymorphic stream as 'SerialT'.
164--
165-- /Since: 0.1.0 ("Streamly")/
166--
167-- @since 0.8.0
168serially :: IsStream t => SerialT m a -> t m a
169serially = adapt
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
178 fromStream = SerialT
179 consM = consMSerial
180 (|:) = consMSerial
182------------------------------------------------------------------------------
183-- Monad
184------------------------------------------------------------------------------
186instance Monad m => Monad (SerialT m) where
187 return = pure
189 {-# INLINE (>>=) #-}
190 (>>=) = K.bindWith K.serial
192 {-# INLINE (>>) #-}
193 (>>) = (*>)
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------------------------------------------------------------------------------
200-- Other instances
201------------------------------------------------------------------------------
203{-# INLINE mapM #-}
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
207-- |
208-- @
209-- map = fmap
210-- @
211--
212-- Same as 'fmap'.
213--
214-- @
215-- > S.toList $ S.map (+1) $ S.fromList [1,2,3]
216-- [2,3,4]
217-- @
218--
219-- @since 0.4.0
220{-# INLINE map #-}
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
240-- INLINE them.
241instance Monad m => Applicative (SerialT m) where
242 {-# INLINE pure #-}
243 pure = SerialT . K.yield
245 {-# INLINE (<*>) #-}
246 (<*>) = apSerial
247 -- (<*>) = K.apSerial
249#if MIN_VERSION_base(4,10,0)
250 {-# INLINE liftA2 #-}
251 liftA2 f x = (<*>) (fmap f x)
252#endif
254 {-# INLINE (*>) #-}
255 (*>) = apSequence
256 -- (*>) = K.apSerialDiscardFst
258 {-# INLINE (<*) #-}
259 (<*) = apDiscardSnd
260 -- (<*) = K.apSerialDiscardSnd
262MONAD_COMMON_INSTANCES(SerialT,)
263LIST_INSTANCES(SerialT)
264NFDATA1_INSTANCE(SerialT)
265FOLDABLE_INSTANCE(SerialT)
266TRAVERSABLE_INSTANCE(SerialT)
268------------------------------------------------------------------------------
269-- WSerialT
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@.
278--
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.
286--
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
289-- of streams.
290--
291-- The @W@ in the name stands for @wide@ or breadth wise scheduling in
292-- contrast to the depth wise scheduling behavior of 'SerialT'.
293--
294-- @
295-- import Streamly
296-- import qualified "Streamly.Prelude" as S
297--
298-- main = (S.toList . 'wSerially' $ (S.fromList [1,2]) \<\> (S.fromList [3,4])) >>= print
299-- @
300-- @
301-- [1,3,2,4]
302-- @
303--
304-- Similarly, the 'Monad' instance interleaves the iterations of the
305-- inner and the outer loop, nesting loops in a breadth first manner.
306--
307--
308-- @
309-- main = S.drain . 'wSerially' $ do
310-- x <- return 1 \<\> return 2
311-- y <- return 3 \<\> return 4
312-- S.yieldM $ print (x, y)
313-- @
314-- @
315-- (1,3)
316-- (2,3)
317-- (1,4)
318-- (2,4)
319-- @
320--
321-- /Since: 0.2.0 ("Streamly")/
322--
323-- @since 0.8.0
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.
329--
330-- /Since: 0.2.0 ("Streamly")/
331--
332-- @since 0.8.0
333type WSerial = WSerialT IO
335-- |
336-- @since 0.1.0
337{-# DEPRECATED InterleavedT "Please use 'WSerialT' instead." #-}
338type InterleavedT = WSerialT
340-- | Fix the type of a polymorphic stream as 'WSerialT'.
341--
342-- /Since: 0.2.0 ("Streamly")/
343--
344-- @since 0.8.0
345wSerially :: IsStream t => WSerialT m a -> t m a
346wSerially = adapt
348-- | Same as 'wSerially'.
349--
350-- @since 0.1.0
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
362 {-# INLINE consM #-}
363 {-# SPECIALIZE consM :: IO a -> WSerialT IO a -> WSerialT IO a #-}
364 consM :: Monad m => m a -> WSerialT m a -> WSerialT m a
365 consM = consMWSerial
367 {-# INLINE (|:) #-}
368 {-# SPECIALIZE (|:) :: IO a -> WSerialT IO a -> WSerialT IO a #-}
369 (|:) :: Monad m => m a -> WSerialT m a -> WSerialT m a
370 (|:) = consMWSerial
372------------------------------------------------------------------------------
373-- Semigroup
374------------------------------------------------------------------------------
376infixr 6 `wSerial`
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.
382--
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
388-- stream.
389--
390-- @since 0.8.0
391--
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
397 single a = yld a 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.
402--
403-- @since 0.7.0
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
410 where
412 yieldSecond s1 s2 = mkStream $ \st yld sng stp -> do
413 let stop = foldStream st yld sng stp s1
414 single a = yld a 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
419-- stops.
420--
421-- @since 0.7.0
422{-# INLINE wSerialMin #-}
423wSerialMin :: IsStream t => t m a -> t m a -> t m a
424wSerialMin m1 m2 = mkStream $ \st yld sng stp -> do
425 let stop = stp
426 single a = sng a
427 yieldk a r = yld a (wSerial m2 r)
428 foldStream st yieldk single stop m1
430instance Semigroup (WSerialT m a) where
431 (<>) = wSerial
433infixr 5 <=>
435-- | Same as 'wSerial'.
436--
437-- @since 0.1.0
438{-# DEPRECATED (<=>) "Please use 'wSerial' instead." #-}
439{-# INLINE (<=>) #-}
440(<=>) :: IsStream t => t m a -> t m a -> t m a
441(<=>) = wSerial
443------------------------------------------------------------------------------
444-- Monoid
445------------------------------------------------------------------------------
447instance Monoid (WSerialT m a) where
448 mempty = K.nil
449 mappend = (<>)
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
458 {-# INLINE pure #-}
459 pure = WSerialT . K.yield
460 {-# INLINE (<*>) #-}
461 (<*>) = apWSerial
463------------------------------------------------------------------------------
464-- Monad
465------------------------------------------------------------------------------
467instance Monad m => Monad (WSerialT m) where
468 return = pure
469 {-# INLINE (>>=) #-}
470 (>>=) = K.bindWith wSerial
472------------------------------------------------------------------------------
473-- Other instances
474------------------------------------------------------------------------------
476MONAD_COMMON_INSTANCES(WSerialT,)
477LIST_INSTANCES(WSerialT)
478NFDATA1_INSTANCE(WSerialT)
479FOLDABLE_INSTANCE(WSerialT)
480TRAVERSABLE_INSTANCE(WSerialT)
482------------------------------------------------------------------------------
483-- Construction
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
489-- example,
490--
491-- @
492-- let f b =
493-- if b > 3
494-- then return Nothing
495-- else print b >> return (Just (b, b + 1))
496-- in drain $ unfoldrM f 0
497-- @
498-- @
499-- 0
500-- 1
501-- 2
502-- 3
503-- @
504--
505-- /Internal/
506--
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)