1 ;;;; This file contains the definition of non-CLASS types (e.g.
2 ;;;; subtypes of interesting BUILT-IN-CLASSes) and the interfaces to
3 ;;;; the type system. Common Lisp type specifiers are parsed into a
4 ;;;; somewhat canonical internal type representation that supports
5 ;;;; type union, intersection, etc. (Except that ALIEN types have
8 ;;;; This software is part of the SBCL system. See the README file for
11 ;;;; This software is derived from the CMU CL system, which was
12 ;;;; written at Carnegie Mellon University and released into the
13 ;;;; public domain. The software is in the public domain and is
14 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
15 ;;;; files for more information.
17 (in-package "SB!KERNEL")
19 (/show0 "late-type.lisp 19")
21 (!begin-collecting-cold-init-forms)
23 ;;; ### Remaining incorrectnesses:
25 ;;; TYPE-UNION (and the OR type) doesn't properly canonicalize an
26 ;;; exhaustive partition or coalesce contiguous ranges of numeric
29 ;;; There are all sorts of nasty problems with open bounds on FLOAT
30 ;;; types (and probably FLOAT types in general.)
32 ;;; RATIO and BIGNUM are not recognized as numeric types.
34 ;;; FIXME: This really should go away. Alas, it doesn't seem to be so
35 ;;; simple to make it go away.. (See bug 123 in BUGS file.)
36 (defvar *use-implementation-types* t ; actually initialized in cold init
38 "*USE-IMPLEMENTATION-TYPES* is a semi-public flag which determines how
39 restrictive we are in determining type membership. If two types are the
40 same in the implementation, then we will consider them them the same when
41 this switch is on. When it is off, we try to be as restrictive as the
42 language allows, allowing us to detect more errors. Currently, this only
43 affects array types.")
44 (!cold-init-forms (setq *use-implementation-types* t))
46 ;;; These functions are used as method for types which need a complex
47 ;;; subtypep method to handle some superclasses, but cover a subtree
48 ;;; of the type graph (i.e. there is no simple way for any other type
49 ;;; class to be a subtype.) There are always still complex ways,
50 ;;; namely UNION and MEMBER types, so we must give TYPE1's method a
51 ;;; chance to run, instead of immediately returning NIL, T.
52 (defun delegate-complex-subtypep-arg2 (type1 type2)
54 (type-class-complex-subtypep-arg1
55 (type-class-info type1))))
57 (funcall subtypep-arg1 type1 type2)
59 (defun delegate-complex-intersection2 (type1 type2)
60 (let ((method (type-class-complex-intersection2 (type-class-info type1))))
61 (if (and method (not (eq method #'delegate-complex-intersection2)))
62 (funcall method type2 type1)
63 (hierarchical-intersection2 type1 type2))))
65 ;;; This is used by !DEFINE-SUPERCLASSES to define the SUBTYPE-ARG1
66 ;;; method. INFO is a list of conses
67 ;;; (SUPERCLASS-CLASS . {GUARD-TYPE-SPECIFIER | NIL}).
68 ;;; This will never be called with a hairy type as TYPE2, since the
69 ;;; hairy type TYPE2 method gets first crack.
70 (defun !has-superclasses-complex-subtypep-arg1 (type1 type2 info)
72 (and (sb!xc:typep type2 'sb!xc:class)
74 (when (or (not (cdr x))
75 (csubtypep type1 (specifier-type (cdr x))))
77 (or (eq type2 (car x))
78 (let ((inherits (layout-inherits (class-layout (car x)))))
79 (dotimes (i (length inherits) nil)
80 (when (eq type2 (layout-class (svref inherits i)))
84 ;;; This function takes a list of specs, each of the form
85 ;;; (SUPERCLASS-NAME &OPTIONAL GUARD).
86 ;;; Consider one spec (with no guard): any instance of the named
87 ;;; TYPE-CLASS is also a subtype of the named superclass and of any of
88 ;;; its superclasses. If there are multiple specs, then some will have
89 ;;; guards. We choose the first spec whose guard is a supertype of
90 ;;; TYPE1 and use its superclass. In effect, a sequence of guards
93 ;;; G0,(and G1 (not G0)), (and G2 (not (or G0 G1))).
95 ;;; WHEN controls when the forms are executed.
96 (defmacro !define-superclasses (type-class-name specs when)
97 (let ((type-class (gensym "TYPE-CLASS-"))
98 (info (gensym "INFO")))
100 (let ((,type-class (type-class-or-lose ',type-class-name))
101 (,info (mapcar (lambda (spec)
103 (super &optional guard)
105 (cons (sb!xc:find-class super) guard)))
107 (setf (type-class-complex-subtypep-arg1 ,type-class)
108 (lambda (type1 type2)
109 (!has-superclasses-complex-subtypep-arg1 type1 type2 ,info)))
110 (setf (type-class-complex-subtypep-arg2 ,type-class)
111 #'delegate-complex-subtypep-arg2)
112 (setf (type-class-complex-intersection2 ,type-class)
113 #'delegate-complex-intersection2)))))
115 ;;;; FUNCTION and VALUES types
117 ;;;; Pretty much all of the general type operations are illegal on
118 ;;;; VALUES types, since we can't discriminate using them, do
119 ;;;; SUBTYPEP, etc. FUNCTION types are acceptable to the normal type
120 ;;;; operations, but are generally considered to be equivalent to
121 ;;;; FUNCTION. These really aren't true types in any type theoretic
122 ;;;; sense, but we still parse them into CTYPE structures for two
125 ;;;; -- Parsing and unparsing work the same way, and indeed we can't
126 ;;;; tell whether a type is a function or values type without
128 ;;;; -- Many of the places that can be annotated with real types can
129 ;;;; also be annotated with function or values types.
131 ;;; the description of a &KEY argument
132 (defstruct (key-info #-sb-xc-host (:pure t)
134 ;; the key (not necessarily a keyword in ANSI)
135 (name (required-argument) :type symbol)
136 ;; the type of the argument value
137 (type (required-argument) :type ctype))
139 (!define-type-method (values :simple-subtypep :complex-subtypep-arg1)
141 (declare (ignore type2))
142 ;; FIXME: should be TYPE-ERROR, here and in next method
143 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type1)))
145 (!define-type-method (values :complex-subtypep-arg2)
147 (declare (ignore type1))
148 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type2)))
150 (!define-type-method (values :unparse) (type)
151 (cons 'values (unparse-args-types type)))
153 ;;; Return true if LIST1 and LIST2 have the same elements in the same
154 ;;; positions according to TYPE=. We return NIL, NIL if there is an
155 ;;; uncertain comparison.
156 (defun type=-list (list1 list2)
157 (declare (list list1 list2))
158 (do ((types1 list1 (cdr types1))
159 (types2 list2 (cdr types2)))
160 ((or (null types1) (null types2))
161 (if (or types1 types2)
164 (multiple-value-bind (val win)
165 (type= (first types1) (first types2))
167 (return (values nil nil)))
169 (return (values nil t))))))
171 (!define-type-method (values :simple-=) (type1 type2)
172 (let ((rest1 (args-type-rest type1))
173 (rest2 (args-type-rest type2)))
174 (cond ((or (args-type-keyp type1) (args-type-keyp type2)
175 (args-type-allowp type1) (args-type-allowp type2))
177 ((and rest1 rest2 (type/= rest1 rest2))
182 (multiple-value-bind (req-val req-win)
183 (type=-list (values-type-required type1)
184 (values-type-required type2))
185 (multiple-value-bind (opt-val opt-win)
186 (type=-list (values-type-optional type1)
187 (values-type-optional type2))
188 (values (and req-val opt-val) (and req-win opt-win))))))))
190 (!define-type-class function)
192 ;;; a flag that we can bind to cause complex function types to be
193 ;;; unparsed as FUNCTION. This is useful when we want a type that we
194 ;;; can pass to TYPEP.
195 (defvar *unparse-fun-type-simplify*)
196 (!cold-init-forms (setq *unparse-fun-type-simplify* nil))
198 (!define-type-method (function :unparse) (type)
199 (if *unparse-fun-type-simplify*
202 (if (fun-type-wild-args type)
204 (unparse-args-types type))
206 (fun-type-returns type)))))
208 ;;; Since all function types are equivalent to FUNCTION, they are all
209 ;;; subtypes of each other.
210 (!define-type-method (function :simple-subtypep) (type1 type2)
211 (declare (ignore type1 type2))
214 (!define-superclasses function ((function)) !cold-init-forms)
216 ;;; The union or intersection of two FUNCTION types is FUNCTION.
217 (!define-type-method (function :simple-union2) (type1 type2)
218 (declare (ignore type1 type2))
219 (specifier-type 'function))
220 (!define-type-method (function :simple-intersection2) (type1 type2)
221 (declare (ignore type1 type2))
222 (specifier-type 'function))
224 ;;; ### Not very real, but good enough for redefining transforms
225 ;;; according to type:
226 (!define-type-method (function :simple-=) (type1 type2)
227 (values (equalp type1 type2) t))
229 (!define-type-class constant :inherits values)
231 (!define-type-method (constant :unparse) (type)
232 `(constant-argument ,(type-specifier (constant-type-type type))))
234 (!define-type-method (constant :simple-=) (type1 type2)
235 (type= (constant-type-type type1) (constant-type-type type2)))
237 (!def-type-translator constant-argument (type)
238 (make-constant-type :type (specifier-type type)))
240 ;;; Given a LAMBDA-LIST-like values type specification and an ARGS-TYPE
241 ;;; structure, fill in the slots in the structure accordingly. This is
242 ;;; used for both FUNCTION and VALUES types.
243 (declaim (ftype (function (list args-type) (values)) parse-args-types))
244 (defun parse-args-types (lambda-list result)
245 (multiple-value-bind (required optional restp rest keyp keys allowp aux)
246 (parse-lambda-list lambda-list)
248 (error "&AUX in a FUNCTION or VALUES type: ~S." lambda-list))
249 (setf (args-type-required result) (mapcar #'specifier-type required))
250 (setf (args-type-optional result) (mapcar #'specifier-type optional))
251 (setf (args-type-rest result) (if restp (specifier-type rest) nil))
252 (setf (args-type-keyp result) keyp)
253 (collect ((key-info))
255 (unless (proper-list-of-length-p key 2)
256 (error "Keyword type description is not a two-list: ~S." key))
257 (let ((kwd (first key)))
258 (when (find kwd (key-info) :key #'key-info-name)
259 (error "~@<repeated keyword ~S in lambda list: ~2I~_~S~:>"
261 (key-info (make-key-info :name kwd
262 :type (specifier-type (second key))))))
263 (setf (args-type-keywords result) (key-info)))
264 (setf (args-type-allowp result) allowp)
267 ;;; Return the lambda-list-like type specification corresponding
269 (declaim (ftype (function (args-type) list) unparse-args-types))
270 (defun unparse-args-types (type)
273 (dolist (arg (args-type-required type))
274 (result (type-specifier arg)))
276 (when (args-type-optional type)
278 (dolist (arg (args-type-optional type))
279 (result (type-specifier arg))))
281 (when (args-type-rest type)
283 (result (type-specifier (args-type-rest type))))
285 (when (args-type-keyp type)
287 (dolist (key (args-type-keywords type))
288 (result (list (key-info-name key)
289 (type-specifier (key-info-type key))))))
291 (when (args-type-allowp type)
292 (result '&allow-other-keys))
296 (!def-type-translator function (&optional (args '*) (result '*))
297 (let ((res (make-fun-type :returns (values-specifier-type result))))
299 (setf (fun-type-wild-args res) t)
300 (parse-args-types args res))
303 (!def-type-translator values (&rest values)
304 (let ((res (make-values-type)))
305 (parse-args-types values res)
308 ;;;; VALUES types interfaces
310 ;;;; We provide a few special operations that can be meaningfully used
311 ;;;; on VALUES types (as well as on any other type).
313 ;;; Return the type of the first value indicated by TYPE. This is used
314 ;;; by people who don't want to have to deal with VALUES types.
315 #!-sb-fluid (declaim (freeze-type values-type))
316 ; (inline single-value-type))
317 (defun single-value-type (type)
318 (declare (type ctype type))
319 (cond ((values-type-p type)
320 (or (car (args-type-required type))
321 (if (args-type-optional type)
322 (type-union (car (args-type-optional type))
323 (specifier-type 'null)))
324 (args-type-rest type)
325 (specifier-type 'null)))
326 ((eq type *wild-type*)
331 ;;; Return the minimum number of arguments that a function can be
332 ;;; called with, and the maximum number or NIL. If not a function
333 ;;; type, return NIL, NIL.
334 (defun fun-type-nargs (type)
335 (declare (type ctype type))
336 (if (fun-type-p type)
337 (let ((fixed (length (args-type-required type))))
338 (if (or (args-type-rest type)
339 (args-type-keyp type)
340 (args-type-allowp type))
342 (values fixed (+ fixed (length (args-type-optional type))))))
345 ;;; Determine whether TYPE corresponds to a definite number of values.
346 ;;; The first value is a list of the types for each value, and the
347 ;;; second value is the number of values. If the number of values is
348 ;;; not fixed, then return NIL and :UNKNOWN.
349 (defun values-types (type)
350 (declare (type ctype type))
351 (cond ((eq type *wild-type*)
352 (values nil :unknown))
353 ((not (values-type-p type))
354 (values (list type) 1))
355 ((or (args-type-optional type)
356 (args-type-rest type)
357 (args-type-keyp type)
358 (args-type-allowp type))
359 (values nil :unknown))
361 (let ((req (args-type-required type)))
362 (values (mapcar #'single-value-type req) (length req))))))
364 ;;; Return two values:
365 ;;; 1. A list of all the positional (fixed and optional) types.
366 ;;; 2. The &REST type (if any). If keywords allowed, *UNIVERSAL-TYPE*.
367 ;;; If no keywords or &REST, then the DEFAULT-TYPE.
368 (defun values-type-types (type &optional (default-type *empty-type*))
369 (declare (type values-type type))
370 (values (append (args-type-required type)
371 (args-type-optional type))
372 (cond ((args-type-keyp type) *universal-type*)
373 ((args-type-rest type))
377 ;;; Return a list of OPERATION applied to the types in TYPES1 and
378 ;;; TYPES2, padding with REST2 as needed. TYPES1 must not be shorter
379 ;;; than TYPES2. The second value is T if OPERATION always returned a
380 ;;; true second value.
381 (defun fixed-values-op (types1 types2 rest2 operation)
382 (declare (list types1 types2) (type ctype rest2) (type function operation))
384 (values (mapcar #'(lambda (t1 t2)
385 (multiple-value-bind (res win)
386 (funcall operation t1 t2)
392 (make-list (- (length types1) (length types2))
393 :initial-element rest2)))
396 ;;; If Type isn't a values type, then make it into one:
397 ;;; <type> ==> (values type &rest t)
398 (defun coerce-to-values (type)
399 (declare (type ctype type))
400 (if (values-type-p type)
402 (make-values-type :required (list type) :rest *universal-type*)))
404 ;;; Do the specified OPERATION on TYPE1 and TYPE2, which may be any
405 ;;; type, including VALUES types. With VALUES types such as:
408 ;;; we compute the more useful result
409 ;;; (VALUES (<operation> a0 b0) (<operation> a1 b1))
410 ;;; rather than the precise result
411 ;;; (<operation> (values a0 a1) (values b0 b1))
412 ;;; This has the virtue of always keeping the VALUES type specifier
413 ;;; outermost, and retains all of the information that is really
414 ;;; useful for static type analysis. We want to know what is always
415 ;;; true of each value independently. It is worthless to know that if
416 ;;; the first value is B0 then the second will be B1.
418 ;;; If the VALUES count signatures differ, then we produce a result with
419 ;;; the required VALUE count chosen by NREQ when applied to the number
420 ;;; of required values in TYPE1 and TYPE2. Any &KEY values become
421 ;;; &REST T (anyone who uses keyword values deserves to lose.)
423 ;;; The second value is true if the result is definitely empty or if
424 ;;; OPERATION returned true as its second value each time we called
425 ;;; it. Since we approximate the intersection of VALUES types, the
426 ;;; second value being true doesn't mean the result is exact.
427 (defun args-type-op (type1 type2 operation nreq default-type)
428 (declare (type ctype type1 type2 default-type)
429 (type function operation nreq))
430 (if (or (values-type-p type1) (values-type-p type2))
431 (let ((type1 (coerce-to-values type1))
432 (type2 (coerce-to-values type2)))
433 (multiple-value-bind (types1 rest1)
434 (values-type-types type1 default-type)
435 (multiple-value-bind (types2 rest2)
436 (values-type-types type2 default-type)
437 (multiple-value-bind (rest rest-exact)
438 (funcall operation rest1 rest2)
439 (multiple-value-bind (res res-exact)
440 (if (< (length types1) (length types2))
441 (fixed-values-op types2 types1 rest1 operation)
442 (fixed-values-op types1 types2 rest2 operation))
443 (let* ((req (funcall nreq
444 (length (args-type-required type1))
445 (length (args-type-required type2))))
446 (required (subseq res 0 req))
447 (opt (subseq res req))
448 (opt-last (position rest opt :test-not #'type=
450 (if (find *empty-type* required :test #'type=)
451 (values *empty-type* t)
452 (values (make-values-type
454 :optional (if opt-last
455 (subseq opt 0 (1+ opt-last))
457 :rest (if (eq rest default-type) nil rest))
458 (and rest-exact res-exact)))))))))
459 (funcall operation type1 type2)))
461 ;;; Do a union or intersection operation on types that might be values
462 ;;; types. The result is optimized for utility rather than exactness,
463 ;;; but it is guaranteed that it will be no smaller (more restrictive)
464 ;;; than the precise result.
466 ;;; The return convention seems to be analogous to
467 ;;; TYPES-EQUAL-OR-INTERSECT. -- WHN 19990910.
468 (defun-cached (values-type-union :hash-function type-cache-hash
471 :init-wrapper !cold-init-forms)
472 ((type1 eq) (type2 eq))
473 (declare (type ctype type1 type2))
474 (cond ((or (eq type1 *wild-type*) (eq type2 *wild-type*)) *wild-type*)
475 ((eq type1 *empty-type*) type2)
476 ((eq type2 *empty-type*) type1)
478 (values (args-type-op type1 type2 #'type-union #'min *empty-type*)))))
479 (defun-cached (values-type-intersection :hash-function type-cache-hash
482 :default (values nil :empty)
483 :init-wrapper !cold-init-forms)
484 ((type1 eq) (type2 eq))
485 (declare (type ctype type1 type2))
486 (cond ((eq type1 *wild-type*) (values type2 t))
487 ((eq type2 *wild-type*) (values type1 t))
489 (args-type-op type1 type2
492 (specifier-type 'null)))))
494 ;;; This is like TYPES-EQUAL-OR-INTERSECT, except that it sort of
495 ;;; works on VALUES types. Note that due to the semantics of
496 ;;; VALUES-TYPE-INTERSECTION, this might return (VALUES T T) when
497 ;;; there isn't really any intersection.
498 (defun values-types-equal-or-intersect (type1 type2)
499 (cond ((or (eq type1 *empty-type*) (eq type2 *empty-type*))
501 ((or (values-type-p type1) (values-type-p type2))
502 (multiple-value-bind (res win) (values-type-intersection type1 type2)
503 (values (not (eq res *empty-type*))
506 (types-equal-or-intersect type1 type2))))
508 ;;; a SUBTYPEP-like operation that can be used on any types, including
510 (defun-cached (values-subtypep :hash-function type-cache-hash
513 :default (values nil :empty)
514 :init-wrapper !cold-init-forms)
515 ((type1 eq) (type2 eq))
516 (declare (type ctype type1 type2))
517 (cond ((eq type2 *wild-type*) (values t t))
518 ((eq type1 *wild-type*)
519 (values (eq type2 *universal-type*) t))
520 ((not (values-types-equal-or-intersect type1 type2))
523 (if (or (values-type-p type1) (values-type-p type2))
524 (let ((type1 (coerce-to-values type1))
525 (type2 (coerce-to-values type2)))
526 (multiple-value-bind (types1 rest1) (values-type-types type1)
527 (multiple-value-bind (types2 rest2) (values-type-types type2)
528 (cond ((< (length (values-type-required type1))
529 (length (values-type-required type2)))
531 ((< (length types1) (length types2))
533 ((or (values-type-keyp type1)
534 (values-type-keyp type2))
537 (do ((t1 types1 (rest t1))
538 (t2 types2 (rest t2)))
540 (csubtypep rest1 rest2))
541 (multiple-value-bind (res win-p)
542 (csubtypep (first t1) (first t2))
544 (return (values nil nil)))
546 (return (values nil t))))))))))
547 (csubtypep type1 type2)))))
549 ;;;; type method interfaces
551 ;;; like SUBTYPEP, only works on CTYPE structures
552 (defun-cached (csubtypep :hash-function type-cache-hash
555 :default (values nil :empty)
556 :init-wrapper !cold-init-forms)
557 ((type1 eq) (type2 eq))
558 (declare (type ctype type1 type2))
559 (cond ((or (eq type1 type2)
560 (eq type1 *empty-type*)
561 (eq type2 *wild-type*))
563 ((or (eq type1 *wild-type*)
564 (eq type2 *empty-type*))
567 (!invoke-type-method :simple-subtypep :complex-subtypep-arg2
569 :complex-arg1 :complex-subtypep-arg1))))
571 ;;; Just parse the type specifiers and call CSUBTYPE.
572 (defun sb!xc:subtypep (type1 type2)
574 "Return two values indicating the relationship between type1 and type2.
575 If values are T and T, type1 definitely is a subtype of type2.
576 If values are NIL and T, type1 definitely is not a subtype of type2.
577 If values are NIL and NIL, it couldn't be determined."
578 (csubtypep (specifier-type type1) (specifier-type type2)))
580 ;;; If two types are definitely equivalent, return true. The second
581 ;;; value indicates whether the first value is definitely correct.
582 ;;; This should only fail in the presence of HAIRY types.
583 (defun-cached (type= :hash-function type-cache-hash
586 :default (values nil :empty)
587 :init-wrapper !cold-init-forms)
588 ((type1 eq) (type2 eq))
589 (declare (type ctype type1 type2))
592 (!invoke-type-method :simple-= :complex-= type1 type2)))
594 ;;; Not exactly the negation of TYPE=, since when the relationship is
595 ;;; uncertain, we still return NIL, NIL. This is useful in cases where
596 ;;; the conservative assumption is =.
597 (defun type/= (type1 type2)
598 (declare (type ctype type1 type2))
599 (multiple-value-bind (res win) (type= type1 type2)
604 ;;; the type method dispatch case of TYPE-UNION2
605 (defun %type-union2 (type1 type2)
606 ;; As in %TYPE-INTERSECTION2, it seems to be a good idea to give
607 ;; both argument orders a chance at COMPLEX-INTERSECTION2. Unlike
608 ;; %TYPE-INTERSECTION2, though, I don't have a specific case which
609 ;; demonstrates this is actually necessary. Also unlike
610 ;; %TYPE-INTERSECTION2, there seems to be no need to distinguish
611 ;; between not finding a method and having a method return NIL.
613 (!invoke-type-method :simple-union2 :complex-union2
616 (declare (inline 1way))
617 (or (1way type1 type2)
618 (1way type2 type1))))
620 ;;; Find a type which includes both types. Any inexactness is
621 ;;; represented by the fuzzy element types; we return a single value
622 ;;; that is precise to the best of our knowledge. This result is
623 ;;; simplified into the canonical form, thus is not a UNION-TYPE
624 ;;; unless we find no other way to represent the result.
625 (defun-cached (type-union2 :hash-function type-cache-hash
627 :init-wrapper !cold-init-forms)
628 ((type1 eq) (type2 eq))
629 ;; KLUDGE: This was generated from TYPE-INTERSECTION2 by Ye Olde Cut And
630 ;; Paste technique of programming. If it stays around (as opposed to
631 ;; e.g. fading away in favor of some CLOS solution) the shared logic
632 ;; should probably become shared code. -- WHN 2001-03-16
633 (declare (type ctype type1 type2))
634 (cond ((eq type1 type2)
636 ((or (union-type-p type1)
637 (union-type-p type2))
638 ;; Unions of UNION-TYPE should have the UNION-TYPE-TYPES
639 ;; values broken out and united separately. The full TYPE-UNION
640 ;; function knows how to do this, so let it handle it.
641 (type-union type1 type2))
643 ;; the ordinary case: we dispatch to type methods
644 (%type-union2 type1 type2))))
646 ;;; the type method dispatch case of TYPE-INTERSECTION2
647 (defun %type-intersection2 (type1 type2)
648 ;; We want to give both argument orders a chance at
649 ;; COMPLEX-INTERSECTION2. Without that, the old CMU CL type
650 ;; methods could give noncommutative results, e.g.
651 ;; (TYPE-INTERSECTION2 *EMPTY-TYPE* SOME-HAIRY-TYPE)
653 ;; (TYPE-INTERSECTION2 SOME-HAIRY-TYPE *EMPTY-TYPE*)
654 ;; => #<NAMED-TYPE NIL>, T
655 ;; We also need to distinguish between the case where we found a
656 ;; type method, and it returned NIL, and the case where we fell
657 ;; through without finding any type method. An example of the first
658 ;; case is the intersection of a HAIRY-TYPE with some ordinary type.
659 ;; An example of the second case is the intersection of two
660 ;; completely-unrelated types, e.g. CONS and NUMBER, or SYMBOL and
663 ;; (Why yes, CLOS probably *would* be nicer..)
665 (!invoke-type-method :simple-intersection2 :complex-intersection2
667 :default :no-type-method-found)))
668 (declare (inline 1way))
669 (let ((xy (1way type1 type2)))
670 (or (and (not (eql xy :no-type-method-found)) xy)
671 (let ((yx (1way type2 type1)))
672 (or (and (not (eql yx :no-type-method-found)) yx)
673 (cond ((and (eql xy :no-type-method-found)
674 (eql yx :no-type-method-found))
677 (aver (and (not xy) (not yx))) ; else handled above
680 (defun-cached (type-intersection2 :hash-function type-cache-hash
684 :init-wrapper !cold-init-forms)
685 ((type1 eq) (type2 eq))
686 (declare (type ctype type1 type2))
687 (cond ((eq type1 type2)
689 ((or (intersection-type-p type1)
690 (intersection-type-p type2))
691 ;; Intersections of INTERSECTION-TYPE should have the
692 ;; INTERSECTION-TYPE-TYPES values broken out and intersected
693 ;; separately. The full TYPE-INTERSECTION function knows how
694 ;; to do that, so let it handle it.
695 (type-intersection type1 type2))
697 ;; the ordinary case: we dispatch to type methods
698 (%type-intersection2 type1 type2))))
700 ;;; Return as restrictive and simple a type as we can discover that is
701 ;;; no more restrictive than the intersection of TYPE1 and TYPE2. At
702 ;;; worst, we arbitrarily return one of the arguments as the first
703 ;;; value (trying not to return a hairy type).
704 (defun type-approx-intersection2 (type1 type2)
705 (cond ((type-intersection2 type1 type2))
706 ((hairy-type-p type1) type2)
709 ;;; a test useful for checking whether a derived type matches a
712 ;;; The first value is true unless the types don't intersect and
713 ;;; aren't equal. The second value is true if the first value is
714 ;;; definitely correct. NIL is considered to intersect with any type.
715 ;;; If T is a subtype of either type, then we also return T, T. This
716 ;;; way we recognize that hairy types might intersect with T.
717 (defun types-equal-or-intersect (type1 type2)
718 (declare (type ctype type1 type2))
719 (if (or (eq type1 *empty-type*) (eq type2 *empty-type*))
721 (let ((intersection2 (type-intersection2 type1 type2)))
722 (cond ((not intersection2)
723 (if (or (csubtypep *universal-type* type1)
724 (csubtypep *universal-type* type2))
727 ((eq intersection2 *empty-type*) (values nil t))
730 ;;; Return a Common Lisp type specifier corresponding to the TYPE
732 (defun type-specifier (type)
733 (declare (type ctype type))
734 (funcall (type-class-unparse (type-class-info type)) type))
736 ;;; (VALUES-SPECIFIER-TYPE and SPECIFIER-TYPE moved from here to
737 ;;; early-type.lisp by WHN ca. 19990201.)
739 ;;; Take a list of type specifiers, computing the translation of each
740 ;;; specifier and defining it as a builtin type.
741 (declaim (ftype (function (list) (values)) precompute-types))
742 (defun precompute-types (specs)
744 (let ((res (specifier-type spec)))
745 (unless (unknown-type-p res)
746 (setf (info :type :builtin spec) res)
747 (setf (info :type :kind spec) :primitive))))
750 ;;;; general TYPE-UNION and TYPE-INTERSECTION operations
752 ;;;; These are fully general operations on CTYPEs: they'll always
753 ;;;; return a CTYPE representing the result.
755 ;;; shared logic for unions and intersections: Stuff TYPE into the
756 ;;; vector TYPES, finding pairs of types which can be simplified by
757 ;;; SIMPLIFY2 (TYPE-UNION2 or TYPE-INTERSECTION2) and replacing them
758 ;;; by their simplified forms.
759 (defun accumulate1-compound-type (type types %compound-type-p simplify2)
760 (declare (type ctype type))
761 (declare (type (vector ctype) types))
762 (declare (type function simplify2))
763 ;; Any input object satisfying %COMPOUND-TYPE-P should've been
764 ;; broken into components before it reached us.
765 (aver (not (funcall %compound-type-p type)))
766 (dotimes (i (length types) (vector-push-extend type types))
767 (let ((simplified2 (funcall simplify2 type (aref types i))))
769 ;; Discard the old (AREF TYPES I).
770 (setf (aref types i) (vector-pop types))
771 ;; Merge the new SIMPLIFIED2 into TYPES, by tail recursing.
772 ;; (Note that the tail recursion is indirect: we go through
773 ;; ACCUMULATE, not ACCUMULATE1, so that if SIMPLIFIED2 is
774 ;; handled properly if it satisfies %COMPOUND-TYPE-P.)
775 (return (accumulate-compound-type simplified2
782 ;;; shared logic for unions and intersections: Use
783 ;;; ACCUMULATE1-COMPOUND-TYPE to merge TYPE into TYPES, either
784 ;;; all in one step or, if %COMPOUND-TYPE-P is satisfied,
785 ;;; component by component.
786 (defun accumulate-compound-type (type types %compound-type-p simplify2)
787 (declare (type function %compound-type-p simplify2))
788 (flet ((accumulate1 (x)
789 (accumulate1-compound-type x types %compound-type-p simplify2)))
790 (declare (inline accumulate1))
791 (if (funcall %compound-type-p type)
792 (map nil #'accumulate1 (compound-type-types type))
796 ;;; shared logic for unions and intersections: Return a vector of
797 ;;; types representing the same types as INPUT-TYPES, but with
798 ;;; COMPOUND-TYPEs satisfying %COMPOUND-TYPE-P broken up into their
799 ;;; component types, and with any SIMPLY2 simplifications applied.
800 (defun simplified-compound-types (input-types %compound-type-p simplify2)
801 (let ((simplified-types (make-array (length input-types)
804 ;; (This INITIAL-ELEMENT shouldn't
805 ;; matter, but helps avoid type
806 ;; warnings at compile time.)
807 :initial-element *empty-type*)))
808 (dolist (input-type input-types)
809 (accumulate-compound-type input-type
815 ;;; shared logic for unions and intersections: Make a COMPOUND-TYPE
816 ;;; object whose components are the types in TYPES, or skip to special
817 ;;; cases when TYPES is short.
818 (defun make-compound-type-or-something (constructor types enumerable identity)
819 (declare (type function constructor))
820 (declare (type (vector ctype) types))
821 (declare (type ctype identity))
825 (t (funcall constructor
827 ;; FIXME: This should be just (COERCE TYPES 'LIST), but as
828 ;; of sbcl-0.6.11.17 the COERCE optimizer is really
829 ;; brain-dead, so that would generate a full call to
830 ;; SPECIFIER-TYPE at runtime, so we get into bootstrap
831 ;; problems in cold init because 'LIST is a compound
832 ;; type, so we need to MAKE-COMPOUND-TYPE-OR-SOMETHING
833 ;; before we know what 'LIST is. Once the COERCE
834 ;; optimizer is less brain-dead, we can make this
835 ;; (COERCE TYPES 'LIST) again.
836 #+sb-xc-host (coerce types 'list)
837 #-sb-xc-host (coerce-to-list types)))))
839 (defun type-intersection (&rest input-types)
840 (let ((simplified-types (simplified-compound-types input-types
841 #'intersection-type-p
842 #'type-intersection2)))
843 (declare (type (vector ctype) simplified-types))
844 ;; We want to have a canonical representation of types (or failing
845 ;; that, punt to HAIRY-TYPE). Canonical representation would have
846 ;; intersections inside unions but not vice versa, since you can
847 ;; always achieve that by the distributive rule. But we don't want
848 ;; to just apply the distributive rule, since it would be too easy
849 ;; to end up with unreasonably huge type expressions. So instead
850 ;; we punt to HAIRY-TYPE when this comes up.
851 (if (and (> (length simplified-types) 1)
852 (some #'union-type-p simplified-types))
854 :specifier `(and ,@(map 'list #'type-specifier simplified-types)))
855 (make-compound-type-or-something #'%make-intersection-type
857 (some #'type-enumerable
861 (defun type-union (&rest input-types)
862 (let ((simplified-types (simplified-compound-types input-types
865 (make-compound-type-or-something #'%make-union-type
867 (every #'type-enumerable simplified-types)
872 (!define-type-class named)
875 (defvar *empty-type*)
876 (defvar *universal-type*)
877 (defvar *universal-fun-type*)
879 (macrolet ((frob (name var)
881 (setq ,var (make-named-type :name ',name))
882 (setf (info :type :kind ',name) :primitive)
883 (setf (info :type :builtin ',name) ,var))))
884 ;; KLUDGE: In ANSI, * isn't really the name of a type, it's just a
885 ;; special symbol which can be stuck in some places where an
886 ;; ordinary type can go, e.g. (ARRAY * 1) instead of (ARRAY T 1).
887 ;; At some point, in order to become more standard, we should
888 ;; convert all the classic CMU CL legacy *s and *WILD-TYPE*s into
889 ;; Ts and *UNIVERSAL-TYPE*s.
891 (frob nil *empty-type*)
892 (frob t *universal-type*))
893 (setf *universal-fun-type*
894 (make-fun-type :wild-args t
895 :returns *wild-type*)))
897 (!define-type-method (named :simple-=) (type1 type2)
898 ;; FIXME: BUG 85: This assertion failed when I added it in
899 ;; sbcl-0.6.11.13. It probably shouldn't fail; but for now it's
900 ;; just commented out.
901 ;;(aver (not (eq type1 *wild-type*))) ; * isn't really a type.
902 (values (eq type1 type2) t))
904 (!define-type-method (named :simple-subtypep) (type1 type2)
905 (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
906 (values (or (eq type1 *empty-type*) (eq type2 *wild-type*)) t))
908 (!define-type-method (named :complex-subtypep-arg1) (type1 type2)
909 (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
910 ;; FIXME: Why does this (old CMU CL) assertion hold? Perhaps 'cause
911 ;; the HAIRY-TYPE COMPLEX-SUBTYPEP-ARG2 method takes precedence over
912 ;; this COMPLEX-SUBTYPE-ARG1 method? (I miss CLOS..)
913 (aver (not (hairy-type-p type2)))
914 ;; Besides the old CMU CL assertion above, we also need to avoid
915 ;; compound types, else we could get into trouble with
916 ;; (SUBTYPEP T '(OR (SATISFIES FOO) (SATISFIES BAR)))
918 ;; (SUBTYPEP T '(AND (SATISFIES FOO) (SATISFIES BAR))).
919 (aver (not (compound-type-p type2)))
920 ;; Then, since TYPE2 is reasonably tractable, we're good to go.
921 (values (eq type1 *empty-type*) t))
923 (!define-type-method (named :complex-subtypep-arg2) (type1 type2)
924 (aver (not (eq type2 *wild-type*))) ; * isn't really a type.
925 (cond ((eq type2 *universal-type*)
927 ((hairy-type-p type1)
930 ;; FIXME: This seems to rely on there only being 2 or 3
931 ;; HAIRY-TYPE values, and the exclusion of various
932 ;; possibilities above. It would be good to explain it and/or
933 ;; rewrite it so that it's clearer.
934 (values (not (eq type2 *empty-type*)) t))))
936 (!define-type-method (named :complex-intersection2) (type1 type2)
937 ;; FIXME: This assertion failed when I added it in sbcl-0.6.11.13.
938 ;; Perhaps when bug 85 is fixed it can be reenabled.
939 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
940 (hierarchical-intersection2 type1 type2))
942 (!define-type-method (named :complex-union2) (type1 type2)
943 ;; Perhaps when bug 85 is fixed this can be reenabled.
944 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
945 (hierarchical-union2 type1 type2))
947 (!define-type-method (named :unparse) (x)
950 ;;;; hairy and unknown types
952 (!define-type-method (hairy :unparse) (x) (hairy-type-specifier x))
954 (!define-type-method (hairy :simple-subtypep) (type1 type2)
955 (let ((hairy-spec1 (hairy-type-specifier type1))
956 (hairy-spec2 (hairy-type-specifier type2)))
957 (cond ((and (consp hairy-spec1) (eq (car hairy-spec1) 'not)
958 (consp hairy-spec2) (eq (car hairy-spec2) 'not))
959 (csubtypep (specifier-type (cadr hairy-spec2))
960 (specifier-type (cadr hairy-spec1))))
961 ((equal hairy-spec1 hairy-spec2)
966 (!define-type-method (hairy :complex-subtypep-arg2) (type1 type2)
967 (let ((hairy-spec (hairy-type-specifier type2)))
968 (cond ((and (consp hairy-spec) (eq (car hairy-spec) 'not))
969 (let* ((complement-type2 (specifier-type (cadr hairy-spec)))
970 (intersection2 (type-intersection2 type1
973 (values (eq intersection2 *empty-type*) t)
978 (!define-type-method (hairy :complex-subtypep-arg1 :complex-=) (type1 type2)
979 (declare (ignore type1 type2))
982 (!define-type-method (hairy :simple-intersection2 :complex-intersection2)
984 (declare (ignore type1 type2))
987 (!define-type-method (hairy :simple-=) (type1 type2)
988 (if (equal (hairy-type-specifier type1)
989 (hairy-type-specifier type2))
993 (!def-type-translator not (&whole whole type)
994 (declare (ignore type))
995 ;; Check legality of arguments.
996 (destructuring-bind (not typespec) whole
997 (declare (ignore not))
998 (specifier-type typespec)) ; must be legal typespec
1000 (make-hairy-type :specifier whole))
1002 (!def-type-translator satisfies (&whole whole fun)
1003 (declare (ignore fun))
1004 ;; Check legality of arguments.
1005 (destructuring-bind (satisfies predicate-name) whole
1006 (declare (ignore satisfies))
1007 (unless (symbolp predicate-name)
1008 (error 'simple-type-error
1009 :datum predicate-name
1010 :expected-type 'symbol
1011 :format-control "~S is not a symbol."
1012 :format-arguments (list predicate-name))))
1014 (make-hairy-type :specifier whole))
1018 (!define-type-class number)
1020 (!define-type-method (number :simple-=) (type1 type2)
1022 (and (eq (numeric-type-class type1) (numeric-type-class type2))
1023 (eq (numeric-type-format type1) (numeric-type-format type2))
1024 (eq (numeric-type-complexp type1) (numeric-type-complexp type2))
1025 (equal (numeric-type-low type1) (numeric-type-low type2))
1026 (equal (numeric-type-high type1) (numeric-type-high type2)))
1029 (!define-type-method (number :unparse) (type)
1030 (let* ((complexp (numeric-type-complexp type))
1031 (low (numeric-type-low type))
1032 (high (numeric-type-high type))
1033 (base (case (numeric-type-class type)
1035 (rational 'rational)
1036 (float (or (numeric-type-format type) 'float))
1039 (cond ((and (eq base 'integer) high low)
1040 (let ((high-count (logcount high))
1041 (high-length (integer-length high)))
1043 (cond ((= high 0) '(integer 0 0))
1045 ((and (= high-count high-length)
1046 (plusp high-length))
1047 `(unsigned-byte ,high-length))
1049 `(mod ,(1+ high)))))
1050 ((and (= low sb!vm:*target-most-negative-fixnum*)
1051 (= high sb!vm:*target-most-positive-fixnum*))
1053 ((and (= low (lognot high))
1054 (= high-count high-length)
1056 `(signed-byte ,(1+ high-length)))
1058 `(integer ,low ,high)))))
1059 (high `(,base ,(or low '*) ,high))
1061 (if (and (eq base 'integer) (= low 0))
1069 (if (eq base+bounds 'real)
1071 `(complex ,base+bounds)))
1073 (aver (eq base+bounds 'real))
1076 ;;; Return true if X is "less than or equal" to Y, taking open bounds
1077 ;;; into consideration. CLOSED is the predicate used to test the bound
1078 ;;; on a closed interval (e.g. <=), and OPEN is the predicate used on
1079 ;;; open bounds (e.g. <). Y is considered to be the outside bound, in
1080 ;;; the sense that if it is infinite (NIL), then the test succeeds,
1081 ;;; whereas if X is infinite, then the test fails (unless Y is also
1084 ;;; This is for comparing bounds of the same kind, e.g. upper and
1085 ;;; upper. Use NUMERIC-BOUND-TEST* for different kinds of bounds.
1086 #!-negative-zero-is-not-zero
1087 (defmacro numeric-bound-test (x y closed open)
1092 (,closed (car ,x) (car ,y))
1093 (,closed (car ,x) ,y)))
1099 #!+negative-zero-is-not-zero
1100 (defmacro numeric-bound-test-zero (op x y)
1101 `(if (and (zerop ,x) (zerop ,y) (floatp ,x) (floatp ,y))
1102 (,op (float-sign ,x) (float-sign ,y))
1105 #!+negative-zero-is-not-zero
1106 (defmacro numeric-bound-test (x y closed open)
1111 (numeric-bound-test-zero ,closed (car ,x) (car ,y))
1112 (numeric-bound-test-zero ,closed (car ,x) ,y)))
1115 (numeric-bound-test-zero ,open ,x (car ,y))
1116 (numeric-bound-test-zero ,closed ,x ,y)))))
1118 ;;; This is used to compare upper and lower bounds. This is different
1119 ;;; from the same-bound case:
1120 ;;; -- Since X = NIL is -infinity, whereas y = NIL is +infinity, we
1121 ;;; return true if *either* arg is NIL.
1122 ;;; -- an open inner bound is "greater" and also squeezes the interval,
1123 ;;; causing us to use the OPEN test for those cases as well.
1124 #!-negative-zero-is-not-zero
1125 (defmacro numeric-bound-test* (x y closed open)
1130 (,open (car ,x) (car ,y))
1131 (,open (car ,x) ,y)))
1137 #!+negative-zero-is-not-zero
1138 (defmacro numeric-bound-test* (x y closed open)
1143 (numeric-bound-test-zero ,open (car ,x) (car ,y))
1144 (numeric-bound-test-zero ,open (car ,x) ,y)))
1147 (numeric-bound-test-zero ,open ,x (car ,y))
1148 (numeric-bound-test-zero ,closed ,x ,y)))))
1150 ;;; Return whichever of the numeric bounds X and Y is "maximal"
1151 ;;; according to the predicates CLOSED (e.g. >=) and OPEN (e.g. >).
1152 ;;; This is only meaningful for maximizing like bounds, i.e. upper and
1153 ;;; upper. If MAX-P is true, then we return NIL if X or Y is NIL,
1154 ;;; otherwise we return the other arg.
1155 (defmacro numeric-bound-max (x y closed open max-p)
1158 `(cond ((not ,n-x) ,(if max-p nil n-y))
1159 ((not ,n-y) ,(if max-p nil n-x))
1162 (if (,closed (car ,n-x) (car ,n-y)) ,n-x ,n-y)
1163 (if (,open (car ,n-x) ,n-y) ,n-x ,n-y)))
1166 (if (,open (car ,n-y) ,n-x) ,n-y ,n-x)
1167 (if (,closed ,n-y ,n-x) ,n-y ,n-x))))))
1169 (!define-type-method (number :simple-subtypep) (type1 type2)
1170 (let ((class1 (numeric-type-class type1))
1171 (class2 (numeric-type-class type2))
1172 (complexp2 (numeric-type-complexp type2))
1173 (format2 (numeric-type-format type2))
1174 (low1 (numeric-type-low type1))
1175 (high1 (numeric-type-high type1))
1176 (low2 (numeric-type-low type2))
1177 (high2 (numeric-type-high type2)))
1178 ;; If one is complex and the other isn't, they are disjoint.
1179 (cond ((not (or (eq (numeric-type-complexp type1) complexp2)
1182 ;; If the classes are specified and different, the types are
1183 ;; disjoint unless type2 is rational and type1 is integer.
1184 ((not (or (eq class1 class2)
1186 (and (eq class1 'integer)
1187 (eq class2 'rational))))
1189 ;; If the float formats are specified and different, the types
1191 ((not (or (eq (numeric-type-format type1) format2)
1194 ;; Check the bounds.
1195 ((and (numeric-bound-test low1 low2 >= >)
1196 (numeric-bound-test high1 high2 <= <))
1201 (!define-superclasses number ((generic-number)) !cold-init-forms)
1203 ;;; If the high bound of LOW is adjacent to the low bound of HIGH,
1204 ;;; then return true, otherwise NIL.
1205 (defun numeric-types-adjacent (low high)
1206 (let ((low-bound (numeric-type-high low))
1207 (high-bound (numeric-type-low high)))
1208 (cond ((not (and low-bound high-bound)) nil)
1209 ((and (consp low-bound) (consp high-bound)) nil)
1211 #!-negative-zero-is-not-zero
1212 (let ((low-value (car low-bound)))
1213 (or (eql low-value high-bound)
1214 (and (eql low-value -0f0) (eql high-bound 0f0))
1215 (and (eql low-value 0f0) (eql high-bound -0f0))
1216 (and (eql low-value -0d0) (eql high-bound 0d0))
1217 (and (eql low-value 0d0) (eql high-bound -0d0))))
1218 #!+negative-zero-is-not-zero
1219 (eql (car low-bound) high-bound))
1221 #!-negative-zero-is-not-zero
1222 (let ((high-value (car high-bound)))
1223 (or (eql high-value low-bound)
1224 (and (eql high-value -0f0) (eql low-bound 0f0))
1225 (and (eql high-value 0f0) (eql low-bound -0f0))
1226 (and (eql high-value -0d0) (eql low-bound 0d0))
1227 (and (eql high-value 0d0) (eql low-bound -0d0))))
1228 #!+negative-zero-is-not-zero
1229 (eql (car high-bound) low-bound))
1230 #!+negative-zero-is-not-zero
1231 ((or (and (eql low-bound -0f0) (eql high-bound 0f0))
1232 (and (eql low-bound -0d0) (eql high-bound 0d0))))
1233 ((and (eq (numeric-type-class low) 'integer)
1234 (eq (numeric-type-class high) 'integer))
1235 (eql (1+ low-bound) high-bound))
1239 ;;; Return a numeric type that is a supertype for both TYPE1 and TYPE2.
1241 ;;; ### Note: we give up early to keep from dropping lots of information on
1242 ;;; the floor by returning overly general types.
1243 (!define-type-method (number :simple-union2) (type1 type2)
1244 (declare (type numeric-type type1 type2))
1245 (cond ((csubtypep type1 type2) type2)
1246 ((csubtypep type2 type1) type1)
1248 (let ((class1 (numeric-type-class type1))
1249 (format1 (numeric-type-format type1))
1250 (complexp1 (numeric-type-complexp type1))
1251 (class2 (numeric-type-class type2))
1252 (format2 (numeric-type-format type2))
1253 (complexp2 (numeric-type-complexp type2)))
1254 (when (and (eq class1 class2)
1255 (eq format1 format2)
1256 (eq complexp1 complexp2)
1257 (or (numeric-types-intersect type1 type2)
1258 (numeric-types-adjacent type1 type2)
1259 (numeric-types-adjacent type2 type1)))
1264 :low (numeric-bound-max (numeric-type-low type1)
1265 (numeric-type-low type2)
1267 :high (numeric-bound-max (numeric-type-high type1)
1268 (numeric-type-high type2)
1272 (setf (info :type :kind 'number) :primitive)
1273 (setf (info :type :builtin 'number)
1274 (make-numeric-type :complexp nil)))
1276 (!def-type-translator complex (&optional (typespec '*))
1277 (if (eq typespec '*)
1278 (make-numeric-type :complexp :complex)
1279 (labels ((not-numeric ()
1280 ;; FIXME: should probably be TYPE-ERROR
1281 (error "The component type for COMPLEX is not numeric: ~S"
1283 (complex1 (component-type)
1284 (unless (numeric-type-p component-type)
1285 ;; FIXME: As per the FIXME below, ANSI says we're
1286 ;; supposed to handle any subtype of REAL, not only
1287 ;; those which can be represented as NUMERIC-TYPE.
1289 (when (eq (numeric-type-complexp component-type) :complex)
1290 (error "The component type for COMPLEX is complex: ~S"
1292 (modified-numeric-type component-type :complexp :complex)))
1293 (let ((type (specifier-type typespec)))
1295 ;; This is all that CMU CL handled.
1296 (numeric-type (complex1 type))
1297 ;; We need to handle UNION-TYPEs in order to deal with
1298 ;; REAL and FLOAT being represented as UNION-TYPEs of more
1300 (union-type (apply #'type-union
1302 (union-type-types type))))
1303 ;; FIXME: ANSI just says that TYPESPEC is a subtype of type
1304 ;; REAL, not necessarily a NUMERIC-TYPE. E.g. TYPESPEC could
1305 ;; legally be (AND REAL (SATISFIES ODDP))! But like the old
1306 ;; CMU CL code, we're still not nearly that general.
1307 (t (not-numeric)))))))
1309 ;;; If X is *, return NIL, otherwise return the bound, which must be a
1310 ;;; member of TYPE or a one-element list of a member of TYPE.
1311 #!-sb-fluid (declaim (inline canonicalized-bound))
1312 (defun canonicalized-bound (bound type)
1313 (cond ((eq bound '*) nil)
1314 ((or (sb!xc:typep bound type)
1316 (sb!xc:typep (car bound) type)
1317 (null (cdr bound))))
1320 (error "Bound is not ~S, a ~S or a list of a ~S: ~S"
1326 (!def-type-translator integer (&optional (low '*) (high '*))
1327 (let* ((l (canonicalized-bound low 'integer))
1328 (lb (if (consp l) (1+ (car l)) l))
1329 (h (canonicalized-bound high 'integer))
1330 (hb (if (consp h) (1- (car h)) h)))
1331 (when (and hb lb (< hb lb))
1332 (error "Lower bound ~S is greater than upper bound ~S." l h))
1333 (make-numeric-type :class 'integer
1335 :enumerable (not (null (and l h)))
1339 (defmacro !def-bounded-type (type class format)
1340 `(!def-type-translator ,type (&optional (low '*) (high '*))
1341 (let ((lb (canonicalized-bound low ',type))
1342 (hb (canonicalized-bound high ',type)))
1343 (unless (numeric-bound-test* lb hb <= <)
1344 (error "Lower bound ~S is not less than upper bound ~S." low high))
1345 (make-numeric-type :class ',class :format ',format :low lb :high hb))))
1347 (!def-bounded-type rational rational nil)
1349 ;;; Unlike CMU CL, we represent the types FLOAT and REAL as
1350 ;;; UNION-TYPEs of more primitive types, in order to make
1351 ;;; type representation more unique, avoiding problems in the
1352 ;;; simplification of things like
1353 ;;; (subtypep '(or (single-float -1.0 1.0) (single-float 0.1))
1354 ;;; '(or (real -1 7) (single-float 0.1) (single-float -1.0 1.0)))
1355 ;;; When we allowed REAL to remain as a separate NUMERIC-TYPE,
1356 ;;; it was too easy for the first argument to be simplified to
1357 ;;; '(SINGLE-FLOAT -1.0), and for the second argument to be simplified
1358 ;;; to '(OR (REAL -1 7) (SINGLE-FLOAT 0.1)) and then for the
1359 ;;; SUBTYPEP to fail (returning NIL,T instead of T,T) because
1360 ;;; the first argument can't be seen to be a subtype of any of the
1361 ;;; terms in the second argument.
1363 ;;; The old CMU CL way was:
1364 ;;; (!def-bounded-type float float nil)
1365 ;;; (!def-bounded-type real nil nil)
1367 ;;; FIXME: If this new way works for a while with no weird new
1368 ;;; problems, we can go back and rip out support for separate FLOAT
1369 ;;; and REAL flavors of NUMERIC-TYPE. The new way was added in
1370 ;;; sbcl-0.6.11.22, 2001-03-21.
1372 ;;; FIXME: It's probably necessary to do something to fix the
1373 ;;; analogous problem with INTEGER and RATIONAL types. Perhaps
1374 ;;; bounded RATIONAL types should be represented as (OR RATIO INTEGER).
1375 (defun coerce-bound (bound type inner-coerce-bound-fun)
1376 (declare (type function inner-coerce-bound-fun))
1377 (cond ((eql bound '*)
1380 (destructuring-bind (inner-bound) bound
1381 (list (funcall inner-coerce-bound-fun inner-bound type))))
1383 (funcall inner-coerce-bound-fun bound type))))
1384 (defun inner-coerce-real-bound (bound type)
1386 (rational (rationalize bound))
1387 (float (if (floatp bound)
1389 ;; Coerce to the widest float format available, to
1390 ;; avoid unnecessary loss of precision:
1391 (coerce bound 'long-float)))))
1392 (defun coerced-real-bound (bound type)
1393 (coerce-bound bound type #'inner-coerce-real-bound))
1394 (defun coerced-float-bound (bound type)
1395 (coerce-bound bound type #'coerce))
1396 (!def-type-translator real (&optional (low '*) (high '*))
1397 (specifier-type `(or (float ,(coerced-real-bound low 'float)
1398 ,(coerced-real-bound high 'float))
1399 (rational ,(coerced-real-bound low 'rational)
1400 ,(coerced-real-bound high 'rational)))))
1401 (!def-type-translator float (&optional (low '*) (high '*))
1403 `(or (single-float ,(coerced-float-bound low 'single-float)
1404 ,(coerced-float-bound high 'single-float))
1405 (double-float ,(coerced-float-bound low 'double-float)
1406 ,(coerced-float-bound high 'double-float))
1407 #!+long-float ,(error "stub: no long float support yet"))))
1409 (defmacro !define-float-format (f)
1410 `(!def-bounded-type ,f float ,f))
1412 (!define-float-format short-float)
1413 (!define-float-format single-float)
1414 (!define-float-format double-float)
1415 (!define-float-format long-float)
1417 (defun numeric-types-intersect (type1 type2)
1418 (declare (type numeric-type type1 type2))
1419 (let* ((class1 (numeric-type-class type1))
1420 (class2 (numeric-type-class type2))
1421 (complexp1 (numeric-type-complexp type1))
1422 (complexp2 (numeric-type-complexp type2))
1423 (format1 (numeric-type-format type1))
1424 (format2 (numeric-type-format type2))
1425 (low1 (numeric-type-low type1))
1426 (high1 (numeric-type-high type1))
1427 (low2 (numeric-type-low type2))
1428 (high2 (numeric-type-high type2)))
1429 ;; If one is complex and the other isn't, then they are disjoint.
1430 (cond ((not (or (eq complexp1 complexp2)
1431 (null complexp1) (null complexp2)))
1433 ;; If either type is a float, then the other must either be
1434 ;; specified to be a float or unspecified. Otherwise, they
1436 ((and (eq class1 'float)
1437 (not (member class2 '(float nil)))) nil)
1438 ((and (eq class2 'float)
1439 (not (member class1 '(float nil)))) nil)
1440 ;; If the float formats are specified and different, the
1441 ;; types are disjoint.
1442 ((not (or (eq format1 format2) (null format1) (null format2)))
1445 ;; Check the bounds. This is a bit odd because we must
1446 ;; always have the outer bound of the interval as the
1448 (if (numeric-bound-test high1 high2 <= <)
1449 (or (and (numeric-bound-test low1 low2 >= >)
1450 (numeric-bound-test* low1 high2 <= <))
1451 (and (numeric-bound-test low2 low1 >= >)
1452 (numeric-bound-test* low2 high1 <= <)))
1453 (or (and (numeric-bound-test* low2 high1 <= <)
1454 (numeric-bound-test low2 low1 >= >))
1455 (and (numeric-bound-test high2 high1 <= <)
1456 (numeric-bound-test* high2 low1 >= >))))))))
1458 ;;; Take the numeric bound X and convert it into something that can be
1459 ;;; used as a bound in a numeric type with the specified CLASS and
1460 ;;; FORMAT. If UP-P is true, then we round up as needed, otherwise we
1461 ;;; round down. UP-P true implies that X is a lower bound, i.e. (N) > N.
1463 ;;; This is used by NUMERIC-TYPE-INTERSECTION to mash the bound into
1464 ;;; the appropriate type number. X may only be a float when CLASS is
1467 ;;; ### Note: it is possible for the coercion to a float to overflow
1468 ;;; or underflow. This happens when the bound doesn't fit in the
1469 ;;; specified format. In this case, we should really return the
1470 ;;; appropriate {Most | Least}-{Positive | Negative}-XXX-Float float
1471 ;;; of desired format. But these conditions aren't currently signalled
1472 ;;; in any useful way.
1474 ;;; Also, when converting an open rational bound into a float we
1475 ;;; should probably convert it to a closed bound of the closest float
1476 ;;; in the specified format. KLUDGE: In general, open float bounds are
1477 ;;; screwed up. -- (comment from original CMU CL)
1478 (defun round-numeric-bound (x class format up-p)
1480 (let ((cx (if (consp x) (car x) x)))
1484 (if (and (consp x) (integerp cx))
1485 (if up-p (1+ cx) (1- cx))
1486 (if up-p (ceiling cx) (floor cx))))
1488 (let ((res (if format (coerce cx format) (float cx))))
1489 (if (consp x) (list res) res)))))
1492 ;;; Handle the case of type intersection on two numeric types. We use
1493 ;;; TYPES-EQUAL-OR-INTERSECT to throw out the case of types with no
1494 ;;; intersection. If an attribute in TYPE1 is unspecified, then we use
1495 ;;; TYPE2's attribute, which must be at least as restrictive. If the
1496 ;;; types intersect, then the only attributes that can be specified
1497 ;;; and different are the class and the bounds.
1499 ;;; When the class differs, we use the more restrictive class. The
1500 ;;; only interesting case is RATIONAL/INTEGER, since RATIONAL includes
1503 ;;; We make the result lower (upper) bound the maximum (minimum) of
1504 ;;; the argument lower (upper) bounds. We convert the bounds into the
1505 ;;; appropriate numeric type before maximizing. This avoids possible
1506 ;;; confusion due to mixed-type comparisons (but I think the result is
1508 (!define-type-method (number :simple-intersection2) (type1 type2)
1509 (declare (type numeric-type type1 type2))
1510 (if (numeric-types-intersect type1 type2)
1511 (let* ((class1 (numeric-type-class type1))
1512 (class2 (numeric-type-class type2))
1513 (class (ecase class1
1515 ((integer float) class1)
1516 (rational (if (eq class2 'integer)
1519 (format (or (numeric-type-format type1)
1520 (numeric-type-format type2))))
1524 :complexp (or (numeric-type-complexp type1)
1525 (numeric-type-complexp type2))
1526 :low (numeric-bound-max
1527 (round-numeric-bound (numeric-type-low type1)
1529 (round-numeric-bound (numeric-type-low type2)
1532 :high (numeric-bound-max
1533 (round-numeric-bound (numeric-type-high type1)
1535 (round-numeric-bound (numeric-type-high type2)
1540 ;;; Given two float formats, return the one with more precision. If
1541 ;;; either one is null, return NIL.
1542 (defun float-format-max (f1 f2)
1544 (dolist (f *float-formats* (error "bad float format: ~S" f1))
1545 (when (or (eq f f1) (eq f f2))
1548 ;;; Return the result of an operation on TYPE1 and TYPE2 according to
1549 ;;; the rules of numeric contagion. This is always NUMBER, some float
1550 ;;; format (possibly complex) or RATIONAL. Due to rational
1551 ;;; canonicalization, there isn't much we can do here with integers or
1552 ;;; rational complex numbers.
1554 ;;; If either argument is not a NUMERIC-TYPE, then return NUMBER. This
1555 ;;; is useful mainly for allowing types that are technically numbers,
1556 ;;; but not a NUMERIC-TYPE.
1557 (defun numeric-contagion (type1 type2)
1558 (if (and (numeric-type-p type1) (numeric-type-p type2))
1559 (let ((class1 (numeric-type-class type1))
1560 (class2 (numeric-type-class type2))
1561 (format1 (numeric-type-format type1))
1562 (format2 (numeric-type-format type2))
1563 (complexp1 (numeric-type-complexp type1))
1564 (complexp2 (numeric-type-complexp type2)))
1565 (cond ((or (null complexp1)
1567 (specifier-type 'number))
1571 :format (ecase class2
1572 (float (float-format-max format1 format2))
1573 ((integer rational) format1)
1575 ;; A double-float with any real number is a
1578 (if (eq format1 'double-float)
1581 ;; A long-float with any real number is a
1584 (if (eq format1 'long-float)
1587 :complexp (if (or (eq complexp1 :complex)
1588 (eq complexp2 :complex))
1591 ((eq class2 'float) (numeric-contagion type2 type1))
1592 ((and (eq complexp1 :real) (eq complexp2 :real))
1594 :class (and class1 class2 'rational)
1597 (specifier-type 'number))))
1598 (specifier-type 'number)))
1602 (!define-type-class array)
1604 ;;; What this does depends on the setting of the
1605 ;;; *USE-IMPLEMENTATION-TYPES* switch. If true, return the specialized
1606 ;;; element type, otherwise return the original element type.
1607 (defun specialized-element-type-maybe (type)
1608 (declare (type array-type type))
1609 (if *use-implementation-types*
1610 (array-type-specialized-element-type type)
1611 (array-type-element-type type)))
1613 (!define-type-method (array :simple-=) (type1 type2)
1614 (values (and (equal (array-type-dimensions type1)
1615 (array-type-dimensions type2))
1616 (eq (array-type-complexp type1)
1617 (array-type-complexp type2))
1618 (type= (specialized-element-type-maybe type1)
1619 (specialized-element-type-maybe type2)))
1622 (!define-type-method (array :unparse) (type)
1623 (let ((dims (array-type-dimensions type))
1624 (eltype (type-specifier (array-type-element-type type)))
1625 (complexp (array-type-complexp type)))
1628 (if complexp 'array 'simple-array)
1629 (if complexp `(array ,eltype) `(simple-array ,eltype))))
1630 ((= (length dims) 1)
1632 (if (eq (car dims) '*)
1635 (base-char 'base-string)
1638 (t `(vector ,eltype)))
1640 (bit `(bit-vector ,(car dims)))
1641 (base-char `(base-string ,(car dims)))
1642 (character `(string ,(car dims)))
1643 (t `(vector ,eltype ,(car dims)))))
1644 (if (eq (car dims) '*)
1646 (bit 'simple-bit-vector)
1647 (base-char 'simple-base-string)
1648 (character 'simple-string)
1649 ((t) 'simple-vector)
1650 (t `(simple-array ,eltype (*))))
1652 (bit `(simple-bit-vector ,(car dims)))
1653 (base-char `(simple-base-string ,(car dims)))
1654 (character `(simple-string ,(car dims)))
1655 ((t) `(simple-vector ,(car dims)))
1656 (t `(simple-array ,eltype ,dims))))))
1659 `(array ,eltype ,dims)
1660 `(simple-array ,eltype ,dims))))))
1662 (!define-type-method (array :simple-subtypep) (type1 type2)
1663 (let ((dims1 (array-type-dimensions type1))
1664 (dims2 (array-type-dimensions type2))
1665 (complexp2 (array-type-complexp type2)))
1666 (cond (;; not subtypep unless dimensions are compatible
1667 (not (or (eq dims2 '*)
1668 (and (not (eq dims1 '*))
1669 ;; (sbcl-0.6.4 has trouble figuring out that
1670 ;; DIMS1 and DIMS2 must be lists at this
1671 ;; point, and knowing that is important to
1672 ;; compiling EVERY efficiently.)
1673 (= (length (the list dims1))
1674 (length (the list dims2)))
1675 (every (lambda (x y)
1676 (or (eq y '*) (eql x y)))
1678 (the list dims2)))))
1680 ;; not subtypep unless complexness is compatible
1681 ((not (or (eq complexp2 :maybe)
1682 (eq (array-type-complexp type1) complexp2)))
1684 ;; Since we didn't fail any of the tests above, we win
1685 ;; if the TYPE2 element type is wild.
1686 ((eq (array-type-element-type type2) *wild-type*)
1688 (;; Since we didn't match any of the special cases above, we
1689 ;; can't give a good answer unless both the element types
1690 ;; have been defined.
1691 (or (unknown-type-p (array-type-element-type type1))
1692 (unknown-type-p (array-type-element-type type2)))
1694 (;; Otherwise, the subtype relationship holds iff the
1695 ;; types are equal, and they're equal iff the specialized
1696 ;; element types are identical.
1698 (values (type= (specialized-element-type-maybe type1)
1699 (specialized-element-type-maybe type2))
1702 (!define-superclasses array
1708 (defun array-types-intersect (type1 type2)
1709 (declare (type array-type type1 type2))
1710 (let ((dims1 (array-type-dimensions type1))
1711 (dims2 (array-type-dimensions type2))
1712 (complexp1 (array-type-complexp type1))
1713 (complexp2 (array-type-complexp type2)))
1714 ;; See whether dimensions are compatible.
1715 (cond ((not (or (eq dims1 '*) (eq dims2 '*)
1716 (and (= (length dims1) (length dims2))
1717 (every #'(lambda (x y)
1718 (or (eq x '*) (eq y '*) (= x y)))
1721 ;; See whether complexpness is compatible.
1722 ((not (or (eq complexp1 :maybe)
1723 (eq complexp2 :maybe)
1724 (eq complexp1 complexp2)))
1726 ;; If either element type is wild, then they intersect.
1727 ;; Otherwise, the types must be identical.
1728 ((or (eq (array-type-element-type type1) *wild-type*)
1729 (eq (array-type-element-type type2) *wild-type*)
1730 (type= (specialized-element-type-maybe type1)
1731 (specialized-element-type-maybe type2)))
1737 (!define-type-method (array :simple-intersection2) (type1 type2)
1738 (declare (type array-type type1 type2))
1739 (if (array-types-intersect type1 type2)
1740 (let ((dims1 (array-type-dimensions type1))
1741 (dims2 (array-type-dimensions type2))
1742 (complexp1 (array-type-complexp type1))
1743 (complexp2 (array-type-complexp type2))
1744 (eltype1 (array-type-element-type type1))
1745 (eltype2 (array-type-element-type type2)))
1746 (specialize-array-type
1748 :dimensions (cond ((eq dims1 '*) dims2)
1749 ((eq dims2 '*) dims1)
1751 (mapcar (lambda (x y) (if (eq x '*) y x))
1753 :complexp (if (eq complexp1 :maybe) complexp2 complexp1)
1754 :element-type (if (eq eltype1 *wild-type*) eltype2 eltype1))))
1757 ;;; Check a supplied dimension list to determine whether it is legal,
1758 ;;; and return it in canonical form (as either '* or a list).
1759 (defun canonical-array-dimensions (dims)
1764 (error "Arrays can't have a negative number of dimensions: ~S" dims))
1765 (when (>= dims sb!xc:array-rank-limit)
1766 (error "array type with too many dimensions: ~S" dims))
1767 (make-list dims :initial-element '*))
1769 (when (>= (length dims) sb!xc:array-rank-limit)
1770 (error "array type with too many dimensions: ~S" dims))
1773 (unless (and (integerp dim)
1775 (< dim sb!xc:array-dimension-limit))
1776 (error "bad dimension in array type: ~S" dim))))
1779 (error "Array dimensions is not a list, integer or *:~% ~S" dims))))
1783 (!define-type-class member)
1785 (!define-type-method (member :unparse) (type)
1786 (let ((members (member-type-members type)))
1787 (if (equal members '(nil))
1789 `(member ,@members))))
1791 (!define-type-method (member :simple-subtypep) (type1 type2)
1792 (values (subsetp (member-type-members type1) (member-type-members type2))
1795 (!define-type-method (member :complex-subtypep-arg1) (type1 type2)
1796 (every/type (swapped-args-fun #'ctypep)
1798 (member-type-members type1)))
1800 ;;; We punt if the odd type is enumerable and intersects with the
1801 ;;; MEMBER type. If not enumerable, then it is definitely not a
1802 ;;; subtype of the MEMBER type.
1803 (!define-type-method (member :complex-subtypep-arg2) (type1 type2)
1804 (cond ((not (type-enumerable type1)) (values nil t))
1805 ((types-equal-or-intersect type1 type2) (values nil nil))
1806 (t (values nil t))))
1808 (!define-type-method (member :simple-intersection2) (type1 type2)
1809 (let ((mem1 (member-type-members type1))
1810 (mem2 (member-type-members type2)))
1811 (cond ((subsetp mem1 mem2) type1)
1812 ((subsetp mem2 mem1) type2)
1814 (let ((res (intersection mem1 mem2)))
1816 (make-member-type :members res)
1819 (!define-type-method (member :complex-intersection2) (type1 type2)
1821 (collect ((members))
1822 (let ((mem2 (member-type-members type2)))
1823 (dolist (member mem2)
1824 (multiple-value-bind (val win) (ctypep member type1)
1826 (return-from punt nil))
1827 (when val (members member))))
1828 (cond ((subsetp mem2 (members)) type2)
1829 ((null (members)) *empty-type*)
1831 (make-member-type :members (members))))))))
1833 ;;; We don't need a :COMPLEX-UNION2, since the only interesting case is
1834 ;;; a union type, and the member/union interaction is handled by the
1835 ;;; union type method.
1836 (!define-type-method (member :simple-union2) (type1 type2)
1837 (let ((mem1 (member-type-members type1))
1838 (mem2 (member-type-members type2)))
1839 (cond ((subsetp mem1 mem2) type2)
1840 ((subsetp mem2 mem1) type1)
1842 (make-member-type :members (union mem1 mem2))))))
1844 (!define-type-method (member :simple-=) (type1 type2)
1845 (let ((mem1 (member-type-members type1))
1846 (mem2 (member-type-members type2)))
1847 (values (and (subsetp mem1 mem2)
1848 (subsetp mem2 mem1))
1851 (!define-type-method (member :complex-=) (type1 type2)
1852 (if (type-enumerable type1)
1853 (multiple-value-bind (val win) (csubtypep type2 type1)
1854 (if (or val (not win))
1859 (!def-type-translator member (&rest members)
1861 (make-member-type :members (remove-duplicates members))
1864 ;;;; intersection types
1866 ;;;; Until version 0.6.10.6, SBCL followed the original CMU CL approach
1867 ;;;; of punting on all AND types, not just the unreasonably complicated
1868 ;;;; ones. The change was motivated by trying to get the KEYWORD type
1869 ;;;; to behave sensibly:
1870 ;;;; ;; reasonable definition
1871 ;;;; (DEFTYPE KEYWORD () '(AND SYMBOL (SATISFIES KEYWORDP)))
1872 ;;;; ;; reasonable behavior
1873 ;;;; (AVER (SUBTYPEP 'KEYWORD 'SYMBOL))
1874 ;;;; Without understanding a little about the semantics of AND, we'd
1875 ;;;; get (SUBTYPEP 'KEYWORD 'SYMBOL)=>NIL,NIL and, for entirely
1876 ;;;; parallel reasons, (SUBTYPEP 'RATIO 'NUMBER)=>NIL,NIL. That's
1879 ;;;; We still follow the example of CMU CL to some extent, by punting
1880 ;;;; (to the opaque HAIRY-TYPE) on sufficiently complicated types
1883 (!define-type-class intersection)
1885 ;;; A few intersection types have special names. The others just get
1886 ;;; mechanically unparsed.
1887 (!define-type-method (intersection :unparse) (type)
1888 (declare (type ctype type))
1889 (or (find type '(ratio bignum keyword) :key #'specifier-type :test #'type=)
1890 `(and ,@(mapcar #'type-specifier (intersection-type-types type)))))
1892 ;;; shared machinery for type equality: true if every type in the set
1893 ;;; TYPES1 matches a type in the set TYPES2 and vice versa
1894 (defun type=-set (types1 types2)
1895 (flet (;; true if every type in the set X matches a type in the set Y
1897 (declare (type list x y))
1898 (every (lambda (xelement)
1899 (position xelement y :test #'type=))
1901 (values (and (type<=-set types1 types2)
1902 (type<=-set types2 types1))
1905 ;;; Two intersection types are equal if their subtypes are equal sets.
1907 ;;; FIXME: Might it be better to use
1908 ;;; (AND (SUBTYPEP X Y) (SUBTYPEP Y X))
1909 ;;; instead, since SUBTYPEP is the usual relationship that we care
1910 ;;; most about, so it would be good to leverage any ingenuity there
1911 ;;; in this more obscure method?
1912 (!define-type-method (intersection :simple-=) (type1 type2)
1913 (type=-set (intersection-type-types type1)
1914 (intersection-type-types type2)))
1916 (defun %intersection-complex-subtypep-arg1 (type1 type2)
1917 (any/type (swapped-args-fun #'csubtypep)
1919 (intersection-type-types type1)))
1921 (!define-type-method (intersection :simple-subtypep) (type1 type2)
1922 (every/type #'%intersection-complex-subtypep-arg1
1924 (intersection-type-types type2)))
1926 (!define-type-method (intersection :complex-subtypep-arg1) (type1 type2)
1927 (%intersection-complex-subtypep-arg1 type1 type2))
1929 (!define-type-method (intersection :complex-subtypep-arg2) (type1 type2)
1930 (every/type #'csubtypep type1 (intersection-type-types type2)))
1932 (!def-type-translator and (&whole whole &rest type-specifiers)
1933 (apply #'type-intersection
1934 (mapcar #'specifier-type
1939 (!define-type-class union)
1941 ;;; The LIST type has a special name. Other union types just get
1942 ;;; mechanically unparsed.
1943 (!define-type-method (union :unparse) (type)
1944 (declare (type ctype type))
1945 (if (type= type (specifier-type 'list))
1947 `(or ,@(mapcar #'type-specifier (union-type-types type)))))
1949 ;;; Two union types are equal if their subtypes are equal sets.
1950 (!define-type-method (union :simple-=) (type1 type2)
1951 (type=-set (union-type-types type1)
1952 (union-type-types type2)))
1954 ;;; Similarly, a union type is a subtype of another if every element
1955 ;;; of TYPE1 is a subtype of some element of TYPE2.
1956 (!define-type-method (union :simple-subtypep) (type1 type2)
1957 (every/type (swapped-args-fun #'union-complex-subtypep-arg2)
1959 (union-type-types type1)))
1961 (defun union-complex-subtypep-arg1 (type1 type2)
1962 (every/type (swapped-args-fun #'csubtypep)
1964 (union-type-types type1)))
1965 (!define-type-method (union :complex-subtypep-arg1) (type1 type2)
1966 (union-complex-subtypep-arg1 type1 type2))
1968 (defun union-complex-subtypep-arg2 (type1 type2)
1969 (any/type #'csubtypep type1 (union-type-types type2)))
1970 (!define-type-method (union :complex-subtypep-arg2) (type1 type2)
1971 (union-complex-subtypep-arg2 type1 type2))
1973 (!define-type-method (union :simple-intersection2 :complex-intersection2)
1975 ;; The CSUBTYPEP clauses here let us simplify e.g.
1976 ;; (TYPE-INTERSECTION2 (SPECIFIER-TYPE 'LIST)
1977 ;; (SPECIFIER-TYPE '(OR LIST VECTOR)))
1978 ;; (where LIST is (OR CONS NULL)).
1980 ;; The tests are more or less (CSUBTYPEP TYPE1 TYPE2) and vice
1981 ;; versa, but it's important that we pre-expand them into
1982 ;; specialized operations on individual elements of
1983 ;; UNION-TYPE-TYPES, instead of using the ordinary call to
1984 ;; CSUBTYPEP, in order to avoid possibly invoking any methods which
1985 ;; might in turn invoke (TYPE-INTERSECTION2 TYPE1 TYPE2) and thus
1986 ;; cause infinite recursion.
1987 (cond ((union-complex-subtypep-arg2 type1 type2)
1989 ((union-complex-subtypep-arg1 type2 type1)
1992 ;; KLUDGE: This code accumulates a sequence of TYPE-UNION2
1993 ;; operations in a particular order, and gives up if any of
1994 ;; the sub-unions turn out not to be simple. In other cases
1995 ;; ca. sbcl-0.6.11.15, that approach to taking a union was a
1996 ;; bad idea, since it can overlook simplifications which
1997 ;; might occur if the terms were accumulated in a different
1998 ;; order. It's possible that that will be a problem here too.
1999 ;; However, I can't think of a good example to demonstrate
2000 ;; it, and without an example to demonstrate it I can't write
2001 ;; test cases, and without test cases I don't want to
2002 ;; complicate the code to address what's still a hypothetical
2003 ;; problem. So I punted. -- WHN 2001-03-20
2004 (let ((accumulator *empty-type*))
2005 (dolist (t2 (union-type-types type2) accumulator)
2007 (type-union2 accumulator
2008 (type-intersection type1 t2)))
2009 ;; When our result isn't simple any more (because
2010 ;; TYPE-UNION2 was unable to give us a simple result)
2014 (!def-type-translator or (&rest type-specifiers)
2016 (mapcar #'specifier-type
2021 (!define-type-class cons)
2023 (!def-type-translator cons (&optional (car-type-spec '*) (cdr-type-spec '*))
2024 (make-cons-type (specifier-type car-type-spec)
2025 (specifier-type cdr-type-spec)))
2027 (!define-type-method (cons :unparse) (type)
2028 (let ((car-eltype (type-specifier (cons-type-car-type type)))
2029 (cdr-eltype (type-specifier (cons-type-cdr-type type))))
2030 (if (and (member car-eltype '(t *))
2031 (member cdr-eltype '(t *)))
2033 `(cons ,car-eltype ,cdr-eltype))))
2035 (!define-type-method (cons :simple-=) (type1 type2)
2036 (declare (type cons-type type1 type2))
2037 (and (type= (cons-type-car-type type1) (cons-type-car-type type2))
2038 (type= (cons-type-cdr-type type1) (cons-type-cdr-type type2))))
2040 (!define-type-method (cons :simple-subtypep) (type1 type2)
2041 (declare (type cons-type type1 type2))
2042 (multiple-value-bind (val-car win-car)
2043 (csubtypep (cons-type-car-type type1) (cons-type-car-type type2))
2044 (multiple-value-bind (val-cdr win-cdr)
2045 (csubtypep (cons-type-cdr-type type1) (cons-type-cdr-type type2))
2046 (if (and val-car val-cdr)
2047 (values t (and win-car win-cdr))
2048 (values nil (or win-car win-cdr))))))
2050 ;;; Give up if a precise type is not possible, to avoid returning
2051 ;;; overly general types.
2052 (!define-type-method (cons :simple-union2) (type1 type2)
2053 (declare (type cons-type type1 type2))
2054 (let ((car-type1 (cons-type-car-type type1))
2055 (car-type2 (cons-type-car-type type2))
2056 (cdr-type1 (cons-type-cdr-type type1))
2057 (cdr-type2 (cons-type-cdr-type type2)))
2058 (cond ((type= car-type1 car-type2)
2059 (make-cons-type car-type1
2060 (type-union cdr-type1 cdr-type2)))
2061 ((type= cdr-type1 cdr-type2)
2062 (make-cons-type (type-union cdr-type1 cdr-type2)
2065 (!define-type-method (cons :simple-intersection2) (type1 type2)
2066 (declare (type cons-type type1 type2))
2069 (and (setf car-int2 (type-intersection2 (cons-type-car-type type1)
2070 (cons-type-car-type type2)))
2071 (setf cdr-int2 (type-intersection2 (cons-type-cdr-type type1)
2072 (cons-type-cdr-type type2)))
2073 (make-cons-type car-int2 cdr-int2))))
2075 ;;; Return the type that describes all objects that are in X but not
2076 ;;; in Y. If we can't determine this type, then return NIL.
2078 ;;; For now, we only are clever dealing with union and member types.
2079 ;;; If either type is not a union type, then we pretend that it is a
2080 ;;; union of just one type. What we do is remove from X all the types
2081 ;;; that are a subtype any type in Y. If any type in X intersects with
2082 ;;; a type in Y but is not a subtype, then we give up.
2084 ;;; We must also special-case any member type that appears in the
2085 ;;; union. We remove from X's members all objects that are TYPEP to Y.
2086 ;;; If Y has any members, we must be careful that none of those
2087 ;;; members are CTYPEP to any of Y's non-member types. We give up in
2088 ;;; this case, since to compute that difference we would have to break
2089 ;;; the type from X into some collection of types that represents the
2090 ;;; type without that particular element. This seems too hairy to be
2091 ;;; worthwhile, given its low utility.
2092 (defun type-difference (x y)
2093 (let ((x-types (if (union-type-p x) (union-type-types x) (list x)))
2094 (y-types (if (union-type-p y) (union-type-types y) (list y))))
2096 (dolist (x-type x-types)
2097 (if (member-type-p x-type)
2098 (collect ((members))
2099 (dolist (mem (member-type-members x-type))
2100 (multiple-value-bind (val win) (ctypep mem y)
2101 (unless win (return-from type-difference nil))
2105 (res (make-member-type :members (members)))))
2106 (dolist (y-type y-types (res x-type))
2107 (multiple-value-bind (val win) (csubtypep x-type y-type)
2108 (unless win (return-from type-difference nil))
2110 (when (types-equal-or-intersect x-type y-type)
2111 (return-from type-difference nil))))))
2112 (let ((y-mem (find-if #'member-type-p y-types)))
2114 (let ((members (member-type-members y-mem)))
2115 (dolist (x-type x-types)
2116 (unless (member-type-p x-type)
2117 (dolist (member members)
2118 (multiple-value-bind (val win) (ctypep member x-type)
2119 (when (or (not win) val)
2120 (return-from type-difference nil)))))))))
2121 (apply #'type-union (res)))))
2123 (!def-type-translator array (&optional (element-type '*)
2125 (specialize-array-type
2126 (make-array-type :dimensions (canonical-array-dimensions dimensions)
2127 :element-type (specifier-type element-type))))
2129 (!def-type-translator simple-array (&optional (element-type '*)
2131 (specialize-array-type
2132 (make-array-type :dimensions (canonical-array-dimensions dimensions)
2133 :element-type (specifier-type element-type)
2136 ;;;; utilities shared between cross-compiler and target system
2138 ;;; Does the type derived from compilation of an actual function
2139 ;;; definition satisfy declarations of a function's type?
2140 (defun defined-ftype-matches-declared-ftype-p (defined-ftype declared-ftype)
2141 (declare (type ctype defined-ftype declared-ftype))
2142 (flet ((is-built-in-class-function-p (ctype)
2143 (and (built-in-class-p ctype)
2144 (eq (built-in-class-%name ctype) 'function))))
2145 (cond (;; DECLARED-FTYPE could certainly be #<BUILT-IN-CLASS FUNCTION>;
2146 ;; that's what happens when we (DECLAIM (FTYPE FUNCTION FOO)).
2147 (is-built-in-class-function-p declared-ftype)
2148 ;; In that case, any definition satisfies the declaration.
2150 (;; It's not clear whether or how DEFINED-FTYPE might be
2151 ;; #<BUILT-IN-CLASS FUNCTION>, but it's not obviously
2152 ;; invalid, so let's handle that case too, just in case.
2153 (is-built-in-class-function-p defined-ftype)
2154 ;; No matter what DECLARED-FTYPE might be, we can't prove
2155 ;; that an object of type FUNCTION doesn't satisfy it, so
2156 ;; we return success no matter what.
2158 (;; Otherwise both of them must be FUN-TYPE objects.
2160 ;; FIXME: For now we only check compatibility of the return
2161 ;; type, not argument types, and we don't even check the
2162 ;; return type very precisely (as per bug 94a). It would be
2163 ;; good to do a better job. Perhaps to check the
2164 ;; compatibility of the arguments, we should (1) redo
2165 ;; VALUES-TYPES-EQUAL-OR-INTERSECT as
2166 ;; ARGS-TYPES-EQUAL-OR-INTERSECT, and then (2) apply it to
2167 ;; the ARGS-TYPE slices of the FUN-TYPEs. (ARGS-TYPE
2168 ;; is a base class both of VALUES-TYPE and of FUN-TYPE.)
2169 (values-types-equal-or-intersect
2170 (fun-type-returns defined-ftype)
2171 (fun-type-returns declared-ftype))))))
2173 ;;; This messy case of CTYPE for NUMBER is shared between the
2174 ;;; cross-compiler and the target system.
2175 (defun ctype-of-number (x)
2176 (let ((num (if (complexp x) (realpart x) x)))
2177 (multiple-value-bind (complexp low high)
2179 (let ((imag (imagpart x)))
2180 (values :complex (min num imag) (max num imag)))
2181 (values :real num num))
2182 (make-numeric-type :class (etypecase num
2184 (rational 'rational)
2186 :format (and (floatp num) (float-format-name num))
2192 ;; Why SAFETY 0? To suppress the is-it-the-right-structure-type
2193 ;; checking for declarations in structure accessors. Otherwise we
2194 ;; can get caught in a chicken-and-egg bootstrapping problem, whose
2195 ;; symptom on x86 OpenBSD sbcl-0.pre7.37.flaky5.22 is an illegal
2196 ;; instruction trap. I haven't tracked it down, but I'm guessing it
2197 ;; has to do with setting LAYOUTs when the LAYOUT hasn't been set
2199 (declare (optimize (safety 0)))
2200 (!defun-from-collected-cold-init-forms !late-type-cold-init))
2202 (/show0 "late-type.lisp end of file")