346e388e4a3bda4ae25c71b9890e7f7a69b7bce0
[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 single-float-p)
119   (def-type-predicate-wrapper %instancep)
120   (def-type-predicate-wrapper symbolp)
121   (def-type-predicate-wrapper system-area-pointer-p)
122   (def-type-predicate-wrapper weak-pointer-p)
123   #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
124   (progn
125     (def-type-predicate-wrapper unsigned-byte-32-p)
126     (def-type-predicate-wrapper signed-byte-32-p))
127   #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
128   (progn
129     (def-type-predicate-wrapper unsigned-byte-64-p)
130     (def-type-predicate-wrapper signed-byte-64-p))
131   ;; Specialized array types
132   (macrolet ((saetp-defs ()
133                `(progn
134                   ,@(map 'list
135                          (lambda (saetp)
136                            `(def-type-predicate-wrapper
137                                 ,(symbolicate (sb!vm:saetp-primitive-type-name saetp) "-P")))
138                          sb!vm:*specialized-array-element-type-properties*))))
139     (saetp-defs))
140   ;; Other array types
141   (def-type-predicate-wrapper simple-array-p)
142   (def-type-predicate-wrapper simple-string-p)
143   (def-type-predicate-wrapper stringp)
144   (def-type-predicate-wrapper vectorp)
145   (def-type-predicate-wrapper vector-nil-p))
146 \f
147 ;;; Return the specifier for the type of object. This is not simply
148 ;;; (TYPE-SPECIFIER (CTYPE-OF OBJECT)) because CTYPE-OF has different
149 ;;; goals than TYPE-OF. In particular, speed is more important than
150 ;;; precision, and it is not permitted to return member types.
151 (defun type-of (object)
152   #!+sb-doc
153   "Return the type of OBJECT."
154   (typecase object
155     (fixnum
156      (cond
157        ((<= 0 object 1) 'bit)
158        ((< object 0) 'fixnum)
159        (t '(integer 0 #.sb!xc:most-positive-fixnum))))
160     (integer
161      (if (>= object 0)
162          '(integer #.(1+ sb!xc:most-positive-fixnum))
163          'bignum))
164     (standard-char 'standard-char)
165     (base-char 'base-char)
166     (extended-char 'extended-char)
167     ((member t) 'boolean)
168     (keyword 'keyword)
169     ((or array complex)
170      (type-specifier (ctype-of object)))
171     (t
172      (let* ((classoid (layout-classoid (layout-of object)))
173             (name (classoid-name classoid)))
174        (if (%instancep object)
175            (case name
176              (sb!alien-internals:alien-value
177               `(sb!alien:alien
178                 ,(sb!alien-internals:unparse-alien-type
179                   (sb!alien-internals:alien-value-type object))))
180              (t
181               (let ((pname (classoid-proper-name classoid)))
182                 (if (classoid-p pname)
183                     (classoid-pcl-class pname)
184                     pname))))
185            name)))))
186 \f
187 ;;;; equality predicates
188
189 ;;; This is real simple, 'cause the compiler takes care of it.
190 (defun eq (obj1 obj2)
191   #!+sb-doc
192   "Return T if OBJ1 and OBJ2 are the same object, otherwise NIL."
193   (eq obj1 obj2))
194
195 (declaim (inline %eql))
196 (defun %eql (obj1 obj2)
197   #!+sb-doc
198   "Return T if OBJ1 and OBJ2 represent the same object, otherwise NIL."
199   (or (eq obj1 obj2)
200       (if (or (typep obj2 'fixnum)
201               (not (typep obj2 'number)))
202           nil
203           (macrolet ((foo (&rest stuff)
204                        `(typecase obj2
205                           ,@(mapcar (lambda (foo)
206                                       (let ((type (car foo))
207                                             (fn (cadr foo)))
208                                         `(,type
209                                           (and (typep obj1 ',type)
210                                                (,fn obj1 obj2)))))
211                                     stuff))))
212             (foo
213              (single-float eql)
214              (double-float eql)
215              #!+long-float
216              (long-float eql)
217              (bignum
218               (lambda (x y)
219                 (zerop (bignum-compare x y))))
220              (ratio
221               (lambda (x y)
222                 (and (eql (numerator x) (numerator y))
223                      (eql (denominator x) (denominator y)))))
224              (complex
225               (lambda (x y)
226                 (and (eql (realpart x) (realpart y))
227                      (eql (imagpart x) (imagpart y))))))))))
228
229 (defun eql (x y)
230   (%eql x y))
231
232 (defun bit-vector-= (x y)
233   (declare (type bit-vector x y))
234   (if (and (simple-bit-vector-p x)
235            (simple-bit-vector-p y))
236       (bit-vector-= x y) ; DEFTRANSFORM
237       (and (= (length x) (length y))
238            (do ((i 0 (1+ i))
239                 (length (length x)))
240                ((= i length) t)
241              (declare (fixnum i))
242              (unless (= (bit x i) (bit y i))
243                (return nil))))))
244
245 (defun equal (x y)
246   #!+sb-doc
247   "Return T if X and Y are EQL or if they are structured components whose
248 elements are EQUAL. Strings and bit-vectors are EQUAL if they are the same
249 length and have identical components. Other arrays must be EQ to be EQUAL."
250   ;; Non-tail self-recursion implemented with a local auxiliary function
251   ;; is a lot faster than doing it the straightforward way (at least
252   ;; on x86oids) due to calling convention differences. -- JES, 2005-12-30
253   (labels ((equal-aux (x y)
254              (cond ((%eql x y)
255                     t)
256                    ((consp x)
257                     (and (consp y)
258                          (equal-aux (car x) (car y))
259                          (equal-aux (cdr x) (cdr y))))
260                    ((stringp x)
261                     (and (stringp y) (string= x y)))
262                    ((pathnamep x)
263                     (and (pathnamep y) (pathname= x y)))
264                    ((bit-vector-p x)
265                     (and (bit-vector-p y)
266                          (bit-vector-= x y)))
267                    (t nil))))
268     ;; Use MAYBE-INLINE to get the inline expansion only once (instead
269     ;; of 200 times with INLINE). -- JES, 2005-12-30
270     (declare (maybe-inline equal-aux))
271     (equal-aux x y)))
272
273 ;;; EQUALP comparison of HASH-TABLE values
274 (defun hash-table-equalp (x y)
275   (declare (type hash-table x y))
276   (or (eq x y)
277       (and (hash-table-p y)
278            (eql (hash-table-count x) (hash-table-count y))
279            (eql (hash-table-test x) (hash-table-test y))
280            (block comparison-of-entries
281              (maphash (lambda (key x-value)
282                         (multiple-value-bind (y-value y-value-p)
283                             (gethash key y)
284                           (unless (and y-value-p (equalp x-value y-value))
285                             (return-from comparison-of-entries nil))))
286                       x)
287              t))))
288
289 (defun equalp (x y)
290   #+nil ; KLUDGE: If doc string, should be accurate: Talk about structures
291   ; and HASH-TABLEs.
292   "This is like EQUAL, except more liberal in several respects.
293   Numbers may be of different types, as long as the values are identical
294   after coercion. Characters may differ in alphabetic case. Vectors and
295   arrays must have identical dimensions and EQUALP elements, but may differ
296   in their type restriction."
297   (cond ((eq x y) t)
298         ((characterp x) (and (characterp y) (char-equal x y)))
299         ((numberp x) (and (numberp y) (= x y)))
300         ((consp x)
301          (and (consp y)
302               (equalp (car x) (car y))
303               (equalp (cdr x) (cdr y))))
304         ((pathnamep x)
305          (and (pathnamep y) (pathname= x y)))
306         ((hash-table-p x)
307          (and (hash-table-p y)
308               (hash-table-equalp x y)))
309         ((%instancep x)
310          (let* ((layout-x (%instance-layout x))
311                 (raw-len (layout-n-untagged-slots layout-x))
312                 (total-len (layout-length layout-x))
313                 (normal-len (- total-len raw-len)))
314            (and (%instancep y)
315                 (eq layout-x (%instance-layout y))
316                 (structure-classoid-p (layout-classoid layout-x))
317                 (dotimes (i normal-len t)
318                   (let ((x-el (%instance-ref x i))
319                         (y-el (%instance-ref y i)))
320                     (unless (or (eq x-el y-el)
321                                 (equalp x-el y-el))
322                       (return nil))))
323                 (if (zerop raw-len)
324                     t
325                     (raw-instance-slots-equalp layout-x x y)))))
326         ((vectorp x)
327          (let ((length (length x)))
328            (and (vectorp y)
329                 (= length (length y))
330                 (dotimes (i length t)
331                   (let ((x-el (aref x i))
332                         (y-el (aref y i)))
333                     (unless (or (eq x-el y-el)
334                                 (equalp x-el y-el))
335                       (return nil)))))))
336         ((arrayp x)
337          (and (arrayp y)
338               (= (array-rank x) (array-rank y))
339               (dotimes (axis (array-rank x) t)
340                 (unless (= (array-dimension x axis)
341                            (array-dimension y axis))
342                   (return nil)))
343               (dotimes (index (array-total-size x) t)
344                 (let ((x-el (row-major-aref x index))
345                       (y-el (row-major-aref y index)))
346                   (unless (or (eq x-el y-el)
347                               (equalp x-el y-el))
348                     (return nil))))))
349         (t nil)))
350
351 (/show0 "about to do test cases in pred.lisp")
352 #!+sb-test
353 (let ((test-cases `((0.0 ,(load-time-value (make-unportable-float :single-float-negative-zero)) t)
354                     (0.0 1.0 nil)
355                     (#c(1 0) #c(1.0 0.0) t)
356                     (#c(0 1) #c(0.0 1.0) t)
357                     (#c(1.1 0.0) #c(11/10 0) nil) ; due to roundoff error
358                     ("Hello" "hello" t)
359                     ("Hello" #(#\h #\E #\l #\l #\o) t)
360                     ("Hello" "goodbye" nil))))
361   (/show0 "TEST-CASES bound in pred.lisp")
362   (dolist (test-case test-cases)
363     (/show0 "about to do a TEST-CASE in pred.lisp")
364     (destructuring-bind (x y expected-result) test-case
365       (let* ((result (equalp x y))
366              (bresult (if result 1 0))
367              (expected-bresult (if expected-result 1 0)))
368         (unless (= bresult expected-bresult)
369           (/show0 "failing test in pred.lisp")
370           (error "failed test (EQUALP ~S ~S)" x y))))))
371 (/show0 "done with test cases in pred.lisp")