solution

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

Solution 10.3.1

Exercise 10.3.1

For : as maps — pre-composition with upstairs corresponds to pre-composition with downstairs. See Adjunction.

Sources: DaoFP Exercise 10.3.1.

Solution 10.3.2

Exercise 10.3.2

For : naturally in . See Representable Functor.

Sources: DaoFP Exercise 10.3.2.

Solution 10.3.3

Exercise 10.3.3

: naturally in .

Sources: DaoFP Exercise 10.3.3.

Solution 10.5.1

Exercise 10.5.1

Counit of : substitute in and take the identity on the right: , the codiagonal. Unit of : , the diagonal. See Unit and Counit of an Adjunction.

Sources: DaoFP Exercise 10.5.1.

Solution 10.5.2

Exercise 10.5.2

: the mate of is .

Sources: DaoFP Exercise 10.5.2.

Solution 10.5.3

program — Exercise 10.5.3

triangle = counit . fmap unit: fmap unit (L (2,'a')) = L (R (\r -> L (2, r)), 'a'), then counit applies the function to 'a', giving L (2, 'a') — the identity, as required.

Sources: DaoFP Exercise 10.5.3.

Solution 10.5.4

program — Exercise 10.5.4

The result is a function, so call it: let R f = triangle' (R (+1)) in f 5 gives 6, agreeing with (+1) 5. unit (R g) = R (\r -> L (R g, r)), and fmap counit turns each L (R g, r) into g r.

Sources: DaoFP Exercise 10.5.4.

Solution 10.9.1

Exercise 10.9.1

Unit , (singleton string). Counit , evaluating a string of elements of by multiplying them (foldr mappend mempty, i.e. mconcat). See Free-Forgetful Adjunction, List Monad.

Sources: DaoFP Exercise 10.9.1.

Solution 10.9.2

program — Exercise 10.9.2

import Data.Monoid (Sum(..), Product(..))
sumL, prodL :: [Int] -> Int
sumL  = getSum     . foldMap Sum
prodL = getProduct . foldMap Product
-- sumL [1,2,3,4] == 10, prodL [1,2,3,4] == 24

The same “program” (the list) run by two interpreters. See Free Monoid.

Sources: DaoFP Exercise 10.9.2.