/[cmucl]/src/code/pred.lisp
ViewVC logotype

Contents of /src/code/pred.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.51 - (show annotations)
Mon Jan 10 15:25:09 2000 UTC (14 years, 3 months ago) by dtc
Branch: MAIN
Changes since 1.50: +2 -2 lines
Fix for equalp when passed a character and a non-character; needed
to check the type of the second argument before calling char-equal.
Spotted by William H. Newman, fix from Peter Van Eynde.
1 ;;; -*- Mode: Lisp; Package: LISP; Log: code.log -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the CMU Common Lisp project at
5 ;;; Carnegie Mellon University, and has been placed in the public domain.
6 ;;;
7 (ext:file-comment
8 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/pred.lisp,v 1.51 2000/01/10 15:25:09 dtc Exp $")
9 ;;;
10 ;;; **********************************************************************
11 ;;;
12 ;;; Predicate functions for CMU Common Lisp.
13 ;;;
14 ;;; Written by William Lott.
15 ;;;
16
17 (in-package "KERNEL")
18 (export '(%instancep instance fixnump bignump bitp ratiop weak-pointer-p
19 %typep class-cell-typep))
20
21 (in-package "SYSTEM")
22 (export '(system-area-pointer system-area-pointer-p))
23
24 (in-package "LISP")
25
26 (export '(typep null symbolp atom consp listp numberp integerp rationalp
27 floatp complexp characterp stringp bit-vector-p vectorp
28 simple-vector-p simple-string-p simple-bit-vector-p arrayp
29 functionp compiled-function-p eq eql equal equalp not
30 type-of upgraded-array-element-type realp
31 ;; Names of types...
32 array atom bignum bit bit-vector character
33 compiled-function complex cons double-float
34 fixnum float function integer keyword list long-float nil
35 null number ratio rational real sequence short-float signed-byte
36 simple-array simple-bit-vector simple-string simple-vector
37 single-float standard-char base-char string symbol t
38 unsigned-byte vector satisfies))
39
40
41
42 ;;;; Primitive predicates. These must be supported by the compiler.
43
44 (eval-when (compile eval)
45 (defparameter primitive-predicates
46 '(array-header-p
47 arrayp
48 atom
49 base-char-p
50 bignump
51 bit-vector-p
52 characterp
53 code-component-p
54 consp
55 compiled-function-p
56 complexp
57 complex-double-float-p
58 complex-float-p
59 #+long-float complex-long-float-p
60 complex-rational-p
61 complex-single-float-p
62 double-float-p
63 fdefn-p
64 fixnump
65 floatp
66 functionp
67 integerp
68 listp
69 long-float-p
70 lra-p
71 not
72 null
73 numberp
74 rationalp
75 ratiop
76 realp
77 scavenger-hook-p
78 short-float-p
79 simple-array-p
80 simple-bit-vector-p
81 simple-string-p
82 simple-vector-p
83 single-float-p
84 stringp
85 %instancep
86 symbolp
87 system-area-pointer-p
88 weak-pointer-p
89 vectorp
90 unsigned-byte-32-p
91 signed-byte-32-p
92 simple-array-unsigned-byte-2-p
93 simple-array-unsigned-byte-4-p
94 simple-array-unsigned-byte-8-p
95 simple-array-unsigned-byte-16-p
96 simple-array-unsigned-byte-32-p
97 simple-array-signed-byte-8-p
98 simple-array-signed-byte-16-p
99 simple-array-signed-byte-30-p
100 simple-array-signed-byte-32-p
101 simple-array-single-float-p
102 simple-array-double-float-p
103 #+long-float simple-array-long-float-p
104 simple-array-complex-single-float-p
105 simple-array-complex-double-float-p
106 #+long-float simple-array-complex-long-float-p
107 )))
108
109 (macrolet
110 ((frob ()
111 `(progn
112 ,@(mapcar #'(lambda (pred)
113 `(defun ,pred (object)
114 ,(format nil
115 "Return T if OBJECT is a~:[~;n~] ~(~A~) ~
116 and NIL otherwise."
117 (find (schar (string pred) 0) "AEIOUaeiou")
118 (string pred))
119 (,pred object)))
120 primitive-predicates))))
121 (frob))
122
123
124 ;;;; TYPE-OF -- public.
125 ;;;
126 ;;; Return the specifier for the type of object. This is not simply
127 ;;; (type-specifier (ctype-of object)) because ctype-of has different goals
128 ;;; than type-of. In particular, speed is more important than precision, and
129 ;;; it is not permitted to return member types.
130 ;;;
131 (defun type-of (object)
132 "Return the type of OBJECT."
133 (if (typep object '(or function array complex))
134 (type-specifier (ctype-of object))
135 (let* ((class (layout-class (layout-of object)))
136 (name (class-name class)))
137 (if (%instancep object)
138 (case name
139 (alien-internals:alien-value
140 `(alien:alien
141 ,(alien-internals:unparse-alien-type
142 (alien-internals:alien-value-type object))))
143 (t
144 (class-proper-name class)))
145 name))))
146
147
148 ;;;; UPGRADED-ARRAY-ELEMENT-TYPE -- public
149 ;;;
150 (defun upgraded-array-element-type (spec)
151 "Return the element type that will actually be used to implement an array
152 with the specifier :ELEMENT-TYPE Spec."
153 (type-specifier
154 (array-type-specialized-element-type
155 (specifier-type `(array ,spec)))))
156
157 ;;;; SUBTYPEP -- public.
158 ;;;
159 ;;; Just parse the type specifiers and call csubtype.
160 ;;;
161 (defun subtypep (type1 type2)
162 "Return two values indicating the relationship between type1 and type2:
163 T and T: type1 definitely is a subtype of type2.
164 NIL and T: type1 definitely is not a subtype of type2.
165 NIL and NIL: who knows?"
166 (csubtypep (specifier-type type1) (specifier-type type2)))
167
168
169 ;;;; TYPEP:
170
171 (declaim (start-block typep %typep class-cell-typep))
172
173 ;;; TYPEP -- public.
174 ;;;
175 ;;; Just call %typep
176 ;;;
177 (defun typep (object type)
178 "Return T iff OBJECT is of type TYPE."
179 (%typep object type))
180
181
182 ;;; %TYPEP -- internal.
183 ;;;
184 ;;; The actual typep engine. The compiler only generates calls to this
185 ;;; function when it can't figure out anything more intelligent to do.
186 ;;;
187 (defun %typep (object specifier)
188 (%%typep object
189 (if (ctype-p specifier)
190 specifier
191 (specifier-type specifier))))
192 ;;;
193 (defun %%typep (object type)
194 (declare (type ctype type))
195 (etypecase type
196 (named-type
197 (ecase (named-type-name type)
198 ((* t) t)
199 ((nil) nil)))
200 (numeric-type
201 (and (numberp object)
202 (let ((num (if (complexp object) (realpart object) object)))
203 (ecase (numeric-type-class type)
204 (integer (integerp num))
205 (rational (rationalp num))
206 (float
207 (ecase (numeric-type-format type)
208 (short-float (typep num 'short-float))
209 (single-float (typep num 'single-float))
210 (double-float (typep num 'double-float))
211 (long-float (typep num 'long-float))
212 ((nil) (floatp num))))
213 ((nil) t)))
214 #-negative-zero-is-not-zero
215 (flet ((bound-test (val)
216 (let ((low (numeric-type-low type))
217 (high (numeric-type-high type)))
218 (and (cond ((null low) t)
219 ((listp low) (> val (car low)))
220 (t (>= val low)))
221 (cond ((null high) t)
222 ((listp high) (< val (car high)))
223 (t (<= val high)))))))
224 (ecase (numeric-type-complexp type)
225 ((nil) t)
226 (:complex
227 (and (complexp object)
228 (bound-test (realpart object))
229 (bound-test (imagpart object))))
230 (:real
231 (and (not (complexp object))
232 (bound-test object)))))
233 #+negative-zero-is-not-zero
234 (labels ((signed-> (x y)
235 (if (and (zerop x) (zerop y) (floatp x) (floatp y))
236 (> (float-sign x) (float-sign y))
237 (> x y)))
238 (signed->= (x y)
239 (if (and (zerop x) (zerop y) (floatp x) (floatp y))
240 (>= (float-sign x) (float-sign y))
241 (>= x y)))
242 (bound-test (val)
243 (let ((low (numeric-type-low type))
244 (high (numeric-type-high type)))
245 (and (cond ((null low) t)
246 ((listp low)
247 (signed-> val (car low)))
248 (t
249 (signed->= val low)))
250 (cond ((null high) t)
251 ((listp high)
252 (signed-> (car high) val))
253 (t
254 (signed->= high val)))))))
255 (ecase (numeric-type-complexp type)
256 ((nil) t)
257 (:complex
258 (and (complexp object)
259 (bound-test (realpart object))
260 (bound-test (imagpart object))))
261 (:real
262 (and (not (complexp object))
263 (bound-test object)))))))
264 (array-type
265 (and (arrayp object)
266 (ecase (array-type-complexp type)
267 ((t) (not (typep object 'simple-array)))
268 ((nil) (typep object 'simple-array))
269 (* t))
270 (or (eq (array-type-dimensions type) '*)
271 (do ((want (array-type-dimensions type) (cdr want))
272 (got (array-dimensions object) (cdr got)))
273 ((and (null want) (null got)) t)
274 (unless (and want got
275 (or (eq (car want) '*)
276 (= (car want) (car got))))
277 (return nil))))
278 (or (eq (array-type-element-type type) *wild-type*)
279 (type= (array-type-specialized-element-type type)
280 (specifier-type (array-element-type object))))))
281 (member-type
282 (if (member object (member-type-members type)) t))
283 (class
284 (class-typep (layout-of object) type object))
285 (union-type
286 (dolist (type (union-type-types type))
287 (when (%%typep object type)
288 (return t))))
289 (unknown-type
290 ;; Parse it again to make sure it's really undefined.
291 (let ((reparse (specifier-type (unknown-type-specifier type))))
292 (if (typep reparse 'unknown-type)
293 (error "Unknown type specifier: ~S"
294 (unknown-type-specifier reparse))
295 (%%typep object reparse))))
296 (hairy-type
297 ;; Now the tricky stuff.
298 (let* ((hairy-spec (hairy-type-specifier type))
299 (symbol (if (consp hairy-spec) (car hairy-spec) hairy-spec)))
300 (ecase symbol
301 (and
302 (or (atom hairy-spec)
303 (dolist (spec (cdr hairy-spec) t)
304 (unless (%%typep object (specifier-type spec))
305 (return nil)))))
306 (not
307 (unless (and (listp hairy-spec) (= (length hairy-spec) 2))
308 (error "Invalid type specifier: ~S" hairy-spec))
309 (not (%%typep object (specifier-type (cadr hairy-spec)))))
310 (satisfies
311 (unless (and (listp hairy-spec) (= (length hairy-spec) 2))
312 (error "Invalid type specifier: ~S" hairy-spec))
313 (let ((fn (cadr hairy-spec)))
314 (if (funcall (typecase fn
315 (function fn)
316 (symbol (symbol-function fn))
317 (t
318 (coerce fn 'function)))
319 object)
320 t
321 nil))))))
322 (alien-type-type
323 (alien-internals:alien-typep object (alien-type-type-alien-type type)))
324 (function-type
325 (error "Function types are not a legal argument to TYPEP:~% ~S"
326 (type-specifier type)))))
327
328
329 ;;; CLASS-CELL-TYPEP -- Interface
330 ;;;
331 ;;; Do type test from a class cell, allowing forward reference and
332 ;;; redefinition.
333 ;;;
334 (defun class-cell-typep (obj-layout cell object)
335 (let ((class (class-cell-class cell)))
336 (unless class
337 (error "Class has not yet been defined: ~S" (class-cell-name cell)))
338 (class-typep obj-layout class object)))
339
340
341 ;;; CLASS-TYPEP -- Internal
342 ;;;
343 ;;; Test whether Obj-Layout is from an instance of Class.
344 ;;;
345 (defun class-typep (obj-layout class object)
346 (declare (optimize speed))
347 (when (layout-invalid obj-layout)
348 (if (and (typep (class-of object) 'standard-class) object)
349 (setq obj-layout (pcl::check-wrapper-validity object))
350 (error "TYPEP on obsolete object (was class ~S)."
351 (class-proper-name (layout-class obj-layout)))))
352 (let ((layout (class-layout class))
353 (obj-inherits (layout-inherits obj-layout)))
354 (when (layout-invalid layout)
355 (error "Class is currently invalid: ~S" class))
356 (or (eq obj-layout layout)
357 (dotimes (i (length obj-inherits) nil)
358 (when (eq (svref obj-inherits i) layout)
359 (return t))))))
360
361 (declaim (end-block))
362
363
364 ;;;; Equality predicates.
365
366 ;;; EQ -- public.
367 ;;;
368 ;;; Real simple, 'cause the compiler takes care of it.
369 ;;;
370
371 (defun eq (obj1 obj2)
372 "Return T if OBJ1 and OBJ2 are the same object, otherwise NIL."
373 (eq obj1 obj2))
374
375
376 ;;; EQUAL -- public.
377 ;;;
378 (defun equal (x y)
379 "Returns T if X and Y are EQL or if they are structured components
380 whose elements are EQUAL. Strings and bit-vectors are EQUAL if they
381 are the same length and have indentical components. Other arrays must be
382 EQ to be EQUAL."
383 (cond ((eql x y) t)
384 ((consp x)
385 (and (consp y)
386 (equal (car x) (car y))
387 (equal (cdr x) (cdr y))))
388 ((stringp x)
389 (and (stringp y) (string= x y)))
390 ((pathnamep x)
391 (and (pathnamep y) (pathname= x y)))
392 ((bit-vector-p x)
393 (and (bit-vector-p y)
394 (= (the fixnum (length x))
395 (the fixnum (length y)))
396 (do ((i 0 (1+ i))
397 (length (length x)))
398 ((= i length) t)
399 (declare (fixnum i))
400 (or (= (the fixnum (bit x i))
401 (the fixnum (bit y i)))
402 (return nil)))))
403 (t nil)))
404
405 ;;; EQUALP -- public.
406 ;;;
407 (defun equalp (x y)
408 "Just like EQUAL, but more liberal in several respects.
409 Numbers may be of different types, as long as the values are identical
410 after coercion. Characters may differ in alphabetic case. Vectors and
411 arrays must have identical dimensions and EQUALP elements, but may differ
412 in their type restriction."
413 (cond ((eq x y) t)
414 ((characterp x) (and (characterp y) (char-equal x y)))
415 ((numberp x) (and (numberp y) (= x y)))
416 ((consp x)
417 (and (consp y)
418 (equalp (car x) (car y))
419 (equalp (cdr x) (cdr y))))
420 ((pathnamep x)
421 (and (pathnamep y) (pathname= x y)))
422 ((%instancep x)
423 (let* ((layout-x (%instance-layout x))
424 (len (layout-length layout-x)))
425 (and (%instancep y)
426 (eq layout-x (%instance-layout y))
427 (structure-class-p (layout-class layout-x))
428 (do ((i 1 (1+ i)))
429 ((= i len) t)
430 (declare (fixnum i))
431 (let ((x-el (%instance-ref x i))
432 (y-el (%instance-ref y i)))
433 (unless (or (eq x-el y-el)
434 (equalp x-el y-el))
435 (return nil)))))))
436 ((vectorp x)
437 (let ((length (length x)))
438 (and (vectorp y)
439 (= length (length y))
440 (dotimes (i length t)
441 (let ((x-el (aref x i))
442 (y-el (aref y i)))
443 (unless (or (eq x-el y-el)
444 (equalp x-el y-el))
445 (return nil)))))))
446 ((arrayp x)
447 (and (arrayp y)
448 (= (array-rank x) (array-rank y))
449 (dotimes (axis (array-rank x) t)
450 (unless (= (array-dimension x axis)
451 (array-dimension y axis))
452 (return nil)))
453 (dotimes (index (array-total-size x) t)
454 (let ((x-el (row-major-aref x index))
455 (y-el (row-major-aref y index)))
456 (unless (or (eq x-el y-el)
457 (equalp x-el y-el))
458 (return nil))))))
459 (t nil)))

  ViewVC Help
Powered by ViewVC 1.1.5