[Haskell-beginners] type class for custom numeric types

Stephen Tetley stephen.tetley at gmail.com
Fri Apr 9 04:30:04 EDT 2010


Hello Ashish

Yes - to get integer literals for a type, you have to make that type
an instance of Num.

Making fromIntegral throw an out-of-bounds error is as much a problem
as using error anywhere in your program. You get into the usual
trade-off between robustness (handling errors, never dying) and
clarity - often robustness isn't the primary virtue. If you wanted,
you could use coercion in the fromIntegral implementation - say values
over 100 are truncated to 100 rather than throw an error.

Ideally a Num type should support the usual properties of addition,
multiplication etc. (associative, commutative, identity for mult, ...)
there is no enforcement of this though, and there are some strange Num
instances "in the wild".

Best wishes

Stephen


More information about the Beginners mailing list