Skip to content

ContentHashable for Hash{Map, Set} is not stable #215

@guibou

Description

@guibou

Describe the bug

  • use ContentHashable to hash the content of an HashMap (directly or indirectly)
  • update my haskell packages. A new version of hashable is released, which change the hash function. The order of HashMap is hence changed, and my nicely content hashed object suddently change their hash.

See the instance for HashMap in the code, it uses toList, the order is unspecified.

https://github.com/tweag/funflow/blob/master/cas/hashable/src/Data/CAS/ContentHashable.hs#L361-L369

To Reproduce

main = do
   print =<< contentHash $ HashMap.fromList [("hello", 10), ("goodbye", 20)]

Run this code with different version of hashable (You may have to try different keys in the map, or a bigger map, in order to trigger the effect).

Expected behavior

It is impossible to guarantee that the hash won't change, but either:

  • Do not expose an instance for a type when it is known that the hash will change systematically on each release of a library as fundamental as hashable
  • (or) sort the values, at the cost of an Ord, Eq constraints on the keys of the HashMap.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions