| 1 |
open Core |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
type result = |
| 6 |
| Null |
| 7 |
| Elem of int |
| 8 |
| Combine of result * result |
| 9 |
[@@deriving sexp, compare] |
| 10 |
|
| 11 |
let ( % ) a b = Combine (a, b) |
| 12 |
|
| 13 |
|
| 14 |
module Ppx_hash_lib = struct |
| 15 |
module Std = struct |
| 16 |
module Hash = struct |
| 17 |
type hash_value = result |
| 18 |
type state = result |
| 19 |
|
| 20 |
let fold_int s x = Combine (s, Elem x) |
| 21 |
let get_hash_value = Fn.id |
| 22 |
|
| 23 |
type seed = unit |
| 24 |
|
| 25 |
let create ?seed:_ () = Null |
| 26 |
end |
| 27 |
|
| 28 |
module Builtin = struct |
| 29 |
let hash_fold_int = Hash.fold_int |
| 30 |
end |
| 31 |
end |
| 32 |
end |
| 33 |
|
| 34 |
open Ppx_hash_lib.Std |
| 35 |
open Builtin |
| 36 |
|
| 37 |
module Tree = struct |
| 38 |
type t = |
| 39 |
| L of int |
| 40 |
| N of t * t |
| 41 |
[@@deriving hash] |
| 42 |
end |
| 43 |
|
| 44 |
let a, b = 100, 200 |
| 45 |
let node = 1 |
| 46 |
let leaf = 0 |
| 47 |
let tree = Tree.(N (L a, L b)) |
| 48 |
|
| 49 |
let%test_unit _ = |
| 50 |
let v = Tree.hash tree in |
| 51 |
|
| 52 |
[%test_result: result] |
| 53 |
v |
| 54 |
~expect:(Null % Elem node % Elem leaf % Elem a % Elem leaf % Elem b) |
| 55 |
;; |