
Foldable doesn't have much in the way of interesting laws, really. And I'm
not sure how to make the relationships between foldMap, foldr, foldl,
foldr', and foldl' clear to beginners. I'll have to think on that a bit.
foldMap unifies finite, left-finite, right-finite, and doubly infinite
structures, which is a tricky thing to explain to beginners!
On Sun, Sep 19, 2021, 12:14 AM Viktor Dukhovni
On Sat, Sep 18, 2021 at 09:57:09PM -0400, David Feuer wrote:
I'll try to take a look. When writing instances (which I consider a beginner-level activity), it's really important to learn to follow the laws. While that may just sound like a bunch of theory, it turns out that in practice violating typeclass laws leads to seriously unintuitive behavior, breaking the expectations users have developed after using many instances.
If that's the intent, the laws for Foldable need a substantially better explanation that the given equations, which shed very little light on the actual requirements.
It would probably be most productive to add new prose explaining the laws to the "Defining instances" section:
https://dnssec-stats.ant.isi.edu/~viktor/haskell/docs/libraries/base/Data-Fo...
and reference the laws also from there, if someone wants the concise form for QuickCheck property tests or the like.
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. The text in "Definining Instances" aims to help readers ensure the implementations are reasonably efficient, but presently does not drag in the laws.
As the author of the laws section, perhaps you'd be willing to contribute explanatory prose???
-- Viktor. _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.