0.8.0.78.vector-nil-string.1:
[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   (def-type-predicate-wrapper bignump)
52   (def-type-predicate-wrapper bit-vector-p)
53   (def-type-predicate-wrapper characterp)
54   (def-type-predicate-wrapper code-component-p)
55   (def-type-predicate-wrapper consp)
56   (def-type-predicate-wrapper compiled-function-p)
57   (def-type-predicate-wrapper complexp)
58   (def-type-predicate-wrapper complex-double-float-p)
59   (def-type-predicate-wrapper complex-float-p)
60   #!+long-float (def-type-predicate-wrapper complex-long-float-p)
61   (def-type-predicate-wrapper complex-rational-p)
62   (def-type-predicate-wrapper complex-single-float-p)
63   ;; (COMPLEX-VECTOR-P is not included here since it's awkward to express
64   ;; the type it tests for in the Common Lisp type system, and since it's
65   ;; only used in the implementation of a few specialized things.)
66   (def-type-predicate-wrapper double-float-p)
67   (def-type-predicate-wrapper fdefn-p)
68   (def-type-predicate-wrapper fixnump)
69   (def-type-predicate-wrapper floatp)
70   (def-type-predicate-wrapper functionp)
71   (def-type-predicate-wrapper integerp)
72   (def-type-predicate-wrapper listp)
73   (def-type-predicate-wrapper long-float-p)
74   (def-type-predicate-wrapper lra-p)
75   (def-type-predicate-wrapper null)
76   (def-type-predicate-wrapper numberp)
77   (def-type-predicate-wrapper rationalp)
78   (def-type-predicate-wrapper ratiop)
79   (def-type-predicate-wrapper realp)
80   (def-type-predicate-wrapper short-float-p)
81   (def-type-predicate-wrapper sb!kernel:simple-array-p)
82   (def-type-predicate-wrapper simple-bit-vector-p)
83   (def-type-predicate-wrapper simple-base-string-p)
84   (def-type-predicate-wrapper simple-string-p)
85   (def-type-predicate-wrapper simple-vector-p)
86   (def-type-predicate-wrapper single-float-p)
87   (def-type-predicate-wrapper stringp)
88   (def-type-predicate-wrapper %instancep)
89   (def-type-predicate-wrapper symbolp)
90   (def-type-predicate-wrapper system-area-pointer-p)
91   (def-type-predicate-wrapper weak-pointer-p)
92   (def-type-predicate-wrapper vectorp)
93   (def-type-predicate-wrapper unsigned-byte-32-p)
94   (def-type-predicate-wrapper signed-byte-32-p)
95   (def-type-predicate-wrapper simple-array-nil-p)
96   (def-type-predicate-wrapper simple-array-unsigned-byte-2-p)
97   (def-type-predicate-wrapper simple-array-unsigned-byte-4-p)
98   (def-type-predicate-wrapper simple-array-unsigned-byte-8-p)
99   (def-type-predicate-wrapper simple-array-unsigned-byte-16-p)
100   (def-type-predicate-wrapper simple-array-unsigned-byte-32-p)
101   (def-type-predicate-wrapper simple-array-signed-byte-8-p)
102   (def-type-predicate-wrapper simple-array-signed-byte-16-p)
103   (def-type-predicate-wrapper simple-array-signed-byte-30-p)
104   (def-type-predicate-wrapper simple-array-signed-byte-32-p)
105   (def-type-predicate-wrapper simple-array-single-float-p)
106   (def-type-predicate-wrapper simple-array-double-float-p)
107   #!+long-float (def-type-predicate-wrapper simple-array-long-float-p)
108   (def-type-predicate-wrapper simple-array-complex-single-float-p)
109   (def-type-predicate-wrapper simple-array-complex-double-float-p)
110   #!+long-float (def-type-predicate-wrapper simple-array-complex-long-float-p))
111 \f
112 ;;; Return the specifier for the type of object. This is not simply
113 ;;; (TYPE-SPECIFIER (CTYPE-OF OBJECT)) because CTYPE-OF has different
114 ;;; goals than TYPE-OF. In particular, speed is more important than
115 ;;; precision, and it is not permitted to return member types.
116 (defun type-of (object)
117   #!+sb-doc
118   "Return the type of OBJECT."
119   (typecase object
120     (fixnum
121      (cond
122        ((<= 0 object 1) 'bit)
123        ((< object 0) 'fixnum)
124        (t '(integer 0 #.sb!xc:most-positive-fixnum))))
125     (integer
126      (if (>= object 0)
127          '(integer #.(1+ sb!xc:most-positive-fixnum))
128          'bignum))
129     (standard-char 'standard-char)
130     ((member t) 'boolean)
131     (keyword 'keyword)
132     ((or array complex) (type-specifier (ctype-of object)))
133     (t
134      (let* ((classoid (layout-classoid (layout-of object)))
135             (name (classoid-name classoid)))
136        (if (typep object 'instance)
137            (case name
138              (sb!alien-internals:alien-value
139               `(sb!alien:alien
140                 ,(sb!alien-internals:unparse-alien-type
141                   (sb!alien-internals:alien-value-type object))))
142              (t
143               (let ((pname (classoid-proper-name classoid)))
144                 (if (classoid-p pname)
145                     (classoid-pcl-class pname)
146                     pname))))
147            name)))))
148 \f
149 ;;;; equality predicates
150
151 ;;; This is real simple, 'cause the compiler takes care of it.
152 (defun eq (obj1 obj2)
153   #!+sb-doc
154   "Return T if OBJ1 and OBJ2 are the same object, otherwise NIL."
155   (eq obj1 obj2))
156
157 (defun bit-vector-= (x y)
158   (declare (type bit-vector x y))
159   (if (and (simple-bit-vector-p x)
160            (simple-bit-vector-p y))
161       (bit-vector-= x y) ; DEFTRANSFORM
162       (and (= (length x) (length y))
163            (do ((i 0 (1+ i))
164                 (length (length x)))
165                ((= i length) t)
166              (declare (fixnum i))
167              (unless (= (bit x i) (bit y i))
168                (return nil))))))
169
170 (defun equal (x y)
171   #!+sb-doc
172   "Return T if X and Y are EQL or if they are structured components
173   whose elements are EQUAL. Strings and bit-vectors are EQUAL if they
174   are the same length and have identical components. Other arrays must be
175   EQ to be EQUAL."
176   (cond ((eql x y) t)
177         ((consp x)
178          (and (consp y)
179               (equal (car x) (car y))
180               (equal (cdr x) (cdr y))))
181         ((stringp x)
182          (and (stringp y) (string= x y)))
183         ((pathnamep x)
184          (and (pathnamep y) (pathname= x y)))
185         ((bit-vector-p x)
186          (and (bit-vector-p y)
187               (bit-vector-= x y)))
188         (t nil)))
189
190 ;;; EQUALP comparison of HASH-TABLE values
191 (defun hash-table-equalp (x y)
192   (declare (type hash-table x y))
193   (or (eq x y)
194       (and (hash-table-p y)
195            (eql (hash-table-count x) (hash-table-count y))
196            (eql (hash-table-test x) (hash-table-test y))
197            (block comparison-of-entries
198              (maphash (lambda (key x-value)
199                         (multiple-value-bind (y-value y-value-p)
200                             (gethash key y)
201                           (unless (and y-value-p (equalp x-value y-value))
202                             (return-from comparison-of-entries nil))))
203                       x)
204              t))))
205
206 (defun equalp (x y)
207   #+nil ; KLUDGE: If doc string, should be accurate: Talk about structures
208   ; and HASH-TABLEs.
209   "This is like EQUAL, except more liberal in several respects.
210   Numbers may be of different types, as long as the values are identical
211   after coercion. Characters may differ in alphabetic case. Vectors and
212   arrays must have identical dimensions and EQUALP elements, but may differ
213   in their type restriction."
214   (cond ((eq x y) t)
215         ((characterp x) (and (characterp y) (char-equal x y)))
216         ((numberp x) (and (numberp y) (= x y)))
217         ((consp x)
218          (and (consp y)
219               (equalp (car x) (car y))
220               (equalp (cdr x) (cdr y))))
221         ((pathnamep x)
222          (and (pathnamep y) (pathname= x y)))
223         ((hash-table-p x)
224          (and (hash-table-p y)
225               (hash-table-equalp x y)))
226         ((typep x 'instance)
227          (let* ((layout-x (%instance-layout x))
228                 (len (layout-length layout-x)))
229            (and (typep y 'instance)
230                 (eq layout-x (%instance-layout y))
231                 (structure-classoid-p (layout-classoid layout-x))
232                 (do ((i 1 (1+ i)))
233                     ((= i len) t)
234                   (declare (fixnum i))
235                   (let ((x-el (%instance-ref x i))
236                         (y-el (%instance-ref y i)))
237                     (unless (or (eq x-el y-el)
238                                 (equalp x-el y-el))
239                       (return nil)))))))
240         ((vectorp x)
241          (let ((length (length x)))
242            (and (vectorp y)
243                 (= length (length y))
244                 (dotimes (i length t)
245                   (let ((x-el (aref x i))
246                         (y-el (aref y i)))
247                     (unless (or (eq x-el y-el)
248                                 (equalp x-el y-el))
249                       (return nil)))))))
250         ((arrayp x)
251          (and (arrayp y)
252               (= (array-rank x) (array-rank y))
253               (dotimes (axis (array-rank x) t)
254                 (unless (= (array-dimension x axis)
255                            (array-dimension y axis))
256                   (return nil)))
257               (dotimes (index (array-total-size x) t)
258                 (let ((x-el (row-major-aref x index))
259                       (y-el (row-major-aref y index)))
260                   (unless (or (eq x-el y-el)
261                               (equalp x-el y-el))
262                     (return nil))))))
263         (t nil)))
264
265 (/show0 "about to do test cases in pred.lisp")
266 #!+sb-test
267 (let ((test-cases `((0.0 ,(load-time-value (make-unportable-float :single-float-negative-zero)) t)
268                     (0.0 1.0 nil)
269                     (#c(1 0) #c(1.0 0) t)
270                     (#c(1.1 0) #c(11/10 0) nil) ; due to roundoff error
271                     ("Hello" "hello" t)
272                     ("Hello" #(#\h #\E #\l #\l #\o) t)
273                     ("Hello" "goodbye" nil))))
274   (/show0 "TEST-CASES bound in pred.lisp")
275   (dolist (test-case test-cases)
276     (/show0 "about to do a TEST-CASE in pred.lisp")
277     (destructuring-bind (x y expected-result) test-case
278       (let* ((result (equalp x y))
279              (bresult (if result 1 0))
280              (expected-bresult (if expected-result 1 0)))
281         (unless (= bresult expected-bresult)
282           (/show0 "failing test in pred.lisp")
283           (error "failed test (EQUALP ~S ~S)" x y))))))
284 (/show0 "done with test cases in pred.lisp")