[Haskell-cafe] Let's fight Int, Pt. 2
Johannes Waldmann
johannes.waldmann at htwk-leipzig.de
Sun Mar 14 20:49:29 UTC 2021
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=numeric#overflow
The Haskell standard
https://www.haskell.org/onlinereport/haskell2010/haskellch6.html#x13-1350006.4
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.
More information about the Haskell-Cafe
mailing list