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