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 ;; This is like the "hierarchical layout depths for other things"
35 ;; case of the TYPEP transform (cf 'typetran'). Ideally it would
36 ;; be preferable to share TYPEP's code rather than repeat it here.
37 (declaim (maybe-inline extended-sequence-p
))
38 (defun extended-sequence-p (x)
39 (let* ((slayout #.
(info :type
:compiler-layout
'sequence
))
40 (depthoid #.
(layout-depthoid (info :type
:compiler-layout
'sequence
)))
42 ;; It is not an error to define a class that is both SEQUENCE and
43 ;; FUNCALLABLE-INSTANCE with metaclass FUNCALLABLE-STANDARD-CLASS
44 (cond ((%instancep x
) (%instance-layout x
))
45 ((funcallable-instance-p x
) (%funcallable-instance-layout x
))
46 (t (return-from extended-sequence-p nil
)))))
47 (when (layout-invalid layout
)
48 (setq layout
(update-object-layout-or-invalid x slayout
)))
49 ;; It's _nearly_ impossible to create an instance which is exactly
50 ;; of type SEQUENCE. To wit: (make-instance 'sequence) =>
51 ;; "Cannot allocate an instance of #<BUILT-IN-CLASS SEQUENCE>."
52 ;; We should not need to check for that, just the 'inherits' vector.
53 ;; However, bootstrap code does a sleazy thing, making an instance of
54 ;; the abstract base type which is impossible for user code to do.
55 ;; Preferably the prototype instance for SEQUENCE would be one that could
56 ;; exist, so it would be a STANDARD-OBJECT and SEQUENCE. But it's not.
57 ;; Hence we have to check for a layout that no code using the documented
58 ;; sequence API would ever see, just to get the boundary case right.
60 ;; - Some builtins use a prototype object that is strictly deeper than
61 ;; layout of the named class because it is indeed the case that no
62 ;; object's layout can ever be EQ to that of the ancestor.
63 ;; e.g. a fixnum as representative of class REAL
64 ;; - Some builtins actually fail (TYPEP (CLASS-PROTOTYPE X) X)
65 ;; but that's not an excuse for getting SEQUENCE wrong:
66 ;; (CLASS-PROTOTYPE (FIND-CLASS 'FLOAT)) => 42
67 ;; (CLASS-PROTOTYPE (FIND-CLASS 'VECTOR)) => 42
68 ;; (CLASS-PROTOTYPE (FIND-CLASS 'LIST)) => 42
69 ;; (CLASS-PROTOTYPE (FIND-CLASS 'STRING)) => 42
70 (let ((inherits (layout-inherits (truly-the layout layout
))))
71 (declare (optimize (safety 0)))
72 (eq (if (> (length inherits
) depthoid
) (svref inherits depthoid
) layout
)
75 ;;; Is X a SEQUENCE? Harder than just (OR VECTOR LIST)
77 (declare (inline extended-sequence-p
))
78 (or (listp x
) (vectorp x
) (extended-sequence-p x
)))
80 ;;;; primitive predicates. These must be supported directly by the
85 "Return T if X is NIL, otherwise return NIL."
88 ;;; All the primitive type predicate wrappers share a parallel form..
89 (macrolet ((def-type-predicate-wrapper (pred)
90 (let* ((name (symbol-name pred
))
91 (stem (string-left-trim "%" (string-right-trim "P-" name
)))
92 (article (if (position (schar name
0) "AEIOU") "an" "a")))
93 `(defun ,pred
(object)
96 "Return true if OBJECT is ~A ~A, and NIL otherwise."
99 ;; (falling through to low-level implementation)
101 (def-type-predicate-wrapper array-header-p
)
102 (def-type-predicate-wrapper arrayp
)
103 (def-type-predicate-wrapper atom
)
104 ;; Testing for BASE-CHAR-P is usually redundant on #-sb-unicode,
105 ;; remove it there completely so that #-sb-unicode build will
106 ;; break when it's used.
107 #!+sb-unicode
(def-type-predicate-wrapper base-char-p
)
108 (def-type-predicate-wrapper base-string-p
)
109 #!+sb-unicode
(def-type-predicate-wrapper character-string-p
)
110 (def-type-predicate-wrapper bignump
)
111 (def-type-predicate-wrapper bit-vector-p
)
112 (def-type-predicate-wrapper characterp
)
113 (def-type-predicate-wrapper code-component-p
)
114 (def-type-predicate-wrapper consp
)
115 (def-type-predicate-wrapper compiled-function-p
)
116 (def-type-predicate-wrapper complexp
)
117 (def-type-predicate-wrapper complex-double-float-p
)
118 (def-type-predicate-wrapper complex-float-p
)
119 #!+long-float
(def-type-predicate-wrapper complex-long-float-p
)
120 (def-type-predicate-wrapper complex-rational-p
)
121 (def-type-predicate-wrapper complex-single-float-p
)
122 ;; (COMPLEX-VECTOR-P is not included here since it's awkward to express
123 ;; the type it tests for in the Common Lisp type system, and since it's
124 ;; only used in the implementation of a few specialized things.)
125 (def-type-predicate-wrapper double-float-p
)
126 (def-type-predicate-wrapper extended-char-p
)
127 (def-type-predicate-wrapper fdefn-p
)
128 (def-type-predicate-wrapper fixnump
)
129 (def-type-predicate-wrapper floatp
)
130 (def-type-predicate-wrapper functionp
)
131 (def-type-predicate-wrapper integerp
)
132 (def-type-predicate-wrapper listp
)
133 (def-type-predicate-wrapper long-float-p
)
134 (def-type-predicate-wrapper lra-p
)
135 (def-type-predicate-wrapper null
)
136 (def-type-predicate-wrapper numberp
)
137 (def-type-predicate-wrapper rationalp
)
138 (def-type-predicate-wrapper ratiop
)
139 (def-type-predicate-wrapper realp
)
140 (def-type-predicate-wrapper short-float-p
)
141 (def-type-predicate-wrapper single-float-p
)
142 #!+sb-simd-pack
(def-type-predicate-wrapper simd-pack-p
)
143 (def-type-predicate-wrapper %instancep
)
144 (def-type-predicate-wrapper symbolp
)
145 (def-type-predicate-wrapper %other-pointer-p
)
146 (def-type-predicate-wrapper system-area-pointer-p
)
147 (def-type-predicate-wrapper weak-pointer-p
)
148 #!+#.
(cl:if
(cl:= 32 sb
!vm
:n-word-bits
) '(and) '(or))
150 (def-type-predicate-wrapper unsigned-byte-32-p
)
151 (def-type-predicate-wrapper signed-byte-32-p
))
152 #!+#.
(cl:if
(cl:= 64 sb
!vm
:n-word-bits
) '(and) '(or))
154 (def-type-predicate-wrapper unsigned-byte-64-p
)
155 (def-type-predicate-wrapper signed-byte-64-p
))
156 ;; Specialized array types
157 (macrolet ((saetp-defs ()
161 `(def-type-predicate-wrapper
162 ,(symbolicate (sb!vm
:saetp-primitive-type-name saetp
) "-P")))
163 sb
!vm
:*specialized-array-element-type-properties
*))))
166 (def-type-predicate-wrapper simple-array-p
)
167 (def-type-predicate-wrapper simple-rank-1-array-
*-p
)
168 (def-type-predicate-wrapper simple-string-p
)
169 (def-type-predicate-wrapper stringp
)
170 (def-type-predicate-wrapper vectorp
)
171 (def-type-predicate-wrapper vector-nil-p
))
173 #!+(or x86 x86-64 arm
)
174 (defun fixnum-mod-p (x limit
)
178 #!+(or x86 x86-64 ppc
)
179 (defun %other-pointer-subtype-p
(x choices
)
180 (and (%other-pointer-p x
)
181 (member (%other-pointer-widetag x
) choices
)
184 ;;; Return the specifier for the type of object. This is not simply
185 ;;; (TYPE-SPECIFIER (CTYPE-OF OBJECT)) because CTYPE-OF has different
186 ;;; goals than TYPE-OF. In particular, speed is more important than
187 ;;; precision here, and it is not permitted to return member types,
188 ;;; negation, union, or intersection types.
189 (defun type-of (object)
191 "Return the type of OBJECT."
192 ;; We have special logic for everything except arrays.
193 ;; Arrays use CTYPE-OF and then convert the answer to a specifier.
197 ((<= 0 object
1) 'bit
)
198 ((< object
0) 'fixnum
)
199 (t '(integer 0 #.sb
!xc
:most-positive-fixnum
))))
202 '(integer #.
(1+ sb
!xc
:most-positive-fixnum
))
204 (standard-char 'standard-char
)
205 (base-char 'base-char
)
206 (extended-char 'extended-char
)
207 ((member t
) 'boolean
)
209 ((or array complex
#!+sb-simd-pack simd-pack
)
210 (let ((sb!kernel
::*unparse-allow-negation
* nil
))
211 (declare (special sb
!kernel
::*unparse-allow-negation
*)) ; forward ref
212 (type-specifier (ctype-of object
))))
214 (let* ((classoid (layout-classoid (layout-of object
)))
215 (name (classoid-name classoid
)))
216 (if (%instancep object
)
218 (sb!alien-internals
:alien-value
220 ,(sb!alien-internals
:unparse-alien-type
221 (sb!alien-internals
:alien-value-type object
))))
223 (let ((pname (classoid-proper-name classoid
)))
224 (if (classoid-p pname
)
225 (classoid-pcl-class pname
)
229 ;;;; equality predicates
231 ;;; This is real simple, 'cause the compiler takes care of it.
232 (defun eq (obj1 obj2
)
234 "Return T if OBJ1 and OBJ2 are the same object, otherwise NIL."
236 ;;; and this too, but it's only needed for backends on which
237 ;;; IR1 might potentially transform EQL into %EQL/INTEGER.
239 (defun %eql
/integer
(obj1 obj2
)
240 (eql (the integer obj1
) (the integer obj2
)))
242 (declaim (inline %eql
))
243 (defun %eql
(obj1 obj2
)
245 "Return T if OBJ1 and OBJ2 represent the same object, otherwise NIL."
247 (if (or (typep obj2
'fixnum
)
248 (not (typep obj2
'number
)))
250 ;; I would think that we could do slightly better here by testing that
251 ;; both objs are OTHER-POINTER-P with equal %OTHER-POINTER-WIDETAGs.
252 ;; Then dispatch on obj2 and elide the TYPEP on obj1 using TRULY-THE.
253 ;; Also would need to deal with immediate single-float for 64-bit.
254 (macrolet ((foo (&rest stuff
)
256 ,@(mapcar (lambda (foo)
257 (let ((type (car foo
))
260 (and (typep obj1
',type
)
269 #!-integer-eql-vop
(lambda (x y
) (zerop (bignum-compare x y
)))
270 #!+integer-eql-vop eql
) ; will become %eql/integer
273 (and (eql (numerator x
) (numerator y
))
274 (eql (denominator x
) (denominator y
)))))
277 (and (eql (realpart x
) (realpart y
))
278 (eql (imagpart x
) (imagpart y
))))))))))
283 (defun bit-vector-= (x y
)
284 (declare (type bit-vector x y
))
286 ((and (simple-bit-vector-p x
)
287 (simple-bit-vector-p y
))
288 (bit-vector-= x y
)) ; DEFTRANSFORM
290 (and (= (length x
) (length y
))
295 (unless (= (bit x i
) (bit y i
))
300 "Return T if X and Y are EQL or if they are structured components whose
301 elements are EQUAL. Strings and bit-vectors are EQUAL if they are the same
302 length and have identical components. Other arrays must be EQ to be EQUAL."
303 ;; Non-tail self-recursion implemented with a local auxiliary function
304 ;; is a lot faster than doing it the straightforward way (at least
305 ;; on x86oids) due to calling convention differences. -- JES, 2005-12-30
306 (labels ((equal-aux (x y
)
311 (equal-aux (car x
) (car y
))
312 (equal-aux (cdr x
) (cdr y
))))
314 (and (stringp y
) (string= x y
)))
316 (and (pathnamep y
) (pathname= x y
)))
318 (and (bit-vector-p y
)
321 ;; Use MAYBE-INLINE to get the inline expansion only once (instead
322 ;; of 200 times with INLINE). -- JES, 2005-12-30
323 (declare (maybe-inline equal-aux
))
326 ;;; EQUALP comparison of HASH-TABLE values
327 (defun hash-table-equalp (x y
)
328 (declare (type hash-table x y
))
330 (and (hash-table-p y
)
331 (eql (hash-table-count x
) (hash-table-count y
))
332 (eql (hash-table-test x
) (hash-table-test y
))
333 (block comparison-of-entries
334 (maphash (lambda (key x-value
)
335 (multiple-value-bind (y-value y-value-p
)
337 (unless (and y-value-p
(equalp x-value y-value
))
338 (return-from comparison-of-entries nil
))))
343 #+nil
; KLUDGE: If doc string, should be accurate: Talk about structures
345 "This is like EQUAL, except more liberal in several respects.
346 Numbers may be of different types, as long as the values are identical
347 after coercion. Characters may differ in alphabetic case. Vectors and
348 arrays must have identical dimensions and EQUALP elements, but may differ
349 in their type restriction."
351 ((characterp x
) (and (characterp y
) (char-equal x y
)))
352 ((numberp x
) (and (numberp y
) (= x y
)))
355 (equalp (car x
) (car y
))
356 (equalp (cdr x
) (cdr y
))))
358 (and (pathnamep y
) (pathname= x y
)))
360 (and (hash-table-p y
)
361 (hash-table-equalp x y
)))
363 (let ((layout-x (%instance-layout x
)))
365 (eq layout-x
(%instance-layout y
))
366 ;; TODO: store one bit in the layout indicating whether EQUALP
367 ;; should scan slots. (basically a STRUCTURE-CLASSOID-P bit)
368 (structure-classoid-p (layout-classoid layout-x
))
369 (macrolet ((slot-ref-equalp ()
370 `(let ((x-el (%instance-ref x i
))
371 (y-el (%instance-ref y i
)))
372 (or (eq x-el y-el
) (equalp x-el y-el
)))))
373 #!-interleaved-raw-slots
374 (let ((raw-len (layout-n-untagged-slots layout-x
))
375 (total-len (layout-length layout-x
)))
376 (and (dotimes (i (- total-len raw-len
) t
)
377 (unless (slot-ref-equalp)
380 (raw-instance-slots-equalp layout-x x y
))))
381 #!+interleaved-raw-slots
382 (let ((metadata (layout-untagged-bitmap layout-x
)))
384 (loop for i of-type index from sb
!vm
:instance-data-start
385 below
(layout-length layout-x
)
386 always
(slot-ref-equalp))
387 (let ((comparators (layout-equalp-tests layout-x
)))
388 (unless (= (length comparators
)
389 (- (layout-length layout-x
) sb
!vm
:instance-data-start
))
390 (bug "EQUALP got incomplete instance layout"))
391 ;; See remark at the source code for %TARGET-DEFSTRUCT
392 ;; explaining how to use the vector of comparators.
393 (loop for i of-type index from sb
!vm
:instance-data-start
394 below
(layout-length layout-x
)
395 for test
= (data-vector-ref
396 comparators
(- i sb
!vm
:instance-data-start
))
397 always
(cond ((eql test
0) (slot-ref-equalp))
399 (funcall test i x y
))
402 (let ((length (length x
)))
404 (= length
(length y
))
405 (dotimes (i length t
)
406 (let ((x-el (aref x i
))
408 (unless (or (eq x-el y-el
)
413 (= (array-rank x
) (array-rank y
))
414 (dotimes (axis (array-rank x
) t
)
415 (unless (= (array-dimension x axis
)
416 (array-dimension y axis
))
418 (dotimes (index (array-total-size x
) t
)
419 (let ((x-el (row-major-aref x index
))
420 (y-el (row-major-aref y index
)))
421 (unless (or (eq x-el y-el
)
426 (/show0
"about to do test cases in pred.lisp")
428 (let ((test-cases `((0.0
,(load-time-value (make-unportable-float :single-float-negative-zero
)) t
)
430 (#c
(1 0) #c
(1.0
0.0) t
)
431 (#c
(0 1) #c
(0.0
1.0) t
)
432 (#c
(1.1
0.0) #c
(11/10 0) nil
) ; due to roundoff error
434 ("Hello" #(#\h
#\E
#\l
#\l
#\o
) t
)
435 ("Hello" "goodbye" nil
))))
436 (/show0
"TEST-CASES bound in pred.lisp")
437 (dolist (test-case test-cases
)
438 (/show0
"about to do a TEST-CASE in pred.lisp")
439 (destructuring-bind (x y expected-result
) test-case
440 (let* ((result (equalp x y
))
441 (bresult (if result
1 0))
442 (expected-bresult (if expected-result
1 0)))
443 (unless (= bresult expected-bresult
)
444 (/show0
"failing test in pred.lisp")
445 (error "failed test (EQUALP ~S ~S)" x y
))))))
446 (/show0
"done with test cases in pred.lisp")