208f63758b69af2f66f84aaa2e67f370608d62d3
[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
14 (file-comment
15   "$Header$")
16 \f
17 ;;;; miscellaneous non-primitive predicates
18
19 #!-sb-fluid (declaim (inline streamp))
20 (defun streamp (stream)
21   (typep stream 'stream))
22
23 ;;; Is X a (VECTOR T)?
24 (defun vector-t-p (x)
25   (or (simple-vector-p x)
26       (and (complex-vector-p x)
27            (simple-vector-p (%array-data-vector x)))))
28 \f
29 ;;;; primitive predicates. These must be supported directly by the
30 ;;;; compiler.
31
32 (defun not (object)
33   #!+sb-doc
34   "Return T if X is NIL, otherwise return NIL."
35   (not object))
36
37 ;;; All the primitive type predicates share a parallel form..
38 (macrolet
39     ((frob ()
40        `(progn
41           ,@(mapcar (lambda (pred)
42                       (let* ((name (symbol-name pred))
43                              (stem (string-right-trim name "P-"))
44                              (article (if (find (schar name 0) "AEIOU")
45                                         "an"
46                                         "a")))
47                         `(defun ,pred (object)
48                            ,(format nil
49                                     "Return T if OBJECT is ~A ~A, ~
50                                      and NIL otherwise."
51                                     article
52                                     stem)
53                            (,pred object))))
54                     '(array-header-p
55                       arrayp
56                       atom
57                       base-char-p
58                       bignump
59                       bit-vector-p
60                       characterp
61                       code-component-p
62                       consp
63                       compiled-function-p
64                       complexp
65                       complex-double-float-p
66                       complex-float-p
67                       #!+long-float complex-long-float-p
68                       complex-rational-p
69                       complex-single-float-p
70                       ;; (COMPLEX-VECTOR-P is not included here since
71                       ;; it's awkward to express the type it tests for
72                       ;; in the Common Lisp type system, and since
73                       ;; it's only used in the implementation of a few
74                       ;; specialized things.)
75                       double-float-p
76                       fdefn-p
77                       fixnump
78                       floatp
79                       functionp
80                       integerp
81                       listp
82                       long-float-p
83                       lra-p
84                       null
85                       numberp
86                       rationalp
87                       ratiop
88                       realp
89                       short-float-p
90                       sb!kernel:simple-array-p
91                       simple-bit-vector-p
92                       simple-string-p
93                       simple-vector-p
94                       single-float-p
95                       stringp
96                       %instancep
97                       symbolp
98                       system-area-pointer-p
99                       weak-pointer-p
100                       vectorp
101                       unsigned-byte-32-p
102                       signed-byte-32-p
103                       simple-array-unsigned-byte-2-p
104                       simple-array-unsigned-byte-4-p
105                       simple-array-unsigned-byte-8-p
106                       simple-array-unsigned-byte-16-p
107                       simple-array-unsigned-byte-32-p
108                       simple-array-signed-byte-8-p
109                       simple-array-signed-byte-16-p
110                       simple-array-signed-byte-30-p
111                       simple-array-signed-byte-32-p
112                       simple-array-single-float-p
113                       simple-array-double-float-p
114                       #!+long-float simple-array-long-float-p
115                       simple-array-complex-single-float-p
116                       simple-array-complex-double-float-p
117                       #!+long-float simple-array-complex-long-float-p
118                       )))))
119   (frob))
120 \f
121 ;;; Return the specifier for the type of object. This is not simply
122 ;;; (TYPE-SPECIFIER (CTYPE-OF OBJECT)) because CTYPE-OF has different
123 ;;; goals than TYPE-OF. In particular, speed is more important than
124 ;;; precision, and it is not permitted to return member types.
125 (defun type-of (object)
126   #!+sb-doc
127   "Return the type of OBJECT."
128   (if (typep object '(or function array complex))
129     (type-specifier (ctype-of object))
130     (let* ((class (layout-class (layout-of object)))
131            (name (class-name class)))
132       (if (typep object 'instance)
133       (case name
134         (sb!alien-internals:alien-value
135          `(sb!alien:alien
136            ,(sb!alien-internals:unparse-alien-type
137              (sb!alien-internals:alien-value-type object))))
138         (t
139          (class-proper-name class)))
140       name))))
141 \f
142 ;;; FIXME: This belongs somewhere else, perhaps in code/array.lisp.
143 (defun upgraded-array-element-type (spec)
144   #!+sb-doc
145   "Return the element type that will actually be used to implement an array
146    with the specifier :ELEMENT-TYPE Spec."
147   (type-specifier
148    (array-type-specialized-element-type
149     (specifier-type `(array ,spec)))))
150 \f
151 ;;;; equality predicates
152
153 ;;; This is real simple, 'cause the compiler takes care of it.
154 (defun eq (obj1 obj2)
155   #!+sb-doc
156   "Return T if OBJ1 and OBJ2 are the same object, otherwise NIL."
157   (eq obj1 obj2))
158
159 (defun equal (x y)
160   #!+sb-doc
161   "Returns T if X and Y are EQL or if they are structured components
162   whose elements are EQUAL. Strings and bit-vectors are EQUAL if they
163   are the same length and have identical components. Other arrays must be
164   EQ to be EQUAL."
165   (cond ((eql x y) t)
166         ((consp x)
167          (and (consp y)
168               (equal (car x) (car y))
169               (equal (cdr x) (cdr y))))
170         ((stringp x)
171          (and (stringp y) (string= x y)))
172         ((pathnamep x)
173          (and (pathnamep y) (pathname= x y)))
174         ((bit-vector-p x)
175          (and (bit-vector-p y)
176               (= (the fixnum (length x))
177                  (the fixnum (length y)))
178               (do ((i 0 (1+ i))
179                    (length (length x)))
180                   ((= i length) t)
181                 (declare (fixnum i))
182                 (or (= (the fixnum (bit x i))
183                        (the fixnum (bit y i)))
184                     (return nil)))))
185         (t nil)))
186
187 ;;; EQUALP comparison of HASH-TABLE values
188 (defun hash-table-equalp (x y)
189   (declare (type hash-table x y))
190   (or (eq x y)
191       (and (hash-table-p y)
192            (eql (hash-table-count x) (hash-table-count y))
193            (eql (hash-table-test x) (hash-table-test y))
194            (block comparison-of-entries
195              (maphash (lambda (key x-value)
196                         (multiple-value-bind (y-value y-value-p)
197                             (gethash key y)
198                           (unless (and y-value-p (equalp x-value y-value))
199                             (return-from comparison-of-entries nil))))
200                       x)
201              t))))
202
203 (defun equalp (x y)
204   #+nil ; KLUDGE: If doc string, should be accurate: Talk about structures
205   ; and HASH-TABLEs.
206   "Just like EQUAL, but more liberal in several respects.
207   Numbers may be of different types, as long as the values are identical
208   after coercion. Characters may differ in alphabetic case. Vectors and
209   arrays must have identical dimensions and EQUALP elements, but may differ
210   in their type restriction."
211   (cond ((eq x y) t)
212         ((characterp x) (and (characterp y) (char-equal x y)))
213         ((numberp x) (and (numberp y) (= x y)))
214         ((consp x)
215          (and (consp y)
216               (equalp (car x) (car y))
217               (equalp (cdr x) (cdr y))))
218         ((pathnamep x)
219          (and (pathnamep y) (pathname= x y)))
220         ((hash-table-p x)
221          (and (hash-table-p y)
222               (hash-table-equalp x y)))
223         ((typep x 'instance)
224          (let* ((layout-x (%instance-layout x))
225                 (len (layout-length layout-x)))
226            (and (typep y 'instance)
227                 (eq layout-x (%instance-layout y))
228                 (structure-class-p (layout-class layout-x))
229                 (do ((i 1 (1+ i)))
230                     ((= i len) t)
231                   (declare (fixnum i))
232                   (let ((x-el (%instance-ref x i))
233                         (y-el (%instance-ref y i)))
234                     (unless (or (eq x-el y-el)
235                                 (equalp x-el y-el))
236                       (return nil)))))))
237         ((vectorp x)
238          (let ((length (length x)))
239            (and (vectorp y)
240                 (= length (length y))
241                 (dotimes (i length t)
242                   (let ((x-el (aref x i))
243                         (y-el (aref y i)))
244                     (unless (or (eq x-el y-el)
245                                 (equalp x-el y-el))
246                       (return nil)))))))
247         ((arrayp x)
248          (and (arrayp y)
249               (= (array-rank x) (array-rank y))
250               (dotimes (axis (array-rank x) t)
251                 (unless (= (array-dimension x axis)
252                            (array-dimension y axis))
253                   (return nil)))
254               (dotimes (index (array-total-size x) t)
255                 (let ((x-el (row-major-aref x index))
256                       (y-el (row-major-aref y index)))
257                   (unless (or (eq x-el y-el)
258                               (equalp x-el y-el))
259                     (return nil))))))
260         (t nil)))
261 #!+sb-test
262 (let ((test-cases '((0.0 -0.0 t)
263                     (0.0 1.0 nil)
264                     (#c(1 0) #c(1.0 0) t)
265                     (#c(1.1 0) #c(11/10 0) nil) ; due to roundoff error
266                     ("Hello" "hello" t)
267                     ("Hello" #(#\h #\E #\l #\l #\o) t)
268                     ("Hello" "goodbye" nil))))
269   (dolist (test-case test-cases)
270     (destructuring-bind (x y expected-result) test-case
271       (let* ((result (equalp x y))
272              (bresult (if result 1 0))
273              (expected-bresult (if expected-result 1 0)))
274         (unless (= bresult expected-bresult)
275           (error "failed test (EQUALP ~S ~S)" x y))))))