package core
Module Core.ComparableSource
Comparable extends Base.Comparable and provides functions for comparing like types.
Usage example:
module Foo = struct
module T = struct
type t = ... [@@deriving compare, sexp]
end
include T
include Comparable.Make (T)
endThen include Comparable.S in the signature (see Comparable_intf for an example).
To add an Infix submodule:
module C = Comparable.Make (T)
include C
module Infix = (C : Comparable.Infix with type t := t)Common pattern: 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 = ..
endOpening Date would shadow now, but opening Date.O doesn't:
let now = .. in
let someday = .. in
Date.O.(now > someday)include With_compare
lexicographic cmps x y compares x and y lexicographically using functions in the list cmps.
lift cmp ~f x y compares x and y by comparing f x and f y via cmp.
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 (>=).
The functions below are analogues of the type-specific functions exported by the Comparable.S interface.
Inherit comparability from a component.
Comparison-only Functors
These functors require only type t and val compare. They do not require val sexp_of_t, and do not generate container datatypes.
Make Functors
The Comparable Make functor family allows users to choose among the following attributes:
*_using_comparatoror not*_binableor not*_plainor not
Thus there are functors like Make_plain or Make_binable_using_comparator, etc.
module Make_plain_using_comparator
(T : sig ... end) :
S_plain
with type t := T.t
with type comparator_witness := T.comparator_witnessmodule Make_using_comparator
(T : sig ... end) :
S with type t := T.t with type comparator_witness := T.comparator_witnessmodule Make_binable_using_comparator
(T : sig ... end) :
S_binable
with type t := T.t
with type comparator_witness := T.comparator_witnessmodule Extend
(M : Base.Comparable.S)
(X : sig ... end) :
S with type t := M.t with type comparator_witness := M.comparator_witnessmodule Extend_binable
(M : Base.Comparable.S)
(X : sig ... end) :
S_binable
with type t := M.t
with type comparator_witness := M.comparator_witnessmodule Map_and_set_binable_using_comparator
(T : sig ... end) :
Map_and_set_binable
with type t := T.t
with type comparator_witness := T.comparator_witnessThe following module types and functors may be used to define stable modules: