Constraints on definition of `length` should be strengthened

Paolo Giarrusso p.giarrusso at gmail.com
Wed Apr 5 16:15:48 UTC 2017


Thanks for all the mails, but I'm still missing an answer, so I must
have done something wrong. I guess I didn't explain my question well.
Or nobody thinks it's worth answering (in which case, sorry, but I'd
prefer to be told I'm misguided, over having the question ignored).

I'm not asking what length is supposed to do. And I'm especially not
trying to argue for a change—I have no new arguments to contribute
there. I'm really asking a different question: do length docs actually
give a complete specification—it still seems to me they don't.

I've given up long ago on proper specifications for library
*functions* (beyond types, since they're often not enough)—there, it's
less bad, as long as you accept that the implementation is in fact the
specification, and information hiding is nowhere in sight. There,
proper specs (especially textual one) would be too expensive.
But default implementations for typeclass methods are not specs, since
you *can* override them.

But that's very confusing: can really debate on `length (a, b) = 1`
have continued for so long, without noticing that `length = getSum .
foldMap (Sum . const 1)` is a fundamental assumption and is not
mandated by anything written down? I assume I must be missing
something, which is why I asked.

Of all incomplete docs, this matters more since `length (a, b)` is so
surprising. The unsuspecting looking at docs lacks the facts needed to
infer this wart. Spelling out implications would IMHO be even better,
for the same reason people state theorems even though they can be
proved, but maybe some disagree. A complete spec would be a starting
point.

Not that anybody has a duty to fix those docs. But if nobody
acknowledges docs aren't doing their job, why should anybody attempt a
fix?

On 3 April 2017 at 17:59, David Feuer <david.feuer at gmail.com> wrote:
> length should be quite well-behaved relative to foldMap:
>
> length = getSum . foldMap (Sum . const 1)

Thanks, that looks useful to add to docs. I was thinking of `length =
length . toList`.

> Another law pretty much everyone agrees on is that *if* f is an instance of
> Traversable, then
>
> foldMap = foldMapDefault

Cheers,
-- 
Paolo G. Giarrusso - Ph.D. Student, Tübingen University
http://ps.informatik.uni-tuebingen.de/team/giarrusso/


More information about the Libraries mailing list