[Haskell-cafe] Sum and Product do not respect the Monoid laws
David Thomas
davidleothomas at gmail.com
Fri Sep 19 14:31:56 UTC 2014
Restricting to Integral only would be overconstraining - Sum Rational
is perfectly well behaved as a monoid. I could go either way on
whether Double and Float should be excluded - I'm actually slightly
sympathetic to the notion that they shouldn't even be Num.
On Fri, Sep 19, 2014 at 6:58 AM, Jason Choy <jjwchoy at gmail.com> wrote:
> 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
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
More information about the Haskell-Cafe
mailing list