Gauche Devlog

< Running code once and only once | Did you mean...? >

2016/02/27

Comparators

A new comparator srfi, srfi:128, was finalized a couple of weeks ago. It is a rewrite of srfi:114, with simpler interface that appears to work with other parts of Scheme better (e.g. the comparison procedure of srfi-114, which returns -1, 0 or 1, doesn't seem Scheme-y, even though such interface is seen in other languages.)

I like srfi-128 better and I would've adopted it natively if Gauche hadn't already adopted srfi-114. At this moment, however, srfi-114 comparator is built-in and tightly coupled with Gauche's native datatypes such as built-in treemaps. Also, Gauche's internal compare API has been returning -1/0/1 scheme, so it goes better with srfi-114.

On the other hand, we expect future portable libraries will use srfi-128, and we want to encourage Gauche libraries to do so. We don't want to penalize code using srfi-128 with the overhead of adaptation layer.

So we chose to support both srfi-114 and srfi-128 natively. Srfi-114 comparator can emulate srfi-128's, and vice versa. Speed-sensitive internal code checks which flavor the passed comparator is, and uses optimized path. Most Scheme-level code would use high-level utilities such as =? and <? anyway, which hides the difference under the hood.

The only conflict in two srfis is the constructor, make-comparator. We already extended srfi-114 make-comparator to take the optional name argument, so it'll be tricky to distinguish two flavors with extra arguments. Besides, we do want to encourage new code to use srfi-128; so we decided to break the backward compatibility.

Gauche's make-comparator will be an extension of srfi-128, with optional name argument. We provide srfi-114 comparator under a different name make-comparator/compare. (The name could be confusing, but it reflects the compare generic function which is compatible to srfi-114's comparison procedure---thus, make-comparator with compare-like procedure.) If you import srfi-114, it imports make-comparator/compare as make-comparator and shadows the built-in one.

Gauche have supported subset of srfi-114 as built-in. Some of them are the same in srfi-128, some have different names, and some are newly introduced in srfi-128. Here's the correspondence.

srfi-114 srfi-128 notes
make-comparator *1
make-comparator
comparator? comparator?
comparator-comparison-procedure? comparator-ordered?
comparator-hash-function? comparator-hashable?
comparator-type-test-procedure comparator-type-test-predicate
comparator-equality-predicate comparator-equality-predicate
comparator-comparison-procedure *2
comparator-ordering-predicate *3
comparator-hash-function comparator-hash-function
comparator-test-type comparator-test-type
comparator-check-type comparator-check-type
comparator-hash comparator-hash
comparator-equal?
comparator-compare *4
default-comparator
make-default-comparator
default-hash *5
  • *1 Provided as make-comparator/compare.
  • *2 If srfi-128 comparator is given, returns a synthesized procedure that uses equality and ordering predicates.
  • *3 If srfi-114 comparator is given, returns a synthesized procedure that uses comparison procedure.
  • *4 If srfi-128 comparator is given, use its equality and ordering predicates.
  • *5 Same as Gauche's hash.

The change is currently undergone. We expect this to be the last major change before releasing 0.9.5.

Tags: Comparator, srfi-114, srfi-128

Post a comment

Name: