[Haskell-cafe] Re: Num instances for 2-dimensional types

Ben Franksen ben.franksen at online.de
Wed Oct 7 12:08:43 EDT 2009

Daniel Fischer wrote:
> Am Montag 05 Oktober 2009 16:29:02 schrieb Job Vranish:
>> In what way is it not a number?
> If there's a natural[1] implementation of fromInteger, good.
> If there isn't, *don't provide one*.
> fromInteger _ = error "Not sensible" is better than doing something
> strange.
> [1] In the case of residue class rings, you may choose between restricting
> [the range of
> legitimate arguments for fromInteger or doing a modulo operation on the
> argument, both ways are natural and meet expectations sufficiently well.

More generally, any ring with multiplicative unit (let's call it 'one') will
do. If there were 'one' and 'zero' methods in class Num, we could even give
a default implementation (however inefficient) as

  fromInteger n | n < 0 = negate (fromInteger n)
  fromInteger n | n > 0 = one + fromInteger (n-1)
  fromInteger _ = zero

In fact, I'd argue that the existence of fromInteger in class Num "morally"
implies a unit for multiplication (namely 'fromInteger 1'), otherwise
fromInteger isn't even a ring morphism.


More information about the Haskell-Cafe mailing list