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 (simple-vector-p (%array-data-vector x)))))
26 ;;;; primitive predicates. These must be supported directly by the
31 "Return T if X is NIL, otherwise return NIL."
34 ;;; All the primitive type predicates share a parallel form..
38 ,@(mapcar (lambda (pred)
39 (let* ((name (symbol-name pred))
40 (stem (string-right-trim name "P-"))
41 (article (if (find (schar name 0) "AEIOU")
44 `(defun ,pred (object)
46 "Return T if OBJECT is ~A ~A, ~
62 complex-double-float-p
64 #!+long-float complex-long-float-p
66 complex-single-float-p
67 ;; (COMPLEX-VECTOR-P is not included here since
68 ;; it's awkward to express the type it tests for
69 ;; in the Common Lisp type system, and since
70 ;; it's only used in the implementation of a few
71 ;; specialized things.)
87 sb!kernel:simple-array-p
100 simple-array-unsigned-byte-2-p
101 simple-array-unsigned-byte-4-p
102 simple-array-unsigned-byte-8-p
103 simple-array-unsigned-byte-16-p
104 simple-array-unsigned-byte-32-p
105 simple-array-signed-byte-8-p
106 simple-array-signed-byte-16-p
107 simple-array-signed-byte-30-p
108 simple-array-signed-byte-32-p
109 simple-array-single-float-p
110 simple-array-double-float-p
111 #!+long-float simple-array-long-float-p
112 simple-array-complex-single-float-p
113 simple-array-complex-double-float-p
114 #!+long-float simple-array-complex-long-float-p
118 ;;; Return the specifier for the type of object. This is not simply
119 ;;; (TYPE-SPECIFIER (CTYPE-OF OBJECT)) because CTYPE-OF has different
120 ;;; goals than TYPE-OF. In particular, speed is more important than
121 ;;; precision, and it is not permitted to return member types.
122 (defun type-of (object)
124 "Return the type of OBJECT."
125 (if (typep object '(or function array complex))
126 (type-specifier (ctype-of object))
127 (let* ((class (layout-class (layout-of object)))
128 (name (class-name class)))
129 (if (typep object 'instance)
131 (sb!alien-internals:alien-value
133 ,(sb!alien-internals:unparse-alien-type
134 (sb!alien-internals:alien-value-type object))))
136 (class-proper-name class)))
139 ;;; FIXME: This belongs somewhere else, perhaps in code/array.lisp.
140 (defun upgraded-array-element-type (spec)
142 "Return the element type that will actually be used to implement an array
143 with the specifier :ELEMENT-TYPE Spec."
145 (array-type-specialized-element-type
146 (specifier-type `(array ,spec)))))
148 ;;;; equality predicates
150 ;;; This is real simple, 'cause the compiler takes care of it.
151 (defun eq (obj1 obj2)
153 "Return T if OBJ1 and OBJ2 are the same object, otherwise NIL."
158 "Returns T if X and Y are EQL or if they are structured components
159 whose elements are EQUAL. Strings and bit-vectors are EQUAL if they
160 are the same length and have identical components. Other arrays must be
165 (equal (car x) (car y))
166 (equal (cdr x) (cdr y))))
168 (and (stringp y) (string= x y)))
170 (and (pathnamep y) (pathname= x y)))
172 (and (bit-vector-p y)
173 (= (the fixnum (length x))
174 (the fixnum (length y)))
179 (or (= (the fixnum (bit x i))
180 (the fixnum (bit y i)))
184 ;;; EQUALP comparison of HASH-TABLE values
185 (defun hash-table-equalp (x y)
186 (declare (type hash-table x y))
188 (and (hash-table-p y)
189 (eql (hash-table-count x) (hash-table-count y))
190 (eql (hash-table-test x) (hash-table-test y))
191 (block comparison-of-entries
192 (maphash (lambda (key x-value)
193 (multiple-value-bind (y-value y-value-p)
195 (unless (and y-value-p (equalp x-value y-value))
196 (return-from comparison-of-entries nil))))
201 #+nil ; KLUDGE: If doc string, should be accurate: Talk about structures
203 "Just like EQUAL, but more liberal in several respects.
204 Numbers may be of different types, as long as the values are identical
205 after coercion. Characters may differ in alphabetic case. Vectors and
206 arrays must have identical dimensions and EQUALP elements, but may differ
207 in their type restriction."
209 ((characterp x) (and (characterp y) (char-equal x y)))
210 ((numberp x) (and (numberp y) (= x y)))
213 (equalp (car x) (car y))
214 (equalp (cdr x) (cdr y))))
216 (and (pathnamep y) (pathname= x y)))
218 (and (hash-table-p y)
219 (hash-table-equalp x y)))
221 (let* ((layout-x (%instance-layout x))
222 (len (layout-length layout-x)))
223 (and (typep y 'instance)
224 (eq layout-x (%instance-layout y))
225 (structure-class-p (layout-class layout-x))
229 (let ((x-el (%instance-ref x i))
230 (y-el (%instance-ref y i)))
231 (unless (or (eq x-el y-el)
235 (let ((length (length x)))
237 (= length (length y))
238 (dotimes (i length t)
239 (let ((x-el (aref x i))
241 (unless (or (eq x-el y-el)
246 (= (array-rank x) (array-rank y))
247 (dotimes (axis (array-rank x) t)
248 (unless (= (array-dimension x axis)
249 (array-dimension y axis))
251 (dotimes (index (array-total-size x) t)
252 (let ((x-el (row-major-aref x index))
253 (y-el (row-major-aref y index)))
254 (unless (or (eq x-el y-el)
259 (let ((test-cases '((0.0 -0.0 t)
261 (#c(1 0) #c(1.0 0) t)
262 (#c(1.1 0) #c(11/10 0) nil) ; due to roundoff error
264 ("Hello" #(#\h #\E #\l #\l #\o) t)
265 ("Hello" "goodbye" nil))))
266 (dolist (test-case test-cases)
267 (destructuring-bind (x y expected-result) test-case
268 (let* ((result (equalp x y))
269 (bresult (if result 1 0))
270 (expected-bresult (if expected-result 1 0)))
271 (unless (= bresult expected-bresult)
272 (error "failed test (EQUALP ~S ~S)" x y))))))