Skip to content
A ppx rewriter that generates hash functions from type expressions and definitions
Branch: master
Clone or download
Latest commit 2392af1 May 3, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
doc v0.9.115.03+73 Jun 8, 2017
expander v0.13-preview.120.26+17 Mar 6, 2019
runtime-lib v0.13-preview.120.30+220 Apr 8, 2019
src v0.12-preview.120.18+252 Jan 16, 2019
test v0.13-preview.120.27+112 Mar 14, 2019
.gitignore v0.13-preview.120.27+112 Mar 14, 2019 v0.11.116.17+187 Mar 16, 2018 v0.11.120.08+153 Nov 6, 2018 v0.12-preview.120.18+252 Jan 16, 2019
Makefile v0.12-preview.120.18+252 Jan 16, 2019
dune v0.12-preview.120.18+252 Jan 16, 2019
dune-project v0.12-preview.120.18+252 Jan 16, 2019
ppx_hash.opam v0.13-preview.120.34+189 May 3, 2019


A ppx writer that generates hash functions from type expressions and definitions.


Type definitions: [@@deriving hash] Expressions: [%hash_fold: TYPE] and [%hash: TYPE] Record fields: [@hash.ignore]

Basic usage

    type t = {
      s : string;
      x : (int * bool) list;
    [@@deriving hash]

This will generate a function hash_fold_t : Hash.state -> t -> Hash.state.

Where Hash is Ppx_hash_lib.Std.Hash.

The generated function follows the structure of the type; allowing user overrides at every level. This is in contrast to ocaml's builtin polymorphic hashing Hashtbl.hash which ignores user overrides.

Also generated is a direct hash-function hash : t -> Hash.hash_value. This function will be named hash_<T> when != "t".

The direct hash function is the one suitable for Hashable.Make.


In a module signature, type t [@@deriving hash] will add both hash_fold_t and hash (or hash_<T>) to the signature.

Hashing without a type definition

A folding hash function is accessed/created as [%hash_fold: TYPE]. A direct hash function is accessed/created as [%hash: TYPE].

Special support for record fields

Record fields can be annotated with [@hash.ignore] so that they are not incorporated into the computed hash value. In the case of mutable fields, there must be such an annotation.

    type t = {
      mutable s : string [@hash.ignore];
      x : (int * bool) list;
    [@@deriving hash]

Special support for ppx_compare

The annotation [@compare.ignore] implies [@no_hashing], in order to preserve the invariant that compare x y = 0 implies hash x = hash y.

Adapting code to ppx_hash

So long as all types in support hashing, the following common pattern:

    module T = struct
      type t = <TYPE-EXP> [@@deriving compare, sexp]
      let hash = Hashtbl.hash
    include T
    include Hashable.Make (T)

Can this be written as:

    module T = struct
      type t = <TYPE-EXP> [@@deriving compare, hash, sexp]
    include T
    include Hashable.Make (T)

More information

You can’t perform that action at this time.