[Haskell-cafe] Where do trivial functions on the usual containers live?

Ignat Insarov kindaro at gmail.com
Sun Dec 20 16:56:15 UTC 2020


I immediately surrender any claims to having given perfect types to the
functions I would like to have. All I want is to have things that work somewhere
at hand, and not at all to have perfect things nowhere.

Nevertheless, I am going to reply to your objections.

> > * Group a collection by an equivalence relation:
> >
> > classify ∷ Ord π ⇒ (a → π) → [a] → [[a]]
>
> If the provided function is indeed an "equivalence relation", shouldn't the
> type be Eq π ⇒ (a → π) → [a] → [[a]], which is basically Data.List.groupBy? On
> the other hand if the resulting list is supposed to be ordered, most practical
> applications will probably use a set or a map anyway.

`Data.List.groupBy` detects only adjacent equivalent elements. The intention
here is to detect distant equivalent elements as well. You may see a more
detailed explanation and examples here:
<https://stackoverflow.com/questions/8262179>. See also previous discussion
here: <https://mail.haskell.org/pipermail/libraries/2020-August/030736.html>.

You may also see from these sources that it is not trivial to define this
function without the `Ord` constraint. In more detail: there are two versions,
one of which requires only `Eq` and runs in _n × m_ time, and the other requires
`Ord` and runs in _n × log n_ time. Neither is clearly better.

The most precise and informative type for the result seems to be `[NonEmpty
α]`. I do not see how `Set` or `Map` would be more suitable.

> > * From a finite map, get a map from its range to its fibers:
> >
> > fibers ∷ (Ord k, Ord v) ⇒ Map k v → Map v (NonEmpty k)
>
> Why NonEmpty, and not Set? Or any Monoid? And more importantly, who are we to
> decide? Again, the cost difference between maintaining and re-discovering one
> "standard" vs. re-implementing something more precisely suited to the task at
> hand via (e.g.) Data.Map.foldMapWithKey doesn't seem worth it.

Because a fiber is non-empty. Ideally, sure, I would like `(k, Set k)`.

The remainder of the quote would be very hard to substantiate. I evaluate the
cost and the benefit differently, that is all.

> > * Put a type into the diagonal of its square:
> >
> > diagonal = λx → (x, x)
>
> Now this might fit into Data.Tuple. Then again, it's faster and more
> self-documenting to re-implement it at hoc than to remember a name.
>
> Also, this (again) is less useful than it seems. It will almost always require
> an application of uncurry as well, which makes the program harder to
> understand. Sometimes it's better to replace both with a single application of
> the Monad instance of (->) – or with the "real" function.
>
> For example:
>
> uncurry (+) . diagonal ≡ join (+) ≡ \x -> x + x ≡ (2*)

I added this for a joke. It has been discussed previously at hilarious
length. See:

<https://mail.haskell.org/pipermail/libraries/2018-October/029051.html>
<https://mail.haskell.org/pipermail/libraries/2019-July/029744.html>
<https://mail.haskell.org/pipermail/libraries/2020-September/030789.html>

Surprizingly, many high profile Haskell programmers agree that it is useful.

> > It is unfeasible to put them into the standard libraries. _(Attempts were
> > made.)_
>
> This sounds like the maintainers of the standard libraries had their own
> objections. I suspect some of them might be similar to the ones above. Which
> leads me to suspect you might not have taken their feedback to heart. Which
> might in fact be the biggest hurdle on getting your code accepted into a
> library.

Turns out your objections mostly come down to evaluation of usefulness, which
would be very hard to substantiate by evidence, especially since usefulness is
potential as much as it is actual.


More information about the Haskell-Cafe mailing list