Fix make-array transforms.
[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 ;;; various (VECTOR FOO) type predicates, not implemented as simple
21 ;;; widetag tests
22 (macrolet
23     ((def ()
24        `(progn
25           ,@(loop for (name spec) in *vector-without-complex-typecode-infos*
26                   collect `(defun ,name (x)
27                              (or (typep x '(simple-array ,spec (*)))
28                                  (and (complex-vector-p x)
29                                       (do ((data (%array-data-vector x) (%array-data-vector data)))
30                                           ((not (array-header-p data)) (typep data '(simple-array ,spec (*))))))))))))
31   (def))
32
33 ;;; Is X an extended sequence?
34 (defun extended-sequence-p (x)
35   (and (not (listp x))
36        (not (vectorp x))
37        (let* ((slayout #.(info :type :compiler-layout 'sequence))
38              (depthoid #.(layout-depthoid (info :type :compiler-layout 'sequence)))
39              (layout (layout-of x)))
40         (when (layout-invalid layout)
41           (setq layout (update-object-layout-or-invalid x slayout)))
42         (if (eq layout slayout)
43             t
44             (let ((inherits (layout-inherits layout)))
45               (declare (optimize (safety 0)))
46               (and (> (length inherits) depthoid)
47                    (eq (svref inherits depthoid) slayout)))))))
48
49 ;;; Is X a SEQUENCE?  Harder than just (OR VECTOR LIST)
50 (defun sequencep (x)
51   (or (listp x)
52       (vectorp x)
53       (let* ((slayout #.(info :type :compiler-layout 'sequence))
54              (depthoid #.(layout-depthoid (info :type :compiler-layout 'sequence)))
55              (layout (layout-of x)))
56         (when (layout-invalid layout)
57           (setq layout (update-object-layout-or-invalid x slayout)))
58         (if (eq layout slayout)
59             t
60             (let ((inherits (layout-inherits layout)))
61               (declare (optimize (safety 0)))
62               (and (> (length inherits) depthoid)
63                    (eq (svref inherits depthoid) slayout)))))))
64 \f
65 ;;;; primitive predicates. These must be supported directly by the
66 ;;;; compiler.
67
68 (defun not (object)
69   #!+sb-doc
70   "Return T if X is NIL, otherwise return NIL."
71   (not object))
72
73 ;;; All the primitive type predicate wrappers share a parallel form..
74 (macrolet ((def-type-predicate-wrapper (pred)
75              (let* ((name (symbol-name pred))
76                     (stem (string-left-trim "%" (string-right-trim "P-" name)))
77                     (article (if (position (schar name 0) "AEIOU") "an" "a")))
78                `(defun ,pred (object)
79                   ,(format nil
80                            "Return true if OBJECT is ~A ~A, and NIL otherwise."
81                            article
82                            stem)
83                   ;; (falling through to low-level implementation)
84                   (,pred object)))))
85   (def-type-predicate-wrapper array-header-p)
86   (def-type-predicate-wrapper arrayp)
87   (def-type-predicate-wrapper atom)
88   ;; Testing for BASE-CHAR-P is usually redundant on #-sb-unicode,
89   ;; remove it there completely so that #-sb-unicode build will
90   ;; break when it's used.
91   #!+sb-unicode (def-type-predicate-wrapper base-char-p)
92   (def-type-predicate-wrapper base-string-p)
93   #!+sb-unicode (def-type-predicate-wrapper character-string-p)
94   (def-type-predicate-wrapper bignump)
95   (def-type-predicate-wrapper bit-vector-p)
96   (def-type-predicate-wrapper characterp)
97   (def-type-predicate-wrapper code-component-p)
98   (def-type-predicate-wrapper consp)
99   (def-type-predicate-wrapper compiled-function-p)
100   (def-type-predicate-wrapper complexp)
101   (def-type-predicate-wrapper complex-double-float-p)
102   (def-type-predicate-wrapper complex-float-p)
103   #!+long-float (def-type-predicate-wrapper complex-long-float-p)
104   (def-type-predicate-wrapper complex-rational-p)
105   (def-type-predicate-wrapper complex-single-float-p)
106   ;; (COMPLEX-VECTOR-P is not included here since it's awkward to express
107   ;; the type it tests for in the Common Lisp type system, and since it's
108   ;; only used in the implementation of a few specialized things.)
109   (def-type-predicate-wrapper double-float-p)
110   (def-type-predicate-wrapper extended-char-p)
111   (def-type-predicate-wrapper fdefn-p)
112   (def-type-predicate-wrapper fixnump)
113   (def-type-predicate-wrapper floatp)
114   (def-type-predicate-wrapper functionp)
115   (def-type-predicate-wrapper integerp)
116   (def-type-predicate-wrapper listp)
117   (def-type-predicate-wrapper long-float-p)
118   (def-type-predicate-wrapper lra-p)
119   (def-type-predicate-wrapper null)
120   (def-type-predicate-wrapper numberp)
121   (def-type-predicate-wrapper rationalp)
122   (def-type-predicate-wrapper ratiop)
123   (def-type-predicate-wrapper realp)
124   (def-type-predicate-wrapper short-float-p)
125   (def-type-predicate-wrapper single-float-p)
126   #!+sb-simd-pack (def-type-predicate-wrapper simd-pack-p)
127   (def-type-predicate-wrapper %instancep)
128   (def-type-predicate-wrapper symbolp)
129   (def-type-predicate-wrapper %other-pointer-p)
130   (def-type-predicate-wrapper system-area-pointer-p)
131   (def-type-predicate-wrapper weak-pointer-p)
132   #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
133   (progn
134     (def-type-predicate-wrapper unsigned-byte-32-p)
135     (def-type-predicate-wrapper signed-byte-32-p))
136   #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
137   (progn
138     (def-type-predicate-wrapper unsigned-byte-64-p)
139     (def-type-predicate-wrapper signed-byte-64-p))
140   ;; Specialized array types
141   (macrolet ((saetp-defs ()
142                `(progn
143                   ,@(map 'list
144                          (lambda (saetp)
145                            `(def-type-predicate-wrapper
146                                 ,(symbolicate (sb!vm:saetp-primitive-type-name saetp) "-P")))
147                          sb!vm:*specialized-array-element-type-properties*))))
148     (saetp-defs))
149   ;; Other array types
150   (def-type-predicate-wrapper simple-array-p)
151   (def-type-predicate-wrapper simple-string-p)
152   (def-type-predicate-wrapper stringp)
153   (def-type-predicate-wrapper vectorp)
154   (def-type-predicate-wrapper vector-nil-p))
155
156 #!+(or x86 x86-64)
157 (defun fixnum-mod-p (x limit)
158   (and (fixnump x)
159        (<= 0 x limit)))
160
161 \f
162 ;;; Return the specifier for the type of object. This is not simply
163 ;;; (TYPE-SPECIFIER (CTYPE-OF OBJECT)) because CTYPE-OF has different
164 ;;; goals than TYPE-OF. In particular, speed is more important than
165 ;;; precision, and it is not permitted to return member types.
166 (defun type-of (object)
167   #!+sb-doc
168   "Return the type of OBJECT."
169   (typecase object
170     (fixnum
171      (cond
172        ((<= 0 object 1) 'bit)
173        ((< object 0) 'fixnum)
174        (t '(integer 0 #.sb!xc:most-positive-fixnum))))
175     (integer
176      (if (>= object 0)
177          '(integer #.(1+ sb!xc:most-positive-fixnum))
178          'bignum))
179     (standard-char 'standard-char)
180     (base-char 'base-char)
181     (extended-char 'extended-char)
182     ((member t) 'boolean)
183     (keyword 'keyword)
184     ((or array complex #!+sb-simd-pack sb!kernel:simd-pack)
185      (type-specifier (ctype-of object)))
186     (t
187      (let* ((classoid (layout-classoid (layout-of object)))
188             (name (classoid-name classoid)))
189        (if (%instancep object)
190            (case name
191              (sb!alien-internals:alien-value
192               `(sb!alien:alien
193                 ,(sb!alien-internals:unparse-alien-type
194                   (sb!alien-internals:alien-value-type object))))
195              (t
196               (let ((pname (classoid-proper-name classoid)))
197                 (if (classoid-p pname)
198                     (classoid-pcl-class pname)
199                     pname))))
200            name)))))
201 \f
202 ;;;; equality predicates
203
204 ;;; This is real simple, 'cause the compiler takes care of it.
205 (defun eq (obj1 obj2)
206   #!+sb-doc
207   "Return T if OBJ1 and OBJ2 are the same object, otherwise NIL."
208   (eq obj1 obj2))
209
210 (declaim (inline %eql))
211 (defun %eql (obj1 obj2)
212   #!+sb-doc
213   "Return T if OBJ1 and OBJ2 represent the same object, otherwise NIL."
214   (or (eq obj1 obj2)
215       (if (or (typep obj2 'fixnum)
216               (not (typep obj2 'number)))
217           nil
218           (macrolet ((foo (&rest stuff)
219                        `(typecase obj2
220                           ,@(mapcar (lambda (foo)
221                                       (let ((type (car foo))
222                                             (fn (cadr foo)))
223                                         `(,type
224                                           (and (typep obj1 ',type)
225                                                (,fn obj1 obj2)))))
226                                     stuff))))
227             (foo
228              (single-float eql)
229              (double-float eql)
230              #!+long-float
231              (long-float eql)
232              (bignum
233               (lambda (x y)
234                 (zerop (bignum-compare x y))))
235              (ratio
236               (lambda (x y)
237                 (and (eql (numerator x) (numerator y))
238                      (eql (denominator x) (denominator y)))))
239              (complex
240               (lambda (x y)
241                 (and (eql (realpart x) (realpart y))
242                      (eql (imagpart x) (imagpart y))))))))))
243
244 (defun eql (x y)
245   (%eql x y))
246
247 (defun bit-vector-= (x y)
248   (declare (type bit-vector x y))
249   (cond ((eq x y))
250         ((and (simple-bit-vector-p x)
251               (simple-bit-vector-p y))
252          (bit-vector-= x y))            ; DEFTRANSFORM
253         (t
254          (and (= (length x) (length y))
255               (do ((i 0 (1+ i))
256                    (length (length x)))
257                   ((= i length) t)
258                 (declare (fixnum i))
259                 (unless (= (bit x i) (bit y i))
260                   (return nil)))))))
261
262 (defun equal (x y)
263   #!+sb-doc
264   "Return T if X and Y are EQL or if they are structured components whose
265 elements are EQUAL. Strings and bit-vectors are EQUAL if they are the same
266 length and have identical components. Other arrays must be EQ to be EQUAL."
267   ;; Non-tail self-recursion implemented with a local auxiliary function
268   ;; is a lot faster than doing it the straightforward way (at least
269   ;; on x86oids) due to calling convention differences. -- JES, 2005-12-30
270   (labels ((equal-aux (x y)
271              (cond ((%eql x y)
272                     t)
273                    ((consp x)
274                     (and (consp y)
275                          (equal-aux (car x) (car y))
276                          (equal-aux (cdr x) (cdr y))))
277                    ((stringp x)
278                     (and (stringp y) (string= x y)))
279                    ((pathnamep x)
280                     (and (pathnamep y) (pathname= x y)))
281                    ((bit-vector-p x)
282                     (and (bit-vector-p y)
283                          (bit-vector-= x y)))
284                    (t nil))))
285     ;; Use MAYBE-INLINE to get the inline expansion only once (instead
286     ;; of 200 times with INLINE). -- JES, 2005-12-30
287     (declare (maybe-inline equal-aux))
288     (equal-aux x y)))
289
290 ;;; EQUALP comparison of HASH-TABLE values
291 (defun hash-table-equalp (x y)
292   (declare (type hash-table x y))
293   (or (eq x y)
294       (and (hash-table-p y)
295            (eql (hash-table-count x) (hash-table-count y))
296            (eql (hash-table-test x) (hash-table-test y))
297            (block comparison-of-entries
298              (maphash (lambda (key x-value)
299                         (multiple-value-bind (y-value y-value-p)
300                             (gethash key y)
301                           (unless (and y-value-p (equalp x-value y-value))
302                             (return-from comparison-of-entries nil))))
303                       x)
304              t))))
305
306 (defun equalp (x y)
307   #+nil ; KLUDGE: If doc string, should be accurate: Talk about structures
308   ; and HASH-TABLEs.
309   "This is like EQUAL, except more liberal in several respects.
310   Numbers may be of different types, as long as the values are identical
311   after coercion. Characters may differ in alphabetic case. Vectors and
312   arrays must have identical dimensions and EQUALP elements, but may differ
313   in their type restriction."
314   (cond ((eq x y) t)
315         ((characterp x) (and (characterp y) (char-equal x y)))
316         ((numberp x) (and (numberp y) (= x y)))
317         ((consp x)
318          (and (consp y)
319               (equalp (car x) (car y))
320               (equalp (cdr x) (cdr y))))
321         ((pathnamep x)
322          (and (pathnamep y) (pathname= x y)))
323         ((hash-table-p x)
324          (and (hash-table-p y)
325               (hash-table-equalp x y)))
326         ((%instancep x)
327          (let* ((layout-x (%instance-layout x))
328                 (raw-len (layout-n-untagged-slots layout-x))
329                 (total-len (layout-length layout-x))
330                 (normal-len (- total-len raw-len)))
331            (and (%instancep y)
332                 (eq layout-x (%instance-layout y))
333                 (structure-classoid-p (layout-classoid layout-x))
334                 (dotimes (i normal-len t)
335                   (let ((x-el (%instance-ref x i))
336                         (y-el (%instance-ref y i)))
337                     (unless (or (eq x-el y-el)
338                                 (equalp x-el y-el))
339                       (return nil))))
340                 (if (zerop raw-len)
341                     t
342                     (raw-instance-slots-equalp layout-x x y)))))
343         ((vectorp x)
344          (let ((length (length x)))
345            (and (vectorp y)
346                 (= length (length y))
347                 (dotimes (i length t)
348                   (let ((x-el (aref x i))
349                         (y-el (aref y i)))
350                     (unless (or (eq x-el y-el)
351                                 (equalp x-el y-el))
352                       (return nil)))))))
353         ((arrayp x)
354          (and (arrayp y)
355               (= (array-rank x) (array-rank y))
356               (dotimes (axis (array-rank x) t)
357                 (unless (= (array-dimension x axis)
358                            (array-dimension y axis))
359                   (return nil)))
360               (dotimes (index (array-total-size x) t)
361                 (let ((x-el (row-major-aref x index))
362                       (y-el (row-major-aref y index)))
363                   (unless (or (eq x-el y-el)
364                               (equalp x-el y-el))
365                     (return nil))))))
366         (t nil)))
367
368 (/show0 "about to do test cases in pred.lisp")
369 #!+sb-test
370 (let ((test-cases `((0.0 ,(load-time-value (make-unportable-float :single-float-negative-zero)) t)
371                     (0.0 1.0 nil)
372                     (#c(1 0) #c(1.0 0.0) t)
373                     (#c(0 1) #c(0.0 1.0) t)
374                     (#c(1.1 0.0) #c(11/10 0) nil) ; due to roundoff error
375                     ("Hello" "hello" t)
376                     ("Hello" #(#\h #\E #\l #\l #\o) t)
377                     ("Hello" "goodbye" nil))))
378   (/show0 "TEST-CASES bound in pred.lisp")
379   (dolist (test-case test-cases)
380     (/show0 "about to do a TEST-CASE in pred.lisp")
381     (destructuring-bind (x y expected-result) test-case
382       (let* ((result (equalp x y))
383              (bresult (if result 1 0))
384              (expected-bresult (if expected-result 1 0)))
385         (unless (= bresult expected-bresult)
386           (/show0 "failing test in pred.lisp")
387           (error "failed test (EQUALP ~S ~S)" x y))))))
388 (/show0 "done with test cases in pred.lisp")