1
+ {-# LANGUAGE GADTs #-}
1
2
module Q4C12.FoldableUtils
2
3
( -- * Intercalation
3
4
intercalate0 , intercalateMap0 , biintercalateMap0 ,
@@ -7,13 +8,18 @@ module Q4C12.FoldableUtils
7
8
prependsMap , prepends , appendsMap , appends ,
8
9
-- * Unfolding
9
10
unfoldr' ,
11
+ -- * Coyoneda on bifunctors
12
+ Bicoyoneda ( Bicoyoneda ), liftBicoyoneda ,
10
13
)
11
14
where
12
15
13
16
import qualified Control.Lens as Lens
14
- import Data.Bifunctor (first )
17
+ import Data.Bifunctor (Bifunctor ( bimap ), first )
15
18
import Data.Bifoldable (Bifoldable (bifoldMap ))
16
19
import Data.Functor.Reverse (Reverse (Reverse ))
20
+ import Data.Kind
21
+ ( Type
22
+ )
17
23
import Data.Semigroup (Dual (Dual ), getDual , Endo (Endo ), appEndo )
18
24
19
25
-- $setup
@@ -22,6 +28,18 @@ import Data.Semigroup (Dual (Dual), getDual, Endo (Endo), appEndo)
22
28
-- >>> import Data.Monoid (Sum (Sum), getSum)
23
29
-- >>> import Data.Semigroup (Last (Last), getLast, Max (Max), getMax)
24
30
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
+
25
43
newtype Joined a = Joined { runJoined :: a -> a }
26
44
27
45
instance (Semigroup a ) => Semigroup (Joined a ) where
0 commit comments