Functoriality
Prev: Functors Next: Function Types
Prev: Functors Next: Function Types
Exercises
- Show that the data type:
data Pair a b = Pair a bis a bifunctor. For additional credit implement all three methods of Bifunctor and use equational reasoning to show that these definitions are compatible with the default implementations whenever they can be applied.
- Show the isomorphism between the standard definition of
Maybeand this desugaring:
type Maybe' a = Either (Const () a) (Identity a)Hint: Define two mappings between the two implementations. For additional credit, show that they are the inverse of each other using equational reasoning.
- Let’s try another data structure. I call it a
PreListbecause it’s a precursor to aList. It replaces recursion with a type parameterb.
data PreList a b = Nil | Cons a bYou could recover our earlier definition of a List by recursively applying PreList to itself (we’ll see how it’s done when we talk about fixed points).
Show that PreList is an instance of Bifunctor.
- Show that the following data types define bifunctors in
aandb:
data K2 c a b = K2 c
data Fst a b = Fst a
data Snd a b = Snd bFor additional credit, check your solutions against Conor McBride’s paper Clowns to the Left of me, Jokers to the Right.
- Define a bifunctor in a language other than Haskell. Implement
bimapfor a generic pair in that language. - Should
std::mapbe considered a bifunctor or a profunctor in the two template argumentsKeyandT? How would you redesign this data type to make it so?