@@ -484,30 +484,36 @@ unfoldrM next = Unfold step return
484-- Specialized Generation
484-- Specialized Generation
485------------------------------------------------------------------------------
485------------------------------------------------------------------------------
487−-- XXX replicateM? Maybe just replicate?
488-- | Generates a stream replicating the seed @n@ times.
487-- | Generates a stream replicating the seed @n@ times.
490{-# INLINE replicateM #-}
489{-# INLINE replicateM #-}
491−replicateM :: Monad m => Int -> Unfold m a a
490+replicateM :: Monad m => Int -> Unfold m (m a) a
492replicateM n = Unfold step inject
491replicateM n = Unfold step inject
494 inject x = return (x, n)
495 inject x = return (x, n)
495 {-# INLINE_LATE step #-}
497 {-# INLINE_LATE step #-}
496− step (x, i) = return $
500+ return
497− if i <= 0
501+ $ if i <= 0
498− then Stop
502+ then Stop
499− else Yield x (x, i - 1)
503+ else Yield x1 (x, i - 1)
501−-- XXX repeatM? Maybe just repeat?
502-- | Generates an infinite stream repeating the seed.
505-- | Generates an infinite stream repeating the seed.
504{-# INLINE repeatM #-}
507{-# INLINE repeatM #-}
505−repeatM :: Monad m => Unfold m a a
508+repeatM :: Monad m => Unfold m (m a) a
506repeatM = Unfold step return
509repeatM = Unfold step return
508 {-# INLINE_LATE step #-}
511 {-# INLINE_LATE step #-}
509− step x = return $ Yield x x
512+ step x = x >>= \x1 -> return $ Yield x1 x
514+-- | Generates an infinite stream starting with the given seed and applying the
515+-- given function repeteadly.
511{-# INLINE iterateM #-}
517{-# INLINE iterateM #-}
512iterateM :: Monad m => (a -> m a) -> Unfold m a a
518iterateM :: Monad m => (a -> m a) -> Unfold m a a
513iterateM f = Unfold step f
519iterateM f = Unfold step f
@@ -517,6 +523,9 @@ iterateM f = Unfold step f
517 fx <- f x
523 fx <- f x
518 return $ Yield fx fx
524 return $ Yield fx fx
526+-- | @fromIndicesM gen@ generates an infinite stream of values using @gen@
527+-- starting from the seed.
520{-# INLINE_NORMAL fromIndicesM #-}
529{-# INLINE_NORMAL fromIndicesM #-}
521fromIndicesM :: Monad m => (Int -> m a) -> Unfold m Int a
530fromIndicesM :: Monad m => (Int -> m a) -> Unfold m Int a
522fromIndicesM gen = Unfold step return
531fromIndicesM gen = Unfold step return
@@ -585,52 +594,59 @@ filter f = filterM (return . f)
585{-# INLINE_NORMAL drop #-}
594{-# INLINE_NORMAL drop #-}
586drop :: Monad m => Int -> Unfold m a b -> Unfold m a b
595drop :: Monad m => Int -> Unfold m a b -> Unfold m a b
587drop n (Unfold step inject) = Unfold step' inject'
596drop n (Unfold step inject) = Unfold step' inject'
589 inject' a = do
600 inject' a = do
590 b <- inject a
601 b <- inject a
591 return (b, n)
602 return (b, n)
592 {-# INLINE_LATE step' #-}
604 {-# INLINE_LATE step' #-}
593 step' (st, i)
605 step' (st, i)
594 | i > 0 = do
606 | i > 0 = do
595 r <- step st
607 r <- step st
597− case r of
609+ $ case r of
598− Yield _ s -> Skip (s, i - 1)
610+ Yield _ s -> Skip (s, i - 1)
599− Skip s -> Skip (s, i)
611+ Skip s -> Skip (s, i)
600− Stop -> Stop
612+ Stop -> Stop
601 | otherwise = do
613 | otherwise = do
602 r <- step st
614 r <- step st
604− case r of
616+ $ case r of
605− Yield x s -> Yield x (s, 0)
617+ Yield x s -> Yield x (s, 0)
606− Skip s -> Skip (s, 0)
618+ Skip s -> Skip (s, 0)
607− Stop -> Stop
619+ Stop -> Stop
609{-# INLINE_NORMAL dropWhileM #-}
621{-# INLINE_NORMAL dropWhileM #-}
610dropWhileM :: Monad m => (b -> m Bool) -> Unfold m a b -> Unfold m a b
622dropWhileM :: Monad m => (b -> m Bool) -> Unfold m a b -> Unfold m a b
611dropWhileM f (Unfold step inject) = Unfold step' inject'
623dropWhileM f (Unfold step inject) = Unfold step' inject'
613 inject' a = do
627 inject' a = do
614 b <- inject a
628 b <- inject a
615 return $ Left b
629 return $ Left b
616 {-# INLINE_LATE step' #-}
631 {-# INLINE_LATE step' #-}
617 step' (Left st) = do
632 step' (Left st) = do
618 r <- step st
633 r <- step st
619 case r of
634 case r of
620 Yield x s -> do
635 Yield x s -> do
623− then return $ Skip (Left s)
639+ then Skip (Left s)
624− else return $ Yield x (Right s)
640+ else Yield x (Right s)
625 Skip s -> return $ Skip (Left s)
641 Skip s -> return $ Skip (Left s)
626 Stop -> return Stop
642 Stop -> return Stop
628 step' (Right st) = do
643 step' (Right st) = do
629 r <- step st
644 r <- step st
630− case r of
646+ $ case r of
631− Yield x s -> return $ Yield x (Right s)
647+ Yield x s -> Yield x (Right s)
632− Skip s -> return $ Skip (Right s)
648+ Skip s -> Skip (Right s)
633− Stop -> return Stop
649+ Stop -> Stop
635{-# INLINE dropWhile #-}
651{-# INLINE dropWhile #-}
636dropWhile :: Monad m => (b -> Bool) -> Unfold m a b -> Unfold m a b
652dropWhile :: Monad m => (b -> Bool) -> Unfold m a b -> Unfold m a b