@@ -250,6 +250,7 @@ import Control.Exception (Exception, mask_)
250250import Control.Monad.Catch (MonadCatch)
251251import Control.Monad.IO.Class (MonadIO(..))
252252import Control.Monad.Trans.Control (MonadBaseControl, liftBaseOp_)
253+import Data.Functor (($>))
253254import GHC.Types (SPEC(..))
254255import Streamly.Internal.Control.Concurrent (MonadAsync)
255256import Streamly.Internal.Data.Fold.Type (Fold(..))
@@ -417,25 +418,20 @@ mapMWithInput f (Unfold ustep uinject) = Unfold step inject
417418--
418419-- /Internal/
419420{-# 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)
421422either (Unfold step1 inject1) = Unfold step inject
422423
423424 where
424425
425− inject (Left a) = do
426− r <- inject1 a
427− return (r, Left)
428− inject (Right a) = do
429− r <- inject1 a
430− return (r, Right)
426+ inject (Left a) = (, Left) <$> inject1 a
427+ inject (Right a) = (, Right) <$> inject1 a
431428
432429 {-# INLINE_LATE step #-}
433430 step (st, f) = do
434− r <- step1 st
435− return $ case r of
431+ (\case
436432 Yield x s -> Yield (f x) (s, f)
437433 Skip s -> Skip (s, f)
438− Stop -> Stop
434+ Stop -> Stop) <$> step1 st
439435
440436-- See StreamD.scanlM' for implementing this.
441437--
@@ -452,30 +448,29 @@ scanlM' = undefined
452448-------------------------------------------------------------------------------
453449
454450{-# INLINE_NORMAL fromStreamD #-}
455−fromStreamD :: Monad m => Unfold m (Stream m a) a
456−fromStreamD = Unfold step return
451+fromStreamD :: Applicative m => Unfold m (Stream m a) a
452+fromStreamD = Unfold step pure
453+
457454 where
458455
459456 {-# INLINE_LATE step #-}
460− step (UnStream step1 state1) = do
461− r <- step1 defState state1
462− return $ case r of
457+ step (UnStream step1 state1) =
458+ (\case
463459 Yield x s -> Yield x (Stream step1 s)
464460 Skip s -> Skip (Stream step1 s)
465− Stop -> Stop
461+ Stop -> Stop) <$> step1 defState state1
466462
467463{-# INLINE_NORMAL fromStreamK #-}
468−fromStreamK :: Monad m => Unfold m (K.Stream m a) a
469−fromStreamK = Unfold step return
464+fromStreamK :: Applicative m => Unfold m (K.Stream m a) a
465+fromStreamK = Unfold step pure
470466
471467 where
472468
473469 {-# INLINE_LATE step #-}
474470 step stream = do
475− r <- K.uncons stream
476− return $ case r of
471+ (\case
477472 Just (x, xs) -> Yield x xs
478− Nothing -> Stop
473+ Nothing -> Stop) <$> K.uncons stream
479474
480475-- XXX Using Unfold.fromStreamD seems to be faster (using cross product test
481476-- case) than using fromStream even if it is implemented using fromStreamD.
@@ -486,7 +481,7 @@ fromStreamK = Unfold step return
486481-- /Since: 0.8.0/
487482--
488483{-# 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
490485fromStream = lmap IsStream.toStream fromStreamK
491486
492487-------------------------------------------------------------------------------
@@ -497,59 +492,61 @@ fromStream = lmap IsStream.toStream fromStreamK
497492-- effect.
498493--
499494{-# INLINE nilM #-}
500−nilM :: Monad m => (a -> m c) -> Unfold m a b
501−nilM f = Unfold step return
495+nilM :: Applicative m => (a -> m c) -> Unfold m a b
496+nilM f = Unfold step pure
497+
502498 where
499+
503500 {-# INLINE_LATE step #-}
504− step x = f x >> return Stop
501+ step x = f x $> Stop
505502
506503-- | Prepend a monadic single element generator function to an 'Unfold'. The
507504-- same seed is used in the action as well as the unfold.
508505--
509506-- /Pre-release/
510507{-# 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
512509consM action unf = Unfold step inject
513510
514511 where
515512
516− inject = return . Left
513+ inject = pure . Left
517514
518515 {-# INLINE_LATE step #-}
519− step (Left a) =
520− action a >>= \r -> return $ Yield r (Right (D.unfold unf a))
516+ step (Left a) = (`Yield` Right (D.unfold unf a)) <$> action a
521517 step (Right (UnStream step1 st)) = do
522− res <- step1 defState st
523− case res of
524− Yield x s -> return $ Yield x (Right (Stream step1 s))
525− Skip s -> return $ Skip (Right (Stream step1 s))
526− Stop -> return Stop
518+ (\case
519+ Yield x s -> Yield x (Right (Stream step1 s))
520+ Skip s -> Skip (Right (Stream step1 s))
521+ Stop -> Stop) <$> step1 defState st
527522
528523-- | Convert a list of pure values to a 'Stream'
529524--
530525-- /Since: 0.8.0/
531526--
532527{-# INLINE_LATE fromList #-}
533−fromList :: Monad m => Unfold m [a] a
534−fromList = Unfold step inject
535− where
536− inject = return
528+fromList :: Applicative m => Unfold m [a] a
529+fromList = Unfold step pure
530+
531+ where
532+
537533 {-# INLINE_LATE step #-}
538− step (x:xs) = return $ Yield x xs
539− step [] = return Stop
534+ step (x:xs) = pure $ Yield x xs
535+ step [] = pure Stop
540536
541537-- | Convert a list of monadic values to a 'Stream'
542538--
543539-- /Since: 0.8.0/
544540--
545541{-# INLINE_LATE fromListM #-}
546−fromListM :: Monad m => Unfold m [m a] a
547−fromListM = Unfold step inject
548− where
549− inject = return
542+fromListM :: Applicative m => Unfold m [m a] a
543+fromListM = Unfold step pure
544+
545+ where
546+
550547 {-# INLINE_LATE step #-}
551− step (x:xs) = x >>= \r -> return $ Yield r xs
552− step [] = return Stop
548+ step (x:xs) = (`Yield` xs) <$> x
549+ step [] = pure Stop
553550
554551------------------------------------------------------------------------------
555552-- Specialized Generation
@@ -560,31 +557,31 @@ fromListM = Unfold step inject
560557-- /Since: 0.8.0/
561558--
562559{-# INLINE replicateM #-}
563−replicateM :: Monad m => Int -> Unfold m (m a) a
560+replicateM :: Applicative m => Int -> Unfold m (m a) a
564561replicateM n = Unfold step inject
565562
566563 where
567564
568− inject x = return (x, n)
565+ inject action = pure (action, n)
569566
570567 {-# INLINE_LATE step #-}
571− step (x, i) =
568+ step (action, i) =
572569 if i <= 0
573− then return Stop
574− else do
575− x1 <- x
576− return $ Yield x1 (x, i - 1)
570+ then pure Stop
571+ else (\x -> Yield x (action, i - 1)) <$> action
577572
578573-- | Generates an infinite stream repeating the seed.
579574--
580575-- /Since: 0.8.0/
581576--
582577{-# INLINE repeatM #-}
583−repeatM :: Monad m => Unfold m (m a) a
584−repeatM = Unfold step return
578+repeatM :: Applicative m => Unfold m (m a) a
579+repeatM = Unfold step pure
580+
585581 where
582+
586583 {-# INLINE_LATE step #-}
587− step x = x >>= \x1 -> return $ Yield x1 x
584+ step action = (`Yield` action) <$> action
588585
589586-- | Generates an infinite stream starting with the given seed and applying the
590587-- given function repeatedly.
@@ -592,13 +589,13 @@ repeatM = Unfold step return
592589-- /Since: 0.8.0/
593590--
594591{-# 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
596593iterateM f = Unfold step id
594+
597595 where
596+
598597 {-# INLINE_LATE step #-}
599− step x = do
600− fx <- f x
601− return $ Yield x fx
598+ step x = Yield x <$> f x
602599
603600-- | @fromIndicesM gen@ generates an infinite stream of values using @gen@
604601-- starting from the seed.
@@ -610,13 +607,13 @@ iterateM f = Unfold step id
610607-- /Pre-release/
611608--
612609{-# INLINE_NORMAL fromIndicesM #-}
613−fromIndicesM :: Monad m => (Int -> m a) -> Unfold m Int a
614−fromIndicesM gen = Unfold step return
615− where
610+fromIndicesM :: Applicative m => (Int -> m a) -> Unfold m Int a
611+fromIndicesM gen = Unfold step pure
612+
613+ where
614+
616615 {-# INLINE_LATE step #-}
617− step i = do
618− x <- gen i
619− return $ Yield x (i + 1)
616+ step i = (`Yield` (i + 1)) <$> gen i
620617
621618-------------------------------------------------------------------------------
622619-- Filtering
@@ -630,20 +627,20 @@ fromIndicesM gen = Unfold step return
630627-- /Since: 0.8.0/
631628--
632629{-# 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
634631take n (Unfold step1 inject1) = Unfold step inject
635− where
636− inject x = do
637− s <- inject1 x
638− return (s, 0)
632+
633+ where
634+
635+ inject x = (, 0) <$> inject1 x
636+
639637 {-# INLINE_LATE step #-}
640638 step (st, i) | i < n = do
641− r <- step1 st
642− return $ case r of
639+ (\case
643640 Yield x s -> Yield x (s, i + 1)
644641 Skip s -> Skip (s, i)
645− Stop -> Stop
646− step (_, _) = return Stop
642+ Stop -> Stop) <$> step1 st
643+ step (_, _) = pure Stop
647644
648645-- | Same as 'filter' but with a monadic predicate.
649646--
@@ -676,31 +673,25 @@ filter f = filterM (return . f)
676673-- /Since: 0.8.0/
677674--
678675{-# 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
680677drop n (Unfold step inject) = Unfold step' inject'
681678
682679 where
683680
684− inject' a = do
685− b <- inject a
686− return (b, n)
681+ inject' a = (, n) <$> inject a
687682
688683 {-# INLINE_LATE step' #-}
689684 step' (st, i)
690685 | i > 0 = do
691− r <- step st
692− return
693− $ case r of
694− Yield _ s -> Skip (s, i - 1)
695− Skip s -> Skip (s, i)
696− Stop -> Stop
686+ (\case
687+ Yield _ s -> Skip (s, i - 1)
688+ Skip s -> Skip (s, i)
689+ Stop -> Stop) <$> step st
697690 | otherwise = do
698− r <- step st
699− return
700− $ case r of
701− Yield x s -> Yield x (s, 0)
702− Skip s -> Skip (s, 0)
703− Stop -> Stop
691+ (\case
692+ Yield x s -> Yield x (s, 0)
693+ Skip s -> Skip (s, 0)
694+ Stop -> Stop) <$> step st
704695
705696-- | @dropWhileM f unf@ drops elements from the stream generated by @unf@ while
706697-- 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) =
793784 return $ Skip (Left r)
794785 step (Left st) = do
795786 res <- estep st
796− case res of
797− Yield x s -> return $ Yield x (Left s)
798− Skip s -> return $ Skip (Left s)
799− Stop -> return Stop
787+ return $ case res of
788+ Yield x s -> Yield x (Left s)
789+ Skip s -> Skip (Left s)
790+ Stop -> Stop
800791
801792-- | Run the alloc action @a -> m c@ with async exceptions disabled but keeping
802793-- blocking operations interruptible (see 'Control.Exception.mask'). Use the
@@ -858,10 +849,10 @@ gbracket bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
858849 return $ Skip (Left r)
859850 step (Left st) = do
860851 res <- estep st
861− case res of
862− Yield x s -> return $ Yield x (Left s)
863− Skip s -> return $ Skip (Left s)
864− Stop -> return Stop
852+ return $ case res of
853+ Yield x s -> Yield x (Left s)
854+ Skip s -> Skip (Left s)
855+ Stop -> Stop
865856
866857-- | Run a side effect @a -> m c@ on the input @a@ before unfolding it using
867858-- @Unfold m a b@.
@@ -964,10 +955,10 @@ onException action (Unfold step1 inject1) = Unfold step inject
964955 {-# INLINE_LATE step #-}
965956 step (st, v) = do
966957 res <- step1 st `MC.onException` action v
967− case res of
968− Yield x s -> return $ Yield x (s, v)
969− Skip s -> return $ Skip (s, v)
970− Stop -> return Stop
958+ return $ case res of
959+ Yield x s -> Yield x (s, v)
960+ Skip s -> Skip (s, v)
961+ Stop -> Stop
971962
972963{-# INLINE_NORMAL _finally #-}
973964_finally :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b