package tezos-stdlib

  1. Overview
  2. Docs

Compare

Monomorphic comparison for common ground types and common type constructors.

Compare provides a module signature for the standard comparison functions and operators as well as modules of that signature for the common OCaml ground types (int, bool, etc.) and type constructors (list, option, etc.).

Signatures and a functor

module type COMPARABLE = sig ... end

COMPARABLE is a signature for basic comparison. It is used only for instantiating full comparison modules of signature S via the functor Make.

module type S = sig ... end

S is a signature for a fully-fledge comparison module. It includes all the functions and operators derived from a compare function.

module Make (P : COMPARABLE) : S with type t := P.t

Base types

The specialised comparison and all the specialised functions and operators on the base types are compatible with the polymorphic comparison and all the polymorphic functions and operators from the Stdlib.

module Char : S with type t = char
module Bool : S with type t = bool
module Int : sig ... end
module Int32 : S with type t = int32
module Uint32 : S with type t = int32
module Int64 : S with type t = int64
module Uint64 : S with type t = int64
module Float : S with type t = float
module String : S with type t = string
module Bytes : S with type t = bytes
module Z : S with type t = Z.t

Type constructors

Provided the functor argument(s) are compatible with the polymorphic comparison of the Stdlib, then the specialised comparison and all the specialised functions and operators on the derived types are compatible with the polymorphic comparison and all the polymorphic functions and operators from the Stdlib.

module List (P : COMPARABLE) : S with type t = P.t list
module Option (P : COMPARABLE) : S with type t = P.t option
module Result (Ok : COMPARABLE) (Error : COMPARABLE) : S with type t = (Ok.t, Error.t) result

Building blocks

val or_else : int -> (unit -> int) -> int

or_else c f is c if c <> 0 or f () otherwise.

The intended use is

let compare (foo_a, bar_a) (foo_b, bar_b) =
  or_else (Foo.compare foo_a foo_b) (fun () -> Bar.compare bar_a bar_b)
OCaml

Innovation. Community. Security.