Skip to content

Commit 16bb40a

Browse files
foldable-utils: Add Bicoyoneda.
1 parent 3ff712c commit 16bb40a

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

packages/foldable-utils/src/Q4C12/FoldableUtils.hs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE GADTs #-}
12
module Q4C12.FoldableUtils
23
( -- * Intercalation
34
intercalate0, intercalateMap0, biintercalateMap0,
@@ -7,13 +8,18 @@ module Q4C12.FoldableUtils
78
prependsMap, prepends, appendsMap, appends,
89
-- * Unfolding
910
unfoldr',
11+
-- * Coyoneda on bifunctors
12+
Bicoyoneda ( Bicoyoneda ), liftBicoyoneda,
1013
)
1114
where
1215

1316
import qualified Control.Lens as Lens
14-
import Data.Bifunctor (first)
17+
import Data.Bifunctor (Bifunctor (bimap), first)
1518
import Data.Bifoldable (Bifoldable (bifoldMap))
1619
import Data.Functor.Reverse (Reverse (Reverse))
20+
import Data.Kind
21+
( Type
22+
)
1723
import Data.Semigroup (Dual (Dual), getDual, Endo (Endo), appEndo)
1824

1925
-- $setup
@@ -22,6 +28,18 @@ import Data.Semigroup (Dual (Dual), getDual, Endo (Endo), appEndo)
2228
-- >>> import Data.Monoid (Sum (Sum), getSum)
2329
-- >>> import Data.Semigroup (Last (Last), getLast, Max (Max), getMax)
2430

31+
data Bicoyoneda :: ( Type -> Type -> Type ) -> Type -> Type -> Type where
32+
Bicoyoneda :: ( s -> a ) -> ( t -> b ) -> f s t -> Bicoyoneda f a b
33+
34+
instance Bifunctor ( Bicoyoneda f ) where
35+
bimap f g ( Bicoyoneda f' g' x ) = Bicoyoneda ( f . f' ) ( g . g' ) x
36+
37+
instance ( Bifoldable f ) => Bifoldable ( Bicoyoneda f ) where
38+
bifoldMap f g ( Bicoyoneda f' g' x ) = bifoldMap ( f . f' ) ( g . g' ) x
39+
40+
liftBicoyoneda :: f a b -> Bicoyoneda f a b
41+
liftBicoyoneda = Bicoyoneda id id
42+
2543
newtype Joined a = Joined { runJoined :: a -> a }
2644

2745
instance (Semigroup a) => Semigroup (Joined a) where

0 commit comments

Comments
 (0)