| 1 | ppx_hash |
| 2 | ======== |
| 3 | |
| 4 | A ppx writer that generates hash functions from type expressions and definitions. |
| 5 | |
| 6 | Syntax |
| 7 | ------ |
| 8 | |
| 9 | Type definitions: `[@@deriving hash]` |
| 10 | Expressions: `[%hash_fold: TYPE]` and `[%hash: TYPE]` |
| 11 | Types, record fields: `[@hash.ignore]` |
| 12 | |
| 13 | Basic usage |
| 14 | ----------- |
| 15 | |
| 16 | ```ocaml |
| 17 | type t = { |
| 18 | s : string; |
| 19 | x : (int * bool) list; |
| 20 | } |
| 21 | [@@deriving hash] |
| 22 | ``` |
| 23 | |
| 24 | This will generate a function `hash_fold_t : Hash.state -> t -> Hash.state`. |
| 25 | |
| 26 | Where `Hash` is `Ppx_hash_lib.Std.Hash`. |
| 27 | |
| 28 | The generated function follows the structure of the type; allowing user overrides at every |
| 29 | level. This is in contrast to ocaml's builtin polymorphic hashing `Hashtbl.hash` which |
| 30 | ignores user overrides. |
| 31 | |
| 32 | Also generated is a direct hash-function `hash : t -> |
| 33 | Hash.hash_value`. This function will be named `hash_<T>` when <T> != "t". |
| 34 | |
| 35 | The direct hash function is the one suitable for `Hashable.Make`. |
| 36 | |
| 37 | Signature |
| 38 | --------- |
| 39 | |
| 40 | In a module signature, `type t [@@deriving hash]` will add both `hash_fold_t` and `hash` |
| 41 | (or `hash_<T>`) to the signature. |
| 42 | |
| 43 | Hashing without a type definition |
| 44 | --------------------------------- |
| 45 | |
| 46 | A folding hash function is accessed/created as `[%hash_fold: TYPE]`. |
| 47 | A direct hash function is accessed/created as `[%hash: TYPE]`. |
| 48 | |
| 49 | Ignoring part of types |
| 50 | ---------------------- |
| 51 | |
| 52 | Types can be annotated with `[@hash.ignore]` so that they are not |
| 53 | incorporated into the computed hash value. |
| 54 | |
| 55 | ```ocaml |
| 56 | type second_only = (string [@hash.ignore]) * int [@@deriving hash] |
| 57 | ``` |
| 58 | |
| 59 | Mutable records fields must have such an annotation. |
| 60 | |
| 61 | ```ocaml |
| 62 | type t = { |
| 63 | mutable s : string [@hash.ignore]; |
| 64 | x : (int * bool) list; |
| 65 | } |
| 66 | [@@deriving hash] |
| 67 | ``` |
| 68 | |
| 69 | Special support for `ppx_compare` |
| 70 | --------------------------------- |
| 71 | |
| 72 | The annotation `[@compare.ignore]` (and `[@ignore]`) implies |
| 73 | `[@hash.ignore]`, in order to preserve the invariant that `compare x y |
| 74 | = 0` implies `hash x = hash y`. |
| 75 | |
| 76 | Adapting code to `ppx_hash` |
| 77 | --------------------------- |
| 78 | |
| 79 | So long as all types in <TYPE-EXP> support hashing, the following common pattern: |
| 80 | |
| 81 | ```ocaml |
| 82 | module T = struct |
| 83 | type t = <TYPE-EXP> [@@deriving compare, sexp] |
| 84 | let hash = Hashtbl.hash |
| 85 | end |
| 86 | include T |
| 87 | include Hashable.Make (T) |
| 88 | ``` |
| 89 | |
| 90 | Can this be written as: |
| 91 | |
| 92 | ```ocaml |
| 93 | module T = struct |
| 94 | type t = <TYPE-EXP> [@@deriving compare, hash, sexp] |
| 95 | end |
| 96 | include T |
| 97 | include Hashable.Make (T) |
| 98 | ``` |
| 99 | |
| 100 | More information |
| 101 | ---------------- |
| 102 | |
| 103 | ppx/ppx_hash/doc/design.notes |
| 104 | ppx/ppx_hash/runtime-lib/make_std.ml |