[Haskell-cafe] Re: Positive integers
iavor.diatchki at gmail.com
Fri Mar 24 13:26:33 EST 2006
On 3/23/06, Ben Rudiak-Gould <Benjamin.Rudiak-Gould at cl.cam.ac.uk> wrote:
> Daniel McAllansmith wrote:
> > 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?
> If you're planning wraparound semantics then you're better off with Int
> indexes. Passing an index congruent to -1 mod 2^32 is certainly an error,
> and (!!) may as well fail immediately rather than try to traverse 2^32-1
> list nodes. I think both Int and Word are equally "correct" here, since both
> are proper supersets of the valid set of indexes. (If you're working with
> lists so long that Int may not be big enough, you shouldn't trust Word either.)
This need not be the case: Haskell has only positive literals, so
there could be a separate class that does not have 'negate' and '(-)'
of which natural numbers are an instance. Then overflows would only
occur through the 'topEnd' of the number :-). Of course one could
still use 'n+k' patterns to decrement naturals in loops.
More information about the Haskell-Cafe