# [Haskell-cafe] Re: Numeric type classes

Ross Paterson ross at soi.city.ac.uk
Thu Sep 14 06:39:44 EDT 2006

```On Thu, Sep 14, 2006 at 01:11:56AM -0400, David Menendez wrote:
> Ross Paterson writes:
> > I've collected some notes on these issues at
> >
>
> Coincidentally, I spent some time last week thinking about a replacement
> for the Num class. I think I managed to come up with something that's
> more flexible than Num, but still mostly comprehensible.

The fact that the first part of your structure is much the same as
the one on the web page (which is essentially that part of the revised
numeric prelude plus a Haskell 98-compatible veneer) is evidence that
it's pretty clear what to do with Num and Fractional.

The only point of contention is whether to factor out monoid and
semiring classes.  Arguments against include:

* There are lots of monoids, and (+) doesn't seem a reasonable symbol
for some of them.

* Having (+) work on lists, tuples and all the other monoids would make
error messages more complicated.

On the other hand, if we had a Natural type, it would be the standard
example of a semiring.

> I'm not sure what the contract is for fromInteger. Perhaps something
> like,
>
>     fromInteger 0         = zero
>     fromInteger 1         = one
>     fromInteger n | n < 0 = negate (fromInteger (negate n))
>     fromInteger n         = one + fromInteger (n-1)
>
> Which, actually, could also be a default definition.

That is also the default definition in the revised numeric prelude,
but we can do better using associativity:

fromInteger n
| n < 0       =  negate (fi (negate n))
| otherwise   =  fi n
where fi 0    =  zero
fi 1    =  one
fi n
| even n    = fin + fin
| otherwise = fin + fin + one
where fin = fi (n `div` 2)

```