Skip to content

Commit 2ebc619

Browse files
authored
Fix postmapM law by adding lifts (#214)
* Add lifts * Fix postMapM law
1 parent ccc7f42 commit 2ebc619

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
- Add [`lifts`](https://github.com/Gabriella439/foldl/pull/214)
12
- Add [`nest`](https://github.com/Gabriella439/foldl/pull/215) for `Fold1`
23
- Add [`Choice`, `Closed`, `Cosieve`, `Extend`, `Semigroupoid`, `Category`, `Strong`, `Arrow` and `ArrowChoice`](https://github.com/Gabriella439/foldl/pull/215) instances for `Fold1`
34
- Add [`Closed`](https://github.com/Gabriella439/foldl/pull/215) instance for `Fold`

src/Control/Foldl.hs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ module Control.Foldl (
118118
, impurely_
119119
, generalize
120120
, simplify
121+
, lifts
121122
, hoists
122123
, duplicateM
123124
, _Fold1
@@ -1169,6 +1170,14 @@ hoists :: (forall x . m x -> n x) -> FoldM m a b -> FoldM n a b
11691170
hoists phi (FoldM step begin done) = FoldM (\a b -> phi (step a b)) (phi begin) (phi . done)
11701171
{-# INLINABLE hoists #-}
11711172

1173+
{- | Lift a monadic value to a 'FoldM';
1174+
works like 'Control.Monad.Trans.Class.lift'.
1175+
1176+
> lifts . pure = pure
1177+
-}
1178+
lifts :: Monad m => m b -> FoldM m a b
1179+
lifts mb = FoldM (\() _ -> pure ()) (pure ()) (\() -> mb)
1180+
11721181
{-| Allows to continue feeding a 'FoldM' even after passing it to a function
11731182
that closes it.
11741183
@@ -1235,11 +1244,11 @@ premapM f (FoldM step begin done) = FoldM step' begin done
12351244

12361245
{-| @(postmapM f folder)@ returns a new 'FoldM' where f is applied to the final value.
12371246
1238-
> postmapM return = id
1247+
> postmapM pure = id
12391248
>
12401249
> postmapM (f >=> g) = postmapM g . postmapM f
12411250
1242-
> postmapM k (pure r) = k r
1251+
> postmapM k (pure r) = lifts (k r)
12431252
-}
12441253
postmapM :: Monad m => (a -> m r) -> FoldM m x a -> FoldM m x r
12451254
postmapM f (FoldM step begin done) = FoldM step begin done'

0 commit comments

Comments
 (0)