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 ;;; various (VECTOR FOO) type predicates, not implemented as simple
25 ,@(loop for (name spec) in *vector-without-complex-typecode-infos*
26 collect `(defun ,name (x)
27 (or (typep x '(simple-array ,spec (*)))
28 (and (complex-vector-p x)
29 (do ((data (%array-data-vector x) (%array-data-vector data)))
30 ((not (array-header-p data)) (typep data '(simple-array ,spec (*))))))))))))
33 ;;; Is X an extended sequence?
34 (defun extended-sequence-p (x)
37 (let* ((slayout #.(info :type :compiler-layout 'sequence))
38 (depthoid #.(layout-depthoid (info :type :compiler-layout 'sequence)))
39 (layout (layout-of x)))
40 (when (layout-invalid layout)
41 (setq layout (update-object-layout-or-invalid x slayout)))
42 (if (eq layout slayout)
44 (let ((inherits (layout-inherits layout)))
45 (declare (optimize (safety 0)))
46 (and (> (length inherits) depthoid)
47 (eq (svref inherits depthoid) slayout)))))))
49 ;;; Is X a SEQUENCE? Harder than just (OR VECTOR LIST)
53 (let* ((slayout #.(info :type :compiler-layout 'sequence))
54 (depthoid #.(layout-depthoid (info :type :compiler-layout 'sequence)))
55 (layout (layout-of x)))
56 (when (layout-invalid layout)
57 (setq layout (update-object-layout-or-invalid x slayout)))
58 (if (eq layout slayout)
60 (let ((inherits (layout-inherits layout)))
61 (declare (optimize (safety 0)))
62 (and (> (length inherits) depthoid)
63 (eq (svref inherits depthoid) slayout)))))))
65 ;;;; primitive predicates. These must be supported directly by the
70 "Return T if X is NIL, otherwise return NIL."
73 ;;; All the primitive type predicate wrappers share a parallel form..
74 (macrolet ((def-type-predicate-wrapper (pred)
75 (let* ((name (symbol-name pred))
76 (stem (string-left-trim "%" (string-right-trim "P-" name)))
77 (article (if (position (schar name 0) "AEIOU") "an" "a")))
78 `(defun ,pred (object)
80 "Return true if OBJECT is ~A ~A, and NIL otherwise."
83 ;; (falling through to low-level implementation)
85 (def-type-predicate-wrapper array-header-p)
86 (def-type-predicate-wrapper arrayp)
87 (def-type-predicate-wrapper atom)
88 (def-type-predicate-wrapper base-char-p)
89 (def-type-predicate-wrapper base-string-p)
90 #!+sb-unicode (def-type-predicate-wrapper character-string-p)
91 (def-type-predicate-wrapper bignump)
92 (def-type-predicate-wrapper bit-vector-p)
93 (def-type-predicate-wrapper characterp)
94 (def-type-predicate-wrapper code-component-p)
95 (def-type-predicate-wrapper consp)
96 (def-type-predicate-wrapper compiled-function-p)
97 (def-type-predicate-wrapper complexp)
98 (def-type-predicate-wrapper complex-double-float-p)
99 (def-type-predicate-wrapper complex-float-p)
100 #!+long-float (def-type-predicate-wrapper complex-long-float-p)
101 (def-type-predicate-wrapper complex-rational-p)
102 (def-type-predicate-wrapper complex-single-float-p)
103 ;; (COMPLEX-VECTOR-P is not included here since it's awkward to express
104 ;; the type it tests for in the Common Lisp type system, and since it's
105 ;; only used in the implementation of a few specialized things.)
106 (def-type-predicate-wrapper double-float-p)
107 (def-type-predicate-wrapper extended-char-p)
108 (def-type-predicate-wrapper fdefn-p)
109 (def-type-predicate-wrapper fixnump)
110 (def-type-predicate-wrapper floatp)
111 (def-type-predicate-wrapper functionp)
112 (def-type-predicate-wrapper integerp)
113 (def-type-predicate-wrapper listp)
114 (def-type-predicate-wrapper long-float-p)
115 (def-type-predicate-wrapper lra-p)
116 (def-type-predicate-wrapper null)
117 (def-type-predicate-wrapper numberp)
118 (def-type-predicate-wrapper rationalp)
119 (def-type-predicate-wrapper ratiop)
120 (def-type-predicate-wrapper realp)
121 (def-type-predicate-wrapper short-float-p)
122 (def-type-predicate-wrapper single-float-p)
123 (def-type-predicate-wrapper %instancep)
124 (def-type-predicate-wrapper symbolp)
125 (def-type-predicate-wrapper system-area-pointer-p)
126 (def-type-predicate-wrapper weak-pointer-p)
127 #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
129 (def-type-predicate-wrapper unsigned-byte-32-p)
130 (def-type-predicate-wrapper signed-byte-32-p))
131 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
133 (def-type-predicate-wrapper unsigned-byte-64-p)
134 (def-type-predicate-wrapper signed-byte-64-p))
135 ;; Specialized array types
136 (macrolet ((saetp-defs ()
140 `(def-type-predicate-wrapper
141 ,(symbolicate (sb!vm:saetp-primitive-type-name saetp) "-P")))
142 sb!vm:*specialized-array-element-type-properties*))))
145 (def-type-predicate-wrapper simple-array-p)
146 (def-type-predicate-wrapper simple-string-p)
147 (def-type-predicate-wrapper stringp)
148 (def-type-predicate-wrapper vectorp)
149 (def-type-predicate-wrapper vector-nil-p))
151 ;;; Return the specifier for the type of object. This is not simply
152 ;;; (TYPE-SPECIFIER (CTYPE-OF OBJECT)) because CTYPE-OF has different
153 ;;; goals than TYPE-OF. In particular, speed is more important than
154 ;;; precision, and it is not permitted to return member types.
155 (defun type-of (object)
157 "Return the type of OBJECT."
161 ((<= 0 object 1) 'bit)
162 ((< object 0) 'fixnum)
163 (t '(integer 0 #.sb!xc:most-positive-fixnum))))
166 '(integer #.(1+ sb!xc:most-positive-fixnum))
168 (standard-char 'standard-char)
169 (base-char 'base-char)
170 (extended-char 'extended-char)
171 ((member t) 'boolean)
174 (type-specifier (ctype-of object)))
176 (let* ((classoid (layout-classoid (layout-of object)))
177 (name (classoid-name classoid)))
178 (if (%instancep object)
180 (sb!alien-internals:alien-value
182 ,(sb!alien-internals:unparse-alien-type
183 (sb!alien-internals:alien-value-type object))))
185 (let ((pname (classoid-proper-name classoid)))
186 (if (classoid-p pname)
187 (classoid-pcl-class pname)
191 ;;;; equality predicates
193 ;;; This is real simple, 'cause the compiler takes care of it.
194 (defun eq (obj1 obj2)
196 "Return T if OBJ1 and OBJ2 are the same object, otherwise NIL."
199 (declaim (inline %eql))
200 (defun %eql (obj1 obj2)
202 "Return T if OBJ1 and OBJ2 represent the same object, otherwise NIL."
204 (if (or (typep obj2 'fixnum)
205 (not (typep obj2 'number)))
207 (macrolet ((foo (&rest stuff)
209 ,@(mapcar (lambda (foo)
210 (let ((type (car foo))
213 (and (typep obj1 ',type)
223 (zerop (bignum-compare x y))))
226 (and (eql (numerator x) (numerator y))
227 (eql (denominator x) (denominator y)))))
230 (and (eql (realpart x) (realpart y))
231 (eql (imagpart x) (imagpart y))))))))))
236 (defun bit-vector-= (x y)
237 (declare (type bit-vector x y))
238 (if (and (simple-bit-vector-p x)
239 (simple-bit-vector-p y))
240 (bit-vector-= x y) ; DEFTRANSFORM
241 (and (= (length x) (length y))
246 (unless (= (bit x i) (bit y i))
251 "Return T if X and Y are EQL or if they are structured components whose
252 elements are EQUAL. Strings and bit-vectors are EQUAL if they are the same
253 length and have identical components. Other arrays must be EQ to be EQUAL."
254 ;; Non-tail self-recursion implemented with a local auxiliary function
255 ;; is a lot faster than doing it the straightforward way (at least
256 ;; on x86oids) due to calling convention differences. -- JES, 2005-12-30
257 (labels ((equal-aux (x y)
262 (equal-aux (car x) (car y))
263 (equal-aux (cdr x) (cdr y))))
265 (and (stringp y) (string= x y)))
267 (and (pathnamep y) (pathname= x y)))
269 (and (bit-vector-p y)
272 ;; Use MAYBE-INLINE to get the inline expansion only once (instead
273 ;; of 200 times with INLINE). -- JES, 2005-12-30
274 (declare (maybe-inline equal-aux))
277 ;;; EQUALP comparison of HASH-TABLE values
278 (defun hash-table-equalp (x y)
279 (declare (type hash-table x y))
281 (and (hash-table-p y)
282 (eql (hash-table-count x) (hash-table-count y))
283 (eql (hash-table-test x) (hash-table-test y))
284 (block comparison-of-entries
285 (maphash (lambda (key x-value)
286 (multiple-value-bind (y-value y-value-p)
288 (unless (and y-value-p (equalp x-value y-value))
289 (return-from comparison-of-entries nil))))
294 #+nil ; KLUDGE: If doc string, should be accurate: Talk about structures
296 "This is like EQUAL, except more liberal in several respects.
297 Numbers may be of different types, as long as the values are identical
298 after coercion. Characters may differ in alphabetic case. Vectors and
299 arrays must have identical dimensions and EQUALP elements, but may differ
300 in their type restriction."
302 ((characterp x) (and (characterp y) (char-equal x y)))
303 ((numberp x) (and (numberp y) (= x y)))
306 (equalp (car x) (car y))
307 (equalp (cdr x) (cdr y))))
309 (and (pathnamep y) (pathname= x y)))
311 (and (hash-table-p y)
312 (hash-table-equalp x y)))
314 (let* ((layout-x (%instance-layout x))
315 (raw-len (layout-n-untagged-slots layout-x))
316 (total-len (layout-length layout-x))
317 (normal-len (- total-len raw-len)))
319 (eq layout-x (%instance-layout y))
320 (structure-classoid-p (layout-classoid layout-x))
321 (dotimes (i normal-len t)
322 (let ((x-el (%instance-ref x i))
323 (y-el (%instance-ref y i)))
324 (unless (or (eq x-el y-el)
329 (raw-instance-slots-equalp layout-x x y)))))
331 (let ((length (length x)))
333 (= length (length y))
334 (dotimes (i length t)
335 (let ((x-el (aref x i))
337 (unless (or (eq x-el y-el)
342 (= (array-rank x) (array-rank y))
343 (dotimes (axis (array-rank x) t)
344 (unless (= (array-dimension x axis)
345 (array-dimension y axis))
347 (dotimes (index (array-total-size x) t)
348 (let ((x-el (row-major-aref x index))
349 (y-el (row-major-aref y index)))
350 (unless (or (eq x-el y-el)
355 (/show0 "about to do test cases in pred.lisp")
357 (let ((test-cases `((0.0 ,(load-time-value (make-unportable-float :single-float-negative-zero)) t)
359 (#c(1 0) #c(1.0 0.0) t)
360 (#c(0 1) #c(0.0 1.0) t)
361 (#c(1.1 0.0) #c(11/10 0) nil) ; due to roundoff error
363 ("Hello" #(#\h #\E #\l #\l #\o) t)
364 ("Hello" "goodbye" nil))))
365 (/show0 "TEST-CASES bound in pred.lisp")
366 (dolist (test-case test-cases)
367 (/show0 "about to do a TEST-CASE in pred.lisp")
368 (destructuring-bind (x y expected-result) test-case
369 (let* ((result (equalp x y))
370 (bresult (if result 1 0))
371 (expected-bresult (if expected-result 1 0)))
372 (unless (= bresult expected-bresult)
373 (/show0 "failing test in pred.lisp")
374 (error "failed test (EQUALP ~S ~S)" x y))))))
375 (/show0 "done with test cases in pred.lisp")