@@ -250,6 +250,7 @@ import Control.Exception (Exception, mask_)
250import Control.Monad.Catch (MonadCatch)
250import Control.Monad.Catch (MonadCatch)
251import Control.Monad.IO.Class (MonadIO(..))
251import Control.Monad.IO.Class (MonadIO(..))
252import Control.Monad.Trans.Control (MonadBaseControl, liftBaseOp_)
252import Control.Monad.Trans.Control (MonadBaseControl, liftBaseOp_)
253+import Data.Functor (($>))
253import GHC.Types (SPEC(..))
254import GHC.Types (SPEC(..))
254import Streamly.Internal.Control.Concurrent (MonadAsync)
255import Streamly.Internal.Control.Concurrent (MonadAsync)
255import Streamly.Internal.Data.Fold.Type (Fold(..))
256import Streamly.Internal.Data.Fold.Type (Fold(..))
@@ -417,25 +418,20 @@ mapMWithInput f (Unfold ustep uinject) = Unfold step inject
418-- /Internal/
419-- /Internal/
419{-# INLINE_NORMAL either #-}
420{-# INLINE_NORMAL either #-}
420−either :: Monad m => Unfold m a b -> Unfold m (Either a a) (Either b b)
421+either :: Applicative m => Unfold m a b -> Unfold m (Either a a) (Either b b)
421either (Unfold step1 inject1) = Unfold step inject
422either (Unfold step1 inject1) = Unfold step inject
425− inject (Left a) = do
426+ inject (Left a) = (, Left) <$> inject1 a
428− inject (Right a) = do
427+ inject (Right a) = (, Right) <$> inject1 a
432 {-# INLINE_LATE step #-}
429 {-# INLINE_LATE step #-}
433 step (st, f) = do
430 step (st, f) = do
435− return $ case r of
431+ (\case
436 Yield x s -> Yield (f x) (s, f)
432 Yield x s -> Yield (f x) (s, f)
437 Skip s -> Skip (s, f)
433 Skip s -> Skip (s, f)
438− Stop -> Stop
434+ Stop -> Stop) <$> step1 st
440-- See StreamD.scanlM' for implementing this.
436-- See StreamD.scanlM' for implementing this.
@@ -452,30 +448,29 @@ scanlM' = undefined
452-------------------------------------------------------------------------------
448-------------------------------------------------------------------------------
454{-# INLINE_NORMAL fromStreamD #-}
450{-# INLINE_NORMAL fromStreamD #-}
455−fromStreamD :: Monad m => Unfold m (Stream m a) a
451+fromStreamD :: Applicative m => Unfold m (Stream m a) a
456−fromStreamD = Unfold step return
452+fromStreamD = Unfold step pure
459 {-# INLINE_LATE step #-}
456 {-# INLINE_LATE step #-}
460− step (UnStream step1 state1) = do
457+ step (UnStream step1 state1) =
461− r <- step1 defState state1
462− return $ case r of
458+ (\case
463 Yield x s -> Yield x (Stream step1 s)
459 Yield x s -> Yield x (Stream step1 s)
464 Skip s -> Skip (Stream step1 s)
460 Skip s -> Skip (Stream step1 s)
465− Stop -> Stop
461+ Stop -> Stop) <$> step1 defState state1
467{-# INLINE_NORMAL fromStreamK #-}
463{-# INLINE_NORMAL fromStreamK #-}
468−fromStreamK :: Monad m => Unfold m (K.Stream m a) a
464+fromStreamK :: Applicative m => Unfold m (K.Stream m a) a
469−fromStreamK = Unfold step return
465+fromStreamK = Unfold step pure
473 {-# INLINE_LATE step #-}
469 {-# INLINE_LATE step #-}
474 step stream = do
470 step stream = do
475− r <- K.uncons stream
476− return $ case r of
471+ (\case
477 Just (x, xs) -> Yield x xs
472 Just (x, xs) -> Yield x xs
478− Nothing -> Stop
473+ Nothing -> Stop) <$> K.uncons stream
480-- XXX Using Unfold.fromStreamD seems to be faster (using cross product test
475-- XXX Using Unfold.fromStreamD seems to be faster (using cross product test
481-- case) than using fromStream even if it is implemented using fromStreamD.
476-- case) than using fromStream even if it is implemented using fromStreamD.
@@ -486,7 +481,7 @@ fromStreamK = Unfold step return
486-- /Since: 0.8.0/
481-- /Since: 0.8.0/
488{-# INLINE_NORMAL fromStream #-}
483{-# INLINE_NORMAL fromStream #-}
489−fromStream :: (IsStream t, Monad m) => Unfold m (t m a) a
484+fromStream :: (IsStream t, Applicative m) => Unfold m (t m a) a
490fromStream = lmap IsStream.toStream fromStreamK
485fromStream = lmap IsStream.toStream fromStreamK
492-------------------------------------------------------------------------------
487-------------------------------------------------------------------------------
@@ -497,59 +492,61 @@ fromStream = lmap IsStream.toStream fromStreamK
497-- effect.
492-- effect.
499{-# INLINE nilM #-}
494{-# INLINE nilM #-}
500−nilM :: Monad m => (a -> m c) -> Unfold m a b
495+nilM :: Applicative m => (a -> m c) -> Unfold m a b
501−nilM f = Unfold step return
496+nilM f = Unfold step pure
503 {-# INLINE_LATE step #-}
500 {-# INLINE_LATE step #-}
504− step x = f x >> return Stop
501+ step x = f x $> Stop
506-- | Prepend a monadic single element generator function to an 'Unfold'. The
503-- | Prepend a monadic single element generator function to an 'Unfold'. The
507-- same seed is used in the action as well as the unfold.
504-- same seed is used in the action as well as the unfold.
509-- /Pre-release/
506-- /Pre-release/
510{-# INLINE_NORMAL consM #-}
507{-# INLINE_NORMAL consM #-}
511−consM :: Monad m => (a -> m b) -> Unfold m a b -> Unfold m a b
508+consM :: Applicative m => (a -> m b) -> Unfold m a b -> Unfold m a b
512consM action unf = Unfold step inject
509consM action unf = Unfold step inject
516− inject = return . Left
513+ inject = pure . Left
518 {-# INLINE_LATE step #-}
515 {-# INLINE_LATE step #-}
520− action a >>= \r -> return $ Yield r (Right (D.unfold unf a))
516+ step (Left a) = (`Yield` Right (D.unfold unf a)) <$> action a
521 step (Right (UnStream step1 st)) = do
517 step (Right (UnStream step1 st)) = do
519+ Yield x s -> Yield x (Right (Stream step1 s))
520+ Skip s -> Skip (Right (Stream step1 s))
522− res <- step1 defState st
521+ Stop -> Stop) <$> step1 defState st
524− Yield x s -> return $ Yield x (Right (Stream step1 s))
525− Skip s -> return $ Skip (Right (Stream step1 s))
528-- | Convert a list of pure values to a 'Stream'
523-- | Convert a list of pure values to a 'Stream'
530-- /Since: 0.8.0/
525-- /Since: 0.8.0/
532{-# INLINE_LATE fromList #-}
527{-# INLINE_LATE fromList #-}
533−fromList :: Monad m => Unfold m [a] a
528+fromList :: Applicative m => Unfold m [a] a
534−fromList = Unfold step inject
529+fromList = Unfold step pure
537 {-# INLINE_LATE step #-}
533 {-# INLINE_LATE step #-}
538− step (x:xs) = return $ Yield x xs
534+ step (x:xs) = pure $ Yield x xs
539− step [] = return Stop
535+ step [] = pure Stop
541-- | Convert a list of monadic values to a 'Stream'
537-- | Convert a list of monadic values to a 'Stream'
543-- /Since: 0.8.0/
539-- /Since: 0.8.0/
545{-# INLINE_LATE fromListM #-}
541{-# INLINE_LATE fromListM #-}
546−fromListM :: Monad m => Unfold m [m a] a
542+fromListM :: Applicative m => Unfold m [m a] a
547−fromListM = Unfold step inject
543+fromListM = Unfold step pure
550 {-# INLINE_LATE step #-}
547 {-# INLINE_LATE step #-}
551− step (x:xs) = x >>= \r -> return $ Yield r xs
548+ step (x:xs) = (`Yield` xs) <$> x
552− step [] = return Stop
549+ step [] = pure Stop
554------------------------------------------------------------------------------
551------------------------------------------------------------------------------
555-- Specialized Generation
552-- Specialized Generation
@@ -560,31 +557,31 @@ fromListM = Unfold step inject
560-- /Since: 0.8.0/
557-- /Since: 0.8.0/
562{-# INLINE replicateM #-}
559{-# INLINE replicateM #-}
563−replicateM :: Monad m => Int -> Unfold m (m a) a
560+replicateM :: Applicative m => Int -> Unfold m (m a) a
564replicateM n = Unfold step inject
561replicateM n = Unfold step inject
568− inject x = return (x, n)
565+ inject action = pure (action, n)
570 {-# INLINE_LATE step #-}
567 {-# INLINE_LATE step #-}
571− step (x, i) =
568+ step (action, i) =
572 if i <= 0
569 if i <= 0
573− then return Stop
570+ then pure Stop
574− else do
571+ else (\x -> Yield x (action, i - 1)) <$> action
576− return $ Yield x1 (x, i - 1)
578-- | Generates an infinite stream repeating the seed.
573-- | Generates an infinite stream repeating the seed.
580-- /Since: 0.8.0/
575-- /Since: 0.8.0/
582{-# INLINE repeatM #-}
577{-# INLINE repeatM #-}
583−repeatM :: Monad m => Unfold m (m a) a
578+repeatM :: Applicative m => Unfold m (m a) a
584−repeatM = Unfold step return
579+repeatM = Unfold step pure
586 {-# INLINE_LATE step #-}
583 {-# INLINE_LATE step #-}
587− step x = x >>= \x1 -> return $ Yield x1 x
584+ step action = (`Yield` action) <$> action
589-- | Generates an infinite stream starting with the given seed and applying the
586-- | Generates an infinite stream starting with the given seed and applying the
590-- given function repeatedly.
587-- given function repeatedly.
@@ -592,13 +589,13 @@ repeatM = Unfold step return
592-- /Since: 0.8.0/
589-- /Since: 0.8.0/
594{-# INLINE iterateM #-}
591{-# INLINE iterateM #-}
595−iterateM :: Monad m => (a -> m a) -> Unfold m (m a) a
592+iterateM :: Applicative m => (a -> m a) -> Unfold m (m a) a
596iterateM f = Unfold step id
593iterateM f = Unfold step id
598 {-# INLINE_LATE step #-}
597 {-# INLINE_LATE step #-}
599− step x = do
598+ step x = Yield x <$> f x
603-- | @fromIndicesM gen@ generates an infinite stream of values using @gen@
600-- | @fromIndicesM gen@ generates an infinite stream of values using @gen@
604-- starting from the seed.
601-- starting from the seed.
@@ -610,13 +607,13 @@ iterateM f = Unfold step id
610-- /Pre-release/
607-- /Pre-release/
612{-# INLINE_NORMAL fromIndicesM #-}
609{-# INLINE_NORMAL fromIndicesM #-}
613−fromIndicesM :: Monad m => (Int -> m a) -> Unfold m Int a
610+fromIndicesM :: Applicative m => (Int -> m a) -> Unfold m Int a
614−fromIndicesM gen = Unfold step return
611+fromIndicesM gen = Unfold step pure
616 {-# INLINE_LATE step #-}
615 {-# INLINE_LATE step #-}
619− return $ Yield x (i + 1)
616+ step i = (`Yield` (i + 1)) <$> gen i
621-------------------------------------------------------------------------------
618-------------------------------------------------------------------------------
622-- Filtering
619-- Filtering
@@ -630,20 +627,20 @@ fromIndicesM gen = Unfold step return
630-- /Since: 0.8.0/
627-- /Since: 0.8.0/
632{-# INLINE_NORMAL take #-}
629{-# INLINE_NORMAL take #-}
633−take :: Monad m => Int -> Unfold m a b -> Unfold m a b
630+take :: Applicative m => Int -> Unfold m a b -> Unfold m a b
634take n (Unfold step1 inject1) = Unfold step inject
631take n (Unfold step1 inject1) = Unfold step inject
636− inject x = do
635+ inject x = (, 0) <$> inject1 x
639 {-# INLINE_LATE step #-}
637 {-# INLINE_LATE step #-}
640 step (st, i) | i < n = do
638 step (st, i) | i < n = do
642− return $ case r of
639+ (\case
643 Yield x s -> Yield x (s, i + 1)
640 Yield x s -> Yield x (s, i + 1)
644 Skip s -> Skip (s, i)
641 Skip s -> Skip (s, i)
645− Stop -> Stop
642+ Stop -> Stop) <$> step1 st
646− step (_, _) = return Stop
643+ step (_, _) = pure Stop
648-- | Same as 'filter' but with a monadic predicate.
645-- | Same as 'filter' but with a monadic predicate.
@@ -676,31 +673,25 @@ filter f = filterM (return . f)
676-- /Since: 0.8.0/
673-- /Since: 0.8.0/
678{-# INLINE_NORMAL drop #-}
675{-# INLINE_NORMAL drop #-}
679−drop :: Monad m => Int -> Unfold m a b -> Unfold m a b
676+drop :: Applicative m => Int -> Unfold m a b -> Unfold m a b
680drop n (Unfold step inject) = Unfold step' inject'
677drop n (Unfold step inject) = Unfold step' inject'
684− inject' a = do
681+ inject' a = (, n) <$> inject a
688 {-# INLINE_LATE step' #-}
683 {-# INLINE_LATE step' #-}
689 step' (st, i)
684 step' (st, i)
690 | i > 0 = do
685 | i > 0 = do
693− $ case r of
686+ (\case
694− Yield _ s -> Skip (s, i - 1)
687+ Yield _ s -> Skip (s, i - 1)
695− Skip s -> Skip (s, i)
688+ Skip s -> Skip (s, i)
696− Stop -> Stop
689+ Stop -> Stop) <$> step st
697 | otherwise = do
690 | otherwise = do
692+ Yield x s -> Yield x (s, 0)
693+ Skip s -> Skip (s, 0)
698− r <- step st
694+ Stop -> Stop) <$> step st
701− Yield x s -> Yield x (s, 0)
702− Skip s -> Skip (s, 0)
705-- | @dropWhileM f unf@ drops elements from the stream generated by @unf@ while
696-- | @dropWhileM f unf@ drops elements from the stream generated by @unf@ while
706-- the condition holds true. The condition function @f@ is /monadic/ in nature.
697-- the condition holds true. The condition function @f@ is /monadic/ in nature.
@@ -793,10 +784,10 @@ gbracket_ bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
793 return $ Skip (Left r)
784 return $ Skip (Left r)
794 step (Left st) = do
785 step (Left st) = do
795 res <- estep st
786 res <- estep st
796− case res of
787+ return $ case res of
797− Yield x s -> return $ Yield x (Left s)
788+ Yield x s -> Yield x (Left s)
798− Skip s -> return $ Skip (Left s)
789+ Skip s -> Skip (Left s)
799− Stop -> return Stop
790+ Stop -> Stop
801-- | Run the alloc action @a -> m c@ with async exceptions disabled but keeping
792-- | Run the alloc action @a -> m c@ with async exceptions disabled but keeping
802-- blocking operations interruptible (see 'Control.Exception.mask'). Use the
793-- blocking operations interruptible (see 'Control.Exception.mask'). Use the
@@ -858,10 +849,10 @@ gbracket bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
858 return $ Skip (Left r)
849 return $ Skip (Left r)
859 step (Left st) = do
850 step (Left st) = do
860 res <- estep st
851 res <- estep st
861− case res of
852+ return $ case res of
862− Yield x s -> return $ Yield x (Left s)
853+ Yield x s -> Yield x (Left s)
863− Skip s -> return $ Skip (Left s)
854+ Skip s -> Skip (Left s)
864− Stop -> return Stop
855+ Stop -> Stop
866-- | Run a side effect @a -> m c@ on the input @a@ before unfolding it using
857-- | Run a side effect @a -> m c@ on the input @a@ before unfolding it using
867-- @Unfold m a b@.
858-- @Unfold m a b@.
@@ -964,10 +955,10 @@ onException action (Unfold step1 inject1) = Unfold step inject
964 {-# INLINE_LATE step #-}
955 {-# INLINE_LATE step #-}
965 step (st, v) = do
956 step (st, v) = do
966 res <- step1 st `MC.onException` action v
957 res <- step1 st `MC.onException` action v
967− case res of
958+ return $ case res of
968− Yield x s -> return $ Yield x (s, v)
959+ Yield x s -> Yield x (s, v)
969− Skip s -> return $ Skip (s, v)
960+ Skip s -> Skip (s, v)
970− Stop -> return Stop
961+ Stop -> Stop
972{-# INLINE_NORMAL _finally #-}
963{-# INLINE_NORMAL _finally #-}
973_finally :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b
964_finally :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b