@@ -1202,38 +1202,38 @@ many (Fold sstep sinitial sextract) (Fold cstep cinitial cextract) =
12021202 -- Caution! There is mutual recursion here, inlining the right functions is
12031203 -- important.
12041204
1205− {-# INLINE handleSplitStep #-}
1206− handleSplitStep branch cs sres =
1205+ {-# INLINE split #-}
1206+ split f cs sres =
12071207 case sres of
1208− Partial ss1 -> return $ Partial $ branch ss1 cs
1209− Done sb -> runCollector ManyFirst cs sb
1208+ Partial ss -> return $ Partial $ f ss cs
1209+ Done sb -> cstep cs sb >>= collect
12101210
1211− {-# INLINE handleCollectStep #-}
1212− handleCollectStep branch cres =
1211+ {-# INLINE collect #-}
1212+ collect cres =
12131213 case cres of
1214− Partial cs -> do
1215− sres <- sinitial
1216− handleSplitStep branch cs sres
1214+ Partial cs -> sinitial >>= split ManyFirst cs
12171215 Done cb -> return $ Done cb
12181216
1219− -- Do not inline this
1220− runCollector branch cs sb = cstep cs sb >>= handleCollectStep branch
1221−
1222− initial = cinitial >>= handleCollectStep ManyFirst
1217+ -- A fold may terminate even without accepting a single input. So we run
1218+ -- the split fold's initial action even if no input is received. However,
1219+ -- this means that if no input was ever received by "step" we discard the
1220+ -- fold's initial result which could have generated an effect. However,
1221+ -- note that if "sinitial" results in Done we do collect its output even
1222+ -- though the fold may not have received any input. XXX Is this
1223+ -- inconsistent?
1224+ initial = cinitial >>= collect
12231225
12241226 {-# INLINE step_ #-}
1225− step_ ss cs a = do
1226− sres <- sstep ss a
1227− handleSplitStep ManyLoop cs sres
1227+ step_ ss cs a = sstep ss a >>= split ManyLoop cs
12281228
12291229 {-# INLINE step #-}
12301230 step (ManyFirst ss cs) a = step_ ss cs a
12311231 step (ManyLoop ss cs) a = step_ ss cs a
12321232
1233+ -- Do not extract the split fold if no item was consumed.
12331234 extract (ManyFirst _ cs) = cextract cs
12341235 extract (ManyLoop ss cs) = do
1235− sb <- sextract ss
1236− cres <- cstep cs sb
1236+ cres <- sextract ss >>= cstep cs
12371237 case cres of
12381238 Partial s -> cextract s
12391239 Done b -> return b
@@ -1262,33 +1262,25 @@ manyPost (Fold sstep sinitial sextract) (Fold cstep cinitial cextract) =
12621262 -- Caution! There is mutual recursion here, inlining the right functions is
12631263 -- important.
12641264
1265− {-# INLINE handleSplitStep #-}
1266− handleSplitStep cs sres =
1265+ {-# INLINE split #-}
1266+ split cs sres =
12671267 case sres of
12681268 Partial ss1 -> return $ Partial $ Tuple' ss1 cs
1269− Done sb -> runCollector cs sb
1269+ Done sb -> cstep cs sb >>= collect
12701270
1271− {-# INLINE handleCollectStep #-}
1272− handleCollectStep cres =
1271+ {-# INLINE collect #-}
1272+ collect cres =
12731273 case cres of
1274− Partial cs -> do
1275− sres <- sinitial
1276− handleSplitStep cs sres
1274+ Partial cs -> sinitial >>= split cs
12771275 Done cb -> return $ Done cb
12781276
1279− -- Do not inline this
1280− runCollector cs sb = cstep cs sb >>= handleCollectStep
1281−
1282− initial = cinitial >>= handleCollectStep
1277+ initial = cinitial >>= collect
12831278
12841279 {-# INLINE step #-}
1285− step (Tuple' ss cs) a = do
1286− sres <- sstep ss a
1287− handleSplitStep cs sres
1280+ step (Tuple' ss cs) a = sstep ss a >>= split cs
12881281
12891282 extract (Tuple' ss cs) = do
1290− sb <- sextract ss
1291− cres <- cstep cs sb
1283+ cres <- sextract ss >>= cstep cs
12921284 case cres of
12931285 Partial s -> cextract s
12941286 Done b -> return b