On Wed, Mar 25, 2026 at 10:23:37AM +0000, Tom Ellis wrote:
On Wed, Mar 25, 2026 at 11:19:20AM +0100, Olaf Klinke via Haskell-Cafe wrote:
For (Bounded a, Ord a) one should replace maximum by an ordinary fold, given a suitable newtype wrapper with a Monoid instance, e.g.
(Ord a, Bounded a) => Monoid (Data.Semigroup.Max a)
For a Semigroup surely it doesn't need Bounded? (A Monoid would need Bounded).
What Olaf seems to have in mind does neet a Monoid, for example: λ> import Data.Coerce λ> import Data.Foldable λ> import Data.Semigroup λ> λ> minimum :: forall t a. (Ord a, Bounded a, Foldable t) => t a -> a; minimum = (coerce @(Min a) @a) . foldMap' (coerce @a @(Min a)) λ> maximum :: forall t a. (Ord a, Bounded a, Foldable t) => t a -> a; maximum = (coerce @(Max a) @a) . foldMap' (coerce @a @(Max a)) λ> λ> maximum [] :: Int -9223372036854775808 λ> minimum [] :: Int 9223372036854775807 λ> maximum [] :: Word 0 λ> minimum [] :: Word 18446744073709551615 λ> maximum [1..10] :: Int 10 λ> minimum [1..10] :: Int 1 -- Viktor. 🇺🇦 Слава Україні!