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

(=) does not use physical equality unlike compare #7502

Closed
vicuna opened this issue Mar 9, 2017 · 3 comments
Closed

(=) does not use physical equality unlike compare #7502

vicuna opened this issue Mar 9, 2017 · 3 comments

Comments

@vicuna
Copy link

vicuna commented Mar 9, 2017

Original bug ID: 7502
Reporter: eponier
Assigned to: @alainfrisch
Status: resolved (set by @alainfrisch on 2017-03-09T10:05:23Z)
Resolution: won't fix
Priority: low
Severity: minor
Category: standard library
Related to: #3921 #7524

Bug description

For some reason, I was playing with:

let rec l = 1 :: l;;

And I tried to manipulate it. But while "compare l l" returns 0 instantaneously, "l = l" loops infinitely. I do not know if structural equality is supposed to use physical equality as a speed-up, but I find this difference in behaviour surprising. The same happens with (<) ad (>).

Moreover, in an interactive session (utop or ocaml), the loop is not interrupted by pressing ctrl+c.

@vicuna
Copy link
Author

vicuna commented Mar 9, 2017

Comment author: eponier

Tested on 4.04.0.

@vicuna
Copy link
Author

vicuna commented Mar 9, 2017

Comment author: @Octachron

There is at least a documentation mismatch between this behavior and the description of compare in the manual that claims compatibily between (=) and compare except for NaN.

@vicuna
Copy link
Author

vicuna commented Mar 9, 2017

Comment author: @alainfrisch

I do not know if structural equality is supposed to use physical equality as a speed-up, but I find this difference in behaviour surprising.

x = y has a different semantics than compare x y = 0, and it cannot use physical equality as a "shortcut". The reason is related to floating points, for which nan = nan should be false even though the two arguments are physically equal, but one wants compare nan nan to return 0. And it has been decided that the same property should hold for nested floats as well, i.e. let x = ("", nan) in x = x should be false, but let x = ("", nan) in compare x x should be 0. Basically, (=) is not an equivalence relation (reflexivity does not hold) while (fun x y -> compare x y = 0) is (on its definition domain).

Honestly, I think it would be better to make (=) reflexive(and use physical equality as a short-cut) and to provide an explicit comparison function on floats using IEEE semantics. But I don't believe such proposal has any chance to be accepted. So I'm "resolving" this ticket for now.

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

No branches or pull requests

2 participants