| 1 | ppx_compare |
| 2 | =========== |
| 3 | |
| 4 | Generation of fast comparison and equality functions from type |
| 5 | expressions and definitions. |
| 6 | |
| 7 | Ppx_compare is a ppx rewriter that derives comparison and equality |
| 8 | functions from type representations. The scaffolded functions are |
| 9 | usually much faster than ocaml's `Pervasives.compare` and |
| 10 | `Pervasives.(=)`. Scaffolding functions also gives you more |
| 11 | flexibility by allowing you to override them for a specific type and |
| 12 | more safety by making sure that you only compare comparable values. |
| 13 | |
| 14 | Syntax |
| 15 | ------ |
| 16 | |
| 17 | Type definitions: `[@@deriving compare, equal]` |
| 18 | Expressions: `[%compare: TYPE]`, `[%equal: TYPE]` and `[%compare.equal: TYPE]` |
| 19 | Types, record fields: `[@compare.ignore]`, `[@equal.ignore]` |
| 20 | |
| 21 | Basic usage |
| 22 | ----------- |
| 23 | |
| 24 | We use `ppx_deriving`/`ppx_type_conv`, so type definitions are annotated this way: |
| 25 | |
| 26 | ```ocaml |
| 27 | type s = v * w [@@deriving compare] |
| 28 | ``` |
| 29 | |
| 30 | This will generate `compare_s : s -> s -> int` function that relies on |
| 31 | `compare_v : v -> v -> int` and `compare_w : w -> w -> int`. |
| 32 | |
| 33 | Compare is not DWIM (do what I mean): it will scaffold a fast well behaved comparison |
| 34 | (reflexive, transitive, symmetric...) function however it does not try to follow any |
| 35 | "natural ordering". For instance arrays of characters are not sorted lexicographically. |
| 36 | |
| 37 | Base types (options,int,array,lists,char,floats...) have the same comparison order as |
| 38 | Pervasives.compare (provided their type parameters also do for the polymorphic ones). |
| 39 | Comparisons for these types must be brought in scope with `open Base`, `open Core`, or |
| 40 | `open Ppx_compare_lib.Builtin`. |
| 41 | |
| 42 | Records fields are compared in the order they are defined (left to right); tuples fields |
| 43 | are compared left to right. When we compare two branches of a sum whichever ones comes |
| 44 | first in the definition is considered lowest. Variants compare in the order they are |
| 45 | listed (increasing top-to-bottom). Polymorphic variants use the same ordering as the |
| 46 | ocaml runtime. |
| 47 | |
| 48 | The same applies to equality functions. |
| 49 | |
| 50 | ### Float equality |
| 51 | |
| 52 | The functions derived by `[@@deriving equal]` are consistent with the |
| 53 | compare functions derived by `[@@deriving compare]` and in particular |
| 54 | do not respect IEEE float comparison. |
| 55 | |
| 56 | Calling `compare` for type `t`s |
| 57 | ------------------------------- |
| 58 | |
| 59 | In compliance (or conformance) with Janestreet's coding standard we assume that |
| 60 | type named `t` are the main types in a module and |
| 61 | |
| 62 | ```ocaml |
| 63 | type t = S.t * T.t [@@deriving compare] |
| 64 | ``` |
| 65 | |
| 66 | will call the functions `S.compare` and `T.compare` instead of calling `S.compare_t` and |
| 67 | `T.compare_t`. This will also generate a `compare : t -> t -> int` function. |
| 68 | |
| 69 | The same applies to equality functions. |
| 70 | |
| 71 | Signature |
| 72 | --------- |
| 73 | |
| 74 | `type t [@@deriving compare]` in a module signature will add `val compare : t -> t -> int` |
| 75 | in the signature. |
| 76 | |
| 77 | The same applies to equality functions. |
| 78 | |
| 79 | Comparison without a type definition |
| 80 | ------------------------------------ |
| 81 | |
| 82 | Sometimes you just want a comparison without having to create a new type. You can create |
| 83 | such a comparison function using the `[%compare: ..]` extension point: |
| 84 | |
| 85 | ```ocaml |
| 86 | let gt x y = [%compare: float * int * [`A | `B | `C] ] x y |
| 87 | ``` |
| 88 | |
| 89 | You can use the type `_`, in which case the corresponding values will be |
| 90 | ignored (i.e. compared using `fun _ _ -> 0`). For instance: |
| 91 | |
| 92 | ```ocaml |
| 93 | assert ([%compare: _ list] [ true ] [ false ] = 0); |
| 94 | assert ([%compare: _ list] [] [ false ] <> 0); |
| 95 | ``` |
| 96 | |
| 97 | The same applies to equality functions. |
| 98 | |
| 99 | You can also check for equality using `[%compare.equal: ..]`, which |
| 100 | produces a function that returns `true` precisely when `[%compare: |
| 101 | ..]` returns `0`. `[%equal: ..]` is preferred over `[%compare.equal: |
| 102 | ..]` and in particular is expected to be slightly faster. However, |
| 103 | `[%compare.equal: ..]` can come in handy for types that only have |
| 104 | `[@@deriving compare]`. In particular, support for `[@@deriving |
| 105 | equal]` was added long after the project started, which means that |
| 106 | many types out there only support `[@deriving compare]`. |
| 107 | |
| 108 | Ignoring part of types |
| 109 | ---------------------- |
| 110 | |
| 111 | The comparison ignores any part of the type declaration that is under |
| 112 | a `[@compare.ignore]` annotation: |
| 113 | |
| 114 | ```ocaml |
| 115 | type t = (float [@compare.ignore]) * string |
| 116 | [@@deriving compare] |
| 117 | ``` |
| 118 | |
| 119 | The same applies for `[@@deriving equal]` by using |
| 120 | `[@equal.ignore]`. In order to ignore part of a type for both |
| 121 | comparison and equality, you can simply use `[@ignore]`. However, be |
| 122 | aware that the general `[@ignore]` attribute will apply to any deriver |
| 123 | that recognize it, not just `compare` and `equal`. |
| 124 | |
| 125 | Note that if you use both the `compare` and `equal` derivers, you need |
| 126 | to use either both `[@compare.ignore]` and `[@equal.ignore]` or |
| 127 | `[@ignore]`. However, you cannot use only one of them. |
| 128 | |
| 129 | For convenience, you can also ignore record fields instead of the |
| 130 | type of record field. In other words, |
| 131 | |
| 132 | ```ocaml |
| 133 | type t = |
| 134 | { a : (float [@compare.ignore]) |
| 135 | ; b : string |
| 136 | } |
| 137 | [@@deriving compare] |
| 138 | ``` |
| 139 | |
| 140 | can be abbreviated: |
| 141 | |
| 142 | ```ocaml |
| 143 | type t = |
| 144 | { a : float [@compare.ignore] |
| 145 | ; b : string |
| 146 | } |
| 147 | [@@deriving compare] |
| 148 | ``` |
| 149 | |
| 150 | Local-accepting compare functions |
| 151 | ---------------------------- |
| 152 | |
| 153 | This ppx includes the option to support local allocation, a nonstandard OCaml extension |
| 154 | available at: https://github.com/ocaml-flambda/ocaml-jst |
| 155 | |
| 156 | In both structures and signatures, `[@@deriving compare ~localize]` (and similarly for |
| 157 | `equal`) generates definitions with the following types, in addition to the usual |
| 158 | definitions: |
| 159 | |
| 160 | ``` |
| 161 | (* Monomorphic types *) |
| 162 | val compare__local : local_ t -> local_ t -> int |
| 163 | val equal__local : local_ t -> local_ t -> bool |
| 164 | |
| 165 | (* Parameterized types *) |
| 166 | val compare__local |
| 167 | : (local_ 'a -> local_ 'a -> int) |
| 168 | -> local_ 'a t |
| 169 | -> local_ 'a t |
| 170 | -> int |
| 171 | val equal__local |
| 172 | : (local_ 'a -> local_ 'a -> bool) |
| 173 | -> local_ 'a t |
| 174 | -> local_ 'a t |
| 175 | -> bool |
| 176 | ``` |
| 177 | |
| 178 | You can also use the `[%compare_local: _]`, `[%equal_local: _]` and |
| 179 | `[%compare_local.equal: _]` extension points to generate the corresponding |
| 180 | types and functions. |
| 181 | |
| 182 | For types named something other than `t`, the naming pattern is similar |
| 183 | to the non-local versions: |
| 184 | |
| 185 | ```ocaml |
| 186 | type foo |
| 187 | |
| 188 | val compare_foo__local : local_ foo -> local_ foo -> int |
| 189 | ``` |