Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file comparable_intf.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223open!ImportmoduletypeInfix=Comparisons.InfixmoduletypePolymorphic_compare=Comparisons.SmoduleSign=Sign0(** @canonical Base.Sign *)moduletypeWith_compare=sig(** Various combinators for [compare] and [equal] functions. *)(** [lexicographic cmps x y] compares [x] and [y] lexicographically using functions in the
list [cmps]. *)vallexicographic:('a->'a->int)list->'a->'a->int(** [lift cmp ~f x y] compares [x] and [y] by comparing [f x] and [f y] via [cmp]. *)vallift:('a->'a->'result)->f:('b->'a)->'b->'b->'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 (>=)]. *)valreverse:('a->'a->'result)->'a->'a->'result(** The functions below are analogues of the type-specific functions exported by the
[Comparable.S] interface. *)valequal:('a->'a->int)->'a->'a->boolvalmax:('a->'a->int)->'a->'a->'avalmin:('a->'a->int)->'a->'a->'aendmoduletypeWith_zero=sigtypetvalis_positive:t->boolvalis_non_negative:t->boolvalis_negative:t->boolvalis_non_positive:t->bool(** Returns [Neg], [Zero], or [Pos] in a way consistent with the above functions. *)valsign:t->Sign.tendmoduletypeS=sigincludePolymorphic_compare(** [ascending] is identical to [compare]. [descending x y = ascending y x]. These are
intended to be mnemonic when used like [List.sort ~compare:ascending] and [List.sort
~cmp:descending], since they cause the list to be sorted in ascending or descending
order, respectively. *)valascending:t->t->intvaldescending:t->t->int(** [between t ~low ~high] means [low <= t <= high] *)valbetween:t->low:t->high:t->bool(** [clamp_exn t ~min ~max] returns [t'], the closest value to [t] such that
[between t' ~low:min ~high:max] is true.
Raises if [not (min <= max)]. *)valclamp_exn:t->min:t->max:t->tvalclamp:t->min:t->max:t->tOr_error.tincludeComparator.Swithtypet:=tend(** Usage example:
{[
module Foo : sig
type t = ...
include Comparable.S with type t := t
end
]}
Then use [Comparable.Make] in the struct (see comparable.mli for an example). *)moduletypeComparable=sig(** 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)
]} *)moduletypeInfix=InfixmoduletypeS=SmoduletypePolymorphic_compare=Polymorphic_comparemoduletypeWith_compare=With_comparemoduletypeWith_zero=With_zeroincludeWith_compare(** Derive [Infix] or [Polymorphic_compare] functions from just [[@@deriving compare]],
without need for the [sexp_of_t] required by [Make*] (see below). *)moduleInfix(T:sigtypet[@@deriving_inlinecompare]includePpx_compare_lib.Comparable.Swithtypet:=t[@@@end]end):Infixwithtypet:=T.tmodulePolymorphic_compare(T:sigtypet[@@deriving_inlinecompare]includePpx_compare_lib.Comparable.Swithtypet:=t[@@@end]end):Polymorphic_comparewithtypet:=T.t(** Inherit comparability from a component. *)moduleInherit(C:sigtypet[@@deriving_inlinecompare]includePpx_compare_lib.Comparable.Swithtypet:=t[@@@end]end)(T:sigtypet[@@deriving_inlinesexp_of]valsexp_of_t:t->Sexplib0.Sexp.t[@@@end]valcomponent:t->C.tend):Swithtypet:=T.tmoduleMake(T:sigtypet[@@deriving_inlinecompare,sexp_of]includePpx_compare_lib.Comparable.Swithtypet:=tvalsexp_of_t:t->Sexplib0.Sexp.t[@@@end]end):Swithtypet:=T.tmoduleMake_using_comparator(T:sigtypet[@@deriving_inlinesexp_of]valsexp_of_t:t->Sexplib0.Sexp.t[@@@end]includeComparator.Swithtypet:=tend):Swithtypet:=T.twithtypecomparator_witness:=T.comparator_witnessmodulePoly(T:sigtypet[@@deriving_inlinesexp_of]valsexp_of_t:t->Sexplib0.Sexp.t[@@@end]end):Swithtypet:=T.tmoduleWith_zero(T:sigtypet[@@deriving_inlinecompare,sexp_of]includePpx_compare_lib.Comparable.Swithtypet:=tvalsexp_of_t:t->Sexplib0.Sexp.t[@@@end]valzero:tend):sigincludeWith_zerowithtypet:=T.tendend