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 ;;; There are all sorts of nasty problems with open bounds on FLOAT
26 ;;; types (and probably FLOAT types in general.)
28 ;;; This condition is signalled whenever we make a UNKNOWN-TYPE so that
29 ;;; compiler warnings can be emitted as appropriate.
30 (define-condition parse-unknown-type (condition)
31 ((specifier :reader parse-unknown-type-specifier :initarg :specifier)))
33 ;;; FIXME: This really should go away. Alas, it doesn't seem to be so
34 ;;; simple to make it go away.. (See bug 123 in BUGS file.)
35 (defvar *use-implementation-types* t ; actually initialized in cold init
37 "*USE-IMPLEMENTATION-TYPES* is a semi-public flag which determines how
38 restrictive we are in determining type membership. If two types are the
39 same in the implementation, then we will consider them them the same when
40 this switch is on. When it is off, we try to be as restrictive as the
41 language allows, allowing us to detect more errors. Currently, this only
42 affects array types.")
43 (!cold-init-forms (setq *use-implementation-types* t))
45 ;;; These functions are used as method for types which need a complex
46 ;;; subtypep method to handle some superclasses, but cover a subtree
47 ;;; of the type graph (i.e. there is no simple way for any other type
48 ;;; class to be a subtype.) There are always still complex ways,
49 ;;; namely UNION and MEMBER types, so we must give TYPE1's method a
50 ;;; chance to run, instead of immediately returning NIL, T.
51 (defun delegate-complex-subtypep-arg2 (type1 type2)
53 (type-class-complex-subtypep-arg1
54 (type-class-info type1))))
56 (funcall subtypep-arg1 type1 type2)
58 (defun delegate-complex-intersection2 (type1 type2)
59 (let ((method (type-class-complex-intersection2 (type-class-info type1))))
60 (if (and method (not (eq method #'delegate-complex-intersection2)))
61 (funcall method type2 type1)
62 (hierarchical-intersection2 type1 type2))))
64 ;;; This is used by !DEFINE-SUPERCLASSES to define the SUBTYPE-ARG1
65 ;;; method. INFO is a list of conses
66 ;;; (SUPERCLASS-CLASS . {GUARD-TYPE-SPECIFIER | NIL}).
67 (defun !has-superclasses-complex-subtypep-arg1 (type1 type2 info)
68 ;; If TYPE2 might be concealing something related to our class
70 (if (type-might-contain-other-types-p type2)
71 ;; too confusing, gotta punt
73 ;; ordinary case expected by old CMU CL code, where the taxonomy
74 ;; of TYPE2's representation accurately reflects the taxonomy of
77 ;; FIXME: This old CMU CL code probably deserves a comment
78 ;; explaining to us mere mortals how it works...
79 (and (sb!xc:typep type2 'classoid)
81 (when (or (not (cdr x))
82 (csubtypep type1 (specifier-type (cdr x))))
84 (or (eq type2 (car x))
85 (let ((inherits (layout-inherits
86 (classoid-layout (car x)))))
87 (dotimes (i (length inherits) nil)
88 (when (eq type2 (layout-classoid (svref inherits i)))
92 ;;; This function takes a list of specs, each of the form
93 ;;; (SUPERCLASS-NAME &OPTIONAL GUARD).
94 ;;; Consider one spec (with no guard): any instance of the named
95 ;;; TYPE-CLASS is also a subtype of the named superclass and of any of
96 ;;; its superclasses. If there are multiple specs, then some will have
97 ;;; guards. We choose the first spec whose guard is a supertype of
98 ;;; TYPE1 and use its superclass. In effect, a sequence of guards
101 ;;; G0,(and G1 (not G0)), (and G2 (not (or G0 G1))).
103 ;;; WHEN controls when the forms are executed.
104 (defmacro !define-superclasses (type-class-name specs when)
105 (with-unique-names (type-class info)
107 (let ((,type-class (type-class-or-lose ',type-class-name))
108 (,info (mapcar (lambda (spec)
110 (super &optional guard)
112 (cons (find-classoid super) guard)))
114 (setf (type-class-complex-subtypep-arg1 ,type-class)
115 (lambda (type1 type2)
116 (!has-superclasses-complex-subtypep-arg1 type1 type2 ,info)))
117 (setf (type-class-complex-subtypep-arg2 ,type-class)
118 #'delegate-complex-subtypep-arg2)
119 (setf (type-class-complex-intersection2 ,type-class)
120 #'delegate-complex-intersection2)))))
122 ;;;; FUNCTION and VALUES types
124 ;;;; Pretty much all of the general type operations are illegal on
125 ;;;; VALUES types, since we can't discriminate using them, do
126 ;;;; SUBTYPEP, etc. FUNCTION types are acceptable to the normal type
127 ;;;; operations, but are generally considered to be equivalent to
128 ;;;; FUNCTION. These really aren't true types in any type theoretic
129 ;;;; sense, but we still parse them into CTYPE structures for two
132 ;;;; -- Parsing and unparsing work the same way, and indeed we can't
133 ;;;; tell whether a type is a function or values type without
135 ;;;; -- Many of the places that can be annotated with real types can
136 ;;;; also be annotated with function or values types.
138 ;;; the description of a &KEY argument
139 (defstruct (key-info #-sb-xc-host (:pure t)
141 ;; the key (not necessarily a keyword in ANSI Common Lisp)
142 (name (missing-arg) :type symbol)
143 ;; the type of the argument value
144 (type (missing-arg) :type ctype))
146 (!define-type-method (values :simple-subtypep :complex-subtypep-arg1)
148 (declare (ignore type2))
149 ;; FIXME: should be TYPE-ERROR, here and in next method
150 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type1)))
152 (!define-type-method (values :complex-subtypep-arg2)
154 (declare (ignore type1))
155 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type2)))
157 (!define-type-method (values :unparse) (type)
159 (let ((unparsed (unparse-args-types type)))
160 (if (or (values-type-optional type)
161 (values-type-rest type)
162 (values-type-allowp type))
164 (nconc unparsed '(&optional))))))
166 ;;; Return true if LIST1 and LIST2 have the same elements in the same
167 ;;; positions according to TYPE=. We return NIL, NIL if there is an
168 ;;; uncertain comparison.
169 (defun type=-list (list1 list2)
170 (declare (list list1 list2))
171 (do ((types1 list1 (cdr types1))
172 (types2 list2 (cdr types2)))
173 ((or (null types1) (null types2))
174 (if (or types1 types2)
177 (multiple-value-bind (val win)
178 (type= (first types1) (first types2))
180 (return (values nil nil)))
182 (return (values nil t))))))
184 (!define-type-method (values :simple-=) (type1 type2)
185 (let ((rest1 (args-type-rest type1))
186 (rest2 (args-type-rest type2)))
187 (cond ((and rest1 rest2 (type/= rest1 rest2))
192 (multiple-value-bind (req-val req-win)
193 (type=-list (values-type-required type1)
194 (values-type-required type2))
195 (multiple-value-bind (opt-val opt-win)
196 (type=-list (values-type-optional type1)
197 (values-type-optional type2))
198 (values (and req-val opt-val) (and req-win opt-win))))))))
200 (!define-type-class function)
202 ;;; a flag that we can bind to cause complex function types to be
203 ;;; unparsed as FUNCTION. This is useful when we want a type that we
204 ;;; can pass to TYPEP.
205 (defvar *unparse-fun-type-simplify*)
206 (!cold-init-forms (setq *unparse-fun-type-simplify* nil))
208 (!define-type-method (function :unparse) (type)
209 (if *unparse-fun-type-simplify*
212 (if (fun-type-wild-args type)
214 (unparse-args-types type))
216 (fun-type-returns type)))))
218 ;;; The meaning of this is a little confused. On the one hand, all
219 ;;; function objects are represented the same way regardless of the
220 ;;; arglists and return values, and apps don't get to ask things like
221 ;;; (TYPEP #'FOO (FUNCTION (FIXNUM) *)) in any meaningful way. On the
222 ;;; other hand, Python wants to reason about function types. So...
223 (!define-type-method (function :simple-subtypep) (type1 type2)
224 (flet ((fun-type-simple-p (type)
225 (not (or (fun-type-rest type)
226 (fun-type-keyp type))))
227 (every-csubtypep (types1 types2)
231 do (multiple-value-bind (res sure-p)
233 (unless res (return (values res sure-p))))
234 finally (return (values t t)))))
235 (and/type (values-subtypep (fun-type-returns type1)
236 (fun-type-returns type2))
237 (cond ((fun-type-wild-args type2) (values t t))
238 ((fun-type-wild-args type1)
239 (cond ((fun-type-keyp type2) (values nil nil))
240 ((not (fun-type-rest type2)) (values nil t))
241 ((not (null (fun-type-required type2)))
243 (t (and/type (type= *universal-type*
244 (fun-type-rest type2))
249 ((not (and (fun-type-simple-p type1)
250 (fun-type-simple-p type2)))
252 (t (multiple-value-bind (min1 max1) (fun-type-nargs type1)
253 (multiple-value-bind (min2 max2) (fun-type-nargs type2)
254 (cond ((or (> max1 max2) (< min1 min2))
256 ((and (= min1 min2) (= max1 max2))
257 (and/type (every-csubtypep
258 (fun-type-required type1)
259 (fun-type-required type2))
261 (fun-type-optional type1)
262 (fun-type-optional type2))))
265 (fun-type-required type1)
266 (fun-type-optional type1))
268 (fun-type-required type2)
269 (fun-type-optional type2))))))))))))
271 (!define-superclasses function ((function)) !cold-init-forms)
273 ;;; The union or intersection of two FUNCTION types is FUNCTION.
274 (!define-type-method (function :simple-union2) (type1 type2)
275 (declare (ignore type1 type2))
276 (specifier-type 'function))
277 (!define-type-method (function :simple-intersection2) (type1 type2)
278 (declare (ignore type1 type2))
279 (specifier-type 'function))
281 ;;; The union or intersection of a subclass of FUNCTION with a
282 ;;; FUNCTION type is somewhat complicated.
283 (!define-type-method (function :complex-intersection2) (type1 type2)
285 ((type= type1 (specifier-type 'function)) type2)
286 ((csubtypep type1 (specifier-type 'function)) nil)
287 (t :call-other-method)))
288 (!define-type-method (function :complex-union2) (type1 type2)
290 ((type= type1 (specifier-type 'function)) type1)
293 (!define-type-method (function :simple-=) (type1 type2)
294 (macrolet ((compare (comparator field)
295 (let ((reader (symbolicate '#:fun-type- field)))
296 `(,comparator (,reader type1) (,reader type2)))))
297 (and/type (compare type= returns)
298 (cond ((neq (fun-type-wild-args type1) (fun-type-wild-args type2))
300 ((eq (fun-type-wild-args type1) t)
303 (cond ((null (fun-type-rest type1))
304 (values (null (fun-type-rest type2)) t))
305 ((null (fun-type-rest type2))
308 (compare type= rest)))
309 (labels ((type-list-= (l1 l2)
311 (values (null l2) t))
314 (t (multiple-value-bind (res winp)
315 (type= (first l1) (first l2))
321 (type-list-= (rest l1)
323 (and/type (and/type (compare type-list-= required)
324 (compare type-list-= optional))
325 (if (or (fun-type-keyp type1) (fun-type-keyp type2))
327 (values t t))))))))))
329 (!define-type-class constant :inherits values)
331 (!define-type-method (constant :unparse) (type)
332 `(constant-arg ,(type-specifier (constant-type-type type))))
334 (!define-type-method (constant :simple-=) (type1 type2)
335 (type= (constant-type-type type1) (constant-type-type type2)))
337 (!def-type-translator constant-arg (type)
338 (make-constant-type :type (single-value-specifier-type type)))
340 ;;; Return the lambda-list-like type specification corresponding
342 (declaim (ftype (function (args-type) list) unparse-args-types))
343 (defun unparse-args-types (type)
346 (dolist (arg (args-type-required type))
347 (result (type-specifier arg)))
349 (when (args-type-optional type)
351 (dolist (arg (args-type-optional type))
352 (result (type-specifier arg))))
354 (when (args-type-rest type)
356 (result (type-specifier (args-type-rest type))))
358 (when (args-type-keyp type)
360 (dolist (key (args-type-keywords type))
361 (result (list (key-info-name key)
362 (type-specifier (key-info-type key))))))
364 (when (args-type-allowp type)
365 (result '&allow-other-keys))
369 (!def-type-translator function (&optional (args '*) (result '*))
370 (make-fun-type :args args
371 :returns (coerce-to-values (values-specifier-type result))))
373 (!def-type-translator values (&rest values)
374 (make-values-type :args values))
376 ;;;; VALUES types interfaces
378 ;;;; We provide a few special operations that can be meaningfully used
379 ;;;; on VALUES types (as well as on any other type).
381 (defun type-single-value-p (type)
382 (and (values-type-p type)
383 (not (values-type-rest type))
384 (null (values-type-optional type))
385 (singleton-p (values-type-required type))))
387 ;;; Return the type of the first value indicated by TYPE. This is used
388 ;;; by people who don't want to have to deal with VALUES types.
389 #!-sb-fluid (declaim (freeze-type values-type))
390 ; (inline single-value-type))
391 (defun single-value-type (type)
392 (declare (type ctype type))
393 (cond ((eq type *wild-type*)
395 ((eq type *empty-type*)
397 ((not (values-type-p type))
399 (t (or (car (args-type-required type))
400 (car (args-type-optional type))
401 (args-type-rest type)
402 (specifier-type 'null)))))
404 ;;; Return the minimum number of arguments that a function can be
405 ;;; called with, and the maximum number or NIL. If not a function
406 ;;; type, return NIL, NIL.
407 (defun fun-type-nargs (type)
408 (declare (type ctype type))
409 (if (and (fun-type-p type) (not (fun-type-wild-args type)))
410 (let ((fixed (length (args-type-required type))))
411 (if (or (args-type-rest type)
412 (args-type-keyp type)
413 (args-type-allowp type))
415 (values fixed (+ fixed (length (args-type-optional type))))))
418 ;;; Determine whether TYPE corresponds to a definite number of values.
419 ;;; The first value is a list of the types for each value, and the
420 ;;; second value is the number of values. If the number of values is
421 ;;; not fixed, then return NIL and :UNKNOWN.
422 (defun values-types (type)
423 (declare (type ctype type))
424 (cond ((or (eq type *wild-type*) (eq type *empty-type*))
425 (values nil :unknown))
426 ((or (args-type-optional type)
427 (args-type-rest type))
428 (values nil :unknown))
430 (let ((req (args-type-required type)))
431 (values req (length req))))))
433 ;;; Return two values:
434 ;;; 1. A list of all the positional (fixed and optional) types.
435 ;;; 2. The &REST type (if any). If no &REST, then the DEFAULT-TYPE.
436 (defun values-type-types (type &optional (default-type *empty-type*))
437 (declare (type ctype type))
438 (if (eq type *wild-type*)
439 (values nil *universal-type*)
440 (values (append (args-type-required type)
441 (args-type-optional type))
442 (cond ((args-type-rest type))
445 ;;; If COUNT values are supplied, which types should they have?
446 (defun values-type-start (type count)
447 (declare (type ctype type) (type unsigned-byte count))
448 (if (eq type *wild-type*)
449 (make-list count :initial-element *universal-type*)
451 (flet ((process-types (types)
452 (loop for type in types
456 (process-types (values-type-required type))
457 (process-types (values-type-optional type))
459 (loop with rest = (the ctype (values-type-rest type))
464 ;;; Return a list of OPERATION applied to the types in TYPES1 and
465 ;;; TYPES2, padding with REST2 as needed. TYPES1 must not be shorter
466 ;;; than TYPES2. The second value is T if OPERATION always returned a
467 ;;; true second value.
468 (defun fixed-values-op (types1 types2 rest2 operation)
469 (declare (list types1 types2) (type ctype rest2) (type function operation))
471 (values (mapcar (lambda (t1 t2)
472 (multiple-value-bind (res win)
473 (funcall operation t1 t2)
479 (make-list (- (length types1) (length types2))
480 :initial-element rest2)))
483 ;;; If TYPE isn't a values type, then make it into one.
484 (defun-cached (%coerce-to-values
486 :hash-function (lambda (type)
487 (logand (type-hash-value type)
490 (cond ((multiple-value-bind (res sure)
491 (csubtypep (specifier-type 'null) type)
492 (and (not res) sure))
493 ;; FIXME: What should we do with (NOT SURE)?
494 (make-values-type :required (list type) :rest *universal-type*))
496 (make-values-type :optional (list type) :rest *universal-type*))))
498 (defun coerce-to-values (type)
499 (declare (type ctype type))
500 (cond ((or (eq type *universal-type*)
501 (eq type *wild-type*))
503 ((values-type-p type)
505 (t (%coerce-to-values type))))
507 ;;; Return type, corresponding to ANSI short form of VALUES type
509 (defun make-short-values-type (types)
510 (declare (list types))
511 (let ((last-required (position-if
513 (not/type (csubtypep (specifier-type 'null) type)))
517 (make-values-type :required (subseq types 0 (1+ last-required))
518 :optional (subseq types (1+ last-required))
519 :rest *universal-type*)
520 (make-values-type :optional types :rest *universal-type*))))
522 (defun make-single-value-type (type)
523 (make-values-type :required (list type)))
525 ;;; Do the specified OPERATION on TYPE1 and TYPE2, which may be any
526 ;;; type, including VALUES types. With VALUES types such as:
529 ;;; we compute the more useful result
530 ;;; (VALUES (<operation> a0 b0) (<operation> a1 b1))
531 ;;; rather than the precise result
532 ;;; (<operation> (values a0 a1) (values b0 b1))
533 ;;; This has the virtue of always keeping the VALUES type specifier
534 ;;; outermost, and retains all of the information that is really
535 ;;; useful for static type analysis. We want to know what is always
536 ;;; true of each value independently. It is worthless to know that if
537 ;;; the first value is B0 then the second will be B1.
539 ;;; If the VALUES count signatures differ, then we produce a result with
540 ;;; the required VALUE count chosen by NREQ when applied to the number
541 ;;; of required values in TYPE1 and TYPE2. Any &KEY values become
542 ;;; &REST T (anyone who uses keyword values deserves to lose.)
544 ;;; The second value is true if the result is definitely empty or if
545 ;;; OPERATION returned true as its second value each time we called
546 ;;; it. Since we approximate the intersection of VALUES types, the
547 ;;; second value being true doesn't mean the result is exact.
548 (defun args-type-op (type1 type2 operation nreq)
549 (declare (type ctype type1 type2)
550 (type function operation nreq))
551 (when (eq type1 type2)
553 (multiple-value-bind (types1 rest1)
554 (values-type-types type1)
555 (multiple-value-bind (types2 rest2)
556 (values-type-types type2)
557 (multiple-value-bind (rest rest-exact)
558 (funcall operation rest1 rest2)
559 (multiple-value-bind (res res-exact)
560 (if (< (length types1) (length types2))
561 (fixed-values-op types2 types1 rest1 operation)
562 (fixed-values-op types1 types2 rest2 operation))
563 (let* ((req (funcall nreq
564 (length (args-type-required type1))
565 (length (args-type-required type2))))
566 (required (subseq res 0 req))
567 (opt (subseq res req)))
568 (values (make-values-type
572 (and rest-exact res-exact))))))))
574 ;;; Do a union or intersection operation on types that might be values
575 ;;; types. The result is optimized for utility rather than exactness,
576 ;;; but it is guaranteed that it will be no smaller (more restrictive)
577 ;;; than the precise result.
579 ;;; The return convention seems to be analogous to
580 ;;; TYPES-EQUAL-OR-INTERSECT. -- WHN 19990910.
581 (defun-cached (values-type-union :hash-function type-cache-hash
584 :init-wrapper !cold-init-forms)
585 ((type1 eq) (type2 eq))
586 (declare (type ctype type1 type2))
587 (cond ((or (eq type1 *wild-type*) (eq type2 *wild-type*)) *wild-type*)
588 ((eq type1 *empty-type*) type2)
589 ((eq type2 *empty-type*) type1)
591 (values (args-type-op type1 type2 #'type-union #'min)))))
593 (defun-cached (values-type-intersection :hash-function type-cache-hash
596 :default (values nil :empty)
597 :init-wrapper !cold-init-forms)
598 ((type1 eq) (type2 eq))
599 (declare (type ctype type1 type2))
600 (cond ((eq type1 *wild-type*) (values (coerce-to-values type2) t))
601 ((or (eq type2 *wild-type*) (eq type2 *universal-type*))
603 ((or (eq type1 *empty-type*) (eq type2 *empty-type*))
605 ((and (not (values-type-p type2))
606 (values-type-required type1))
607 (let ((req1 (values-type-required type1)))
608 (make-values-type :required (cons (type-intersection (first req1) type2)
610 :optional (values-type-optional type1)
611 :rest (values-type-rest type1)
612 :allowp (values-type-allowp type1))))
614 (args-type-op type1 (coerce-to-values type2)
618 ;;; This is like TYPES-EQUAL-OR-INTERSECT, except that it sort of
619 ;;; works on VALUES types. Note that due to the semantics of
620 ;;; VALUES-TYPE-INTERSECTION, this might return (VALUES T T) when
621 ;;; there isn't really any intersection.
622 (defun values-types-equal-or-intersect (type1 type2)
623 (cond ((or (eq type1 *empty-type*) (eq type2 *empty-type*))
625 ((or (eq type1 *wild-type*) (eq type2 *wild-type*))
628 (multiple-value-bind (res win) (values-type-intersection type1 type2)
629 (values (not (eq res *empty-type*))
632 ;;; a SUBTYPEP-like operation that can be used on any types, including
634 (defun-cached (values-subtypep :hash-function type-cache-hash
637 :default (values nil :empty)
638 :init-wrapper !cold-init-forms)
639 ((type1 eq) (type2 eq))
640 (declare (type ctype type1 type2))
641 (cond ((or (eq type2 *wild-type*) (eq type2 *universal-type*)
642 (eq type1 *empty-type*))
644 ((eq type1 *wild-type*)
645 (values (eq type2 *wild-type*) t))
646 ((or (eq type2 *empty-type*)
647 (not (values-types-equal-or-intersect type1 type2)))
649 ((and (not (values-type-p type2))
650 (values-type-required type1))
651 (csubtypep (first (values-type-required type1))
653 (t (setq type2 (coerce-to-values type2))
654 (multiple-value-bind (types1 rest1) (values-type-types type1)
655 (multiple-value-bind (types2 rest2) (values-type-types type2)
656 (cond ((< (length (values-type-required type1))
657 (length (values-type-required type2)))
659 ((< (length types1) (length types2))
662 (do ((t1 types1 (rest t1))
663 (t2 types2 (rest t2)))
665 (csubtypep rest1 rest2))
666 (multiple-value-bind (res win-p)
667 (csubtypep (first t1) (first t2))
669 (return (values nil nil)))
671 (return (values nil t))))))))))))
673 ;;;; type method interfaces
675 ;;; like SUBTYPEP, only works on CTYPE structures
676 (defun-cached (csubtypep :hash-function type-cache-hash
679 :default (values nil :empty)
680 :init-wrapper !cold-init-forms)
681 ((type1 eq) (type2 eq))
682 (declare (type ctype type1 type2))
683 (cond ((or (eq type1 type2)
684 (eq type1 *empty-type*)
685 (eq type2 *universal-type*))
688 ((eq type1 *universal-type*)
691 (!invoke-type-method :simple-subtypep :complex-subtypep-arg2
693 :complex-arg1 :complex-subtypep-arg1))))
695 ;;; Just parse the type specifiers and call CSUBTYPE.
696 (defun sb!xc:subtypep (type1 type2 &optional environment)
698 "Return two values indicating the relationship between type1 and type2.
699 If values are T and T, type1 definitely is a subtype of type2.
700 If values are NIL and T, type1 definitely is not a subtype of type2.
701 If values are NIL and NIL, it couldn't be determined."
702 (declare (ignore environment))
703 (csubtypep (specifier-type type1) (specifier-type type2)))
705 ;;; If two types are definitely equivalent, return true. The second
706 ;;; value indicates whether the first value is definitely correct.
707 ;;; This should only fail in the presence of HAIRY types.
708 (defun-cached (type= :hash-function type-cache-hash
711 :default (values nil :empty)
712 :init-wrapper !cold-init-forms)
713 ((type1 eq) (type2 eq))
714 (declare (type ctype type1 type2))
717 (!invoke-type-method :simple-= :complex-= type1 type2)))
719 ;;; Not exactly the negation of TYPE=, since when the relationship is
720 ;;; uncertain, we still return NIL, NIL. This is useful in cases where
721 ;;; the conservative assumption is =.
722 (defun type/= (type1 type2)
723 (declare (type ctype type1 type2))
724 (multiple-value-bind (res win) (type= type1 type2)
729 ;;; the type method dispatch case of TYPE-UNION2
730 (defun %type-union2 (type1 type2)
731 ;; As in %TYPE-INTERSECTION2, it seems to be a good idea to give
732 ;; both argument orders a chance at COMPLEX-INTERSECTION2. Unlike
733 ;; %TYPE-INTERSECTION2, though, I don't have a specific case which
734 ;; demonstrates this is actually necessary. Also unlike
735 ;; %TYPE-INTERSECTION2, there seems to be no need to distinguish
736 ;; between not finding a method and having a method return NIL.
738 (!invoke-type-method :simple-union2 :complex-union2
741 (declare (inline 1way))
742 (or (1way type1 type2)
743 (1way type2 type1))))
745 ;;; Find a type which includes both types. Any inexactness is
746 ;;; represented by the fuzzy element types; we return a single value
747 ;;; that is precise to the best of our knowledge. This result is
748 ;;; simplified into the canonical form, thus is not a UNION-TYPE
749 ;;; unless we find no other way to represent the result.
750 (defun-cached (type-union2 :hash-function type-cache-hash
752 :init-wrapper !cold-init-forms)
753 ((type1 eq) (type2 eq))
754 ;; KLUDGE: This was generated from TYPE-INTERSECTION2 by Ye Olde Cut And
755 ;; Paste technique of programming. If it stays around (as opposed to
756 ;; e.g. fading away in favor of some CLOS solution) the shared logic
757 ;; should probably become shared code. -- WHN 2001-03-16
758 (declare (type ctype type1 type2))
759 (cond ((eq type1 type2)
761 ((csubtypep type1 type2) type2)
762 ((csubtypep type2 type1) type1)
763 ((or (union-type-p type1)
764 (union-type-p type2))
765 ;; Unions of UNION-TYPE should have the UNION-TYPE-TYPES
766 ;; values broken out and united separately. The full TYPE-UNION
767 ;; function knows how to do this, so let it handle it.
768 (type-union type1 type2))
770 ;; the ordinary case: we dispatch to type methods
771 (%type-union2 type1 type2))))
773 ;;; the type method dispatch case of TYPE-INTERSECTION2
774 (defun %type-intersection2 (type1 type2)
775 ;; We want to give both argument orders a chance at
776 ;; COMPLEX-INTERSECTION2. Without that, the old CMU CL type
777 ;; methods could give noncommutative results, e.g.
778 ;; (TYPE-INTERSECTION2 *EMPTY-TYPE* SOME-HAIRY-TYPE)
780 ;; (TYPE-INTERSECTION2 SOME-HAIRY-TYPE *EMPTY-TYPE*)
781 ;; => #<NAMED-TYPE NIL>, T
782 ;; We also need to distinguish between the case where we found a
783 ;; type method, and it returned NIL, and the case where we fell
784 ;; through without finding any type method. An example of the first
785 ;; case is the intersection of a HAIRY-TYPE with some ordinary type.
786 ;; An example of the second case is the intersection of two
787 ;; completely-unrelated types, e.g. CONS and NUMBER, or SYMBOL and
790 ;; (Why yes, CLOS probably *would* be nicer..)
792 (!invoke-type-method :simple-intersection2 :complex-intersection2
794 :default :call-other-method)))
795 (declare (inline 1way))
796 (let ((xy (1way type1 type2)))
797 (or (and (not (eql xy :call-other-method)) xy)
798 (let ((yx (1way type2 type1)))
799 (or (and (not (eql yx :call-other-method)) yx)
800 (cond ((and (eql xy :call-other-method)
801 (eql yx :call-other-method))
804 (aver (and (not xy) (not yx))) ; else handled above
807 (defun-cached (type-intersection2 :hash-function type-cache-hash
811 :init-wrapper !cold-init-forms)
812 ((type1 eq) (type2 eq))
813 (declare (type ctype type1 type2))
814 (cond ((eq type1 type2)
815 ;; FIXME: For some reason, this doesn't catch e.g. type1 =
816 ;; type2 = (SPECIFIER-TYPE
817 ;; 'SOME-UNKNOWN-TYPE). Investigate. - CSR, 2002-04-10
819 ((or (intersection-type-p type1)
820 (intersection-type-p type2))
821 ;; Intersections of INTERSECTION-TYPE should have the
822 ;; INTERSECTION-TYPE-TYPES values broken out and intersected
823 ;; separately. The full TYPE-INTERSECTION function knows how
824 ;; to do that, so let it handle it.
825 (type-intersection type1 type2))
827 ;; the ordinary case: we dispatch to type methods
828 (%type-intersection2 type1 type2))))
830 ;;; Return as restrictive and simple a type as we can discover that is
831 ;;; no more restrictive than the intersection of TYPE1 and TYPE2. At
832 ;;; worst, we arbitrarily return one of the arguments as the first
833 ;;; value (trying not to return a hairy type).
834 (defun type-approx-intersection2 (type1 type2)
835 (cond ((type-intersection2 type1 type2))
836 ((hairy-type-p type1) type2)
839 ;;; a test useful for checking whether a derived type matches a
842 ;;; The first value is true unless the types don't intersect and
843 ;;; aren't equal. The second value is true if the first value is
844 ;;; definitely correct. NIL is considered to intersect with any type.
845 ;;; If T is a subtype of either type, then we also return T, T. This
846 ;;; way we recognize that hairy types might intersect with T.
847 (defun types-equal-or-intersect (type1 type2)
848 (declare (type ctype type1 type2))
849 (if (or (eq type1 *empty-type*) (eq type2 *empty-type*))
851 (let ((intersection2 (type-intersection2 type1 type2)))
852 (cond ((not intersection2)
853 (if (or (csubtypep *universal-type* type1)
854 (csubtypep *universal-type* type2))
857 ((eq intersection2 *empty-type*) (values nil t))
860 ;;; Return a Common Lisp type specifier corresponding to the TYPE
862 (defun type-specifier (type)
863 (declare (type ctype type))
864 (funcall (type-class-unparse (type-class-info type)) type))
866 ;;; (VALUES-SPECIFIER-TYPE and SPECIFIER-TYPE moved from here to
867 ;;; early-type.lisp by WHN ca. 19990201.)
869 ;;; Take a list of type specifiers, computing the translation of each
870 ;;; specifier and defining it as a builtin type.
871 (declaim (ftype (function (list) (values)) precompute-types))
872 (defun precompute-types (specs)
874 (let ((res (specifier-type spec)))
875 (unless (unknown-type-p res)
876 (setf (info :type :builtin spec) res)
877 ;; KLUDGE: the three copies of this idiom in this file (and
878 ;; the one in class.lisp as at sbcl-0.7.4.1x) should be
879 ;; coalesced, or perhaps the error-detecting code that
880 ;; disallows redefinition of :PRIMITIVE types should be
881 ;; rewritten to use *TYPE-SYSTEM-FINALIZED* (rather than
882 ;; *TYPE-SYSTEM-INITIALIZED*). The effect of this is not to
883 ;; cause redefinition errors when precompute-types is called
884 ;; for a second time while building the target compiler using
885 ;; the cross-compiler. -- CSR, trying to explain why this
886 ;; isn't completely wrong, 2002-06-07
887 (setf (info :type :kind spec) #+sb-xc-host :defined #-sb-xc-host :primitive))))
890 ;;;; general TYPE-UNION and TYPE-INTERSECTION operations
892 ;;;; These are fully general operations on CTYPEs: they'll always
893 ;;;; return a CTYPE representing the result.
895 ;;; shared logic for unions and intersections: Return a vector of
896 ;;; types representing the same types as INPUT-TYPES, but with
897 ;;; COMPOUND-TYPEs satisfying %COMPOUND-TYPE-P broken up into their
898 ;;; component types, and with any SIMPLY2 simplifications applied.
899 (declaim (inline simplified-compound-types))
900 (defun simplified-compound-types (input-types %compound-type-p simplify2)
901 (declare (function %compound-type-p simplify2))
902 (let ((types (make-array (length input-types)
905 :element-type 'ctype)))
906 (labels ((accumulate-compound-type (type)
907 (if (funcall %compound-type-p type)
908 (dolist (type (compound-type-types type))
909 (accumulate1-compound-type type))
910 (accumulate1-compound-type type)))
911 (accumulate1-compound-type (type)
912 (declare (type ctype type))
913 ;; Any input object satisfying %COMPOUND-TYPE-P should've been
914 ;; broken into components before it reached us.
915 (aver (not (funcall %compound-type-p type)))
916 (dotimes (i (length types) (vector-push-extend type types))
917 (let ((simplified2 (funcall simplify2 type (aref types i))))
919 ;; Discard the old (AREF TYPES I).
920 (setf (aref types i) (vector-pop types))
921 ;; Merge the new SIMPLIFIED2 into TYPES, by tail recursing.
922 ;; (Note that the tail recursion is indirect: we go through
923 ;; ACCUMULATE, not ACCUMULATE1, so that if SIMPLIFIED2 is
924 ;; handled properly if it satisfies %COMPOUND-TYPE-P.)
925 (return (accumulate-compound-type simplified2)))))))
926 (dolist (input-type input-types)
927 (accumulate-compound-type input-type)))
930 ;;; shared logic for unions and intersections: Make a COMPOUND-TYPE
931 ;;; object whose components are the types in TYPES, or skip to special
932 ;;; cases when TYPES is short.
933 (defun make-probably-compound-type (constructor types enumerable identity)
934 (declare (type function constructor))
935 (declare (type (vector ctype) types))
936 (declare (type ctype identity))
940 (t (funcall constructor
942 ;; FIXME: This should be just (COERCE TYPES 'LIST), but as
943 ;; of sbcl-0.6.11.17 the COERCE optimizer is really
944 ;; brain-dead, so that would generate a full call to
945 ;; SPECIFIER-TYPE at runtime, so we get into bootstrap
946 ;; problems in cold init because 'LIST is a compound
947 ;; type, so we need to MAKE-PROBABLY-COMPOUND-TYPE
948 ;; before we know what 'LIST is. Once the COERCE
949 ;; optimizer is less brain-dead, we can make this
950 ;; (COERCE TYPES 'LIST) again.
951 #+sb-xc-host (coerce types 'list)
952 #-sb-xc-host (coerce-to-list types)))))
954 (defun maybe-distribute-one-union (union-type types)
955 (let* ((intersection (apply #'type-intersection types))
956 (union (mapcar (lambda (x) (type-intersection x intersection))
957 (union-type-types union-type))))
958 (if (notany (lambda (x) (or (hairy-type-p x)
959 (intersection-type-p x)))
964 (defun type-intersection (&rest input-types)
965 (%type-intersection input-types))
966 (defun-cached (%type-intersection :hash-bits 8
967 :hash-function (lambda (x)
968 (logand (sxhash x) #xff)))
969 ((input-types equal))
970 (let ((simplified-types (simplified-compound-types input-types
971 #'intersection-type-p
972 #'type-intersection2)))
973 (declare (type (vector ctype) simplified-types))
974 ;; We want to have a canonical representation of types (or failing
975 ;; that, punt to HAIRY-TYPE). Canonical representation would have
976 ;; intersections inside unions but not vice versa, since you can
977 ;; always achieve that by the distributive rule. But we don't want
978 ;; to just apply the distributive rule, since it would be too easy
979 ;; to end up with unreasonably huge type expressions. So instead
980 ;; we try to generate a simple type by distributing the union; if
981 ;; the type can't be made simple, we punt to HAIRY-TYPE.
982 (if (and (> (length simplified-types) 1)
983 (some #'union-type-p simplified-types))
984 (let* ((first-union (find-if #'union-type-p simplified-types))
985 (other-types (coerce (remove first-union simplified-types)
987 (distributed (maybe-distribute-one-union first-union
990 (apply #'type-union distributed)
992 :specifier `(and ,@(map 'list
994 simplified-types)))))
995 (make-probably-compound-type #'%make-intersection-type
997 (some #'type-enumerable
1001 (defun type-union (&rest input-types)
1002 (%type-union input-types))
1003 (defun-cached (%type-union :hash-bits 8
1004 :hash-function (lambda (x)
1005 (logand (sxhash x) #xff)))
1006 ((input-types equal))
1007 (let ((simplified-types (simplified-compound-types input-types
1010 (make-probably-compound-type #'make-union-type
1012 (every #'type-enumerable simplified-types)
1017 (!define-type-class named)
1019 (defvar *wild-type*)
1020 (defvar *empty-type*)
1021 (defvar *universal-type*)
1022 (defvar *universal-fun-type*)
1025 (macrolet ((frob (name var)
1027 (setq ,var (make-named-type :name ',name))
1028 (setf (info :type :kind ',name)
1029 #+sb-xc-host :defined #-sb-xc-host :primitive)
1030 (setf (info :type :builtin ',name) ,var))))
1031 ;; KLUDGE: In ANSI, * isn't really the name of a type, it's just a
1032 ;; special symbol which can be stuck in some places where an
1033 ;; ordinary type can go, e.g. (ARRAY * 1) instead of (ARRAY T 1).
1034 ;; In SBCL it also used to denote universal VALUES type.
1035 (frob * *wild-type*)
1036 (frob nil *empty-type*)
1037 (frob t *universal-type*))
1038 (setf *universal-fun-type*
1039 (make-fun-type :wild-args t
1040 :returns *wild-type*)))
1042 (!define-type-method (named :simple-=) (type1 type2)
1043 ;;(aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1044 (values (eq type1 type2) t))
1046 (!define-type-method (named :complex-=) (type1 type2)
1048 ((and (eq type2 *empty-type*)
1049 (intersection-type-p type1)
1050 ;; not allowed to be unsure on these... FIXME: keep the list
1051 ;; of CL types that are intersection types once and only
1053 (not (or (type= type1 (specifier-type 'ratio))
1054 (type= type1 (specifier-type 'keyword)))))
1055 ;; things like (AND (EQL 0) (SATISFIES ODDP)) or (AND FUNCTION
1056 ;; STREAM) can get here. In general, we can't really tell
1057 ;; whether these are equal to NIL or not, so
1059 ((type-might-contain-other-types-p type1)
1060 (invoke-complex-=-other-method type1 type2))
1061 (t (values nil t))))
1063 (!define-type-method (named :simple-subtypep) (type1 type2)
1064 (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1065 (values (or (eq type1 *empty-type*) (eq type2 *wild-type*)) t))
1067 (!define-type-method (named :complex-subtypep-arg1) (type1 type2)
1068 ;; This AVER causes problems if we write accurate methods for the
1069 ;; union (and possibly intersection) types which then delegate to
1070 ;; us; while a user shouldn't get here, because of the odd status of
1071 ;; *wild-type* a type-intersection executed by the compiler can. -
1074 ;; (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1075 (cond ((eq type1 *empty-type*)
1077 (;; When TYPE2 might be the universal type in disguise
1078 (type-might-contain-other-types-p type2)
1079 ;; Now that the UNION and HAIRY COMPLEX-SUBTYPEP-ARG2 methods
1080 ;; can delegate to us (more or less as CALL-NEXT-METHOD) when
1081 ;; they're uncertain, we can't just barf on COMPOUND-TYPE and
1082 ;; HAIRY-TYPEs as we used to. Instead we deal with the
1083 ;; problem (where at least part of the problem is cases like
1084 ;; (SUBTYPEP T '(SATISFIES FOO))
1086 ;; (SUBTYPEP T '(AND (SATISFIES FOO) (SATISFIES BAR)))
1087 ;; where the second type is a hairy type like SATISFIES, or
1088 ;; is a compound type which might contain a hairy type) by
1089 ;; returning uncertainty.
1092 ;; By elimination, TYPE1 is the universal type.
1093 (aver (eq type1 *universal-type*))
1094 ;; This case would have been picked off by the SIMPLE-SUBTYPEP
1095 ;; method, and so shouldn't appear here.
1096 (aver (not (eq type2 *universal-type*)))
1097 ;; Since TYPE2 is not EQ *UNIVERSAL-TYPE* and is not the
1098 ;; universal type in disguise, TYPE2 is not a superset of TYPE1.
1101 (!define-type-method (named :complex-subtypep-arg2) (type1 type2)
1102 (aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1103 (cond ((eq type2 *universal-type*)
1105 ((type-might-contain-other-types-p type1)
1106 ;; those types can be *EMPTY-TYPE* or *UNIVERSAL-TYPE* in
1107 ;; disguise. So we'd better delegate.
1108 (invoke-complex-subtypep-arg1-method type1 type2))
1110 ;; FIXME: This seems to rely on there only being 2 or 3
1111 ;; NAMED-TYPE values, and the exclusion of various
1112 ;; possibilities above. It would be good to explain it and/or
1113 ;; rewrite it so that it's clearer.
1114 (values (not (eq type2 *empty-type*)) t))))
1116 (!define-type-method (named :complex-intersection2) (type1 type2)
1117 ;; FIXME: This assertion failed when I added it in sbcl-0.6.11.13.
1118 ;; Perhaps when bug 85 is fixed it can be reenabled.
1119 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1120 (hierarchical-intersection2 type1 type2))
1122 (!define-type-method (named :complex-union2) (type1 type2)
1123 ;; Perhaps when bug 85 is fixed this can be reenabled.
1124 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1125 (hierarchical-union2 type1 type2))
1127 (!define-type-method (named :unparse) (x)
1128 (named-type-name x))
1130 ;;;; hairy and unknown types
1132 (!define-type-method (hairy :unparse) (x)
1133 (hairy-type-specifier x))
1135 (!define-type-method (hairy :simple-subtypep) (type1 type2)
1136 (let ((hairy-spec1 (hairy-type-specifier type1))
1137 (hairy-spec2 (hairy-type-specifier type2)))
1138 (cond ((equal-but-no-car-recursion hairy-spec1 hairy-spec2)
1141 (values nil nil)))))
1143 (!define-type-method (hairy :complex-subtypep-arg2) (type1 type2)
1144 (invoke-complex-subtypep-arg1-method type1 type2))
1146 (!define-type-method (hairy :complex-subtypep-arg1) (type1 type2)
1147 (declare (ignore type1 type2))
1150 (!define-type-method (hairy :complex-=) (type1 type2)
1151 (if (unknown-type-p type2)
1152 (let ((type2 (specifier-type (unknown-type-specifier type2))))
1153 (if (unknown-type-p type2)
1155 (type= type1 type2)))
1158 (!define-type-method (hairy :simple-intersection2 :complex-intersection2)
1160 (if (type= type1 type2)
1164 (!define-type-method (hairy :simple-union2)
1166 (if (type= type1 type2)
1170 (!define-type-method (hairy :simple-=) (type1 type2)
1171 (if (equal-but-no-car-recursion (hairy-type-specifier type1)
1172 (hairy-type-specifier type2))
1176 (!def-type-translator satisfies (&whole whole fun)
1177 (declare (ignore fun))
1178 ;; Check legality of arguments.
1179 (destructuring-bind (satisfies predicate-name) whole
1180 (declare (ignore satisfies))
1181 (unless (symbolp predicate-name)
1182 (error 'simple-type-error
1183 :datum predicate-name
1184 :expected-type 'symbol
1185 :format-control "The SATISFIES predicate name is not a symbol: ~S"
1186 :format-arguments (list predicate-name))))
1188 (make-hairy-type :specifier whole))
1192 (!define-type-method (negation :unparse) (x)
1193 `(not ,(type-specifier (negation-type-type x))))
1195 (!define-type-method (negation :simple-subtypep) (type1 type2)
1196 (csubtypep (negation-type-type type2) (negation-type-type type1)))
1198 (!define-type-method (negation :complex-subtypep-arg2) (type1 type2)
1199 (let* ((complement-type2 (negation-type-type type2))
1200 (intersection2 (type-intersection2 type1
1203 ;; FIXME: if uncertain, maybe try arg1?
1204 (type= intersection2 *empty-type*)
1205 (invoke-complex-subtypep-arg1-method type1 type2))))
1207 (!define-type-method (negation :complex-subtypep-arg1) (type1 type2)
1208 ;; "Incrementally extended heuristic algorithms tend inexorably toward the
1209 ;; incomprehensible." -- http://www.unlambda.com/~james/lambda/lambda.txt
1211 ;; You may not believe this. I couldn't either. But then I sat down
1212 ;; and drew lots of Venn diagrams. Comments involving a and b refer
1213 ;; to the call (subtypep '(not a) 'b) -- CSR, 2002-02-27.
1215 ;; (Several logical truths in this block are true as long as
1216 ;; b/=T. As of sbcl-0.7.1.28, it seems impossible to construct a
1217 ;; case with b=T where we actually reach this type method, but
1218 ;; we'll test for and exclude this case anyway, since future
1219 ;; maintenance might make it possible for it to end up in this
1221 (multiple-value-bind (equal certain)
1222 (type= type2 *universal-type*)
1224 (return (values nil nil)))
1226 (return (values t t))))
1227 (let ((complement-type1 (negation-type-type type1)))
1228 ;; Do the special cases first, in order to give us a chance if
1229 ;; subtype/supertype relationships are hairy.
1230 (multiple-value-bind (equal certain)
1231 (type= complement-type1 type2)
1232 ;; If a = b, ~a is not a subtype of b (unless b=T, which was
1235 (return (values nil nil)))
1237 (return (values nil t))))
1238 ;; KLUDGE: ANSI requires that the SUBTYPEP result between any
1239 ;; two built-in atomic type specifiers never be uncertain. This
1240 ;; is hard to do cleanly for the built-in types whose
1241 ;; definitions include (NOT FOO), i.e. CONS and RATIO. However,
1242 ;; we can do it with this hack, which uses our global knowledge
1243 ;; that our implementation of the type system uses disjoint
1244 ;; implementation types to represent disjoint sets (except when
1245 ;; types are contained in other types). (This is a KLUDGE
1246 ;; because it's fragile. Various changes in internal
1247 ;; representation in the type system could make it start
1248 ;; confidently returning incorrect results.) -- WHN 2002-03-08
1249 (unless (or (type-might-contain-other-types-p complement-type1)
1250 (type-might-contain-other-types-p type2))
1251 ;; Because of the way our types which don't contain other
1252 ;; types are disjoint subsets of the space of possible values,
1253 ;; (SUBTYPEP '(NOT AA) 'B)=NIL when AA and B are simple (and B
1254 ;; is not T, as checked above).
1255 (return (values nil t)))
1256 ;; The old (TYPE= TYPE1 TYPE2) branch would never be taken, as
1257 ;; TYPE1 and TYPE2 will only be equal if they're both NOT types,
1258 ;; and then the :SIMPLE-SUBTYPEP method would be used instead.
1259 ;; But a CSUBTYPEP relationship might still hold:
1260 (multiple-value-bind (equal certain)
1261 (csubtypep complement-type1 type2)
1262 ;; If a is a subtype of b, ~a is not a subtype of b (unless
1263 ;; b=T, which was excluded above).
1265 (return (values nil nil)))
1267 (return (values nil t))))
1268 (multiple-value-bind (equal certain)
1269 (csubtypep type2 complement-type1)
1270 ;; If b is a subtype of a, ~a is not a subtype of b. (FIXME:
1271 ;; That's not true if a=T. Do we know at this point that a is
1274 (return (values nil nil)))
1276 (return (values nil t))))
1277 ;; old CSR comment ca. 0.7.2, now obsoleted by the SIMPLE-CTYPE?
1278 ;; KLUDGE case above: Other cases here would rely on being able
1279 ;; to catch all possible cases, which the fragility of this type
1280 ;; system doesn't inspire me; for instance, if a is type= to ~b,
1281 ;; then we want T, T; if this is not the case and the types are
1282 ;; disjoint (have an intersection of *empty-type*) then we want
1283 ;; NIL, T; else if the union of a and b is the *universal-type*
1284 ;; then we want T, T. So currently we still claim to be unsure
1285 ;; about e.g. (subtypep '(not fixnum) 'single-float).
1287 ;; OTOH we might still get here:
1290 (!define-type-method (negation :complex-=) (type1 type2)
1291 ;; (NOT FOO) isn't equivalent to anything that's not a negation
1292 ;; type, except possibly a type that might contain it in disguise.
1293 (declare (ignore type2))
1294 (if (type-might-contain-other-types-p type1)
1298 (!define-type-method (negation :simple-intersection2) (type1 type2)
1299 (let ((not1 (negation-type-type type1))
1300 (not2 (negation-type-type type2)))
1302 ((csubtypep not1 not2) type2)
1303 ((csubtypep not2 not1) type1)
1304 ;; Why no analagous clause to the disjoint in the SIMPLE-UNION2
1305 ;; method, below? The clause would read
1307 ;; ((EQ (TYPE-UNION NOT1 NOT2) *UNIVERSAL-TYPE*) *EMPTY-TYPE*)
1309 ;; but with proper canonicalization of negation types, there's
1310 ;; no way of constructing two negation types with union of their
1311 ;; negations being the universal type.
1313 (aver (not (eq (type-union not1 not2) *universal-type*)))
1316 (!define-type-method (negation :complex-intersection2) (type1 type2)
1318 ((csubtypep type1 (negation-type-type type2)) *empty-type*)
1319 ((eq (type-intersection type1 (negation-type-type type2)) *empty-type*)
1323 (!define-type-method (negation :simple-union2) (type1 type2)
1324 (let ((not1 (negation-type-type type1))
1325 (not2 (negation-type-type type2)))
1327 ((csubtypep not1 not2) type1)
1328 ((csubtypep not2 not1) type2)
1329 ((eq (type-intersection not1 not2) *empty-type*)
1333 (!define-type-method (negation :complex-union2) (type1 type2)
1335 ((csubtypep (negation-type-type type2) type1) *universal-type*)
1336 ((eq (type-intersection type1 (negation-type-type type2)) *empty-type*)
1340 (!define-type-method (negation :simple-=) (type1 type2)
1341 (type= (negation-type-type type1) (negation-type-type type2)))
1343 (!def-type-translator not (typespec)
1344 (let* ((not-type (specifier-type typespec))
1345 (spec (type-specifier not-type)))
1347 ;; canonicalize (NOT (NOT FOO))
1348 ((and (listp spec) (eq (car spec) 'not))
1349 (specifier-type (cadr spec)))
1350 ;; canonicalize (NOT NIL) and (NOT T)
1351 ((eq not-type *empty-type*) *universal-type*)
1352 ((eq not-type *universal-type*) *empty-type*)
1353 ((and (numeric-type-p not-type)
1354 (null (numeric-type-low not-type))
1355 (null (numeric-type-high not-type)))
1356 (make-negation-type :type not-type))
1357 ((numeric-type-p not-type)
1360 :type (modified-numeric-type not-type :low nil :high nil))
1362 ((null (numeric-type-low not-type))
1363 (modified-numeric-type
1365 :low (let ((h (numeric-type-high not-type)))
1366 (if (consp h) (car h) (list h)))
1368 ((null (numeric-type-high not-type))
1369 (modified-numeric-type
1372 :high (let ((l (numeric-type-low not-type)))
1373 (if (consp l) (car l) (list l)))))
1375 (modified-numeric-type
1378 :high (let ((l (numeric-type-low not-type)))
1379 (if (consp l) (car l) (list l))))
1380 (modified-numeric-type
1382 :low (let ((h (numeric-type-high not-type)))
1383 (if (consp h) (car h) (list h)))
1385 ((intersection-type-p not-type)
1387 (mapcar #'(lambda (x)
1388 (specifier-type `(not ,(type-specifier x))))
1389 (intersection-type-types not-type))))
1390 ((union-type-p not-type)
1391 (apply #'type-intersection
1392 (mapcar #'(lambda (x)
1393 (specifier-type `(not ,(type-specifier x))))
1394 (union-type-types not-type))))
1395 ((member-type-p not-type)
1396 (let ((members (member-type-members not-type)))
1397 (if (some #'floatp members)
1399 (dolist (pair `((0.0f0 . ,(load-time-value (make-unportable-float :single-float-negative-zero)))
1400 (0.0d0 . ,(load-time-value (make-unportable-float :double-float-negative-zero)))
1402 (0.0l0 . ,(load-time-value (make-unportable-float :long-float-negative-zero)))))
1403 (when (member (car pair) members)
1404 (aver (not (member (cdr pair) members)))
1405 (push (cdr pair) floats)
1406 (setf members (remove (car pair) members)))
1407 (when (member (cdr pair) members)
1408 (aver (not (member (car pair) members)))
1409 (push (car pair) floats)
1410 (setf members (remove (cdr pair) members))))
1411 (apply #'type-intersection
1415 :type (make-member-type :members members)))
1418 (let ((type (ctype-of x)))
1421 :type (modified-numeric-type type
1422 :low nil :high nil))
1423 (modified-numeric-type type
1424 :low nil :high (list x))
1425 (make-member-type :members (list x))
1426 (modified-numeric-type type
1427 :low (list x) :high nil))))
1429 (make-negation-type :type not-type))))
1430 ((and (cons-type-p not-type)
1431 (eq (cons-type-car-type not-type) *universal-type*)
1432 (eq (cons-type-cdr-type not-type) *universal-type*))
1433 (make-negation-type :type not-type))
1434 ((cons-type-p not-type)
1436 (make-negation-type :type (specifier-type 'cons))
1438 ((and (not (eq (cons-type-car-type not-type) *universal-type*))
1439 (not (eq (cons-type-cdr-type not-type) *universal-type*)))
1442 (specifier-type `(not ,(type-specifier
1443 (cons-type-car-type not-type))))
1447 (specifier-type `(not ,(type-specifier
1448 (cons-type-cdr-type not-type)))))))
1449 ((not (eq (cons-type-car-type not-type) *universal-type*))
1451 (specifier-type `(not ,(type-specifier
1452 (cons-type-car-type not-type))))
1454 ((not (eq (cons-type-cdr-type not-type) *universal-type*))
1457 (specifier-type `(not ,(type-specifier
1458 (cons-type-cdr-type not-type))))))
1459 (t (bug "Weird CONS type ~S" not-type)))))
1460 (t (make-negation-type :type not-type)))))
1464 (!define-type-class number)
1466 (!define-type-method (number :simple-=) (type1 type2)
1468 (and (eq (numeric-type-class type1) (numeric-type-class type2))
1469 (eq (numeric-type-format type1) (numeric-type-format type2))
1470 (eq (numeric-type-complexp type1) (numeric-type-complexp type2))
1471 (equalp (numeric-type-low type1) (numeric-type-low type2))
1472 (equalp (numeric-type-high type1) (numeric-type-high type2)))
1475 (!define-type-method (number :unparse) (type)
1476 (let* ((complexp (numeric-type-complexp type))
1477 (low (numeric-type-low type))
1478 (high (numeric-type-high type))
1479 (base (case (numeric-type-class type)
1481 (rational 'rational)
1482 (float (or (numeric-type-format type) 'float))
1485 (cond ((and (eq base 'integer) high low)
1486 (let ((high-count (logcount high))
1487 (high-length (integer-length high)))
1489 (cond ((= high 0) '(integer 0 0))
1491 ((and (= high-count high-length)
1492 (plusp high-length))
1493 `(unsigned-byte ,high-length))
1495 `(mod ,(1+ high)))))
1496 ((and (= low sb!xc:most-negative-fixnum)
1497 (= high sb!xc:most-positive-fixnum))
1499 ((and (= low (lognot high))
1500 (= high-count high-length)
1502 `(signed-byte ,(1+ high-length)))
1504 `(integer ,low ,high)))))
1505 (high `(,base ,(or low '*) ,high))
1507 (if (and (eq base 'integer) (= low 0))
1515 (if (eq base+bounds 'real)
1517 `(complex ,base+bounds)))
1519 (aver (eq base+bounds 'real))
1522 ;;; Return true if X is "less than or equal" to Y, taking open bounds
1523 ;;; into consideration. CLOSED is the predicate used to test the bound
1524 ;;; on a closed interval (e.g. <=), and OPEN is the predicate used on
1525 ;;; open bounds (e.g. <). Y is considered to be the outside bound, in
1526 ;;; the sense that if it is infinite (NIL), then the test succeeds,
1527 ;;; whereas if X is infinite, then the test fails (unless Y is also
1530 ;;; This is for comparing bounds of the same kind, e.g. upper and
1531 ;;; upper. Use NUMERIC-BOUND-TEST* for different kinds of bounds.
1532 (defmacro numeric-bound-test (x y closed open)
1537 (,closed (car ,x) (car ,y))
1538 (,closed (car ,x) ,y)))
1544 ;;; This is used to compare upper and lower bounds. This is different
1545 ;;; from the same-bound case:
1546 ;;; -- Since X = NIL is -infinity, whereas y = NIL is +infinity, we
1547 ;;; return true if *either* arg is NIL.
1548 ;;; -- an open inner bound is "greater" and also squeezes the interval,
1549 ;;; causing us to use the OPEN test for those cases as well.
1550 (defmacro numeric-bound-test* (x y closed open)
1555 (,open (car ,x) (car ,y))
1556 (,open (car ,x) ,y)))
1562 ;;; Return whichever of the numeric bounds X and Y is "maximal"
1563 ;;; according to the predicates CLOSED (e.g. >=) and OPEN (e.g. >).
1564 ;;; This is only meaningful for maximizing like bounds, i.e. upper and
1565 ;;; upper. If MAX-P is true, then we return NIL if X or Y is NIL,
1566 ;;; otherwise we return the other arg.
1567 (defmacro numeric-bound-max (x y closed open max-p)
1570 `(cond ((not ,n-x) ,(if max-p nil n-y))
1571 ((not ,n-y) ,(if max-p nil n-x))
1574 (if (,closed (car ,n-x) (car ,n-y)) ,n-x ,n-y)
1575 (if (,open (car ,n-x) ,n-y) ,n-x ,n-y)))
1578 (if (,open (car ,n-y) ,n-x) ,n-y ,n-x)
1579 (if (,closed ,n-y ,n-x) ,n-y ,n-x))))))
1581 (!define-type-method (number :simple-subtypep) (type1 type2)
1582 (let ((class1 (numeric-type-class type1))
1583 (class2 (numeric-type-class type2))
1584 (complexp2 (numeric-type-complexp type2))
1585 (format2 (numeric-type-format type2))
1586 (low1 (numeric-type-low type1))
1587 (high1 (numeric-type-high type1))
1588 (low2 (numeric-type-low type2))
1589 (high2 (numeric-type-high type2)))
1590 ;; If one is complex and the other isn't, they are disjoint.
1591 (cond ((not (or (eq (numeric-type-complexp type1) complexp2)
1594 ;; If the classes are specified and different, the types are
1595 ;; disjoint unless type2 is RATIONAL and type1 is INTEGER.
1596 ;; [ or type1 is INTEGER and type2 is of the form (RATIONAL
1597 ;; X X) for integral X, but this is dealt with in the
1598 ;; canonicalization inside MAKE-NUMERIC-TYPE ]
1599 ((not (or (eq class1 class2)
1601 (and (eq class1 'integer) (eq class2 'rational))))
1603 ;; If the float formats are specified and different, the types
1605 ((not (or (eq (numeric-type-format type1) format2)
1608 ;; Check the bounds.
1609 ((and (numeric-bound-test low1 low2 >= >)
1610 (numeric-bound-test high1 high2 <= <))
1615 (!define-superclasses number ((number)) !cold-init-forms)
1617 ;;; If the high bound of LOW is adjacent to the low bound of HIGH,
1618 ;;; then return true, otherwise NIL.
1619 (defun numeric-types-adjacent (low high)
1620 (let ((low-bound (numeric-type-high low))
1621 (high-bound (numeric-type-low high)))
1622 (cond ((not (and low-bound high-bound)) nil)
1623 ((and (consp low-bound) (consp high-bound)) nil)
1625 (let ((low-value (car low-bound)))
1626 (or (eql low-value high-bound)
1628 (load-time-value (make-unportable-float
1629 :single-float-negative-zero)))
1630 (eql high-bound 0f0))
1631 (and (eql low-value 0f0)
1633 (load-time-value (make-unportable-float
1634 :single-float-negative-zero))))
1636 (load-time-value (make-unportable-float
1637 :double-float-negative-zero)))
1638 (eql high-bound 0d0))
1639 (and (eql low-value 0d0)
1641 (load-time-value (make-unportable-float
1642 :double-float-negative-zero)))))))
1644 (let ((high-value (car high-bound)))
1645 (or (eql high-value low-bound)
1646 (and (eql high-value
1647 (load-time-value (make-unportable-float
1648 :single-float-negative-zero)))
1649 (eql low-bound 0f0))
1650 (and (eql high-value 0f0)
1652 (load-time-value (make-unportable-float
1653 :single-float-negative-zero))))
1654 (and (eql high-value
1655 (load-time-value (make-unportable-float
1656 :double-float-negative-zero)))
1657 (eql low-bound 0d0))
1658 (and (eql high-value 0d0)
1660 (load-time-value (make-unportable-float
1661 :double-float-negative-zero)))))))
1662 ((and (eq (numeric-type-class low) 'integer)
1663 (eq (numeric-type-class high) 'integer))
1664 (eql (1+ low-bound) high-bound))
1668 ;;; Return a numeric type that is a supertype for both TYPE1 and TYPE2.
1670 ;;; Old comment, probably no longer applicable:
1672 ;;; ### Note: we give up early to keep from dropping lots of
1673 ;;; information on the floor by returning overly general types.
1674 (!define-type-method (number :simple-union2) (type1 type2)
1675 (declare (type numeric-type type1 type2))
1676 (cond ((csubtypep type1 type2) type2)
1677 ((csubtypep type2 type1) type1)
1679 (let ((class1 (numeric-type-class type1))
1680 (format1 (numeric-type-format type1))
1681 (complexp1 (numeric-type-complexp type1))
1682 (class2 (numeric-type-class type2))
1683 (format2 (numeric-type-format type2))
1684 (complexp2 (numeric-type-complexp type2)))
1686 ((and (eq class1 class2)
1687 (eq format1 format2)
1688 (eq complexp1 complexp2)
1689 (or (numeric-types-intersect type1 type2)
1690 (numeric-types-adjacent type1 type2)
1691 (numeric-types-adjacent type2 type1)))
1696 :low (numeric-bound-max (numeric-type-low type1)
1697 (numeric-type-low type2)
1699 :high (numeric-bound-max (numeric-type-high type1)
1700 (numeric-type-high type2)
1702 ;; FIXME: These two clauses are almost identical, and the
1703 ;; consequents are in fact identical in every respect.
1704 ((and (eq class1 'rational)
1705 (eq class2 'integer)
1706 (eq format1 format2)
1707 (eq complexp1 complexp2)
1708 (integerp (numeric-type-low type2))
1709 (integerp (numeric-type-high type2))
1710 (= (numeric-type-low type2) (numeric-type-high type2))
1711 (or (numeric-types-adjacent type1 type2)
1712 (numeric-types-adjacent type2 type1)))
1717 :low (numeric-bound-max (numeric-type-low type1)
1718 (numeric-type-low type2)
1720 :high (numeric-bound-max (numeric-type-high type1)
1721 (numeric-type-high type2)
1723 ((and (eq class1 'integer)
1724 (eq class2 'rational)
1725 (eq format1 format2)
1726 (eq complexp1 complexp2)
1727 (integerp (numeric-type-low type1))
1728 (integerp (numeric-type-high type1))
1729 (= (numeric-type-low type1) (numeric-type-high type1))
1730 (or (numeric-types-adjacent type1 type2)
1731 (numeric-types-adjacent type2 type1)))
1736 :low (numeric-bound-max (numeric-type-low type1)
1737 (numeric-type-low type2)
1739 :high (numeric-bound-max (numeric-type-high type1)
1740 (numeric-type-high type2)
1746 (setf (info :type :kind 'number)
1747 #+sb-xc-host :defined #-sb-xc-host :primitive)
1748 (setf (info :type :builtin 'number)
1749 (make-numeric-type :complexp nil)))
1751 (!def-type-translator complex (&optional (typespec '*))
1752 (if (eq typespec '*)
1753 (make-numeric-type :complexp :complex)
1754 (labels ((not-numeric ()
1755 (error "The component type for COMPLEX is not numeric: ~S"
1758 (error "The component type for COMPLEX is not real: ~S"
1760 (complex1 (component-type)
1761 (unless (numeric-type-p component-type)
1763 (when (eq (numeric-type-complexp component-type) :complex)
1765 (modified-numeric-type component-type :complexp :complex))
1766 (complex-union (component)
1767 (unless (numberp component)
1769 ;; KLUDGE: This TYPECASE more or less does
1770 ;; (UPGRADED-COMPLEX-PART-TYPE (TYPE-OF COMPONENT)),
1771 ;; (plus a small hack to treat (EQL COMPONENT 0) specially)
1772 ;; but uses logic cut and pasted from the DEFUN of
1773 ;; UPGRADED-COMPLEX-PART-TYPE. That's fragile, because
1774 ;; changing the definition of UPGRADED-COMPLEX-PART-TYPE
1775 ;; would tend to break the code here. Unfortunately,
1776 ;; though, reusing UPGRADED-COMPLEX-PART-TYPE here
1777 ;; would cause another kind of fragility, because
1778 ;; ANSI's definition of TYPE-OF is so weak that e.g.
1779 ;; (UPGRADED-COMPLEX-PART-TYPE (TYPE-OF 1/2)) could
1780 ;; end up being (UPGRADED-COMPLEX-PART-TYPE 'REAL)
1781 ;; instead of (UPGRADED-COMPLEX-PART-TYPE 'RATIONAL).
1782 ;; So using TYPE-OF would mean that ANSI-conforming
1783 ;; maintenance changes in TYPE-OF could break the code here.
1784 ;; It's not clear how best to fix this. -- WHN 2002-01-21,
1785 ;; trying to summarize CSR's concerns in his patch
1787 (complex (error "The component type for COMPLEX (EQL X) ~
1790 ((eql 0) (specifier-type nil)) ; as required by ANSI
1791 (single-float (specifier-type '(complex single-float)))
1792 (double-float (specifier-type '(complex double-float)))
1794 (long-float (specifier-type '(complex long-float)))
1795 (rational (specifier-type '(complex rational)))
1796 (t (specifier-type '(complex real))))))
1797 (let ((ctype (specifier-type typespec)))
1799 (numeric-type (complex1 ctype))
1800 (union-type (apply #'type-union
1801 ;; FIXME: This code could suffer from
1802 ;; (admittedly very obscure) cases of
1803 ;; bug 145 e.g. when TYPE is
1804 ;; (OR (AND INTEGER (SATISFIES ODDP))
1805 ;; (AND FLOAT (SATISFIES FOO))
1806 ;; and not even report the problem very well.
1808 (union-type-types ctype))))
1809 ;; MEMBER-TYPE is almost the same as UNION-TYPE, but
1810 ;; there's a gotcha: (COMPLEX (EQL 0)) is, according to
1811 ;; ANSI, equal to type NIL, the empty set.
1812 (member-type (apply #'type-union
1813 (mapcar #'complex-union
1814 (member-type-members ctype))))
1816 (multiple-value-bind (subtypep certainly)
1817 (csubtypep ctype (specifier-type 'real))
1818 (if (and (not subtypep) certainly)
1820 ;; ANSI just says that TYPESPEC is any subtype of
1821 ;; type REAL, not necessarily a NUMERIC-TYPE. In
1822 ;; particular, at this point TYPESPEC could legally be
1823 ;; an intersection type like (AND REAL (SATISFIES ODDP)),
1824 ;; in which case we fall through the logic above and
1825 ;; end up here, stumped.
1826 (bug "~@<(known bug #145): The type ~S is too hairy to be
1827 used for a COMPLEX component.~:@>"
1830 ;;; If X is *, return NIL, otherwise return the bound, which must be a
1831 ;;; member of TYPE or a one-element list of a member of TYPE.
1832 #!-sb-fluid (declaim (inline canonicalized-bound))
1833 (defun canonicalized-bound (bound type)
1834 (cond ((eq bound '*) nil)
1835 ((or (sb!xc:typep bound type)
1837 (sb!xc:typep (car bound) type)
1838 (null (cdr bound))))
1841 (error "Bound is not ~S, a ~S or a list of a ~S: ~S"
1847 (!def-type-translator integer (&optional (low '*) (high '*))
1848 (let* ((l (canonicalized-bound low 'integer))
1849 (lb (if (consp l) (1+ (car l)) l))
1850 (h (canonicalized-bound high 'integer))
1851 (hb (if (consp h) (1- (car h)) h)))
1852 (if (and hb lb (< hb lb))
1854 (make-numeric-type :class 'integer
1856 :enumerable (not (null (and l h)))
1860 (defmacro !def-bounded-type (type class format)
1861 `(!def-type-translator ,type (&optional (low '*) (high '*))
1862 (let ((lb (canonicalized-bound low ',type))
1863 (hb (canonicalized-bound high ',type)))
1864 (if (not (numeric-bound-test* lb hb <= <))
1866 (make-numeric-type :class ',class
1871 (!def-bounded-type rational rational nil)
1873 ;;; Unlike CMU CL, we represent the types FLOAT and REAL as
1874 ;;; UNION-TYPEs of more primitive types, in order to make
1875 ;;; type representation more unique, avoiding problems in the
1876 ;;; simplification of things like
1877 ;;; (subtypep '(or (single-float -1.0 1.0) (single-float 0.1))
1878 ;;; '(or (real -1 7) (single-float 0.1) (single-float -1.0 1.0)))
1879 ;;; When we allowed REAL to remain as a separate NUMERIC-TYPE,
1880 ;;; it was too easy for the first argument to be simplified to
1881 ;;; '(SINGLE-FLOAT -1.0), and for the second argument to be simplified
1882 ;;; to '(OR (REAL -1 7) (SINGLE-FLOAT 0.1)) and then for the
1883 ;;; SUBTYPEP to fail (returning NIL,T instead of T,T) because
1884 ;;; the first argument can't be seen to be a subtype of any of the
1885 ;;; terms in the second argument.
1887 ;;; The old CMU CL way was:
1888 ;;; (!def-bounded-type float float nil)
1889 ;;; (!def-bounded-type real nil nil)
1891 ;;; FIXME: If this new way works for a while with no weird new
1892 ;;; problems, we can go back and rip out support for separate FLOAT
1893 ;;; and REAL flavors of NUMERIC-TYPE. The new way was added in
1894 ;;; sbcl-0.6.11.22, 2001-03-21.
1896 ;;; FIXME: It's probably necessary to do something to fix the
1897 ;;; analogous problem with INTEGER and RATIONAL types. Perhaps
1898 ;;; bounded RATIONAL types should be represented as (OR RATIO INTEGER).
1899 (defun coerce-bound (bound type inner-coerce-bound-fun)
1900 (declare (type function inner-coerce-bound-fun))
1901 (cond ((eql bound '*)
1904 (destructuring-bind (inner-bound) bound
1905 (list (funcall inner-coerce-bound-fun inner-bound type))))
1907 (funcall inner-coerce-bound-fun bound type))))
1908 (defun inner-coerce-real-bound (bound type)
1910 (rational (rationalize bound))
1911 (float (if (floatp bound)
1913 ;; Coerce to the widest float format available, to
1914 ;; avoid unnecessary loss of precision:
1915 (coerce bound 'long-float)))))
1916 (defun coerced-real-bound (bound type)
1917 (coerce-bound bound type #'inner-coerce-real-bound))
1918 (defun coerced-float-bound (bound type)
1919 (coerce-bound bound type #'coerce))
1920 (!def-type-translator real (&optional (low '*) (high '*))
1921 (specifier-type `(or (float ,(coerced-real-bound low 'float)
1922 ,(coerced-real-bound high 'float))
1923 (rational ,(coerced-real-bound low 'rational)
1924 ,(coerced-real-bound high 'rational)))))
1925 (!def-type-translator float (&optional (low '*) (high '*))
1927 `(or (single-float ,(coerced-float-bound low 'single-float)
1928 ,(coerced-float-bound high 'single-float))
1929 (double-float ,(coerced-float-bound low 'double-float)
1930 ,(coerced-float-bound high 'double-float))
1931 #!+long-float ,(error "stub: no long float support yet"))))
1933 (defmacro !define-float-format (f)
1934 `(!def-bounded-type ,f float ,f))
1936 (!define-float-format short-float)
1937 (!define-float-format single-float)
1938 (!define-float-format double-float)
1939 (!define-float-format long-float)
1941 (defun numeric-types-intersect (type1 type2)
1942 (declare (type numeric-type type1 type2))
1943 (let* ((class1 (numeric-type-class type1))
1944 (class2 (numeric-type-class type2))
1945 (complexp1 (numeric-type-complexp type1))
1946 (complexp2 (numeric-type-complexp type2))
1947 (format1 (numeric-type-format type1))
1948 (format2 (numeric-type-format type2))
1949 (low1 (numeric-type-low type1))
1950 (high1 (numeric-type-high type1))
1951 (low2 (numeric-type-low type2))
1952 (high2 (numeric-type-high type2)))
1953 ;; If one is complex and the other isn't, then they are disjoint.
1954 (cond ((not (or (eq complexp1 complexp2)
1955 (null complexp1) (null complexp2)))
1957 ;; If either type is a float, then the other must either be
1958 ;; specified to be a float or unspecified. Otherwise, they
1960 ((and (eq class1 'float)
1961 (not (member class2 '(float nil)))) nil)
1962 ((and (eq class2 'float)
1963 (not (member class1 '(float nil)))) nil)
1964 ;; If the float formats are specified and different, the
1965 ;; types are disjoint.
1966 ((not (or (eq format1 format2) (null format1) (null format2)))
1969 ;; Check the bounds. This is a bit odd because we must
1970 ;; always have the outer bound of the interval as the
1972 (if (numeric-bound-test high1 high2 <= <)
1973 (or (and (numeric-bound-test low1 low2 >= >)
1974 (numeric-bound-test* low1 high2 <= <))
1975 (and (numeric-bound-test low2 low1 >= >)
1976 (numeric-bound-test* low2 high1 <= <)))
1977 (or (and (numeric-bound-test* low2 high1 <= <)
1978 (numeric-bound-test low2 low1 >= >))
1979 (and (numeric-bound-test high2 high1 <= <)
1980 (numeric-bound-test* high2 low1 >= >))))))))
1982 ;;; Take the numeric bound X and convert it into something that can be
1983 ;;; used as a bound in a numeric type with the specified CLASS and
1984 ;;; FORMAT. If UP-P is true, then we round up as needed, otherwise we
1985 ;;; round down. UP-P true implies that X is a lower bound, i.e. (N) > N.
1987 ;;; This is used by NUMERIC-TYPE-INTERSECTION to mash the bound into
1988 ;;; the appropriate type number. X may only be a float when CLASS is
1991 ;;; ### Note: it is possible for the coercion to a float to overflow
1992 ;;; or underflow. This happens when the bound doesn't fit in the
1993 ;;; specified format. In this case, we should really return the
1994 ;;; appropriate {Most | Least}-{Positive | Negative}-XXX-Float float
1995 ;;; of desired format. But these conditions aren't currently signalled
1996 ;;; in any useful way.
1998 ;;; Also, when converting an open rational bound into a float we
1999 ;;; should probably convert it to a closed bound of the closest float
2000 ;;; in the specified format. KLUDGE: In general, open float bounds are
2001 ;;; screwed up. -- (comment from original CMU CL)
2002 (defun round-numeric-bound (x class format up-p)
2004 (let ((cx (if (consp x) (car x) x)))
2008 (if (and (consp x) (integerp cx))
2009 (if up-p (1+ cx) (1- cx))
2010 (if up-p (ceiling cx) (floor cx))))
2012 (let ((res (if format (coerce cx format) (float cx))))
2013 (if (consp x) (list res) res)))))
2016 ;;; Handle the case of type intersection on two numeric types. We use
2017 ;;; TYPES-EQUAL-OR-INTERSECT to throw out the case of types with no
2018 ;;; intersection. If an attribute in TYPE1 is unspecified, then we use
2019 ;;; TYPE2's attribute, which must be at least as restrictive. If the
2020 ;;; types intersect, then the only attributes that can be specified
2021 ;;; and different are the class and the bounds.
2023 ;;; When the class differs, we use the more restrictive class. The
2024 ;;; only interesting case is RATIONAL/INTEGER, since RATIONAL includes
2027 ;;; We make the result lower (upper) bound the maximum (minimum) of
2028 ;;; the argument lower (upper) bounds. We convert the bounds into the
2029 ;;; appropriate numeric type before maximizing. This avoids possible
2030 ;;; confusion due to mixed-type comparisons (but I think the result is
2032 (!define-type-method (number :simple-intersection2) (type1 type2)
2033 (declare (type numeric-type type1 type2))
2034 (if (numeric-types-intersect type1 type2)
2035 (let* ((class1 (numeric-type-class type1))
2036 (class2 (numeric-type-class type2))
2037 (class (ecase class1
2039 ((integer float) class1)
2040 (rational (if (eq class2 'integer)
2043 (format (or (numeric-type-format type1)
2044 (numeric-type-format type2))))
2048 :complexp (or (numeric-type-complexp type1)
2049 (numeric-type-complexp type2))
2050 :low (numeric-bound-max
2051 (round-numeric-bound (numeric-type-low type1)
2053 (round-numeric-bound (numeric-type-low type2)
2056 :high (numeric-bound-max
2057 (round-numeric-bound (numeric-type-high type1)
2059 (round-numeric-bound (numeric-type-high type2)
2064 ;;; Given two float formats, return the one with more precision. If
2065 ;;; either one is null, return NIL.
2066 (defun float-format-max (f1 f2)
2068 (dolist (f *float-formats* (error "bad float format: ~S" f1))
2069 (when (or (eq f f1) (eq f f2))
2072 ;;; Return the result of an operation on TYPE1 and TYPE2 according to
2073 ;;; the rules of numeric contagion. This is always NUMBER, some float
2074 ;;; format (possibly complex) or RATIONAL. Due to rational
2075 ;;; canonicalization, there isn't much we can do here with integers or
2076 ;;; rational complex numbers.
2078 ;;; If either argument is not a NUMERIC-TYPE, then return NUMBER. This
2079 ;;; is useful mainly for allowing types that are technically numbers,
2080 ;;; but not a NUMERIC-TYPE.
2081 (defun numeric-contagion (type1 type2)
2082 (if (and (numeric-type-p type1) (numeric-type-p type2))
2083 (let ((class1 (numeric-type-class type1))
2084 (class2 (numeric-type-class type2))
2085 (format1 (numeric-type-format type1))
2086 (format2 (numeric-type-format type2))
2087 (complexp1 (numeric-type-complexp type1))
2088 (complexp2 (numeric-type-complexp type2)))
2089 (cond ((or (null complexp1)
2091 (specifier-type 'number))
2095 :format (ecase class2
2096 (float (float-format-max format1 format2))
2097 ((integer rational) format1)
2099 ;; A double-float with any real number is a
2102 (if (eq format1 'double-float)
2105 ;; A long-float with any real number is a
2108 (if (eq format1 'long-float)
2111 :complexp (if (or (eq complexp1 :complex)
2112 (eq complexp2 :complex))
2115 ((eq class2 'float) (numeric-contagion type2 type1))
2116 ((and (eq complexp1 :real) (eq complexp2 :real))
2118 :class (and class1 class2 'rational)
2121 (specifier-type 'number))))
2122 (specifier-type 'number)))
2126 (!define-type-class array)
2128 ;;; What this does depends on the setting of the
2129 ;;; *USE-IMPLEMENTATION-TYPES* switch. If true, return the specialized
2130 ;;; element type, otherwise return the original element type.
2131 (defun specialized-element-type-maybe (type)
2132 (declare (type array-type type))
2133 (if *use-implementation-types*
2134 (array-type-specialized-element-type type)
2135 (array-type-element-type type)))
2137 (!define-type-method (array :simple-=) (type1 type2)
2138 (if (or (unknown-type-p (array-type-element-type type1))
2139 (unknown-type-p (array-type-element-type type2)))
2140 (multiple-value-bind (equalp certainp)
2141 (type= (array-type-element-type type1)
2142 (array-type-element-type type2))
2143 ;; by its nature, the call to TYPE= should never return NIL,
2144 ;; T, as we don't know what the UNKNOWN-TYPE will grow up to
2145 ;; be. -- CSR, 2002-08-19
2146 (aver (not (and (not equalp) certainp)))
2147 (values equalp certainp))
2148 (values (and (equal (array-type-dimensions type1)
2149 (array-type-dimensions type2))
2150 (eq (array-type-complexp type1)
2151 (array-type-complexp type2))
2152 (type= (specialized-element-type-maybe type1)
2153 (specialized-element-type-maybe type2)))
2156 (!define-type-method (array :unparse) (type)
2157 (let ((dims (array-type-dimensions type))
2158 (eltype (type-specifier (array-type-element-type type)))
2159 (complexp (array-type-complexp type)))
2162 (if complexp 'array 'simple-array)
2163 (if complexp `(array ,eltype) `(simple-array ,eltype))))
2164 ((= (length dims) 1)
2166 (if (eq (car dims) '*)
2169 (base-char 'base-string)
2172 (t `(vector ,eltype)))
2174 (bit `(bit-vector ,(car dims)))
2175 (base-char `(base-string ,(car dims)))
2176 (character `(string ,(car dims)))
2177 (t `(vector ,eltype ,(car dims)))))
2178 (if (eq (car dims) '*)
2180 (bit 'simple-bit-vector)
2181 (base-char 'simple-base-string)
2182 (character 'simple-string)
2183 ((t) 'simple-vector)
2184 (t `(simple-array ,eltype (*))))
2186 (bit `(simple-bit-vector ,(car dims)))
2187 (base-char `(simple-base-string ,(car dims)))
2188 (character `(simple-string ,(car dims)))
2189 ((t) `(simple-vector ,(car dims)))
2190 (t `(simple-array ,eltype ,dims))))))
2193 `(array ,eltype ,dims)
2194 `(simple-array ,eltype ,dims))))))
2196 (!define-type-method (array :simple-subtypep) (type1 type2)
2197 (let ((dims1 (array-type-dimensions type1))
2198 (dims2 (array-type-dimensions type2))
2199 (complexp2 (array-type-complexp type2)))
2200 (cond (;; not subtypep unless dimensions are compatible
2201 (not (or (eq dims2 '*)
2202 (and (not (eq dims1 '*))
2203 ;; (sbcl-0.6.4 has trouble figuring out that
2204 ;; DIMS1 and DIMS2 must be lists at this
2205 ;; point, and knowing that is important to
2206 ;; compiling EVERY efficiently.)
2207 (= (length (the list dims1))
2208 (length (the list dims2)))
2209 (every (lambda (x y)
2210 (or (eq y '*) (eql x y)))
2212 (the list dims2)))))
2214 ;; not subtypep unless complexness is compatible
2215 ((not (or (eq complexp2 :maybe)
2216 (eq (array-type-complexp type1) complexp2)))
2218 ;; Since we didn't fail any of the tests above, we win
2219 ;; if the TYPE2 element type is wild.
2220 ((eq (array-type-element-type type2) *wild-type*)
2222 (;; Since we didn't match any of the special cases above, we
2223 ;; can't give a good answer unless both the element types
2224 ;; have been defined.
2225 (or (unknown-type-p (array-type-element-type type1))
2226 (unknown-type-p (array-type-element-type type2)))
2228 (;; Otherwise, the subtype relationship holds iff the
2229 ;; types are equal, and they're equal iff the specialized
2230 ;; element types are identical.
2232 (values (type= (specialized-element-type-maybe type1)
2233 (specialized-element-type-maybe type2))
2236 (!define-superclasses array
2242 (defun array-types-intersect (type1 type2)
2243 (declare (type array-type type1 type2))
2244 (let ((dims1 (array-type-dimensions type1))
2245 (dims2 (array-type-dimensions type2))
2246 (complexp1 (array-type-complexp type1))
2247 (complexp2 (array-type-complexp type2)))
2248 ;; See whether dimensions are compatible.
2249 (cond ((not (or (eq dims1 '*) (eq dims2 '*)
2250 (and (= (length dims1) (length dims2))
2251 (every (lambda (x y)
2252 (or (eq x '*) (eq y '*) (= x y)))
2255 ;; See whether complexpness is compatible.
2256 ((not (or (eq complexp1 :maybe)
2257 (eq complexp2 :maybe)
2258 (eq complexp1 complexp2)))
2262 ;; If either element type is wild, then they intersect.
2263 ;; Otherwise, the types must be identical.
2265 ;; FIXME: There seems to have been a fair amount of
2266 ;; confusion about the distinction between requested element
2267 ;; type and specialized element type; here is one of
2268 ;; them. If we request an array to hold objects of an
2269 ;; unknown type, we can do no better than represent that
2270 ;; type as an array specialized on wild-type. We keep the
2271 ;; requested element-type in the -ELEMENT-TYPE slot, and
2272 ;; *WILD-TYPE* in the -SPECIALIZED-ELEMENT-TYPE. So, here,
2273 ;; we must test for the SPECIALIZED slot being *WILD-TYPE*,
2274 ;; not just the ELEMENT-TYPE slot. Maybe the return value
2275 ;; in that specific case should be T, NIL? Or maybe this
2276 ;; function should really be called
2277 ;; ARRAY-TYPES-COULD-POSSIBLY-INTERSECT? In any case, this
2278 ;; was responsible for bug #123, and this whole issue could
2279 ;; do with a rethink and/or a rewrite. -- CSR, 2002-08-21
2280 ((or (eq (array-type-specialized-element-type type1) *wild-type*)
2281 (eq (array-type-specialized-element-type type2) *wild-type*)
2282 (type= (specialized-element-type-maybe type1)
2283 (specialized-element-type-maybe type2)))
2289 (!define-type-method (array :simple-intersection2) (type1 type2)
2290 (declare (type array-type type1 type2))
2291 (if (array-types-intersect type1 type2)
2292 (let ((dims1 (array-type-dimensions type1))
2293 (dims2 (array-type-dimensions type2))
2294 (complexp1 (array-type-complexp type1))
2295 (complexp2 (array-type-complexp type2))
2296 (eltype1 (array-type-element-type type1))
2297 (eltype2 (array-type-element-type type2)))
2298 (specialize-array-type
2300 :dimensions (cond ((eq dims1 '*) dims2)
2301 ((eq dims2 '*) dims1)
2303 (mapcar (lambda (x y) (if (eq x '*) y x))
2305 :complexp (if (eq complexp1 :maybe) complexp2 complexp1)
2307 ((eq eltype1 *wild-type*) eltype2)
2308 ((eq eltype2 *wild-type*) eltype1)
2309 (t (type-intersection eltype1 eltype2))))))
2312 ;;; Check a supplied dimension list to determine whether it is legal,
2313 ;;; and return it in canonical form (as either '* or a list).
2314 (defun canonical-array-dimensions (dims)
2319 (error "Arrays can't have a negative number of dimensions: ~S" dims))
2320 (when (>= dims sb!xc:array-rank-limit)
2321 (error "array type with too many dimensions: ~S" dims))
2322 (make-list dims :initial-element '*))
2324 (when (>= (length dims) sb!xc:array-rank-limit)
2325 (error "array type with too many dimensions: ~S" dims))
2328 (unless (and (integerp dim)
2330 (< dim sb!xc:array-dimension-limit))
2331 (error "bad dimension in array type: ~S" dim))))
2334 (error "Array dimensions is not a list, integer or *:~% ~S" dims))))
2338 (!define-type-class member)
2340 (!define-type-method (member :unparse) (type)
2341 (let ((members (member-type-members type)))
2343 ((equal members '(nil)) 'null)
2344 ((type= type (specifier-type 'standard-char)) 'standard-char)
2345 (t `(member ,@members)))))
2347 (!define-type-method (member :simple-subtypep) (type1 type2)
2348 (values (subsetp (member-type-members type1) (member-type-members type2))
2351 (!define-type-method (member :complex-subtypep-arg1) (type1 type2)
2352 (every/type (swapped-args-fun #'ctypep)
2354 (member-type-members type1)))
2356 ;;; We punt if the odd type is enumerable and intersects with the
2357 ;;; MEMBER type. If not enumerable, then it is definitely not a
2358 ;;; subtype of the MEMBER type.
2359 (!define-type-method (member :complex-subtypep-arg2) (type1 type2)
2360 (cond ((not (type-enumerable type1)) (values nil t))
2361 ((types-equal-or-intersect type1 type2)
2362 (invoke-complex-subtypep-arg1-method type1 type2))
2363 (t (values nil t))))
2365 (!define-type-method (member :simple-intersection2) (type1 type2)
2366 (let ((mem1 (member-type-members type1))
2367 (mem2 (member-type-members type2)))
2368 (cond ((subsetp mem1 mem2) type1)
2369 ((subsetp mem2 mem1) type2)
2371 (let ((res (intersection mem1 mem2)))
2373 (make-member-type :members res)
2376 (!define-type-method (member :complex-intersection2) (type1 type2)
2378 (collect ((members))
2379 (let ((mem2 (member-type-members type2)))
2380 (dolist (member mem2)
2381 (multiple-value-bind (val win) (ctypep member type1)
2383 (return-from punt nil))
2384 (when val (members member))))
2385 (cond ((subsetp mem2 (members)) type2)
2386 ((null (members)) *empty-type*)
2388 (make-member-type :members (members))))))))
2390 ;;; We don't need a :COMPLEX-UNION2, since the only interesting case is
2391 ;;; a union type, and the member/union interaction is handled by the
2392 ;;; union type method.
2393 (!define-type-method (member :simple-union2) (type1 type2)
2394 (let ((mem1 (member-type-members type1))
2395 (mem2 (member-type-members type2)))
2396 (cond ((subsetp mem1 mem2) type2)
2397 ((subsetp mem2 mem1) type1)
2399 (make-member-type :members (union mem1 mem2))))))
2401 (!define-type-method (member :simple-=) (type1 type2)
2402 (let ((mem1 (member-type-members type1))
2403 (mem2 (member-type-members type2)))
2404 (values (and (subsetp mem1 mem2)
2405 (subsetp mem2 mem1))
2408 (!define-type-method (member :complex-=) (type1 type2)
2409 (if (type-enumerable type1)
2410 (multiple-value-bind (val win) (csubtypep type2 type1)
2411 (if (or val (not win))
2416 (!def-type-translator member (&rest members)
2419 (dolist (m (remove-duplicates members))
2421 (float (if (zerop m)
2423 (push (ctype-of m) numbers)))
2424 (number (push (ctype-of m) numbers))
2428 (make-member-type :members ms)
2430 (nreverse numbers)))
2433 ;;;; intersection types
2435 ;;;; Until version 0.6.10.6, SBCL followed the original CMU CL approach
2436 ;;;; of punting on all AND types, not just the unreasonably complicated
2437 ;;;; ones. The change was motivated by trying to get the KEYWORD type
2438 ;;;; to behave sensibly:
2439 ;;;; ;; reasonable definition
2440 ;;;; (DEFTYPE KEYWORD () '(AND SYMBOL (SATISFIES KEYWORDP)))
2441 ;;;; ;; reasonable behavior
2442 ;;;; (AVER (SUBTYPEP 'KEYWORD 'SYMBOL))
2443 ;;;; Without understanding a little about the semantics of AND, we'd
2444 ;;;; get (SUBTYPEP 'KEYWORD 'SYMBOL)=>NIL,NIL and, for entirely
2445 ;;;; parallel reasons, (SUBTYPEP 'RATIO 'NUMBER)=>NIL,NIL. That's
2448 ;;;; We still follow the example of CMU CL to some extent, by punting
2449 ;;;; (to the opaque HAIRY-TYPE) on sufficiently complicated types
2452 (!define-type-class intersection)
2454 ;;; A few intersection types have special names. The others just get
2455 ;;; mechanically unparsed.
2456 (!define-type-method (intersection :unparse) (type)
2457 (declare (type ctype type))
2458 (or (find type '(ratio keyword) :key #'specifier-type :test #'type=)
2459 `(and ,@(mapcar #'type-specifier (intersection-type-types type)))))
2461 ;;; shared machinery for type equality: true if every type in the set
2462 ;;; TYPES1 matches a type in the set TYPES2 and vice versa
2463 (defun type=-set (types1 types2)
2464 (flet ((type<=-set (x y)
2465 (declare (type list x y))
2466 (every/type (lambda (x y-element)
2467 (any/type #'type= y-element x))
2469 (and/type (type<=-set types1 types2)
2470 (type<=-set types2 types1))))
2472 ;;; Two intersection types are equal if their subtypes are equal sets.
2474 ;;; FIXME: Might it be better to use
2475 ;;; (AND (SUBTYPEP X Y) (SUBTYPEP Y X))
2476 ;;; instead, since SUBTYPEP is the usual relationship that we care
2477 ;;; most about, so it would be good to leverage any ingenuity there
2478 ;;; in this more obscure method?
2479 (!define-type-method (intersection :simple-=) (type1 type2)
2480 (type=-set (intersection-type-types type1)
2481 (intersection-type-types type2)))
2483 (defun %intersection-complex-subtypep-arg1 (type1 type2)
2484 (type= type1 (type-intersection type1 type2)))
2486 (defun %intersection-simple-subtypep (type1 type2)
2487 (every/type #'%intersection-complex-subtypep-arg1
2489 (intersection-type-types type2)))
2491 (!define-type-method (intersection :simple-subtypep) (type1 type2)
2492 (%intersection-simple-subtypep type1 type2))
2494 (!define-type-method (intersection :complex-subtypep-arg1) (type1 type2)
2495 (%intersection-complex-subtypep-arg1 type1 type2))
2497 (defun %intersection-complex-subtypep-arg2 (type1 type2)
2498 (every/type #'csubtypep type1 (intersection-type-types type2)))
2500 (!define-type-method (intersection :complex-subtypep-arg2) (type1 type2)
2501 (%intersection-complex-subtypep-arg2 type1 type2))
2503 ;;; FIXME: This will look eeriely familiar to readers of the UNION
2504 ;;; :SIMPLE-INTERSECTION2 :COMPLEX-INTERSECTION2 method. That's
2505 ;;; because it was generated by cut'n'paste methods. Given that
2506 ;;; intersections and unions have all sorts of symmetries known to
2507 ;;; mathematics, it shouldn't be beyond the ken of some programmers to
2508 ;;; reflect those symmetries in code in a way that ties them together
2509 ;;; more strongly than having two independent near-copies :-/
2510 (!define-type-method (intersection :simple-union2 :complex-union2)
2512 ;; Within this method, type2 is guaranteed to be an intersection
2514 (aver (intersection-type-p type2))
2515 ;; Make sure to call only the applicable methods...
2516 (cond ((and (intersection-type-p type1)
2517 (%intersection-simple-subtypep type1 type2)) type2)
2518 ((and (intersection-type-p type1)
2519 (%intersection-simple-subtypep type2 type1)) type1)
2520 ((and (not (intersection-type-p type1))
2521 (%intersection-complex-subtypep-arg2 type1 type2))
2523 ((and (not (intersection-type-p type1))
2524 (%intersection-complex-subtypep-arg1 type2 type1))
2526 ;; KLUDGE: This special (and somewhat hairy) magic is required
2527 ;; to deal with the RATIONAL/INTEGER special case. The UNION
2528 ;; of (INTEGER * -1) and (AND (RATIONAL * -1/2) (NOT INTEGER))
2529 ;; should be (RATIONAL * -1/2) -- CSR, 2003-02-28
2530 ((and (csubtypep type2 (specifier-type 'ratio))
2531 (numeric-type-p type1)
2532 (csubtypep type1 (specifier-type 'integer))
2537 :low (if (null (numeric-type-low type1))
2539 (list (1- (numeric-type-low type1))))
2540 :high (if (null (numeric-type-high type1))
2542 (list (1+ (numeric-type-high type1)))))))
2544 (apply #'type-intersection
2545 (remove (specifier-type '(not integer))
2546 (intersection-type-types type2)
2549 (let ((accumulator *universal-type*))
2550 (do ((t2s (intersection-type-types type2) (cdr t2s)))
2551 ((null t2s) accumulator)
2552 (let ((union (type-union type1 (car t2s))))
2553 (when (union-type-p union)
2554 ;; we have to give up here -- there are all sorts of
2555 ;; ordering worries, but it's better than before.
2556 ;; Doing exactly the same as in the UNION
2557 ;; :SIMPLE/:COMPLEX-INTERSECTION2 method causes stack
2558 ;; overflow with the mutual recursion never bottoming
2560 (if (and (eq accumulator *universal-type*)
2562 ;; KLUDGE: if we get here, we have a partially
2563 ;; simplified result. While this isn't by any
2564 ;; means a universal simplification, including
2565 ;; this logic here means that we can get (OR
2566 ;; KEYWORD (NOT KEYWORD)) canonicalized to T.
2570 (type-intersection accumulator union))))))))
2572 (!def-type-translator and (&whole whole &rest type-specifiers)
2573 (apply #'type-intersection
2574 (mapcar #'specifier-type
2579 (!define-type-class union)
2581 ;;; The LIST, FLOAT and REAL types have special names. Other union
2582 ;;; types just get mechanically unparsed.
2583 (!define-type-method (union :unparse) (type)
2584 (declare (type ctype type))
2586 ((type= type (specifier-type 'list)) 'list)
2587 ((type= type (specifier-type 'float)) 'float)
2588 ((type= type (specifier-type 'real)) 'real)
2589 ((type= type (specifier-type 'sequence)) 'sequence)
2590 ((type= type (specifier-type 'bignum)) 'bignum)
2591 (t `(or ,@(mapcar #'type-specifier (union-type-types type))))))
2593 ;;; Two union types are equal if they are each subtypes of each
2594 ;;; other. We need to be this clever because our complex subtypep
2595 ;;; methods are now more accurate; we don't get infinite recursion
2596 ;;; because the simple-subtypep method delegates to complex-subtypep
2597 ;;; of the individual types of type1. - CSR, 2002-04-09
2599 ;;; Previous comment, now obsolete, but worth keeping around because
2600 ;;; it is true, though too strong a condition:
2602 ;;; Two union types are equal if their subtypes are equal sets.
2603 (!define-type-method (union :simple-=) (type1 type2)
2604 (multiple-value-bind (subtype certain?)
2605 (csubtypep type1 type2)
2607 (csubtypep type2 type1)
2608 ;; we might as well become as certain as possible.
2611 (multiple-value-bind (subtype certain?)
2612 (csubtypep type2 type1)
2613 (declare (ignore subtype))
2614 (values nil certain?))))))
2616 (!define-type-method (union :complex-=) (type1 type2)
2617 (declare (ignore type1))
2618 (if (some #'type-might-contain-other-types-p
2619 (union-type-types type2))
2623 ;;; Similarly, a union type is a subtype of another if and only if
2624 ;;; every element of TYPE1 is a subtype of TYPE2.
2625 (defun union-simple-subtypep (type1 type2)
2626 (every/type (swapped-args-fun #'union-complex-subtypep-arg2)
2628 (union-type-types type1)))
2630 (!define-type-method (union :simple-subtypep) (type1 type2)
2631 (union-simple-subtypep type1 type2))
2633 (defun union-complex-subtypep-arg1 (type1 type2)
2634 (every/type (swapped-args-fun #'csubtypep)
2636 (union-type-types type1)))
2638 (!define-type-method (union :complex-subtypep-arg1) (type1 type2)
2639 (union-complex-subtypep-arg1 type1 type2))
2641 (defun union-complex-subtypep-arg2 (type1 type2)
2642 (multiple-value-bind (sub-value sub-certain?)
2643 ;; was: (any/type #'csubtypep type1 (union-type-types type2)),
2644 ;; which turns out to be too restrictive, causing bug 91.
2646 ;; the following reimplementation might look dodgy. It is
2647 ;; dodgy. It depends on the union :complex-= method not doing
2648 ;; very much work -- certainly, not using subtypep. Reasoning:
2650 ;; At this stage, we know that type2 is a union type and type1
2651 ;; isn't. We might as well check this, though:
2652 (aver (union-type-p type2))
2653 (aver (not (union-type-p type1)))
2654 ;; A is a subset of (B1 u B2)
2655 ;; <=> A n (B1 u B2) = A
2656 ;; <=> (A n B1) u (A n B2) = A
2658 ;; But, we have to be careful not to delegate this type= to
2659 ;; something that could invoke subtypep, which might get us
2660 ;; back here -> stack explosion. We therefore ensure that the
2661 ;; second type (which is the one that's dispatched on) is
2662 ;; either a union type (where we've ensured that the complex-=
2663 ;; method will not call subtypep) or something with no union
2664 ;; types involved, in which case we'll never come back here.
2666 ;; If we don't do this, then e.g.
2667 ;; (SUBTYPEP '(MEMBER 3) '(OR (SATISFIES FOO) (SATISFIES BAR)))
2668 ;; would loop infinitely, as the member :complex-= method is
2669 ;; implemented in terms of subtypep.
2671 ;; Ouch. - CSR, 2002-04-10
2674 (mapcar (lambda (x) (type-intersection type1 x))
2675 (union-type-types type2)))))
2677 (values sub-value sub-certain?)
2678 ;; The ANY/TYPE expression above is a sufficient condition for
2679 ;; subsetness, but not a necessary one, so we might get a more
2680 ;; certain answer by this CALL-NEXT-METHOD-ish step when the
2681 ;; ANY/TYPE expression is uncertain.
2682 (invoke-complex-subtypep-arg1-method type1 type2))))
2684 (!define-type-method (union :complex-subtypep-arg2) (type1 type2)
2685 (union-complex-subtypep-arg2 type1 type2))
2687 (!define-type-method (union :simple-intersection2 :complex-intersection2)
2689 ;; The CSUBTYPEP clauses here let us simplify e.g.
2690 ;; (TYPE-INTERSECTION2 (SPECIFIER-TYPE 'LIST)
2691 ;; (SPECIFIER-TYPE '(OR LIST VECTOR)))
2692 ;; (where LIST is (OR CONS NULL)).
2694 ;; The tests are more or less (CSUBTYPEP TYPE1 TYPE2) and vice
2695 ;; versa, but it's important that we pre-expand them into
2696 ;; specialized operations on individual elements of
2697 ;; UNION-TYPE-TYPES, instead of using the ordinary call to
2698 ;; CSUBTYPEP, in order to avoid possibly invoking any methods which
2699 ;; might in turn invoke (TYPE-INTERSECTION2 TYPE1 TYPE2) and thus
2700 ;; cause infinite recursion.
2702 ;; Within this method, type2 is guaranteed to be a union type:
2703 (aver (union-type-p type2))
2704 ;; Make sure to call only the applicable methods...
2705 (cond ((and (union-type-p type1)
2706 (union-simple-subtypep type1 type2)) type1)
2707 ((and (union-type-p type1)
2708 (union-simple-subtypep type2 type1)) type2)
2709 ((and (not (union-type-p type1))
2710 (union-complex-subtypep-arg2 type1 type2))
2712 ((and (not (union-type-p type1))
2713 (union-complex-subtypep-arg1 type2 type1))
2716 ;; KLUDGE: This code accumulates a sequence of TYPE-UNION2
2717 ;; operations in a particular order, and gives up if any of
2718 ;; the sub-unions turn out not to be simple. In other cases
2719 ;; ca. sbcl-0.6.11.15, that approach to taking a union was a
2720 ;; bad idea, since it can overlook simplifications which
2721 ;; might occur if the terms were accumulated in a different
2722 ;; order. It's possible that that will be a problem here too.
2723 ;; However, I can't think of a good example to demonstrate
2724 ;; it, and without an example to demonstrate it I can't write
2725 ;; test cases, and without test cases I don't want to
2726 ;; complicate the code to address what's still a hypothetical
2727 ;; problem. So I punted. -- WHN 2001-03-20
2728 (let ((accumulator *empty-type*))
2729 (dolist (t2 (union-type-types type2) accumulator)
2731 (type-union accumulator
2732 (type-intersection type1 t2))))))))
2734 (!def-type-translator or (&rest type-specifiers)
2736 (mapcar #'specifier-type
2741 (!define-type-class cons)
2743 (!def-type-translator cons (&optional (car-type-spec '*) (cdr-type-spec '*))
2744 (let ((car-type (single-value-specifier-type car-type-spec))
2745 (cdr-type (single-value-specifier-type cdr-type-spec)))
2746 (make-cons-type car-type cdr-type)))
2748 (!define-type-method (cons :unparse) (type)
2749 (let ((car-eltype (type-specifier (cons-type-car-type type)))
2750 (cdr-eltype (type-specifier (cons-type-cdr-type type))))
2751 (if (and (member car-eltype '(t *))
2752 (member cdr-eltype '(t *)))
2754 `(cons ,car-eltype ,cdr-eltype))))
2756 (!define-type-method (cons :simple-=) (type1 type2)
2757 (declare (type cons-type type1 type2))
2758 (and (type= (cons-type-car-type type1) (cons-type-car-type type2))
2759 (type= (cons-type-cdr-type type1) (cons-type-cdr-type type2))))
2761 (!define-type-method (cons :simple-subtypep) (type1 type2)
2762 (declare (type cons-type type1 type2))
2763 (multiple-value-bind (val-car win-car)
2764 (csubtypep (cons-type-car-type type1) (cons-type-car-type type2))
2765 (multiple-value-bind (val-cdr win-cdr)
2766 (csubtypep (cons-type-cdr-type type1) (cons-type-cdr-type type2))
2767 (if (and val-car val-cdr)
2768 (values t (and win-car win-cdr))
2769 (values nil (or win-car win-cdr))))))
2771 ;;; Give up if a precise type is not possible, to avoid returning
2772 ;;; overly general types.
2773 (!define-type-method (cons :simple-union2) (type1 type2)
2774 (declare (type cons-type type1 type2))
2775 (let ((car-type1 (cons-type-car-type type1))
2776 (car-type2 (cons-type-car-type type2))
2777 (cdr-type1 (cons-type-cdr-type type1))
2778 (cdr-type2 (cons-type-cdr-type type2)))
2779 ;; UGH. -- CSR, 2003-02-24
2780 (macrolet ((frob-car (car1 car2 cdr1 cdr2)
2782 (make-cons-type ,car1 (type-union ,cdr1 ,cdr2))
2784 (type-intersection ,car2
2786 `(not ,(type-specifier ,car1))))
2788 (cond ((type= car-type1 car-type2)
2789 (make-cons-type car-type1
2790 (type-union cdr-type1 cdr-type2)))
2791 ((type= cdr-type1 cdr-type2)
2792 (make-cons-type (type-union car-type1 car-type2)
2794 ((csubtypep car-type1 car-type2)
2795 (frob-car car-type1 car-type2 cdr-type1 cdr-type2))
2796 ((csubtypep car-type2 car-type1)
2797 (frob-car car-type2 car-type1 cdr-type2 cdr-type1))
2798 ;; Don't put these in -- consider the effect of taking the
2799 ;; union of (CONS (INTEGER 0 2) (INTEGER 5 7)) and
2800 ;; (CONS (INTEGER 0 3) (INTEGER 5 6)).
2802 ((csubtypep cdr-type1 cdr-type2)
2803 (frob-cdr car-type1 car-type2 cdr-type1 cdr-type2))
2805 ((csubtypep cdr-type2 cdr-type1)
2806 (frob-cdr car-type2 car-type1 cdr-type2 cdr-type1))))))
2808 (!define-type-method (cons :simple-intersection2) (type1 type2)
2809 (declare (type cons-type type1 type2))
2812 (and (setf car-int2 (type-intersection2 (cons-type-car-type type1)
2813 (cons-type-car-type type2)))
2814 (setf cdr-int2 (type-intersection2 (cons-type-cdr-type type1)
2815 (cons-type-cdr-type type2)))
2816 (make-cons-type car-int2 cdr-int2))))
2818 ;;; Return the type that describes all objects that are in X but not
2819 ;;; in Y. If we can't determine this type, then return NIL.
2821 ;;; For now, we only are clever dealing with union and member types.
2822 ;;; If either type is not a union type, then we pretend that it is a
2823 ;;; union of just one type. What we do is remove from X all the types
2824 ;;; that are a subtype any type in Y. If any type in X intersects with
2825 ;;; a type in Y but is not a subtype, then we give up.
2827 ;;; We must also special-case any member type that appears in the
2828 ;;; union. We remove from X's members all objects that are TYPEP to Y.
2829 ;;; If Y has any members, we must be careful that none of those
2830 ;;; members are CTYPEP to any of Y's non-member types. We give up in
2831 ;;; this case, since to compute that difference we would have to break
2832 ;;; the type from X into some collection of types that represents the
2833 ;;; type without that particular element. This seems too hairy to be
2834 ;;; worthwhile, given its low utility.
2835 (defun type-difference (x y)
2836 (let ((x-types (if (union-type-p x) (union-type-types x) (list x)))
2837 (y-types (if (union-type-p y) (union-type-types y) (list y))))
2839 (dolist (x-type x-types)
2840 (if (member-type-p x-type)
2841 (collect ((members))
2842 (dolist (mem (member-type-members x-type))
2843 (multiple-value-bind (val win) (ctypep mem y)
2844 (unless win (return-from type-difference nil))
2848 (res (make-member-type :members (members)))))
2849 (dolist (y-type y-types (res x-type))
2850 (multiple-value-bind (val win) (csubtypep x-type y-type)
2851 (unless win (return-from type-difference nil))
2853 (when (types-equal-or-intersect x-type y-type)
2854 (return-from type-difference nil))))))
2855 (let ((y-mem (find-if #'member-type-p y-types)))
2857 (let ((members (member-type-members y-mem)))
2858 (dolist (x-type x-types)
2859 (unless (member-type-p x-type)
2860 (dolist (member members)
2861 (multiple-value-bind (val win) (ctypep member x-type)
2862 (when (or (not win) val)
2863 (return-from type-difference nil)))))))))
2864 (apply #'type-union (res)))))
2866 (!def-type-translator array (&optional (element-type '*)
2868 (specialize-array-type
2869 (make-array-type :dimensions (canonical-array-dimensions dimensions)
2871 :element-type (if (eq element-type '*)
2873 (specifier-type element-type)))))
2875 (!def-type-translator simple-array (&optional (element-type '*)
2877 (specialize-array-type
2878 (make-array-type :dimensions (canonical-array-dimensions dimensions)
2880 :element-type (if (eq element-type '*)
2882 (specifier-type element-type)))))
2884 ;;;; utilities shared between cross-compiler and target system
2886 ;;; Does the type derived from compilation of an actual function
2887 ;;; definition satisfy declarations of a function's type?
2888 (defun defined-ftype-matches-declared-ftype-p (defined-ftype declared-ftype)
2889 (declare (type ctype defined-ftype declared-ftype))
2890 (flet ((is-built-in-class-function-p (ctype)
2891 (and (built-in-classoid-p ctype)
2892 (eq (built-in-classoid-name ctype) 'function))))
2893 (cond (;; DECLARED-FTYPE could certainly be #<BUILT-IN-CLASS FUNCTION>;
2894 ;; that's what happens when we (DECLAIM (FTYPE FUNCTION FOO)).
2895 (is-built-in-class-function-p declared-ftype)
2896 ;; In that case, any definition satisfies the declaration.
2898 (;; It's not clear whether or how DEFINED-FTYPE might be
2899 ;; #<BUILT-IN-CLASS FUNCTION>, but it's not obviously
2900 ;; invalid, so let's handle that case too, just in case.
2901 (is-built-in-class-function-p defined-ftype)
2902 ;; No matter what DECLARED-FTYPE might be, we can't prove
2903 ;; that an object of type FUNCTION doesn't satisfy it, so
2904 ;; we return success no matter what.
2906 (;; Otherwise both of them must be FUN-TYPE objects.
2908 ;; FIXME: For now we only check compatibility of the return
2909 ;; type, not argument types, and we don't even check the
2910 ;; return type very precisely (as per bug 94a). It would be
2911 ;; good to do a better job. Perhaps to check the
2912 ;; compatibility of the arguments, we should (1) redo
2913 ;; VALUES-TYPES-EQUAL-OR-INTERSECT as
2914 ;; ARGS-TYPES-EQUAL-OR-INTERSECT, and then (2) apply it to
2915 ;; the ARGS-TYPE slices of the FUN-TYPEs. (ARGS-TYPE
2916 ;; is a base class both of VALUES-TYPE and of FUN-TYPE.)
2917 (values-types-equal-or-intersect
2918 (fun-type-returns defined-ftype)
2919 (fun-type-returns declared-ftype))))))
2921 ;;; This messy case of CTYPE for NUMBER is shared between the
2922 ;;; cross-compiler and the target system.
2923 (defun ctype-of-number (x)
2924 (let ((num (if (complexp x) (realpart x) x)))
2925 (multiple-value-bind (complexp low high)
2927 (let ((imag (imagpart x)))
2928 (values :complex (min num imag) (max num imag)))
2929 (values :real num num))
2930 (make-numeric-type :class (etypecase num
2932 (rational 'rational)
2934 :format (and (floatp num) (float-format-name num))
2940 ;; Why SAFETY 0? To suppress the is-it-the-right-structure-type
2941 ;; checking for declarations in structure accessors. Otherwise we
2942 ;; can get caught in a chicken-and-egg bootstrapping problem, whose
2943 ;; symptom on x86 OpenBSD sbcl-0.pre7.37.flaky5.22 is an illegal
2944 ;; instruction trap. I haven't tracked it down, but I'm guessing it
2945 ;; has to do with setting LAYOUTs when the LAYOUT hasn't been set
2947 (declare (optimize (safety 0)))
2948 (!defun-from-collected-cold-init-forms !late-type-cold-init))
2950 (/show0 "late-type.lisp end of file")