[Haskell-cafe] Re: A question about "monad laws"

Richard A. O'Keefe ok at cs.otago.ac.nz
Mon Feb 11 20:02:20 EST 2008

On 12 Feb 2008, at 4:35 am, Andrew Butterfield wrote:
[floating point addition is not associative]

And this is an excellent example of why violating expected laws is BAD.
The failure of floating point addition to be associative means that  
are umpteen ways of computing polynomials, for example, and doing it  
ways will give you different answers.  This is *not* a good way to write
reliable software.  I did enough Numerical Analysis papers in my pre- 
PhD years
to get quite scared sometimes.  Oh, here's a good one:

	dot1 [] [] = 0
	dot1 (x:xs) (y:ys) = x*y + dots1 xs ys

Obvious naive code for dot product.  Switch over to tail recursion

	dot2 xs ys = aux xs ys 0
	  where aux [] [] s = s
		aux (x:xs) (y:ys) s = aux xs ys (s + x*y)

The problem is that (a) in floating point arithmetic these two functions
give DIFFERENT answers, and (b) NEITHER of them is wrong (arguably,  
of them is right either).  For integers, of course, they must agree  
(if I
haven't made any silly mistakes).

This kind of thing makes it incredibly hard to think about numerical

Basically, laws are stated so that implementors will make stuff that
clients don't have to think about until their brains melt.

More information about the Haskell-Cafe mailing list