@@ -417,13 +417,13 @@ concatMapM f (Unfold step1 inject1) = Unfold step inject
418-- /Internal/
418-- /Internal/
419{-# INLINE_NORMAL before #-}
419{-# INLINE_NORMAL before #-}
420−before :: Monad m => m c -> Unfold m a b -> Unfold m a b
420+before :: Monad m => (a -> m c) -> Unfold m a b -> Unfold m a b
421before action (Unfold step1 inject1) = Unfold step inject
421before action (Unfold step1 inject1) = Unfold step inject
425 inject x = do
425 inject x = do
426− _ <- action
426+ _ <- action x
427 st <- inject1 x
427 st <- inject1 x
428 return st
428 return st
@@ -439,34 +439,42 @@ before action (Unfold step1 inject1) = Unfold step inject
440-- /Internal/
440-- /Internal/
441{-# INLINE_NORMAL after #-}
441{-# INLINE_NORMAL after #-}
442−after :: Monad m => m c -> Unfold m a b -> Unfold m a b
442+after :: Monad m => (a -> m c) -> Unfold m a b -> Unfold m a b
443−after action (Unfold step1 inject1) = Unfold step inject1
443+after action (Unfold step1 inject1) = Unfold step inject
447 {-# INLINE_LATE step #-}
451 {-# INLINE_LATE step #-}
448− step st = do
452+ step (st, v) = do
449 res <- step1 st
453 res <- step1 st
450 case res of
454 case res of
451− Yield x s -> return $ Yield x s
455+ Yield x s -> return $ Yield x (s, v)
452− Skip s -> return $ Skip s
456+ Skip s -> return $ Skip (s, v)
453− Stop -> action >> return Stop
457+ Stop -> action v >> return Stop
455-- | Run a side effect whenever the unfold aborts due to an exception.
459-- | Run a side effect whenever the unfold aborts due to an exception.
457-- /Internal/
461-- /Internal/
458{-# INLINE_NORMAL onException #-}
462{-# INLINE_NORMAL onException #-}
459−onException :: MonadCatch m => m c -> Unfold m a b -> Unfold m a b
463+onException :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b
460−onException action (Unfold step1 inject1) = Unfold step inject1
464+onException action (Unfold step1 inject1) = Unfold step inject
464 {-# INLINE_LATE step #-}
472 {-# INLINE_LATE step #-}
465− step st = do
473+ step (st, v) = do
466− res <- step1 st `MC.onException` action
474+ res <- step1 st `MC.onException` action v
467 case res of
475 case res of
468− Yield x s -> return $ Yield x s
476+ Yield x s -> return $ Yield x (s, v)
469− Skip s -> return $ Skip s
477+ Skip s -> return $ Skip (s, v)
470 Stop -> return Stop
478 Stop -> return Stop
472-- | Run a side effect whenever the unfold stops normally or aborts due to an
480-- | Run a side effect whenever the unfold stops normally or aborts due to an
@@ -474,46 +482,47 @@ onException action (Unfold step1 inject1) = Unfold step inject1
475-- /Internal/
483-- /Internal/
476{-# INLINE_NORMAL finally #-}
484{-# INLINE_NORMAL finally #-}
477−finally :: MonadCatch m => m c -> Unfold m a b -> Unfold m a b
485+finally :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b
478−finally action (Unfold step1 inject1) = Unfold step inject1
486+finally action (Unfold step1 inject1) = Unfold step inject
482 {-# INLINE_LATE step #-}
494 {-# INLINE_LATE step #-}
483− step st = do
495+ step (st, v) = do
484− res <- step1 st `MC.onException` action
496+ res <- step1 st `MC.onException` action v
485 case res of
497 case res of
486− Yield x s -> return $ Yield x s
498+ Yield x s -> return $ Yield x (s, v)
487− Skip s -> return $ Skip s
499+ Skip s -> return $ Skip (s, v)
488− Stop -> action >> return Stop
500+ Stop -> action v >> return Stop
490−-- | @bracket before after between@ runs the @before@ action and then runs the
502+-- | @bracket before after between@ runs the @before@ action and then unfolds
491−-- @between@ function using the output of @before@ as its argument. When the
503+-- its output using the @between@ unfold. When the @between@ unfold is done or
492−-- @between@ unfold is done or if an exception occurs while running @between@
493−-- the @after@ action is run with the output of @before@ as argument.
504+-- if an exception occurs then the @after@ action is run with the output of
505+-- @before@ as argument.
495-- /Internal/
507-- /Internal/
496{-# INLINE_NORMAL bracket #-}
508{-# INLINE_NORMAL bracket #-}
497bracket :: MonadCatch m
509bracket :: MonadCatch m
498− => m c -> (c -> m d) -> (c -> Unfold m a b) -> Unfold m a b
510+ => (a -> m c) -> (c -> m d) -> Unfold m c b -> Unfold m a b
499−bracket bef aft bet = Unfold step inject
511+bracket bef aft (Unfold step1 inject1) = Unfold step inject
503 inject x = do
515 inject x = do
504− r <- bef
516+ r <- bef x
505− go (bet r) r
517+ s <- inject1 r
507− go (Unfold step2 inject2) r = do
509− return (Stream (\_ -> step2) st, r)
518+ return (s, r)
511 {-# INLINE_LATE step #-}
520 {-# INLINE_LATE step #-}
512− step (UnStream step2 state, v) = do
521+ step (st, v) = do
513− res <- step2 defState state `MC.onException` aft v
522+ res <- step1 st `MC.onException` aft v
514 case res of
523 case res of
515− Yield x s -> return $ Yield x (Stream step2 s, v)
524+ Yield x s -> return $ Yield x (s, v)
516− Skip s -> return $ Skip (Stream step2 s, v)
525+ Skip s -> return $ Skip (s, v)
517 Stop -> aft v >> return Stop
526 Stop -> aft v >> return Stop
519-- | When unfolding an unfold if an exception occurs, unfold aborts
528-- | When unfolding an unfold if an exception occurs, unfold aborts