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