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: It seems to me that this should be set to NIL by default,
35 ;;; and perhaps not even optionally set to T.
36 (defvar *use-implementation-types* t
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.")
45 (!cold-init-forms (setq *use-implementation-types* t))
47 ;;; These functions are used as method for types which need a complex
48 ;;; subtypep method to handle some superclasses, but cover a subtree
49 ;;; of the type graph (i.e. there is no simple way for any other type
50 ;;; class to be a subtype.) There are always still complex ways,
51 ;;; namely UNION and MEMBER types, so we must give TYPE1's method a
52 ;;; chance to run, instead of immediately returning NIL, T.
53 (defun delegate-complex-subtypep-arg2 (type1 type2)
55 (type-class-complex-subtypep-arg1
56 (type-class-info type1))))
58 (funcall subtypep-arg1 type1 type2)
60 (defun delegate-complex-intersection2 (type1 type2)
61 (let ((method (type-class-complex-intersection2 (type-class-info type1))))
62 (if (and method (not (eq method #'delegate-complex-intersection2)))
63 (funcall method type2 type1)
64 (hierarchical-intersection2 type1 type2))))
66 ;;; This is used by !DEFINE-SUPERCLASSES to define the SUBTYPE-ARG1
67 ;;; method. INFO is a list of conses
68 ;;; (SUPERCLASS-CLASS . {GUARD-TYPE-SPECIFIER | NIL}).
69 ;;; This will never be called with a hairy type as TYPE2, since the
70 ;;; hairy type TYPE2 method gets first crack.
71 (defun !has-superclasses-complex-subtypep-arg1 (type1 type2 info)
73 (and (sb!xc:typep type2 'sb!xc:class)
75 (when (or (not (cdr x))
76 (csubtypep type1 (specifier-type (cdr x))))
78 (or (eq type2 (car x))
79 (let ((inherits (layout-inherits (class-layout (car x)))))
80 (dotimes (i (length inherits) nil)
81 (when (eq type2 (layout-class (svref inherits i)))
85 ;;; This function takes a list of specs, each of the form
86 ;;; (SUPERCLASS-NAME &OPTIONAL GUARD).
87 ;;; Consider one spec (with no guard): any instance of the named
88 ;;; TYPE-CLASS is also a subtype of the named superclass and of any of
89 ;;; its superclasses. If there are multiple specs, then some will have
90 ;;; guards. We choose the first spec whose guard is a supertype of
91 ;;; TYPE1 and use its superclass. In effect, a sequence of guards
94 ;;; G0,(and G1 (not G0)), (and G2 (not (or G0 G1))).
96 ;;; WHEN controls when the forms are executed.
97 (defmacro !define-superclasses (type-class-name specs when)
98 (let ((type-class (gensym "TYPE-CLASS-"))
99 (info (gensym "INFO")))
101 (let ((,type-class (type-class-or-lose ',type-class-name))
102 (,info (mapcar (lambda (spec)
104 (super &optional guard)
106 (cons (sb!xc:find-class super) guard)))
108 (setf (type-class-complex-subtypep-arg1 ,type-class)
109 (lambda (type1 type2)
110 (!has-superclasses-complex-subtypep-arg1 type1 type2 ,info)))
111 (setf (type-class-complex-subtypep-arg2 ,type-class)
112 #'delegate-complex-subtypep-arg2)
113 (setf (type-class-complex-intersection2 ,type-class)
114 #'delegate-complex-intersection2)))))
116 ;;;; FUNCTION and VALUES types
118 ;;;; Pretty much all of the general type operations are illegal on
119 ;;;; VALUES types, since we can't discriminate using them, do
120 ;;;; SUBTYPEP, etc. FUNCTION types are acceptable to the normal type
121 ;;;; operations, but are generally considered to be equivalent to
122 ;;;; FUNCTION. These really aren't true types in any type theoretic
123 ;;;; sense, but we still parse them into CTYPE structures for two
126 ;;;; -- Parsing and unparsing work the same way, and indeed we can't
127 ;;;; tell whether a type is a function or values type without
129 ;;;; -- Many of the places that can be annotated with real types can
130 ;;;; also be annotated with function or values types.
132 ;;; the description of a &KEY argument
133 (defstruct (key-info #-sb-xc-host (:pure t)
135 ;; the key (not necessarily a keyword in ANSI)
136 (name (required-argument) :type symbol)
137 ;; the type of the argument value
138 (type (required-argument) :type ctype))
140 (!define-type-method (values :simple-subtypep :complex-subtypep-arg1)
142 (declare (ignore type2))
143 ;; FIXME: should be TYPE-ERROR, here and in next method
144 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type1)))
146 (!define-type-method (values :complex-subtypep-arg2)
148 (declare (ignore type1))
149 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type2)))
151 (!define-type-method (values :unparse) (type)
152 (cons 'values (unparse-args-types type)))
154 ;;; Return true if LIST1 and LIST2 have the same elements in the same
155 ;;; positions according to TYPE=. We return NIL, NIL if there is an
156 ;;; uncertain comparison.
157 (defun type=-list (list1 list2)
158 (declare (list list1 list2))
159 (do ((types1 list1 (cdr types1))
160 (types2 list2 (cdr types2)))
161 ((or (null types1) (null types2))
162 (if (or types1 types2)
165 (multiple-value-bind (val win)
166 (type= (first types1) (first types2))
168 (return (values nil nil)))
170 (return (values nil t))))))
172 (!define-type-method (values :simple-=) (type1 type2)
173 (let ((rest1 (args-type-rest type1))
174 (rest2 (args-type-rest type2)))
175 (cond ((or (args-type-keyp type1) (args-type-keyp type2)
176 (args-type-allowp type1) (args-type-allowp type2))
178 ((and rest1 rest2 (type/= rest1 rest2))
183 (multiple-value-bind (req-val req-win)
184 (type=-list (values-type-required type1)
185 (values-type-required type2))
186 (multiple-value-bind (opt-val opt-win)
187 (type=-list (values-type-optional type1)
188 (values-type-optional type2))
189 (values (and req-val opt-val) (and req-win opt-win))))))))
191 (!define-type-class function)
193 ;;; a flag that we can bind to cause complex function types to be
194 ;;; unparsed as FUNCTION. This is useful when we want a type that we
195 ;;; can pass to TYPEP.
196 (defvar *unparse-function-type-simplify*)
197 (!cold-init-forms (setq *unparse-function-type-simplify* nil))
199 (!define-type-method (function :unparse) (type)
200 (if *unparse-function-type-simplify*
203 (if (function-type-wild-args type)
205 (unparse-args-types type))
207 (function-type-returns type)))))
209 ;;; Since all function types are equivalent to FUNCTION, they are all
210 ;;; subtypes of each other.
211 (!define-type-method (function :simple-subtypep) (type1 type2)
212 (declare (ignore type1 type2))
215 (!define-superclasses function ((function)) !cold-init-forms)
217 ;;; The union or intersection of two FUNCTION types is FUNCTION.
218 (!define-type-method (function :simple-union2) (type1 type2)
219 (declare (ignore type1 type2))
220 (specifier-type 'function))
221 (!define-type-method (function :simple-intersection2) (type1 type2)
222 (declare (ignore type1 type2))
223 (specifier-type 'function))
225 ;;; ### Not very real, but good enough for redefining transforms
226 ;;; according to type:
227 (!define-type-method (function :simple-=) (type1 type2)
228 (values (equalp type1 type2) t))
230 (!define-type-class constant :inherits values)
232 (!define-type-method (constant :unparse) (type)
233 `(constant-argument ,(type-specifier (constant-type-type type))))
235 (!define-type-method (constant :simple-=) (type1 type2)
236 (type= (constant-type-type type1) (constant-type-type type2)))
238 (!def-type-translator constant-argument (type)
239 (make-constant-type :type (specifier-type type)))
241 ;;; Given a LAMBDA-LIST-like values type specification and an ARGS-TYPE
242 ;;; structure, fill in the slots in the structure accordingly. This is
243 ;;; used for both FUNCTION and VALUES types.
244 (declaim (ftype (function (list args-type) (values)) parse-args-types))
245 (defun parse-args-types (lambda-list result)
246 (multiple-value-bind (required optional restp rest keyp keys allowp aux)
247 (parse-lambda-list lambda-list)
249 (error "&AUX in a FUNCTION or VALUES type: ~S." lambda-list))
250 (setf (args-type-required result) (mapcar #'specifier-type required))
251 (setf (args-type-optional result) (mapcar #'specifier-type optional))
252 (setf (args-type-rest result) (if restp (specifier-type rest) nil))
253 (setf (args-type-keyp result) keyp)
254 (collect ((key-info))
256 (unless (proper-list-of-length-p key 2)
257 (error "Keyword type description is not a two-list: ~S." key))
258 (let ((kwd (first key)))
259 (when (find kwd (key-info) :key #'key-info-name)
260 (error "~@<repeated keyword ~S in lambda list: ~2I~_~S~:>"
262 (key-info (make-key-info :name kwd
263 :type (specifier-type (second key))))))
264 (setf (args-type-keywords result) (key-info)))
265 (setf (args-type-allowp result) allowp)
268 ;;; Return the lambda-list-like type specification corresponding
270 (declaim (ftype (function (args-type) list) unparse-args-types))
271 (defun unparse-args-types (type)
274 (dolist (arg (args-type-required type))
275 (result (type-specifier arg)))
277 (when (args-type-optional type)
279 (dolist (arg (args-type-optional type))
280 (result (type-specifier arg))))
282 (when (args-type-rest type)
284 (result (type-specifier (args-type-rest type))))
286 (when (args-type-keyp type)
288 (dolist (key (args-type-keywords type))
289 (result (list (key-info-name key)
290 (type-specifier (key-info-type key))))))
292 (when (args-type-allowp type)
293 (result '&allow-other-keys))
297 (!def-type-translator function (&optional (args '*) (result '*))
298 (let ((res (make-function-type
299 :returns (values-specifier-type result))))
301 (setf (function-type-wild-args res) t)
302 (parse-args-types args res))
305 (!def-type-translator values (&rest values)
306 (let ((res (make-values-type)))
307 (parse-args-types values res)
310 ;;;; VALUES types interfaces
312 ;;;; We provide a few special operations that can be meaningfully used
313 ;;;; on VALUES types (as well as on any other type).
315 ;;; Return the type of the first value indicated by TYPE. This is used
316 ;;; by people who don't want to have to deal with VALUES types.
317 #!-sb-fluid (declaim (freeze-type values-type))
318 ; (inline single-value-type))
319 (defun single-value-type (type)
320 (declare (type ctype type))
321 (cond ((values-type-p type)
322 (or (car (args-type-required type))
323 (if (args-type-optional type)
324 (type-union (car (args-type-optional type))
325 (specifier-type 'null)))
326 (args-type-rest type)
327 (specifier-type 'null)))
328 ((eq type *wild-type*)
333 ;;; Return the minimum number of arguments that a function can be
334 ;;; called with, and the maximum number or NIL. If not a function
335 ;;; type, return NIL, NIL.
336 (defun function-type-nargs (type)
337 (declare (type ctype type))
338 (if (function-type-p type)
339 (let ((fixed (length (args-type-required type))))
340 (if (or (args-type-rest type)
341 (args-type-keyp type)
342 (args-type-allowp type))
344 (values fixed (+ fixed (length (args-type-optional type))))))
347 ;;; Determine whether TYPE corresponds to a definite number of values.
348 ;;; The first value is a list of the types for each value, and the
349 ;;; second value is the number of values. If the number of values is
350 ;;; not fixed, then return NIL and :UNKNOWN.
351 (defun values-types (type)
352 (declare (type ctype type))
353 (cond ((eq type *wild-type*)
354 (values nil :unknown))
355 ((not (values-type-p type))
356 (values (list type) 1))
357 ((or (args-type-optional type)
358 (args-type-rest type)
359 (args-type-keyp type)
360 (args-type-allowp type))
361 (values nil :unknown))
363 (let ((req (args-type-required type)))
364 (values (mapcar #'single-value-type req) (length req))))))
366 ;;; Return two values:
367 ;;; 1. A list of all the positional (fixed and optional) types.
368 ;;; 2. The &REST type (if any). If keywords allowed, *UNIVERSAL-TYPE*.
369 ;;; If no keywords or &REST, then the DEFAULT-TYPE.
370 (defun values-type-types (type &optional (default-type *empty-type*))
371 (declare (type values-type type))
372 (values (append (args-type-required type)
373 (args-type-optional type))
374 (cond ((args-type-keyp type) *universal-type*)
375 ((args-type-rest type))
379 ;;; Return a list of OPERATION applied to the types in TYPES1 and
380 ;;; TYPES2, padding with REST2 as needed. TYPES1 must not be shorter
381 ;;; than TYPES2. The second value is T if OPERATION always returned a
382 ;;; true second value.
383 (defun fixed-values-op (types1 types2 rest2 operation)
384 (declare (list types1 types2) (type ctype rest2) (type function operation))
386 (values (mapcar #'(lambda (t1 t2)
387 (multiple-value-bind (res win)
388 (funcall operation t1 t2)
394 (make-list (- (length types1) (length types2))
395 :initial-element rest2)))
398 ;;; If Type isn't a values type, then make it into one:
399 ;;; <type> ==> (values type &rest t)
400 (defun coerce-to-values (type)
401 (declare (type ctype type))
402 (if (values-type-p type)
404 (make-values-type :required (list type) :rest *universal-type*)))
406 ;;; Do the specified OPERATION on TYPE1 and TYPE2, which may be any
407 ;;; type, including VALUES types. With VALUES types such as:
410 ;;; we compute the more useful result
411 ;;; (VALUES (<operation> a0 b0) (<operation> a1 b1))
412 ;;; rather than the precise result
413 ;;; (<operation> (values a0 a1) (values b0 b1))
414 ;;; This has the virtue of always keeping the VALUES type specifier
415 ;;; outermost, and retains all of the information that is really
416 ;;; useful for static type analysis. We want to know what is always
417 ;;; true of each value independently. It is worthless to know that if
418 ;;; the first value is B0 then the second will be B1.
420 ;;; If the VALUES count signatures differ, then we produce a result with
421 ;;; the required VALUE count chosen by NREQ when applied to the number
422 ;;; of required values in TYPE1 and TYPE2. Any &KEY values become
423 ;;; &REST T (anyone who uses keyword values deserves to lose.)
425 ;;; The second value is true if the result is definitely empty or if
426 ;;; OPERATION returned true as its second value each time we called
427 ;;; it. Since we approximate the intersection of VALUES types, the
428 ;;; second value being true doesn't mean the result is exact.
429 (defun args-type-op (type1 type2 operation nreq default-type)
430 (declare (type ctype type1 type2 default-type)
431 (type function operation nreq))
432 (if (or (values-type-p type1) (values-type-p type2))
433 (let ((type1 (coerce-to-values type1))
434 (type2 (coerce-to-values type2)))
435 (multiple-value-bind (types1 rest1)
436 (values-type-types type1 default-type)
437 (multiple-value-bind (types2 rest2)
438 (values-type-types type2 default-type)
439 (multiple-value-bind (rest rest-exact)
440 (funcall operation rest1 rest2)
441 (multiple-value-bind (res res-exact)
442 (if (< (length types1) (length types2))
443 (fixed-values-op types2 types1 rest1 operation)
444 (fixed-values-op types1 types2 rest2 operation))
445 (let* ((req (funcall nreq
446 (length (args-type-required type1))
447 (length (args-type-required type2))))
448 (required (subseq res 0 req))
449 (opt (subseq res req))
450 (opt-last (position rest opt :test-not #'type=
452 (if (find *empty-type* required :test #'type=)
453 (values *empty-type* t)
454 (values (make-values-type
456 :optional (if opt-last
457 (subseq opt 0 (1+ opt-last))
459 :rest (if (eq rest default-type) nil rest))
460 (and rest-exact res-exact)))))))))
461 (funcall operation type1 type2)))
463 ;;; Do a union or intersection operation on types that might be values
464 ;;; types. The result is optimized for utility rather than exactness,
465 ;;; but it is guaranteed that it will be no smaller (more restrictive)
466 ;;; than the precise result.
468 ;;; The return convention seems to be analogous to
469 ;;; TYPES-INTERSECT. -- WHN 19990910.
470 (defun-cached (values-type-union :hash-function type-cache-hash
473 :init-wrapper !cold-init-forms)
474 ((type1 eq) (type2 eq))
475 (declare (type ctype type1 type2))
476 (cond ((or (eq type1 *wild-type*) (eq type2 *wild-type*)) *wild-type*)
477 ((eq type1 *empty-type*) type2)
478 ((eq type2 *empty-type*) type1)
480 (values (args-type-op type1 type2 #'type-union #'min *empty-type*)))))
481 (defun-cached (values-type-intersection :hash-function type-cache-hash
484 :default (values nil :empty)
485 :init-wrapper !cold-init-forms)
486 ((type1 eq) (type2 eq))
487 (declare (type ctype type1 type2))
488 (cond ((eq type1 *wild-type*) (values type2 t))
489 ((eq type2 *wild-type*) (values type1 t))
491 (args-type-op type1 type2
494 (specifier-type 'null)))))
496 ;;; This is like TYPES-INTERSECT, except that it sort of works on
497 ;;; VALUES types. Note that due to the semantics of
498 ;;; VALUES-TYPE-INTERSECTION, this might return (VALUES T T) when
499 ;;; there isn't really any intersection (?).
501 ;;; The return convention seems to be analogous to
502 ;;; TYPES-INTERSECT. -- WHN 19990910.
503 (defun values-types-intersect (type1 type2)
504 (cond ((or (eq type1 *empty-type*) (eq type2 *empty-type*))
506 ((or (values-type-p type1) (values-type-p type2))
507 (multiple-value-bind (res win) (values-type-intersection type1 type2)
508 (values (not (eq res *empty-type*))
511 (types-intersect type1 type2))))
513 ;;; a SUBTYPEP-like operation that can be used on any types, including
515 (defun-cached (values-subtypep :hash-function type-cache-hash
518 :default (values nil :empty)
519 :init-wrapper !cold-init-forms)
520 ((type1 eq) (type2 eq))
521 (declare (type ctype type1 type2))
522 (cond ((eq type2 *wild-type*) (values t t))
523 ((eq type1 *wild-type*)
524 (values (eq type2 *universal-type*) t))
525 ((not (values-types-intersect type1 type2))
528 (if (or (values-type-p type1) (values-type-p type2))
529 (let ((type1 (coerce-to-values type1))
530 (type2 (coerce-to-values type2)))
531 (multiple-value-bind (types1 rest1) (values-type-types type1)
532 (multiple-value-bind (types2 rest2) (values-type-types type2)
533 (cond ((< (length (values-type-required type1))
534 (length (values-type-required type2)))
536 ((< (length types1) (length types2))
538 ((or (values-type-keyp type1)
539 (values-type-keyp type2))
542 (do ((t1 types1 (rest t1))
543 (t2 types2 (rest t2)))
545 (csubtypep rest1 rest2))
546 (multiple-value-bind (res win-p)
547 (csubtypep (first t1) (first t2))
549 (return (values nil nil)))
551 (return (values nil t))))))))))
552 (csubtypep type1 type2)))))
554 ;;;; type method interfaces
556 ;;; like SUBTYPEP, only works on CTYPE structures
557 (defun-cached (csubtypep :hash-function type-cache-hash
560 :default (values nil :empty)
561 :init-wrapper !cold-init-forms)
562 ((type1 eq) (type2 eq))
563 (declare (type ctype type1 type2))
564 (cond ((or (eq type1 type2)
565 (eq type1 *empty-type*)
566 (eq type2 *wild-type*))
568 ((or (eq type1 *wild-type*)
569 (eq type2 *empty-type*))
572 (!invoke-type-method :simple-subtypep :complex-subtypep-arg2
574 :complex-arg1 :complex-subtypep-arg1))))
576 ;;; Just parse the type specifiers and call CSUBTYPE.
577 (defun sb!xc:subtypep (type1 type2)
579 "Return two values indicating the relationship between type1 and type2.
580 If values are T and T, type1 definitely is a subtype of type2.
581 If values are NIL and T, type1 definitely is not a subtype of type2.
582 If values are NIL and NIL, it couldn't be determined."
583 (csubtypep (specifier-type type1) (specifier-type type2)))
585 ;;; If two types are definitely equivalent, return true. The second
586 ;;; value indicates whether the first value is definitely correct.
587 ;;; This should only fail in the presence of HAIRY types.
588 (defun-cached (type= :hash-function type-cache-hash
591 :default (values nil :empty)
592 :init-wrapper !cold-init-forms)
593 ((type1 eq) (type2 eq))
594 (declare (type ctype type1 type2))
597 (!invoke-type-method :simple-= :complex-= type1 type2)))
599 ;;; Not exactly the negation of TYPE=, since when the relationship is
600 ;;; uncertain, we still return NIL, NIL. This is useful in cases where
601 ;;; the conservative assumption is =.
602 (defun type/= (type1 type2)
603 (declare (type ctype type1 type2))
604 (multiple-value-bind (res win) (type= type1 type2)
609 ;;; the type method dispatch case of TYPE-UNION2
610 (defun %type-union2 (type1 type2)
611 ;; As in %TYPE-INTERSECTION2, it seems to be a good idea to give
612 ;; both argument orders a chance at COMPLEX-INTERSECTION2. Unlike
613 ;; %TYPE-INTERSECTION2, though, I don't have a specific case which
614 ;; demonstrates this is actually necessary. Also unlike
615 ;; %TYPE-INTERSECTION2, there seems to be no need to distinguish
616 ;; between not finding a method and having a method return NIL.
618 (let ((result (!invoke-type-method :simple-union2 :complex-union2
621 ;; UNION2 type methods are supposed to return results
622 ;; which are better than just brute-forcibly smashing the
623 ;; terms together into UNION-TYPEs. But they're derived
624 ;; from old CMU CL UNION type methods which played by
625 ;; somewhat different rules. Here we check to make sure
626 ;; we don't get ambushed by diehard old-style code.
627 (assert (not (union-type-p result)))
629 (declare (inline 1way))
630 (or (1way type1 type2)
631 (1way type2 type1))))
633 ;;; Find a type which includes both types. Any inexactness is
634 ;;; represented by the fuzzy element types; we return a single value
635 ;;; that is precise to the best of our knowledge. This result is
636 ;;; simplified into the canonical form, thus is not a UNION-TYPE
637 ;;; unless we find no other way to represent the result.
638 (defun-cached (type-union2 :hash-function type-cache-hash
640 :init-wrapper !cold-init-forms)
641 ((type1 eq) (type2 eq))
642 ;; KLUDGE: This was generated from TYPE-INTERSECTION2 by Ye Olde Cut And
643 ;; Paste technique of programming. If it stays around (as opposed to
644 ;; e.g. fading away in favor of some CLOS solution) the shared logic
645 ;; should probably become shared code. -- WHN 2001-03-16
646 (declare (type ctype type1 type2))
647 (cond ((eq type1 type2)
649 ((or (union-type-p type1)
650 (union-type-p type2))
651 ;; Unions of UNION-TYPE should have the UNION-TYPE-TYPES
652 ;; values broken out and united separately. The full TYPE-UNION
653 ;; function knows how to do this, so let it handle it.
654 (type-union type1 type2))
656 ;; the ordinary case: we dispatch to type methods
657 (%type-union2 type1 type2))))
659 ;;; the type method dispatch case of TYPE-INTERSECTION2
660 (defun %type-intersection2 (type1 type2)
661 ;; We want to give both argument orders a chance at
662 ;; COMPLEX-INTERSECTION2. Without that, the old CMU CL type
663 ;; methods could give noncommutative results, e.g.
664 ;; (TYPE-INTERSECTION2 *EMPTY-TYPE* SOME-HAIRY-TYPE)
666 ;; (TYPE-INTERSECTION2 SOME-HAIRY-TYPE *EMPTY-TYPE*)
667 ;; => #<NAMED-TYPE NIL>, T
668 ;; We also need to distinguish between the case where we found a
669 ;; type method, and it returned NIL, and the case where we fell
670 ;; through without finding any type method. An example of the first
671 ;; case is the intersection of a HAIRY-TYPE with some ordinary type.
672 ;; An example of the second case is the intersection of two
673 ;; completely-unrelated types, e.g. CONS and NUMBER, or SYMBOL and
676 ;; (Why yes, CLOS probably *would* be nicer..)
679 (!invoke-type-method :simple-intersection2
680 :complex-intersection2
682 :default :no-type-method-found)))
683 ;; INTERSECTION2 type methods are supposed to return
684 ;; results which are better than just brute-forcibly
685 ;; smashing the terms together into INTERSECTION-TYPEs.
686 ;; But they're derived from old CMU CL INTERSECTION type
687 ;; methods which played by somewhat different rules. Here
688 ;; we check to make sure we don't get ambushed by diehard
690 (assert (not (intersection-type-p result)))
692 (declare (inline 1way))
693 (let ((xy (1way type1 type2)))
694 (or (and (not (eql xy :no-type-method-found)) xy)
695 (let ((yx (1way type2 type1)))
696 (or (and (not (eql yx :no-type-method-found)) yx)
697 (cond ((and (eql xy :no-type-method-found)
698 (eql yx :no-type-method-found))
701 (assert (and (not xy) (not yx))) ; else handled above
704 (defun-cached (type-intersection2 :hash-function type-cache-hash
708 :init-wrapper !cold-init-forms)
709 ((type1 eq) (type2 eq))
710 (declare (type ctype type1 type2))
711 (cond ((eq type1 type2)
713 ((or (intersection-type-p type1)
714 (intersection-type-p type2))
715 ;; Intersections of INTERSECTION-TYPE should have the
716 ;; INTERSECTION-TYPE-TYPES values broken out and intersected
717 ;; separately. The full TYPE-INTERSECTION function knows how
718 ;; to do that, so let it handle it.
719 (type-intersection type1 type2))
721 ;; the ordinary case: we dispatch to type methods
722 (%type-intersection2 type1 type2))))
724 ;;; Return as restrictive and simple a type as we can discover that is
725 ;;; no more restrictive than the intersection of TYPE1 and TYPE2. At
726 ;;; worst, we arbitrarily return one of the arguments as the first
727 ;;; value (trying not to return a hairy type).
728 (defun type-approx-intersection2 (type1 type2)
729 (cond ((type-intersection2 type1 type2))
730 ((hairy-type-p type1) type2)
733 ;;; The first value is true unless the types don't intersect. The
734 ;;; second value is true if the first value is definitely correct. NIL
735 ;;; is considered to intersect with any type. If T is a subtype of
736 ;;; either type, then we also return T, T. This way we recognize
737 ;;; that hairy types might intersect with T.
739 ;;; FIXME: It would be more accurate to call this TYPES-MIGHT-INTERSECT,
740 ;;; and rename VALUES-TYPES-INTERSECT the same way.
741 (defun types-intersect (type1 type2)
742 (declare (type ctype type1 type2))
743 (if (or (eq type1 *empty-type*) (eq type2 *empty-type*))
745 (let ((intersection2 (type-intersection2 type1 type2)))
746 (cond ((not intersection2)
747 (if (or (csubtypep *universal-type* type1)
748 (csubtypep *universal-type* type2))
751 ((eq intersection2 *empty-type*) (values nil t))
754 ;;; Return a Common Lisp type specifier corresponding to the TYPE
756 (defun type-specifier (type)
757 (declare (type ctype type))
758 (funcall (type-class-unparse (type-class-info type)) type))
760 ;;; (VALUES-SPECIFIER-TYPE and SPECIFIER-TYPE moved from here to
761 ;;; early-type.lisp by WHN ca. 19990201.)
763 ;;; Take a list of type specifiers, computing the translation of each
764 ;;; specifier and defining it as a builtin type.
765 (declaim (ftype (function (list) (values)) precompute-types))
766 (defun precompute-types (specs)
768 (let ((res (specifier-type spec)))
769 (unless (unknown-type-p res)
770 (setf (info :type :builtin spec) res)
771 (setf (info :type :kind spec) :primitive))))
774 ;;;; general TYPE-UNION and TYPE-INTERSECTION operations
776 ;;;; These are fully general operations on CTYPEs: they'll always
777 ;;;; return a CTYPE representing the result.
779 ;;; shared logic for unions and intersections: Stuff TYPE into the
780 ;;; vector TYPES, finding pairs of types which can be simplified by
781 ;;; SIMPLIFY2 and replacing them by their simplified forms.
782 (defun accumulate-compound-type (type types simplify2)
783 (declare (type ctype type))
784 (declare (type (vector ctype) types))
785 (declare (type function simplify2))
786 (dotimes (i (length types) (vector-push-extend type types))
787 (let ((simplified2 (funcall simplify2 type (aref types i))))
789 ;; Discard the old (AREF TYPES I).
790 (setf (aref types i) (vector-pop types))
791 ;; Add the new SIMPLIFIED2 to TYPES, by tail recursing.
792 (return (accumulate-compound-type simplified2
797 ;;; shared logic for unions and intersections: Return a vector of
798 ;;; types representing the same types as INPUT-TYPES, but with
799 ;;; COMPOUND-TYPEs satisfying %COMPOUND-TYPE-P broken up into their
800 ;;; component types, and with any SIMPLY2 simplifications applied.
801 (defun simplified-compound-types (input-types %compound-type-p simplify2)
802 (let ((simplified-types (make-array (length input-types)
805 ;; (This INITIAL-ELEMENT shouldn't
806 ;; matter, but helps avoid type
807 ;; warnings at compile time.)
808 :initial-element *empty-type*)))
809 (flet ((accumulate (type)
810 (accumulate-compound-type type simplified-types simplify2)))
811 (declare (inline accumulate))
812 (dolist (type input-types)
813 (if (funcall %compound-type-p type)
814 (map nil #'accumulate (compound-type-types type))
818 ;;; shared logic for unions and intersections: Make a COMPOUND-TYPE
819 ;;; object whose components are the types in TYPES, or skip to special
820 ;;; cases when TYPES is short.
821 (defun make-compound-type-or-something (constructor types enumerable identity)
822 (declare (type function constructor))
823 (declare (type (vector ctype) types))
824 (declare (type ctype identity))
828 (t (funcall constructor
830 ;; FIXME: This should be just (COERCE TYPES 'LIST), but as
831 ;; of sbcl-0.6.11.17 the COERCE optimizer is really
832 ;; brain-dead, so that would generate a full call to
833 ;; SPECIFIER-TYPE at runtime, so we get into bootstrap
834 ;; problems in cold init because 'LIST is a compound
835 ;; type, so we need to MAKE-COMPOUND-TYPE-OR-SOMETHING
836 ;; before we know what 'LIST is. Once the COERCE
837 ;; optimizer is less brain-dead, we can make this
838 ;; (COERCE TYPES 'LIST) again.
839 #+sb-xc-host (coerce types 'list)
840 #-sb-xc-host (coerce-to-list types)))))
842 (defun type-intersection (&rest input-types)
843 (let ((simplified-types (simplified-compound-types input-types
844 #'intersection-type-p
845 #'type-intersection2)))
846 ;; We want to have a canonical representation of types (or failing
847 ;; that, punt to HAIRY-TYPE). Canonical representation would have
848 ;; intersections inside unions but not vice versa, since you can
849 ;; always achieve that by the distributive rule. But we don't want
850 ;; to just apply the distributive rule, since it would be too easy
851 ;; to end up with unreasonably huge type expressions. So instead
852 ;; we punt to HAIRY-TYPE when this comes up.
853 (if (and (> (length simplified-types) 1)
854 (some #'union-type-p simplified-types))
856 :specifier `(and ,@(map 'list #'type-specifier simplified-types)))
857 (make-compound-type-or-something #'%make-intersection-type
859 (some #'type-enumerable
863 (defun type-union (&rest input-types)
864 (let ((simplified-types (simplified-compound-types input-types
867 (make-compound-type-or-something #'%make-union-type
869 (every #'type-enumerable simplified-types)
874 (!define-type-class named)
877 (defvar *empty-type*)
878 (defvar *universal-type*)
881 (macrolet ((frob (name var)
883 (setq ,var (make-named-type :name ',name))
884 (setf (info :type :kind ',name) :primitive)
885 (setf (info :type :builtin ',name) ,var))))
886 ;; KLUDGE: In ANSI, * isn't really the name of a type, it's just a
887 ;; special symbol which can be stuck in some places where an
888 ;; ordinary type can go, e.g. (ARRAY * 1) instead of (ARRAY T 1).
889 ;; At some point, in order to become more standard, we should
890 ;; convert all the classic CMU CL legacy *s and *WILD-TYPE*s into
891 ;; Ts and *UNIVERSAL-TYPE*s.
893 (frob nil *empty-type*)
894 (frob t *universal-type*)))
896 (!define-type-method (named :simple-=) (type1 type2)
897 ;; FIXME: BUG 85: This assertion failed when I added it in
898 ;; sbcl-0.6.11.13. It probably shouldn't fail; but for now it's
899 ;; just commented out.
900 ;;(assert (not (eq type1 *wild-type*))) ; * isn't really a type.
901 (values (eq type1 type2) t))
903 (!define-type-method (named :simple-subtypep) (type1 type2)
904 (assert (not (eq type1 *wild-type*))) ; * isn't really a type.
905 (values (or (eq type1 *empty-type*) (eq type2 *wild-type*)) t))
907 (!define-type-method (named :complex-subtypep-arg1) (type1 type2)
908 (assert (not (eq type1 *wild-type*))) ; * isn't really a type.
909 ;; FIXME: Why does this (old CMU CL) assertion hold? Perhaps 'cause
910 ;; the HAIRY-TYPE COMPLEX-SUBTYPEP-ARG2 method takes precedence over
911 ;; this COMPLEX-SUBTYPE-ARG1 method? (I miss CLOS..)
912 (assert (not (hairy-type-p type2)))
913 ;; Besides the old CMU CL assertion above, we also need to avoid
914 ;; compound types, else we could get into trouble with
915 ;; (SUBTYPEP 'T '(OR (SATISFIES FOO) (SATISFIES BAR)))
917 ;; (SUBTYPEP 'T '(AND (SATISFIES FOO) (SATISFIES BAR))).
918 (assert (not (compound-type-p type2)))
919 ;; Then, since TYPE2 is reasonably tractable, we're good to go.
920 (values (eq type1 *empty-type*) t))
922 (!define-type-method (named :complex-subtypep-arg2) (type1 type2)
923 (assert (not (eq type2 *wild-type*))) ; * isn't really a type.
924 (cond ((eq type2 *universal-type*)
926 ((hairy-type-p type1)
929 ;; FIXME: This seems to rely on there only being 2 or 3
930 ;; HAIRY-TYPE values, and the exclusion of various
931 ;; possibilities above. It would be good to explain it and/or
932 ;; rewrite it so that it's clearer.
933 (values (not (eq type2 *empty-type*)) t))))
935 (!define-type-method (named :complex-intersection2) (type1 type2)
936 ;; FIXME: This assertion failed when I added it in sbcl-0.6.11.13.
937 ;; Perhaps when bug 85 is fixed it can be reenabled.
938 ;;(assert (not (eq type2 *wild-type*))) ; * isn't really a type.
939 (hierarchical-intersection2 type1 type2))
941 (!define-type-method (named :complex-union2) (type1 type2)
942 ;; Perhaps when bug 85 is fixed this can be reenabled.
943 ;;(assert (not (eq type2 *wild-type*))) ; * isn't really a type.
944 (hierarchical-union2 type1 type2))
946 (!define-type-method (named :unparse) (x)
949 ;;;; hairy and unknown types
951 (!define-type-method (hairy :unparse) (x) (hairy-type-specifier x))
953 (!define-type-method (hairy :simple-subtypep) (type1 type2)
954 (let ((hairy-spec1 (hairy-type-specifier type1))
955 (hairy-spec2 (hairy-type-specifier type2)))
956 (cond ((and (consp hairy-spec1) (eq (car hairy-spec1) 'not)
957 (consp hairy-spec2) (eq (car hairy-spec2) 'not))
958 (csubtypep (specifier-type (cadr hairy-spec2))
959 (specifier-type (cadr hairy-spec1))))
960 ((equal hairy-spec1 hairy-spec2)
965 (!define-type-method (hairy :complex-subtypep-arg2) (type1 type2)
966 (let ((hairy-spec (hairy-type-specifier type2)))
967 (cond ((and (consp hairy-spec) (eq (car hairy-spec) 'not))
968 (let* ((complement-type2 (specifier-type (cadr hairy-spec)))
969 (intersection2 (type-intersection2 type1
972 (values (eq intersection2 *empty-type*) t)
977 (!define-type-method (hairy :complex-subtypep-arg1 :complex-=) (type1 type2)
978 (declare (ignore type1 type2))
981 (!define-type-method (hairy :simple-intersection2 :complex-intersection2)
983 (declare (ignore type1 type2))
986 (!define-type-method (hairy :simple-=) (type1 type2)
987 (if (equal (hairy-type-specifier type1)
988 (hairy-type-specifier type2))
992 (!def-type-translator not (&whole whole type)
993 (declare (ignore type))
994 ;; Check legality of arguments.
995 (destructuring-bind (not typespec) whole
996 (declare (ignore not))
997 (specifier-type typespec)) ; must be legal typespec
999 (make-hairy-type :specifier whole))
1001 (!def-type-translator satisfies (&whole whole fun)
1002 (declare (ignore fun))
1003 ;; Check legality of arguments.
1004 (destructuring-bind (satisfies predicate-name) whole
1005 (declare (ignore satisfies))
1006 (unless (symbolp predicate-name)
1007 (error 'simple-type-error
1008 :datum predicate-name
1009 :expected-type 'symbol
1010 :format-control "~S is not a symbol."
1011 :format-arguments (list predicate-name))))
1013 (make-hairy-type :specifier whole))
1017 #!+negative-zero-is-not-zero
1018 (defun make-numeric-type (&key class format (complexp :real) low high
1020 (flet ((canonicalise-low-bound (x)
1021 ;; Canonicalise a low bound of (-0.0) to 0.0.
1022 (if (and (consp x) (floatp (car x)) (zerop (car x))
1023 (minusp (float-sign (car x))))
1026 (canonicalise-high-bound (x)
1027 ;; Canonicalise a high bound of (+0.0) to -0.0.
1028 (if (and (consp x) (floatp (car x)) (zerop (car x))
1029 (plusp (float-sign (car x))))
1030 (float -0.0 (car x))
1032 (%make-numeric-type :class class
1035 :low (canonicalise-low-bound low)
1036 :high (canonicalise-high-bound high)
1037 :enumerable enumerable)))
1039 (!define-type-class number)
1041 (!define-type-method (number :simple-=) (type1 type2)
1043 (and (eq (numeric-type-class type1) (numeric-type-class type2))
1044 (eq (numeric-type-format type1) (numeric-type-format type2))
1045 (eq (numeric-type-complexp type1) (numeric-type-complexp type2))
1046 (equal (numeric-type-low type1) (numeric-type-low type2))
1047 (equal (numeric-type-high type1) (numeric-type-high type2)))
1050 (!define-type-method (number :unparse) (type)
1051 (let* ((complexp (numeric-type-complexp type))
1052 (low (numeric-type-low type))
1053 (high (numeric-type-high type))
1054 (base (case (numeric-type-class type)
1056 (rational 'rational)
1057 (float (or (numeric-type-format type) 'float))
1060 (cond ((and (eq base 'integer) high low)
1061 (let ((high-count (logcount high))
1062 (high-length (integer-length high)))
1064 (cond ((= high 0) '(integer 0 0))
1066 ((and (= high-count high-length)
1067 (plusp high-length))
1068 `(unsigned-byte ,high-length))
1070 `(mod ,(1+ high)))))
1071 ((and (= low sb!vm:*target-most-negative-fixnum*)
1072 (= high sb!vm:*target-most-positive-fixnum*))
1074 ((and (= low (lognot high))
1075 (= high-count high-length)
1077 `(signed-byte ,(1+ high-length)))
1079 `(integer ,low ,high)))))
1080 (high `(,base ,(or low '*) ,high))
1082 (if (and (eq base 'integer) (= low 0))
1090 (if (eq base+bounds 'real)
1092 `(complex ,base+bounds)))
1094 (assert (eq base+bounds 'real))
1097 ;;; Return true if X is "less than or equal" to Y, taking open bounds
1098 ;;; into consideration. CLOSED is the predicate used to test the bound
1099 ;;; on a closed interval (e.g. <=), and OPEN is the predicate used on
1100 ;;; open bounds (e.g. <). Y is considered to be the outside bound, in
1101 ;;; the sense that if it is infinite (NIL), then the test succeeds,
1102 ;;; whereas if X is infinite, then the test fails (unless Y is also
1105 ;;; This is for comparing bounds of the same kind, e.g. upper and
1106 ;;; upper. Use NUMERIC-BOUND-TEST* for different kinds of bounds.
1107 #!-negative-zero-is-not-zero
1108 (defmacro numeric-bound-test (x y closed open)
1113 (,closed (car ,x) (car ,y))
1114 (,closed (car ,x) ,y)))
1120 #!+negative-zero-is-not-zero
1121 (defmacro numeric-bound-test-zero (op x y)
1122 `(if (and (zerop ,x) (zerop ,y) (floatp ,x) (floatp ,y))
1123 (,op (float-sign ,x) (float-sign ,y))
1126 #!+negative-zero-is-not-zero
1127 (defmacro numeric-bound-test (x y closed open)
1132 (numeric-bound-test-zero ,closed (car ,x) (car ,y))
1133 (numeric-bound-test-zero ,closed (car ,x) ,y)))
1136 (numeric-bound-test-zero ,open ,x (car ,y))
1137 (numeric-bound-test-zero ,closed ,x ,y)))))
1139 ;;; This is used to compare upper and lower bounds. This is different
1140 ;;; from the same-bound case:
1141 ;;; -- Since X = NIL is -infinity, whereas y = NIL is +infinity, we
1142 ;;; return true if *either* arg is NIL.
1143 ;;; -- an open inner bound is "greater" and also squeezes the interval,
1144 ;;; causing us to use the OPEN test for those cases as well.
1145 #!-negative-zero-is-not-zero
1146 (defmacro numeric-bound-test* (x y closed open)
1151 (,open (car ,x) (car ,y))
1152 (,open (car ,x) ,y)))
1158 #!+negative-zero-is-not-zero
1159 (defmacro numeric-bound-test* (x y closed open)
1164 (numeric-bound-test-zero ,open (car ,x) (car ,y))
1165 (numeric-bound-test-zero ,open (car ,x) ,y)))
1168 (numeric-bound-test-zero ,open ,x (car ,y))
1169 (numeric-bound-test-zero ,closed ,x ,y)))))
1171 ;;; Return whichever of the numeric bounds X and Y is "maximal"
1172 ;;; according to the predicates CLOSED (e.g. >=) and OPEN (e.g. >).
1173 ;;; This is only meaningful for maximizing like bounds, i.e. upper and
1174 ;;; upper. If MAX-P is true, then we return NIL if X or Y is NIL,
1175 ;;; otherwise we return the other arg.
1176 (defmacro numeric-bound-max (x y closed open max-p)
1179 `(cond ((not ,n-x) ,(if max-p nil n-y))
1180 ((not ,n-y) ,(if max-p nil n-x))
1183 (if (,closed (car ,n-x) (car ,n-y)) ,n-x ,n-y)
1184 (if (,open (car ,n-x) ,n-y) ,n-x ,n-y)))
1187 (if (,open (car ,n-y) ,n-x) ,n-y ,n-x)
1188 (if (,closed ,n-y ,n-x) ,n-y ,n-x))))))
1190 (!define-type-method (number :simple-subtypep) (type1 type2)
1191 (let ((class1 (numeric-type-class type1))
1192 (class2 (numeric-type-class type2))
1193 (complexp2 (numeric-type-complexp type2))
1194 (format2 (numeric-type-format type2))
1195 (low1 (numeric-type-low type1))
1196 (high1 (numeric-type-high type1))
1197 (low2 (numeric-type-low type2))
1198 (high2 (numeric-type-high type2)))
1199 ;; If one is complex and the other isn't, they are disjoint.
1200 (cond ((not (or (eq (numeric-type-complexp type1) complexp2)
1203 ;; If the classes are specified and different, the types are
1204 ;; disjoint unless type2 is rational and type1 is integer.
1205 ((not (or (eq class1 class2)
1207 (and (eq class1 'integer)
1208 (eq class2 'rational))))
1210 ;; If the float formats are specified and different, the types
1212 ((not (or (eq (numeric-type-format type1) format2)
1215 ;; Check the bounds.
1216 ((and (numeric-bound-test low1 low2 >= >)
1217 (numeric-bound-test high1 high2 <= <))
1222 (!define-superclasses number ((generic-number)) !cold-init-forms)
1224 ;;; If the high bound of LOW is adjacent to the low bound of HIGH,
1225 ;;; then return true, otherwise NIL.
1226 (defun numeric-types-adjacent (low high)
1227 (let ((low-bound (numeric-type-high low))
1228 (high-bound (numeric-type-low high)))
1229 (cond ((not (and low-bound high-bound)) nil)
1230 ((and (consp low-bound) (consp high-bound)) nil)
1232 #!-negative-zero-is-not-zero
1233 (let ((low-value (car low-bound)))
1234 (or (eql low-value high-bound)
1235 (and (eql low-value -0f0) (eql high-bound 0f0))
1236 (and (eql low-value 0f0) (eql high-bound -0f0))
1237 (and (eql low-value -0d0) (eql high-bound 0d0))
1238 (and (eql low-value 0d0) (eql high-bound -0d0))))
1239 #!+negative-zero-is-not-zero
1240 (eql (car low-bound) high-bound))
1242 #!-negative-zero-is-not-zero
1243 (let ((high-value (car high-bound)))
1244 (or (eql high-value low-bound)
1245 (and (eql high-value -0f0) (eql low-bound 0f0))
1246 (and (eql high-value 0f0) (eql low-bound -0f0))
1247 (and (eql high-value -0d0) (eql low-bound 0d0))
1248 (and (eql high-value 0d0) (eql low-bound -0d0))))
1249 #!+negative-zero-is-not-zero
1250 (eql (car high-bound) low-bound))
1251 #!+negative-zero-is-not-zero
1252 ((or (and (eql low-bound -0f0) (eql high-bound 0f0))
1253 (and (eql low-bound -0d0) (eql high-bound 0d0))))
1254 ((and (eq (numeric-type-class low) 'integer)
1255 (eq (numeric-type-class high) 'integer))
1256 (eql (1+ low-bound) high-bound))
1260 ;;; Return a numeric type that is a supertype for both TYPE1 and TYPE2.
1262 ;;; ### Note: we give up early to keep from dropping lots of information on
1263 ;;; the floor by returning overly general types.
1264 (!define-type-method (number :simple-union2) (type1 type2)
1265 (declare (type numeric-type type1 type2))
1266 (cond ((csubtypep type1 type2) type2)
1267 ((csubtypep type2 type1) type1)
1269 (let ((class1 (numeric-type-class type1))
1270 (format1 (numeric-type-format type1))
1271 (complexp1 (numeric-type-complexp type1))
1272 (class2 (numeric-type-class type2))
1273 (format2 (numeric-type-format type2))
1274 (complexp2 (numeric-type-complexp type2)))
1275 (when (and (eq class1 class2)
1276 (eq format1 format2)
1277 (eq complexp1 complexp2)
1278 (or (numeric-types-intersect type1 type2)
1279 (numeric-types-adjacent type1 type2)
1280 (numeric-types-adjacent type2 type1)))
1285 :low (numeric-bound-max (numeric-type-low type1)
1286 (numeric-type-low type2)
1288 :high (numeric-bound-max (numeric-type-high type1)
1289 (numeric-type-high type2)
1293 (setf (info :type :kind 'number) :primitive)
1294 (setf (info :type :builtin 'number)
1295 (make-numeric-type :complexp nil)))
1297 (!def-type-translator complex (&optional (spec '*))
1299 (make-numeric-type :complexp :complex)
1300 (let ((type (specifier-type spec)))
1301 (unless (numeric-type-p type)
1302 (error "The component type for COMPLEX is not numeric: ~S" spec))
1303 (when (eq (numeric-type-complexp type) :complex)
1304 (error "The component type for COMPLEX is complex: ~S" spec))
1305 (let ((res (copy-numeric-type type)))
1306 (setf (numeric-type-complexp res) :complex)
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)
1348 (!def-bounded-type float float nil)
1349 (!def-bounded-type real nil nil)
1351 (defmacro !define-float-format (f)
1352 `(!def-bounded-type ,f float ,f))
1354 (!define-float-format short-float)
1355 (!define-float-format single-float)
1356 (!define-float-format double-float)
1357 (!define-float-format long-float)
1359 (defun numeric-types-intersect (type1 type2)
1360 (declare (type numeric-type type1 type2))
1361 (let* ((class1 (numeric-type-class type1))
1362 (class2 (numeric-type-class type2))
1363 (complexp1 (numeric-type-complexp type1))
1364 (complexp2 (numeric-type-complexp type2))
1365 (format1 (numeric-type-format type1))
1366 (format2 (numeric-type-format type2))
1367 (low1 (numeric-type-low type1))
1368 (high1 (numeric-type-high type1))
1369 (low2 (numeric-type-low type2))
1370 (high2 (numeric-type-high type2)))
1371 ;; If one is complex and the other isn't, then they are disjoint.
1372 (cond ((not (or (eq complexp1 complexp2)
1373 (null complexp1) (null complexp2)))
1375 ;; If either type is a float, then the other must either be
1376 ;; specified to be a float or unspecified. Otherwise, they
1378 ((and (eq class1 'float)
1379 (not (member class2 '(float nil)))) nil)
1380 ((and (eq class2 'float)
1381 (not (member class1 '(float nil)))) nil)
1382 ;; If the float formats are specified and different, the
1383 ;; types are disjoint.
1384 ((not (or (eq format1 format2) (null format1) (null format2)))
1387 ;; Check the bounds. This is a bit odd because we must
1388 ;; always have the outer bound of the interval as the
1390 (if (numeric-bound-test high1 high2 <= <)
1391 (or (and (numeric-bound-test low1 low2 >= >)
1392 (numeric-bound-test* low1 high2 <= <))
1393 (and (numeric-bound-test low2 low1 >= >)
1394 (numeric-bound-test* low2 high1 <= <)))
1395 (or (and (numeric-bound-test* low2 high1 <= <)
1396 (numeric-bound-test low2 low1 >= >))
1397 (and (numeric-bound-test high2 high1 <= <)
1398 (numeric-bound-test* high2 low1 >= >))))))))
1400 ;;; Take the numeric bound X and convert it into something that can be
1401 ;;; used as a bound in a numeric type with the specified CLASS and
1402 ;;; FORMAT. If UP-P is true, then we round up as needed, otherwise we
1403 ;;; round down. UP-P true implies that X is a lower bound, i.e. (N) > N.
1405 ;;; This is used by NUMERIC-TYPE-INTERSECTION to mash the bound into
1406 ;;; the appropriate type number. X may only be a float when CLASS is
1409 ;;; ### Note: it is possible for the coercion to a float to overflow
1410 ;;; or underflow. This happens when the bound doesn't fit in the
1411 ;;; specified format. In this case, we should really return the
1412 ;;; appropriate {Most | Least}-{Positive | Negative}-XXX-Float float
1413 ;;; of desired format. But these conditions aren't currently signalled
1414 ;;; in any useful way.
1416 ;;; Also, when converting an open rational bound into a float we
1417 ;;; should probably convert it to a closed bound of the closest float
1418 ;;; in the specified format. KLUDGE: In general, open float bounds are
1419 ;;; screwed up. -- (comment from original CMU CL)
1420 (defun round-numeric-bound (x class format up-p)
1422 (let ((cx (if (consp x) (car x) x)))
1426 (if (and (consp x) (integerp cx))
1427 (if up-p (1+ cx) (1- cx))
1428 (if up-p (ceiling cx) (floor cx))))
1430 (let ((res (if format (coerce cx format) (float cx))))
1431 (if (consp x) (list res) res)))))
1434 ;;; Handle the case of type intersection on two numeric types. We use
1435 ;;; TYPES-INTERSECT to throw out the case of types with no
1436 ;;; intersection. If an attribute in TYPE1 is unspecified, then we use
1437 ;;; TYPE2's attribute, which must be at least as restrictive. If the
1438 ;;; types intersect, then the only attributes that can be specified
1439 ;;; and different are the class and the bounds.
1441 ;;; When the class differs, we use the more restrictive class. The
1442 ;;; only interesting case is RATIONAL/INTEGER, since RATIONAL includes
1445 ;;; We make the result lower (upper) bound the maximum (minimum) of
1446 ;;; the argument lower (upper) bounds. We convert the bounds into the
1447 ;;; appropriate numeric type before maximizing. This avoids possible
1448 ;;; confusion due to mixed-type comparisons (but I think the result is
1450 (!define-type-method (number :simple-intersection2) (type1 type2)
1451 (declare (type numeric-type type1 type2))
1452 (if (numeric-types-intersect type1 type2)
1453 (let* ((class1 (numeric-type-class type1))
1454 (class2 (numeric-type-class type2))
1455 (class (ecase class1
1457 ((integer float) class1)
1458 (rational (if (eq class2 'integer)
1461 (format (or (numeric-type-format type1)
1462 (numeric-type-format type2))))
1466 :complexp (or (numeric-type-complexp type1)
1467 (numeric-type-complexp type2))
1468 :low (numeric-bound-max
1469 (round-numeric-bound (numeric-type-low type1)
1471 (round-numeric-bound (numeric-type-low type2)
1474 :high (numeric-bound-max
1475 (round-numeric-bound (numeric-type-high type1)
1477 (round-numeric-bound (numeric-type-high type2)
1482 ;;; Given two float formats, return the one with more precision. If
1483 ;;; either one is null, return NIL.
1484 (defun float-format-max (f1 f2)
1486 (dolist (f *float-formats* (error "bad float format: ~S" f1))
1487 (when (or (eq f f1) (eq f f2))
1490 ;;; Return the result of an operation on TYPE1 and TYPE2 according to
1491 ;;; the rules of numeric contagion. This is always NUMBER, some float
1492 ;;; format (possibly complex) or RATIONAL. Due to rational
1493 ;;; canonicalization, there isn't much we can do here with integers or
1494 ;;; rational complex numbers.
1496 ;;; If either argument is not a NUMERIC-TYPE, then return NUMBER. This
1497 ;;; is useful mainly for allowing types that are technically numbers,
1498 ;;; but not a NUMERIC-TYPE.
1499 (defun numeric-contagion (type1 type2)
1500 (if (and (numeric-type-p type1) (numeric-type-p type2))
1501 (let ((class1 (numeric-type-class type1))
1502 (class2 (numeric-type-class type2))
1503 (format1 (numeric-type-format type1))
1504 (format2 (numeric-type-format type2))
1505 (complexp1 (numeric-type-complexp type1))
1506 (complexp2 (numeric-type-complexp type2)))
1507 (cond ((or (null complexp1)
1509 (specifier-type 'number))
1513 :format (ecase class2
1514 (float (float-format-max format1 format2))
1515 ((integer rational) format1)
1517 ;; A double-float with any real number is a
1520 (if (eq format1 'double-float)
1523 ;; A long-float with any real number is a
1526 (if (eq format1 'long-float)
1529 :complexp (if (or (eq complexp1 :complex)
1530 (eq complexp2 :complex))
1533 ((eq class2 'float) (numeric-contagion type2 type1))
1534 ((and (eq complexp1 :real) (eq complexp2 :real))
1536 :class (and class1 class2 'rational)
1539 (specifier-type 'number))))
1540 (specifier-type 'number)))
1544 (!define-type-class array)
1546 ;;; What this does depends on the setting of the
1547 ;;; *USE-IMPLEMENTATION-TYPES* switch. If true, return the specialized
1548 ;;; element type, otherwise return the original element type.
1549 (defun specialized-element-type-maybe (type)
1550 (declare (type array-type type))
1551 (if *use-implementation-types*
1552 (array-type-specialized-element-type type)
1553 (array-type-element-type type)))
1555 (!define-type-method (array :simple-=) (type1 type2)
1556 (values (and (equal (array-type-dimensions type1)
1557 (array-type-dimensions type2))
1558 (eq (array-type-complexp type1)
1559 (array-type-complexp type2))
1560 (type= (specialized-element-type-maybe type1)
1561 (specialized-element-type-maybe type2)))
1564 (!define-type-method (array :unparse) (type)
1565 (let ((dims (array-type-dimensions type))
1566 (eltype (type-specifier (array-type-element-type type)))
1567 (complexp (array-type-complexp type)))
1570 (if complexp 'array 'simple-array)
1571 (if complexp `(array ,eltype) `(simple-array ,eltype))))
1572 ((= (length dims) 1)
1574 (if (eq (car dims) '*)
1577 (base-char 'base-string)
1580 (t `(vector ,eltype)))
1582 (bit `(bit-vector ,(car dims)))
1583 (base-char `(base-string ,(car dims)))
1584 (character `(string ,(car dims)))
1585 (t `(vector ,eltype ,(car dims)))))
1586 (if (eq (car dims) '*)
1588 (bit 'simple-bit-vector)
1589 (base-char 'simple-base-string)
1590 (character 'simple-string)
1591 ((t) 'simple-vector)
1592 (t `(simple-array ,eltype (*))))
1594 (bit `(simple-bit-vector ,(car dims)))
1595 (base-char `(simple-base-string ,(car dims)))
1596 (character `(simple-string ,(car dims)))
1597 ((t) `(simple-vector ,(car dims)))
1598 (t `(simple-array ,eltype ,dims))))))
1601 `(array ,eltype ,dims)
1602 `(simple-array ,eltype ,dims))))))
1604 (!define-type-method (array :simple-subtypep) (type1 type2)
1605 (let ((dims1 (array-type-dimensions type1))
1606 (dims2 (array-type-dimensions type2))
1607 (complexp2 (array-type-complexp type2)))
1608 (cond (;; not subtypep unless dimensions are compatible
1609 (not (or (eq dims2 '*)
1610 (and (not (eq dims1 '*))
1611 ;; (sbcl-0.6.4 has trouble figuring out that
1612 ;; DIMS1 and DIMS2 must be lists at this
1613 ;; point, and knowing that is important to
1614 ;; compiling EVERY efficiently.)
1615 (= (length (the list dims1))
1616 (length (the list dims2)))
1617 (every (lambda (x y)
1618 (or (eq y '*) (eql x y)))
1620 (the list dims2)))))
1622 ;; not subtypep unless complexness is compatible
1623 ((not (or (eq complexp2 :maybe)
1624 (eq (array-type-complexp type1) complexp2)))
1626 ;; Since we didn't fail any of the tests above, we win
1627 ;; if the TYPE2 element type is wild.
1628 ((eq (array-type-element-type type2) *wild-type*)
1630 (;; Since we didn't match any of the special cases above, we
1631 ;; can't give a good answer unless both the element types
1632 ;; have been defined.
1633 (or (unknown-type-p (array-type-element-type type1))
1634 (unknown-type-p (array-type-element-type type2)))
1636 (;; Otherwise, the subtype relationship holds iff the
1637 ;; types are equal, and they're equal iff the specialized
1638 ;; element types are identical.
1640 (values (type= (specialized-element-type-maybe type1)
1641 (specialized-element-type-maybe type2))
1644 (!define-superclasses array
1650 (defun array-types-intersect (type1 type2)
1651 (declare (type array-type type1 type2))
1652 (let ((dims1 (array-type-dimensions type1))
1653 (dims2 (array-type-dimensions type2))
1654 (complexp1 (array-type-complexp type1))
1655 (complexp2 (array-type-complexp type2)))
1656 ;; See whether dimensions are compatible.
1657 (cond ((not (or (eq dims1 '*) (eq dims2 '*)
1658 (and (= (length dims1) (length dims2))
1659 (every #'(lambda (x y)
1660 (or (eq x '*) (eq y '*) (= x y)))
1663 ;; See whether complexpness is compatible.
1664 ((not (or (eq complexp1 :maybe)
1665 (eq complexp2 :maybe)
1666 (eq complexp1 complexp2)))
1668 ;; If either element type is wild, then they intersect.
1669 ;; Otherwise, the types must be identical.
1670 ((or (eq (array-type-element-type type1) *wild-type*)
1671 (eq (array-type-element-type type2) *wild-type*)
1672 (type= (specialized-element-type-maybe type1)
1673 (specialized-element-type-maybe type2)))
1679 (!define-type-method (array :simple-intersection2) (type1 type2)
1680 (declare (type array-type type1 type2))
1681 (if (array-types-intersect type1 type2)
1682 (let ((dims1 (array-type-dimensions type1))
1683 (dims2 (array-type-dimensions type2))
1684 (complexp1 (array-type-complexp type1))
1685 (complexp2 (array-type-complexp type2))
1686 (eltype1 (array-type-element-type type1))
1687 (eltype2 (array-type-element-type type2)))
1688 (specialize-array-type
1690 :dimensions (cond ((eq dims1 '*) dims2)
1691 ((eq dims2 '*) dims1)
1693 (mapcar (lambda (x y) (if (eq x '*) y x))
1695 :complexp (if (eq complexp1 :maybe) complexp2 complexp1)
1696 :element-type (if (eq eltype1 *wild-type*) eltype2 eltype1))))
1699 ;;; Check a supplied dimension list to determine whether it is legal,
1700 ;;; and return it in canonical form (as either '* or a list).
1701 (defun canonical-array-dimensions (dims)
1706 (error "Arrays can't have a negative number of dimensions: ~S" dims))
1707 (when (>= dims sb!xc:array-rank-limit)
1708 (error "array type with too many dimensions: ~S" dims))
1709 (make-list dims :initial-element '*))
1711 (when (>= (length dims) sb!xc:array-rank-limit)
1712 (error "array type with too many dimensions: ~S" dims))
1715 (unless (and (integerp dim)
1717 (< dim sb!xc:array-dimension-limit))
1718 (error "bad dimension in array type: ~S" dim))))
1721 (error "Array dimensions is not a list, integer or *:~% ~S" dims))))
1725 (!define-type-class member)
1727 (!define-type-method (member :unparse) (type)
1728 (let ((members (member-type-members type)))
1729 (if (equal members '(nil))
1731 `(member ,@members))))
1733 (!define-type-method (member :simple-subtypep) (type1 type2)
1734 (values (subsetp (member-type-members type1) (member-type-members type2))
1737 (!define-type-method (member :complex-subtypep-arg1) (type1 type2)
1738 (every/type (swapped-args-fun #'ctypep)
1740 (member-type-members type1)))
1742 ;;; We punt if the odd type is enumerable and intersects with the
1743 ;;; MEMBER type. If not enumerable, then it is definitely not a
1744 ;;; subtype of the MEMBER type.
1745 (!define-type-method (member :complex-subtypep-arg2) (type1 type2)
1746 (cond ((not (type-enumerable type1)) (values nil t))
1747 ((types-intersect type1 type2) (values nil nil))
1748 (t (values nil t))))
1750 (!define-type-method (member :simple-intersection2) (type1 type2)
1751 (let ((mem1 (member-type-members type1))
1752 (mem2 (member-type-members type2)))
1753 (cond ((subsetp mem1 mem2) type1)
1754 ((subsetp mem2 mem1) type2)
1756 (let ((res (intersection mem1 mem2)))
1758 (make-member-type :members res)
1761 (!define-type-method (member :complex-intersection2) (type1 type2)
1763 (collect ((members))
1764 (let ((mem2 (member-type-members type2)))
1765 (dolist (member mem2)
1766 (multiple-value-bind (val win) (ctypep member type1)
1768 (return-from punt nil))
1769 (when val (members member))))
1770 (cond ((subsetp mem2 (members)) type2)
1771 ((null (members)) *empty-type*)
1773 (make-member-type :members (members))))))))
1775 ;;; We don't need a :COMPLEX-UNION2, since the only interesting case is
1776 ;;; a union type, and the member/union interaction is handled by the
1777 ;;; union type method.
1778 (!define-type-method (member :simple-union2) (type1 type2)
1779 (let ((mem1 (member-type-members type1))
1780 (mem2 (member-type-members type2)))
1781 (cond ((subsetp mem1 mem2) type2)
1782 ((subsetp mem2 mem1) type1)
1784 (make-member-type :members (union mem1 mem2))))))
1786 (!define-type-method (member :simple-=) (type1 type2)
1787 (let ((mem1 (member-type-members type1))
1788 (mem2 (member-type-members type2)))
1789 (values (and (subsetp mem1 mem2)
1790 (subsetp mem2 mem1))
1793 (!define-type-method (member :complex-=) (type1 type2)
1794 (if (type-enumerable type1)
1795 (multiple-value-bind (val win) (csubtypep type2 type1)
1796 (if (or val (not win))
1801 (!def-type-translator member (&rest members)
1803 (make-member-type :members (remove-duplicates members))
1806 ;;;; intersection types
1808 ;;;; Until version 0.6.10.6, SBCL followed the original CMU CL approach
1809 ;;;; of punting on all AND types, not just the unreasonably complicated
1810 ;;;; ones. The change was motivated by trying to get the KEYWORD type
1811 ;;;; to behave sensibly:
1812 ;;;; ;; reasonable definition
1813 ;;;; (DEFTYPE KEYWORD () '(AND SYMBOL (SATISFIES KEYWORDP)))
1814 ;;;; ;; reasonable behavior
1815 ;;;; (ASSERT (SUBTYPEP 'KEYWORD 'SYMBOL))
1816 ;;;; Without understanding a little about the semantics of AND, we'd
1817 ;;;; get (SUBTYPEP 'KEYWORD 'SYMBOL)=>NIL,NIL and, for entirely
1818 ;;;; parallel reasons, (SUBTYPEP 'RATIO 'NUMBER)=>NIL,NIL. That's
1821 ;;;; We still follow the example of CMU CL to some extent, by punting
1822 ;;;; (to the opaque HAIRY-TYPE) on sufficiently complicated types
1825 (!define-type-class intersection)
1827 ;;; A few intersection types have special names. The others just get
1828 ;;; mechanically unparsed.
1829 (!define-type-method (intersection :unparse) (type)
1830 (declare (type ctype type))
1831 (or (find type '(ratio bignum keyword) :key #'specifier-type :test #'type=)
1832 `(and ,@(mapcar #'type-specifier (intersection-type-types type)))))
1834 ;;; shared machinery for type equality: true if every type in the set
1835 ;;; TYPES1 matches a type in the set TYPES2 and vice versa
1836 (defun type=-set (types1 types2)
1837 (flet (;; true if every type in the set X matches a type in the set Y
1839 (declare (type list x y))
1840 (every (lambda (xelement)
1841 (position xelement y :test #'type=))
1843 (values (and (type<=-set types1 types2)
1844 (type<=-set types2 types1))
1847 ;;; Two intersection types are equal if their subtypes are equal sets.
1849 ;;; FIXME: Might it be better to use
1850 ;;; (AND (SUBTYPEP X Y) (SUBTYPEP Y X))
1851 ;;; instead, since SUBTYPEP is the usual relationship that we care
1852 ;;; most about, so it would be good to leverage any ingenuity there
1853 ;;; in this more obscure method?
1854 (!define-type-method (intersection :simple-=) (type1 type2)
1855 (type=-set (intersection-type-types type1)
1856 (intersection-type-types type2)))
1858 (flet ((intersection-complex-subtypep-arg1 (type1 type2)
1859 (any/type (swapped-args-fun #'csubtypep)
1861 (intersection-type-types type1))))
1862 (!define-type-method (intersection :simple-subtypep) (type1 type2)
1863 (every/type #'intersection-complex-subtypep-arg1
1865 (intersection-type-types type2)))
1866 (!define-type-method (intersection :complex-subtypep-arg1) (type1 type2)
1867 (intersection-complex-subtypep-arg1 type1 type2)))
1869 (!define-type-method (intersection :complex-subtypep-arg2) (type1 type2)
1870 (every/type #'csubtypep type1 (intersection-type-types type2)))
1872 (!def-type-translator and (&whole whole &rest type-specifiers)
1873 (apply #'type-intersection
1874 (mapcar #'specifier-type
1879 (!define-type-class union)
1881 ;;; The LIST type has a special name. Other union types just get
1882 ;;; mechanically unparsed.
1883 (!define-type-method (union :unparse) (type)
1884 (declare (type ctype type))
1885 (if (type= type (specifier-type 'list))
1887 `(or ,@(mapcar #'type-specifier (union-type-types type)))))
1889 ;;; Two union types are equal if their subtypes are equal sets.
1890 (!define-type-method (union :simple-=) (type1 type2)
1891 (type=-set (union-type-types type1)
1892 (union-type-types type2)))
1894 ;;; Similarly, a union type is a subtype of another if every element
1895 ;;; of TYPE1 is a subtype of some element of TYPE2.
1897 ;;; KLUDGE: This definition seems redundant, here in UNION-TYPE and
1898 ;;; similarly in INTERSECTION-TYPE, with the logic in the
1899 ;;; corresponding :COMPLEX-SUBTYPEP-ARG1 and :COMPLEX-SUBTYPEP-ARG2
1900 ;;; methods. Ideally there's probably some way to make the
1901 ;;; :SIMPLE-SUBTYPEP method default to the :COMPLEX-SUBTYPEP-FOO
1902 ;;; methods in such a way that this definition could go away, but I
1903 ;;; don't grok the system well enough to tell whether it's simple to
1904 ;;; arrange this. -- WHN 2000-02-03
1905 (!define-type-method (union :simple-subtypep) (type1 type2)
1906 (dolist (t1 (union-type-types type1) (values t t))
1907 (multiple-value-bind (subtypep validp)
1908 (union-complex-subtypep-arg2 t1 type2)
1910 (return (values nil nil)))
1912 (return (values nil t)))))))
1914 (defun union-complex-subtypep-arg1 (type1 type2)
1915 (every/type (swapped-args-fun #'csubtypep)
1917 (union-type-types type1)))
1918 (!define-type-method (union :complex-subtypep-arg1) (type1 type2)
1919 (union-complex-subtypep-arg1 type1 type2))
1921 (defun union-complex-subtypep-arg2 (type1 type2)
1922 (any/type #'csubtypep type1 (union-type-types type2)))
1923 (!define-type-method (union :complex-subtypep-arg2) (type1 type2)
1924 (union-complex-subtypep-arg2 type1 type2))
1926 (!define-type-method (union :simple-intersection2 :complex-intersection2)
1928 ;; The CSUBTYPEP clauses here let us simplify e.g.
1929 ;; (TYPE-INTERSECTION2 (SPECIFIER-TYPE 'LIST)
1930 ;; (SPECIFIER-TYPE '(OR LIST VECTOR)))
1931 ;; (where LIST is (OR CONS NULL)).
1933 ;; The tests are more or less (CSUBTYPEP TYPE1 TYPE2) and vice
1934 ;; versa, but it's important that we pre-expand them into
1935 ;; specialized operations on individual elements of
1936 ;; UNION-TYPE-TYPES, instead of using the ordinary call to
1937 ;; CSUBTYPEP, in order to avoid possibly invoking any methods which
1938 ;; might in turn invoke (TYPE-INTERSECTION2 TYPE1 TYPE2) and thus
1939 ;; cause infinite recursion.
1940 (cond ((union-complex-subtypep-arg2 type1 type2)
1942 ((union-complex-subtypep-arg1 type2 type1)
1945 (let ((accumulator *empty-type*))
1946 (dolist (t2 (union-type-types type2) accumulator)
1948 (type-union2 accumulator
1949 (type-intersection type1 t2)))
1950 ;; When our result isn't simple any more
1952 ;; (TYPE-UNION2 couldn't find a sufficiently simple
1953 ;; result, so we can't either.)
1955 ;; (A result containing an intersection isn't
1956 ;; sufficiently simple for us. FIXME: Maybe it
1957 ;; should be sufficiently simple for us?
1958 ;; UNION-TYPEs aren't supposed to be nested inside
1959 ;; INTERSECTION-TYPEs, so if we punt with NIL,
1960 ;; we're condemning the expression to become a
1961 ;; HAIRY-TYPE. If it were possible for us to
1962 ;; return an INTERSECTION-TYPE, then the
1963 ;; INTERSECTION-TYPE-TYPES could be merged into
1964 ;; the outer INTERSECTION-TYPE which may be under
1965 ;; construction. E.g. if this function could
1966 ;; return an intersection type, and the calling
1967 ;; functions were smart enough to handle it, then
1968 ;; we could simplify (AND (OR FIXNUM KEYWORD)
1969 ;; SYMBOL) to KEYWORD, even though KEYWORD
1970 ;; is an intersection type.)
1971 (intersection-type-p accumulator))
1974 (!def-type-translator or (&rest type-specifiers)
1976 (mapcar #'specifier-type
1981 (!define-type-class cons)
1983 (!def-type-translator cons (&optional (car-type-spec '*) (cdr-type-spec '*))
1984 (make-cons-type (specifier-type car-type-spec)
1985 (specifier-type cdr-type-spec)))
1987 (!define-type-method (cons :unparse) (type)
1988 (let ((car-eltype (type-specifier (cons-type-car-type type)))
1989 (cdr-eltype (type-specifier (cons-type-cdr-type type))))
1990 (if (and (member car-eltype '(t *))
1991 (member cdr-eltype '(t *)))
1993 `(cons ,car-eltype ,cdr-eltype))))
1995 (!define-type-method (cons :simple-=) (type1 type2)
1996 (declare (type cons-type type1 type2))
1997 (and (type= (cons-type-car-type type1) (cons-type-car-type type2))
1998 (type= (cons-type-cdr-type type1) (cons-type-cdr-type type2))))
2000 (!define-type-method (cons :simple-subtypep) (type1 type2)
2001 (declare (type cons-type type1 type2))
2002 (multiple-value-bind (val-car win-car)
2003 (csubtypep (cons-type-car-type type1) (cons-type-car-type type2))
2004 (multiple-value-bind (val-cdr win-cdr)
2005 (csubtypep (cons-type-cdr-type type1) (cons-type-cdr-type type2))
2006 (if (and val-car val-cdr)
2007 (values t (and win-car win-cdr))
2008 (values nil (or win-car win-cdr))))))
2010 ;;; Give up if a precise type is not possible, to avoid returning
2011 ;;; overly general types.
2012 (!define-type-method (cons :simple-union2) (type1 type2)
2013 (declare (type cons-type type1 type2))
2014 (let ((car-type1 (cons-type-car-type type1))
2015 (car-type2 (cons-type-car-type type2))
2016 (cdr-type1 (cons-type-cdr-type type1))
2017 (cdr-type2 (cons-type-cdr-type type2)))
2018 (cond ((type= car-type1 car-type2)
2019 (make-cons-type car-type1
2020 (type-union cdr-type1 cdr-type2)))
2021 ((type= cdr-type1 cdr-type2)
2022 (make-cons-type (type-union cdr-type1 cdr-type2)
2025 (!define-type-method (cons :simple-intersection2) (type1 type2)
2026 (declare (type cons-type type1 type2))
2029 (and (setf car-int2 (type-intersection2 (cons-type-car-type type1)
2030 (cons-type-car-type type2)))
2031 (setf cdr-int2 (type-intersection2 (cons-type-cdr-type type1)
2032 (cons-type-cdr-type type2)))
2033 (make-cons-type car-int2 cdr-int2))))
2035 ;;; Return the type that describes all objects that are in X but not
2036 ;;; in Y. If we can't determine this type, then return NIL.
2038 ;;; For now, we only are clever dealing with union and member types.
2039 ;;; If either type is not a union type, then we pretend that it is a
2040 ;;; union of just one type. What we do is remove from X all the types
2041 ;;; that are a subtype any type in Y. If any type in X intersects with
2042 ;;; a type in Y but is not a subtype, then we give up.
2044 ;;; We must also special-case any member type that appears in the
2045 ;;; union. We remove from X's members all objects that are TYPEP to Y.
2046 ;;; If Y has any members, we must be careful that none of those
2047 ;;; members are CTYPEP to any of Y's non-member types. We give up in
2048 ;;; this case, since to compute that difference we would have to break
2049 ;;; the type from X into some collection of types that represents the
2050 ;;; type without that particular element. This seems too hairy to be
2051 ;;; worthwhile, given its low utility.
2052 (defun type-difference (x y)
2053 (let ((x-types (if (union-type-p x) (union-type-types x) (list x)))
2054 (y-types (if (union-type-p y) (union-type-types y) (list y))))
2056 (dolist (x-type x-types)
2057 (if (member-type-p x-type)
2058 (collect ((members))
2059 (dolist (mem (member-type-members x-type))
2060 (multiple-value-bind (val win) (ctypep mem y)
2061 (unless win (return-from type-difference nil))
2065 (res (make-member-type :members (members)))))
2066 (dolist (y-type y-types (res x-type))
2067 (multiple-value-bind (val win) (csubtypep x-type y-type)
2068 (unless win (return-from type-difference nil))
2070 (when (types-intersect x-type y-type)
2071 (return-from type-difference nil))))))
2072 (let ((y-mem (find-if #'member-type-p y-types)))
2074 (let ((members (member-type-members y-mem)))
2075 (dolist (x-type x-types)
2076 (unless (member-type-p x-type)
2077 (dolist (member members)
2078 (multiple-value-bind (val win) (ctypep member x-type)
2079 (when (or (not win) val)
2080 (return-from type-difference nil)))))))))
2081 (apply #'type-union (res)))))
2083 (!def-type-translator array (&optional (element-type '*)
2085 (specialize-array-type
2086 (make-array-type :dimensions (canonical-array-dimensions dimensions)
2087 :element-type (specifier-type element-type))))
2089 (!def-type-translator simple-array (&optional (element-type '*)
2091 (specialize-array-type
2092 (make-array-type :dimensions (canonical-array-dimensions dimensions)
2093 :element-type (specifier-type element-type)
2096 (!defun-from-collected-cold-init-forms !late-type-cold-init)
2098 (/show0 "late-type.lisp end of file")