-
Notifications
You must be signed in to change notification settings - Fork 2
/
generic-comparability.lisp
381 lines (309 loc) · 11.6 KB
/
generic-comparability.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; generic-comparability.lisp
;;;; paul nathan 2013, 2014
;;;; license: LLGPL
;;;; A mostly conforming implementation of CDR-8
;;;; http://cdr.eurolisp.org/document/8/cleqcmp.html
;;;;
;;;; The documentation is largely cribbed from CDR-8 itself, in order
;;;; to provide corresponding understanding of the intent.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defpackage #:generic-comparability
(:use #:cl #:alexandria)
(:export
#:equals
#:compare
#:incomparable-object
#:lt
#:lte
#:gt
#:gte
#:lessp
#:not-lessp
#:greaterp
#:not-greaterp
#:hash-code))
(in-package #:generic-comparability)
(defgeneric equals (a b &rest keys &key recursive key &allow-other-keys)
(:documentation
"a b -- Common Lisp objects.
recursive -- a generalized boolean; default is NIL.
result -- a boolean.
keys -- a list (as per the usual behavior).
by-key -- a generalized boolean; default is T.
by-values -- a generalized boolean; default is T.
check-properties -- a generalized boolean; default is NIL.
case-sensitive -- a generalized boolean; default is T.
Description:
The EQUALS generic functions defines methods to test for `equality` of
two objects a and b. When two objects a and b are EQUALS under an
appropriate and context-dependent notion of `equality`, then the
function returns T as result; otherwise EQUALS returns NIL as result.
If the argument recursive is T, then EQUALS may recurse down the
`structure` of a and b. The description of each known method contains
the relevant information about its recursive dependent behavior.
EQUALS provides some default behavior, but it is intended mostly as a
hook for users. As such, it is allowed to add keyword arguments to
user-defined EQUALS methods, as the &key and &allow-other-keys
lambda-list markers imply."))
(defmethod equals ((a t) (b t) &rest keys)
(declare (ignore keys))
(equalp a b))
(defmethod equals ((a float) (b float)
&rest keys
&key
floating-compare
(max-relative-diff 1.19e-7))
"EQUALS float float &key (floating-compare nil) (max-relative-diff 1.19e7)
If floating-compare is true, then the floating comparison algorithm is used, as opposed to `=`.
The floating comparison is derived from here:
http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
The Internet Archive has a copy of this URL.
The implemented algorithm is AlmostEqualRelative. It is not the best
possible float equality algorithm, but is simple and
understandable (and easy to code). Improvements with citations from
the literature are welcome.
Observe the following comparison and beware:
CL-USER> (equals 1.0 1.0000001 :floating-compare t)
NIL
CL-USER> (equals 1.0 1.00000001 :floating-compare t)
T
"
(declare (ignore keys))
(if floating-compare
(let ((diff (abs (- a b)))
(a (abs a))
(b (abs b)))
(<= diff (* (max a b) max-relative-diff)))
;; If we decided to go with error-prone =...
(= a b)))
(defmethod equals ((a number) (b number) &rest keys)
(declare (ignore keys))
(= a b))
(defmethod equals ((a cons) (b cons) &rest keys)
(declare (ignore keys))
(tree-equal a b :test #'equals))
(defmethod equals ((a character) (b character)
&rest keys
&key (case-sensitive t))
(declare (ignore keys))
(if case-sensitive
(char= a b)
(char-equal a b)))
(defmethod equals ((a string) (b string)
&rest keys
&key (case-sensitive t))
(declare (ignore keys))
(if case-sensitive
(string= a b)
(string-equal a b)))
(defmethod equals ((a structure-object) (b structure-object)
&rest keys
&key (reference-equality t))
(declare (ignore keys))
(if reference-equality
(eq a b)
(equalp a b)))
(defmethod equals ((a standard-object) (b standard-object) &rest keys)
(declare (ignore keys))
(eq a b))
;;; compound structure comparison is trickier.
;;;
;;; An open question is if the by-key and by-value keys are passed
;;; into the child comparisons. And, should
(defmethod equals ((a hash-table) (b hash-table)
&rest keys
&key
recursive
(by-key t)
(by-value t)
(check-properties t) &allow-other-keys)
"EQUALS hash-table hash-table &key recursive (by-key t) (by-value t) (check-properties t)
by-key implies checking hash table keys for equality
by-value implies checking hash table values for equality
check-properties implies checking both hash-table-rehash-size and
hash-table rehash-threshhold.
This is expensive: O( 2 * n * (time-of-key-comparison + time-of-value-comparison))."
;; Are these the same object?
(when (eq a b)
(return-from equals t))
;; Do they have different numbers of keys?
(when (/= (hash-table-size a) (hash-table-size b))
(return-from equals nil))
;; And now to check.
(and
(if by-key
(loop
for k1 in (hash-table-keys a)
for k2 in (hash-table-keys b)
always (apply
#'(lambda (a b)
(equals a b :recursive recursive))
k1 k2 keys))
t)
(if by-value
(loop
for v1 in (hash-table-values a)
for v2 in (hash-table-values b)
always (apply #'(lambda
(a b) (equals a b :recursive recursive))
v1 v2 keys))
t)
(when check-properties
(and (= (hash-table-rehash-size a)
(hash-table-rehash-size b))
(= (hash-table-rehash-threshold a)
(hash-table-rehash-threshold b))))))
(defmethod equals ((a array) (b array)
&rest keys
&key recursive &allow-other-keys)
"EQUALS array array &key recursive
This implements a straightforward comparison element by element of the array."
;; If a more sophisticated array EQUALS is desired, please submit such
(when (equal (array-dimensions a)
(array-dimensions b))
(loop for i from 0 below (array-total-size a)
always (apply #'(lambda
(a b) (equals a b :recursive recursive))
(row-major-aref a i)
(row-major-aref b i)
keys))))
(defgeneric compare (a b &rest keys &key recursive &allow-other-keys)
(:documentation "The generic function COMPARE defines methods to
test the ordering of two objects a and b, if such order exists. The
result value returned by COMPARE is one of the four symbols: :<, :>,
:=, or :/=. The COMPARE function returns :/= as result by default; thus
it can represent partial orders among objects. The equality tests
should be coherent with what the generic function EQUALS does.
If the argument recursive is T, then COMPARE may recurse down the
`structure` of a and b. The description of each known method contains
the relevant information about its recursive dependent behavior. "))
(defmethod compare ((a t) (b t) &rest keys)
'/=)
(defmethod compare ((a number) (b number) &rest keys)
(declare (ignore keys))
(cond
((< a b) '<)
((> a b) '>)
(t '=)))
(defmethod compare ((a symbol) (b symbol) &rest keys)
(declare (ignore keys))
(if (eq a b)
'=
'/=))
(defmethod compare ((a character) (b character)
&rest keys
&key (case-sensitive t))
(declare (ignore keys))
(if case-sensitive
(cond ((string< a b) '<)
((string> a b) '>)
((string= a b) '=)
(t '/=))
(cond ((string-lessp a b) '<)
((string-greaterp a b) '>)
((string-equal a b) '=)
(t '/=))))
(defmethod compare ((a string) (b string)
&rest keys
&key (case-sensitive t))
(declare (ignore keys))
(if case-sensitive
(cond ((string< a b) '<)
((string> a b) '>)
((string= a b) '=)
(t '/=))
(cond ((string-lessp a b) '<)
((string-greaterp a b) '>)
((string-equal a b) '=)
(t '=))))
(define-condition incomparable-object (error)
((value1
:initarg :value1
:accessor value1
:initform nil)
(value2
:initarg :value2
:accessor value2
:initform nil)))
(defmethod print-object ((object incomparable-object) stream)
(print-unreadable-object (object stream :type t :identity t)
(format stream " - ~a, an ~a, not comparable to~& ~a, an ~a"
(value1 object)
(type-of (value1 object))
(value2 object)
(type-of (value2 object)))))
(defun incomparable-object (v1 v2)
(error (make-instance 'incomparable-object
:value1 v1
:value2 v2 )))
(defgeneric lt (a b &rest keys &key recursive &allow-other-keys))
(defgeneric lte (a b &rest keys &key recursive &allow-other-keys))
(defgeneric gt (a b &rest keys &key recursive &allow-other-keys))
(defgeneric gte (a b &rest keys &key recursive &allow-other-keys))
(let ((underlying #'(lambda (a b &rest keys)
(apply #'compare a b keys)) ))
(defmethod lt (a b &rest keys)
(case (apply underlying a b keys)
(< t)
(> nil)
(= nil)
(t
(incomparable-object a b))))
(defmethod lte (a b &rest keys)
(case (apply underlying a b keys)
(< t)
(> nil)
(= t)
(t
(incomparable-object a b))))
(defmethod gt (a b &rest keys)
(case (apply underlying a b keys)
(< nil)
(> t)
(= nil)
(t
(incomparable-object a b))))
(defmethod gte (a b &rest keys)
(case (apply underlying a b keys)
(< nil)
(> t)
(= t)
(t
(incomparable-object a b)))))
(setf (fdefinition 'lessp) #'lt)
(setf (fdefinition 'not-greaterp) #'lte)
(setf (fdefinition 'greaterp) #'gt)
(setf (fdefinition 'not-lessp) #'gte)
(defgeneric hash-code (a)
(:documentation "The HASH-CODE generic function is provided as a
companion to EQUALS for the benefit of those Common Lisp
implementations that provide a handle on the inner working of hash
tables (usually in the form of an extra :sxhash or :hash-function
keyword argument to make-hash-table), or for bottom-up hash table
implementations.
HASH-CODE is modeled after the Java hashCode() method of
java.lang.Object. The same description applies almost unchanged.
The general contract of HASH-CODE is the following.
Whenever it is invoked on the same object more than once during an a
Common Lisp session, the HASH-CODE generic function must consistently
return the same fixnum, provided no information used in EQUALS
comparisons on the object a is modified. This integer need not remain
consistent from one Common Lisp session to another. If two objects
are equal according to the EQUALS generic predicate, then calling the
HASH-CODE generic function on each of the two objects must produce the
same integer result. It is not required that if two objects are
unequal according to the EQUALS generic predicate, then calling the
HASH-CODE generic function on each of the two objects must produce
distinct integer results. However, the programmer should be aware that
producing distinct integer results for unequal objects may improve the
performance of hashtables.
"))
(defmethod hash-code ((a T))
"The generic hash-code implementation defaults to
SXHASH of the value passed in."
(sxhash a))
;; Register the implementation of cdr-8
(when (boundp '*features*)
(unless (member :cdr-8 *features*)
(push :cdr-8 *features*)))