Category Theory for Programmers Chapter 8: Functoriality
Last chapter was about functors, but this is about how we can make them useful. In particular, combining functors via bifunctors. A reminder of what a bifunctor is:
class Bifunctor f where
bimap :: (a -> c) -> (b -> d) -> f a b -> f c d
bimap g h = first g . second h
first :: (a -> c) -> f a b -> f c b
first g = bimap g id
second :: (b -> d) -> f a b -> f a d
second = bimap id
-
Show the data type
data Pair a b = Pair a bis a bifunctor. Specifically, we show that ifaorbis a functor, thenPairis a functor ifinstance Bifunctor (Pair a b) where bimap f g (Pair a b) = Pair (f a) (g b) -
Show isomorphism between
Maybeandtype Maybe' a = Either (Const () a) (Identity a)maybeToEither :: Maybe a -> Maybe' a maybeToEither Nothing = Left (Const ()) maybeToEither (Just x) = Right (Identity x) eitherToMaybe :: Maybe' a -> Maybe a eitherToMaybe (Left _) = Nothing eitherToMaybe (Right (Identity x)) = Just x -
Show that
PreListis an instance ofBifunctor,data PreList a b = Nil | Cons a bLet’s make this look like some things we know are functors and bifunctors already.
data PreList a b = Maybe (a, b) = Either (Const () c) (Identity a, Identity b)I don’t really know where to go with this other than we see it’s functors and bifunctors all the way down. But it’s probably easiest to just implement the interface outright.
-
Show
K2,Fst, andSnddefine bifunctors inaandb.interface Bifunctor (K2 c a b) where bimap _ _ (K2 c) = K2 c interface Bifunctor (Fst a b) where bimap f _ (Fst a) = Fst (f a) interface Bifunctor (Snd a b) where bimap _ g (Snd a b) = Snd a (g b)As for the rest of this, a couple good resources about profunctors are this link and this video.