Module NS.Comparer

Singleton types for compare functions

type ('a, 'compare_a) t = private 'a -> 'a -> int

A comparer ('a, 'compare_a) t for type 'a is a "compare" function of type 'a -> 'a -> int tagged with a phantom type 'compare_a acting as a singleton type denoting an individual compare function.

module type S = sig ... end
module Make (Ord : sig ... end) : S with type t = Ord.t

Make takes a compare function, mints a fresh compare type to act as a singleton type denoting that one compare function, and returns the compare function at a type stamped with its singleton type. In this way, Make applied to two different compare functions for the same type of values yields comparers with incompatible types.

module Counterfeit (Ord : sig ... end) : S with type t = Ord.t with type compare = Ord.compare

Counterfeit takes a compare function and type and yields a comparer that asserts that the given compare type is a singleton for the given compare function. This is not checked by the type system. It is the client's responsibility to ensure that distinct types are provided for distinct compare functions. If the same type is used for multiple functions, then Counterfeit will produce type-compatible comparers even though the wrapped compare functions differ.

module Apply (F : sig ... end) (A : S) : sig ... end

Apply (F) (A) takes a type ('a, 'compare_a) F.t with a type parameter 'a and a compare type 'compare_a for 'a, and a comparer A, and creates a comparer for F.t with 'a instantiated to A.t.

module type S1 = sig ... end
module Apply1 (F : sig ... end) (A : S) : sig ... end

Apply1 (F) (A) takes a type ('a, 'b, 'compare_a) F.t with two type parameters 'a, 'b and a compare type 'compare_a for 'a, and a comparer A, and creates a comparer for F.t with 'a instantiated to A.t.