1 ;;;; This file contains the definition of non-CLASS types (e.g.
2 ;;;; subtypes of interesting BUILT-IN-CLASSes) and the interfaces to
3 ;;;; the type system. Common Lisp type specifiers are parsed into a
4 ;;;; somewhat canonical internal type representation that supports
5 ;;;; type union, intersection, etc. (Except that ALIEN types have
8 ;;;; This software is part of the SBCL system. See the README file for
11 ;;;; This software is derived from the CMU CL system, which was
12 ;;;; written at Carnegie Mellon University and released into the
13 ;;;; public domain. The software is in the public domain and is
14 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
15 ;;;; files for more information.
17 (in-package "SB!KERNEL")
19 (/show0 "late-type.lisp 19")
21 (!begin-collecting-cold-init-forms)
23 ;;; ### Remaining incorrectnesses:
25 ;;; There are all sorts of nasty problems with open bounds on FLOAT
26 ;;; types (and probably FLOAT types in general.)
28 ;;; FIXME: This really should go away. Alas, it doesn't seem to be so
29 ;;; simple to make it go away.. (See bug 123 in BUGS file.)
30 (defvar *use-implementation-types* t ; actually initialized in cold init
32 "*USE-IMPLEMENTATION-TYPES* is a semi-public flag which determines how
33 restrictive we are in determining type membership. If two types are the
34 same in the implementation, then we will consider them them the same when
35 this switch is on. When it is off, we try to be as restrictive as the
36 language allows, allowing us to detect more errors. Currently, this only
37 affects array types.")
38 (!cold-init-forms (setq *use-implementation-types* t))
40 ;;; These functions are used as method for types which need a complex
41 ;;; subtypep method to handle some superclasses, but cover a subtree
42 ;;; of the type graph (i.e. there is no simple way for any other type
43 ;;; class to be a subtype.) There are always still complex ways,
44 ;;; namely UNION and MEMBER types, so we must give TYPE1's method a
45 ;;; chance to run, instead of immediately returning NIL, T.
46 (defun delegate-complex-subtypep-arg2 (type1 type2)
48 (type-class-complex-subtypep-arg1
49 (type-class-info type1))))
51 (funcall subtypep-arg1 type1 type2)
53 (defun delegate-complex-intersection2 (type1 type2)
54 (let ((method (type-class-complex-intersection2 (type-class-info type1))))
55 (if (and method (not (eq method #'delegate-complex-intersection2)))
56 (funcall method type2 type1)
57 (hierarchical-intersection2 type1 type2))))
59 ;;; This is used by !DEFINE-SUPERCLASSES to define the SUBTYPE-ARG1
60 ;;; method. INFO is a list of conses
61 ;;; (SUPERCLASS-CLASS . {GUARD-TYPE-SPECIFIER | NIL}).
62 (defun !has-superclasses-complex-subtypep-arg1 (type1 type2 info)
63 ;; If TYPE2 might be concealing something related to our class
65 (if (type-might-contain-other-types-p type2)
66 ;; too confusing, gotta punt
68 ;; ordinary case expected by old CMU CL code, where the taxonomy
69 ;; of TYPE2's representation accurately reflects the taxonomy of
72 ;; FIXME: This old CMU CL code probably deserves a comment
73 ;; explaining to us mere mortals how it works...
74 (and (sb!xc:typep type2 'classoid)
76 (when (or (not (cdr x))
77 (csubtypep type1 (specifier-type (cdr x))))
79 (or (eq type2 (car x))
80 (let ((inherits (layout-inherits
81 (classoid-layout (car x)))))
82 (dotimes (i (length inherits) nil)
83 (when (eq type2 (layout-classoid (svref inherits i)))
87 ;;; This function takes a list of specs, each of the form
88 ;;; (SUPERCLASS-NAME &OPTIONAL GUARD).
89 ;;; Consider one spec (with no guard): any instance of the named
90 ;;; TYPE-CLASS is also a subtype of the named superclass and of any of
91 ;;; its superclasses. If there are multiple specs, then some will have
92 ;;; guards. We choose the first spec whose guard is a supertype of
93 ;;; TYPE1 and use its superclass. In effect, a sequence of guards
96 ;;; G0,(and G1 (not G0)), (and G2 (not (or G0 G1))).
98 ;;; WHEN controls when the forms are executed.
99 (defmacro !define-superclasses (type-class-name specs when)
100 (with-unique-names (type-class info)
102 (let ((,type-class (type-class-or-lose ',type-class-name))
103 (,info (mapcar (lambda (spec)
105 (super &optional guard)
107 (cons (find-classoid super) guard)))
109 (setf (type-class-complex-subtypep-arg1 ,type-class)
110 (lambda (type1 type2)
111 (!has-superclasses-complex-subtypep-arg1 type1 type2 ,info)))
112 (setf (type-class-complex-subtypep-arg2 ,type-class)
113 #'delegate-complex-subtypep-arg2)
114 (setf (type-class-complex-intersection2 ,type-class)
115 #'delegate-complex-intersection2)))))
117 ;;;; FUNCTION and VALUES types
119 ;;;; Pretty much all of the general type operations are illegal on
120 ;;;; VALUES types, since we can't discriminate using them, do
121 ;;;; SUBTYPEP, etc. FUNCTION types are acceptable to the normal type
122 ;;;; operations, but are generally considered to be equivalent to
123 ;;;; FUNCTION. These really aren't true types in any type theoretic
124 ;;;; sense, but we still parse them into CTYPE structures for two
127 ;;;; -- Parsing and unparsing work the same way, and indeed we can't
128 ;;;; tell whether a type is a function or values type without
130 ;;;; -- Many of the places that can be annotated with real types can
131 ;;;; also be annotated with function or values types.
133 ;;; the description of a &KEY argument
134 (defstruct (key-info #-sb-xc-host (:pure t)
136 ;; the key (not necessarily a keyword in ANSI Common Lisp)
137 (name (missing-arg) :type symbol)
138 ;; the type of the argument value
139 (type (missing-arg) :type ctype))
141 (!define-type-method (values :simple-subtypep :complex-subtypep-arg1)
143 (declare (ignore type2))
144 ;; FIXME: should be TYPE-ERROR, here and in next method
145 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type1)))
147 (!define-type-method (values :complex-subtypep-arg2)
149 (declare (ignore type1))
150 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type2)))
152 (!define-type-method (values :unparse) (type)
154 (let ((unparsed (unparse-args-types type)))
155 (if (or (values-type-optional type)
156 (values-type-rest type)
157 (values-type-allowp type))
159 (nconc unparsed '(&optional))))))
161 ;;; Return true if LIST1 and LIST2 have the same elements in the same
162 ;;; positions according to TYPE=. We return NIL, NIL if there is an
163 ;;; uncertain comparison.
164 (defun type=-list (list1 list2)
165 (declare (list list1 list2))
166 (do ((types1 list1 (cdr types1))
167 (types2 list2 (cdr types2)))
168 ((or (null types1) (null types2))
169 (if (or types1 types2)
172 (multiple-value-bind (val win)
173 (type= (first types1) (first types2))
175 (return (values nil nil)))
177 (return (values nil t))))))
179 (!define-type-method (values :simple-=) (type1 type2)
180 (let ((rest1 (args-type-rest type1))
181 (rest2 (args-type-rest type2)))
182 (cond ((and rest1 rest2 (type/= rest1 rest2))
187 (multiple-value-bind (req-val req-win)
188 (type=-list (values-type-required type1)
189 (values-type-required type2))
190 (multiple-value-bind (opt-val opt-win)
191 (type=-list (values-type-optional type1)
192 (values-type-optional type2))
193 (values (and req-val opt-val) (and req-win opt-win))))))))
195 (!define-type-class function)
197 ;;; a flag that we can bind to cause complex function types to be
198 ;;; unparsed as FUNCTION. This is useful when we want a type that we
199 ;;; can pass to TYPEP.
200 (defvar *unparse-fun-type-simplify*)
201 (!cold-init-forms (setq *unparse-fun-type-simplify* nil))
203 (!define-type-method (function :unparse) (type)
204 (if *unparse-fun-type-simplify*
207 (if (fun-type-wild-args type)
209 (unparse-args-types type))
211 (fun-type-returns type)))))
213 ;;; The meaning of this is a little confused. On the one hand, all
214 ;;; function objects are represented the same way regardless of the
215 ;;; arglists and return values, and apps don't get to ask things like
216 ;;; (TYPEP #'FOO (FUNCTION (FIXNUM) *)) in any meaningful way. On the
217 ;;; other hand, Python wants to reason about function types. So...
218 (!define-type-method (function :simple-subtypep) (type1 type2)
219 (flet ((fun-type-simple-p (type)
220 (not (or (fun-type-rest type)
221 (fun-type-keyp type))))
222 (every-csubtypep (types1 types2)
226 do (multiple-value-bind (res sure-p)
228 (unless res (return (values res sure-p))))
229 finally (return (values t t)))))
230 (and/type (values-subtypep (fun-type-returns type1)
231 (fun-type-returns type2))
232 (cond ((fun-type-wild-args type2) (values t t))
233 ((fun-type-wild-args type1)
234 (cond ((fun-type-keyp type2) (values nil nil))
235 ((not (fun-type-rest type2)) (values nil t))
236 ((not (null (fun-type-required type2))) (values nil t))
237 (t (and/type (type= *universal-type* (fun-type-rest type2))
238 (every/type #'type= *universal-type*
239 (fun-type-optional type2))))))
240 ((not (and (fun-type-simple-p type1)
241 (fun-type-simple-p type2)))
243 (t (multiple-value-bind (min1 max1) (fun-type-nargs type1)
244 (multiple-value-bind (min2 max2) (fun-type-nargs type2)
245 (cond ((or (> max1 max2) (< min1 min2))
247 ((and (= min1 min2) (= max1 max2))
248 (and/type (every-csubtypep (fun-type-required type1)
249 (fun-type-required type2))
250 (every-csubtypep (fun-type-optional type1)
251 (fun-type-optional type2))))
254 (fun-type-required type1)
255 (fun-type-optional type1))
257 (fun-type-required type2)
258 (fun-type-optional type2))))))))))))
260 (!define-superclasses function ((function)) !cold-init-forms)
262 ;;; The union or intersection of two FUNCTION types is FUNCTION.
263 (!define-type-method (function :simple-union2) (type1 type2)
264 (declare (ignore type1 type2))
265 (specifier-type 'function))
266 (!define-type-method (function :simple-intersection2) (type1 type2)
267 (declare (ignore type1 type2))
268 (specifier-type 'function))
270 ;;; The union or intersection of a subclass of FUNCTION with a
271 ;;; FUNCTION type is somewhat complicated.
272 (!define-type-method (function :complex-intersection2) (type1 type2)
274 ((type= type1 (specifier-type 'function)) type2)
275 ((csubtypep type1 (specifier-type 'function)) nil)
276 (t :call-other-method)))
277 (!define-type-method (function :complex-union2) (type1 type2)
279 ((type= type1 (specifier-type 'function)) type1)
282 (!define-type-method (function :simple-=) (type1 type2)
283 (macrolet ((compare (comparator field)
284 (let ((reader (symbolicate '#:fun-type- field)))
285 `(,comparator (,reader type1) (,reader type2)))))
286 (and/type (compare type= returns)
287 (cond ((neq (fun-type-wild-args type1) (fun-type-wild-args type2))
289 ((eq (fun-type-wild-args type1) t)
292 (cond ((null (fun-type-rest type1))
293 (values (null (fun-type-rest type2)) t))
294 ((null (fun-type-rest type2))
297 (compare type= rest)))
298 (labels ((type-list-= (l1 l2)
300 (values (null l2) t))
303 (t (multiple-value-bind (res winp)
304 (type= (first l1) (first l2))
310 (type-list-= (rest l1)
312 (and/type (and/type (compare type-list-= required)
313 (compare type-list-= optional))
314 (if (or (fun-type-keyp type1) (fun-type-keyp type2))
316 (values t t))))))))))
318 (!define-type-class constant :inherits values)
320 (!define-type-method (constant :unparse) (type)
321 `(constant-arg ,(type-specifier (constant-type-type type))))
323 (!define-type-method (constant :simple-=) (type1 type2)
324 (type= (constant-type-type type1) (constant-type-type type2)))
326 (!def-type-translator constant-arg (type)
327 (make-constant-type :type (single-value-specifier-type type)))
329 ;;; Return the lambda-list-like type specification corresponding
331 (declaim (ftype (function (args-type) list) unparse-args-types))
332 (defun unparse-args-types (type)
335 (dolist (arg (args-type-required type))
336 (result (type-specifier arg)))
338 (when (args-type-optional type)
340 (dolist (arg (args-type-optional type))
341 (result (type-specifier arg))))
343 (when (args-type-rest type)
345 (result (type-specifier (args-type-rest type))))
347 (when (args-type-keyp type)
349 (dolist (key (args-type-keywords type))
350 (result (list (key-info-name key)
351 (type-specifier (key-info-type key))))))
353 (when (args-type-allowp type)
354 (result '&allow-other-keys))
358 (!def-type-translator function (&optional (args '*) (result '*))
359 (make-fun-type :args args
360 :returns (coerce-to-values (values-specifier-type result))))
362 (!def-type-translator values (&rest values)
363 (make-values-type :args values))
365 ;;;; VALUES types interfaces
367 ;;;; We provide a few special operations that can be meaningfully used
368 ;;;; on VALUES types (as well as on any other type).
370 (defun type-single-value-p (type)
371 (and (values-type-p type)
372 (not (values-type-rest type))
373 (null (values-type-optional type))
374 (singleton-p (values-type-required type))))
376 ;;; Return the type of the first value indicated by TYPE. This is used
377 ;;; by people who don't want to have to deal with VALUES types.
378 #!-sb-fluid (declaim (freeze-type values-type))
379 ; (inline single-value-type))
380 (defun single-value-type (type)
381 (declare (type ctype type))
382 (cond ((eq type *wild-type*)
384 ((eq type *empty-type*)
386 ((not (values-type-p type))
388 (t (or (car (args-type-required type))
389 (car (args-type-optional type))
390 (args-type-rest type)
391 (specifier-type 'null)))))
393 ;;; Return the minimum number of arguments that a function can be
394 ;;; called with, and the maximum number or NIL. If not a function
395 ;;; type, return NIL, NIL.
396 (defun fun-type-nargs (type)
397 (declare (type ctype type))
398 (if (and (fun-type-p type) (not (fun-type-wild-args type)))
399 (let ((fixed (length (args-type-required type))))
400 (if (or (args-type-rest type)
401 (args-type-keyp type)
402 (args-type-allowp type))
404 (values fixed (+ fixed (length (args-type-optional type))))))
407 ;;; Determine whether TYPE corresponds to a definite number of values.
408 ;;; The first value is a list of the types for each value, and the
409 ;;; second value is the number of values. If the number of values is
410 ;;; not fixed, then return NIL and :UNKNOWN.
411 (defun values-types (type)
412 (declare (type ctype type))
413 (cond ((or (eq type *wild-type*) (eq type *empty-type*))
414 (values nil :unknown))
415 ((or (args-type-optional type)
416 (args-type-rest type))
417 (values nil :unknown))
419 (let ((req (args-type-required type)))
420 (values req (length req))))))
422 ;;; Return two values:
423 ;;; 1. A list of all the positional (fixed and optional) types.
424 ;;; 2. The &REST type (if any). If no &REST, then the DEFAULT-TYPE.
425 (defun values-type-types (type &optional (default-type *empty-type*))
426 (declare (type ctype type))
427 (if (eq type *wild-type*)
428 (values nil *universal-type*)
429 (values (append (args-type-required type)
430 (args-type-optional type))
431 (cond ((args-type-rest type))
434 ;;; If COUNT values are supplied, which types should they have?
435 (defun values-type-start (type count)
436 (declare (type ctype type) (type unsigned-byte count))
437 (if (eq type *wild-type*)
438 (make-list count :initial-element *universal-type*)
440 (flet ((process-types (types)
441 (loop for type in types
445 (process-types (values-type-required type))
446 (process-types (values-type-optional type))
448 (loop with rest = (the ctype (values-type-rest type))
453 ;;; Return a list of OPERATION applied to the types in TYPES1 and
454 ;;; TYPES2, padding with REST2 as needed. TYPES1 must not be shorter
455 ;;; than TYPES2. The second value is T if OPERATION always returned a
456 ;;; true second value.
457 (defun fixed-values-op (types1 types2 rest2 operation)
458 (declare (list types1 types2) (type ctype rest2) (type function operation))
460 (values (mapcar (lambda (t1 t2)
461 (multiple-value-bind (res win)
462 (funcall operation t1 t2)
468 (make-list (- (length types1) (length types2))
469 :initial-element rest2)))
472 ;;; If TYPE isn't a values type, then make it into one.
473 (defun-cached (%coerce-to-values
475 :hash-function (lambda (type)
476 (logand (type-hash-value type)
479 (cond ((multiple-value-bind (res sure)
480 (csubtypep (specifier-type 'null) type)
481 (and (not res) sure))
482 ;; FIXME: What should we do with (NOT SURE)?
483 (make-values-type :required (list type) :rest *universal-type*))
485 (make-values-type :optional (list type) :rest *universal-type*))))
487 (defun coerce-to-values (type)
488 (declare (type ctype type))
489 (cond ((or (eq type *universal-type*)
490 (eq type *wild-type*))
492 ((values-type-p type)
494 (t (%coerce-to-values type))))
496 ;;; Return type, corresponding to ANSI short form of VALUES type
498 (defun make-short-values-type (types)
499 (declare (list types))
500 (let ((last-required (position-if
502 (not/type (csubtypep (specifier-type 'null) type)))
506 (make-values-type :required (subseq types 0 (1+ last-required))
507 :optional (subseq types (1+ last-required))
508 :rest *universal-type*)
509 (make-values-type :optional types :rest *universal-type*))))
511 (defun make-single-value-type (type)
512 (make-values-type :required (list type)))
514 ;;; Do the specified OPERATION on TYPE1 and TYPE2, which may be any
515 ;;; type, including VALUES types. With VALUES types such as:
518 ;;; we compute the more useful result
519 ;;; (VALUES (<operation> a0 b0) (<operation> a1 b1))
520 ;;; rather than the precise result
521 ;;; (<operation> (values a0 a1) (values b0 b1))
522 ;;; This has the virtue of always keeping the VALUES type specifier
523 ;;; outermost, and retains all of the information that is really
524 ;;; useful for static type analysis. We want to know what is always
525 ;;; true of each value independently. It is worthless to know that if
526 ;;; the first value is B0 then the second will be B1.
528 ;;; If the VALUES count signatures differ, then we produce a result with
529 ;;; the required VALUE count chosen by NREQ when applied to the number
530 ;;; of required values in TYPE1 and TYPE2. Any &KEY values become
531 ;;; &REST T (anyone who uses keyword values deserves to lose.)
533 ;;; The second value is true if the result is definitely empty or if
534 ;;; OPERATION returned true as its second value each time we called
535 ;;; it. Since we approximate the intersection of VALUES types, the
536 ;;; second value being true doesn't mean the result is exact.
537 (defun args-type-op (type1 type2 operation nreq)
538 (declare (type ctype type1 type2)
539 (type function operation nreq))
540 (when (eq type1 type2)
542 (multiple-value-bind (types1 rest1)
543 (values-type-types type1)
544 (multiple-value-bind (types2 rest2)
545 (values-type-types type2)
546 (multiple-value-bind (rest rest-exact)
547 (funcall operation rest1 rest2)
548 (multiple-value-bind (res res-exact)
549 (if (< (length types1) (length types2))
550 (fixed-values-op types2 types1 rest1 operation)
551 (fixed-values-op types1 types2 rest2 operation))
552 (let* ((req (funcall nreq
553 (length (args-type-required type1))
554 (length (args-type-required type2))))
555 (required (subseq res 0 req))
556 (opt (subseq res req)))
557 (values (make-values-type
561 (and rest-exact res-exact))))))))
563 ;;; Do a union or intersection operation on types that might be values
564 ;;; types. The result is optimized for utility rather than exactness,
565 ;;; but it is guaranteed that it will be no smaller (more restrictive)
566 ;;; than the precise result.
568 ;;; The return convention seems to be analogous to
569 ;;; TYPES-EQUAL-OR-INTERSECT. -- WHN 19990910.
570 (defun-cached (values-type-union :hash-function type-cache-hash
573 :init-wrapper !cold-init-forms)
574 ((type1 eq) (type2 eq))
575 (declare (type ctype type1 type2))
576 (cond ((or (eq type1 *wild-type*) (eq type2 *wild-type*)) *wild-type*)
577 ((eq type1 *empty-type*) type2)
578 ((eq type2 *empty-type*) type1)
580 (values (args-type-op type1 type2 #'type-union #'min)))))
582 (defun-cached (values-type-intersection :hash-function type-cache-hash
585 :default (values nil :empty)
586 :init-wrapper !cold-init-forms)
587 ((type1 eq) (type2 eq))
588 (declare (type ctype type1 type2))
589 (cond ((eq type1 *wild-type*) (values (coerce-to-values type2) t))
590 ((or (eq type2 *wild-type*) (eq type2 *universal-type*))
592 ((or (eq type1 *empty-type*) (eq type2 *empty-type*))
594 ((and (not (values-type-p type2))
595 (values-type-required type1))
596 (let ((req1 (values-type-required type1)))
597 (make-values-type :required (cons (type-intersection (first req1) type2)
599 :optional (values-type-optional type1)
600 :rest (values-type-rest type1)
601 :allowp (values-type-allowp type1))))
603 (args-type-op type1 (coerce-to-values type2)
607 ;;; This is like TYPES-EQUAL-OR-INTERSECT, except that it sort of
608 ;;; works on VALUES types. Note that due to the semantics of
609 ;;; VALUES-TYPE-INTERSECTION, this might return (VALUES T T) when
610 ;;; there isn't really any intersection.
611 (defun values-types-equal-or-intersect (type1 type2)
612 (cond ((or (eq type1 *empty-type*) (eq type2 *empty-type*))
614 ((or (eq type1 *wild-type*) (eq type2 *wild-type*))
617 (multiple-value-bind (res win) (values-type-intersection type1 type2)
618 (values (not (eq res *empty-type*))
621 ;;; a SUBTYPEP-like operation that can be used on any types, including
623 (defun-cached (values-subtypep :hash-function type-cache-hash
626 :default (values nil :empty)
627 :init-wrapper !cold-init-forms)
628 ((type1 eq) (type2 eq))
629 (declare (type ctype type1 type2))
630 (cond ((or (eq type2 *wild-type*) (eq type2 *universal-type*)
631 (eq type1 *empty-type*))
633 ((eq type1 *wild-type*)
634 (values (eq type2 *wild-type*) t))
635 ((or (eq type2 *empty-type*)
636 (not (values-types-equal-or-intersect type1 type2)))
638 ((and (not (values-type-p type2))
639 (values-type-required type1))
640 (csubtypep (first (values-type-required type1))
642 (t (setq type2 (coerce-to-values type2))
643 (multiple-value-bind (types1 rest1) (values-type-types type1)
644 (multiple-value-bind (types2 rest2) (values-type-types type2)
645 (cond ((< (length (values-type-required type1))
646 (length (values-type-required type2)))
648 ((< (length types1) (length types2))
651 (do ((t1 types1 (rest t1))
652 (t2 types2 (rest t2)))
654 (csubtypep rest1 rest2))
655 (multiple-value-bind (res win-p)
656 (csubtypep (first t1) (first t2))
658 (return (values nil nil)))
660 (return (values nil t))))))))))))
662 ;;;; type method interfaces
664 ;;; like SUBTYPEP, only works on CTYPE structures
665 (defun-cached (csubtypep :hash-function type-cache-hash
668 :default (values nil :empty)
669 :init-wrapper !cold-init-forms)
670 ((type1 eq) (type2 eq))
671 (declare (type ctype type1 type2))
672 (cond ((or (eq type1 type2)
673 (eq type1 *empty-type*)
674 (eq type2 *universal-type*))
677 ((eq type1 *universal-type*)
680 (!invoke-type-method :simple-subtypep :complex-subtypep-arg2
682 :complex-arg1 :complex-subtypep-arg1))))
684 ;;; Just parse the type specifiers and call CSUBTYPE.
685 (defun sb!xc:subtypep (type1 type2 &optional environment)
687 "Return two values indicating the relationship between type1 and type2.
688 If values are T and T, type1 definitely is a subtype of type2.
689 If values are NIL and T, type1 definitely is not a subtype of type2.
690 If values are NIL and NIL, it couldn't be determined."
691 (declare (ignore environment))
692 (csubtypep (specifier-type type1) (specifier-type type2)))
694 ;;; If two types are definitely equivalent, return true. The second
695 ;;; value indicates whether the first value is definitely correct.
696 ;;; This should only fail in the presence of HAIRY types.
697 (defun-cached (type= :hash-function type-cache-hash
700 :default (values nil :empty)
701 :init-wrapper !cold-init-forms)
702 ((type1 eq) (type2 eq))
703 (declare (type ctype type1 type2))
706 (!invoke-type-method :simple-= :complex-= type1 type2)))
708 ;;; Not exactly the negation of TYPE=, since when the relationship is
709 ;;; uncertain, we still return NIL, NIL. This is useful in cases where
710 ;;; the conservative assumption is =.
711 (defun type/= (type1 type2)
712 (declare (type ctype type1 type2))
713 (multiple-value-bind (res win) (type= type1 type2)
718 ;;; the type method dispatch case of TYPE-UNION2
719 (defun %type-union2 (type1 type2)
720 ;; As in %TYPE-INTERSECTION2, it seems to be a good idea to give
721 ;; both argument orders a chance at COMPLEX-INTERSECTION2. Unlike
722 ;; %TYPE-INTERSECTION2, though, I don't have a specific case which
723 ;; demonstrates this is actually necessary. Also unlike
724 ;; %TYPE-INTERSECTION2, there seems to be no need to distinguish
725 ;; between not finding a method and having a method return NIL.
727 (!invoke-type-method :simple-union2 :complex-union2
730 (declare (inline 1way))
731 (or (1way type1 type2)
732 (1way type2 type1))))
734 ;;; Find a type which includes both types. Any inexactness is
735 ;;; represented by the fuzzy element types; we return a single value
736 ;;; that is precise to the best of our knowledge. This result is
737 ;;; simplified into the canonical form, thus is not a UNION-TYPE
738 ;;; unless we find no other way to represent the result.
739 (defun-cached (type-union2 :hash-function type-cache-hash
741 :init-wrapper !cold-init-forms)
742 ((type1 eq) (type2 eq))
743 ;; KLUDGE: This was generated from TYPE-INTERSECTION2 by Ye Olde Cut And
744 ;; Paste technique of programming. If it stays around (as opposed to
745 ;; e.g. fading away in favor of some CLOS solution) the shared logic
746 ;; should probably become shared code. -- WHN 2001-03-16
747 (declare (type ctype type1 type2))
748 (cond ((eq type1 type2)
750 ((csubtypep type1 type2) type2)
751 ((csubtypep type2 type1) type1)
752 ((or (union-type-p type1)
753 (union-type-p type2))
754 ;; Unions of UNION-TYPE should have the UNION-TYPE-TYPES
755 ;; values broken out and united separately. The full TYPE-UNION
756 ;; function knows how to do this, so let it handle it.
757 (type-union type1 type2))
759 ;; the ordinary case: we dispatch to type methods
760 (%type-union2 type1 type2))))
762 ;;; the type method dispatch case of TYPE-INTERSECTION2
763 (defun %type-intersection2 (type1 type2)
764 ;; We want to give both argument orders a chance at
765 ;; COMPLEX-INTERSECTION2. Without that, the old CMU CL type
766 ;; methods could give noncommutative results, e.g.
767 ;; (TYPE-INTERSECTION2 *EMPTY-TYPE* SOME-HAIRY-TYPE)
769 ;; (TYPE-INTERSECTION2 SOME-HAIRY-TYPE *EMPTY-TYPE*)
770 ;; => #<NAMED-TYPE NIL>, T
771 ;; We also need to distinguish between the case where we found a
772 ;; type method, and it returned NIL, and the case where we fell
773 ;; through without finding any type method. An example of the first
774 ;; case is the intersection of a HAIRY-TYPE with some ordinary type.
775 ;; An example of the second case is the intersection of two
776 ;; completely-unrelated types, e.g. CONS and NUMBER, or SYMBOL and
779 ;; (Why yes, CLOS probably *would* be nicer..)
781 (!invoke-type-method :simple-intersection2 :complex-intersection2
783 :default :call-other-method)))
784 (declare (inline 1way))
785 (let ((xy (1way type1 type2)))
786 (or (and (not (eql xy :call-other-method)) xy)
787 (let ((yx (1way type2 type1)))
788 (or (and (not (eql yx :call-other-method)) yx)
789 (cond ((and (eql xy :call-other-method)
790 (eql yx :call-other-method))
793 (aver (and (not xy) (not yx))) ; else handled above
796 (defun-cached (type-intersection2 :hash-function type-cache-hash
800 :init-wrapper !cold-init-forms)
801 ((type1 eq) (type2 eq))
802 (declare (type ctype type1 type2))
803 (cond ((eq type1 type2)
804 ;; FIXME: For some reason, this doesn't catch e.g. type1 =
805 ;; type2 = (SPECIFIER-TYPE
806 ;; 'SOME-UNKNOWN-TYPE). Investigate. - CSR, 2002-04-10
808 ((or (intersection-type-p type1)
809 (intersection-type-p type2))
810 ;; Intersections of INTERSECTION-TYPE should have the
811 ;; INTERSECTION-TYPE-TYPES values broken out and intersected
812 ;; separately. The full TYPE-INTERSECTION function knows how
813 ;; to do that, so let it handle it.
814 (type-intersection type1 type2))
816 ;; the ordinary case: we dispatch to type methods
817 (%type-intersection2 type1 type2))))
819 ;;; Return as restrictive and simple a type as we can discover that is
820 ;;; no more restrictive than the intersection of TYPE1 and TYPE2. At
821 ;;; worst, we arbitrarily return one of the arguments as the first
822 ;;; value (trying not to return a hairy type).
823 (defun type-approx-intersection2 (type1 type2)
824 (cond ((type-intersection2 type1 type2))
825 ((hairy-type-p type1) type2)
828 ;;; a test useful for checking whether a derived type matches a
831 ;;; The first value is true unless the types don't intersect and
832 ;;; aren't equal. The second value is true if the first value is
833 ;;; definitely correct. NIL is considered to intersect with any type.
834 ;;; If T is a subtype of either type, then we also return T, T. This
835 ;;; way we recognize that hairy types might intersect with T.
836 (defun types-equal-or-intersect (type1 type2)
837 (declare (type ctype type1 type2))
838 (if (or (eq type1 *empty-type*) (eq type2 *empty-type*))
840 (let ((intersection2 (type-intersection2 type1 type2)))
841 (cond ((not intersection2)
842 (if (or (csubtypep *universal-type* type1)
843 (csubtypep *universal-type* type2))
846 ((eq intersection2 *empty-type*) (values nil t))
849 ;;; Return a Common Lisp type specifier corresponding to the TYPE
851 (defun type-specifier (type)
852 (declare (type ctype type))
853 (funcall (type-class-unparse (type-class-info type)) type))
855 ;;; (VALUES-SPECIFIER-TYPE and SPECIFIER-TYPE moved from here to
856 ;;; early-type.lisp by WHN ca. 19990201.)
858 ;;; Take a list of type specifiers, computing the translation of each
859 ;;; specifier and defining it as a builtin type.
860 (declaim (ftype (function (list) (values)) precompute-types))
861 (defun precompute-types (specs)
863 (let ((res (specifier-type spec)))
864 (unless (unknown-type-p res)
865 (setf (info :type :builtin spec) res)
866 ;; KLUDGE: the three copies of this idiom in this file (and
867 ;; the one in class.lisp as at sbcl-0.7.4.1x) should be
868 ;; coalesced, or perhaps the error-detecting code that
869 ;; disallows redefinition of :PRIMITIVE types should be
870 ;; rewritten to use *TYPE-SYSTEM-FINALIZED* (rather than
871 ;; *TYPE-SYSTEM-INITIALIZED*). The effect of this is not to
872 ;; cause redefinition errors when precompute-types is called
873 ;; for a second time while building the target compiler using
874 ;; the cross-compiler. -- CSR, trying to explain why this
875 ;; isn't completely wrong, 2002-06-07
876 (setf (info :type :kind spec) #+sb-xc-host :defined #-sb-xc-host :primitive))))
879 ;;;; general TYPE-UNION and TYPE-INTERSECTION operations
881 ;;;; These are fully general operations on CTYPEs: they'll always
882 ;;;; return a CTYPE representing the result.
884 ;;; shared logic for unions and intersections: Return a vector of
885 ;;; types representing the same types as INPUT-TYPES, but with
886 ;;; COMPOUND-TYPEs satisfying %COMPOUND-TYPE-P broken up into their
887 ;;; component types, and with any SIMPLY2 simplifications applied.
888 (declaim (inline simplified-compound-types))
889 (defun simplified-compound-types (input-types %compound-type-p simplify2)
890 (declare (function %compound-type-p simplify2))
891 (let ((types (make-array (length input-types)
894 :element-type 'ctype)))
895 (labels ((accumulate-compound-type (type)
896 (if (funcall %compound-type-p type)
897 (dolist (type (compound-type-types type))
898 (accumulate1-compound-type type))
899 (accumulate1-compound-type type)))
900 (accumulate1-compound-type (type)
901 (declare (type ctype type))
902 ;; Any input object satisfying %COMPOUND-TYPE-P should've been
903 ;; broken into components before it reached us.
904 (aver (not (funcall %compound-type-p type)))
905 (dotimes (i (length types) (vector-push-extend type types))
906 (let ((simplified2 (funcall simplify2 type (aref types i))))
908 ;; Discard the old (AREF TYPES I).
909 (setf (aref types i) (vector-pop types))
910 ;; Merge the new SIMPLIFIED2 into TYPES, by tail recursing.
911 ;; (Note that the tail recursion is indirect: we go through
912 ;; ACCUMULATE, not ACCUMULATE1, so that if SIMPLIFIED2 is
913 ;; handled properly if it satisfies %COMPOUND-TYPE-P.)
914 (return (accumulate-compound-type simplified2)))))))
915 (dolist (input-type input-types)
916 (accumulate-compound-type input-type)))
919 ;;; shared logic for unions and intersections: Make a COMPOUND-TYPE
920 ;;; object whose components are the types in TYPES, or skip to special
921 ;;; cases when TYPES is short.
922 (defun make-probably-compound-type (constructor types enumerable identity)
923 (declare (type function constructor))
924 (declare (type (vector ctype) types))
925 (declare (type ctype identity))
929 (t (funcall constructor
931 ;; FIXME: This should be just (COERCE TYPES 'LIST), but as
932 ;; of sbcl-0.6.11.17 the COERCE optimizer is really
933 ;; brain-dead, so that would generate a full call to
934 ;; SPECIFIER-TYPE at runtime, so we get into bootstrap
935 ;; problems in cold init because 'LIST is a compound
936 ;; type, so we need to MAKE-PROBABLY-COMPOUND-TYPE
937 ;; before we know what 'LIST is. Once the COERCE
938 ;; optimizer is less brain-dead, we can make this
939 ;; (COERCE TYPES 'LIST) again.
940 #+sb-xc-host (coerce types 'list)
941 #-sb-xc-host (coerce-to-list types)))))
943 (defun maybe-distribute-one-union (union-type types)
944 (let* ((intersection (apply #'type-intersection types))
945 (union (mapcar (lambda (x) (type-intersection x intersection))
946 (union-type-types union-type))))
947 (if (notany (lambda (x) (or (hairy-type-p x)
948 (intersection-type-p x)))
953 (defun type-intersection (&rest input-types)
954 (%type-intersection input-types))
955 (defun-cached (%type-intersection :hash-bits 8
956 :hash-function (lambda (x)
957 (logand (sxhash x) #xff)))
958 ((input-types equal))
959 (let ((simplified-types (simplified-compound-types input-types
960 #'intersection-type-p
961 #'type-intersection2)))
962 (declare (type (vector ctype) simplified-types))
963 ;; We want to have a canonical representation of types (or failing
964 ;; that, punt to HAIRY-TYPE). Canonical representation would have
965 ;; intersections inside unions but not vice versa, since you can
966 ;; always achieve that by the distributive rule. But we don't want
967 ;; to just apply the distributive rule, since it would be too easy
968 ;; to end up with unreasonably huge type expressions. So instead
969 ;; we try to generate a simple type by distributing the union; if
970 ;; the type can't be made simple, we punt to HAIRY-TYPE.
971 (if (and (> (length simplified-types) 1)
972 (some #'union-type-p simplified-types))
973 (let* ((first-union (find-if #'union-type-p simplified-types))
974 (other-types (coerce (remove first-union simplified-types)
976 (distributed (maybe-distribute-one-union first-union
979 (apply #'type-union distributed)
981 :specifier `(and ,@(map 'list
983 simplified-types)))))
984 (make-probably-compound-type #'%make-intersection-type
986 (some #'type-enumerable
990 (defun type-union (&rest input-types)
991 (%type-union input-types))
992 (defun-cached (%type-union :hash-bits 8
993 :hash-function (lambda (x)
994 (logand (sxhash x) #xff)))
995 ((input-types equal))
996 (let ((simplified-types (simplified-compound-types input-types
999 (make-probably-compound-type #'make-union-type
1001 (every #'type-enumerable simplified-types)
1006 (!define-type-class named)
1008 (defvar *wild-type*)
1009 (defvar *empty-type*)
1010 (defvar *universal-type*)
1011 (defvar *universal-fun-type*)
1014 (macrolet ((frob (name var)
1016 (setq ,var (make-named-type :name ',name))
1017 (setf (info :type :kind ',name)
1018 #+sb-xc-host :defined #-sb-xc-host :primitive)
1019 (setf (info :type :builtin ',name) ,var))))
1020 ;; KLUDGE: In ANSI, * isn't really the name of a type, it's just a
1021 ;; special symbol which can be stuck in some places where an
1022 ;; ordinary type can go, e.g. (ARRAY * 1) instead of (ARRAY T 1).
1023 ;; In SBCL it also used to denote universal VALUES type.
1024 (frob * *wild-type*)
1025 (frob nil *empty-type*)
1026 (frob t *universal-type*))
1027 (setf *universal-fun-type*
1028 (make-fun-type :wild-args t
1029 :returns *wild-type*)))
1031 (!define-type-method (named :simple-=) (type1 type2)
1032 ;;(aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1033 (values (eq type1 type2) t))
1035 (!define-type-method (named :complex-=) (type1 type2)
1037 ((and (eq type2 *empty-type*)
1038 (intersection-type-p type1)
1039 ;; not allowed to be unsure on these... FIXME: keep the list
1040 ;; of CL types that are intersection types once and only
1042 (not (or (type= type1 (specifier-type 'ratio))
1043 (type= type1 (specifier-type 'keyword)))))
1044 ;; things like (AND (EQL 0) (SATISFIES ODDP)) or (AND FUNCTION
1045 ;; STREAM) can get here. In general, we can't really tell
1046 ;; whether these are equal to NIL or not, so
1048 ((type-might-contain-other-types-p type1)
1049 (invoke-complex-=-other-method type1 type2))
1050 (t (values nil t))))
1052 (!define-type-method (named :simple-subtypep) (type1 type2)
1053 (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1054 (values (or (eq type1 *empty-type*) (eq type2 *wild-type*)) t))
1056 (!define-type-method (named :complex-subtypep-arg1) (type1 type2)
1057 ;; This AVER causes problems if we write accurate methods for the
1058 ;; union (and possibly intersection) types which then delegate to
1059 ;; us; while a user shouldn't get here, because of the odd status of
1060 ;; *wild-type* a type-intersection executed by the compiler can. -
1063 ;; (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1064 (cond ((eq type1 *empty-type*)
1066 (;; When TYPE2 might be the universal type in disguise
1067 (type-might-contain-other-types-p type2)
1068 ;; Now that the UNION and HAIRY COMPLEX-SUBTYPEP-ARG2 methods
1069 ;; can delegate to us (more or less as CALL-NEXT-METHOD) when
1070 ;; they're uncertain, we can't just barf on COMPOUND-TYPE and
1071 ;; HAIRY-TYPEs as we used to. Instead we deal with the
1072 ;; problem (where at least part of the problem is cases like
1073 ;; (SUBTYPEP T '(SATISFIES FOO))
1075 ;; (SUBTYPEP T '(AND (SATISFIES FOO) (SATISFIES BAR)))
1076 ;; where the second type is a hairy type like SATISFIES, or
1077 ;; is a compound type which might contain a hairy type) by
1078 ;; returning uncertainty.
1081 ;; By elimination, TYPE1 is the universal type.
1082 (aver (eq type1 *universal-type*))
1083 ;; This case would have been picked off by the SIMPLE-SUBTYPEP
1084 ;; method, and so shouldn't appear here.
1085 (aver (not (eq type2 *universal-type*)))
1086 ;; Since TYPE2 is not EQ *UNIVERSAL-TYPE* and is not the
1087 ;; universal type in disguise, TYPE2 is not a superset of TYPE1.
1090 (!define-type-method (named :complex-subtypep-arg2) (type1 type2)
1091 (aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1092 (cond ((eq type2 *universal-type*)
1094 ((type-might-contain-other-types-p type1)
1095 ;; those types can be *EMPTY-TYPE* or *UNIVERSAL-TYPE* in
1096 ;; disguise. So we'd better delegate.
1097 (invoke-complex-subtypep-arg1-method type1 type2))
1099 ;; FIXME: This seems to rely on there only being 2 or 3
1100 ;; NAMED-TYPE values, and the exclusion of various
1101 ;; possibilities above. It would be good to explain it and/or
1102 ;; rewrite it so that it's clearer.
1103 (values (not (eq type2 *empty-type*)) t))))
1105 (!define-type-method (named :complex-intersection2) (type1 type2)
1106 ;; FIXME: This assertion failed when I added it in sbcl-0.6.11.13.
1107 ;; Perhaps when bug 85 is fixed it can be reenabled.
1108 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1109 (hierarchical-intersection2 type1 type2))
1111 (!define-type-method (named :complex-union2) (type1 type2)
1112 ;; Perhaps when bug 85 is fixed this can be reenabled.
1113 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1114 (hierarchical-union2 type1 type2))
1116 (!define-type-method (named :unparse) (x)
1117 (named-type-name x))
1119 ;;;; hairy and unknown types
1121 (!define-type-method (hairy :unparse) (x)
1122 (hairy-type-specifier x))
1124 (!define-type-method (hairy :simple-subtypep) (type1 type2)
1125 (let ((hairy-spec1 (hairy-type-specifier type1))
1126 (hairy-spec2 (hairy-type-specifier type2)))
1127 (cond ((equal-but-no-car-recursion hairy-spec1 hairy-spec2)
1130 (values nil nil)))))
1132 (!define-type-method (hairy :complex-subtypep-arg2) (type1 type2)
1133 (invoke-complex-subtypep-arg1-method type1 type2))
1135 (!define-type-method (hairy :complex-subtypep-arg1) (type1 type2)
1136 (declare (ignore type1 type2))
1139 (!define-type-method (hairy :complex-=) (type1 type2)
1140 (declare (ignore type1 type2))
1143 (!define-type-method (hairy :simple-intersection2 :complex-intersection2)
1145 (if (type= type1 type2)
1149 (!define-type-method (hairy :simple-union2)
1151 (if (type= type1 type2)
1155 (!define-type-method (hairy :simple-=) (type1 type2)
1156 (if (equal-but-no-car-recursion (hairy-type-specifier type1)
1157 (hairy-type-specifier type2))
1161 (!def-type-translator satisfies (&whole whole fun)
1162 (declare (ignore fun))
1163 ;; Check legality of arguments.
1164 (destructuring-bind (satisfies predicate-name) whole
1165 (declare (ignore satisfies))
1166 (unless (symbolp predicate-name)
1167 (error 'simple-type-error
1168 :datum predicate-name
1169 :expected-type 'symbol
1170 :format-control "The SATISFIES predicate name is not a symbol: ~S"
1171 :format-arguments (list predicate-name))))
1173 (make-hairy-type :specifier whole))
1177 (!define-type-method (negation :unparse) (x)
1178 `(not ,(type-specifier (negation-type-type x))))
1180 (!define-type-method (negation :simple-subtypep) (type1 type2)
1181 (csubtypep (negation-type-type type2) (negation-type-type type1)))
1183 (!define-type-method (negation :complex-subtypep-arg2) (type1 type2)
1184 (let* ((complement-type2 (negation-type-type type2))
1185 (intersection2 (type-intersection2 type1
1188 ;; FIXME: if uncertain, maybe try arg1?
1189 (type= intersection2 *empty-type*)
1190 (invoke-complex-subtypep-arg1-method type1 type2))))
1192 (!define-type-method (negation :complex-subtypep-arg1) (type1 type2)
1193 ;; "Incrementally extended heuristic algorithms tend inexorably toward the
1194 ;; incomprehensible." -- http://www.unlambda.com/~james/lambda/lambda.txt
1196 ;; You may not believe this. I couldn't either. But then I sat down
1197 ;; and drew lots of Venn diagrams. Comments involving a and b refer
1198 ;; to the call (subtypep '(not a) 'b) -- CSR, 2002-02-27.
1200 ;; (Several logical truths in this block are true as long as
1201 ;; b/=T. As of sbcl-0.7.1.28, it seems impossible to construct a
1202 ;; case with b=T where we actually reach this type method, but
1203 ;; we'll test for and exclude this case anyway, since future
1204 ;; maintenance might make it possible for it to end up in this
1206 (multiple-value-bind (equal certain)
1207 (type= type2 *universal-type*)
1209 (return (values nil nil)))
1211 (return (values t t))))
1212 (let ((complement-type1 (negation-type-type type1)))
1213 ;; Do the special cases first, in order to give us a chance if
1214 ;; subtype/supertype relationships are hairy.
1215 (multiple-value-bind (equal certain)
1216 (type= complement-type1 type2)
1217 ;; If a = b, ~a is not a subtype of b (unless b=T, which was
1220 (return (values nil nil)))
1222 (return (values nil t))))
1223 ;; KLUDGE: ANSI requires that the SUBTYPEP result between any
1224 ;; two built-in atomic type specifiers never be uncertain. This
1225 ;; is hard to do cleanly for the built-in types whose
1226 ;; definitions include (NOT FOO), i.e. CONS and RATIO. However,
1227 ;; we can do it with this hack, which uses our global knowledge
1228 ;; that our implementation of the type system uses disjoint
1229 ;; implementation types to represent disjoint sets (except when
1230 ;; types are contained in other types). (This is a KLUDGE
1231 ;; because it's fragile. Various changes in internal
1232 ;; representation in the type system could make it start
1233 ;; confidently returning incorrect results.) -- WHN 2002-03-08
1234 (unless (or (type-might-contain-other-types-p complement-type1)
1235 (type-might-contain-other-types-p type2))
1236 ;; Because of the way our types which don't contain other
1237 ;; types are disjoint subsets of the space of possible values,
1238 ;; (SUBTYPEP '(NOT AA) 'B)=NIL when AA and B are simple (and B
1239 ;; is not T, as checked above).
1240 (return (values nil t)))
1241 ;; The old (TYPE= TYPE1 TYPE2) branch would never be taken, as
1242 ;; TYPE1 and TYPE2 will only be equal if they're both NOT types,
1243 ;; and then the :SIMPLE-SUBTYPEP method would be used instead.
1244 ;; But a CSUBTYPEP relationship might still hold:
1245 (multiple-value-bind (equal certain)
1246 (csubtypep complement-type1 type2)
1247 ;; If a is a subtype of b, ~a is not a subtype of b (unless
1248 ;; b=T, which was excluded above).
1250 (return (values nil nil)))
1252 (return (values nil t))))
1253 (multiple-value-bind (equal certain)
1254 (csubtypep type2 complement-type1)
1255 ;; If b is a subtype of a, ~a is not a subtype of b. (FIXME:
1256 ;; That's not true if a=T. Do we know at this point that a is
1259 (return (values nil nil)))
1261 (return (values nil t))))
1262 ;; old CSR comment ca. 0.7.2, now obsoleted by the SIMPLE-CTYPE?
1263 ;; KLUDGE case above: Other cases here would rely on being able
1264 ;; to catch all possible cases, which the fragility of this type
1265 ;; system doesn't inspire me; for instance, if a is type= to ~b,
1266 ;; then we want T, T; if this is not the case and the types are
1267 ;; disjoint (have an intersection of *empty-type*) then we want
1268 ;; NIL, T; else if the union of a and b is the *universal-type*
1269 ;; then we want T, T. So currently we still claim to be unsure
1270 ;; about e.g. (subtypep '(not fixnum) 'single-float).
1272 ;; OTOH we might still get here:
1275 (!define-type-method (negation :complex-=) (type1 type2)
1276 ;; (NOT FOO) isn't equivalent to anything that's not a negation
1277 ;; type, except possibly a type that might contain it in disguise.
1278 (declare (ignore type2))
1279 (if (type-might-contain-other-types-p type1)
1283 (!define-type-method (negation :simple-intersection2) (type1 type2)
1284 (let ((not1 (negation-type-type type1))
1285 (not2 (negation-type-type type2)))
1287 ((csubtypep not1 not2) type2)
1288 ((csubtypep not2 not1) type1)
1289 ;; Why no analagous clause to the disjoint in the SIMPLE-UNION2
1290 ;; method, below? The clause would read
1292 ;; ((EQ (TYPE-UNION NOT1 NOT2) *UNIVERSAL-TYPE*) *EMPTY-TYPE*)
1294 ;; but with proper canonicalization of negation types, there's
1295 ;; no way of constructing two negation types with union of their
1296 ;; negations being the universal type.
1298 (aver (not (eq (type-union not1 not2) *universal-type*)))
1301 (!define-type-method (negation :complex-intersection2) (type1 type2)
1303 ((csubtypep type1 (negation-type-type type2)) *empty-type*)
1304 ((eq (type-intersection type1 (negation-type-type type2)) *empty-type*)
1308 (!define-type-method (negation :simple-union2) (type1 type2)
1309 (let ((not1 (negation-type-type type1))
1310 (not2 (negation-type-type type2)))
1312 ((csubtypep not1 not2) type1)
1313 ((csubtypep not2 not1) type2)
1314 ((eq (type-intersection not1 not2) *empty-type*)
1318 (!define-type-method (negation :complex-union2) (type1 type2)
1320 ((csubtypep (negation-type-type type2) type1) *universal-type*)
1321 ((eq (type-intersection type1 (negation-type-type type2)) *empty-type*)
1325 (!define-type-method (negation :simple-=) (type1 type2)
1326 (type= (negation-type-type type1) (negation-type-type type2)))
1328 (!def-type-translator not (typespec)
1329 (let* ((not-type (specifier-type typespec))
1330 (spec (type-specifier not-type)))
1332 ;; canonicalize (NOT (NOT FOO))
1333 ((and (listp spec) (eq (car spec) 'not))
1334 (specifier-type (cadr spec)))
1335 ;; canonicalize (NOT NIL) and (NOT T)
1336 ((eq not-type *empty-type*) *universal-type*)
1337 ((eq not-type *universal-type*) *empty-type*)
1338 ((and (numeric-type-p not-type)
1339 (null (numeric-type-low not-type))
1340 (null (numeric-type-high not-type)))
1341 (make-negation-type :type not-type))
1342 ((numeric-type-p not-type)
1345 :type (modified-numeric-type not-type :low nil :high nil))
1347 ((null (numeric-type-low not-type))
1348 (modified-numeric-type
1350 :low (let ((h (numeric-type-high not-type)))
1351 (if (consp h) (car h) (list h)))
1353 ((null (numeric-type-high not-type))
1354 (modified-numeric-type
1357 :high (let ((l (numeric-type-low not-type)))
1358 (if (consp l) (car l) (list l)))))
1360 (modified-numeric-type
1363 :high (let ((l (numeric-type-low not-type)))
1364 (if (consp l) (car l) (list l))))
1365 (modified-numeric-type
1367 :low (let ((h (numeric-type-high not-type)))
1368 (if (consp h) (car h) (list h)))
1370 ((intersection-type-p not-type)
1372 (mapcar #'(lambda (x)
1373 (specifier-type `(not ,(type-specifier x))))
1374 (intersection-type-types not-type))))
1375 ((union-type-p not-type)
1376 (apply #'type-intersection
1377 (mapcar #'(lambda (x)
1378 (specifier-type `(not ,(type-specifier x))))
1379 (union-type-types not-type))))
1380 ((member-type-p not-type)
1381 (let ((members (member-type-members not-type)))
1382 (if (some #'floatp members)
1384 (dolist (pair `((0.0f0 . ,(load-time-value (make-unportable-float :single-float-negative-zero)))
1385 (0.0d0 . ,(load-time-value (make-unportable-float :double-float-negative-zero)))
1387 (0.0l0 . ,(load-time-value (make-unportable-float :long-float-negative-zero)))))
1388 (when (member (car pair) members)
1389 (aver (not (member (cdr pair) members)))
1390 (push (cdr pair) floats)
1391 (setf members (remove (car pair) members)))
1392 (when (member (cdr pair) members)
1393 (aver (not (member (car pair) members)))
1394 (push (car pair) floats)
1395 (setf members (remove (cdr pair) members))))
1396 (apply #'type-intersection
1400 :type (make-member-type :members members)))
1403 (let ((type (ctype-of x)))
1406 :type (modified-numeric-type type
1407 :low nil :high nil))
1408 (modified-numeric-type type
1409 :low nil :high (list x))
1410 (make-member-type :members (list x))
1411 (modified-numeric-type type
1412 :low (list x) :high nil))))
1414 (make-negation-type :type not-type))))
1415 ((and (cons-type-p not-type)
1416 (eq (cons-type-car-type not-type) *universal-type*)
1417 (eq (cons-type-cdr-type not-type) *universal-type*))
1418 (make-negation-type :type not-type))
1419 ((cons-type-p not-type)
1421 (make-negation-type :type (specifier-type 'cons))
1423 ((and (not (eq (cons-type-car-type not-type) *universal-type*))
1424 (not (eq (cons-type-cdr-type not-type) *universal-type*)))
1427 (specifier-type `(not ,(type-specifier
1428 (cons-type-car-type not-type))))
1432 (specifier-type `(not ,(type-specifier
1433 (cons-type-cdr-type not-type)))))))
1434 ((not (eq (cons-type-car-type not-type) *universal-type*))
1436 (specifier-type `(not ,(type-specifier
1437 (cons-type-car-type not-type))))
1439 ((not (eq (cons-type-cdr-type not-type) *universal-type*))
1442 (specifier-type `(not ,(type-specifier
1443 (cons-type-cdr-type not-type))))))
1444 (t (bug "Weird CONS type ~S" not-type)))))
1445 (t (make-negation-type :type not-type)))))
1449 (!define-type-class number)
1451 (!define-type-method (number :simple-=) (type1 type2)
1453 (and (eq (numeric-type-class type1) (numeric-type-class type2))
1454 (eq (numeric-type-format type1) (numeric-type-format type2))
1455 (eq (numeric-type-complexp type1) (numeric-type-complexp type2))
1456 (equalp (numeric-type-low type1) (numeric-type-low type2))
1457 (equalp (numeric-type-high type1) (numeric-type-high type2)))
1460 (!define-type-method (number :unparse) (type)
1461 (let* ((complexp (numeric-type-complexp type))
1462 (low (numeric-type-low type))
1463 (high (numeric-type-high type))
1464 (base (case (numeric-type-class type)
1466 (rational 'rational)
1467 (float (or (numeric-type-format type) 'float))
1470 (cond ((and (eq base 'integer) high low)
1471 (let ((high-count (logcount high))
1472 (high-length (integer-length high)))
1474 (cond ((= high 0) '(integer 0 0))
1476 ((and (= high-count high-length)
1477 (plusp high-length))
1478 `(unsigned-byte ,high-length))
1480 `(mod ,(1+ high)))))
1481 ((and (= low sb!xc:most-negative-fixnum)
1482 (= high sb!xc:most-positive-fixnum))
1484 ((and (= low (lognot high))
1485 (= high-count high-length)
1487 `(signed-byte ,(1+ high-length)))
1489 `(integer ,low ,high)))))
1490 (high `(,base ,(or low '*) ,high))
1492 (if (and (eq base 'integer) (= low 0))
1500 (if (eq base+bounds 'real)
1502 `(complex ,base+bounds)))
1504 (aver (eq base+bounds 'real))
1507 ;;; Return true if X is "less than or equal" to Y, taking open bounds
1508 ;;; into consideration. CLOSED is the predicate used to test the bound
1509 ;;; on a closed interval (e.g. <=), and OPEN is the predicate used on
1510 ;;; open bounds (e.g. <). Y is considered to be the outside bound, in
1511 ;;; the sense that if it is infinite (NIL), then the test succeeds,
1512 ;;; whereas if X is infinite, then the test fails (unless Y is also
1515 ;;; This is for comparing bounds of the same kind, e.g. upper and
1516 ;;; upper. Use NUMERIC-BOUND-TEST* for different kinds of bounds.
1517 (defmacro numeric-bound-test (x y closed open)
1522 (,closed (car ,x) (car ,y))
1523 (,closed (car ,x) ,y)))
1529 ;;; This is used to compare upper and lower bounds. This is different
1530 ;;; from the same-bound case:
1531 ;;; -- Since X = NIL is -infinity, whereas y = NIL is +infinity, we
1532 ;;; return true if *either* arg is NIL.
1533 ;;; -- an open inner bound is "greater" and also squeezes the interval,
1534 ;;; causing us to use the OPEN test for those cases as well.
1535 (defmacro numeric-bound-test* (x y closed open)
1540 (,open (car ,x) (car ,y))
1541 (,open (car ,x) ,y)))
1547 ;;; Return whichever of the numeric bounds X and Y is "maximal"
1548 ;;; according to the predicates CLOSED (e.g. >=) and OPEN (e.g. >).
1549 ;;; This is only meaningful for maximizing like bounds, i.e. upper and
1550 ;;; upper. If MAX-P is true, then we return NIL if X or Y is NIL,
1551 ;;; otherwise we return the other arg.
1552 (defmacro numeric-bound-max (x y closed open max-p)
1555 `(cond ((not ,n-x) ,(if max-p nil n-y))
1556 ((not ,n-y) ,(if max-p nil n-x))
1559 (if (,closed (car ,n-x) (car ,n-y)) ,n-x ,n-y)
1560 (if (,open (car ,n-x) ,n-y) ,n-x ,n-y)))
1563 (if (,open (car ,n-y) ,n-x) ,n-y ,n-x)
1564 (if (,closed ,n-y ,n-x) ,n-y ,n-x))))))
1566 (!define-type-method (number :simple-subtypep) (type1 type2)
1567 (let ((class1 (numeric-type-class type1))
1568 (class2 (numeric-type-class type2))
1569 (complexp2 (numeric-type-complexp type2))
1570 (format2 (numeric-type-format type2))
1571 (low1 (numeric-type-low type1))
1572 (high1 (numeric-type-high type1))
1573 (low2 (numeric-type-low type2))
1574 (high2 (numeric-type-high type2)))
1575 ;; If one is complex and the other isn't, they are disjoint.
1576 (cond ((not (or (eq (numeric-type-complexp type1) complexp2)
1579 ;; If the classes are specified and different, the types are
1580 ;; disjoint unless type2 is RATIONAL and type1 is INTEGER.
1581 ;; [ or type1 is INTEGER and type2 is of the form (RATIONAL
1582 ;; X X) for integral X, but this is dealt with in the
1583 ;; canonicalization inside MAKE-NUMERIC-TYPE ]
1584 ((not (or (eq class1 class2)
1586 (and (eq class1 'integer) (eq class2 'rational))))
1588 ;; If the float formats are specified and different, the types
1590 ((not (or (eq (numeric-type-format type1) format2)
1593 ;; Check the bounds.
1594 ((and (numeric-bound-test low1 low2 >= >)
1595 (numeric-bound-test high1 high2 <= <))
1600 (!define-superclasses number ((number)) !cold-init-forms)
1602 ;;; If the high bound of LOW is adjacent to the low bound of HIGH,
1603 ;;; then return true, otherwise NIL.
1604 (defun numeric-types-adjacent (low high)
1605 (let ((low-bound (numeric-type-high low))
1606 (high-bound (numeric-type-low high)))
1607 (cond ((not (and low-bound high-bound)) nil)
1608 ((and (consp low-bound) (consp high-bound)) nil)
1610 (let ((low-value (car low-bound)))
1611 (or (eql low-value high-bound)
1612 (and (eql low-value (load-time-value (make-unportable-float :single-float-negative-zero))) (eql high-bound 0f0))
1613 (and (eql low-value 0f0) (eql high-bound (load-time-value (make-unportable-float :single-float-negative-zero))))
1614 (and (eql low-value (load-time-value (make-unportable-float :double-float-negative-zero))) (eql high-bound 0d0))
1615 (and (eql low-value 0d0) (eql high-bound (load-time-value (make-unportable-float :double-float-negative-zero)))))))
1617 (let ((high-value (car high-bound)))
1618 (or (eql high-value low-bound)
1619 (and (eql high-value (load-time-value (make-unportable-float :single-float-negative-zero))) (eql low-bound 0f0))
1620 (and (eql high-value 0f0) (eql low-bound (load-time-value (make-unportable-float :single-float-negative-zero))))
1621 (and (eql high-value (load-time-value (make-unportable-float :double-float-negative-zero))) (eql low-bound 0d0))
1622 (and (eql high-value 0d0) (eql low-bound (load-time-value (make-unportable-float :double-float-negative-zero)))))))
1623 ((and (eq (numeric-type-class low) 'integer)
1624 (eq (numeric-type-class high) 'integer))
1625 (eql (1+ low-bound) high-bound))
1629 ;;; Return a numeric type that is a supertype for both TYPE1 and TYPE2.
1631 ;;; Old comment, probably no longer applicable:
1633 ;;; ### Note: we give up early to keep from dropping lots of
1634 ;;; information on the floor by returning overly general types.
1635 (!define-type-method (number :simple-union2) (type1 type2)
1636 (declare (type numeric-type type1 type2))
1637 (cond ((csubtypep type1 type2) type2)
1638 ((csubtypep type2 type1) type1)
1640 (let ((class1 (numeric-type-class type1))
1641 (format1 (numeric-type-format type1))
1642 (complexp1 (numeric-type-complexp type1))
1643 (class2 (numeric-type-class type2))
1644 (format2 (numeric-type-format type2))
1645 (complexp2 (numeric-type-complexp type2)))
1647 ((and (eq class1 class2)
1648 (eq format1 format2)
1649 (eq complexp1 complexp2)
1650 (or (numeric-types-intersect type1 type2)
1651 (numeric-types-adjacent type1 type2)
1652 (numeric-types-adjacent type2 type1)))
1657 :low (numeric-bound-max (numeric-type-low type1)
1658 (numeric-type-low type2)
1660 :high (numeric-bound-max (numeric-type-high type1)
1661 (numeric-type-high type2)
1663 ;; FIXME: These two clauses are almost identical, and the
1664 ;; consequents are in fact identical in every respect.
1665 ((and (eq class1 'rational)
1666 (eq class2 'integer)
1667 (eq format1 format2)
1668 (eq complexp1 complexp2)
1669 (integerp (numeric-type-low type2))
1670 (integerp (numeric-type-high type2))
1671 (= (numeric-type-low type2) (numeric-type-high type2))
1672 (or (numeric-types-adjacent type1 type2)
1673 (numeric-types-adjacent type2 type1)))
1678 :low (numeric-bound-max (numeric-type-low type1)
1679 (numeric-type-low type2)
1681 :high (numeric-bound-max (numeric-type-high type1)
1682 (numeric-type-high type2)
1684 ((and (eq class1 'integer)
1685 (eq class2 'rational)
1686 (eq format1 format2)
1687 (eq complexp1 complexp2)
1688 (integerp (numeric-type-low type1))
1689 (integerp (numeric-type-high type1))
1690 (= (numeric-type-low type1) (numeric-type-high type1))
1691 (or (numeric-types-adjacent type1 type2)
1692 (numeric-types-adjacent type2 type1)))
1697 :low (numeric-bound-max (numeric-type-low type1)
1698 (numeric-type-low type2)
1700 :high (numeric-bound-max (numeric-type-high type1)
1701 (numeric-type-high type2)
1707 (setf (info :type :kind 'number)
1708 #+sb-xc-host :defined #-sb-xc-host :primitive)
1709 (setf (info :type :builtin 'number)
1710 (make-numeric-type :complexp nil)))
1712 (!def-type-translator complex (&optional (typespec '*))
1713 (if (eq typespec '*)
1714 (make-numeric-type :complexp :complex)
1715 (labels ((not-numeric ()
1716 (error "The component type for COMPLEX is not numeric: ~S"
1719 (error "The component type for COMPLEX is not real: ~S"
1721 (complex1 (component-type)
1722 (unless (numeric-type-p component-type)
1724 (when (eq (numeric-type-complexp component-type) :complex)
1726 (modified-numeric-type component-type :complexp :complex))
1727 (complex-union (component)
1728 (unless (numberp component)
1730 ;; KLUDGE: This TYPECASE more or less does
1731 ;; (UPGRADED-COMPLEX-PART-TYPE (TYPE-OF COMPONENT)),
1732 ;; (plus a small hack to treat (EQL COMPONENT 0) specially)
1733 ;; but uses logic cut and pasted from the DEFUN of
1734 ;; UPGRADED-COMPLEX-PART-TYPE. That's fragile, because
1735 ;; changing the definition of UPGRADED-COMPLEX-PART-TYPE
1736 ;; would tend to break the code here. Unfortunately,
1737 ;; though, reusing UPGRADED-COMPLEX-PART-TYPE here
1738 ;; would cause another kind of fragility, because
1739 ;; ANSI's definition of TYPE-OF is so weak that e.g.
1740 ;; (UPGRADED-COMPLEX-PART-TYPE (TYPE-OF 1/2)) could
1741 ;; end up being (UPGRADED-COMPLEX-PART-TYPE 'REAL)
1742 ;; instead of (UPGRADED-COMPLEX-PART-TYPE 'RATIONAL).
1743 ;; So using TYPE-OF would mean that ANSI-conforming
1744 ;; maintenance changes in TYPE-OF could break the code here.
1745 ;; It's not clear how best to fix this. -- WHN 2002-01-21,
1746 ;; trying to summarize CSR's concerns in his patch
1748 (complex (error "The component type for COMPLEX (EQL X) ~
1751 ((eql 0) (specifier-type nil)) ; as required by ANSI
1752 (single-float (specifier-type '(complex single-float)))
1753 (double-float (specifier-type '(complex double-float)))
1755 (long-float (specifier-type '(complex long-float)))
1756 (rational (specifier-type '(complex rational)))
1757 (t (specifier-type '(complex real))))))
1758 (let ((ctype (specifier-type typespec)))
1760 (numeric-type (complex1 ctype))
1761 (union-type (apply #'type-union
1762 ;; FIXME: This code could suffer from
1763 ;; (admittedly very obscure) cases of
1764 ;; bug 145 e.g. when TYPE is
1765 ;; (OR (AND INTEGER (SATISFIES ODDP))
1766 ;; (AND FLOAT (SATISFIES FOO))
1767 ;; and not even report the problem very well.
1769 (union-type-types ctype))))
1770 ;; MEMBER-TYPE is almost the same as UNION-TYPE, but
1771 ;; there's a gotcha: (COMPLEX (EQL 0)) is, according to
1772 ;; ANSI, equal to type NIL, the empty set.
1773 (member-type (apply #'type-union
1774 (mapcar #'complex-union
1775 (member-type-members ctype))))
1777 (multiple-value-bind (subtypep certainly)
1778 (csubtypep ctype (specifier-type 'real))
1779 (if (and (not subtypep) certainly)
1781 ;; ANSI just says that TYPESPEC is any subtype of
1782 ;; type REAL, not necessarily a NUMERIC-TYPE. In
1783 ;; particular, at this point TYPESPEC could legally be
1784 ;; an intersection type like (AND REAL (SATISFIES ODDP)),
1785 ;; in which case we fall through the logic above and
1786 ;; end up here, stumped.
1787 (bug "~@<(known bug #145): The type ~S is too hairy to be
1788 used for a COMPLEX component.~:@>"
1791 ;;; If X is *, return NIL, otherwise return the bound, which must be a
1792 ;;; member of TYPE or a one-element list of a member of TYPE.
1793 #!-sb-fluid (declaim (inline canonicalized-bound))
1794 (defun canonicalized-bound (bound type)
1795 (cond ((eq bound '*) nil)
1796 ((or (sb!xc:typep bound type)
1798 (sb!xc:typep (car bound) type)
1799 (null (cdr bound))))
1802 (error "Bound is not ~S, a ~S or a list of a ~S: ~S"
1808 (!def-type-translator integer (&optional (low '*) (high '*))
1809 (let* ((l (canonicalized-bound low 'integer))
1810 (lb (if (consp l) (1+ (car l)) l))
1811 (h (canonicalized-bound high 'integer))
1812 (hb (if (consp h) (1- (car h)) h)))
1813 (if (and hb lb (< hb lb))
1815 (make-numeric-type :class 'integer
1817 :enumerable (not (null (and l h)))
1821 (defmacro !def-bounded-type (type class format)
1822 `(!def-type-translator ,type (&optional (low '*) (high '*))
1823 (let ((lb (canonicalized-bound low ',type))
1824 (hb (canonicalized-bound high ',type)))
1825 (if (not (numeric-bound-test* lb hb <= <))
1827 (make-numeric-type :class ',class
1832 (!def-bounded-type rational rational nil)
1834 ;;; Unlike CMU CL, we represent the types FLOAT and REAL as
1835 ;;; UNION-TYPEs of more primitive types, in order to make
1836 ;;; type representation more unique, avoiding problems in the
1837 ;;; simplification of things like
1838 ;;; (subtypep '(or (single-float -1.0 1.0) (single-float 0.1))
1839 ;;; '(or (real -1 7) (single-float 0.1) (single-float -1.0 1.0)))
1840 ;;; When we allowed REAL to remain as a separate NUMERIC-TYPE,
1841 ;;; it was too easy for the first argument to be simplified to
1842 ;;; '(SINGLE-FLOAT -1.0), and for the second argument to be simplified
1843 ;;; to '(OR (REAL -1 7) (SINGLE-FLOAT 0.1)) and then for the
1844 ;;; SUBTYPEP to fail (returning NIL,T instead of T,T) because
1845 ;;; the first argument can't be seen to be a subtype of any of the
1846 ;;; terms in the second argument.
1848 ;;; The old CMU CL way was:
1849 ;;; (!def-bounded-type float float nil)
1850 ;;; (!def-bounded-type real nil nil)
1852 ;;; FIXME: If this new way works for a while with no weird new
1853 ;;; problems, we can go back and rip out support for separate FLOAT
1854 ;;; and REAL flavors of NUMERIC-TYPE. The new way was added in
1855 ;;; sbcl-0.6.11.22, 2001-03-21.
1857 ;;; FIXME: It's probably necessary to do something to fix the
1858 ;;; analogous problem with INTEGER and RATIONAL types. Perhaps
1859 ;;; bounded RATIONAL types should be represented as (OR RATIO INTEGER).
1860 (defun coerce-bound (bound type inner-coerce-bound-fun)
1861 (declare (type function inner-coerce-bound-fun))
1862 (cond ((eql bound '*)
1865 (destructuring-bind (inner-bound) bound
1866 (list (funcall inner-coerce-bound-fun inner-bound type))))
1868 (funcall inner-coerce-bound-fun bound type))))
1869 (defun inner-coerce-real-bound (bound type)
1871 (rational (rationalize bound))
1872 (float (if (floatp bound)
1874 ;; Coerce to the widest float format available, to
1875 ;; avoid unnecessary loss of precision:
1876 (coerce bound 'long-float)))))
1877 (defun coerced-real-bound (bound type)
1878 (coerce-bound bound type #'inner-coerce-real-bound))
1879 (defun coerced-float-bound (bound type)
1880 (coerce-bound bound type #'coerce))
1881 (!def-type-translator real (&optional (low '*) (high '*))
1882 (specifier-type `(or (float ,(coerced-real-bound low 'float)
1883 ,(coerced-real-bound high 'float))
1884 (rational ,(coerced-real-bound low 'rational)
1885 ,(coerced-real-bound high 'rational)))))
1886 (!def-type-translator float (&optional (low '*) (high '*))
1888 `(or (single-float ,(coerced-float-bound low 'single-float)
1889 ,(coerced-float-bound high 'single-float))
1890 (double-float ,(coerced-float-bound low 'double-float)
1891 ,(coerced-float-bound high 'double-float))
1892 #!+long-float ,(error "stub: no long float support yet"))))
1894 (defmacro !define-float-format (f)
1895 `(!def-bounded-type ,f float ,f))
1897 (!define-float-format short-float)
1898 (!define-float-format single-float)
1899 (!define-float-format double-float)
1900 (!define-float-format long-float)
1902 (defun numeric-types-intersect (type1 type2)
1903 (declare (type numeric-type type1 type2))
1904 (let* ((class1 (numeric-type-class type1))
1905 (class2 (numeric-type-class type2))
1906 (complexp1 (numeric-type-complexp type1))
1907 (complexp2 (numeric-type-complexp type2))
1908 (format1 (numeric-type-format type1))
1909 (format2 (numeric-type-format type2))
1910 (low1 (numeric-type-low type1))
1911 (high1 (numeric-type-high type1))
1912 (low2 (numeric-type-low type2))
1913 (high2 (numeric-type-high type2)))
1914 ;; If one is complex and the other isn't, then they are disjoint.
1915 (cond ((not (or (eq complexp1 complexp2)
1916 (null complexp1) (null complexp2)))
1918 ;; If either type is a float, then the other must either be
1919 ;; specified to be a float or unspecified. Otherwise, they
1921 ((and (eq class1 'float)
1922 (not (member class2 '(float nil)))) nil)
1923 ((and (eq class2 'float)
1924 (not (member class1 '(float nil)))) nil)
1925 ;; If the float formats are specified and different, the
1926 ;; types are disjoint.
1927 ((not (or (eq format1 format2) (null format1) (null format2)))
1930 ;; Check the bounds. This is a bit odd because we must
1931 ;; always have the outer bound of the interval as the
1933 (if (numeric-bound-test high1 high2 <= <)
1934 (or (and (numeric-bound-test low1 low2 >= >)
1935 (numeric-bound-test* low1 high2 <= <))
1936 (and (numeric-bound-test low2 low1 >= >)
1937 (numeric-bound-test* low2 high1 <= <)))
1938 (or (and (numeric-bound-test* low2 high1 <= <)
1939 (numeric-bound-test low2 low1 >= >))
1940 (and (numeric-bound-test high2 high1 <= <)
1941 (numeric-bound-test* high2 low1 >= >))))))))
1943 ;;; Take the numeric bound X and convert it into something that can be
1944 ;;; used as a bound in a numeric type with the specified CLASS and
1945 ;;; FORMAT. If UP-P is true, then we round up as needed, otherwise we
1946 ;;; round down. UP-P true implies that X is a lower bound, i.e. (N) > N.
1948 ;;; This is used by NUMERIC-TYPE-INTERSECTION to mash the bound into
1949 ;;; the appropriate type number. X may only be a float when CLASS is
1952 ;;; ### Note: it is possible for the coercion to a float to overflow
1953 ;;; or underflow. This happens when the bound doesn't fit in the
1954 ;;; specified format. In this case, we should really return the
1955 ;;; appropriate {Most | Least}-{Positive | Negative}-XXX-Float float
1956 ;;; of desired format. But these conditions aren't currently signalled
1957 ;;; in any useful way.
1959 ;;; Also, when converting an open rational bound into a float we
1960 ;;; should probably convert it to a closed bound of the closest float
1961 ;;; in the specified format. KLUDGE: In general, open float bounds are
1962 ;;; screwed up. -- (comment from original CMU CL)
1963 (defun round-numeric-bound (x class format up-p)
1965 (let ((cx (if (consp x) (car x) x)))
1969 (if (and (consp x) (integerp cx))
1970 (if up-p (1+ cx) (1- cx))
1971 (if up-p (ceiling cx) (floor cx))))
1973 (let ((res (if format (coerce cx format) (float cx))))
1974 (if (consp x) (list res) res)))))
1977 ;;; Handle the case of type intersection on two numeric types. We use
1978 ;;; TYPES-EQUAL-OR-INTERSECT to throw out the case of types with no
1979 ;;; intersection. If an attribute in TYPE1 is unspecified, then we use
1980 ;;; TYPE2's attribute, which must be at least as restrictive. If the
1981 ;;; types intersect, then the only attributes that can be specified
1982 ;;; and different are the class and the bounds.
1984 ;;; When the class differs, we use the more restrictive class. The
1985 ;;; only interesting case is RATIONAL/INTEGER, since RATIONAL includes
1988 ;;; We make the result lower (upper) bound the maximum (minimum) of
1989 ;;; the argument lower (upper) bounds. We convert the bounds into the
1990 ;;; appropriate numeric type before maximizing. This avoids possible
1991 ;;; confusion due to mixed-type comparisons (but I think the result is
1993 (!define-type-method (number :simple-intersection2) (type1 type2)
1994 (declare (type numeric-type type1 type2))
1995 (if (numeric-types-intersect type1 type2)
1996 (let* ((class1 (numeric-type-class type1))
1997 (class2 (numeric-type-class type2))
1998 (class (ecase class1
2000 ((integer float) class1)
2001 (rational (if (eq class2 'integer)
2004 (format (or (numeric-type-format type1)
2005 (numeric-type-format type2))))
2009 :complexp (or (numeric-type-complexp type1)
2010 (numeric-type-complexp type2))
2011 :low (numeric-bound-max
2012 (round-numeric-bound (numeric-type-low type1)
2014 (round-numeric-bound (numeric-type-low type2)
2017 :high (numeric-bound-max
2018 (round-numeric-bound (numeric-type-high type1)
2020 (round-numeric-bound (numeric-type-high type2)
2025 ;;; Given two float formats, return the one with more precision. If
2026 ;;; either one is null, return NIL.
2027 (defun float-format-max (f1 f2)
2029 (dolist (f *float-formats* (error "bad float format: ~S" f1))
2030 (when (or (eq f f1) (eq f f2))
2033 ;;; Return the result of an operation on TYPE1 and TYPE2 according to
2034 ;;; the rules of numeric contagion. This is always NUMBER, some float
2035 ;;; format (possibly complex) or RATIONAL. Due to rational
2036 ;;; canonicalization, there isn't much we can do here with integers or
2037 ;;; rational complex numbers.
2039 ;;; If either argument is not a NUMERIC-TYPE, then return NUMBER. This
2040 ;;; is useful mainly for allowing types that are technically numbers,
2041 ;;; but not a NUMERIC-TYPE.
2042 (defun numeric-contagion (type1 type2)
2043 (if (and (numeric-type-p type1) (numeric-type-p type2))
2044 (let ((class1 (numeric-type-class type1))
2045 (class2 (numeric-type-class type2))
2046 (format1 (numeric-type-format type1))
2047 (format2 (numeric-type-format type2))
2048 (complexp1 (numeric-type-complexp type1))
2049 (complexp2 (numeric-type-complexp type2)))
2050 (cond ((or (null complexp1)
2052 (specifier-type 'number))
2056 :format (ecase class2
2057 (float (float-format-max format1 format2))
2058 ((integer rational) format1)
2060 ;; A double-float with any real number is a
2063 (if (eq format1 'double-float)
2066 ;; A long-float with any real number is a
2069 (if (eq format1 'long-float)
2072 :complexp (if (or (eq complexp1 :complex)
2073 (eq complexp2 :complex))
2076 ((eq class2 'float) (numeric-contagion type2 type1))
2077 ((and (eq complexp1 :real) (eq complexp2 :real))
2079 :class (and class1 class2 'rational)
2082 (specifier-type 'number))))
2083 (specifier-type 'number)))
2087 (!define-type-class array)
2089 ;;; What this does depends on the setting of the
2090 ;;; *USE-IMPLEMENTATION-TYPES* switch. If true, return the specialized
2091 ;;; element type, otherwise return the original element type.
2092 (defun specialized-element-type-maybe (type)
2093 (declare (type array-type type))
2094 (if *use-implementation-types*
2095 (array-type-specialized-element-type type)
2096 (array-type-element-type type)))
2098 (!define-type-method (array :simple-=) (type1 type2)
2099 (if (or (unknown-type-p (array-type-element-type type1))
2100 (unknown-type-p (array-type-element-type type2)))
2101 (multiple-value-bind (equalp certainp)
2102 (type= (array-type-element-type type1)
2103 (array-type-element-type type2))
2104 ;; by its nature, the call to TYPE= should never return NIL,
2105 ;; T, as we don't know what the UNKNOWN-TYPE will grow up to
2106 ;; be. -- CSR, 2002-08-19
2107 (aver (not (and (not equalp) certainp)))
2108 (values equalp certainp))
2109 (values (and (equal (array-type-dimensions type1)
2110 (array-type-dimensions type2))
2111 (eq (array-type-complexp type1)
2112 (array-type-complexp type2))
2113 (type= (specialized-element-type-maybe type1)
2114 (specialized-element-type-maybe type2)))
2117 (!define-type-method (array :unparse) (type)
2118 (let ((dims (array-type-dimensions type))
2119 (eltype (type-specifier (array-type-element-type type)))
2120 (complexp (array-type-complexp type)))
2123 (if complexp 'array 'simple-array)
2124 (if complexp `(array ,eltype) `(simple-array ,eltype))))
2125 ((= (length dims) 1)
2127 (if (eq (car dims) '*)
2130 (base-char 'base-string)
2133 (t `(vector ,eltype)))
2135 (bit `(bit-vector ,(car dims)))
2136 (base-char `(base-string ,(car dims)))
2137 (character `(string ,(car dims)))
2138 (t `(vector ,eltype ,(car dims)))))
2139 (if (eq (car dims) '*)
2141 (bit 'simple-bit-vector)
2142 (base-char 'simple-base-string)
2143 (character 'simple-string)
2144 ((t) 'simple-vector)
2145 (t `(simple-array ,eltype (*))))
2147 (bit `(simple-bit-vector ,(car dims)))
2148 (base-char `(simple-base-string ,(car dims)))
2149 (character `(simple-string ,(car dims)))
2150 ((t) `(simple-vector ,(car dims)))
2151 (t `(simple-array ,eltype ,dims))))))
2154 `(array ,eltype ,dims)
2155 `(simple-array ,eltype ,dims))))))
2157 (!define-type-method (array :simple-subtypep) (type1 type2)
2158 (let ((dims1 (array-type-dimensions type1))
2159 (dims2 (array-type-dimensions type2))
2160 (complexp2 (array-type-complexp type2)))
2161 (cond (;; not subtypep unless dimensions are compatible
2162 (not (or (eq dims2 '*)
2163 (and (not (eq dims1 '*))
2164 ;; (sbcl-0.6.4 has trouble figuring out that
2165 ;; DIMS1 and DIMS2 must be lists at this
2166 ;; point, and knowing that is important to
2167 ;; compiling EVERY efficiently.)
2168 (= (length (the list dims1))
2169 (length (the list dims2)))
2170 (every (lambda (x y)
2171 (or (eq y '*) (eql x y)))
2173 (the list dims2)))))
2175 ;; not subtypep unless complexness is compatible
2176 ((not (or (eq complexp2 :maybe)
2177 (eq (array-type-complexp type1) complexp2)))
2179 ;; Since we didn't fail any of the tests above, we win
2180 ;; if the TYPE2 element type is wild.
2181 ((eq (array-type-element-type type2) *wild-type*)
2183 (;; Since we didn't match any of the special cases above, we
2184 ;; can't give a good answer unless both the element types
2185 ;; have been defined.
2186 (or (unknown-type-p (array-type-element-type type1))
2187 (unknown-type-p (array-type-element-type type2)))
2189 (;; Otherwise, the subtype relationship holds iff the
2190 ;; types are equal, and they're equal iff the specialized
2191 ;; element types are identical.
2193 (values (type= (specialized-element-type-maybe type1)
2194 (specialized-element-type-maybe type2))
2197 (!define-superclasses array
2203 (defun array-types-intersect (type1 type2)
2204 (declare (type array-type type1 type2))
2205 (let ((dims1 (array-type-dimensions type1))
2206 (dims2 (array-type-dimensions type2))
2207 (complexp1 (array-type-complexp type1))
2208 (complexp2 (array-type-complexp type2)))
2209 ;; See whether dimensions are compatible.
2210 (cond ((not (or (eq dims1 '*) (eq dims2 '*)
2211 (and (= (length dims1) (length dims2))
2212 (every (lambda (x y)
2213 (or (eq x '*) (eq y '*) (= x y)))
2216 ;; See whether complexpness is compatible.
2217 ((not (or (eq complexp1 :maybe)
2218 (eq complexp2 :maybe)
2219 (eq complexp1 complexp2)))
2223 ;; If either element type is wild, then they intersect.
2224 ;; Otherwise, the types must be identical.
2226 ;; FIXME: There seems to have been a fair amount of
2227 ;; confusion about the distinction between requested element
2228 ;; type and specialized element type; here is one of
2229 ;; them. If we request an array to hold objects of an
2230 ;; unknown type, we can do no better than represent that
2231 ;; type as an array specialized on wild-type. We keep the
2232 ;; requested element-type in the -ELEMENT-TYPE slot, and
2233 ;; *WILD-TYPE* in the -SPECIALIZED-ELEMENT-TYPE. So, here,
2234 ;; we must test for the SPECIALIZED slot being *WILD-TYPE*,
2235 ;; not just the ELEMENT-TYPE slot. Maybe the return value
2236 ;; in that specific case should be T, NIL? Or maybe this
2237 ;; function should really be called
2238 ;; ARRAY-TYPES-COULD-POSSIBLY-INTERSECT? In any case, this
2239 ;; was responsible for bug #123, and this whole issue could
2240 ;; do with a rethink and/or a rewrite. -- CSR, 2002-08-21
2241 ((or (eq (array-type-specialized-element-type type1) *wild-type*)
2242 (eq (array-type-specialized-element-type type2) *wild-type*)
2243 (type= (specialized-element-type-maybe type1)
2244 (specialized-element-type-maybe type2)))
2250 (!define-type-method (array :simple-intersection2) (type1 type2)
2251 (declare (type array-type type1 type2))
2252 (if (array-types-intersect type1 type2)
2253 (let ((dims1 (array-type-dimensions type1))
2254 (dims2 (array-type-dimensions type2))
2255 (complexp1 (array-type-complexp type1))
2256 (complexp2 (array-type-complexp type2))
2257 (eltype1 (array-type-element-type type1))
2258 (eltype2 (array-type-element-type type2)))
2259 (specialize-array-type
2261 :dimensions (cond ((eq dims1 '*) dims2)
2262 ((eq dims2 '*) dims1)
2264 (mapcar (lambda (x y) (if (eq x '*) y x))
2266 :complexp (if (eq complexp1 :maybe) complexp2 complexp1)
2267 :element-type (if (eq eltype1 *wild-type*) eltype2 eltype1))))
2270 ;;; Check a supplied dimension list to determine whether it is legal,
2271 ;;; and return it in canonical form (as either '* or a list).
2272 (defun canonical-array-dimensions (dims)
2277 (error "Arrays can't have a negative number of dimensions: ~S" dims))
2278 (when (>= dims sb!xc:array-rank-limit)
2279 (error "array type with too many dimensions: ~S" dims))
2280 (make-list dims :initial-element '*))
2282 (when (>= (length dims) sb!xc:array-rank-limit)
2283 (error "array type with too many dimensions: ~S" dims))
2286 (unless (and (integerp dim)
2288 (< dim sb!xc:array-dimension-limit))
2289 (error "bad dimension in array type: ~S" dim))))
2292 (error "Array dimensions is not a list, integer or *:~% ~S" dims))))
2296 (!define-type-class member)
2298 (!define-type-method (member :unparse) (type)
2299 (let ((members (member-type-members type)))
2301 ((equal members '(nil)) 'null)
2302 ((type= type (specifier-type 'standard-char)) 'standard-char)
2303 (t `(member ,@members)))))
2305 (!define-type-method (member :simple-subtypep) (type1 type2)
2306 (values (subsetp (member-type-members type1) (member-type-members type2))
2309 (!define-type-method (member :complex-subtypep-arg1) (type1 type2)
2310 (every/type (swapped-args-fun #'ctypep)
2312 (member-type-members type1)))
2314 ;;; We punt if the odd type is enumerable and intersects with the
2315 ;;; MEMBER type. If not enumerable, then it is definitely not a
2316 ;;; subtype of the MEMBER type.
2317 (!define-type-method (member :complex-subtypep-arg2) (type1 type2)
2318 (cond ((not (type-enumerable type1)) (values nil t))
2319 ((types-equal-or-intersect type1 type2)
2320 (invoke-complex-subtypep-arg1-method type1 type2))
2321 (t (values nil t))))
2323 (!define-type-method (member :simple-intersection2) (type1 type2)
2324 (let ((mem1 (member-type-members type1))
2325 (mem2 (member-type-members type2)))
2326 (cond ((subsetp mem1 mem2) type1)
2327 ((subsetp mem2 mem1) type2)
2329 (let ((res (intersection mem1 mem2)))
2331 (make-member-type :members res)
2334 (!define-type-method (member :complex-intersection2) (type1 type2)
2336 (collect ((members))
2337 (let ((mem2 (member-type-members type2)))
2338 (dolist (member mem2)
2339 (multiple-value-bind (val win) (ctypep member type1)
2341 (return-from punt nil))
2342 (when val (members member))))
2343 (cond ((subsetp mem2 (members)) type2)
2344 ((null (members)) *empty-type*)
2346 (make-member-type :members (members))))))))
2348 ;;; We don't need a :COMPLEX-UNION2, since the only interesting case is
2349 ;;; a union type, and the member/union interaction is handled by the
2350 ;;; union type method.
2351 (!define-type-method (member :simple-union2) (type1 type2)
2352 (let ((mem1 (member-type-members type1))
2353 (mem2 (member-type-members type2)))
2354 (cond ((subsetp mem1 mem2) type2)
2355 ((subsetp mem2 mem1) type1)
2357 (make-member-type :members (union mem1 mem2))))))
2359 (!define-type-method (member :simple-=) (type1 type2)
2360 (let ((mem1 (member-type-members type1))
2361 (mem2 (member-type-members type2)))
2362 (values (and (subsetp mem1 mem2)
2363 (subsetp mem2 mem1))
2366 (!define-type-method (member :complex-=) (type1 type2)
2367 (if (type-enumerable type1)
2368 (multiple-value-bind (val win) (csubtypep type2 type1)
2369 (if (or val (not win))
2374 (!def-type-translator member (&rest members)
2377 (dolist (m (remove-duplicates members))
2379 (float (if (zerop m)
2381 (push (ctype-of m) numbers)))
2382 (number (push (ctype-of m) numbers))
2386 (make-member-type :members ms)
2388 (nreverse numbers)))
2391 ;;;; intersection types
2393 ;;;; Until version 0.6.10.6, SBCL followed the original CMU CL approach
2394 ;;;; of punting on all AND types, not just the unreasonably complicated
2395 ;;;; ones. The change was motivated by trying to get the KEYWORD type
2396 ;;;; to behave sensibly:
2397 ;;;; ;; reasonable definition
2398 ;;;; (DEFTYPE KEYWORD () '(AND SYMBOL (SATISFIES KEYWORDP)))
2399 ;;;; ;; reasonable behavior
2400 ;;;; (AVER (SUBTYPEP 'KEYWORD 'SYMBOL))
2401 ;;;; Without understanding a little about the semantics of AND, we'd
2402 ;;;; get (SUBTYPEP 'KEYWORD 'SYMBOL)=>NIL,NIL and, for entirely
2403 ;;;; parallel reasons, (SUBTYPEP 'RATIO 'NUMBER)=>NIL,NIL. That's
2406 ;;;; We still follow the example of CMU CL to some extent, by punting
2407 ;;;; (to the opaque HAIRY-TYPE) on sufficiently complicated types
2410 (!define-type-class intersection)
2412 ;;; A few intersection types have special names. The others just get
2413 ;;; mechanically unparsed.
2414 (!define-type-method (intersection :unparse) (type)
2415 (declare (type ctype type))
2416 (or (find type '(ratio keyword) :key #'specifier-type :test #'type=)
2417 `(and ,@(mapcar #'type-specifier (intersection-type-types type)))))
2419 ;;; shared machinery for type equality: true if every type in the set
2420 ;;; TYPES1 matches a type in the set TYPES2 and vice versa
2421 (defun type=-set (types1 types2)
2422 (flet ((type<=-set (x y)
2423 (declare (type list x y))
2424 (every/type (lambda (x y-element)
2425 (any/type #'type= y-element x))
2427 (and/type (type<=-set types1 types2)
2428 (type<=-set types2 types1))))
2430 ;;; Two intersection types are equal if their subtypes are equal sets.
2432 ;;; FIXME: Might it be better to use
2433 ;;; (AND (SUBTYPEP X Y) (SUBTYPEP Y X))
2434 ;;; instead, since SUBTYPEP is the usual relationship that we care
2435 ;;; most about, so it would be good to leverage any ingenuity there
2436 ;;; in this more obscure method?
2437 (!define-type-method (intersection :simple-=) (type1 type2)
2438 (type=-set (intersection-type-types type1)
2439 (intersection-type-types type2)))
2441 (defun %intersection-complex-subtypep-arg1 (type1 type2)
2442 (type= type1 (type-intersection type1 type2)))
2444 (defun %intersection-simple-subtypep (type1 type2)
2445 (every/type #'%intersection-complex-subtypep-arg1
2447 (intersection-type-types type2)))
2449 (!define-type-method (intersection :simple-subtypep) (type1 type2)
2450 (%intersection-simple-subtypep type1 type2))
2452 (!define-type-method (intersection :complex-subtypep-arg1) (type1 type2)
2453 (%intersection-complex-subtypep-arg1 type1 type2))
2455 (defun %intersection-complex-subtypep-arg2 (type1 type2)
2456 (every/type #'csubtypep type1 (intersection-type-types type2)))
2458 (!define-type-method (intersection :complex-subtypep-arg2) (type1 type2)
2459 (%intersection-complex-subtypep-arg2 type1 type2))
2461 ;;; FIXME: This will look eeriely familiar to readers of the UNION
2462 ;;; :SIMPLE-INTERSECTION2 :COMPLEX-INTERSECTION2 method. That's
2463 ;;; because it was generated by cut'n'paste methods. Given that
2464 ;;; intersections and unions have all sorts of symmetries known to
2465 ;;; mathematics, it shouldn't be beyond the ken of some programmers to
2466 ;;; reflect those symmetries in code in a way that ties them together
2467 ;;; more strongly than having two independent near-copies :-/
2468 (!define-type-method (intersection :simple-union2 :complex-union2)
2470 ;; Within this method, type2 is guaranteed to be an intersection
2472 (aver (intersection-type-p type2))
2473 ;; Make sure to call only the applicable methods...
2474 (cond ((and (intersection-type-p type1)
2475 (%intersection-simple-subtypep type1 type2)) type2)
2476 ((and (intersection-type-p type1)
2477 (%intersection-simple-subtypep type2 type1)) type1)
2478 ((and (not (intersection-type-p type1))
2479 (%intersection-complex-subtypep-arg2 type1 type2))
2481 ((and (not (intersection-type-p type1))
2482 (%intersection-complex-subtypep-arg1 type2 type1))
2484 ;; KLUDGE: This special (and somewhat hairy) magic is required
2485 ;; to deal with the RATIONAL/INTEGER special case. The UNION
2486 ;; of (INTEGER * -1) and (AND (RATIONAL * -1/2) (NOT INTEGER))
2487 ;; should be (RATIONAL * -1/2) -- CSR, 2003-02-28
2488 ((and (csubtypep type2 (specifier-type 'ratio))
2489 (numeric-type-p type1)
2490 (csubtypep type1 (specifier-type 'integer))
2495 :low (if (null (numeric-type-low type1))
2497 (list (1- (numeric-type-low type1))))
2498 :high (if (null (numeric-type-high type1))
2500 (list (1+ (numeric-type-high type1)))))))
2502 (apply #'type-intersection
2503 (remove (specifier-type '(not integer))
2504 (intersection-type-types type2)
2507 (let ((accumulator *universal-type*))
2508 (do ((t2s (intersection-type-types type2) (cdr t2s)))
2509 ((null t2s) accumulator)
2510 (let ((union (type-union type1 (car t2s))))
2511 (when (union-type-p union)
2512 ;; we have to give up here -- there are all sorts of
2513 ;; ordering worries, but it's better than before.
2514 ;; Doing exactly the same as in the UNION
2515 ;; :SIMPLE/:COMPLEX-INTERSECTION2 method causes stack
2516 ;; overflow with the mutual recursion never bottoming
2518 (if (and (eq accumulator *universal-type*)
2520 ;; KLUDGE: if we get here, we have a partially
2521 ;; simplified result. While this isn't by any
2522 ;; means a universal simplification, including
2523 ;; this logic here means that we can get (OR
2524 ;; KEYWORD (NOT KEYWORD)) canonicalized to T.
2528 (type-intersection accumulator union))))))))
2530 (!def-type-translator and (&whole whole &rest type-specifiers)
2531 (apply #'type-intersection
2532 (mapcar #'specifier-type
2537 (!define-type-class union)
2539 ;;; The LIST, FLOAT and REAL types have special names. Other union
2540 ;;; types just get mechanically unparsed.
2541 (!define-type-method (union :unparse) (type)
2542 (declare (type ctype type))
2544 ((type= type (specifier-type 'list)) 'list)
2545 ((type= type (specifier-type 'float)) 'float)
2546 ((type= type (specifier-type 'real)) 'real)
2547 ((type= type (specifier-type 'sequence)) 'sequence)
2548 ((type= type (specifier-type 'bignum)) 'bignum)
2549 (t `(or ,@(mapcar #'type-specifier (union-type-types type))))))
2551 ;;; Two union types are equal if they are each subtypes of each
2552 ;;; other. We need to be this clever because our complex subtypep
2553 ;;; methods are now more accurate; we don't get infinite recursion
2554 ;;; because the simple-subtypep method delegates to complex-subtypep
2555 ;;; of the individual types of type1. - CSR, 2002-04-09
2557 ;;; Previous comment, now obsolete, but worth keeping around because
2558 ;;; it is true, though too strong a condition:
2560 ;;; Two union types are equal if their subtypes are equal sets.
2561 (!define-type-method (union :simple-=) (type1 type2)
2562 (multiple-value-bind (subtype certain?)
2563 (csubtypep type1 type2)
2565 (csubtypep type2 type1)
2566 ;; we might as well become as certain as possible.
2569 (multiple-value-bind (subtype certain?)
2570 (csubtypep type2 type1)
2571 (declare (ignore subtype))
2572 (values nil certain?))))))
2574 (!define-type-method (union :complex-=) (type1 type2)
2575 (declare (ignore type1))
2576 (if (some #'type-might-contain-other-types-p
2577 (union-type-types type2))
2581 ;;; Similarly, a union type is a subtype of another if and only if
2582 ;;; every element of TYPE1 is a subtype of TYPE2.
2583 (defun union-simple-subtypep (type1 type2)
2584 (every/type (swapped-args-fun #'union-complex-subtypep-arg2)
2586 (union-type-types type1)))
2588 (!define-type-method (union :simple-subtypep) (type1 type2)
2589 (union-simple-subtypep type1 type2))
2591 (defun union-complex-subtypep-arg1 (type1 type2)
2592 (every/type (swapped-args-fun #'csubtypep)
2594 (union-type-types type1)))
2596 (!define-type-method (union :complex-subtypep-arg1) (type1 type2)
2597 (union-complex-subtypep-arg1 type1 type2))
2599 (defun union-complex-subtypep-arg2 (type1 type2)
2600 (multiple-value-bind (sub-value sub-certain?)
2601 ;; was: (any/type #'csubtypep type1 (union-type-types type2)),
2602 ;; which turns out to be too restrictive, causing bug 91.
2604 ;; the following reimplementation might look dodgy. It is
2605 ;; dodgy. It depends on the union :complex-= method not doing
2606 ;; very much work -- certainly, not using subtypep. Reasoning:
2608 ;; At this stage, we know that type2 is a union type and type1
2609 ;; isn't. We might as well check this, though:
2610 (aver (union-type-p type2))
2611 (aver (not (union-type-p type1)))
2612 ;; A is a subset of (B1 u B2)
2613 ;; <=> A n (B1 u B2) = A
2614 ;; <=> (A n B1) u (A n B2) = A
2616 ;; But, we have to be careful not to delegate this type= to
2617 ;; something that could invoke subtypep, which might get us
2618 ;; back here -> stack explosion. We therefore ensure that the
2619 ;; second type (which is the one that's dispatched on) is
2620 ;; either a union type (where we've ensured that the complex-=
2621 ;; method will not call subtypep) or something with no union
2622 ;; types involved, in which case we'll never come back here.
2624 ;; If we don't do this, then e.g.
2625 ;; (SUBTYPEP '(MEMBER 3) '(OR (SATISFIES FOO) (SATISFIES BAR)))
2626 ;; would loop infinitely, as the member :complex-= method is
2627 ;; implemented in terms of subtypep.
2629 ;; Ouch. - CSR, 2002-04-10
2632 (mapcar (lambda (x) (type-intersection type1 x))
2633 (union-type-types type2)))))
2635 (values sub-value sub-certain?)
2636 ;; The ANY/TYPE expression above is a sufficient condition for
2637 ;; subsetness, but not a necessary one, so we might get a more
2638 ;; certain answer by this CALL-NEXT-METHOD-ish step when the
2639 ;; ANY/TYPE expression is uncertain.
2640 (invoke-complex-subtypep-arg1-method type1 type2))))
2642 (!define-type-method (union :complex-subtypep-arg2) (type1 type2)
2643 (union-complex-subtypep-arg2 type1 type2))
2645 (!define-type-method (union :simple-intersection2 :complex-intersection2)
2647 ;; The CSUBTYPEP clauses here let us simplify e.g.
2648 ;; (TYPE-INTERSECTION2 (SPECIFIER-TYPE 'LIST)
2649 ;; (SPECIFIER-TYPE '(OR LIST VECTOR)))
2650 ;; (where LIST is (OR CONS NULL)).
2652 ;; The tests are more or less (CSUBTYPEP TYPE1 TYPE2) and vice
2653 ;; versa, but it's important that we pre-expand them into
2654 ;; specialized operations on individual elements of
2655 ;; UNION-TYPE-TYPES, instead of using the ordinary call to
2656 ;; CSUBTYPEP, in order to avoid possibly invoking any methods which
2657 ;; might in turn invoke (TYPE-INTERSECTION2 TYPE1 TYPE2) and thus
2658 ;; cause infinite recursion.
2660 ;; Within this method, type2 is guaranteed to be a union type:
2661 (aver (union-type-p type2))
2662 ;; Make sure to call only the applicable methods...
2663 (cond ((and (union-type-p type1)
2664 (union-simple-subtypep type1 type2)) type1)
2665 ((and (union-type-p type1)
2666 (union-simple-subtypep type2 type1)) type2)
2667 ((and (not (union-type-p type1))
2668 (union-complex-subtypep-arg2 type1 type2))
2670 ((and (not (union-type-p type1))
2671 (union-complex-subtypep-arg1 type2 type1))
2674 ;; KLUDGE: This code accumulates a sequence of TYPE-UNION2
2675 ;; operations in a particular order, and gives up if any of
2676 ;; the sub-unions turn out not to be simple. In other cases
2677 ;; ca. sbcl-0.6.11.15, that approach to taking a union was a
2678 ;; bad idea, since it can overlook simplifications which
2679 ;; might occur if the terms were accumulated in a different
2680 ;; order. It's possible that that will be a problem here too.
2681 ;; However, I can't think of a good example to demonstrate
2682 ;; it, and without an example to demonstrate it I can't write
2683 ;; test cases, and without test cases I don't want to
2684 ;; complicate the code to address what's still a hypothetical
2685 ;; problem. So I punted. -- WHN 2001-03-20
2686 (let ((accumulator *empty-type*))
2687 (dolist (t2 (union-type-types type2) accumulator)
2689 (type-union accumulator
2690 (type-intersection type1 t2))))))))
2692 (!def-type-translator or (&rest type-specifiers)
2694 (mapcar #'specifier-type
2699 (!define-type-class cons)
2701 (!def-type-translator cons (&optional (car-type-spec '*) (cdr-type-spec '*))
2702 (let ((car-type (single-value-specifier-type car-type-spec))
2703 (cdr-type (single-value-specifier-type cdr-type-spec)))
2704 (make-cons-type car-type cdr-type)))
2706 (!define-type-method (cons :unparse) (type)
2707 (let ((car-eltype (type-specifier (cons-type-car-type type)))
2708 (cdr-eltype (type-specifier (cons-type-cdr-type type))))
2709 (if (and (member car-eltype '(t *))
2710 (member cdr-eltype '(t *)))
2712 `(cons ,car-eltype ,cdr-eltype))))
2714 (!define-type-method (cons :simple-=) (type1 type2)
2715 (declare (type cons-type type1 type2))
2716 (and (type= (cons-type-car-type type1) (cons-type-car-type type2))
2717 (type= (cons-type-cdr-type type1) (cons-type-cdr-type type2))))
2719 (!define-type-method (cons :simple-subtypep) (type1 type2)
2720 (declare (type cons-type type1 type2))
2721 (multiple-value-bind (val-car win-car)
2722 (csubtypep (cons-type-car-type type1) (cons-type-car-type type2))
2723 (multiple-value-bind (val-cdr win-cdr)
2724 (csubtypep (cons-type-cdr-type type1) (cons-type-cdr-type type2))
2725 (if (and val-car val-cdr)
2726 (values t (and win-car win-cdr))
2727 (values nil (or win-car win-cdr))))))
2729 ;;; Give up if a precise type is not possible, to avoid returning
2730 ;;; overly general types.
2731 (!define-type-method (cons :simple-union2) (type1 type2)
2732 (declare (type cons-type type1 type2))
2733 (let ((car-type1 (cons-type-car-type type1))
2734 (car-type2 (cons-type-car-type type2))
2735 (cdr-type1 (cons-type-cdr-type type1))
2736 (cdr-type2 (cons-type-cdr-type type2)))
2737 ;; UGH. -- CSR, 2003-02-24
2738 (macrolet ((frob-car (car1 car2 cdr1 cdr2)
2740 (make-cons-type ,car1 (type-union ,cdr1 ,cdr2))
2742 (type-intersection ,car2
2744 `(not ,(type-specifier ,car1))))
2746 (cond ((type= car-type1 car-type2)
2747 (make-cons-type car-type1
2748 (type-union cdr-type1 cdr-type2)))
2749 ((type= cdr-type1 cdr-type2)
2750 (make-cons-type (type-union car-type1 car-type2)
2752 ((csubtypep car-type1 car-type2)
2753 (frob-car car-type1 car-type2 cdr-type1 cdr-type2))
2754 ((csubtypep car-type2 car-type1)
2755 (frob-car car-type2 car-type1 cdr-type2 cdr-type1))
2756 ;; Don't put these in -- consider the effect of taking the
2757 ;; union of (CONS (INTEGER 0 2) (INTEGER 5 7)) and
2758 ;; (CONS (INTEGER 0 3) (INTEGER 5 6)).
2760 ((csubtypep cdr-type1 cdr-type2)
2761 (frob-cdr car-type1 car-type2 cdr-type1 cdr-type2))
2763 ((csubtypep cdr-type2 cdr-type1)
2764 (frob-cdr car-type2 car-type1 cdr-type2 cdr-type1))))))
2766 (!define-type-method (cons :simple-intersection2) (type1 type2)
2767 (declare (type cons-type type1 type2))
2770 (and (setf car-int2 (type-intersection2 (cons-type-car-type type1)
2771 (cons-type-car-type type2)))
2772 (setf cdr-int2 (type-intersection2 (cons-type-cdr-type type1)
2773 (cons-type-cdr-type type2)))
2774 (make-cons-type car-int2 cdr-int2))))
2776 ;;; Return the type that describes all objects that are in X but not
2777 ;;; in Y. If we can't determine this type, then return NIL.
2779 ;;; For now, we only are clever dealing with union and member types.
2780 ;;; If either type is not a union type, then we pretend that it is a
2781 ;;; union of just one type. What we do is remove from X all the types
2782 ;;; that are a subtype any type in Y. If any type in X intersects with
2783 ;;; a type in Y but is not a subtype, then we give up.
2785 ;;; We must also special-case any member type that appears in the
2786 ;;; union. We remove from X's members all objects that are TYPEP to Y.
2787 ;;; If Y has any members, we must be careful that none of those
2788 ;;; members are CTYPEP to any of Y's non-member types. We give up in
2789 ;;; this case, since to compute that difference we would have to break
2790 ;;; the type from X into some collection of types that represents the
2791 ;;; type without that particular element. This seems too hairy to be
2792 ;;; worthwhile, given its low utility.
2793 (defun type-difference (x y)
2794 (let ((x-types (if (union-type-p x) (union-type-types x) (list x)))
2795 (y-types (if (union-type-p y) (union-type-types y) (list y))))
2797 (dolist (x-type x-types)
2798 (if (member-type-p x-type)
2799 (collect ((members))
2800 (dolist (mem (member-type-members x-type))
2801 (multiple-value-bind (val win) (ctypep mem y)
2802 (unless win (return-from type-difference nil))
2806 (res (make-member-type :members (members)))))
2807 (dolist (y-type y-types (res x-type))
2808 (multiple-value-bind (val win) (csubtypep x-type y-type)
2809 (unless win (return-from type-difference nil))
2811 (when (types-equal-or-intersect x-type y-type)
2812 (return-from type-difference nil))))))
2813 (let ((y-mem (find-if #'member-type-p y-types)))
2815 (let ((members (member-type-members y-mem)))
2816 (dolist (x-type x-types)
2817 (unless (member-type-p x-type)
2818 (dolist (member members)
2819 (multiple-value-bind (val win) (ctypep member x-type)
2820 (when (or (not win) val)
2821 (return-from type-difference nil)))))))))
2822 (apply #'type-union (res)))))
2824 (!def-type-translator array (&optional (element-type '*)
2826 (specialize-array-type
2827 (make-array-type :dimensions (canonical-array-dimensions dimensions)
2829 :element-type (if (eq element-type '*)
2831 (specifier-type element-type)))))
2833 (!def-type-translator simple-array (&optional (element-type '*)
2835 (specialize-array-type
2836 (make-array-type :dimensions (canonical-array-dimensions dimensions)
2838 :element-type (if (eq element-type '*)
2840 (specifier-type element-type)))))
2842 ;;;; utilities shared between cross-compiler and target system
2844 ;;; Does the type derived from compilation of an actual function
2845 ;;; definition satisfy declarations of a function's type?
2846 (defun defined-ftype-matches-declared-ftype-p (defined-ftype declared-ftype)
2847 (declare (type ctype defined-ftype declared-ftype))
2848 (flet ((is-built-in-class-function-p (ctype)
2849 (and (built-in-classoid-p ctype)
2850 (eq (built-in-classoid-name ctype) 'function))))
2851 (cond (;; DECLARED-FTYPE could certainly be #<BUILT-IN-CLASS FUNCTION>;
2852 ;; that's what happens when we (DECLAIM (FTYPE FUNCTION FOO)).
2853 (is-built-in-class-function-p declared-ftype)
2854 ;; In that case, any definition satisfies the declaration.
2856 (;; It's not clear whether or how DEFINED-FTYPE might be
2857 ;; #<BUILT-IN-CLASS FUNCTION>, but it's not obviously
2858 ;; invalid, so let's handle that case too, just in case.
2859 (is-built-in-class-function-p defined-ftype)
2860 ;; No matter what DECLARED-FTYPE might be, we can't prove
2861 ;; that an object of type FUNCTION doesn't satisfy it, so
2862 ;; we return success no matter what.
2864 (;; Otherwise both of them must be FUN-TYPE objects.
2866 ;; FIXME: For now we only check compatibility of the return
2867 ;; type, not argument types, and we don't even check the
2868 ;; return type very precisely (as per bug 94a). It would be
2869 ;; good to do a better job. Perhaps to check the
2870 ;; compatibility of the arguments, we should (1) redo
2871 ;; VALUES-TYPES-EQUAL-OR-INTERSECT as
2872 ;; ARGS-TYPES-EQUAL-OR-INTERSECT, and then (2) apply it to
2873 ;; the ARGS-TYPE slices of the FUN-TYPEs. (ARGS-TYPE
2874 ;; is a base class both of VALUES-TYPE and of FUN-TYPE.)
2875 (values-types-equal-or-intersect
2876 (fun-type-returns defined-ftype)
2877 (fun-type-returns declared-ftype))))))
2879 ;;; This messy case of CTYPE for NUMBER is shared between the
2880 ;;; cross-compiler and the target system.
2881 (defun ctype-of-number (x)
2882 (let ((num (if (complexp x) (realpart x) x)))
2883 (multiple-value-bind (complexp low high)
2885 (let ((imag (imagpart x)))
2886 (values :complex (min num imag) (max num imag)))
2887 (values :real num num))
2888 (make-numeric-type :class (etypecase num
2890 (rational 'rational)
2892 :format (and (floatp num) (float-format-name num))
2898 ;; Why SAFETY 0? To suppress the is-it-the-right-structure-type
2899 ;; checking for declarations in structure accessors. Otherwise we
2900 ;; can get caught in a chicken-and-egg bootstrapping problem, whose
2901 ;; symptom on x86 OpenBSD sbcl-0.pre7.37.flaky5.22 is an illegal
2902 ;; instruction trap. I haven't tracked it down, but I'm guessing it
2903 ;; has to do with setting LAYOUTs when the LAYOUT hasn't been set
2905 (declare (optimize (safety 0)))
2906 (!defun-from-collected-cold-init-forms !late-type-cold-init))
2908 (/show0 "late-type.lisp end of file")