| 1 |
|
| 2 |
Design for [ppx_hash] syntax extension: |
| 3 |
|
| 4 |
(1) [@@deriving hash] |
| 5 |
|
| 6 |
From: type t = ... [@@deriving hash] |
| 7 |
|
| 8 |
Generate a folding-style function. |
| 9 |
[hash_fold_t] : [Hash.state -> t -> Hash.state] |
| 10 |
|
| 11 |
And generate a direct-style function. |
| 12 |
[hash] : [t -> Hash.hash_value] (named [hash_<T>] when <T> != "t") |
| 13 |
|
| 14 |
where [Hash] is [Ppx_hash_lib.Std.Hash]. |
| 15 |
|
| 16 |
The folding-style function [hash_fold_<T>] function is compositional, following the |
| 17 |
structure of the type; allowing user overrides at every level. This is in contrast to |
| 18 |
ocaml's builtin polymorphic hashing [Hashtbl.hash] which ignores user overrides. |
| 19 |
|
| 20 |
The direct-style function is a wrapper around the folding-style function, and is not used |
| 21 |
in a compositional way. |
| 22 |
|
| 23 |
[hash_fold_t state x] is supposed to disturb the [state] by mixing in all the |
| 24 |
information present in [x]. It should not discard [state]. |
| 25 |
|
| 26 |
To have collision resistance, it should expand to different sequences of |
| 27 |
built-in mixing functions for different values of [x]. No such sequence is |
| 28 |
allowed to be a prefix of another. |
| 29 |
|
| 30 |
We also support the inline syntax extensions [%hash_fold: TYPE] and [%hash: TYPE] |
| 31 |
|
| 32 |
(2) [Hash.state], [Hash.hash_value], [Hash.seed] |
| 33 |
|
| 34 |
The [ppx_hash] extension is not tied to any specific hash-function, with the generated |
| 35 |
code making no assumptions over the detail of these types. |
| 36 |
|
| 37 |
These types are defined by the specific hash-function selected as [Ppx_hash_lib.Std.Hash] |
| 38 |
We have conservatively selected to use the builtin hash-function defined internally in |
| 39 |
ocaml - which we refer to as "internalhash" - (but used in a compositional way). |
| 40 |
|
| 41 |
This hash-function has: [type seed = int] [type hash_value = int] |
| 42 |
[Hash.state] is abstract, but is an immediate value, so avoiding allocation issues. |
| 43 |
|
| 44 |
(3) User interface: |
| 45 |
|
| 46 |
Normally hashing is performed with the generated direct-style hash function: |
| 47 |
hash x |
| 48 |
|
| 49 |
Alternatively, the generated folding-style function can be run with [Hash.run]: |
| 50 |
(In addition, this allows a non-default seed to be passed.) |
| 51 |
Hash.run ?seed hash_fold_t x |
| 52 |
|
| 53 |
Or we can use the syntax extensions: |
| 54 |
|
| 55 |
[%hash: T] x |
| 56 |
Hash.run ?seed [%hash_fold: T] x |
| 57 |
|
| 58 |
(4) Code generation: |
| 59 |
|
| 60 |
The generated code follows the structure of the type. |
| 61 |
|
| 62 |
For leaf types, (in these examples [a], [b] and [c]), the generated function expects the |
| 63 |
corresponding hash_fold function ([hash_fold_a], [hash_fold_b] and [hash_fold_c]) to be in |
| 64 |
scope, accompanying the types in scope. |
| 65 |
|
| 66 |
Tuples: |
| 67 |
|
| 68 |
type t1 = a * b * c [@@deriving hash] |
| 69 |
|
| 70 |
let hash_fold_t1 : Hash.t -> t1 -> Hash.t = |
| 71 |
fun hsv -> |
| 72 |
fun arg -> |
| 73 |
let (e0,e1,e2) = arg in |
| 74 |
hash_fold_c (hash_fold_b (hash_fold_a hsv e0) e1) e2 |
| 75 |
|
| 76 |
Records: |
| 77 |
|
| 78 |
type t2 = {a: a; b: b; c: c;} [@@deriving hash] |
| 79 |
|
| 80 |
let hash_fold_t2 : Hash.t -> t2 -> Hash.t = |
| 81 |
fun hsv -> |
| 82 |
fun arg -> |
| 83 |
hash_fold_c (hash_fold_b (hash_fold_a hsv arg.a) arg.b) arg.c |
| 84 |
|
| 85 |
|
| 86 |
For variants, we also take account of (the position of) the variant tag: |
| 87 |
|
| 88 |
type t3 = Foo | Bar of a | Qaz of b * c [@@deriving hash] |
| 89 |
|
| 90 |
let hash_fold_t3 : Hash.t -> t3 -> Hash.t = |
| 91 |
fun hsv -> |
| 92 |
fun arg -> |
| 93 |
match arg with |
| 94 |
| Foo -> Hash.fold_int hsv 0 |
| 95 |
| Bar a0 -> hash_fold_a (Hash.fold_int hsv 1) a0 |
| 96 |
| Qaz (a0,a1) -> hash_fold_c (hash_fold_b (Hash.fold_int hsv 2) a0) a1 |
| 97 |
|
| 98 |
|
| 99 |
For polymorphic-variants, we use the ocaml hash value of the polymorphic-variant tag, |
| 100 |
returned by [Btype.hash_variant]: |
| 101 |
|
| 102 |
type t4 = [ `Foo of a | `Bar ] [@@deriving hash] |
| 103 |
|
| 104 |
let hash_fold_t4 : Hash.t -> t4 -> Hash.t = |
| 105 |
fun hsv -> |
| 106 |
fun arg -> |
| 107 |
match arg with |
| 108 |
| `Foo _v -> hash_fold_a (Hash.fold_int hsv 3505894) _v |
| 109 |
| `Bar -> Hash.fold_int hsv 3303859 |
| 110 |
|
| 111 |
|
| 112 |
For parametrised types we generate a hash function parametrised over the hash function |
| 113 |
for the element type (nothing new here). |
| 114 |
|
| 115 |
type 'a t5 = ('a * 'a) list [@@deriving hash] |
| 116 |
|
| 117 |
let hash_fold_t5 : 'a . (Hash.t -> 'a -> Hash.t) -> Hash.t -> 'a t5 -> Hash.t = |
| 118 |
fun _hash_fold_a -> |
| 119 |
fun hsv -> |
| 120 |
fun arg -> |
| 121 |
hash_fold_list |
| 122 |
(fun hsv -> |
| 123 |
fun arg -> |
| 124 |
let (e0,e1) = arg in _hash_fold_a (_hash_fold_a hsv e0) e1) |
| 125 |
hsv arg |
| 126 |
|
| 127 |
|
| 128 |
(5) Special support for record fields: |
| 129 |
|
| 130 |
Record fields can be annotated with [@hash.ignore] so that they are not |
| 131 |
incorporated into the computed hash value. In the case of mutable fields, there |
| 132 |
must be such an annotation. |
| 133 |
|
| 134 |
type t = { |
| 135 |
mutable s : string; [@hash.ignore] |
| 136 |
i : int; |
| 137 |
} [@@deriving hash] |
| 138 |
|
| 139 |
let hash_fold_t : Hash.t -> t -> Hash.t = |
| 140 |
fun hsv -> fun arg -> hash_fold_int hsv arg.i |
| 141 |
|
| 142 |
|
| 143 |
(6) Support for builtins: |
| 144 |
|
| 145 |
We do nothing special for built-in types such as [int] or [float], or build-in type |
| 146 |
constructors such as [list] and [option]. We just expect the corresponding [hash_fold_<T>] |
| 147 |
functions to be in scope. |
| 148 |
|
| 149 |
This is the same approach as taken by sexp-conv, but different from ppx_compare, which |
| 150 |
does treat built-in types & constructors specially, leading to buggy behaviour when those |
| 151 |
names are redefined. |
| 152 |
|
| 153 |
A runtime library defines the hash functions for the built-in types & constructors, and is |
| 154 |
put in scope by [open Core] as is done for built-in sexp converters. |
| 155 |
|
| 156 |
type 'a folder = Hash.t -> 'a -> Hash.t |
| 157 |
|
| 158 |
val hash_fold_nativeint : nativeint folder |
| 159 |
val hash_fold_int64 : int64 folder |
| 160 |
val hash_fold_int32 : int32 folder |
| 161 |
val hash_fold_char : char folder |
| 162 |
val hash_fold_int : int folder |
| 163 |
val hash_fold_bool : bool folder |
| 164 |
val hash_fold_string : string folder |
| 165 |
val hash_fold_float : float folder |
| 166 |
val hash_fold_unit : unit folder |
| 167 |
|
| 168 |
val hash_fold_option : 'a folder -> 'a option folder |
| 169 |
val hash_fold_list : 'a folder -> 'a list folder |
| 170 |
val hash_fold_lazy_t : 'a folder -> 'a lazy_t folder |
| 171 |
|
| 172 |
|
| 173 |
(7) Array/Ref: |
| 174 |
|
| 175 |
Hash support for [array] and [ref] is not provided directly, because of the danger |
| 176 |
when hashing mutable values: the computed hash changes when the value mutates. |
| 177 |
|
| 178 |
Instead we provide [.._frozen] type aliases, with the corresponding [hash_fold_<T>] function. |
| 179 |
|
| 180 |
type 'a ref_frozen = 'a ref |
| 181 |
type 'a array_frozen = 'a array |
| 182 |
|
| 183 |
val hash_fold_ref_frozen : 'a folder -> 'a ref folder |
| 184 |
val hash_fold_array_frozen : 'a folder -> 'a array folder |
| 185 |
|
| 186 |
These are not safe if the ref/array value hashed is mutated. |
| 187 |
|
| 188 |
|
| 189 |
(8) [lazy_t]: |
| 190 |
|
| 191 |
We avoid the bug in ocaml's internal function on [lazy_t] values, by defining: |
| 192 |
|
| 193 |
let hash_fold_lazy_t hash_fold_elem s x = |
| 194 |
hash_fold_elem s (Lazy.force x) |
| 195 |
|
| 196 |
(9) GADTs: |
| 197 |
|
| 198 |
GADTs are not explicitly supported. Some examples will be fine, but examples with |
| 199 |
existential types wont work and will generate ill-typed code. We make no attempt to |
| 200 |
distinguish the cases. |