1 ;;;; This file contains the definition of non-CLASS types (e.g.
2 ;;;; subtypes of interesting BUILT-IN-CLASSes) and the interfaces to
3 ;;;; the type system. Common Lisp type specifiers are parsed into a
4 ;;;; somewhat canonical internal type representation that supports
5 ;;;; type union, intersection, etc. (Except that ALIEN types have
8 ;;;; This software is part of the SBCL system. See the README file for
11 ;;;; This software is derived from the CMU CL system, which was
12 ;;;; written at Carnegie Mellon University and released into the
13 ;;;; public domain. The software is in the public domain and is
14 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
15 ;;;; files for more information.
17 (in-package "SB!KERNEL")
19 (/show0 "late-type.lisp 19")
21 (!begin-collecting-cold-init-forms)
23 ;;; ### Remaining incorrectnesses:
25 ;;; TYPE-UNION (and the OR type) doesn't properly canonicalize an
26 ;;; exhaustive partition or coalesce contiguous ranges of numeric
29 ;;; There are all sorts of nasty problems with open bounds on FLOAT
30 ;;; types (and probably FLOAT types in general.)
32 ;;; RATIO and BIGNUM are not recognized as numeric types.
34 ;;; FIXME: This really should go away. Alas, it doesn't seem to be so
35 ;;; simple to make it go away.. (See bug 123 in BUGS file.)
36 (defvar *use-implementation-types* t ; actually initialized in cold init
38 "*USE-IMPLEMENTATION-TYPES* is a semi-public flag which determines how
39 restrictive we are in determining type membership. If two types are the
40 same in the implementation, then we will consider them them the same when
41 this switch is on. When it is off, we try to be as restrictive as the
42 language allows, allowing us to detect more errors. Currently, this only
43 affects array types.")
44 (!cold-init-forms (setq *use-implementation-types* t))
46 ;;; These functions are used as method for types which need a complex
47 ;;; subtypep method to handle some superclasses, but cover a subtree
48 ;;; of the type graph (i.e. there is no simple way for any other type
49 ;;; class to be a subtype.) There are always still complex ways,
50 ;;; namely UNION and MEMBER types, so we must give TYPE1's method a
51 ;;; chance to run, instead of immediately returning NIL, T.
52 (defun delegate-complex-subtypep-arg2 (type1 type2)
54 (type-class-complex-subtypep-arg1
55 (type-class-info type1))))
57 (funcall subtypep-arg1 type1 type2)
59 (defun delegate-complex-intersection2 (type1 type2)
60 (let ((method (type-class-complex-intersection2 (type-class-info type1))))
61 (if (and method (not (eq method #'delegate-complex-intersection2)))
62 (funcall method type2 type1)
63 (hierarchical-intersection2 type1 type2))))
65 ;;; This is used by !DEFINE-SUPERCLASSES to define the SUBTYPE-ARG1
66 ;;; method. INFO is a list of conses
67 ;;; (SUPERCLASS-CLASS . {GUARD-TYPE-SPECIFIER | NIL}).
68 (defun !has-superclasses-complex-subtypep-arg1 (type1 type2 info)
69 ;; If TYPE2 might be concealing something related to our class
71 (if (type-might-contain-other-types-p type2)
72 ;; too confusing, gotta punt
74 ;; ordinary case expected by old CMU CL code, where the taxonomy
75 ;; of TYPE2's representation accurately reflects the taxonomy of
78 ;; FIXME: This old CMU CL code probably deserves a comment
79 ;; explaining to us mere mortals how it works...
80 (and (sb!xc:typep type2 'classoid)
82 (when (or (not (cdr x))
83 (csubtypep type1 (specifier-type (cdr x))))
85 (or (eq type2 (car x))
86 (let ((inherits (layout-inherits
87 (classoid-layout (car x)))))
88 (dotimes (i (length inherits) nil)
89 (when (eq type2 (layout-classoid (svref inherits i)))
93 ;;; This function takes a list of specs, each of the form
94 ;;; (SUPERCLASS-NAME &OPTIONAL GUARD).
95 ;;; Consider one spec (with no guard): any instance of the named
96 ;;; TYPE-CLASS is also a subtype of the named superclass and of any of
97 ;;; its superclasses. If there are multiple specs, then some will have
98 ;;; guards. We choose the first spec whose guard is a supertype of
99 ;;; TYPE1 and use its superclass. In effect, a sequence of guards
102 ;;; G0,(and G1 (not G0)), (and G2 (not (or G0 G1))).
104 ;;; WHEN controls when the forms are executed.
105 (defmacro !define-superclasses (type-class-name specs when)
106 (let ((type-class (gensym "TYPE-CLASS-"))
107 (info (gensym "INFO")))
109 (let ((,type-class (type-class-or-lose ',type-class-name))
110 (,info (mapcar (lambda (spec)
112 (super &optional guard)
114 (cons (find-classoid super) guard)))
116 (setf (type-class-complex-subtypep-arg1 ,type-class)
117 (lambda (type1 type2)
118 (!has-superclasses-complex-subtypep-arg1 type1 type2 ,info)))
119 (setf (type-class-complex-subtypep-arg2 ,type-class)
120 #'delegate-complex-subtypep-arg2)
121 (setf (type-class-complex-intersection2 ,type-class)
122 #'delegate-complex-intersection2)))))
124 ;;;; FUNCTION and VALUES types
126 ;;;; Pretty much all of the general type operations are illegal on
127 ;;;; VALUES types, since we can't discriminate using them, do
128 ;;;; SUBTYPEP, etc. FUNCTION types are acceptable to the normal type
129 ;;;; operations, but are generally considered to be equivalent to
130 ;;;; FUNCTION. These really aren't true types in any type theoretic
131 ;;;; sense, but we still parse them into CTYPE structures for two
134 ;;;; -- Parsing and unparsing work the same way, and indeed we can't
135 ;;;; tell whether a type is a function or values type without
137 ;;;; -- Many of the places that can be annotated with real types can
138 ;;;; also be annotated with function or values types.
140 ;;; the description of a &KEY argument
141 (defstruct (key-info #-sb-xc-host (:pure t)
143 ;; the key (not necessarily a keyword in ANSI Common Lisp)
144 (name (missing-arg) :type symbol)
145 ;; the type of the argument value
146 (type (missing-arg) :type ctype))
148 (!define-type-method (values :simple-subtypep :complex-subtypep-arg1)
150 (declare (ignore type2))
151 ;; FIXME: should be TYPE-ERROR, here and in next method
152 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type1)))
154 (!define-type-method (values :complex-subtypep-arg2)
156 (declare (ignore type1))
157 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type2)))
159 (!define-type-method (values :unparse) (type)
160 (cons 'values (unparse-args-types type)))
162 ;;; Return true if LIST1 and LIST2 have the same elements in the same
163 ;;; positions according to TYPE=. We return NIL, NIL if there is an
164 ;;; uncertain comparison.
165 (defun type=-list (list1 list2)
166 (declare (list list1 list2))
167 (do ((types1 list1 (cdr types1))
168 (types2 list2 (cdr types2)))
169 ((or (null types1) (null types2))
170 (if (or types1 types2)
173 (multiple-value-bind (val win)
174 (type= (first types1) (first types2))
176 (return (values nil nil)))
178 (return (values nil t))))))
180 (!define-type-method (values :simple-=) (type1 type2)
181 (let ((rest1 (args-type-rest type1))
182 (rest2 (args-type-rest type2)))
183 (cond ((or (args-type-keyp type1) (args-type-keyp type2)
184 (args-type-allowp type1) (args-type-allowp type2))
186 ((and rest1 rest2 (type/= rest1 rest2))
191 (multiple-value-bind (req-val req-win)
192 (type=-list (values-type-required type1)
193 (values-type-required type2))
194 (multiple-value-bind (opt-val opt-win)
195 (type=-list (values-type-optional type1)
196 (values-type-optional type2))
197 (values (and req-val opt-val) (and req-win opt-win))))))))
199 (!define-type-class function)
201 ;;; a flag that we can bind to cause complex function types to be
202 ;;; unparsed as FUNCTION. This is useful when we want a type that we
203 ;;; can pass to TYPEP.
204 (defvar *unparse-fun-type-simplify*)
205 (!cold-init-forms (setq *unparse-fun-type-simplify* nil))
207 (!define-type-method (function :unparse) (type)
208 (if *unparse-fun-type-simplify*
211 (if (fun-type-wild-args type)
213 (unparse-args-types type))
215 (fun-type-returns type)))))
217 ;;; The meaning of this is a little confused. On the one hand, all
218 ;;; function objects are represented the same way regardless of the
219 ;;; arglists and return values, and apps don't get to ask things like
220 ;;; (TYPEP #'FOO (FUNCTION (FIXNUM) *)) in any meaningful way. On the
221 ;;; other hand, Python wants to reason about function types. So...
222 (!define-type-method (function :simple-subtypep) (type1 type2)
223 (flet ((fun-type-simple-p (type)
224 (not (or (fun-type-rest type)
225 (fun-type-keyp type))))
226 (every-csubtypep (types1 types2)
230 do (multiple-value-bind (res sure-p)
232 (unless res (return (values res sure-p))))
233 finally (return (values t t)))))
234 (and/type (values-subtypep (fun-type-returns type1)
235 (fun-type-returns type2))
236 (cond ((fun-type-wild-args type2) (values t t))
237 ((fun-type-wild-args type1)
238 (cond ((fun-type-keyp type2) (values nil nil))
239 ((not (fun-type-rest type2)) (values nil t))
240 ((not (null (fun-type-required type2))) (values nil t))
241 (t (and/type (type= *universal-type* (fun-type-rest type2))
242 (every/type #'type= *universal-type*
243 (fun-type-optional type2))))))
244 ((not (and (fun-type-simple-p type1)
245 (fun-type-simple-p type2)))
247 (t (multiple-value-bind (min1 max1) (fun-type-nargs type1)
248 (multiple-value-bind (min2 max2) (fun-type-nargs type2)
249 (cond ((or (> max1 max2) (< min1 min2))
251 ((and (= min1 min2) (= max1 max2))
252 (and/type (every-csubtypep (fun-type-required type1)
253 (fun-type-required type2))
254 (every-csubtypep (fun-type-optional type1)
255 (fun-type-optional type2))))
258 (fun-type-required type1)
259 (fun-type-optional type1))
261 (fun-type-required type2)
262 (fun-type-optional type2))))))))))))
264 (!define-superclasses function ((function)) !cold-init-forms)
266 ;;; The union or intersection of two FUNCTION types is FUNCTION.
267 (!define-type-method (function :simple-union2) (type1 type2)
268 (declare (ignore type1 type2))
269 (specifier-type 'function))
270 (!define-type-method (function :simple-intersection2) (type1 type2)
271 (declare (ignore type1 type2))
272 (specifier-type 'function))
274 ;;; ### Not very real, but good enough for redefining transforms
275 ;;; according to type:
276 (!define-type-method (function :simple-=) (type1 type2)
277 (values (equalp type1 type2) t))
279 (!define-type-class constant :inherits values)
281 (!define-type-method (constant :unparse) (type)
282 `(constant-arg ,(type-specifier (constant-type-type type))))
284 (!define-type-method (constant :simple-=) (type1 type2)
285 (type= (constant-type-type type1) (constant-type-type type2)))
287 (!def-type-translator constant-arg (type)
288 (make-constant-type :type (specifier-type type)))
290 ;;; Given a LAMBDA-LIST-like values type specification and an ARGS-TYPE
291 ;;; structure, fill in the slots in the structure accordingly. This is
292 ;;; used for both FUNCTION and VALUES types.
293 (declaim (ftype (function (list args-type) (values)) parse-args-types))
294 (defun parse-args-types (lambda-list result)
295 (multiple-value-bind (required optional restp rest keyp keys allowp auxp aux)
296 (parse-lambda-list-like-thing lambda-list)
297 (declare (ignore aux)) ; since we require AUXP=NIL
299 (error "&AUX in a FUNCTION or VALUES type: ~S." lambda-list))
300 (setf (args-type-required result)
301 (mapcar #'single-value-specifier-type required))
302 (setf (args-type-optional result)
303 (mapcar #'single-value-specifier-type optional))
304 (setf (args-type-rest result)
305 (if restp (single-value-specifier-type rest) nil))
306 (setf (args-type-keyp result) keyp)
307 (collect ((key-info))
309 (unless (proper-list-of-length-p key 2)
310 (error "Keyword type description is not a two-list: ~S." key))
311 (let ((kwd (first key)))
312 (when (find kwd (key-info) :key #'key-info-name)
313 (error "~@<repeated keyword ~S in lambda list: ~2I~_~S~:>"
315 (key-info (make-key-info :name kwd
316 :type (single-value-specifier-type (second key))))))
317 (setf (args-type-keywords result) (key-info)))
318 (setf (args-type-allowp result) allowp)
321 ;;; Return the lambda-list-like type specification corresponding
323 (declaim (ftype (function (args-type) list) unparse-args-types))
324 (defun unparse-args-types (type)
327 (dolist (arg (args-type-required type))
328 (result (type-specifier arg)))
330 (when (args-type-optional type)
332 (dolist (arg (args-type-optional type))
333 (result (type-specifier arg))))
335 (when (args-type-rest type)
337 (result (type-specifier (args-type-rest type))))
339 (when (args-type-keyp type)
341 (dolist (key (args-type-keywords type))
342 (result (list (key-info-name key)
343 (type-specifier (key-info-type key))))))
345 (when (args-type-allowp type)
346 (result '&allow-other-keys))
350 (!def-type-translator function (&optional (args '*) (result '*))
351 (let ((res (make-fun-type :returns (values-specifier-type result))))
353 (setf (fun-type-wild-args res) t)
354 (parse-args-types args res))
357 (!def-type-translator values (&rest values)
358 (let ((res (%make-values-type)))
359 (parse-args-types values res)
362 ;;;; VALUES types interfaces
364 ;;;; We provide a few special operations that can be meaningfully used
365 ;;;; on VALUES types (as well as on any other type).
367 ;;; Return the type of the first value indicated by TYPE. This is used
368 ;;; by people who don't want to have to deal with VALUES types.
369 #!-sb-fluid (declaim (freeze-type values-type))
370 ; (inline single-value-type))
371 (defun single-value-type (type)
372 (declare (type ctype type))
373 (cond ((values-type-p type)
374 (or (car (args-type-required type))
375 (if (args-type-optional type)
376 (type-union (car (args-type-optional type))
377 (specifier-type 'null)))
378 (args-type-rest type)
379 (specifier-type 'null)))
380 ((eq type *wild-type*)
385 ;;; Return the minimum number of arguments that a function can be
386 ;;; called with, and the maximum number or NIL. If not a function
387 ;;; type, return NIL, NIL.
388 (defun fun-type-nargs (type)
389 (declare (type ctype type))
390 (if (fun-type-p type)
391 (let ((fixed (length (args-type-required type))))
392 (if (or (args-type-rest type)
393 (args-type-keyp type)
394 (args-type-allowp type))
396 (values fixed (+ fixed (length (args-type-optional type))))))
399 ;;; Determine whether TYPE corresponds to a definite number of values.
400 ;;; The first value is a list of the types for each value, and the
401 ;;; second value is the number of values. If the number of values is
402 ;;; not fixed, then return NIL and :UNKNOWN.
403 (defun values-types (type)
404 (declare (type ctype type))
405 (cond ((eq type *wild-type*)
406 (values nil :unknown))
407 ((not (values-type-p type))
408 (values (list type) 1))
409 ((or (args-type-optional type)
410 (args-type-rest type)
411 (args-type-keyp type)
412 (args-type-allowp type))
413 (values nil :unknown))
415 (let ((req (args-type-required type)))
416 (values (mapcar #'single-value-type req) (length req))))))
418 ;;; Return two values:
419 ;;; 1. A list of all the positional (fixed and optional) types.
420 ;;; 2. The &REST type (if any). If keywords allowed, *UNIVERSAL-TYPE*.
421 ;;; If no keywords or &REST, then the DEFAULT-TYPE.
422 (defun values-type-types (type &optional (default-type *empty-type*))
423 (declare (type values-type type))
424 (values (append (args-type-required type)
425 (args-type-optional type))
426 (cond ((args-type-keyp type) *universal-type*)
427 ((args-type-rest type))
431 ;;; Return a list of OPERATION applied to the types in TYPES1 and
432 ;;; TYPES2, padding with REST2 as needed. TYPES1 must not be shorter
433 ;;; than TYPES2. The second value is T if OPERATION always returned a
434 ;;; true second value.
435 (defun fixed-values-op (types1 types2 rest2 operation)
436 (declare (list types1 types2) (type ctype rest2) (type function operation))
438 (values (mapcar (lambda (t1 t2)
439 (multiple-value-bind (res win)
440 (funcall operation t1 t2)
446 (make-list (- (length types1) (length types2))
447 :initial-element rest2)))
450 ;;; If TYPE isn't a values type, then make it into one:
451 ;;; <type> ==> (values type &rest t)
452 (defun coerce-to-values (type)
453 (declare (type ctype type))
454 (if (values-type-p type)
456 (make-values-type :required (list type) :rest *universal-type*)))
458 ;;; Do the specified OPERATION on TYPE1 and TYPE2, which may be any
459 ;;; type, including VALUES types. With VALUES types such as:
462 ;;; we compute the more useful result
463 ;;; (VALUES (<operation> a0 b0) (<operation> a1 b1))
464 ;;; rather than the precise result
465 ;;; (<operation> (values a0 a1) (values b0 b1))
466 ;;; This has the virtue of always keeping the VALUES type specifier
467 ;;; outermost, and retains all of the information that is really
468 ;;; useful for static type analysis. We want to know what is always
469 ;;; true of each value independently. It is worthless to know that if
470 ;;; the first value is B0 then the second will be B1.
472 ;;; If the VALUES count signatures differ, then we produce a result with
473 ;;; the required VALUE count chosen by NREQ when applied to the number
474 ;;; of required values in TYPE1 and TYPE2. Any &KEY values become
475 ;;; &REST T (anyone who uses keyword values deserves to lose.)
477 ;;; The second value is true if the result is definitely empty or if
478 ;;; OPERATION returned true as its second value each time we called
479 ;;; it. Since we approximate the intersection of VALUES types, the
480 ;;; second value being true doesn't mean the result is exact.
481 (defun args-type-op (type1 type2 operation nreq default-type)
482 (declare (type ctype type1 type2 default-type)
483 (type function operation nreq))
484 (when (eq type1 type2)
486 (if (or (values-type-p type1) (values-type-p type2))
487 (let ((type1 (coerce-to-values type1))
488 (type2 (coerce-to-values type2)))
489 (multiple-value-bind (types1 rest1)
490 (values-type-types type1 default-type)
491 (multiple-value-bind (types2 rest2)
492 (values-type-types type2 default-type)
493 (multiple-value-bind (rest rest-exact)
494 (funcall operation rest1 rest2)
495 (multiple-value-bind (res res-exact)
496 (if (< (length types1) (length types2))
497 (fixed-values-op types2 types1 rest1 operation)
498 (fixed-values-op types1 types2 rest2 operation))
499 (let* ((req (funcall nreq
500 (length (args-type-required type1))
501 (length (args-type-required type2))))
502 (required (subseq res 0 req))
503 (opt (subseq res req))
504 (opt-last (position rest opt :test-not #'type=
506 (if (find *empty-type* required :test #'type=)
507 (values *empty-type* t)
508 (values (make-values-type
510 :optional (if opt-last
511 (subseq opt 0 (1+ opt-last))
513 :rest (if (eq rest default-type) nil rest))
514 (and rest-exact res-exact)))))))))
515 (funcall operation type1 type2)))
517 ;;; Do a union or intersection operation on types that might be values
518 ;;; types. The result is optimized for utility rather than exactness,
519 ;;; but it is guaranteed that it will be no smaller (more restrictive)
520 ;;; than the precise result.
522 ;;; The return convention seems to be analogous to
523 ;;; TYPES-EQUAL-OR-INTERSECT. -- WHN 19990910.
524 (defun-cached (values-type-union :hash-function type-cache-hash
527 :init-wrapper !cold-init-forms)
528 ((type1 eq) (type2 eq))
529 (declare (type ctype type1 type2))
530 (cond ((or (eq type1 *wild-type*) (eq type2 *wild-type*)) *wild-type*)
531 ((eq type1 *empty-type*) type2)
532 ((eq type2 *empty-type*) type1)
534 (values (args-type-op type1 type2 #'type-union #'min *empty-type*)))))
535 (defun-cached (values-type-intersection :hash-function type-cache-hash
538 :default (values nil :empty)
539 :init-wrapper !cold-init-forms)
540 ((type1 eq) (type2 eq))
541 (declare (type ctype type1 type2))
542 (cond ((eq type1 *wild-type*) (values type2 t))
543 ((eq type2 *wild-type*) (values type1 t))
545 (args-type-op type1 type2
548 (specifier-type 'null)))))
550 ;;; This is like TYPES-EQUAL-OR-INTERSECT, except that it sort of
551 ;;; works on VALUES types. Note that due to the semantics of
552 ;;; VALUES-TYPE-INTERSECTION, this might return (VALUES T T) when
553 ;;; there isn't really any intersection.
554 (defun values-types-equal-or-intersect (type1 type2)
555 (cond ((or (eq type1 *empty-type*) (eq type2 *empty-type*))
557 ((or (values-type-p type1) (values-type-p type2))
558 (multiple-value-bind (res win) (values-type-intersection type1 type2)
559 (values (not (eq res *empty-type*))
562 (types-equal-or-intersect type1 type2))))
564 ;;; a SUBTYPEP-like operation that can be used on any types, including
566 (defun-cached (values-subtypep :hash-function type-cache-hash
569 :default (values nil :empty)
570 :init-wrapper !cold-init-forms)
571 ((type1 eq) (type2 eq))
572 (declare (type ctype type1 type2))
573 (cond ((eq type2 *wild-type*) (values t t))
574 ((eq type1 *wild-type*)
575 (values (eq type2 *universal-type*) t))
576 ((not (values-types-equal-or-intersect type1 type2))
579 (if (or (values-type-p type1) (values-type-p type2))
580 (let ((type1 (coerce-to-values type1))
581 (type2 (coerce-to-values type2)))
582 (multiple-value-bind (types1 rest1) (values-type-types type1)
583 (multiple-value-bind (types2 rest2) (values-type-types type2)
584 (cond ((< (length (values-type-required type1))
585 (length (values-type-required type2)))
587 ((< (length types1) (length types2))
589 ((or (values-type-keyp type1)
590 (values-type-keyp type2))
593 (do ((t1 types1 (rest t1))
594 (t2 types2 (rest t2)))
596 (csubtypep rest1 rest2))
597 (multiple-value-bind (res win-p)
598 (csubtypep (first t1) (first t2))
600 (return (values nil nil)))
602 (return (values nil t))))))))))
603 (csubtypep type1 type2)))))
605 ;;;; type method interfaces
607 ;;; like SUBTYPEP, only works on CTYPE structures
608 (defun-cached (csubtypep :hash-function type-cache-hash
611 :default (values nil :empty)
612 :init-wrapper !cold-init-forms)
613 ((type1 eq) (type2 eq))
614 (declare (type ctype type1 type2))
615 (cond ((or (eq type1 type2)
616 (eq type1 *empty-type*)
617 (eq type2 *wild-type*))
619 ((eq type1 *wild-type*)
622 (!invoke-type-method :simple-subtypep :complex-subtypep-arg2
624 :complex-arg1 :complex-subtypep-arg1))))
626 ;;; Just parse the type specifiers and call CSUBTYPE.
627 (defun sb!xc:subtypep (type1 type2 &optional environment)
629 "Return two values indicating the relationship between type1 and type2.
630 If values are T and T, type1 definitely is a subtype of type2.
631 If values are NIL and T, type1 definitely is not a subtype of type2.
632 If values are NIL and NIL, it couldn't be determined."
633 (declare (ignore environment))
634 (csubtypep (specifier-type type1) (specifier-type type2)))
636 ;;; If two types are definitely equivalent, return true. The second
637 ;;; value indicates whether the first value is definitely correct.
638 ;;; This should only fail in the presence of HAIRY types.
639 (defun-cached (type= :hash-function type-cache-hash
642 :default (values nil :empty)
643 :init-wrapper !cold-init-forms)
644 ((type1 eq) (type2 eq))
645 (declare (type ctype type1 type2))
648 (!invoke-type-method :simple-= :complex-= type1 type2)))
650 ;;; Not exactly the negation of TYPE=, since when the relationship is
651 ;;; uncertain, we still return NIL, NIL. This is useful in cases where
652 ;;; the conservative assumption is =.
653 (defun type/= (type1 type2)
654 (declare (type ctype type1 type2))
655 (multiple-value-bind (res win) (type= type1 type2)
660 ;;; the type method dispatch case of TYPE-UNION2
661 (defun %type-union2 (type1 type2)
662 ;; As in %TYPE-INTERSECTION2, it seems to be a good idea to give
663 ;; both argument orders a chance at COMPLEX-INTERSECTION2. Unlike
664 ;; %TYPE-INTERSECTION2, though, I don't have a specific case which
665 ;; demonstrates this is actually necessary. Also unlike
666 ;; %TYPE-INTERSECTION2, there seems to be no need to distinguish
667 ;; between not finding a method and having a method return NIL.
669 (!invoke-type-method :simple-union2 :complex-union2
672 (declare (inline 1way))
673 (or (1way type1 type2)
674 (1way type2 type1))))
676 ;;; Find a type which includes both types. Any inexactness is
677 ;;; represented by the fuzzy element types; we return a single value
678 ;;; that is precise to the best of our knowledge. This result is
679 ;;; simplified into the canonical form, thus is not a UNION-TYPE
680 ;;; unless we find no other way to represent the result.
681 (defun-cached (type-union2 :hash-function type-cache-hash
683 :init-wrapper !cold-init-forms)
684 ((type1 eq) (type2 eq))
685 ;; KLUDGE: This was generated from TYPE-INTERSECTION2 by Ye Olde Cut And
686 ;; Paste technique of programming. If it stays around (as opposed to
687 ;; e.g. fading away in favor of some CLOS solution) the shared logic
688 ;; should probably become shared code. -- WHN 2001-03-16
689 (declare (type ctype type1 type2))
690 (cond ((eq type1 type2)
692 ((csubtypep type1 type2) type2)
693 ((csubtypep type2 type1) type1)
694 ((or (union-type-p type1)
695 (union-type-p type2))
696 ;; Unions of UNION-TYPE should have the UNION-TYPE-TYPES
697 ;; values broken out and united separately. The full TYPE-UNION
698 ;; function knows how to do this, so let it handle it.
699 (type-union type1 type2))
701 ;; the ordinary case: we dispatch to type methods
702 (%type-union2 type1 type2))))
704 ;;; the type method dispatch case of TYPE-INTERSECTION2
705 (defun %type-intersection2 (type1 type2)
706 ;; We want to give both argument orders a chance at
707 ;; COMPLEX-INTERSECTION2. Without that, the old CMU CL type
708 ;; methods could give noncommutative results, e.g.
709 ;; (TYPE-INTERSECTION2 *EMPTY-TYPE* SOME-HAIRY-TYPE)
711 ;; (TYPE-INTERSECTION2 SOME-HAIRY-TYPE *EMPTY-TYPE*)
712 ;; => #<NAMED-TYPE NIL>, T
713 ;; We also need to distinguish between the case where we found a
714 ;; type method, and it returned NIL, and the case where we fell
715 ;; through without finding any type method. An example of the first
716 ;; case is the intersection of a HAIRY-TYPE with some ordinary type.
717 ;; An example of the second case is the intersection of two
718 ;; completely-unrelated types, e.g. CONS and NUMBER, or SYMBOL and
721 ;; (Why yes, CLOS probably *would* be nicer..)
723 (!invoke-type-method :simple-intersection2 :complex-intersection2
725 :default :no-type-method-found)))
726 (declare (inline 1way))
727 (let ((xy (1way type1 type2)))
728 (or (and (not (eql xy :no-type-method-found)) xy)
729 (let ((yx (1way type2 type1)))
730 (or (and (not (eql yx :no-type-method-found)) yx)
731 (cond ((and (eql xy :no-type-method-found)
732 (eql yx :no-type-method-found))
735 (aver (and (not xy) (not yx))) ; else handled above
738 (defun-cached (type-intersection2 :hash-function type-cache-hash
742 :init-wrapper !cold-init-forms)
743 ((type1 eq) (type2 eq))
744 (declare (type ctype type1 type2))
745 (cond ((eq type1 type2)
746 ;; FIXME: For some reason, this doesn't catch e.g. type1 =
747 ;; type2 = (SPECIFIER-TYPE
748 ;; 'SOME-UNKNOWN-TYPE). Investigate. - CSR, 2002-04-10
750 ((or (intersection-type-p type1)
751 (intersection-type-p type2))
752 ;; Intersections of INTERSECTION-TYPE should have the
753 ;; INTERSECTION-TYPE-TYPES values broken out and intersected
754 ;; separately. The full TYPE-INTERSECTION function knows how
755 ;; to do that, so let it handle it.
756 (type-intersection type1 type2))
758 ;; the ordinary case: we dispatch to type methods
759 (%type-intersection2 type1 type2))))
761 ;;; Return as restrictive and simple a type as we can discover that is
762 ;;; no more restrictive than the intersection of TYPE1 and TYPE2. At
763 ;;; worst, we arbitrarily return one of the arguments as the first
764 ;;; value (trying not to return a hairy type).
765 (defun type-approx-intersection2 (type1 type2)
766 (cond ((type-intersection2 type1 type2))
767 ((hairy-type-p type1) type2)
770 ;;; a test useful for checking whether a derived type matches a
773 ;;; The first value is true unless the types don't intersect and
774 ;;; aren't equal. The second value is true if the first value is
775 ;;; definitely correct. NIL is considered to intersect with any type.
776 ;;; If T is a subtype of either type, then we also return T, T. This
777 ;;; way we recognize that hairy types might intersect with T.
778 (defun types-equal-or-intersect (type1 type2)
779 (declare (type ctype type1 type2))
780 (if (or (eq type1 *empty-type*) (eq type2 *empty-type*))
782 (let ((intersection2 (type-intersection2 type1 type2)))
783 (cond ((not intersection2)
784 (if (or (csubtypep *universal-type* type1)
785 (csubtypep *universal-type* type2))
788 ((eq intersection2 *empty-type*) (values nil t))
791 ;;; Return a Common Lisp type specifier corresponding to the TYPE
793 (defun type-specifier (type)
794 (declare (type ctype type))
795 (funcall (type-class-unparse (type-class-info type)) type))
797 ;;; (VALUES-SPECIFIER-TYPE and SPECIFIER-TYPE moved from here to
798 ;;; early-type.lisp by WHN ca. 19990201.)
800 ;;; Take a list of type specifiers, computing the translation of each
801 ;;; specifier and defining it as a builtin type.
802 (declaim (ftype (function (list) (values)) precompute-types))
803 (defun precompute-types (specs)
805 (let ((res (specifier-type spec)))
806 (unless (unknown-type-p res)
807 (setf (info :type :builtin spec) res)
808 ;; KLUDGE: the three copies of this idiom in this file (and
809 ;; the one in class.lisp as at sbcl-0.7.4.1x) should be
810 ;; coalesced, or perhaps the error-detecting code that
811 ;; disallows redefinition of :PRIMITIVE types should be
812 ;; rewritten to use *TYPE-SYSTEM-FINALIZED* (rather than
813 ;; *TYPE-SYSTEM-INITIALIZED*). The effect of this is not to
814 ;; cause redefinition errors when precompute-types is called
815 ;; for a second time while building the target compiler using
816 ;; the cross-compiler. -- CSR, trying to explain why this
817 ;; isn't completely wrong, 2002-06-07
818 (setf (info :type :kind spec) #+sb-xc-host :defined #-sb-xc-host :primitive))))
821 ;;;; general TYPE-UNION and TYPE-INTERSECTION operations
823 ;;;; These are fully general operations on CTYPEs: they'll always
824 ;;;; return a CTYPE representing the result.
826 ;;; shared logic for unions and intersections: Stuff TYPE into the
827 ;;; vector TYPES, finding pairs of types which can be simplified by
828 ;;; SIMPLIFY2 (TYPE-UNION2 or TYPE-INTERSECTION2) and replacing them
829 ;;; by their simplified forms.
830 (defun accumulate1-compound-type (type types %compound-type-p simplify2)
831 (declare (type ctype type))
832 (declare (type (vector ctype) types))
833 (declare (type function %compound-type-p simplify2))
834 ;; Any input object satisfying %COMPOUND-TYPE-P should've been
835 ;; broken into components before it reached us.
836 (aver (not (funcall %compound-type-p type)))
837 (dotimes (i (length types) (vector-push-extend type types))
838 (let ((simplified2 (funcall simplify2 type (aref types i))))
840 ;; Discard the old (AREF TYPES I).
841 (setf (aref types i) (vector-pop types))
842 ;; Merge the new SIMPLIFIED2 into TYPES, by tail recursing.
843 ;; (Note that the tail recursion is indirect: we go through
844 ;; ACCUMULATE, not ACCUMULATE1, so that if SIMPLIFIED2 is
845 ;; handled properly if it satisfies %COMPOUND-TYPE-P.)
846 (return (accumulate-compound-type simplified2
853 ;;; shared logic for unions and intersections: Use
854 ;;; ACCUMULATE1-COMPOUND-TYPE to merge TYPE into TYPES, either
855 ;;; all in one step or, if %COMPOUND-TYPE-P is satisfied,
856 ;;; component by component.
857 (defun accumulate-compound-type (type types %compound-type-p simplify2)
858 (declare (type function %compound-type-p simplify2))
859 (flet ((accumulate1 (x)
860 (accumulate1-compound-type x types %compound-type-p simplify2)))
861 (declare (inline accumulate1))
862 (if (funcall %compound-type-p type)
863 (map nil #'accumulate1 (compound-type-types type))
867 ;;; shared logic for unions and intersections: Return a vector of
868 ;;; types representing the same types as INPUT-TYPES, but with
869 ;;; COMPOUND-TYPEs satisfying %COMPOUND-TYPE-P broken up into their
870 ;;; component types, and with any SIMPLY2 simplifications applied.
871 (defun simplified-compound-types (input-types %compound-type-p simplify2)
872 (let ((simplified-types (make-array (length input-types)
876 ;; (This INITIAL-ELEMENT shouldn't
877 ;; matter, but helps avoid type
878 ;; warnings at compile time.)
879 :initial-element *empty-type*)))
880 (dolist (input-type input-types)
881 (accumulate-compound-type input-type
887 ;;; shared logic for unions and intersections: Make a COMPOUND-TYPE
888 ;;; object whose components are the types in TYPES, or skip to special
889 ;;; cases when TYPES is short.
890 (defun make-probably-compound-type (constructor types enumerable identity)
891 (declare (type function constructor))
892 (declare (type (vector ctype) types))
893 (declare (type ctype identity))
897 (t (funcall constructor
899 ;; FIXME: This should be just (COERCE TYPES 'LIST), but as
900 ;; of sbcl-0.6.11.17 the COERCE optimizer is really
901 ;; brain-dead, so that would generate a full call to
902 ;; SPECIFIER-TYPE at runtime, so we get into bootstrap
903 ;; problems in cold init because 'LIST is a compound
904 ;; type, so we need to MAKE-PROBABLY-COMPOUND-TYPE
905 ;; before we know what 'LIST is. Once the COERCE
906 ;; optimizer is less brain-dead, we can make this
907 ;; (COERCE TYPES 'LIST) again.
908 #+sb-xc-host (coerce types 'list)
909 #-sb-xc-host (coerce-to-list types)))))
911 (defun maybe-distribute-one-union (union-type types)
912 (let* ((intersection (apply #'type-intersection types))
913 (union (mapcar (lambda (x) (type-intersection x intersection))
914 (union-type-types union-type))))
915 (if (notany (lambda (x) (or (hairy-type-p x)
916 (intersection-type-p x)))
921 (defun type-intersection (&rest input-types)
922 (%type-intersection input-types))
923 (defun-cached (%type-intersection :hash-bits 8
924 :hash-function (lambda (x)
925 (logand (sxhash x) #xff)))
926 ((input-types equal))
927 (let ((simplified-types (simplified-compound-types input-types
928 #'intersection-type-p
929 #'type-intersection2)))
930 (declare (type (vector ctype) simplified-types))
931 ;; We want to have a canonical representation of types (or failing
932 ;; that, punt to HAIRY-TYPE). Canonical representation would have
933 ;; intersections inside unions but not vice versa, since you can
934 ;; always achieve that by the distributive rule. But we don't want
935 ;; to just apply the distributive rule, since it would be too easy
936 ;; to end up with unreasonably huge type expressions. So instead
937 ;; we try to generate a simple type by distributing the union; if
938 ;; the type can't be made simple, we punt to HAIRY-TYPE.
939 (if (and (> (length simplified-types) 1)
940 (some #'union-type-p simplified-types))
941 (let* ((first-union (find-if #'union-type-p simplified-types))
942 (other-types (coerce (remove first-union simplified-types)
944 (distributed (maybe-distribute-one-union first-union
947 (apply #'type-union distributed)
949 :specifier `(and ,@(map 'list
951 simplified-types)))))
952 (make-probably-compound-type #'%make-intersection-type
954 (some #'type-enumerable
958 (defun type-union (&rest input-types)
959 (%type-union input-types))
960 (defun-cached (%type-union :hash-bits 8
961 :hash-function (lambda (x)
962 (logand (sxhash x) #xff)))
963 ((input-types equal))
964 (let ((simplified-types (simplified-compound-types input-types
967 (make-probably-compound-type #'make-union-type
969 (every #'type-enumerable simplified-types)
974 (!define-type-class named)
977 (defvar *empty-type*)
978 (defvar *universal-type*)
979 (defvar *universal-fun-type*)
981 (macrolet ((frob (name var)
983 (setq ,var (make-named-type :name ',name))
984 (setf (info :type :kind ',name)
985 #+sb-xc-host :defined #-sb-xc-host :primitive)
986 (setf (info :type :builtin ',name) ,var))))
987 ;; KLUDGE: In ANSI, * isn't really the name of a type, it's just a
988 ;; special symbol which can be stuck in some places where an
989 ;; ordinary type can go, e.g. (ARRAY * 1) instead of (ARRAY T 1).
990 ;; At some point, in order to become more standard, we should
991 ;; convert all the classic CMU CL legacy *s and *WILD-TYPE*s into
992 ;; Ts and *UNIVERSAL-TYPE*s.
994 (frob nil *empty-type*)
995 (frob t *universal-type*))
996 (setf *universal-fun-type*
997 (make-fun-type :wild-args t
998 :returns *wild-type*)))
1000 (!define-type-method (named :simple-=) (type1 type2)
1001 ;; FIXME: BUG 85: This assertion failed when I added it in
1002 ;; sbcl-0.6.11.13. It probably shouldn't fail; but for now it's
1003 ;; just commented out.
1004 ;;(aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1005 (values (eq type1 type2) t))
1007 (!define-type-method (named :complex-=) (type1 type2)
1009 ((and (eq type2 *empty-type*)
1010 (intersection-type-p type1)
1011 ;; not allowed to be unsure on these... FIXME: keep the list
1012 ;; of CL types that are intersection types once and only
1014 (not (or (type= type1 (specifier-type 'ratio))
1015 (type= type1 (specifier-type 'keyword)))))
1016 ;; things like (AND (EQL 0) (SATISFIES ODDP)) or (AND FUNCTION
1017 ;; STREAM) can get here. In general, we can't really tell
1018 ;; whether these are equal to NIL or not, so
1020 ((type-might-contain-other-types-p type1)
1021 (invoke-complex-=-other-method type1 type2))
1022 (t (values nil t))))
1024 (!define-type-method (named :simple-subtypep) (type1 type2)
1025 (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1026 (values (or (eq type1 *empty-type*) (eq type2 *wild-type*)) t))
1028 (!define-type-method (named :complex-subtypep-arg1) (type1 type2)
1029 ;; This AVER causes problems if we write accurate methods for the
1030 ;; union (and possibly intersection) types which then delegate to
1031 ;; us; while a user shouldn't get here, because of the odd status of
1032 ;; *wild-type* a type-intersection executed by the compiler can. -
1035 ;; (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1036 (cond ((eq type1 *empty-type*)
1038 (;; When TYPE2 might be the universal type in disguise
1039 (type-might-contain-other-types-p type2)
1040 ;; Now that the UNION and HAIRY COMPLEX-SUBTYPEP-ARG2 methods
1041 ;; can delegate to us (more or less as CALL-NEXT-METHOD) when
1042 ;; they're uncertain, we can't just barf on COMPOUND-TYPE and
1043 ;; HAIRY-TYPEs as we used to. Instead we deal with the
1044 ;; problem (where at least part of the problem is cases like
1045 ;; (SUBTYPEP T '(SATISFIES FOO))
1047 ;; (SUBTYPEP T '(AND (SATISFIES FOO) (SATISFIES BAR)))
1048 ;; where the second type is a hairy type like SATISFIES, or
1049 ;; is a compound type which might contain a hairy type) by
1050 ;; returning uncertainty.
1053 ;; By elimination, TYPE1 is the universal type.
1054 (aver (or (eq type1 *wild-type*) (eq type1 *universal-type*)))
1055 ;; This case would have been picked off by the SIMPLE-SUBTYPEP
1056 ;; method, and so shouldn't appear here.
1057 (aver (not (eq type2 *universal-type*)))
1058 ;; Since TYPE2 is not EQ *UNIVERSAL-TYPE* and is not the
1059 ;; universal type in disguise, TYPE2 is not a superset of TYPE1.
1062 (!define-type-method (named :complex-subtypep-arg2) (type1 type2)
1063 (aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1064 (cond ((eq type2 *universal-type*)
1066 ((type-might-contain-other-types-p type1)
1067 ;; those types can be *EMPTY-TYPE* or *UNIVERSAL-TYPE* in
1068 ;; disguise. So we'd better delegate.
1069 (invoke-complex-subtypep-arg1-method type1 type2))
1071 ;; FIXME: This seems to rely on there only being 2 or 3
1072 ;; NAMED-TYPE values, and the exclusion of various
1073 ;; possibilities above. It would be good to explain it and/or
1074 ;; rewrite it so that it's clearer.
1075 (values (not (eq type2 *empty-type*)) t))))
1077 (!define-type-method (named :complex-intersection2) (type1 type2)
1078 ;; FIXME: This assertion failed when I added it in sbcl-0.6.11.13.
1079 ;; Perhaps when bug 85 is fixed it can be reenabled.
1080 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1081 (hierarchical-intersection2 type1 type2))
1083 (!define-type-method (named :complex-union2) (type1 type2)
1084 ;; Perhaps when bug 85 is fixed this can be reenabled.
1085 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1086 (hierarchical-union2 type1 type2))
1088 (!define-type-method (named :unparse) (x)
1089 (named-type-name x))
1091 ;;;; hairy and unknown types
1093 (!define-type-method (hairy :unparse) (x)
1094 (hairy-type-specifier x))
1096 (!define-type-method (hairy :simple-subtypep) (type1 type2)
1097 (let ((hairy-spec1 (hairy-type-specifier type1))
1098 (hairy-spec2 (hairy-type-specifier type2)))
1099 (cond ((equal-but-no-car-recursion hairy-spec1 hairy-spec2)
1102 (values nil nil)))))
1104 (!define-type-method (hairy :complex-subtypep-arg2) (type1 type2)
1105 (invoke-complex-subtypep-arg1-method type1 type2))
1107 (!define-type-method (hairy :complex-subtypep-arg1) (type1 type2)
1108 (declare (ignore type1 type2))
1111 (!define-type-method (hairy :complex-=) (type1 type2)
1112 (declare (ignore type1 type2))
1115 (!define-type-method (hairy :simple-intersection2 :complex-intersection2)
1117 (if (type= type1 type2)
1121 (!define-type-method (hairy :simple-union2)
1123 (if (type= type1 type2)
1127 (!define-type-method (hairy :simple-=) (type1 type2)
1128 (if (equal-but-no-car-recursion (hairy-type-specifier type1)
1129 (hairy-type-specifier type2))
1133 (!def-type-translator satisfies (&whole whole fun)
1134 (declare (ignore fun))
1135 ;; Check legality of arguments.
1136 (destructuring-bind (satisfies predicate-name) whole
1137 (declare (ignore satisfies))
1138 (unless (symbolp predicate-name)
1139 (error 'simple-type-error
1140 :datum predicate-name
1141 :expected-type 'symbol
1142 :format-control "The SATISFIES predicate name is not a symbol: ~S"
1143 :format-arguments (list predicate-name))))
1145 (make-hairy-type :specifier whole))
1149 (!define-type-method (negation :unparse) (x)
1150 `(not ,(type-specifier (negation-type-type x))))
1152 (!define-type-method (negation :simple-subtypep) (type1 type2)
1153 (csubtypep (negation-type-type type2) (negation-type-type type1)))
1155 (!define-type-method (negation :complex-subtypep-arg2) (type1 type2)
1156 (let* ((complement-type2 (negation-type-type type2))
1157 (intersection2 (type-intersection2 type1
1160 ;; FIXME: if uncertain, maybe try arg1?
1161 (type= intersection2 *empty-type*)
1162 (invoke-complex-subtypep-arg1-method type1 type2))))
1164 (!define-type-method (negation :complex-subtypep-arg1) (type1 type2)
1165 ;; "Incrementally extended heuristic algorithms tend inexorably toward the
1166 ;; incomprehensible." -- http://www.unlambda.com/~james/lambda/lambda.txt
1168 ;; You may not believe this. I couldn't either. But then I sat down
1169 ;; and drew lots of Venn diagrams. Comments involving a and b refer
1170 ;; to the call (subtypep '(not a) 'b) -- CSR, 2002-02-27.
1172 ;; (Several logical truths in this block are true as long as
1173 ;; b/=T. As of sbcl-0.7.1.28, it seems impossible to construct a
1174 ;; case with b=T where we actually reach this type method, but
1175 ;; we'll test for and exclude this case anyway, since future
1176 ;; maintenance might make it possible for it to end up in this
1178 (multiple-value-bind (equal certain)
1179 (type= type2 *universal-type*)
1181 (return (values nil nil)))
1183 (return (values t t))))
1184 (let ((complement-type1 (negation-type-type type1)))
1185 ;; Do the special cases first, in order to give us a chance if
1186 ;; subtype/supertype relationships are hairy.
1187 (multiple-value-bind (equal certain)
1188 (type= complement-type1 type2)
1189 ;; If a = b, ~a is not a subtype of b (unless b=T, which was
1192 (return (values nil nil)))
1194 (return (values nil t))))
1195 ;; KLUDGE: ANSI requires that the SUBTYPEP result between any
1196 ;; two built-in atomic type specifiers never be uncertain. This
1197 ;; is hard to do cleanly for the built-in types whose
1198 ;; definitions include (NOT FOO), i.e. CONS and RATIO. However,
1199 ;; we can do it with this hack, which uses our global knowledge
1200 ;; that our implementation of the type system uses disjoint
1201 ;; implementation types to represent disjoint sets (except when
1202 ;; types are contained in other types). (This is a KLUDGE
1203 ;; because it's fragile. Various changes in internal
1204 ;; representation in the type system could make it start
1205 ;; confidently returning incorrect results.) -- WHN 2002-03-08
1206 (unless (or (type-might-contain-other-types-p complement-type1)
1207 (type-might-contain-other-types-p type2))
1208 ;; Because of the way our types which don't contain other
1209 ;; types are disjoint subsets of the space of possible values,
1210 ;; (SUBTYPEP '(NOT AA) 'B)=NIL when AA and B are simple (and B
1211 ;; is not T, as checked above).
1212 (return (values nil t)))
1213 ;; The old (TYPE= TYPE1 TYPE2) branch would never be taken, as
1214 ;; TYPE1 and TYPE2 will only be equal if they're both NOT types,
1215 ;; and then the :SIMPLE-SUBTYPEP method would be used instead.
1216 ;; But a CSUBTYPEP relationship might still hold:
1217 (multiple-value-bind (equal certain)
1218 (csubtypep complement-type1 type2)
1219 ;; If a is a subtype of b, ~a is not a subtype of b (unless
1220 ;; b=T, which was excluded above).
1222 (return (values nil nil)))
1224 (return (values nil t))))
1225 (multiple-value-bind (equal certain)
1226 (csubtypep type2 complement-type1)
1227 ;; If b is a subtype of a, ~a is not a subtype of b. (FIXME:
1228 ;; That's not true if a=T. Do we know at this point that a is
1231 (return (values nil nil)))
1233 (return (values nil t))))
1234 ;; old CSR comment ca. 0.7.2, now obsoleted by the SIMPLE-CTYPE?
1235 ;; KLUDGE case above: Other cases here would rely on being able
1236 ;; to catch all possible cases, which the fragility of this type
1237 ;; system doesn't inspire me; for instance, if a is type= to ~b,
1238 ;; then we want T, T; if this is not the case and the types are
1239 ;; disjoint (have an intersection of *empty-type*) then we want
1240 ;; NIL, T; else if the union of a and b is the *universal-type*
1241 ;; then we want T, T. So currently we still claim to be unsure
1242 ;; about e.g. (subtypep '(not fixnum) 'single-float).
1244 ;; OTOH we might still get here:
1247 (!define-type-method (negation :complex-=) (type1 type2)
1248 ;; (NOT FOO) isn't equivalent to anything that's not a negation
1249 ;; type, except possibly a type that might contain it in disguise.
1250 (declare (ignore type2))
1251 (if (type-might-contain-other-types-p type1)
1255 (!define-type-method (negation :simple-intersection2) (type1 type2)
1256 (let ((not1 (negation-type-type type1))
1257 (not2 (negation-type-type type2)))
1259 ((csubtypep not1 not2) type2)
1260 ((csubtypep not2 not1) type1)
1261 ;; Why no analagous clause to the disjoint in the SIMPLE-UNION2
1262 ;; method, below? The clause would read
1264 ;; ((EQ (TYPE-UNION NOT1 NOT2) *UNIVERSAL-TYPE*) *EMPTY-TYPE*)
1266 ;; but with proper canonicalization of negation types, there's
1267 ;; no way of constructing two negation types with union of their
1268 ;; negations being the universal type.
1270 (aver (not (eq (type-union not1 not2) *universal-type*)))
1273 (!define-type-method (negation :complex-intersection2) (type1 type2)
1275 ((csubtypep type1 (negation-type-type type2)) *empty-type*)
1276 ((eq (type-intersection type1 (negation-type-type type2)) *empty-type*)
1280 (!define-type-method (negation :simple-union2) (type1 type2)
1281 (let ((not1 (negation-type-type type1))
1282 (not2 (negation-type-type type2)))
1284 ((csubtypep not1 not2) type1)
1285 ((csubtypep not2 not1) type2)
1286 ((eq (type-intersection not1 not2) *empty-type*)
1290 (!define-type-method (negation :complex-union2) (type1 type2)
1292 ((csubtypep (negation-type-type type2) type1) *universal-type*)
1293 ((eq (type-intersection type1 (negation-type-type type2)) *empty-type*)
1297 (!define-type-method (negation :simple-=) (type1 type2)
1298 (type= (negation-type-type type1) (negation-type-type type2)))
1300 (!def-type-translator not (typespec)
1301 (let* ((not-type (specifier-type typespec))
1302 (spec (type-specifier not-type)))
1304 ;; canonicalize (NOT (NOT FOO))
1305 ((and (listp spec) (eq (car spec) 'not))
1306 (specifier-type (cadr spec)))
1307 ;; canonicalize (NOT NIL) and (NOT T)
1308 ((eq not-type *empty-type*) *universal-type*)
1309 ((eq not-type *universal-type*) *empty-type*)
1310 ((and (numeric-type-p not-type)
1311 (null (numeric-type-low not-type))
1312 (null (numeric-type-high not-type)))
1313 (make-negation-type :type not-type))
1314 ((numeric-type-p not-type)
1317 :type (modified-numeric-type not-type :low nil :high nil))
1319 ((null (numeric-type-low not-type))
1320 (modified-numeric-type
1322 :low (let ((h (numeric-type-high not-type)))
1323 (if (consp h) (car h) (list h)))
1325 ((null (numeric-type-high not-type))
1326 (modified-numeric-type
1329 :high (let ((l (numeric-type-low not-type)))
1330 (if (consp l) (car l) (list l)))))
1332 (modified-numeric-type
1335 :high (let ((l (numeric-type-low not-type)))
1336 (if (consp l) (car l) (list l))))
1337 (modified-numeric-type
1339 :low (let ((h (numeric-type-high not-type)))
1340 (if (consp h) (car h) (list h)))
1342 ((intersection-type-p not-type)
1344 (mapcar #'(lambda (x)
1345 (specifier-type `(not ,(type-specifier x))))
1346 (intersection-type-types not-type))))
1347 ((union-type-p not-type)
1348 (apply #'type-intersection
1349 (mapcar #'(lambda (x)
1350 (specifier-type `(not ,(type-specifier x))))
1351 (union-type-types not-type))))
1352 ((and (cons-type-p not-type)
1353 (eq (cons-type-car-type not-type) *universal-type*)
1354 (eq (cons-type-cdr-type not-type) *universal-type*))
1355 (make-negation-type :type not-type))
1356 ((cons-type-p not-type)
1358 (make-negation-type :type (specifier-type 'cons))
1360 ((and (not (eq (cons-type-car-type not-type) *universal-type*))
1361 (not (eq (cons-type-cdr-type not-type) *universal-type*)))
1364 (specifier-type `(not ,(type-specifier
1365 (cons-type-car-type not-type))))
1369 (specifier-type `(not ,(type-specifier
1370 (cons-type-cdr-type not-type)))))))
1371 ((not (eq (cons-type-car-type not-type) *universal-type*))
1373 (specifier-type `(not ,(type-specifier
1374 (cons-type-car-type not-type))))
1376 ((not (eq (cons-type-cdr-type not-type) *universal-type*))
1379 (specifier-type `(not ,(type-specifier
1380 (cons-type-cdr-type not-type))))))
1381 (t (bug "Weird CONS type ~S" not-type)))))
1382 (t (make-negation-type :type not-type)))))
1386 (!define-type-class number)
1388 (!define-type-method (number :simple-=) (type1 type2)
1390 (and (eq (numeric-type-class type1) (numeric-type-class type2))
1391 (eq (numeric-type-format type1) (numeric-type-format type2))
1392 (eq (numeric-type-complexp type1) (numeric-type-complexp type2))
1393 (equal (numeric-type-low type1) (numeric-type-low type2))
1394 (equal (numeric-type-high type1) (numeric-type-high type2)))
1397 (!define-type-method (number :unparse) (type)
1398 (let* ((complexp (numeric-type-complexp type))
1399 (low (numeric-type-low type))
1400 (high (numeric-type-high type))
1401 (base (case (numeric-type-class type)
1403 (rational 'rational)
1404 (float (or (numeric-type-format type) 'float))
1407 (cond ((and (eq base 'integer) high low)
1408 (let ((high-count (logcount high))
1409 (high-length (integer-length high)))
1411 (cond ((= high 0) '(integer 0 0))
1413 ((and (= high-count high-length)
1414 (plusp high-length))
1415 `(unsigned-byte ,high-length))
1417 `(mod ,(1+ high)))))
1418 ((and (= low sb!xc:most-negative-fixnum)
1419 (= high sb!xc:most-positive-fixnum))
1421 ((and (= low (lognot high))
1422 (= high-count high-length)
1424 `(signed-byte ,(1+ high-length)))
1426 `(integer ,low ,high)))))
1427 (high `(,base ,(or low '*) ,high))
1429 (if (and (eq base 'integer) (= low 0))
1437 (if (eq base+bounds 'real)
1439 `(complex ,base+bounds)))
1441 (aver (eq base+bounds 'real))
1444 ;;; Return true if X is "less than or equal" to Y, taking open bounds
1445 ;;; into consideration. CLOSED is the predicate used to test the bound
1446 ;;; on a closed interval (e.g. <=), and OPEN is the predicate used on
1447 ;;; open bounds (e.g. <). Y is considered to be the outside bound, in
1448 ;;; the sense that if it is infinite (NIL), then the test succeeds,
1449 ;;; whereas if X is infinite, then the test fails (unless Y is also
1452 ;;; This is for comparing bounds of the same kind, e.g. upper and
1453 ;;; upper. Use NUMERIC-BOUND-TEST* for different kinds of bounds.
1454 #!-negative-zero-is-not-zero
1455 (defmacro numeric-bound-test (x y closed open)
1460 (,closed (car ,x) (car ,y))
1461 (,closed (car ,x) ,y)))
1467 #!+negative-zero-is-not-zero
1468 (defmacro numeric-bound-test-zero (op x y)
1469 `(if (and (zerop ,x) (zerop ,y) (floatp ,x) (floatp ,y))
1470 (,op (float-sign ,x) (float-sign ,y))
1473 #!+negative-zero-is-not-zero
1474 (defmacro numeric-bound-test (x y closed open)
1479 (numeric-bound-test-zero ,closed (car ,x) (car ,y))
1480 (numeric-bound-test-zero ,closed (car ,x) ,y)))
1483 (numeric-bound-test-zero ,open ,x (car ,y))
1484 (numeric-bound-test-zero ,closed ,x ,y)))))
1486 ;;; This is used to compare upper and lower bounds. This is different
1487 ;;; from the same-bound case:
1488 ;;; -- Since X = NIL is -infinity, whereas y = NIL is +infinity, we
1489 ;;; return true if *either* arg is NIL.
1490 ;;; -- an open inner bound is "greater" and also squeezes the interval,
1491 ;;; causing us to use the OPEN test for those cases as well.
1492 #!-negative-zero-is-not-zero
1493 (defmacro numeric-bound-test* (x y closed open)
1498 (,open (car ,x) (car ,y))
1499 (,open (car ,x) ,y)))
1505 #!+negative-zero-is-not-zero
1506 (defmacro numeric-bound-test* (x y closed open)
1511 (numeric-bound-test-zero ,open (car ,x) (car ,y))
1512 (numeric-bound-test-zero ,open (car ,x) ,y)))
1515 (numeric-bound-test-zero ,open ,x (car ,y))
1516 (numeric-bound-test-zero ,closed ,x ,y)))))
1518 ;;; Return whichever of the numeric bounds X and Y is "maximal"
1519 ;;; according to the predicates CLOSED (e.g. >=) and OPEN (e.g. >).
1520 ;;; This is only meaningful for maximizing like bounds, i.e. upper and
1521 ;;; upper. If MAX-P is true, then we return NIL if X or Y is NIL,
1522 ;;; otherwise we return the other arg.
1523 (defmacro numeric-bound-max (x y closed open max-p)
1526 `(cond ((not ,n-x) ,(if max-p nil n-y))
1527 ((not ,n-y) ,(if max-p nil n-x))
1530 (if (,closed (car ,n-x) (car ,n-y)) ,n-x ,n-y)
1531 (if (,open (car ,n-x) ,n-y) ,n-x ,n-y)))
1534 (if (,open (car ,n-y) ,n-x) ,n-y ,n-x)
1535 (if (,closed ,n-y ,n-x) ,n-y ,n-x))))))
1537 (!define-type-method (number :simple-subtypep) (type1 type2)
1538 (let ((class1 (numeric-type-class type1))
1539 (class2 (numeric-type-class type2))
1540 (complexp2 (numeric-type-complexp type2))
1541 (format2 (numeric-type-format type2))
1542 (low1 (numeric-type-low type1))
1543 (high1 (numeric-type-high type1))
1544 (low2 (numeric-type-low type2))
1545 (high2 (numeric-type-high type2)))
1546 ;; If one is complex and the other isn't, they are disjoint.
1547 (cond ((not (or (eq (numeric-type-complexp type1) complexp2)
1550 ;; If the classes are specified and different, the types are
1551 ;; disjoint unless type2 is RATIONAL and type1 is INTEGER.
1552 ;; [ or type1 is INTEGER and type2 is of the form (RATIONAL
1553 ;; X X) for integral X, but this is dealt with in the
1554 ;; canonicalization inside MAKE-NUMERIC-TYPE ]
1555 ((not (or (eq class1 class2)
1557 (and (eq class1 'integer) (eq class2 'rational))))
1559 ;; If the float formats are specified and different, the types
1561 ((not (or (eq (numeric-type-format type1) format2)
1564 ;; Check the bounds.
1565 ((and (numeric-bound-test low1 low2 >= >)
1566 (numeric-bound-test high1 high2 <= <))
1571 (!define-superclasses number ((number)) !cold-init-forms)
1573 ;;; If the high bound of LOW is adjacent to the low bound of HIGH,
1574 ;;; then return true, otherwise NIL.
1575 (defun numeric-types-adjacent (low high)
1576 (let ((low-bound (numeric-type-high low))
1577 (high-bound (numeric-type-low high)))
1578 (cond ((not (and low-bound high-bound)) nil)
1579 ((and (consp low-bound) (consp high-bound)) nil)
1581 #!-negative-zero-is-not-zero
1582 (let ((low-value (car low-bound)))
1583 (or (eql low-value high-bound)
1584 (and (eql low-value -0f0) (eql high-bound 0f0))
1585 (and (eql low-value 0f0) (eql high-bound -0f0))
1586 (and (eql low-value -0d0) (eql high-bound 0d0))
1587 (and (eql low-value 0d0) (eql high-bound -0d0))))
1588 #!+negative-zero-is-not-zero
1589 (eql (car low-bound) high-bound))
1591 #!-negative-zero-is-not-zero
1592 (let ((high-value (car high-bound)))
1593 (or (eql high-value low-bound)
1594 (and (eql high-value -0f0) (eql low-bound 0f0))
1595 (and (eql high-value 0f0) (eql low-bound -0f0))
1596 (and (eql high-value -0d0) (eql low-bound 0d0))
1597 (and (eql high-value 0d0) (eql low-bound -0d0))))
1598 #!+negative-zero-is-not-zero
1599 (eql (car high-bound) low-bound))
1600 #!+negative-zero-is-not-zero
1601 ((or (and (eql low-bound -0f0) (eql high-bound 0f0))
1602 (and (eql low-bound -0d0) (eql high-bound 0d0))))
1603 ((and (eq (numeric-type-class low) 'integer)
1604 (eq (numeric-type-class high) 'integer))
1605 (eql (1+ low-bound) high-bound))
1609 ;;; Return a numeric type that is a supertype for both TYPE1 and TYPE2.
1611 ;;; Old comment, probably no longer applicable:
1613 ;;; ### Note: we give up early to keep from dropping lots of
1614 ;;; information on the floor by returning overly general types.
1615 (!define-type-method (number :simple-union2) (type1 type2)
1616 (declare (type numeric-type type1 type2))
1617 (cond ((csubtypep type1 type2) type2)
1618 ((csubtypep type2 type1) type1)
1620 (let ((class1 (numeric-type-class type1))
1621 (format1 (numeric-type-format type1))
1622 (complexp1 (numeric-type-complexp type1))
1623 (class2 (numeric-type-class type2))
1624 (format2 (numeric-type-format type2))
1625 (complexp2 (numeric-type-complexp type2)))
1627 ((and (eq class1 class2)
1628 (eq format1 format2)
1629 (eq complexp1 complexp2)
1630 (or (numeric-types-intersect type1 type2)
1631 (numeric-types-adjacent type1 type2)
1632 (numeric-types-adjacent type2 type1)))
1637 :low (numeric-bound-max (numeric-type-low type1)
1638 (numeric-type-low type2)
1640 :high (numeric-bound-max (numeric-type-high type1)
1641 (numeric-type-high type2)
1643 ;; FIXME: These two clauses are almost identical, and the
1644 ;; consequents are in fact identical in every respect.
1645 ((and (eq class1 'rational)
1646 (eq class2 'integer)
1647 (eq format1 format2)
1648 (eq complexp1 complexp2)
1649 (integerp (numeric-type-low type2))
1650 (integerp (numeric-type-high type2))
1651 (= (numeric-type-low type2) (numeric-type-high type2))
1652 (or (numeric-types-adjacent type1 type2)
1653 (numeric-types-adjacent type2 type1)))
1658 :low (numeric-bound-max (numeric-type-low type1)
1659 (numeric-type-low type2)
1661 :high (numeric-bound-max (numeric-type-high type1)
1662 (numeric-type-high type2)
1664 ((and (eq class1 'integer)
1665 (eq class2 'rational)
1666 (eq format1 format2)
1667 (eq complexp1 complexp2)
1668 (integerp (numeric-type-low type1))
1669 (integerp (numeric-type-high type1))
1670 (= (numeric-type-low type1) (numeric-type-high type1))
1671 (or (numeric-types-adjacent type1 type2)
1672 (numeric-types-adjacent type2 type1)))
1677 :low (numeric-bound-max (numeric-type-low type1)
1678 (numeric-type-low type2)
1680 :high (numeric-bound-max (numeric-type-high type1)
1681 (numeric-type-high type2)
1687 (setf (info :type :kind 'number)
1688 #+sb-xc-host :defined #-sb-xc-host :primitive)
1689 (setf (info :type :builtin 'number)
1690 (make-numeric-type :complexp nil)))
1692 (!def-type-translator complex (&optional (typespec '*))
1693 (if (eq typespec '*)
1694 (make-numeric-type :complexp :complex)
1695 (labels ((not-numeric ()
1696 (error "The component type for COMPLEX is not numeric: ~S"
1699 (error "The component type for COMPLEX is not real: ~S"
1701 (complex1 (component-type)
1702 (unless (numeric-type-p component-type)
1704 (when (eq (numeric-type-complexp component-type) :complex)
1706 (modified-numeric-type component-type :complexp :complex))
1707 (complex-union (component)
1708 (unless (numberp component)
1710 ;; KLUDGE: This TYPECASE more or less does
1711 ;; (UPGRADED-COMPLEX-PART-TYPE (TYPE-OF COMPONENT)),
1712 ;; (plus a small hack to treat (EQL COMPONENT 0) specially)
1713 ;; but uses logic cut and pasted from the DEFUN of
1714 ;; UPGRADED-COMPLEX-PART-TYPE. That's fragile, because
1715 ;; changing the definition of UPGRADED-COMPLEX-PART-TYPE
1716 ;; would tend to break the code here. Unfortunately,
1717 ;; though, reusing UPGRADED-COMPLEX-PART-TYPE here
1718 ;; would cause another kind of fragility, because
1719 ;; ANSI's definition of TYPE-OF is so weak that e.g.
1720 ;; (UPGRADED-COMPLEX-PART-TYPE (TYPE-OF 1/2)) could
1721 ;; end up being (UPGRADED-COMPLEX-PART-TYPE 'REAL)
1722 ;; instead of (UPGRADED-COMPLEX-PART-TYPE 'RATIONAL).
1723 ;; So using TYPE-OF would mean that ANSI-conforming
1724 ;; maintenance changes in TYPE-OF could break the code here.
1725 ;; It's not clear how best to fix this. -- WHN 2002-01-21,
1726 ;; trying to summarize CSR's concerns in his patch
1728 (complex (error "The component type for COMPLEX (EQL X) ~
1731 ((eql 0) (specifier-type nil)) ; as required by ANSI
1732 (single-float (specifier-type '(complex single-float)))
1733 (double-float (specifier-type '(complex double-float)))
1735 (long-float (specifier-type '(complex long-float)))
1736 (rational (specifier-type '(complex rational)))
1737 (t (specifier-type '(complex real))))))
1738 (let ((ctype (specifier-type typespec)))
1740 (numeric-type (complex1 ctype))
1741 (union-type (apply #'type-union
1742 ;; FIXME: This code could suffer from
1743 ;; (admittedly very obscure) cases of
1744 ;; bug 145 e.g. when TYPE is
1745 ;; (OR (AND INTEGER (SATISFIES ODDP))
1746 ;; (AND FLOAT (SATISFIES FOO))
1747 ;; and not even report the problem very well.
1749 (union-type-types ctype))))
1750 ;; MEMBER-TYPE is almost the same as UNION-TYPE, but
1751 ;; there's a gotcha: (COMPLEX (EQL 0)) is, according to
1752 ;; ANSI, equal to type NIL, the empty set.
1753 (member-type (apply #'type-union
1754 (mapcar #'complex-union
1755 (member-type-members ctype))))
1757 (multiple-value-bind (subtypep certainly)
1758 (csubtypep ctype (specifier-type 'real))
1759 (if (and (not subtypep) certainly)
1761 ;; ANSI just says that TYPESPEC is any subtype of
1762 ;; type REAL, not necessarily a NUMERIC-TYPE. In
1763 ;; particular, at this point TYPESPEC could legally be
1764 ;; an intersection type like (AND REAL (SATISFIES ODDP)),
1765 ;; in which case we fall through the logic above and
1766 ;; end up here, stumped.
1767 (bug "~@<(known bug #145): The type ~S is too hairy to be
1768 used for a COMPLEX component.~:@>"
1771 ;;; If X is *, return NIL, otherwise return the bound, which must be a
1772 ;;; member of TYPE or a one-element list of a member of TYPE.
1773 #!-sb-fluid (declaim (inline canonicalized-bound))
1774 (defun canonicalized-bound (bound type)
1775 (cond ((eq bound '*) nil)
1776 ((or (sb!xc:typep bound type)
1778 (sb!xc:typep (car bound) type)
1779 (null (cdr bound))))
1782 (error "Bound is not ~S, a ~S or a list of a ~S: ~S"
1788 (!def-type-translator integer (&optional (low '*) (high '*))
1789 (let* ((l (canonicalized-bound low 'integer))
1790 (lb (if (consp l) (1+ (car l)) l))
1791 (h (canonicalized-bound high 'integer))
1792 (hb (if (consp h) (1- (car h)) h)))
1793 (if (and hb lb (< hb lb))
1795 (make-numeric-type :class 'integer
1797 :enumerable (not (null (and l h)))
1801 (defmacro !def-bounded-type (type class format)
1802 `(!def-type-translator ,type (&optional (low '*) (high '*))
1803 (let ((lb (canonicalized-bound low ',type))
1804 (hb (canonicalized-bound high ',type)))
1805 (if (not (numeric-bound-test* lb hb <= <))
1807 (make-numeric-type :class ',class
1812 (!def-bounded-type rational rational nil)
1814 ;;; Unlike CMU CL, we represent the types FLOAT and REAL as
1815 ;;; UNION-TYPEs of more primitive types, in order to make
1816 ;;; type representation more unique, avoiding problems in the
1817 ;;; simplification of things like
1818 ;;; (subtypep '(or (single-float -1.0 1.0) (single-float 0.1))
1819 ;;; '(or (real -1 7) (single-float 0.1) (single-float -1.0 1.0)))
1820 ;;; When we allowed REAL to remain as a separate NUMERIC-TYPE,
1821 ;;; it was too easy for the first argument to be simplified to
1822 ;;; '(SINGLE-FLOAT -1.0), and for the second argument to be simplified
1823 ;;; to '(OR (REAL -1 7) (SINGLE-FLOAT 0.1)) and then for the
1824 ;;; SUBTYPEP to fail (returning NIL,T instead of T,T) because
1825 ;;; the first argument can't be seen to be a subtype of any of the
1826 ;;; terms in the second argument.
1828 ;;; The old CMU CL way was:
1829 ;;; (!def-bounded-type float float nil)
1830 ;;; (!def-bounded-type real nil nil)
1832 ;;; FIXME: If this new way works for a while with no weird new
1833 ;;; problems, we can go back and rip out support for separate FLOAT
1834 ;;; and REAL flavors of NUMERIC-TYPE. The new way was added in
1835 ;;; sbcl-0.6.11.22, 2001-03-21.
1837 ;;; FIXME: It's probably necessary to do something to fix the
1838 ;;; analogous problem with INTEGER and RATIONAL types. Perhaps
1839 ;;; bounded RATIONAL types should be represented as (OR RATIO INTEGER).
1840 (defun coerce-bound (bound type inner-coerce-bound-fun)
1841 (declare (type function inner-coerce-bound-fun))
1842 (cond ((eql bound '*)
1845 (destructuring-bind (inner-bound) bound
1846 (list (funcall inner-coerce-bound-fun inner-bound type))))
1848 (funcall inner-coerce-bound-fun bound type))))
1849 (defun inner-coerce-real-bound (bound type)
1851 (rational (rationalize bound))
1852 (float (if (floatp bound)
1854 ;; Coerce to the widest float format available, to
1855 ;; avoid unnecessary loss of precision:
1856 (coerce bound 'long-float)))))
1857 (defun coerced-real-bound (bound type)
1858 (coerce-bound bound type #'inner-coerce-real-bound))
1859 (defun coerced-float-bound (bound type)
1860 (coerce-bound bound type #'coerce))
1861 (!def-type-translator real (&optional (low '*) (high '*))
1862 (specifier-type `(or (float ,(coerced-real-bound low 'float)
1863 ,(coerced-real-bound high 'float))
1864 (rational ,(coerced-real-bound low 'rational)
1865 ,(coerced-real-bound high 'rational)))))
1866 (!def-type-translator float (&optional (low '*) (high '*))
1868 `(or (single-float ,(coerced-float-bound low 'single-float)
1869 ,(coerced-float-bound high 'single-float))
1870 (double-float ,(coerced-float-bound low 'double-float)
1871 ,(coerced-float-bound high 'double-float))
1872 #!+long-float ,(error "stub: no long float support yet"))))
1874 (defmacro !define-float-format (f)
1875 `(!def-bounded-type ,f float ,f))
1877 (!define-float-format short-float)
1878 (!define-float-format single-float)
1879 (!define-float-format double-float)
1880 (!define-float-format long-float)
1882 (defun numeric-types-intersect (type1 type2)
1883 (declare (type numeric-type type1 type2))
1884 (let* ((class1 (numeric-type-class type1))
1885 (class2 (numeric-type-class type2))
1886 (complexp1 (numeric-type-complexp type1))
1887 (complexp2 (numeric-type-complexp type2))
1888 (format1 (numeric-type-format type1))
1889 (format2 (numeric-type-format type2))
1890 (low1 (numeric-type-low type1))
1891 (high1 (numeric-type-high type1))
1892 (low2 (numeric-type-low type2))
1893 (high2 (numeric-type-high type2)))
1894 ;; If one is complex and the other isn't, then they are disjoint.
1895 (cond ((not (or (eq complexp1 complexp2)
1896 (null complexp1) (null complexp2)))
1898 ;; If either type is a float, then the other must either be
1899 ;; specified to be a float or unspecified. Otherwise, they
1901 ((and (eq class1 'float)
1902 (not (member class2 '(float nil)))) nil)
1903 ((and (eq class2 'float)
1904 (not (member class1 '(float nil)))) nil)
1905 ;; If the float formats are specified and different, the
1906 ;; types are disjoint.
1907 ((not (or (eq format1 format2) (null format1) (null format2)))
1910 ;; Check the bounds. This is a bit odd because we must
1911 ;; always have the outer bound of the interval as the
1913 (if (numeric-bound-test high1 high2 <= <)
1914 (or (and (numeric-bound-test low1 low2 >= >)
1915 (numeric-bound-test* low1 high2 <= <))
1916 (and (numeric-bound-test low2 low1 >= >)
1917 (numeric-bound-test* low2 high1 <= <)))
1918 (or (and (numeric-bound-test* low2 high1 <= <)
1919 (numeric-bound-test low2 low1 >= >))
1920 (and (numeric-bound-test high2 high1 <= <)
1921 (numeric-bound-test* high2 low1 >= >))))))))
1923 ;;; Take the numeric bound X and convert it into something that can be
1924 ;;; used as a bound in a numeric type with the specified CLASS and
1925 ;;; FORMAT. If UP-P is true, then we round up as needed, otherwise we
1926 ;;; round down. UP-P true implies that X is a lower bound, i.e. (N) > N.
1928 ;;; This is used by NUMERIC-TYPE-INTERSECTION to mash the bound into
1929 ;;; the appropriate type number. X may only be a float when CLASS is
1932 ;;; ### Note: it is possible for the coercion to a float to overflow
1933 ;;; or underflow. This happens when the bound doesn't fit in the
1934 ;;; specified format. In this case, we should really return the
1935 ;;; appropriate {Most | Least}-{Positive | Negative}-XXX-Float float
1936 ;;; of desired format. But these conditions aren't currently signalled
1937 ;;; in any useful way.
1939 ;;; Also, when converting an open rational bound into a float we
1940 ;;; should probably convert it to a closed bound of the closest float
1941 ;;; in the specified format. KLUDGE: In general, open float bounds are
1942 ;;; screwed up. -- (comment from original CMU CL)
1943 (defun round-numeric-bound (x class format up-p)
1945 (let ((cx (if (consp x) (car x) x)))
1949 (if (and (consp x) (integerp cx))
1950 (if up-p (1+ cx) (1- cx))
1951 (if up-p (ceiling cx) (floor cx))))
1953 (let ((res (if format (coerce cx format) (float cx))))
1954 (if (consp x) (list res) res)))))
1957 ;;; Handle the case of type intersection on two numeric types. We use
1958 ;;; TYPES-EQUAL-OR-INTERSECT to throw out the case of types with no
1959 ;;; intersection. If an attribute in TYPE1 is unspecified, then we use
1960 ;;; TYPE2's attribute, which must be at least as restrictive. If the
1961 ;;; types intersect, then the only attributes that can be specified
1962 ;;; and different are the class and the bounds.
1964 ;;; When the class differs, we use the more restrictive class. The
1965 ;;; only interesting case is RATIONAL/INTEGER, since RATIONAL includes
1968 ;;; We make the result lower (upper) bound the maximum (minimum) of
1969 ;;; the argument lower (upper) bounds. We convert the bounds into the
1970 ;;; appropriate numeric type before maximizing. This avoids possible
1971 ;;; confusion due to mixed-type comparisons (but I think the result is
1973 (!define-type-method (number :simple-intersection2) (type1 type2)
1974 (declare (type numeric-type type1 type2))
1975 (if (numeric-types-intersect type1 type2)
1976 (let* ((class1 (numeric-type-class type1))
1977 (class2 (numeric-type-class type2))
1978 (class (ecase class1
1980 ((integer float) class1)
1981 (rational (if (eq class2 'integer)
1984 (format (or (numeric-type-format type1)
1985 (numeric-type-format type2))))
1989 :complexp (or (numeric-type-complexp type1)
1990 (numeric-type-complexp type2))
1991 :low (numeric-bound-max
1992 (round-numeric-bound (numeric-type-low type1)
1994 (round-numeric-bound (numeric-type-low type2)
1997 :high (numeric-bound-max
1998 (round-numeric-bound (numeric-type-high type1)
2000 (round-numeric-bound (numeric-type-high type2)
2005 ;;; Given two float formats, return the one with more precision. If
2006 ;;; either one is null, return NIL.
2007 (defun float-format-max (f1 f2)
2009 (dolist (f *float-formats* (error "bad float format: ~S" f1))
2010 (when (or (eq f f1) (eq f f2))
2013 ;;; Return the result of an operation on TYPE1 and TYPE2 according to
2014 ;;; the rules of numeric contagion. This is always NUMBER, some float
2015 ;;; format (possibly complex) or RATIONAL. Due to rational
2016 ;;; canonicalization, there isn't much we can do here with integers or
2017 ;;; rational complex numbers.
2019 ;;; If either argument is not a NUMERIC-TYPE, then return NUMBER. This
2020 ;;; is useful mainly for allowing types that are technically numbers,
2021 ;;; but not a NUMERIC-TYPE.
2022 (defun numeric-contagion (type1 type2)
2023 (if (and (numeric-type-p type1) (numeric-type-p type2))
2024 (let ((class1 (numeric-type-class type1))
2025 (class2 (numeric-type-class type2))
2026 (format1 (numeric-type-format type1))
2027 (format2 (numeric-type-format type2))
2028 (complexp1 (numeric-type-complexp type1))
2029 (complexp2 (numeric-type-complexp type2)))
2030 (cond ((or (null complexp1)
2032 (specifier-type 'number))
2036 :format (ecase class2
2037 (float (float-format-max format1 format2))
2038 ((integer rational) format1)
2040 ;; A double-float with any real number is a
2043 (if (eq format1 'double-float)
2046 ;; A long-float with any real number is a
2049 (if (eq format1 'long-float)
2052 :complexp (if (or (eq complexp1 :complex)
2053 (eq complexp2 :complex))
2056 ((eq class2 'float) (numeric-contagion type2 type1))
2057 ((and (eq complexp1 :real) (eq complexp2 :real))
2059 :class (and class1 class2 'rational)
2062 (specifier-type 'number))))
2063 (specifier-type 'number)))
2067 (!define-type-class array)
2069 ;;; What this does depends on the setting of the
2070 ;;; *USE-IMPLEMENTATION-TYPES* switch. If true, return the specialized
2071 ;;; element type, otherwise return the original element type.
2072 (defun specialized-element-type-maybe (type)
2073 (declare (type array-type type))
2074 (if *use-implementation-types*
2075 (array-type-specialized-element-type type)
2076 (array-type-element-type type)))
2078 (!define-type-method (array :simple-=) (type1 type2)
2079 (if (or (unknown-type-p (array-type-element-type type1))
2080 (unknown-type-p (array-type-element-type type2)))
2081 (multiple-value-bind (equalp certainp)
2082 (type= (array-type-element-type type1)
2083 (array-type-element-type type2))
2084 ;; by its nature, the call to TYPE= should never return NIL,
2085 ;; T, as we don't know what the UNKNOWN-TYPE will grow up to
2086 ;; be. -- CSR, 2002-08-19
2087 (aver (not (and (not equalp) certainp)))
2088 (values equalp certainp))
2089 (values (and (equal (array-type-dimensions type1)
2090 (array-type-dimensions type2))
2091 (eq (array-type-complexp type1)
2092 (array-type-complexp type2))
2093 (type= (specialized-element-type-maybe type1)
2094 (specialized-element-type-maybe type2)))
2097 (!define-type-method (array :unparse) (type)
2098 (let ((dims (array-type-dimensions type))
2099 (eltype (type-specifier (array-type-element-type type)))
2100 (complexp (array-type-complexp type)))
2103 (if complexp 'array 'simple-array)
2104 (if complexp `(array ,eltype) `(simple-array ,eltype))))
2105 ((= (length dims) 1)
2107 (if (eq (car dims) '*)
2110 (base-char 'base-string)
2113 (t `(vector ,eltype)))
2115 (bit `(bit-vector ,(car dims)))
2116 (base-char `(base-string ,(car dims)))
2117 (character `(string ,(car dims)))
2118 (t `(vector ,eltype ,(car dims)))))
2119 (if (eq (car dims) '*)
2121 (bit 'simple-bit-vector)
2122 (base-char 'simple-base-string)
2123 (character 'simple-string)
2124 ((t) 'simple-vector)
2125 (t `(simple-array ,eltype (*))))
2127 (bit `(simple-bit-vector ,(car dims)))
2128 (base-char `(simple-base-string ,(car dims)))
2129 (character `(simple-string ,(car dims)))
2130 ((t) `(simple-vector ,(car dims)))
2131 (t `(simple-array ,eltype ,dims))))))
2134 `(array ,eltype ,dims)
2135 `(simple-array ,eltype ,dims))))))
2137 (!define-type-method (array :simple-subtypep) (type1 type2)
2138 (let ((dims1 (array-type-dimensions type1))
2139 (dims2 (array-type-dimensions type2))
2140 (complexp2 (array-type-complexp type2)))
2141 (cond (;; not subtypep unless dimensions are compatible
2142 (not (or (eq dims2 '*)
2143 (and (not (eq dims1 '*))
2144 ;; (sbcl-0.6.4 has trouble figuring out that
2145 ;; DIMS1 and DIMS2 must be lists at this
2146 ;; point, and knowing that is important to
2147 ;; compiling EVERY efficiently.)
2148 (= (length (the list dims1))
2149 (length (the list dims2)))
2150 (every (lambda (x y)
2151 (or (eq y '*) (eql x y)))
2153 (the list dims2)))))
2155 ;; not subtypep unless complexness is compatible
2156 ((not (or (eq complexp2 :maybe)
2157 (eq (array-type-complexp type1) complexp2)))
2159 ;; Since we didn't fail any of the tests above, we win
2160 ;; if the TYPE2 element type is wild.
2161 ((eq (array-type-element-type type2) *wild-type*)
2163 (;; Since we didn't match any of the special cases above, we
2164 ;; can't give a good answer unless both the element types
2165 ;; have been defined.
2166 (or (unknown-type-p (array-type-element-type type1))
2167 (unknown-type-p (array-type-element-type type2)))
2169 (;; Otherwise, the subtype relationship holds iff the
2170 ;; types are equal, and they're equal iff the specialized
2171 ;; element types are identical.
2173 (values (type= (specialized-element-type-maybe type1)
2174 (specialized-element-type-maybe type2))
2177 (!define-superclasses array
2183 (defun array-types-intersect (type1 type2)
2184 (declare (type array-type type1 type2))
2185 (let ((dims1 (array-type-dimensions type1))
2186 (dims2 (array-type-dimensions type2))
2187 (complexp1 (array-type-complexp type1))
2188 (complexp2 (array-type-complexp type2)))
2189 ;; See whether dimensions are compatible.
2190 (cond ((not (or (eq dims1 '*) (eq dims2 '*)
2191 (and (= (length dims1) (length dims2))
2192 (every (lambda (x y)
2193 (or (eq x '*) (eq y '*) (= x y)))
2196 ;; See whether complexpness is compatible.
2197 ((not (or (eq complexp1 :maybe)
2198 (eq complexp2 :maybe)
2199 (eq complexp1 complexp2)))
2203 ;; If either element type is wild, then they intersect.
2204 ;; Otherwise, the types must be identical.
2206 ;; FIXME: There seems to have been a fair amount of
2207 ;; confusion about the distinction between requested element
2208 ;; type and specialized element type; here is one of
2209 ;; them. If we request an array to hold objects of an
2210 ;; unknown type, we can do no better than represent that
2211 ;; type as an array specialized on wild-type. We keep the
2212 ;; requested element-type in the -ELEMENT-TYPE slot, and
2213 ;; *WILD-TYPE* in the -SPECIALIZED-ELEMENT-TYPE. So, here,
2214 ;; we must test for the SPECIALIZED slot being *WILD-TYPE*,
2215 ;; not just the ELEMENT-TYPE slot. Maybe the return value
2216 ;; in that specific case should be T, NIL? Or maybe this
2217 ;; function should really be called
2218 ;; ARRAY-TYPES-COULD-POSSIBLY-INTERSECT? In any case, this
2219 ;; was responsible for bug #123, and this whole issue could
2220 ;; do with a rethink and/or a rewrite. -- CSR, 2002-08-21
2221 ((or (eq (array-type-specialized-element-type type1) *wild-type*)
2222 (eq (array-type-specialized-element-type type2) *wild-type*)
2223 (type= (specialized-element-type-maybe type1)
2224 (specialized-element-type-maybe type2)))
2230 (!define-type-method (array :simple-intersection2) (type1 type2)
2231 (declare (type array-type type1 type2))
2232 (if (array-types-intersect type1 type2)
2233 (let ((dims1 (array-type-dimensions type1))
2234 (dims2 (array-type-dimensions type2))
2235 (complexp1 (array-type-complexp type1))
2236 (complexp2 (array-type-complexp type2))
2237 (eltype1 (array-type-element-type type1))
2238 (eltype2 (array-type-element-type type2)))
2239 (specialize-array-type
2241 :dimensions (cond ((eq dims1 '*) dims2)
2242 ((eq dims2 '*) dims1)
2244 (mapcar (lambda (x y) (if (eq x '*) y x))
2246 :complexp (if (eq complexp1 :maybe) complexp2 complexp1)
2247 :element-type (if (eq eltype1 *wild-type*) eltype2 eltype1))))
2250 ;;; Check a supplied dimension list to determine whether it is legal,
2251 ;;; and return it in canonical form (as either '* or a list).
2252 (defun canonical-array-dimensions (dims)
2257 (error "Arrays can't have a negative number of dimensions: ~S" dims))
2258 (when (>= dims sb!xc:array-rank-limit)
2259 (error "array type with too many dimensions: ~S" dims))
2260 (make-list dims :initial-element '*))
2262 (when (>= (length dims) sb!xc:array-rank-limit)
2263 (error "array type with too many dimensions: ~S" dims))
2266 (unless (and (integerp dim)
2268 (< dim sb!xc:array-dimension-limit))
2269 (error "bad dimension in array type: ~S" dim))))
2272 (error "Array dimensions is not a list, integer or *:~% ~S" dims))))
2276 (!define-type-class member)
2278 (!define-type-method (member :unparse) (type)
2279 (let ((members (member-type-members type)))
2281 ((equal members '(nil)) 'null)
2282 ((type= type (specifier-type 'standard-char)) 'standard-char)
2283 (t `(member ,@members)))))
2285 (!define-type-method (member :simple-subtypep) (type1 type2)
2286 (values (subsetp (member-type-members type1) (member-type-members type2))
2289 (!define-type-method (member :complex-subtypep-arg1) (type1 type2)
2290 (every/type (swapped-args-fun #'ctypep)
2292 (member-type-members type1)))
2294 ;;; We punt if the odd type is enumerable and intersects with the
2295 ;;; MEMBER type. If not enumerable, then it is definitely not a
2296 ;;; subtype of the MEMBER type.
2297 (!define-type-method (member :complex-subtypep-arg2) (type1 type2)
2298 (cond ((not (type-enumerable type1)) (values nil t))
2299 ((types-equal-or-intersect type1 type2)
2300 (invoke-complex-subtypep-arg1-method type1 type2))
2301 (t (values nil t))))
2303 (!define-type-method (member :simple-intersection2) (type1 type2)
2304 (let ((mem1 (member-type-members type1))
2305 (mem2 (member-type-members type2)))
2306 (cond ((subsetp mem1 mem2) type1)
2307 ((subsetp mem2 mem1) type2)
2309 (let ((res (intersection mem1 mem2)))
2311 (make-member-type :members res)
2314 (!define-type-method (member :complex-intersection2) (type1 type2)
2316 (collect ((members))
2317 (let ((mem2 (member-type-members type2)))
2318 (dolist (member mem2)
2319 (multiple-value-bind (val win) (ctypep member type1)
2321 (return-from punt nil))
2322 (when val (members member))))
2323 (cond ((subsetp mem2 (members)) type2)
2324 ((null (members)) *empty-type*)
2326 (make-member-type :members (members))))))))
2328 ;;; We don't need a :COMPLEX-UNION2, since the only interesting case is
2329 ;;; a union type, and the member/union interaction is handled by the
2330 ;;; union type method.
2331 (!define-type-method (member :simple-union2) (type1 type2)
2332 (let ((mem1 (member-type-members type1))
2333 (mem2 (member-type-members type2)))
2334 (cond ((subsetp mem1 mem2) type2)
2335 ((subsetp mem2 mem1) type1)
2337 (make-member-type :members (union mem1 mem2))))))
2339 (!define-type-method (member :simple-=) (type1 type2)
2340 (let ((mem1 (member-type-members type1))
2341 (mem2 (member-type-members type2)))
2342 (values (and (subsetp mem1 mem2)
2343 (subsetp mem2 mem1))
2346 (!define-type-method (member :complex-=) (type1 type2)
2347 (if (type-enumerable type1)
2348 (multiple-value-bind (val win) (csubtypep type2 type1)
2349 (if (or val (not win))
2354 (!def-type-translator member (&rest members)
2357 (dolist (m (remove-duplicates members))
2359 (number (push (ctype-of m) numbers))
2363 (make-member-type :members ms)
2365 (nreverse numbers)))
2368 ;;;; intersection types
2370 ;;;; Until version 0.6.10.6, SBCL followed the original CMU CL approach
2371 ;;;; of punting on all AND types, not just the unreasonably complicated
2372 ;;;; ones. The change was motivated by trying to get the KEYWORD type
2373 ;;;; to behave sensibly:
2374 ;;;; ;; reasonable definition
2375 ;;;; (DEFTYPE KEYWORD () '(AND SYMBOL (SATISFIES KEYWORDP)))
2376 ;;;; ;; reasonable behavior
2377 ;;;; (AVER (SUBTYPEP 'KEYWORD 'SYMBOL))
2378 ;;;; Without understanding a little about the semantics of AND, we'd
2379 ;;;; get (SUBTYPEP 'KEYWORD 'SYMBOL)=>NIL,NIL and, for entirely
2380 ;;;; parallel reasons, (SUBTYPEP 'RATIO 'NUMBER)=>NIL,NIL. That's
2383 ;;;; We still follow the example of CMU CL to some extent, by punting
2384 ;;;; (to the opaque HAIRY-TYPE) on sufficiently complicated types
2387 (!define-type-class intersection)
2389 ;;; A few intersection types have special names. The others just get
2390 ;;; mechanically unparsed.
2391 (!define-type-method (intersection :unparse) (type)
2392 (declare (type ctype type))
2393 (or (find type '(ratio keyword) :key #'specifier-type :test #'type=)
2394 `(and ,@(mapcar #'type-specifier (intersection-type-types type)))))
2396 ;;; shared machinery for type equality: true if every type in the set
2397 ;;; TYPES1 matches a type in the set TYPES2 and vice versa
2398 (defun type=-set (types1 types2)
2399 (flet ((type<=-set (x y)
2400 (declare (type list x y))
2401 (every/type (lambda (x y-element)
2402 (any/type #'type= y-element x))
2404 (and/type (type<=-set types1 types2)
2405 (type<=-set types2 types1))))
2407 ;;; Two intersection types are equal if their subtypes are equal sets.
2409 ;;; FIXME: Might it be better to use
2410 ;;; (AND (SUBTYPEP X Y) (SUBTYPEP Y X))
2411 ;;; instead, since SUBTYPEP is the usual relationship that we care
2412 ;;; most about, so it would be good to leverage any ingenuity there
2413 ;;; in this more obscure method?
2414 (!define-type-method (intersection :simple-=) (type1 type2)
2415 (type=-set (intersection-type-types type1)
2416 (intersection-type-types type2)))
2418 (defun %intersection-complex-subtypep-arg1 (type1 type2)
2419 (type= type1 (type-intersection type1 type2)))
2421 (defun %intersection-simple-subtypep (type1 type2)
2422 (every/type #'%intersection-complex-subtypep-arg1
2424 (intersection-type-types type2)))
2426 (!define-type-method (intersection :simple-subtypep) (type1 type2)
2427 (%intersection-simple-subtypep type1 type2))
2429 (!define-type-method (intersection :complex-subtypep-arg1) (type1 type2)
2430 (%intersection-complex-subtypep-arg1 type1 type2))
2432 (defun %intersection-complex-subtypep-arg2 (type1 type2)
2433 (every/type #'csubtypep type1 (intersection-type-types type2)))
2435 (!define-type-method (intersection :complex-subtypep-arg2) (type1 type2)
2436 (%intersection-complex-subtypep-arg2 type1 type2))
2438 ;;; FIXME: This will look eeriely familiar to readers of the UNION
2439 ;;; :SIMPLE-INTERSECTION2 :COMPLEX-INTERSECTION2 method. That's
2440 ;;; because it was generated by cut'n'paste methods. Given that
2441 ;;; intersections and unions have all sorts of symmetries known to
2442 ;;; mathematics, it shouldn't be beyond the ken of some programmers to
2443 ;;; reflect those symmetries in code in a way that ties them together
2444 ;;; more strongly than having two independent near-copies :-/
2445 (!define-type-method (intersection :simple-union2 :complex-union2)
2447 ;; Within this method, type2 is guaranteed to be an intersection
2449 (aver (intersection-type-p type2))
2450 ;; Make sure to call only the applicable methods...
2451 (cond ((and (intersection-type-p type1)
2452 (%intersection-simple-subtypep type1 type2)) type2)
2453 ((and (intersection-type-p type1)
2454 (%intersection-simple-subtypep type2 type1)) type1)
2455 ((and (not (intersection-type-p type1))
2456 (%intersection-complex-subtypep-arg2 type1 type2))
2458 ((and (not (intersection-type-p type1))
2459 (%intersection-complex-subtypep-arg1 type2 type1))
2461 ;; KLUDGE: This special (and somewhat hairy) magic is required
2462 ;; to deal with the RATIONAL/INTEGER special case. The UNION
2463 ;; of (INTEGER * -1) and (AND (RATIONAL * -1/2) (NOT INTEGER))
2464 ;; should be (RATIONAL * -1/2) -- CSR, 2003-02-28
2465 ((and (csubtypep type2 (specifier-type 'ratio))
2466 (numeric-type-p type1)
2467 (csubtypep type1 (specifier-type 'integer))
2472 :low (if (null (numeric-type-low type1))
2474 (list (1- (numeric-type-low type1))))
2475 :high (if (null (numeric-type-high type1))
2477 (list (1+ (numeric-type-high type1)))))))
2479 (apply #'type-intersection
2480 (remove (specifier-type '(not integer))
2481 (intersection-type-types type2)
2484 (let ((accumulator *universal-type*))
2485 (do ((t2s (intersection-type-types type2) (cdr t2s)))
2486 ((null t2s) accumulator)
2487 (let ((union (type-union type1 (car t2s))))
2488 (when (union-type-p union)
2489 ;; we have to give up here -- there are all sorts of
2490 ;; ordering worries, but it's better than before.
2491 ;; Doing exactly the same as in the UNION
2492 ;; :SIMPLE/:COMPLEX-INTERSECTION2 method causes stack
2493 ;; overflow with the mutual recursion never bottoming
2495 (if (and (eq accumulator *universal-type*)
2497 ;; KLUDGE: if we get here, we have a partially
2498 ;; simplified result. While this isn't by any
2499 ;; means a universal simplification, including
2500 ;; this logic here means that we can get (OR
2501 ;; KEYWORD (NOT KEYWORD)) canonicalized to T.
2505 (type-intersection accumulator union))))))))
2507 (!def-type-translator and (&whole whole &rest type-specifiers)
2508 (apply #'type-intersection
2509 (mapcar #'specifier-type
2514 (!define-type-class union)
2516 ;;; The LIST, FLOAT and REAL types have special names. Other union
2517 ;;; types just get mechanically unparsed.
2518 (!define-type-method (union :unparse) (type)
2519 (declare (type ctype type))
2521 ((type= type (specifier-type 'list)) 'list)
2522 ((type= type (specifier-type 'float)) 'float)
2523 ((type= type (specifier-type 'real)) 'real)
2524 ((type= type (specifier-type 'sequence)) 'sequence)
2525 ((type= type (specifier-type 'bignum)) 'bignum)
2526 (t `(or ,@(mapcar #'type-specifier (union-type-types type))))))
2528 ;;; Two union types are equal if they are each subtypes of each
2529 ;;; other. We need to be this clever because our complex subtypep
2530 ;;; methods are now more accurate; we don't get infinite recursion
2531 ;;; because the simple-subtypep method delegates to complex-subtypep
2532 ;;; of the individual types of type1. - CSR, 2002-04-09
2534 ;;; Previous comment, now obsolete, but worth keeping around because
2535 ;;; it is true, though too strong a condition:
2537 ;;; Two union types are equal if their subtypes are equal sets.
2538 (!define-type-method (union :simple-=) (type1 type2)
2539 (multiple-value-bind (subtype certain?)
2540 (csubtypep type1 type2)
2542 (csubtypep type2 type1)
2543 ;; we might as well become as certain as possible.
2546 (multiple-value-bind (subtype certain?)
2547 (csubtypep type2 type1)
2548 (declare (ignore subtype))
2549 (values nil certain?))))))
2551 (!define-type-method (union :complex-=) (type1 type2)
2552 (declare (ignore type1))
2553 (if (some #'type-might-contain-other-types-p
2554 (union-type-types type2))
2558 ;;; Similarly, a union type is a subtype of another if and only if
2559 ;;; every element of TYPE1 is a subtype of TYPE2.
2560 (defun union-simple-subtypep (type1 type2)
2561 (every/type (swapped-args-fun #'union-complex-subtypep-arg2)
2563 (union-type-types type1)))
2565 (!define-type-method (union :simple-subtypep) (type1 type2)
2566 (union-simple-subtypep type1 type2))
2568 (defun union-complex-subtypep-arg1 (type1 type2)
2569 (every/type (swapped-args-fun #'csubtypep)
2571 (union-type-types type1)))
2573 (!define-type-method (union :complex-subtypep-arg1) (type1 type2)
2574 (union-complex-subtypep-arg1 type1 type2))
2576 (defun union-complex-subtypep-arg2 (type1 type2)
2577 (multiple-value-bind (sub-value sub-certain?)
2578 ;; was: (any/type #'csubtypep type1 (union-type-types type2)),
2579 ;; which turns out to be too restrictive, causing bug 91.
2581 ;; the following reimplementation might look dodgy. It is
2582 ;; dodgy. It depends on the union :complex-= method not doing
2583 ;; very much work -- certainly, not using subtypep. Reasoning:
2585 ;; At this stage, we know that type2 is a union type and type1
2586 ;; isn't. We might as well check this, though:
2587 (aver (union-type-p type2))
2588 (aver (not (union-type-p type1)))
2589 ;; A is a subset of (B1 u B2)
2590 ;; <=> A n (B1 u B2) = A
2591 ;; <=> (A n B1) u (A n B2) = A
2593 ;; But, we have to be careful not to delegate this type= to
2594 ;; something that could invoke subtypep, which might get us
2595 ;; back here -> stack explosion. We therefore ensure that the
2596 ;; second type (which is the one that's dispatched on) is
2597 ;; either a union type (where we've ensured that the complex-=
2598 ;; method will not call subtypep) or something with no union
2599 ;; types involved, in which case we'll never come back here.
2601 ;; If we don't do this, then e.g.
2602 ;; (SUBTYPEP '(MEMBER 3) '(OR (SATISFIES FOO) (SATISFIES BAR)))
2603 ;; would loop infinitely, as the member :complex-= method is
2604 ;; implemented in terms of subtypep.
2606 ;; Ouch. - CSR, 2002-04-10
2609 (mapcar (lambda (x) (type-intersection type1 x))
2610 (union-type-types type2)))))
2612 (values sub-value sub-certain?)
2613 ;; The ANY/TYPE expression above is a sufficient condition for
2614 ;; subsetness, but not a necessary one, so we might get a more
2615 ;; certain answer by this CALL-NEXT-METHOD-ish step when the
2616 ;; ANY/TYPE expression is uncertain.
2617 (invoke-complex-subtypep-arg1-method type1 type2))))
2619 (!define-type-method (union :complex-subtypep-arg2) (type1 type2)
2620 (union-complex-subtypep-arg2 type1 type2))
2622 (!define-type-method (union :simple-intersection2 :complex-intersection2)
2624 ;; The CSUBTYPEP clauses here let us simplify e.g.
2625 ;; (TYPE-INTERSECTION2 (SPECIFIER-TYPE 'LIST)
2626 ;; (SPECIFIER-TYPE '(OR LIST VECTOR)))
2627 ;; (where LIST is (OR CONS NULL)).
2629 ;; The tests are more or less (CSUBTYPEP TYPE1 TYPE2) and vice
2630 ;; versa, but it's important that we pre-expand them into
2631 ;; specialized operations on individual elements of
2632 ;; UNION-TYPE-TYPES, instead of using the ordinary call to
2633 ;; CSUBTYPEP, in order to avoid possibly invoking any methods which
2634 ;; might in turn invoke (TYPE-INTERSECTION2 TYPE1 TYPE2) and thus
2635 ;; cause infinite recursion.
2637 ;; Within this method, type2 is guaranteed to be a union type:
2638 (aver (union-type-p type2))
2639 ;; Make sure to call only the applicable methods...
2640 (cond ((and (union-type-p type1)
2641 (union-simple-subtypep type1 type2)) type1)
2642 ((and (union-type-p type1)
2643 (union-simple-subtypep type2 type1)) type2)
2644 ((and (not (union-type-p type1))
2645 (union-complex-subtypep-arg2 type1 type2))
2647 ((and (not (union-type-p type1))
2648 (union-complex-subtypep-arg1 type2 type1))
2651 ;; KLUDGE: This code accumulates a sequence of TYPE-UNION2
2652 ;; operations in a particular order, and gives up if any of
2653 ;; the sub-unions turn out not to be simple. In other cases
2654 ;; ca. sbcl-0.6.11.15, that approach to taking a union was a
2655 ;; bad idea, since it can overlook simplifications which
2656 ;; might occur if the terms were accumulated in a different
2657 ;; order. It's possible that that will be a problem here too.
2658 ;; However, I can't think of a good example to demonstrate
2659 ;; it, and without an example to demonstrate it I can't write
2660 ;; test cases, and without test cases I don't want to
2661 ;; complicate the code to address what's still a hypothetical
2662 ;; problem. So I punted. -- WHN 2001-03-20
2663 (let ((accumulator *empty-type*))
2664 (dolist (t2 (union-type-types type2) accumulator)
2666 (type-union accumulator
2667 (type-intersection type1 t2))))))))
2669 (!def-type-translator or (&rest type-specifiers)
2671 (mapcar #'specifier-type
2676 (!define-type-class cons)
2678 (!def-type-translator cons (&optional (car-type-spec '*) (cdr-type-spec '*))
2679 (let ((car-type (specifier-type car-type-spec))
2680 (cdr-type (specifier-type cdr-type-spec)))
2681 (make-cons-type car-type cdr-type)))
2683 (!define-type-method (cons :unparse) (type)
2684 (let ((car-eltype (type-specifier (cons-type-car-type type)))
2685 (cdr-eltype (type-specifier (cons-type-cdr-type type))))
2686 (if (and (member car-eltype '(t *))
2687 (member cdr-eltype '(t *)))
2689 `(cons ,car-eltype ,cdr-eltype))))
2691 (!define-type-method (cons :simple-=) (type1 type2)
2692 (declare (type cons-type type1 type2))
2693 (and (type= (cons-type-car-type type1) (cons-type-car-type type2))
2694 (type= (cons-type-cdr-type type1) (cons-type-cdr-type type2))))
2696 (!define-type-method (cons :simple-subtypep) (type1 type2)
2697 (declare (type cons-type type1 type2))
2698 (multiple-value-bind (val-car win-car)
2699 (csubtypep (cons-type-car-type type1) (cons-type-car-type type2))
2700 (multiple-value-bind (val-cdr win-cdr)
2701 (csubtypep (cons-type-cdr-type type1) (cons-type-cdr-type type2))
2702 (if (and val-car val-cdr)
2703 (values t (and win-car win-cdr))
2704 (values nil (or win-car win-cdr))))))
2706 ;;; Give up if a precise type is not possible, to avoid returning
2707 ;;; overly general types.
2708 (!define-type-method (cons :simple-union2) (type1 type2)
2709 (declare (type cons-type type1 type2))
2710 (let ((car-type1 (cons-type-car-type type1))
2711 (car-type2 (cons-type-car-type type2))
2712 (cdr-type1 (cons-type-cdr-type type1))
2713 (cdr-type2 (cons-type-cdr-type type2)))
2714 ;; UGH. -- CSR, 2003-02-24
2715 (macrolet ((frob-car (car1 car2 cdr1 cdr2)
2717 (make-cons-type ,car1 (type-union ,cdr1 ,cdr2))
2719 (type-intersection ,car2
2721 `(not ,(type-specifier ,car1))))
2723 (cond ((type= car-type1 car-type2)
2724 (make-cons-type car-type1
2725 (type-union cdr-type1 cdr-type2)))
2726 ((type= cdr-type1 cdr-type2)
2727 (make-cons-type (type-union car-type1 car-type2)
2729 ((csubtypep car-type1 car-type2)
2730 (frob-car car-type1 car-type2 cdr-type1 cdr-type2))
2731 ((csubtypep car-type2 car-type1)
2732 (frob-car car-type2 car-type1 cdr-type2 cdr-type1))
2733 ;; Don't put these in -- consider the effect of taking the
2734 ;; union of (CONS (INTEGER 0 2) (INTEGER 5 7)) and
2735 ;; (CONS (INTEGER 0 3) (INTEGER 5 6)).
2737 ((csubtypep cdr-type1 cdr-type2)
2738 (frob-cdr car-type1 car-type2 cdr-type1 cdr-type2))
2740 ((csubtypep cdr-type2 cdr-type1)
2741 (frob-cdr car-type2 car-type1 cdr-type2 cdr-type1))))))
2743 (!define-type-method (cons :simple-intersection2) (type1 type2)
2744 (declare (type cons-type type1 type2))
2747 (and (setf car-int2 (type-intersection2 (cons-type-car-type type1)
2748 (cons-type-car-type type2)))
2749 (setf cdr-int2 (type-intersection2 (cons-type-cdr-type type1)
2750 (cons-type-cdr-type type2)))
2751 (make-cons-type car-int2 cdr-int2))))
2753 ;;; Return the type that describes all objects that are in X but not
2754 ;;; in Y. If we can't determine this type, then return NIL.
2756 ;;; For now, we only are clever dealing with union and member types.
2757 ;;; If either type is not a union type, then we pretend that it is a
2758 ;;; union of just one type. What we do is remove from X all the types
2759 ;;; that are a subtype any type in Y. If any type in X intersects with
2760 ;;; a type in Y but is not a subtype, then we give up.
2762 ;;; We must also special-case any member type that appears in the
2763 ;;; union. We remove from X's members all objects that are TYPEP to Y.
2764 ;;; If Y has any members, we must be careful that none of those
2765 ;;; members are CTYPEP to any of Y's non-member types. We give up in
2766 ;;; this case, since to compute that difference we would have to break
2767 ;;; the type from X into some collection of types that represents the
2768 ;;; type without that particular element. This seems too hairy to be
2769 ;;; worthwhile, given its low utility.
2770 (defun type-difference (x y)
2771 (let ((x-types (if (union-type-p x) (union-type-types x) (list x)))
2772 (y-types (if (union-type-p y) (union-type-types y) (list y))))
2774 (dolist (x-type x-types)
2775 (if (member-type-p x-type)
2776 (collect ((members))
2777 (dolist (mem (member-type-members x-type))
2778 (multiple-value-bind (val win) (ctypep mem y)
2779 (unless win (return-from type-difference nil))
2783 (res (make-member-type :members (members)))))
2784 (dolist (y-type y-types (res x-type))
2785 (multiple-value-bind (val win) (csubtypep x-type y-type)
2786 (unless win (return-from type-difference nil))
2788 (when (types-equal-or-intersect x-type y-type)
2789 (return-from type-difference nil))))))
2790 (let ((y-mem (find-if #'member-type-p y-types)))
2792 (let ((members (member-type-members y-mem)))
2793 (dolist (x-type x-types)
2794 (unless (member-type-p x-type)
2795 (dolist (member members)
2796 (multiple-value-bind (val win) (ctypep member x-type)
2797 (when (or (not win) val)
2798 (return-from type-difference nil)))))))))
2799 (apply #'type-union (res)))))
2801 (!def-type-translator array (&optional (element-type '*)
2803 (specialize-array-type
2804 (make-array-type :dimensions (canonical-array-dimensions dimensions)
2806 :element-type (specifier-type element-type))))
2808 (!def-type-translator simple-array (&optional (element-type '*)
2810 (specialize-array-type
2811 (make-array-type :dimensions (canonical-array-dimensions dimensions)
2813 :element-type (specifier-type element-type))))
2815 ;;;; utilities shared between cross-compiler and target system
2817 ;;; Does the type derived from compilation of an actual function
2818 ;;; definition satisfy declarations of a function's type?
2819 (defun defined-ftype-matches-declared-ftype-p (defined-ftype declared-ftype)
2820 (declare (type ctype defined-ftype declared-ftype))
2821 (flet ((is-built-in-class-function-p (ctype)
2822 (and (built-in-classoid-p ctype)
2823 (eq (built-in-classoid-name ctype) 'function))))
2824 (cond (;; DECLARED-FTYPE could certainly be #<BUILT-IN-CLASS FUNCTION>;
2825 ;; that's what happens when we (DECLAIM (FTYPE FUNCTION FOO)).
2826 (is-built-in-class-function-p declared-ftype)
2827 ;; In that case, any definition satisfies the declaration.
2829 (;; It's not clear whether or how DEFINED-FTYPE might be
2830 ;; #<BUILT-IN-CLASS FUNCTION>, but it's not obviously
2831 ;; invalid, so let's handle that case too, just in case.
2832 (is-built-in-class-function-p defined-ftype)
2833 ;; No matter what DECLARED-FTYPE might be, we can't prove
2834 ;; that an object of type FUNCTION doesn't satisfy it, so
2835 ;; we return success no matter what.
2837 (;; Otherwise both of them must be FUN-TYPE objects.
2839 ;; FIXME: For now we only check compatibility of the return
2840 ;; type, not argument types, and we don't even check the
2841 ;; return type very precisely (as per bug 94a). It would be
2842 ;; good to do a better job. Perhaps to check the
2843 ;; compatibility of the arguments, we should (1) redo
2844 ;; VALUES-TYPES-EQUAL-OR-INTERSECT as
2845 ;; ARGS-TYPES-EQUAL-OR-INTERSECT, and then (2) apply it to
2846 ;; the ARGS-TYPE slices of the FUN-TYPEs. (ARGS-TYPE
2847 ;; is a base class both of VALUES-TYPE and of FUN-TYPE.)
2848 (values-types-equal-or-intersect
2849 (fun-type-returns defined-ftype)
2850 (fun-type-returns declared-ftype))))))
2852 ;;; This messy case of CTYPE for NUMBER is shared between the
2853 ;;; cross-compiler and the target system.
2854 (defun ctype-of-number (x)
2855 (let ((num (if (complexp x) (realpart x) x)))
2856 (multiple-value-bind (complexp low high)
2858 (let ((imag (imagpart x)))
2859 (values :complex (min num imag) (max num imag)))
2860 (values :real num num))
2861 (make-numeric-type :class (etypecase num
2863 (rational 'rational)
2865 :format (and (floatp num) (float-format-name num))
2871 ;; Why SAFETY 0? To suppress the is-it-the-right-structure-type
2872 ;; checking for declarations in structure accessors. Otherwise we
2873 ;; can get caught in a chicken-and-egg bootstrapping problem, whose
2874 ;; symptom on x86 OpenBSD sbcl-0.pre7.37.flaky5.22 is an illegal
2875 ;; instruction trap. I haven't tracked it down, but I'm guessing it
2876 ;; has to do with setting LAYOUTs when the LAYOUT hasn't been set
2878 (declare (optimize (safety 0)))
2879 (!defun-from-collected-cold-init-forms !late-type-cold-init))
2881 (/show0 "late-type.lisp end of file")