[Haskell-cafe] Re: Positive integers

Daniel McAllansmith dagda at xtra.co.nz
Thu Mar 23 20:52:13 EST 2006

On Friday 24 March 2006 13:14, Aaron Denney wrote:
> On 2006-03-24, Daniel McAllansmith <dagda at xtra.co.nz> 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.

I can see the domain bounds check would be a problem in theory, but in 
practice doesn't the type enforce that?  Keeping Word positive costs nothing 
because it just overflows.  Wouldn't it be much the same?
Not that I'm really concerned about the absence, probably because of your 
other point.

> > Also, it seems Word would be a far better type in the likes of (!!),
> > length, etc.  Is it just tradition that resulted in the use of Int?
> No.  I'd like to be able to get the differences in length both positive
> and negative, for example.  (This could be fixed by making (+) and (-)
> instance of an MPTC, though, as additive torsors.)

An additive torsor is?

I'd maintain that the difference between two lengths is an entirely different 
quantity from the length of a list.  Though I'll admit the extra work 
converting to Integrals to get that quantity would be a pain, and probably 
not worth it.

I was more concerned about functions with Int as input, such as (!!), that can 
result in errors.
If practically feasible I would have preferred (!!) to take a Word rather than 
Int, even though you'd sometimes need bounds checks at fromInteger calls to 
be safe.

I suppose the 'correct' type for the index in (!!) would be
(Integral n, LowBounded n) => n
if such a low bound type class existed.

More information about the Haskell-Cafe mailing list