package core_kernel
Module Core_kernel.Comparable
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)module type Infix = Comparable.Infixmodule type Map_and_set_binable = Comparable.Map_and_set_binablemodule type Polymorphic_compare = Comparable.Polymorphic_comparemodule type S_plain = Comparable.S_plainmodule type S = Comparable.Smodule type S_binable = Comparable.S_binablemodule type S_common = Comparable.S_commonmodule type Validate = Comparable.Validatemodule type With_zero = Comparable.With_zerolexicographic 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
Note: The Comparable.S interface exports both ascending and descending comparisons, so in most cases, it's better to use those.
Inherit comparability from a component.
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 (T : sig ... end) : sig ... endmodule Make_plain_using_comparator (T : sig ... end) : sig ... endmodule Make_using_comparator (T : sig ... end) : sig ... endmodule Make_binable (T : sig ... end) : sig ... endmodule Make_binable_using_comparator (T : sig ... end) : sig ... endmodule Extend (M : Base.Comparable.S) (X : sig ... end) : sig ... endmodule Extend_binable (M : Base.Comparable.S) (X : sig ... end) : sig ... endmodule Map_and_set_binable (T : sig ... end) : sig ... endmodule Map_and_set_binable_using_comparator (T : sig ... end) : sig ... endmodule Validate_with_zero (T : sig ... end) : sig ... endThe following module types and functors may be used to define stable modules:
module Stable : sig ... end