-
Notifications
You must be signed in to change notification settings - Fork 10
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
Don't use 0 as the hash of false #8
Comments
@rightfold
|
In arrays, we start with accumulator 1, so the array length contributes to the array hash even when all elements hash to 0, as @nukisman observed. I made this mistake in the prelude PR however, so thanks for pointing it out! Changing the hash of false, Nothing, ... would be an option too. However, the identity is a natural hash function for Int, and in general counting from 0 seems reasonable. Therefore, I'd rather fix the hash functions of containers if they don't work well with 0 (or any other value). |
Records and tuples still seem to have the same problem:
It seems quite easy to accidentally fall foul of this issue with the current design. Did you consider using something a little closer to the Haskell |
Good point. I only thought about varying lengths, not ordering. I did not like that Haskell's To be honest, I'm not terribly concerned about the records and tuples thing. It's easy enough to fix. Putting the right thing into the prelude is important, though. I will think about it some more. |
Would doing |
I would prefer not to do anything that sets us up for a future breaking release in Prelude. If there's an alternative API we should consider (I'm not aware of what Rust does), then let's discuss that before adding anything to Prelude. Personally I'd prefer to leave hashable out of 0.14, mostly because it's already been a really long time since we made a compiler release. |
FWIW, me too. I have somewhat recently (that is sometime this year, IIRC) rebased and improved the deriving implementation, but there's still a bit of work to do and I'm not going to have time to do it before the end of December. In any case, adding any of the hashable things is not (very) breaking, so there's absolutely no reason to delay 0.14. |
The combining function is
a * 31 + b
, so combining with zero doesn't do anything to the hash. This means that the arrays[false, false, false, true]
,[false, false, true]
,[false, true]
and[true]
all have the same hash.The solution would be not to use zero as the hash of false. Similar for Nothing.
The text was updated successfully, but these errors were encountered: