[Haskell-cafe] Haskell function composition commutivity?

Galaxy Being borgauf at gmail.com
Tue Apr 13 16:30:10 UTC 2021


I'm in chapter 4 of Bird's very interesting *Thinking Functionally with
Haskell *and he has a problem at the end of the chapter where he lists
these equations

map f . take n    = take n . map f
map f . reverse   = reverse . map f
map f . sort      = sort . map f
map f . filter p  = map fst . filter snd . map (fork (f,p))
filter (p . g)    = map (invertg) . filter p . map g
reverse . concat  = concat . reverse . map reverse
filter p . concat = concat . map (filter p)

adding this caveat for the 3rd equation

iff x <= y <=> f x <= f y

and this for the 4th equation

fork (f,g) x = (f x, g x)

and for the 5th invertg satisfies invertg . g = id

My confusion is over the commutative-ness of most of this but only
anecdotally. With the particularly dense

map f . filter p = map fst . filter snd . map (fork (f,p))

We have

> :t (map myF . filter myP)
Integral b => [b] -> [b]
> :t (map fst . filter snd . map (myFork (myF,myP)))
Integral b => [b] -> [b]

Is there anything universal to be drawn from these anecdotal examples of
seeming commutativity? My breakdown of the third equation shows the same
type definition for both sides. Is this a way to find equality? All in all,
Bird doesn't indicate that there are any underlying truths, just "almost"
commutativity.

LB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20210413/00cc2db3/attachment.html>


More information about the Haskell-Cafe mailing list