123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289open!ImportmoduletypeInfix=Base.Comparable.InfixmoduletypePolymorphic_compare=Base.Comparable.Polymorphic_comparemoduletypeWith_compare=Base.Comparable.With_comparemoduletypeValidate=sigtypetvalvalidate_lbound:min:tMaybe_bound.t->tValidate.checkvalvalidate_ubound:max:tMaybe_bound.t->tValidate.checkvalvalidate_bound:min:tMaybe_bound.t->max:tMaybe_bound.t->tValidate.checkendmoduletypeValidate_with_zero=sigtypetincludeValidatewithtypet:=tvalvalidate_positive:tValidate.checkvalvalidate_non_negative:tValidate.checkvalvalidate_negative:tValidate.checkvalvalidate_non_positive:tValidate.checkendmoduletypeWith_zero=sigtypetincludeBase.Comparable.With_zerowithtypet:=tincludeValidate_with_zerowithtypet:=tendmoduletypeS_common=sigincludeBase.Comparable.SincludeValidatewithtypet:=tmoduleReplace_polymorphic_compare:Polymorphic_comparewithtypet:=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). *)moduletypeS_plain=sigincludeS_commonmoduleMap:Map.S_plainwithtypeKey.t=twithtypeKey.comparator_witness=comparator_witnessmoduleSet:Set.S_plainwithtypeElt.t=twithtypeElt.comparator_witness=comparator_witnessendmoduletypeS=sigincludeS_commonmoduleMap:Map.SwithtypeKey.t=twithtypeKey.comparator_witness=comparator_witnessmoduleSet:Set.SwithtypeElt.t=twithtypeElt.comparator_witness=comparator_witnessendmoduletypeMap_and_set_binable=sigtypetincludeComparator.Swithtypet:=tmoduleMap:Map.S_binablewithtypeKey.t=twithtypeKey.comparator_witness=comparator_witnessmoduleSet:Set.S_binablewithtypeElt.t=twithtypeElt.comparator_witness=comparator_witnessendmoduletypeS_binable=sigincludeS_commonincludeMap_and_set_binablewithtypet:=twithtypecomparator_witness:=comparator_witnessendmoduletypeComparable=sig(** Comparable extends {{!Base.Comparable}[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)
end
]}
Then 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 = ..
end
]}
Opening [Date] would shadow [now], but opening [Date.O] doesn't:
{[
let now = .. in
let someday = .. in
Date.O.(now > someday)
]}
*)moduletypeInfix=InfixmoduletypeMap_and_set_binable=Map_and_set_binablemoduletypePolymorphic_compare=Polymorphic_comparemoduletypeS_plain=S_plainmoduletypeS=SmoduletypeS_binable=S_binablemoduletypeS_common=S_commonmoduletypeValidate=ValidatemoduletypeValidate_with_zero=Validate_with_zeromoduletypeWith_compare=With_comparemoduletypeWith_zero=With_zeroincludeWith_compare(** Inherit comparability from a component. *)moduleInherit(C:sigtypet[@@derivingcompare]end)(T:sigtypet[@@derivingsexp]valcomponent:t->C.tend):Swithtypet:=T.t(** {2 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.
*)moduleInfix(T:sigtypet[@@derivingcompare]end):Infixwithtypet:=T.tmodulePolymorphic_compare(T:sigtypet[@@derivingcompare]end):Polymorphic_comparewithtypet:=T.t(** {2 Make Functors}
The Comparable Make functor family allows users to choose among the following
attributes:
- [*_using_comparator] or not
- [*_binable] or not
- [*_plain] or not
Thus there are functors like [Make_plain] or [Make_binable_using_comparator], etc.
*)moduleMake_plain(T:sigtypet[@@derivingcompare,sexp_of]end):S_plainwithtypet:=T.tmoduleMake(T:sigtypet[@@derivingcompare,sexp]end):Swithtypet:=T.tmoduleMake_plain_using_comparator(T:sigtypet[@@derivingsexp_of]includeComparator.Swithtypet:=tend):S_plainwithtypet:=T.twithtypecomparator_witness:=T.comparator_witnessmoduleMake_using_comparator(T:sigtypet[@@derivingsexp]includeComparator.Swithtypet:=tend):Swithtypet:=T.twithtypecomparator_witness:=T.comparator_witnessmoduleMake_binable(T:sigtypet[@@derivingbin_io,compare,sexp]end):S_binablewithtypet:=T.tmoduleMake_binable_using_comparator(T:sigtypet[@@derivingbin_io,sexp]includeComparator.Swithtypet:=tend):S_binablewithtypet:=T.twithtypecomparator_witness:=T.comparator_witnessmoduleExtend(M:Base.Comparable.S)(X:sigtypet=M.t[@@derivingsexp]end):Swithtypet:=M.twithtypecomparator_witness:=M.comparator_witnessmoduleExtend_binable(M:Base.Comparable.S)(X:sigtypet=M.t[@@derivingbin_io,sexp]end):S_binablewithtypet:=M.twithtypecomparator_witness:=M.comparator_witnessmoduleMap_and_set_binable(T:sigtypet[@@derivingbin_io,compare,sexp]end):Map_and_set_binablewithtypet:=T.tmoduleMap_and_set_binable_using_comparator(T:sigtypet[@@derivingbin_io,compare,sexp]includeComparator.Swithtypet:=tend):Map_and_set_binablewithtypet:=T.twithtypecomparator_witness:=T.comparator_witnessmodulePoly(T:sigtypet[@@derivingsexp]end):Swithtypet:=T.tmoduleValidate(T:sigtypet[@@derivingcompare,sexp_of]end):Validatewithtypet:=T.tmoduleValidate_with_zero(T:sigtypet[@@derivingcompare,sexp_of]valzero:tend):Validate_with_zerowithtypet:=T.tmoduleWith_zero(T:sigtypet[@@derivingcompare,sexp_of]valzero:tend):With_zerowithtypet:=T.t(** The following module types and functors may be used to define stable modules: *)moduleStable:sigmoduleV1:sigmoduletypeS=sigtypecomparabletypecomparator_witnessmoduleMap:Map.Stable.V1.Swithtypekey:=comparablewithtypecomparator_witness:=comparator_witnessmoduleSet:Set.Stable.V1.Swithtypeelt:=comparablewithtypeelt_comparator_witness:=comparator_witnessendmoduleMake(X:Stable_module_types.S0):Swithtypecomparable:=X.twithtypecomparator_witness:=X.comparator_witnessendendend