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

Contents of /src/code/pred.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.58 - (show annotations)
Sun Apr 27 14:52:27 2003 UTC (10 years, 11 months ago) by toy
Branch: MAIN
Changes since 1.57: +1 -33 lines
Remove all stuff relating to :negative-zero-is-not-zero which enabled
the non-ANSI feature of making -0 and +0 different in
type-specifiers.  Internal code still assumes this in various places,
however.  Since our type system is much smarter now than when this was
written (and handles type unions and member types much better),
perhaps we should clean this up?
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.58 2003/04/27 14:52:27 toy 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 &optional environment)
151 "Return the element type that will actually be used to implement an array
152 with the specifier :ELEMENT-TYPE Spec."
153 ;; Type expansion (TYPE-EXPAND) currently doesn't handle environments.
154 (declare (ignore environment))
155 (type-specifier
156 (array-type-specialized-element-type
157 (specifier-type `(array ,spec)))))
158
159 ;;;; SUBTYPEP -- public.
160 ;;;
161 ;;; Just parse the type specifiers and call csubtype.
162 ;;;
163 (defun subtypep (type1 type2 &optional environment)
164 "Return two values indicating the relationship between type1 and type2:
165 T and T: type1 definitely is a subtype of type2.
166 NIL and T: type1 definitely is not a subtype of type2.
167 NIL and NIL: who knows?"
168 (declare (ignore environment))
169 (csubtypep (specifier-type type1) (specifier-type type2)))
170
171
172 ;;;; TYPEP:
173
174 (declaim (start-block typep %typep class-cell-typep))
175
176 ;;; TYPEP -- public.
177 ;;;
178 ;;; Just call %typep
179 ;;;
180 (defun typep (object type &optional environment)
181 "Return T iff OBJECT is of type TYPE."
182 (declare (ignore environment))
183 (%typep object type))
184
185
186 ;;; %TYPEP -- internal.
187 ;;;
188 ;;; The actual typep engine. The compiler only generates calls to this
189 ;;; function when it can't figure out anything more intelligent to do.
190 ;;;
191 (defun %typep (object specifier)
192 (%%typep object
193 (if (ctype-p specifier)
194 specifier
195 (specifier-type specifier))))
196 ;;;
197 (defun %%typep (object type)
198 (declare (type ctype type))
199 (etypecase type
200 (named-type
201 (ecase (named-type-name type)
202 ((* t) t)
203 ((nil) nil)))
204 (numeric-type
205 (and (numberp object)
206 (let ((num (if (complexp object) (realpart object) object)))
207 (ecase (numeric-type-class type)
208 (integer (integerp num))
209 (rational (rationalp num))
210 (float
211 (ecase (numeric-type-format type)
212 (short-float (typep num 'short-float))
213 (single-float (typep num 'single-float))
214 (double-float (typep num 'double-float))
215 (long-float (typep num 'long-float))
216 ((nil) (floatp num))))
217 ((nil) t)))
218 (flet ((bound-test (val)
219 (let ((low (numeric-type-low type))
220 (high (numeric-type-high type)))
221 (and (cond ((null low) t)
222 ((listp low) (> val (car low)))
223 (t (>= val low)))
224 (cond ((null high) t)
225 ((listp high) (< val (car high)))
226 (t (<= val high)))))))
227 (ecase (numeric-type-complexp type)
228 ((nil) t)
229 (:complex
230 (and (complexp object)
231 (bound-test (realpart object))
232 (bound-test (imagpart object))))
233 (:real
234 (and (not (complexp object))
235 (bound-test object)))))))
236 (array-type
237 (and (arrayp object)
238 (ecase (array-type-complexp type)
239 ((t) (not (typep object 'simple-array)))
240 ((nil) (typep object 'simple-array))
241 ((* :maybe) t))
242 (or (eq (array-type-dimensions type) '*)
243 (do ((want (array-type-dimensions type) (cdr want))
244 (got (array-dimensions object) (cdr got)))
245 ((and (null want) (null got)) t)
246 (unless (and want got
247 (or (eq (car want) '*)
248 (= (car want) (car got))))
249 (return nil))))
250 (if (unknown-type-p (array-type-element-type type))
251 ;; better to fail this way than to get bogosities like
252 ;; (TYPEP (MAKE-ARRAY 11) '(ARRAY SOME-UNDEFINED-TYPE)) => T
253 (error "~@<unknown element type in array type: ~2I~_~S~:>"
254 (type-specifier type))
255 t)
256 (or (eq (array-type-element-type type) *wild-type*)
257 (values (type= (array-type-specialized-element-type type)
258 (specifier-type (array-element-type
259 object)))))))
260 (member-type
261 (if (member object (member-type-members type)) t))
262 (kernel::class
263 (class-typep (layout-of object) type object))
264 (union-type
265 (some (lambda (type) (%%typep object type))
266 (union-type-types type)))
267 (intersection-type
268 (every (lambda (type) (%%typep object type))
269 (intersection-type-types type)))
270 (cons-type
271 (and (consp object)
272 (%%typep (car object) (cons-type-car-type type))
273 (%%typep (cdr object) (cons-type-cdr-type type))))
274 (unknown-type
275 ;; Parse it again to make sure it's really undefined.
276 (let ((reparse (specifier-type (unknown-type-specifier type))))
277 (if (typep reparse 'unknown-type)
278 (error "Unknown type specifier: ~S"
279 (unknown-type-specifier reparse))
280 (%%typep object reparse))))
281 (negation-type
282 (not (%%typep object (negation-type-type type))))
283 (hairy-type
284 ;; Now the tricky stuff.
285 (let* ((hairy-spec (hairy-type-specifier type))
286 (symbol (if (consp hairy-spec) (car hairy-spec) hairy-spec)))
287 (ecase symbol
288 (and
289 (every (lambda (spec) (%%typep object (specifier-type spec)))
290 (rest hairy-spec)))
291 ;; Note: it should be safe to skip OR here, because union
292 ;; types can always be represented as UNION-TYPE in general
293 ;; or other CTYPEs in special cases; we never need to use
294 ;; HAIRY-TYPE for them.
295 (not
296 (unless (and (listp hairy-spec) (= (length hairy-spec) 2))
297 (error "Invalid type specifier: ~S" hairy-spec))
298 (not (%%typep object (specifier-type (cadr hairy-spec)))))
299 (satisfies
300 (unless (and (listp hairy-spec) (= (length hairy-spec) 2))
301 (error "Invalid type specifier: ~S" hairy-spec))
302 (let ((fn (cadr hairy-spec)))
303 (if (funcall (typecase fn
304 (function fn)
305 (symbol (symbol-function fn))
306 (t
307 (coerce fn 'function)))
308 object)
309 t
310 nil))))))
311 (alien-type-type
312 (alien-internals:alien-typep object (alien-type-type-alien-type type)))
313 (function-type
314 (error "Function types are not a legal argument to TYPEP:~% ~S"
315 (type-specifier type)))))
316
317
318 ;;; CLASS-CELL-TYPEP -- Interface
319 ;;;
320 ;;; Do type test from a class cell, allowing forward reference and
321 ;;; redefinition.
322 ;;;
323 (defun class-cell-typep (obj-layout cell object)
324 (let ((class (class-cell-class cell)))
325 (unless class
326 (error "Class has not yet been defined: ~S" (class-cell-name cell)))
327 (class-typep obj-layout class object)))
328
329
330 ;;; CLASS-TYPEP -- Internal
331 ;;;
332 ;;; Test whether Obj-Layout is from an instance of Class.
333 ;;;
334 (defun class-typep (obj-layout class object)
335 (declare (optimize speed))
336 (when (layout-invalid obj-layout)
337 (if (and (typep (kernel::class-of object) 'kernel::standard-class) object)
338 (setq obj-layout (pcl::check-wrapper-validity object))
339 (error "TYPEP on obsolete object (was class ~S)."
340 (class-proper-name (layout-class obj-layout)))))
341 (let ((layout (%class-layout class))
342 (obj-inherits (layout-inherits obj-layout)))
343 (when (layout-invalid layout)
344 (error "Class is currently invalid: ~S" class))
345 (or (eq obj-layout layout)
346 (dotimes (i (length obj-inherits) nil)
347 (when (eq (svref obj-inherits i) layout)
348 (return t))))))
349
350 (declaim (end-block))
351
352
353 ;;;; Equality predicates.
354
355 ;;; EQ -- public.
356 ;;;
357 ;;; Real simple, 'cause the compiler takes care of it.
358 ;;;
359
360 (defun eq (obj1 obj2)
361 "Return T if OBJ1 and OBJ2 are the same object, otherwise NIL."
362 (eq obj1 obj2))
363
364
365 ;;; EQUAL -- public.
366 ;;;
367 (defun equal (x y)
368 "Returns T if X and Y are EQL or if they are structured components
369 whose elements are EQUAL. Strings and bit-vectors are EQUAL if they
370 are the same length and have indentical components. Other arrays must be
371 EQ to be EQUAL."
372 (cond ((eql x y) t)
373 ((consp x)
374 (and (consp y)
375 (equal (car x) (car y))
376 (equal (cdr x) (cdr y))))
377 ((stringp x)
378 (and (stringp y) (string= x y)))
379 ((pathnamep x)
380 (and (pathnamep y) (pathname= x y)))
381 ((bit-vector-p x)
382 (and (bit-vector-p y)
383 (= (the fixnum (length x))
384 (the fixnum (length y)))
385 (do ((i 0 (1+ i))
386 (length (length x)))
387 ((= i length) t)
388 (declare (fixnum i))
389 (or (= (the fixnum (bit x i))
390 (the fixnum (bit y i)))
391 (return nil)))))
392 (t nil)))
393
394 ;;; EQUALP -- public.
395 ;;;
396 (defun equalp (x y)
397 "Just like EQUAL, but more liberal in several respects.
398 Numbers may be of different types, as long as the values are identical
399 after coercion. Characters may differ in alphabetic case. Vectors and
400 arrays must have identical dimensions and EQUALP elements, but may differ
401 in their type restriction."
402 (cond ((eq x y) t)
403 ((characterp x) (and (characterp y) (char-equal x y)))
404 ((numberp x) (and (numberp y) (= x y)))
405 ((consp x)
406 (and (consp y)
407 (equalp (car x) (car y))
408 (equalp (cdr x) (cdr y))))
409 ((pathnamep x)
410 (and (pathnamep y) (pathname= x y)))
411 ((hash-table-p x)
412 (and (hash-table-p y)
413 (eql (hash-table-count x) (hash-table-count y))
414 (eql (hash-table-test x) (hash-table-test y))
415 (with-hash-table-iterator (next x)
416 (loop
417 (multiple-value-bind (more x-key x-value)
418 (next)
419 (cond (more
420 (multiple-value-bind (y-value foundp)
421 (gethash x-key y)
422 (unless (and foundp (equalp x-value y-value))
423 (return nil))))
424 (t
425 (return t))))))))
426 ((%instancep x)
427 (let* ((layout-x (%instance-layout x))
428 (len (layout-length layout-x)))
429 (and (%instancep y)
430 (eq layout-x (%instance-layout y))
431 (structure-class-p (layout-class layout-x))
432 (do ((i 1 (1+ i)))
433 ((= i len) t)
434 (declare (fixnum i))
435 (let ((x-el (%instance-ref x i))
436 (y-el (%instance-ref y i)))
437 (unless (or (eq x-el y-el)
438 (equalp x-el y-el))
439 (return nil)))))))
440 ((vectorp x)
441 (let ((length (length x)))
442 (and (vectorp y)
443 (= length (length y))
444 (dotimes (i length t)
445 (let ((x-el (aref x i))
446 (y-el (aref y i)))
447 (unless (or (eq x-el y-el)
448 (equalp x-el y-el))
449 (return nil)))))))
450 ((arrayp x)
451 (and (arrayp y)
452 (= (array-rank x) (array-rank y))
453 (dotimes (axis (array-rank x) t)
454 (unless (= (array-dimension x axis)
455 (array-dimension y axis))
456 (return nil)))
457 (dotimes (index (array-total-size x) t)
458 (let ((x-el (row-major-aref x index))
459 (y-el (row-major-aref y index)))
460 (unless (or (eq x-el y-el)
461 (equalp x-el y-el))
462 (return nil))))))
463 (t nil)))

  ViewVC Help
Powered by ViewVC 1.1.5