Delete :sb-test feature. Always do those tests
[sbcl.git] / src / code / pred.lisp
blobdaa41f91be1390ecf5f509785ce6d5c731d00dda
1 ;;;; predicate functions (EQUAL and friends, and type predicates)
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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
21 ;;; widetag tests
22 (macrolet
23 ((def ()
24 `(progn
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 (*))))))))))))
31 (def))
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)))
41 (layout
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.
59 ;; Note also:
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)
73 slayout))))
75 ;;; Is X a SEQUENCE? Harder than just (OR VECTOR LIST)
76 (defun sequencep (x)
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
81 ;;;; compiler.
83 (defun not (object)
84 "Return T if X is NIL, otherwise return NIL."
85 (not object))
87 ;;; All the primitive type predicate wrappers share a parallel form..
88 (macrolet ((def-type-predicate-wrapper (pred)
89 (let* ((name (symbol-name pred))
90 (stem (string-left-trim "%" (string-right-trim "P-" name)))
91 (article (if (position (schar name 0) "AEIOU") "an" "a")))
92 `(defun ,pred (object)
93 ,(format nil
94 "Return true if OBJECT is ~A ~A, and NIL otherwise."
95 article
96 stem)
97 ;; (falling through to low-level implementation)
98 (,pred object)))))
99 (def-type-predicate-wrapper array-header-p)
100 (def-type-predicate-wrapper arrayp)
101 (def-type-predicate-wrapper atom)
102 ;; Testing for BASE-CHAR-P is usually redundant on #-sb-unicode,
103 ;; remove it there completely so that #-sb-unicode build will
104 ;; break when it's used.
105 #!+sb-unicode (def-type-predicate-wrapper base-char-p)
106 (def-type-predicate-wrapper base-string-p)
107 #!+sb-unicode (def-type-predicate-wrapper character-string-p)
108 (def-type-predicate-wrapper bignump)
109 (def-type-predicate-wrapper bit-vector-p)
110 (def-type-predicate-wrapper characterp)
111 (def-type-predicate-wrapper code-component-p)
112 (def-type-predicate-wrapper consp)
113 (def-type-predicate-wrapper compiled-function-p)
114 (def-type-predicate-wrapper complexp)
115 (def-type-predicate-wrapper complex-double-float-p)
116 (def-type-predicate-wrapper complex-float-p)
117 #!+long-float (def-type-predicate-wrapper complex-long-float-p)
118 (def-type-predicate-wrapper complex-rational-p)
119 (def-type-predicate-wrapper complex-single-float-p)
120 ;; (COMPLEX-VECTOR-P is not included here since it's awkward to express
121 ;; the type it tests for in the Common Lisp type system, and since it's
122 ;; only used in the implementation of a few specialized things.)
123 (def-type-predicate-wrapper double-float-p)
124 (def-type-predicate-wrapper extended-char-p)
125 (def-type-predicate-wrapper fdefn-p)
126 (def-type-predicate-wrapper fixnump)
127 (def-type-predicate-wrapper floatp)
128 (def-type-predicate-wrapper functionp)
129 (def-type-predicate-wrapper integerp)
130 (def-type-predicate-wrapper listp)
131 (def-type-predicate-wrapper long-float-p)
132 #!-(or x86 x86-64) (def-type-predicate-wrapper lra-p)
133 (def-type-predicate-wrapper null)
134 (def-type-predicate-wrapper numberp)
135 (def-type-predicate-wrapper rationalp)
136 (def-type-predicate-wrapper ratiop)
137 (def-type-predicate-wrapper realp)
138 (def-type-predicate-wrapper short-float-p)
139 (def-type-predicate-wrapper single-float-p)
140 #!+sb-simd-pack (def-type-predicate-wrapper simd-pack-p)
141 (def-type-predicate-wrapper %instancep)
142 (def-type-predicate-wrapper symbolp)
143 (def-type-predicate-wrapper %other-pointer-p)
144 (def-type-predicate-wrapper system-area-pointer-p)
145 (def-type-predicate-wrapper weak-pointer-p)
146 #!-64-bit
147 (progn
148 (def-type-predicate-wrapper unsigned-byte-32-p)
149 (def-type-predicate-wrapper signed-byte-32-p))
150 #!+64-bit
151 (progn
152 (def-type-predicate-wrapper unsigned-byte-64-p)
153 (def-type-predicate-wrapper signed-byte-64-p))
154 ;; Specialized array types
155 (macrolet ((saetp-defs ()
156 `(progn
157 ,@(map 'list
158 (lambda (saetp)
159 `(def-type-predicate-wrapper
160 ,(symbolicate (sb!vm:saetp-primitive-type-name saetp) "-P")))
161 sb!vm:*specialized-array-element-type-properties*))))
162 (saetp-defs))
163 ;; Other array types
164 (def-type-predicate-wrapper simple-array-p)
165 (def-type-predicate-wrapper simple-rank-1-array-*-p)
166 (def-type-predicate-wrapper simple-string-p)
167 (def-type-predicate-wrapper stringp)
168 (def-type-predicate-wrapper vectorp)
169 (def-type-predicate-wrapper vector-nil-p))
171 #!+(or x86 x86-64 arm arm64)
172 (defun fixnum-mod-p (x limit)
173 (and (fixnump x)
174 (<= 0 x limit)))
176 #!+(or x86 x86-64 ppc)
177 (defun %other-pointer-subtype-p (x choices)
178 (and (%other-pointer-p x)
179 (member (%other-pointer-widetag x) choices)
182 ;;; Return the layout for an object. This is the basic operation for
183 ;;; finding out the "type" of an object, and is used for generic
184 ;;; function dispatch. The standard doesn't seem to say as much as it
185 ;;; should about what this returns for built-in objects. For example,
186 ;;; it seems that we must return NULL rather than LIST when X is NIL
187 ;;; so that GF's can specialize on NULL.
188 (declaim (inline layout-of))
189 #-sb-xc-host
190 (defun layout-of (x)
191 (declare (optimize (speed 3) (safety 0)))
192 (cond ((%instancep x) (%instance-layout x))
193 ((funcallable-instance-p x) (%funcallable-instance-layout x))
194 ;; Compiler can dump literal layouts, which handily sidesteps
195 ;; the question of when cold-init runs L-T-V forms.
196 ((null x) #.(find-layout 'null))
198 ;; Note that WIDETAG-OF is slightly suboptimal here and could be
199 ;; improved - we've already ruled out some of the lowtags.
200 (svref (load-time-value sb!kernel::**built-in-class-codes** t)
201 (widetag-of x)))))
203 (declaim (inline classoid-of))
204 #-sb-xc-host
205 (defun classoid-of (object)
206 "Return the class of the supplied object, which may be any Lisp object, not
207 just a CLOS STANDARD-OBJECT."
208 (layout-classoid (layout-of object)))
210 ;;; Return the specifier for the type of object. This is not simply
211 ;;; (TYPE-SPECIFIER (CTYPE-OF OBJECT)) because CTYPE-OF has different
212 ;;; goals than TYPE-OF. In particular, speed is more important than
213 ;;; precision here, and it is not permitted to return member types,
214 ;;; negation, union, or intersection types.
215 (defun type-of (object)
216 "Return the type of OBJECT."
217 (declare (explicit-check))
218 ;; We have special logic for everything except arrays.
219 ;; Arrays use CTYPE-OF and then convert the answer to a specifier.
220 (typecase object
221 (fixnum
222 (cond
223 ((<= 0 object 1) 'bit)
224 ((< object 0) 'fixnum)
225 (t '(integer 0 #.sb!xc:most-positive-fixnum))))
226 (integer
227 (if (>= object 0)
228 '(integer #.(1+ sb!xc:most-positive-fixnum))
229 'bignum))
230 (character
231 (typecase object
232 (standard-char 'standard-char)
233 (base-char 'base-char)
234 (extended-char 'extended-char)))
235 ;; We "have to" (or have chosen to) pick off KEYWORD and BOOLEAN,
236 ;; so we may as well have a branch that returns early for any SYMBOL
237 ;; rather than falling into the CLASSOID-based test. But then since we
238 ;; do that, we also have to pick off NIL so that it doesn't say SYMBOL.
239 (symbol
240 (cond ((eq object t) 'boolean)
241 ((eq object nil) 'null)
242 ((eq (symbol-package object) *keyword-package*) 'keyword)
243 (t 'symbol)))
244 ((or array complex #!+sb-simd-pack simd-pack)
245 (let ((sb!kernel::*unparse-allow-negation* nil))
246 (declare (special sb!kernel::*unparse-allow-negation*)) ; forward ref
247 (type-specifier (ctype-of object))))
249 (let* ((classoid (classoid-of object))
250 (name (classoid-name classoid)))
251 (if (%instancep object)
252 (case name
253 (sb!alien-internals:alien-value
254 `(alien
255 ,(sb!alien-internals:unparse-alien-type
256 (sb!alien-internals:alien-value-type object))))
258 (let ((pname (classoid-proper-name classoid)))
259 (if (classoid-p pname)
260 (classoid-pcl-class pname)
261 pname))))
262 name)))))
264 ;;;; equality predicates
266 ;;; This is real simple, 'cause the compiler takes care of it.
267 (defun eq (obj1 obj2)
268 "Return T if OBJ1 and OBJ2 are the same object, otherwise NIL."
269 (eq obj1 obj2))
270 ;;; and this too, but it's only needed for backends on which
271 ;;; IR1 might potentially transform EQL into %EQL/INTEGER.
272 #!+integer-eql-vop
273 (defun %eql/integer (obj1 obj2)
274 ;; This is just for constant folding, no need to transform into the %EQL/INTEGER VOP
275 (eql obj1 obj2))
277 (declaim (inline %eql))
278 (defun %eql (obj1 obj2)
279 "Return T if OBJ1 and OBJ2 represent the same object, otherwise NIL."
280 (or (eq obj1 obj2)
281 (if (or (typep obj2 'fixnum)
282 (not (typep obj2 'number)))
284 ;; I would think that we could do slightly better here by testing that
285 ;; both objs are OTHER-POINTER-P with equal %OTHER-POINTER-WIDETAGs.
286 ;; Then dispatch on obj2 and elide the TYPEP on obj1 using TRULY-THE.
287 ;; Also would need to deal with immediate single-float for 64-bit.
288 (macrolet ((foo (&rest stuff)
289 `(typecase obj2
290 ,@(mapcar (lambda (foo)
291 (let ((type (car foo))
292 (fn (cadr foo)))
293 `(,type
294 (and (typep obj1 ',type)
295 (,fn obj1 obj2)))))
296 stuff))))
297 (foo
298 (single-float eql)
299 (double-float eql)
300 #!+long-float
301 (long-float eql)
302 (bignum
303 #!-integer-eql-vop (lambda (x y) (zerop (bignum-compare x y)))
304 #!+integer-eql-vop eql) ; will become %eql/integer
305 (ratio
306 (lambda (x y)
307 (and (eql (numerator x) (numerator y))
308 (eql (denominator x) (denominator y)))))
309 ((complex single-float)
310 (lambda (x y)
311 (and (eql (realpart x) (realpart y))
312 (eql (imagpart x) (imagpart y)))))
313 ((complex double-float)
314 (lambda (x y)
315 (and (eql (realpart x) (realpart y))
316 (eql (imagpart x) (imagpart y)))))
317 ((complex rational)
318 (lambda (x y)
319 (and (eql (realpart x) (realpart y))
320 (eql (imagpart x) (imagpart y))))))))))
322 (defun eql (x y)
323 (%eql x y))
325 (defun bit-vector-= (x y)
326 (declare (type bit-vector x y))
327 (cond ((eq x y))
328 ((and (simple-bit-vector-p x)
329 (simple-bit-vector-p y))
330 (bit-vector-= x y)) ; DEFTRANSFORM
332 (and (= (length x) (length y))
333 (with-array-data ((x x) (start-x) (end-x) :force-inline t
334 :check-fill-pointer t)
335 (with-array-data ((y y) (start-y) (end-y) :force-inline t
336 :check-fill-pointer t)
337 (declare (ignore end-y))
338 (loop for x-i fixnum from start-x below end-x
339 for y-i fixnum from start-y
340 always (or (= (sbit x x-i)
341 (sbit y y-i))))))))))
343 (defun equal (x y)
344 "Return T if X and Y are EQL or if they are structured components whose
345 elements are EQUAL. Strings and bit-vectors are EQUAL if they are the same
346 length and have identical components. Other arrays must be EQ to be EQUAL."
347 ;; Non-tail self-recursion implemented with a local auxiliary function
348 ;; is a lot faster than doing it the straightforward way (at least
349 ;; on x86oids) due to calling convention differences. -- JES, 2005-12-30
350 (labels ((equal-aux (x y)
351 (cond ((%eql x y)
353 ((consp x)
354 (and (consp y)
355 (equal-aux (car x) (car y))
356 (equal-aux (cdr x) (cdr y))))
357 ((stringp x)
358 (and (stringp y) (string= x y)))
359 ((pathnamep x)
360 (and (pathnamep y) (pathname= x y)))
361 ((bit-vector-p x)
362 (and (bit-vector-p y)
363 (bit-vector-= x y)))
364 (t nil))))
365 ;; Use MAYBE-INLINE to get the inline expansion only once (instead
366 ;; of 200 times with INLINE). -- JES, 2005-12-30
367 (declare (maybe-inline equal-aux))
368 (equal-aux x y)))
370 ;;; EQUALP comparison of HASH-TABLE values
371 (defun hash-table-equalp (x y)
372 (declare (type hash-table x y))
373 (or (eq x y)
374 (and (hash-table-p y)
375 (eql (hash-table-count x) (hash-table-count y))
376 (eql (hash-table-test x) (hash-table-test y))
377 (block comparison-of-entries
378 (maphash (lambda (key x-value)
379 (multiple-value-bind (y-value y-value-p)
380 (gethash key y)
381 (unless (and y-value-p (equalp x-value y-value))
382 (return-from comparison-of-entries nil))))
384 t))))
386 (defun instance-equalp (x y)
387 (let ((layout-x (%instance-layout x)))
388 (and
389 (eq layout-x (%instance-layout y))
390 ;; TODO: store one bit in the layout indicating whether EQUALP
391 ;; should scan slots. (basically a STRUCTURE-CLASSOID-P bit)
392 (structure-classoid-p (layout-classoid layout-x))
393 (macrolet ((slot-ref-equalp ()
394 `(let ((x-el (%instance-ref x i))
395 (y-el (%instance-ref y i)))
396 (or (eq x-el y-el) (equalp x-el y-el)))))
397 (if (eql (layout-bitmap layout-x) sb!kernel::+layout-all-tagged+)
398 (loop for i of-type index from sb!vm:instance-data-start
399 below (layout-length layout-x)
400 always (slot-ref-equalp))
401 (let ((comparators (layout-equalp-tests layout-x)))
402 (unless (= (length comparators)
403 (- (layout-length layout-x) sb!vm:instance-data-start))
404 (bug "EQUALP got incomplete instance layout"))
405 ;; See remark at the source code for %TARGET-DEFSTRUCT
406 ;; explaining how to use the vector of comparators.
407 (loop for i of-type index from sb!vm:instance-data-start
408 below (layout-length layout-x)
409 for test = (data-vector-ref
410 comparators (- i sb!vm:instance-data-start))
411 always (cond ((eql test 0) (slot-ref-equalp))
412 ((functionp test)
413 (funcall test i x y))
414 (t)))))))))
416 ;;; Doesn't work on simple vectors
417 (defun array-equal-p (x y)
418 (declare (array x y))
419 (let ((rank (array-rank x)))
420 (and
421 (= rank (array-rank y))
422 (dotimes (axis rank t)
423 (unless (= (%array-dimension x axis)
424 (%array-dimension y axis))
425 (return nil)))
426 (with-array-data ((x x) (start-x) (end-x) :force-inline t
427 :array-header-p t)
428 (with-array-data ((y y) (start-y) (end-y) :force-inline t
429 :array-header-p t)
430 (declare (ignore end-y))
431 (let* ((reffers %%data-vector-reffers%%)
432 (getter-x (truly-the function (svref reffers (%other-pointer-widetag x))))
433 (getter-y (truly-the function (svref reffers (%other-pointer-widetag y)))))
434 (loop for x-i fixnum from start-x below end-x
435 for y-i fixnum from start-y
436 for x-el = (funcall getter-x x x-i)
437 for y-el = (funcall getter-y y y-i)
438 always (or (eq x-el y-el)
439 (equalp x-el y-el)))))))))
441 (defun vector-equalp (x y)
442 (declare (vector x y))
443 (let ((length (length x)))
444 (and (= length (length y))
445 (with-array-data ((x x) (start-x) (end-x) :force-inline t
446 :check-fill-pointer t)
447 (with-array-data ((y y) (start-y) (end-y) :force-inline t
448 :check-fill-pointer t)
449 (declare (ignore end-y))
450 (let* ((reffers %%data-vector-reffers%%)
451 (getter-x (truly-the function (svref reffers (%other-pointer-widetag x))))
452 (getter-y (truly-the function (svref reffers (%other-pointer-widetag y)))))
453 (loop for x-i fixnum from start-x below end-x
454 for y-i fixnum from start-y
455 for x-el = (funcall getter-x x x-i)
456 for y-el = (funcall getter-y y y-i)
457 always (or (eq x-el y-el)
458 (equalp x-el y-el)))))))))
460 (defun equalp (x y)
461 #+nil ; KLUDGE: If doc string, should be accurate: Talk about structures
462 ; and HASH-TABLEs.
463 "This is like EQUAL, except more liberal in several respects.
464 Numbers may be of different types, as long as the values are identical
465 after coercion. Characters may differ in alphabetic case. Vectors and
466 arrays must have identical dimensions and EQUALP elements, but may differ
467 in their type restriction."
468 (cond ((eq x y) t)
469 ((characterp x) (and (characterp y) (char-equal x y)))
470 ((numberp x) (and (numberp y) (= x y)))
471 ((consp x)
472 (and (consp y)
473 (equalp (car x) (car y))
474 (equalp (cdr x) (cdr y))))
475 ((pathnamep x)
476 (and (pathnamep y) (pathname= x y)))
477 ((hash-table-p x)
478 (and (hash-table-p y)
479 (hash-table-equalp x y)))
480 ((%instancep x)
481 (and (%instancep y)
482 (instance-equalp x y)))
483 ((and (bit-vector-p x)
484 (bit-vector-p y))
485 (bit-vector-= x y))
486 ((vectorp x)
487 (and (vectorp y)
488 (vector-equalp x y)))
489 ((arrayp x)
490 (and (arrayp y)
491 (array-equal-p x y)))
492 (t nil)))
494 (/show0 "about to do test cases in pred.lisp")
495 (let ((test-cases `((0.0 ,(load-time-value (make-unportable-float :single-float-negative-zero)) t)
496 (0.0 1.0 nil)
497 (#c(1 0) #c(1.0 0.0) t)
498 (#c(0 1) #c(0.0 1.0) t)
499 (#c(1.1 0.0) #c(11/10 0) nil) ; due to roundoff error
500 ("Hello" "hello" t)
501 ("Hello" #(#\h #\E #\l #\l #\o) t)
502 ("Hello" "goodbye" nil))))
503 (/show0 "TEST-CASES bound in pred.lisp")
504 (dolist (test-case test-cases)
505 (/show0 "about to do a TEST-CASE in pred.lisp")
506 (destructuring-bind (x y expected-result) test-case
507 (let* ((result (equalp x y))
508 (bresult (if result 1 0))
509 (expected-bresult (if expected-result 1 0)))
510 (unless (= bresult expected-bresult)
511 (/show0 "failing test in pred.lisp")
512 (error "failed test (EQUALP ~S ~S)" x y))))))
513 (/show0 "done with test cases in pred.lisp")