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

Impossible values of allunique() and allequal() #55134

Closed
Eldriitch opened this issue Jul 15, 2024 · 3 comments
Closed

Impossible values of allunique() and allequal() #55134

Eldriitch opened this issue Jul 15, 2024 · 3 comments
Labels
collections Data structures holding multiple items, e.g. sets duplicate Indicates similar issues or pull requests equality Issues relating to equality relations: ==, ===, isequal hashing

Comments

@Eldriitch
Copy link

With the following struct, allunique and allequal are giving incompatible (and mathematically impossible!) results.
MWE:

module MWE
struct Problem
    position::Array{Float64}
    value::Float64
end
Base.:(==)(a::Problem, b::Problem) = a.position == b.position && a.value == b.value
end

and then in the REPL

julia> X = [MWE.Problem([0.0,0.0,0.0], 4*pi) for i in 1:32];

julia> allunique(X)
true
julia> allequal(X)
true

And even more confusingly

julia> allunique(X[1:31])
false
julia> allequal(X[1:31])
true

So not only are we getting incompatible values of allunique and allequal, we also fail to have allunique(X) => allunique(X[1:end]).
Clearly allunique is behaving both contrary to the documentation and any reasonable expectation, for iterators with at least 32 entries.
Julia versioninfo:

julia> versioninfo()
Julia Version 1.10.4
Commit 48d4fd48430 (2024-06-04 10:41 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 16 × AMD Ryzen 9 7940HS w/ Radeon 780M Graphics
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, znver3)
Threads: 1 default, 0 interactive, 1 GC (on 16 virtual cores)
@mbauman
Copy link
Member

mbauman commented Jul 15, 2024

When you change how a struct defines equality, you must also symmetrically change how it hashes. Check out AutoHashEquals.jl.

Closing as a duplicate of #12198.

@mbauman mbauman closed this as not planned Won't fix, can't repro, duplicate, stale Jul 15, 2024
@Eldriitch
Copy link
Author

I see -- that makes sense. Not great that the documentation is just... literally wrong then.

@mbauman
Copy link
Member

mbauman commented Jul 15, 2024

The biggest issue that I see is that the == docstring could be worded more strongly in what it means to implement. See my suggested improvement in #55135.

@nsajko nsajko added collections Data structures holding multiple items, e.g. sets hashing duplicate Indicates similar issues or pull requests equality Issues relating to equality relations: ==, ===, isequal labels Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
collections Data structures holding multiple items, e.g. sets duplicate Indicates similar issues or pull requests equality Issues relating to equality relations: ==, ===, isequal hashing
Projects
None yet
Development

No branches or pull requests

3 participants