Let's fight Int, Pt. 2

Dear Cafe, it's bad to use Int where you really mean Nat, because this creates extra work (defensive programming, or mental gymnastics to prove that you don't need it) Another Int anti-feature is arithmetics with silent wrap-around - with respect to an unspecified modulus, as highlighted by a recent example: https://gitlab.haskell.org/ghc/ghc/-/issues/19500 What are the possibilities for a "checked Int"? (also, "checked Nat") A historical precedent is set by Ada http://archive.adaic.com/standards/83lrm/html/lrm-03-05.html#3.5.4 "The exception NUMERIC_ERROR is raised by the execution of an operation ... that cannot deliver the correct result" Another point in the design space is Rust: "Integer operators will panic when they overflow when compiled in debug mode." https://doc.rust-lang.org/reference/expressions/operator-expr.html?highlight... The Haskell standard https://www.haskell.org/onlinereport/haskell2010/haskellch6.html#x13-1350006... says that "The results of exceptional conditions (such as overflow or underflow) on the fixed-precision numeric types are undefined;" so any program that relies on Int's wrap-around, is already broken. GHC's base library specifies "modulo 2^width" semantics for signed (Data.Int) and unsigned (Data.Word) integer types. Finally, about "fighting" - I guess it's more about strategies to cope with the present situation. - J.W.

On Sun, 14 Mar 2021, Johannes Waldmann wrote:
A historical precedent is set by Ada http://archive.adaic.com/standards/83lrm/html/lrm-03-05.html#3.5.4 "The exception NUMERIC_ERROR is raised by the execution of an operation ... that cannot deliver the correct result"
Even in C, 'int' has no wrap-around semantics. Chris Lattner, creator of LLVM, blogs about how the LLVM optimizer makes use of that fact: https://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html I guess, if Haskeller's use the LLVM backend and assume wrap-around semantics they will run into the same trouble that Chris describes in his blog posts.
participants (2)
-
Henning Thielemann
-
Johannes Waldmann