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