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

Contents of /src/code/pred.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.58 - (hide 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 wlott 1.7 ;;; -*- Mode: Lisp; Package: LISP; Log: code.log -*-
2 ram 1.1 ;;;
3     ;;; **********************************************************************
4 ram 1.16 ;;; 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 toy 1.58 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/pred.lisp,v 1.58 2003/04/27 14:52:27 toy Exp $")
9 ram 1.16 ;;;
10 ram 1.1 ;;; **********************************************************************
11     ;;;
12 wlott 1.7 ;;; Predicate functions for CMU Common Lisp.
13 ram 1.1 ;;;
14 wlott 1.7 ;;; Written by William Lott.
15     ;;;
16    
17 ram 1.29 (in-package "KERNEL")
18     (export '(%instancep instance fixnump bignump bitp ratiop weak-pointer-p
19 ram 1.33 %typep class-cell-typep))
20 wlott 1.7
21     (in-package "SYSTEM")
22     (export '(system-area-pointer system-area-pointer-p))
23    
24 ram 1.33 (in-package "LISP")
25 wlott 1.7
26 ram 1.1 (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 pw 1.36 functionp compiled-function-p eq eql equal equalp not
30 wlott 1.26 type-of upgraded-array-element-type realp
31 ram 1.1 ;; Names of types...
32 dtc 1.48 array atom bignum bit bit-vector character
33 ram 1.1 compiled-function complex cons double-float
34     fixnum float function integer keyword list long-float nil
35 wlott 1.7 null number ratio rational real sequence short-float signed-byte
36 ram 1.1 simple-array simple-bit-vector simple-string simple-vector
37 dtc 1.45 single-float standard-char base-char string symbol t
38 ram 1.29 unsigned-byte vector satisfies))
39 ram 1.1
40    
41 wlott 1.7
42     ;;;; Primitive predicates. These must be supported by the compiler.
43 ram 1.1
44 wlott 1.7 (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 wlott 1.18 code-component-p
54 wlott 1.7 consp
55     compiled-function-p
56     complexp
57 dtc 1.49 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 wlott 1.7 double-float-p
63 wlott 1.25 fdefn-p
64 wlott 1.7 fixnump
65     floatp
66     functionp
67     integerp
68     listp
69 wlott 1.10 long-float-p
70 wlott 1.18 lra-p
71 wlott 1.7 not
72     null
73     numberp
74     rationalp
75     ratiop
76     realp
77 wlott 1.18 scavenger-hook-p
78 wlott 1.10 short-float-p
79 wlott 1.7 simple-array-p
80     simple-bit-vector-p
81     simple-string-p
82     simple-vector-p
83     single-float-p
84     stringp
85 ram 1.29 %instancep
86 wlott 1.7 symbolp
87     system-area-pointer-p
88     weak-pointer-p
89     vectorp
90 ram 1.20 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 dtc 1.49 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 ram 1.20 simple-array-single-float-p
102     simple-array-double-float-p
103 dtc 1.46 #+long-float simple-array-long-float-p
104 dtc 1.49 simple-array-complex-single-float-p
105     simple-array-complex-double-float-p
106     #+long-float simple-array-complex-long-float-p
107 wlott 1.7 )))
108 ram 1.1
109 wlott 1.7 (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 ram 1.1
123    
124 wlott 1.7 ;;;; 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 ram 1.29 ;;; than type-of. In particular, speed is more important than precision, and
129     ;;; it is not permitted to return member types.
130 wlott 1.7 ;;;
131 ram 1.1 (defun type-of (object)
132 wlott 1.7 "Return the type of OBJECT."
133 dtc 1.49 (if (typep object '(or function array complex))
134 ram 1.29 (type-specifier (ctype-of object))
135     (let* ((class (layout-class (layout-of object)))
136 gerd 1.56 (name (%class-name class)))
137 ram 1.30 (if (%instancep object)
138 ram 1.29 (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 wlott 1.8
147 ram 1.22
148     ;;;; UPGRADED-ARRAY-ELEMENT-TYPE -- public
149     ;;;
150 toy 1.55 (defun upgraded-array-element-type (spec &optional environment)
151 ram 1.22 "Return the element type that will actually be used to implement an array
152     with the specifier :ELEMENT-TYPE Spec."
153 toy 1.55 ;; Type expansion (TYPE-EXPAND) currently doesn't handle environments.
154     (declare (ignore environment))
155 ram 1.22 (type-specifier
156     (array-type-specialized-element-type
157     (specifier-type `(array ,spec)))))
158 wlott 1.7
159     ;;;; SUBTYPEP -- public.
160 ram 1.1 ;;;
161 wlott 1.7 ;;; Just parse the type specifiers and call csubtype.
162     ;;;
163 toy 1.55 (defun subtypep (type1 type2 &optional environment)
164 wlott 1.7 "Return two values indicating the relationship between type1 and type2:
165 ram 1.33 T and T: type1 definitely is a subtype of type2.
166     NIL and T: type1 definitely is not a subtype of type2.
167 wlott 1.7 NIL and NIL: who knows?"
168 toy 1.55 (declare (ignore environment))
169 wlott 1.7 (csubtypep (specifier-type type1) (specifier-type type2)))
170 ram 1.1
171    
172 ram 1.33 ;;;; TYPEP:
173    
174     (declaim (start-block typep %typep class-cell-typep))
175    
176     ;;; TYPEP -- public.
177 wlott 1.7 ;;;
178     ;;; Just call %typep
179     ;;;
180 toy 1.55 (defun typep (object type &optional environment)
181 wlott 1.7 "Return T iff OBJECT is of type TYPE."
182 toy 1.55 (declare (ignore environment))
183 wlott 1.7 (%typep object type))
184 ram 1.1
185 wlott 1.28
186 wlott 1.7 ;;; %TYPEP -- internal.
187 ram 1.1 ;;;
188 wlott 1.7 ;;; 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 wlott 1.13 (%%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 ram 1.29 (ecase (named-type-name type)
202     ((* t) t)
203     ((nil) nil)))
204 wlott 1.13 (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 ram 1.35 (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 wlott 1.13 ((nil) (floatp num))))
217 wlott 1.27 ((nil) t)))
218 wlott 1.13 (flet ((bound-test (val)
219 ram 1.33 (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 wlott 1.13 (ecase (numeric-type-complexp type)
228     ((nil) t)
229     (:complex
230     (and (complexp object)
231 wlott 1.26 (bound-test (realpart object))
232     (bound-test (imagpart object))))
233 wlott 1.13 (: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 gerd 1.57 ((* :maybe) t))
242 wlott 1.13 (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 gerd 1.57 (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 wlott 1.13 (or (eq (array-type-element-type type) *wild-type*)
257 gerd 1.57 (values (type= (array-type-specialized-element-type type)
258     (specifier-type (array-element-type
259     object)))))))
260 wlott 1.13 (member-type
261     (if (member object (member-type-members type)) t))
262 gerd 1.56 (kernel::class
263 pw 1.37 (class-typep (layout-of object) type object))
264 wlott 1.13 (union-type
265 gerd 1.57 (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 dtc 1.52 (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 wlott 1.13 (unknown-type
275 ram 1.29 ;; 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 gerd 1.57 (negation-type
282     (not (%%typep object (negation-type-type type))))
283 wlott 1.13 (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 gerd 1.57 (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 wlott 1.13 (not
296     (unless (and (listp hairy-spec) (= (length hairy-spec) 2))
297     (error "Invalid type specifier: ~S" hairy-spec))
298 wlott 1.15 (not (%%typep object (specifier-type (cadr hairy-spec)))))
299 wlott 1.13 (satisfies
300     (unless (and (listp hairy-spec) (= (length hairy-spec) 2))
301     (error "Invalid type specifier: ~S" hairy-spec))
302 wlott 1.17 (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 wlott 1.23 (alien-type-type
312     (alien-internals:alien-typep object (alien-type-type-alien-type type)))
313 wlott 1.13 (function-type
314     (error "Function types are not a legal argument to TYPEP:~% ~S"
315     (type-specifier type)))))
316    
317 ram 1.1
318 ram 1.33 ;;; CLASS-CELL-TYPEP -- Interface
319     ;;;
320     ;;; Do type test from a class cell, allowing forward reference and
321     ;;; redefinition.
322     ;;;
323 dtc 1.47 (defun class-cell-typep (obj-layout cell object)
324 ram 1.33 (let ((class (class-cell-class cell)))
325     (unless class
326     (error "Class has not yet been defined: ~S" (class-cell-name cell)))
327 pw 1.37 (class-typep obj-layout class object)))
328 ram 1.1
329 ram 1.33
330 ram 1.29 ;;; CLASS-TYPEP -- Internal
331 ram 1.1 ;;;
332 ram 1.29 ;;; Test whether Obj-Layout is from an instance of Class.
333 ram 1.1 ;;;
334 pw 1.37 (defun class-typep (obj-layout class object)
335 ram 1.1 (declare (optimize speed))
336 ram 1.29 (when (layout-invalid obj-layout)
337 gerd 1.56 (if (and (typep (kernel::class-of object) 'kernel::standard-class) object)
338 pw 1.37 (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 gerd 1.56 (let ((layout (%class-layout class))
342 ram 1.33 (obj-inherits (layout-inherits obj-layout)))
343 ram 1.29 (when (layout-invalid layout)
344     (error "Class is currently invalid: ~S" class))
345 ram 1.33 (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 ram 1.1
352    
353 wlott 1.7 ;;;; Equality predicates.
354 ram 1.1
355 wlott 1.7 ;;; EQ -- public.
356     ;;;
357     ;;; Real simple, 'cause the compiler takes care of it.
358     ;;;
359 ram 1.1
360 wlott 1.7 (defun eq (obj1 obj2)
361     "Return T if OBJ1 and OBJ2 are the same object, otherwise NIL."
362     (eq obj1 obj2))
363 ram 1.1
364    
365 wlott 1.7 ;;; EQUAL -- public.
366     ;;;
367 ram 1.1 (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 ram 1.20 (and (pathnamep y) (pathname= x y)))
381 ram 1.1 ((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 wlott 1.7 ;;; EQUALP -- public.
395     ;;;
396 ram 1.1 (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 wlott 1.12 (cond ((eq x y) t)
403 dtc 1.51 ((characterp x) (and (characterp y) (char-equal x y)))
404 ram 1.1 ((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 ram 1.20 ((pathnamep x)
410     (and (pathnamep y) (pathname= x y)))
411 dtc 1.53 ((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 ram 1.29 ((%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 ram 1.1 ((vectorp x)
441     (let ((length (length x)))
442     (and (vectorp y)
443 wlott 1.7 (= length (length y))
444 ram 1.1 (dotimes (i length t)
445     (let ((x-el (aref x i))
446     (y-el (aref y i)))
447 wlott 1.12 (unless (or (eq x-el y-el)
448 ram 1.1 (equalp x-el y-el))
449     (return nil)))))))
450     ((arrayp x)
451 wlott 1.7 (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 wlott 1.12 (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 ram 1.1 (t nil)))

  ViewVC Help
Powered by ViewVC 1.1.5