@@ -161,10 +161,7 @@ module Streamly.Streams.StreamK
161161where
162162
163163import Control.Monad (void)
164−import Control.Monad.IO.Class (MonadIO(liftIO))
165164import Control.Monad.Reader.Class (MonadReader(..))
166−import Control.Monad.Trans.Class (MonadTrans(lift))
167−import Data.Semigroup (Semigroup(..))
168165import Prelude
169166 hiding (foldl, foldr, last, map, mapM, mapM_, repeat, sequence,
170167 take, filter, all, any, takeWhile, drop, dropWhile, minimum,
@@ -173,180 +170,18 @@ import Prelude
173170import qualified Prelude
174171
175172import Streamly.SVar
176−
177−------------------------------------------------------------------------------
178−-- The basic stream type
179−------------------------------------------------------------------------------
180−
181−-- | The type @Stream m a@ represents a monadic stream of values of type 'a'
182−-- constructed using actions in monad 'm'. It uses stop, singleton and yield
183−-- continuations equivalent to the following direct style type:
184−--
185−-- @
186−-- data Stream m a = Stop | Singleton a | Yield a (Stream m a)
187−-- @
188−--
189−-- To facilitate parallel composition we maintain a local state in an 'SVar'
190−-- that is shared across and is used for synchronization of the streams being
191−-- composed.
192−--
193−-- The singleton case can be expressed in terms of stop and yield but we have
194−-- it as a separate case to optimize composition operations for streams with
195−-- single element. We build singleton streams in the implementation of 'pure'
196−-- for Applicative and Monad, and in 'lift' for MonadTrans.
197−--
198−newtype Stream m a =
199− Stream (forall r.
200− State Stream m a -- state
201− -> m r -- stop
202− -> (a -> m r) -- singleton
203− -> (a -> Stream m a -> m r) -- yield
204− -> m r
205− )
206−
207−-- Run a stream in detached mode i.e. not joining an SVar
208−{-# INLINE unStream #-}
209−unStream ::
210− Stream m a
211− -> State Stream m b -- state
212− -> m r -- stop
213− -> (a -> m r) -- singleton
214− -> (a -> Stream m a -> m r) -- yield
215− -> m r
216−unStream (Stream runner) st = runner (adaptState st)
173+import Streamly.Streams.StreamKType
217174
218175-- | Detach a stream from an SVar
219176{-# INLINE unShare #-}
220177unShare :: Stream m a -> Stream m a
221−unShare x = Stream $ \st stp sng yld ->
178+unShare x = mkStream $ \st stp sng yld ->
222179 unStream x st stp sng yld
223180
224−-- | Like unstream, but passes a shared SVar across continuations.
225−{-# INLINE unStreamShared #-}
226−unStreamShared ::
227− Stream m a
228− -> State Stream m a -- state
229− -> m r -- stop
230− -> (a -> m r) -- singleton
231− -> (a -> Stream m a -> m r) -- yield
232− -> m r
233−unStreamShared (Stream runner) st stp sng yld = runner st stp sng yld
234−
235−-- Run the stream using a run function associated with the SVar that runs the
236−-- streams with a captured snapshot of the monadic state.
237−{-# INLINE runStreamSVar #-}
238−runStreamSVar
239− :: MonadIO m
240− => SVar Stream m a
241− -> Stream m a
242− -> State Stream m a -- state
243− -> m r -- stop
244− -> (a -> m r) -- singleton
245− -> (a -> Stream m a -> m r) -- yield
246− -> m ()
247−runStreamSVar sv m st stp sng yld =
248− let mrun = runInIO $ svarMrun sv
249− in void $ liftIO $ mrun $ unStreamShared m st stp sng yld
250−
251−------------------------------------------------------------------------------
252−-- Types that can behave as a Stream
253−------------------------------------------------------------------------------
254−
255−infixr 5 `consM`
256−infixr 5 |:
257−
258−-- | Class of types that can represent a stream of elements of some type 'a' in
259−-- some monad 'm'.
260−--
261−-- @since 0.2.0
262−class IsStream t where
263− toStream :: t m a -> Stream m a
264− fromStream :: Stream m a -> t m a
265− -- | Constructs a stream by adding a monadic action at the head of an
266− -- existing stream. For example:
267− --
268− -- @
269− -- > toList $ getLine \`consM` getLine \`consM` nil
270− -- hello
271− -- world
272− -- ["hello","world"]
273− -- @
274− --
275− -- /Concurrent (do not use 'parallely' to construct infinite streams)/
276− --
277− -- @since 0.2.0
278− consM :: MonadAsync m => m a -> t m a -> t m a
279− -- | Operator equivalent of 'consM'. We can read it as "@parallel colon@"
280− -- to remember that @|@ comes before ':'.
281− --
282− -- @
283− -- > toList $ getLine |: getLine |: nil
284− -- hello
285− -- world
286− -- ["hello","world"]
287− -- @
288− --
289− -- @
290− -- let delay = threadDelay 1000000 >> print 1
291− -- runStream $ serially $ delay |: delay |: delay |: nil
292− -- runStream $ parallely $ delay |: delay |: delay |: nil
293− -- @
294− --
295− -- /Concurrent (do not use 'parallely' to construct infinite streams)/
296− --
297− -- @since 0.2.0
298− (|:) :: MonadAsync m => m a -> t m a -> t m a
299− -- We can define (|:) just as 'consM' but it is defined explicitly for each
300− -- type because we want to use SPECIALIZE pragma on the definition.
301−
302−-- | Same as 'IsStream'.
303−--
304−-- @since 0.1.0
305−{-# DEPRECATED Streaming "Please use IsStream instead." #-}
306−type Streaming = IsStream
307−
308−-------------------------------------------------------------------------------
309−-- Type adapting combinators
310−-------------------------------------------------------------------------------
311−
312−-- | Adapt any specific stream type to any other specific stream type.
313−--
314−-- @since 0.1.0
315−adapt :: (IsStream t1, IsStream t2) => t1 m a -> t2 m a
316−adapt = fromStream . toStream
317−
318−------------------------------------------------------------------------------
319−-- Building a stream
320−------------------------------------------------------------------------------
321−
322−-- | Build a stream from an 'SVar', a stop continuation, a singleton stream
323−-- continuation and a yield continuation.
324−mkStream:: IsStream t
325− => (forall r. State Stream m a
326− -> m r
327− -> (a -> m r)
328− -> (a -> t m a -> m r)
329− -> m r)
330− -> t m a
331−mkStream k = fromStream $ Stream $ \st stp sng yld ->
332− let yieldk a r = yld a (toStream r)
333− in k st stp sng yieldk
334−
335181------------------------------------------------------------------------------
336182-- Construction
337183------------------------------------------------------------------------------
338184
339−-- | An empty stream.
340−--
341−-- @
342−-- > toList nil
343−-- []
344−-- @
345−--
346−-- @since 0.1.0
347−nil :: IsStream t => t m a
348−nil = fromStream $ Stream $ \_ stp _ _ -> stp
349−
350185infixr 5 `cons`
351186
352187-- faster than consM because there is no bind.
@@ -362,7 +197,7 @@ infixr 5 `cons`
362197--
363198-- @since 0.1.0
364199cons :: IsStream t => a -> t m a -> t m a
365−cons a r = fromStream $ Stream $ \_ _ _ yld -> yld a (toStream r)
200+cons a r = fromStream $ mkStream $ \_ _ _ yld -> yld a (toStream r)
366201
367202infixr 5 .:
368203
@@ -377,47 +212,25 @@ infixr 5 .:
377212(.:) :: IsStream t => a -> t m a -> t m a
378213(.:) = cons
379214
380−{-# INLINE consMSerial #-}
381−consMSerial :: (Monad m) => m a -> Stream m a -> Stream m a
382−consMSerial m r = Stream $ \_ _ _ yld -> m >>= \a -> yld a r
383−
384215------------------------------------------------------------------------------
385216-- Asynchronous construction
386217------------------------------------------------------------------------------
387218
388219-- | Make an empty stream from a callback function.
389220nilK :: IsStream t => (forall r. m r -> m r) -> t m a
390−nilK k = fromStream $ Stream $ \_ stp _ _ -> k stp
221+nilK k = fromStream $ mkStream $ \_ stp _ _ -> k stp
391222
392223-- | Make a singleton stream from a one shot callback function.
393224yieldK :: IsStream t => (forall r. (a -> m r) -> m r) -> t m a
394−yieldK k = fromStream $ Stream $ \_ _ sng _ -> k sng
225+yieldK k = fromStream $ mkStream $ \_ _ sng _ -> k sng
395226
396227-- | Construct a stream from a callback function.
397228consK :: IsStream t => (forall r. (a -> m r) -> m r) -> t m a -> t m a
398−consK k r = fromStream $ Stream $ \_ _ _ yld -> k (\x -> yld x (toStream r))
229+consK k r = fromStream $ mkStream $ \_ _ _ yld -> k (\x -> yld x (toStream r))
399230
400231-- XXX consK with concurrent callbacks
401232-- XXX Build a stream from a repeating callback function.
402233
403−-------------------------------------------------------------------------------
404−-- IsStream Stream
405−-------------------------------------------------------------------------------
406−
407−instance IsStream Stream where
408− toStream = id
409− fromStream = id
410−
411− {-# INLINE consM #-}
412− {-# SPECIALIZE consM :: IO a -> Stream IO a -> Stream IO a #-}
413− consM :: Monad m => m a -> Stream m a -> Stream m a
414− consM = consMSerial
415−
416− {-# INLINE (|:) #-}
417− {-# SPECIALIZE (|:) :: IO a -> Stream IO a -> Stream IO a #-}
418− (|:) :: Monad m => m a -> Stream m a -> Stream m a
419− (|:) = consMSerial
420−
421234-------------------------------------------------------------------------------
422235-- Deconstruction
423236-------------------------------------------------------------------------------
@@ -438,7 +251,7 @@ uncons m =
438251unfoldr :: IsStream t => (b -> Maybe (a, b)) -> b -> t m a
439252unfoldr step = fromStream . go
440253 where
441− go s = Stream $ \_ stp _ yld ->
254+ go s = mkStream $ \_ stp _ yld ->
442255 case step s of
443256 Nothing -> stp
444257 Just (a, b) -> yld a (go b)
@@ -447,7 +260,7 @@ unfoldr step = fromStream . go
447260unfoldrM :: (IsStream t, MonadAsync m) => (b -> m (Maybe (a, b))) -> b -> t m a
448261unfoldrM step = go
449262 where
450− go s = fromStream $ Stream $ \svr stp sng yld -> do
263+ go s = fromStream $ mkStream $ \svr stp sng yld -> do
451264 mayb <- step s
452265 case mayb of
453266 Nothing -> stp
@@ -459,11 +272,7 @@ unfoldrM step = go
459272-------------------------------------------------------------------------------
460273
461274yield :: IsStream t => a -> t m a
462−yield a = fromStream $ Stream $ \_ _ single _ -> single a
463−
464−{-# INLINE yieldM #-}
465−yieldM :: (Monad m, IsStream t) => m a -> t m a
466−yieldM m = fromStream $ Stream $ \_ _ single _ -> m >>= single
275+yield a = fromStream $ mkStream $ \_ _ single _ -> single a
467276
468277-- | Same as yieldM
469278--
@@ -523,20 +332,6 @@ fromStreamK = id
523332-- Elimination by Folding
524333-------------------------------------------------------------------------------
525334
526−-- | Fold a stream by providing an SVar, a stop continuation, a singleton
527−-- continuation and a yield continuation.
528−foldStream
529− :: IsStream t
530− => State Stream m a
531− -> m r
532− -> (a -> m r)
533− -> (a -> t m a -> m r)
534− -> t m a
535− -> m r
536−foldStream st blank single step m =
537− let yieldk a x = step a (fromStream x)
538− in unStreamShared (toStream m) st blank single yieldk
539−
540335-- | Lazy right associative fold.
541336foldr :: (IsStream t, Monad m) => (a -> b -> b) -> b -> t m a -> m b
542337foldr step acc m = go (toStream m)
@@ -594,7 +389,7 @@ foldx step begin done m = get $ go (toStream m) begin
594389 -- however that is more expensive because of unnecessary recursion
595390 -- that cannot be tail call optimized. Unfolding recursion explicitly via
596391 -- continuations is much more efficient.
597− go m1 !acc = Stream $ \_ _ sng yld ->
392+ go m1 !acc = mkStream $ \_ _ sng yld ->
598393 let stop = sng acc
599394 single a = sng $ step acc a
600395 yieldk a r =
@@ -670,7 +465,7 @@ init m = go1 (toStream m)
670465 case r of
671466 Nothing -> return Nothing
672467 Just (h, t) -> return . Just . fromStream $ go h t
673− go p m1 = Stream $ \_ stp sng yld ->
468+ go p m1 = mkStream $ \_ stp sng yld ->
674469 let single _ = sng p
675470 yieldk a x = yld p $ go a x
676471 in unStream m1 defState stp single yieldk
@@ -850,7 +645,7 @@ find p = findM (return . p)
850645findIndices :: IsStream t => (a -> Bool) -> t m a -> t m Int
851646findIndices p = fromStream . go 0 . toStream
852647 where
853− go offset m1 = Stream $ \st stp sng yld ->
648+ go offset m1 = mkStream $ \st stp sng yld ->
854649 let single a | p a = sng offset
855650 | otherwise = stp
856651 yieldk a x | p a = yld offset $ go (offset + 1) x
@@ -893,7 +688,7 @@ scanx :: IsStream t => (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
893688scanx step begin done m =
894689 cons (done begin) $ fromStream $ go (toStream m) begin
895690 where
896− go m1 !acc = Stream $ \st stp sng yld ->
691+ go m1 !acc = mkStream $ \st stp sng yld ->
897692 let single a = sng (done $ step acc a)
898693 yieldk a r =
899694 let s = step acc a
@@ -912,7 +707,7 @@ scanl' step begin = scanx step begin id
912707filter :: IsStream t => (a -> Bool) -> t m a -> t m a
913708filter p m = fromStream $ go (toStream m)
914709 where
915− go m1 = Stream $ \st stp sng yld ->
710+ go m1 = mkStream $ \st stp sng yld ->
916711 let single a | p a = sng a
917712 | otherwise = stp
918713 yieldk a r | p a = yld a (go r)
@@ -923,7 +718,7 @@ filter p m = fromStream $ go (toStream m)
923718take :: IsStream t => Int -> t m a -> t m a
924719take n m = fromStream $ go n (toStream m)
925720 where
926− go n1 m1 = Stream $ \st stp sng yld ->
721+ go n1 m1 = mkStream $ \st stp sng yld ->
927722 let yieldk a r = yld a (go (n1 - 1) r)
928723 in if n1 <= 0
929724 then stp
@@ -933,7 +728,7 @@ take n m = fromStream $ go n (toStream m)
933728takeWhile :: IsStream t => (a -> Bool) -> t m a -> t m a
934729takeWhile p m = fromStream $ go (toStream m)
935730 where
936− go m1 = Stream $ \st stp sng yld ->
731+ go m1 = mkStream $ \st stp sng yld ->
937732 let single a | p a = sng a
938733 | otherwise = stp
939734 yieldk a r | p a = yld a (go r)
@@ -941,10 +736,10 @@ takeWhile p m = fromStream $ go (toStream m)
941736 in unStream m1 st stp single yieldk
942737
943738drop :: IsStream t => Int -> t m a -> t m a
944−drop n m = fromStream $ Stream $ \st stp sng yld ->
739+drop n m = fromStream $ mkStream $ \st stp sng yld ->
945740 unStream (go n (toStream m)) st stp sng yld
946741 where
947− go n1 m1 = Stream $ \st stp sng yld ->
742+ go n1 m1 = mkStream $ \st stp sng yld ->
948743 let single _ = stp
949744 yieldk _ r = (unStream $ go (n1 - 1) r) st stp sng yld
950745 -- Somehow "<=" check performs better than a ">"
@@ -956,7 +751,7 @@ drop n m = fromStream $ Stream $ \st stp sng yld ->
956751dropWhile :: IsStream t => (a -> Bool) -> t m a -> t m a
957752dropWhile p m = fromStream $ go (toStream m)
958753 where
959− go m1 = Stream $ \st stp sng yld ->
754+ go m1 = mkStream $ \st stp sng yld ->
960755 let single a | p a = stp
961756 | otherwise = sng a
962757 yieldk a r | p a = unStream r st stp single yieldk
@@ -967,20 +762,13 @@ dropWhile p m = fromStream $ go (toStream m)
967762-- Mapping
968763-------------------------------------------------------------------------------
969764
970−{-# INLINE map #-}
971−map :: (IsStream t, Monad m) => (a -> b) -> t m a -> t m b
972−map f m = fromStream $ Stream $ \st stp sng yld ->
973− let single = sng . f
974− yieldk a r = yld (f a) (fmap f r)
975− in unStream (toStream m) st stp single yieldk
976−
977765-- Be careful when modifying this, this uses a consM (|:) deliberately to allow
978766-- other stream types to overload it.
979767{-# INLINE mapM #-}
980768mapM :: (IsStream t, MonadAsync m) => (a -> m b) -> t m a -> t m b
981769mapM f m = go (toStream m)
982770 where
983− go m1 = fromStream $ Stream $ \st stp sng yld ->
771+ go m1 = fromStream $ mkStream $ \st stp sng yld ->
984772 let single a = f a >>= sng
985773 yieldk a r = unStreamShared (toStream (f a |: go r)) st stp sng yld
986774 in unStream m1 st stp single yieldk
@@ -991,7 +779,7 @@ mapM f m = go (toStream m)
991779sequence :: (IsStream t, MonadAsync m) => t m (m a) -> t m a
992780sequence m = go (toStream m)
993781 where
994− go m1 = fromStream $ Stream $ \st stp sng yld ->
782+ go m1 = fromStream $ mkStream $ \st stp sng yld ->
995783 let single ma = ma >>= sng
996784 yieldk ma r = unStreamShared (toStream $ ma |: go r) st stp sng yld
997785 in unStream m1 st stp single yieldk
@@ -1004,10 +792,10 @@ sequence m = go (toStream m)
1004792intersperseM :: (IsStream t, MonadAsync m) => m a -> t m a -> t m a
1005793intersperseM a m = fromStream $ prependingStart (toStream m)
1006794 where
1007− prependingStart m1 = Stream $ \st stp sng yld ->
795+ prependingStart m1 = mkStream $ \st stp sng yld ->
1008796 let yieldk i x = unStreamShared (return i |: go x) st stp sng yld
1009797 in unStream m1 st stp sng yieldk
1010− go m2 = fromStream $ Stream $ \st stp sng yld ->
798+ go m2 = fromStream $ mkStream $ \st stp sng yld ->
1011799 let single i = unStreamShared (a |: yield i) st stp sng yld
1012800 yieldk i x = unStreamShared (a |: return i |: go x) st stp sng yld
1013801 in unStream m2 st stp single yieldk
@@ -1016,7 +804,7 @@ intersperseM a m = fromStream $ prependingStart (toStream m)
1016804insertBy :: IsStream t => (a -> a -> Ordering) -> a -> t m a -> t m a
1017805insertBy cmp x m = fromStream $ go (toStream m)
1018806 where
1019− go m1 = Stream $ \st _ _ yld ->
807+ go m1 = mkStream $ \st _ _ yld ->
1020808 let single a = case cmp x a of
1021809 GT -> yld a (yield x)
1022810 _ -> yld x (yield a)
@@ -1034,7 +822,7 @@ insertBy cmp x m = fromStream $ go (toStream m)
1034822deleteBy :: IsStream t => (a -> a -> Bool) -> a -> t m a -> t m a
1035823deleteBy eq x m = fromStream $ go (toStream m)
1036824 where
1037− go m1 = Stream $ \st stp sng yld ->
825+ go m1 = mkStream $ \st stp sng yld ->
1038826 let single a = if eq x a then stp else sng a
1039827 yieldk a r = if eq x a
1040828 then unStream r st stp sng yld
@@ -1049,7 +837,7 @@ deleteBy eq x m = fromStream $ go (toStream m)
1049837mapMaybe :: IsStream t => (a -> Maybe b) -> t m a -> t m b
1050838mapMaybe f m = go (toStream m)
1051839 where
1052− go m1 = fromStream $ Stream $ \st stp sng yld ->
840+ go m1 = fromStream $ mkStream $ \st stp sng yld ->
1053841 let single a = case f a of
1054842 Just b -> sng b
1055843 Nothing -> stp
@@ -1066,7 +854,7 @@ mapMaybe f m = go (toStream m)
1066854zipWithS :: (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
1067855zipWithS f = go
1068856 where
1069− go mx my = Stream $ \st stp sng yld -> do
857+ go mx my = mkStream $ \st stp sng yld -> do
1070858 let merge a ra =
1071859 let single2 b = sng (f a b)
1072860 yield2 b rb = yld (f a b) (go ra rb)
@@ -1088,7 +876,7 @@ zipWith f m1 m2 = fromStream $ zipWithS f (toStream m1) (toStream m2)
1088876zipWithM :: (IsStream t, Monad m) => (a -> b -> m c) -> t m a -> t m b -> t m c
1089877zipWithM f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
1090878 where
1091− go mx my = Stream $ \st stp sng yld -> do
879+ go mx my = mkStream $ \st stp sng yld -> do
1092880 let merge a ra =
1093881 let runIt x = unStream x st stp sng yld
1094882 single2 b = f a b >>= sng
@@ -1108,7 +896,7 @@ mergeByS
1108896 => (a -> a -> m Ordering) -> Stream m a -> Stream m a -> Stream m a
1109897mergeByS cmp = go
1110898 where
1111− go mx my = Stream $ \st stp sng yld -> do
899+ go mx my = mkStream $ \st stp sng yld -> do
1112900 let mergeWithY a ra =
1113901 let stop2 = unStream mx st stp sng yld
1114902 single2 b = do
@@ -1158,40 +946,6 @@ the m = do
1158946 | otherwise = return Nothing
1159947 in unStream m1 defState (return $ Just h) single yieldk
1160948
1161−------------------------------------------------------------------------------
1162−-- Semigroup
1163−------------------------------------------------------------------------------
1164−
1165−-- | Concatenates two streams sequentially i.e. the first stream is
1166−-- exhausted completely before yielding any element from the second stream.
1167−{-# INLINE serial #-}
1168−serial :: Stream m a -> Stream m a -> Stream m a
1169−serial m1 m2 = go m1
1170− where
1171− go m = Stream $ \st stp sng yld ->
1172− let stop = unStream m2 st stp sng yld
1173− single a = yld a m2
1174− yieldk a r = yld a (go r)
1175− in unStream m st stop single yieldk
1176−
1177−instance Semigroup (Stream m a) where
1178− (<>) = serial
1179−
1180−------------------------------------------------------------------------------
1181−-- Monoid
1182−------------------------------------------------------------------------------
1183−
1184−instance Monoid (Stream m a) where
1185− mempty = nil
1186− mappend = (<>)
1187−
1188−-------------------------------------------------------------------------------
1189−-- Functor
1190−-------------------------------------------------------------------------------
1191−
1192−instance Monad m => Functor (Stream m) where
1193− fmap = map
1194−
1195949-------------------------------------------------------------------------------
1196950-- Bind utility
1197951-------------------------------------------------------------------------------
@@ -1205,7 +959,7 @@ bindWith
1205959bindWith par m1 f = go m1
1206960 where
1207961 go m =
1208− Stream $ \st stp sng yld ->
962+ mkStream $ \st stp sng yld ->
1209963 let runShared x = unStreamShared x st stp sng yld
1210964 runIsolated x = unStream x st stp sng yld
1211965
@@ -1218,7 +972,7 @@ bindWith par m1 f = go m1
1218972------------------------------------------------------------------------------
1219973
1220974_alt :: Stream m a -> Stream m a -> Stream m a
1221−_alt m1 m2 = Stream $ \st stp sng yld ->
975+_alt m1 m2 = mkStream $ \st stp sng yld ->
1222976 let stop = unStream m2 st stp sng yld
1223977 in unStream m1 st stop sng yld
1224978
@@ -1228,7 +982,7 @@ _alt m1 m2 = Stream $ \st stp sng yld ->
1228982
1229983withLocal :: MonadReader r m => (r -> r) -> Stream m a -> Stream m a
1230984withLocal f m =
1231− Stream $ \st stp sng yld ->
985+ mkStream $ \st stp sng yld ->
1232986 let single = local f . sng
1233987 yieldk a r = local f $ yld a (withLocal f r)
1234988 in unStream m st (local f stp) single yieldk
@@ -1243,16 +997,10 @@ withCatchError
1243997 :: MonadError e m
1244998 => Stream m a -> (e -> Stream m a) -> Stream m a
1245999withCatchError m h =
1246− Stream $ \_ stp sng yld ->
1000+ mkStream $ \_ stp sng yld ->
12471001 let run x = unStream x Nothing stp sng yieldk
12481002 handle r = r `catchError` \e -> run $ h e
12491003 yieldk a r = yld a (withCatchError r h)
12501004 in handle $ run m
12511005-}
12521006
1253−-------------------------------------------------------------------------------
1254−-- Transformers
1255−-------------------------------------------------------------------------------
1256−
1257−instance MonadTrans Stream where
1258− lift = yieldM