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 ;;; FIXME: This really should go away. Alas, it doesn't seem to be so
29 ;;; simple to make it go away.. (See bug 123 in BUGS file.)
30 (defvar *use-implementation-types* t ; actually initialized in cold init
32 "*USE-IMPLEMENTATION-TYPES* is a semi-public flag which determines how
33 restrictive we are in determining type membership. If two types are the
34 same in the implementation, then we will consider them them the same when
35 this switch is on. When it is off, we try to be as restrictive as the
36 language allows, allowing us to detect more errors. Currently, this only
37 affects array types.")
38 (!cold-init-forms (setq *use-implementation-types* t))
40 ;;; These functions are used as method for types which need a complex
41 ;;; subtypep method to handle some superclasses, but cover a subtree
42 ;;; of the type graph (i.e. there is no simple way for any other type
43 ;;; class to be a subtype.) There are always still complex ways,
44 ;;; namely UNION and MEMBER types, so we must give TYPE1's method a
45 ;;; chance to run, instead of immediately returning NIL, T.
46 (defun delegate-complex-subtypep-arg2 (type1 type2)
48 (type-class-complex-subtypep-arg1
49 (type-class-info type1))))
51 (funcall subtypep-arg1 type1 type2)
53 (defun delegate-complex-intersection2 (type1 type2)
54 (let ((method (type-class-complex-intersection2 (type-class-info type1))))
55 (if (and method (not (eq method #'delegate-complex-intersection2)))
56 (funcall method type2 type1)
57 (hierarchical-intersection2 type1 type2))))
59 ;;; This is used by !DEFINE-SUPERCLASSES to define the SUBTYPE-ARG1
60 ;;; method. INFO is a list of conses
61 ;;; (SUPERCLASS-CLASS . {GUARD-TYPE-SPECIFIER | NIL}).
62 (defun !has-superclasses-complex-subtypep-arg1 (type1 type2 info)
63 ;; If TYPE2 might be concealing something related to our class
65 (if (type-might-contain-other-types-p type2)
66 ;; too confusing, gotta punt
68 ;; ordinary case expected by old CMU CL code, where the taxonomy
69 ;; of TYPE2's representation accurately reflects the taxonomy of
72 ;; FIXME: This old CMU CL code probably deserves a comment
73 ;; explaining to us mere mortals how it works...
74 (and (sb!xc:typep type2 'classoid)
76 (when (or (not (cdr x))
77 (csubtypep type1 (specifier-type (cdr x))))
79 (or (eq type2 (car x))
80 (let ((inherits (layout-inherits
81 (classoid-layout (car x)))))
82 (dotimes (i (length inherits) nil)
83 (when (eq type2 (layout-classoid (svref inherits i)))
87 ;;; This function takes a list of specs, each of the form
88 ;;; (SUPERCLASS-NAME &OPTIONAL GUARD).
89 ;;; Consider one spec (with no guard): any instance of the named
90 ;;; TYPE-CLASS is also a subtype of the named superclass and of any of
91 ;;; its superclasses. If there are multiple specs, then some will have
92 ;;; guards. We choose the first spec whose guard is a supertype of
93 ;;; TYPE1 and use its superclass. In effect, a sequence of guards
96 ;;; G0,(and G1 (not G0)), (and G2 (not (or G0 G1))).
98 ;;; WHEN controls when the forms are executed.
99 (defmacro !define-superclasses (type-class-name specs when)
100 (with-unique-names (type-class info)
102 (let ((,type-class (type-class-or-lose ',type-class-name))
103 (,info (mapcar (lambda (spec)
105 (super &optional guard)
107 (cons (find-classoid super) guard)))
109 (setf (type-class-complex-subtypep-arg1 ,type-class)
110 (lambda (type1 type2)
111 (!has-superclasses-complex-subtypep-arg1 type1 type2 ,info)))
112 (setf (type-class-complex-subtypep-arg2 ,type-class)
113 #'delegate-complex-subtypep-arg2)
114 (setf (type-class-complex-intersection2 ,type-class)
115 #'delegate-complex-intersection2)))))
117 ;;;; FUNCTION and VALUES types
119 ;;;; Pretty much all of the general type operations are illegal on
120 ;;;; VALUES types, since we can't discriminate using them, do
121 ;;;; SUBTYPEP, etc. FUNCTION types are acceptable to the normal type
122 ;;;; operations, but are generally considered to be equivalent to
123 ;;;; FUNCTION. These really aren't true types in any type theoretic
124 ;;;; sense, but we still parse them into CTYPE structures for two
127 ;;;; -- Parsing and unparsing work the same way, and indeed we can't
128 ;;;; tell whether a type is a function or values type without
130 ;;;; -- Many of the places that can be annotated with real types can
131 ;;;; also be annotated with function or values types.
133 ;;; the description of a &KEY argument
134 (defstruct (key-info #-sb-xc-host (:pure t)
136 ;; the key (not necessarily a keyword in ANSI Common Lisp)
137 (name (missing-arg) :type symbol)
138 ;; the type of the argument value
139 (type (missing-arg) :type ctype))
141 (!define-type-method (values :simple-subtypep :complex-subtypep-arg1)
143 (declare (ignore type2))
144 ;; FIXME: should be TYPE-ERROR, here and in next method
145 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type1)))
147 (!define-type-method (values :complex-subtypep-arg2)
149 (declare (ignore type1))
150 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type2)))
152 (!define-type-method (values :unparse) (type)
153 (cons 'values (unparse-args-types type)))
155 ;;; Return true if LIST1 and LIST2 have the same elements in the same
156 ;;; positions according to TYPE=. We return NIL, NIL if there is an
157 ;;; uncertain comparison.
158 (defun type=-list (list1 list2)
159 (declare (list list1 list2))
160 (do ((types1 list1 (cdr types1))
161 (types2 list2 (cdr types2)))
162 ((or (null types1) (null types2))
163 (if (or types1 types2)
166 (multiple-value-bind (val win)
167 (type= (first types1) (first types2))
169 (return (values nil nil)))
171 (return (values nil t))))))
173 (!define-type-method (values :simple-=) (type1 type2)
174 (let ((rest1 (args-type-rest type1))
175 (rest2 (args-type-rest type2)))
176 (cond ((or (args-type-keyp type1) (args-type-keyp type2)
177 (args-type-allowp type1) (args-type-allowp type2))
179 ((and rest1 rest2 (type/= rest1 rest2))
184 (multiple-value-bind (req-val req-win)
185 (type=-list (values-type-required type1)
186 (values-type-required type2))
187 (multiple-value-bind (opt-val opt-win)
188 (type=-list (values-type-optional type1)
189 (values-type-optional type2))
190 (values (and req-val opt-val) (and req-win opt-win))))))))
192 (!define-type-class function)
194 ;;; a flag that we can bind to cause complex function types to be
195 ;;; unparsed as FUNCTION. This is useful when we want a type that we
196 ;;; can pass to TYPEP.
197 (defvar *unparse-fun-type-simplify*)
198 (!cold-init-forms (setq *unparse-fun-type-simplify* nil))
200 (!define-type-method (function :unparse) (type)
201 (if *unparse-fun-type-simplify*
204 (if (fun-type-wild-args type)
206 (unparse-args-types type))
208 (fun-type-returns type)))))
210 ;;; The meaning of this is a little confused. On the one hand, all
211 ;;; function objects are represented the same way regardless of the
212 ;;; arglists and return values, and apps don't get to ask things like
213 ;;; (TYPEP #'FOO (FUNCTION (FIXNUM) *)) in any meaningful way. On the
214 ;;; other hand, Python wants to reason about function types. So...
215 (!define-type-method (function :simple-subtypep) (type1 type2)
216 (flet ((fun-type-simple-p (type)
217 (not (or (fun-type-rest type)
218 (fun-type-keyp type))))
219 (every-csubtypep (types1 types2)
223 do (multiple-value-bind (res sure-p)
225 (unless res (return (values res sure-p))))
226 finally (return (values t t)))))
227 (and/type (values-subtypep (fun-type-returns type1)
228 (fun-type-returns type2))
229 (cond ((fun-type-wild-args type2) (values t t))
230 ((fun-type-wild-args type1)
231 (cond ((fun-type-keyp type2) (values nil nil))
232 ((not (fun-type-rest type2)) (values nil t))
233 ((not (null (fun-type-required type2))) (values nil t))
234 (t (and/type (type= *universal-type* (fun-type-rest type2))
235 (every/type #'type= *universal-type*
236 (fun-type-optional type2))))))
237 ((not (and (fun-type-simple-p type1)
238 (fun-type-simple-p type2)))
240 (t (multiple-value-bind (min1 max1) (fun-type-nargs type1)
241 (multiple-value-bind (min2 max2) (fun-type-nargs type2)
242 (cond ((or (> max1 max2) (< min1 min2))
244 ((and (= min1 min2) (= max1 max2))
245 (and/type (every-csubtypep (fun-type-required type1)
246 (fun-type-required type2))
247 (every-csubtypep (fun-type-optional type1)
248 (fun-type-optional type2))))
251 (fun-type-required type1)
252 (fun-type-optional type1))
254 (fun-type-required type2)
255 (fun-type-optional type2))))))))))))
257 (!define-superclasses function ((function)) !cold-init-forms)
259 ;;; The union or intersection of two FUNCTION types is FUNCTION.
260 (!define-type-method (function :simple-union2) (type1 type2)
261 (declare (ignore type1 type2))
262 (specifier-type 'function))
263 (!define-type-method (function :simple-intersection2) (type1 type2)
264 (declare (ignore type1 type2))
265 (specifier-type 'function))
267 ;;; The union or intersection of a subclass of FUNCTION with a
268 ;;; FUNCTION type is somewhat complicated.
269 (!define-type-method (function :complex-intersection2) (type1 type2)
271 ((type= type1 (specifier-type 'function)) type2)
272 ((csubtypep type1 (specifier-type 'function)) nil)
273 (t :call-other-method)))
274 (!define-type-method (function :complex-union2) (type1 type2)
276 ((type= type1 (specifier-type 'function)) type1)
279 ;;; ### Not very real, but good enough for redefining transforms
280 ;;; according to type:
281 (!define-type-method (function :simple-=) (type1 type2)
282 (values (equalp type1 type2) t))
284 (!define-type-class constant :inherits values)
286 (!define-type-method (constant :unparse) (type)
287 `(constant-arg ,(type-specifier (constant-type-type type))))
289 (!define-type-method (constant :simple-=) (type1 type2)
290 (type= (constant-type-type type1) (constant-type-type type2)))
292 (!def-type-translator constant-arg (type)
293 (make-constant-type :type (specifier-type type)))
295 ;;; Return the lambda-list-like type specification corresponding
297 (declaim (ftype (function (args-type) list) unparse-args-types))
298 (defun unparse-args-types (type)
301 (dolist (arg (args-type-required type))
302 (result (type-specifier arg)))
304 (when (args-type-optional type)
306 (dolist (arg (args-type-optional type))
307 (result (type-specifier arg))))
309 (when (args-type-rest type)
311 (result (type-specifier (args-type-rest type))))
313 (when (args-type-keyp type)
315 (dolist (key (args-type-keywords type))
316 (result (list (key-info-name key)
317 (type-specifier (key-info-type key))))))
319 (when (args-type-allowp type)
320 (result '&allow-other-keys))
324 (!def-type-translator function (&optional (args '*) (result '*))
325 (make-fun-type :args args :returns (values-specifier-type result)))
327 (!def-type-translator values (&rest values)
328 (make-values-type :args values))
330 ;;;; VALUES types interfaces
332 ;;;; We provide a few special operations that can be meaningfully used
333 ;;;; on VALUES types (as well as on any other type).
335 ;;; Return the type of the first value indicated by TYPE. This is used
336 ;;; by people who don't want to have to deal with VALUES types.
337 #!-sb-fluid (declaim (freeze-type values-type))
338 ; (inline single-value-type))
339 (defun single-value-type (type)
340 (declare (type ctype type))
341 (cond ((values-type-p type)
342 (or (car (args-type-required type))
343 (if (args-type-optional type)
344 (type-union (car (args-type-optional type))
345 (specifier-type 'null)))
346 (args-type-rest type)
347 (specifier-type 'null)))
348 ((eq type *wild-type*)
353 ;;; Return the minimum number of arguments that a function can be
354 ;;; called with, and the maximum number or NIL. If not a function
355 ;;; type, return NIL, NIL.
356 (defun fun-type-nargs (type)
357 (declare (type ctype type))
358 (if (and (fun-type-p type) (not (fun-type-wild-args type)))
359 (let ((fixed (length (args-type-required type))))
360 (if (or (args-type-rest type)
361 (args-type-keyp type)
362 (args-type-allowp type))
364 (values fixed (+ fixed (length (args-type-optional type))))))
367 ;;; Determine whether TYPE corresponds to a definite number of values.
368 ;;; The first value is a list of the types for each value, and the
369 ;;; second value is the number of values. If the number of values is
370 ;;; not fixed, then return NIL and :UNKNOWN.
371 (defun values-types (type)
372 (declare (type ctype type))
373 (cond ((eq type *wild-type*)
374 (values nil :unknown))
375 ((not (values-type-p type))
376 (values (list type) 1))
377 ((or (args-type-optional type)
378 (args-type-rest type)
379 (args-type-keyp type)
380 (args-type-allowp type))
381 (values nil :unknown))
383 (let ((req (args-type-required type)))
384 (values (mapcar #'single-value-type req) (length req))))))
386 ;;; Return two values:
387 ;;; 1. A list of all the positional (fixed and optional) types.
388 ;;; 2. The &REST type (if any). If keywords allowed, *UNIVERSAL-TYPE*.
389 ;;; If no keywords or &REST, then the DEFAULT-TYPE.
390 (defun values-type-types (type &optional (default-type *empty-type*))
391 (declare (type values-type type))
392 (values (append (args-type-required type)
393 (args-type-optional type))
394 (cond ((args-type-keyp type) *universal-type*)
395 ((args-type-rest type))
399 ;;; Return a list of OPERATION applied to the types in TYPES1 and
400 ;;; TYPES2, padding with REST2 as needed. TYPES1 must not be shorter
401 ;;; than TYPES2. The second value is T if OPERATION always returned a
402 ;;; true second value.
403 (defun fixed-values-op (types1 types2 rest2 operation)
404 (declare (list types1 types2) (type ctype rest2) (type function operation))
406 (values (mapcar (lambda (t1 t2)
407 (multiple-value-bind (res win)
408 (funcall operation t1 t2)
414 (make-list (- (length types1) (length types2))
415 :initial-element rest2)))
418 ;;; If TYPE isn't a values type, then make it into one:
419 ;;; <type> ==> (values type &rest t)
420 (defun coerce-to-values (type)
421 (declare (type ctype type))
422 (if (values-type-p type)
424 (make-values-type :required (list type) :rest *universal-type*)))
426 ;;; Do the specified OPERATION on TYPE1 and TYPE2, which may be any
427 ;;; type, including VALUES types. With VALUES types such as:
430 ;;; we compute the more useful result
431 ;;; (VALUES (<operation> a0 b0) (<operation> a1 b1))
432 ;;; rather than the precise result
433 ;;; (<operation> (values a0 a1) (values b0 b1))
434 ;;; This has the virtue of always keeping the VALUES type specifier
435 ;;; outermost, and retains all of the information that is really
436 ;;; useful for static type analysis. We want to know what is always
437 ;;; true of each value independently. It is worthless to know that if
438 ;;; the first value is B0 then the second will be B1.
440 ;;; If the VALUES count signatures differ, then we produce a result with
441 ;;; the required VALUE count chosen by NREQ when applied to the number
442 ;;; of required values in TYPE1 and TYPE2. Any &KEY values become
443 ;;; &REST T (anyone who uses keyword values deserves to lose.)
445 ;;; The second value is true if the result is definitely empty or if
446 ;;; OPERATION returned true as its second value each time we called
447 ;;; it. Since we approximate the intersection of VALUES types, the
448 ;;; second value being true doesn't mean the result is exact.
449 (defun args-type-op (type1 type2 operation nreq default-type)
450 (declare (type ctype type1 type2 default-type)
451 (type function operation nreq))
452 (when (eq type1 type2)
454 (if (or (values-type-p type1) (values-type-p type2))
455 (let ((type1 (coerce-to-values type1))
456 (type2 (coerce-to-values type2)))
457 (multiple-value-bind (types1 rest1)
458 (values-type-types type1 default-type)
459 (multiple-value-bind (types2 rest2)
460 (values-type-types type2 default-type)
461 (multiple-value-bind (rest rest-exact)
462 (funcall operation rest1 rest2)
463 (multiple-value-bind (res res-exact)
464 (if (< (length types1) (length types2))
465 (fixed-values-op types2 types1 rest1 operation)
466 (fixed-values-op types1 types2 rest2 operation))
467 (let* ((req (funcall nreq
468 (length (args-type-required type1))
469 (length (args-type-required type2))))
470 (required (subseq res 0 req))
471 (opt (subseq res req))
472 (opt-last (position rest opt :test-not #'type=
474 (if (find *empty-type* required :test #'type=)
475 (values *empty-type* t)
476 (values (make-values-type
478 :optional (if opt-last
479 (subseq opt 0 (1+ opt-last))
481 :rest (if (eq rest default-type) nil rest))
482 (and rest-exact res-exact)))))))))
483 (funcall operation type1 type2)))
485 ;;; Do a union or intersection operation on types that might be values
486 ;;; types. The result is optimized for utility rather than exactness,
487 ;;; but it is guaranteed that it will be no smaller (more restrictive)
488 ;;; than the precise result.
490 ;;; The return convention seems to be analogous to
491 ;;; TYPES-EQUAL-OR-INTERSECT. -- WHN 19990910.
492 (defun-cached (values-type-union :hash-function type-cache-hash
495 :init-wrapper !cold-init-forms)
496 ((type1 eq) (type2 eq))
497 (declare (type ctype type1 type2))
498 (cond ((or (eq type1 *wild-type*) (eq type2 *wild-type*)) *wild-type*)
499 ((eq type1 *empty-type*) type2)
500 ((eq type2 *empty-type*) type1)
502 (values (args-type-op type1 type2 #'type-union #'min *empty-type*)))))
503 (defun-cached (values-type-intersection :hash-function type-cache-hash
506 :default (values nil :empty)
507 :init-wrapper !cold-init-forms)
508 ((type1 eq) (type2 eq))
509 (declare (type ctype type1 type2))
510 (cond ((eq type1 *wild-type*) (values type2 t))
511 ((eq type2 *wild-type*) (values type1 t))
513 (args-type-op type1 type2
516 (specifier-type 'null)))))
518 ;;; This is like TYPES-EQUAL-OR-INTERSECT, except that it sort of
519 ;;; works on VALUES types. Note that due to the semantics of
520 ;;; VALUES-TYPE-INTERSECTION, this might return (VALUES T T) when
521 ;;; there isn't really any intersection.
522 (defun values-types-equal-or-intersect (type1 type2)
523 (cond ((or (eq type1 *empty-type*) (eq type2 *empty-type*))
525 ((or (values-type-p type1) (values-type-p type2))
526 (multiple-value-bind (res win) (values-type-intersection type1 type2)
527 (values (not (eq res *empty-type*))
530 (types-equal-or-intersect type1 type2))))
532 ;;; a SUBTYPEP-like operation that can be used on any types, including
534 (defun-cached (values-subtypep :hash-function type-cache-hash
537 :default (values nil :empty)
538 :init-wrapper !cold-init-forms)
539 ((type1 eq) (type2 eq))
540 (declare (type ctype type1 type2))
541 (cond ((eq type2 *wild-type*) (values t t))
542 ((eq type1 *wild-type*)
543 (values (eq type2 *universal-type*) t))
544 ((not (values-types-equal-or-intersect type1 type2))
547 (if (or (values-type-p type1) (values-type-p type2))
548 (let ((type1 (coerce-to-values type1))
549 (type2 (coerce-to-values type2)))
550 (multiple-value-bind (types1 rest1) (values-type-types type1)
551 (multiple-value-bind (types2 rest2) (values-type-types type2)
552 (cond ((< (length (values-type-required type1))
553 (length (values-type-required type2)))
555 ((< (length types1) (length types2))
557 ((or (values-type-keyp type1)
558 (values-type-keyp type2))
561 (do ((t1 types1 (rest t1))
562 (t2 types2 (rest t2)))
564 (csubtypep rest1 rest2))
565 (multiple-value-bind (res win-p)
566 (csubtypep (first t1) (first t2))
568 (return (values nil nil)))
570 (return (values nil t))))))))))
571 (csubtypep type1 type2)))))
573 ;;;; type method interfaces
575 ;;; like SUBTYPEP, only works on CTYPE structures
576 (defun-cached (csubtypep :hash-function type-cache-hash
579 :default (values nil :empty)
580 :init-wrapper !cold-init-forms)
581 ((type1 eq) (type2 eq))
582 (declare (type ctype type1 type2))
583 (cond ((or (eq type1 type2)
584 (eq type1 *empty-type*)
585 (eq type2 *wild-type*))
587 ((eq type1 *wild-type*)
590 (!invoke-type-method :simple-subtypep :complex-subtypep-arg2
592 :complex-arg1 :complex-subtypep-arg1))))
594 ;;; Just parse the type specifiers and call CSUBTYPE.
595 (defun sb!xc:subtypep (type1 type2 &optional environment)
597 "Return two values indicating the relationship between type1 and type2.
598 If values are T and T, type1 definitely is a subtype of type2.
599 If values are NIL and T, type1 definitely is not a subtype of type2.
600 If values are NIL and NIL, it couldn't be determined."
601 (declare (ignore environment))
602 (csubtypep (specifier-type type1) (specifier-type type2)))
604 ;;; If two types are definitely equivalent, return true. The second
605 ;;; value indicates whether the first value is definitely correct.
606 ;;; This should only fail in the presence of HAIRY types.
607 (defun-cached (type= :hash-function type-cache-hash
610 :default (values nil :empty)
611 :init-wrapper !cold-init-forms)
612 ((type1 eq) (type2 eq))
613 (declare (type ctype type1 type2))
616 (!invoke-type-method :simple-= :complex-= type1 type2)))
618 ;;; Not exactly the negation of TYPE=, since when the relationship is
619 ;;; uncertain, we still return NIL, NIL. This is useful in cases where
620 ;;; the conservative assumption is =.
621 (defun type/= (type1 type2)
622 (declare (type ctype type1 type2))
623 (multiple-value-bind (res win) (type= type1 type2)
628 ;;; the type method dispatch case of TYPE-UNION2
629 (defun %type-union2 (type1 type2)
630 ;; As in %TYPE-INTERSECTION2, it seems to be a good idea to give
631 ;; both argument orders a chance at COMPLEX-INTERSECTION2. Unlike
632 ;; %TYPE-INTERSECTION2, though, I don't have a specific case which
633 ;; demonstrates this is actually necessary. Also unlike
634 ;; %TYPE-INTERSECTION2, there seems to be no need to distinguish
635 ;; between not finding a method and having a method return NIL.
637 (!invoke-type-method :simple-union2 :complex-union2
640 (declare (inline 1way))
641 (or (1way type1 type2)
642 (1way type2 type1))))
644 ;;; Find a type which includes both types. Any inexactness is
645 ;;; represented by the fuzzy element types; we return a single value
646 ;;; that is precise to the best of our knowledge. This result is
647 ;;; simplified into the canonical form, thus is not a UNION-TYPE
648 ;;; unless we find no other way to represent the result.
649 (defun-cached (type-union2 :hash-function type-cache-hash
651 :init-wrapper !cold-init-forms)
652 ((type1 eq) (type2 eq))
653 ;; KLUDGE: This was generated from TYPE-INTERSECTION2 by Ye Olde Cut And
654 ;; Paste technique of programming. If it stays around (as opposed to
655 ;; e.g. fading away in favor of some CLOS solution) the shared logic
656 ;; should probably become shared code. -- WHN 2001-03-16
657 (declare (type ctype type1 type2))
658 (cond ((eq type1 type2)
660 ((csubtypep type1 type2) type2)
661 ((csubtypep type2 type1) type1)
662 ((or (union-type-p type1)
663 (union-type-p type2))
664 ;; Unions of UNION-TYPE should have the UNION-TYPE-TYPES
665 ;; values broken out and united separately. The full TYPE-UNION
666 ;; function knows how to do this, so let it handle it.
667 (type-union type1 type2))
669 ;; the ordinary case: we dispatch to type methods
670 (%type-union2 type1 type2))))
672 ;;; the type method dispatch case of TYPE-INTERSECTION2
673 (defun %type-intersection2 (type1 type2)
674 ;; We want to give both argument orders a chance at
675 ;; COMPLEX-INTERSECTION2. Without that, the old CMU CL type
676 ;; methods could give noncommutative results, e.g.
677 ;; (TYPE-INTERSECTION2 *EMPTY-TYPE* SOME-HAIRY-TYPE)
679 ;; (TYPE-INTERSECTION2 SOME-HAIRY-TYPE *EMPTY-TYPE*)
680 ;; => #<NAMED-TYPE NIL>, T
681 ;; We also need to distinguish between the case where we found a
682 ;; type method, and it returned NIL, and the case where we fell
683 ;; through without finding any type method. An example of the first
684 ;; case is the intersection of a HAIRY-TYPE with some ordinary type.
685 ;; An example of the second case is the intersection of two
686 ;; completely-unrelated types, e.g. CONS and NUMBER, or SYMBOL and
689 ;; (Why yes, CLOS probably *would* be nicer..)
691 (!invoke-type-method :simple-intersection2 :complex-intersection2
693 :default :call-other-method)))
694 (declare (inline 1way))
695 (let ((xy (1way type1 type2)))
696 (or (and (not (eql xy :call-other-method)) xy)
697 (let ((yx (1way type2 type1)))
698 (or (and (not (eql yx :call-other-method)) yx)
699 (cond ((and (eql xy :call-other-method)
700 (eql yx :call-other-method))
703 (aver (and (not xy) (not yx))) ; else handled above
706 (defun-cached (type-intersection2 :hash-function type-cache-hash
710 :init-wrapper !cold-init-forms)
711 ((type1 eq) (type2 eq))
712 (declare (type ctype type1 type2))
713 (cond ((eq type1 type2)
714 ;; FIXME: For some reason, this doesn't catch e.g. type1 =
715 ;; type2 = (SPECIFIER-TYPE
716 ;; 'SOME-UNKNOWN-TYPE). Investigate. - CSR, 2002-04-10
718 ((or (intersection-type-p type1)
719 (intersection-type-p type2))
720 ;; Intersections of INTERSECTION-TYPE should have the
721 ;; INTERSECTION-TYPE-TYPES values broken out and intersected
722 ;; separately. The full TYPE-INTERSECTION function knows how
723 ;; to do that, so let it handle it.
724 (type-intersection type1 type2))
726 ;; the ordinary case: we dispatch to type methods
727 (%type-intersection2 type1 type2))))
729 ;;; Return as restrictive and simple a type as we can discover that is
730 ;;; no more restrictive than the intersection of TYPE1 and TYPE2. At
731 ;;; worst, we arbitrarily return one of the arguments as the first
732 ;;; value (trying not to return a hairy type).
733 (defun type-approx-intersection2 (type1 type2)
734 (cond ((type-intersection2 type1 type2))
735 ((hairy-type-p type1) type2)
738 ;;; a test useful for checking whether a derived type matches a
741 ;;; The first value is true unless the types don't intersect and
742 ;;; aren't equal. The second value is true if the first value is
743 ;;; definitely correct. NIL is considered to intersect with any type.
744 ;;; If T is a subtype of either type, then we also return T, T. This
745 ;;; way we recognize that hairy types might intersect with T.
746 (defun types-equal-or-intersect (type1 type2)
747 (declare (type ctype type1 type2))
748 (if (or (eq type1 *empty-type*) (eq type2 *empty-type*))
750 (let ((intersection2 (type-intersection2 type1 type2)))
751 (cond ((not intersection2)
752 (if (or (csubtypep *universal-type* type1)
753 (csubtypep *universal-type* type2))
756 ((eq intersection2 *empty-type*) (values nil t))
759 ;;; Return a Common Lisp type specifier corresponding to the TYPE
761 (defun type-specifier (type)
762 (declare (type ctype type))
763 (funcall (type-class-unparse (type-class-info type)) type))
765 ;;; (VALUES-SPECIFIER-TYPE and SPECIFIER-TYPE moved from here to
766 ;;; early-type.lisp by WHN ca. 19990201.)
768 ;;; Take a list of type specifiers, computing the translation of each
769 ;;; specifier and defining it as a builtin type.
770 (declaim (ftype (function (list) (values)) precompute-types))
771 (defun precompute-types (specs)
773 (let ((res (specifier-type spec)))
774 (unless (unknown-type-p res)
775 (setf (info :type :builtin spec) res)
776 ;; KLUDGE: the three copies of this idiom in this file (and
777 ;; the one in class.lisp as at sbcl-0.7.4.1x) should be
778 ;; coalesced, or perhaps the error-detecting code that
779 ;; disallows redefinition of :PRIMITIVE types should be
780 ;; rewritten to use *TYPE-SYSTEM-FINALIZED* (rather than
781 ;; *TYPE-SYSTEM-INITIALIZED*). The effect of this is not to
782 ;; cause redefinition errors when precompute-types is called
783 ;; for a second time while building the target compiler using
784 ;; the cross-compiler. -- CSR, trying to explain why this
785 ;; isn't completely wrong, 2002-06-07
786 (setf (info :type :kind spec) #+sb-xc-host :defined #-sb-xc-host :primitive))))
789 ;;;; general TYPE-UNION and TYPE-INTERSECTION operations
791 ;;;; These are fully general operations on CTYPEs: they'll always
792 ;;;; return a CTYPE representing the result.
794 ;;; shared logic for unions and intersections: Stuff TYPE into the
795 ;;; vector TYPES, finding pairs of types which can be simplified by
796 ;;; SIMPLIFY2 (TYPE-UNION2 or TYPE-INTERSECTION2) and replacing them
797 ;;; by their simplified forms.
798 (defun accumulate1-compound-type (type types %compound-type-p simplify2)
799 (declare (type ctype type))
800 (declare (type (vector ctype) types))
801 (declare (type function %compound-type-p simplify2))
802 ;; Any input object satisfying %COMPOUND-TYPE-P should've been
803 ;; broken into components before it reached us.
804 (aver (not (funcall %compound-type-p type)))
805 (dotimes (i (length types) (vector-push-extend type types))
806 (let ((simplified2 (funcall simplify2 type (aref types i))))
808 ;; Discard the old (AREF TYPES I).
809 (setf (aref types i) (vector-pop types))
810 ;; Merge the new SIMPLIFIED2 into TYPES, by tail recursing.
811 ;; (Note that the tail recursion is indirect: we go through
812 ;; ACCUMULATE, not ACCUMULATE1, so that if SIMPLIFIED2 is
813 ;; handled properly if it satisfies %COMPOUND-TYPE-P.)
814 (return (accumulate-compound-type simplified2
821 ;;; shared logic for unions and intersections: Use
822 ;;; ACCUMULATE1-COMPOUND-TYPE to merge TYPE into TYPES, either
823 ;;; all in one step or, if %COMPOUND-TYPE-P is satisfied,
824 ;;; component by component.
825 (defun accumulate-compound-type (type types %compound-type-p simplify2)
826 (declare (type function %compound-type-p simplify2))
827 (flet ((accumulate1 (x)
828 (accumulate1-compound-type x types %compound-type-p simplify2)))
829 (declare (inline accumulate1))
830 (if (funcall %compound-type-p type)
831 (map nil #'accumulate1 (compound-type-types type))
835 ;;; shared logic for unions and intersections: Return a vector of
836 ;;; types representing the same types as INPUT-TYPES, but with
837 ;;; COMPOUND-TYPEs satisfying %COMPOUND-TYPE-P broken up into their
838 ;;; component types, and with any SIMPLY2 simplifications applied.
839 (defun simplified-compound-types (input-types %compound-type-p simplify2)
840 (let ((simplified-types (make-array (length input-types)
844 ;; (This INITIAL-ELEMENT shouldn't
845 ;; matter, but helps avoid type
846 ;; warnings at compile time.)
847 :initial-element *empty-type*)))
848 (dolist (input-type input-types)
849 (accumulate-compound-type input-type
855 ;;; shared logic for unions and intersections: Make a COMPOUND-TYPE
856 ;;; object whose components are the types in TYPES, or skip to special
857 ;;; cases when TYPES is short.
858 (defun make-probably-compound-type (constructor types enumerable identity)
859 (declare (type function constructor))
860 (declare (type (vector ctype) types))
861 (declare (type ctype identity))
865 (t (funcall constructor
867 ;; FIXME: This should be just (COERCE TYPES 'LIST), but as
868 ;; of sbcl-0.6.11.17 the COERCE optimizer is really
869 ;; brain-dead, so that would generate a full call to
870 ;; SPECIFIER-TYPE at runtime, so we get into bootstrap
871 ;; problems in cold init because 'LIST is a compound
872 ;; type, so we need to MAKE-PROBABLY-COMPOUND-TYPE
873 ;; before we know what 'LIST is. Once the COERCE
874 ;; optimizer is less brain-dead, we can make this
875 ;; (COERCE TYPES 'LIST) again.
876 #+sb-xc-host (coerce types 'list)
877 #-sb-xc-host (coerce-to-list types)))))
879 (defun maybe-distribute-one-union (union-type types)
880 (let* ((intersection (apply #'type-intersection types))
881 (union (mapcar (lambda (x) (type-intersection x intersection))
882 (union-type-types union-type))))
883 (if (notany (lambda (x) (or (hairy-type-p x)
884 (intersection-type-p x)))
889 (defun type-intersection (&rest input-types)
890 (%type-intersection input-types))
891 (defun-cached (%type-intersection :hash-bits 8
892 :hash-function (lambda (x)
893 (logand (sxhash x) #xff)))
894 ((input-types equal))
895 (let ((simplified-types (simplified-compound-types input-types
896 #'intersection-type-p
897 #'type-intersection2)))
898 (declare (type (vector ctype) simplified-types))
899 ;; We want to have a canonical representation of types (or failing
900 ;; that, punt to HAIRY-TYPE). Canonical representation would have
901 ;; intersections inside unions but not vice versa, since you can
902 ;; always achieve that by the distributive rule. But we don't want
903 ;; to just apply the distributive rule, since it would be too easy
904 ;; to end up with unreasonably huge type expressions. So instead
905 ;; we try to generate a simple type by distributing the union; if
906 ;; the type can't be made simple, we punt to HAIRY-TYPE.
907 (if (and (> (length simplified-types) 1)
908 (some #'union-type-p simplified-types))
909 (let* ((first-union (find-if #'union-type-p simplified-types))
910 (other-types (coerce (remove first-union simplified-types)
912 (distributed (maybe-distribute-one-union first-union
915 (apply #'type-union distributed)
917 :specifier `(and ,@(map 'list
919 simplified-types)))))
920 (make-probably-compound-type #'%make-intersection-type
922 (some #'type-enumerable
926 (defun type-union (&rest input-types)
927 (%type-union input-types))
928 (defun-cached (%type-union :hash-bits 8
929 :hash-function (lambda (x)
930 (logand (sxhash x) #xff)))
931 ((input-types equal))
932 (let ((simplified-types (simplified-compound-types input-types
935 (make-probably-compound-type #'make-union-type
937 (every #'type-enumerable simplified-types)
942 (!define-type-class named)
945 (defvar *empty-type*)
946 (defvar *universal-type*)
947 (defvar *universal-fun-type*)
949 (macrolet ((frob (name var)
951 (setq ,var (make-named-type :name ',name))
952 (setf (info :type :kind ',name)
953 #+sb-xc-host :defined #-sb-xc-host :primitive)
954 (setf (info :type :builtin ',name) ,var))))
955 ;; KLUDGE: In ANSI, * isn't really the name of a type, it's just a
956 ;; special symbol which can be stuck in some places where an
957 ;; ordinary type can go, e.g. (ARRAY * 1) instead of (ARRAY T 1).
958 ;; At some point, in order to become more standard, we should
959 ;; convert all the classic CMU CL legacy *s and *WILD-TYPE*s into
960 ;; Ts and *UNIVERSAL-TYPE*s.
962 (frob nil *empty-type*)
963 (frob t *universal-type*))
964 (setf *universal-fun-type*
965 (make-fun-type :wild-args t
966 :returns *wild-type*)))
968 (!define-type-method (named :simple-=) (type1 type2)
969 ;; FIXME: BUG 85: This assertion failed when I added it in
970 ;; sbcl-0.6.11.13. It probably shouldn't fail; but for now it's
971 ;; just commented out.
972 ;;(aver (not (eq type1 *wild-type*))) ; * isn't really a type.
973 (values (eq type1 type2) t))
975 (!define-type-method (named :complex-=) (type1 type2)
977 ((and (eq type2 *empty-type*)
978 (intersection-type-p type1)
979 ;; not allowed to be unsure on these... FIXME: keep the list
980 ;; of CL types that are intersection types once and only
982 (not (or (type= type1 (specifier-type 'ratio))
983 (type= type1 (specifier-type 'keyword)))))
984 ;; things like (AND (EQL 0) (SATISFIES ODDP)) or (AND FUNCTION
985 ;; STREAM) can get here. In general, we can't really tell
986 ;; whether these are equal to NIL or not, so
988 ((type-might-contain-other-types-p type1)
989 (invoke-complex-=-other-method type1 type2))
992 (!define-type-method (named :simple-subtypep) (type1 type2)
993 (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
994 (values (or (eq type1 *empty-type*) (eq type2 *wild-type*)) t))
996 (!define-type-method (named :complex-subtypep-arg1) (type1 type2)
997 ;; This AVER causes problems if we write accurate methods for the
998 ;; union (and possibly intersection) types which then delegate to
999 ;; us; while a user shouldn't get here, because of the odd status of
1000 ;; *wild-type* a type-intersection executed by the compiler can. -
1003 ;; (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1004 (cond ((eq type1 *empty-type*)
1006 (;; When TYPE2 might be the universal type in disguise
1007 (type-might-contain-other-types-p type2)
1008 ;; Now that the UNION and HAIRY COMPLEX-SUBTYPEP-ARG2 methods
1009 ;; can delegate to us (more or less as CALL-NEXT-METHOD) when
1010 ;; they're uncertain, we can't just barf on COMPOUND-TYPE and
1011 ;; HAIRY-TYPEs as we used to. Instead we deal with the
1012 ;; problem (where at least part of the problem is cases like
1013 ;; (SUBTYPEP T '(SATISFIES FOO))
1015 ;; (SUBTYPEP T '(AND (SATISFIES FOO) (SATISFIES BAR)))
1016 ;; where the second type is a hairy type like SATISFIES, or
1017 ;; is a compound type which might contain a hairy type) by
1018 ;; returning uncertainty.
1021 ;; By elimination, TYPE1 is the universal type.
1022 (aver (or (eq type1 *wild-type*) (eq type1 *universal-type*)))
1023 ;; This case would have been picked off by the SIMPLE-SUBTYPEP
1024 ;; method, and so shouldn't appear here.
1025 (aver (not (eq type2 *universal-type*)))
1026 ;; Since TYPE2 is not EQ *UNIVERSAL-TYPE* and is not the
1027 ;; universal type in disguise, TYPE2 is not a superset of TYPE1.
1030 (!define-type-method (named :complex-subtypep-arg2) (type1 type2)
1031 (aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1032 (cond ((eq type2 *universal-type*)
1034 ((type-might-contain-other-types-p type1)
1035 ;; those types can be *EMPTY-TYPE* or *UNIVERSAL-TYPE* in
1036 ;; disguise. So we'd better delegate.
1037 (invoke-complex-subtypep-arg1-method type1 type2))
1039 ;; FIXME: This seems to rely on there only being 2 or 3
1040 ;; NAMED-TYPE values, and the exclusion of various
1041 ;; possibilities above. It would be good to explain it and/or
1042 ;; rewrite it so that it's clearer.
1043 (values (not (eq type2 *empty-type*)) t))))
1045 (!define-type-method (named :complex-intersection2) (type1 type2)
1046 ;; FIXME: This assertion failed when I added it in sbcl-0.6.11.13.
1047 ;; Perhaps when bug 85 is fixed it can be reenabled.
1048 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1049 (hierarchical-intersection2 type1 type2))
1051 (!define-type-method (named :complex-union2) (type1 type2)
1052 ;; Perhaps when bug 85 is fixed this can be reenabled.
1053 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1054 (hierarchical-union2 type1 type2))
1056 (!define-type-method (named :unparse) (x)
1057 (named-type-name x))
1059 ;;;; hairy and unknown types
1061 (!define-type-method (hairy :unparse) (x)
1062 (hairy-type-specifier x))
1064 (!define-type-method (hairy :simple-subtypep) (type1 type2)
1065 (let ((hairy-spec1 (hairy-type-specifier type1))
1066 (hairy-spec2 (hairy-type-specifier type2)))
1067 (cond ((equal-but-no-car-recursion hairy-spec1 hairy-spec2)
1070 (values nil nil)))))
1072 (!define-type-method (hairy :complex-subtypep-arg2) (type1 type2)
1073 (invoke-complex-subtypep-arg1-method type1 type2))
1075 (!define-type-method (hairy :complex-subtypep-arg1) (type1 type2)
1076 (declare (ignore type1 type2))
1079 (!define-type-method (hairy :complex-=) (type1 type2)
1080 (declare (ignore type1 type2))
1083 (!define-type-method (hairy :simple-intersection2 :complex-intersection2)
1085 (if (type= type1 type2)
1089 (!define-type-method (hairy :simple-union2)
1091 (if (type= type1 type2)
1095 (!define-type-method (hairy :simple-=) (type1 type2)
1096 (if (equal-but-no-car-recursion (hairy-type-specifier type1)
1097 (hairy-type-specifier type2))
1101 (!def-type-translator satisfies (&whole whole fun)
1102 (declare (ignore fun))
1103 ;; Check legality of arguments.
1104 (destructuring-bind (satisfies predicate-name) whole
1105 (declare (ignore satisfies))
1106 (unless (symbolp predicate-name)
1107 (error 'simple-type-error
1108 :datum predicate-name
1109 :expected-type 'symbol
1110 :format-control "The SATISFIES predicate name is not a symbol: ~S"
1111 :format-arguments (list predicate-name))))
1113 (make-hairy-type :specifier whole))
1117 (!define-type-method (negation :unparse) (x)
1118 `(not ,(type-specifier (negation-type-type x))))
1120 (!define-type-method (negation :simple-subtypep) (type1 type2)
1121 (csubtypep (negation-type-type type2) (negation-type-type type1)))
1123 (!define-type-method (negation :complex-subtypep-arg2) (type1 type2)
1124 (let* ((complement-type2 (negation-type-type type2))
1125 (intersection2 (type-intersection2 type1
1128 ;; FIXME: if uncertain, maybe try arg1?
1129 (type= intersection2 *empty-type*)
1130 (invoke-complex-subtypep-arg1-method type1 type2))))
1132 (!define-type-method (negation :complex-subtypep-arg1) (type1 type2)
1133 ;; "Incrementally extended heuristic algorithms tend inexorably toward the
1134 ;; incomprehensible." -- http://www.unlambda.com/~james/lambda/lambda.txt
1136 ;; You may not believe this. I couldn't either. But then I sat down
1137 ;; and drew lots of Venn diagrams. Comments involving a and b refer
1138 ;; to the call (subtypep '(not a) 'b) -- CSR, 2002-02-27.
1140 ;; (Several logical truths in this block are true as long as
1141 ;; b/=T. As of sbcl-0.7.1.28, it seems impossible to construct a
1142 ;; case with b=T where we actually reach this type method, but
1143 ;; we'll test for and exclude this case anyway, since future
1144 ;; maintenance might make it possible for it to end up in this
1146 (multiple-value-bind (equal certain)
1147 (type= type2 *universal-type*)
1149 (return (values nil nil)))
1151 (return (values t t))))
1152 (let ((complement-type1 (negation-type-type type1)))
1153 ;; Do the special cases first, in order to give us a chance if
1154 ;; subtype/supertype relationships are hairy.
1155 (multiple-value-bind (equal certain)
1156 (type= complement-type1 type2)
1157 ;; If a = b, ~a is not a subtype of b (unless b=T, which was
1160 (return (values nil nil)))
1162 (return (values nil t))))
1163 ;; KLUDGE: ANSI requires that the SUBTYPEP result between any
1164 ;; two built-in atomic type specifiers never be uncertain. This
1165 ;; is hard to do cleanly for the built-in types whose
1166 ;; definitions include (NOT FOO), i.e. CONS and RATIO. However,
1167 ;; we can do it with this hack, which uses our global knowledge
1168 ;; that our implementation of the type system uses disjoint
1169 ;; implementation types to represent disjoint sets (except when
1170 ;; types are contained in other types). (This is a KLUDGE
1171 ;; because it's fragile. Various changes in internal
1172 ;; representation in the type system could make it start
1173 ;; confidently returning incorrect results.) -- WHN 2002-03-08
1174 (unless (or (type-might-contain-other-types-p complement-type1)
1175 (type-might-contain-other-types-p type2))
1176 ;; Because of the way our types which don't contain other
1177 ;; types are disjoint subsets of the space of possible values,
1178 ;; (SUBTYPEP '(NOT AA) 'B)=NIL when AA and B are simple (and B
1179 ;; is not T, as checked above).
1180 (return (values nil t)))
1181 ;; The old (TYPE= TYPE1 TYPE2) branch would never be taken, as
1182 ;; TYPE1 and TYPE2 will only be equal if they're both NOT types,
1183 ;; and then the :SIMPLE-SUBTYPEP method would be used instead.
1184 ;; But a CSUBTYPEP relationship might still hold:
1185 (multiple-value-bind (equal certain)
1186 (csubtypep complement-type1 type2)
1187 ;; If a is a subtype of b, ~a is not a subtype of b (unless
1188 ;; b=T, which was excluded above).
1190 (return (values nil nil)))
1192 (return (values nil t))))
1193 (multiple-value-bind (equal certain)
1194 (csubtypep type2 complement-type1)
1195 ;; If b is a subtype of a, ~a is not a subtype of b. (FIXME:
1196 ;; That's not true if a=T. Do we know at this point that a is
1199 (return (values nil nil)))
1201 (return (values nil t))))
1202 ;; old CSR comment ca. 0.7.2, now obsoleted by the SIMPLE-CTYPE?
1203 ;; KLUDGE case above: Other cases here would rely on being able
1204 ;; to catch all possible cases, which the fragility of this type
1205 ;; system doesn't inspire me; for instance, if a is type= to ~b,
1206 ;; then we want T, T; if this is not the case and the types are
1207 ;; disjoint (have an intersection of *empty-type*) then we want
1208 ;; NIL, T; else if the union of a and b is the *universal-type*
1209 ;; then we want T, T. So currently we still claim to be unsure
1210 ;; about e.g. (subtypep '(not fixnum) 'single-float).
1212 ;; OTOH we might still get here:
1215 (!define-type-method (negation :complex-=) (type1 type2)
1216 ;; (NOT FOO) isn't equivalent to anything that's not a negation
1217 ;; type, except possibly a type that might contain it in disguise.
1218 (declare (ignore type2))
1219 (if (type-might-contain-other-types-p type1)
1223 (!define-type-method (negation :simple-intersection2) (type1 type2)
1224 (let ((not1 (negation-type-type type1))
1225 (not2 (negation-type-type type2)))
1227 ((csubtypep not1 not2) type2)
1228 ((csubtypep not2 not1) type1)
1229 ;; Why no analagous clause to the disjoint in the SIMPLE-UNION2
1230 ;; method, below? The clause would read
1232 ;; ((EQ (TYPE-UNION NOT1 NOT2) *UNIVERSAL-TYPE*) *EMPTY-TYPE*)
1234 ;; but with proper canonicalization of negation types, there's
1235 ;; no way of constructing two negation types with union of their
1236 ;; negations being the universal type.
1238 (aver (not (eq (type-union not1 not2) *universal-type*)))
1241 (!define-type-method (negation :complex-intersection2) (type1 type2)
1243 ((csubtypep type1 (negation-type-type type2)) *empty-type*)
1244 ((eq (type-intersection type1 (negation-type-type type2)) *empty-type*)
1248 (!define-type-method (negation :simple-union2) (type1 type2)
1249 (let ((not1 (negation-type-type type1))
1250 (not2 (negation-type-type type2)))
1252 ((csubtypep not1 not2) type1)
1253 ((csubtypep not2 not1) type2)
1254 ((eq (type-intersection not1 not2) *empty-type*)
1258 (!define-type-method (negation :complex-union2) (type1 type2)
1260 ((csubtypep (negation-type-type type2) type1) *universal-type*)
1261 ((eq (type-intersection type1 (negation-type-type type2)) *empty-type*)
1265 (!define-type-method (negation :simple-=) (type1 type2)
1266 (type= (negation-type-type type1) (negation-type-type type2)))
1268 (!def-type-translator not (typespec)
1269 (let* ((not-type (specifier-type typespec))
1270 (spec (type-specifier not-type)))
1272 ;; canonicalize (NOT (NOT FOO))
1273 ((and (listp spec) (eq (car spec) 'not))
1274 (specifier-type (cadr spec)))
1275 ;; canonicalize (NOT NIL) and (NOT T)
1276 ((eq not-type *empty-type*) *universal-type*)
1277 ((eq not-type *universal-type*) *empty-type*)
1278 ((and (numeric-type-p not-type)
1279 (null (numeric-type-low not-type))
1280 (null (numeric-type-high not-type)))
1281 (make-negation-type :type not-type))
1282 ((numeric-type-p not-type)
1285 :type (modified-numeric-type not-type :low nil :high nil))
1287 ((null (numeric-type-low not-type))
1288 (modified-numeric-type
1290 :low (let ((h (numeric-type-high not-type)))
1291 (if (consp h) (car h) (list h)))
1293 ((null (numeric-type-high not-type))
1294 (modified-numeric-type
1297 :high (let ((l (numeric-type-low not-type)))
1298 (if (consp l) (car l) (list l)))))
1300 (modified-numeric-type
1303 :high (let ((l (numeric-type-low not-type)))
1304 (if (consp l) (car l) (list l))))
1305 (modified-numeric-type
1307 :low (let ((h (numeric-type-high not-type)))
1308 (if (consp h) (car h) (list h)))
1310 ((intersection-type-p not-type)
1312 (mapcar #'(lambda (x)
1313 (specifier-type `(not ,(type-specifier x))))
1314 (intersection-type-types not-type))))
1315 ((union-type-p not-type)
1316 (apply #'type-intersection
1317 (mapcar #'(lambda (x)
1318 (specifier-type `(not ,(type-specifier x))))
1319 (union-type-types not-type))))
1320 ((member-type-p not-type)
1321 (let ((members (member-type-members not-type)))
1322 (if (some #'floatp members)
1324 (dolist (pair `((0.0f0 . ,(load-time-value (make-unportable-float :single-float-negative-zero)))
1325 (0.0d0 . ,(load-time-value (make-unportable-float :double-float-negative-zero)))
1327 (0.0l0 . ,(load-time-value (make-unportable-float :long-float-negative-zero)))))
1328 (when (member (car pair) members)
1329 (aver (not (member (cdr pair) members)))
1330 (push (cdr pair) floats)
1331 (setf members (remove (car pair) members)))
1332 (when (member (cdr pair) members)
1333 (aver (not (member (car pair) members)))
1334 (push (car pair) floats)
1335 (setf members (remove (cdr pair) members))))
1336 (apply #'type-intersection
1340 :type (make-member-type :members members)))
1343 (let ((type (ctype-of x)))
1346 :type (modified-numeric-type type
1347 :low nil :high nil))
1348 (modified-numeric-type type
1349 :low nil :high (list x))
1350 (make-member-type :members (list x))
1351 (modified-numeric-type type
1352 :low (list x) :high nil))))
1354 (make-negation-type :type not-type))))
1355 ((and (cons-type-p not-type)
1356 (eq (cons-type-car-type not-type) *universal-type*)
1357 (eq (cons-type-cdr-type not-type) *universal-type*))
1358 (make-negation-type :type not-type))
1359 ((cons-type-p not-type)
1361 (make-negation-type :type (specifier-type 'cons))
1363 ((and (not (eq (cons-type-car-type not-type) *universal-type*))
1364 (not (eq (cons-type-cdr-type not-type) *universal-type*)))
1367 (specifier-type `(not ,(type-specifier
1368 (cons-type-car-type not-type))))
1372 (specifier-type `(not ,(type-specifier
1373 (cons-type-cdr-type not-type)))))))
1374 ((not (eq (cons-type-car-type not-type) *universal-type*))
1376 (specifier-type `(not ,(type-specifier
1377 (cons-type-car-type not-type))))
1379 ((not (eq (cons-type-cdr-type not-type) *universal-type*))
1382 (specifier-type `(not ,(type-specifier
1383 (cons-type-cdr-type not-type))))))
1384 (t (bug "Weird CONS type ~S" not-type)))))
1385 (t (make-negation-type :type not-type)))))
1389 (!define-type-class number)
1391 (!define-type-method (number :simple-=) (type1 type2)
1393 (and (eq (numeric-type-class type1) (numeric-type-class type2))
1394 (eq (numeric-type-format type1) (numeric-type-format type2))
1395 (eq (numeric-type-complexp type1) (numeric-type-complexp type2))
1396 (equalp (numeric-type-low type1) (numeric-type-low type2))
1397 (equalp (numeric-type-high type1) (numeric-type-high type2)))
1400 (!define-type-method (number :unparse) (type)
1401 (let* ((complexp (numeric-type-complexp type))
1402 (low (numeric-type-low type))
1403 (high (numeric-type-high type))
1404 (base (case (numeric-type-class type)
1406 (rational 'rational)
1407 (float (or (numeric-type-format type) 'float))
1410 (cond ((and (eq base 'integer) high low)
1411 (let ((high-count (logcount high))
1412 (high-length (integer-length high)))
1414 (cond ((= high 0) '(integer 0 0))
1416 ((and (= high-count high-length)
1417 (plusp high-length))
1418 `(unsigned-byte ,high-length))
1420 `(mod ,(1+ high)))))
1421 ((and (= low sb!xc:most-negative-fixnum)
1422 (= high sb!xc:most-positive-fixnum))
1424 ((and (= low (lognot high))
1425 (= high-count high-length)
1427 `(signed-byte ,(1+ high-length)))
1429 `(integer ,low ,high)))))
1430 (high `(,base ,(or low '*) ,high))
1432 (if (and (eq base 'integer) (= low 0))
1440 (if (eq base+bounds 'real)
1442 `(complex ,base+bounds)))
1444 (aver (eq base+bounds 'real))
1447 ;;; Return true if X is "less than or equal" to Y, taking open bounds
1448 ;;; into consideration. CLOSED is the predicate used to test the bound
1449 ;;; on a closed interval (e.g. <=), and OPEN is the predicate used on
1450 ;;; open bounds (e.g. <). Y is considered to be the outside bound, in
1451 ;;; the sense that if it is infinite (NIL), then the test succeeds,
1452 ;;; whereas if X is infinite, then the test fails (unless Y is also
1455 ;;; This is for comparing bounds of the same kind, e.g. upper and
1456 ;;; upper. Use NUMERIC-BOUND-TEST* for different kinds of bounds.
1457 (defmacro numeric-bound-test (x y closed open)
1462 (,closed (car ,x) (car ,y))
1463 (,closed (car ,x) ,y)))
1469 ;;; This is used to compare upper and lower bounds. This is different
1470 ;;; from the same-bound case:
1471 ;;; -- Since X = NIL is -infinity, whereas y = NIL is +infinity, we
1472 ;;; return true if *either* arg is NIL.
1473 ;;; -- an open inner bound is "greater" and also squeezes the interval,
1474 ;;; causing us to use the OPEN test for those cases as well.
1475 (defmacro numeric-bound-test* (x y closed open)
1480 (,open (car ,x) (car ,y))
1481 (,open (car ,x) ,y)))
1487 ;;; Return whichever of the numeric bounds X and Y is "maximal"
1488 ;;; according to the predicates CLOSED (e.g. >=) and OPEN (e.g. >).
1489 ;;; This is only meaningful for maximizing like bounds, i.e. upper and
1490 ;;; upper. If MAX-P is true, then we return NIL if X or Y is NIL,
1491 ;;; otherwise we return the other arg.
1492 (defmacro numeric-bound-max (x y closed open max-p)
1495 `(cond ((not ,n-x) ,(if max-p nil n-y))
1496 ((not ,n-y) ,(if max-p nil n-x))
1499 (if (,closed (car ,n-x) (car ,n-y)) ,n-x ,n-y)
1500 (if (,open (car ,n-x) ,n-y) ,n-x ,n-y)))
1503 (if (,open (car ,n-y) ,n-x) ,n-y ,n-x)
1504 (if (,closed ,n-y ,n-x) ,n-y ,n-x))))))
1506 (!define-type-method (number :simple-subtypep) (type1 type2)
1507 (let ((class1 (numeric-type-class type1))
1508 (class2 (numeric-type-class type2))
1509 (complexp2 (numeric-type-complexp type2))
1510 (format2 (numeric-type-format type2))
1511 (low1 (numeric-type-low type1))
1512 (high1 (numeric-type-high type1))
1513 (low2 (numeric-type-low type2))
1514 (high2 (numeric-type-high type2)))
1515 ;; If one is complex and the other isn't, they are disjoint.
1516 (cond ((not (or (eq (numeric-type-complexp type1) complexp2)
1519 ;; If the classes are specified and different, the types are
1520 ;; disjoint unless type2 is RATIONAL and type1 is INTEGER.
1521 ;; [ or type1 is INTEGER and type2 is of the form (RATIONAL
1522 ;; X X) for integral X, but this is dealt with in the
1523 ;; canonicalization inside MAKE-NUMERIC-TYPE ]
1524 ((not (or (eq class1 class2)
1526 (and (eq class1 'integer) (eq class2 'rational))))
1528 ;; If the float formats are specified and different, the types
1530 ((not (or (eq (numeric-type-format type1) format2)
1533 ;; Check the bounds.
1534 ((and (numeric-bound-test low1 low2 >= >)
1535 (numeric-bound-test high1 high2 <= <))
1540 (!define-superclasses number ((number)) !cold-init-forms)
1542 ;;; If the high bound of LOW is adjacent to the low bound of HIGH,
1543 ;;; then return true, otherwise NIL.
1544 (defun numeric-types-adjacent (low high)
1545 (let ((low-bound (numeric-type-high low))
1546 (high-bound (numeric-type-low high)))
1547 (cond ((not (and low-bound high-bound)) nil)
1548 ((and (consp low-bound) (consp high-bound)) nil)
1550 (let ((low-value (car low-bound)))
1551 (or (eql low-value high-bound)
1552 (and (eql low-value (load-time-value (make-unportable-float :single-float-negative-zero))) (eql high-bound 0f0))
1553 (and (eql low-value 0f0) (eql high-bound (load-time-value (make-unportable-float :single-float-negative-zero))))
1554 (and (eql low-value (load-time-value (make-unportable-float :double-float-negative-zero))) (eql high-bound 0d0))
1555 (and (eql low-value 0d0) (eql high-bound (load-time-value (make-unportable-float :double-float-negative-zero)))))))
1557 (let ((high-value (car high-bound)))
1558 (or (eql high-value low-bound)
1559 (and (eql high-value (load-time-value (make-unportable-float :single-float-negative-zero))) (eql low-bound 0f0))
1560 (and (eql high-value 0f0) (eql low-bound (load-time-value (make-unportable-float :single-float-negative-zero))))
1561 (and (eql high-value (load-time-value (make-unportable-float :double-float-negative-zero))) (eql low-bound 0d0))
1562 (and (eql high-value 0d0) (eql low-bound (load-time-value (make-unportable-float :double-float-negative-zero)))))))
1563 ((and (eq (numeric-type-class low) 'integer)
1564 (eq (numeric-type-class high) 'integer))
1565 (eql (1+ low-bound) high-bound))
1569 ;;; Return a numeric type that is a supertype for both TYPE1 and TYPE2.
1571 ;;; Old comment, probably no longer applicable:
1573 ;;; ### Note: we give up early to keep from dropping lots of
1574 ;;; information on the floor by returning overly general types.
1575 (!define-type-method (number :simple-union2) (type1 type2)
1576 (declare (type numeric-type type1 type2))
1577 (cond ((csubtypep type1 type2) type2)
1578 ((csubtypep type2 type1) type1)
1580 (let ((class1 (numeric-type-class type1))
1581 (format1 (numeric-type-format type1))
1582 (complexp1 (numeric-type-complexp type1))
1583 (class2 (numeric-type-class type2))
1584 (format2 (numeric-type-format type2))
1585 (complexp2 (numeric-type-complexp type2)))
1587 ((and (eq class1 class2)
1588 (eq format1 format2)
1589 (eq complexp1 complexp2)
1590 (or (numeric-types-intersect type1 type2)
1591 (numeric-types-adjacent type1 type2)
1592 (numeric-types-adjacent type2 type1)))
1597 :low (numeric-bound-max (numeric-type-low type1)
1598 (numeric-type-low type2)
1600 :high (numeric-bound-max (numeric-type-high type1)
1601 (numeric-type-high type2)
1603 ;; FIXME: These two clauses are almost identical, and the
1604 ;; consequents are in fact identical in every respect.
1605 ((and (eq class1 'rational)
1606 (eq class2 'integer)
1607 (eq format1 format2)
1608 (eq complexp1 complexp2)
1609 (integerp (numeric-type-low type2))
1610 (integerp (numeric-type-high type2))
1611 (= (numeric-type-low type2) (numeric-type-high type2))
1612 (or (numeric-types-adjacent type1 type2)
1613 (numeric-types-adjacent type2 type1)))
1618 :low (numeric-bound-max (numeric-type-low type1)
1619 (numeric-type-low type2)
1621 :high (numeric-bound-max (numeric-type-high type1)
1622 (numeric-type-high type2)
1624 ((and (eq class1 'integer)
1625 (eq class2 'rational)
1626 (eq format1 format2)
1627 (eq complexp1 complexp2)
1628 (integerp (numeric-type-low type1))
1629 (integerp (numeric-type-high type1))
1630 (= (numeric-type-low type1) (numeric-type-high type1))
1631 (or (numeric-types-adjacent type1 type2)
1632 (numeric-types-adjacent type2 type1)))
1637 :low (numeric-bound-max (numeric-type-low type1)
1638 (numeric-type-low type2)
1640 :high (numeric-bound-max (numeric-type-high type1)
1641 (numeric-type-high type2)
1647 (setf (info :type :kind 'number)
1648 #+sb-xc-host :defined #-sb-xc-host :primitive)
1649 (setf (info :type :builtin 'number)
1650 (make-numeric-type :complexp nil)))
1652 (!def-type-translator complex (&optional (typespec '*))
1653 (if (eq typespec '*)
1654 (make-numeric-type :complexp :complex)
1655 (labels ((not-numeric ()
1656 (error "The component type for COMPLEX is not numeric: ~S"
1659 (error "The component type for COMPLEX is not real: ~S"
1661 (complex1 (component-type)
1662 (unless (numeric-type-p component-type)
1664 (when (eq (numeric-type-complexp component-type) :complex)
1666 (modified-numeric-type component-type :complexp :complex))
1667 (complex-union (component)
1668 (unless (numberp component)
1670 ;; KLUDGE: This TYPECASE more or less does
1671 ;; (UPGRADED-COMPLEX-PART-TYPE (TYPE-OF COMPONENT)),
1672 ;; (plus a small hack to treat (EQL COMPONENT 0) specially)
1673 ;; but uses logic cut and pasted from the DEFUN of
1674 ;; UPGRADED-COMPLEX-PART-TYPE. That's fragile, because
1675 ;; changing the definition of UPGRADED-COMPLEX-PART-TYPE
1676 ;; would tend to break the code here. Unfortunately,
1677 ;; though, reusing UPGRADED-COMPLEX-PART-TYPE here
1678 ;; would cause another kind of fragility, because
1679 ;; ANSI's definition of TYPE-OF is so weak that e.g.
1680 ;; (UPGRADED-COMPLEX-PART-TYPE (TYPE-OF 1/2)) could
1681 ;; end up being (UPGRADED-COMPLEX-PART-TYPE 'REAL)
1682 ;; instead of (UPGRADED-COMPLEX-PART-TYPE 'RATIONAL).
1683 ;; So using TYPE-OF would mean that ANSI-conforming
1684 ;; maintenance changes in TYPE-OF could break the code here.
1685 ;; It's not clear how best to fix this. -- WHN 2002-01-21,
1686 ;; trying to summarize CSR's concerns in his patch
1688 (complex (error "The component type for COMPLEX (EQL X) ~
1691 ((eql 0) (specifier-type nil)) ; as required by ANSI
1692 (single-float (specifier-type '(complex single-float)))
1693 (double-float (specifier-type '(complex double-float)))
1695 (long-float (specifier-type '(complex long-float)))
1696 (rational (specifier-type '(complex rational)))
1697 (t (specifier-type '(complex real))))))
1698 (let ((ctype (specifier-type typespec)))
1700 (numeric-type (complex1 ctype))
1701 (union-type (apply #'type-union
1702 ;; FIXME: This code could suffer from
1703 ;; (admittedly very obscure) cases of
1704 ;; bug 145 e.g. when TYPE is
1705 ;; (OR (AND INTEGER (SATISFIES ODDP))
1706 ;; (AND FLOAT (SATISFIES FOO))
1707 ;; and not even report the problem very well.
1709 (union-type-types ctype))))
1710 ;; MEMBER-TYPE is almost the same as UNION-TYPE, but
1711 ;; there's a gotcha: (COMPLEX (EQL 0)) is, according to
1712 ;; ANSI, equal to type NIL, the empty set.
1713 (member-type (apply #'type-union
1714 (mapcar #'complex-union
1715 (member-type-members ctype))))
1717 (multiple-value-bind (subtypep certainly)
1718 (csubtypep ctype (specifier-type 'real))
1719 (if (and (not subtypep) certainly)
1721 ;; ANSI just says that TYPESPEC is any subtype of
1722 ;; type REAL, not necessarily a NUMERIC-TYPE. In
1723 ;; particular, at this point TYPESPEC could legally be
1724 ;; an intersection type like (AND REAL (SATISFIES ODDP)),
1725 ;; in which case we fall through the logic above and
1726 ;; end up here, stumped.
1727 (bug "~@<(known bug #145): The type ~S is too hairy to be
1728 used for a COMPLEX component.~:@>"
1731 ;;; If X is *, return NIL, otherwise return the bound, which must be a
1732 ;;; member of TYPE or a one-element list of a member of TYPE.
1733 #!-sb-fluid (declaim (inline canonicalized-bound))
1734 (defun canonicalized-bound (bound type)
1735 (cond ((eq bound '*) nil)
1736 ((or (sb!xc:typep bound type)
1738 (sb!xc:typep (car bound) type)
1739 (null (cdr bound))))
1742 (error "Bound is not ~S, a ~S or a list of a ~S: ~S"
1748 (!def-type-translator integer (&optional (low '*) (high '*))
1749 (let* ((l (canonicalized-bound low 'integer))
1750 (lb (if (consp l) (1+ (car l)) l))
1751 (h (canonicalized-bound high 'integer))
1752 (hb (if (consp h) (1- (car h)) h)))
1753 (if (and hb lb (< hb lb))
1755 (make-numeric-type :class 'integer
1757 :enumerable (not (null (and l h)))
1761 (defmacro !def-bounded-type (type class format)
1762 `(!def-type-translator ,type (&optional (low '*) (high '*))
1763 (let ((lb (canonicalized-bound low ',type))
1764 (hb (canonicalized-bound high ',type)))
1765 (if (not (numeric-bound-test* lb hb <= <))
1767 (make-numeric-type :class ',class
1772 (!def-bounded-type rational rational nil)
1774 ;;; Unlike CMU CL, we represent the types FLOAT and REAL as
1775 ;;; UNION-TYPEs of more primitive types, in order to make
1776 ;;; type representation more unique, avoiding problems in the
1777 ;;; simplification of things like
1778 ;;; (subtypep '(or (single-float -1.0 1.0) (single-float 0.1))
1779 ;;; '(or (real -1 7) (single-float 0.1) (single-float -1.0 1.0)))
1780 ;;; When we allowed REAL to remain as a separate NUMERIC-TYPE,
1781 ;;; it was too easy for the first argument to be simplified to
1782 ;;; '(SINGLE-FLOAT -1.0), and for the second argument to be simplified
1783 ;;; to '(OR (REAL -1 7) (SINGLE-FLOAT 0.1)) and then for the
1784 ;;; SUBTYPEP to fail (returning NIL,T instead of T,T) because
1785 ;;; the first argument can't be seen to be a subtype of any of the
1786 ;;; terms in the second argument.
1788 ;;; The old CMU CL way was:
1789 ;;; (!def-bounded-type float float nil)
1790 ;;; (!def-bounded-type real nil nil)
1792 ;;; FIXME: If this new way works for a while with no weird new
1793 ;;; problems, we can go back and rip out support for separate FLOAT
1794 ;;; and REAL flavors of NUMERIC-TYPE. The new way was added in
1795 ;;; sbcl-0.6.11.22, 2001-03-21.
1797 ;;; FIXME: It's probably necessary to do something to fix the
1798 ;;; analogous problem with INTEGER and RATIONAL types. Perhaps
1799 ;;; bounded RATIONAL types should be represented as (OR RATIO INTEGER).
1800 (defun coerce-bound (bound type inner-coerce-bound-fun)
1801 (declare (type function inner-coerce-bound-fun))
1802 (cond ((eql bound '*)
1805 (destructuring-bind (inner-bound) bound
1806 (list (funcall inner-coerce-bound-fun inner-bound type))))
1808 (funcall inner-coerce-bound-fun bound type))))
1809 (defun inner-coerce-real-bound (bound type)
1811 (rational (rationalize bound))
1812 (float (if (floatp bound)
1814 ;; Coerce to the widest float format available, to
1815 ;; avoid unnecessary loss of precision:
1816 (coerce bound 'long-float)))))
1817 (defun coerced-real-bound (bound type)
1818 (coerce-bound bound type #'inner-coerce-real-bound))
1819 (defun coerced-float-bound (bound type)
1820 (coerce-bound bound type #'coerce))
1821 (!def-type-translator real (&optional (low '*) (high '*))
1822 (specifier-type `(or (float ,(coerced-real-bound low 'float)
1823 ,(coerced-real-bound high 'float))
1824 (rational ,(coerced-real-bound low 'rational)
1825 ,(coerced-real-bound high 'rational)))))
1826 (!def-type-translator float (&optional (low '*) (high '*))
1828 `(or (single-float ,(coerced-float-bound low 'single-float)
1829 ,(coerced-float-bound high 'single-float))
1830 (double-float ,(coerced-float-bound low 'double-float)
1831 ,(coerced-float-bound high 'double-float))
1832 #!+long-float ,(error "stub: no long float support yet"))))
1834 (defmacro !define-float-format (f)
1835 `(!def-bounded-type ,f float ,f))
1837 (!define-float-format short-float)
1838 (!define-float-format single-float)
1839 (!define-float-format double-float)
1840 (!define-float-format long-float)
1842 (defun numeric-types-intersect (type1 type2)
1843 (declare (type numeric-type type1 type2))
1844 (let* ((class1 (numeric-type-class type1))
1845 (class2 (numeric-type-class type2))
1846 (complexp1 (numeric-type-complexp type1))
1847 (complexp2 (numeric-type-complexp type2))
1848 (format1 (numeric-type-format type1))
1849 (format2 (numeric-type-format type2))
1850 (low1 (numeric-type-low type1))
1851 (high1 (numeric-type-high type1))
1852 (low2 (numeric-type-low type2))
1853 (high2 (numeric-type-high type2)))
1854 ;; If one is complex and the other isn't, then they are disjoint.
1855 (cond ((not (or (eq complexp1 complexp2)
1856 (null complexp1) (null complexp2)))
1858 ;; If either type is a float, then the other must either be
1859 ;; specified to be a float or unspecified. Otherwise, they
1861 ((and (eq class1 'float)
1862 (not (member class2 '(float nil)))) nil)
1863 ((and (eq class2 'float)
1864 (not (member class1 '(float nil)))) nil)
1865 ;; If the float formats are specified and different, the
1866 ;; types are disjoint.
1867 ((not (or (eq format1 format2) (null format1) (null format2)))
1870 ;; Check the bounds. This is a bit odd because we must
1871 ;; always have the outer bound of the interval as the
1873 (if (numeric-bound-test high1 high2 <= <)
1874 (or (and (numeric-bound-test low1 low2 >= >)
1875 (numeric-bound-test* low1 high2 <= <))
1876 (and (numeric-bound-test low2 low1 >= >)
1877 (numeric-bound-test* low2 high1 <= <)))
1878 (or (and (numeric-bound-test* low2 high1 <= <)
1879 (numeric-bound-test low2 low1 >= >))
1880 (and (numeric-bound-test high2 high1 <= <)
1881 (numeric-bound-test* high2 low1 >= >))))))))
1883 ;;; Take the numeric bound X and convert it into something that can be
1884 ;;; used as a bound in a numeric type with the specified CLASS and
1885 ;;; FORMAT. If UP-P is true, then we round up as needed, otherwise we
1886 ;;; round down. UP-P true implies that X is a lower bound, i.e. (N) > N.
1888 ;;; This is used by NUMERIC-TYPE-INTERSECTION to mash the bound into
1889 ;;; the appropriate type number. X may only be a float when CLASS is
1892 ;;; ### Note: it is possible for the coercion to a float to overflow
1893 ;;; or underflow. This happens when the bound doesn't fit in the
1894 ;;; specified format. In this case, we should really return the
1895 ;;; appropriate {Most | Least}-{Positive | Negative}-XXX-Float float
1896 ;;; of desired format. But these conditions aren't currently signalled
1897 ;;; in any useful way.
1899 ;;; Also, when converting an open rational bound into a float we
1900 ;;; should probably convert it to a closed bound of the closest float
1901 ;;; in the specified format. KLUDGE: In general, open float bounds are
1902 ;;; screwed up. -- (comment from original CMU CL)
1903 (defun round-numeric-bound (x class format up-p)
1905 (let ((cx (if (consp x) (car x) x)))
1909 (if (and (consp x) (integerp cx))
1910 (if up-p (1+ cx) (1- cx))
1911 (if up-p (ceiling cx) (floor cx))))
1913 (let ((res (if format (coerce cx format) (float cx))))
1914 (if (consp x) (list res) res)))))
1917 ;;; Handle the case of type intersection on two numeric types. We use
1918 ;;; TYPES-EQUAL-OR-INTERSECT to throw out the case of types with no
1919 ;;; intersection. If an attribute in TYPE1 is unspecified, then we use
1920 ;;; TYPE2's attribute, which must be at least as restrictive. If the
1921 ;;; types intersect, then the only attributes that can be specified
1922 ;;; and different are the class and the bounds.
1924 ;;; When the class differs, we use the more restrictive class. The
1925 ;;; only interesting case is RATIONAL/INTEGER, since RATIONAL includes
1928 ;;; We make the result lower (upper) bound the maximum (minimum) of
1929 ;;; the argument lower (upper) bounds. We convert the bounds into the
1930 ;;; appropriate numeric type before maximizing. This avoids possible
1931 ;;; confusion due to mixed-type comparisons (but I think the result is
1933 (!define-type-method (number :simple-intersection2) (type1 type2)
1934 (declare (type numeric-type type1 type2))
1935 (if (numeric-types-intersect type1 type2)
1936 (let* ((class1 (numeric-type-class type1))
1937 (class2 (numeric-type-class type2))
1938 (class (ecase class1
1940 ((integer float) class1)
1941 (rational (if (eq class2 'integer)
1944 (format (or (numeric-type-format type1)
1945 (numeric-type-format type2))))
1949 :complexp (or (numeric-type-complexp type1)
1950 (numeric-type-complexp type2))
1951 :low (numeric-bound-max
1952 (round-numeric-bound (numeric-type-low type1)
1954 (round-numeric-bound (numeric-type-low type2)
1957 :high (numeric-bound-max
1958 (round-numeric-bound (numeric-type-high type1)
1960 (round-numeric-bound (numeric-type-high type2)
1965 ;;; Given two float formats, return the one with more precision. If
1966 ;;; either one is null, return NIL.
1967 (defun float-format-max (f1 f2)
1969 (dolist (f *float-formats* (error "bad float format: ~S" f1))
1970 (when (or (eq f f1) (eq f f2))
1973 ;;; Return the result of an operation on TYPE1 and TYPE2 according to
1974 ;;; the rules of numeric contagion. This is always NUMBER, some float
1975 ;;; format (possibly complex) or RATIONAL. Due to rational
1976 ;;; canonicalization, there isn't much we can do here with integers or
1977 ;;; rational complex numbers.
1979 ;;; If either argument is not a NUMERIC-TYPE, then return NUMBER. This
1980 ;;; is useful mainly for allowing types that are technically numbers,
1981 ;;; but not a NUMERIC-TYPE.
1982 (defun numeric-contagion (type1 type2)
1983 (if (and (numeric-type-p type1) (numeric-type-p type2))
1984 (let ((class1 (numeric-type-class type1))
1985 (class2 (numeric-type-class type2))
1986 (format1 (numeric-type-format type1))
1987 (format2 (numeric-type-format type2))
1988 (complexp1 (numeric-type-complexp type1))
1989 (complexp2 (numeric-type-complexp type2)))
1990 (cond ((or (null complexp1)
1992 (specifier-type 'number))
1996 :format (ecase class2
1997 (float (float-format-max format1 format2))
1998 ((integer rational) format1)
2000 ;; A double-float with any real number is a
2003 (if (eq format1 'double-float)
2006 ;; A long-float with any real number is a
2009 (if (eq format1 'long-float)
2012 :complexp (if (or (eq complexp1 :complex)
2013 (eq complexp2 :complex))
2016 ((eq class2 'float) (numeric-contagion type2 type1))
2017 ((and (eq complexp1 :real) (eq complexp2 :real))
2019 :class (and class1 class2 'rational)
2022 (specifier-type 'number))))
2023 (specifier-type 'number)))
2027 (!define-type-class array)
2029 ;;; What this does depends on the setting of the
2030 ;;; *USE-IMPLEMENTATION-TYPES* switch. If true, return the specialized
2031 ;;; element type, otherwise return the original element type.
2032 (defun specialized-element-type-maybe (type)
2033 (declare (type array-type type))
2034 (if *use-implementation-types*
2035 (array-type-specialized-element-type type)
2036 (array-type-element-type type)))
2038 (!define-type-method (array :simple-=) (type1 type2)
2039 (if (or (unknown-type-p (array-type-element-type type1))
2040 (unknown-type-p (array-type-element-type type2)))
2041 (multiple-value-bind (equalp certainp)
2042 (type= (array-type-element-type type1)
2043 (array-type-element-type type2))
2044 ;; by its nature, the call to TYPE= should never return NIL,
2045 ;; T, as we don't know what the UNKNOWN-TYPE will grow up to
2046 ;; be. -- CSR, 2002-08-19
2047 (aver (not (and (not equalp) certainp)))
2048 (values equalp certainp))
2049 (values (and (equal (array-type-dimensions type1)
2050 (array-type-dimensions type2))
2051 (eq (array-type-complexp type1)
2052 (array-type-complexp type2))
2053 (type= (specialized-element-type-maybe type1)
2054 (specialized-element-type-maybe type2)))
2057 (!define-type-method (array :unparse) (type)
2058 (let ((dims (array-type-dimensions type))
2059 (eltype (type-specifier (array-type-element-type type)))
2060 (complexp (array-type-complexp type)))
2063 (if complexp 'array 'simple-array)
2064 (if complexp `(array ,eltype) `(simple-array ,eltype))))
2065 ((= (length dims) 1)
2067 (if (eq (car dims) '*)
2070 (base-char 'base-string)
2073 (t `(vector ,eltype)))
2075 (bit `(bit-vector ,(car dims)))
2076 (base-char `(base-string ,(car dims)))
2077 (character `(string ,(car dims)))
2078 (t `(vector ,eltype ,(car dims)))))
2079 (if (eq (car dims) '*)
2081 (bit 'simple-bit-vector)
2082 (base-char 'simple-base-string)
2083 (character 'simple-string)
2084 ((t) 'simple-vector)
2085 (t `(simple-array ,eltype (*))))
2087 (bit `(simple-bit-vector ,(car dims)))
2088 (base-char `(simple-base-string ,(car dims)))
2089 (character `(simple-string ,(car dims)))
2090 ((t) `(simple-vector ,(car dims)))
2091 (t `(simple-array ,eltype ,dims))))))
2094 `(array ,eltype ,dims)
2095 `(simple-array ,eltype ,dims))))))
2097 (!define-type-method (array :simple-subtypep) (type1 type2)
2098 (let ((dims1 (array-type-dimensions type1))
2099 (dims2 (array-type-dimensions type2))
2100 (complexp2 (array-type-complexp type2)))
2101 (cond (;; not subtypep unless dimensions are compatible
2102 (not (or (eq dims2 '*)
2103 (and (not (eq dims1 '*))
2104 ;; (sbcl-0.6.4 has trouble figuring out that
2105 ;; DIMS1 and DIMS2 must be lists at this
2106 ;; point, and knowing that is important to
2107 ;; compiling EVERY efficiently.)
2108 (= (length (the list dims1))
2109 (length (the list dims2)))
2110 (every (lambda (x y)
2111 (or (eq y '*) (eql x y)))
2113 (the list dims2)))))
2115 ;; not subtypep unless complexness is compatible
2116 ((not (or (eq complexp2 :maybe)
2117 (eq (array-type-complexp type1) complexp2)))
2119 ;; Since we didn't fail any of the tests above, we win
2120 ;; if the TYPE2 element type is wild.
2121 ((eq (array-type-element-type type2) *wild-type*)
2123 (;; Since we didn't match any of the special cases above, we
2124 ;; can't give a good answer unless both the element types
2125 ;; have been defined.
2126 (or (unknown-type-p (array-type-element-type type1))
2127 (unknown-type-p (array-type-element-type type2)))
2129 (;; Otherwise, the subtype relationship holds iff the
2130 ;; types are equal, and they're equal iff the specialized
2131 ;; element types are identical.
2133 (values (type= (specialized-element-type-maybe type1)
2134 (specialized-element-type-maybe type2))
2137 (!define-superclasses array
2143 (defun array-types-intersect (type1 type2)
2144 (declare (type array-type type1 type2))
2145 (let ((dims1 (array-type-dimensions type1))
2146 (dims2 (array-type-dimensions type2))
2147 (complexp1 (array-type-complexp type1))
2148 (complexp2 (array-type-complexp type2)))
2149 ;; See whether dimensions are compatible.
2150 (cond ((not (or (eq dims1 '*) (eq dims2 '*)
2151 (and (= (length dims1) (length dims2))
2152 (every (lambda (x y)
2153 (or (eq x '*) (eq y '*) (= x y)))
2156 ;; See whether complexpness is compatible.
2157 ((not (or (eq complexp1 :maybe)
2158 (eq complexp2 :maybe)
2159 (eq complexp1 complexp2)))
2163 ;; If either element type is wild, then they intersect.
2164 ;; Otherwise, the types must be identical.
2166 ;; FIXME: There seems to have been a fair amount of
2167 ;; confusion about the distinction between requested element
2168 ;; type and specialized element type; here is one of
2169 ;; them. If we request an array to hold objects of an
2170 ;; unknown type, we can do no better than represent that
2171 ;; type as an array specialized on wild-type. We keep the
2172 ;; requested element-type in the -ELEMENT-TYPE slot, and
2173 ;; *WILD-TYPE* in the -SPECIALIZED-ELEMENT-TYPE. So, here,
2174 ;; we must test for the SPECIALIZED slot being *WILD-TYPE*,
2175 ;; not just the ELEMENT-TYPE slot. Maybe the return value
2176 ;; in that specific case should be T, NIL? Or maybe this
2177 ;; function should really be called
2178 ;; ARRAY-TYPES-COULD-POSSIBLY-INTERSECT? In any case, this
2179 ;; was responsible for bug #123, and this whole issue could
2180 ;; do with a rethink and/or a rewrite. -- CSR, 2002-08-21
2181 ((or (eq (array-type-specialized-element-type type1) *wild-type*)
2182 (eq (array-type-specialized-element-type type2) *wild-type*)
2183 (type= (specialized-element-type-maybe type1)
2184 (specialized-element-type-maybe type2)))
2190 (!define-type-method (array :simple-intersection2) (type1 type2)
2191 (declare (type array-type type1 type2))
2192 (if (array-types-intersect type1 type2)
2193 (let ((dims1 (array-type-dimensions type1))
2194 (dims2 (array-type-dimensions type2))
2195 (complexp1 (array-type-complexp type1))
2196 (complexp2 (array-type-complexp type2))
2197 (eltype1 (array-type-element-type type1))
2198 (eltype2 (array-type-element-type type2)))
2199 (specialize-array-type
2201 :dimensions (cond ((eq dims1 '*) dims2)
2202 ((eq dims2 '*) dims1)
2204 (mapcar (lambda (x y) (if (eq x '*) y x))
2206 :complexp (if (eq complexp1 :maybe) complexp2 complexp1)
2207 :element-type (if (eq eltype1 *wild-type*) eltype2 eltype1))))
2210 ;;; Check a supplied dimension list to determine whether it is legal,
2211 ;;; and return it in canonical form (as either '* or a list).
2212 (defun canonical-array-dimensions (dims)
2217 (error "Arrays can't have a negative number of dimensions: ~S" dims))
2218 (when (>= dims sb!xc:array-rank-limit)
2219 (error "array type with too many dimensions: ~S" dims))
2220 (make-list dims :initial-element '*))
2222 (when (>= (length dims) sb!xc:array-rank-limit)
2223 (error "array type with too many dimensions: ~S" dims))
2226 (unless (and (integerp dim)
2228 (< dim sb!xc:array-dimension-limit))
2229 (error "bad dimension in array type: ~S" dim))))
2232 (error "Array dimensions is not a list, integer or *:~% ~S" dims))))
2236 (!define-type-class member)
2238 (!define-type-method (member :unparse) (type)
2239 (let ((members (member-type-members type)))
2241 ((equal members '(nil)) 'null)
2242 ((type= type (specifier-type 'standard-char)) 'standard-char)
2243 (t `(member ,@members)))))
2245 (!define-type-method (member :simple-subtypep) (type1 type2)
2246 (values (subsetp (member-type-members type1) (member-type-members type2))
2249 (!define-type-method (member :complex-subtypep-arg1) (type1 type2)
2250 (every/type (swapped-args-fun #'ctypep)
2252 (member-type-members type1)))
2254 ;;; We punt if the odd type is enumerable and intersects with the
2255 ;;; MEMBER type. If not enumerable, then it is definitely not a
2256 ;;; subtype of the MEMBER type.
2257 (!define-type-method (member :complex-subtypep-arg2) (type1 type2)
2258 (cond ((not (type-enumerable type1)) (values nil t))
2259 ((types-equal-or-intersect type1 type2)
2260 (invoke-complex-subtypep-arg1-method type1 type2))
2261 (t (values nil t))))
2263 (!define-type-method (member :simple-intersection2) (type1 type2)
2264 (let ((mem1 (member-type-members type1))
2265 (mem2 (member-type-members type2)))
2266 (cond ((subsetp mem1 mem2) type1)
2267 ((subsetp mem2 mem1) type2)
2269 (let ((res (intersection mem1 mem2)))
2271 (make-member-type :members res)
2274 (!define-type-method (member :complex-intersection2) (type1 type2)
2276 (collect ((members))
2277 (let ((mem2 (member-type-members type2)))
2278 (dolist (member mem2)
2279 (multiple-value-bind (val win) (ctypep member type1)
2281 (return-from punt nil))
2282 (when val (members member))))
2283 (cond ((subsetp mem2 (members)) type2)
2284 ((null (members)) *empty-type*)
2286 (make-member-type :members (members))))))))
2288 ;;; We don't need a :COMPLEX-UNION2, since the only interesting case is
2289 ;;; a union type, and the member/union interaction is handled by the
2290 ;;; union type method.
2291 (!define-type-method (member :simple-union2) (type1 type2)
2292 (let ((mem1 (member-type-members type1))
2293 (mem2 (member-type-members type2)))
2294 (cond ((subsetp mem1 mem2) type2)
2295 ((subsetp mem2 mem1) type1)
2297 (make-member-type :members (union mem1 mem2))))))
2299 (!define-type-method (member :simple-=) (type1 type2)
2300 (let ((mem1 (member-type-members type1))
2301 (mem2 (member-type-members type2)))
2302 (values (and (subsetp mem1 mem2)
2303 (subsetp mem2 mem1))
2306 (!define-type-method (member :complex-=) (type1 type2)
2307 (if (type-enumerable type1)
2308 (multiple-value-bind (val win) (csubtypep type2 type1)
2309 (if (or val (not win))
2314 (!def-type-translator member (&rest members)
2317 (dolist (m (remove-duplicates members))
2319 (float (if (zerop m)
2321 (push (ctype-of m) numbers)))
2322 (number (push (ctype-of m) numbers))
2326 (make-member-type :members ms)
2328 (nreverse numbers)))
2331 ;;;; intersection types
2333 ;;;; Until version 0.6.10.6, SBCL followed the original CMU CL approach
2334 ;;;; of punting on all AND types, not just the unreasonably complicated
2335 ;;;; ones. The change was motivated by trying to get the KEYWORD type
2336 ;;;; to behave sensibly:
2337 ;;;; ;; reasonable definition
2338 ;;;; (DEFTYPE KEYWORD () '(AND SYMBOL (SATISFIES KEYWORDP)))
2339 ;;;; ;; reasonable behavior
2340 ;;;; (AVER (SUBTYPEP 'KEYWORD 'SYMBOL))
2341 ;;;; Without understanding a little about the semantics of AND, we'd
2342 ;;;; get (SUBTYPEP 'KEYWORD 'SYMBOL)=>NIL,NIL and, for entirely
2343 ;;;; parallel reasons, (SUBTYPEP 'RATIO 'NUMBER)=>NIL,NIL. That's
2346 ;;;; We still follow the example of CMU CL to some extent, by punting
2347 ;;;; (to the opaque HAIRY-TYPE) on sufficiently complicated types
2350 (!define-type-class intersection)
2352 ;;; A few intersection types have special names. The others just get
2353 ;;; mechanically unparsed.
2354 (!define-type-method (intersection :unparse) (type)
2355 (declare (type ctype type))
2356 (or (find type '(ratio keyword) :key #'specifier-type :test #'type=)
2357 `(and ,@(mapcar #'type-specifier (intersection-type-types type)))))
2359 ;;; shared machinery for type equality: true if every type in the set
2360 ;;; TYPES1 matches a type in the set TYPES2 and vice versa
2361 (defun type=-set (types1 types2)
2362 (flet ((type<=-set (x y)
2363 (declare (type list x y))
2364 (every/type (lambda (x y-element)
2365 (any/type #'type= y-element x))
2367 (and/type (type<=-set types1 types2)
2368 (type<=-set types2 types1))))
2370 ;;; Two intersection types are equal if their subtypes are equal sets.
2372 ;;; FIXME: Might it be better to use
2373 ;;; (AND (SUBTYPEP X Y) (SUBTYPEP Y X))
2374 ;;; instead, since SUBTYPEP is the usual relationship that we care
2375 ;;; most about, so it would be good to leverage any ingenuity there
2376 ;;; in this more obscure method?
2377 (!define-type-method (intersection :simple-=) (type1 type2)
2378 (type=-set (intersection-type-types type1)
2379 (intersection-type-types type2)))
2381 (defun %intersection-complex-subtypep-arg1 (type1 type2)
2382 (type= type1 (type-intersection type1 type2)))
2384 (defun %intersection-simple-subtypep (type1 type2)
2385 (every/type #'%intersection-complex-subtypep-arg1
2387 (intersection-type-types type2)))
2389 (!define-type-method (intersection :simple-subtypep) (type1 type2)
2390 (%intersection-simple-subtypep type1 type2))
2392 (!define-type-method (intersection :complex-subtypep-arg1) (type1 type2)
2393 (%intersection-complex-subtypep-arg1 type1 type2))
2395 (defun %intersection-complex-subtypep-arg2 (type1 type2)
2396 (every/type #'csubtypep type1 (intersection-type-types type2)))
2398 (!define-type-method (intersection :complex-subtypep-arg2) (type1 type2)
2399 (%intersection-complex-subtypep-arg2 type1 type2))
2401 ;;; FIXME: This will look eeriely familiar to readers of the UNION
2402 ;;; :SIMPLE-INTERSECTION2 :COMPLEX-INTERSECTION2 method. That's
2403 ;;; because it was generated by cut'n'paste methods. Given that
2404 ;;; intersections and unions have all sorts of symmetries known to
2405 ;;; mathematics, it shouldn't be beyond the ken of some programmers to
2406 ;;; reflect those symmetries in code in a way that ties them together
2407 ;;; more strongly than having two independent near-copies :-/
2408 (!define-type-method (intersection :simple-union2 :complex-union2)
2410 ;; Within this method, type2 is guaranteed to be an intersection
2412 (aver (intersection-type-p type2))
2413 ;; Make sure to call only the applicable methods...
2414 (cond ((and (intersection-type-p type1)
2415 (%intersection-simple-subtypep type1 type2)) type2)
2416 ((and (intersection-type-p type1)
2417 (%intersection-simple-subtypep type2 type1)) type1)
2418 ((and (not (intersection-type-p type1))
2419 (%intersection-complex-subtypep-arg2 type1 type2))
2421 ((and (not (intersection-type-p type1))
2422 (%intersection-complex-subtypep-arg1 type2 type1))
2424 ;; KLUDGE: This special (and somewhat hairy) magic is required
2425 ;; to deal with the RATIONAL/INTEGER special case. The UNION
2426 ;; of (INTEGER * -1) and (AND (RATIONAL * -1/2) (NOT INTEGER))
2427 ;; should be (RATIONAL * -1/2) -- CSR, 2003-02-28
2428 ((and (csubtypep type2 (specifier-type 'ratio))
2429 (numeric-type-p type1)
2430 (csubtypep type1 (specifier-type 'integer))
2435 :low (if (null (numeric-type-low type1))
2437 (list (1- (numeric-type-low type1))))
2438 :high (if (null (numeric-type-high type1))
2440 (list (1+ (numeric-type-high type1)))))))
2442 (apply #'type-intersection
2443 (remove (specifier-type '(not integer))
2444 (intersection-type-types type2)
2447 (let ((accumulator *universal-type*))
2448 (do ((t2s (intersection-type-types type2) (cdr t2s)))
2449 ((null t2s) accumulator)
2450 (let ((union (type-union type1 (car t2s))))
2451 (when (union-type-p union)
2452 ;; we have to give up here -- there are all sorts of
2453 ;; ordering worries, but it's better than before.
2454 ;; Doing exactly the same as in the UNION
2455 ;; :SIMPLE/:COMPLEX-INTERSECTION2 method causes stack
2456 ;; overflow with the mutual recursion never bottoming
2458 (if (and (eq accumulator *universal-type*)
2460 ;; KLUDGE: if we get here, we have a partially
2461 ;; simplified result. While this isn't by any
2462 ;; means a universal simplification, including
2463 ;; this logic here means that we can get (OR
2464 ;; KEYWORD (NOT KEYWORD)) canonicalized to T.
2468 (type-intersection accumulator union))))))))
2470 (!def-type-translator and (&whole whole &rest type-specifiers)
2471 (apply #'type-intersection
2472 (mapcar #'specifier-type
2477 (!define-type-class union)
2479 ;;; The LIST, FLOAT and REAL types have special names. Other union
2480 ;;; types just get mechanically unparsed.
2481 (!define-type-method (union :unparse) (type)
2482 (declare (type ctype type))
2484 ((type= type (specifier-type 'list)) 'list)
2485 ((type= type (specifier-type 'float)) 'float)
2486 ((type= type (specifier-type 'real)) 'real)
2487 ((type= type (specifier-type 'sequence)) 'sequence)
2488 ((type= type (specifier-type 'bignum)) 'bignum)
2489 (t `(or ,@(mapcar #'type-specifier (union-type-types type))))))
2491 ;;; Two union types are equal if they are each subtypes of each
2492 ;;; other. We need to be this clever because our complex subtypep
2493 ;;; methods are now more accurate; we don't get infinite recursion
2494 ;;; because the simple-subtypep method delegates to complex-subtypep
2495 ;;; of the individual types of type1. - CSR, 2002-04-09
2497 ;;; Previous comment, now obsolete, but worth keeping around because
2498 ;;; it is true, though too strong a condition:
2500 ;;; Two union types are equal if their subtypes are equal sets.
2501 (!define-type-method (union :simple-=) (type1 type2)
2502 (multiple-value-bind (subtype certain?)
2503 (csubtypep type1 type2)
2505 (csubtypep type2 type1)
2506 ;; we might as well become as certain as possible.
2509 (multiple-value-bind (subtype certain?)
2510 (csubtypep type2 type1)
2511 (declare (ignore subtype))
2512 (values nil certain?))))))
2514 (!define-type-method (union :complex-=) (type1 type2)
2515 (declare (ignore type1))
2516 (if (some #'type-might-contain-other-types-p
2517 (union-type-types type2))
2521 ;;; Similarly, a union type is a subtype of another if and only if
2522 ;;; every element of TYPE1 is a subtype of TYPE2.
2523 (defun union-simple-subtypep (type1 type2)
2524 (every/type (swapped-args-fun #'union-complex-subtypep-arg2)
2526 (union-type-types type1)))
2528 (!define-type-method (union :simple-subtypep) (type1 type2)
2529 (union-simple-subtypep type1 type2))
2531 (defun union-complex-subtypep-arg1 (type1 type2)
2532 (every/type (swapped-args-fun #'csubtypep)
2534 (union-type-types type1)))
2536 (!define-type-method (union :complex-subtypep-arg1) (type1 type2)
2537 (union-complex-subtypep-arg1 type1 type2))
2539 (defun union-complex-subtypep-arg2 (type1 type2)
2540 (multiple-value-bind (sub-value sub-certain?)
2541 ;; was: (any/type #'csubtypep type1 (union-type-types type2)),
2542 ;; which turns out to be too restrictive, causing bug 91.
2544 ;; the following reimplementation might look dodgy. It is
2545 ;; dodgy. It depends on the union :complex-= method not doing
2546 ;; very much work -- certainly, not using subtypep. Reasoning:
2548 ;; At this stage, we know that type2 is a union type and type1
2549 ;; isn't. We might as well check this, though:
2550 (aver (union-type-p type2))
2551 (aver (not (union-type-p type1)))
2552 ;; A is a subset of (B1 u B2)
2553 ;; <=> A n (B1 u B2) = A
2554 ;; <=> (A n B1) u (A n B2) = A
2556 ;; But, we have to be careful not to delegate this type= to
2557 ;; something that could invoke subtypep, which might get us
2558 ;; back here -> stack explosion. We therefore ensure that the
2559 ;; second type (which is the one that's dispatched on) is
2560 ;; either a union type (where we've ensured that the complex-=
2561 ;; method will not call subtypep) or something with no union
2562 ;; types involved, in which case we'll never come back here.
2564 ;; If we don't do this, then e.g.
2565 ;; (SUBTYPEP '(MEMBER 3) '(OR (SATISFIES FOO) (SATISFIES BAR)))
2566 ;; would loop infinitely, as the member :complex-= method is
2567 ;; implemented in terms of subtypep.
2569 ;; Ouch. - CSR, 2002-04-10
2572 (mapcar (lambda (x) (type-intersection type1 x))
2573 (union-type-types type2)))))
2575 (values sub-value sub-certain?)
2576 ;; The ANY/TYPE expression above is a sufficient condition for
2577 ;; subsetness, but not a necessary one, so we might get a more
2578 ;; certain answer by this CALL-NEXT-METHOD-ish step when the
2579 ;; ANY/TYPE expression is uncertain.
2580 (invoke-complex-subtypep-arg1-method type1 type2))))
2582 (!define-type-method (union :complex-subtypep-arg2) (type1 type2)
2583 (union-complex-subtypep-arg2 type1 type2))
2585 (!define-type-method (union :simple-intersection2 :complex-intersection2)
2587 ;; The CSUBTYPEP clauses here let us simplify e.g.
2588 ;; (TYPE-INTERSECTION2 (SPECIFIER-TYPE 'LIST)
2589 ;; (SPECIFIER-TYPE '(OR LIST VECTOR)))
2590 ;; (where LIST is (OR CONS NULL)).
2592 ;; The tests are more or less (CSUBTYPEP TYPE1 TYPE2) and vice
2593 ;; versa, but it's important that we pre-expand them into
2594 ;; specialized operations on individual elements of
2595 ;; UNION-TYPE-TYPES, instead of using the ordinary call to
2596 ;; CSUBTYPEP, in order to avoid possibly invoking any methods which
2597 ;; might in turn invoke (TYPE-INTERSECTION2 TYPE1 TYPE2) and thus
2598 ;; cause infinite recursion.
2600 ;; Within this method, type2 is guaranteed to be a union type:
2601 (aver (union-type-p type2))
2602 ;; Make sure to call only the applicable methods...
2603 (cond ((and (union-type-p type1)
2604 (union-simple-subtypep type1 type2)) type1)
2605 ((and (union-type-p type1)
2606 (union-simple-subtypep type2 type1)) type2)
2607 ((and (not (union-type-p type1))
2608 (union-complex-subtypep-arg2 type1 type2))
2610 ((and (not (union-type-p type1))
2611 (union-complex-subtypep-arg1 type2 type1))
2614 ;; KLUDGE: This code accumulates a sequence of TYPE-UNION2
2615 ;; operations in a particular order, and gives up if any of
2616 ;; the sub-unions turn out not to be simple. In other cases
2617 ;; ca. sbcl-0.6.11.15, that approach to taking a union was a
2618 ;; bad idea, since it can overlook simplifications which
2619 ;; might occur if the terms were accumulated in a different
2620 ;; order. It's possible that that will be a problem here too.
2621 ;; However, I can't think of a good example to demonstrate
2622 ;; it, and without an example to demonstrate it I can't write
2623 ;; test cases, and without test cases I don't want to
2624 ;; complicate the code to address what's still a hypothetical
2625 ;; problem. So I punted. -- WHN 2001-03-20
2626 (let ((accumulator *empty-type*))
2627 (dolist (t2 (union-type-types type2) accumulator)
2629 (type-union accumulator
2630 (type-intersection type1 t2))))))))
2632 (!def-type-translator or (&rest type-specifiers)
2634 (mapcar #'specifier-type
2639 (!define-type-class cons)
2641 (!def-type-translator cons (&optional (car-type-spec '*) (cdr-type-spec '*))
2642 (let ((car-type (specifier-type car-type-spec))
2643 (cdr-type (specifier-type cdr-type-spec)))
2644 (make-cons-type car-type cdr-type)))
2646 (!define-type-method (cons :unparse) (type)
2647 (let ((car-eltype (type-specifier (cons-type-car-type type)))
2648 (cdr-eltype (type-specifier (cons-type-cdr-type type))))
2649 (if (and (member car-eltype '(t *))
2650 (member cdr-eltype '(t *)))
2652 `(cons ,car-eltype ,cdr-eltype))))
2654 (!define-type-method (cons :simple-=) (type1 type2)
2655 (declare (type cons-type type1 type2))
2656 (and (type= (cons-type-car-type type1) (cons-type-car-type type2))
2657 (type= (cons-type-cdr-type type1) (cons-type-cdr-type type2))))
2659 (!define-type-method (cons :simple-subtypep) (type1 type2)
2660 (declare (type cons-type type1 type2))
2661 (multiple-value-bind (val-car win-car)
2662 (csubtypep (cons-type-car-type type1) (cons-type-car-type type2))
2663 (multiple-value-bind (val-cdr win-cdr)
2664 (csubtypep (cons-type-cdr-type type1) (cons-type-cdr-type type2))
2665 (if (and val-car val-cdr)
2666 (values t (and win-car win-cdr))
2667 (values nil (or win-car win-cdr))))))
2669 ;;; Give up if a precise type is not possible, to avoid returning
2670 ;;; overly general types.
2671 (!define-type-method (cons :simple-union2) (type1 type2)
2672 (declare (type cons-type type1 type2))
2673 (let ((car-type1 (cons-type-car-type type1))
2674 (car-type2 (cons-type-car-type type2))
2675 (cdr-type1 (cons-type-cdr-type type1))
2676 (cdr-type2 (cons-type-cdr-type type2)))
2677 ;; UGH. -- CSR, 2003-02-24
2678 (macrolet ((frob-car (car1 car2 cdr1 cdr2)
2680 (make-cons-type ,car1 (type-union ,cdr1 ,cdr2))
2682 (type-intersection ,car2
2684 `(not ,(type-specifier ,car1))))
2686 (cond ((type= car-type1 car-type2)
2687 (make-cons-type car-type1
2688 (type-union cdr-type1 cdr-type2)))
2689 ((type= cdr-type1 cdr-type2)
2690 (make-cons-type (type-union car-type1 car-type2)
2692 ((csubtypep car-type1 car-type2)
2693 (frob-car car-type1 car-type2 cdr-type1 cdr-type2))
2694 ((csubtypep car-type2 car-type1)
2695 (frob-car car-type2 car-type1 cdr-type2 cdr-type1))
2696 ;; Don't put these in -- consider the effect of taking the
2697 ;; union of (CONS (INTEGER 0 2) (INTEGER 5 7)) and
2698 ;; (CONS (INTEGER 0 3) (INTEGER 5 6)).
2700 ((csubtypep cdr-type1 cdr-type2)
2701 (frob-cdr car-type1 car-type2 cdr-type1 cdr-type2))
2703 ((csubtypep cdr-type2 cdr-type1)
2704 (frob-cdr car-type2 car-type1 cdr-type2 cdr-type1))))))
2706 (!define-type-method (cons :simple-intersection2) (type1 type2)
2707 (declare (type cons-type type1 type2))
2710 (and (setf car-int2 (type-intersection2 (cons-type-car-type type1)
2711 (cons-type-car-type type2)))
2712 (setf cdr-int2 (type-intersection2 (cons-type-cdr-type type1)
2713 (cons-type-cdr-type type2)))
2714 (make-cons-type car-int2 cdr-int2))))
2716 ;;; Return the type that describes all objects that are in X but not
2717 ;;; in Y. If we can't determine this type, then return NIL.
2719 ;;; For now, we only are clever dealing with union and member types.
2720 ;;; If either type is not a union type, then we pretend that it is a
2721 ;;; union of just one type. What we do is remove from X all the types
2722 ;;; that are a subtype any type in Y. If any type in X intersects with
2723 ;;; a type in Y but is not a subtype, then we give up.
2725 ;;; We must also special-case any member type that appears in the
2726 ;;; union. We remove from X's members all objects that are TYPEP to Y.
2727 ;;; If Y has any members, we must be careful that none of those
2728 ;;; members are CTYPEP to any of Y's non-member types. We give up in
2729 ;;; this case, since to compute that difference we would have to break
2730 ;;; the type from X into some collection of types that represents the
2731 ;;; type without that particular element. This seems too hairy to be
2732 ;;; worthwhile, given its low utility.
2733 (defun type-difference (x y)
2734 (let ((x-types (if (union-type-p x) (union-type-types x) (list x)))
2735 (y-types (if (union-type-p y) (union-type-types y) (list y))))
2737 (dolist (x-type x-types)
2738 (if (member-type-p x-type)
2739 (collect ((members))
2740 (dolist (mem (member-type-members x-type))
2741 (multiple-value-bind (val win) (ctypep mem y)
2742 (unless win (return-from type-difference nil))
2746 (res (make-member-type :members (members)))))
2747 (dolist (y-type y-types (res x-type))
2748 (multiple-value-bind (val win) (csubtypep x-type y-type)
2749 (unless win (return-from type-difference nil))
2751 (when (types-equal-or-intersect x-type y-type)
2752 (return-from type-difference nil))))))
2753 (let ((y-mem (find-if #'member-type-p y-types)))
2755 (let ((members (member-type-members y-mem)))
2756 (dolist (x-type x-types)
2757 (unless (member-type-p x-type)
2758 (dolist (member members)
2759 (multiple-value-bind (val win) (ctypep member x-type)
2760 (when (or (not win) val)
2761 (return-from type-difference nil)))))))))
2762 (apply #'type-union (res)))))
2764 (!def-type-translator array (&optional (element-type '*)
2766 (specialize-array-type
2767 (make-array-type :dimensions (canonical-array-dimensions dimensions)
2769 :element-type (specifier-type element-type))))
2771 (!def-type-translator simple-array (&optional (element-type '*)
2773 (specialize-array-type
2774 (make-array-type :dimensions (canonical-array-dimensions dimensions)
2776 :element-type (specifier-type element-type))))
2778 ;;;; utilities shared between cross-compiler and target system
2780 ;;; Does the type derived from compilation of an actual function
2781 ;;; definition satisfy declarations of a function's type?
2782 (defun defined-ftype-matches-declared-ftype-p (defined-ftype declared-ftype)
2783 (declare (type ctype defined-ftype declared-ftype))
2784 (flet ((is-built-in-class-function-p (ctype)
2785 (and (built-in-classoid-p ctype)
2786 (eq (built-in-classoid-name ctype) 'function))))
2787 (cond (;; DECLARED-FTYPE could certainly be #<BUILT-IN-CLASS FUNCTION>;
2788 ;; that's what happens when we (DECLAIM (FTYPE FUNCTION FOO)).
2789 (is-built-in-class-function-p declared-ftype)
2790 ;; In that case, any definition satisfies the declaration.
2792 (;; It's not clear whether or how DEFINED-FTYPE might be
2793 ;; #<BUILT-IN-CLASS FUNCTION>, but it's not obviously
2794 ;; invalid, so let's handle that case too, just in case.
2795 (is-built-in-class-function-p defined-ftype)
2796 ;; No matter what DECLARED-FTYPE might be, we can't prove
2797 ;; that an object of type FUNCTION doesn't satisfy it, so
2798 ;; we return success no matter what.
2800 (;; Otherwise both of them must be FUN-TYPE objects.
2802 ;; FIXME: For now we only check compatibility of the return
2803 ;; type, not argument types, and we don't even check the
2804 ;; return type very precisely (as per bug 94a). It would be
2805 ;; good to do a better job. Perhaps to check the
2806 ;; compatibility of the arguments, we should (1) redo
2807 ;; VALUES-TYPES-EQUAL-OR-INTERSECT as
2808 ;; ARGS-TYPES-EQUAL-OR-INTERSECT, and then (2) apply it to
2809 ;; the ARGS-TYPE slices of the FUN-TYPEs. (ARGS-TYPE
2810 ;; is a base class both of VALUES-TYPE and of FUN-TYPE.)
2811 (values-types-equal-or-intersect
2812 (fun-type-returns defined-ftype)
2813 (fun-type-returns declared-ftype))))))
2815 ;;; This messy case of CTYPE for NUMBER is shared between the
2816 ;;; cross-compiler and the target system.
2817 (defun ctype-of-number (x)
2818 (let ((num (if (complexp x) (realpart x) x)))
2819 (multiple-value-bind (complexp low high)
2821 (let ((imag (imagpart x)))
2822 (values :complex (min num imag) (max num imag)))
2823 (values :real num num))
2824 (make-numeric-type :class (etypecase num
2826 (rational 'rational)
2828 :format (and (floatp num) (float-format-name num))
2834 ;; Why SAFETY 0? To suppress the is-it-the-right-structure-type
2835 ;; checking for declarations in structure accessors. Otherwise we
2836 ;; can get caught in a chicken-and-egg bootstrapping problem, whose
2837 ;; symptom on x86 OpenBSD sbcl-0.pre7.37.flaky5.22 is an illegal
2838 ;; instruction trap. I haven't tracked it down, but I'm guessing it
2839 ;; has to do with setting LAYOUTs when the LAYOUT hasn't been set
2841 (declare (optimize (safety 0)))
2842 (!defun-from-collected-cold-init-forms !late-type-cold-init))
2844 (/show0 "late-type.lisp end of file")