[Haskell-cafe] Sum and Product do not respect the Monoid laws

Jason Choy jjwchoy at gmail.com
Fri Sep 19 13:58:00 UTC 2014


The Monoid instances for Sum and Product do not respect the Monoid laws.

The instances are:

Num a => instance Monoid (Sum a)
Num a => instance Monoid (Product a)

Float and Double are instances of the Num typeclass, however, floating
point addition and multiplication are not associative. Here's an example:

> (Sum 1234.567 `mappend` Sum 45.67834) `mappend` Sum 0.0004
Sum {getSum = 1280.2457399999998}

> Sum 1234.567 `mappend` (Sum 45.67834 `mappend` Sum 0.0004)
Sum {getSum = 1280.24574}

Shouldn't these instances be constrained to Integral types?

Jason
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140919/19b8e14b/attachment.html>


More information about the Haskell-Cafe mailing list