Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Int bottom is not safe #312

Open
ursi opened this issue Dec 2, 2024 · 5 comments
Open

Int bottom is not safe #312

ursi opened this issue Dec 2, 2024 · 5 comments

Comments

@ursi
Copy link

ursi commented Dec 2, 2024

export const topInt = 2147483647;
export const bottomInt = -2147483648;

The bottom and top values for Int are listed as above. The problem here is that -bottom is greater than top, which means bottom is actually not a safe value for the Ring instance of Int. You can verify for yourself that negate bottom == bottom!

@acple
Copy link

acple commented Feb 23, 2025

In the 32 bit integer arithmetic, -bottom == bottom is true. Any problems?

@ursi
Copy link
Author

ursi commented Feb 23, 2025

It makes a possible for abs x to be negative, while still inside the bounds that are supposed to be safe. That is was brought this to my attention in the first place.

@acple
Copy link

acple commented Feb 24, 2025

PureScript Int is a representation of 32 bit signed integer so in my opinion it is even reasonable behavior.
However, the behavior of abs bottom for signed integer varies depending on the programming language. Scala(Java), Haskell behaves same as current PureScript. C#, Rust raises an exception.
How would you like it to be?

@ursi
Copy link
Author

ursi commented Feb 24, 2025

Well, maybe I should just "git gud" and acknowledge this edge case is quite common. But what seemed nice to me was to raise the value by 1 so these behaviours go away. Perhaps the choice of what is best would come down to is what assumption people are going to do more "assumption oriented coding" around - bottom being exactly -2^31, or bottom being safe with abs/negate.

@Quelklef
Copy link

Quelklef commented Feb 24, 2025

The choices here seem to be between:

  1. Int is an approximation of ℤ (which happens to be implemented with 32-bit ints). We ought to strive for negate x == x holding iff x == 0, since that's how ℤ acts.
  2. Int is a representation of 32-bit ints. Hence, negate bottom == bottom is both sensible and expected since it matches spec.

For the standard integer type of a general-purpose programming language like PureScript, I think (1) makes more sense. Int should be ℤ with asterisks — most people using Int just want a reasonably-behaved integer value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants