[Haskell-cafe] Re: Why 'round' does not just round numbers ?

Richard O'Keefe ok at cs.otago.ac.nz
Tue Oct 28 17:35:09 EDT 2008


Let me offer another suggestion which I think can be
fitted into Haskell quite well.  For the applications
of rounding choice that I'm aware of, you want to
choose when you write the code, not when you run it.

This was actually reflected in the design of a real
machine: the DEC Alpha.  Floating point instructions
had variants for "whatever the PSW says" and for
"this particular mode".  It would have been nice (had
DEC survived, if the Alpha were still alive) to be
able to generate the first-direction instructions, as
that's rather more efficient than switching rounding
modes twice.

Now the Haskell Way is to do things with types.
So,

	newtype RoundedUp   t = RoundedUp t
	newtype RoundedDown t = RoundedDown t
	newtype Truncated   t = Truncated t

	instance Floating t => Num (RoundedUp t)
	  where ...
	instance Floating t => Num (RoundedDown t)
	  where ...
	instance Floating t => Num (Truncated t)
	  where ...
	...

Then one could write

	let i = round (Truncated x + Truncated y)

This is just a quick sketch.  I'm sure it has problems.
It does, however, deal with a fairly major problem in
attempting to use hardware rounding modes in Haskell,
and that is laziness.  Putting the rounding mode in the
type means that the information is right there at each
operator without having to use monadic floating point.



More information about the Haskell-Cafe mailing list