1.0.3.5: slightly different SEQUENCE type handling.
[sbcl.git] / src / code / pred.lisp
1 ;;;; predicate functions (EQUAL and friends, and type predicates)
2
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.
11
12 (in-package "SB!IMPL")
13 \f
14 ;;;; miscellaneous non-primitive predicates
15
16 #!-sb-fluid (declaim (inline streamp))
17 (defun streamp (stream)
18   (typep stream 'stream))
19
20 ;;; Is X a (VECTOR T)?
21 (defun vector-t-p (x)
22   (or (simple-vector-p x)
23       (and (complex-vector-p x)
24            (do ((data (%array-data-vector x) (%array-data-vector data)))
25                ((not (array-header-p data)) (simple-vector-p data))))))
26
27 ;;; Is X an extended sequence?
28 (defun extended-sequence-p (x)
29   (and (not (listp x))
30        (not (vectorp x))
31        (let* ((slayout #.(info :type :compiler-layout 'sequence))
32              (depthoid #.(layout-depthoid (info :type :compiler-layout 'sequence)))
33              (layout (layout-of x)))
34         (when (layout-invalid layout)
35           (setq layout (update-object-layout-or-invalid x slayout)))
36         (if (eq layout slayout)
37             t
38             (let ((inherits (layout-inherits layout)))
39               (declare (optimize (safety 0)))
40               (and (> (length inherits) depthoid)
41                    (eq (svref inherits depthoid) slayout)))))))
42
43 ;;; Is X a SEQUENCE?  Harder than just (OR VECTOR LIST)
44 (defun sequencep (x)
45   (or (listp x)
46       (vectorp x)
47       (let* ((slayout #.(info :type :compiler-layout 'sequence))
48              (depthoid #.(layout-depthoid (info :type :compiler-layout 'sequence)))
49              (layout (layout-of x)))
50         (when (layout-invalid layout)
51           (setq layout (update-object-layout-or-invalid x slayout)))
52         (if (eq layout slayout)
53             t
54             (let ((inherits (layout-inherits layout)))
55               (declare (optimize (safety 0)))
56               (and (> (length inherits) depthoid)
57                    (eq (svref inherits depthoid) slayout)))))))
58 \f
59 ;;;; primitive predicates. These must be supported directly by the
60 ;;;; compiler.
61
62 (defun not (object)
63   #!+sb-doc
64   "Return T if X is NIL, otherwise return NIL."
65   (not object))
66
67 ;;; All the primitive type predicate wrappers share a parallel form..
68 (macrolet ((def-type-predicate-wrapper (pred)
69              (let* ((name (symbol-name pred))
70                     (stem (string-left-trim "%" (string-right-trim "P-" name)))
71                     (article (if (position (schar name 0) "AEIOU") "an" "a")))
72                `(defun ,pred (object)
73                   ,(format nil
74                            "Return true if OBJECT is ~A ~A, and NIL otherwise."
75                            article
76                            stem)
77                   ;; (falling through to low-level implementation)
78                   (,pred object)))))
79   (def-type-predicate-wrapper array-header-p)
80   (def-type-predicate-wrapper arrayp)
81   (def-type-predicate-wrapper atom)
82   (def-type-predicate-wrapper base-char-p)
83   (def-type-predicate-wrapper base-string-p)
84   #!+sb-unicode (def-type-predicate-wrapper character-string-p)
85   (def-type-predicate-wrapper bignump)
86   (def-type-predicate-wrapper bit-vector-p)
87   (def-type-predicate-wrapper characterp)
88   (def-type-predicate-wrapper code-component-p)
89   (def-type-predicate-wrapper consp)
90   (def-type-predicate-wrapper compiled-function-p)
91   (def-type-predicate-wrapper complexp)
92   (def-type-predicate-wrapper complex-double-float-p)
93   (def-type-predicate-wrapper complex-float-p)
94   #!+long-float (def-type-predicate-wrapper complex-long-float-p)
95   (def-type-predicate-wrapper complex-rational-p)
96   (def-type-predicate-wrapper complex-single-float-p)
97   ;; (COMPLEX-VECTOR-P is not included here since it's awkward to express
98   ;; the type it tests for in the Common Lisp type system, and since it's
99   ;; only used in the implementation of a few specialized things.)
100   (def-type-predicate-wrapper double-float-p)
101   (def-type-predicate-wrapper extended-char-p)
102   (def-type-predicate-wrapper fdefn-p)
103   (def-type-predicate-wrapper fixnump)
104   (def-type-predicate-wrapper floatp)
105   (def-type-predicate-wrapper functionp)
106   (def-type-predicate-wrapper integerp)
107   (def-type-predicate-wrapper listp)
108   (def-type-predicate-wrapper long-float-p)
109   #!+(and sb-thread sb-lutex)
110   (def-type-predicate-wrapper lutexp)
111   (def-type-predicate-wrapper lra-p)
112   (def-type-predicate-wrapper null)
113   (def-type-predicate-wrapper numberp)
114   (def-type-predicate-wrapper rationalp)
115   (def-type-predicate-wrapper ratiop)
116   (def-type-predicate-wrapper realp)
117   (def-type-predicate-wrapper short-float-p)
118   (def-type-predicate-wrapper simple-array-p)
119   (def-type-predicate-wrapper simple-bit-vector-p)
120   (def-type-predicate-wrapper simple-base-string-p)
121   #!+sb-unicode (def-type-predicate-wrapper simple-character-string-p)
122   (def-type-predicate-wrapper simple-string-p)
123   (def-type-predicate-wrapper simple-vector-p)
124   (def-type-predicate-wrapper single-float-p)
125   (def-type-predicate-wrapper stringp)
126   (def-type-predicate-wrapper %instancep)
127   (def-type-predicate-wrapper symbolp)
128   (def-type-predicate-wrapper system-area-pointer-p)
129   (def-type-predicate-wrapper weak-pointer-p)
130   (def-type-predicate-wrapper vectorp)
131   #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
132   (def-type-predicate-wrapper unsigned-byte-32-p)
133   #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
134   (def-type-predicate-wrapper signed-byte-32-p)
135   #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
136   (def-type-predicate-wrapper unsigned-byte-64-p)
137   #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
138   (def-type-predicate-wrapper signed-byte-64-p)
139   (def-type-predicate-wrapper simple-array-nil-p)
140   (def-type-predicate-wrapper simple-array-unsigned-byte-2-p)
141   (def-type-predicate-wrapper simple-array-unsigned-byte-4-p)
142   (def-type-predicate-wrapper simple-array-unsigned-byte-8-p)
143   (def-type-predicate-wrapper simple-array-unsigned-byte-16-p)
144   (def-type-predicate-wrapper simple-array-unsigned-byte-32-p)
145   (def-type-predicate-wrapper simple-array-signed-byte-8-p)
146   (def-type-predicate-wrapper simple-array-signed-byte-16-p)
147   (def-type-predicate-wrapper simple-array-signed-byte-30-p)
148   (def-type-predicate-wrapper simple-array-signed-byte-32-p)
149   (def-type-predicate-wrapper simple-array-single-float-p)
150   (def-type-predicate-wrapper simple-array-double-float-p)
151   #!+long-float (def-type-predicate-wrapper simple-array-long-float-p)
152   (def-type-predicate-wrapper simple-array-complex-single-float-p)
153   (def-type-predicate-wrapper simple-array-complex-double-float-p)
154   #!+long-float (def-type-predicate-wrapper simple-array-complex-long-float-p)
155   (def-type-predicate-wrapper vector-nil-p))
156 \f
157 ;;; Return the specifier for the type of object. This is not simply
158 ;;; (TYPE-SPECIFIER (CTYPE-OF OBJECT)) because CTYPE-OF has different
159 ;;; goals than TYPE-OF. In particular, speed is more important than
160 ;;; precision, and it is not permitted to return member types.
161 (defun type-of (object)
162   #!+sb-doc
163   "Return the type of OBJECT."
164   (typecase object
165     (fixnum
166      (cond
167        ((<= 0 object 1) 'bit)
168        ((< object 0) 'fixnum)
169        (t '(integer 0 #.sb!xc:most-positive-fixnum))))
170     (integer
171      (if (>= object 0)
172          '(integer #.(1+ sb!xc:most-positive-fixnum))
173          'bignum))
174     (standard-char 'standard-char)
175     (base-char 'base-char)
176     (extended-char 'extended-char)
177     ((member t) 'boolean)
178     (keyword 'keyword)
179     ((or array complex) (type-specifier (ctype-of object)))
180     (t
181      (let* ((classoid (layout-classoid (layout-of object)))
182             (name (classoid-name classoid)))
183        (if (%instancep object)
184            (case name
185              (sb!alien-internals:alien-value
186               `(sb!alien:alien
187                 ,(sb!alien-internals:unparse-alien-type
188                   (sb!alien-internals:alien-value-type object))))
189              (t
190               (let ((pname (classoid-proper-name classoid)))
191                 (if (classoid-p pname)
192                     (classoid-pcl-class pname)
193                     pname))))
194            name)))))
195 \f
196 ;;;; equality predicates
197
198 ;;; This is real simple, 'cause the compiler takes care of it.
199 (defun eq (obj1 obj2)
200   #!+sb-doc
201   "Return T if OBJ1 and OBJ2 are the same object, otherwise NIL."
202   (eq obj1 obj2))
203
204 (declaim (inline %eql))
205 (defun %eql (obj1 obj2)
206   #!+sb-doc
207   "Return T if OBJ1 and OBJ2 represent the same object, otherwise NIL."
208   (or (eq obj1 obj2)
209       (if (or (typep obj2 'fixnum)
210               (not (typep obj2 'number)))
211           nil
212           (macrolet ((foo (&rest stuff)
213                        `(typecase obj2
214                           ,@(mapcar (lambda (foo)
215                                       (let ((type (car foo))
216                                             (fn (cadr foo)))
217                                         `(,type
218                                           (and (typep obj1 ',type)
219                                                (,fn obj1 obj2)))))
220                                     stuff))))
221             (foo
222              (single-float eql)
223              (double-float eql)
224              #!+long-float
225              (long-float eql)
226              (bignum
227               (lambda (x y)
228                 (zerop (bignum-compare x y))))
229              (ratio
230               (lambda (x y)
231                 (and (eql (numerator x) (numerator y))
232                      (eql (denominator x) (denominator y)))))
233              (complex
234               (lambda (x y)
235                 (and (eql (realpart x) (realpart y))
236                      (eql (imagpart x) (imagpart y))))))))))
237
238 (defun eql (x y)
239   (%eql x y))
240
241 (defun bit-vector-= (x y)
242   (declare (type bit-vector x y))
243   (if (and (simple-bit-vector-p x)
244            (simple-bit-vector-p y))
245       (bit-vector-= x y) ; DEFTRANSFORM
246       (and (= (length x) (length y))
247            (do ((i 0 (1+ i))
248                 (length (length x)))
249                ((= i length) t)
250              (declare (fixnum i))
251              (unless (= (bit x i) (bit y i))
252                (return nil))))))
253
254 (defun equal (x y)
255   #!+sb-doc
256   "Return T if X and Y are EQL or if they are structured components whose
257 elements are EQUAL. Strings and bit-vectors are EQUAL if they are the same
258 length and have identical components. Other arrays must be EQ to be EQUAL."
259   ;; Non-tail self-recursion implemented with a local auxiliary function
260   ;; is a lot faster than doing it the straightforward way (at least
261   ;; on x86oids) due to calling convention differences. -- JES, 2005-12-30
262   (labels ((equal-aux (x y)
263              (cond ((%eql x y)
264                     t)
265                    ((consp x)
266                     (and (consp y)
267                          (equal-aux (car x) (car y))
268                          (equal-aux (cdr x) (cdr y))))
269                    ((stringp x)
270                     (and (stringp y) (string= x y)))
271                    ((pathnamep x)
272                     (and (pathnamep y) (pathname= x y)))
273                    ((bit-vector-p x)
274                     (and (bit-vector-p y)
275                          (bit-vector-= x y)))
276                    (t nil))))
277     ;; Use MAYBE-INLINE to get the inline expansion only once (instead
278     ;; of 200 times with INLINE). -- JES, 2005-12-30
279     (declare (maybe-inline equal-aux))
280     (equal-aux x y)))
281
282 ;;; EQUALP comparison of HASH-TABLE values
283 (defun hash-table-equalp (x y)
284   (declare (type hash-table x y))
285   (or (eq x y)
286       (and (hash-table-p y)
287            (eql (hash-table-count x) (hash-table-count y))
288            (eql (hash-table-test x) (hash-table-test y))
289            (block comparison-of-entries
290              (maphash (lambda (key x-value)
291                         (multiple-value-bind (y-value y-value-p)
292                             (gethash key y)
293                           (unless (and y-value-p (equalp x-value y-value))
294                             (return-from comparison-of-entries nil))))
295                       x)
296              t))))
297
298 (defun equalp (x y)
299   #+nil ; KLUDGE: If doc string, should be accurate: Talk about structures
300   ; and HASH-TABLEs.
301   "This is like EQUAL, except more liberal in several respects.
302   Numbers may be of different types, as long as the values are identical
303   after coercion. Characters may differ in alphabetic case. Vectors and
304   arrays must have identical dimensions and EQUALP elements, but may differ
305   in their type restriction."
306   (cond ((eq x y) t)
307         ((characterp x) (and (characterp y) (char-equal x y)))
308         ((numberp x) (and (numberp y) (= x y)))
309         ((consp x)
310          (and (consp y)
311               (equalp (car x) (car y))
312               (equalp (cdr x) (cdr y))))
313         ((pathnamep x)
314          (and (pathnamep y) (pathname= x y)))
315         ((hash-table-p x)
316          (and (hash-table-p y)
317               (hash-table-equalp x y)))
318         ((%instancep x)
319          (let* ((layout-x (%instance-layout x))
320                 (len (layout-length layout-x)))
321            (and (%instancep y)
322                 (eq layout-x (%instance-layout y))
323                 (structure-classoid-p (layout-classoid layout-x))
324                 (do ((i 1 (1+ i)))
325                     ((= i len) t)
326                   (declare (fixnum i))
327                   (let ((x-el (%instance-ref x i))
328                         (y-el (%instance-ref y i)))
329                     (unless (or (eq x-el y-el)
330                                 (equalp x-el y-el))
331                       (return nil)))))))
332         ((vectorp x)
333          (let ((length (length x)))
334            (and (vectorp y)
335                 (= length (length y))
336                 (dotimes (i length t)
337                   (let ((x-el (aref x i))
338                         (y-el (aref y i)))
339                     (unless (or (eq x-el y-el)
340                                 (equalp x-el y-el))
341                       (return nil)))))))
342         ((arrayp x)
343          (and (arrayp y)
344               (= (array-rank x) (array-rank y))
345               (dotimes (axis (array-rank x) t)
346                 (unless (= (array-dimension x axis)
347                            (array-dimension y axis))
348                   (return nil)))
349               (dotimes (index (array-total-size x) t)
350                 (let ((x-el (row-major-aref x index))
351                       (y-el (row-major-aref y index)))
352                   (unless (or (eq x-el y-el)
353                               (equalp x-el y-el))
354                     (return nil))))))
355         (t nil)))
356
357 (/show0 "about to do test cases in pred.lisp")
358 #!+sb-test
359 (let ((test-cases `((0.0 ,(load-time-value (make-unportable-float :single-float-negative-zero)) t)
360                     (0.0 1.0 nil)
361                     (#c(1 0) #c(1.0 0) t)
362                     (#c(1.1 0) #c(11/10 0) nil) ; due to roundoff error
363                     ("Hello" "hello" t)
364                     ("Hello" #(#\h #\E #\l #\l #\o) t)
365                     ("Hello" "goodbye" nil))))
366   (/show0 "TEST-CASES bound in pred.lisp")
367   (dolist (test-case test-cases)
368     (/show0 "about to do a TEST-CASE in pred.lisp")
369     (destructuring-bind (x y expected-result) test-case
370       (let* ((result (equalp x y))
371              (bresult (if result 1 0))
372              (expected-bresult (if expected-result 1 0)))
373         (unless (= bresult expected-bresult)
374           (/show0 "failing test in pred.lisp")
375           (error "failed test (EQUALP ~S ~S)" x y))))))
376 (/show0 "done with test cases in pred.lisp")