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