JuliaApproximation / DomainSets.jl

A Julia package for describing domains as continuous sets of elements
MIT License
72 stars 12 forks source link

Hashes of domains should be consistent with equality #145

Closed daanhb closed 6 months ago

daanhb commented 11 months ago

The logic of the hash function should be the same as that of isequal, since the intention is that a == b should imply hash(a) == hash(b). I doubt this is the case right now.

An example when using intervals is given in https://github.com/JuliaMath/IntervalSets.jl/issues/167:

julia> unique([2..1, 3..1])
2-element Vector{ClosedInterval{Int64}}:
 2 .. 1
 3 .. 1

julia> 2..1 == 3..1
true

The fact that 2..1 and 3..1 hash to different values results in unique returning two elements rather than one. DomainSets has an elaborate way of checking for equality of domains, but the hash function doesn't, so I bet there are many more examples of this type of error.

daanhb commented 7 months ago

Bump, and in addition: defining hash for intervals should not be done in DomainSets.

daanhb commented 7 months ago

I've concluded that this is hard to do. It is harder than deciding on the equality of domains, because you only have a single argument and not a combination of arguments. Since Point(0.5) == 0.5..0.5 their hashes should be the same. But a closed ball with radius zero is also a point, so should have the same hash. A unit ball in 1D is a unit interval. Etcetera.

daanhb commented 6 months ago

This was completed in 8742d3a, noting that the definition of hash in Base can still be invoked as a fallback, even if a generic implementation is made at the level of Domain.