1.0.28.65: fix compiling with *PROFILE-HASH-CACHE* set to T
[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)
180      (type-specifier (ctype-of object)))
181     (t
182      (let* ((classoid (layout-classoid (layout-of object)))
183             (name (classoid-name classoid)))
184        (if (%instancep object)
185            (case name
186              (sb!alien-internals:alien-value
187               `(sb!alien:alien
188                 ,(sb!alien-internals:unparse-alien-type
189                   (sb!alien-internals:alien-value-type object))))
190              (t
191               (let ((pname (classoid-proper-name classoid)))
192                 (if (classoid-p pname)
193                     (classoid-pcl-class pname)
194                     pname))))
195            name)))))
196 \f
197 ;;;; equality predicates
198
199 ;;; This is real simple, 'cause the compiler takes care of it.
200 (defun eq (obj1 obj2)
201   #!+sb-doc
202   "Return T if OBJ1 and OBJ2 are the same object, otherwise NIL."
203   (eq obj1 obj2))
204
205 (declaim (inline %eql))
206 (defun %eql (obj1 obj2)
207   #!+sb-doc
208   "Return T if OBJ1 and OBJ2 represent the same object, otherwise NIL."
209   (or (eq obj1 obj2)
210       (if (or (typep obj2 'fixnum)
211               (not (typep obj2 'number)))
212           nil
213           (macrolet ((foo (&rest stuff)
214                        `(typecase obj2
215                           ,@(mapcar (lambda (foo)
216                                       (let ((type (car foo))
217                                             (fn (cadr foo)))
218                                         `(,type
219                                           (and (typep obj1 ',type)
220                                                (,fn obj1 obj2)))))
221                                     stuff))))
222             (foo
223              (single-float eql)
224              (double-float eql)
225              #!+long-float
226              (long-float eql)
227              (bignum
228               (lambda (x y)
229                 (zerop (bignum-compare x y))))
230              (ratio
231               (lambda (x y)
232                 (and (eql (numerator x) (numerator y))
233                      (eql (denominator x) (denominator y)))))
234              (complex
235               (lambda (x y)
236                 (and (eql (realpart x) (realpart y))
237                      (eql (imagpart x) (imagpart y))))))))))
238
239 (defun eql (x y)
240   (%eql x y))
241
242 (defun bit-vector-= (x y)
243   (declare (type bit-vector x y))
244   (if (and (simple-bit-vector-p x)
245            (simple-bit-vector-p y))
246       (bit-vector-= x y) ; DEFTRANSFORM
247       (and (= (length x) (length y))
248            (do ((i 0 (1+ i))
249                 (length (length x)))
250                ((= i length) t)
251              (declare (fixnum i))
252              (unless (= (bit x i) (bit y i))
253                (return nil))))))
254
255 (defun equal (x y)
256   #!+sb-doc
257   "Return T if X and Y are EQL or if they are structured components whose
258 elements are EQUAL. Strings and bit-vectors are EQUAL if they are the same
259 length and have identical components. Other arrays must be EQ to be EQUAL."
260   ;; Non-tail self-recursion implemented with a local auxiliary function
261   ;; is a lot faster than doing it the straightforward way (at least
262   ;; on x86oids) due to calling convention differences. -- JES, 2005-12-30
263   (labels ((equal-aux (x y)
264              (cond ((%eql x y)
265                     t)
266                    ((consp x)
267                     (and (consp y)
268                          (equal-aux (car x) (car y))
269                          (equal-aux (cdr x) (cdr y))))
270                    ((stringp x)
271                     (and (stringp y) (string= x y)))
272                    ((pathnamep x)
273                     (and (pathnamep y) (pathname= x y)))
274                    ((bit-vector-p x)
275                     (and (bit-vector-p y)
276                          (bit-vector-= x y)))
277                    (t nil))))
278     ;; Use MAYBE-INLINE to get the inline expansion only once (instead
279     ;; of 200 times with INLINE). -- JES, 2005-12-30
280     (declare (maybe-inline equal-aux))
281     (equal-aux x y)))
282
283 ;;; EQUALP comparison of HASH-TABLE values
284 (defun hash-table-equalp (x y)
285   (declare (type hash-table x y))
286   (or (eq x y)
287       (and (hash-table-p y)
288            (eql (hash-table-count x) (hash-table-count y))
289            (eql (hash-table-test x) (hash-table-test y))
290            (block comparison-of-entries
291              (maphash (lambda (key x-value)
292                         (multiple-value-bind (y-value y-value-p)
293                             (gethash key y)
294                           (unless (and y-value-p (equalp x-value y-value))
295                             (return-from comparison-of-entries nil))))
296                       x)
297              t))))
298
299 (defun equalp (x y)
300   #+nil ; KLUDGE: If doc string, should be accurate: Talk about structures
301   ; and HASH-TABLEs.
302   "This is like EQUAL, except more liberal in several respects.
303   Numbers may be of different types, as long as the values are identical
304   after coercion. Characters may differ in alphabetic case. Vectors and
305   arrays must have identical dimensions and EQUALP elements, but may differ
306   in their type restriction."
307   (cond ((eq x y) t)
308         ((characterp x) (and (characterp y) (char-equal x y)))
309         ((numberp x) (and (numberp y) (= x y)))
310         ((consp x)
311          (and (consp y)
312               (equalp (car x) (car y))
313               (equalp (cdr x) (cdr y))))
314         ((pathnamep x)
315          (and (pathnamep y) (pathname= x y)))
316         ((hash-table-p x)
317          (and (hash-table-p y)
318               (hash-table-equalp x y)))
319         ((%instancep x)
320          (let* ((layout-x (%instance-layout x))
321                 (raw-len (layout-n-untagged-slots layout-x))
322                 (total-len (layout-length layout-x))
323                 (normal-len (- total-len raw-len)))
324            (and (%instancep y)
325                 (eq layout-x (%instance-layout y))
326                 (structure-classoid-p (layout-classoid layout-x))
327                 (dotimes (i normal-len t)
328                   (let ((x-el (%instance-ref x i))
329                         (y-el (%instance-ref y i)))
330                     (unless (or (eq x-el y-el)
331                                 (equalp x-el y-el))
332                       (return nil))))
333                 (if (zerop raw-len)
334                     t
335                     (raw-instance-slots-equalp layout-x x y)))))
336         ((vectorp x)
337          (let ((length (length x)))
338            (and (vectorp y)
339                 (= length (length y))
340                 (dotimes (i length t)
341                   (let ((x-el (aref x i))
342                         (y-el (aref y i)))
343                     (unless (or (eq x-el y-el)
344                                 (equalp x-el y-el))
345                       (return nil)))))))
346         ((arrayp x)
347          (and (arrayp y)
348               (= (array-rank x) (array-rank y))
349               (dotimes (axis (array-rank x) t)
350                 (unless (= (array-dimension x axis)
351                            (array-dimension y axis))
352                   (return nil)))
353               (dotimes (index (array-total-size x) t)
354                 (let ((x-el (row-major-aref x index))
355                       (y-el (row-major-aref y index)))
356                   (unless (or (eq x-el y-el)
357                               (equalp x-el y-el))
358                     (return nil))))))
359         (t nil)))
360
361 (/show0 "about to do test cases in pred.lisp")
362 #!+sb-test
363 (let ((test-cases `((0.0 ,(load-time-value (make-unportable-float :single-float-negative-zero)) t)
364                     (0.0 1.0 nil)
365                     (#c(1 0) #c(1.0 0.0) t)
366                     (#c(0 1) #c(0.0 1.0) t)
367                     (#c(1.1 0.0) #c(11/10 0) nil) ; due to roundoff error
368                     ("Hello" "hello" t)
369                     ("Hello" #(#\h #\E #\l #\l #\o) t)
370                     ("Hello" "goodbye" nil))))
371   (/show0 "TEST-CASES bound in pred.lisp")
372   (dolist (test-case test-cases)
373     (/show0 "about to do a TEST-CASE in pred.lisp")
374     (destructuring-bind (x y expected-result) test-case
375       (let* ((result (equalp x y))
376              (bresult (if result 1 0))
377              (expected-bresult (if expected-result 1 0)))
378         (unless (= bresult expected-bresult)
379           (/show0 "failing test in pred.lisp")
380           (error "failed test (EQUALP ~S ~S)" x y))))))
381 (/show0 "done with test cases in pred.lisp")