This SRFI is currently in ``final'' status.
To see an explanation of
each status that a SRFI can hold, see here.
To provide input on this SRFI, please
<srfi minus 114 at srfi dot schemers dot org>. See
instructions here to
subscribe to the list. You can access previous messages via
the archive of the mailing
You can access
post-finalization messages via
the archive of the mailing list.
This proposal is a rewrite of SRFI 67, Compare Procedures, extending it from procedures that represent a total order to procedure bundles that represent one or more of a total order, an equality predicate, and a hash function. By packaging these procedures together, along with a type test predicate, they can be treated as a single item for use in the implementation of data structures.
All issues closed.
The four procedures above have complex dependencies on one another, and it is inconvenient to have to pass them all to other procedures that might or might not make use of all of them. For example, a set implementation naturally requires only an equality predicate, but if it is implemented using a hash table, an appropriate hash function is also required if the implementation does not provide one; alternatively, if it is implemented using a tree, a comparison procedure is required. By passing a comparator rather than a bare equality predicate, the set implementation can make use of whatever procedures are available and useful to it.
This SRFI could not have been written without the work of Sebastian Egner and Jens Axel Søgaard on SRFI 67; much of the credit for this SRFI is due to them, but none of the blame. In addition, many of the design decisions of this SRFI are copied from SRFI 67's design rationale.
The procedures in this SRFI are in the
(srfi 114) library (or
(srfi :114) on R6RS), but the sample implementation currently places them in the
A comparator is an object of a disjoint type. It is a bundle of procedures that are useful for comparing two objects either for equality or for ordering. There are four procedures in the bundle:
The type test predicate returns
#t if its argument has the correct type to be passed as an argument to the other three procedures, and
The equality predicate returns
#t if the two objects are the same in the sense of the comparator, and
#f otherwise. It is the programmer's responsibility to ensure that it is reflexive, symmetric, transitive, and can handle any arguments that satisfy the type test predicate.
The comparison procedure returns -1, 0, or 1 if the first object precedes the second, is equal to the second, or follows the second, respectively, in a total order defined by the comparator. It is the programmer's responsibility to ensure that it is reflexive, weakly antisymmetric, transitive, can handle any arguments that satisfy the type test predicate, and returns 0 iff the equality predicate returns
#t. Comparison procedures are compatible with the compare procedures of SRFI 67; see SRFI 67 for the rationale for adopting this return convention.
The hash function takes one argument, and returns an exact non-negative integer. It is the programmer's responsibility to ensure that it can handle any argument that satisfies the type test predicate, and that it returns the same value on two objects if the equality predicate says they are the same (but not necessarily the converse).
It is also the programmer's responsibility to ensure that all four procedures provide the same result whenever they are applied to the same object(s) (in the sense of
eqv?), unless the object(s) have been mutated since the last invocation. In particular, they must not depend in any way on memory addresses in implementations where the garbage collector can move objects in memory.
The comparator objects defined in this SRFI are not applicable to circular structure or to NaNs or objects containing them. Attempts to pass any such objects to any procedure defined here, or to any procedure that is part of a comparator defined here, is an error except as otherwise noted.
comparator?, comparator-comparison-procedure?, comparator-hash-function?
boolean-comparator, char-comparator, char-ci-comparator, string-comparator, string-ci-comparator, symbol-comparator, exact-integer-comparator, integer-comparator, rational-comparator, real-comparator, complex-comparator, number-comparator, pair-comparator, list-comparator, vector-comparator, bytevector-comparator
The default comparator:
make-comparator, make-inexact-real-comparator, make-vector-comparator, make-bytevector-comparator, make-list-comparator, make-vectorwise-comparator, make-listwise-comparator, make-car-comparator, make-cdr-comparator, make-pair-comparator, make-improper-list-comparator, make-selecting-comparator, make-refining-comparator, make-reverse-comparator, make-debug-comparator
Wrapped equality predicates:
eq-comparator, eqv-comparator, equal-comparator
comparator-type-test-procedure, comparator-equality-predicate, comparator-comparison-procedure, comparator-hash-function
comparator-test-type, comparator-check-type, comparator-equal?, comparator-compare, comparator-hash
Comparison procedure constructors:
make-comparison<, make-comparison>, make-comparison<=, make-comparison>=, make-comparison=/< make-comparison=/>
if3, if=?, if<?, if>?, if<=?, if>=?, if-not=?
=?, <?, >?, <=?, >=?
Comparison predicate constructors:
make=, make< , make>, make<=, make>=
Interval (ternary) comparison predicates:
in-open-interval?, in-closed-interval?, in-open-closed-interval?, in-closed-open-interval?
Min/max comparison procedures:
#t if obj is a comparator, and
#t if comparator has a supplied comparison procedure, and
#t if comparator has a supplied hash function, and
The following comparators are analogous to the standard compare procedures of SRFI 67. They all provide appropriate hash functions as well.
Compares booleans using the total order
Compares characters using the total order implied by
char<?. On R6RS and R7RS systems, this is Unicode codepoint order.
Compares characters using the total order implied by
char-ci<? On R6RS and R7RS systems, this is Unicode codepoint order after the characters have been folded to lower case.
Compares strings using the total order implied by
string<?. Note that this order is implementation-dependent.
Compares strings using the total order implied by
string-ci<?. Note that this order is implementation-dependent.
Compares symbols using the total order implied by applying
symbol->string to the symbols and comparing them using the total order implied by
string<?. It is not a requirement that the hash function of
symbol-comparator be consistent with the hash function of
These comparators compare exact integers, integers, rational numbers, real numbers, complex numbers, and any numbers using the total order implied by
<. They must be compatible with the R5RS numerical tower in the following sense: If S is a subtype of the numerical type T and the two objects are members of S , then the equality predicate and comparison procedures (but not necessarily the hash function) of S-comparator and T-comparator compute the same results on those objects.
Since non-real numbers cannot be compared with
<, the following least-surprising ordering is defined: If the real parts are < or >, so are the numbers; otherwise, the numbers are ordered by their imaginary parts. This can still produce surprising results if one real part is exact and the other is inexact.
This comparator compares pairs using
default-comparator (see below) on their cars. If the cars are not equal, that value is returned. If they are equal,
default-comparator is used on their cdrs and that value is returned.
This comparator compares lists lexicographically, as follows:
default-comparator(see below), then the result is the result of that comparison. Otherwise, the cdrs are compared using
These comparators compare vectors and bytevectors by comparing their lengths. A shorter argument is always less than a longer one. If the lengths are equal, then each element is compared in turn using
default-comparator (see below) until a pair of unequal elements is found, in which case the result is the result of that comparison. If all elements are equal, the arguments are equal.
If the implementation does not support bytevectors,
bytevector-comparator has a type testing procedure that always returns
This is a comparator that accepts any two Scheme values (with the exceptions listed in the Limitations section) and orders them in some implementation-defined way, subject to the following conditions:
The following ordering between types must hold: the empty list precedes pairs, which precede booleans, which precede characters, which precede strings, which precede symbols, which precede numbers, which precede vectors, which precede bytevectors, which precede all other objects. This ordering is compatible with SRFI 67.
bytevector-comparatorrespectively. The same should be true when applied to an object or objects of a type for which a standard comparator is defined elsewhere.
Given disjoint types a and b, one of three conditions must hold:
Most of the following comparator constructors are close analogues of the standard compare procedures of SRFI 67. They all provide appropriate hash functions as well. Note that comparator constructors are allowed to cache their results: they need not return a newly allocated object, since comparators are purely functional.
(make-comparator type-test equality compare hash
Returns a comparator which bundles the type-test, equality, compare, and hash procedures provided. As a convenience, the following additional values are accepted:
#t, a type-test procedure that accepts any arguments is provided.
#t, an equality predicate is provided that returns
#tiff compare returns 0.
#f, a procedure is provided that signals an error on application. The predicates
comparator-hash-function?, respectively, will return
#fin these cases.
(make-inexact-real-comparator epsilon rounding nan-handling
Returns a comparator that compares inexact real numbers including NaNs as follows: if after rounding to the nearest epsilon they are the same, they compare equal; otherwise they compare as specified by
<. The direction of rounding is specified by the rounding argument, which is either a procedure accepting two arguments (the number and epsilon, or else one of the symbols
The argument nan-handling specifies how to compare NaN arguments to non-NaN arguments. If it is a procedure, the procedure is invoked on the other argument if either argument is a NaN. If it is the symbol
min, NaN values precede all other values; if it is the symbol
max, they follow all other values, and if it is the symbol
error, an error is signaled if a NaN value is compared. If both arguments are NaNs, however, they always compare as equal.
These procedures return comparators which compare two lists, vectors, or bytevectors in the same way as
bytevector-comparator respectively, but using element-comparator rather than
If the implementation does not support bytevectors, the result of invoking
make-bytevector-comparator is a comparator whose type testing procedure always returns
(make-listwise-comparator type-test element-comparator empty? head tail
Returns a comparator which compares two objects that satisfy type-test as if they were lists, using the empty? procedure to determine if an object is empty, and the head and tail procedures to access particular elements.
(make-vectorwise-comparator type-test element-comparator length ref
Returns a comparator which compares two objects that satisfy type-test as if they were vectors, using the length procedure to determine the length of the object, and the ref procedure to access a particular element.
Returns a comparator that compares pairs on their cars alone using comparator.
Returns a comparator that compares pairs on their cdrs alone using comparator.
(make-pair-comparator car-comparator cdr-comparator
Returns a comparator that compares pairs first on their cars using car-comparator. If the cars are equal, it compares the cdrs using cdr-comparator.
Returns a comparator that compares arbitrary objects as follows: the empty list precedes all pairs, which precede all other objects. Pairs are compared as if with
). All other objects are compared using element-comparator.
(make-selecting-comparator comparator1 comparator2 ...
Returns a comparator whose procedures make use of the comparators as follows:
The type test predicate passes its argument to the type test predicates of comparators in the sequence given. If any of them returns
#t, so does the type test predicate; otherwise, it returns
The arguments of the equality, compare, and hash functions are passed to the type test predicate of each comparator in sequence. The first comparator whose type test predicate is satisfied on all the arguments is used when comparing those arguments. All other comparators are ignored. If no type test predicate is satisfied, an error is signaled.
This procedure is analogous to the expression types
cond-compare from SRFI 67.
(make-refining-comparator comparator1 comparator2 ...
Returns a comparator that makes use of the comparators in the same way as
make-selecting-comparator, except that its procedures can look past the first comparator whose type test predicate is satisfied. If the comparison procedure of that comparator returns zero, then the next comparator whose type test predicate is satisfied is tried in place of it until one returns a non-zero value. If there are no more such comparators, then the comparison procedure returns zero. The equality predicate is defined in the same way. If no type test predicate is satisfied, an error is signaled.
The hash function of the result returns a value which depends, in an implementation-defined way, on the results of invoking the hash functions of the comparators whose type test predicates are satisfied on its argument. In particular, it may depend solely on the first or last such hash function. If no type test predicate is satisfied, an error is signaled.
This procedure is analogous to the expression type
refine-compare from SRFI 67.
Returns a comparator that behaves like comparator, except that the compare procedure returns 1, 0, and -1 instead of -1, 0, and 1 respectively. This allows ordering in reverse.
Returns a comparator that behaves exactly like comparator, except that whenever any of its procedures are invoked, it verifies all the programmer responsibilities (except stability), and an error is signaled if any of them are violated. Because it requires three arguments, transitivity is not tested on the first call to a debug comparator; it is tested on all future calls using an arbitrarily chosen argument from the previous invocation. Note that this may cause unexpected storage leaks.
The equality predicates of these comparators are
equal? respectively. When their comparison procedures are applied to non-equal objects, their behavior is implementation-defined. The type test predicates always return
These comparators accept circular structure (in the case of
equal-comparator, provided the implementation's
does so) and NaNs.
Returns the type test predicate of comparator.
Returns the equality predicate of comparator.
Returns the comparison procedure of comparator.
Returns the hash function of comparator.
(comparator-test-type comparator obj
Invokes the type test predicate of comparator on obj and returns what it returns.
(comparator-check-type comparator obj
Invokes the type test predicate of comparator on obj and returns true if it returns true and signals an error otherwise.
(comparator-equal? comparator obj1 obj2
Invokes the equality predicate of comparator on obj1 and obj2 and returns what it returns.
(comparator-compare comparator obj1 obj2
Invokes the comparison procedure of comparator on obj1 and obj2 and returns what it returns.
(comparator-hash comparator obj
Invokes the hash function of comparator on obj and returns what it returns.
(make-comparison=/< eq-pred lt-pred
(make-comparison=/> eq-pred gt-pred
These procedures return a comparison procedure, given a less-than predicate, a greater-than predicate, a less-than-or-equal-to predicate, a greater-than-or-equal-to predicate, or the combination of an equality predicate and either a less-than or a greater-than predicate.
They are the same as the corresponding SRFI 67
compare-by procedures. Note that they do not accept comparand arguments.
The following expression types allow the convenient use of comparison procedures. They come directly from SRFI 67.
(if3 <expr> <less> <equal> <greater>
The expression <expr> is evaluated; it will typically, but not necessarily, be a call on a comparison procedure. If the result is -1, <less> is evaluated and its value(s) are returned; if the result is 0, <equal> is evaluated and its value(s) are returned; if the result is 1, <greater> is evaluated and its value(s) are returned. Otherwise an error is signaled.
(if=? <expr> <consequent> [ <alternate> ]
(if<? <expr> <consequent> [ <alternate> ]
(if>? <expr> <consequent> [ <alternate> ]
(if<=? <expr> <consequent> [ <alternate> ]
(if>=? <expr> <consequent> [ <alternate> ]
(if-not=? <expr> <consequent> [ <alternate> ]
The expression <expr> is evaluated; it will typically, but not necessarily, be a call on a comparison procedure. It is an error if its value is not -1, 0, or 1. If the value is consistent with the specified relation, <consequent> is evaluated and its value(s) are returned. Otherwise, if <alternate> is present, it is evaluated and its value(s) are returned; if it is absent, an unspecified value is returned.
(=? comparator object1 object2 object3 ...
(<? comparator object1 object2 object3 ...
(>? comparator object1 object2 object3 ...
(<=? comparator object1 object2 object3 ...
(>=? comparator object1 object2 object3 ...
These procedures are analogous to the number, character, and string comparison predicates of Scheme. They allow the convenient use of comparators in situations where the expression types are not usable. They are also analogous to the similarly named procedures SRFI 67, but handle arbitrary numbers of arguments, which in SRFI 67 requires the use of the variants whose names begin with
These procedures apply the comparison procedure of comparator to the objects as follows. If the specified relation returns
#t for all objecti and objectj where n is the number of objects and 1 <= i < j <= n, then the procedures return
#t, but otherwise
The order in which the values are compared is unspecified. Because the relations are transitive, it suffices to compare each object with its successor.
These procedures return predicates which, when applied to two or more arguments, return
#t if comparing obj1 and obj2 using the equality or comparison procedures of comparator shows that the objects bear the specified relation to one another. Such predicates can be used in contexts that do not understand or expect comparators.
These procedures return true or false depending on whether an object is contained in an open, closed, or half-open interval. All comparisons are done in the sense of comparator, which is
default-comparator if omitted.
(in-open-interval? [ comparator ] obj1 obj2 obj3
#t if obj1 is less than obj2, which is less thanobj3, and
(in-closed-interval? [ comparator ] obj1 obj2 obj3
#t if obj1 is less than or equal to obj2, which is less than or equal to obj3, and
(in-open-closed-interval? [ comparator ] obj1 obj2 obj3
#t if obj1 is less than obj2, which is less than or equal to obj3, and
(in-closed-open-interval? [ comparator ] obj1 obj2 obj3
#t if obj1 is less than or equal to obj2, which is less than obj3, and
(comparator-min comparator object1 object2 ...
(comparator-max comparator object1 object2 ...
These procedures are analogous to
max respectively. They apply the comparison procedure of comparator to the objects to find and return a minimal (or maximal) object. The order in which the values are compared is unspecified.
Note: The SRFI 67 procedures
kth-largest involve sorting their arguments, and are not provided by this proposal in order to avoid an otherwise unnecessary implementation dependency. They are easily provided by a sorting package that makes use of comparators.
The sample implementation contains the following files:
basics.scm- the syntax, record type definition, and simple constructors and procedures
default.scm- a simple implementation of the default constructor, which should be improved by implementers to handle records and implementation-specific types
constructors.scm- most of the constructors
advanced.scm- the more complex constructors
r7rs-shim.scm- procedures for R7RS compatibility, including a trivial implementation of bytevectors on top of SRFI 4 u8vectors
complex-shim.scm- a trivial implementation of
imag-partfor Schemes that don't have complex numbers
comparators.sld- an R7RS library
comparators.scm- a Chicken library
A future release will include a test program using the Chicken
test egg, which is available on Chibi as the
(chibi test) library.
Copyright (C) John Cowan 2013. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.