
On 2006-03-24, Henning Thielemann
On Fri, 24 Mar 2006, Aaron Denney wrote:
On 2006-03-24, Daniel McAllansmith
wrote: Unless I've missed it, there is no typeclass for positive integers in GHC. Is there any particular reason it doesn't exist?
The number of useable operations is small, and checks for leaving the domain would have to be done all the time. It basically doesn't buy anything.
A new type, say Cardinal as in Modula, would document for the user of a function that only non-negative numbers are allowed and the function writer can be sure, that only non-negative numbers are passed. Today function writers have to check explicitly for negative numbers, when they are not wanted. With a Cardinal type some of these checks can be dropped because of the static guarantee that Cardinals are never negative.
As long as the constructors aren't exposed, and then the checks get pushed into the injection function. Which really doesn't give us "static safety", but does let us push the discovery earlier, so may still be useful. And indexing still needs to check for overflows.
Further on think of QuickCheck: A Cardinal type with an Arbitrary instance would save us the (>=0) condition
True.
The (n+k) pattern are often defended because of its use in inductive definitions. So I claim that Cardinals are at least as important as (n+k) patterns. :-)
And neither need to be in the language. :) Basically, my big objection is that it's hard to define many useful operations on them that are statically safe. Any definition of Num a for instance leaves a whole bunch of unsafe methods, or just plain inapplicable methods, such as "negate". Now granted, the numeric hierarchy should be broken up a bit (hmm, I should finish my strawman proposal for Haskell'), but even then I see problems. -- Aaron Denney -><-