package ppx_hash

  1. Overview
  2. Docs
A ppx rewriter that generates hash functions from type expressions and definitions

Install

Dune Dependency

Authors

Maintainers

Sources

ppx_hash-v0.9.0.tar.gz
sha256=878f2ef0312d27cd14ed491e56f0a7e696710dc43bbf16e0f35cd6446f5a676d
md5=44b6a5acdd7eabe11348499f4ef2a054

Description

Part of the Jane Street's PPX rewriters collection.

Published: 22 Mar 2017

README

title: ppx_hash parent: ../README.md

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

Syntax

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

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, as a wrapper around the hash-fold function.. (named hash_<T> when != "t")

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

Signature

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 [@no_hashing] 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; [@no_hashing]
      x : (int * bool) list;
    } [@@deriving hash]

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 sexp, compare]
      let hash = Hashtbl.hash
    end
    include T
    include Hashable.Make(T)

Can this be written as:

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

More information

ppx/ppx_hash/doc/design.notes
ppx/ppx_hash/runtime-lib/make_std.ml

Dependencies (10)

  1. ocaml-migrate-parsetree >= "0.4" & < "2.0.0"
  2. ppx_type_conv >= "v0.9" & < "v0.10"
  3. ppx_sexp_conv >= "v0.9" & < "v0.10"
  4. ppx_metaquot >= "v0.9" & < "v0.10"
  5. ppx_driver >= "v0.9" & < "v0.10"
  6. ppx_core >= "v0.9" & < "v0.10"
  7. ppx_compare >= "v0.9" & < "v0.10"
  8. jbuilder >= "1.0+beta4"
  9. base >= "v0.9" & < "v0.10"
  10. ocaml >= "4.03.0"

Dev Dependencies

None

Used by (8)

  1. boomerang
  2. core_kernel >= "v0.9.0" & < "v0.10.0"
  3. data-encoding >= "0.6"
  4. farith
  5. nsq >= "0.5.0" & < "0.5.2"
  6. opine
  7. ppx_bap < "v0.14.0"
  8. ppx_base < "v0.10.0"

Conflicts

None

OCaml

Innovation. Community. Security.