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

Contents of /src/code/pred.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.52 - (show annotations)
Tue May 2 04:44:05 2000 UTC (13 years, 11 months ago) by dtc
Branch: MAIN
Changes since 1.51: +5 -1 lines
Implement a specializing CONS class as per issue CONS-TYPE-SPECIFIER:ADD.
The new cons-type which maintains the specialised CAR and CDR types:

o Typep recurses, checking the respective car and cdr specialisations;
  two cons-types are type= if both their car and cdr types are type=.

o Subtypep recurses, applying subtypep to the respective car and
  cdr types. If both sub-tests return true then the cons test returns
  true with a win only when both sub-tests win. If either sub-test
  returns false then the cons tests returns false with a win if either
  of sub-tests win.

o Type-intersection is applied to the respective car and cdr types,
  and wins if both win.

o The type-union of two cons-types handles cases for which either the
  respective car or cdr types are type=, in which case type-union is
  applied to the other type. This avoid returning an overly general result.

o Ctype-of a cons object simply returns (cons * *); and does not attempt
  to recurse.

o The compiler recursively checks the specialised type of the cons-type,
  which allows it to also use this type information to derive the result
  type car and cdr. Inline code is currently only generated when
  natively compiling.
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.52 2000/05/02 04:44:05 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 (cons-type
290 (and (consp object)
291 (%%typep (car object) (cons-type-car-type type))
292 (%%typep (cdr object) (cons-type-cdr-type type))))
293 (unknown-type
294 ;; Parse it again to make sure it's really undefined.
295 (let ((reparse (specifier-type (unknown-type-specifier type))))
296 (if (typep reparse 'unknown-type)
297 (error "Unknown type specifier: ~S"
298 (unknown-type-specifier reparse))
299 (%%typep object reparse))))
300 (hairy-type
301 ;; Now the tricky stuff.
302 (let* ((hairy-spec (hairy-type-specifier type))
303 (symbol (if (consp hairy-spec) (car hairy-spec) hairy-spec)))
304 (ecase symbol
305 (and
306 (or (atom hairy-spec)
307 (dolist (spec (cdr hairy-spec) t)
308 (unless (%%typep object (specifier-type spec))
309 (return nil)))))
310 (not
311 (unless (and (listp hairy-spec) (= (length hairy-spec) 2))
312 (error "Invalid type specifier: ~S" hairy-spec))
313 (not (%%typep object (specifier-type (cadr hairy-spec)))))
314 (satisfies
315 (unless (and (listp hairy-spec) (= (length hairy-spec) 2))
316 (error "Invalid type specifier: ~S" hairy-spec))
317 (let ((fn (cadr hairy-spec)))
318 (if (funcall (typecase fn
319 (function fn)
320 (symbol (symbol-function fn))
321 (t
322 (coerce fn 'function)))
323 object)
324 t
325 nil))))))
326 (alien-type-type
327 (alien-internals:alien-typep object (alien-type-type-alien-type type)))
328 (function-type
329 (error "Function types are not a legal argument to TYPEP:~% ~S"
330 (type-specifier type)))))
331
332
333 ;;; CLASS-CELL-TYPEP -- Interface
334 ;;;
335 ;;; Do type test from a class cell, allowing forward reference and
336 ;;; redefinition.
337 ;;;
338 (defun class-cell-typep (obj-layout cell object)
339 (let ((class (class-cell-class cell)))
340 (unless class
341 (error "Class has not yet been defined: ~S" (class-cell-name cell)))
342 (class-typep obj-layout class object)))
343
344
345 ;;; CLASS-TYPEP -- Internal
346 ;;;
347 ;;; Test whether Obj-Layout is from an instance of Class.
348 ;;;
349 (defun class-typep (obj-layout class object)
350 (declare (optimize speed))
351 (when (layout-invalid obj-layout)
352 (if (and (typep (class-of object) 'standard-class) object)
353 (setq obj-layout (pcl::check-wrapper-validity object))
354 (error "TYPEP on obsolete object (was class ~S)."
355 (class-proper-name (layout-class obj-layout)))))
356 (let ((layout (class-layout class))
357 (obj-inherits (layout-inherits obj-layout)))
358 (when (layout-invalid layout)
359 (error "Class is currently invalid: ~S" class))
360 (or (eq obj-layout layout)
361 (dotimes (i (length obj-inherits) nil)
362 (when (eq (svref obj-inherits i) layout)
363 (return t))))))
364
365 (declaim (end-block))
366
367
368 ;;;; Equality predicates.
369
370 ;;; EQ -- public.
371 ;;;
372 ;;; Real simple, 'cause the compiler takes care of it.
373 ;;;
374
375 (defun eq (obj1 obj2)
376 "Return T if OBJ1 and OBJ2 are the same object, otherwise NIL."
377 (eq obj1 obj2))
378
379
380 ;;; EQUAL -- public.
381 ;;;
382 (defun equal (x y)
383 "Returns T if X and Y are EQL or if they are structured components
384 whose elements are EQUAL. Strings and bit-vectors are EQUAL if they
385 are the same length and have indentical components. Other arrays must be
386 EQ to be EQUAL."
387 (cond ((eql x y) t)
388 ((consp x)
389 (and (consp y)
390 (equal (car x) (car y))
391 (equal (cdr x) (cdr y))))
392 ((stringp x)
393 (and (stringp y) (string= x y)))
394 ((pathnamep x)
395 (and (pathnamep y) (pathname= x y)))
396 ((bit-vector-p x)
397 (and (bit-vector-p y)
398 (= (the fixnum (length x))
399 (the fixnum (length y)))
400 (do ((i 0 (1+ i))
401 (length (length x)))
402 ((= i length) t)
403 (declare (fixnum i))
404 (or (= (the fixnum (bit x i))
405 (the fixnum (bit y i)))
406 (return nil)))))
407 (t nil)))
408
409 ;;; EQUALP -- public.
410 ;;;
411 (defun equalp (x y)
412 "Just like EQUAL, but more liberal in several respects.
413 Numbers may be of different types, as long as the values are identical
414 after coercion. Characters may differ in alphabetic case. Vectors and
415 arrays must have identical dimensions and EQUALP elements, but may differ
416 in their type restriction."
417 (cond ((eq x y) t)
418 ((characterp x) (and (characterp y) (char-equal x y)))
419 ((numberp x) (and (numberp y) (= x y)))
420 ((consp x)
421 (and (consp y)
422 (equalp (car x) (car y))
423 (equalp (cdr x) (cdr y))))
424 ((pathnamep x)
425 (and (pathnamep y) (pathname= x y)))
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