@@ -259,6 +259,15 @@ ghci> map abs . sort $ [5,-1,3,-7]
259259[7 ,1 ,3 ,5 ]
260260```
261261
262+ Compared to an actual function with the polymorphic type, like ` take 3 ` :
263+
264+ ``` haskell
265+ ghci> take 3 . map abs $ [5 ,- 1 ,3 ,- 7 ]
266+ [5 ,1 ,3 ]
267+ ghci> map abs . take 3 $ [5 ,- 1 ,3 ,- 7 ]
268+ [5 ,1 ,3 ]
269+ ```
270+
262271How about:
263272
264273``` haskell
@@ -276,6 +285,20 @@ And again we have the same free theorem, `doIt . map f === fmap f . doIt`. No
276285matter how you implement `doIt` , it is guaranteed to commute with `map` and
277286`fmap` !
278287
288+ ```haskell
289+ -- no free theorem: `maximumMay`
290+ ghci> minimumMay . map abs $ [5 ,- 1 ,3 ,- 7 ]
291+ Just 1
292+ ghci> map abs . minimumMay $ [5 ,- 1 ,3 ,- 7 ]
293+ Just 7
294+
295+ -- free theorem: `listToMaybe`
296+ ghci> listToMaybe . map abs $ [5 ,- 1 ,3 ,- 7 ]
297+ Just 5
298+ ghci> map abs . listToMaybe $ [5 ,- 1 ,3 ,- 7 ]
299+ Just 5
300+ ```
301+
279302Let's try another one:
280303
281304``` haskell
@@ -289,6 +312,47 @@ the _contents_ of the list. We also have another free theorem, `collapse . map
289312f == collapse`: mapping a function shouldn't change the output, because none of
290313the actual values matter.
291314
315+ ```haskell
316+ -- no free theorem: `sum`
317+ ghci> sum . map abs $ [5 ,- 1 ,3 ,- 7 ]
318+ 16
319+ ghci> sum [5 ,- 1 ,3 ,- 7 ]
320+ 0
321+
322+ -- free theorem: `length`
323+ ghci> length . map abs $ [5 ,- 1 ,3 ,- 7 ]
324+ 4
325+ ghci> length [5 ,- 1 ,3 ,- 7 ]
326+ 4
327+ ```
328+
329+ How about something in the opposite direction:
330+
331+ ``` haskell
332+ dupper :: a -> [a ]
333+ ```
334+
335+ From this type signature, we can conclude that the final list must contain the
336+ _same item_! The only possible inhabitants are `replicate n` for some `n` . In
337+ fact, `forall a. a -> [a]` is isomorphic to the natural numbers
338+ (`Natural `)... see if you can prove it for fun?
339+
340+ Again , we have a free theorem: `map f . dupper == dupper . f`
341+
342+ ```haskell
343+ -- no free theorem: `take 3 . iterate (+1)`
344+ ghci> take 3 . iterate (+ 1 ) . negate $ 4
345+ [- 4 ,- 3 ,- 2 ]
346+ ghci> map negate . take 3 . iterate (+ 1 ) $ 4
347+ [- 4 ,- 5 ,- 6 ]
348+
349+ -- free theorem: `replicate 3`
350+ ghci> replicate 3 . negate $ 4
351+ [- 4 ,- 4 ,- 4 ]
352+ ghci> map negate . replicate 3 $ 4
353+ [- 4 ,- 4 ,- 4 ]
354+ ```
355+
292356One final one, with a higher-rank variable:
293357
294358``` haskell
0 commit comments