Was: [Haskell-cafe] Haskell reference documentation, laws first or laws last?

Anthony Clayden anthony.d.clayden at gmail.com
Sun Sep 19 06:43:29 UTC 2021

(Moving this discussion to glasgow-users. It's just not appropriate on the

> I am no longer a novice, and yet would still have a hard time making any use
of the laws as written in constructing instances. Instead, I'd ignore the
laws and write a natural intuitive instance, and it would invariably work.

Seems my approach is very similar to Viktor's. My (very informal)
understanding of the Laws looks nothing like the docos. I regard Foldable
structures as merely more efficient ways to hold a List. Then I expect
'moral equivalences':

> toList . fromList ~=~ id -- going via the Foldable structure
> fromList . toList ~=~ id
> toList ~=~ foldr (:) []

But those aren't equalities. 'moral equivalence' means the Lists have the
same elements, not necessarily in the same order; the structures have the
same elements but possibly in a different arrangement -- that is, in the
`Tree` example, there might be `Empty` scattered about, and elements held
variously in `Leaf`s vs `Node`s. So more accurately:

> fromList . toList . fromList === fromList -- i.e. there's a 'canonical'
> toList . fromList . toList === toList -- i.e. there's a 'canonical' List

(That triple-journey business is a similar style to defining Lattice
pseudocomplements https://en.wikipedia.org/wiki/Pseudocomplement#Properties --
if I can chuck in some math theory.)

I'd expect all other methods to be one of: `reduceStuff === reduceStuff .
toList` or `mapStuff === fromList . mapStuff . toList`.

But! there's no method `fromList` in Foldable. Why not?/please explain.
(Are there Foldable structures which we can't load from a List? At least
assuming the List is finite.) `fromList` is the first thing I write after
declaring the datatype, so I can easily load up some test data. There is
one example `fromList` in the doco. Is that not generalisable? `foldMap
Leaf` would be brutal, but should work? `foldMap singleton` ? (But there's
no method `singleton`.)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/glasgow-haskell-users/attachments/20210919/a312b3ec/attachment.html>

More information about the Glasgow-haskell-users mailing list