@@ -3616,23 +3616,27 @@ scanlx' fstep begin done s =
36163616------------------------------------------------------------------------------
36173617
36183618{-# INLINE_NORMAL postscanlM' #-}
3619−postscanlM' :: Monad m => (b -> a -> m b) -> b -> Stream m a -> Stream m b
3619+postscanlM' :: Monad m => (b -> a -> m b) -> m b -> Stream m a -> Stream m b
36203620postscanlM' fstep begin (Stream step state) =
3621− begin `seq` Stream step' (state, begin)
3621+ Stream step' Nothing
36223622 where
36233623 {-# INLINE_LATE step' #-}
3624− step' gst (st, acc) = acc `seq` do
3624+ step' _ Nothing = do
3625+ !x <- begin
3626+ return $ Skip (Just (state, x))
3627+
3628+ step' gst (Just (st, acc)) = do
36253629 r <- step (adaptState gst) st
36263630 case r of
36273631 Yield x s -> do
3628− y <- fstep acc x
3629− y `seq` return (Yield y (s, y))
3630− Skip s -> return $ Skip (s, acc)
3632+ !y <- fstep acc x
3633+ return $ Yield y (Just (s, y))
3634+ Skip s -> return $ Skip (Just (s, acc))
36313635 Stop -> return Stop
36323636
36333637{-# INLINE_NORMAL postscanl' #-}
36343638postscanl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
3635−postscanl' f = postscanlM' (\a b -> return (f a b))
3639+postscanl' f seed = postscanlM' (\a b -> return (f a b)) (return seed)
36363640
36373641-- We can possibly have the "done" function as a Maybe to provide an option to
36383642-- emit or not emit the accumulator when the stream stops.
@@ -3660,26 +3664,43 @@ postscanlMAfter' fstep initial done (Stream step1 state1) = do
36603664 step _ Nothing = return Stop
36613665
36623666{-# INLINE_NORMAL postscanlM #-}
3663−postscanlM :: Monad m => (b -> a -> m b) -> b -> Stream m a -> Stream m b
3664−postscanlM fstep begin (Stream step state) = Stream step' (state, begin)
3667+postscanlM :: Monad m => (b -> a -> m b) -> m b -> Stream m a -> Stream m b
3668+postscanlM fstep begin (Stream step state) = Stream step' Nothing
36653669 where
36663670 {-# INLINE_LATE step' #-}
3667− step' gst (st, acc) = do
3671+ step' _ Nothing = do
3672+ r <- begin
3673+ return $ Skip (Just (state, r))
3674+
3675+ step' gst (Just (st, acc)) = do
36683676 r <- step (adaptState gst) st
36693677 case r of
36703678 Yield x s -> do
36713679 y <- fstep acc x
3672− return (Yield y (s, y))
3673− Skip s -> return $ Skip (s, acc)
3680+ return (Yield y (Just (s, y)))
3681+ Skip s -> return $ Skip (Just (s, acc))
36743682 Stop -> return Stop
36753683
36763684{-# INLINE_NORMAL postscanl #-}
36773685postscanl :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
3678−postscanl f = postscanlM (\a b -> return (f a b))
3686+postscanl f seed = postscanlM (\a b -> return (f a b)) (return seed)
36793687
36803688{-# INLINE_NORMAL scanlM' #-}
3681−scanlM' :: Monad m => (b -> a -> m b) -> b -> Stream m a -> Stream m b
3682−scanlM' fstep begin s = begin `seq` (begin `cons` postscanlM' fstep begin s)
3689+scanlM' :: Monad m => (b -> a -> m b) -> m b -> Stream m a -> Stream m b
3690+scanlM' fstep begin (Stream step state) = Stream step' Nothing
3691+ where
3692+ {-# INLINE_LATE step' #-}
3693+ step' _ Nothing = do
3694+ !x <- begin
3695+ return $ Yield x (Just (state, x))
3696+ step' gst (Just (st, acc)) = do
3697+ r <- step (adaptState gst) st
3698+ case r of
3699+ Yield x s -> do
3700+ !y <- fstep acc x
3701+ return $ Yield y (Just (s, y))
3702+ Skip s -> return $ Skip (Just (s, acc))
3703+ Stop -> return Stop
36833704
36843705{-# INLINE scanlMAfter' #-}
36853706scanlMAfter' :: Monad m
@@ -3690,15 +3711,28 @@ scanlMAfter' fstep initial done s =
36903711
36913712{-# INLINE scanl' #-}
36923713scanl' :: Monad m => (b -> a -> b) -> b -> Stream m a -> Stream m b
3693−scanl' f = scanlM' (\a b -> return (f a b))
3714+scanl' f seed = scanlM' (\a b -> return (f a b)) (return seed)
36943715
36953716{-# INLINE_NORMAL scanlM #-}
3696−scanlM :: Monad m => (b -> a -> m b) -> b -> Stream m a -> Stream m b
3697−scanlM fstep begin s = begin `cons` postscanlM fstep begin s
3717+scanlM :: Monad m => (b -> a -> m b) -> m b -> Stream m a -> Stream m b
3718+scanlM fstep begin (Stream step state) = Stream step' Nothing
3719+ where
3720+ {-# INLINE_LATE step' #-}
3721+ step' _ Nothing = do
3722+ x <- begin
3723+ return $ Yield x (Just (state, x))
3724+ step' gst (Just (st, acc)) = do
3725+ r <- step (adaptState gst) st
3726+ case r of
3727+ Yield x s -> do
3728+ y <- fstep acc x
3729+ return $ Yield y (Just (s, y))
3730+ Skip s -> return $ Skip (Just (s, acc))
3731+ Stop -> return $ Stop
36983732
36993733{-# INLINE scanl #-}
37003734scanl :: Monad m => (b -> a -> b) -> b -> Stream m a -> Stream m b
3701−scanl f = scanlM (\a b -> return (f a b))
3735+scanl f seed = scanlM (\a b -> return (f a b)) (return seed)
37023736
37033737{-# INLINE_NORMAL scanl1M #-}
37043738scanl1M :: Monad m => (a -> a -> m a) -> Stream m a -> Stream m a