solution

Solutions to the exercises of DaoFP, Chapter 5: DaoFP Chapter 5 Exercises. Index: Map of Content.

Solution 5.1.1

proof — Exercise 5.1.1

The bijection sends to (the component into is forced). Changing focus along : post-composing with gives , whose image is ; applying the bijection first and then gives too. Naturality in (pre-composition with ) is equally immediate. Hence is a natural isomorphism.

Sources: DaoFP Exercise 5.1.1.

Solution 5.1.2

program — Exercise 5.1.2

A map into a product is a pair; each component maps out of a sum, so is itself a pair:

It is not unique: e.g. the first component could use on both summands (forgetting the ), or the second component could be any other arrow . The type does not pin down the function.

h :: Either b (a, b) -> (Either () a, b)
h (Left b)       = (Left (), b)
h (Right (a, b)) = (Right a, b)

Sources: DaoFP Exercise 5.1.2.

Solution 5.1.3

program — Exercise 5.1.3

A map out of is a pair with and , each a map into a product: , . So — the same function as before, reached by decomposing in the other order (either (\b -> (Left (), b)) (\(a, b) -> (Right a, b))).

Sources: DaoFP Exercise 5.1.3.

Solution 5.1.4

program — Exercise 5.1.4

maybeAB :: Either b (a, b) -> (Maybe a, b)
maybeAB (Left b)       = (Nothing, b)
maybeAB (Right (a, b)) = (Just a, b)

Not unique: maybeAB (Right (a, b)) = (Nothing, b) also type-checks, as would returning Nothing everywhere. Parametricity forces the b component (the only b available) but leaves the Maybe a component free.

Sources: DaoFP Exercise 5.1.4.