@@ -484,30 +484,36 @@ unfoldrM next = Unfold step return
484484-- Specialized Generation
485485------------------------------------------------------------------------------
486486
487−-- XXX replicateM? Maybe just replicate?
488487-- | Generates a stream replicating the seed @n@ times.
489488--
490489{-# INLINE replicateM #-}
491−replicateM :: Monad m => Int -> Unfold m a a
490+replicateM :: Monad m => Int -> Unfold m (m a) a
492491replicateM n = Unfold step inject
492+
493493 where
494+
494495 inject x = return (x, n)
496+
495497 {-# INLINE_LATE step #-}
496− step (x, i) = return $
497− if i <= 0
498− then Stop
499− else Yield x (x, i - 1)
498+ step (x, i) = do
499+ x1 <- x
500+ return
501+ $ if i <= 0
502+ then Stop
503+ else Yield x1 (x, i - 1)
500504
501−-- XXX repeatM? Maybe just repeat?
502505-- | Generates an infinite stream repeating the seed.
503506--
504507{-# INLINE repeatM #-}
505−repeatM :: Monad m => Unfold m a a
508+repeatM :: Monad m => Unfold m (m a) a
506509repeatM = Unfold step return
507510 where
508511 {-# INLINE_LATE step #-}
509− step x = return $ Yield x x
512+ step x = x >>= \x1 -> return $ Yield x1 x
510513
514+-- | Generates an infinite stream starting with the given seed and applying the
515+-- given function repeteadly.
516+--
511517{-# INLINE iterateM #-}
512518iterateM :: Monad m => (a -> m a) -> Unfold m a a
513519iterateM f = Unfold step f
@@ -517,6 +523,9 @@ iterateM f = Unfold step f
517523 fx <- f x
518524 return $ Yield fx fx
519525
526+-- | @fromIndicesM gen@ generates an infinite stream of values using @gen@
527+-- starting from the seed.
528+--
520529{-# INLINE_NORMAL fromIndicesM #-}
521530fromIndicesM :: Monad m => (Int -> m a) -> Unfold m Int a
522531fromIndicesM gen = Unfold step return
@@ -585,52 +594,59 @@ filter f = filterM (return . f)
585594{-# INLINE_NORMAL drop #-}
586595drop :: Monad m => Int -> Unfold m a b -> Unfold m a b
587596drop n (Unfold step inject) = Unfold step' inject'
588− where
597+
598+ where
599+
589600 inject' a = do
590601 b <- inject a
591602 return (b, n)
603+
592604 {-# INLINE_LATE step' #-}
593605 step' (st, i)
594606 | i > 0 = do
595607 r <- step st
596− return $
597− case r of
598− Yield _ s -> Skip (s, i - 1)
599− Skip s -> Skip (s, i)
600− Stop -> Stop
608+ return
609+ $ case r of
610+ Yield _ s -> Skip (s, i - 1)
611+ Skip s -> Skip (s, i)
612+ Stop -> Stop
601613 | otherwise = do
602614 r <- step st
603− return $
604− case r of
605− Yield x s -> Yield x (s, 0)
606− Skip s -> Skip (s, 0)
607− Stop -> Stop
615+ return
616+ $ case r of
617+ Yield x s -> Yield x (s, 0)
618+ Skip s -> Skip (s, 0)
619+ Stop -> Stop
608620
609621{-# INLINE_NORMAL dropWhileM #-}
610622dropWhileM :: Monad m => (b -> m Bool) -> Unfold m a b -> Unfold m a b
611623dropWhileM f (Unfold step inject) = Unfold step' inject'
612− where
624+
625+ where
626+
613627 inject' a = do
614628 b <- inject a
615629 return $ Left b
630+
616631 {-# INLINE_LATE step' #-}
617632 step' (Left st) = do
618633 r <- step st
619634 case r of
620635 Yield x s -> do
621636 b <- f x
622− if b
623− then return $ Skip (Left s)
624− else return $ Yield x (Right s)
637+ return
638+ $ if b
639+ then Skip (Left s)
640+ else Yield x (Right s)
625641 Skip s -> return $ Skip (Left s)
626642 Stop -> return Stop
627−
628643 step' (Right st) = do
629644 r <- step st
630− case r of
631− Yield x s -> return $ Yield x (Right s)
632− Skip s -> return $ Skip (Right s)
633− Stop -> return Stop
645+ return
646+ $ case r of
647+ Yield x s -> Yield x (Right s)
648+ Skip s -> Skip (Right s)
649+ Stop -> Stop
634650
635651{-# INLINE dropWhile #-}
636652dropWhile :: Monad m => (b -> Bool) -> Unfold m a b -> Unfold m a b