1 ;;;; This file contains the definition of non-CLASS types (e.g.
2 ;;;; subtypes of interesting BUILT-IN-CLASSes) and the interfaces to
3 ;;;; the type system. Common Lisp type specifiers are parsed into a
4 ;;;; somewhat canonical internal type representation that supports
5 ;;;; type union, intersection, etc. (Except that ALIEN types have
8 ;;;; This software is part of the SBCL system. See the README file for
11 ;;;; This software is derived from the CMU CL system, which was
12 ;;;; written at Carnegie Mellon University and released into the
13 ;;;; public domain. The software is in the public domain and is
14 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
15 ;;;; files for more information.
17 (in-package "SB!KERNEL")
19 (/show0 "late-type.lisp 19")
21 (!begin-collecting-cold-init-forms)
23 ;;; ### Remaining incorrectnesses:
25 ;;; TYPE-UNION (and the OR type) doesn't properly canonicalize an
26 ;;; exhaustive partition or coalesce contiguous ranges of numeric
29 ;;; There are all sorts of nasty problems with open bounds on FLOAT
30 ;;; types (and probably FLOAT types in general.)
32 ;;; RATIO and BIGNUM are not recognized as numeric types.
34 ;;; FIXME: This really should go away. Alas, it doesn't seem to be so
35 ;;; simple to make it go away.. (See bug 123 in BUGS file.)
36 (defvar *use-implementation-types* t ; actually initialized in cold init
38 "*USE-IMPLEMENTATION-TYPES* is a semi-public flag which determines how
39 restrictive we are in determining type membership. If two types are the
40 same in the implementation, then we will consider them them the same when
41 this switch is on. When it is off, we try to be as restrictive as the
42 language allows, allowing us to detect more errors. Currently, this only
43 affects array types.")
44 (!cold-init-forms (setq *use-implementation-types* t))
46 ;;; These functions are used as method for types which need a complex
47 ;;; subtypep method to handle some superclasses, but cover a subtree
48 ;;; of the type graph (i.e. there is no simple way for any other type
49 ;;; class to be a subtype.) There are always still complex ways,
50 ;;; namely UNION and MEMBER types, so we must give TYPE1's method a
51 ;;; chance to run, instead of immediately returning NIL, T.
52 (defun delegate-complex-subtypep-arg2 (type1 type2)
54 (type-class-complex-subtypep-arg1
55 (type-class-info type1))))
57 (funcall subtypep-arg1 type1 type2)
59 (defun delegate-complex-intersection2 (type1 type2)
60 (let ((method (type-class-complex-intersection2 (type-class-info type1))))
61 (if (and method (not (eq method #'delegate-complex-intersection2)))
62 (funcall method type2 type1)
63 (hierarchical-intersection2 type1 type2))))
65 ;;; This is used by !DEFINE-SUPERCLASSES to define the SUBTYPE-ARG1
66 ;;; method. INFO is a list of conses
67 ;;; (SUPERCLASS-CLASS . {GUARD-TYPE-SPECIFIER | NIL}).
68 ;;; This will never be called with a hairy type as TYPE2, since the
69 ;;; hairy type TYPE2 method gets first crack.
70 (defun !has-superclasses-complex-subtypep-arg1 (type1 type2 info)
72 (and (sb!xc:typep type2 'sb!xc:class)
74 (when (or (not (cdr x))
75 (csubtypep type1 (specifier-type (cdr x))))
77 (or (eq type2 (car x))
78 (let ((inherits (layout-inherits (class-layout (car x)))))
79 (dotimes (i (length inherits) nil)
80 (when (eq type2 (layout-class (svref inherits i)))
84 ;;; This function takes a list of specs, each of the form
85 ;;; (SUPERCLASS-NAME &OPTIONAL GUARD).
86 ;;; Consider one spec (with no guard): any instance of the named
87 ;;; TYPE-CLASS is also a subtype of the named superclass and of any of
88 ;;; its superclasses. If there are multiple specs, then some will have
89 ;;; guards. We choose the first spec whose guard is a supertype of
90 ;;; TYPE1 and use its superclass. In effect, a sequence of guards
93 ;;; G0,(and G1 (not G0)), (and G2 (not (or G0 G1))).
95 ;;; WHEN controls when the forms are executed.
96 (defmacro !define-superclasses (type-class-name specs when)
97 (let ((type-class (gensym "TYPE-CLASS-"))
98 (info (gensym "INFO")))
100 (let ((,type-class (type-class-or-lose ',type-class-name))
101 (,info (mapcar (lambda (spec)
103 (super &optional guard)
105 (cons (sb!xc:find-class super) guard)))
107 (setf (type-class-complex-subtypep-arg1 ,type-class)
108 (lambda (type1 type2)
109 (!has-superclasses-complex-subtypep-arg1 type1 type2 ,info)))
110 (setf (type-class-complex-subtypep-arg2 ,type-class)
111 #'delegate-complex-subtypep-arg2)
112 (setf (type-class-complex-intersection2 ,type-class)
113 #'delegate-complex-intersection2)))))
115 ;;;; FUNCTION and VALUES types
117 ;;;; Pretty much all of the general type operations are illegal on
118 ;;;; VALUES types, since we can't discriminate using them, do
119 ;;;; SUBTYPEP, etc. FUNCTION types are acceptable to the normal type
120 ;;;; operations, but are generally considered to be equivalent to
121 ;;;; FUNCTION. These really aren't true types in any type theoretic
122 ;;;; sense, but we still parse them into CTYPE structures for two
125 ;;;; -- Parsing and unparsing work the same way, and indeed we can't
126 ;;;; tell whether a type is a function or values type without
128 ;;;; -- Many of the places that can be annotated with real types can
129 ;;;; also be annotated with function or values types.
131 ;;; the description of a &KEY argument
132 (defstruct (key-info #-sb-xc-host (:pure t)
134 ;; the key (not necessarily a keyword in ANSI)
135 (name (required-argument) :type symbol)
136 ;; the type of the argument value
137 (type (required-argument) :type ctype))
139 (!define-type-method (values :simple-subtypep :complex-subtypep-arg1)
141 (declare (ignore type2))
142 ;; FIXME: should be TYPE-ERROR, here and in next method
143 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type1)))
145 (!define-type-method (values :complex-subtypep-arg2)
147 (declare (ignore type1))
148 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type2)))
150 (!define-type-method (values :unparse) (type)
151 (cons 'values (unparse-args-types type)))
153 ;;; Return true if LIST1 and LIST2 have the same elements in the same
154 ;;; positions according to TYPE=. We return NIL, NIL if there is an
155 ;;; uncertain comparison.
156 (defun type=-list (list1 list2)
157 (declare (list list1 list2))
158 (do ((types1 list1 (cdr types1))
159 (types2 list2 (cdr types2)))
160 ((or (null types1) (null types2))
161 (if (or types1 types2)
164 (multiple-value-bind (val win)
165 (type= (first types1) (first types2))
167 (return (values nil nil)))
169 (return (values nil t))))))
171 (!define-type-method (values :simple-=) (type1 type2)
172 (let ((rest1 (args-type-rest type1))
173 (rest2 (args-type-rest type2)))
174 (cond ((or (args-type-keyp type1) (args-type-keyp type2)
175 (args-type-allowp type1) (args-type-allowp type2))
177 ((and rest1 rest2 (type/= rest1 rest2))
182 (multiple-value-bind (req-val req-win)
183 (type=-list (values-type-required type1)
184 (values-type-required type2))
185 (multiple-value-bind (opt-val opt-win)
186 (type=-list (values-type-optional type1)
187 (values-type-optional type2))
188 (values (and req-val opt-val) (and req-win opt-win))))))))
190 (!define-type-class function)
192 ;;; a flag that we can bind to cause complex function types to be
193 ;;; unparsed as FUNCTION. This is useful when we want a type that we
194 ;;; can pass to TYPEP.
195 (defvar *unparse-function-type-simplify*)
196 (!cold-init-forms (setq *unparse-function-type-simplify* nil))
198 (!define-type-method (function :unparse) (type)
199 (if *unparse-function-type-simplify*
202 (if (function-type-wild-args type)
204 (unparse-args-types type))
206 (function-type-returns type)))))
208 ;;; Since all function types are equivalent to FUNCTION, they are all
209 ;;; subtypes of each other.
210 (!define-type-method (function :simple-subtypep) (type1 type2)
211 (declare (ignore type1 type2))
214 (!define-superclasses function ((function)) !cold-init-forms)
216 ;;; The union or intersection of two FUNCTION types is FUNCTION.
217 (!define-type-method (function :simple-union2) (type1 type2)
218 (declare (ignore type1 type2))
219 (specifier-type 'function))
220 (!define-type-method (function :simple-intersection2) (type1 type2)
221 (declare (ignore type1 type2))
222 (specifier-type 'function))
224 ;;; ### Not very real, but good enough for redefining transforms
225 ;;; according to type:
226 (!define-type-method (function :simple-=) (type1 type2)
227 (values (equalp type1 type2) t))
229 (!define-type-class constant :inherits values)
231 (!define-type-method (constant :unparse) (type)
232 `(constant-argument ,(type-specifier (constant-type-type type))))
234 (!define-type-method (constant :simple-=) (type1 type2)
235 (type= (constant-type-type type1) (constant-type-type type2)))
237 (!def-type-translator constant-argument (type)
238 (make-constant-type :type (specifier-type type)))
240 ;;; Given a LAMBDA-LIST-like values type specification and an ARGS-TYPE
241 ;;; structure, fill in the slots in the structure accordingly. This is
242 ;;; used for both FUNCTION and VALUES types.
243 (declaim (ftype (function (list args-type) (values)) parse-args-types))
244 (defun parse-args-types (lambda-list result)
245 (multiple-value-bind (required optional restp rest keyp keys allowp aux)
246 (parse-lambda-list lambda-list)
248 (error "&AUX in a FUNCTION or VALUES type: ~S." lambda-list))
249 (setf (args-type-required result) (mapcar #'specifier-type required))
250 (setf (args-type-optional result) (mapcar #'specifier-type optional))
251 (setf (args-type-rest result) (if restp (specifier-type rest) nil))
252 (setf (args-type-keyp result) keyp)
253 (collect ((key-info))
255 (unless (proper-list-of-length-p key 2)
256 (error "Keyword type description is not a two-list: ~S." key))
257 (let ((kwd (first key)))
258 (when (find kwd (key-info) :key #'key-info-name)
259 (error "~@<repeated keyword ~S in lambda list: ~2I~_~S~:>"
261 (key-info (make-key-info :name kwd
262 :type (specifier-type (second key))))))
263 (setf (args-type-keywords result) (key-info)))
264 (setf (args-type-allowp result) allowp)
267 ;;; Return the lambda-list-like type specification corresponding
269 (declaim (ftype (function (args-type) list) unparse-args-types))
270 (defun unparse-args-types (type)
273 (dolist (arg (args-type-required type))
274 (result (type-specifier arg)))
276 (when (args-type-optional type)
278 (dolist (arg (args-type-optional type))
279 (result (type-specifier arg))))
281 (when (args-type-rest type)
283 (result (type-specifier (args-type-rest type))))
285 (when (args-type-keyp type)
287 (dolist (key (args-type-keywords type))
288 (result (list (key-info-name key)
289 (type-specifier (key-info-type key))))))
291 (when (args-type-allowp type)
292 (result '&allow-other-keys))
296 (!def-type-translator function (&optional (args '*) (result '*))
297 (let ((res (make-function-type
298 :returns (values-specifier-type result))))
300 (setf (function-type-wild-args res) t)
301 (parse-args-types args res))
304 (!def-type-translator values (&rest values)
305 (let ((res (make-values-type)))
306 (parse-args-types values res)
309 ;;;; VALUES types interfaces
311 ;;;; We provide a few special operations that can be meaningfully used
312 ;;;; on VALUES types (as well as on any other type).
314 ;;; Return the type of the first value indicated by TYPE. This is used
315 ;;; by people who don't want to have to deal with VALUES types.
316 #!-sb-fluid (declaim (freeze-type values-type))
317 ; (inline single-value-type))
318 (defun single-value-type (type)
319 (declare (type ctype type))
320 (cond ((values-type-p type)
321 (or (car (args-type-required type))
322 (if (args-type-optional type)
323 (type-union (car (args-type-optional type))
324 (specifier-type 'null)))
325 (args-type-rest type)
326 (specifier-type 'null)))
327 ((eq type *wild-type*)
332 ;;; Return the minimum number of arguments that a function can be
333 ;;; called with, and the maximum number or NIL. If not a function
334 ;;; type, return NIL, NIL.
335 (defun function-type-nargs (type)
336 (declare (type ctype type))
337 (if (function-type-p type)
338 (let ((fixed (length (args-type-required type))))
339 (if (or (args-type-rest type)
340 (args-type-keyp type)
341 (args-type-allowp type))
343 (values fixed (+ fixed (length (args-type-optional type))))))
346 ;;; Determine whether TYPE corresponds to a definite number of values.
347 ;;; The first value is a list of the types for each value, and the
348 ;;; second value is the number of values. If the number of values is
349 ;;; not fixed, then return NIL and :UNKNOWN.
350 (defun values-types (type)
351 (declare (type ctype type))
352 (cond ((eq type *wild-type*)
353 (values nil :unknown))
354 ((not (values-type-p type))
355 (values (list type) 1))
356 ((or (args-type-optional type)
357 (args-type-rest type)
358 (args-type-keyp type)
359 (args-type-allowp type))
360 (values nil :unknown))
362 (let ((req (args-type-required type)))
363 (values (mapcar #'single-value-type req) (length req))))))
365 ;;; Return two values:
366 ;;; 1. A list of all the positional (fixed and optional) types.
367 ;;; 2. The &REST type (if any). If keywords allowed, *UNIVERSAL-TYPE*.
368 ;;; If no keywords or &REST, then the DEFAULT-TYPE.
369 (defun values-type-types (type &optional (default-type *empty-type*))
370 (declare (type values-type type))
371 (values (append (args-type-required type)
372 (args-type-optional type))
373 (cond ((args-type-keyp type) *universal-type*)
374 ((args-type-rest type))
378 ;;; Return a list of OPERATION applied to the types in TYPES1 and
379 ;;; TYPES2, padding with REST2 as needed. TYPES1 must not be shorter
380 ;;; than TYPES2. The second value is T if OPERATION always returned a
381 ;;; true second value.
382 (defun fixed-values-op (types1 types2 rest2 operation)
383 (declare (list types1 types2) (type ctype rest2) (type function operation))
385 (values (mapcar #'(lambda (t1 t2)
386 (multiple-value-bind (res win)
387 (funcall operation t1 t2)
393 (make-list (- (length types1) (length types2))
394 :initial-element rest2)))
397 ;;; If Type isn't a values type, then make it into one:
398 ;;; <type> ==> (values type &rest t)
399 (defun coerce-to-values (type)
400 (declare (type ctype type))
401 (if (values-type-p type)
403 (make-values-type :required (list type) :rest *universal-type*)))
405 ;;; Do the specified OPERATION on TYPE1 and TYPE2, which may be any
406 ;;; type, including VALUES types. With VALUES types such as:
409 ;;; we compute the more useful result
410 ;;; (VALUES (<operation> a0 b0) (<operation> a1 b1))
411 ;;; rather than the precise result
412 ;;; (<operation> (values a0 a1) (values b0 b1))
413 ;;; This has the virtue of always keeping the VALUES type specifier
414 ;;; outermost, and retains all of the information that is really
415 ;;; useful for static type analysis. We want to know what is always
416 ;;; true of each value independently. It is worthless to know that if
417 ;;; the first value is B0 then the second will be B1.
419 ;;; If the VALUES count signatures differ, then we produce a result with
420 ;;; the required VALUE count chosen by NREQ when applied to the number
421 ;;; of required values in TYPE1 and TYPE2. Any &KEY values become
422 ;;; &REST T (anyone who uses keyword values deserves to lose.)
424 ;;; The second value is true if the result is definitely empty or if
425 ;;; OPERATION returned true as its second value each time we called
426 ;;; it. Since we approximate the intersection of VALUES types, the
427 ;;; second value being true doesn't mean the result is exact.
428 (defun args-type-op (type1 type2 operation nreq default-type)
429 (declare (type ctype type1 type2 default-type)
430 (type function operation nreq))
431 (if (or (values-type-p type1) (values-type-p type2))
432 (let ((type1 (coerce-to-values type1))
433 (type2 (coerce-to-values type2)))
434 (multiple-value-bind (types1 rest1)
435 (values-type-types type1 default-type)
436 (multiple-value-bind (types2 rest2)
437 (values-type-types type2 default-type)
438 (multiple-value-bind (rest rest-exact)
439 (funcall operation rest1 rest2)
440 (multiple-value-bind (res res-exact)
441 (if (< (length types1) (length types2))
442 (fixed-values-op types2 types1 rest1 operation)
443 (fixed-values-op types1 types2 rest2 operation))
444 (let* ((req (funcall nreq
445 (length (args-type-required type1))
446 (length (args-type-required type2))))
447 (required (subseq res 0 req))
448 (opt (subseq res req))
449 (opt-last (position rest opt :test-not #'type=
451 (if (find *empty-type* required :test #'type=)
452 (values *empty-type* t)
453 (values (make-values-type
455 :optional (if opt-last
456 (subseq opt 0 (1+ opt-last))
458 :rest (if (eq rest default-type) nil rest))
459 (and rest-exact res-exact)))))))))
460 (funcall operation type1 type2)))
462 ;;; Do a union or intersection operation on types that might be values
463 ;;; types. The result is optimized for utility rather than exactness,
464 ;;; but it is guaranteed that it will be no smaller (more restrictive)
465 ;;; than the precise result.
467 ;;; The return convention seems to be analogous to
468 ;;; TYPES-EQUAL-OR-INTERSECT. -- WHN 19990910.
469 (defun-cached (values-type-union :hash-function type-cache-hash
472 :init-wrapper !cold-init-forms)
473 ((type1 eq) (type2 eq))
474 (declare (type ctype type1 type2))
475 (cond ((or (eq type1 *wild-type*) (eq type2 *wild-type*)) *wild-type*)
476 ((eq type1 *empty-type*) type2)
477 ((eq type2 *empty-type*) type1)
479 (values (args-type-op type1 type2 #'type-union #'min *empty-type*)))))
480 (defun-cached (values-type-intersection :hash-function type-cache-hash
483 :default (values nil :empty)
484 :init-wrapper !cold-init-forms)
485 ((type1 eq) (type2 eq))
486 (declare (type ctype type1 type2))
487 (cond ((eq type1 *wild-type*) (values type2 t))
488 ((eq type2 *wild-type*) (values type1 t))
490 (args-type-op type1 type2
493 (specifier-type 'null)))))
495 ;;; This is like TYPES-EQUAL-OR-INTERSECT, except that it sort of
496 ;;; works on VALUES types. Note that due to the semantics of
497 ;;; VALUES-TYPE-INTERSECTION, this might return (VALUES T T) when
498 ;;; there isn't really any intersection.
499 (defun values-types-equal-or-intersect (type1 type2)
500 (cond ((or (eq type1 *empty-type*) (eq type2 *empty-type*))
502 ((or (values-type-p type1) (values-type-p type2))
503 (multiple-value-bind (res win) (values-type-intersection type1 type2)
504 (values (not (eq res *empty-type*))
507 (types-equal-or-intersect type1 type2))))
509 ;;; a SUBTYPEP-like operation that can be used on any types, including
511 (defun-cached (values-subtypep :hash-function type-cache-hash
514 :default (values nil :empty)
515 :init-wrapper !cold-init-forms)
516 ((type1 eq) (type2 eq))
517 (declare (type ctype type1 type2))
518 (cond ((eq type2 *wild-type*) (values t t))
519 ((eq type1 *wild-type*)
520 (values (eq type2 *universal-type*) t))
521 ((not (values-types-equal-or-intersect type1 type2))
524 (if (or (values-type-p type1) (values-type-p type2))
525 (let ((type1 (coerce-to-values type1))
526 (type2 (coerce-to-values type2)))
527 (multiple-value-bind (types1 rest1) (values-type-types type1)
528 (multiple-value-bind (types2 rest2) (values-type-types type2)
529 (cond ((< (length (values-type-required type1))
530 (length (values-type-required type2)))
532 ((< (length types1) (length types2))
534 ((or (values-type-keyp type1)
535 (values-type-keyp type2))
538 (do ((t1 types1 (rest t1))
539 (t2 types2 (rest t2)))
541 (csubtypep rest1 rest2))
542 (multiple-value-bind (res win-p)
543 (csubtypep (first t1) (first t2))
545 (return (values nil nil)))
547 (return (values nil t))))))))))
548 (csubtypep type1 type2)))))
550 ;;;; type method interfaces
552 ;;; like SUBTYPEP, only works on CTYPE structures
553 (defun-cached (csubtypep :hash-function type-cache-hash
556 :default (values nil :empty)
557 :init-wrapper !cold-init-forms)
558 ((type1 eq) (type2 eq))
559 (declare (type ctype type1 type2))
560 (cond ((or (eq type1 type2)
561 (eq type1 *empty-type*)
562 (eq type2 *wild-type*))
564 ((or (eq type1 *wild-type*)
565 (eq type2 *empty-type*))
568 (!invoke-type-method :simple-subtypep :complex-subtypep-arg2
570 :complex-arg1 :complex-subtypep-arg1))))
572 ;;; Just parse the type specifiers and call CSUBTYPE.
573 (defun sb!xc:subtypep (type1 type2)
575 "Return two values indicating the relationship between type1 and type2.
576 If values are T and T, type1 definitely is a subtype of type2.
577 If values are NIL and T, type1 definitely is not a subtype of type2.
578 If values are NIL and NIL, it couldn't be determined."
579 (csubtypep (specifier-type type1) (specifier-type type2)))
581 ;;; If two types are definitely equivalent, return true. The second
582 ;;; value indicates whether the first value is definitely correct.
583 ;;; This should only fail in the presence of HAIRY types.
584 (defun-cached (type= :hash-function type-cache-hash
587 :default (values nil :empty)
588 :init-wrapper !cold-init-forms)
589 ((type1 eq) (type2 eq))
590 (declare (type ctype type1 type2))
593 (!invoke-type-method :simple-= :complex-= type1 type2)))
595 ;;; Not exactly the negation of TYPE=, since when the relationship is
596 ;;; uncertain, we still return NIL, NIL. This is useful in cases where
597 ;;; the conservative assumption is =.
598 (defun type/= (type1 type2)
599 (declare (type ctype type1 type2))
600 (multiple-value-bind (res win) (type= type1 type2)
605 ;;; the type method dispatch case of TYPE-UNION2
606 (defun %type-union2 (type1 type2)
607 ;; As in %TYPE-INTERSECTION2, it seems to be a good idea to give
608 ;; both argument orders a chance at COMPLEX-INTERSECTION2. Unlike
609 ;; %TYPE-INTERSECTION2, though, I don't have a specific case which
610 ;; demonstrates this is actually necessary. Also unlike
611 ;; %TYPE-INTERSECTION2, there seems to be no need to distinguish
612 ;; between not finding a method and having a method return NIL.
614 (!invoke-type-method :simple-union2 :complex-union2
617 (declare (inline 1way))
618 (or (1way type1 type2)
619 (1way type2 type1))))
621 ;;; Find a type which includes both types. Any inexactness is
622 ;;; represented by the fuzzy element types; we return a single value
623 ;;; that is precise to the best of our knowledge. This result is
624 ;;; simplified into the canonical form, thus is not a UNION-TYPE
625 ;;; unless we find no other way to represent the result.
626 (defun-cached (type-union2 :hash-function type-cache-hash
628 :init-wrapper !cold-init-forms)
629 ((type1 eq) (type2 eq))
630 ;; KLUDGE: This was generated from TYPE-INTERSECTION2 by Ye Olde Cut And
631 ;; Paste technique of programming. If it stays around (as opposed to
632 ;; e.g. fading away in favor of some CLOS solution) the shared logic
633 ;; should probably become shared code. -- WHN 2001-03-16
634 (declare (type ctype type1 type2))
635 (cond ((eq type1 type2)
637 ((or (union-type-p type1)
638 (union-type-p type2))
639 ;; Unions of UNION-TYPE should have the UNION-TYPE-TYPES
640 ;; values broken out and united separately. The full TYPE-UNION
641 ;; function knows how to do this, so let it handle it.
642 (type-union type1 type2))
644 ;; the ordinary case: we dispatch to type methods
645 (%type-union2 type1 type2))))
647 ;;; the type method dispatch case of TYPE-INTERSECTION2
648 (defun %type-intersection2 (type1 type2)
649 ;; We want to give both argument orders a chance at
650 ;; COMPLEX-INTERSECTION2. Without that, the old CMU CL type
651 ;; methods could give noncommutative results, e.g.
652 ;; (TYPE-INTERSECTION2 *EMPTY-TYPE* SOME-HAIRY-TYPE)
654 ;; (TYPE-INTERSECTION2 SOME-HAIRY-TYPE *EMPTY-TYPE*)
655 ;; => #<NAMED-TYPE NIL>, T
656 ;; We also need to distinguish between the case where we found a
657 ;; type method, and it returned NIL, and the case where we fell
658 ;; through without finding any type method. An example of the first
659 ;; case is the intersection of a HAIRY-TYPE with some ordinary type.
660 ;; An example of the second case is the intersection of two
661 ;; completely-unrelated types, e.g. CONS and NUMBER, or SYMBOL and
664 ;; (Why yes, CLOS probably *would* be nicer..)
666 (!invoke-type-method :simple-intersection2 :complex-intersection2
668 :default :no-type-method-found)))
669 (declare (inline 1way))
670 (let ((xy (1way type1 type2)))
671 (or (and (not (eql xy :no-type-method-found)) xy)
672 (let ((yx (1way type2 type1)))
673 (or (and (not (eql yx :no-type-method-found)) yx)
674 (cond ((and (eql xy :no-type-method-found)
675 (eql yx :no-type-method-found))
678 (aver (and (not xy) (not yx))) ; else handled above
681 (defun-cached (type-intersection2 :hash-function type-cache-hash
685 :init-wrapper !cold-init-forms)
686 ((type1 eq) (type2 eq))
687 (declare (type ctype type1 type2))
688 (cond ((eq type1 type2)
690 ((or (intersection-type-p type1)
691 (intersection-type-p type2))
692 ;; Intersections of INTERSECTION-TYPE should have the
693 ;; INTERSECTION-TYPE-TYPES values broken out and intersected
694 ;; separately. The full TYPE-INTERSECTION function knows how
695 ;; to do that, so let it handle it.
696 (type-intersection type1 type2))
698 ;; the ordinary case: we dispatch to type methods
699 (%type-intersection2 type1 type2))))
701 ;;; Return as restrictive and simple a type as we can discover that is
702 ;;; no more restrictive than the intersection of TYPE1 and TYPE2. At
703 ;;; worst, we arbitrarily return one of the arguments as the first
704 ;;; value (trying not to return a hairy type).
705 (defun type-approx-intersection2 (type1 type2)
706 (cond ((type-intersection2 type1 type2))
707 ((hairy-type-p type1) type2)
710 ;;; a test useful for checking whether a derived type matches a
713 ;;; The first value is true unless the types don't intersect and
714 ;;; aren't equal. The second value is true if the first value is
715 ;;; definitely correct. NIL is considered to intersect with any type.
716 ;;; If T is a subtype of either type, then we also return T, T. This
717 ;;; way we recognize that hairy types might intersect with T.
718 (defun types-equal-or-intersect (type1 type2)
719 (declare (type ctype type1 type2))
720 (if (or (eq type1 *empty-type*) (eq type2 *empty-type*))
722 (let ((intersection2 (type-intersection2 type1 type2)))
723 (cond ((not intersection2)
724 (if (or (csubtypep *universal-type* type1)
725 (csubtypep *universal-type* type2))
728 ((eq intersection2 *empty-type*) (values nil t))
731 ;;; Return a Common Lisp type specifier corresponding to the TYPE
733 (defun type-specifier (type)
734 (declare (type ctype type))
735 (funcall (type-class-unparse (type-class-info type)) type))
737 ;;; (VALUES-SPECIFIER-TYPE and SPECIFIER-TYPE moved from here to
738 ;;; early-type.lisp by WHN ca. 19990201.)
740 ;;; Take a list of type specifiers, computing the translation of each
741 ;;; specifier and defining it as a builtin type.
742 (declaim (ftype (function (list) (values)) precompute-types))
743 (defun precompute-types (specs)
745 (let ((res (specifier-type spec)))
746 (unless (unknown-type-p res)
747 (setf (info :type :builtin spec) res)
748 (setf (info :type :kind spec) :primitive))))
751 ;;;; general TYPE-UNION and TYPE-INTERSECTION operations
753 ;;;; These are fully general operations on CTYPEs: they'll always
754 ;;;; return a CTYPE representing the result.
756 ;;; shared logic for unions and intersections: Stuff TYPE into the
757 ;;; vector TYPES, finding pairs of types which can be simplified by
758 ;;; SIMPLIFY2 (TYPE-UNION2 or TYPE-INTERSECTION2) and replacing them
759 ;;; by their simplified forms.
760 (defun accumulate1-compound-type (type types %compound-type-p simplify2)
761 (declare (type ctype type))
762 (declare (type (vector ctype) types))
763 (declare (type function simplify2))
764 ;; Any input object satisfying %COMPOUND-TYPE-P should've been
765 ;; broken into components before it reached us.
766 (aver (not (funcall %compound-type-p type)))
767 (dotimes (i (length types) (vector-push-extend type types))
768 (let ((simplified2 (funcall simplify2 type (aref types i))))
770 ;; Discard the old (AREF TYPES I).
771 (setf (aref types i) (vector-pop types))
772 ;; Merge the new SIMPLIFIED2 into TYPES, by tail recursing.
773 ;; (Note that the tail recursion is indirect: we go through
774 ;; ACCUMULATE, not ACCUMULATE1, so that if SIMPLIFIED2 is
775 ;; handled properly if it satisfies %COMPOUND-TYPE-P.)
776 (return (accumulate-compound-type simplified2
783 ;;; shared logic for unions and intersections: Use
784 ;;; ACCUMULATE1-COMPOUND-TYPE to merge TYPE into TYPES, either
785 ;;; all in one step or, if %COMPOUND-TYPE-P is satisfied,
786 ;;; component by component.
787 (defun accumulate-compound-type (type types %compound-type-p simplify2)
788 (declare (type function %compound-type-p simplify2))
789 (flet ((accumulate1 (x)
790 (accumulate1-compound-type x types %compound-type-p simplify2)))
791 (declare (inline accumulate1))
792 (if (funcall %compound-type-p type)
793 (map nil #'accumulate1 (compound-type-types type))
797 ;;; shared logic for unions and intersections: Return a vector of
798 ;;; types representing the same types as INPUT-TYPES, but with
799 ;;; COMPOUND-TYPEs satisfying %COMPOUND-TYPE-P broken up into their
800 ;;; component types, and with any SIMPLY2 simplifications applied.
801 (defun simplified-compound-types (input-types %compound-type-p simplify2)
802 (let ((simplified-types (make-array (length input-types)
805 ;; (This INITIAL-ELEMENT shouldn't
806 ;; matter, but helps avoid type
807 ;; warnings at compile time.)
808 :initial-element *empty-type*)))
809 (dolist (input-type input-types)
810 (accumulate-compound-type input-type
816 ;;; shared logic for unions and intersections: Make a COMPOUND-TYPE
817 ;;; object whose components are the types in TYPES, or skip to special
818 ;;; cases when TYPES is short.
819 (defun make-compound-type-or-something (constructor types enumerable identity)
820 (declare (type function constructor))
821 (declare (type (vector ctype) types))
822 (declare (type ctype identity))
826 (t (funcall constructor
828 ;; FIXME: This should be just (COERCE TYPES 'LIST), but as
829 ;; of sbcl-0.6.11.17 the COERCE optimizer is really
830 ;; brain-dead, so that would generate a full call to
831 ;; SPECIFIER-TYPE at runtime, so we get into bootstrap
832 ;; problems in cold init because 'LIST is a compound
833 ;; type, so we need to MAKE-COMPOUND-TYPE-OR-SOMETHING
834 ;; before we know what 'LIST is. Once the COERCE
835 ;; optimizer is less brain-dead, we can make this
836 ;; (COERCE TYPES 'LIST) again.
837 #+sb-xc-host (coerce types 'list)
838 #-sb-xc-host (coerce-to-list types)))))
840 (defun type-intersection (&rest input-types)
841 (let ((simplified-types (simplified-compound-types input-types
842 #'intersection-type-p
843 #'type-intersection2)))
844 (declare (type (vector ctype) simplified-types))
845 ;; We want to have a canonical representation of types (or failing
846 ;; that, punt to HAIRY-TYPE). Canonical representation would have
847 ;; intersections inside unions but not vice versa, since you can
848 ;; always achieve that by the distributive rule. But we don't want
849 ;; to just apply the distributive rule, since it would be too easy
850 ;; to end up with unreasonably huge type expressions. So instead
851 ;; we punt to HAIRY-TYPE when this comes up.
852 (if (and (> (length simplified-types) 1)
853 (some #'union-type-p simplified-types))
855 :specifier `(and ,@(map 'list #'type-specifier simplified-types)))
856 (make-compound-type-or-something #'%make-intersection-type
858 (some #'type-enumerable
862 (defun type-union (&rest input-types)
863 (let ((simplified-types (simplified-compound-types input-types
866 (make-compound-type-or-something #'%make-union-type
868 (every #'type-enumerable simplified-types)
873 (!define-type-class named)
876 (defvar *empty-type*)
877 (defvar *universal-type*)
878 (defvar *universal-function-type*)
880 (macrolet ((frob (name var)
882 (setq ,var (make-named-type :name ',name))
883 (setf (info :type :kind ',name) :primitive)
884 (setf (info :type :builtin ',name) ,var))))
885 ;; KLUDGE: In ANSI, * isn't really the name of a type, it's just a
886 ;; special symbol which can be stuck in some places where an
887 ;; ordinary type can go, e.g. (ARRAY * 1) instead of (ARRAY T 1).
888 ;; At some point, in order to become more standard, we should
889 ;; convert all the classic CMU CL legacy *s and *WILD-TYPE*s into
890 ;; Ts and *UNIVERSAL-TYPE*s.
892 (frob nil *empty-type*)
893 (frob t *universal-type*))
894 (setf *universal-function-type*
895 (make-function-type :wild-args t
896 :returns *wild-type*)))
898 (!define-type-method (named :simple-=) (type1 type2)
899 ;; FIXME: BUG 85: This assertion failed when I added it in
900 ;; sbcl-0.6.11.13. It probably shouldn't fail; but for now it's
901 ;; just commented out.
902 ;;(aver (not (eq type1 *wild-type*))) ; * isn't really a type.
903 (values (eq type1 type2) t))
905 (!define-type-method (named :simple-subtypep) (type1 type2)
906 (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
907 (values (or (eq type1 *empty-type*) (eq type2 *wild-type*)) t))
909 (!define-type-method (named :complex-subtypep-arg1) (type1 type2)
910 (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
911 ;; FIXME: Why does this (old CMU CL) assertion hold? Perhaps 'cause
912 ;; the HAIRY-TYPE COMPLEX-SUBTYPEP-ARG2 method takes precedence over
913 ;; this COMPLEX-SUBTYPE-ARG1 method? (I miss CLOS..)
914 (aver (not (hairy-type-p type2)))
915 ;; Besides the old CMU CL assertion above, we also need to avoid
916 ;; compound types, else we could get into trouble with
917 ;; (SUBTYPEP T '(OR (SATISFIES FOO) (SATISFIES BAR)))
919 ;; (SUBTYPEP T '(AND (SATISFIES FOO) (SATISFIES BAR))).
920 (aver (not (compound-type-p type2)))
921 ;; Then, since TYPE2 is reasonably tractable, we're good to go.
922 (values (eq type1 *empty-type*) t))
924 (!define-type-method (named :complex-subtypep-arg2) (type1 type2)
925 (aver (not (eq type2 *wild-type*))) ; * isn't really a type.
926 (cond ((eq type2 *universal-type*)
928 ((hairy-type-p type1)
931 ;; FIXME: This seems to rely on there only being 2 or 3
932 ;; HAIRY-TYPE values, and the exclusion of various
933 ;; possibilities above. It would be good to explain it and/or
934 ;; rewrite it so that it's clearer.
935 (values (not (eq type2 *empty-type*)) t))))
937 (!define-type-method (named :complex-intersection2) (type1 type2)
938 ;; FIXME: This assertion failed when I added it in sbcl-0.6.11.13.
939 ;; Perhaps when bug 85 is fixed it can be reenabled.
940 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
941 (hierarchical-intersection2 type1 type2))
943 (!define-type-method (named :complex-union2) (type1 type2)
944 ;; Perhaps when bug 85 is fixed this can be reenabled.
945 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
946 (hierarchical-union2 type1 type2))
948 (!define-type-method (named :unparse) (x)
951 ;;;; hairy and unknown types
953 (!define-type-method (hairy :unparse) (x) (hairy-type-specifier x))
955 (!define-type-method (hairy :simple-subtypep) (type1 type2)
956 (let ((hairy-spec1 (hairy-type-specifier type1))
957 (hairy-spec2 (hairy-type-specifier type2)))
958 (cond ((and (consp hairy-spec1) (eq (car hairy-spec1) 'not)
959 (consp hairy-spec2) (eq (car hairy-spec2) 'not))
960 (csubtypep (specifier-type (cadr hairy-spec2))
961 (specifier-type (cadr hairy-spec1))))
962 ((equal hairy-spec1 hairy-spec2)
967 (!define-type-method (hairy :complex-subtypep-arg2) (type1 type2)
968 (let ((hairy-spec (hairy-type-specifier type2)))
969 (cond ((and (consp hairy-spec) (eq (car hairy-spec) 'not))
970 (let* ((complement-type2 (specifier-type (cadr hairy-spec)))
971 (intersection2 (type-intersection2 type1
974 (values (eq intersection2 *empty-type*) t)
979 (!define-type-method (hairy :complex-subtypep-arg1 :complex-=) (type1 type2)
980 (declare (ignore type1 type2))
983 (!define-type-method (hairy :simple-intersection2 :complex-intersection2)
985 (declare (ignore type1 type2))
988 (!define-type-method (hairy :simple-=) (type1 type2)
989 (if (equal (hairy-type-specifier type1)
990 (hairy-type-specifier type2))
994 (!def-type-translator not (&whole whole type)
995 (declare (ignore type))
996 ;; Check legality of arguments.
997 (destructuring-bind (not typespec) whole
998 (declare (ignore not))
999 (specifier-type typespec)) ; must be legal typespec
1001 (make-hairy-type :specifier whole))
1003 (!def-type-translator satisfies (&whole whole fun)
1004 (declare (ignore fun))
1005 ;; Check legality of arguments.
1006 (destructuring-bind (satisfies predicate-name) whole
1007 (declare (ignore satisfies))
1008 (unless (symbolp predicate-name)
1009 (error 'simple-type-error
1010 :datum predicate-name
1011 :expected-type 'symbol
1012 :format-control "~S is not a symbol."
1013 :format-arguments (list predicate-name))))
1015 (make-hairy-type :specifier whole))
1019 (!define-type-class number)
1021 (!define-type-method (number :simple-=) (type1 type2)
1023 (and (eq (numeric-type-class type1) (numeric-type-class type2))
1024 (eq (numeric-type-format type1) (numeric-type-format type2))
1025 (eq (numeric-type-complexp type1) (numeric-type-complexp type2))
1026 (equal (numeric-type-low type1) (numeric-type-low type2))
1027 (equal (numeric-type-high type1) (numeric-type-high type2)))
1030 (!define-type-method (number :unparse) (type)
1031 (let* ((complexp (numeric-type-complexp type))
1032 (low (numeric-type-low type))
1033 (high (numeric-type-high type))
1034 (base (case (numeric-type-class type)
1036 (rational 'rational)
1037 (float (or (numeric-type-format type) 'float))
1040 (cond ((and (eq base 'integer) high low)
1041 (let ((high-count (logcount high))
1042 (high-length (integer-length high)))
1044 (cond ((= high 0) '(integer 0 0))
1046 ((and (= high-count high-length)
1047 (plusp high-length))
1048 `(unsigned-byte ,high-length))
1050 `(mod ,(1+ high)))))
1051 ((and (= low sb!vm:*target-most-negative-fixnum*)
1052 (= high sb!vm:*target-most-positive-fixnum*))
1054 ((and (= low (lognot high))
1055 (= high-count high-length)
1057 `(signed-byte ,(1+ high-length)))
1059 `(integer ,low ,high)))))
1060 (high `(,base ,(or low '*) ,high))
1062 (if (and (eq base 'integer) (= low 0))
1070 (if (eq base+bounds 'real)
1072 `(complex ,base+bounds)))
1074 (aver (eq base+bounds 'real))
1077 ;;; Return true if X is "less than or equal" to Y, taking open bounds
1078 ;;; into consideration. CLOSED is the predicate used to test the bound
1079 ;;; on a closed interval (e.g. <=), and OPEN is the predicate used on
1080 ;;; open bounds (e.g. <). Y is considered to be the outside bound, in
1081 ;;; the sense that if it is infinite (NIL), then the test succeeds,
1082 ;;; whereas if X is infinite, then the test fails (unless Y is also
1085 ;;; This is for comparing bounds of the same kind, e.g. upper and
1086 ;;; upper. Use NUMERIC-BOUND-TEST* for different kinds of bounds.
1087 #!-negative-zero-is-not-zero
1088 (defmacro numeric-bound-test (x y closed open)
1093 (,closed (car ,x) (car ,y))
1094 (,closed (car ,x) ,y)))
1100 #!+negative-zero-is-not-zero
1101 (defmacro numeric-bound-test-zero (op x y)
1102 `(if (and (zerop ,x) (zerop ,y) (floatp ,x) (floatp ,y))
1103 (,op (float-sign ,x) (float-sign ,y))
1106 #!+negative-zero-is-not-zero
1107 (defmacro numeric-bound-test (x y closed open)
1112 (numeric-bound-test-zero ,closed (car ,x) (car ,y))
1113 (numeric-bound-test-zero ,closed (car ,x) ,y)))
1116 (numeric-bound-test-zero ,open ,x (car ,y))
1117 (numeric-bound-test-zero ,closed ,x ,y)))))
1119 ;;; This is used to compare upper and lower bounds. This is different
1120 ;;; from the same-bound case:
1121 ;;; -- Since X = NIL is -infinity, whereas y = NIL is +infinity, we
1122 ;;; return true if *either* arg is NIL.
1123 ;;; -- an open inner bound is "greater" and also squeezes the interval,
1124 ;;; causing us to use the OPEN test for those cases as well.
1125 #!-negative-zero-is-not-zero
1126 (defmacro numeric-bound-test* (x y closed open)
1131 (,open (car ,x) (car ,y))
1132 (,open (car ,x) ,y)))
1138 #!+negative-zero-is-not-zero
1139 (defmacro numeric-bound-test* (x y closed open)
1144 (numeric-bound-test-zero ,open (car ,x) (car ,y))
1145 (numeric-bound-test-zero ,open (car ,x) ,y)))
1148 (numeric-bound-test-zero ,open ,x (car ,y))
1149 (numeric-bound-test-zero ,closed ,x ,y)))))
1151 ;;; Return whichever of the numeric bounds X and Y is "maximal"
1152 ;;; according to the predicates CLOSED (e.g. >=) and OPEN (e.g. >).
1153 ;;; This is only meaningful for maximizing like bounds, i.e. upper and
1154 ;;; upper. If MAX-P is true, then we return NIL if X or Y is NIL,
1155 ;;; otherwise we return the other arg.
1156 (defmacro numeric-bound-max (x y closed open max-p)
1159 `(cond ((not ,n-x) ,(if max-p nil n-y))
1160 ((not ,n-y) ,(if max-p nil n-x))
1163 (if (,closed (car ,n-x) (car ,n-y)) ,n-x ,n-y)
1164 (if (,open (car ,n-x) ,n-y) ,n-x ,n-y)))
1167 (if (,open (car ,n-y) ,n-x) ,n-y ,n-x)
1168 (if (,closed ,n-y ,n-x) ,n-y ,n-x))))))
1170 (!define-type-method (number :simple-subtypep) (type1 type2)
1171 (let ((class1 (numeric-type-class type1))
1172 (class2 (numeric-type-class type2))
1173 (complexp2 (numeric-type-complexp type2))
1174 (format2 (numeric-type-format type2))
1175 (low1 (numeric-type-low type1))
1176 (high1 (numeric-type-high type1))
1177 (low2 (numeric-type-low type2))
1178 (high2 (numeric-type-high type2)))
1179 ;; If one is complex and the other isn't, they are disjoint.
1180 (cond ((not (or (eq (numeric-type-complexp type1) complexp2)
1183 ;; If the classes are specified and different, the types are
1184 ;; disjoint unless type2 is rational and type1 is integer.
1185 ((not (or (eq class1 class2)
1187 (and (eq class1 'integer)
1188 (eq class2 'rational))))
1190 ;; If the float formats are specified and different, the types
1192 ((not (or (eq (numeric-type-format type1) format2)
1195 ;; Check the bounds.
1196 ((and (numeric-bound-test low1 low2 >= >)
1197 (numeric-bound-test high1 high2 <= <))
1202 (!define-superclasses number ((generic-number)) !cold-init-forms)
1204 ;;; If the high bound of LOW is adjacent to the low bound of HIGH,
1205 ;;; then return true, otherwise NIL.
1206 (defun numeric-types-adjacent (low high)
1207 (let ((low-bound (numeric-type-high low))
1208 (high-bound (numeric-type-low high)))
1209 (cond ((not (and low-bound high-bound)) nil)
1210 ((and (consp low-bound) (consp high-bound)) nil)
1212 #!-negative-zero-is-not-zero
1213 (let ((low-value (car low-bound)))
1214 (or (eql low-value high-bound)
1215 (and (eql low-value -0f0) (eql high-bound 0f0))
1216 (and (eql low-value 0f0) (eql high-bound -0f0))
1217 (and (eql low-value -0d0) (eql high-bound 0d0))
1218 (and (eql low-value 0d0) (eql high-bound -0d0))))
1219 #!+negative-zero-is-not-zero
1220 (eql (car low-bound) high-bound))
1222 #!-negative-zero-is-not-zero
1223 (let ((high-value (car high-bound)))
1224 (or (eql high-value low-bound)
1225 (and (eql high-value -0f0) (eql low-bound 0f0))
1226 (and (eql high-value 0f0) (eql low-bound -0f0))
1227 (and (eql high-value -0d0) (eql low-bound 0d0))
1228 (and (eql high-value 0d0) (eql low-bound -0d0))))
1229 #!+negative-zero-is-not-zero
1230 (eql (car high-bound) low-bound))
1231 #!+negative-zero-is-not-zero
1232 ((or (and (eql low-bound -0f0) (eql high-bound 0f0))
1233 (and (eql low-bound -0d0) (eql high-bound 0d0))))
1234 ((and (eq (numeric-type-class low) 'integer)
1235 (eq (numeric-type-class high) 'integer))
1236 (eql (1+ low-bound) high-bound))
1240 ;;; Return a numeric type that is a supertype for both TYPE1 and TYPE2.
1242 ;;; ### Note: we give up early to keep from dropping lots of information on
1243 ;;; the floor by returning overly general types.
1244 (!define-type-method (number :simple-union2) (type1 type2)
1245 (declare (type numeric-type type1 type2))
1246 (cond ((csubtypep type1 type2) type2)
1247 ((csubtypep type2 type1) type1)
1249 (let ((class1 (numeric-type-class type1))
1250 (format1 (numeric-type-format type1))
1251 (complexp1 (numeric-type-complexp type1))
1252 (class2 (numeric-type-class type2))
1253 (format2 (numeric-type-format type2))
1254 (complexp2 (numeric-type-complexp type2)))
1255 (when (and (eq class1 class2)
1256 (eq format1 format2)
1257 (eq complexp1 complexp2)
1258 (or (numeric-types-intersect type1 type2)
1259 (numeric-types-adjacent type1 type2)
1260 (numeric-types-adjacent type2 type1)))
1265 :low (numeric-bound-max (numeric-type-low type1)
1266 (numeric-type-low type2)
1268 :high (numeric-bound-max (numeric-type-high type1)
1269 (numeric-type-high type2)
1273 (setf (info :type :kind 'number) :primitive)
1274 (setf (info :type :builtin 'number)
1275 (make-numeric-type :complexp nil)))
1277 (!def-type-translator complex (&optional (typespec '*))
1278 (if (eq typespec '*)
1279 (make-numeric-type :complexp :complex)
1280 (labels ((not-numeric ()
1281 ;; FIXME: should probably be TYPE-ERROR
1282 (error "The component type for COMPLEX is not numeric: ~S"
1284 (complex1 (component-type)
1285 (unless (numeric-type-p component-type)
1286 ;; FIXME: As per the FIXME below, ANSI says we're
1287 ;; supposed to handle any subtype of REAL, not only
1288 ;; those which can be represented as NUMERIC-TYPE.
1290 (when (eq (numeric-type-complexp component-type) :complex)
1291 (error "The component type for COMPLEX is complex: ~S"
1293 (modified-numeric-type component-type :complexp :complex)))
1294 (let ((type (specifier-type typespec)))
1296 ;; This is all that CMU CL handled.
1297 (numeric-type (complex1 type))
1298 ;; We need to handle UNION-TYPEs in order to deal with
1299 ;; REAL and FLOAT being represented as UNION-TYPEs of more
1301 (union-type (apply #'type-union
1303 (union-type-types type))))
1304 ;; FIXME: ANSI just says that TYPESPEC is a subtype of type
1305 ;; REAL, not necessarily a NUMERIC-TYPE. E.g. TYPESPEC could
1306 ;; legally be (AND REAL (SATISFIES ODDP))! But like the old
1307 ;; CMU CL code, we're still not nearly that general.
1308 (t (not-numeric)))))))
1310 ;;; If X is *, return NIL, otherwise return the bound, which must be a
1311 ;;; member of TYPE or a one-element list of a member of TYPE.
1312 #!-sb-fluid (declaim (inline canonicalized-bound))
1313 (defun canonicalized-bound (bound type)
1314 (cond ((eq bound '*) nil)
1315 ((or (sb!xc:typep bound type)
1317 (sb!xc:typep (car bound) type)
1318 (null (cdr bound))))
1321 (error "Bound is not ~S, a ~S or a list of a ~S: ~S"
1327 (!def-type-translator integer (&optional (low '*) (high '*))
1328 (let* ((l (canonicalized-bound low 'integer))
1329 (lb (if (consp l) (1+ (car l)) l))
1330 (h (canonicalized-bound high 'integer))
1331 (hb (if (consp h) (1- (car h)) h)))
1332 (when (and hb lb (< hb lb))
1333 (error "Lower bound ~S is greater than upper bound ~S." l h))
1334 (make-numeric-type :class 'integer
1336 :enumerable (not (null (and l h)))
1340 (defmacro !def-bounded-type (type class format)
1341 `(!def-type-translator ,type (&optional (low '*) (high '*))
1342 (let ((lb (canonicalized-bound low ',type))
1343 (hb (canonicalized-bound high ',type)))
1344 (unless (numeric-bound-test* lb hb <= <)
1345 (error "Lower bound ~S is not less than upper bound ~S." low high))
1346 (make-numeric-type :class ',class :format ',format :low lb :high hb))))
1348 (!def-bounded-type rational rational nil)
1350 ;;; Unlike CMU CL, we represent the types FLOAT and REAL as
1351 ;;; UNION-TYPEs of more primitive types, in order to make
1352 ;;; type representation more unique, avoiding problems in the
1353 ;;; simplification of things like
1354 ;;; (subtypep '(or (single-float -1.0 1.0) (single-float 0.1))
1355 ;;; '(or (real -1 7) (single-float 0.1) (single-float -1.0 1.0)))
1356 ;;; When we allowed REAL to remain as a separate NUMERIC-TYPE,
1357 ;;; it was too easy for the first argument to be simplified to
1358 ;;; '(SINGLE-FLOAT -1.0), and for the second argument to be simplified
1359 ;;; to '(OR (REAL -1 7) (SINGLE-FLOAT 0.1)) and then for the
1360 ;;; SUBTYPEP to fail (returning NIL,T instead of T,T) because
1361 ;;; the first argument can't be seen to be a subtype of any of the
1362 ;;; terms in the second argument.
1364 ;;; The old CMU CL way was:
1365 ;;; (!def-bounded-type float float nil)
1366 ;;; (!def-bounded-type real nil nil)
1368 ;;; FIXME: If this new way works for a while with no weird new
1369 ;;; problems, we can go back and rip out support for separate FLOAT
1370 ;;; and REAL flavors of NUMERIC-TYPE. The new way was added in
1371 ;;; sbcl-0.6.11.22, 2001-03-21.
1373 ;;; FIXME: It's probably necessary to do something to fix the
1374 ;;; analogous problem with INTEGER and RATIONAL types. Perhaps
1375 ;;; bounded RATIONAL types should be represented as (OR RATIO INTEGER).
1376 (defun coerce-bound (bound type inner-coerce-bound-fun)
1377 (declare (type function inner-coerce-bound-fun))
1378 (cond ((eql bound '*)
1381 (destructuring-bind (inner-bound) bound
1382 (list (funcall inner-coerce-bound-fun inner-bound type))))
1384 (funcall inner-coerce-bound-fun bound type))))
1385 (defun inner-coerce-real-bound (bound type)
1387 (rational (rationalize bound))
1388 (float (if (floatp bound)
1390 ;; Coerce to the widest float format available, to
1391 ;; avoid unnecessary loss of precision:
1392 (coerce bound 'long-float)))))
1393 (defun coerced-real-bound (bound type)
1394 (coerce-bound bound type #'inner-coerce-real-bound))
1395 (defun coerced-float-bound (bound type)
1396 (coerce-bound bound type #'coerce))
1397 (!def-type-translator real (&optional (low '*) (high '*))
1398 (specifier-type `(or (float ,(coerced-real-bound low 'float)
1399 ,(coerced-real-bound high 'float))
1400 (rational ,(coerced-real-bound low 'rational)
1401 ,(coerced-real-bound high 'rational)))))
1402 (!def-type-translator float (&optional (low '*) (high '*))
1404 `(or (single-float ,(coerced-float-bound low 'single-float)
1405 ,(coerced-float-bound high 'single-float))
1406 (double-float ,(coerced-float-bound low 'double-float)
1407 ,(coerced-float-bound high 'double-float))
1408 #!+long-float ,(error "stub: no long float support yet"))))
1410 (defmacro !define-float-format (f)
1411 `(!def-bounded-type ,f float ,f))
1413 (!define-float-format short-float)
1414 (!define-float-format single-float)
1415 (!define-float-format double-float)
1416 (!define-float-format long-float)
1418 (defun numeric-types-intersect (type1 type2)
1419 (declare (type numeric-type type1 type2))
1420 (let* ((class1 (numeric-type-class type1))
1421 (class2 (numeric-type-class type2))
1422 (complexp1 (numeric-type-complexp type1))
1423 (complexp2 (numeric-type-complexp type2))
1424 (format1 (numeric-type-format type1))
1425 (format2 (numeric-type-format type2))
1426 (low1 (numeric-type-low type1))
1427 (high1 (numeric-type-high type1))
1428 (low2 (numeric-type-low type2))
1429 (high2 (numeric-type-high type2)))
1430 ;; If one is complex and the other isn't, then they are disjoint.
1431 (cond ((not (or (eq complexp1 complexp2)
1432 (null complexp1) (null complexp2)))
1434 ;; If either type is a float, then the other must either be
1435 ;; specified to be a float or unspecified. Otherwise, they
1437 ((and (eq class1 'float)
1438 (not (member class2 '(float nil)))) nil)
1439 ((and (eq class2 'float)
1440 (not (member class1 '(float nil)))) nil)
1441 ;; If the float formats are specified and different, the
1442 ;; types are disjoint.
1443 ((not (or (eq format1 format2) (null format1) (null format2)))
1446 ;; Check the bounds. This is a bit odd because we must
1447 ;; always have the outer bound of the interval as the
1449 (if (numeric-bound-test high1 high2 <= <)
1450 (or (and (numeric-bound-test low1 low2 >= >)
1451 (numeric-bound-test* low1 high2 <= <))
1452 (and (numeric-bound-test low2 low1 >= >)
1453 (numeric-bound-test* low2 high1 <= <)))
1454 (or (and (numeric-bound-test* low2 high1 <= <)
1455 (numeric-bound-test low2 low1 >= >))
1456 (and (numeric-bound-test high2 high1 <= <)
1457 (numeric-bound-test* high2 low1 >= >))))))))
1459 ;;; Take the numeric bound X and convert it into something that can be
1460 ;;; used as a bound in a numeric type with the specified CLASS and
1461 ;;; FORMAT. If UP-P is true, then we round up as needed, otherwise we
1462 ;;; round down. UP-P true implies that X is a lower bound, i.e. (N) > N.
1464 ;;; This is used by NUMERIC-TYPE-INTERSECTION to mash the bound into
1465 ;;; the appropriate type number. X may only be a float when CLASS is
1468 ;;; ### Note: it is possible for the coercion to a float to overflow
1469 ;;; or underflow. This happens when the bound doesn't fit in the
1470 ;;; specified format. In this case, we should really return the
1471 ;;; appropriate {Most | Least}-{Positive | Negative}-XXX-Float float
1472 ;;; of desired format. But these conditions aren't currently signalled
1473 ;;; in any useful way.
1475 ;;; Also, when converting an open rational bound into a float we
1476 ;;; should probably convert it to a closed bound of the closest float
1477 ;;; in the specified format. KLUDGE: In general, open float bounds are
1478 ;;; screwed up. -- (comment from original CMU CL)
1479 (defun round-numeric-bound (x class format up-p)
1481 (let ((cx (if (consp x) (car x) x)))
1485 (if (and (consp x) (integerp cx))
1486 (if up-p (1+ cx) (1- cx))
1487 (if up-p (ceiling cx) (floor cx))))
1489 (let ((res (if format (coerce cx format) (float cx))))
1490 (if (consp x) (list res) res)))))
1493 ;;; Handle the case of type intersection on two numeric types. We use
1494 ;;; TYPES-EQUAL-OR-INTERSECT to throw out the case of types with no
1495 ;;; intersection. If an attribute in TYPE1 is unspecified, then we use
1496 ;;; TYPE2's attribute, which must be at least as restrictive. If the
1497 ;;; types intersect, then the only attributes that can be specified
1498 ;;; and different are the class and the bounds.
1500 ;;; When the class differs, we use the more restrictive class. The
1501 ;;; only interesting case is RATIONAL/INTEGER, since RATIONAL includes
1504 ;;; We make the result lower (upper) bound the maximum (minimum) of
1505 ;;; the argument lower (upper) bounds. We convert the bounds into the
1506 ;;; appropriate numeric type before maximizing. This avoids possible
1507 ;;; confusion due to mixed-type comparisons (but I think the result is
1509 (!define-type-method (number :simple-intersection2) (type1 type2)
1510 (declare (type numeric-type type1 type2))
1511 (if (numeric-types-intersect type1 type2)
1512 (let* ((class1 (numeric-type-class type1))
1513 (class2 (numeric-type-class type2))
1514 (class (ecase class1
1516 ((integer float) class1)
1517 (rational (if (eq class2 'integer)
1520 (format (or (numeric-type-format type1)
1521 (numeric-type-format type2))))
1525 :complexp (or (numeric-type-complexp type1)
1526 (numeric-type-complexp type2))
1527 :low (numeric-bound-max
1528 (round-numeric-bound (numeric-type-low type1)
1530 (round-numeric-bound (numeric-type-low type2)
1533 :high (numeric-bound-max
1534 (round-numeric-bound (numeric-type-high type1)
1536 (round-numeric-bound (numeric-type-high type2)
1541 ;;; Given two float formats, return the one with more precision. If
1542 ;;; either one is null, return NIL.
1543 (defun float-format-max (f1 f2)
1545 (dolist (f *float-formats* (error "bad float format: ~S" f1))
1546 (when (or (eq f f1) (eq f f2))
1549 ;;; Return the result of an operation on TYPE1 and TYPE2 according to
1550 ;;; the rules of numeric contagion. This is always NUMBER, some float
1551 ;;; format (possibly complex) or RATIONAL. Due to rational
1552 ;;; canonicalization, there isn't much we can do here with integers or
1553 ;;; rational complex numbers.
1555 ;;; If either argument is not a NUMERIC-TYPE, then return NUMBER. This
1556 ;;; is useful mainly for allowing types that are technically numbers,
1557 ;;; but not a NUMERIC-TYPE.
1558 (defun numeric-contagion (type1 type2)
1559 (if (and (numeric-type-p type1) (numeric-type-p type2))
1560 (let ((class1 (numeric-type-class type1))
1561 (class2 (numeric-type-class type2))
1562 (format1 (numeric-type-format type1))
1563 (format2 (numeric-type-format type2))
1564 (complexp1 (numeric-type-complexp type1))
1565 (complexp2 (numeric-type-complexp type2)))
1566 (cond ((or (null complexp1)
1568 (specifier-type 'number))
1572 :format (ecase class2
1573 (float (float-format-max format1 format2))
1574 ((integer rational) format1)
1576 ;; A double-float with any real number is a
1579 (if (eq format1 'double-float)
1582 ;; A long-float with any real number is a
1585 (if (eq format1 'long-float)
1588 :complexp (if (or (eq complexp1 :complex)
1589 (eq complexp2 :complex))
1592 ((eq class2 'float) (numeric-contagion type2 type1))
1593 ((and (eq complexp1 :real) (eq complexp2 :real))
1595 :class (and class1 class2 'rational)
1598 (specifier-type 'number))))
1599 (specifier-type 'number)))
1603 (!define-type-class array)
1605 ;;; What this does depends on the setting of the
1606 ;;; *USE-IMPLEMENTATION-TYPES* switch. If true, return the specialized
1607 ;;; element type, otherwise return the original element type.
1608 (defun specialized-element-type-maybe (type)
1609 (declare (type array-type type))
1610 (if *use-implementation-types*
1611 (array-type-specialized-element-type type)
1612 (array-type-element-type type)))
1614 (!define-type-method (array :simple-=) (type1 type2)
1615 (values (and (equal (array-type-dimensions type1)
1616 (array-type-dimensions type2))
1617 (eq (array-type-complexp type1)
1618 (array-type-complexp type2))
1619 (type= (specialized-element-type-maybe type1)
1620 (specialized-element-type-maybe type2)))
1623 (!define-type-method (array :unparse) (type)
1624 (let ((dims (array-type-dimensions type))
1625 (eltype (type-specifier (array-type-element-type type)))
1626 (complexp (array-type-complexp type)))
1629 (if complexp 'array 'simple-array)
1630 (if complexp `(array ,eltype) `(simple-array ,eltype))))
1631 ((= (length dims) 1)
1633 (if (eq (car dims) '*)
1636 (base-char 'base-string)
1639 (t `(vector ,eltype)))
1641 (bit `(bit-vector ,(car dims)))
1642 (base-char `(base-string ,(car dims)))
1643 (character `(string ,(car dims)))
1644 (t `(vector ,eltype ,(car dims)))))
1645 (if (eq (car dims) '*)
1647 (bit 'simple-bit-vector)
1648 (base-char 'simple-base-string)
1649 (character 'simple-string)
1650 ((t) 'simple-vector)
1651 (t `(simple-array ,eltype (*))))
1653 (bit `(simple-bit-vector ,(car dims)))
1654 (base-char `(simple-base-string ,(car dims)))
1655 (character `(simple-string ,(car dims)))
1656 ((t) `(simple-vector ,(car dims)))
1657 (t `(simple-array ,eltype ,dims))))))
1660 `(array ,eltype ,dims)
1661 `(simple-array ,eltype ,dims))))))
1663 (!define-type-method (array :simple-subtypep) (type1 type2)
1664 (let ((dims1 (array-type-dimensions type1))
1665 (dims2 (array-type-dimensions type2))
1666 (complexp2 (array-type-complexp type2)))
1667 (cond (;; not subtypep unless dimensions are compatible
1668 (not (or (eq dims2 '*)
1669 (and (not (eq dims1 '*))
1670 ;; (sbcl-0.6.4 has trouble figuring out that
1671 ;; DIMS1 and DIMS2 must be lists at this
1672 ;; point, and knowing that is important to
1673 ;; compiling EVERY efficiently.)
1674 (= (length (the list dims1))
1675 (length (the list dims2)))
1676 (every (lambda (x y)
1677 (or (eq y '*) (eql x y)))
1679 (the list dims2)))))
1681 ;; not subtypep unless complexness is compatible
1682 ((not (or (eq complexp2 :maybe)
1683 (eq (array-type-complexp type1) complexp2)))
1685 ;; Since we didn't fail any of the tests above, we win
1686 ;; if the TYPE2 element type is wild.
1687 ((eq (array-type-element-type type2) *wild-type*)
1689 (;; Since we didn't match any of the special cases above, we
1690 ;; can't give a good answer unless both the element types
1691 ;; have been defined.
1692 (or (unknown-type-p (array-type-element-type type1))
1693 (unknown-type-p (array-type-element-type type2)))
1695 (;; Otherwise, the subtype relationship holds iff the
1696 ;; types are equal, and they're equal iff the specialized
1697 ;; element types are identical.
1699 (values (type= (specialized-element-type-maybe type1)
1700 (specialized-element-type-maybe type2))
1703 (!define-superclasses array
1709 (defun array-types-intersect (type1 type2)
1710 (declare (type array-type type1 type2))
1711 (let ((dims1 (array-type-dimensions type1))
1712 (dims2 (array-type-dimensions type2))
1713 (complexp1 (array-type-complexp type1))
1714 (complexp2 (array-type-complexp type2)))
1715 ;; See whether dimensions are compatible.
1716 (cond ((not (or (eq dims1 '*) (eq dims2 '*)
1717 (and (= (length dims1) (length dims2))
1718 (every #'(lambda (x y)
1719 (or (eq x '*) (eq y '*) (= x y)))
1722 ;; See whether complexpness is compatible.
1723 ((not (or (eq complexp1 :maybe)
1724 (eq complexp2 :maybe)
1725 (eq complexp1 complexp2)))
1727 ;; If either element type is wild, then they intersect.
1728 ;; Otherwise, the types must be identical.
1729 ((or (eq (array-type-element-type type1) *wild-type*)
1730 (eq (array-type-element-type type2) *wild-type*)
1731 (type= (specialized-element-type-maybe type1)
1732 (specialized-element-type-maybe type2)))
1738 (!define-type-method (array :simple-intersection2) (type1 type2)
1739 (declare (type array-type type1 type2))
1740 (if (array-types-intersect type1 type2)
1741 (let ((dims1 (array-type-dimensions type1))
1742 (dims2 (array-type-dimensions type2))
1743 (complexp1 (array-type-complexp type1))
1744 (complexp2 (array-type-complexp type2))
1745 (eltype1 (array-type-element-type type1))
1746 (eltype2 (array-type-element-type type2)))
1747 (specialize-array-type
1749 :dimensions (cond ((eq dims1 '*) dims2)
1750 ((eq dims2 '*) dims1)
1752 (mapcar (lambda (x y) (if (eq x '*) y x))
1754 :complexp (if (eq complexp1 :maybe) complexp2 complexp1)
1755 :element-type (if (eq eltype1 *wild-type*) eltype2 eltype1))))
1758 ;;; Check a supplied dimension list to determine whether it is legal,
1759 ;;; and return it in canonical form (as either '* or a list).
1760 (defun canonical-array-dimensions (dims)
1765 (error "Arrays can't have a negative number of dimensions: ~S" dims))
1766 (when (>= dims sb!xc:array-rank-limit)
1767 (error "array type with too many dimensions: ~S" dims))
1768 (make-list dims :initial-element '*))
1770 (when (>= (length dims) sb!xc:array-rank-limit)
1771 (error "array type with too many dimensions: ~S" dims))
1774 (unless (and (integerp dim)
1776 (< dim sb!xc:array-dimension-limit))
1777 (error "bad dimension in array type: ~S" dim))))
1780 (error "Array dimensions is not a list, integer or *:~% ~S" dims))))
1784 (!define-type-class member)
1786 (!define-type-method (member :unparse) (type)
1787 (let ((members (member-type-members type)))
1788 (if (equal members '(nil))
1790 `(member ,@members))))
1792 (!define-type-method (member :simple-subtypep) (type1 type2)
1793 (values (subsetp (member-type-members type1) (member-type-members type2))
1796 (!define-type-method (member :complex-subtypep-arg1) (type1 type2)
1797 (every/type (swapped-args-fun #'ctypep)
1799 (member-type-members type1)))
1801 ;;; We punt if the odd type is enumerable and intersects with the
1802 ;;; MEMBER type. If not enumerable, then it is definitely not a
1803 ;;; subtype of the MEMBER type.
1804 (!define-type-method (member :complex-subtypep-arg2) (type1 type2)
1805 (cond ((not (type-enumerable type1)) (values nil t))
1806 ((types-equal-or-intersect type1 type2) (values nil nil))
1807 (t (values nil t))))
1809 (!define-type-method (member :simple-intersection2) (type1 type2)
1810 (let ((mem1 (member-type-members type1))
1811 (mem2 (member-type-members type2)))
1812 (cond ((subsetp mem1 mem2) type1)
1813 ((subsetp mem2 mem1) type2)
1815 (let ((res (intersection mem1 mem2)))
1817 (make-member-type :members res)
1820 (!define-type-method (member :complex-intersection2) (type1 type2)
1822 (collect ((members))
1823 (let ((mem2 (member-type-members type2)))
1824 (dolist (member mem2)
1825 (multiple-value-bind (val win) (ctypep member type1)
1827 (return-from punt nil))
1828 (when val (members member))))
1829 (cond ((subsetp mem2 (members)) type2)
1830 ((null (members)) *empty-type*)
1832 (make-member-type :members (members))))))))
1834 ;;; We don't need a :COMPLEX-UNION2, since the only interesting case is
1835 ;;; a union type, and the member/union interaction is handled by the
1836 ;;; union type method.
1837 (!define-type-method (member :simple-union2) (type1 type2)
1838 (let ((mem1 (member-type-members type1))
1839 (mem2 (member-type-members type2)))
1840 (cond ((subsetp mem1 mem2) type2)
1841 ((subsetp mem2 mem1) type1)
1843 (make-member-type :members (union mem1 mem2))))))
1845 (!define-type-method (member :simple-=) (type1 type2)
1846 (let ((mem1 (member-type-members type1))
1847 (mem2 (member-type-members type2)))
1848 (values (and (subsetp mem1 mem2)
1849 (subsetp mem2 mem1))
1852 (!define-type-method (member :complex-=) (type1 type2)
1853 (if (type-enumerable type1)
1854 (multiple-value-bind (val win) (csubtypep type2 type1)
1855 (if (or val (not win))
1860 (!def-type-translator member (&rest members)
1862 (make-member-type :members (remove-duplicates members))
1865 ;;;; intersection types
1867 ;;;; Until version 0.6.10.6, SBCL followed the original CMU CL approach
1868 ;;;; of punting on all AND types, not just the unreasonably complicated
1869 ;;;; ones. The change was motivated by trying to get the KEYWORD type
1870 ;;;; to behave sensibly:
1871 ;;;; ;; reasonable definition
1872 ;;;; (DEFTYPE KEYWORD () '(AND SYMBOL (SATISFIES KEYWORDP)))
1873 ;;;; ;; reasonable behavior
1874 ;;;; (AVER (SUBTYPEP 'KEYWORD 'SYMBOL))
1875 ;;;; Without understanding a little about the semantics of AND, we'd
1876 ;;;; get (SUBTYPEP 'KEYWORD 'SYMBOL)=>NIL,NIL and, for entirely
1877 ;;;; parallel reasons, (SUBTYPEP 'RATIO 'NUMBER)=>NIL,NIL. That's
1880 ;;;; We still follow the example of CMU CL to some extent, by punting
1881 ;;;; (to the opaque HAIRY-TYPE) on sufficiently complicated types
1884 (!define-type-class intersection)
1886 ;;; A few intersection types have special names. The others just get
1887 ;;; mechanically unparsed.
1888 (!define-type-method (intersection :unparse) (type)
1889 (declare (type ctype type))
1890 (or (find type '(ratio bignum keyword) :key #'specifier-type :test #'type=)
1891 `(and ,@(mapcar #'type-specifier (intersection-type-types type)))))
1893 ;;; shared machinery for type equality: true if every type in the set
1894 ;;; TYPES1 matches a type in the set TYPES2 and vice versa
1895 (defun type=-set (types1 types2)
1896 (flet (;; true if every type in the set X matches a type in the set Y
1898 (declare (type list x y))
1899 (every (lambda (xelement)
1900 (position xelement y :test #'type=))
1902 (values (and (type<=-set types1 types2)
1903 (type<=-set types2 types1))
1906 ;;; Two intersection types are equal if their subtypes are equal sets.
1908 ;;; FIXME: Might it be better to use
1909 ;;; (AND (SUBTYPEP X Y) (SUBTYPEP Y X))
1910 ;;; instead, since SUBTYPEP is the usual relationship that we care
1911 ;;; most about, so it would be good to leverage any ingenuity there
1912 ;;; in this more obscure method?
1913 (!define-type-method (intersection :simple-=) (type1 type2)
1914 (type=-set (intersection-type-types type1)
1915 (intersection-type-types type2)))
1917 (defun %intersection-complex-subtypep-arg1 (type1 type2)
1918 (any/type (swapped-args-fun #'csubtypep)
1920 (intersection-type-types type1)))
1922 (!define-type-method (intersection :simple-subtypep) (type1 type2)
1923 (every/type #'%intersection-complex-subtypep-arg1
1925 (intersection-type-types type2)))
1927 (!define-type-method (intersection :complex-subtypep-arg1) (type1 type2)
1928 (%intersection-complex-subtypep-arg1 type1 type2))
1930 (!define-type-method (intersection :complex-subtypep-arg2) (type1 type2)
1931 (every/type #'csubtypep type1 (intersection-type-types type2)))
1933 (!def-type-translator and (&whole whole &rest type-specifiers)
1934 (apply #'type-intersection
1935 (mapcar #'specifier-type
1940 (!define-type-class union)
1942 ;;; The LIST type has a special name. Other union types just get
1943 ;;; mechanically unparsed.
1944 (!define-type-method (union :unparse) (type)
1945 (declare (type ctype type))
1946 (if (type= type (specifier-type 'list))
1948 `(or ,@(mapcar #'type-specifier (union-type-types type)))))
1950 ;;; Two union types are equal if their subtypes are equal sets.
1951 (!define-type-method (union :simple-=) (type1 type2)
1952 (type=-set (union-type-types type1)
1953 (union-type-types type2)))
1955 ;;; Similarly, a union type is a subtype of another if every element
1956 ;;; of TYPE1 is a subtype of some element of TYPE2.
1957 (!define-type-method (union :simple-subtypep) (type1 type2)
1958 (every/type (swapped-args-fun #'union-complex-subtypep-arg2)
1960 (union-type-types type1)))
1962 (defun union-complex-subtypep-arg1 (type1 type2)
1963 (every/type (swapped-args-fun #'csubtypep)
1965 (union-type-types type1)))
1966 (!define-type-method (union :complex-subtypep-arg1) (type1 type2)
1967 (union-complex-subtypep-arg1 type1 type2))
1969 (defun union-complex-subtypep-arg2 (type1 type2)
1970 (any/type #'csubtypep type1 (union-type-types type2)))
1971 (!define-type-method (union :complex-subtypep-arg2) (type1 type2)
1972 (union-complex-subtypep-arg2 type1 type2))
1974 (!define-type-method (union :simple-intersection2 :complex-intersection2)
1976 ;; The CSUBTYPEP clauses here let us simplify e.g.
1977 ;; (TYPE-INTERSECTION2 (SPECIFIER-TYPE 'LIST)
1978 ;; (SPECIFIER-TYPE '(OR LIST VECTOR)))
1979 ;; (where LIST is (OR CONS NULL)).
1981 ;; The tests are more or less (CSUBTYPEP TYPE1 TYPE2) and vice
1982 ;; versa, but it's important that we pre-expand them into
1983 ;; specialized operations on individual elements of
1984 ;; UNION-TYPE-TYPES, instead of using the ordinary call to
1985 ;; CSUBTYPEP, in order to avoid possibly invoking any methods which
1986 ;; might in turn invoke (TYPE-INTERSECTION2 TYPE1 TYPE2) and thus
1987 ;; cause infinite recursion.
1988 (cond ((union-complex-subtypep-arg2 type1 type2)
1990 ((union-complex-subtypep-arg1 type2 type1)
1993 ;; KLUDGE: This code accumulates a sequence of TYPE-UNION2
1994 ;; operations in a particular order, and gives up if any of
1995 ;; the sub-unions turn out not to be simple. In other cases
1996 ;; ca. sbcl-0.6.11.15, that approach to taking a union was a
1997 ;; bad idea, since it can overlook simplifications which
1998 ;; might occur if the terms were accumulated in a different
1999 ;; order. It's possible that that will be a problem here too.
2000 ;; However, I can't think of a good example to demonstrate
2001 ;; it, and without an example to demonstrate it I can't write
2002 ;; test cases, and without test cases I don't want to
2003 ;; complicate the code to address what's still a hypothetical
2004 ;; problem. So I punted. -- WHN 2001-03-20
2005 (let ((accumulator *empty-type*))
2006 (dolist (t2 (union-type-types type2) accumulator)
2008 (type-union2 accumulator
2009 (type-intersection type1 t2)))
2010 ;; When our result isn't simple any more (because
2011 ;; TYPE-UNION2 was unable to give us a simple result)
2015 (!def-type-translator or (&rest type-specifiers)
2017 (mapcar #'specifier-type
2022 (!define-type-class cons)
2024 (!def-type-translator cons (&optional (car-type-spec '*) (cdr-type-spec '*))
2025 (make-cons-type (specifier-type car-type-spec)
2026 (specifier-type cdr-type-spec)))
2028 (!define-type-method (cons :unparse) (type)
2029 (let ((car-eltype (type-specifier (cons-type-car-type type)))
2030 (cdr-eltype (type-specifier (cons-type-cdr-type type))))
2031 (if (and (member car-eltype '(t *))
2032 (member cdr-eltype '(t *)))
2034 `(cons ,car-eltype ,cdr-eltype))))
2036 (!define-type-method (cons :simple-=) (type1 type2)
2037 (declare (type cons-type type1 type2))
2038 (and (type= (cons-type-car-type type1) (cons-type-car-type type2))
2039 (type= (cons-type-cdr-type type1) (cons-type-cdr-type type2))))
2041 (!define-type-method (cons :simple-subtypep) (type1 type2)
2042 (declare (type cons-type type1 type2))
2043 (multiple-value-bind (val-car win-car)
2044 (csubtypep (cons-type-car-type type1) (cons-type-car-type type2))
2045 (multiple-value-bind (val-cdr win-cdr)
2046 (csubtypep (cons-type-cdr-type type1) (cons-type-cdr-type type2))
2047 (if (and val-car val-cdr)
2048 (values t (and win-car win-cdr))
2049 (values nil (or win-car win-cdr))))))
2051 ;;; Give up if a precise type is not possible, to avoid returning
2052 ;;; overly general types.
2053 (!define-type-method (cons :simple-union2) (type1 type2)
2054 (declare (type cons-type type1 type2))
2055 (let ((car-type1 (cons-type-car-type type1))
2056 (car-type2 (cons-type-car-type type2))
2057 (cdr-type1 (cons-type-cdr-type type1))
2058 (cdr-type2 (cons-type-cdr-type type2)))
2059 (cond ((type= car-type1 car-type2)
2060 (make-cons-type car-type1
2061 (type-union cdr-type1 cdr-type2)))
2062 ((type= cdr-type1 cdr-type2)
2063 (make-cons-type (type-union cdr-type1 cdr-type2)
2066 (!define-type-method (cons :simple-intersection2) (type1 type2)
2067 (declare (type cons-type type1 type2))
2070 (and (setf car-int2 (type-intersection2 (cons-type-car-type type1)
2071 (cons-type-car-type type2)))
2072 (setf cdr-int2 (type-intersection2 (cons-type-cdr-type type1)
2073 (cons-type-cdr-type type2)))
2074 (make-cons-type car-int2 cdr-int2))))
2076 ;;; Return the type that describes all objects that are in X but not
2077 ;;; in Y. If we can't determine this type, then return NIL.
2079 ;;; For now, we only are clever dealing with union and member types.
2080 ;;; If either type is not a union type, then we pretend that it is a
2081 ;;; union of just one type. What we do is remove from X all the types
2082 ;;; that are a subtype any type in Y. If any type in X intersects with
2083 ;;; a type in Y but is not a subtype, then we give up.
2085 ;;; We must also special-case any member type that appears in the
2086 ;;; union. We remove from X's members all objects that are TYPEP to Y.
2087 ;;; If Y has any members, we must be careful that none of those
2088 ;;; members are CTYPEP to any of Y's non-member types. We give up in
2089 ;;; this case, since to compute that difference we would have to break
2090 ;;; the type from X into some collection of types that represents the
2091 ;;; type without that particular element. This seems too hairy to be
2092 ;;; worthwhile, given its low utility.
2093 (defun type-difference (x y)
2094 (let ((x-types (if (union-type-p x) (union-type-types x) (list x)))
2095 (y-types (if (union-type-p y) (union-type-types y) (list y))))
2097 (dolist (x-type x-types)
2098 (if (member-type-p x-type)
2099 (collect ((members))
2100 (dolist (mem (member-type-members x-type))
2101 (multiple-value-bind (val win) (ctypep mem y)
2102 (unless win (return-from type-difference nil))
2106 (res (make-member-type :members (members)))))
2107 (dolist (y-type y-types (res x-type))
2108 (multiple-value-bind (val win) (csubtypep x-type y-type)
2109 (unless win (return-from type-difference nil))
2111 (when (types-equal-or-intersect x-type y-type)
2112 (return-from type-difference nil))))))
2113 (let ((y-mem (find-if #'member-type-p y-types)))
2115 (let ((members (member-type-members y-mem)))
2116 (dolist (x-type x-types)
2117 (unless (member-type-p x-type)
2118 (dolist (member members)
2119 (multiple-value-bind (val win) (ctypep member x-type)
2120 (when (or (not win) val)
2121 (return-from type-difference nil)))))))))
2122 (apply #'type-union (res)))))
2124 (!def-type-translator array (&optional (element-type '*)
2126 (specialize-array-type
2127 (make-array-type :dimensions (canonical-array-dimensions dimensions)
2128 :element-type (specifier-type element-type))))
2130 (!def-type-translator simple-array (&optional (element-type '*)
2132 (specialize-array-type
2133 (make-array-type :dimensions (canonical-array-dimensions dimensions)
2134 :element-type (specifier-type element-type)
2137 ;;;; utilities shared between cross-compiler and target system
2139 ;;; Does the type derived from compilation of an actual function
2140 ;;; definition satisfy declarations of a function's type?
2141 (defun defined-ftype-matches-declared-ftype-p (defined-ftype declared-ftype)
2142 (declare (type ctype defined-ftype declared-ftype))
2143 (flet ((is-built-in-class-function-p (ctype)
2144 (and (built-in-class-p ctype)
2145 (eq (built-in-class-%name ctype) 'function))))
2146 (cond (;; DECLARED-FTYPE could certainly be #<BUILT-IN-CLASS FUNCTION>;
2147 ;; that's what happens when we (DECLAIM (FTYPE FUNCTION FOO)).
2148 (is-built-in-class-function-p declared-ftype)
2149 ;; In that case, any definition satisfies the declaration.
2151 (;; It's not clear whether or how DEFINED-FTYPE might be
2152 ;; #<BUILT-IN-CLASS FUNCTION>, but it's not obviously
2153 ;; invalid, so let's handle that case too, just in case.
2154 (is-built-in-class-function-p defined-ftype)
2155 ;; No matter what DECLARED-FTYPE might be, we can't prove
2156 ;; that an object of type FUNCTION doesn't satisfy it, so
2157 ;; we return success no matter what.
2159 (;; Otherwise both of them must be FUNCTION-TYPE objects.
2161 ;; FIXME: For now we only check compatibility of the return
2162 ;; type, not argument types, and we don't even check the
2163 ;; return type very precisely (as per bug 94a). It would be
2164 ;; good to do a better job. Perhaps to check the
2165 ;; compatibility of the arguments, we should (1) redo
2166 ;; VALUES-TYPES-EQUAL-OR-INTERSECT as
2167 ;; ARGS-TYPES-EQUAL-OR-INTERSECT, and then (2) apply it to
2168 ;; the ARGS-TYPE slices of the FUNCTION-TYPEs. (ARGS-TYPE
2169 ;; is a base class both of VALUES-TYPE and of FUNCTION-TYPE.)
2170 (values-types-equal-or-intersect
2171 (function-type-returns defined-ftype)
2172 (function-type-returns declared-ftype))))))
2174 ;;; This messy case of CTYPE for NUMBER is shared between the
2175 ;;; cross-compiler and the target system.
2176 (defun ctype-of-number (x)
2177 (let ((num (if (complexp x) (realpart x) x)))
2178 (multiple-value-bind (complexp low high)
2180 (let ((imag (imagpart x)))
2181 (values :complex (min num imag) (max num imag)))
2182 (values :real num num))
2183 (make-numeric-type :class (etypecase num
2185 (rational 'rational)
2187 :format (and (floatp num) (float-format-name num))
2193 ;; Why SAFETY 0? To suppress the is-it-the-right-structure-type
2194 ;; checking for declarations in structure accessors. Otherwise we
2195 ;; can get caught in a chicken-and-egg bootstrapping problem, whose
2196 ;; symptom on x86 OpenBSD sbcl-0.pre7.37.flaky5.22 is an illegal
2197 ;; instruction trap. I haven't tracked it down, but I'm guessing it
2198 ;; has to do with setting LAYOUTs when the LAYOUT hasn't been set
2200 (declare (optimize (safety 0)))
2201 (!defun-from-collected-cold-init-forms !late-type-cold-init))
2203 (/show0 "late-type.lisp end of file")