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")
17 ;;;; miscellaneous non-primitive predicates
19 #!-sb-fluid (declaim (inline streamp))
20 (defun streamp (stream)
21 (typep stream 'stream))
23 ;;; Is X a (VECTOR T)?
25 (or (simple-vector-p x)
26 (and (complex-vector-p x)
27 (simple-vector-p (%array-data-vector x)))))
29 ;;;; primitive predicates. These must be supported directly by the
34 "Return T if X is NIL, otherwise return NIL."
37 ;;; All the primitive type predicates share a parallel form..
41 ,@(mapcar (lambda (pred)
42 (let* ((name (symbol-name pred))
43 (stem (string-right-trim name "P-"))
44 (article (if (find (schar name 0) "AEIOU")
47 `(defun ,pred (object)
49 "Return T if OBJECT is ~A ~A, ~
65 complex-double-float-p
67 #!+long-float complex-long-float-p
69 complex-single-float-p
70 ;; (COMPLEX-VECTOR-P is not included here since
71 ;; it's awkward to express the type it tests for
72 ;; in the Common Lisp type system, and since
73 ;; it's only used in the implementation of a few
74 ;; specialized things.)
90 sb!kernel:simple-array-p
103 simple-array-unsigned-byte-2-p
104 simple-array-unsigned-byte-4-p
105 simple-array-unsigned-byte-8-p
106 simple-array-unsigned-byte-16-p
107 simple-array-unsigned-byte-32-p
108 simple-array-signed-byte-8-p
109 simple-array-signed-byte-16-p
110 simple-array-signed-byte-30-p
111 simple-array-signed-byte-32-p
112 simple-array-single-float-p
113 simple-array-double-float-p
114 #!+long-float simple-array-long-float-p
115 simple-array-complex-single-float-p
116 simple-array-complex-double-float-p
117 #!+long-float simple-array-complex-long-float-p
121 ;;; Return the specifier for the type of object. This is not simply
122 ;;; (TYPE-SPECIFIER (CTYPE-OF OBJECT)) because CTYPE-OF has different
123 ;;; goals than TYPE-OF. In particular, speed is more important than
124 ;;; precision, and it is not permitted to return member types.
125 (defun type-of (object)
127 "Return the type of OBJECT."
128 (if (typep object '(or function array complex))
129 (type-specifier (ctype-of object))
130 (let* ((class (layout-class (layout-of object)))
131 (name (class-name class)))
132 (if (typep object 'instance)
134 (sb!alien-internals:alien-value
136 ,(sb!alien-internals:unparse-alien-type
137 (sb!alien-internals:alien-value-type object))))
139 (class-proper-name class)))
142 ;;; FIXME: This belongs somewhere else, perhaps in code/array.lisp.
143 (defun upgraded-array-element-type (spec)
145 "Return the element type that will actually be used to implement an array
146 with the specifier :ELEMENT-TYPE Spec."
148 (array-type-specialized-element-type
149 (specifier-type `(array ,spec)))))
151 ;;;; equality predicates
153 ;;; This is real simple, 'cause the compiler takes care of it.
154 (defun eq (obj1 obj2)
156 "Return T if OBJ1 and OBJ2 are the same object, otherwise NIL."
161 "Returns T if X and Y are EQL or if they are structured components
162 whose elements are EQUAL. Strings and bit-vectors are EQUAL if they
163 are the same length and have identical components. Other arrays must be
168 (equal (car x) (car y))
169 (equal (cdr x) (cdr y))))
171 (and (stringp y) (string= x y)))
173 (and (pathnamep y) (pathname= x y)))
175 (and (bit-vector-p y)
176 (= (the fixnum (length x))
177 (the fixnum (length y)))
182 (or (= (the fixnum (bit x i))
183 (the fixnum (bit y i)))
187 ;;; EQUALP comparison of HASH-TABLE values
188 (defun hash-table-equalp (x y)
189 (declare (type hash-table x y))
191 (and (hash-table-p y)
192 (eql (hash-table-count x) (hash-table-count y))
193 (eql (hash-table-test x) (hash-table-test y))
194 (block comparison-of-entries
195 (maphash (lambda (key x-value)
196 (multiple-value-bind (y-value y-value-p)
198 (unless (and y-value-p (equalp x-value y-value))
199 (return-from comparison-of-entries nil))))
204 #+nil ; KLUDGE: If doc string, should be accurate: Talk about structures
206 "Just like EQUAL, but more liberal in several respects.
207 Numbers may be of different types, as long as the values are identical
208 after coercion. Characters may differ in alphabetic case. Vectors and
209 arrays must have identical dimensions and EQUALP elements, but may differ
210 in their type restriction."
212 ((characterp x) (and (characterp y) (char-equal x y)))
213 ((numberp x) (and (numberp y) (= x y)))
216 (equalp (car x) (car y))
217 (equalp (cdr x) (cdr y))))
219 (and (pathnamep y) (pathname= x y)))
221 (and (hash-table-p y)
222 (hash-table-equalp x y)))
224 (let* ((layout-x (%instance-layout x))
225 (len (layout-length layout-x)))
226 (and (typep y 'instance)
227 (eq layout-x (%instance-layout y))
228 (structure-class-p (layout-class layout-x))
232 (let ((x-el (%instance-ref x i))
233 (y-el (%instance-ref y i)))
234 (unless (or (eq x-el y-el)
238 (let ((length (length x)))
240 (= length (length y))
241 (dotimes (i length t)
242 (let ((x-el (aref x i))
244 (unless (or (eq x-el y-el)
249 (= (array-rank x) (array-rank y))
250 (dotimes (axis (array-rank x) t)
251 (unless (= (array-dimension x axis)
252 (array-dimension y axis))
254 (dotimes (index (array-total-size x) t)
255 (let ((x-el (row-major-aref x index))
256 (y-el (row-major-aref y index)))
257 (unless (or (eq x-el y-el)
262 (let ((test-cases '((0.0 -0.0 t)
264 (#c(1 0) #c(1.0 0) t)
265 (#c(1.1 0) #c(11/10 0) nil) ; due to roundoff error
267 ("Hello" #(#\h #\E #\l #\l #\o) t)
268 ("Hello" "goodbye" nil))))
269 (dolist (test-case test-cases)
270 (destructuring-bind (x y expected-result) test-case
271 (let* ((result (equalp x y))
272 (bresult (if result 1 0))
273 (expected-bresult (if expected-result 1 0)))
274 (unless (= bresult expected-bresult)
275 (error "failed test (EQUALP ~S ~S)" x y))))))