Re: HasCallStack constraint for maximum in Prelude?
Mind you there's a conflict between the suggested `foldMap'` implementation for Bounded elements and efficient implementations with ordered Foldable collections (Map, IntMap, Set, ...).
The latter, just return quickly return the least or greatest element without paying a linear cost to perform the proposed fold.
... which is why functions like sum, product, maximum and minimum have become Foldable class methods, to be overridden whenever it is more efficient. I fail to see how your boundedMinimum reduces time complexity. It is still a general fold in the non-empty case. I wonder, though, how sustainable it is to bloat the Foldable interface with special cases of foldMap that can be overridden. What special monoid structures do exist that warrant a specialized fold implementation? We have (1) commutative monoids, such as Sum and Product, (2) semilattices, where (x<>) is idempotent for every x, (3) Short-circuiting monoids such as First and Last, where either (x<>) or (<>x) is constant for x /= mempty. Evidently, for cases (2) and (3) there are examples where the short- circuiting behavior of the monoid itself can be leveraged to short- circuit foldMap. Does anyone know of a real-world example where sum and product are overridden? All instances Hoogle turns up are O(n). I can't cast that into a type signature right now, but I feel all of null, sum, product, maximum, minimum can be unified given a suitable sub-class that relates the particular Foldable f to the contraint (Ord, Num etc.) present in the head of the function. Olaf
On Fri, Mar 27, 2026 at 10:09:33AM +0100, Olaf Klinke wrote:
... which is why functions like sum, product, maximum and minimum have become Foldable class methods, to be overridden whenever it is more efficient. I fail to see how your boundedMinimum reduces time complexity. It is still a general fold in the non-empty case.
In the *general case* yes, but if a Foldable container has an more efficient non-default `minimum` implementation, it'll get used. -- Viktor. 🇺🇦 Слава Україні!
participants (2)
-
Olaf Klinke -
Viktor Dukhovni