package ppx_compare

  1. Overview
  2. Docs
Generation of comparison functions from types

Install

Dune Dependency

Authors

Maintainers

Sources

ppx_compare-v0.9.0.tar.gz
sha256=6c5cb77417486535576ec2198fe21186f8357408873a7f834304c46401a3877f
md5=0ba4d7bb540cddafa73919100e7e89d8

Description

Part of the Jane Street's PPX rewriters collection.

Published: 22 Mar 2017

README

title: ppx_compare parent: ../README.md

Generation of fast comparison functions from type expressions and definitions.

Ppx_compare is a ppx rewriter that derives comparison functions from type representations. The scaffolded functions are usually much faster than ocaml's Pervasives.compare. Scaffolding functions also gives you more flexibility by allowing you to override them for a specific type and more safety by making sure that you only compare comparable values.

We use ppx_deriving/ppx_type_conv, so type definitions are annotated this way:

type s = v * w [@@deriving compare]

This will generate compare_s : s -> s -> int function that relies on compare_v : v -> v -> int and compare_w : w -> w -> int.

Compare is not DWIM (do what I mean): it will scaffold a fast well behaved comparison (reflexive, transitive, symmetric...) function however it does not try to follow any "natural ordering". For instance arrays of characters are not sorted lexicographically.

Base types (options,int,array,lists,char,floats...) have the same comparison order than Pervasives.compare (provided their type parameters also do for the polymorphic ones).

Records fields are compared in the order they are defined (left to right); tuples fields are compared left to right. When we compare two branches of a sum whichever ones comes first in the definition is considered lowest. Polymorphic variants use the same ordering as the ocaml runtime.

Calling compare for type ts

In compliance (or conformance) with Janestreet's coding standard we assume that type named t are the main types in a module and

type t = S.t * T.t [@@deriving compare]

will call the functions S.compare and T.compare instead of calling S.compare_t and T.compare_t. This will also generate a compare : t -> t -> int function.

Signature

type t [@@deriving compare] in a module signature will add val compare : t -> t -> int in the signature.

Comparison without a type definition

Sometimes you just want a comparison without having to create a new type. You can create such a comparison function using the [%compare: ..] extension point:

let gt x y = [%compare: float * int * [`A | `B | `C] ] x y

You can also check for equality using [%compare.equal: ..], which produces a function that returns true precisely when [%compare: ..] returns 0.

Dependencies (8)

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

Dev Dependencies

None

Used by (17)

  1. bin_prot >= "v0.9.0" & < "v0.10.0"
  2. biocaml >= "0.4.0"
  3. camlix
  4. frenetic >= "5.0.0" & < "5.0.5"
  5. h1_parser
  6. http
  7. inferno >= "20220603"
  8. little_logger < "0.2.0"
  9. nsq >= "0.2.5" & < "0.5.2"
  10. oci
  11. opine
  12. parsexp < "v0.10.0"
  13. ppx_assert = "v0.9.0"
  14. ppx_bap < "v0.14.0"
  15. ppx_base < "v0.10.0"
  16. ppx_expect = "v0.9.0"
  17. ppx_hash < "v0.10.0"

Conflicts

None

OCaml

Innovation. Community. Security.