It is a bit surprising that Haskell has no type for natural numbers. The Haskell Report defines only
Integer for, well, integers and
Int for integers that are reasonably small. The base package then adds
Word as the unsigned variant of
Int as well as types for signed and unsigned integers with a fixed number of bits. An unsigned variant of
Integer is missing.
That is why I recently wrote the natural-numbers package. It defines a type
Natural as a simple wrapper around
Integer. Negative numbers are avoided by checks, which are only performed where a negative number can actually occur. So performance should be quite good, although I have not done any tests in this direction.
Surprisingly, nobody seems to have published such a package on HackageDB before. The naturals package is pretty close to what I have written. However, its
Natural type covers a so-called indeterminate value. This value is used where negative numbers would occur usually. For example, it is the result of the expression
3 - 4. I think this is not reasonable, since that indeterminate value is not a natural, while being a value of type
Natural. So the natural-numbers package I have developed always raises an error if an operation cannot be performed.
There are further packages on HackageDB that deal with natural numbers, but these have goals different from mine. The nat package has a natural number implementation that employs lazyness, while the packages type-level-natural-number, natural-number, and type-unary deal with type-level representations of natural numbers.