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.Streams.Serial
13-- Copyright : (c) 2017 Harendra Kumar
16-- Maintainer : harendra.kumar@gmail.com
17-- Stability : experimental
21module Streamly.Streams.Serial
23 -- * Serial appending stream
25 , StreamT -- deprecated
30 -- * Serial interleaving stream
32 , InterleavedT -- deprecated
37 , interleaving -- deprecated
45import Control.Applicative (liftA2)
46import Control.DeepSeq (NFData(..), NFData1(..), rnf1)
47import Control.Monad (ap)
48import Control.Monad.Base (MonadBase(..), liftBaseDefault)
49import Control.Monad.Catch (MonadThrow, throwM)
50-- import Control.Monad.Error.Class (MonadError(..))
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.Functor.Identity (Identity(..), runIdentity)
56import Data.Foldable (fold)
57import Data.Semigroup (Semigroup(..))
58import GHC.Exts (IsList(..), IsString(..))
59import Text.Read (Lexeme(Ident), lexP, parens, prec, readPrec, readListPrec,
61import Prelude hiding (map, mapM)
63import Streamly.SVar (rstState)
64import Streamly.Streams.StreamK (IsStream(..), adapt, Stream(..))
65import qualified Streamly.Streams.Prelude as P
66import qualified Streamly.Streams.StreamK as K
67import qualified Streamly.Streams.StreamD as D
69#include "Instances.hs"
72------------------------------------------------------------------------------
74------------------------------------------------------------------------------
76-- | Deep serial composition or serial composition with depth first traversal.
77-- The 'Semigroup' instance of 'SerialT' appends two streams serially in a
78-- depth first manner, yielding all elements from the first stream, and then
79-- all elements from the second stream.
83-- import qualified "Streamly.Prelude" as S
85-- main = ('toList' . 'serially' $ (fromFoldable [1,2]) \<\> (fromFoldable [3,4])) >>= print
91-- The 'Monad' instance runs the /monadic continuation/ for each
92-- element of the stream, serially.
95-- main = 'runStream' . 'serially' $ do
96-- x <- return 1 \<\> return 2
104-- 'SerialT' nests streams serially in a depth first manner.
107-- main = 'runStream' . 'serially' $ do
108-- x <- return 1 \<\> return 2
109-- y <- return 3 \<\> return 4
110-- S.yieldM $ print (x, y)
119-- This behavior of 'SerialT' is exactly like a list transformer. We call the
120-- monadic code being run for each element of the stream a monadic
121-- continuation. In imperative paradigm we can think of this composition as
122-- nested @for@ loops and the monadic continuation is the body of the loop. The
123-- loop iterates for all elements of the stream.
125-- The 'serially' combinator can be omitted as the default stream type is
127-- Note that serial composition with depth first traversal can be used to
128-- combine an infinite number of streams as it explores only one stream at a
132newtype SerialT m a = SerialT {getSerialT :: Stream m a}
133 deriving (Semigroup, Monoid, MonadTrans)
135-- | A serial IO stream of elements of type @a@. See 'SerialT' documentation
139type Serial = SerialT IO
143{-# DEPRECATED StreamT "Please use 'SerialT' instead." #-}
144type StreamT = SerialT
146-- | Fix the type of a polymorphic stream as 'SerialT'.
149serially :: IsStream t => SerialT m a -> t m a
152instance IsStream SerialT where
153 toStream = getSerialT
157 {-# SPECIALIZE consM :: IO a -> SerialT IO a -> SerialT IO a #-}
158 consM :: Monad m => m a -> SerialT m a -> SerialT m a
159 consM m r = fromStream $ K.consMSerial m (toStream r)
162 {-# SPECIALIZE (|:) :: IO a -> SerialT IO a -> SerialT IO a #-}
163 (|:) :: Monad m => m a -> SerialT m a -> SerialT m a
164 m |: r = fromStream $ K.consMSerial m (toStream r)
166------------------------------------------------------------------------------
168------------------------------------------------------------------------------
170-- | Polymorphic version of the 'Semigroup' operation '<>' of 'SerialT'.
171-- Appends two streams sequentially, yielding all elements from the first
172-- stream, and then all elements from the second stream.
176serial :: IsStream t => t m a -> t m a -> t m a
177serial m1 m2 = fromStream $ Stream $ \st stp sng yld ->
178 unStream (K.serial (toStream m1) (toStream m2))
179 (rstState st) stp sng yld
181------------------------------------------------------------------------------
183------------------------------------------------------------------------------
185instance Monad m => Monad (SerialT m) where
187 (SerialT (Stream m)) >>= f = SerialT $ Stream $ \st stp sng yld ->
188 let run x = unStream x (rstState st) stp sng yld
189 single a = run $ toStream (f a)
190 yieldk a r = run $ toStream $ f a <> (fromStream r >>= f)
191 in m (rstState st) stp single yieldk
193------------------------------------------------------------------------------
195------------------------------------------------------------------------------
197{-# INLINE_EARLY mapM #-}
198mapM :: (IsStream t, Monad m) => (a -> m b) -> t m a -> t m b
199mapM f m = fromStream $ D.toStreamK $ D.mapM f $ D.fromStreamK (toStream m)
205map :: (IsStream t, Monad m) => (a -> b) -> t m a -> t m b
206map f = mapM (return . f)
208MONAD_APPLICATIVE_INSTANCE(SerialT,)
209MONAD_COMMON_INSTANCES(SerialT,)
211------------------------------------------------------------------------------
213------------------------------------------------------------------------------
215-- | Wide serial composition or serial composition with a breadth first
216-- traversal. The 'Semigroup' instance of 'WSerialT' traverses
217-- the two streams in a breadth first manner. In other words, it interleaves
218-- two streams, yielding one element from each stream alternately.
222-- import qualified "Streamly.Prelude" as S
224-- main = ('toList' . 'wSerially' $ (fromFoldable [1,2]) \<\> (fromFoldable [3,4])) >>= print
230-- Similarly, the 'Monad' instance interleaves the iterations of the
231-- inner and the outer loop, nesting loops in a breadth first manner.
235-- main = 'runStream' . 'wSerially' $ do
236-- x <- return 1 \<\> return 2
237-- y <- return 3 \<\> return 4
238-- S.yieldM $ print (x, y)
247-- Note that a serial composition with breadth first traversal can only combine
248-- a finite number of streams as it needs to retain state for each unfinished
252newtype WSerialT m a = WSerialT {getWSerialT :: Stream m a}
253 deriving (MonadTrans)
255-- | An interleaving serial IO stream of elements of type @a@. See 'WSerialT'
256-- documentation for more details.
259type WSerial = WSerialT IO
263{-# DEPRECATED InterleavedT "Please use 'WSerialT' instead." #-}
264type InterleavedT = WSerialT
266-- | Fix the type of a polymorphic stream as 'WSerialT'.
269wSerially :: IsStream t => WSerialT m a -> t m a
272-- | Same as 'wSerially'.
275{-# DEPRECATED interleaving "Please use wSerially instead." #-}
276interleaving :: IsStream t => WSerialT m a -> t m a
277interleaving = wSerially
279instance IsStream WSerialT where
280 toStream = getWSerialT
281 fromStream = WSerialT
284 {-# SPECIALIZE consM :: IO a -> WSerialT IO a -> WSerialT IO a #-}
285 consM :: Monad m => m a -> WSerialT m a -> WSerialT m a
286 consM m r = fromStream $ K.consMSerial m (toStream r)
289 {-# SPECIALIZE (|:) :: IO a -> WSerialT IO a -> WSerialT IO a #-}
290 (|:) :: Monad m => m a -> WSerialT m a -> WSerialT m a
291 m |: r = fromStream $ K.consMSerial m (toStream r)
293------------------------------------------------------------------------------
295------------------------------------------------------------------------------
297{-# INLINE interleave #-}
298interleave :: Stream m a -> Stream m a -> Stream m a
299interleave m1 m2 = Stream $ \st stp sng yld -> do
300 let stop = unStream m2 (rstState st) stp sng yld
302 yieldk a r = yld a (interleave m2 r)
303 unStream m1 (rstState st) stop single yieldk
305-- | Polymorphic version of the 'Semigroup' operation '<>' of 'WSerialT'.
306-- Interleaves two streams, yielding one element from each stream alternately.
309{-# INLINE wSerial #-}
310wSerial :: IsStream t => t m a -> t m a -> t m a
311wSerial m1 m2 = fromStream $ Stream $ \st stp sng yld ->
312 unStream (interleave (toStream m1) (toStream m2))
313 (rstState st) stp sng yld
315instance Semigroup (WSerialT m a) where
320-- | Same as 'wSerial'.
323{-# DEPRECATED (<=>) "Please use 'wSerial' instead." #-}
325(<=>) :: IsStream t => t m a -> t m a -> t m a
328------------------------------------------------------------------------------
330------------------------------------------------------------------------------
332instance Monoid (WSerialT m a) where
336------------------------------------------------------------------------------
338------------------------------------------------------------------------------
340instance Monad m => Monad (WSerialT m) where
342 (WSerialT (Stream m)) >>= f = WSerialT $ Stream $ \st stp sng yld ->
343 let run x = unStream x (rstState st) stp sng yld
344 single a = run $ toStream (f a)
345 yieldk a r = run $ toStream $ f a <> (fromStream r >>= f)
346 in m (rstState st) stp single yieldk
348------------------------------------------------------------------------------
350------------------------------------------------------------------------------
352MONAD_APPLICATIVE_INSTANCE(WSerialT,)
353MONAD_COMMON_INSTANCES(WSerialT,)
355LIST_INSTANCES(SerialT)
356LIST_INSTANCES(WSerialT)
358FOLDABLE_INSTANCE(SerialT)
359FOLDABLE_INSTANCE(WSerialT)
361TRAVERSABLE_INSTANCE(SerialT)
362TRAVERSABLE_INSTANCE(WSerialT)
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