1 ;;;; This file contains the definition of non-CLASS types (e.g.
2 ;;;; subtypes of interesting BUILT-IN-CLASSes) and the interfaces to
3 ;;;; the type system. Common Lisp type specifiers are parsed into a
4 ;;;; somewhat canonical internal type representation that supports
5 ;;;; type union, intersection, etc. (Except that ALIEN types have
8 ;;;; This software is part of the SBCL system. See the README file for
11 ;;;; This software is derived from the CMU CL system, which was
12 ;;;; written at Carnegie Mellon University and released into the
13 ;;;; public domain. The software is in the public domain and is
14 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
15 ;;;; files for more information.
17 (in-package "SB!KERNEL")
19 (/show0 "late-type.lisp 19")
21 (!begin-collecting-cold-init-forms)
23 ;;; ### Remaining incorrectnesses:
25 ;;; There are all sorts of nasty problems with open bounds on FLOAT
26 ;;; types (and probably FLOAT types in general.)
28 ;;; This condition is signalled whenever we make a UNKNOWN-TYPE so that
29 ;;; compiler warnings can be emitted as appropriate.
30 (define-condition parse-unknown-type (condition)
31 ((specifier :reader parse-unknown-type-specifier :initarg :specifier)))
33 ;;; FIXME: This really should go away. Alas, it doesn't seem to be so
34 ;;; simple to make it go away.. (See bug 123 in BUGS file.)
35 (defvar *use-implementation-types* t ; actually initialized in cold init
37 "*USE-IMPLEMENTATION-TYPES* is a semi-public flag which determines how
38 restrictive we are in determining type membership. If two types are the
39 same in the implementation, then we will consider them them the same when
40 this switch is on. When it is off, we try to be as restrictive as the
41 language allows, allowing us to detect more errors. Currently, this only
42 affects array types.")
43 (!cold-init-forms (setq *use-implementation-types* t))
45 ;;; These functions are used as method for types which need a complex
46 ;;; subtypep method to handle some superclasses, but cover a subtree
47 ;;; of the type graph (i.e. there is no simple way for any other type
48 ;;; class to be a subtype.) There are always still complex ways,
49 ;;; namely UNION and MEMBER types, so we must give TYPE1's method a
50 ;;; chance to run, instead of immediately returning NIL, T.
51 (defun delegate-complex-subtypep-arg2 (type1 type2)
53 (type-class-complex-subtypep-arg1
54 (type-class-info type1))))
56 (funcall subtypep-arg1 type1 type2)
58 (defun delegate-complex-intersection2 (type1 type2)
59 (let ((method (type-class-complex-intersection2 (type-class-info type1))))
60 (if (and method (not (eq method #'delegate-complex-intersection2)))
61 (funcall method type2 type1)
62 (hierarchical-intersection2 type1 type2))))
64 ;;; This is used by !DEFINE-SUPERCLASSES to define the SUBTYPE-ARG1
65 ;;; method. INFO is a list of conses
66 ;;; (SUPERCLASS-CLASS . {GUARD-TYPE-SPECIFIER | NIL}).
67 (defun !has-superclasses-complex-subtypep-arg1 (type1 type2 info)
68 ;; If TYPE2 might be concealing something related to our class
70 (if (type-might-contain-other-types-p type2)
71 ;; too confusing, gotta punt
73 ;; ordinary case expected by old CMU CL code, where the taxonomy
74 ;; of TYPE2's representation accurately reflects the taxonomy of
77 ;; FIXME: This old CMU CL code probably deserves a comment
78 ;; explaining to us mere mortals how it works...
79 (and (sb!xc:typep type2 'classoid)
81 (when (or (not (cdr x))
82 (csubtypep type1 (specifier-type (cdr x))))
84 (or (eq type2 (car x))
85 (let ((inherits (layout-inherits
86 (classoid-layout (car x)))))
87 (dotimes (i (length inherits) nil)
88 (when (eq type2 (layout-classoid (svref inherits i)))
92 ;;; This function takes a list of specs, each of the form
93 ;;; (SUPERCLASS-NAME &OPTIONAL GUARD).
94 ;;; Consider one spec (with no guard): any instance of the named
95 ;;; TYPE-CLASS is also a subtype of the named superclass and of any of
96 ;;; its superclasses. If there are multiple specs, then some will have
97 ;;; guards. We choose the first spec whose guard is a supertype of
98 ;;; TYPE1 and use its superclass. In effect, a sequence of guards
101 ;;; G0,(and G1 (not G0)), (and G2 (not (or G0 G1))).
103 ;;; WHEN controls when the forms are executed.
104 (defmacro !define-superclasses (type-class-name specs when)
105 (with-unique-names (type-class info)
107 (let ((,type-class (type-class-or-lose ',type-class-name))
108 (,info (mapcar (lambda (spec)
110 (super &optional guard)
112 (cons (find-classoid super) guard)))
114 (setf (type-class-complex-subtypep-arg1 ,type-class)
115 (lambda (type1 type2)
116 (!has-superclasses-complex-subtypep-arg1 type1 type2 ,info)))
117 (setf (type-class-complex-subtypep-arg2 ,type-class)
118 #'delegate-complex-subtypep-arg2)
119 (setf (type-class-complex-intersection2 ,type-class)
120 #'delegate-complex-intersection2)))))
122 ;;;; FUNCTION and VALUES types
124 ;;;; Pretty much all of the general type operations are illegal on
125 ;;;; VALUES types, since we can't discriminate using them, do
126 ;;;; SUBTYPEP, etc. FUNCTION types are acceptable to the normal type
127 ;;;; operations, but are generally considered to be equivalent to
128 ;;;; FUNCTION. These really aren't true types in any type theoretic
129 ;;;; sense, but we still parse them into CTYPE structures for two
132 ;;;; -- Parsing and unparsing work the same way, and indeed we can't
133 ;;;; tell whether a type is a function or values type without
135 ;;;; -- Many of the places that can be annotated with real types can
136 ;;;; also be annotated with function or values types.
138 ;;; the description of a &KEY argument
139 (defstruct (key-info #-sb-xc-host (:pure t)
141 ;; the key (not necessarily a keyword in ANSI Common Lisp)
142 (name (missing-arg) :type symbol)
143 ;; the type of the argument value
144 (type (missing-arg) :type ctype))
146 (!define-type-method (values :simple-subtypep :complex-subtypep-arg1)
148 (declare (ignore type2))
149 ;; FIXME: should be TYPE-ERROR, here and in next method
150 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type1)))
152 (!define-type-method (values :complex-subtypep-arg2)
154 (declare (ignore type1))
155 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type2)))
157 (!define-type-method (values :negate) (type)
158 (error "NOT VALUES too confusing on ~S" (type-specifier type)))
160 (!define-type-method (values :unparse) (type)
162 (let ((unparsed (unparse-args-types type)))
163 (if (or (values-type-optional type)
164 (values-type-rest type)
165 (values-type-allowp type))
167 (nconc unparsed '(&optional))))))
169 ;;; Return true if LIST1 and LIST2 have the same elements in the same
170 ;;; positions according to TYPE=. We return NIL, NIL if there is an
171 ;;; uncertain comparison.
172 (defun type=-list (list1 list2)
173 (declare (list list1 list2))
174 (do ((types1 list1 (cdr types1))
175 (types2 list2 (cdr types2)))
176 ((or (null types1) (null types2))
177 (if (or types1 types2)
180 (multiple-value-bind (val win)
181 (type= (first types1) (first types2))
183 (return (values nil nil)))
185 (return (values nil t))))))
187 (!define-type-method (values :simple-=) (type1 type2)
188 (type=-args type1 type2))
190 (!define-type-class function)
192 ;;; a flag that we can bind to cause complex function types to be
193 ;;; unparsed as FUNCTION. This is useful when we want a type that we
194 ;;; can pass to TYPEP.
195 (defvar *unparse-fun-type-simplify*)
196 (!cold-init-forms (setq *unparse-fun-type-simplify* nil))
198 (!define-type-method (function :negate) (type)
199 (make-negation-type :type type))
201 (!define-type-method (function :unparse) (type)
202 (if *unparse-fun-type-simplify*
205 (if (fun-type-wild-args type)
207 (unparse-args-types type))
209 (fun-type-returns type)))))
211 ;;; The meaning of this is a little confused. On the one hand, all
212 ;;; function objects are represented the same way regardless of the
213 ;;; arglists and return values, and apps don't get to ask things like
214 ;;; (TYPEP #'FOO (FUNCTION (FIXNUM) *)) in any meaningful way. On the
215 ;;; other hand, Python wants to reason about function types. So...
216 (!define-type-method (function :simple-subtypep) (type1 type2)
217 (flet ((fun-type-simple-p (type)
218 (not (or (fun-type-rest type)
219 (fun-type-keyp type))))
220 (every-csubtypep (types1 types2)
224 do (multiple-value-bind (res sure-p)
226 (unless res (return (values res sure-p))))
227 finally (return (values t t)))))
228 (and/type (values-subtypep (fun-type-returns type1)
229 (fun-type-returns type2))
230 (cond ((fun-type-wild-args type2) (values t t))
231 ((fun-type-wild-args type1)
232 (cond ((fun-type-keyp type2) (values nil nil))
233 ((not (fun-type-rest type2)) (values nil t))
234 ((not (null (fun-type-required type2)))
236 (t (and/type (type= *universal-type*
237 (fun-type-rest type2))
242 ((not (and (fun-type-simple-p type1)
243 (fun-type-simple-p type2)))
245 (t (multiple-value-bind (min1 max1) (fun-type-nargs type1)
246 (multiple-value-bind (min2 max2) (fun-type-nargs type2)
247 (cond ((or (> max1 max2) (< min1 min2))
249 ((and (= min1 min2) (= max1 max2))
250 (and/type (every-csubtypep
251 (fun-type-required type1)
252 (fun-type-required type2))
254 (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 (let ((ftype (specifier-type 'function)))
272 (cond ((eq type1 ftype) type2)
273 ((eq type2 ftype) type1)
274 (t (let ((rtype (values-type-intersection (fun-type-returns type1)
275 (fun-type-returns type2))))
276 (flet ((change-returns (ftype rtype)
277 (declare (type fun-type ftype) (type ctype rtype))
278 (make-fun-type :required (fun-type-required ftype)
279 :optional (fun-type-optional ftype)
280 :keyp (fun-type-keyp ftype)
281 :keywords (fun-type-keywords ftype)
282 :allowp (fun-type-allowp ftype)
285 ((fun-type-wild-args type1)
286 (if (fun-type-wild-args type2)
287 (make-fun-type :wild-args t
289 (change-returns type2 rtype)))
290 ((fun-type-wild-args type2)
291 (change-returns type1 rtype))
292 (t (multiple-value-bind (req opt rest)
293 (args-type-op type1 type2 #'type-intersection #'max)
294 (make-fun-type :required req
298 :allowp (and (fun-type-allowp type1)
299 (fun-type-allowp type2))
300 :returns rtype))))))))))
302 ;;; The union or intersection of a subclass of FUNCTION with a
303 ;;; FUNCTION type is somewhat complicated.
304 (!define-type-method (function :complex-intersection2) (type1 type2)
306 ((type= type1 (specifier-type 'function)) type2)
307 ((csubtypep type1 (specifier-type 'function)) nil)
308 (t :call-other-method)))
309 (!define-type-method (function :complex-union2) (type1 type2)
310 (declare (ignore type2))
311 ;; TYPE2 is a FUNCTION type. If TYPE1 is a classoid type naming
312 ;; FUNCTION, then it is the union of the two; otherwise, there is no
315 ((type= type1 (specifier-type 'function)) type1)
318 (!define-type-method (function :simple-=) (type1 type2)
319 (macrolet ((compare (comparator field)
320 (let ((reader (symbolicate '#:fun-type- field)))
321 `(,comparator (,reader type1) (,reader type2)))))
322 (and/type (compare type= returns)
323 (cond ((neq (fun-type-wild-args type1) (fun-type-wild-args type2))
325 ((eq (fun-type-wild-args type1) t)
327 (t (type=-args type1 type2))))))
329 (!define-type-class constant :inherits values)
331 (!define-type-method (constant :negate) (type)
332 (error "NOT CONSTANT too confusing on ~S" (type-specifier type)))
334 (!define-type-method (constant :unparse) (type)
335 `(constant-arg ,(type-specifier (constant-type-type type))))
337 (!define-type-method (constant :simple-=) (type1 type2)
338 (type= (constant-type-type type1) (constant-type-type type2)))
340 (!def-type-translator constant-arg (type)
341 (make-constant-type :type (single-value-specifier-type type)))
343 ;;; Return the lambda-list-like type specification corresponding
345 (declaim (ftype (function (args-type) list) unparse-args-types))
346 (defun unparse-args-types (type)
349 (dolist (arg (args-type-required type))
350 (result (type-specifier arg)))
352 (when (args-type-optional type)
354 (dolist (arg (args-type-optional type))
355 (result (type-specifier arg))))
357 (when (args-type-rest type)
359 (result (type-specifier (args-type-rest type))))
361 (when (args-type-keyp type)
363 (dolist (key (args-type-keywords type))
364 (result (list (key-info-name key)
365 (type-specifier (key-info-type key))))))
367 (when (args-type-allowp type)
368 (result '&allow-other-keys))
372 (!def-type-translator function (&optional (args '*) (result '*))
373 (make-fun-type :args args
374 :returns (coerce-to-values (values-specifier-type result))))
376 (!def-type-translator values (&rest values)
377 (make-values-type :args values))
379 ;;;; VALUES types interfaces
381 ;;;; We provide a few special operations that can be meaningfully used
382 ;;;; on VALUES types (as well as on any other type).
384 (defun type-single-value-p (type)
385 (and (values-type-p type)
386 (not (values-type-rest type))
387 (null (values-type-optional type))
388 (singleton-p (values-type-required type))))
390 ;;; Return the type of the first value indicated by TYPE. This is used
391 ;;; by people who don't want to have to deal with VALUES types.
392 #!-sb-fluid (declaim (freeze-type values-type))
393 ; (inline single-value-type))
394 (defun single-value-type (type)
395 (declare (type ctype type))
396 (cond ((eq type *wild-type*)
398 ((eq type *empty-type*)
400 ((not (values-type-p type))
402 (t (or (car (args-type-required type))
403 (car (args-type-optional type))
404 (args-type-rest type)
405 (specifier-type 'null)))))
407 ;;; Return the minimum number of arguments that a function can be
408 ;;; called with, and the maximum number or NIL. If not a function
409 ;;; type, return NIL, NIL.
410 (defun fun-type-nargs (type)
411 (declare (type ctype type))
412 (if (and (fun-type-p type) (not (fun-type-wild-args type)))
413 (let ((fixed (length (args-type-required type))))
414 (if (or (args-type-rest type)
415 (args-type-keyp type)
416 (args-type-allowp type))
418 (values fixed (+ fixed (length (args-type-optional type))))))
421 ;;; Determine whether TYPE corresponds to a definite number of values.
422 ;;; The first value is a list of the types for each value, and the
423 ;;; second value is the number of values. If the number of values is
424 ;;; not fixed, then return NIL and :UNKNOWN.
425 (defun values-types (type)
426 (declare (type ctype type))
427 (cond ((or (eq type *wild-type*) (eq type *empty-type*))
428 (values nil :unknown))
429 ((or (args-type-optional type)
430 (args-type-rest type))
431 (values nil :unknown))
433 (let ((req (args-type-required type)))
434 (values req (length req))))))
436 ;;; Return two values:
437 ;;; 1. A list of all the positional (fixed and optional) types.
438 ;;; 2. The &REST type (if any). If no &REST, then the DEFAULT-TYPE.
439 (defun values-type-types (type &optional (default-type *empty-type*))
440 (declare (type ctype type))
441 (if (eq type *wild-type*)
442 (values nil *universal-type*)
443 (values (append (args-type-required type)
444 (args-type-optional type))
445 (cond ((args-type-rest type))
448 ;;; types of values in (the <type> (values o_1 ... o_n))
449 (defun values-type-out (type count)
450 (declare (type ctype type) (type unsigned-byte count))
451 (if (eq type *wild-type*)
452 (make-list count :initial-element *universal-type*)
454 (flet ((process-types (types)
455 (loop for type in types
459 (process-types (values-type-required type))
460 (process-types (values-type-optional type))
462 (loop with rest = (the ctype (values-type-rest type))
467 ;;; types of variable in (m-v-bind (v_1 ... v_n) (the <type> ...
468 (defun values-type-in (type count)
469 (declare (type ctype type) (type unsigned-byte count))
470 (if (eq type *wild-type*)
471 (make-list count :initial-element *universal-type*)
473 (let ((null-type (specifier-type 'null)))
474 (loop for type in (values-type-required type)
478 (loop for type in (values-type-optional type)
481 do (res (type-union type null-type)))
483 (loop with rest = (acond ((values-type-rest type)
484 (type-union it null-type))
490 ;;; Return a list of OPERATION applied to the types in TYPES1 and
491 ;;; TYPES2, padding with REST2 as needed. TYPES1 must not be shorter
492 ;;; than TYPES2. The second value is T if OPERATION always returned a
493 ;;; true second value.
494 (defun fixed-values-op (types1 types2 rest2 operation)
495 (declare (list types1 types2) (type ctype rest2) (type function operation))
497 (values (mapcar (lambda (t1 t2)
498 (multiple-value-bind (res win)
499 (funcall operation t1 t2)
505 (make-list (- (length types1) (length types2))
506 :initial-element rest2)))
509 ;;; If TYPE isn't a values type, then make it into one.
510 (defun-cached (%coerce-to-values
512 :hash-function (lambda (type)
513 (logand (type-hash-value type)
516 (cond ((multiple-value-bind (res sure)
517 (csubtypep (specifier-type 'null) type)
518 (and (not res) sure))
519 ;; FIXME: What should we do with (NOT SURE)?
520 (make-values-type :required (list type) :rest *universal-type*))
522 (make-values-type :optional (list type) :rest *universal-type*))))
524 (defun coerce-to-values (type)
525 (declare (type ctype type))
526 (cond ((or (eq type *universal-type*)
527 (eq type *wild-type*))
529 ((values-type-p type)
531 (t (%coerce-to-values type))))
533 ;;; Return type, corresponding to ANSI short form of VALUES type
535 (defun make-short-values-type (types)
536 (declare (list types))
537 (let ((last-required (position-if
539 (not/type (csubtypep (specifier-type 'null) type)))
543 (make-values-type :required (subseq types 0 (1+ last-required))
544 :optional (subseq types (1+ last-required))
545 :rest *universal-type*)
546 (make-values-type :optional types :rest *universal-type*))))
548 (defun make-single-value-type (type)
549 (make-values-type :required (list type)))
551 ;;; Do the specified OPERATION on TYPE1 and TYPE2, which may be any
552 ;;; type, including VALUES types. With VALUES types such as:
555 ;;; we compute the more useful result
556 ;;; (VALUES (<operation> a0 b0) (<operation> a1 b1))
557 ;;; rather than the precise result
558 ;;; (<operation> (values a0 a1) (values b0 b1))
559 ;;; This has the virtue of always keeping the VALUES type specifier
560 ;;; outermost, and retains all of the information that is really
561 ;;; useful for static type analysis. We want to know what is always
562 ;;; true of each value independently. It is worthless to know that if
563 ;;; the first value is B0 then the second will be B1.
565 ;;; If the VALUES count signatures differ, then we produce a result with
566 ;;; the required VALUE count chosen by NREQ when applied to the number
567 ;;; of required values in TYPE1 and TYPE2. Any &KEY values become
568 ;;; &REST T (anyone who uses keyword values deserves to lose.)
570 ;;; The second value is true if the result is definitely empty or if
571 ;;; OPERATION returned true as its second value each time we called
572 ;;; it. Since we approximate the intersection of VALUES types, the
573 ;;; second value being true doesn't mean the result is exact.
574 (defun args-type-op (type1 type2 operation nreq)
575 (declare (type ctype type1 type2)
576 (type function operation nreq))
577 (when (eq type1 type2)
579 (multiple-value-bind (types1 rest1)
580 (values-type-types type1)
581 (multiple-value-bind (types2 rest2)
582 (values-type-types type2)
583 (multiple-value-bind (rest rest-exact)
584 (funcall operation rest1 rest2)
585 (multiple-value-bind (res res-exact)
586 (if (< (length types1) (length types2))
587 (fixed-values-op types2 types1 rest1 operation)
588 (fixed-values-op types1 types2 rest2 operation))
589 (let* ((req (funcall nreq
590 (length (args-type-required type1))
591 (length (args-type-required type2))))
592 (required (subseq res 0 req))
593 (opt (subseq res req)))
594 (values required opt rest
595 (and rest-exact res-exact))))))))
597 (defun values-type-op (type1 type2 operation nreq)
598 (multiple-value-bind (required optional rest exactp)
599 (args-type-op type1 type2 operation nreq)
600 (values (make-values-type :required required
605 (defun type=-args (type1 type2)
606 (macrolet ((compare (comparator field)
607 (let ((reader (symbolicate '#:args-type- field)))
608 `(,comparator (,reader type1) (,reader type2)))))
610 (cond ((null (args-type-rest type1))
611 (values (null (args-type-rest type2)) t))
612 ((null (args-type-rest type2))
615 (compare type= rest)))
616 (and/type (and/type (compare type=-list required)
617 (compare type=-list optional))
618 (if (or (args-type-keyp type1) (args-type-keyp type2))
622 ;;; Do a union or intersection operation on types that might be values
623 ;;; types. The result is optimized for utility rather than exactness,
624 ;;; but it is guaranteed that it will be no smaller (more restrictive)
625 ;;; than the precise result.
627 ;;; The return convention seems to be analogous to
628 ;;; TYPES-EQUAL-OR-INTERSECT. -- WHN 19990910.
629 (defun-cached (values-type-union :hash-function type-cache-hash
632 :init-wrapper !cold-init-forms)
633 ((type1 eq) (type2 eq))
634 (declare (type ctype type1 type2))
635 (cond ((or (eq type1 *wild-type*) (eq type2 *wild-type*)) *wild-type*)
636 ((eq type1 *empty-type*) type2)
637 ((eq type2 *empty-type*) type1)
639 (values (values-type-op type1 type2 #'type-union #'min)))))
641 (defun-cached (values-type-intersection :hash-function type-cache-hash
643 :default (values nil)
644 :init-wrapper !cold-init-forms)
645 ((type1 eq) (type2 eq))
646 (declare (type ctype type1 type2))
647 (cond ((eq type1 *wild-type*)
648 (coerce-to-values type2))
649 ((or (eq type2 *wild-type*) (eq type2 *universal-type*))
651 ((or (eq type1 *empty-type*) (eq type2 *empty-type*))
653 ((and (not (values-type-p type2))
654 (values-type-required type1))
655 (let ((req1 (values-type-required type1)))
656 (make-values-type :required (cons (type-intersection (first req1) type2)
658 :optional (values-type-optional type1)
659 :rest (values-type-rest type1)
660 :allowp (values-type-allowp type1))))
662 (values (values-type-op type1 (coerce-to-values type2)
666 ;;; This is like TYPES-EQUAL-OR-INTERSECT, except that it sort of
667 ;;; works on VALUES types. Note that due to the semantics of
668 ;;; VALUES-TYPE-INTERSECTION, this might return (VALUES T T) when
669 ;;; there isn't really any intersection.
670 (defun values-types-equal-or-intersect (type1 type2)
671 (cond ((or (eq type1 *empty-type*) (eq type2 *empty-type*))
673 ((or (eq type1 *wild-type*) (eq type2 *wild-type*))
676 (let ((res (values-type-intersection type1 type2)))
677 (values (not (eq res *empty-type*))
680 ;;; a SUBTYPEP-like operation that can be used on any types, including
682 (defun-cached (values-subtypep :hash-function type-cache-hash
685 :default (values nil :empty)
686 :init-wrapper !cold-init-forms)
687 ((type1 eq) (type2 eq))
688 (declare (type ctype type1 type2))
689 (cond ((or (eq type2 *wild-type*) (eq type2 *universal-type*)
690 (eq type1 *empty-type*))
692 ((eq type1 *wild-type*)
693 (values (eq type2 *wild-type*) t))
694 ((or (eq type2 *empty-type*)
695 (not (values-types-equal-or-intersect type1 type2)))
697 ((and (not (values-type-p type2))
698 (values-type-required type1))
699 (csubtypep (first (values-type-required type1))
701 (t (setq type2 (coerce-to-values type2))
702 (multiple-value-bind (types1 rest1) (values-type-types type1)
703 (multiple-value-bind (types2 rest2) (values-type-types type2)
704 (cond ((< (length (values-type-required type1))
705 (length (values-type-required type2)))
707 ((< (length types1) (length types2))
710 (do ((t1 types1 (rest t1))
711 (t2 types2 (rest t2)))
713 (csubtypep rest1 rest2))
714 (multiple-value-bind (res win-p)
715 (csubtypep (first t1) (first t2))
717 (return (values nil nil)))
719 (return (values nil t))))))))))))
721 ;;;; type method interfaces
723 ;;; like SUBTYPEP, only works on CTYPE structures
724 (defun-cached (csubtypep :hash-function type-cache-hash
727 :default (values nil :empty)
728 :init-wrapper !cold-init-forms)
729 ((type1 eq) (type2 eq))
730 (declare (type ctype type1 type2))
731 (cond ((or (eq type1 type2)
732 (eq type1 *empty-type*)
733 (eq type2 *universal-type*))
736 ((eq type1 *universal-type*)
739 (!invoke-type-method :simple-subtypep :complex-subtypep-arg2
741 :complex-arg1 :complex-subtypep-arg1))))
743 ;;; Just parse the type specifiers and call CSUBTYPE.
744 (defun sb!xc:subtypep (type1 type2 &optional environment)
746 "Return two values indicating the relationship between type1 and type2.
747 If values are T and T, type1 definitely is a subtype of type2.
748 If values are NIL and T, type1 definitely is not a subtype of type2.
749 If values are NIL and NIL, it couldn't be determined."
750 (declare (ignore environment))
751 (csubtypep (specifier-type type1) (specifier-type type2)))
753 ;;; If two types are definitely equivalent, return true. The second
754 ;;; value indicates whether the first value is definitely correct.
755 ;;; This should only fail in the presence of HAIRY types.
756 (defun-cached (type= :hash-function type-cache-hash
759 :default (values nil :empty)
760 :init-wrapper !cold-init-forms)
761 ((type1 eq) (type2 eq))
762 (declare (type ctype type1 type2))
765 (!invoke-type-method :simple-= :complex-= type1 type2)))
767 ;;; Not exactly the negation of TYPE=, since when the relationship is
768 ;;; uncertain, we still return NIL, NIL. This is useful in cases where
769 ;;; the conservative assumption is =.
770 (defun type/= (type1 type2)
771 (declare (type ctype type1 type2))
772 (multiple-value-bind (res win) (type= type1 type2)
777 ;;; the type method dispatch case of TYPE-UNION2
778 (defun %type-union2 (type1 type2)
779 ;; As in %TYPE-INTERSECTION2, it seems to be a good idea to give
780 ;; both argument orders a chance at COMPLEX-INTERSECTION2. Unlike
781 ;; %TYPE-INTERSECTION2, though, I don't have a specific case which
782 ;; demonstrates this is actually necessary. Also unlike
783 ;; %TYPE-INTERSECTION2, there seems to be no need to distinguish
784 ;; between not finding a method and having a method return NIL.
786 (!invoke-type-method :simple-union2 :complex-union2
789 (declare (inline 1way))
790 (or (1way type1 type2)
791 (1way type2 type1))))
793 ;;; Find a type which includes both types. Any inexactness is
794 ;;; represented by the fuzzy element types; we return a single value
795 ;;; that is precise to the best of our knowledge. This result is
796 ;;; simplified into the canonical form, thus is not a UNION-TYPE
797 ;;; unless we find no other way to represent the result.
798 (defun-cached (type-union2 :hash-function type-cache-hash
800 :init-wrapper !cold-init-forms)
801 ((type1 eq) (type2 eq))
802 ;; KLUDGE: This was generated from TYPE-INTERSECTION2 by Ye Olde Cut And
803 ;; Paste technique of programming. If it stays around (as opposed to
804 ;; e.g. fading away in favor of some CLOS solution) the shared logic
805 ;; should probably become shared code. -- WHN 2001-03-16
806 (declare (type ctype type1 type2))
807 (cond ((eq type1 type2)
809 ((csubtypep type1 type2) type2)
810 ((csubtypep type2 type1) type1)
811 ((or (union-type-p type1)
812 (union-type-p type2))
813 ;; Unions of UNION-TYPE should have the UNION-TYPE-TYPES
814 ;; values broken out and united separately. The full TYPE-UNION
815 ;; function knows how to do this, so let it handle it.
816 (type-union type1 type2))
818 ;; the ordinary case: we dispatch to type methods
819 (%type-union2 type1 type2))))
821 ;;; the type method dispatch case of TYPE-INTERSECTION2
822 (defun %type-intersection2 (type1 type2)
823 ;; We want to give both argument orders a chance at
824 ;; COMPLEX-INTERSECTION2. Without that, the old CMU CL type
825 ;; methods could give noncommutative results, e.g.
826 ;; (TYPE-INTERSECTION2 *EMPTY-TYPE* SOME-HAIRY-TYPE)
828 ;; (TYPE-INTERSECTION2 SOME-HAIRY-TYPE *EMPTY-TYPE*)
829 ;; => #<NAMED-TYPE NIL>, T
830 ;; We also need to distinguish between the case where we found a
831 ;; type method, and it returned NIL, and the case where we fell
832 ;; through without finding any type method. An example of the first
833 ;; case is the intersection of a HAIRY-TYPE with some ordinary type.
834 ;; An example of the second case is the intersection of two
835 ;; completely-unrelated types, e.g. CONS and NUMBER, or SYMBOL and
838 ;; (Why yes, CLOS probably *would* be nicer..)
840 (!invoke-type-method :simple-intersection2 :complex-intersection2
842 :default :call-other-method)))
843 (declare (inline 1way))
844 (let ((xy (1way type1 type2)))
845 (or (and (not (eql xy :call-other-method)) xy)
846 (let ((yx (1way type2 type1)))
847 (or (and (not (eql yx :call-other-method)) yx)
848 (cond ((and (eql xy :call-other-method)
849 (eql yx :call-other-method))
852 (aver (and (not xy) (not yx))) ; else handled above
855 (defun-cached (type-intersection2 :hash-function type-cache-hash
859 :init-wrapper !cold-init-forms)
860 ((type1 eq) (type2 eq))
861 (declare (type ctype type1 type2))
862 (cond ((eq type1 type2)
863 ;; FIXME: For some reason, this doesn't catch e.g. type1 =
864 ;; type2 = (SPECIFIER-TYPE
865 ;; 'SOME-UNKNOWN-TYPE). Investigate. - CSR, 2002-04-10
867 ((or (intersection-type-p type1)
868 (intersection-type-p type2))
869 ;; Intersections of INTERSECTION-TYPE should have the
870 ;; INTERSECTION-TYPE-TYPES values broken out and intersected
871 ;; separately. The full TYPE-INTERSECTION function knows how
872 ;; to do that, so let it handle it.
873 (type-intersection type1 type2))
875 ;; the ordinary case: we dispatch to type methods
876 (%type-intersection2 type1 type2))))
878 ;;; Return as restrictive and simple a type as we can discover that is
879 ;;; no more restrictive than the intersection of TYPE1 and TYPE2. At
880 ;;; worst, we arbitrarily return one of the arguments as the first
881 ;;; value (trying not to return a hairy type).
882 (defun type-approx-intersection2 (type1 type2)
883 (cond ((type-intersection2 type1 type2))
884 ((hairy-type-p type1) type2)
887 ;;; a test useful for checking whether a derived type matches a
890 ;;; The first value is true unless the types don't intersect and
891 ;;; aren't equal. The second value is true if the first value is
892 ;;; definitely correct. NIL is considered to intersect with any type.
893 ;;; If T is a subtype of either type, then we also return T, T. This
894 ;;; way we recognize that hairy types might intersect with T.
895 (defun types-equal-or-intersect (type1 type2)
896 (declare (type ctype type1 type2))
897 (if (or (eq type1 *empty-type*) (eq type2 *empty-type*))
899 (let ((intersection2 (type-intersection2 type1 type2)))
900 (cond ((not intersection2)
901 (if (or (csubtypep *universal-type* type1)
902 (csubtypep *universal-type* type2))
905 ((eq intersection2 *empty-type*) (values nil t))
908 ;;; Return a Common Lisp type specifier corresponding to the TYPE
910 (defun type-specifier (type)
911 (declare (type ctype type))
912 (funcall (type-class-unparse (type-class-info type)) type))
914 (defun-cached (type-negation :hash-function (lambda (type)
915 (logand (type-hash-value type)
920 :init-wrapper !cold-init-forms)
922 (declare (type ctype type))
923 (funcall (type-class-negate (type-class-info type)) type))
925 ;;; (VALUES-SPECIFIER-TYPE and SPECIFIER-TYPE moved from here to
926 ;;; early-type.lisp by WHN ca. 19990201.)
928 ;;; Take a list of type specifiers, computing the translation of each
929 ;;; specifier and defining it as a builtin type.
930 (declaim (ftype (function (list) (values)) precompute-types))
931 (defun precompute-types (specs)
933 (let ((res (specifier-type spec)))
934 (unless (unknown-type-p res)
935 (setf (info :type :builtin spec) res)
936 ;; KLUDGE: the three copies of this idiom in this file (and
937 ;; the one in class.lisp as at sbcl-0.7.4.1x) should be
938 ;; coalesced, or perhaps the error-detecting code that
939 ;; disallows redefinition of :PRIMITIVE types should be
940 ;; rewritten to use *TYPE-SYSTEM-FINALIZED* (rather than
941 ;; *TYPE-SYSTEM-INITIALIZED*). The effect of this is not to
942 ;; cause redefinition errors when precompute-types is called
943 ;; for a second time while building the target compiler using
944 ;; the cross-compiler. -- CSR, trying to explain why this
945 ;; isn't completely wrong, 2002-06-07
946 (setf (info :type :kind spec) #+sb-xc-host :defined #-sb-xc-host :primitive))))
949 ;;;; general TYPE-UNION and TYPE-INTERSECTION operations
951 ;;;; These are fully general operations on CTYPEs: they'll always
952 ;;;; return a CTYPE representing the result.
954 ;;; shared logic for unions and intersections: Return a list of
955 ;;; types representing the same types as INPUT-TYPES, but with
956 ;;; COMPOUND-TYPEs satisfying %COMPOUND-TYPE-P broken up into their
957 ;;; component types, and with any SIMPLY2 simplifications applied.
959 ((def (name compound-type-p simplify2)
960 `(defun ,name (types)
962 (multiple-value-bind (first rest)
963 (if (,compound-type-p (car types))
964 (values (car (compound-type-types (car types)))
965 (append (cdr (compound-type-types (car types)))
967 (values (car types) (cdr types)))
968 (let ((rest (,name rest)) u)
969 (dolist (r rest (cons first rest))
970 (when (setq u (,simplify2 first r))
971 (return (,name (nsubstitute u r rest)))))))))))
972 (def simplify-intersections intersection-type-p type-intersection2)
973 (def simplify-unions union-type-p type-union2))
975 (defun maybe-distribute-one-union (union-type types)
976 (let* ((intersection (apply #'type-intersection types))
977 (union (mapcar (lambda (x) (type-intersection x intersection))
978 (union-type-types union-type))))
979 (if (notany (lambda (x) (or (hairy-type-p x)
980 (intersection-type-p x)))
985 (defun type-intersection (&rest input-types)
986 (%type-intersection input-types))
987 (defun-cached (%type-intersection :hash-bits 8
988 :hash-function (lambda (x)
989 (logand (sxhash x) #xff)))
990 ((input-types equal))
991 (let ((simplified-types (simplify-intersections input-types)))
992 (declare (type list simplified-types))
993 ;; We want to have a canonical representation of types (or failing
994 ;; that, punt to HAIRY-TYPE). Canonical representation would have
995 ;; intersections inside unions but not vice versa, since you can
996 ;; always achieve that by the distributive rule. But we don't want
997 ;; to just apply the distributive rule, since it would be too easy
998 ;; to end up with unreasonably huge type expressions. So instead
999 ;; we try to generate a simple type by distributing the union; if
1000 ;; the type can't be made simple, we punt to HAIRY-TYPE.
1001 (if (and (cdr simplified-types) (some #'union-type-p simplified-types))
1002 (let* ((first-union (find-if #'union-type-p simplified-types))
1003 (other-types (coerce (remove first-union simplified-types)
1005 (distributed (maybe-distribute-one-union first-union
1008 (apply #'type-union distributed)
1010 :specifier `(and ,@(map 'list
1012 simplified-types)))))
1014 ((null simplified-types) *universal-type*)
1015 ((null (cdr simplified-types)) (car simplified-types))
1016 (t (%make-intersection-type
1017 (some #'type-enumerable simplified-types)
1018 simplified-types))))))
1020 (defun type-union (&rest input-types)
1021 (%type-union input-types))
1022 (defun-cached (%type-union :hash-bits 8
1023 :hash-function (lambda (x)
1024 (logand (sxhash x) #xff)))
1025 ((input-types equal))
1026 (let ((simplified-types (simplify-unions input-types)))
1028 ((null simplified-types) *empty-type*)
1029 ((null (cdr simplified-types)) (car simplified-types))
1031 (every #'type-enumerable simplified-types)
1032 simplified-types)))))
1036 (!define-type-class named)
1039 (macrolet ((frob (name var)
1041 (setq ,var (make-named-type :name ',name))
1042 (setf (info :type :kind ',name)
1043 #+sb-xc-host :defined #-sb-xc-host :primitive)
1044 (setf (info :type :builtin ',name) ,var))))
1045 ;; KLUDGE: In ANSI, * isn't really the name of a type, it's just a
1046 ;; special symbol which can be stuck in some places where an
1047 ;; ordinary type can go, e.g. (ARRAY * 1) instead of (ARRAY T 1).
1048 ;; In SBCL it also used to denote universal VALUES type.
1049 (frob * *wild-type*)
1050 (frob nil *empty-type*)
1051 (frob t *universal-type*))
1052 (setf *universal-fun-type*
1053 (make-fun-type :wild-args t
1054 :returns *wild-type*)))
1056 (!define-type-method (named :simple-=) (type1 type2)
1057 ;;(aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1058 (values (eq type1 type2) t))
1060 (defun cons-type-might-be-empty-type (type)
1061 (declare (type cons-type type))
1062 (let ((car-type (cons-type-car-type type))
1063 (cdr-type (cons-type-cdr-type type)))
1065 (if (cons-type-p car-type)
1066 (cons-type-might-be-empty-type car-type)
1067 (multiple-value-bind (yes surep)
1068 (type= car-type *empty-type*)
1071 (if (cons-type-p cdr-type)
1072 (cons-type-might-be-empty-type cdr-type)
1073 (multiple-value-bind (yes surep)
1074 (type= cdr-type *empty-type*)
1078 (!define-type-method (named :complex-=) (type1 type2)
1080 ((and (eq type2 *empty-type*)
1081 (or (and (intersection-type-p type1)
1082 ;; not allowed to be unsure on these... FIXME: keep
1083 ;; the list of CL types that are intersection types
1084 ;; once and only once.
1085 (not (or (type= type1 (specifier-type 'ratio))
1086 (type= type1 (specifier-type 'keyword)))))
1087 (and (cons-type-p type1)
1088 (cons-type-might-be-empty-type type1))))
1089 ;; things like (AND (EQL 0) (SATISFIES ODDP)) or (AND FUNCTION
1090 ;; STREAM) can get here. In general, we can't really tell
1091 ;; whether these are equal to NIL or not, so
1093 ((type-might-contain-other-types-p type1)
1094 (invoke-complex-=-other-method type1 type2))
1095 (t (values nil t))))
1097 (!define-type-method (named :simple-subtypep) (type1 type2)
1098 (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1099 (values (or (eq type1 *empty-type*) (eq type2 *wild-type*)) t))
1101 (!define-type-method (named :complex-subtypep-arg1) (type1 type2)
1102 ;; This AVER causes problems if we write accurate methods for the
1103 ;; union (and possibly intersection) types which then delegate to
1104 ;; us; while a user shouldn't get here, because of the odd status of
1105 ;; *wild-type* a type-intersection executed by the compiler can. -
1108 ;; (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1109 (cond ((eq type1 *empty-type*)
1111 (;; When TYPE2 might be the universal type in disguise
1112 (type-might-contain-other-types-p type2)
1113 ;; Now that the UNION and HAIRY COMPLEX-SUBTYPEP-ARG2 methods
1114 ;; can delegate to us (more or less as CALL-NEXT-METHOD) when
1115 ;; they're uncertain, we can't just barf on COMPOUND-TYPE and
1116 ;; HAIRY-TYPEs as we used to. Instead we deal with the
1117 ;; problem (where at least part of the problem is cases like
1118 ;; (SUBTYPEP T '(SATISFIES FOO))
1120 ;; (SUBTYPEP T '(AND (SATISFIES FOO) (SATISFIES BAR)))
1121 ;; where the second type is a hairy type like SATISFIES, or
1122 ;; is a compound type which might contain a hairy type) by
1123 ;; returning uncertainty.
1126 ;; By elimination, TYPE1 is the universal type.
1127 (aver (eq type1 *universal-type*))
1128 ;; This case would have been picked off by the SIMPLE-SUBTYPEP
1129 ;; method, and so shouldn't appear here.
1130 (aver (not (eq type2 *universal-type*)))
1131 ;; Since TYPE2 is not EQ *UNIVERSAL-TYPE* and is not the
1132 ;; universal type in disguise, TYPE2 is not a superset of TYPE1.
1135 (!define-type-method (named :complex-subtypep-arg2) (type1 type2)
1136 (aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1137 (cond ((eq type2 *universal-type*)
1139 ((or (type-might-contain-other-types-p type1)
1140 (and (cons-type-p type1)
1141 (cons-type-might-be-empty-type type1)))
1142 ;; those types can be *EMPTY-TYPE* or *UNIVERSAL-TYPE* in
1143 ;; disguise. So we'd better delegate.
1144 (invoke-complex-subtypep-arg1-method type1 type2))
1146 ;; FIXME: This seems to rely on there only being 2 or 3
1147 ;; NAMED-TYPE values, and the exclusion of various
1148 ;; possibilities above. It would be good to explain it and/or
1149 ;; rewrite it so that it's clearer.
1150 (values (not (eq type2 *empty-type*)) t))))
1152 (!define-type-method (named :complex-intersection2) (type1 type2)
1153 ;; FIXME: This assertion failed when I added it in sbcl-0.6.11.13.
1154 ;; Perhaps when bug 85 is fixed it can be reenabled.
1155 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1156 (hierarchical-intersection2 type1 type2))
1158 (!define-type-method (named :complex-union2) (type1 type2)
1159 ;; Perhaps when bug 85 is fixed this can be reenabled.
1160 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1161 (hierarchical-union2 type1 type2))
1163 (!define-type-method (named :negate) (x)
1164 (aver (not (eq x *wild-type*)))
1166 ((eq x *universal-type*) *empty-type*)
1167 ((eq x *empty-type*) *universal-type*)
1168 (t (bug "NAMED type not universal, wild or empty: ~S" x))))
1170 (!define-type-method (named :unparse) (x)
1171 (named-type-name x))
1173 ;;;; hairy and unknown types
1175 (!define-type-method (hairy :negate) (x)
1176 (make-negation-type :type x))
1178 (!define-type-method (hairy :unparse) (x)
1179 (hairy-type-specifier x))
1181 (!define-type-method (hairy :simple-subtypep) (type1 type2)
1182 (let ((hairy-spec1 (hairy-type-specifier type1))
1183 (hairy-spec2 (hairy-type-specifier type2)))
1184 (cond ((equal-but-no-car-recursion hairy-spec1 hairy-spec2)
1187 (values nil nil)))))
1189 (!define-type-method (hairy :complex-subtypep-arg2) (type1 type2)
1190 (invoke-complex-subtypep-arg1-method type1 type2))
1192 (!define-type-method (hairy :complex-subtypep-arg1) (type1 type2)
1193 (declare (ignore type1 type2))
1196 (!define-type-method (hairy :complex-=) (type1 type2)
1197 (if (and (unknown-type-p type2)
1198 (let* ((specifier2 (unknown-type-specifier type2))
1199 (name2 (if (consp specifier2)
1202 (info :type :kind name2)))
1203 (let ((type2 (specifier-type (unknown-type-specifier type2))))
1204 (if (unknown-type-p type2)
1206 (type= type1 type2)))
1209 (!define-type-method (hairy :simple-intersection2 :complex-intersection2)
1211 (if (type= type1 type2)
1215 (!define-type-method (hairy :simple-union2)
1217 (if (type= type1 type2)
1221 (!define-type-method (hairy :simple-=) (type1 type2)
1222 (if (equal-but-no-car-recursion (hairy-type-specifier type1)
1223 (hairy-type-specifier type2))
1227 (!def-type-translator satisfies (&whole whole fun)
1228 (declare (ignore fun))
1229 ;; Check legality of arguments.
1230 (destructuring-bind (satisfies predicate-name) whole
1231 (declare (ignore satisfies))
1232 (unless (symbolp predicate-name)
1233 (error 'simple-type-error
1234 :datum predicate-name
1235 :expected-type 'symbol
1236 :format-control "The SATISFIES predicate name is not a symbol: ~S"
1237 :format-arguments (list predicate-name))))
1239 (make-hairy-type :specifier whole))
1243 (!define-type-method (negation :negate) (x)
1244 (negation-type-type x))
1246 (!define-type-method (negation :unparse) (x)
1247 (if (type= (negation-type-type x) (specifier-type 'cons))
1249 `(not ,(type-specifier (negation-type-type x)))))
1251 (!define-type-method (negation :simple-subtypep) (type1 type2)
1252 (csubtypep (negation-type-type type2) (negation-type-type type1)))
1254 (!define-type-method (negation :complex-subtypep-arg2) (type1 type2)
1255 (let* ((complement-type2 (negation-type-type type2))
1256 (intersection2 (type-intersection2 type1
1259 ;; FIXME: if uncertain, maybe try arg1?
1260 (type= intersection2 *empty-type*)
1261 (invoke-complex-subtypep-arg1-method type1 type2))))
1263 (!define-type-method (negation :complex-subtypep-arg1) (type1 type2)
1264 ;; "Incrementally extended heuristic algorithms tend inexorably toward the
1265 ;; incomprehensible." -- http://www.unlambda.com/~james/lambda/lambda.txt
1267 ;; You may not believe this. I couldn't either. But then I sat down
1268 ;; and drew lots of Venn diagrams. Comments involving a and b refer
1269 ;; to the call (subtypep '(not a) 'b) -- CSR, 2002-02-27.
1271 ;; (Several logical truths in this block are true as long as
1272 ;; b/=T. As of sbcl-0.7.1.28, it seems impossible to construct a
1273 ;; case with b=T where we actually reach this type method, but
1274 ;; we'll test for and exclude this case anyway, since future
1275 ;; maintenance might make it possible for it to end up in this
1277 (multiple-value-bind (equal certain)
1278 (type= type2 *universal-type*)
1280 (return (values nil nil)))
1282 (return (values t t))))
1283 (let ((complement-type1 (negation-type-type type1)))
1284 ;; Do the special cases first, in order to give us a chance if
1285 ;; subtype/supertype relationships are hairy.
1286 (multiple-value-bind (equal certain)
1287 (type= complement-type1 type2)
1288 ;; If a = b, ~a is not a subtype of b (unless b=T, which was
1291 (return (values nil nil)))
1293 (return (values nil t))))
1294 ;; KLUDGE: ANSI requires that the SUBTYPEP result between any
1295 ;; two built-in atomic type specifiers never be uncertain. This
1296 ;; is hard to do cleanly for the built-in types whose
1297 ;; definitions include (NOT FOO), i.e. CONS and RATIO. However,
1298 ;; we can do it with this hack, which uses our global knowledge
1299 ;; that our implementation of the type system uses disjoint
1300 ;; implementation types to represent disjoint sets (except when
1301 ;; types are contained in other types). (This is a KLUDGE
1302 ;; because it's fragile. Various changes in internal
1303 ;; representation in the type system could make it start
1304 ;; confidently returning incorrect results.) -- WHN 2002-03-08
1305 (unless (or (type-might-contain-other-types-p complement-type1)
1306 (type-might-contain-other-types-p type2))
1307 ;; Because of the way our types which don't contain other
1308 ;; types are disjoint subsets of the space of possible values,
1309 ;; (SUBTYPEP '(NOT AA) 'B)=NIL when AA and B are simple (and B
1310 ;; is not T, as checked above).
1311 (return (values nil t)))
1312 ;; The old (TYPE= TYPE1 TYPE2) branch would never be taken, as
1313 ;; TYPE1 and TYPE2 will only be equal if they're both NOT types,
1314 ;; and then the :SIMPLE-SUBTYPEP method would be used instead.
1315 ;; But a CSUBTYPEP relationship might still hold:
1316 (multiple-value-bind (equal certain)
1317 (csubtypep complement-type1 type2)
1318 ;; If a is a subtype of b, ~a is not a subtype of b (unless
1319 ;; b=T, which was excluded above).
1321 (return (values nil nil)))
1323 (return (values nil t))))
1324 (multiple-value-bind (equal certain)
1325 (csubtypep type2 complement-type1)
1326 ;; If b is a subtype of a, ~a is not a subtype of b. (FIXME:
1327 ;; That's not true if a=T. Do we know at this point that a is
1330 (return (values nil nil)))
1332 (return (values nil t))))
1333 ;; old CSR comment ca. 0.7.2, now obsoleted by the SIMPLE-CTYPE?
1334 ;; KLUDGE case above: Other cases here would rely on being able
1335 ;; to catch all possible cases, which the fragility of this type
1336 ;; system doesn't inspire me; for instance, if a is type= to ~b,
1337 ;; then we want T, T; if this is not the case and the types are
1338 ;; disjoint (have an intersection of *empty-type*) then we want
1339 ;; NIL, T; else if the union of a and b is the *universal-type*
1340 ;; then we want T, T. So currently we still claim to be unsure
1341 ;; about e.g. (subtypep '(not fixnum) 'single-float).
1343 ;; OTOH we might still get here:
1346 (!define-type-method (negation :complex-=) (type1 type2)
1347 ;; (NOT FOO) isn't equivalent to anything that's not a negation
1348 ;; type, except possibly a type that might contain it in disguise.
1349 (declare (ignore type2))
1350 (if (type-might-contain-other-types-p type1)
1354 (!define-type-method (negation :simple-intersection2) (type1 type2)
1355 (let ((not1 (negation-type-type type1))
1356 (not2 (negation-type-type type2)))
1358 ((csubtypep not1 not2) type2)
1359 ((csubtypep not2 not1) type1)
1360 ;; Why no analagous clause to the disjoint in the SIMPLE-UNION2
1361 ;; method, below? The clause would read
1363 ;; ((EQ (TYPE-UNION NOT1 NOT2) *UNIVERSAL-TYPE*) *EMPTY-TYPE*)
1365 ;; but with proper canonicalization of negation types, there's
1366 ;; no way of constructing two negation types with union of their
1367 ;; negations being the universal type.
1369 (aver (not (eq (type-union not1 not2) *universal-type*)))
1372 (!define-type-method (negation :complex-intersection2) (type1 type2)
1374 ((csubtypep type1 (negation-type-type type2)) *empty-type*)
1375 ((eq (type-intersection type1 (negation-type-type type2)) *empty-type*)
1379 (!define-type-method (negation :simple-union2) (type1 type2)
1380 (let ((not1 (negation-type-type type1))
1381 (not2 (negation-type-type type2)))
1383 ((csubtypep not1 not2) type1)
1384 ((csubtypep not2 not1) type2)
1385 ((eq (type-intersection not1 not2) *empty-type*)
1389 (!define-type-method (negation :complex-union2) (type1 type2)
1391 ((csubtypep (negation-type-type type2) type1) *universal-type*)
1392 ((eq (type-intersection type1 (negation-type-type type2)) *empty-type*)
1396 (!define-type-method (negation :simple-=) (type1 type2)
1397 (type= (negation-type-type type1) (negation-type-type type2)))
1399 (!def-type-translator not (typespec)
1400 (type-negation (specifier-type typespec)))
1404 (!define-type-class number)
1406 (declaim (inline numeric-type-equal))
1407 (defun numeric-type-equal (type1 type2)
1408 (and (eq (numeric-type-class type1) (numeric-type-class type2))
1409 (eq (numeric-type-format type1) (numeric-type-format type2))
1410 (eq (numeric-type-complexp type1) (numeric-type-complexp type2))))
1412 (!define-type-method (number :simple-=) (type1 type2)
1414 (and (numeric-type-equal type1 type2)
1415 (equalp (numeric-type-low type1) (numeric-type-low type2))
1416 (equalp (numeric-type-high type1) (numeric-type-high type2)))
1419 (!define-type-method (number :negate) (type)
1420 (if (and (null (numeric-type-low type)) (null (numeric-type-high type)))
1421 (make-negation-type :type type)
1424 :type (modified-numeric-type type :low nil :high nil))
1426 ((null (numeric-type-low type))
1427 (modified-numeric-type
1429 :low (let ((h (numeric-type-high type)))
1430 (if (consp h) (car h) (list h)))
1432 ((null (numeric-type-high type))
1433 (modified-numeric-type
1436 :high (let ((l (numeric-type-low type)))
1437 (if (consp l) (car l) (list l)))))
1439 (modified-numeric-type
1442 :high (let ((l (numeric-type-low type)))
1443 (if (consp l) (car l) (list l))))
1444 (modified-numeric-type
1446 :low (let ((h (numeric-type-high type)))
1447 (if (consp h) (car h) (list h)))
1450 (!define-type-method (number :unparse) (type)
1451 (let* ((complexp (numeric-type-complexp type))
1452 (low (numeric-type-low type))
1453 (high (numeric-type-high type))
1454 (base (case (numeric-type-class type)
1456 (rational 'rational)
1457 (float (or (numeric-type-format type) 'float))
1460 (cond ((and (eq base 'integer) high low)
1461 (let ((high-count (logcount high))
1462 (high-length (integer-length high)))
1464 (cond ((= high 0) '(integer 0 0))
1466 ((and (= high-count high-length)
1467 (plusp high-length))
1468 `(unsigned-byte ,high-length))
1470 `(mod ,(1+ high)))))
1471 ((and (= low sb!xc:most-negative-fixnum)
1472 (= high sb!xc:most-positive-fixnum))
1474 ((and (= low (lognot high))
1475 (= high-count high-length)
1477 `(signed-byte ,(1+ high-length)))
1479 `(integer ,low ,high)))))
1480 (high `(,base ,(or low '*) ,high))
1482 (if (and (eq base 'integer) (= low 0))
1490 (aver (neq base+bounds 'real))
1491 `(complex ,base+bounds))
1493 (aver (eq base+bounds 'real))
1496 ;;; Return true if X is "less than or equal" to Y, taking open bounds
1497 ;;; into consideration. CLOSED is the predicate used to test the bound
1498 ;;; on a closed interval (e.g. <=), and OPEN is the predicate used on
1499 ;;; open bounds (e.g. <). Y is considered to be the outside bound, in
1500 ;;; the sense that if it is infinite (NIL), then the test succeeds,
1501 ;;; whereas if X is infinite, then the test fails (unless Y is also
1504 ;;; This is for comparing bounds of the same kind, e.g. upper and
1505 ;;; upper. Use NUMERIC-BOUND-TEST* for different kinds of bounds.
1506 (defmacro numeric-bound-test (x y closed open)
1511 (,closed (car ,x) (car ,y))
1512 (,closed (car ,x) ,y)))
1518 ;;; This is used to compare upper and lower bounds. This is different
1519 ;;; from the same-bound case:
1520 ;;; -- Since X = NIL is -infinity, whereas y = NIL is +infinity, we
1521 ;;; return true if *either* arg is NIL.
1522 ;;; -- an open inner bound is "greater" and also squeezes the interval,
1523 ;;; causing us to use the OPEN test for those cases as well.
1524 (defmacro numeric-bound-test* (x y closed open)
1529 (,open (car ,x) (car ,y))
1530 (,open (car ,x) ,y)))
1536 ;;; Return whichever of the numeric bounds X and Y is "maximal"
1537 ;;; according to the predicates CLOSED (e.g. >=) and OPEN (e.g. >).
1538 ;;; This is only meaningful for maximizing like bounds, i.e. upper and
1539 ;;; upper. If MAX-P is true, then we return NIL if X or Y is NIL,
1540 ;;; otherwise we return the other arg.
1541 (defmacro numeric-bound-max (x y closed open max-p)
1544 `(cond ((not ,n-x) ,(if max-p nil n-y))
1545 ((not ,n-y) ,(if max-p nil n-x))
1548 (if (,closed (car ,n-x) (car ,n-y)) ,n-x ,n-y)
1549 (if (,open (car ,n-x) ,n-y) ,n-x ,n-y)))
1552 (if (,open (car ,n-y) ,n-x) ,n-y ,n-x)
1553 (if (,closed ,n-y ,n-x) ,n-y ,n-x))))))
1555 (!define-type-method (number :simple-subtypep) (type1 type2)
1556 (let ((class1 (numeric-type-class type1))
1557 (class2 (numeric-type-class type2))
1558 (complexp2 (numeric-type-complexp type2))
1559 (format2 (numeric-type-format type2))
1560 (low1 (numeric-type-low type1))
1561 (high1 (numeric-type-high type1))
1562 (low2 (numeric-type-low type2))
1563 (high2 (numeric-type-high type2)))
1564 ;; If one is complex and the other isn't, they are disjoint.
1565 (cond ((not (or (eq (numeric-type-complexp type1) complexp2)
1568 ;; If the classes are specified and different, the types are
1569 ;; disjoint unless type2 is RATIONAL and type1 is INTEGER.
1570 ;; [ or type1 is INTEGER and type2 is of the form (RATIONAL
1571 ;; X X) for integral X, but this is dealt with in the
1572 ;; canonicalization inside MAKE-NUMERIC-TYPE ]
1573 ((not (or (eq class1 class2)
1575 (and (eq class1 'integer) (eq class2 'rational))))
1577 ;; If the float formats are specified and different, the types
1579 ((not (or (eq (numeric-type-format type1) format2)
1582 ;; Check the bounds.
1583 ((and (numeric-bound-test low1 low2 >= >)
1584 (numeric-bound-test high1 high2 <= <))
1589 (!define-superclasses number ((number)) !cold-init-forms)
1591 ;;; If the high bound of LOW is adjacent to the low bound of HIGH,
1592 ;;; then return true, otherwise NIL.
1593 (defun numeric-types-adjacent (low high)
1594 (let ((low-bound (numeric-type-high low))
1595 (high-bound (numeric-type-low high)))
1596 (cond ((not (and low-bound high-bound)) nil)
1597 ((and (consp low-bound) (consp high-bound)) nil)
1599 (let ((low-value (car low-bound)))
1600 (or (eql low-value high-bound)
1602 (load-time-value (make-unportable-float
1603 :single-float-negative-zero)))
1604 (eql high-bound 0f0))
1605 (and (eql low-value 0f0)
1607 (load-time-value (make-unportable-float
1608 :single-float-negative-zero))))
1610 (load-time-value (make-unportable-float
1611 :double-float-negative-zero)))
1612 (eql high-bound 0d0))
1613 (and (eql low-value 0d0)
1615 (load-time-value (make-unportable-float
1616 :double-float-negative-zero)))))))
1618 (let ((high-value (car high-bound)))
1619 (or (eql high-value low-bound)
1620 (and (eql high-value
1621 (load-time-value (make-unportable-float
1622 :single-float-negative-zero)))
1623 (eql low-bound 0f0))
1624 (and (eql high-value 0f0)
1626 (load-time-value (make-unportable-float
1627 :single-float-negative-zero))))
1628 (and (eql high-value
1629 (load-time-value (make-unportable-float
1630 :double-float-negative-zero)))
1631 (eql low-bound 0d0))
1632 (and (eql high-value 0d0)
1634 (load-time-value (make-unportable-float
1635 :double-float-negative-zero)))))))
1636 ((and (eq (numeric-type-class low) 'integer)
1637 (eq (numeric-type-class high) 'integer))
1638 (eql (1+ low-bound) high-bound))
1642 ;;; Return a numeric type that is a supertype for both TYPE1 and TYPE2.
1644 ;;; Old comment, probably no longer applicable:
1646 ;;; ### Note: we give up early to keep from dropping lots of
1647 ;;; information on the floor by returning overly general types.
1648 (!define-type-method (number :simple-union2) (type1 type2)
1649 (declare (type numeric-type type1 type2))
1650 (cond ((csubtypep type1 type2) type2)
1651 ((csubtypep type2 type1) type1)
1653 (let ((class1 (numeric-type-class type1))
1654 (format1 (numeric-type-format type1))
1655 (complexp1 (numeric-type-complexp type1))
1656 (class2 (numeric-type-class type2))
1657 (format2 (numeric-type-format type2))
1658 (complexp2 (numeric-type-complexp type2)))
1660 ((and (eq class1 class2)
1661 (eq format1 format2)
1662 (eq complexp1 complexp2)
1663 (or (numeric-types-intersect type1 type2)
1664 (numeric-types-adjacent type1 type2)
1665 (numeric-types-adjacent type2 type1)))
1670 :low (numeric-bound-max (numeric-type-low type1)
1671 (numeric-type-low type2)
1673 :high (numeric-bound-max (numeric-type-high type1)
1674 (numeric-type-high type2)
1676 ;; FIXME: These two clauses are almost identical, and the
1677 ;; consequents are in fact identical in every respect.
1678 ((and (eq class1 'rational)
1679 (eq class2 'integer)
1680 (eq format1 format2)
1681 (eq complexp1 complexp2)
1682 (integerp (numeric-type-low type2))
1683 (integerp (numeric-type-high type2))
1684 (= (numeric-type-low type2) (numeric-type-high type2))
1685 (or (numeric-types-adjacent type1 type2)
1686 (numeric-types-adjacent type2 type1)))
1691 :low (numeric-bound-max (numeric-type-low type1)
1692 (numeric-type-low type2)
1694 :high (numeric-bound-max (numeric-type-high type1)
1695 (numeric-type-high type2)
1697 ((and (eq class1 'integer)
1698 (eq class2 'rational)
1699 (eq format1 format2)
1700 (eq complexp1 complexp2)
1701 (integerp (numeric-type-low type1))
1702 (integerp (numeric-type-high type1))
1703 (= (numeric-type-low type1) (numeric-type-high type1))
1704 (or (numeric-types-adjacent type1 type2)
1705 (numeric-types-adjacent type2 type1)))
1710 :low (numeric-bound-max (numeric-type-low type1)
1711 (numeric-type-low type2)
1713 :high (numeric-bound-max (numeric-type-high type1)
1714 (numeric-type-high type2)
1720 (setf (info :type :kind 'number)
1721 #+sb-xc-host :defined #-sb-xc-host :primitive)
1722 (setf (info :type :builtin 'number)
1723 (make-numeric-type :complexp nil)))
1725 (!def-type-translator complex (&optional (typespec '*))
1726 (if (eq typespec '*)
1727 (specifier-type '(complex real))
1728 (labels ((not-numeric ()
1729 (error "The component type for COMPLEX is not numeric: ~S"
1732 (error "The component type for COMPLEX is not a subtype of REAL: ~S"
1734 (complex1 (component-type)
1735 (unless (numeric-type-p component-type)
1737 (when (eq (numeric-type-complexp component-type) :complex)
1739 (if (csubtypep component-type (specifier-type '(eql 0)))
1741 (modified-numeric-type component-type
1742 :complexp :complex)))
1745 ((eq ctype *empty-type*) *empty-type*)
1746 ((eq ctype *universal-type*) (not-real))
1747 ((typep ctype 'numeric-type) (complex1 ctype))
1748 ((typep ctype 'union-type)
1750 (mapcar #'do-complex (union-type-types ctype))))
1751 ((typep ctype 'member-type)
1753 (mapcar (lambda (x) (do-complex (ctype-of x)))
1754 (member-type-members ctype))))
1755 ((and (typep ctype 'intersection-type)
1756 ;; FIXME: This is very much a
1757 ;; not-quite-worst-effort, but we are required to do
1758 ;; something here because of our representation of
1759 ;; RATIO as (AND RATIONAL (NOT INTEGER)): we must
1760 ;; allow users to ask about (COMPLEX RATIO). This
1761 ;; will of course fail to work right on such types
1762 ;; as (AND INTEGER (SATISFIES ZEROP))...
1763 (let ((numbers (remove-if-not
1765 (intersection-type-types ctype))))
1767 (null (cdr numbers))
1768 (eq (numeric-type-complexp (car numbers)) :real)
1769 (complex1 (car numbers))))))
1771 (multiple-value-bind (subtypep certainly)
1772 (csubtypep ctype (specifier-type 'real))
1773 (if (and (not subtypep) certainly)
1775 ;; ANSI just says that TYPESPEC is any subtype of
1776 ;; type REAL, not necessarily a NUMERIC-TYPE. In
1777 ;; particular, at this point TYPESPEC could legally
1778 ;; be a hairy type like (AND NUMBER (SATISFIES
1779 ;; REALP) (SATISFIES ZEROP)), in which case we fall
1780 ;; through the logic above and end up here,
1782 (bug "~@<(known bug #145): The type ~S is too hairy to be ~
1783 used for a COMPLEX component.~:@>"
1785 (let ((ctype (specifier-type typespec)))
1786 (do-complex ctype)))))
1788 ;;; If X is *, return NIL, otherwise return the bound, which must be a
1789 ;;; member of TYPE or a one-element list of a member of TYPE.
1790 #!-sb-fluid (declaim (inline canonicalized-bound))
1791 (defun canonicalized-bound (bound type)
1792 (cond ((eq bound '*) nil)
1793 ((or (sb!xc:typep bound type)
1795 (sb!xc:typep (car bound) type)
1796 (null (cdr bound))))
1799 (error "Bound is not ~S, a ~S or a list of a ~S: ~S"
1805 (!def-type-translator integer (&optional (low '*) (high '*))
1806 (let* ((l (canonicalized-bound low 'integer))
1807 (lb (if (consp l) (1+ (car l)) l))
1808 (h (canonicalized-bound high 'integer))
1809 (hb (if (consp h) (1- (car h)) h)))
1810 (if (and hb lb (< hb lb))
1812 (make-numeric-type :class 'integer
1814 :enumerable (not (null (and l h)))
1818 (defmacro !def-bounded-type (type class format)
1819 `(!def-type-translator ,type (&optional (low '*) (high '*))
1820 (let ((lb (canonicalized-bound low ',type))
1821 (hb (canonicalized-bound high ',type)))
1822 (if (not (numeric-bound-test* lb hb <= <))
1824 (make-numeric-type :class ',class
1829 (!def-bounded-type rational rational nil)
1831 ;;; Unlike CMU CL, we represent the types FLOAT and REAL as
1832 ;;; UNION-TYPEs of more primitive types, in order to make
1833 ;;; type representation more unique, avoiding problems in the
1834 ;;; simplification of things like
1835 ;;; (subtypep '(or (single-float -1.0 1.0) (single-float 0.1))
1836 ;;; '(or (real -1 7) (single-float 0.1) (single-float -1.0 1.0)))
1837 ;;; When we allowed REAL to remain as a separate NUMERIC-TYPE,
1838 ;;; it was too easy for the first argument to be simplified to
1839 ;;; '(SINGLE-FLOAT -1.0), and for the second argument to be simplified
1840 ;;; to '(OR (REAL -1 7) (SINGLE-FLOAT 0.1)) and then for the
1841 ;;; SUBTYPEP to fail (returning NIL,T instead of T,T) because
1842 ;;; the first argument can't be seen to be a subtype of any of the
1843 ;;; terms in the second argument.
1845 ;;; The old CMU CL way was:
1846 ;;; (!def-bounded-type float float nil)
1847 ;;; (!def-bounded-type real nil nil)
1849 ;;; FIXME: If this new way works for a while with no weird new
1850 ;;; problems, we can go back and rip out support for separate FLOAT
1851 ;;; and REAL flavors of NUMERIC-TYPE. The new way was added in
1852 ;;; sbcl-0.6.11.22, 2001-03-21.
1854 ;;; FIXME: It's probably necessary to do something to fix the
1855 ;;; analogous problem with INTEGER and RATIONAL types. Perhaps
1856 ;;; bounded RATIONAL types should be represented as (OR RATIO INTEGER).
1857 (defun coerce-bound (bound type upperp inner-coerce-bound-fun)
1858 (declare (type function inner-coerce-bound-fun))
1861 (funcall inner-coerce-bound-fun bound type upperp)))
1862 (defun inner-coerce-real-bound (bound type upperp)
1863 #+sb-xc-host (declare (ignore upperp))
1864 (let #+sb-xc-host ()
1866 ((nl (load-time-value (symbol-value 'sb!xc:most-negative-long-float)))
1867 (pl (load-time-value (symbol-value 'sb!xc:most-positive-long-float))))
1868 (let ((nbound (if (consp bound) (car bound) bound))
1869 (consp (consp bound)))
1873 (list (rational nbound))
1877 ((floatp nbound) bound)
1879 ;; Coerce to the widest float format available, to avoid
1880 ;; unnecessary loss of precision, but don't coerce
1881 ;; unrepresentable numbers, except on the host where we
1882 ;; shouldn't be making these types (but KLUDGE: can't even
1883 ;; assert portably that we're not).
1887 (when (< nbound nl) (return-from inner-coerce-real-bound nl)))
1889 (when (> nbound pl) (return-from inner-coerce-real-bound pl))))
1890 (let ((result (coerce nbound 'long-float)))
1891 (if consp (list result) result)))))))))
1892 (defun inner-coerce-float-bound (bound type upperp)
1893 #+sb-xc-host (declare (ignore upperp))
1894 (let #+sb-xc-host ()
1896 ((nd (load-time-value (symbol-value 'sb!xc:most-negative-double-float)))
1897 (pd (load-time-value (symbol-value 'sb!xc:most-positive-double-float)))
1898 (ns (load-time-value (symbol-value 'sb!xc:most-negative-single-float)))
1899 (ps (load-time-value
1900 (symbol-value 'sb!xc:most-positive-single-float))))
1901 (let ((nbound (if (consp bound) (car bound) bound))
1902 (consp (consp bound)))
1906 ((typep nbound 'single-float) bound)
1911 (when (< nbound ns) (return-from inner-coerce-float-bound ns)))
1913 (when (> nbound ps) (return-from inner-coerce-float-bound ps))))
1914 (let ((result (coerce nbound 'single-float)))
1915 (if consp (list result) result)))))
1918 ((typep nbound 'double-float) bound)
1923 (when (< nbound nd) (return-from inner-coerce-float-bound nd)))
1925 (when (> nbound pd) (return-from inner-coerce-float-bound pd))))
1926 (let ((result (coerce nbound 'double-float)))
1927 (if consp (list result) result)))))))))
1928 (defun coerced-real-bound (bound type upperp)
1929 (coerce-bound bound type upperp #'inner-coerce-real-bound))
1930 (defun coerced-float-bound (bound type upperp)
1931 (coerce-bound bound type upperp #'inner-coerce-float-bound))
1932 (!def-type-translator real (&optional (low '*) (high '*))
1933 (specifier-type `(or (float ,(coerced-real-bound low 'float nil)
1934 ,(coerced-real-bound high 'float t))
1935 (rational ,(coerced-real-bound low 'rational nil)
1936 ,(coerced-real-bound high 'rational t)))))
1937 (!def-type-translator float (&optional (low '*) (high '*))
1939 `(or (single-float ,(coerced-float-bound low 'single-float nil)
1940 ,(coerced-float-bound high 'single-float t))
1941 (double-float ,(coerced-float-bound low 'double-float nil)
1942 ,(coerced-float-bound high 'double-float t))
1943 #!+long-float ,(error "stub: no long float support yet"))))
1945 (defmacro !define-float-format (f)
1946 `(!def-bounded-type ,f float ,f))
1948 (!define-float-format short-float)
1949 (!define-float-format single-float)
1950 (!define-float-format double-float)
1951 (!define-float-format long-float)
1953 (defun numeric-types-intersect (type1 type2)
1954 (declare (type numeric-type type1 type2))
1955 (let* ((class1 (numeric-type-class type1))
1956 (class2 (numeric-type-class type2))
1957 (complexp1 (numeric-type-complexp type1))
1958 (complexp2 (numeric-type-complexp type2))
1959 (format1 (numeric-type-format type1))
1960 (format2 (numeric-type-format type2))
1961 (low1 (numeric-type-low type1))
1962 (high1 (numeric-type-high type1))
1963 (low2 (numeric-type-low type2))
1964 (high2 (numeric-type-high type2)))
1965 ;; If one is complex and the other isn't, then they are disjoint.
1966 (cond ((not (or (eq complexp1 complexp2)
1967 (null complexp1) (null complexp2)))
1969 ;; If either type is a float, then the other must either be
1970 ;; specified to be a float or unspecified. Otherwise, they
1972 ((and (eq class1 'float)
1973 (not (member class2 '(float nil)))) nil)
1974 ((and (eq class2 'float)
1975 (not (member class1 '(float nil)))) nil)
1976 ;; If the float formats are specified and different, the
1977 ;; types are disjoint.
1978 ((not (or (eq format1 format2) (null format1) (null format2)))
1981 ;; Check the bounds. This is a bit odd because we must
1982 ;; always have the outer bound of the interval as the
1984 (if (numeric-bound-test high1 high2 <= <)
1985 (or (and (numeric-bound-test low1 low2 >= >)
1986 (numeric-bound-test* low1 high2 <= <))
1987 (and (numeric-bound-test low2 low1 >= >)
1988 (numeric-bound-test* low2 high1 <= <)))
1989 (or (and (numeric-bound-test* low2 high1 <= <)
1990 (numeric-bound-test low2 low1 >= >))
1991 (and (numeric-bound-test high2 high1 <= <)
1992 (numeric-bound-test* high2 low1 >= >))))))))
1994 ;;; Take the numeric bound X and convert it into something that can be
1995 ;;; used as a bound in a numeric type with the specified CLASS and
1996 ;;; FORMAT. If UP-P is true, then we round up as needed, otherwise we
1997 ;;; round down. UP-P true implies that X is a lower bound, i.e. (N) > N.
1999 ;;; This is used by NUMERIC-TYPE-INTERSECTION to mash the bound into
2000 ;;; the appropriate type number. X may only be a float when CLASS is
2003 ;;; ### Note: it is possible for the coercion to a float to overflow
2004 ;;; or underflow. This happens when the bound doesn't fit in the
2005 ;;; specified format. In this case, we should really return the
2006 ;;; appropriate {Most | Least}-{Positive | Negative}-XXX-Float float
2007 ;;; of desired format. But these conditions aren't currently signalled
2008 ;;; in any useful way.
2010 ;;; Also, when converting an open rational bound into a float we
2011 ;;; should probably convert it to a closed bound of the closest float
2012 ;;; in the specified format. KLUDGE: In general, open float bounds are
2013 ;;; screwed up. -- (comment from original CMU CL)
2014 (defun round-numeric-bound (x class format up-p)
2016 (let ((cx (if (consp x) (car x) x)))
2020 (if (and (consp x) (integerp cx))
2021 (if up-p (1+ cx) (1- cx))
2022 (if up-p (ceiling cx) (floor cx))))
2026 ((and format (subtypep format 'double-float))
2027 (if (<= most-negative-double-float cx most-positive-double-float)
2031 (if (<= most-negative-single-float cx most-positive-single-float)
2034 (if (consp x) (list res) res)))))
2037 ;;; Handle the case of type intersection on two numeric types. We use
2038 ;;; TYPES-EQUAL-OR-INTERSECT to throw out the case of types with no
2039 ;;; intersection. If an attribute in TYPE1 is unspecified, then we use
2040 ;;; TYPE2's attribute, which must be at least as restrictive. If the
2041 ;;; types intersect, then the only attributes that can be specified
2042 ;;; and different are the class and the bounds.
2044 ;;; When the class differs, we use the more restrictive class. The
2045 ;;; only interesting case is RATIONAL/INTEGER, since RATIONAL includes
2048 ;;; We make the result lower (upper) bound the maximum (minimum) of
2049 ;;; the argument lower (upper) bounds. We convert the bounds into the
2050 ;;; appropriate numeric type before maximizing. This avoids possible
2051 ;;; confusion due to mixed-type comparisons (but I think the result is
2053 (!define-type-method (number :simple-intersection2) (type1 type2)
2054 (declare (type numeric-type type1 type2))
2055 (if (numeric-types-intersect type1 type2)
2056 (let* ((class1 (numeric-type-class type1))
2057 (class2 (numeric-type-class type2))
2058 (class (ecase class1
2060 ((integer float) class1)
2061 (rational (if (eq class2 'integer)
2064 (format (or (numeric-type-format type1)
2065 (numeric-type-format type2))))
2069 :complexp (or (numeric-type-complexp type1)
2070 (numeric-type-complexp type2))
2071 :low (numeric-bound-max
2072 (round-numeric-bound (numeric-type-low type1)
2074 (round-numeric-bound (numeric-type-low type2)
2077 :high (numeric-bound-max
2078 (round-numeric-bound (numeric-type-high type1)
2080 (round-numeric-bound (numeric-type-high type2)
2085 ;;; Given two float formats, return the one with more precision. If
2086 ;;; either one is null, return NIL.
2087 (defun float-format-max (f1 f2)
2089 (dolist (f *float-formats* (error "bad float format: ~S" f1))
2090 (when (or (eq f f1) (eq f f2))
2093 ;;; Return the result of an operation on TYPE1 and TYPE2 according to
2094 ;;; the rules of numeric contagion. This is always NUMBER, some float
2095 ;;; format (possibly complex) or RATIONAL. Due to rational
2096 ;;; canonicalization, there isn't much we can do here with integers or
2097 ;;; rational complex numbers.
2099 ;;; If either argument is not a NUMERIC-TYPE, then return NUMBER. This
2100 ;;; is useful mainly for allowing types that are technically numbers,
2101 ;;; but not a NUMERIC-TYPE.
2102 (defun numeric-contagion (type1 type2)
2103 (if (and (numeric-type-p type1) (numeric-type-p type2))
2104 (let ((class1 (numeric-type-class type1))
2105 (class2 (numeric-type-class type2))
2106 (format1 (numeric-type-format type1))
2107 (format2 (numeric-type-format type2))
2108 (complexp1 (numeric-type-complexp type1))
2109 (complexp2 (numeric-type-complexp type2)))
2110 (cond ((or (null complexp1)
2112 (specifier-type 'number))
2116 :format (ecase class2
2117 (float (float-format-max format1 format2))
2118 ((integer rational) format1)
2120 ;; A double-float with any real number is a
2123 (if (eq format1 'double-float)
2126 ;; A long-float with any real number is a
2129 (if (eq format1 'long-float)
2132 :complexp (if (or (eq complexp1 :complex)
2133 (eq complexp2 :complex))
2136 ((eq class2 'float) (numeric-contagion type2 type1))
2137 ((and (eq complexp1 :real) (eq complexp2 :real))
2139 :class (and class1 class2 'rational)
2142 (specifier-type 'number))))
2143 (specifier-type 'number)))
2147 (!define-type-class array)
2149 ;;; What this does depends on the setting of the
2150 ;;; *USE-IMPLEMENTATION-TYPES* switch. If true, return the specialized
2151 ;;; element type, otherwise return the original element type.
2152 (defun specialized-element-type-maybe (type)
2153 (declare (type array-type type))
2154 (if *use-implementation-types*
2155 (array-type-specialized-element-type type)
2156 (array-type-element-type type)))
2158 (!define-type-method (array :simple-=) (type1 type2)
2159 (if (or (unknown-type-p (array-type-element-type type1))
2160 (unknown-type-p (array-type-element-type type2)))
2161 (multiple-value-bind (equalp certainp)
2162 (type= (array-type-element-type type1)
2163 (array-type-element-type type2))
2164 ;; By its nature, the call to TYPE= should never return NIL,
2165 ;; T, as we don't know what the UNKNOWN-TYPE will grow up to
2166 ;; be. -- CSR, 2002-08-19
2167 (aver (not (and (not equalp) certainp)))
2168 (values equalp certainp))
2169 (values (and (equal (array-type-dimensions type1)
2170 (array-type-dimensions type2))
2171 (eq (array-type-complexp type1)
2172 (array-type-complexp type2))
2173 (type= (specialized-element-type-maybe type1)
2174 (specialized-element-type-maybe type2)))
2177 (!define-type-method (array :negate) (type)
2178 ;; FIXME (and hint to PFD): we're vulnerable here to attacks of the
2179 ;; form "are (AND ARRAY (NOT (ARRAY T))) and (OR (ARRAY BIT) (ARRAY
2180 ;; NIL) (ARRAY CHAR) ...) equivalent?" -- CSR, 2003-12-10
2181 (make-negation-type :type type))
2183 (!define-type-method (array :unparse) (type)
2184 (let ((dims (array-type-dimensions type))
2185 (eltype (type-specifier (array-type-element-type type)))
2186 (complexp (array-type-complexp type)))
2189 (if complexp 'array 'simple-array)
2190 (if complexp `(array ,eltype) `(simple-array ,eltype))))
2191 ((= (length dims) 1)
2193 (if (eq (car dims) '*)
2196 ((base-char #!-sb-unicode character) 'base-string)
2198 (t `(vector ,eltype)))
2200 (bit `(bit-vector ,(car dims)))
2201 ((base-char #!-sb-unicode character)
2202 `(base-string ,(car dims)))
2203 (t `(vector ,eltype ,(car dims)))))
2204 (if (eq (car dims) '*)
2206 (bit 'simple-bit-vector)
2207 ((base-char #!-sb-unicode character) 'simple-base-string)
2208 ((t) 'simple-vector)
2209 (t `(simple-array ,eltype (*))))
2211 (bit `(simple-bit-vector ,(car dims)))
2212 ((base-char #!-sb-unicode character)
2213 `(simple-base-string ,(car dims)))
2214 ((t) `(simple-vector ,(car dims)))
2215 (t `(simple-array ,eltype ,dims))))))
2218 `(array ,eltype ,dims)
2219 `(simple-array ,eltype ,dims))))))
2221 (!define-type-method (array :simple-subtypep) (type1 type2)
2222 (let ((dims1 (array-type-dimensions type1))
2223 (dims2 (array-type-dimensions type2))
2224 (complexp2 (array-type-complexp type2)))
2225 (cond (;; not subtypep unless dimensions are compatible
2226 (not (or (eq dims2 '*)
2227 (and (not (eq dims1 '*))
2228 ;; (sbcl-0.6.4 has trouble figuring out that
2229 ;; DIMS1 and DIMS2 must be lists at this
2230 ;; point, and knowing that is important to
2231 ;; compiling EVERY efficiently.)
2232 (= (length (the list dims1))
2233 (length (the list dims2)))
2234 (every (lambda (x y)
2235 (or (eq y '*) (eql x y)))
2237 (the list dims2)))))
2239 ;; not subtypep unless complexness is compatible
2240 ((not (or (eq complexp2 :maybe)
2241 (eq (array-type-complexp type1) complexp2)))
2243 ;; Since we didn't fail any of the tests above, we win
2244 ;; if the TYPE2 element type is wild.
2245 ((eq (array-type-element-type type2) *wild-type*)
2247 (;; Since we didn't match any of the special cases above, we
2248 ;; can't give a good answer unless both the element types
2249 ;; have been defined.
2250 (or (unknown-type-p (array-type-element-type type1))
2251 (unknown-type-p (array-type-element-type type2)))
2253 (;; Otherwise, the subtype relationship holds iff the
2254 ;; types are equal, and they're equal iff the specialized
2255 ;; element types are identical.
2257 (values (type= (specialized-element-type-maybe type1)
2258 (specialized-element-type-maybe type2))
2261 ;;; FIXME: is this dead?
2262 (!define-superclasses array
2263 ((base-string base-string)
2268 (defun array-types-intersect (type1 type2)
2269 (declare (type array-type type1 type2))
2270 (let ((dims1 (array-type-dimensions type1))
2271 (dims2 (array-type-dimensions type2))
2272 (complexp1 (array-type-complexp type1))
2273 (complexp2 (array-type-complexp type2)))
2274 ;; See whether dimensions are compatible.
2275 (cond ((not (or (eq dims1 '*) (eq dims2 '*)
2276 (and (= (length dims1) (length dims2))
2277 (every (lambda (x y)
2278 (or (eq x '*) (eq y '*) (= x y)))
2281 ;; See whether complexpness is compatible.
2282 ((not (or (eq complexp1 :maybe)
2283 (eq complexp2 :maybe)
2284 (eq complexp1 complexp2)))
2288 ;; If either element type is wild, then they intersect.
2289 ;; Otherwise, the types must be identical.
2291 ;; FIXME: There seems to have been a fair amount of
2292 ;; confusion about the distinction between requested element
2293 ;; type and specialized element type; here is one of
2294 ;; them. If we request an array to hold objects of an
2295 ;; unknown type, we can do no better than represent that
2296 ;; type as an array specialized on wild-type. We keep the
2297 ;; requested element-type in the -ELEMENT-TYPE slot, and
2298 ;; *WILD-TYPE* in the -SPECIALIZED-ELEMENT-TYPE. So, here,
2299 ;; we must test for the SPECIALIZED slot being *WILD-TYPE*,
2300 ;; not just the ELEMENT-TYPE slot. Maybe the return value
2301 ;; in that specific case should be T, NIL? Or maybe this
2302 ;; function should really be called
2303 ;; ARRAY-TYPES-COULD-POSSIBLY-INTERSECT? In any case, this
2304 ;; was responsible for bug #123, and this whole issue could
2305 ;; do with a rethink and/or a rewrite. -- CSR, 2002-08-21
2306 ((or (eq (array-type-specialized-element-type type1) *wild-type*)
2307 (eq (array-type-specialized-element-type type2) *wild-type*)
2308 (type= (specialized-element-type-maybe type1)
2309 (specialized-element-type-maybe type2)))
2315 (!define-type-method (array :simple-intersection2) (type1 type2)
2316 (declare (type array-type type1 type2))
2317 (if (array-types-intersect type1 type2)
2318 (let ((dims1 (array-type-dimensions type1))
2319 (dims2 (array-type-dimensions type2))
2320 (complexp1 (array-type-complexp type1))
2321 (complexp2 (array-type-complexp type2))
2322 (eltype1 (array-type-element-type type1))
2323 (eltype2 (array-type-element-type type2)))
2324 (specialize-array-type
2326 :dimensions (cond ((eq dims1 '*) dims2)
2327 ((eq dims2 '*) dims1)
2329 (mapcar (lambda (x y) (if (eq x '*) y x))
2331 :complexp (if (eq complexp1 :maybe) complexp2 complexp1)
2333 ((eq eltype1 *wild-type*) eltype2)
2334 ((eq eltype2 *wild-type*) eltype1)
2335 (t (type-intersection eltype1 eltype2))))))
2338 ;;; Check a supplied dimension list to determine whether it is legal,
2339 ;;; and return it in canonical form (as either '* or a list).
2340 (defun canonical-array-dimensions (dims)
2345 (error "Arrays can't have a negative number of dimensions: ~S" dims))
2346 (when (>= dims sb!xc:array-rank-limit)
2347 (error "array type with too many dimensions: ~S" dims))
2348 (make-list dims :initial-element '*))
2350 (when (>= (length dims) sb!xc:array-rank-limit)
2351 (error "array type with too many dimensions: ~S" dims))
2354 (unless (and (integerp dim)
2356 (< dim sb!xc:array-dimension-limit))
2357 (error "bad dimension in array type: ~S" dim))))
2360 (error "Array dimensions is not a list, integer or *:~% ~S" dims))))
2364 (!define-type-class member)
2366 (!define-type-method (member :negate) (type)
2367 (let ((members (member-type-members type)))
2368 (if (some #'floatp members)
2370 (dolist (pair `((0.0f0 . ,(load-time-value (make-unportable-float :single-float-negative-zero)))
2371 (0.0d0 . ,(load-time-value (make-unportable-float :double-float-negative-zero)))
2373 (0.0l0 . ,(load-time-value (make-unportable-float :long-float-negative-zero)))))
2374 (when (member (car pair) members)
2375 (aver (not (member (cdr pair) members)))
2376 (push (cdr pair) floats)
2377 (setf members (remove (car pair) members)))
2378 (when (member (cdr pair) members)
2379 (aver (not (member (car pair) members)))
2380 (push (car pair) floats)
2381 (setf members (remove (cdr pair) members))))
2382 (apply #'type-intersection
2386 :type (make-member-type :members members)))
2389 (let ((type (ctype-of x)))
2392 :type (modified-numeric-type type
2393 :low nil :high nil))
2394 (modified-numeric-type type
2395 :low nil :high (list x))
2396 (make-member-type :members (list x))
2397 (modified-numeric-type type
2398 :low (list x) :high nil))))
2400 (make-negation-type :type type))))
2402 (!define-type-method (member :unparse) (type)
2403 (let ((members (member-type-members type)))
2405 ((equal members '(nil)) 'null)
2406 ((type= type (specifier-type 'standard-char)) 'standard-char)
2407 (t `(member ,@members)))))
2409 (!define-type-method (member :simple-subtypep) (type1 type2)
2410 (values (subsetp (member-type-members type1) (member-type-members type2))
2413 (!define-type-method (member :complex-subtypep-arg1) (type1 type2)
2414 (every/type (swapped-args-fun #'ctypep)
2416 (member-type-members type1)))
2418 ;;; We punt if the odd type is enumerable and intersects with the
2419 ;;; MEMBER type. If not enumerable, then it is definitely not a
2420 ;;; subtype of the MEMBER type.
2421 (!define-type-method (member :complex-subtypep-arg2) (type1 type2)
2422 (cond ((not (type-enumerable type1)) (values nil t))
2423 ((types-equal-or-intersect type1 type2)
2424 (invoke-complex-subtypep-arg1-method type1 type2))
2425 (t (values nil t))))
2427 (!define-type-method (member :simple-intersection2) (type1 type2)
2428 (let ((mem1 (member-type-members type1))
2429 (mem2 (member-type-members type2)))
2430 (cond ((subsetp mem1 mem2) type1)
2431 ((subsetp mem2 mem1) type2)
2433 (let ((res (intersection mem1 mem2)))
2435 (make-member-type :members res)
2438 (!define-type-method (member :complex-intersection2) (type1 type2)
2440 (collect ((members))
2441 (let ((mem2 (member-type-members type2)))
2442 (dolist (member mem2)
2443 (multiple-value-bind (val win) (ctypep member type1)
2445 (return-from punt nil))
2446 (when val (members member))))
2447 (cond ((subsetp mem2 (members)) type2)
2448 ((null (members)) *empty-type*)
2450 (make-member-type :members (members))))))))
2452 ;;; We don't need a :COMPLEX-UNION2, since the only interesting case is
2453 ;;; a union type, and the member/union interaction is handled by the
2454 ;;; union type method.
2455 (!define-type-method (member :simple-union2) (type1 type2)
2456 (let ((mem1 (member-type-members type1))
2457 (mem2 (member-type-members type2)))
2458 (cond ((subsetp mem1 mem2) type2)
2459 ((subsetp mem2 mem1) type1)
2461 (make-member-type :members (union mem1 mem2))))))
2463 (!define-type-method (member :simple-=) (type1 type2)
2464 (let ((mem1 (member-type-members type1))
2465 (mem2 (member-type-members type2)))
2466 (values (and (subsetp mem1 mem2)
2467 (subsetp mem2 mem1))
2470 (!define-type-method (member :complex-=) (type1 type2)
2471 (if (type-enumerable type1)
2472 (multiple-value-bind (val win) (csubtypep type2 type1)
2473 (if (or val (not win))
2478 (!def-type-translator member (&rest members)
2480 (let (ms numbers char-codes)
2481 (dolist (m (remove-duplicates members))
2483 (float (if (zerop m)
2485 (push (ctype-of m) numbers)))
2486 (real (push (ctype-of m) numbers))
2487 (character (push (sb!xc:char-code m) char-codes))
2491 (make-member-type :members ms)
2494 (make-character-set-type
2495 :pairs (mapcar (lambda (x) (cons x x))
2496 (sort char-codes #'<)))
2498 (nreverse numbers)))
2501 ;;;; intersection types
2503 ;;;; Until version 0.6.10.6, SBCL followed the original CMU CL approach
2504 ;;;; of punting on all AND types, not just the unreasonably complicated
2505 ;;;; ones. The change was motivated by trying to get the KEYWORD type
2506 ;;;; to behave sensibly:
2507 ;;;; ;; reasonable definition
2508 ;;;; (DEFTYPE KEYWORD () '(AND SYMBOL (SATISFIES KEYWORDP)))
2509 ;;;; ;; reasonable behavior
2510 ;;;; (AVER (SUBTYPEP 'KEYWORD 'SYMBOL))
2511 ;;;; Without understanding a little about the semantics of AND, we'd
2512 ;;;; get (SUBTYPEP 'KEYWORD 'SYMBOL)=>NIL,NIL and, for entirely
2513 ;;;; parallel reasons, (SUBTYPEP 'RATIO 'NUMBER)=>NIL,NIL. That's
2516 ;;;; We still follow the example of CMU CL to some extent, by punting
2517 ;;;; (to the opaque HAIRY-TYPE) on sufficiently complicated types
2520 (!define-type-class intersection)
2522 (!define-type-method (intersection :negate) (type)
2524 (mapcar #'type-negation (intersection-type-types type))))
2526 ;;; A few intersection types have special names. The others just get
2527 ;;; mechanically unparsed.
2528 (!define-type-method (intersection :unparse) (type)
2529 (declare (type ctype type))
2530 (or (find type '(ratio keyword) :key #'specifier-type :test #'type=)
2531 `(and ,@(mapcar #'type-specifier (intersection-type-types type)))))
2533 ;;; shared machinery for type equality: true if every type in the set
2534 ;;; TYPES1 matches a type in the set TYPES2 and vice versa
2535 (defun type=-set (types1 types2)
2536 (flet ((type<=-set (x y)
2537 (declare (type list x y))
2538 (every/type (lambda (x y-element)
2539 (any/type #'type= y-element x))
2541 (and/type (type<=-set types1 types2)
2542 (type<=-set types2 types1))))
2544 ;;; Two intersection types are equal if their subtypes are equal sets.
2546 ;;; FIXME: Might it be better to use
2547 ;;; (AND (SUBTYPEP X Y) (SUBTYPEP Y X))
2548 ;;; instead, since SUBTYPEP is the usual relationship that we care
2549 ;;; most about, so it would be good to leverage any ingenuity there
2550 ;;; in this more obscure method?
2551 (!define-type-method (intersection :simple-=) (type1 type2)
2552 (type=-set (intersection-type-types type1)
2553 (intersection-type-types type2)))
2555 (defun %intersection-complex-subtypep-arg1 (type1 type2)
2556 (type= type1 (type-intersection type1 type2)))
2558 (defun %intersection-simple-subtypep (type1 type2)
2559 (every/type #'%intersection-complex-subtypep-arg1
2561 (intersection-type-types type2)))
2563 (!define-type-method (intersection :simple-subtypep) (type1 type2)
2564 (%intersection-simple-subtypep type1 type2))
2566 (!define-type-method (intersection :complex-subtypep-arg1) (type1 type2)
2567 (%intersection-complex-subtypep-arg1 type1 type2))
2569 (defun %intersection-complex-subtypep-arg2 (type1 type2)
2570 (every/type #'csubtypep type1 (intersection-type-types type2)))
2572 (!define-type-method (intersection :complex-subtypep-arg2) (type1 type2)
2573 (%intersection-complex-subtypep-arg2 type1 type2))
2575 ;;; FIXME: This will look eeriely familiar to readers of the UNION
2576 ;;; :SIMPLE-INTERSECTION2 :COMPLEX-INTERSECTION2 method. That's
2577 ;;; because it was generated by cut'n'paste methods. Given that
2578 ;;; intersections and unions have all sorts of symmetries known to
2579 ;;; mathematics, it shouldn't be beyond the ken of some programmers to
2580 ;;; reflect those symmetries in code in a way that ties them together
2581 ;;; more strongly than having two independent near-copies :-/
2582 (!define-type-method (intersection :simple-union2 :complex-union2)
2584 ;; Within this method, type2 is guaranteed to be an intersection
2586 (aver (intersection-type-p type2))
2587 ;; Make sure to call only the applicable methods...
2588 (cond ((and (intersection-type-p type1)
2589 (%intersection-simple-subtypep type1 type2)) type2)
2590 ((and (intersection-type-p type1)
2591 (%intersection-simple-subtypep type2 type1)) type1)
2592 ((and (not (intersection-type-p type1))
2593 (%intersection-complex-subtypep-arg2 type1 type2))
2595 ((and (not (intersection-type-p type1))
2596 (%intersection-complex-subtypep-arg1 type2 type1))
2598 ;; KLUDGE: This special (and somewhat hairy) magic is required
2599 ;; to deal with the RATIONAL/INTEGER special case. The UNION
2600 ;; of (INTEGER * -1) and (AND (RATIONAL * -1/2) (NOT INTEGER))
2601 ;; should be (RATIONAL * -1/2) -- CSR, 2003-02-28
2602 ((and (csubtypep type2 (specifier-type 'ratio))
2603 (numeric-type-p type1)
2604 (csubtypep type1 (specifier-type 'integer))
2609 :low (if (null (numeric-type-low type1))
2611 (list (1- (numeric-type-low type1))))
2612 :high (if (null (numeric-type-high type1))
2614 (list (1+ (numeric-type-high type1)))))))
2616 (apply #'type-intersection
2617 (remove (specifier-type '(not integer))
2618 (intersection-type-types type2)
2621 (let ((accumulator *universal-type*))
2622 (do ((t2s (intersection-type-types type2) (cdr t2s)))
2623 ((null t2s) accumulator)
2624 (let ((union (type-union type1 (car t2s))))
2625 (when (union-type-p union)
2626 ;; we have to give up here -- there are all sorts of
2627 ;; ordering worries, but it's better than before.
2628 ;; Doing exactly the same as in the UNION
2629 ;; :SIMPLE/:COMPLEX-INTERSECTION2 method causes stack
2630 ;; overflow with the mutual recursion never bottoming
2632 (if (and (eq accumulator *universal-type*)
2634 ;; KLUDGE: if we get here, we have a partially
2635 ;; simplified result. While this isn't by any
2636 ;; means a universal simplification, including
2637 ;; this logic here means that we can get (OR
2638 ;; KEYWORD (NOT KEYWORD)) canonicalized to T.
2642 (type-intersection accumulator union))))))))
2644 (!def-type-translator and (&whole whole &rest type-specifiers)
2645 (apply #'type-intersection
2646 (mapcar #'specifier-type type-specifiers)))
2650 (!define-type-class union)
2652 (!define-type-method (union :negate) (type)
2653 (declare (type ctype type))
2654 (apply #'type-intersection
2655 (mapcar #'type-negation (union-type-types type))))
2657 ;;; The LIST, FLOAT and REAL types have special names. Other union
2658 ;;; types just get mechanically unparsed.
2659 (!define-type-method (union :unparse) (type)
2660 (declare (type ctype type))
2662 ((type= type (specifier-type 'list)) 'list)
2663 ((type= type (specifier-type 'float)) 'float)
2664 ((type= type (specifier-type 'real)) 'real)
2665 ((type= type (specifier-type 'sequence)) 'sequence)
2666 ((type= type (specifier-type 'bignum)) 'bignum)
2667 ((type= type (specifier-type 'simple-string)) 'simple-string)
2668 ((type= type (specifier-type 'string)) 'string)
2669 ((type= type (specifier-type 'complex)) 'complex)
2670 ((type= type (specifier-type 'standard-char)) 'standard-char)
2671 (t `(or ,@(mapcar #'type-specifier (union-type-types type))))))
2673 ;;; Two union types are equal if they are each subtypes of each
2674 ;;; other. We need to be this clever because our complex subtypep
2675 ;;; methods are now more accurate; we don't get infinite recursion
2676 ;;; because the simple-subtypep method delegates to complex-subtypep
2677 ;;; of the individual types of type1. - CSR, 2002-04-09
2679 ;;; Previous comment, now obsolete, but worth keeping around because
2680 ;;; it is true, though too strong a condition:
2682 ;;; Two union types are equal if their subtypes are equal sets.
2683 (!define-type-method (union :simple-=) (type1 type2)
2684 (multiple-value-bind (subtype certain?)
2685 (csubtypep type1 type2)
2687 (csubtypep type2 type1)
2688 ;; we might as well become as certain as possible.
2691 (multiple-value-bind (subtype certain?)
2692 (csubtypep type2 type1)
2693 (declare (ignore subtype))
2694 (values nil certain?))))))
2696 (!define-type-method (union :complex-=) (type1 type2)
2697 (declare (ignore type1))
2698 (if (some #'type-might-contain-other-types-p
2699 (union-type-types type2))
2703 ;;; Similarly, a union type is a subtype of another if and only if
2704 ;;; every element of TYPE1 is a subtype of TYPE2.
2705 (defun union-simple-subtypep (type1 type2)
2706 (every/type (swapped-args-fun #'union-complex-subtypep-arg2)
2708 (union-type-types type1)))
2710 (!define-type-method (union :simple-subtypep) (type1 type2)
2711 (union-simple-subtypep type1 type2))
2713 (defun union-complex-subtypep-arg1 (type1 type2)
2714 (every/type (swapped-args-fun #'csubtypep)
2716 (union-type-types type1)))
2718 (!define-type-method (union :complex-subtypep-arg1) (type1 type2)
2719 (union-complex-subtypep-arg1 type1 type2))
2721 (defun union-complex-subtypep-arg2 (type1 type2)
2722 (multiple-value-bind (sub-value sub-certain?)
2723 ;; was: (any/type #'csubtypep type1 (union-type-types type2)),
2724 ;; which turns out to be too restrictive, causing bug 91.
2726 ;; the following reimplementation might look dodgy. It is
2727 ;; dodgy. It depends on the union :complex-= method not doing
2728 ;; very much work -- certainly, not using subtypep. Reasoning:
2730 ;; At this stage, we know that type2 is a union type and type1
2731 ;; isn't. We might as well check this, though:
2732 (aver (union-type-p type2))
2733 (aver (not (union-type-p type1)))
2734 ;; A is a subset of (B1 u B2)
2735 ;; <=> A n (B1 u B2) = A
2736 ;; <=> (A n B1) u (A n B2) = A
2738 ;; But, we have to be careful not to delegate this type= to
2739 ;; something that could invoke subtypep, which might get us
2740 ;; back here -> stack explosion. We therefore ensure that the
2741 ;; second type (which is the one that's dispatched on) is
2742 ;; either a union type (where we've ensured that the complex-=
2743 ;; method will not call subtypep) or something with no union
2744 ;; types involved, in which case we'll never come back here.
2746 ;; If we don't do this, then e.g.
2747 ;; (SUBTYPEP '(MEMBER 3) '(OR (SATISFIES FOO) (SATISFIES BAR)))
2748 ;; would loop infinitely, as the member :complex-= method is
2749 ;; implemented in terms of subtypep.
2751 ;; Ouch. - CSR, 2002-04-10
2754 (mapcar (lambda (x) (type-intersection type1 x))
2755 (union-type-types type2)))))
2757 (values sub-value sub-certain?)
2758 ;; The ANY/TYPE expression above is a sufficient condition for
2759 ;; subsetness, but not a necessary one, so we might get a more
2760 ;; certain answer by this CALL-NEXT-METHOD-ish step when the
2761 ;; ANY/TYPE expression is uncertain.
2762 (invoke-complex-subtypep-arg1-method type1 type2))))
2764 (!define-type-method (union :complex-subtypep-arg2) (type1 type2)
2765 (union-complex-subtypep-arg2 type1 type2))
2767 (!define-type-method (union :simple-intersection2 :complex-intersection2)
2769 ;; The CSUBTYPEP clauses here let us simplify e.g.
2770 ;; (TYPE-INTERSECTION2 (SPECIFIER-TYPE 'LIST)
2771 ;; (SPECIFIER-TYPE '(OR LIST VECTOR)))
2772 ;; (where LIST is (OR CONS NULL)).
2774 ;; The tests are more or less (CSUBTYPEP TYPE1 TYPE2) and vice
2775 ;; versa, but it's important that we pre-expand them into
2776 ;; specialized operations on individual elements of
2777 ;; UNION-TYPE-TYPES, instead of using the ordinary call to
2778 ;; CSUBTYPEP, in order to avoid possibly invoking any methods which
2779 ;; might in turn invoke (TYPE-INTERSECTION2 TYPE1 TYPE2) and thus
2780 ;; cause infinite recursion.
2782 ;; Within this method, type2 is guaranteed to be a union type:
2783 (aver (union-type-p type2))
2784 ;; Make sure to call only the applicable methods...
2785 (cond ((and (union-type-p type1)
2786 (union-simple-subtypep type1 type2)) type1)
2787 ((and (union-type-p type1)
2788 (union-simple-subtypep type2 type1)) type2)
2789 ((and (not (union-type-p type1))
2790 (union-complex-subtypep-arg2 type1 type2))
2792 ((and (not (union-type-p type1))
2793 (union-complex-subtypep-arg1 type2 type1))
2796 ;; KLUDGE: This code accumulates a sequence of TYPE-UNION2
2797 ;; operations in a particular order, and gives up if any of
2798 ;; the sub-unions turn out not to be simple. In other cases
2799 ;; ca. sbcl-0.6.11.15, that approach to taking a union was a
2800 ;; bad idea, since it can overlook simplifications which
2801 ;; might occur if the terms were accumulated in a different
2802 ;; order. It's possible that that will be a problem here too.
2803 ;; However, I can't think of a good example to demonstrate
2804 ;; it, and without an example to demonstrate it I can't write
2805 ;; test cases, and without test cases I don't want to
2806 ;; complicate the code to address what's still a hypothetical
2807 ;; problem. So I punted. -- WHN 2001-03-20
2808 (let ((accumulator *empty-type*))
2809 (dolist (t2 (union-type-types type2) accumulator)
2811 (type-union accumulator
2812 (type-intersection type1 t2))))))))
2814 (!def-type-translator or (&rest type-specifiers)
2816 (mapcar #'specifier-type
2821 (!define-type-class cons)
2823 (!def-type-translator cons (&optional (car-type-spec '*) (cdr-type-spec '*))
2824 (let ((car-type (single-value-specifier-type car-type-spec))
2825 (cdr-type (single-value-specifier-type cdr-type-spec)))
2826 (make-cons-type car-type cdr-type)))
2828 (!define-type-method (cons :negate) (type)
2829 (if (and (eq (cons-type-car-type type) *universal-type*)
2830 (eq (cons-type-cdr-type type) *universal-type*))
2831 (make-negation-type :type type)
2833 (make-negation-type :type (specifier-type 'cons))
2835 ((and (not (eq (cons-type-car-type type) *universal-type*))
2836 (not (eq (cons-type-cdr-type type) *universal-type*)))
2839 (type-negation (cons-type-car-type type))
2843 (type-negation (cons-type-cdr-type type)))))
2844 ((not (eq (cons-type-car-type type) *universal-type*))
2846 (type-negation (cons-type-car-type type))
2848 ((not (eq (cons-type-cdr-type type) *universal-type*))
2851 (type-negation (cons-type-cdr-type type))))
2852 (t (bug "Weird CONS type ~S" type))))))
2854 (!define-type-method (cons :unparse) (type)
2855 (let ((car-eltype (type-specifier (cons-type-car-type type)))
2856 (cdr-eltype (type-specifier (cons-type-cdr-type type))))
2857 (if (and (member car-eltype '(t *))
2858 (member cdr-eltype '(t *)))
2860 `(cons ,car-eltype ,cdr-eltype))))
2862 (!define-type-method (cons :simple-=) (type1 type2)
2863 (declare (type cons-type type1 type2))
2864 (and (type= (cons-type-car-type type1) (cons-type-car-type type2))
2865 (type= (cons-type-cdr-type type1) (cons-type-cdr-type type2))))
2867 (!define-type-method (cons :simple-subtypep) (type1 type2)
2868 (declare (type cons-type type1 type2))
2869 (multiple-value-bind (val-car win-car)
2870 (csubtypep (cons-type-car-type type1) (cons-type-car-type type2))
2871 (multiple-value-bind (val-cdr win-cdr)
2872 (csubtypep (cons-type-cdr-type type1) (cons-type-cdr-type type2))
2873 (if (and val-car val-cdr)
2874 (values t (and win-car win-cdr))
2875 (values nil (or (and (not val-car) win-car)
2876 (and (not val-cdr) win-cdr)))))))
2878 ;;; Give up if a precise type is not possible, to avoid returning
2879 ;;; overly general types.
2880 (!define-type-method (cons :simple-union2) (type1 type2)
2881 (declare (type cons-type type1 type2))
2882 (let ((car-type1 (cons-type-car-type type1))
2883 (car-type2 (cons-type-car-type type2))
2884 (cdr-type1 (cons-type-cdr-type type1))
2885 (cdr-type2 (cons-type-cdr-type type2))
2888 ;; UGH. -- CSR, 2003-02-24
2889 (macrolet ((frob-car (car1 car2 cdr1 cdr2
2890 &optional (not1 nil not1p))
2892 (make-cons-type ,car1 (type-union ,cdr1 ,cdr2))
2894 (type-intersection ,car2
2897 `(type-negation ,car1)))
2899 (cond ((type= car-type1 car-type2)
2900 (make-cons-type car-type1
2901 (type-union cdr-type1 cdr-type2)))
2902 ((type= cdr-type1 cdr-type2)
2903 (make-cons-type (type-union car-type1 car-type2)
2905 ((csubtypep car-type1 car-type2)
2906 (frob-car car-type1 car-type2 cdr-type1 cdr-type2))
2907 ((csubtypep car-type2 car-type1)
2908 (frob-car car-type2 car-type1 cdr-type2 cdr-type1))
2909 ;; more general case of the above, but harder to compute
2911 (setf car-not1 (type-negation car-type1))
2912 (not (csubtypep car-type2 car-not1)))
2913 (frob-car car-type1 car-type2 cdr-type1 cdr-type2 car-not1))
2915 (setf car-not2 (type-negation car-type2))
2916 (not (csubtypep car-type1 car-not2)))
2917 (frob-car car-type2 car-type1 cdr-type2 cdr-type1 car-not2))
2918 ;; Don't put these in -- consider the effect of taking the
2919 ;; union of (CONS (INTEGER 0 2) (INTEGER 5 7)) and
2920 ;; (CONS (INTEGER 0 3) (INTEGER 5 6)).
2922 ((csubtypep cdr-type1 cdr-type2)
2923 (frob-cdr car-type1 car-type2 cdr-type1 cdr-type2))
2925 ((csubtypep cdr-type2 cdr-type1)
2926 (frob-cdr car-type2 car-type1 cdr-type2 cdr-type1))))))
2928 (!define-type-method (cons :simple-intersection2) (type1 type2)
2929 (declare (type cons-type type1 type2))
2930 (let ((car-int2 (type-intersection2 (cons-type-car-type type1)
2931 (cons-type-car-type type2)))
2932 (cdr-int2 (type-intersection2 (cons-type-cdr-type type1)
2933 (cons-type-cdr-type type2))))
2935 ((and car-int2 cdr-int2) (make-cons-type car-int2 cdr-int2))
2936 (car-int2 (make-cons-type car-int2
2938 (cons-type-cdr-type type1)
2939 (cons-type-cdr-type type2))))
2940 (cdr-int2 (make-cons-type
2941 (type-intersection (cons-type-car-type type1)
2942 (cons-type-car-type type2))
2945 ;;;; CHARACTER-SET types
2947 (!define-type-class character-set)
2949 (!def-type-translator character-set
2950 (&optional (pairs '((0 . #.(1- sb!xc:char-code-limit)))))
2951 (make-character-set-type :pairs pairs))
2953 (!define-type-method (character-set :negate) (type)
2954 (let ((pairs (character-set-type-pairs type)))
2955 (if (and (= (length pairs) 1)
2957 (= (cdar pairs) (1- sb!xc:char-code-limit)))
2958 (make-negation-type :type type)
2959 (let ((not-character
2961 :type (make-character-set-type
2962 :pairs '((0 . #.(1- sb!xc:char-code-limit)))))))
2965 (make-character-set-type
2966 :pairs (let (not-pairs)
2967 (when (> (caar pairs) 0)
2968 (push (cons 0 (1- (caar pairs))) not-pairs))
2969 (do* ((tail pairs (cdr tail))
2971 (low2 (caadr tail)))
2973 (when (< (cdar tail) (1- sb!xc:char-code-limit))
2974 (push (cons (1+ (cdar tail))
2975 (1- sb!xc:char-code-limit))
2977 (nreverse not-pairs))
2978 (push (cons (1+ high1) (1- low2)) not-pairs)))))))))
2980 (!define-type-method (character-set :unparse) (type)
2982 ((type= type (specifier-type 'character)) 'character)
2983 ((type= type (specifier-type 'base-char)) 'base-char)
2984 ((type= type (specifier-type 'extended-char)) 'extended-char)
2985 ((type= type (specifier-type 'standard-char)) 'standard-char)
2986 (t (let ((pairs (character-set-type-pairs type)))
2987 `(member ,@(loop for (low . high) in pairs
2988 nconc (loop for code from low upto high
2989 collect (sb!xc:code-char code))))))))
2991 (!define-type-method (character-set :simple-=) (type1 type2)
2992 (let ((pairs1 (character-set-type-pairs type1))
2993 (pairs2 (character-set-type-pairs type2)))
2994 (values (equal pairs1 pairs2) t)))
2996 (!define-type-method (character-set :simple-subtypep) (type1 type2)
2998 (dolist (pair (character-set-type-pairs type1) t)
2999 (unless (position pair (character-set-type-pairs type2)
3000 :test (lambda (x y) (and (>= (car x) (car y))
3001 (<= (cdr x) (cdr y)))))
3005 (!define-type-method (character-set :simple-union2) (type1 type2)
3006 ;; KLUDGE: the canonizing in the MAKE-CHARACTER-SET-TYPE function
3007 ;; actually does the union for us. It might be a little fragile to
3009 (make-character-set-type
3011 (copy-alist (character-set-type-pairs type1))
3012 (copy-alist (character-set-type-pairs type2))
3015 (!define-type-method (character-set :simple-intersection2) (type1 type2)
3016 ;; KLUDGE: brute force.
3019 (dolist (pair1 (character-set-type-pairs type1)
3020 (make-character-set-type
3021 :pairs (sort pairs #'< :key #'car)))
3022 (dolist (pair2 (character-set-type-pairs type2))
3024 ((<= (car pair1) (car pair2) (cdr pair1))
3025 (push (cons (car pair2) (min (cdr pair1) (cdr pair2))) pairs))
3026 ((<= (car pair2) (car pair1) (cdr pair2))
3027 (push (cons (car pair1) (min (cdr pair1) (cdr pair2))) pairs))))))
3029 (make-character-set-type
3030 :pairs (intersect-type-pairs
3031 (character-set-type-pairs type1)
3032 (character-set-type-pairs type2))))
3035 ;;; Intersect two ordered lists of pairs
3036 ;;; Each list is of the form ((start1 . end1) ... (startn . endn)),
3037 ;;; where start1 <= end1 < start2 <= end2 < ... < startn <= endn.
3038 ;;; Each pair represents the integer interval start..end.
3040 (defun intersect-type-pairs (alist1 alist2)
3041 (if (and alist1 alist2)
3043 (pair1 (pop alist1))
3044 (pair2 (pop alist2)))
3046 (when (> (car pair1) (car pair2))
3047 (rotatef pair1 pair2)
3048 (rotatef alist1 alist2))
3049 (let ((pair1-cdr (cdr pair1)))
3051 ((> (car pair2) pair1-cdr)
3052 ;; No over lap -- discard pair1
3053 (unless alist1 (return))
3054 (setq pair1 (pop alist1)))
3055 ((<= (cdr pair2) pair1-cdr)
3056 (push (cons (car pair2) (cdr pair2)) res)
3058 ((= (cdr pair2) pair1-cdr)
3059 (unless alist1 (return))
3060 (unless alist2 (return))
3061 (setq pair1 (pop alist1)
3062 pair2 (pop alist2)))
3063 (t ;; (< (cdr pair2) pair1-cdr)
3064 (unless alist2 (return))
3065 (setq pair1 (cons (1+ (cdr pair2)) pair1-cdr))
3066 (setq pair2 (pop alist2)))))
3067 (t ;; (> (cdr pair2) (cdr pair1))
3068 (push (cons (car pair2) pair1-cdr) res)
3069 (unless alist1 (return))
3070 (setq pair2 (cons (1+ pair1-cdr) (cdr pair2)))
3071 (setq pair1 (pop alist1))))))
3076 ;;; Return the type that describes all objects that are in X but not
3077 ;;; in Y. If we can't determine this type, then return NIL.
3079 ;;; For now, we only are clever dealing with union and member types.
3080 ;;; If either type is not a union type, then we pretend that it is a
3081 ;;; union of just one type. What we do is remove from X all the types
3082 ;;; that are a subtype any type in Y. If any type in X intersects with
3083 ;;; a type in Y but is not a subtype, then we give up.
3085 ;;; We must also special-case any member type that appears in the
3086 ;;; union. We remove from X's members all objects that are TYPEP to Y.
3087 ;;; If Y has any members, we must be careful that none of those
3088 ;;; members are CTYPEP to any of Y's non-member types. We give up in
3089 ;;; this case, since to compute that difference we would have to break
3090 ;;; the type from X into some collection of types that represents the
3091 ;;; type without that particular element. This seems too hairy to be
3092 ;;; worthwhile, given its low utility.
3093 (defun type-difference (x y)
3094 (let ((x-types (if (union-type-p x) (union-type-types x) (list x)))
3095 (y-types (if (union-type-p y) (union-type-types y) (list y))))
3097 (dolist (x-type x-types)
3098 (if (member-type-p x-type)
3099 (collect ((members))
3100 (dolist (mem (member-type-members x-type))
3101 (multiple-value-bind (val win) (ctypep mem y)
3102 (unless win (return-from type-difference nil))
3106 (res (make-member-type :members (members)))))
3107 (dolist (y-type y-types (res x-type))
3108 (multiple-value-bind (val win) (csubtypep x-type y-type)
3109 (unless win (return-from type-difference nil))
3111 (when (types-equal-or-intersect x-type y-type)
3112 (return-from type-difference nil))))))
3113 (let ((y-mem (find-if #'member-type-p y-types)))
3115 (let ((members (member-type-members y-mem)))
3116 (dolist (x-type x-types)
3117 (unless (member-type-p x-type)
3118 (dolist (member members)
3119 (multiple-value-bind (val win) (ctypep member x-type)
3120 (when (or (not win) val)
3121 (return-from type-difference nil)))))))))
3122 (apply #'type-union (res)))))
3124 (!def-type-translator array (&optional (element-type '*)
3126 (specialize-array-type
3127 (make-array-type :dimensions (canonical-array-dimensions dimensions)
3129 :element-type (if (eq element-type '*)
3131 (specifier-type element-type)))))
3133 (!def-type-translator simple-array (&optional (element-type '*)
3135 (specialize-array-type
3136 (make-array-type :dimensions (canonical-array-dimensions dimensions)
3138 :element-type (if (eq element-type '*)
3140 (specifier-type element-type)))))
3142 ;;;; utilities shared between cross-compiler and target system
3144 ;;; Does the type derived from compilation of an actual function
3145 ;;; definition satisfy declarations of a function's type?
3146 (defun defined-ftype-matches-declared-ftype-p (defined-ftype declared-ftype)
3147 (declare (type ctype defined-ftype declared-ftype))
3148 (flet ((is-built-in-class-function-p (ctype)
3149 (and (built-in-classoid-p ctype)
3150 (eq (built-in-classoid-name ctype) 'function))))
3151 (cond (;; DECLARED-FTYPE could certainly be #<BUILT-IN-CLASS FUNCTION>;
3152 ;; that's what happens when we (DECLAIM (FTYPE FUNCTION FOO)).
3153 (is-built-in-class-function-p declared-ftype)
3154 ;; In that case, any definition satisfies the declaration.
3156 (;; It's not clear whether or how DEFINED-FTYPE might be
3157 ;; #<BUILT-IN-CLASS FUNCTION>, but it's not obviously
3158 ;; invalid, so let's handle that case too, just in case.
3159 (is-built-in-class-function-p defined-ftype)
3160 ;; No matter what DECLARED-FTYPE might be, we can't prove
3161 ;; that an object of type FUNCTION doesn't satisfy it, so
3162 ;; we return success no matter what.
3164 (;; Otherwise both of them must be FUN-TYPE objects.
3166 ;; FIXME: For now we only check compatibility of the return
3167 ;; type, not argument types, and we don't even check the
3168 ;; return type very precisely (as per bug 94a). It would be
3169 ;; good to do a better job. Perhaps to check the
3170 ;; compatibility of the arguments, we should (1) redo
3171 ;; VALUES-TYPES-EQUAL-OR-INTERSECT as
3172 ;; ARGS-TYPES-EQUAL-OR-INTERSECT, and then (2) apply it to
3173 ;; the ARGS-TYPE slices of the FUN-TYPEs. (ARGS-TYPE
3174 ;; is a base class both of VALUES-TYPE and of FUN-TYPE.)
3175 (values-types-equal-or-intersect
3176 (fun-type-returns defined-ftype)
3177 (fun-type-returns declared-ftype))))))
3179 ;;; This messy case of CTYPE for NUMBER is shared between the
3180 ;;; cross-compiler and the target system.
3181 (defun ctype-of-number (x)
3182 (let ((num (if (complexp x) (realpart x) x)))
3183 (multiple-value-bind (complexp low high)
3185 (let ((imag (imagpart x)))
3186 (values :complex (min num imag) (max num imag)))
3187 (values :real num num))
3188 (make-numeric-type :class (etypecase num
3189 (integer (if (complexp x)
3190 (if (integerp (imagpart x))
3194 (rational 'rational)
3196 :format (and (floatp num) (float-format-name num))
3202 ;; Why SAFETY 0? To suppress the is-it-the-right-structure-type
3203 ;; checking for declarations in structure accessors. Otherwise we
3204 ;; can get caught in a chicken-and-egg bootstrapping problem, whose
3205 ;; symptom on x86 OpenBSD sbcl-0.pre7.37.flaky5.22 is an illegal
3206 ;; instruction trap. I haven't tracked it down, but I'm guessing it
3207 ;; has to do with setting LAYOUTs when the LAYOUT hasn't been set
3209 (declare (optimize (safety 0)))
3210 (!defun-from-collected-cold-init-forms !late-type-cold-init))
3212 (/show0 "late-type.lisp end of file")