1 ;;;; predicate functions (EQUAL and friends, and type predicates)
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 ;;;; miscellaneous non-primitive predicates
16 #!-sb-fluid (declaim (inline streamp))
17 (defun streamp (stream)
18 (typep stream 'stream))
20 ;;; Is X a (VECTOR T)?
22 (or (simple-vector-p x)
23 (and (complex-vector-p x)
24 (do ((data (%array-data-vector x) (%array-data-vector data)))
25 ((not (array-header-p data)) (simple-vector-p data))))))
27 ;;;; primitive predicates. These must be supported directly by the
32 "Return T if X is NIL, otherwise return NIL."
35 ;;; All the primitive type predicate wrappers share a parallel form..
36 (macrolet ((def-type-predicate-wrapper (pred)
37 (let* ((name (symbol-name pred))
38 (stem (string-left-trim "%" (string-right-trim "P-" name)))
39 (article (if (position (schar name 0) "AEIOU") "an" "a")))
40 `(defun ,pred (object)
42 "Return true if OBJECT is ~A ~A, and NIL otherwise."
45 ;; (falling through to low-level implementation)
47 (def-type-predicate-wrapper array-header-p)
48 (def-type-predicate-wrapper arrayp)
49 (def-type-predicate-wrapper atom)
50 (def-type-predicate-wrapper base-char-p)
51 (def-type-predicate-wrapper base-string-p)
52 #!+sb-unicode (def-type-predicate-wrapper character-string-p)
53 (def-type-predicate-wrapper bignump)
54 (def-type-predicate-wrapper bit-vector-p)
55 (def-type-predicate-wrapper characterp)
56 (def-type-predicate-wrapper code-component-p)
57 (def-type-predicate-wrapper consp)
58 (def-type-predicate-wrapper compiled-function-p)
59 (def-type-predicate-wrapper complexp)
60 (def-type-predicate-wrapper complex-double-float-p)
61 (def-type-predicate-wrapper complex-float-p)
62 #!+long-float (def-type-predicate-wrapper complex-long-float-p)
63 (def-type-predicate-wrapper complex-rational-p)
64 (def-type-predicate-wrapper complex-single-float-p)
65 ;; (COMPLEX-VECTOR-P is not included here since it's awkward to express
66 ;; the type it tests for in the Common Lisp type system, and since it's
67 ;; only used in the implementation of a few specialized things.)
68 (def-type-predicate-wrapper double-float-p)
69 (def-type-predicate-wrapper extended-char-p)
70 (def-type-predicate-wrapper fdefn-p)
71 (def-type-predicate-wrapper fixnump)
72 (def-type-predicate-wrapper floatp)
73 (def-type-predicate-wrapper functionp)
74 (def-type-predicate-wrapper integerp)
75 (def-type-predicate-wrapper listp)
76 (def-type-predicate-wrapper long-float-p)
77 (def-type-predicate-wrapper lra-p)
78 (def-type-predicate-wrapper null)
79 (def-type-predicate-wrapper numberp)
80 (def-type-predicate-wrapper rationalp)
81 (def-type-predicate-wrapper ratiop)
82 (def-type-predicate-wrapper realp)
83 (def-type-predicate-wrapper short-float-p)
84 (def-type-predicate-wrapper simple-array-p)
85 (def-type-predicate-wrapper simple-bit-vector-p)
86 (def-type-predicate-wrapper simple-base-string-p)
87 #!+sb-unicode (def-type-predicate-wrapper simple-character-string-p)
88 (def-type-predicate-wrapper simple-string-p)
89 (def-type-predicate-wrapper simple-vector-p)
90 (def-type-predicate-wrapper single-float-p)
91 (def-type-predicate-wrapper stringp)
92 (def-type-predicate-wrapper %instancep)
93 (def-type-predicate-wrapper symbolp)
94 (def-type-predicate-wrapper system-area-pointer-p)
95 (def-type-predicate-wrapper weak-pointer-p)
96 (def-type-predicate-wrapper vectorp)
97 #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
98 (def-type-predicate-wrapper unsigned-byte-32-p)
99 #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
100 (def-type-predicate-wrapper signed-byte-32-p)
101 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
102 (def-type-predicate-wrapper unsigned-byte-64-p)
103 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
104 (def-type-predicate-wrapper signed-byte-64-p)
105 (def-type-predicate-wrapper simple-array-nil-p)
106 (def-type-predicate-wrapper simple-array-unsigned-byte-2-p)
107 (def-type-predicate-wrapper simple-array-unsigned-byte-4-p)
108 (def-type-predicate-wrapper simple-array-unsigned-byte-8-p)
109 (def-type-predicate-wrapper simple-array-unsigned-byte-16-p)
110 (def-type-predicate-wrapper simple-array-unsigned-byte-32-p)
111 (def-type-predicate-wrapper simple-array-signed-byte-8-p)
112 (def-type-predicate-wrapper simple-array-signed-byte-16-p)
113 (def-type-predicate-wrapper simple-array-signed-byte-30-p)
114 (def-type-predicate-wrapper simple-array-signed-byte-32-p)
115 (def-type-predicate-wrapper simple-array-single-float-p)
116 (def-type-predicate-wrapper simple-array-double-float-p)
117 #!+long-float (def-type-predicate-wrapper simple-array-long-float-p)
118 (def-type-predicate-wrapper simple-array-complex-single-float-p)
119 (def-type-predicate-wrapper simple-array-complex-double-float-p)
120 #!+long-float (def-type-predicate-wrapper simple-array-complex-long-float-p)
121 (def-type-predicate-wrapper vector-nil-p))
123 ;;; Return the specifier for the type of object. This is not simply
124 ;;; (TYPE-SPECIFIER (CTYPE-OF OBJECT)) because CTYPE-OF has different
125 ;;; goals than TYPE-OF. In particular, speed is more important than
126 ;;; precision, and it is not permitted to return member types.
127 (defun type-of (object)
129 "Return the type of OBJECT."
133 ((<= 0 object 1) 'bit)
134 ((< object 0) 'fixnum)
135 (t '(integer 0 #.sb!xc:most-positive-fixnum))))
138 '(integer #.(1+ sb!xc:most-positive-fixnum))
140 (standard-char 'standard-char)
141 (base-char 'base-char)
142 (extended-char 'extended-char)
143 ((member t) 'boolean)
145 ((or array complex) (type-specifier (ctype-of object)))
147 (let* ((classoid (layout-classoid (layout-of object)))
148 (name (classoid-name classoid)))
149 (if (%instancep object)
151 (sb!alien-internals:alien-value
153 ,(sb!alien-internals:unparse-alien-type
154 (sb!alien-internals:alien-value-type object))))
156 (let ((pname (classoid-proper-name classoid)))
157 (if (classoid-p pname)
158 (classoid-pcl-class pname)
162 ;;;; equality predicates
164 ;;; This is real simple, 'cause the compiler takes care of it.
165 (defun eq (obj1 obj2)
167 "Return T if OBJ1 and OBJ2 are the same object, otherwise NIL."
170 (declaim (inline %eql))
171 (defun %eql (obj1 obj2)
173 "Return T if OBJ1 and OBJ2 represent the same object, otherwise NIL."
175 (if (or (typep obj2 'fixnum)
176 (not (typep obj2 'number)))
178 (macrolet ((foo (&rest stuff)
180 ,@(mapcar (lambda (foo)
181 (let ((type (car foo))
184 (and (typep obj1 ',type)
194 (zerop (bignum-compare x y))))
197 (and (eql (numerator x) (numerator y))
198 (eql (denominator x) (denominator y)))))
201 (and (eql (realpart x) (realpart y))
202 (eql (imagpart x) (imagpart y))))))))))
207 (defun bit-vector-= (x y)
208 (declare (type bit-vector x y))
209 (if (and (simple-bit-vector-p x)
210 (simple-bit-vector-p y))
211 (bit-vector-= x y) ; DEFTRANSFORM
212 (and (= (length x) (length y))
217 (unless (= (bit x i) (bit y i))
222 "Return T if X and Y are EQL or if they are structured components
223 whose elements are EQUAL. Strings and bit-vectors are EQUAL if they
224 are the same length and have identical components. Other arrays must be
226 ;; Non-tail self-recursion implemented with a local auxiliary function
227 ;; is a lot faster than doing it the straightforward way (at least
228 ;; on x86oids) due to calling convention differences. -- JES, 2005-12-30
229 (labels ((equal-aux (x y)
234 (equal-aux (car x) (car y))
235 (equal-aux (cdr x) (cdr y))))
237 (and (stringp y) (string= x y)))
239 (and (pathnamep y) (pathname= x y)))
241 (and (bit-vector-p y)
244 ;; Use MAYBE-INLINE to get the inline expansion only once (instead
245 ;; of 200 times with INLINE). -- JES, 2005-12-30
246 (declare (maybe-inline equal-aux))
249 ;;; EQUALP comparison of HASH-TABLE values
250 (defun hash-table-equalp (x y)
251 (declare (type hash-table x y))
253 (and (hash-table-p y)
254 (eql (hash-table-count x) (hash-table-count y))
255 (eql (hash-table-test x) (hash-table-test y))
256 (block comparison-of-entries
257 (maphash (lambda (key x-value)
258 (multiple-value-bind (y-value y-value-p)
260 (unless (and y-value-p (equalp x-value y-value))
261 (return-from comparison-of-entries nil))))
266 #+nil ; KLUDGE: If doc string, should be accurate: Talk about structures
268 "This is like EQUAL, except more liberal in several respects.
269 Numbers may be of different types, as long as the values are identical
270 after coercion. Characters may differ in alphabetic case. Vectors and
271 arrays must have identical dimensions and EQUALP elements, but may differ
272 in their type restriction."
274 ((characterp x) (and (characterp y) (char-equal x y)))
275 ((numberp x) (and (numberp y) (= x y)))
278 (equalp (car x) (car y))
279 (equalp (cdr x) (cdr y))))
281 (and (pathnamep y) (pathname= x y)))
283 (and (hash-table-p y)
284 (hash-table-equalp x y)))
286 (let* ((layout-x (%instance-layout x))
287 (len (layout-length layout-x)))
289 (eq layout-x (%instance-layout y))
290 (structure-classoid-p (layout-classoid layout-x))
294 (let ((x-el (%instance-ref x i))
295 (y-el (%instance-ref y i)))
296 (unless (or (eq x-el y-el)
300 (let ((length (length x)))
302 (= length (length y))
303 (dotimes (i length t)
304 (let ((x-el (aref x i))
306 (unless (or (eq x-el y-el)
311 (= (array-rank x) (array-rank y))
312 (dotimes (axis (array-rank x) t)
313 (unless (= (array-dimension x axis)
314 (array-dimension y axis))
316 (dotimes (index (array-total-size x) t)
317 (let ((x-el (row-major-aref x index))
318 (y-el (row-major-aref y index)))
319 (unless (or (eq x-el y-el)
324 (/show0 "about to do test cases in pred.lisp")
326 (let ((test-cases `((0.0 ,(load-time-value (make-unportable-float :single-float-negative-zero)) t)
328 (#c(1 0) #c(1.0 0) t)
329 (#c(1.1 0) #c(11/10 0) nil) ; due to roundoff error
331 ("Hello" #(#\h #\E #\l #\l #\o) t)
332 ("Hello" "goodbye" nil))))
333 (/show0 "TEST-CASES bound in pred.lisp")
334 (dolist (test-case test-cases)
335 (/show0 "about to do a TEST-CASE in pred.lisp")
336 (destructuring-bind (x y expected-result) test-case
337 (let* ((result (equalp x y))
338 (bresult (if result 1 0))
339 (expected-bresult (if expected-result 1 0)))
340 (unless (= bresult expected-bresult)
341 (/show0 "failing test in pred.lisp")
342 (error "failed test (EQUALP ~S ~S)" x y))))))
343 (/show0 "done with test cases in pred.lisp")