From 7bb2e678e312b5155cc36d9d63b54ec83dc7fb3f Mon Sep 17 00:00:00 2001 From: Josh Berdine Date: Thu, 1 Jul 2021 03:30:45 -0700 Subject: [PATCH] [sledge] Add Ord.( @? ) to lexicographically compose compare functions Summary: This allows easily defining lexicographic orders as a composition of other orders. For example, the natural ordering on `int * string` pairs can be written: ``` Ord.Infix.((Int.compare >|= fst) @? (String.compare >|= snd)) ``` The `@?` name is chosen as a hybrid between `@@` which denotes function composition and `` which is the infix operator of `Containers.Ord` for defining lexicographic orders. Differential Revision: D29441161 fbshipit-source-id: ae9143cdc --- sledge/nonstdlib/NS.mli | 12 +++++++++++- sledge/nonstdlib/NS0.ml | 12 +++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/sledge/nonstdlib/NS.mli b/sledge/nonstdlib/NS.mli index 2c2115102..9685eb97a 100644 --- a/sledge/nonstdlib/NS.mli +++ b/sledge/nonstdlib/NS.mli @@ -43,7 +43,17 @@ module Poly : sig val hash : 'a -> int end -module Ord = Containers.Ord +module Ord : sig + include module type of Containers.Ord + + val ( @? ) : 'a t -> 'a t -> 'a t + + module Infix : sig + include module type of Containers.Ord.Infix + + val ( @? ) : 'a t -> 'a t -> 'a t + end +end (** Function combinators *) diff --git a/sledge/nonstdlib/NS0.ml b/sledge/nonstdlib/NS0.ml index 98f454c4e..cd0331aa1 100644 --- a/sledge/nonstdlib/NS0.ml +++ b/sledge/nonstdlib/NS0.ml @@ -55,7 +55,17 @@ module Poly = struct let hash = Stdlib.Hashtbl.hash end -module Ord = Containers.Ord +module Ord = struct + include Containers.Ord + + let ( @? ) c1 c2 x y = c1 x y (c2, x, y) + + module Infix = struct + include Infix + + let ( @? ) = ( @? ) + end +end (** Function combinators *)