HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulererebase-1.21.2Haskell2010

Data.Hashable

  • 1 type
  • 1 class
  • 12 values
classclass Eq a => Hashable a where
#

The class of types that can be converted to a hash value.

Minimal implementation: hashWithSalt.

Hashable is intended exclusively for use in in-memory data structures. . Hashable does not have a fixed standard. This allows it to improve over time. . Because it does not have a fixed standard, different computers or computers on different versions of the code will observe different hash values. As such, Hashable is not recommended for use other than in-memory datastructures. Specifically, Hashable is not intended for network use or in applications which persist hashed values. For stable hashing use named hashes: sha256, crc32, xxhash etc.

If you are looking for Hashable instance in time package, check time-compat

Methods

  • hashWithSalt :: Int -> a -> Intinfixl 0

    Return a hash value for the argument, using the given salt.

    The general contract of hashWithSalt is:

    • If two values are equal according to the == method, then applying the hashWithSalt method on each of the two values must produce the same integer result if the same salt is used in each case.

    • It is not required that if two values are unequal according to the == method, then applying the hashWithSalt method on each of the two values must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal values may improve the performance of hashing-based data structures.

    • This method can be used to compute different hash values for the same input by providing a different salt in each application of the method. This implies that any instance that defines hashWithSalt must make use of the salt in its implementation.

    • hashWithSalt may return negative Int values.

  • hash :: a -> Int

    Like hashWithSalt, but no salt is used. The default implementation uses hashWithSalt with some default salt. Instances might want to implement this method to provide a more efficient implementation than the default implementation.

Instances94Hashable, …
valuedefaultHashWithSalt :: Hashable a => Int -> a -> Int
#

Since we support a generic implementation of hashWithSalt we cannot also provide a default implementation for that method for the non-generic instance use case. Instead we provide defaultHashWith.

valuehashByteArray
  1. :: ByteArray#

    data to hash

  2. -> Int

    offset, in bytes

  3. -> Int

    length, in bytes

  4. -> Int

    hash value

#

Compute a hash value for the content of this ByteArray#, beginning at the specified offset, using specified number of bytes.

valuehashPtr
  1. :: Ptr a

    pointer to the data to hash

  2. -> Int

    length, in bytes

  3. -> IO Int

    hash value

#

Compute a hash value for the content of this pointer.

valuehashUsing
  1. :: Hashable b
  2. => (a -> b)

    Transformation function.

  3. -> Int

    Salt.

  4. -> a

    Value to transform.

  5. -> Int
#

Transform a value into a Hashable value, then hash the transformed value using the given salt.

This is a useful shorthand in cases where a type can easily be mapped to another type that is already an instance of Hashable. Example:

data Foo = Foo | Bar
         deriving (Enum)

instance Hashable Foo where
    hashWithSalt = hashUsing fromEnum
valuehashByteArrayWithSalt
  1. :: ByteArray#

    data to hash

  2. -> Int

    offset, in bytes

  3. -> Int

    length, in bytes

  4. -> Salt

    salt

  5. -> Salt

    hash value

#

Compute a hash value for the content of this ByteArray#, using an initial salt.

This function can for example be used to hash non-contiguous segments of memory as if they were one contiguous segment, by using the output of one hash as the salt for the next.

valuehashPtrWithSalt
  1. :: Ptr a

    pointer to the data to hash

  2. -> Int

    length, in bytes

  3. -> Salt

    salt

  4. -> IO Salt

    hash value

#

Compute a hash value for the content of this pointer, using an initial salt.

This function can for example be used to hash non-contiguous segments of memory as if they were one contiguous segment, by using the output of one hash as the salt for the next.

datadata Hashed a
#

A hashable value along with the result of the hash function.

Instances11Foldable, Eq1, Ord1, Show1, Hashable1, Eq, …
  • Foldable HashedDefined in hashable-1.4.7.0 · Data.Hashable.Class
  • Eq1 HashedDefined in hashable-1.4.7.0 · Data.Hashable.Class
  • Ord1 HashedDefined in hashable-1.4.7.0 · Data.Hashable.Class
  • Show1 HashedDefined in hashable-1.4.7.0 · Data.Hashable.Class
  • Hashable1 HashedDefined in hashable-1.4.7.0 · Data.Hashable.Class
  • Eq a => Eq (Hashed a)Defined in hashable-1.4.7.0 · Data.Hashable.Class

    Uses precomputed hash to detect inequality faster

  • Ord a => Ord (Hashed a)Defined in hashable-1.4.7.0 · Data.Hashable.Class
  • Show a => Show (Hashed a)Defined in hashable-1.4.7.0 · Data.Hashable.Class
  • (IsString a, Hashable a) => IsString (Hashed a)Defined in hashable-1.4.7.0 · Data.Hashable.Class
  • NFData a => NFData (Hashed a)Defined in hashable-1.4.7.0 · Data.Hashable.Class
  • Eq a => Hashable (Hashed a)Defined in hashable-1.4.7.0 · Data.Hashable.Class