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 :negate) (type)
158 (error "NOT VALUES too confusing on ~S" (type-specifier type)))
160 (!define-type-method (values :unparse) (type)
162 (let ((unparsed (unparse-args-types type)))
163 (if (or (values-type-optional type)
164 (values-type-rest type)
165 (values-type-allowp type))
167 (nconc unparsed '(&optional))))))
169 ;;; Return true if LIST1 and LIST2 have the same elements in the same
170 ;;; positions according to TYPE=. We return NIL, NIL if there is an
171 ;;; uncertain comparison.
172 (defun type=-list (list1 list2)
173 (declare (list list1 list2))
174 (do ((types1 list1 (cdr types1))
175 (types2 list2 (cdr types2)))
176 ((or (null types1) (null types2))
177 (if (or types1 types2)
180 (multiple-value-bind (val win)
181 (type= (first types1) (first types2))
183 (return (values nil nil)))
185 (return (values nil t))))))
187 (!define-type-method (values :simple-=) (type1 type2)
188 (type=-args type1 type2))
190 (!define-type-class function)
192 ;;; a flag that we can bind to cause complex function types to be
193 ;;; unparsed as FUNCTION. This is useful when we want a type that we
194 ;;; can pass to TYPEP.
195 (defvar *unparse-fun-type-simplify*)
196 (!cold-init-forms (setq *unparse-fun-type-simplify* nil))
198 (!define-type-method (function :negate) (type)
199 (error "NOT FUNCTION too confusing on ~S" (type-specifier type)))
201 (!define-type-method (function :unparse) (type)
202 (if *unparse-fun-type-simplify*
205 (if (fun-type-wild-args type)
207 (unparse-args-types type))
209 (fun-type-returns type)))))
211 ;;; The meaning of this is a little confused. On the one hand, all
212 ;;; function objects are represented the same way regardless of the
213 ;;; arglists and return values, and apps don't get to ask things like
214 ;;; (TYPEP #'FOO (FUNCTION (FIXNUM) *)) in any meaningful way. On the
215 ;;; other hand, Python wants to reason about function types. So...
216 (!define-type-method (function :simple-subtypep) (type1 type2)
217 (flet ((fun-type-simple-p (type)
218 (not (or (fun-type-rest type)
219 (fun-type-keyp type))))
220 (every-csubtypep (types1 types2)
224 do (multiple-value-bind (res sure-p)
226 (unless res (return (values res sure-p))))
227 finally (return (values t t)))))
228 (and/type (values-subtypep (fun-type-returns type1)
229 (fun-type-returns type2))
230 (cond ((fun-type-wild-args type2) (values t t))
231 ((fun-type-wild-args type1)
232 (cond ((fun-type-keyp type2) (values nil nil))
233 ((not (fun-type-rest type2)) (values nil t))
234 ((not (null (fun-type-required type2)))
236 (t (and/type (type= *universal-type*
237 (fun-type-rest type2))
242 ((not (and (fun-type-simple-p type1)
243 (fun-type-simple-p type2)))
245 (t (multiple-value-bind (min1 max1) (fun-type-nargs type1)
246 (multiple-value-bind (min2 max2) (fun-type-nargs type2)
247 (cond ((or (> max1 max2) (< min1 min2))
249 ((and (= min1 min2) (= max1 max2))
250 (and/type (every-csubtypep
251 (fun-type-required type1)
252 (fun-type-required type2))
254 (fun-type-optional type1)
255 (fun-type-optional type2))))
258 (fun-type-required type1)
259 (fun-type-optional type1))
261 (fun-type-required type2)
262 (fun-type-optional type2))))))))))))
264 (!define-superclasses function ((function)) !cold-init-forms)
266 ;;; The union or intersection of two FUNCTION types is FUNCTION.
267 (!define-type-method (function :simple-union2) (type1 type2)
268 (declare (ignore type1 type2))
269 (specifier-type 'function))
270 (!define-type-method (function :simple-intersection2) (type1 type2)
271 (let ((ftype (specifier-type 'function)))
272 (cond ((eq type1 ftype) type2)
273 ((eq type2 ftype) type1)
274 (t (let ((rtype (values-type-intersection (fun-type-returns type1)
275 (fun-type-returns type2))))
276 (flet ((change-returns (ftype rtype)
277 (declare (type fun-type ftype) (type ctype rtype))
278 (make-fun-type :required (fun-type-required ftype)
279 :optional (fun-type-optional ftype)
280 :keyp (fun-type-keyp ftype)
281 :keywords (fun-type-keywords ftype)
282 :allowp (fun-type-allowp ftype)
285 ((fun-type-wild-args type1)
286 (if (fun-type-wild-args type2)
287 (make-fun-type :wild-args t
289 (change-returns type2 rtype)))
290 ((fun-type-wild-args type2)
291 (change-returns type1 rtype))
292 (t (multiple-value-bind (req opt rest)
293 (args-type-op type1 type2 #'type-intersection #'max)
294 (make-fun-type :required req
298 :allowp (and (fun-type-allowp type1)
299 (fun-type-allowp type2))
300 :returns rtype))))))))))
302 ;;; The union or intersection of a subclass of FUNCTION with a
303 ;;; FUNCTION type is somewhat complicated.
304 (!define-type-method (function :complex-intersection2) (type1 type2)
306 ((type= type1 (specifier-type 'function)) type2)
307 ((csubtypep type1 (specifier-type 'function)) nil)
308 (t :call-other-method)))
309 (!define-type-method (function :complex-union2) (type1 type2)
311 ((type= type1 (specifier-type 'function)) type1)
314 (!define-type-method (function :simple-=) (type1 type2)
315 (macrolet ((compare (comparator field)
316 (let ((reader (symbolicate '#:fun-type- field)))
317 `(,comparator (,reader type1) (,reader type2)))))
318 (and/type (compare type= returns)
319 (cond ((neq (fun-type-wild-args type1) (fun-type-wild-args type2))
321 ((eq (fun-type-wild-args type1) t)
323 (t (type=-args type1 type2))))))
325 (!define-type-class constant :inherits values)
327 (!define-type-method (constant :negate) (type)
328 (error "NOT CONSTANT too confusing on ~S" (type-specifier type)))
330 (!define-type-method (constant :unparse) (type)
331 `(constant-arg ,(type-specifier (constant-type-type type))))
333 (!define-type-method (constant :simple-=) (type1 type2)
334 (type= (constant-type-type type1) (constant-type-type type2)))
336 (!def-type-translator constant-arg (type)
337 (make-constant-type :type (single-value-specifier-type type)))
339 ;;; Return the lambda-list-like type specification corresponding
341 (declaim (ftype (function (args-type) list) unparse-args-types))
342 (defun unparse-args-types (type)
345 (dolist (arg (args-type-required type))
346 (result (type-specifier arg)))
348 (when (args-type-optional type)
350 (dolist (arg (args-type-optional type))
351 (result (type-specifier arg))))
353 (when (args-type-rest type)
355 (result (type-specifier (args-type-rest type))))
357 (when (args-type-keyp type)
359 (dolist (key (args-type-keywords type))
360 (result (list (key-info-name key)
361 (type-specifier (key-info-type key))))))
363 (when (args-type-allowp type)
364 (result '&allow-other-keys))
368 (!def-type-translator function (&optional (args '*) (result '*))
369 (make-fun-type :args args
370 :returns (coerce-to-values (values-specifier-type result))))
372 (!def-type-translator values (&rest values)
373 (make-values-type :args values))
375 ;;;; VALUES types interfaces
377 ;;;; We provide a few special operations that can be meaningfully used
378 ;;;; on VALUES types (as well as on any other type).
380 (defun type-single-value-p (type)
381 (and (values-type-p type)
382 (not (values-type-rest type))
383 (null (values-type-optional type))
384 (singleton-p (values-type-required type))))
386 ;;; Return the type of the first value indicated by TYPE. This is used
387 ;;; by people who don't want to have to deal with VALUES types.
388 #!-sb-fluid (declaim (freeze-type values-type))
389 ; (inline single-value-type))
390 (defun single-value-type (type)
391 (declare (type ctype type))
392 (cond ((eq type *wild-type*)
394 ((eq type *empty-type*)
396 ((not (values-type-p type))
398 (t (or (car (args-type-required type))
399 (car (args-type-optional type))
400 (args-type-rest type)
401 (specifier-type 'null)))))
403 ;;; Return the minimum number of arguments that a function can be
404 ;;; called with, and the maximum number or NIL. If not a function
405 ;;; type, return NIL, NIL.
406 (defun fun-type-nargs (type)
407 (declare (type ctype type))
408 (if (and (fun-type-p type) (not (fun-type-wild-args type)))
409 (let ((fixed (length (args-type-required type))))
410 (if (or (args-type-rest type)
411 (args-type-keyp type)
412 (args-type-allowp type))
414 (values fixed (+ fixed (length (args-type-optional type))))))
417 ;;; Determine whether TYPE corresponds to a definite number of values.
418 ;;; The first value is a list of the types for each value, and the
419 ;;; second value is the number of values. If the number of values is
420 ;;; not fixed, then return NIL and :UNKNOWN.
421 (defun values-types (type)
422 (declare (type ctype type))
423 (cond ((or (eq type *wild-type*) (eq type *empty-type*))
424 (values nil :unknown))
425 ((or (args-type-optional type)
426 (args-type-rest type))
427 (values nil :unknown))
429 (let ((req (args-type-required type)))
430 (values req (length req))))))
432 ;;; Return two values:
433 ;;; 1. A list of all the positional (fixed and optional) types.
434 ;;; 2. The &REST type (if any). If no &REST, then the DEFAULT-TYPE.
435 (defun values-type-types (type &optional (default-type *empty-type*))
436 (declare (type ctype type))
437 (if (eq type *wild-type*)
438 (values nil *universal-type*)
439 (values (append (args-type-required type)
440 (args-type-optional type))
441 (cond ((args-type-rest type))
444 ;;; types of values in (the <type> (values o_1 ... o_n))
445 (defun values-type-out (type count)
446 (declare (type ctype type) (type unsigned-byte count))
447 (if (eq type *wild-type*)
448 (make-list count :initial-element *universal-type*)
450 (flet ((process-types (types)
451 (loop for type in types
455 (process-types (values-type-required type))
456 (process-types (values-type-optional type))
458 (loop with rest = (the ctype (values-type-rest type))
463 ;;; types of variable in (m-v-bind (v_1 ... v_n) (the <type> ...
464 (defun values-type-in (type count)
465 (declare (type ctype type) (type unsigned-byte count))
466 (if (eq type *wild-type*)
467 (make-list count :initial-element *universal-type*)
469 (let ((null-type (specifier-type 'null)))
470 (loop for type in (values-type-required type)
474 (loop for type in (values-type-optional type)
477 do (res (type-union type null-type)))
479 (loop with rest = (acond ((values-type-rest type)
480 (type-union it null-type))
486 ;;; Return a list of OPERATION applied to the types in TYPES1 and
487 ;;; TYPES2, padding with REST2 as needed. TYPES1 must not be shorter
488 ;;; than TYPES2. The second value is T if OPERATION always returned a
489 ;;; true second value.
490 (defun fixed-values-op (types1 types2 rest2 operation)
491 (declare (list types1 types2) (type ctype rest2) (type function operation))
493 (values (mapcar (lambda (t1 t2)
494 (multiple-value-bind (res win)
495 (funcall operation t1 t2)
501 (make-list (- (length types1) (length types2))
502 :initial-element rest2)))
505 ;;; If TYPE isn't a values type, then make it into one.
506 (defun-cached (%coerce-to-values
508 :hash-function (lambda (type)
509 (logand (type-hash-value type)
512 (cond ((multiple-value-bind (res sure)
513 (csubtypep (specifier-type 'null) type)
514 (and (not res) sure))
515 ;; FIXME: What should we do with (NOT SURE)?
516 (make-values-type :required (list type) :rest *universal-type*))
518 (make-values-type :optional (list type) :rest *universal-type*))))
520 (defun coerce-to-values (type)
521 (declare (type ctype type))
522 (cond ((or (eq type *universal-type*)
523 (eq type *wild-type*))
525 ((values-type-p type)
527 (t (%coerce-to-values type))))
529 ;;; Return type, corresponding to ANSI short form of VALUES type
531 (defun make-short-values-type (types)
532 (declare (list types))
533 (let ((last-required (position-if
535 (not/type (csubtypep (specifier-type 'null) type)))
539 (make-values-type :required (subseq types 0 (1+ last-required))
540 :optional (subseq types (1+ last-required))
541 :rest *universal-type*)
542 (make-values-type :optional types :rest *universal-type*))))
544 (defun make-single-value-type (type)
545 (make-values-type :required (list type)))
547 ;;; Do the specified OPERATION on TYPE1 and TYPE2, which may be any
548 ;;; type, including VALUES types. With VALUES types such as:
551 ;;; we compute the more useful result
552 ;;; (VALUES (<operation> a0 b0) (<operation> a1 b1))
553 ;;; rather than the precise result
554 ;;; (<operation> (values a0 a1) (values b0 b1))
555 ;;; This has the virtue of always keeping the VALUES type specifier
556 ;;; outermost, and retains all of the information that is really
557 ;;; useful for static type analysis. We want to know what is always
558 ;;; true of each value independently. It is worthless to know that if
559 ;;; the first value is B0 then the second will be B1.
561 ;;; If the VALUES count signatures differ, then we produce a result with
562 ;;; the required VALUE count chosen by NREQ when applied to the number
563 ;;; of required values in TYPE1 and TYPE2. Any &KEY values become
564 ;;; &REST T (anyone who uses keyword values deserves to lose.)
566 ;;; The second value is true if the result is definitely empty or if
567 ;;; OPERATION returned true as its second value each time we called
568 ;;; it. Since we approximate the intersection of VALUES types, the
569 ;;; second value being true doesn't mean the result is exact.
570 (defun args-type-op (type1 type2 operation nreq)
571 (declare (type ctype type1 type2)
572 (type function operation nreq))
573 (when (eq type1 type2)
575 (multiple-value-bind (types1 rest1)
576 (values-type-types type1)
577 (multiple-value-bind (types2 rest2)
578 (values-type-types type2)
579 (multiple-value-bind (rest rest-exact)
580 (funcall operation rest1 rest2)
581 (multiple-value-bind (res res-exact)
582 (if (< (length types1) (length types2))
583 (fixed-values-op types2 types1 rest1 operation)
584 (fixed-values-op types1 types2 rest2 operation))
585 (let* ((req (funcall nreq
586 (length (args-type-required type1))
587 (length (args-type-required type2))))
588 (required (subseq res 0 req))
589 (opt (subseq res req)))
590 (values required opt rest
591 (and rest-exact res-exact))))))))
593 (defun values-type-op (type1 type2 operation nreq)
594 (multiple-value-bind (required optional rest exactp)
595 (args-type-op type1 type2 operation nreq)
596 (values (make-values-type :required required
601 (defun type=-args (type1 type2)
602 (macrolet ((compare (comparator field)
603 (let ((reader (symbolicate '#:args-type- field)))
604 `(,comparator (,reader type1) (,reader type2)))))
606 (cond ((null (args-type-rest type1))
607 (values (null (args-type-rest type2)) t))
608 ((null (args-type-rest type2))
611 (compare type= rest)))
612 (and/type (and/type (compare type=-list required)
613 (compare type=-list optional))
614 (if (or (args-type-keyp type1) (args-type-keyp type2))
618 ;;; Do a union or intersection operation on types that might be values
619 ;;; types. The result is optimized for utility rather than exactness,
620 ;;; but it is guaranteed that it will be no smaller (more restrictive)
621 ;;; than the precise result.
623 ;;; The return convention seems to be analogous to
624 ;;; TYPES-EQUAL-OR-INTERSECT. -- WHN 19990910.
625 (defun-cached (values-type-union :hash-function type-cache-hash
628 :init-wrapper !cold-init-forms)
629 ((type1 eq) (type2 eq))
630 (declare (type ctype type1 type2))
631 (cond ((or (eq type1 *wild-type*) (eq type2 *wild-type*)) *wild-type*)
632 ((eq type1 *empty-type*) type2)
633 ((eq type2 *empty-type*) type1)
635 (values (values-type-op type1 type2 #'type-union #'min)))))
637 (defun-cached (values-type-intersection :hash-function type-cache-hash
639 :default (values nil)
640 :init-wrapper !cold-init-forms)
641 ((type1 eq) (type2 eq))
642 (declare (type ctype type1 type2))
643 (cond ((eq type1 *wild-type*)
644 (coerce-to-values type2))
645 ((or (eq type2 *wild-type*) (eq type2 *universal-type*))
647 ((or (eq type1 *empty-type*) (eq type2 *empty-type*))
649 ((and (not (values-type-p type2))
650 (values-type-required type1))
651 (let ((req1 (values-type-required type1)))
652 (make-values-type :required (cons (type-intersection (first req1) type2)
654 :optional (values-type-optional type1)
655 :rest (values-type-rest type1)
656 :allowp (values-type-allowp type1))))
658 (values (values-type-op type1 (coerce-to-values type2)
662 ;;; This is like TYPES-EQUAL-OR-INTERSECT, except that it sort of
663 ;;; works on VALUES types. Note that due to the semantics of
664 ;;; VALUES-TYPE-INTERSECTION, this might return (VALUES T T) when
665 ;;; there isn't really any intersection.
666 (defun values-types-equal-or-intersect (type1 type2)
667 (cond ((or (eq type1 *empty-type*) (eq type2 *empty-type*))
669 ((or (eq type1 *wild-type*) (eq type2 *wild-type*))
672 (let ((res (values-type-intersection type1 type2)))
673 (values (not (eq res *empty-type*))
676 ;;; a SUBTYPEP-like operation that can be used on any types, including
678 (defun-cached (values-subtypep :hash-function type-cache-hash
681 :default (values nil :empty)
682 :init-wrapper !cold-init-forms)
683 ((type1 eq) (type2 eq))
684 (declare (type ctype type1 type2))
685 (cond ((or (eq type2 *wild-type*) (eq type2 *universal-type*)
686 (eq type1 *empty-type*))
688 ((eq type1 *wild-type*)
689 (values (eq type2 *wild-type*) t))
690 ((or (eq type2 *empty-type*)
691 (not (values-types-equal-or-intersect type1 type2)))
693 ((and (not (values-type-p type2))
694 (values-type-required type1))
695 (csubtypep (first (values-type-required type1))
697 (t (setq type2 (coerce-to-values type2))
698 (multiple-value-bind (types1 rest1) (values-type-types type1)
699 (multiple-value-bind (types2 rest2) (values-type-types type2)
700 (cond ((< (length (values-type-required type1))
701 (length (values-type-required type2)))
703 ((< (length types1) (length types2))
706 (do ((t1 types1 (rest t1))
707 (t2 types2 (rest t2)))
709 (csubtypep rest1 rest2))
710 (multiple-value-bind (res win-p)
711 (csubtypep (first t1) (first t2))
713 (return (values nil nil)))
715 (return (values nil t))))))))))))
717 ;;;; type method interfaces
719 ;;; like SUBTYPEP, only works on CTYPE structures
720 (defun-cached (csubtypep :hash-function type-cache-hash
723 :default (values nil :empty)
724 :init-wrapper !cold-init-forms)
725 ((type1 eq) (type2 eq))
726 (declare (type ctype type1 type2))
727 (cond ((or (eq type1 type2)
728 (eq type1 *empty-type*)
729 (eq type2 *universal-type*))
732 ((eq type1 *universal-type*)
735 (!invoke-type-method :simple-subtypep :complex-subtypep-arg2
737 :complex-arg1 :complex-subtypep-arg1))))
739 ;;; Just parse the type specifiers and call CSUBTYPE.
740 (defun sb!xc:subtypep (type1 type2 &optional environment)
742 "Return two values indicating the relationship between type1 and type2.
743 If values are T and T, type1 definitely is a subtype of type2.
744 If values are NIL and T, type1 definitely is not a subtype of type2.
745 If values are NIL and NIL, it couldn't be determined."
746 (declare (ignore environment))
747 (csubtypep (specifier-type type1) (specifier-type type2)))
749 ;;; If two types are definitely equivalent, return true. The second
750 ;;; value indicates whether the first value is definitely correct.
751 ;;; This should only fail in the presence of HAIRY types.
752 (defun-cached (type= :hash-function type-cache-hash
755 :default (values nil :empty)
756 :init-wrapper !cold-init-forms)
757 ((type1 eq) (type2 eq))
758 (declare (type ctype type1 type2))
761 (!invoke-type-method :simple-= :complex-= type1 type2)))
763 ;;; Not exactly the negation of TYPE=, since when the relationship is
764 ;;; uncertain, we still return NIL, NIL. This is useful in cases where
765 ;;; the conservative assumption is =.
766 (defun type/= (type1 type2)
767 (declare (type ctype type1 type2))
768 (multiple-value-bind (res win) (type= type1 type2)
773 ;;; the type method dispatch case of TYPE-UNION2
774 (defun %type-union2 (type1 type2)
775 ;; As in %TYPE-INTERSECTION2, it seems to be a good idea to give
776 ;; both argument orders a chance at COMPLEX-INTERSECTION2. Unlike
777 ;; %TYPE-INTERSECTION2, though, I don't have a specific case which
778 ;; demonstrates this is actually necessary. Also unlike
779 ;; %TYPE-INTERSECTION2, there seems to be no need to distinguish
780 ;; between not finding a method and having a method return NIL.
782 (!invoke-type-method :simple-union2 :complex-union2
785 (declare (inline 1way))
786 (or (1way type1 type2)
787 (1way type2 type1))))
789 ;;; Find a type which includes both types. Any inexactness is
790 ;;; represented by the fuzzy element types; we return a single value
791 ;;; that is precise to the best of our knowledge. This result is
792 ;;; simplified into the canonical form, thus is not a UNION-TYPE
793 ;;; unless we find no other way to represent the result.
794 (defun-cached (type-union2 :hash-function type-cache-hash
796 :init-wrapper !cold-init-forms)
797 ((type1 eq) (type2 eq))
798 ;; KLUDGE: This was generated from TYPE-INTERSECTION2 by Ye Olde Cut And
799 ;; Paste technique of programming. If it stays around (as opposed to
800 ;; e.g. fading away in favor of some CLOS solution) the shared logic
801 ;; should probably become shared code. -- WHN 2001-03-16
802 (declare (type ctype type1 type2))
803 (cond ((eq type1 type2)
805 ((csubtypep type1 type2) type2)
806 ((csubtypep type2 type1) type1)
807 ((or (union-type-p type1)
808 (union-type-p type2))
809 ;; Unions of UNION-TYPE should have the UNION-TYPE-TYPES
810 ;; values broken out and united separately. The full TYPE-UNION
811 ;; function knows how to do this, so let it handle it.
812 (type-union type1 type2))
814 ;; the ordinary case: we dispatch to type methods
815 (%type-union2 type1 type2))))
817 ;;; the type method dispatch case of TYPE-INTERSECTION2
818 (defun %type-intersection2 (type1 type2)
819 ;; We want to give both argument orders a chance at
820 ;; COMPLEX-INTERSECTION2. Without that, the old CMU CL type
821 ;; methods could give noncommutative results, e.g.
822 ;; (TYPE-INTERSECTION2 *EMPTY-TYPE* SOME-HAIRY-TYPE)
824 ;; (TYPE-INTERSECTION2 SOME-HAIRY-TYPE *EMPTY-TYPE*)
825 ;; => #<NAMED-TYPE NIL>, T
826 ;; We also need to distinguish between the case where we found a
827 ;; type method, and it returned NIL, and the case where we fell
828 ;; through without finding any type method. An example of the first
829 ;; case is the intersection of a HAIRY-TYPE with some ordinary type.
830 ;; An example of the second case is the intersection of two
831 ;; completely-unrelated types, e.g. CONS and NUMBER, or SYMBOL and
834 ;; (Why yes, CLOS probably *would* be nicer..)
836 (!invoke-type-method :simple-intersection2 :complex-intersection2
838 :default :call-other-method)))
839 (declare (inline 1way))
840 (let ((xy (1way type1 type2)))
841 (or (and (not (eql xy :call-other-method)) xy)
842 (let ((yx (1way type2 type1)))
843 (or (and (not (eql yx :call-other-method)) yx)
844 (cond ((and (eql xy :call-other-method)
845 (eql yx :call-other-method))
848 (aver (and (not xy) (not yx))) ; else handled above
851 (defun-cached (type-intersection2 :hash-function type-cache-hash
855 :init-wrapper !cold-init-forms)
856 ((type1 eq) (type2 eq))
857 (declare (type ctype type1 type2))
858 (cond ((eq type1 type2)
859 ;; FIXME: For some reason, this doesn't catch e.g. type1 =
860 ;; type2 = (SPECIFIER-TYPE
861 ;; 'SOME-UNKNOWN-TYPE). Investigate. - CSR, 2002-04-10
863 ((or (intersection-type-p type1)
864 (intersection-type-p type2))
865 ;; Intersections of INTERSECTION-TYPE should have the
866 ;; INTERSECTION-TYPE-TYPES values broken out and intersected
867 ;; separately. The full TYPE-INTERSECTION function knows how
868 ;; to do that, so let it handle it.
869 (type-intersection type1 type2))
871 ;; the ordinary case: we dispatch to type methods
872 (%type-intersection2 type1 type2))))
874 ;;; Return as restrictive and simple a type as we can discover that is
875 ;;; no more restrictive than the intersection of TYPE1 and TYPE2. At
876 ;;; worst, we arbitrarily return one of the arguments as the first
877 ;;; value (trying not to return a hairy type).
878 (defun type-approx-intersection2 (type1 type2)
879 (cond ((type-intersection2 type1 type2))
880 ((hairy-type-p type1) type2)
883 ;;; a test useful for checking whether a derived type matches a
886 ;;; The first value is true unless the types don't intersect and
887 ;;; aren't equal. The second value is true if the first value is
888 ;;; definitely correct. NIL is considered to intersect with any type.
889 ;;; If T is a subtype of either type, then we also return T, T. This
890 ;;; way we recognize that hairy types might intersect with T.
891 (defun types-equal-or-intersect (type1 type2)
892 (declare (type ctype type1 type2))
893 (if (or (eq type1 *empty-type*) (eq type2 *empty-type*))
895 (let ((intersection2 (type-intersection2 type1 type2)))
896 (cond ((not intersection2)
897 (if (or (csubtypep *universal-type* type1)
898 (csubtypep *universal-type* type2))
901 ((eq intersection2 *empty-type*) (values nil t))
904 ;;; Return a Common Lisp type specifier corresponding to the TYPE
906 (defun type-specifier (type)
907 (declare (type ctype type))
908 (funcall (type-class-unparse (type-class-info type)) type))
910 (defun-cached (type-negation :hash-function (lambda (type)
911 (logand (type-hash-value type)
916 :init-wrapper !cold-init-forms)
918 (declare (type ctype type))
919 (funcall (type-class-negate (type-class-info type)) type))
921 ;;; (VALUES-SPECIFIER-TYPE and SPECIFIER-TYPE moved from here to
922 ;;; early-type.lisp by WHN ca. 19990201.)
924 ;;; Take a list of type specifiers, computing the translation of each
925 ;;; specifier and defining it as a builtin type.
926 (declaim (ftype (function (list) (values)) precompute-types))
927 (defun precompute-types (specs)
929 (let ((res (specifier-type spec)))
930 (unless (unknown-type-p res)
931 (setf (info :type :builtin spec) res)
932 ;; KLUDGE: the three copies of this idiom in this file (and
933 ;; the one in class.lisp as at sbcl-0.7.4.1x) should be
934 ;; coalesced, or perhaps the error-detecting code that
935 ;; disallows redefinition of :PRIMITIVE types should be
936 ;; rewritten to use *TYPE-SYSTEM-FINALIZED* (rather than
937 ;; *TYPE-SYSTEM-INITIALIZED*). The effect of this is not to
938 ;; cause redefinition errors when precompute-types is called
939 ;; for a second time while building the target compiler using
940 ;; the cross-compiler. -- CSR, trying to explain why this
941 ;; isn't completely wrong, 2002-06-07
942 (setf (info :type :kind spec) #+sb-xc-host :defined #-sb-xc-host :primitive))))
945 ;;;; general TYPE-UNION and TYPE-INTERSECTION operations
947 ;;;; These are fully general operations on CTYPEs: they'll always
948 ;;;; return a CTYPE representing the result.
950 ;;; shared logic for unions and intersections: Return a list of
951 ;;; types representing the same types as INPUT-TYPES, but with
952 ;;; COMPOUND-TYPEs satisfying %COMPOUND-TYPE-P broken up into their
953 ;;; component types, and with any SIMPLY2 simplifications applied.
955 ((def (name compound-type-p simplify2)
956 `(defun ,name (types)
958 (multiple-value-bind (first rest)
959 (if (,compound-type-p (car types))
960 (values (car (compound-type-types (car types)))
961 (append (cdr (compound-type-types (car types)))
963 (values (car types) (cdr types)))
964 (let ((rest (,name rest)) u)
965 (dolist (r rest (cons first rest))
966 (when (setq u (,simplify2 first r))
967 (return (,name (nsubstitute u r rest)))))))))))
968 (def simplify-intersections intersection-type-p type-intersection2)
969 (def simplify-unions union-type-p type-union2))
971 (defun maybe-distribute-one-union (union-type types)
972 (let* ((intersection (apply #'type-intersection types))
973 (union (mapcar (lambda (x) (type-intersection x intersection))
974 (union-type-types union-type))))
975 (if (notany (lambda (x) (or (hairy-type-p x)
976 (intersection-type-p x)))
981 (defun type-intersection (&rest input-types)
982 (%type-intersection input-types))
983 (defun-cached (%type-intersection :hash-bits 8
984 :hash-function (lambda (x)
985 (logand (sxhash x) #xff)))
986 ((input-types equal))
987 (let ((simplified-types (simplify-intersections input-types)))
988 (declare (type list simplified-types))
989 ;; We want to have a canonical representation of types (or failing
990 ;; that, punt to HAIRY-TYPE). Canonical representation would have
991 ;; intersections inside unions but not vice versa, since you can
992 ;; always achieve that by the distributive rule. But we don't want
993 ;; to just apply the distributive rule, since it would be too easy
994 ;; to end up with unreasonably huge type expressions. So instead
995 ;; we try to generate a simple type by distributing the union; if
996 ;; the type can't be made simple, we punt to HAIRY-TYPE.
997 (if (and (cdr simplified-types) (some #'union-type-p simplified-types))
998 (let* ((first-union (find-if #'union-type-p simplified-types))
999 (other-types (coerce (remove first-union simplified-types)
1001 (distributed (maybe-distribute-one-union first-union
1004 (apply #'type-union distributed)
1006 :specifier `(and ,@(map 'list
1008 simplified-types)))))
1010 ((null simplified-types) *universal-type*)
1011 ((null (cdr simplified-types)) (car simplified-types))
1012 (t (%make-intersection-type
1013 (some #'type-enumerable simplified-types)
1014 simplified-types))))))
1016 (defun type-union (&rest input-types)
1017 (%type-union input-types))
1018 (defun-cached (%type-union :hash-bits 8
1019 :hash-function (lambda (x)
1020 (logand (sxhash x) #xff)))
1021 ((input-types equal))
1022 (let ((simplified-types (simplify-unions input-types)))
1024 ((null simplified-types) *empty-type*)
1025 ((null (cdr simplified-types)) (car simplified-types))
1027 (every #'type-enumerable simplified-types)
1028 simplified-types)))))
1032 (!define-type-class named)
1035 (macrolet ((frob (name var)
1037 (setq ,var (make-named-type :name ',name))
1038 (setf (info :type :kind ',name)
1039 #+sb-xc-host :defined #-sb-xc-host :primitive)
1040 (setf (info :type :builtin ',name) ,var))))
1041 ;; KLUDGE: In ANSI, * isn't really the name of a type, it's just a
1042 ;; special symbol which can be stuck in some places where an
1043 ;; ordinary type can go, e.g. (ARRAY * 1) instead of (ARRAY T 1).
1044 ;; In SBCL it also used to denote universal VALUES type.
1045 (frob * *wild-type*)
1046 (frob nil *empty-type*)
1047 (frob t *universal-type*))
1048 (setf *universal-fun-type*
1049 (make-fun-type :wild-args t
1050 :returns *wild-type*)))
1052 (!define-type-method (named :simple-=) (type1 type2)
1053 ;;(aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1054 (values (eq type1 type2) t))
1056 (!define-type-method (named :complex-=) (type1 type2)
1058 ((and (eq type2 *empty-type*)
1059 (intersection-type-p type1)
1060 ;; not allowed to be unsure on these... FIXME: keep the list
1061 ;; of CL types that are intersection types once and only
1063 (not (or (type= type1 (specifier-type 'ratio))
1064 (type= type1 (specifier-type 'keyword)))))
1065 ;; things like (AND (EQL 0) (SATISFIES ODDP)) or (AND FUNCTION
1066 ;; STREAM) can get here. In general, we can't really tell
1067 ;; whether these are equal to NIL or not, so
1069 ((type-might-contain-other-types-p type1)
1070 (invoke-complex-=-other-method type1 type2))
1071 (t (values nil t))))
1073 (!define-type-method (named :simple-subtypep) (type1 type2)
1074 (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1075 (values (or (eq type1 *empty-type*) (eq type2 *wild-type*)) t))
1077 (!define-type-method (named :complex-subtypep-arg1) (type1 type2)
1078 ;; This AVER causes problems if we write accurate methods for the
1079 ;; union (and possibly intersection) types which then delegate to
1080 ;; us; while a user shouldn't get here, because of the odd status of
1081 ;; *wild-type* a type-intersection executed by the compiler can. -
1084 ;; (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1085 (cond ((eq type1 *empty-type*)
1087 (;; When TYPE2 might be the universal type in disguise
1088 (type-might-contain-other-types-p type2)
1089 ;; Now that the UNION and HAIRY COMPLEX-SUBTYPEP-ARG2 methods
1090 ;; can delegate to us (more or less as CALL-NEXT-METHOD) when
1091 ;; they're uncertain, we can't just barf on COMPOUND-TYPE and
1092 ;; HAIRY-TYPEs as we used to. Instead we deal with the
1093 ;; problem (where at least part of the problem is cases like
1094 ;; (SUBTYPEP T '(SATISFIES FOO))
1096 ;; (SUBTYPEP T '(AND (SATISFIES FOO) (SATISFIES BAR)))
1097 ;; where the second type is a hairy type like SATISFIES, or
1098 ;; is a compound type which might contain a hairy type) by
1099 ;; returning uncertainty.
1102 ;; By elimination, TYPE1 is the universal type.
1103 (aver (eq type1 *universal-type*))
1104 ;; This case would have been picked off by the SIMPLE-SUBTYPEP
1105 ;; method, and so shouldn't appear here.
1106 (aver (not (eq type2 *universal-type*)))
1107 ;; Since TYPE2 is not EQ *UNIVERSAL-TYPE* and is not the
1108 ;; universal type in disguise, TYPE2 is not a superset of TYPE1.
1111 (!define-type-method (named :complex-subtypep-arg2) (type1 type2)
1112 (aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1113 (cond ((eq type2 *universal-type*)
1115 ((type-might-contain-other-types-p type1)
1116 ;; those types can be *EMPTY-TYPE* or *UNIVERSAL-TYPE* in
1117 ;; disguise. So we'd better delegate.
1118 (invoke-complex-subtypep-arg1-method type1 type2))
1120 ;; FIXME: This seems to rely on there only being 2 or 3
1121 ;; NAMED-TYPE values, and the exclusion of various
1122 ;; possibilities above. It would be good to explain it and/or
1123 ;; rewrite it so that it's clearer.
1124 (values (not (eq type2 *empty-type*)) t))))
1126 (!define-type-method (named :complex-intersection2) (type1 type2)
1127 ;; FIXME: This assertion failed when I added it in sbcl-0.6.11.13.
1128 ;; Perhaps when bug 85 is fixed it can be reenabled.
1129 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1130 (hierarchical-intersection2 type1 type2))
1132 (!define-type-method (named :complex-union2) (type1 type2)
1133 ;; Perhaps when bug 85 is fixed this can be reenabled.
1134 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1135 (hierarchical-union2 type1 type2))
1137 (!define-type-method (named :negate) (x)
1138 (aver (not (eq x *wild-type*)))
1140 ((eq x *universal-type*) *empty-type*)
1141 ((eq x *empty-type*) *universal-type*)
1142 (t (bug "NAMED type not universal, wild or empty: ~S" x))))
1144 (!define-type-method (named :unparse) (x)
1145 (named-type-name x))
1147 ;;;; hairy and unknown types
1149 (!define-type-method (hairy :negate) (x)
1150 (make-negation-type :type x))
1152 (!define-type-method (hairy :unparse) (x)
1153 (hairy-type-specifier x))
1155 (!define-type-method (hairy :simple-subtypep) (type1 type2)
1156 (let ((hairy-spec1 (hairy-type-specifier type1))
1157 (hairy-spec2 (hairy-type-specifier type2)))
1158 (cond ((equal-but-no-car-recursion hairy-spec1 hairy-spec2)
1161 (values nil nil)))))
1163 (!define-type-method (hairy :complex-subtypep-arg2) (type1 type2)
1164 (invoke-complex-subtypep-arg1-method type1 type2))
1166 (!define-type-method (hairy :complex-subtypep-arg1) (type1 type2)
1167 (declare (ignore type1 type2))
1170 (!define-type-method (hairy :complex-=) (type1 type2)
1171 (if (and (unknown-type-p type2)
1172 (let* ((specifier2 (unknown-type-specifier type2))
1173 (name2 (if (consp specifier2)
1176 (info :type :kind name2)))
1177 (let ((type2 (specifier-type (unknown-type-specifier type2))))
1178 (if (unknown-type-p type2)
1180 (type= type1 type2)))
1183 (!define-type-method (hairy :simple-intersection2 :complex-intersection2)
1185 (if (type= type1 type2)
1189 (!define-type-method (hairy :simple-union2)
1191 (if (type= type1 type2)
1195 (!define-type-method (hairy :simple-=) (type1 type2)
1196 (if (equal-but-no-car-recursion (hairy-type-specifier type1)
1197 (hairy-type-specifier type2))
1201 (!def-type-translator satisfies (&whole whole fun)
1202 (declare (ignore fun))
1203 ;; Check legality of arguments.
1204 (destructuring-bind (satisfies predicate-name) whole
1205 (declare (ignore satisfies))
1206 (unless (symbolp predicate-name)
1207 (error 'simple-type-error
1208 :datum predicate-name
1209 :expected-type 'symbol
1210 :format-control "The SATISFIES predicate name is not a symbol: ~S"
1211 :format-arguments (list predicate-name))))
1213 (make-hairy-type :specifier whole))
1217 (!define-type-method (negation :negate) (x)
1218 (negation-type-type x))
1220 (!define-type-method (negation :unparse) (x)
1221 (if (type= (negation-type-type x) (specifier-type 'cons))
1223 `(not ,(type-specifier (negation-type-type x)))))
1225 (!define-type-method (negation :simple-subtypep) (type1 type2)
1226 (csubtypep (negation-type-type type2) (negation-type-type type1)))
1228 (!define-type-method (negation :complex-subtypep-arg2) (type1 type2)
1229 (let* ((complement-type2 (negation-type-type type2))
1230 (intersection2 (type-intersection2 type1
1233 ;; FIXME: if uncertain, maybe try arg1?
1234 (type= intersection2 *empty-type*)
1235 (invoke-complex-subtypep-arg1-method type1 type2))))
1237 (!define-type-method (negation :complex-subtypep-arg1) (type1 type2)
1238 ;; "Incrementally extended heuristic algorithms tend inexorably toward the
1239 ;; incomprehensible." -- http://www.unlambda.com/~james/lambda/lambda.txt
1241 ;; You may not believe this. I couldn't either. But then I sat down
1242 ;; and drew lots of Venn diagrams. Comments involving a and b refer
1243 ;; to the call (subtypep '(not a) 'b) -- CSR, 2002-02-27.
1245 ;; (Several logical truths in this block are true as long as
1246 ;; b/=T. As of sbcl-0.7.1.28, it seems impossible to construct a
1247 ;; case with b=T where we actually reach this type method, but
1248 ;; we'll test for and exclude this case anyway, since future
1249 ;; maintenance might make it possible for it to end up in this
1251 (multiple-value-bind (equal certain)
1252 (type= type2 *universal-type*)
1254 (return (values nil nil)))
1256 (return (values t t))))
1257 (let ((complement-type1 (negation-type-type type1)))
1258 ;; Do the special cases first, in order to give us a chance if
1259 ;; subtype/supertype relationships are hairy.
1260 (multiple-value-bind (equal certain)
1261 (type= complement-type1 type2)
1262 ;; If a = b, ~a is not a subtype of b (unless b=T, which was
1265 (return (values nil nil)))
1267 (return (values nil t))))
1268 ;; KLUDGE: ANSI requires that the SUBTYPEP result between any
1269 ;; two built-in atomic type specifiers never be uncertain. This
1270 ;; is hard to do cleanly for the built-in types whose
1271 ;; definitions include (NOT FOO), i.e. CONS and RATIO. However,
1272 ;; we can do it with this hack, which uses our global knowledge
1273 ;; that our implementation of the type system uses disjoint
1274 ;; implementation types to represent disjoint sets (except when
1275 ;; types are contained in other types). (This is a KLUDGE
1276 ;; because it's fragile. Various changes in internal
1277 ;; representation in the type system could make it start
1278 ;; confidently returning incorrect results.) -- WHN 2002-03-08
1279 (unless (or (type-might-contain-other-types-p complement-type1)
1280 (type-might-contain-other-types-p type2))
1281 ;; Because of the way our types which don't contain other
1282 ;; types are disjoint subsets of the space of possible values,
1283 ;; (SUBTYPEP '(NOT AA) 'B)=NIL when AA and B are simple (and B
1284 ;; is not T, as checked above).
1285 (return (values nil t)))
1286 ;; The old (TYPE= TYPE1 TYPE2) branch would never be taken, as
1287 ;; TYPE1 and TYPE2 will only be equal if they're both NOT types,
1288 ;; and then the :SIMPLE-SUBTYPEP method would be used instead.
1289 ;; But a CSUBTYPEP relationship might still hold:
1290 (multiple-value-bind (equal certain)
1291 (csubtypep complement-type1 type2)
1292 ;; If a is a subtype of b, ~a is not a subtype of b (unless
1293 ;; b=T, which was excluded above).
1295 (return (values nil nil)))
1297 (return (values nil t))))
1298 (multiple-value-bind (equal certain)
1299 (csubtypep type2 complement-type1)
1300 ;; If b is a subtype of a, ~a is not a subtype of b. (FIXME:
1301 ;; That's not true if a=T. Do we know at this point that a is
1304 (return (values nil nil)))
1306 (return (values nil t))))
1307 ;; old CSR comment ca. 0.7.2, now obsoleted by the SIMPLE-CTYPE?
1308 ;; KLUDGE case above: Other cases here would rely on being able
1309 ;; to catch all possible cases, which the fragility of this type
1310 ;; system doesn't inspire me; for instance, if a is type= to ~b,
1311 ;; then we want T, T; if this is not the case and the types are
1312 ;; disjoint (have an intersection of *empty-type*) then we want
1313 ;; NIL, T; else if the union of a and b is the *universal-type*
1314 ;; then we want T, T. So currently we still claim to be unsure
1315 ;; about e.g. (subtypep '(not fixnum) 'single-float).
1317 ;; OTOH we might still get here:
1320 (!define-type-method (negation :complex-=) (type1 type2)
1321 ;; (NOT FOO) isn't equivalent to anything that's not a negation
1322 ;; type, except possibly a type that might contain it in disguise.
1323 (declare (ignore type2))
1324 (if (type-might-contain-other-types-p type1)
1328 (!define-type-method (negation :simple-intersection2) (type1 type2)
1329 (let ((not1 (negation-type-type type1))
1330 (not2 (negation-type-type type2)))
1332 ((csubtypep not1 not2) type2)
1333 ((csubtypep not2 not1) type1)
1334 ;; Why no analagous clause to the disjoint in the SIMPLE-UNION2
1335 ;; method, below? The clause would read
1337 ;; ((EQ (TYPE-UNION NOT1 NOT2) *UNIVERSAL-TYPE*) *EMPTY-TYPE*)
1339 ;; but with proper canonicalization of negation types, there's
1340 ;; no way of constructing two negation types with union of their
1341 ;; negations being the universal type.
1343 (aver (not (eq (type-union not1 not2) *universal-type*)))
1346 (!define-type-method (negation :complex-intersection2) (type1 type2)
1348 ((csubtypep type1 (negation-type-type type2)) *empty-type*)
1349 ((eq (type-intersection type1 (negation-type-type type2)) *empty-type*)
1353 (!define-type-method (negation :simple-union2) (type1 type2)
1354 (let ((not1 (negation-type-type type1))
1355 (not2 (negation-type-type type2)))
1357 ((csubtypep not1 not2) type1)
1358 ((csubtypep not2 not1) type2)
1359 ((eq (type-intersection not1 not2) *empty-type*)
1363 (!define-type-method (negation :complex-union2) (type1 type2)
1365 ((csubtypep (negation-type-type type2) type1) *universal-type*)
1366 ((eq (type-intersection type1 (negation-type-type type2)) *empty-type*)
1370 (!define-type-method (negation :simple-=) (type1 type2)
1371 (type= (negation-type-type type1) (negation-type-type type2)))
1373 (!def-type-translator not (typespec)
1374 (type-negation (specifier-type typespec)))
1378 (!define-type-class number)
1380 (declaim (inline numeric-type-equal))
1381 (defun numeric-type-equal (type1 type2)
1382 (and (eq (numeric-type-class type1) (numeric-type-class type2))
1383 (eq (numeric-type-format type1) (numeric-type-format type2))
1384 (eq (numeric-type-complexp type1) (numeric-type-complexp type2))))
1386 (!define-type-method (number :simple-=) (type1 type2)
1388 (and (numeric-type-equal type1 type2)
1389 (equalp (numeric-type-low type1) (numeric-type-low type2))
1390 (equalp (numeric-type-high type1) (numeric-type-high type2)))
1393 (!define-type-method (number :negate) (type)
1394 (if (and (null (numeric-type-low type)) (null (numeric-type-high type)))
1395 (make-negation-type :type type)
1398 :type (modified-numeric-type type :low nil :high nil))
1400 ((null (numeric-type-low type))
1401 (modified-numeric-type
1403 :low (let ((h (numeric-type-high type)))
1404 (if (consp h) (car h) (list h)))
1406 ((null (numeric-type-high type))
1407 (modified-numeric-type
1410 :high (let ((l (numeric-type-low type)))
1411 (if (consp l) (car l) (list l)))))
1413 (modified-numeric-type
1416 :high (let ((l (numeric-type-low type)))
1417 (if (consp l) (car l) (list l))))
1418 (modified-numeric-type
1420 :low (let ((h (numeric-type-high type)))
1421 (if (consp h) (car h) (list h)))
1424 (!define-type-method (number :unparse) (type)
1425 (let* ((complexp (numeric-type-complexp type))
1426 (low (numeric-type-low type))
1427 (high (numeric-type-high type))
1428 (base (case (numeric-type-class type)
1430 (rational 'rational)
1431 (float (or (numeric-type-format type) 'float))
1434 (cond ((and (eq base 'integer) high low)
1435 (let ((high-count (logcount high))
1436 (high-length (integer-length high)))
1438 (cond ((= high 0) '(integer 0 0))
1440 ((and (= high-count high-length)
1441 (plusp high-length))
1442 `(unsigned-byte ,high-length))
1444 `(mod ,(1+ high)))))
1445 ((and (= low sb!xc:most-negative-fixnum)
1446 (= high sb!xc:most-positive-fixnum))
1448 ((and (= low (lognot high))
1449 (= high-count high-length)
1451 `(signed-byte ,(1+ high-length)))
1453 `(integer ,low ,high)))))
1454 (high `(,base ,(or low '*) ,high))
1456 (if (and (eq base 'integer) (= low 0))
1464 (if (eq base+bounds 'real)
1466 `(complex ,base+bounds)))
1468 (aver (eq base+bounds 'real))
1471 ;;; Return true if X is "less than or equal" to Y, taking open bounds
1472 ;;; into consideration. CLOSED is the predicate used to test the bound
1473 ;;; on a closed interval (e.g. <=), and OPEN is the predicate used on
1474 ;;; open bounds (e.g. <). Y is considered to be the outside bound, in
1475 ;;; the sense that if it is infinite (NIL), then the test succeeds,
1476 ;;; whereas if X is infinite, then the test fails (unless Y is also
1479 ;;; This is for comparing bounds of the same kind, e.g. upper and
1480 ;;; upper. Use NUMERIC-BOUND-TEST* for different kinds of bounds.
1481 (defmacro numeric-bound-test (x y closed open)
1486 (,closed (car ,x) (car ,y))
1487 (,closed (car ,x) ,y)))
1493 ;;; This is used to compare upper and lower bounds. This is different
1494 ;;; from the same-bound case:
1495 ;;; -- Since X = NIL is -infinity, whereas y = NIL is +infinity, we
1496 ;;; return true if *either* arg is NIL.
1497 ;;; -- an open inner bound is "greater" and also squeezes the interval,
1498 ;;; causing us to use the OPEN test for those cases as well.
1499 (defmacro numeric-bound-test* (x y closed open)
1504 (,open (car ,x) (car ,y))
1505 (,open (car ,x) ,y)))
1511 ;;; Return whichever of the numeric bounds X and Y is "maximal"
1512 ;;; according to the predicates CLOSED (e.g. >=) and OPEN (e.g. >).
1513 ;;; This is only meaningful for maximizing like bounds, i.e. upper and
1514 ;;; upper. If MAX-P is true, then we return NIL if X or Y is NIL,
1515 ;;; otherwise we return the other arg.
1516 (defmacro numeric-bound-max (x y closed open max-p)
1519 `(cond ((not ,n-x) ,(if max-p nil n-y))
1520 ((not ,n-y) ,(if max-p nil n-x))
1523 (if (,closed (car ,n-x) (car ,n-y)) ,n-x ,n-y)
1524 (if (,open (car ,n-x) ,n-y) ,n-x ,n-y)))
1527 (if (,open (car ,n-y) ,n-x) ,n-y ,n-x)
1528 (if (,closed ,n-y ,n-x) ,n-y ,n-x))))))
1530 (!define-type-method (number :simple-subtypep) (type1 type2)
1531 (let ((class1 (numeric-type-class type1))
1532 (class2 (numeric-type-class type2))
1533 (complexp2 (numeric-type-complexp type2))
1534 (format2 (numeric-type-format type2))
1535 (low1 (numeric-type-low type1))
1536 (high1 (numeric-type-high type1))
1537 (low2 (numeric-type-low type2))
1538 (high2 (numeric-type-high type2)))
1539 ;; If one is complex and the other isn't, they are disjoint.
1540 (cond ((not (or (eq (numeric-type-complexp type1) complexp2)
1543 ;; If the classes are specified and different, the types are
1544 ;; disjoint unless type2 is RATIONAL and type1 is INTEGER.
1545 ;; [ or type1 is INTEGER and type2 is of the form (RATIONAL
1546 ;; X X) for integral X, but this is dealt with in the
1547 ;; canonicalization inside MAKE-NUMERIC-TYPE ]
1548 ((not (or (eq class1 class2)
1550 (and (eq class1 'integer) (eq class2 'rational))))
1552 ;; If the float formats are specified and different, the types
1554 ((not (or (eq (numeric-type-format type1) format2)
1557 ;; Check the bounds.
1558 ((and (numeric-bound-test low1 low2 >= >)
1559 (numeric-bound-test high1 high2 <= <))
1564 (!define-superclasses number ((number)) !cold-init-forms)
1566 ;;; If the high bound of LOW is adjacent to the low bound of HIGH,
1567 ;;; then return true, otherwise NIL.
1568 (defun numeric-types-adjacent (low high)
1569 (let ((low-bound (numeric-type-high low))
1570 (high-bound (numeric-type-low high)))
1571 (cond ((not (and low-bound high-bound)) nil)
1572 ((and (consp low-bound) (consp high-bound)) nil)
1574 (let ((low-value (car low-bound)))
1575 (or (eql low-value high-bound)
1577 (load-time-value (make-unportable-float
1578 :single-float-negative-zero)))
1579 (eql high-bound 0f0))
1580 (and (eql low-value 0f0)
1582 (load-time-value (make-unportable-float
1583 :single-float-negative-zero))))
1585 (load-time-value (make-unportable-float
1586 :double-float-negative-zero)))
1587 (eql high-bound 0d0))
1588 (and (eql low-value 0d0)
1590 (load-time-value (make-unportable-float
1591 :double-float-negative-zero)))))))
1593 (let ((high-value (car high-bound)))
1594 (or (eql high-value low-bound)
1595 (and (eql high-value
1596 (load-time-value (make-unportable-float
1597 :single-float-negative-zero)))
1598 (eql low-bound 0f0))
1599 (and (eql high-value 0f0)
1601 (load-time-value (make-unportable-float
1602 :single-float-negative-zero))))
1603 (and (eql high-value
1604 (load-time-value (make-unportable-float
1605 :double-float-negative-zero)))
1606 (eql low-bound 0d0))
1607 (and (eql high-value 0d0)
1609 (load-time-value (make-unportable-float
1610 :double-float-negative-zero)))))))
1611 ((and (eq (numeric-type-class low) 'integer)
1612 (eq (numeric-type-class high) 'integer))
1613 (eql (1+ low-bound) high-bound))
1617 ;;; Return a numeric type that is a supertype for both TYPE1 and TYPE2.
1619 ;;; Old comment, probably no longer applicable:
1621 ;;; ### Note: we give up early to keep from dropping lots of
1622 ;;; information on the floor by returning overly general types.
1623 (!define-type-method (number :simple-union2) (type1 type2)
1624 (declare (type numeric-type type1 type2))
1625 (cond ((csubtypep type1 type2) type2)
1626 ((csubtypep type2 type1) type1)
1628 (let ((class1 (numeric-type-class type1))
1629 (format1 (numeric-type-format type1))
1630 (complexp1 (numeric-type-complexp type1))
1631 (class2 (numeric-type-class type2))
1632 (format2 (numeric-type-format type2))
1633 (complexp2 (numeric-type-complexp type2)))
1635 ((and (eq class1 class2)
1636 (eq format1 format2)
1637 (eq complexp1 complexp2)
1638 (or (numeric-types-intersect type1 type2)
1639 (numeric-types-adjacent type1 type2)
1640 (numeric-types-adjacent type2 type1)))
1645 :low (numeric-bound-max (numeric-type-low type1)
1646 (numeric-type-low type2)
1648 :high (numeric-bound-max (numeric-type-high type1)
1649 (numeric-type-high type2)
1651 ;; FIXME: These two clauses are almost identical, and the
1652 ;; consequents are in fact identical in every respect.
1653 ((and (eq class1 'rational)
1654 (eq class2 'integer)
1655 (eq format1 format2)
1656 (eq complexp1 complexp2)
1657 (integerp (numeric-type-low type2))
1658 (integerp (numeric-type-high type2))
1659 (= (numeric-type-low type2) (numeric-type-high type2))
1660 (or (numeric-types-adjacent type1 type2)
1661 (numeric-types-adjacent type2 type1)))
1666 :low (numeric-bound-max (numeric-type-low type1)
1667 (numeric-type-low type2)
1669 :high (numeric-bound-max (numeric-type-high type1)
1670 (numeric-type-high type2)
1672 ((and (eq class1 'integer)
1673 (eq class2 'rational)
1674 (eq format1 format2)
1675 (eq complexp1 complexp2)
1676 (integerp (numeric-type-low type1))
1677 (integerp (numeric-type-high type1))
1678 (= (numeric-type-low type1) (numeric-type-high type1))
1679 (or (numeric-types-adjacent type1 type2)
1680 (numeric-types-adjacent type2 type1)))
1685 :low (numeric-bound-max (numeric-type-low type1)
1686 (numeric-type-low type2)
1688 :high (numeric-bound-max (numeric-type-high type1)
1689 (numeric-type-high type2)
1695 (setf (info :type :kind 'number)
1696 #+sb-xc-host :defined #-sb-xc-host :primitive)
1697 (setf (info :type :builtin 'number)
1698 (make-numeric-type :complexp nil)))
1700 (!def-type-translator complex (&optional (typespec '*))
1701 (if (eq typespec '*)
1702 (make-numeric-type :complexp :complex)
1703 (labels ((not-numeric ()
1704 (error "The component type for COMPLEX is not numeric: ~S"
1707 (error "The component type for COMPLEX is not real: ~S"
1709 (complex1 (component-type)
1710 (unless (numeric-type-p component-type)
1712 (when (eq (numeric-type-complexp component-type) :complex)
1714 (modified-numeric-type component-type :complexp :complex))
1715 (complex-union (component)
1716 (unless (numberp component)
1718 ;; KLUDGE: This TYPECASE more or less does
1719 ;; (UPGRADED-COMPLEX-PART-TYPE (TYPE-OF COMPONENT)),
1720 ;; (plus a small hack to treat (EQL COMPONENT 0) specially)
1721 ;; but uses logic cut and pasted from the DEFUN of
1722 ;; UPGRADED-COMPLEX-PART-TYPE. That's fragile, because
1723 ;; changing the definition of UPGRADED-COMPLEX-PART-TYPE
1724 ;; would tend to break the code here. Unfortunately,
1725 ;; though, reusing UPGRADED-COMPLEX-PART-TYPE here
1726 ;; would cause another kind of fragility, because
1727 ;; ANSI's definition of TYPE-OF is so weak that e.g.
1728 ;; (UPGRADED-COMPLEX-PART-TYPE (TYPE-OF 1/2)) could
1729 ;; end up being (UPGRADED-COMPLEX-PART-TYPE 'REAL)
1730 ;; instead of (UPGRADED-COMPLEX-PART-TYPE 'RATIONAL).
1731 ;; So using TYPE-OF would mean that ANSI-conforming
1732 ;; maintenance changes in TYPE-OF could break the code here.
1733 ;; It's not clear how best to fix this. -- WHN 2002-01-21,
1734 ;; trying to summarize CSR's concerns in his patch
1736 (complex (error "The component type for COMPLEX (EQL X) ~
1739 ((eql 0) (specifier-type nil)) ; as required by ANSI
1740 (single-float (specifier-type '(complex single-float)))
1741 (double-float (specifier-type '(complex double-float)))
1743 (long-float (specifier-type '(complex long-float)))
1744 (rational (specifier-type '(complex rational)))
1745 (t (specifier-type '(complex real))))))
1746 (let ((ctype (specifier-type typespec)))
1748 (numeric-type (complex1 ctype))
1749 (union-type (apply #'type-union
1750 ;; FIXME: This code could suffer from
1751 ;; (admittedly very obscure) cases of
1752 ;; bug 145 e.g. when TYPE is
1753 ;; (OR (AND INTEGER (SATISFIES ODDP))
1754 ;; (AND FLOAT (SATISFIES FOO))
1755 ;; and not even report the problem very well.
1757 (union-type-types ctype))))
1758 ;; MEMBER-TYPE is almost the same as UNION-TYPE, but
1759 ;; there's a gotcha: (COMPLEX (EQL 0)) is, according to
1760 ;; ANSI, equal to type NIL, the empty set.
1761 (member-type (apply #'type-union
1762 (mapcar #'complex-union
1763 (member-type-members ctype))))
1765 (multiple-value-bind (subtypep certainly)
1766 (csubtypep ctype (specifier-type 'real))
1767 (if (and (not subtypep) certainly)
1769 ;; ANSI just says that TYPESPEC is any subtype of
1770 ;; type REAL, not necessarily a NUMERIC-TYPE. In
1771 ;; particular, at this point TYPESPEC could legally be
1772 ;; an intersection type like (AND REAL (SATISFIES ODDP)),
1773 ;; in which case we fall through the logic above and
1774 ;; end up here, stumped.
1775 (bug "~@<(known bug #145): The type ~S is too hairy to be
1776 used for a COMPLEX component.~:@>"
1779 ;;; If X is *, return NIL, otherwise return the bound, which must be a
1780 ;;; member of TYPE or a one-element list of a member of TYPE.
1781 #!-sb-fluid (declaim (inline canonicalized-bound))
1782 (defun canonicalized-bound (bound type)
1783 (cond ((eq bound '*) nil)
1784 ((or (sb!xc:typep bound type)
1786 (sb!xc:typep (car bound) type)
1787 (null (cdr bound))))
1790 (error "Bound is not ~S, a ~S or a list of a ~S: ~S"
1796 (!def-type-translator integer (&optional (low '*) (high '*))
1797 (let* ((l (canonicalized-bound low 'integer))
1798 (lb (if (consp l) (1+ (car l)) l))
1799 (h (canonicalized-bound high 'integer))
1800 (hb (if (consp h) (1- (car h)) h)))
1801 (if (and hb lb (< hb lb))
1803 (make-numeric-type :class 'integer
1805 :enumerable (not (null (and l h)))
1809 (defmacro !def-bounded-type (type class format)
1810 `(!def-type-translator ,type (&optional (low '*) (high '*))
1811 (let ((lb (canonicalized-bound low ',type))
1812 (hb (canonicalized-bound high ',type)))
1813 (if (not (numeric-bound-test* lb hb <= <))
1815 (make-numeric-type :class ',class
1820 (!def-bounded-type rational rational nil)
1822 ;;; Unlike CMU CL, we represent the types FLOAT and REAL as
1823 ;;; UNION-TYPEs of more primitive types, in order to make
1824 ;;; type representation more unique, avoiding problems in the
1825 ;;; simplification of things like
1826 ;;; (subtypep '(or (single-float -1.0 1.0) (single-float 0.1))
1827 ;;; '(or (real -1 7) (single-float 0.1) (single-float -1.0 1.0)))
1828 ;;; When we allowed REAL to remain as a separate NUMERIC-TYPE,
1829 ;;; it was too easy for the first argument to be simplified to
1830 ;;; '(SINGLE-FLOAT -1.0), and for the second argument to be simplified
1831 ;;; to '(OR (REAL -1 7) (SINGLE-FLOAT 0.1)) and then for the
1832 ;;; SUBTYPEP to fail (returning NIL,T instead of T,T) because
1833 ;;; the first argument can't be seen to be a subtype of any of the
1834 ;;; terms in the second argument.
1836 ;;; The old CMU CL way was:
1837 ;;; (!def-bounded-type float float nil)
1838 ;;; (!def-bounded-type real nil nil)
1840 ;;; FIXME: If this new way works for a while with no weird new
1841 ;;; problems, we can go back and rip out support for separate FLOAT
1842 ;;; and REAL flavors of NUMERIC-TYPE. The new way was added in
1843 ;;; sbcl-0.6.11.22, 2001-03-21.
1845 ;;; FIXME: It's probably necessary to do something to fix the
1846 ;;; analogous problem with INTEGER and RATIONAL types. Perhaps
1847 ;;; bounded RATIONAL types should be represented as (OR RATIO INTEGER).
1848 (defun coerce-bound (bound type inner-coerce-bound-fun)
1849 (declare (type function inner-coerce-bound-fun))
1850 (cond ((eql bound '*)
1853 (destructuring-bind (inner-bound) bound
1854 (list (funcall inner-coerce-bound-fun inner-bound type))))
1856 (funcall inner-coerce-bound-fun bound type))))
1857 (defun inner-coerce-real-bound (bound type)
1859 (rational (rationalize bound))
1860 (float (if (floatp bound)
1862 ;; Coerce to the widest float format available, to
1863 ;; avoid unnecessary loss of precision:
1864 (coerce bound 'long-float)))))
1865 (defun coerced-real-bound (bound type)
1866 (coerce-bound bound type #'inner-coerce-real-bound))
1867 (defun coerced-float-bound (bound type)
1868 (coerce-bound bound type #'coerce))
1869 (!def-type-translator real (&optional (low '*) (high '*))
1870 (specifier-type `(or (float ,(coerced-real-bound low 'float)
1871 ,(coerced-real-bound high 'float))
1872 (rational ,(coerced-real-bound low 'rational)
1873 ,(coerced-real-bound high 'rational)))))
1874 (!def-type-translator float (&optional (low '*) (high '*))
1876 `(or (single-float ,(coerced-float-bound low 'single-float)
1877 ,(coerced-float-bound high 'single-float))
1878 (double-float ,(coerced-float-bound low 'double-float)
1879 ,(coerced-float-bound high 'double-float))
1880 #!+long-float ,(error "stub: no long float support yet"))))
1882 (defmacro !define-float-format (f)
1883 `(!def-bounded-type ,f float ,f))
1885 (!define-float-format short-float)
1886 (!define-float-format single-float)
1887 (!define-float-format double-float)
1888 (!define-float-format long-float)
1890 (defun numeric-types-intersect (type1 type2)
1891 (declare (type numeric-type type1 type2))
1892 (let* ((class1 (numeric-type-class type1))
1893 (class2 (numeric-type-class type2))
1894 (complexp1 (numeric-type-complexp type1))
1895 (complexp2 (numeric-type-complexp type2))
1896 (format1 (numeric-type-format type1))
1897 (format2 (numeric-type-format type2))
1898 (low1 (numeric-type-low type1))
1899 (high1 (numeric-type-high type1))
1900 (low2 (numeric-type-low type2))
1901 (high2 (numeric-type-high type2)))
1902 ;; If one is complex and the other isn't, then they are disjoint.
1903 (cond ((not (or (eq complexp1 complexp2)
1904 (null complexp1) (null complexp2)))
1906 ;; If either type is a float, then the other must either be
1907 ;; specified to be a float or unspecified. Otherwise, they
1909 ((and (eq class1 'float)
1910 (not (member class2 '(float nil)))) nil)
1911 ((and (eq class2 'float)
1912 (not (member class1 '(float nil)))) nil)
1913 ;; If the float formats are specified and different, the
1914 ;; types are disjoint.
1915 ((not (or (eq format1 format2) (null format1) (null format2)))
1918 ;; Check the bounds. This is a bit odd because we must
1919 ;; always have the outer bound of the interval as the
1921 (if (numeric-bound-test high1 high2 <= <)
1922 (or (and (numeric-bound-test low1 low2 >= >)
1923 (numeric-bound-test* low1 high2 <= <))
1924 (and (numeric-bound-test low2 low1 >= >)
1925 (numeric-bound-test* low2 high1 <= <)))
1926 (or (and (numeric-bound-test* low2 high1 <= <)
1927 (numeric-bound-test low2 low1 >= >))
1928 (and (numeric-bound-test high2 high1 <= <)
1929 (numeric-bound-test* high2 low1 >= >))))))))
1931 ;;; Take the numeric bound X and convert it into something that can be
1932 ;;; used as a bound in a numeric type with the specified CLASS and
1933 ;;; FORMAT. If UP-P is true, then we round up as needed, otherwise we
1934 ;;; round down. UP-P true implies that X is a lower bound, i.e. (N) > N.
1936 ;;; This is used by NUMERIC-TYPE-INTERSECTION to mash the bound into
1937 ;;; the appropriate type number. X may only be a float when CLASS is
1940 ;;; ### Note: it is possible for the coercion to a float to overflow
1941 ;;; or underflow. This happens when the bound doesn't fit in the
1942 ;;; specified format. In this case, we should really return the
1943 ;;; appropriate {Most | Least}-{Positive | Negative}-XXX-Float float
1944 ;;; of desired format. But these conditions aren't currently signalled
1945 ;;; in any useful way.
1947 ;;; Also, when converting an open rational bound into a float we
1948 ;;; should probably convert it to a closed bound of the closest float
1949 ;;; in the specified format. KLUDGE: In general, open float bounds are
1950 ;;; screwed up. -- (comment from original CMU CL)
1951 (defun round-numeric-bound (x class format up-p)
1953 (let ((cx (if (consp x) (car x) x)))
1957 (if (and (consp x) (integerp cx))
1958 (if up-p (1+ cx) (1- cx))
1959 (if up-p (ceiling cx) (floor cx))))
1961 (let ((res (if format (coerce cx format) (float cx))))
1962 (if (consp x) (list res) res)))))
1965 ;;; Handle the case of type intersection on two numeric types. We use
1966 ;;; TYPES-EQUAL-OR-INTERSECT to throw out the case of types with no
1967 ;;; intersection. If an attribute in TYPE1 is unspecified, then we use
1968 ;;; TYPE2's attribute, which must be at least as restrictive. If the
1969 ;;; types intersect, then the only attributes that can be specified
1970 ;;; and different are the class and the bounds.
1972 ;;; When the class differs, we use the more restrictive class. The
1973 ;;; only interesting case is RATIONAL/INTEGER, since RATIONAL includes
1976 ;;; We make the result lower (upper) bound the maximum (minimum) of
1977 ;;; the argument lower (upper) bounds. We convert the bounds into the
1978 ;;; appropriate numeric type before maximizing. This avoids possible
1979 ;;; confusion due to mixed-type comparisons (but I think the result is
1981 (!define-type-method (number :simple-intersection2) (type1 type2)
1982 (declare (type numeric-type type1 type2))
1983 (if (numeric-types-intersect type1 type2)
1984 (let* ((class1 (numeric-type-class type1))
1985 (class2 (numeric-type-class type2))
1986 (class (ecase class1
1988 ((integer float) class1)
1989 (rational (if (eq class2 'integer)
1992 (format (or (numeric-type-format type1)
1993 (numeric-type-format type2))))
1997 :complexp (or (numeric-type-complexp type1)
1998 (numeric-type-complexp type2))
1999 :low (numeric-bound-max
2000 (round-numeric-bound (numeric-type-low type1)
2002 (round-numeric-bound (numeric-type-low type2)
2005 :high (numeric-bound-max
2006 (round-numeric-bound (numeric-type-high type1)
2008 (round-numeric-bound (numeric-type-high type2)
2013 ;;; Given two float formats, return the one with more precision. If
2014 ;;; either one is null, return NIL.
2015 (defun float-format-max (f1 f2)
2017 (dolist (f *float-formats* (error "bad float format: ~S" f1))
2018 (when (or (eq f f1) (eq f f2))
2021 ;;; Return the result of an operation on TYPE1 and TYPE2 according to
2022 ;;; the rules of numeric contagion. This is always NUMBER, some float
2023 ;;; format (possibly complex) or RATIONAL. Due to rational
2024 ;;; canonicalization, there isn't much we can do here with integers or
2025 ;;; rational complex numbers.
2027 ;;; If either argument is not a NUMERIC-TYPE, then return NUMBER. This
2028 ;;; is useful mainly for allowing types that are technically numbers,
2029 ;;; but not a NUMERIC-TYPE.
2030 (defun numeric-contagion (type1 type2)
2031 (if (and (numeric-type-p type1) (numeric-type-p type2))
2032 (let ((class1 (numeric-type-class type1))
2033 (class2 (numeric-type-class type2))
2034 (format1 (numeric-type-format type1))
2035 (format2 (numeric-type-format type2))
2036 (complexp1 (numeric-type-complexp type1))
2037 (complexp2 (numeric-type-complexp type2)))
2038 (cond ((or (null complexp1)
2040 (specifier-type 'number))
2044 :format (ecase class2
2045 (float (float-format-max format1 format2))
2046 ((integer rational) format1)
2048 ;; A double-float with any real number is a
2051 (if (eq format1 'double-float)
2054 ;; A long-float with any real number is a
2057 (if (eq format1 'long-float)
2060 :complexp (if (or (eq complexp1 :complex)
2061 (eq complexp2 :complex))
2064 ((eq class2 'float) (numeric-contagion type2 type1))
2065 ((and (eq complexp1 :real) (eq complexp2 :real))
2067 :class (and class1 class2 'rational)
2070 (specifier-type 'number))))
2071 (specifier-type 'number)))
2075 (!define-type-class array)
2077 ;;; What this does depends on the setting of the
2078 ;;; *USE-IMPLEMENTATION-TYPES* switch. If true, return the specialized
2079 ;;; element type, otherwise return the original element type.
2080 (defun specialized-element-type-maybe (type)
2081 (declare (type array-type type))
2082 (if *use-implementation-types*
2083 (array-type-specialized-element-type type)
2084 (array-type-element-type type)))
2086 (!define-type-method (array :simple-=) (type1 type2)
2087 (if (or (unknown-type-p (array-type-element-type type1))
2088 (unknown-type-p (array-type-element-type type2)))
2089 (multiple-value-bind (equalp certainp)
2090 (type= (array-type-element-type type1)
2091 (array-type-element-type type2))
2092 ;; By its nature, the call to TYPE= should never return NIL,
2093 ;; T, as we don't know what the UNKNOWN-TYPE will grow up to
2094 ;; be. -- CSR, 2002-08-19
2095 (aver (not (and (not equalp) certainp)))
2096 (values equalp certainp))
2097 (values (and (equal (array-type-dimensions type1)
2098 (array-type-dimensions type2))
2099 (eq (array-type-complexp type1)
2100 (array-type-complexp type2))
2101 (type= (specialized-element-type-maybe type1)
2102 (specialized-element-type-maybe type2)))
2105 (!define-type-method (array :negate) (type)
2106 ;; FIXME (and hint to PFD): we're vulnerable here to attacks of the
2107 ;; form "are (AND ARRAY (NOT (ARRAY T))) and (OR (ARRAY BIT) (ARRAY
2108 ;; NIL) (ARRAY CHAR) ...) equivalent?" -- CSR, 2003-12-10
2109 (make-negation-type :type type))
2111 (!define-type-method (array :unparse) (type)
2112 (let ((dims (array-type-dimensions type))
2113 (eltype (type-specifier (array-type-element-type type)))
2114 (complexp (array-type-complexp type)))
2117 (if complexp 'array 'simple-array)
2118 (if complexp `(array ,eltype) `(simple-array ,eltype))))
2119 ((= (length dims) 1)
2121 (if (eq (car dims) '*)
2124 (base-char 'base-string)
2126 (t `(vector ,eltype)))
2128 (bit `(bit-vector ,(car dims)))
2129 (base-char `(base-string ,(car dims)))
2130 (t `(vector ,eltype ,(car dims)))))
2131 (if (eq (car dims) '*)
2133 (bit 'simple-bit-vector)
2134 (base-char 'simple-base-string)
2135 ((t) 'simple-vector)
2136 (t `(simple-array ,eltype (*))))
2138 (bit `(simple-bit-vector ,(car dims)))
2139 (base-char `(simple-base-string ,(car dims)))
2140 ((t) `(simple-vector ,(car dims)))
2141 (t `(simple-array ,eltype ,dims))))))
2144 `(array ,eltype ,dims)
2145 `(simple-array ,eltype ,dims))))))
2147 (!define-type-method (array :simple-subtypep) (type1 type2)
2148 (let ((dims1 (array-type-dimensions type1))
2149 (dims2 (array-type-dimensions type2))
2150 (complexp2 (array-type-complexp type2)))
2151 (cond (;; not subtypep unless dimensions are compatible
2152 (not (or (eq dims2 '*)
2153 (and (not (eq dims1 '*))
2154 ;; (sbcl-0.6.4 has trouble figuring out that
2155 ;; DIMS1 and DIMS2 must be lists at this
2156 ;; point, and knowing that is important to
2157 ;; compiling EVERY efficiently.)
2158 (= (length (the list dims1))
2159 (length (the list dims2)))
2160 (every (lambda (x y)
2161 (or (eq y '*) (eql x y)))
2163 (the list dims2)))))
2165 ;; not subtypep unless complexness is compatible
2166 ((not (or (eq complexp2 :maybe)
2167 (eq (array-type-complexp type1) complexp2)))
2169 ;; Since we didn't fail any of the tests above, we win
2170 ;; if the TYPE2 element type is wild.
2171 ((eq (array-type-element-type type2) *wild-type*)
2173 (;; Since we didn't match any of the special cases above, we
2174 ;; can't give a good answer unless both the element types
2175 ;; have been defined.
2176 (or (unknown-type-p (array-type-element-type type1))
2177 (unknown-type-p (array-type-element-type type2)))
2179 (;; Otherwise, the subtype relationship holds iff the
2180 ;; types are equal, and they're equal iff the specialized
2181 ;; element types are identical.
2183 (values (type= (specialized-element-type-maybe type1)
2184 (specialized-element-type-maybe type2))
2187 ;;; FIXME: is this dead?
2188 (!define-superclasses array
2189 ((base-string base-string)
2194 (defun array-types-intersect (type1 type2)
2195 (declare (type array-type type1 type2))
2196 (let ((dims1 (array-type-dimensions type1))
2197 (dims2 (array-type-dimensions type2))
2198 (complexp1 (array-type-complexp type1))
2199 (complexp2 (array-type-complexp type2)))
2200 ;; See whether dimensions are compatible.
2201 (cond ((not (or (eq dims1 '*) (eq dims2 '*)
2202 (and (= (length dims1) (length dims2))
2203 (every (lambda (x y)
2204 (or (eq x '*) (eq y '*) (= x y)))
2207 ;; See whether complexpness is compatible.
2208 ((not (or (eq complexp1 :maybe)
2209 (eq complexp2 :maybe)
2210 (eq complexp1 complexp2)))
2214 ;; If either element type is wild, then they intersect.
2215 ;; Otherwise, the types must be identical.
2217 ;; FIXME: There seems to have been a fair amount of
2218 ;; confusion about the distinction between requested element
2219 ;; type and specialized element type; here is one of
2220 ;; them. If we request an array to hold objects of an
2221 ;; unknown type, we can do no better than represent that
2222 ;; type as an array specialized on wild-type. We keep the
2223 ;; requested element-type in the -ELEMENT-TYPE slot, and
2224 ;; *WILD-TYPE* in the -SPECIALIZED-ELEMENT-TYPE. So, here,
2225 ;; we must test for the SPECIALIZED slot being *WILD-TYPE*,
2226 ;; not just the ELEMENT-TYPE slot. Maybe the return value
2227 ;; in that specific case should be T, NIL? Or maybe this
2228 ;; function should really be called
2229 ;; ARRAY-TYPES-COULD-POSSIBLY-INTERSECT? In any case, this
2230 ;; was responsible for bug #123, and this whole issue could
2231 ;; do with a rethink and/or a rewrite. -- CSR, 2002-08-21
2232 ((or (eq (array-type-specialized-element-type type1) *wild-type*)
2233 (eq (array-type-specialized-element-type type2) *wild-type*)
2234 (type= (specialized-element-type-maybe type1)
2235 (specialized-element-type-maybe type2)))
2241 (!define-type-method (array :simple-intersection2) (type1 type2)
2242 (declare (type array-type type1 type2))
2243 (if (array-types-intersect 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 (eltype1 (array-type-element-type type1))
2249 (eltype2 (array-type-element-type type2)))
2250 (specialize-array-type
2252 :dimensions (cond ((eq dims1 '*) dims2)
2253 ((eq dims2 '*) dims1)
2255 (mapcar (lambda (x y) (if (eq x '*) y x))
2257 :complexp (if (eq complexp1 :maybe) complexp2 complexp1)
2259 ((eq eltype1 *wild-type*) eltype2)
2260 ((eq eltype2 *wild-type*) eltype1)
2261 (t (type-intersection eltype1 eltype2))))))
2264 ;;; Check a supplied dimension list to determine whether it is legal,
2265 ;;; and return it in canonical form (as either '* or a list).
2266 (defun canonical-array-dimensions (dims)
2271 (error "Arrays can't have a negative number of dimensions: ~S" dims))
2272 (when (>= dims sb!xc:array-rank-limit)
2273 (error "array type with too many dimensions: ~S" dims))
2274 (make-list dims :initial-element '*))
2276 (when (>= (length dims) sb!xc:array-rank-limit)
2277 (error "array type with too many dimensions: ~S" dims))
2280 (unless (and (integerp dim)
2282 (< dim sb!xc:array-dimension-limit))
2283 (error "bad dimension in array type: ~S" dim))))
2286 (error "Array dimensions is not a list, integer or *:~% ~S" dims))))
2290 (!define-type-class member)
2292 (!define-type-method (member :negate) (type)
2293 (let ((members (member-type-members type)))
2294 (if (some #'floatp members)
2296 (dolist (pair `((0.0f0 . ,(load-time-value (make-unportable-float :single-float-negative-zero)))
2297 (0.0d0 . ,(load-time-value (make-unportable-float :double-float-negative-zero)))
2299 (0.0l0 . ,(load-time-value (make-unportable-float :long-float-negative-zero)))))
2300 (when (member (car pair) members)
2301 (aver (not (member (cdr pair) members)))
2302 (push (cdr pair) floats)
2303 (setf members (remove (car pair) members)))
2304 (when (member (cdr pair) members)
2305 (aver (not (member (car pair) members)))
2306 (push (car pair) floats)
2307 (setf members (remove (cdr pair) members))))
2308 (apply #'type-intersection
2312 :type (make-member-type :members members)))
2315 (let ((type (ctype-of x)))
2318 :type (modified-numeric-type type
2319 :low nil :high nil))
2320 (modified-numeric-type type
2321 :low nil :high (list x))
2322 (make-member-type :members (list x))
2323 (modified-numeric-type type
2324 :low (list x) :high nil))))
2326 (make-negation-type :type type))))
2328 (!define-type-method (member :unparse) (type)
2329 (let ((members (member-type-members type)))
2331 ((equal members '(nil)) 'null)
2332 ((type= type (specifier-type 'standard-char)) 'standard-char)
2333 (t `(member ,@members)))))
2335 (!define-type-method (member :simple-subtypep) (type1 type2)
2336 (values (subsetp (member-type-members type1) (member-type-members type2))
2339 (!define-type-method (member :complex-subtypep-arg1) (type1 type2)
2340 (every/type (swapped-args-fun #'ctypep)
2342 (member-type-members type1)))
2344 ;;; We punt if the odd type is enumerable and intersects with the
2345 ;;; MEMBER type. If not enumerable, then it is definitely not a
2346 ;;; subtype of the MEMBER type.
2347 (!define-type-method (member :complex-subtypep-arg2) (type1 type2)
2348 (cond ((not (type-enumerable type1)) (values nil t))
2349 ((types-equal-or-intersect type1 type2)
2350 (invoke-complex-subtypep-arg1-method type1 type2))
2351 (t (values nil t))))
2353 (!define-type-method (member :simple-intersection2) (type1 type2)
2354 (let ((mem1 (member-type-members type1))
2355 (mem2 (member-type-members type2)))
2356 (cond ((subsetp mem1 mem2) type1)
2357 ((subsetp mem2 mem1) type2)
2359 (let ((res (intersection mem1 mem2)))
2361 (make-member-type :members res)
2364 (!define-type-method (member :complex-intersection2) (type1 type2)
2366 (collect ((members))
2367 (let ((mem2 (member-type-members type2)))
2368 (dolist (member mem2)
2369 (multiple-value-bind (val win) (ctypep member type1)
2371 (return-from punt nil))
2372 (when val (members member))))
2373 (cond ((subsetp mem2 (members)) type2)
2374 ((null (members)) *empty-type*)
2376 (make-member-type :members (members))))))))
2378 ;;; We don't need a :COMPLEX-UNION2, since the only interesting case is
2379 ;;; a union type, and the member/union interaction is handled by the
2380 ;;; union type method.
2381 (!define-type-method (member :simple-union2) (type1 type2)
2382 (let ((mem1 (member-type-members type1))
2383 (mem2 (member-type-members type2)))
2384 (cond ((subsetp mem1 mem2) type2)
2385 ((subsetp mem2 mem1) type1)
2387 (make-member-type :members (union mem1 mem2))))))
2389 (!define-type-method (member :simple-=) (type1 type2)
2390 (let ((mem1 (member-type-members type1))
2391 (mem2 (member-type-members type2)))
2392 (values (and (subsetp mem1 mem2)
2393 (subsetp mem2 mem1))
2396 (!define-type-method (member :complex-=) (type1 type2)
2397 (if (type-enumerable type1)
2398 (multiple-value-bind (val win) (csubtypep type2 type1)
2399 (if (or val (not win))
2404 (!def-type-translator member (&rest members)
2407 (dolist (m (remove-duplicates members))
2409 (float (if (zerop m)
2411 (push (ctype-of m) numbers)))
2412 (number (push (ctype-of m) numbers))
2416 (make-member-type :members ms)
2418 (nreverse numbers)))
2421 ;;;; intersection types
2423 ;;;; Until version 0.6.10.6, SBCL followed the original CMU CL approach
2424 ;;;; of punting on all AND types, not just the unreasonably complicated
2425 ;;;; ones. The change was motivated by trying to get the KEYWORD type
2426 ;;;; to behave sensibly:
2427 ;;;; ;; reasonable definition
2428 ;;;; (DEFTYPE KEYWORD () '(AND SYMBOL (SATISFIES KEYWORDP)))
2429 ;;;; ;; reasonable behavior
2430 ;;;; (AVER (SUBTYPEP 'KEYWORD 'SYMBOL))
2431 ;;;; Without understanding a little about the semantics of AND, we'd
2432 ;;;; get (SUBTYPEP 'KEYWORD 'SYMBOL)=>NIL,NIL and, for entirely
2433 ;;;; parallel reasons, (SUBTYPEP 'RATIO 'NUMBER)=>NIL,NIL. That's
2436 ;;;; We still follow the example of CMU CL to some extent, by punting
2437 ;;;; (to the opaque HAIRY-TYPE) on sufficiently complicated types
2440 (!define-type-class intersection)
2442 (!define-type-method (intersection :negate) (type)
2444 (mapcar #'type-negation (intersection-type-types type))))
2446 ;;; A few intersection types have special names. The others just get
2447 ;;; mechanically unparsed.
2448 (!define-type-method (intersection :unparse) (type)
2449 (declare (type ctype type))
2450 (or (find type '(ratio keyword) :key #'specifier-type :test #'type=)
2451 `(and ,@(mapcar #'type-specifier (intersection-type-types type)))))
2453 ;;; shared machinery for type equality: true if every type in the set
2454 ;;; TYPES1 matches a type in the set TYPES2 and vice versa
2455 (defun type=-set (types1 types2)
2456 (flet ((type<=-set (x y)
2457 (declare (type list x y))
2458 (every/type (lambda (x y-element)
2459 (any/type #'type= y-element x))
2461 (and/type (type<=-set types1 types2)
2462 (type<=-set types2 types1))))
2464 ;;; Two intersection types are equal if their subtypes are equal sets.
2466 ;;; FIXME: Might it be better to use
2467 ;;; (AND (SUBTYPEP X Y) (SUBTYPEP Y X))
2468 ;;; instead, since SUBTYPEP is the usual relationship that we care
2469 ;;; most about, so it would be good to leverage any ingenuity there
2470 ;;; in this more obscure method?
2471 (!define-type-method (intersection :simple-=) (type1 type2)
2472 (type=-set (intersection-type-types type1)
2473 (intersection-type-types type2)))
2475 (defun %intersection-complex-subtypep-arg1 (type1 type2)
2476 (type= type1 (type-intersection type1 type2)))
2478 (defun %intersection-simple-subtypep (type1 type2)
2479 (every/type #'%intersection-complex-subtypep-arg1
2481 (intersection-type-types type2)))
2483 (!define-type-method (intersection :simple-subtypep) (type1 type2)
2484 (%intersection-simple-subtypep type1 type2))
2486 (!define-type-method (intersection :complex-subtypep-arg1) (type1 type2)
2487 (%intersection-complex-subtypep-arg1 type1 type2))
2489 (defun %intersection-complex-subtypep-arg2 (type1 type2)
2490 (every/type #'csubtypep type1 (intersection-type-types type2)))
2492 (!define-type-method (intersection :complex-subtypep-arg2) (type1 type2)
2493 (%intersection-complex-subtypep-arg2 type1 type2))
2495 ;;; FIXME: This will look eeriely familiar to readers of the UNION
2496 ;;; :SIMPLE-INTERSECTION2 :COMPLEX-INTERSECTION2 method. That's
2497 ;;; because it was generated by cut'n'paste methods. Given that
2498 ;;; intersections and unions have all sorts of symmetries known to
2499 ;;; mathematics, it shouldn't be beyond the ken of some programmers to
2500 ;;; reflect those symmetries in code in a way that ties them together
2501 ;;; more strongly than having two independent near-copies :-/
2502 (!define-type-method (intersection :simple-union2 :complex-union2)
2504 ;; Within this method, type2 is guaranteed to be an intersection
2506 (aver (intersection-type-p type2))
2507 ;; Make sure to call only the applicable methods...
2508 (cond ((and (intersection-type-p type1)
2509 (%intersection-simple-subtypep type1 type2)) type2)
2510 ((and (intersection-type-p type1)
2511 (%intersection-simple-subtypep type2 type1)) type1)
2512 ((and (not (intersection-type-p type1))
2513 (%intersection-complex-subtypep-arg2 type1 type2))
2515 ((and (not (intersection-type-p type1))
2516 (%intersection-complex-subtypep-arg1 type2 type1))
2518 ;; KLUDGE: This special (and somewhat hairy) magic is required
2519 ;; to deal with the RATIONAL/INTEGER special case. The UNION
2520 ;; of (INTEGER * -1) and (AND (RATIONAL * -1/2) (NOT INTEGER))
2521 ;; should be (RATIONAL * -1/2) -- CSR, 2003-02-28
2522 ((and (csubtypep type2 (specifier-type 'ratio))
2523 (numeric-type-p type1)
2524 (csubtypep type1 (specifier-type 'integer))
2529 :low (if (null (numeric-type-low type1))
2531 (list (1- (numeric-type-low type1))))
2532 :high (if (null (numeric-type-high type1))
2534 (list (1+ (numeric-type-high type1)))))))
2536 (apply #'type-intersection
2537 (remove (specifier-type '(not integer))
2538 (intersection-type-types type2)
2541 (let ((accumulator *universal-type*))
2542 (do ((t2s (intersection-type-types type2) (cdr t2s)))
2543 ((null t2s) accumulator)
2544 (let ((union (type-union type1 (car t2s))))
2545 (when (union-type-p union)
2546 ;; we have to give up here -- there are all sorts of
2547 ;; ordering worries, but it's better than before.
2548 ;; Doing exactly the same as in the UNION
2549 ;; :SIMPLE/:COMPLEX-INTERSECTION2 method causes stack
2550 ;; overflow with the mutual recursion never bottoming
2552 (if (and (eq accumulator *universal-type*)
2554 ;; KLUDGE: if we get here, we have a partially
2555 ;; simplified result. While this isn't by any
2556 ;; means a universal simplification, including
2557 ;; this logic here means that we can get (OR
2558 ;; KEYWORD (NOT KEYWORD)) canonicalized to T.
2562 (type-intersection accumulator union))))))))
2564 (!def-type-translator and (&whole whole &rest type-specifiers)
2565 (apply #'type-intersection
2566 (mapcar #'specifier-type type-specifiers)))
2570 (!define-type-class union)
2572 (!define-type-method (union :negate) (type)
2573 (declare (type ctype type))
2574 (apply #'type-intersection
2575 (mapcar #'type-negation (union-type-types type))))
2577 ;;; The LIST, FLOAT and REAL types have special names. Other union
2578 ;;; types just get mechanically unparsed.
2579 (!define-type-method (union :unparse) (type)
2580 (declare (type ctype type))
2582 ((type= type (specifier-type 'list)) 'list)
2583 ((type= type (specifier-type 'float)) 'float)
2584 ((type= type (specifier-type 'real)) 'real)
2585 ((type= type (specifier-type 'sequence)) 'sequence)
2586 ((type= type (specifier-type 'bignum)) 'bignum)
2587 ((type= type (specifier-type 'simple-string)) 'simple-string)
2588 ((type= type (specifier-type 'string)) 'string)
2589 (t `(or ,@(mapcar #'type-specifier (union-type-types type))))))
2591 ;;; Two union types are equal if they are each subtypes of each
2592 ;;; other. We need to be this clever because our complex subtypep
2593 ;;; methods are now more accurate; we don't get infinite recursion
2594 ;;; because the simple-subtypep method delegates to complex-subtypep
2595 ;;; of the individual types of type1. - CSR, 2002-04-09
2597 ;;; Previous comment, now obsolete, but worth keeping around because
2598 ;;; it is true, though too strong a condition:
2600 ;;; Two union types are equal if their subtypes are equal sets.
2601 (!define-type-method (union :simple-=) (type1 type2)
2602 (multiple-value-bind (subtype certain?)
2603 (csubtypep type1 type2)
2605 (csubtypep type2 type1)
2606 ;; we might as well become as certain as possible.
2609 (multiple-value-bind (subtype certain?)
2610 (csubtypep type2 type1)
2611 (declare (ignore subtype))
2612 (values nil certain?))))))
2614 (!define-type-method (union :complex-=) (type1 type2)
2615 (declare (ignore type1))
2616 (if (some #'type-might-contain-other-types-p
2617 (union-type-types type2))
2621 ;;; Similarly, a union type is a subtype of another if and only if
2622 ;;; every element of TYPE1 is a subtype of TYPE2.
2623 (defun union-simple-subtypep (type1 type2)
2624 (every/type (swapped-args-fun #'union-complex-subtypep-arg2)
2626 (union-type-types type1)))
2628 (!define-type-method (union :simple-subtypep) (type1 type2)
2629 (union-simple-subtypep type1 type2))
2631 (defun union-complex-subtypep-arg1 (type1 type2)
2632 (every/type (swapped-args-fun #'csubtypep)
2634 (union-type-types type1)))
2636 (!define-type-method (union :complex-subtypep-arg1) (type1 type2)
2637 (union-complex-subtypep-arg1 type1 type2))
2639 (defun union-complex-subtypep-arg2 (type1 type2)
2640 (multiple-value-bind (sub-value sub-certain?)
2641 ;; was: (any/type #'csubtypep type1 (union-type-types type2)),
2642 ;; which turns out to be too restrictive, causing bug 91.
2644 ;; the following reimplementation might look dodgy. It is
2645 ;; dodgy. It depends on the union :complex-= method not doing
2646 ;; very much work -- certainly, not using subtypep. Reasoning:
2648 ;; At this stage, we know that type2 is a union type and type1
2649 ;; isn't. We might as well check this, though:
2650 (aver (union-type-p type2))
2651 (aver (not (union-type-p type1)))
2652 ;; A is a subset of (B1 u B2)
2653 ;; <=> A n (B1 u B2) = A
2654 ;; <=> (A n B1) u (A n B2) = A
2656 ;; But, we have to be careful not to delegate this type= to
2657 ;; something that could invoke subtypep, which might get us
2658 ;; back here -> stack explosion. We therefore ensure that the
2659 ;; second type (which is the one that's dispatched on) is
2660 ;; either a union type (where we've ensured that the complex-=
2661 ;; method will not call subtypep) or something with no union
2662 ;; types involved, in which case we'll never come back here.
2664 ;; If we don't do this, then e.g.
2665 ;; (SUBTYPEP '(MEMBER 3) '(OR (SATISFIES FOO) (SATISFIES BAR)))
2666 ;; would loop infinitely, as the member :complex-= method is
2667 ;; implemented in terms of subtypep.
2669 ;; Ouch. - CSR, 2002-04-10
2672 (mapcar (lambda (x) (type-intersection type1 x))
2673 (union-type-types type2)))))
2675 (values sub-value sub-certain?)
2676 ;; The ANY/TYPE expression above is a sufficient condition for
2677 ;; subsetness, but not a necessary one, so we might get a more
2678 ;; certain answer by this CALL-NEXT-METHOD-ish step when the
2679 ;; ANY/TYPE expression is uncertain.
2680 (invoke-complex-subtypep-arg1-method type1 type2))))
2682 (!define-type-method (union :complex-subtypep-arg2) (type1 type2)
2683 (union-complex-subtypep-arg2 type1 type2))
2685 (!define-type-method (union :simple-intersection2 :complex-intersection2)
2687 ;; The CSUBTYPEP clauses here let us simplify e.g.
2688 ;; (TYPE-INTERSECTION2 (SPECIFIER-TYPE 'LIST)
2689 ;; (SPECIFIER-TYPE '(OR LIST VECTOR)))
2690 ;; (where LIST is (OR CONS NULL)).
2692 ;; The tests are more or less (CSUBTYPEP TYPE1 TYPE2) and vice
2693 ;; versa, but it's important that we pre-expand them into
2694 ;; specialized operations on individual elements of
2695 ;; UNION-TYPE-TYPES, instead of using the ordinary call to
2696 ;; CSUBTYPEP, in order to avoid possibly invoking any methods which
2697 ;; might in turn invoke (TYPE-INTERSECTION2 TYPE1 TYPE2) and thus
2698 ;; cause infinite recursion.
2700 ;; Within this method, type2 is guaranteed to be a union type:
2701 (aver (union-type-p type2))
2702 ;; Make sure to call only the applicable methods...
2703 (cond ((and (union-type-p type1)
2704 (union-simple-subtypep type1 type2)) type1)
2705 ((and (union-type-p type1)
2706 (union-simple-subtypep type2 type1)) type2)
2707 ((and (not (union-type-p type1))
2708 (union-complex-subtypep-arg2 type1 type2))
2710 ((and (not (union-type-p type1))
2711 (union-complex-subtypep-arg1 type2 type1))
2714 ;; KLUDGE: This code accumulates a sequence of TYPE-UNION2
2715 ;; operations in a particular order, and gives up if any of
2716 ;; the sub-unions turn out not to be simple. In other cases
2717 ;; ca. sbcl-0.6.11.15, that approach to taking a union was a
2718 ;; bad idea, since it can overlook simplifications which
2719 ;; might occur if the terms were accumulated in a different
2720 ;; order. It's possible that that will be a problem here too.
2721 ;; However, I can't think of a good example to demonstrate
2722 ;; it, and without an example to demonstrate it I can't write
2723 ;; test cases, and without test cases I don't want to
2724 ;; complicate the code to address what's still a hypothetical
2725 ;; problem. So I punted. -- WHN 2001-03-20
2726 (let ((accumulator *empty-type*))
2727 (dolist (t2 (union-type-types type2) accumulator)
2729 (type-union accumulator
2730 (type-intersection type1 t2))))))))
2732 (!def-type-translator or (&rest type-specifiers)
2734 (mapcar #'specifier-type
2739 (!define-type-class cons)
2741 (!def-type-translator cons (&optional (car-type-spec '*) (cdr-type-spec '*))
2742 (let ((car-type (single-value-specifier-type car-type-spec))
2743 (cdr-type (single-value-specifier-type cdr-type-spec)))
2744 (make-cons-type car-type cdr-type)))
2746 (!define-type-method (cons :negate) (type)
2747 (if (and (eq (cons-type-car-type type) *universal-type*)
2748 (eq (cons-type-cdr-type type) *universal-type*))
2749 (make-negation-type :type type)
2751 (make-negation-type :type (specifier-type 'cons))
2753 ((and (not (eq (cons-type-car-type type) *universal-type*))
2754 (not (eq (cons-type-cdr-type type) *universal-type*)))
2757 (type-negation (cons-type-car-type type))
2761 (type-negation (cons-type-cdr-type type)))))
2762 ((not (eq (cons-type-car-type type) *universal-type*))
2764 (type-negation (cons-type-car-type type))
2766 ((not (eq (cons-type-cdr-type type) *universal-type*))
2769 (type-negation (cons-type-cdr-type type))))
2770 (t (bug "Weird CONS type ~S" type))))))
2772 (!define-type-method (cons :unparse) (type)
2773 (let ((car-eltype (type-specifier (cons-type-car-type type)))
2774 (cdr-eltype (type-specifier (cons-type-cdr-type type))))
2775 (if (and (member car-eltype '(t *))
2776 (member cdr-eltype '(t *)))
2778 `(cons ,car-eltype ,cdr-eltype))))
2780 (!define-type-method (cons :simple-=) (type1 type2)
2781 (declare (type cons-type type1 type2))
2782 (and (type= (cons-type-car-type type1) (cons-type-car-type type2))
2783 (type= (cons-type-cdr-type type1) (cons-type-cdr-type type2))))
2785 (!define-type-method (cons :simple-subtypep) (type1 type2)
2786 (declare (type cons-type type1 type2))
2787 (multiple-value-bind (val-car win-car)
2788 (csubtypep (cons-type-car-type type1) (cons-type-car-type type2))
2789 (multiple-value-bind (val-cdr win-cdr)
2790 (csubtypep (cons-type-cdr-type type1) (cons-type-cdr-type type2))
2791 (if (and val-car val-cdr)
2792 (values t (and win-car win-cdr))
2793 (values nil (or win-car win-cdr))))))
2795 ;;; Give up if a precise type is not possible, to avoid returning
2796 ;;; overly general types.
2797 (!define-type-method (cons :simple-union2) (type1 type2)
2798 (declare (type cons-type type1 type2))
2799 (let ((car-type1 (cons-type-car-type type1))
2800 (car-type2 (cons-type-car-type type2))
2801 (cdr-type1 (cons-type-cdr-type type1))
2802 (cdr-type2 (cons-type-cdr-type type2))
2805 ;; UGH. -- CSR, 2003-02-24
2806 (macrolet ((frob-car (car1 car2 cdr1 cdr2
2807 &optional (not1 nil not1p))
2809 (make-cons-type ,car1 (type-union ,cdr1 ,cdr2))
2811 (type-intersection ,car2
2814 `(type-negation ,car1)))
2816 (cond ((type= car-type1 car-type2)
2817 (make-cons-type car-type1
2818 (type-union cdr-type1 cdr-type2)))
2819 ((type= cdr-type1 cdr-type2)
2820 (make-cons-type (type-union car-type1 car-type2)
2822 ((csubtypep car-type1 car-type2)
2823 (frob-car car-type1 car-type2 cdr-type1 cdr-type2))
2824 ((csubtypep car-type2 car-type1)
2825 (frob-car car-type2 car-type1 cdr-type2 cdr-type1))
2826 ;; more general case of the above, but harder to compute
2828 (setf car-not1 (type-negation car-type1))
2829 (not (csubtypep car-type2 car-not1)))
2830 (frob-car car-type1 car-type2 cdr-type1 cdr-type2 car-not1))
2832 (setf car-not2 (type-negation car-type2))
2833 (not (csubtypep car-type1 car-not2)))
2834 (frob-car car-type2 car-type1 cdr-type2 cdr-type1 car-not2))
2835 ;; Don't put these in -- consider the effect of taking the
2836 ;; union of (CONS (INTEGER 0 2) (INTEGER 5 7)) and
2837 ;; (CONS (INTEGER 0 3) (INTEGER 5 6)).
2839 ((csubtypep cdr-type1 cdr-type2)
2840 (frob-cdr car-type1 car-type2 cdr-type1 cdr-type2))
2842 ((csubtypep cdr-type2 cdr-type1)
2843 (frob-cdr car-type2 car-type1 cdr-type2 cdr-type1))))))
2845 (!define-type-method (cons :simple-intersection2) (type1 type2)
2846 (declare (type cons-type type1 type2))
2847 (let ((car-int2 (type-intersection2 (cons-type-car-type type1)
2848 (cons-type-car-type type2)))
2849 (cdr-int2 (type-intersection2 (cons-type-cdr-type type1)
2850 (cons-type-cdr-type type2))))
2852 ((and car-int2 cdr-int2) (make-cons-type car-int2 cdr-int2))
2853 (car-int2 (make-cons-type car-int2
2855 (cons-type-cdr-type type1)
2856 (cons-type-cdr-type type2))))
2857 (cdr-int2 (make-cons-type
2858 (type-intersection (cons-type-car-type type1)
2859 (cons-type-car-type type2))
2862 ;;; Return the type that describes all objects that are in X but not
2863 ;;; in Y. If we can't determine this type, then return NIL.
2865 ;;; For now, we only are clever dealing with union and member types.
2866 ;;; If either type is not a union type, then we pretend that it is a
2867 ;;; union of just one type. What we do is remove from X all the types
2868 ;;; that are a subtype any type in Y. If any type in X intersects with
2869 ;;; a type in Y but is not a subtype, then we give up.
2871 ;;; We must also special-case any member type that appears in the
2872 ;;; union. We remove from X's members all objects that are TYPEP to Y.
2873 ;;; If Y has any members, we must be careful that none of those
2874 ;;; members are CTYPEP to any of Y's non-member types. We give up in
2875 ;;; this case, since to compute that difference we would have to break
2876 ;;; the type from X into some collection of types that represents the
2877 ;;; type without that particular element. This seems too hairy to be
2878 ;;; worthwhile, given its low utility.
2879 (defun type-difference (x y)
2880 (let ((x-types (if (union-type-p x) (union-type-types x) (list x)))
2881 (y-types (if (union-type-p y) (union-type-types y) (list y))))
2883 (dolist (x-type x-types)
2884 (if (member-type-p x-type)
2885 (collect ((members))
2886 (dolist (mem (member-type-members x-type))
2887 (multiple-value-bind (val win) (ctypep mem y)
2888 (unless win (return-from type-difference nil))
2892 (res (make-member-type :members (members)))))
2893 (dolist (y-type y-types (res x-type))
2894 (multiple-value-bind (val win) (csubtypep x-type y-type)
2895 (unless win (return-from type-difference nil))
2897 (when (types-equal-or-intersect x-type y-type)
2898 (return-from type-difference nil))))))
2899 (let ((y-mem (find-if #'member-type-p y-types)))
2901 (let ((members (member-type-members y-mem)))
2902 (dolist (x-type x-types)
2903 (unless (member-type-p x-type)
2904 (dolist (member members)
2905 (multiple-value-bind (val win) (ctypep member x-type)
2906 (when (or (not win) val)
2907 (return-from type-difference nil)))))))))
2908 (apply #'type-union (res)))))
2910 (!def-type-translator array (&optional (element-type '*)
2912 (specialize-array-type
2913 (make-array-type :dimensions (canonical-array-dimensions dimensions)
2915 :element-type (if (eq element-type '*)
2917 (specifier-type element-type)))))
2919 (!def-type-translator simple-array (&optional (element-type '*)
2921 (specialize-array-type
2922 (make-array-type :dimensions (canonical-array-dimensions dimensions)
2924 :element-type (if (eq element-type '*)
2926 (specifier-type element-type)))))
2928 ;;;; utilities shared between cross-compiler and target system
2930 ;;; Does the type derived from compilation of an actual function
2931 ;;; definition satisfy declarations of a function's type?
2932 (defun defined-ftype-matches-declared-ftype-p (defined-ftype declared-ftype)
2933 (declare (type ctype defined-ftype declared-ftype))
2934 (flet ((is-built-in-class-function-p (ctype)
2935 (and (built-in-classoid-p ctype)
2936 (eq (built-in-classoid-name ctype) 'function))))
2937 (cond (;; DECLARED-FTYPE could certainly be #<BUILT-IN-CLASS FUNCTION>;
2938 ;; that's what happens when we (DECLAIM (FTYPE FUNCTION FOO)).
2939 (is-built-in-class-function-p declared-ftype)
2940 ;; In that case, any definition satisfies the declaration.
2942 (;; It's not clear whether or how DEFINED-FTYPE might be
2943 ;; #<BUILT-IN-CLASS FUNCTION>, but it's not obviously
2944 ;; invalid, so let's handle that case too, just in case.
2945 (is-built-in-class-function-p defined-ftype)
2946 ;; No matter what DECLARED-FTYPE might be, we can't prove
2947 ;; that an object of type FUNCTION doesn't satisfy it, so
2948 ;; we return success no matter what.
2950 (;; Otherwise both of them must be FUN-TYPE objects.
2952 ;; FIXME: For now we only check compatibility of the return
2953 ;; type, not argument types, and we don't even check the
2954 ;; return type very precisely (as per bug 94a). It would be
2955 ;; good to do a better job. Perhaps to check the
2956 ;; compatibility of the arguments, we should (1) redo
2957 ;; VALUES-TYPES-EQUAL-OR-INTERSECT as
2958 ;; ARGS-TYPES-EQUAL-OR-INTERSECT, and then (2) apply it to
2959 ;; the ARGS-TYPE slices of the FUN-TYPEs. (ARGS-TYPE
2960 ;; is a base class both of VALUES-TYPE and of FUN-TYPE.)
2961 (values-types-equal-or-intersect
2962 (fun-type-returns defined-ftype)
2963 (fun-type-returns declared-ftype))))))
2965 ;;; This messy case of CTYPE for NUMBER is shared between the
2966 ;;; cross-compiler and the target system.
2967 (defun ctype-of-number (x)
2968 (let ((num (if (complexp x) (realpart x) x)))
2969 (multiple-value-bind (complexp low high)
2971 (let ((imag (imagpart x)))
2972 (values :complex (min num imag) (max num imag)))
2973 (values :real num num))
2974 (make-numeric-type :class (etypecase num
2976 (rational 'rational)
2978 :format (and (floatp num) (float-format-name num))
2984 ;; Why SAFETY 0? To suppress the is-it-the-right-structure-type
2985 ;; checking for declarations in structure accessors. Otherwise we
2986 ;; can get caught in a chicken-and-egg bootstrapping problem, whose
2987 ;; symptom on x86 OpenBSD sbcl-0.pre7.37.flaky5.22 is an illegal
2988 ;; instruction trap. I haven't tracked it down, but I'm guessing it
2989 ;; has to do with setting LAYOUTs when the LAYOUT hasn't been set
2991 (declare (optimize (safety 0)))
2992 (!defun-from-collected-cold-init-forms !late-type-cold-init))
2994 (/show0 "late-type.lisp end of file")