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