4 ms·
The problem is not unsigned types. The problem is overflow and underflow conditions not being reported. Compare this to, say, the "declarations-as-assertions"
by betterunix 13y ago
The problem is not unsigned types. The problem is overflow and underflow conditions not being reported.
Compare this to, say, the "declarations-as-assertions" extension in CMUCL/SBCL: when you declare an unsigned type, underflow conditions will be reported:
* (defun test (x)
(the (integer 0) (1- x))
)
* (test 0)
debugger invoked on a TYPE-ERROR in thread
#<THREAD "main thread" RUNNING {1002B2AF23}>:
The value -1 is not of type UNSIGNED-BYTE.
- dllthomas 13y agoSure. Of course, at the same time sometimes overflow and undeflow is fine and should be ignored (when treating unsigned ints as the modular ring they actually are). This is of course not a criticism of CL, which I understand does not check these by default and so is clearly capable of not checking - just a note to anyone who might take more from your comment than was there.
- betterunix 13y agoThe way I see it, if you want arithmetic modulo N, you should be explicit. For example, in CL: [Edit: Just noticed this got posted, but someone replied. The correct version of this is above.]
- dllthomas 13y agoSure, I don't disagree. I'm just saying that's a legitimate use case that should be supported somehow.
- betterunix 13y agoActually, CL goes a step further, which I like: by default, integer arithmetic is arbitrary precision. There are no overflows, unless you count running out of RAM to store your numbers as an overflow. Modular arithmetic must be explicitly specified, leaving no room for surprises: (mod (+ x y) N) The reason I prefer this behavior to C's is that it is more natural. Most of the time when we think of integer arithmetic, we do not assume that it is modulo N; we assume things like " x - 1 < x " will always be true. Forcing unexpected semantics on programmers is a recipe for bugs, and integer underflow/overflow bugs are not uncommon in C programs.
- dllthomas 13y agoI think that'd probably be inappropriate for C's use cases, but I think generally it's a good move. I do like encoding the modular behavior in the type, FWIW; specifying a mod manually everywhere it goes leads to unreadable code. I agree, however, that it should probably not be the default type.