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 ;;; Is X an extended sequence?
28 (defun extended-sequence-p (x)
31 (let* ((slayout #.(info :type :compiler-layout 'sequence))
32 (depthoid #.(layout-depthoid (info :type :compiler-layout 'sequence)))
33 (layout (layout-of x)))
34 (when (layout-invalid layout)
35 (setq layout (update-object-layout-or-invalid x slayout)))
36 (if (eq layout slayout)
38 (let ((inherits (layout-inherits layout)))
39 (declare (optimize (safety 0)))
40 (and (> (length inherits) depthoid)
41 (eq (svref inherits depthoid) slayout)))))))
43 ;;; Is X a SEQUENCE? Harder than just (OR VECTOR LIST)
47 (let* ((slayout #.(info :type :compiler-layout 'sequence))
48 (depthoid #.(layout-depthoid (info :type :compiler-layout 'sequence)))
49 (layout (layout-of x)))
50 (when (layout-invalid layout)
51 (setq layout (update-object-layout-or-invalid x slayout)))
52 (if (eq layout slayout)
54 (let ((inherits (layout-inherits layout)))
55 (declare (optimize (safety 0)))
56 (and (> (length inherits) depthoid)
57 (eq (svref inherits depthoid) slayout)))))))
59 ;;;; primitive predicates. These must be supported directly by the
64 "Return T if X is NIL, otherwise return NIL."
67 ;;; All the primitive type predicate wrappers share a parallel form..
68 (macrolet ((def-type-predicate-wrapper (pred)
69 (let* ((name (symbol-name pred))
70 (stem (string-left-trim "%" (string-right-trim "P-" name)))
71 (article (if (position (schar name 0) "AEIOU") "an" "a")))
72 `(defun ,pred (object)
74 "Return true if OBJECT is ~A ~A, and NIL otherwise."
77 ;; (falling through to low-level implementation)
79 (def-type-predicate-wrapper array-header-p)
80 (def-type-predicate-wrapper arrayp)
81 (def-type-predicate-wrapper atom)
82 (def-type-predicate-wrapper base-char-p)
83 (def-type-predicate-wrapper base-string-p)
84 #!+sb-unicode (def-type-predicate-wrapper character-string-p)
85 (def-type-predicate-wrapper bignump)
86 (def-type-predicate-wrapper bit-vector-p)
87 (def-type-predicate-wrapper characterp)
88 (def-type-predicate-wrapper code-component-p)
89 (def-type-predicate-wrapper consp)
90 (def-type-predicate-wrapper compiled-function-p)
91 (def-type-predicate-wrapper complexp)
92 (def-type-predicate-wrapper complex-double-float-p)
93 (def-type-predicate-wrapper complex-float-p)
94 #!+long-float (def-type-predicate-wrapper complex-long-float-p)
95 (def-type-predicate-wrapper complex-rational-p)
96 (def-type-predicate-wrapper complex-single-float-p)
97 ;; (COMPLEX-VECTOR-P is not included here since it's awkward to express
98 ;; the type it tests for in the Common Lisp type system, and since it's
99 ;; only used in the implementation of a few specialized things.)
100 (def-type-predicate-wrapper double-float-p)
101 (def-type-predicate-wrapper extended-char-p)
102 (def-type-predicate-wrapper fdefn-p)
103 (def-type-predicate-wrapper fixnump)
104 (def-type-predicate-wrapper floatp)
105 (def-type-predicate-wrapper functionp)
106 (def-type-predicate-wrapper integerp)
107 (def-type-predicate-wrapper listp)
108 (def-type-predicate-wrapper long-float-p)
109 #!+(and sb-thread sb-lutex)
110 (def-type-predicate-wrapper lutexp)
111 (def-type-predicate-wrapper lra-p)
112 (def-type-predicate-wrapper null)
113 (def-type-predicate-wrapper numberp)
114 (def-type-predicate-wrapper rationalp)
115 (def-type-predicate-wrapper ratiop)
116 (def-type-predicate-wrapper realp)
117 (def-type-predicate-wrapper short-float-p)
118 (def-type-predicate-wrapper single-float-p)
119 (def-type-predicate-wrapper %instancep)
120 (def-type-predicate-wrapper symbolp)
121 (def-type-predicate-wrapper system-area-pointer-p)
122 (def-type-predicate-wrapper weak-pointer-p)
123 #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
125 (def-type-predicate-wrapper unsigned-byte-32-p)
126 (def-type-predicate-wrapper signed-byte-32-p))
127 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
129 (def-type-predicate-wrapper unsigned-byte-64-p)
130 (def-type-predicate-wrapper signed-byte-64-p))
131 ;; Specialized array types
132 (macrolet ((saetp-defs ()
136 `(def-type-predicate-wrapper
137 ,(symbolicate (sb!vm:saetp-primitive-type-name saetp) "-P")))
138 sb!vm:*specialized-array-element-type-properties*))))
141 (def-type-predicate-wrapper simple-array-p)
142 (def-type-predicate-wrapper simple-string-p)
143 (def-type-predicate-wrapper stringp)
144 (def-type-predicate-wrapper vectorp)
145 (def-type-predicate-wrapper vector-nil-p))
147 ;;; Return the specifier for the type of object. This is not simply
148 ;;; (TYPE-SPECIFIER (CTYPE-OF OBJECT)) because CTYPE-OF has different
149 ;;; goals than TYPE-OF. In particular, speed is more important than
150 ;;; precision, and it is not permitted to return member types.
151 (defun type-of (object)
153 "Return the type of OBJECT."
157 ((<= 0 object 1) 'bit)
158 ((< object 0) 'fixnum)
159 (t '(integer 0 #.sb!xc:most-positive-fixnum))))
162 '(integer #.(1+ sb!xc:most-positive-fixnum))
164 (standard-char 'standard-char)
165 (base-char 'base-char)
166 (extended-char 'extended-char)
167 ((member t) 'boolean)
170 (type-specifier (ctype-of object)))
172 (let* ((classoid (layout-classoid (layout-of object)))
173 (name (classoid-name classoid)))
174 (if (%instancep object)
176 (sb!alien-internals:alien-value
178 ,(sb!alien-internals:unparse-alien-type
179 (sb!alien-internals:alien-value-type object))))
181 (let ((pname (classoid-proper-name classoid)))
182 (if (classoid-p pname)
183 (classoid-pcl-class pname)
187 ;;;; equality predicates
189 ;;; This is real simple, 'cause the compiler takes care of it.
190 (defun eq (obj1 obj2)
192 "Return T if OBJ1 and OBJ2 are the same object, otherwise NIL."
195 (declaim (inline %eql))
196 (defun %eql (obj1 obj2)
198 "Return T if OBJ1 and OBJ2 represent the same object, otherwise NIL."
200 (if (or (typep obj2 'fixnum)
201 (not (typep obj2 'number)))
203 (macrolet ((foo (&rest stuff)
205 ,@(mapcar (lambda (foo)
206 (let ((type (car foo))
209 (and (typep obj1 ',type)
219 (zerop (bignum-compare x y))))
222 (and (eql (numerator x) (numerator y))
223 (eql (denominator x) (denominator y)))))
226 (and (eql (realpart x) (realpart y))
227 (eql (imagpart x) (imagpart y))))))))))
232 (defun bit-vector-= (x y)
233 (declare (type bit-vector x y))
234 (if (and (simple-bit-vector-p x)
235 (simple-bit-vector-p y))
236 (bit-vector-= x y) ; DEFTRANSFORM
237 (and (= (length x) (length y))
242 (unless (= (bit x i) (bit y i))
247 "Return T if X and Y are EQL or if they are structured components whose
248 elements are EQUAL. Strings and bit-vectors are EQUAL if they are the same
249 length and have identical components. Other arrays must be EQ to be EQUAL."
250 ;; Non-tail self-recursion implemented with a local auxiliary function
251 ;; is a lot faster than doing it the straightforward way (at least
252 ;; on x86oids) due to calling convention differences. -- JES, 2005-12-30
253 (labels ((equal-aux (x y)
258 (equal-aux (car x) (car y))
259 (equal-aux (cdr x) (cdr y))))
261 (and (stringp y) (string= x y)))
263 (and (pathnamep y) (pathname= x y)))
265 (and (bit-vector-p y)
268 ;; Use MAYBE-INLINE to get the inline expansion only once (instead
269 ;; of 200 times with INLINE). -- JES, 2005-12-30
270 (declare (maybe-inline equal-aux))
273 ;;; EQUALP comparison of HASH-TABLE values
274 (defun hash-table-equalp (x y)
275 (declare (type hash-table x y))
277 (and (hash-table-p y)
278 (eql (hash-table-count x) (hash-table-count y))
279 (eql (hash-table-test x) (hash-table-test y))
280 (block comparison-of-entries
281 (maphash (lambda (key x-value)
282 (multiple-value-bind (y-value y-value-p)
284 (unless (and y-value-p (equalp x-value y-value))
285 (return-from comparison-of-entries nil))))
290 #+nil ; KLUDGE: If doc string, should be accurate: Talk about structures
292 "This is like EQUAL, except more liberal in several respects.
293 Numbers may be of different types, as long as the values are identical
294 after coercion. Characters may differ in alphabetic case. Vectors and
295 arrays must have identical dimensions and EQUALP elements, but may differ
296 in their type restriction."
298 ((characterp x) (and (characterp y) (char-equal x y)))
299 ((numberp x) (and (numberp y) (= x y)))
302 (equalp (car x) (car y))
303 (equalp (cdr x) (cdr y))))
305 (and (pathnamep y) (pathname= x y)))
307 (and (hash-table-p y)
308 (hash-table-equalp x y)))
310 (let* ((layout-x (%instance-layout x))
311 (raw-len (layout-n-untagged-slots layout-x))
312 (total-len (layout-length layout-x))
313 (normal-len (- total-len raw-len)))
315 (eq layout-x (%instance-layout y))
316 (structure-classoid-p (layout-classoid layout-x))
317 (dotimes (i normal-len t)
318 (let ((x-el (%instance-ref x i))
319 (y-el (%instance-ref y i)))
320 (unless (or (eq x-el y-el)
325 (raw-instance-slots-equalp layout-x x y)))))
327 (let ((length (length x)))
329 (= length (length y))
330 (dotimes (i length t)
331 (let ((x-el (aref x i))
333 (unless (or (eq x-el y-el)
338 (= (array-rank x) (array-rank y))
339 (dotimes (axis (array-rank x) t)
340 (unless (= (array-dimension x axis)
341 (array-dimension y axis))
343 (dotimes (index (array-total-size x) t)
344 (let ((x-el (row-major-aref x index))
345 (y-el (row-major-aref y index)))
346 (unless (or (eq x-el y-el)
351 (/show0 "about to do test cases in pred.lisp")
353 (let ((test-cases `((0.0 ,(load-time-value (make-unportable-float :single-float-negative-zero)) t)
355 (#c(1 0) #c(1.0 0.0) t)
356 (#c(0 1) #c(0.0 1.0) t)
357 (#c(1.1 0.0) #c(11/10 0) nil) ; due to roundoff error
359 ("Hello" #(#\h #\E #\l #\l #\o) t)
360 ("Hello" "goodbye" nil))))
361 (/show0 "TEST-CASES bound in pred.lisp")
362 (dolist (test-case test-cases)
363 (/show0 "about to do a TEST-CASE in pred.lisp")
364 (destructuring-bind (x y expected-result) test-case
365 (let* ((result (equalp x y))
366 (bresult (if result 1 0))
367 (expected-bresult (if expected-result 1 0)))
368 (unless (= bresult expected-bresult)
369 (/show0 "failing test in pred.lisp")
370 (error "failed test (EQUALP ~S ~S)" x y))))))
371 (/show0 "done with test cases in pred.lisp")