@@ -522,7 +522,7 @@ repeatM = Unfold step return
522522 step x = x >>= \x1 -> return $ Yield x1 x
523523
524524-- | Generates an infinite stream starting with the given seed and applying the
525−-- given function repeteadly.
525+-- given function repeatedly.
526526--
527527-- /Internal/
528528--
@@ -1027,7 +1027,7 @@ apDiscardSnd (Unfold _step1 _inject1) (Unfold _step2 _inject2) = undefined
10271027data ConcatMapState s1 s2 = ConcatMapOuter s1 | ConcatMapInner s1 s2
10281028
10291029-- | Map an unfold generating action to each element of an unfold and
1030−-- flattern the results into a single stream.
1030+-- flatten the results into a single stream.
10311031--
10321032{-# INLINE_NORMAL concatMapM #-}
10331033concatMapM :: Monad m
@@ -1061,9 +1061,14 @@ concatMapM f (Unfold step1 inject1) = Unfold step inject
10611061-- Exceptions
10621062------------------------------------------------------------------------------
10631063
1064−-- | The most general bracketing and exception combinator. All other
1065−-- combinators can be expressed in terms of this combinator. This can also be
1066−-- used for cases which are not covered by the standard combinators.
1064+-- | Like 'gbracket' but with following differences:
1065+--
1066+-- * alloc action @a -> m c@ runs with async exceptions enabled
1067+-- * cleanup action @c -> m d@ won't run if the stream is garbage collected
1068+-- after partial evaluation.
1069+-- * does not require a 'MonadAsync' constraint.
1070+--
1071+-- /Inhibits stream fusion/
10671072--
10681073-- /Internal/
10691074--
@@ -1094,6 +1099,7 @@ gbracket_ bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
10941099 Yield x s -> return $ Yield x (Right (s, v))
10951100 Skip s -> return $ Skip (Right (s, v))
10961101 Stop -> aft v >> return Stop
1102+ -- XXX Do not handle async exceptions, just rethrow them.
10971103 Left e -> do
10981104 r <- einject (v, e)
10991105 return $ Skip (Left r)
@@ -1104,12 +1110,22 @@ gbracket_ bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
11041110 Skip s -> return $ Skip (Left s)
11051111 Stop -> return Stop
11061112
1107−-- | The most general bracketing and exception combinator. All other
1108−-- combinators can be expressed in terms of this combinator. This can also be
1109−-- used for cases which are not covered by the standard combinators.
1113+-- | Run the alloc action @a -> m c@ with async exceptions disabled but keeping
1114+-- blocking operations interruptible (see 'Control.Exception.mask'). Use the
1115+-- output @c@ as input to @Unfold m c b@ to generate an output stream. When
1116+-- unfolding use the supplied @try@ operation @forall s. m s -> m (Either e s)@
1117+-- to catch synchronous exceptions. If an exception occurs run the exception
1118+-- handling unfold @Unfold m (c, e) b@.
11101119--
1111−-- /Internal/
1120+-- The cleanup action @c -> m d@, runs whenever the stream ends normally, due
1121+-- to a sync or async exception or if it gets garbage collected after a partial
1122+-- lazy evaluation. See 'bracket' for the semantics of the cleanup action.
11121123--
1124+-- 'gbracket' can express all other exception handling combinators.
1125+--
1126+-- /Inhibits stream fusion/
1127+--
1128+-- /Internal/
11131129{-# INLINE_NORMAL gbracket #-}
11141130gbracket
11151131 :: (MonadIO m, MonadBaseControl IO m)
@@ -1144,6 +1160,7 @@ gbracket bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
11441160 Stop -> do
11451161 D.runIORefFinalizer ref
11461162 return Stop
1163+ -- XXX Do not handle async exceptions, just rethrow them.
11471164 Left e -> do
11481165 -- Clearing of finalizer and running of exception handler must
11491166 -- be atomic wrt async exceptions. Otherwise if we have cleared
@@ -1167,7 +1184,8 @@ _before :: Monad m => (a -> m c) -> Unfold m a b -> Unfold m a b
11671184_before action = gbracket_ (\x -> action x >> return x) (fmap Right)
11681185 (\_ -> return ()) undefined
11691186
1170−-- | Run a side effect before the unfold yields its first element.
1187+-- | Run a side effect @a -> m c@ on the input @a@ before unfolding it using
1188+-- @Unfold m a b@.
11711189--
11721190-- /Internal/
11731191{-# INLINE_NORMAL before #-}
@@ -1192,11 +1210,11 @@ before action (Unfold step1 inject1) = Unfold step inject
11921210_after :: Monad m => (a -> m c) -> Unfold m a b -> Unfold m a b
11931211_after aft = gbracket_ return (fmap Right) aft undefined
11941212
1195−-- | Run a side effect whenever the unfold stops normally.
1213+-- | Like 'after' with following differences:
11961214--
1197−-- Prefer 'after' over this as the @after@ action in this combinator is not
1198−-- executed if the unfold is partially evaluated lazily and then garbage
1199−-- collected.
1215+-- * action @a -> m c@ won't run if the stream is garbage collected
1216+-- after partial evaluation.
1217+-- * Monad @m@ does not require any other constraints.
12001218--
12011219-- /Internal/
12021220{-# INLINE_NORMAL after_ #-}
@@ -1217,8 +1235,14 @@ after_ action (Unfold step1 inject1) = Unfold step inject
12171235 Skip s -> return $ Skip (s, v)
12181236 Stop -> action v >> return Stop
12191237
1220−-- | Run a side effect whenever the unfold stops normally
1221−-- or is garbage collected after a partial lazy evaluation.
1238+-- | Unfold the input @a@ using @Unfold m a b@, run an action on @a@ whenever
1239+-- the unfold stops normally, or if it is garbage collected after a partial
1240+-- lazy evaluation.
1241+--
1242+-- The semantics of the action @a -> m c@ are similar to the cleanup action
1243+-- semantics in 'bracket'.
1244+--
1245+-- /See also 'after_'/
12221246--
12231247-- /Internal/
12241248{-# INLINE_NORMAL after #-}
@@ -1250,7 +1274,10 @@ _onException action =
12501274 (\_ -> return ())
12511275 (nilM (\(a, e :: MC.SomeException) -> action a >> MC.throwM e))
12521276
1253−-- | Run a side effect whenever the unfold aborts due to an exception.
1277+-- | Unfold the input @a@ using @Unfold m a b@, run the action @a -> m c@ on
1278+-- @a@ if the unfold aborts due to an exception.
1279+--
1280+-- /Inhibits stream fusion/
12541281--
12551282-- /Internal/
12561283{-# INLINE_NORMAL onException #-}
@@ -1277,12 +1304,13 @@ _finally action =
12771304 gbracket_ return MC.try action
12781305 (nilM (\(a, e :: MC.SomeException) -> action a >> MC.throwM e))
12791306
1280−-- | Run a side effect whenever the unfold stops normally or aborts due to an
1281−-- exception.
1307+-- | Like 'finally' with following differences:
12821308--
1283−-- Prefer 'finally' over this as the @after@ action in this combinator is not
1284−-- executed if the unfold is partially evaluated lazily and then garbage
1285−-- collected.
1309+-- * action @a -> m c@ won't run if the stream is garbage collected
1310+-- after partial evaluation.
1311+-- * does not require a 'MonadAsync' constraint.
1312+--
1313+-- /Inhibits stream fusion/
12861314--
12871315-- /Internal/
12881316{-# INLINE_NORMAL finally_ #-}
@@ -1303,8 +1331,20 @@ finally_ action (Unfold step1 inject1) = Unfold step inject
13031331 Skip s -> return $ Skip (s, v)
13041332 Stop -> action v >> return Stop
13051333
1306−-- | Run a side effect whenever the unfold stops normally, aborts due to an
1307−-- exception or if it is garbage collected after a partial lazy evaluation.
1334+-- | Unfold the input @a@ using @Unfold m a b@, run an action on @a@ whenever
1335+-- the unfold stops normally, aborts due to an exception or if it is garbage
1336+-- collected after a partial lazy evaluation.
1337+--
1338+-- The semantics of the action @a -> m c@ are similar to the cleanup action
1339+-- semantics in 'bracket'.
1340+--
1341+-- @
1342+-- finally release = bracket return release
1343+-- @
1344+--
1345+-- /See also 'finally_'/
1346+--
1347+-- /Inhibits stream fusion/
13081348--
13091349-- /Internal/
13101350{-# INLINE_NORMAL finally #-}
@@ -1336,14 +1376,14 @@ _bracket bef aft =
13361376 gbracket_ bef MC.try aft (nilM (\(a, e :: MC.SomeException) -> aft a >>
13371377 MC.throwM e))
13381378
1339−-- | @bracket_ before after between@ runs the @before@ action and then unfolds
1340−-- its output using the @between@ unfold. When the @between@ unfold is done or
1341−-- if an exception occurs then the @after@ action is run with the output of
1342−-- @before@ as argument.
1379+-- | Like 'bracket' but with following differences:
1380+--
1381+-- * alloc action @a -> m c@ runs with async exceptions enabled
1382+-- * cleanup action @c -> m d@ won't run if the stream is garbage collected
1383+-- after partial evaluation.
1384+-- * does not require a 'MonadAsync' constraint.
13431385--
1344−-- Prefer 'bracket' over this as the @after@ action in this combinator is not
1345−-- executed if the unfold is partially evaluated lazily and then garbage
1346−-- collected.
1386+-- /Inhibits stream fusion/
13471387--
13481388-- /Internal/
13491389{-# INLINE_NORMAL bracket_ #-}
@@ -1366,11 +1406,26 @@ bracket_ bef aft (Unfold step1 inject1) = Unfold step inject
13661406 Skip s -> return $ Skip (s, v)
13671407 Stop -> aft v >> return Stop
13681408
1369−-- | @bracket before after between@ runs the @before@ action and then unfolds
1370−-- its output using the @between@ unfold. When the @between@ unfold is done or
1371−-- if an exception occurs then the @after@ action is run with the output of
1372−-- @before@ as argument. The after action is also executed if the unfold is
1373−-- paritally evaluated and then garbage collected.
1409+-- | Run the alloc action @a -> m c@ with async exceptions disabled but keeping
1410+-- blocking operations interruptible (see 'Control.Exception.mask'). Use the
1411+-- output @c@ as input to @Unfold m c b@ to generate an output stream.
1412+--
1413+-- @c@ is usually a resource under the state of monad @m@, e.g. a file
1414+-- handle, that requires a cleanup after use. The cleanup action @c -> m d@,
1415+-- runs whenever the stream ends normally, due to a sync or async exception or
1416+-- if it gets garbage collected after a partial lazy evaluation.
1417+--
1418+-- 'bracket' only guarantees that the cleanup action runs, and it runs with
1419+-- async exceptions enabled. The action must ensure that it can successfully
1420+-- cleanup the resource in the face of sync or async exceptions.
1421+--
1422+-- When the stream ends normally or on a sync exception, cleanup action runs
1423+-- immediately in the current thread context, whereas in other cases it runs in
1424+-- the GC context, therefore, cleanup may be delayed until the GC gets to run.
1425+--
1426+-- /See also: 'bracket_', 'gbracket'/
1427+--
1428+-- /Inhibits stream fusion/
13741429--
13751430-- /Internal/
13761431{-# INLINE_NORMAL bracket #-}
@@ -1400,8 +1455,10 @@ bracket bef aft (Unfold step1 inject1) = Unfold step inject
14001455 D.runIORefFinalizer ref
14011456 return Stop
14021457
1403−-- | When unfolding if an exception occurs, unfold the exception using the
1404−-- exception unfold supplied as the first argument to 'handle'.
1458+-- | When unfolding @Unfold m a b@ if an exception @e@ occurs, unfold @e@ using
1459+-- @Unfold m e b@.
1460+--
1461+-- /Inhibits stream fusion/
14051462--
14061463-- /Internal/
14071464{-# INLINE_NORMAL handle #-}