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