Module Base.Comparable

Defines functors for making modules comparable.

Usage example:

module Foo = struct
  module T = struct
    type t = ... [@@deriving compare, sexp]
  end
  include T
  include Comparable.Make (T)
end

Then include Comparable.S in the signature

module Foo : sig
  type t = ...
  include Comparable.S with type t := t
end

To add an Infix submodule:

module C = Comparable.Make (T)
include C
module Infix = (C : Comparable.Infix with type t := t)

A common pattern is to define a module O with a restricted signature. It aims to be (locally) opened to bring useful operators into scope without shadowing unexpected variable names. E.g., in the Date module:

module O = struct
  include (C : Comparable.Infix with type t := t)
  let to_string t = ..
end

Opening Date would shadow now, but opening Date.O doesn't:

let now = .. in
let someday = .. in
Date.O.(now > someday)
module type Infix = Comparisons.Infix
module type S = sig ... end
module type Comparisons = Comparisons.S
module type With_compare = sig ... end

Various combinators for compare and equal functions.

module type With_zero = sig ... end
include With_compare
val lexicographic : ('a -> 'a -> int) list -> 'a -> 'a -> int

lexicographic cmps x y compares x and y lexicographically using functions in the list cmps.

val lift : ('a -> 'a -> 'result) -> f:('b -> 'a) -> 'b -> 'b -> 'result

lift cmp ~f x y compares x and y by comparing f x and f y via cmp.

val reverse : ('a -> 'a -> 'result) -> 'a -> 'a -> 'result

reverse cmp x y = cmp y x

Reverses the direction of asymmetric relations by swapping their arguments. Useful, e.g., for relations implementing "is a subset of" or "is a descendant of".

Where reversed relations are already provided, use them directly. For example, Comparable.S provides ascending and descending, which are more readable as a pair than compare and reverse compare. Similarly, <= is more idiomatic than reverse (>=).

type 'a reversed = 'a

reversed is the identity type but its associated compare function is the same as the reverse function above. It allows you to get reversed comparisons with ppx_compare, writing, for example, [%compare: string Comparable.reversed] to have strings ordered in the reverse order.

val compare_reversed : ('a -> 'a -> int) -> 'a reversed -> 'a reversed -> int

The functions below are analogues of the type-specific functions exported by the Comparable.S interface.

val equal : ('a -> 'a -> int) -> 'a -> 'a -> bool
val max : ('a -> 'a -> int) -> 'a -> 'a -> 'a
val min : ('a -> 'a -> int) -> 'a -> 'a -> 'a

Derive Infix or Comparisons functions from just [@@deriving compare], without need for the sexp_of_t required by Make* (see below).

module Infix (T : sig ... end) : Infix with type t := T.t
module Comparisons (T : sig ... end) : Comparisons with type t := T.t
module Inherit (C : sig ... end) (T : sig ... end) : S with type t := T.t

Inherit comparability from a component.

module Make (T : sig ... end) : S with type t := T.t
module Make_using_comparator (T : sig ... end) : S with type t := T.t with type comparator_witness := T.comparator_witness
module Poly (T : sig ... end) : S with type t := T.t
module With_zero (T : sig ... end) : sig ... end