2{-# LANGUAGE ConstraintKinds #-}
3{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE GeneralizedNewtypeDeriving#-}
6{-# LANGUAGE InstanceSigs #-}
7{-# LANGUAGE MultiParamTypeClasses #-}
8{-# LANGUAGE TypeFamilies #-}
9{-# LANGUAGE UndecidableInstances #-} -- XXX
12-- Module : Streamly.Internal.Data.Stream.Serial
13-- Copyright : (c) 2017 Harendra Kumar
16-- Maintainer : streamly@composewell.com
17-- Stability : experimental
21module Streamly.Internal.Data.Stream.Serial
23 -- * Serial appending stream
29 -- * Serial interleaving stream
52import Control.Applicative (liftA2)
53import Control.DeepSeq (NFData(..))
54#if MIN_VERSION_deepseq(1,4,3)
55import Control.DeepSeq (NFData1(..))
57import Control.Monad.Base (MonadBase(..), liftBaseDefault)
58import Control.Monad.Catch (MonadThrow, throwM)
59-- import Control.Monad.Error.Class (MonadError(..))
60import Control.Monad.IO.Class (MonadIO(..))
61import Control.Monad.Reader.Class (MonadReader(..))
62import Control.Monad.State.Class (MonadState(..))
63import Control.Monad.Trans.Class (MonadTrans(lift))
64import Data.Functor.Identity (Identity(..), runIdentity)
65import Data.Foldable (fold)
66#if __GLASGOW_HASKELL__ < 808
67import Data.Semigroup (Semigroup(..))
69import GHC.Exts (IsList(..), IsString(..))
70import Text.Read (Lexeme(Ident), lexP, parens, prec, readPrec, readListPrec,
72import Prelude hiding (map, mapM)
74import Streamly.Internal.Data.Stream.StreamK (IsStream(..), adapt, Stream, mkStream,
76import qualified Streamly.Internal.Data.Stream.Prelude as P
77import qualified Streamly.Internal.Data.Stream.StreamK as K
78import qualified Streamly.Internal.Data.Stream.StreamD as D
79import qualified Streamly.Internal.Data.Unfold as UF
81#include "Instances.hs"
84------------------------------------------------------------------------------
86------------------------------------------------------------------------------
88-- | The 'Semigroup' operation for 'SerialT' behaves like a regular append
89-- operation. Therefore, when @a <> b@ is evaluated, stream @a@ is evaluated
90-- first until it exhausts and then stream @b@ is evaluated. In other words,
91-- the elements of stream @b@ are appended to the elements of stream @a@. This
92-- operation can be used to fold an infinite lazy container of streams.
96-- import qualified "Streamly.Prelude" as S
98-- main = (S.toList . 'serially' $ (S.fromList [1,2]) \<\> (S.fromList [3,4])) >>= print
104-- The 'Monad' instance runs the /monadic continuation/ for each
105-- element of the stream, serially.
108-- main = S.drain . 'serially' $ do
109-- x <- return 1 \<\> return 2
117-- 'SerialT' nests streams serially in a depth first manner.
120-- main = S.drain . 'serially' $ do
121-- x <- return 1 \<\> return 2
122-- y <- return 3 \<\> return 4
123-- S.yieldM $ print (x, y)
132-- We call the monadic code being run for each element of the stream a monadic
133-- continuation. In imperative paradigm we can think of this composition as
134-- nested @for@ loops and the monadic continuation is the body of the loop. The
135-- loop iterates for all elements of the stream.
137-- Note that the behavior and semantics of 'SerialT', including 'Semigroup'
138-- and 'Monad' instances are exactly like Haskell lists except that 'SerialT'
139-- can contain effectful actions while lists are pure.
141-- In the code above, the 'serially' combinator can be omitted as the default
142-- stream type is 'SerialT'.
145newtype SerialT m a = SerialT {getSerialT :: Stream m a}
146 deriving (Semigroup, Monoid, MonadTrans)
148-- | A serial IO stream of elements of type @a@. See 'SerialT' documentation
152type Serial = SerialT IO
156{-# DEPRECATED StreamT "Please use 'SerialT' instead." #-}
157type StreamT = SerialT
159-- | Fix the type of a polymorphic stream as 'SerialT'.
162serially :: IsStream t => SerialT m a -> t m a
165{-# INLINE consMSerial #-}
166{-# SPECIALIZE consMSerial :: IO a -> SerialT IO a -> SerialT IO a #-}
167consMSerial :: Monad m => m a -> SerialT m a -> SerialT m a
168consMSerial m ms = fromStream $ K.consMStream m (toStream ms)
170instance IsStream SerialT where
171 toStream = getSerialT
176------------------------------------------------------------------------------
178------------------------------------------------------------------------------
180instance Monad m => Monad (SerialT m) where
183 (>>=) = K.bindWith K.serial
185 -- StreamD based implementation
186 -- return = SerialT . D.fromStreamD . D.yield
187 -- m >>= f = D.fromStreamD $ D.concatMap (\a -> D.toStreamD (f a)) (D.toStreamD m)
189------------------------------------------------------------------------------
191------------------------------------------------------------------------------
194mapM :: (IsStream t, Monad m) => (a -> m b) -> t m a -> t m b
195mapM f m = D.fromStreamD $ D.mapM f $ D.toStreamD m
205-- > S.toList $ S.map (+1) $ S.fromList [1,2,3]
211map :: (IsStream t, Monad m) => (a -> b) -> t m a -> t m b
212map f = mapM (return . f)
214{-# INLINE apSerial #-}
215apSerial :: Monad m => SerialT m (a -> b) -> SerialT m a -> SerialT m b
216apSerial (SerialT m1) (SerialT m2) =
217 let f x1 = D.concatMapU (UF.singleton (pure . x1)) (D.toStreamD m2)
218 in D.fromStreamD $ D.concatMap f (D.toStreamD m1)
220instance Monad m => Applicative (SerialT m) where
222 pure = SerialT . K.yield
226MONAD_COMMON_INSTANCES(SerialT,)
227LIST_INSTANCES(SerialT)
228NFDATA1_INSTANCE(SerialT)
229FOLDABLE_INSTANCE(SerialT)
230TRAVERSABLE_INSTANCE(SerialT)
232------------------------------------------------------------------------------
234------------------------------------------------------------------------------
236-- | The 'Semigroup' operation for 'WSerialT' interleaves the elements from the
237-- two streams. Therefore, when @a <> b@ is evaluated, stream @a@ is evaluated
238-- first to produce the first element of the combined stream and then stream
239-- @b@ is evaluated to produce the next element of the combined stream, and
240-- then we go back to evaluating stream @a@ and so on. In other words, the
241-- elements of stream @a@ are interleaved with the elements of stream @b@.
243-- Note that when multiple actions are combined like @a <> b <> c ... <> z@ we
244-- interleave them in a binary fashion i.e. @a@ and @b@ are interleaved with
245-- each other and the result is interleaved with @c@ and so on. This will not
246-- act as a true round-robin scheduling across all the streams. Note that this
247-- operation cannot be used to fold a container of infinite streams as the
248-- state that it needs to maintain is proportional to the number of streams.
252-- import qualified "Streamly.Prelude" as S
254-- main = (S.toList . 'wSerially' $ (S.fromList [1,2]) \<\> (S.fromList [3,4])) >>= print
260-- Similarly, the 'Monad' instance interleaves the iterations of the
261-- inner and the outer loop, nesting loops in a breadth first manner.
265-- main = S.drain . 'wSerially' $ do
266-- x <- return 1 \<\> return 2
267-- y <- return 3 \<\> return 4
268-- S.yieldM $ print (x, y)
278newtype WSerialT m a = WSerialT {getWSerialT :: Stream m a}
279 deriving (MonadTrans)
281-- | An interleaving serial IO stream of elements of type @a@. See 'WSerialT'
282-- documentation for more details.
285type WSerial = WSerialT IO
289{-# DEPRECATED InterleavedT "Please use 'WSerialT' instead." #-}
290type InterleavedT = WSerialT
292-- | Fix the type of a polymorphic stream as 'WSerialT'.
295wSerially :: IsStream t => WSerialT m a -> t m a
298-- | Same as 'wSerially'.
301{-# DEPRECATED interleaving "Please use wSerially instead." #-}
302interleaving :: IsStream t => WSerialT m a -> t m a
303interleaving = wSerially
305consMWSerial :: Monad m => m a -> WSerialT m a -> WSerialT m a
306consMWSerial m ms = fromStream $ K.consMStream m (toStream ms)
308instance IsStream WSerialT where
309 toStream = getWSerialT
310 fromStream = WSerialT
313 {-# SPECIALIZE consM :: IO a -> WSerialT IO a -> WSerialT IO a #-}
314 consM :: Monad m => m a -> WSerialT m a -> WSerialT m a
318 {-# SPECIALIZE (|:) :: IO a -> WSerialT IO a -> WSerialT IO a #-}
319 (|:) :: Monad m => m a -> WSerialT m a -> WSerialT m a
322------------------------------------------------------------------------------
324------------------------------------------------------------------------------
326-- Additionally we can have m elements yield from the first stream and n
327-- elements yielding from the second stream. We can also have time slicing
328-- variants of positional interleaving, e.g. run first stream for m seconds and
329-- run the second stream for n seconds.
331-- Similar combinators can be implemented using WAhead style.
333-- | Polymorphic version of the 'Semigroup' operation '<>' of 'WSerialT'.
334-- Interleaves two streams, yielding one element from each stream alternately.
335-- When one stream stops the rest of the other stream is used in the output
339{-# INLINE wSerial #-}
340wSerial :: IsStream t => t m a -> t m a -> t m a
341wSerial m1 m2 = mkStream $ \st yld sng stp -> do
342 let stop = foldStream st yld sng stp m2
344 yieldk a r = yld a (wSerial m2 r)
345 foldStream st yieldk single stop m1
347-- | Like `wSerial` but stops interleaving as soon as the first stream stops.
350{-# INLINE wSerialFst #-}
351wSerialFst :: IsStream t => t m a -> t m a -> t m a
352wSerialFst m1 m2 = mkStream $ \st yld sng stp -> do
353 let yieldFirst a r = yld a (yieldSecond r m2)
354 in foldStream st yieldFirst sng stp m1
358 yieldSecond s1 s2 = mkStream $ \st yld sng stp -> do
359 let stop = foldStream st yld sng stp s1
361 yieldk a r = yld a (wSerial s1 r)
362 in foldStream st yieldk single stop s2
364-- | Like `wSerial` but stops interleaving as soon as any of the two streams
368{-# INLINE wSerialMin #-}
369wSerialMin :: IsStream t => t m a -> t m a -> t m a
370wSerialMin m1 m2 = mkStream $ \st yld sng stp -> do
373 yieldk a r = yld a (wSerial m2 r)
374 foldStream st yieldk single stop m1
376instance Semigroup (WSerialT m a) where
381-- | Same as 'wSerial'.
384{-# DEPRECATED (<=>) "Please use 'wSerial' instead." #-}
386(<=>) :: IsStream t => t m a -> t m a -> t m a
389------------------------------------------------------------------------------
391------------------------------------------------------------------------------
393instance Monoid (WSerialT m a) where
397{-# INLINE apWSerial #-}
398apWSerial :: Monad m => WSerialT m (a -> b) -> WSerialT m a -> WSerialT m b
399apWSerial (WSerialT m1) (WSerialT m2) =
400 let f x1 = K.concatMapBy wSerial (pure . x1) m2
401 in WSerialT $ K.concatMapBy wSerial f m1
403instance Monad m => Applicative (WSerialT m) where
405 pure = WSerialT . K.yield
409------------------------------------------------------------------------------
411------------------------------------------------------------------------------
413instance Monad m => Monad (WSerialT m) where
416 (>>=) = K.bindWith wSerial
418------------------------------------------------------------------------------
420------------------------------------------------------------------------------
422MONAD_COMMON_INSTANCES(WSerialT,)
423LIST_INSTANCES(WSerialT)
424NFDATA1_INSTANCE(WSerialT)
425FOLDABLE_INSTANCE(WSerialT)
426TRAVERSABLE_INSTANCE(WSerialT)
428------------------------------------------------------------------------------
430------------------------------------------------------------------------------
432-- | Build a stream by unfolding a /monadic/ step function starting from a
433-- seed. The step function returns the next element in the stream and the next
434-- seed value. When it is done it returns 'Nothing' and the stream ends. For
440-- then return Nothing
441-- else print b >> return (Just (b, b + 1))
442-- in drain $ unfoldrM f 0
453{-# INLINE unfoldrM #-}
454unfoldrM :: (IsStream t, Monad m) => (b -> m (Maybe (a, b))) -> b -> t m a
455unfoldrM 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