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 ;;; These functions are used as method for types which need a complex
34 ;;; subtypep method to handle some superclasses, but cover a subtree
35 ;;; of the type graph (i.e. there is no simple way for any other type
36 ;;; class to be a subtype.) There are always still complex ways,
37 ;;; namely UNION and MEMBER types, so we must give TYPE1's method a
38 ;;; chance to run, instead of immediately returning NIL, T.
39 (defun delegate-complex-subtypep-arg2 (type1 type2
)
41 (type-class-complex-subtypep-arg1 (type-class-info type1
))))
43 (funcall subtypep-arg1 type1 type2
)
45 (defun delegate-complex-intersection2 (type1 type2
)
46 (let ((method (type-class-complex-intersection2 (type-class-info type1
))))
47 (if (and method
(not (eq method
#'delegate-complex-intersection2
)))
48 (funcall method type2 type1
)
49 (hierarchical-intersection2 type1 type2
))))
51 (defun contains-unknown-type-p (ctype)
52 (cond ((unknown-type-p ctype
) t
)
53 ((compound-type-p ctype
)
54 (some #'contains-unknown-type-p
(compound-type-types ctype
)))
55 ((negation-type-p ctype
)
56 (contains-unknown-type-p (negation-type-type ctype
)))))
58 ;;; This is used by !DEFINE-SUPERCLASSES to define the SUBTYPE-ARG1
59 ;;; method. INFO is a list of conses
60 ;;; (SUPERCLASS-CLASS . {GUARD-TYPE-SPECIFIER | NIL}).
61 (defun has-superclasses-complex-subtypep-arg1 (type1 type2 info
)
62 ;; If TYPE2 might be concealing something related to our class
64 (if (type-might-contain-other-types-p type2
)
65 ;; too confusing, gotta punt
67 ;; ordinary case expected by old CMU CL code, where the taxonomy
68 ;; of TYPE2's representation accurately reflects the taxonomy of
71 ;; FIXME: This old CMU CL code probably deserves a comment
72 ;; explaining to us mere mortals how it works...
73 (and (sb!xc
:typep type2
'classoid
)
75 (when (or (not (cdr x
))
76 (csubtypep type1
(specifier-type (cdr x
))))
78 (or (eq type2
(car x
))
79 (let ((inherits (layout-inherits
80 (classoid-layout (car x
)))))
81 (dotimes (i (length inherits
) nil
)
82 (when (eq type2
(layout-classoid (svref inherits i
)))
86 ;;; This function takes a list of specs, each of the form
87 ;;; (SUPERCLASS-NAME &OPTIONAL GUARD).
88 ;;; Consider one spec (with no guard): any instance of the named
89 ;;; TYPE-CLASS is also a subtype of the named superclass and of any of
90 ;;; its superclasses. If there are multiple specs, then some will have
91 ;;; guards. We choose the first spec whose guard is a supertype of
92 ;;; TYPE1 and use its superclass. In effect, a sequence of guards
95 ;;; G0,(and G1 (not G0)), (and G2 (not (or G0 G1))).
97 ;;; WHEN controls when the forms are executed.
98 (defmacro !define-superclasses
(type-class-name specs when
)
99 (with-unique-names (type-class info
)
101 (let ((,type-class
(type-class-or-lose ',type-class-name
))
102 (,info
(mapcar (lambda (spec)
104 (super &optional guard
)
106 (cons (find-classoid super
) guard
)))
108 (setf (type-class-complex-subtypep-arg1 ,type-class
)
109 (lambda (type1 type2
)
110 (has-superclasses-complex-subtypep-arg1 type1 type2
,info
)))
111 (setf (type-class-complex-subtypep-arg2 ,type-class
)
112 #'delegate-complex-subtypep-arg2
)
113 (setf (type-class-complex-intersection2 ,type-class
)
114 #'delegate-complex-intersection2
)))))
116 ;;;; FUNCTION and VALUES types
118 ;;;; Pretty much all of the general type operations are illegal on
119 ;;;; VALUES types, since we can't discriminate using them, do
120 ;;;; SUBTYPEP, etc. FUNCTION types are acceptable to the normal type
121 ;;;; operations, but are generally considered to be equivalent to
122 ;;;; FUNCTION. These really aren't true types in any type theoretic
123 ;;;; sense, but we still parse them into CTYPE structures for two
126 ;;;; -- Parsing and unparsing work the same way, and indeed we can't
127 ;;;; tell whether a type is a function or values type without
129 ;;;; -- Many of the places that can be annotated with real types can
130 ;;;; also be annotated with function or values types.
132 ;;; the description of a &KEY argument
133 (defstruct (key-info #-sb-xc-host
(:pure t
)
135 ;; the key (not necessarily a keyword in ANSI Common Lisp)
136 (name (missing-arg) :type symbol
:read-only t
)
137 ;; the type of the argument value
138 (type (missing-arg) :type ctype
:read-only t
))
140 (!define-type-method
(values :simple-subtypep
:complex-subtypep-arg1
)
142 (declare (ignore type2
))
143 ;; FIXME: should be TYPE-ERROR, here and in next method
144 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type1
)))
146 (!define-type-method
(values :complex-subtypep-arg2
)
148 (declare (ignore type1
))
149 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type2
)))
151 (!define-type-method
(values :negate
) (type)
152 (error "NOT VALUES too confusing on ~S" (type-specifier type
)))
154 (!define-type-method
(values :unparse
) (type)
156 (let ((unparsed (unparse-args-types type
)))
157 (if (or (values-type-optional type
)
158 (values-type-rest type
)
159 (values-type-allowp type
))
161 (nconc unparsed
'(&optional
))))))
163 ;;; Return true if LIST1 and LIST2 have the same elements in the same
164 ;;; positions according to TYPE=. We return NIL, NIL if there is an
165 ;;; uncertain comparison.
166 (defun type=-list
(list1 list2
)
167 (declare (list list1 list2
))
168 (do ((types1 list1
(cdr types1
))
169 (types2 list2
(cdr types2
)))
170 ((or (null types1
) (null types2
))
171 (if (or types1 types2
)
174 (multiple-value-bind (val win
)
175 (type= (first types1
) (first types2
))
177 (return (values nil nil
)))
179 (return (values nil t
))))))
181 (!define-type-method
(values :simple-
=) (type1 type2
)
182 (type=-args type1 type2
))
184 (!define-type-class function
:enumerable nil
185 :might-contain-other-types nil
)
187 ;;; a flag that we can bind to cause complex function types to be
188 ;;; unparsed as FUNCTION. This is useful when we want a type that we
189 ;;; can pass to TYPEP.
190 (!defvar
*unparse-fun-type-simplify
* nil
)
191 ;;; A flag to prevent TYPE-OF calls by user applications from returning
192 ;;; (NOT x). TYPE-SPECIFIER usually allows it to preserve information.
193 (!defvar
*unparse-allow-negation
* t
)
195 (!define-type-method
(function :negate
) (type)
196 (make-negation-type :type type
))
198 (!define-type-method
(function :unparse
) (type)
199 (if *unparse-fun-type-simplify
*
202 (if (fun-type-wild-args type
)
204 (unparse-args-types type
))
206 (fun-type-returns type
)))))
208 ;;; The meaning of this is a little confused. On the one hand, all
209 ;;; function objects are represented the same way regardless of the
210 ;;; arglists and return values, and apps don't get to ask things like
211 ;;; (TYPEP #'FOO (FUNCTION (FIXNUM) *)) in any meaningful way. On the
212 ;;; other hand, Python wants to reason about function types. So...
213 (!define-type-method
(function :simple-subtypep
) (type1 type2
)
214 (flet ((fun-type-simple-p (type)
215 (not (or (fun-type-rest type
)
216 (fun-type-keyp type
))))
217 (every-csubtypep (types1 types2
)
221 do
(multiple-value-bind (res sure-p
)
223 (unless res
(return (values res sure-p
))))
224 finally
(return (values t t
)))))
225 (and/type
(values-subtypep (fun-type-returns type1
)
226 (fun-type-returns type2
))
227 (cond ((fun-type-wild-args type2
) (values t t
))
228 ((fun-type-wild-args type1
)
229 (cond ((fun-type-keyp type2
) (values nil nil
))
230 ((not (fun-type-rest type2
)) (values nil t
))
231 ((not (null (fun-type-required type2
)))
233 (t (and/type
(type= *universal-type
*
234 (fun-type-rest type2
))
239 ((not (and (fun-type-simple-p type1
)
240 (fun-type-simple-p type2
)))
242 (t (multiple-value-bind (min1 max1
) (fun-type-nargs type1
)
243 (multiple-value-bind (min2 max2
) (fun-type-nargs type2
)
244 (cond ((or (> max1 max2
) (< min1 min2
))
246 ((and (= min1 min2
) (= max1 max2
))
247 (and/type
(every-csubtypep
248 (fun-type-required type1
)
249 (fun-type-required type2
))
251 (fun-type-optional type1
)
252 (fun-type-optional type2
))))
255 (fun-type-required type1
)
256 (fun-type-optional type1
))
258 (fun-type-required type2
)
259 (fun-type-optional type2
))))))))))))
261 (!define-superclasses function
((function)) !cold-init-forms
)
263 ;;; The union or intersection of two FUNCTION types is FUNCTION.
264 (!define-type-method
(function :simple-union2
) (type1 type2
)
265 (declare (ignore type1 type2
))
266 (specifier-type 'function
))
267 (!define-type-method
(function :simple-intersection2
) (type1 type2
)
268 (let ((ftype (specifier-type 'function
)))
269 (cond ((eq type1 ftype
) type2
)
270 ((eq type2 ftype
) type1
)
271 (t (let ((rtype (values-type-intersection (fun-type-returns type1
)
272 (fun-type-returns type2
))))
273 (flet ((change-returns (ftype rtype
)
274 (declare (type fun-type ftype
) (type ctype rtype
))
275 (make-fun-type :required
(fun-type-required ftype
)
276 :optional
(fun-type-optional ftype
)
277 :keyp
(fun-type-keyp ftype
)
278 :keywords
(fun-type-keywords ftype
)
279 :allowp
(fun-type-allowp ftype
)
282 ((fun-type-wild-args type1
)
283 (if (fun-type-wild-args type2
)
284 (make-fun-type :wild-args t
286 (change-returns type2 rtype
)))
287 ((fun-type-wild-args type2
)
288 (change-returns type1 rtype
))
289 (t (multiple-value-bind (req opt rest
)
290 (args-type-op type1 type2
#'type-intersection
#'max
)
291 (make-fun-type :required req
295 :allowp
(and (fun-type-allowp type1
)
296 (fun-type-allowp type2
))
297 :returns rtype
))))))))))
299 ;;; The union or intersection of a subclass of FUNCTION with a
300 ;;; FUNCTION type is somewhat complicated.
301 (!define-type-method
(function :complex-intersection2
) (type1 type2
)
303 ((type= type1
(specifier-type 'function
)) type2
)
304 ((csubtypep type1
(specifier-type 'function
)) nil
)
305 (t :call-other-method
)))
306 (!define-type-method
(function :complex-union2
) (type1 type2
)
307 (declare (ignore type2
))
308 ;; TYPE2 is a FUNCTION type. If TYPE1 is a classoid type naming
309 ;; FUNCTION, then it is the union of the two; otherwise, there is no
312 ((type= type1
(specifier-type 'function
)) type1
)
315 (!define-type-method
(function :simple-
=) (type1 type2
)
316 (macrolet ((compare (comparator field
)
317 (let ((reader (symbolicate '#:fun-type- field
)))
318 `(,comparator
(,reader type1
) (,reader type2
)))))
319 (and/type
(compare type
= returns
)
320 (cond ((neq (fun-type-wild-args type1
) (fun-type-wild-args type2
))
322 ((eq (fun-type-wild-args type1
) t
)
324 (t (type=-args type1 type2
))))))
326 (!define-type-class constant
:inherits values
)
328 (!define-type-method
(constant :negate
) (type)
329 (error "NOT CONSTANT too confusing on ~S" (type-specifier type
)))
331 (!define-type-method
(constant :unparse
) (type)
332 `(constant-arg ,(type-specifier (constant-type-type type
))))
334 (!define-type-method
(constant :simple-
=) (type1 type2
)
335 (type= (constant-type-type type1
) (constant-type-type type2
)))
337 (!def-type-translator constant-arg
(type)
338 (make-constant-type :type
(single-value-specifier-type type
)))
340 ;;; Return the lambda-list-like type specification corresponding
342 (declaim (ftype (function (args-type) list
) unparse-args-types
))
343 (defun unparse-args-types (type)
346 (dolist (arg (args-type-required type
))
347 (result (type-specifier arg
)))
349 (when (args-type-optional type
)
351 (dolist (arg (args-type-optional type
))
352 (result (type-specifier arg
))))
354 (when (args-type-rest type
)
356 (result (type-specifier (args-type-rest type
))))
358 (when (args-type-keyp type
)
360 (dolist (key (args-type-keywords type
))
361 (result (list (key-info-name key
)
362 (type-specifier (key-info-type key
))))))
364 (when (args-type-allowp type
)
365 (result '&allow-other-keys
))
369 (!def-type-translator function
(&optional
(args '*) (result '*))
370 (let ((result (coerce-to-values (values-specifier-type result
))))
372 (if (eq result
*wild-type
*)
373 (specifier-type 'function
)
374 (make-fun-type :wild-args t
:returns result
))
375 (multiple-value-bind (required optional rest keyp keywords allowp
)
376 (parse-args-types args
)
377 (if (and (null required
)
379 (eq rest
*universal-type
*)
381 (if (eq result
*wild-type
*)
382 (specifier-type 'function
)
383 (make-fun-type :wild-args t
:returns result
))
384 (make-fun-type :required required
390 :returns result
))))))
392 (!def-type-translator values
(&rest values
)
395 (multiple-value-bind (required optional rest keyp keywords allowp llk-p
)
396 (parse-args-types values
)
397 (declare (ignore keywords
))
399 (error "&KEY appeared in a VALUES type specifier ~S."
402 (make-values-type :required required
407 (make-short-values-type required
))))))
409 ;;;; VALUES types interfaces
411 ;;;; We provide a few special operations that can be meaningfully used
412 ;;;; on VALUES types (as well as on any other type).
414 ;;; Return the minimum number of values possibly matching VALUES type
416 (defun values-type-min-value-count (type)
419 (ecase (named-type-name type
)
423 (length (values-type-required type
)))))
425 ;;; Return the maximum number of values possibly matching VALUES type
427 (defun values-type-max-value-count (type)
430 (ecase (named-type-name type
)
431 ((t *) call-arguments-limit
)
434 (if (values-type-rest type
)
436 (+ (length (values-type-optional type
))
437 (length (values-type-required type
)))))))
439 (defun values-type-may-be-single-value-p (type)
440 (<= (values-type-min-value-count type
)
442 (values-type-max-value-count type
)))
444 ;;; VALUES type with a single value.
445 (defun type-single-value-p (type)
446 (and (%values-type-p type
)
447 (not (values-type-rest type
))
448 (null (values-type-optional type
))
449 (singleton-p (values-type-required type
))))
451 ;;; Return the type of the first value indicated by TYPE. This is used
452 ;;; by people who don't want to have to deal with VALUES types.
453 #!-sb-fluid
(declaim (freeze-type values-type
))
454 ; (inline single-value-type))
455 (defun single-value-type (type)
456 (declare (type ctype type
))
457 (cond ((eq type
*wild-type
*)
459 ((eq type
*empty-type
*)
461 ((not (values-type-p type
))
463 ((car (args-type-required type
)))
464 (t (type-union (specifier-type 'null
)
465 (or (car (args-type-optional type
))
466 (args-type-rest type
)
467 (specifier-type 'null
))))))
469 ;;; Return the minimum number of arguments that a function can be
470 ;;; called with, and the maximum number or NIL. If not a function
471 ;;; type, return NIL, NIL.
472 (defun fun-type-nargs (type)
473 (declare (type ctype type
))
474 (if (and (fun-type-p type
) (not (fun-type-wild-args type
)))
475 (let ((fixed (length (args-type-required type
))))
476 (if (or (args-type-rest type
)
477 (args-type-keyp type
)
478 (args-type-allowp type
))
480 (values fixed
(+ fixed
(length (args-type-optional type
))))))
483 ;;; Determine whether TYPE corresponds to a definite number of values.
484 ;;; The first value is a list of the types for each value, and the
485 ;;; second value is the number of values. If the number of values is
486 ;;; not fixed, then return NIL and :UNKNOWN.
487 (defun values-types (type)
488 (declare (type ctype type
))
489 (cond ((or (eq type
*wild-type
*) (eq type
*empty-type
*))
490 (values nil
:unknown
))
491 ((or (args-type-optional type
)
492 (args-type-rest type
))
493 (values nil
:unknown
))
495 (let ((req (args-type-required type
)))
496 (values req
(length req
))))))
498 ;;; Return two values:
499 ;;; 1. A list of all the positional (fixed and optional) types.
500 ;;; 2. The &REST type (if any). If no &REST, then the DEFAULT-TYPE.
501 (defun values-type-types (type &optional
(default-type *empty-type
*))
502 (declare (type ctype type
))
503 (if (eq type
*wild-type
*)
504 (values nil
*universal-type
*)
505 (values (append (args-type-required type
)
506 (args-type-optional type
))
507 (cond ((args-type-rest type
))
510 ;;; types of values in (the <type> (values o_1 ... o_n))
511 (defun values-type-out (type count
)
512 (declare (type ctype type
) (type unsigned-byte count
))
513 (if (eq type
*wild-type
*)
514 (make-list count
:initial-element
*universal-type
*)
516 (flet ((process-types (types)
517 (loop for type in types
521 (process-types (values-type-required type
))
522 (process-types (values-type-optional type
))
524 (loop with rest
= (the ctype
(values-type-rest type
))
529 ;;; types of variable in (m-v-bind (v_1 ... v_n) (the <type> ...
530 (defun values-type-in (type count
)
531 (declare (type ctype type
) (type unsigned-byte count
))
532 (if (eq type
*wild-type
*)
533 (make-list count
:initial-element
*universal-type
*)
535 (let ((null-type (specifier-type 'null
)))
536 (loop for type in
(values-type-required type
)
540 (loop for type in
(values-type-optional type
)
543 do
(res (type-union type null-type
)))
545 (loop with rest
= (acond ((values-type-rest type
)
546 (type-union it null-type
))
552 ;;; Return a list of OPERATION applied to the types in TYPES1 and
553 ;;; TYPES2, padding with REST2 as needed. TYPES1 must not be shorter
554 ;;; than TYPES2. The second value is T if OPERATION always returned a
555 ;;; true second value.
556 (defun fixed-values-op (types1 types2 rest2 operation
)
557 (declare (list types1 types2
) (type ctype rest2
) (type function operation
))
559 (values (mapcar (lambda (t1 t2
)
560 (multiple-value-bind (res win
)
561 (funcall operation t1 t2
)
567 (make-list (- (length types1
) (length types2
))
568 :initial-element rest2
)))
571 ;;; If TYPE isn't a values type, then make it into one.
572 (defun-cached (%coerce-to-values
:hash-bits
8 :hash-function
#'type-hash-value
)
574 (cond ((multiple-value-bind (res sure
)
575 (csubtypep (specifier-type 'null
) type
)
576 (and (not res
) sure
))
577 ;; FIXME: What should we do with (NOT SURE)?
578 (make-values-type :required
(list type
) :rest
*universal-type
*))
580 (make-values-type :optional
(list type
) :rest
*universal-type
*))))
582 (defun coerce-to-values (type)
583 (declare (type ctype type
))
584 (cond ((or (eq type
*universal-type
*)
585 (eq type
*wild-type
*))
587 ((values-type-p type
)
589 (t (%coerce-to-values type
))))
591 ;;; Return type, corresponding to ANSI short form of VALUES type
593 (defun make-short-values-type (types)
594 (declare (list types
))
595 (let ((last-required (position-if
597 (not/type
(csubtypep (specifier-type 'null
) type
)))
601 (make-values-type :required
(subseq types
0 (1+ last-required
))
602 :optional
(subseq types
(1+ last-required
))
603 :rest
*universal-type
*)
604 (make-values-type :optional types
:rest
*universal-type
*))))
606 (defun make-single-value-type (type)
607 (make-values-type :required
(list type
)))
609 ;;; Do the specified OPERATION on TYPE1 and TYPE2, which may be any
610 ;;; type, including VALUES types. With VALUES types such as:
613 ;;; we compute the more useful result
614 ;;; (VALUES (<operation> a0 b0) (<operation> a1 b1))
615 ;;; rather than the precise result
616 ;;; (<operation> (values a0 a1) (values b0 b1))
617 ;;; This has the virtue of always keeping the VALUES type specifier
618 ;;; outermost, and retains all of the information that is really
619 ;;; useful for static type analysis. We want to know what is always
620 ;;; true of each value independently. It is worthless to know that if
621 ;;; the first value is B0 then the second will be B1.
623 ;;; If the VALUES count signatures differ, then we produce a result with
624 ;;; the required VALUE count chosen by NREQ when applied to the number
625 ;;; of required values in TYPE1 and TYPE2. Any &KEY values become
626 ;;; &REST T (anyone who uses keyword values deserves to lose.)
628 ;;; The second value is true if the result is definitely empty or if
629 ;;; OPERATION returned true as its second value each time we called
630 ;;; it. Since we approximate the intersection of VALUES types, the
631 ;;; second value being true doesn't mean the result is exact.
632 (defun args-type-op (type1 type2 operation nreq
)
633 (declare (type ctype type1 type2
)
634 (type function operation nreq
))
635 (when (eq type1 type2
)
637 (multiple-value-bind (types1 rest1
)
638 (values-type-types type1
)
639 (multiple-value-bind (types2 rest2
)
640 (values-type-types type2
)
641 (multiple-value-bind (rest rest-exact
)
642 (funcall operation rest1 rest2
)
643 (multiple-value-bind (res res-exact
)
644 (if (< (length types1
) (length types2
))
645 (fixed-values-op types2 types1 rest1 operation
)
646 (fixed-values-op types1 types2 rest2 operation
))
647 (let* ((req (funcall nreq
648 (length (args-type-required type1
))
649 (length (args-type-required type2
))))
650 (required (subseq res
0 req
))
651 (opt (subseq res req
)))
652 (values required opt rest
653 (and rest-exact res-exact
))))))))
655 (defun values-type-op (type1 type2 operation nreq
)
656 (multiple-value-bind (required optional rest exactp
)
657 (args-type-op type1 type2 operation nreq
)
658 (values (make-values-type :required required
663 (defun compare-key-args (type1 type2
)
664 (let ((keys1 (args-type-keywords type1
))
665 (keys2 (args-type-keywords type2
)))
666 (and (= (length keys1
) (length keys2
))
667 (eq (args-type-allowp type1
)
668 (args-type-allowp type2
))
669 (loop for key1 in keys1
670 for match
= (find (key-info-name key1
)
671 keys2
:key
#'key-info-name
)
673 (type= (key-info-type key1
)
674 (key-info-type match
)))))))
676 (defun type=-args
(type1 type2
)
677 (macrolet ((compare (comparator field
)
678 (let ((reader (symbolicate '#:args-type- field
)))
679 `(,comparator
(,reader type1
) (,reader type2
)))))
681 (cond ((null (args-type-rest type1
))
682 (values (null (args-type-rest type2
)) t
))
683 ((null (args-type-rest type2
))
686 (compare type
= rest
)))
687 (and/type
(and/type
(compare type
=-list required
)
688 (compare type
=-list optional
))
689 (if (or (args-type-keyp type1
) (args-type-keyp type2
))
690 (values (compare-key-args type1 type2
) t
)
693 ;;; Do a union or intersection operation on types that might be values
694 ;;; types. The result is optimized for utility rather than exactness,
695 ;;; but it is guaranteed that it will be no smaller (more restrictive)
696 ;;; than the precise result.
698 ;;; The return convention seems to be analogous to
699 ;;; TYPES-EQUAL-OR-INTERSECT. -- WHN 19990910.
700 (defun-cached (values-type-union :hash-function
#'type-cache-hash
702 ((type1 eq
) (type2 eq
))
703 (declare (type ctype type1 type2
))
704 (cond ((or (eq type1
*wild-type
*) (eq type2
*wild-type
*)) *wild-type
*)
705 ((eq type1
*empty-type
*) type2
)
706 ((eq type2
*empty-type
*) type1
)
708 (values (values-type-op type1 type2
#'type-union
#'min
)))))
710 (defun-cached (values-type-intersection :hash-function
#'type-cache-hash
712 ((type1 eq
) (type2 eq
))
713 (declare (type ctype type1 type2
))
714 (cond ((eq type1
*wild-type
*)
715 (coerce-to-values type2
))
716 ((or (eq type2
*wild-type
*) (eq type2
*universal-type
*))
718 ((or (eq type1
*empty-type
*) (eq type2
*empty-type
*))
720 ((and (not (values-type-p type2
))
721 (values-type-required type1
))
722 (let ((req1 (values-type-required type1
)))
723 (make-values-type :required
(cons (type-intersection (first req1
) type2
)
725 :optional
(values-type-optional type1
)
726 :rest
(values-type-rest type1
)
727 :allowp
(values-type-allowp type1
))))
729 (values (values-type-op type1
(coerce-to-values type2
)
733 ;;; This is like TYPES-EQUAL-OR-INTERSECT, except that it sort of
734 ;;; works on VALUES types. Note that due to the semantics of
735 ;;; VALUES-TYPE-INTERSECTION, this might return (VALUES T T) when
736 ;;; there isn't really any intersection.
737 (defun values-types-equal-or-intersect (type1 type2
)
738 (cond ((or (eq type1
*empty-type
*) (eq type2
*empty-type
*))
740 ((or (eq type1
*wild-type
*) (eq type2
*wild-type
*))
743 (let ((res (values-type-intersection type1 type2
)))
744 (values (not (eq res
*empty-type
*))
747 ;;; a SUBTYPEP-like operation that can be used on any types, including
749 (defun-cached (values-subtypep :hash-function
#'type-cache-hash
752 ((type1 eq
) (type2 eq
))
753 (declare (type ctype type1 type2
))
754 (cond ((or (eq type2
*wild-type
*) (eq type2
*universal-type
*)
755 (eq type1
*empty-type
*))
757 ((eq type1
*wild-type
*)
758 (values (eq type2
*wild-type
*) t
))
759 ((or (eq type2
*empty-type
*)
760 (not (values-types-equal-or-intersect type1 type2
)))
762 ((and (not (values-type-p type2
))
763 (values-type-required type1
))
764 (csubtypep (first (values-type-required type1
))
766 (t (setq type2
(coerce-to-values type2
))
767 (multiple-value-bind (types1 rest1
) (values-type-types type1
)
768 (multiple-value-bind (types2 rest2
) (values-type-types type2
)
769 (cond ((< (length (values-type-required type1
))
770 (length (values-type-required type2
)))
772 ((< (length types1
) (length types2
))
775 (do ((t1 types1
(rest t1
))
776 (t2 types2
(rest t2
)))
778 (csubtypep rest1 rest2
))
779 (multiple-value-bind (res win-p
)
780 (csubtypep (first t1
) (first t2
))
782 (return (values nil nil
)))
784 (return (values nil t
))))))))))))
786 ;;;; type method interfaces
788 ;;; like SUBTYPEP, only works on CTYPE structures
789 (defun-cached (csubtypep :hash-function
#'type-cache-hash
793 ((type1 eq
) (type2 eq
))
794 (declare (type ctype type1 type2
))
795 (cond ((or (eq type1 type2
)
796 (eq type1
*empty-type
*)
797 (eq type2
*universal-type
*))
800 ((eq type1
*universal-type
*)
804 (!invoke-type-method
:simple-subtypep
:complex-subtypep-arg2
806 :complex-arg1
:complex-subtypep-arg1
)))))
808 ;;; Just parse the type specifiers and call CSUBTYPE.
809 (defun sb!xc
:subtypep
(type1 type2
&optional environment
)
811 "Return two values indicating the relationship between type1 and type2.
812 If values are T and T, type1 definitely is a subtype of type2.
813 If values are NIL and T, type1 definitely is not a subtype of type2.
814 If values are NIL and NIL, it couldn't be determined."
815 (declare (ignore environment
))
816 (csubtypep (specifier-type type1
) (specifier-type type2
)))
818 ;;; If two types are definitely equivalent, return true. The second
819 ;;; value indicates whether the first value is definitely correct.
820 ;;; This should only fail in the presence of HAIRY types.
821 (defun-cached (type= :hash-function
#'type-cache-hash
825 ((type1 eq
) (type2 eq
))
826 (declare (type ctype type1 type2
))
829 (memoize (!invoke-type-method
:simple-
= :complex-
= type1 type2
))))
831 ;;; Not exactly the negation of TYPE=, since when the relationship is
832 ;;; uncertain, we still return NIL, NIL. This is useful in cases where
833 ;;; the conservative assumption is =.
834 (defun type/= (type1 type2
)
835 (declare (type ctype type1 type2
))
836 (multiple-value-bind (res win
) (type= type1 type2
)
841 ;;; the type method dispatch case of TYPE-UNION2
842 (defun %type-union2
(type1 type2
)
843 ;; As in %TYPE-INTERSECTION2, it seems to be a good idea to give
844 ;; both argument orders a chance at COMPLEX-INTERSECTION2. Unlike
845 ;; %TYPE-INTERSECTION2, though, I don't have a specific case which
846 ;; demonstrates this is actually necessary. Also unlike
847 ;; %TYPE-INTERSECTION2, there seems to be no need to distinguish
848 ;; between not finding a method and having a method return NIL.
850 (!invoke-type-method
:simple-union2
:complex-union2
853 (declare (inline 1way
))
854 (or (1way type1 type2
)
855 (1way type2 type1
))))
857 ;;; Find a type which includes both types. Any inexactness is
858 ;;; represented by the fuzzy element types; we return a single value
859 ;;; that is precise to the best of our knowledge. This result is
860 ;;; simplified into the canonical form, thus is not a UNION-TYPE
861 ;;; unless we find no other way to represent the result.
862 (defun-cached (type-union2 :hash-function
#'type-cache-hash
865 ((type1 eq
) (type2 eq
))
866 ;; KLUDGE: This was generated from TYPE-INTERSECTION2 by Ye Olde Cut And
867 ;; Paste technique of programming. If it stays around (as opposed to
868 ;; e.g. fading away in favor of some CLOS solution) the shared logic
869 ;; should probably become shared code. -- WHN 2001-03-16
870 (declare (type ctype type1 type2
))
876 ;; CSUBTYPEP for array-types answers questions about the
877 ;; specialized type, yet for union we want to take the
878 ;; expressed type in account too.
879 ((and (not (and (array-type-p type1
) (array-type-p type2
)))
880 (or (setf t2
(csubtypep type1 type2
))
881 (csubtypep type2 type1
)))
883 ((or (union-type-p type1
)
884 (union-type-p type2
))
885 ;; Unions of UNION-TYPE should have the UNION-TYPE-TYPES
886 ;; values broken out and united separately. The full TYPE-UNION
887 ;; function knows how to do this, so let it handle it.
888 (type-union type1 type2
))
890 ;; the ordinary case: we dispatch to type methods
891 (%type-union2 type1 type2
)))))))
893 ;;; the type method dispatch case of TYPE-INTERSECTION2
894 (defun %type-intersection2
(type1 type2
)
895 ;; We want to give both argument orders a chance at
896 ;; COMPLEX-INTERSECTION2. Without that, the old CMU CL type
897 ;; methods could give noncommutative results, e.g.
898 ;; (TYPE-INTERSECTION2 *EMPTY-TYPE* SOME-HAIRY-TYPE)
900 ;; (TYPE-INTERSECTION2 SOME-HAIRY-TYPE *EMPTY-TYPE*)
901 ;; => #<NAMED-TYPE NIL>, T
902 ;; We also need to distinguish between the case where we found a
903 ;; type method, and it returned NIL, and the case where we fell
904 ;; through without finding any type method. An example of the first
905 ;; case is the intersection of a HAIRY-TYPE with some ordinary type.
906 ;; An example of the second case is the intersection of two
907 ;; completely-unrelated types, e.g. CONS and NUMBER, or SYMBOL and
910 ;; (Why yes, CLOS probably *would* be nicer..)
912 (!invoke-type-method
:simple-intersection2
:complex-intersection2
914 :default
:call-other-method
)))
915 (declare (inline 1way
))
916 (let ((xy (1way type1 type2
)))
917 (or (and (not (eql xy
:call-other-method
)) xy
)
918 (let ((yx (1way type2 type1
)))
919 (or (and (not (eql yx
:call-other-method
)) yx
)
920 (cond ((and (eql xy
:call-other-method
)
921 (eql yx
:call-other-method
))
926 (defun-cached (type-intersection2 :hash-function
#'type-cache-hash
930 ((type1 eq
) (type2 eq
))
931 (declare (type ctype type1 type2
))
933 ;; FIXME: For some reason, this doesn't catch e.g. type1 =
934 ;; type2 = (SPECIFIER-TYPE
935 ;; 'SOME-UNKNOWN-TYPE). Investigate. - CSR, 2002-04-10
939 ((or (intersection-type-p type1
)
940 (intersection-type-p type2
))
941 ;; Intersections of INTERSECTION-TYPE should have the
942 ;; INTERSECTION-TYPE-TYPES values broken out and intersected
943 ;; separately. The full TYPE-INTERSECTION function knows how
944 ;; to do that, so let it handle it.
945 (type-intersection type1 type2
))
947 ;; the ordinary case: we dispatch to type methods
948 (%type-intersection2 type1 type2
))))))
950 ;;; Return as restrictive and simple a type as we can discover that is
951 ;;; no more restrictive than the intersection of TYPE1 and TYPE2. At
952 ;;; worst, we arbitrarily return one of the arguments as the first
953 ;;; value (trying not to return a hairy type).
954 (defun type-approx-intersection2 (type1 type2
)
955 (cond ((type-intersection2 type1 type2
))
956 ((hairy-type-p type1
) type2
)
959 ;;; a test useful for checking whether a derived type matches a
962 ;;; The first value is true unless the types don't intersect and
963 ;;; aren't equal. The second value is true if the first value is
964 ;;; definitely correct. NIL is considered to intersect with any type.
965 ;;; If T is a subtype of either type, then we also return T, T. This
966 ;;; way we recognize that hairy types might intersect with T.
968 ;;; Well now given the statement above that this is "useful for ..."
969 ;;; a particular thing, I see how treating *empty-type* magically could
970 ;;; be useful, however given all the _other_ calls to this function within
971 ;;; this file, it seems suboptimal, because logically it is wrong.
972 (defun types-equal-or-intersect (type1 type2
)
973 (declare (type ctype type1 type2
))
974 (if (or (eq type1
*empty-type
*) (eq type2
*empty-type
*))
976 (let ((intersection2 (type-intersection2 type1 type2
)))
977 (cond ((not intersection2
)
978 (if (or (csubtypep *universal-type
* type1
)
979 (csubtypep *universal-type
* type2
))
982 ((eq intersection2
*empty-type
*) (values nil t
))
985 ;;; Return a Common Lisp type specifier corresponding to the TYPE
987 (defun type-specifier (type)
988 (declare (type ctype type
))
989 (funcall (type-class-unparse (type-class-info type
)) type
))
991 (defun-cached (type-negation :hash-function
#'type-hash-value
995 (declare (type ctype type
))
996 (funcall (type-class-negate (type-class-info type
)) type
))
998 (defun-cached (type-singleton-p :hash-function
#'type-hash-value
1002 (declare (type ctype type
))
1003 (let ((function (type-class-singleton-p (type-class-info type
))))
1005 (funcall function type
)
1008 ;;; (VALUES-SPECIFIER-TYPE and SPECIFIER-TYPE moved from here to
1009 ;;; early-type.lisp by WHN ca. 19990201.)
1011 ;;; Take a list of type specifiers, computing the translation of each
1012 ;;; specifier and defining it as a builtin type.
1013 (declaim (ftype (function (list) (values)) precompute-types
))
1014 (defun precompute-types (specs)
1015 (dolist (spec specs
)
1016 (let ((res (specifier-type spec
)))
1017 (unless (unknown-type-p res
)
1018 (setf (info :type
:builtin spec
) res
)
1019 ;; KLUDGE: the three copies of this idiom in this file (and
1020 ;; the one in class.lisp as at sbcl-0.7.4.1x) should be
1021 ;; coalesced, or perhaps the error-detecting code that
1022 ;; disallows redefinition of :PRIMITIVE types should be
1023 ;; rewritten to use *TYPE-SYSTEM-FINALIZED* (rather than
1024 ;; *TYPE-SYSTEM-INITIALIZED*). The effect of this is not to
1025 ;; cause redefinition errors when precompute-types is called
1026 ;; for a second time while building the target compiler using
1027 ;; the cross-compiler. -- CSR, trying to explain why this
1028 ;; isn't completely wrong, 2002-06-07
1029 (setf (info :type
:kind spec
) #+sb-xc-host
:defined
#-sb-xc-host
:primitive
))))
1032 ;;;; general TYPE-UNION and TYPE-INTERSECTION operations
1034 ;;;; These are fully general operations on CTYPEs: they'll always
1035 ;;;; return a CTYPE representing the result.
1037 ;;; shared logic for unions and intersections: Return a list of
1038 ;;; types representing the same types as INPUT-TYPES, but with
1039 ;;; COMPOUND-TYPEs satisfying %COMPOUND-TYPE-P broken up into their
1040 ;;; component types, and with any SIMPLY2 simplifications applied.
1042 ((def (name compound-type-p simplify2
)
1043 `(defun ,name
(types)
1045 (multiple-value-bind (first rest
)
1046 (if (,compound-type-p
(car types
))
1047 (values (car (compound-type-types (car types
)))
1048 (append (cdr (compound-type-types (car types
)))
1050 (values (car types
) (cdr types
)))
1051 (let ((rest (,name rest
)) u
)
1052 (dolist (r rest
(cons first rest
))
1053 (when (setq u
(,simplify2 first r
))
1054 (return (,name
(nsubstitute u r rest
)))))))))))
1055 (def simplify-intersections intersection-type-p type-intersection2
)
1056 (def simplify-unions union-type-p type-union2
))
1058 (defun maybe-distribute-one-union (union-type types
)
1059 (let* ((intersection (apply #'type-intersection types
))
1060 (union (mapcar (lambda (x) (type-intersection x intersection
))
1061 (union-type-types union-type
))))
1062 (if (notany (lambda (x) (or (hairy-type-p x
)
1063 (intersection-type-p x
)))
1068 (defun type-intersection (&rest input-types
)
1069 (%type-intersection input-types
))
1070 (defun-cached (%type-intersection
:hash-bits
10 :hash-function
#'type-list-cache-hash
)
1071 ((input-types equal
))
1072 (let ((simplified-types (simplify-intersections input-types
)))
1073 (declare (type list simplified-types
))
1074 ;; We want to have a canonical representation of types (or failing
1075 ;; that, punt to HAIRY-TYPE). Canonical representation would have
1076 ;; intersections inside unions but not vice versa, since you can
1077 ;; always achieve that by the distributive rule. But we don't want
1078 ;; to just apply the distributive rule, since it would be too easy
1079 ;; to end up with unreasonably huge type expressions. So instead
1080 ;; we try to generate a simple type by distributing the union; if
1081 ;; the type can't be made simple, we punt to HAIRY-TYPE.
1082 (if (and (cdr simplified-types
) (some #'union-type-p simplified-types
))
1083 (let* ((first-union (find-if #'union-type-p simplified-types
))
1084 (other-types (coerce (remove first-union simplified-types
)
1086 (distributed (maybe-distribute-one-union first-union
1089 (apply #'type-union distributed
)
1090 (%make-hairy-type
`(and ,@(map 'list
#'type-specifier
1091 simplified-types
)))))
1093 ((null simplified-types
) *universal-type
*)
1094 ((null (cdr simplified-types
)) (car simplified-types
))
1095 (t (%make-intersection-type
1096 (some #'type-enumerable simplified-types
)
1097 simplified-types
))))))
1099 (defun type-union (&rest input-types
)
1100 (%type-union input-types
))
1101 (defun-cached (%type-union
:hash-bits
8 :hash-function
#'type-list-cache-hash
)
1102 ((input-types equal
))
1103 (let ((simplified-types (simplify-unions input-types
)))
1105 ((null simplified-types
) *empty-type
*)
1106 ((null (cdr simplified-types
)) (car simplified-types
))
1108 (every #'type-enumerable simplified-types
)
1109 simplified-types
)))))
1113 (!define-type-class named
:enumerable nil
:might-contain-other-types nil
)
1116 (macrolet ((frob (name var
)
1118 (setq ,var
(make-named-type :name
',name
))
1119 (setf (info :type
:kind
',name
)
1120 #+sb-xc-host
:defined
#-sb-xc-host
:primitive
)
1121 (setf (info :type
:builtin
',name
) ,var
))))
1122 ;; KLUDGE: In ANSI, * isn't really the name of a type, it's just a
1123 ;; special symbol which can be stuck in some places where an
1124 ;; ordinary type can go, e.g. (ARRAY * 1) instead of (ARRAY T 1).
1125 ;; In SBCL it also used to denote universal VALUES type.
1126 (frob * *wild-type
*)
1127 (frob nil
*empty-type
*)
1128 (frob t
*universal-type
*)
1129 (setf (sb!c
::type-info-default
(sb!c
::type-info-or-lose
:variable
:type
))
1131 ;; new in sbcl-0.9.5: these used to be CLASSOID types, but that
1132 ;; view of them was incompatible with requirements on the MOP
1133 ;; metaobject class hierarchy: the INSTANCE and
1134 ;; FUNCALLABLE-INSTANCE types are disjoint (instances have
1135 ;; instance-pointer-lowtag; funcallable-instances have
1136 ;; fun-pointer-lowtag), while FUNCALLABLE-STANDARD-OBJECT is
1137 ;; required to be a subclass of STANDARD-OBJECT. -- CSR,
1139 (frob instance
*instance-type
*)
1140 (frob funcallable-instance
*funcallable-instance-type
*)
1141 ;; new in sbcl-1.0.3.3: necessary to act as a join point for the
1142 ;; extended sequence hierarchy. (Might be removed later if we use
1143 ;; a dedicated FUNDAMENTAL-SEQUENCE class for this.)
1144 (frob extended-sequence
*extended-sequence-type
*))
1145 (setf *satisfies-keywordp-type
* (%make-hairy-type
'(satisfies keywordp
)))
1146 (setf *fun-name-type
* (%make-hairy-type
'(satisfies legal-fun-name-p
)))
1147 (setf *universal-fun-type
*
1148 (make-fun-type :wild-args t
1149 :returns
*wild-type
*)))
1151 (!define-type-method
(named :simple-
=) (type1 type2
)
1152 ;;(aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1153 (values (eq type1 type2
) t
))
1155 (defun cons-type-might-be-empty-type (type)
1156 (declare (type cons-type type
))
1157 (let ((car-type (cons-type-car-type type
))
1158 (cdr-type (cons-type-cdr-type type
)))
1160 (if (cons-type-p car-type
)
1161 (cons-type-might-be-empty-type car-type
)
1162 (multiple-value-bind (yes surep
)
1163 (type= car-type
*empty-type
*)
1166 (if (cons-type-p cdr-type
)
1167 (cons-type-might-be-empty-type cdr-type
)
1168 (multiple-value-bind (yes surep
)
1169 (type= cdr-type
*empty-type
*)
1173 (!define-type-method
(named :complex-
=) (type1 type2
)
1175 ((and (eq type2
*empty-type
*)
1176 (or (and (intersection-type-p type1
)
1177 ;; not allowed to be unsure on these... FIXME: keep
1178 ;; the list of CL types that are intersection types
1179 ;; once and only once.
1180 (not (or (type= type1
(specifier-type 'ratio
))
1181 (type= type1
(specifier-type 'keyword
)))))
1182 (and (cons-type-p type1
)
1183 (cons-type-might-be-empty-type type1
))))
1184 ;; things like (AND (EQL 0) (SATISFIES ODDP)) or (AND FUNCTION
1185 ;; STREAM) can get here. In general, we can't really tell
1186 ;; whether these are equal to NIL or not, so
1188 ((type-might-contain-other-types-p type1
)
1189 (invoke-complex-=-other-method type1 type2
))
1190 (t (values nil t
))))
1192 (!define-type-method
(named :simple-subtypep
) (type1 type2
)
1193 (aver (not (eq type1
*wild-type
*))) ; * isn't really a type.
1194 (aver (not (eq type1 type2
)))
1195 (values (or (eq type1
*empty-type
*)
1196 (eq type2
*wild-type
*)
1197 (eq type2
*universal-type
*)) t
))
1199 (!define-type-method
(named :complex-subtypep-arg1
) (type1 type2
)
1200 ;; This AVER causes problems if we write accurate methods for the
1201 ;; union (and possibly intersection) types which then delegate to
1202 ;; us; while a user shouldn't get here, because of the odd status of
1203 ;; *wild-type* a type-intersection executed by the compiler can. -
1206 ;; (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1207 (cond ((eq type1
*empty-type
*)
1209 (;; When TYPE2 might be the universal type in disguise
1210 (type-might-contain-other-types-p type2
)
1211 ;; Now that the UNION and HAIRY COMPLEX-SUBTYPEP-ARG2 methods
1212 ;; can delegate to us (more or less as CALL-NEXT-METHOD) when
1213 ;; they're uncertain, we can't just barf on COMPOUND-TYPE and
1214 ;; HAIRY-TYPEs as we used to. Instead we deal with the
1215 ;; problem (where at least part of the problem is cases like
1216 ;; (SUBTYPEP T '(SATISFIES FOO))
1218 ;; (SUBTYPEP T '(AND (SATISFIES FOO) (SATISFIES BAR)))
1219 ;; where the second type is a hairy type like SATISFIES, or
1220 ;; is a compound type which might contain a hairy type) by
1221 ;; returning uncertainty.
1223 ((eq type1
*funcallable-instance-type
*)
1224 (values (eq type2
(specifier-type 'function
)) t
))
1226 ;; This case would have been picked off by the SIMPLE-SUBTYPEP
1227 ;; method, and so shouldn't appear here.
1228 (aver (not (named-type-p type2
)))
1229 ;; Since TYPE2 is not EQ *UNIVERSAL-TYPE* and is not another
1230 ;; named type in disguise, TYPE2 is not a superset of TYPE1.
1233 (!define-type-method
(named :complex-subtypep-arg2
) (type1 type2
)
1234 (aver (not (eq type2
*wild-type
*))) ; * isn't really a type.
1235 (cond ((eq type2
*universal-type
*)
1237 ;; some CONS types can conceal danger
1238 ((and (cons-type-p type1
) (cons-type-might-be-empty-type type1
))
1240 ((type-might-contain-other-types-p type1
)
1241 ;; those types can be other types in disguise. So we'd
1243 (invoke-complex-subtypep-arg1-method type1 type2
))
1244 ((and (or (eq type2
*instance-type
*)
1245 (eq type2
*funcallable-instance-type
*))
1246 (member-type-p type1
))
1247 ;; member types can be subtypep INSTANCE and
1248 ;; FUNCALLABLE-INSTANCE in surprising ways.
1249 (invoke-complex-subtypep-arg1-method type1 type2
))
1250 ((and (eq type2
*extended-sequence-type
*) (classoid-p type1
))
1251 (let* ((layout (classoid-layout type1
))
1252 (inherits (layout-inherits layout
))
1253 (sequencep (find (classoid-layout (find-classoid 'sequence
))
1255 (values (if sequencep t nil
) t
)))
1256 ((and (eq type2
*instance-type
*) (classoid-p type1
))
1257 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1259 (let* ((layout (classoid-layout type1
))
1260 (inherits (layout-inherits layout
))
1261 (functionp (find (classoid-layout (find-classoid 'function
))
1266 ((eq type1
(find-classoid 'function
))
1268 ((or (structure-classoid-p type1
)
1270 (condition-classoid-p type1
))
1272 (t (values nil nil
))))))
1273 ((and (eq type2
*funcallable-instance-type
*) (classoid-p type1
))
1274 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1276 (let* ((layout (classoid-layout type1
))
1277 (inherits (layout-inherits layout
))
1278 (functionp (find (classoid-layout (find-classoid 'function
))
1280 (values (if functionp t nil
) t
))))
1282 ;; FIXME: This seems to rely on there only being 4 or 5
1283 ;; NAMED-TYPE values, and the exclusion of various
1284 ;; possibilities above. It would be good to explain it and/or
1285 ;; rewrite it so that it's clearer.
1288 (!define-type-method
(named :complex-intersection2
) (type1 type2
)
1289 ;; FIXME: This assertion failed when I added it in sbcl-0.6.11.13.
1290 ;; Perhaps when bug 85 is fixed it can be reenabled.
1291 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1293 ((eq type2
*extended-sequence-type
*)
1295 (structure-classoid *empty-type
*)
1297 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1299 (if (find (classoid-layout (find-classoid 'sequence
))
1300 (layout-inherits (classoid-layout type1
)))
1304 (if (or (type-might-contain-other-types-p type1
)
1305 (member-type-p type1
))
1308 ((eq type2
*instance-type
*)
1310 (structure-classoid type1
)
1312 (if (and (not (member type1
*non-instance-classoid-types
*
1313 :key
#'find-classoid
))
1314 (not (eq type1
(find-classoid 'function
)))
1315 (not (find (classoid-layout (find-classoid 'function
))
1316 (layout-inherits (classoid-layout type1
)))))
1320 (if (or (type-might-contain-other-types-p type1
)
1321 (member-type-p type1
))
1324 ((eq type2
*funcallable-instance-type
*)
1326 (structure-classoid *empty-type
*)
1328 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1330 (if (find (classoid-layout (find-classoid 'function
))
1331 (layout-inherits (classoid-layout type1
)))
1333 (if (type= type1
(find-classoid 'function
))
1338 (if (or (type-might-contain-other-types-p type1
)
1339 (member-type-p type1
))
1342 (t (hierarchical-intersection2 type1 type2
))))
1344 (!define-type-method
(named :complex-union2
) (type1 type2
)
1345 ;; Perhaps when bug 85 is fixed this can be reenabled.
1346 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1348 ((eq type2
*extended-sequence-type
*)
1349 (if (classoid-p type1
)
1350 (if (or (member type1
*non-instance-classoid-types
*
1351 :key
#'find-classoid
)
1352 (not (find (classoid-layout (find-classoid 'sequence
))
1353 (layout-inherits (classoid-layout type1
)))))
1357 ((eq type2
*instance-type
*)
1358 (if (classoid-p type1
)
1359 (if (or (member type1
*non-instance-classoid-types
*
1360 :key
#'find-classoid
)
1361 (find (classoid-layout (find-classoid 'function
))
1362 (layout-inherits (classoid-layout type1
))))
1366 ((eq type2
*funcallable-instance-type
*)
1367 (if (classoid-p type1
)
1368 (if (or (member type1
*non-instance-classoid-types
*
1369 :key
#'find-classoid
)
1370 (not (find (classoid-layout (find-classoid 'function
))
1371 (layout-inherits (classoid-layout type1
)))))
1373 (if (eq type1
(specifier-type 'function
))
1377 (t (hierarchical-union2 type1 type2
))))
1379 (!define-type-method
(named :negate
) (x)
1380 (aver (not (eq x
*wild-type
*)))
1382 ((eq x
*universal-type
*) *empty-type
*)
1383 ((eq x
*empty-type
*) *universal-type
*)
1384 ((or (eq x
*instance-type
*)
1385 (eq x
*funcallable-instance-type
*)
1386 (eq x
*extended-sequence-type
*))
1387 (make-negation-type :type x
))
1388 (t (bug "NAMED type unexpected: ~S" x
))))
1390 (!define-type-method
(named :unparse
) (x)
1391 (named-type-name x
))
1393 ;;;; hairy and unknown types
1395 (!define-type-method
(hairy :negate
) (x)
1396 (make-negation-type :type x
))
1398 (!define-type-method
(hairy :unparse
) (x)
1399 (hairy-type-specifier x
))
1401 (!define-type-method
(hairy :simple-subtypep
) (type1 type2
)
1402 (let ((hairy-spec1 (hairy-type-specifier type1
))
1403 (hairy-spec2 (hairy-type-specifier type2
)))
1404 (cond ((equal-but-no-car-recursion hairy-spec1 hairy-spec2
)
1406 ((maybe-reparse-specifier! type1
)
1407 (csubtypep type1 type2
))
1408 ((maybe-reparse-specifier! type2
)
1409 (csubtypep type1 type2
))
1411 (values nil nil
)))))
1413 (!define-type-method
(hairy :complex-subtypep-arg2
) (type1 type2
)
1414 (if (maybe-reparse-specifier! type2
)
1415 (csubtypep type1 type2
)
1416 (let ((specifier (hairy-type-specifier type2
)))
1417 (cond ((and (consp specifier
) (eql (car specifier
) 'satisfies
))
1418 (case (cadr specifier
)
1419 ((keywordp) (if (type= type1
(specifier-type 'symbol
))
1421 (invoke-complex-subtypep-arg1-method type1 type2
)))
1422 (t (invoke-complex-subtypep-arg1-method type1 type2
))))
1424 (invoke-complex-subtypep-arg1-method type1 type2
))))))
1426 (!define-type-method
(hairy :complex-subtypep-arg1
) (type1 type2
)
1427 (if (maybe-reparse-specifier! type1
)
1428 (csubtypep type1 type2
)
1431 (!define-type-method
(hairy :complex-
=) (type1 type2
)
1432 (if (maybe-reparse-specifier! type2
)
1436 (!define-type-method
(hairy :simple-intersection2
:complex-intersection2
)
1438 (cond ((type= type1 type2
)
1440 ((eq type2
*satisfies-keywordp-type
*)
1441 ;; (AND (MEMBER A) (SATISFIES KEYWORDP)) is possibly non-empty
1442 ;; if A is re-homed as :A. However as a special case that really
1443 ;; does occur, (AND (MEMBER NIL) (SATISFIES KEYWORDP))
1444 ;; is empty because of the illegality of changing NIL's package.
1445 (if (eq type1
*null-type
*)
1447 (multiple-value-bind (answer certain
)
1448 (types-equal-or-intersect type1
(specifier-type 'symbol
))
1449 (if (and (not answer
) certain
)
1452 ((eq type2
*fun-name-type
*)
1453 (multiple-value-bind (answer certain
)
1454 (types-equal-or-intersect type1
(specifier-type 'symbol
))
1455 (if (and (not answer
) certain
)
1456 (multiple-value-bind (answer certain
)
1457 (types-equal-or-intersect type1
(specifier-type 'cons
))
1458 (if (and (not answer
) certain
)
1464 (!define-type-method
(hairy :simple-union2
)
1466 (if (type= type1 type2
)
1470 (!define-type-method
(hairy :simple-
=) (type1 type2
)
1471 (if (equal-but-no-car-recursion (hairy-type-specifier type1
)
1472 (hairy-type-specifier type2
))
1476 (!def-type-translator satisfies
(&whole whole fun
)
1477 (declare (ignore fun
))
1478 ;; Check legality of arguments.
1479 (destructuring-bind (satisfies predicate-name
) whole
1480 (declare (ignore satisfies
))
1481 (unless (symbolp predicate-name
)
1482 (error 'simple-type-error
1483 :datum predicate-name
1484 :expected-type
'symbol
1485 :format-control
"The SATISFIES predicate name is not a symbol: ~S"
1486 :format-arguments
(list predicate-name
)))
1488 (case predicate-name
1489 (keywordp *satisfies-keywordp-type
*)
1490 (legal-fun-name-p *fun-name-type
*)
1491 (t (%make-hairy-type whole
)))))
1495 (!define-type-method
(negation :negate
) (x)
1496 (negation-type-type x
))
1498 (!define-type-method
(negation :unparse
) (x)
1499 (if (type= (negation-type-type x
) (specifier-type 'cons
))
1501 `(not ,(type-specifier (negation-type-type x
)))))
1503 (!define-type-method
(negation :simple-subtypep
) (type1 type2
)
1504 (csubtypep (negation-type-type type2
) (negation-type-type type1
)))
1506 (!define-type-method
(negation :complex-subtypep-arg2
) (type1 type2
)
1507 (let* ((complement-type2 (negation-type-type type2
))
1508 (intersection2 (type-intersection2 type1
1511 ;; FIXME: if uncertain, maybe try arg1?
1512 (type= intersection2
*empty-type
*)
1513 (invoke-complex-subtypep-arg1-method type1 type2
))))
1515 (!define-type-method
(negation :complex-subtypep-arg1
) (type1 type2
)
1516 ;; "Incrementally extended heuristic algorithms tend inexorably toward the
1517 ;; incomprehensible." -- http://www.unlambda.com/~james/lambda/lambda.txt
1519 ;; You may not believe this. I couldn't either. But then I sat down
1520 ;; and drew lots of Venn diagrams. Comments involving a and b refer
1521 ;; to the call (subtypep '(not a) 'b) -- CSR, 2002-02-27.
1523 ;; (Several logical truths in this block are true as long as
1524 ;; b/=T. As of sbcl-0.7.1.28, it seems impossible to construct a
1525 ;; case with b=T where we actually reach this type method, but
1526 ;; we'll test for and exclude this case anyway, since future
1527 ;; maintenance might make it possible for it to end up in this
1529 (multiple-value-bind (equal certain
)
1530 (type= type2
*universal-type
*)
1532 (return (values nil nil
)))
1534 (return (values t t
))))
1535 (let ((complement-type1 (negation-type-type type1
)))
1536 ;; Do the special cases first, in order to give us a chance if
1537 ;; subtype/supertype relationships are hairy.
1538 (multiple-value-bind (equal certain
)
1539 (type= complement-type1 type2
)
1540 ;; If a = b, ~a is not a subtype of b (unless b=T, which was
1543 (return (values nil nil
)))
1545 (return (values nil t
))))
1546 ;; KLUDGE: ANSI requires that the SUBTYPEP result between any
1547 ;; two built-in atomic type specifiers never be uncertain. This
1548 ;; is hard to do cleanly for the built-in types whose
1549 ;; definitions include (NOT FOO), i.e. CONS and RATIO. However,
1550 ;; we can do it with this hack, which uses our global knowledge
1551 ;; that our implementation of the type system uses disjoint
1552 ;; implementation types to represent disjoint sets (except when
1553 ;; types are contained in other types). (This is a KLUDGE
1554 ;; because it's fragile. Various changes in internal
1555 ;; representation in the type system could make it start
1556 ;; confidently returning incorrect results.) -- WHN 2002-03-08
1557 (unless (or (type-might-contain-other-types-p complement-type1
)
1558 (type-might-contain-other-types-p type2
))
1559 ;; Because of the way our types which don't contain other
1560 ;; types are disjoint subsets of the space of possible values,
1561 ;; (SUBTYPEP '(NOT AA) 'B)=NIL when AA and B are simple (and B
1562 ;; is not T, as checked above).
1563 (return (values nil t
)))
1564 ;; The old (TYPE= TYPE1 TYPE2) branch would never be taken, as
1565 ;; TYPE1 and TYPE2 will only be equal if they're both NOT types,
1566 ;; and then the :SIMPLE-SUBTYPEP method would be used instead.
1567 ;; But a CSUBTYPEP relationship might still hold:
1568 (multiple-value-bind (equal certain
)
1569 (csubtypep complement-type1 type2
)
1570 ;; If a is a subtype of b, ~a is not a subtype of b (unless
1571 ;; b=T, which was excluded above).
1573 (return (values nil nil
)))
1575 (return (values nil t
))))
1576 (multiple-value-bind (equal certain
)
1577 (csubtypep type2 complement-type1
)
1578 ;; If b is a subtype of a, ~a is not a subtype of b. (FIXME:
1579 ;; That's not true if a=T. Do we know at this point that a is
1582 (return (values nil nil
)))
1584 (return (values nil t
))))
1585 ;; old CSR comment ca. 0.7.2, now obsoleted by the SIMPLE-CTYPE?
1586 ;; KLUDGE case above: Other cases here would rely on being able
1587 ;; to catch all possible cases, which the fragility of this type
1588 ;; system doesn't inspire me; for instance, if a is type= to ~b,
1589 ;; then we want T, T; if this is not the case and the types are
1590 ;; disjoint (have an intersection of *empty-type*) then we want
1591 ;; NIL, T; else if the union of a and b is the *universal-type*
1592 ;; then we want T, T. So currently we still claim to be unsure
1593 ;; about e.g. (subtypep '(not fixnum) 'single-float).
1595 ;; OTOH we might still get here:
1598 (!define-type-method
(negation :complex-
=) (type1 type2
)
1599 ;; (NOT FOO) isn't equivalent to anything that's not a negation
1600 ;; type, except possibly a type that might contain it in disguise.
1601 (declare (ignore type2
))
1602 (if (type-might-contain-other-types-p type1
)
1606 (!define-type-method
(negation :simple-intersection2
) (type1 type2
)
1607 (let ((not1 (negation-type-type type1
))
1608 (not2 (negation-type-type type2
)))
1610 ((csubtypep not1 not2
) type2
)
1611 ((csubtypep not2 not1
) type1
)
1612 ;; Why no analagous clause to the disjoint in the SIMPLE-UNION2
1613 ;; method, below? The clause would read
1615 ;; ((EQ (TYPE-UNION NOT1 NOT2) *UNIVERSAL-TYPE*) *EMPTY-TYPE*)
1617 ;; but with proper canonicalization of negation types, there's
1618 ;; no way of constructing two negation types with union of their
1619 ;; negations being the universal type.
1621 (aver (not (eq (type-union not1 not2
) *universal-type
*)))
1624 (defun maybe-complex-array-refinement (type1 type2
)
1625 (let* ((ntype (negation-type-type type2
))
1626 (ndims (array-type-dimensions ntype
))
1627 (ncomplexp (array-type-complexp ntype
))
1628 (nseltype (array-type-specialized-element-type ntype
))
1629 (neltype (array-type-element-type ntype
)))
1630 (if (and (eql ndims
'*) (null ncomplexp
)
1631 (eql neltype
*wild-type
*) (eql nseltype
*wild-type
*))
1632 (make-array-type (array-type-dimensions type1
)
1634 :element-type
(array-type-element-type type1
)
1635 :specialized-element-type
(array-type-specialized-element-type type1
)))))
1637 (!define-type-method
(negation :complex-intersection2
) (type1 type2
)
1639 ((csubtypep type1
(negation-type-type type2
)) *empty-type
*)
1640 ((eq (type-intersection type1
(negation-type-type type2
)) *empty-type
*)
1642 ((and (array-type-p type1
) (array-type-p (negation-type-type type2
)))
1643 (maybe-complex-array-refinement type1 type2
))
1646 (!define-type-method
(negation :simple-union2
) (type1 type2
)
1647 (let ((not1 (negation-type-type type1
))
1648 (not2 (negation-type-type type2
)))
1650 ((csubtypep not1 not2
) type1
)
1651 ((csubtypep not2 not1
) type2
)
1652 ((eq (type-intersection not1 not2
) *empty-type
*)
1656 (!define-type-method
(negation :complex-union2
) (type1 type2
)
1658 ((csubtypep (negation-type-type type2
) type1
) *universal-type
*)
1659 ((eq (type-intersection type1
(negation-type-type type2
)) *empty-type
*)
1663 (!define-type-method
(negation :simple-
=) (type1 type2
)
1664 (type= (negation-type-type type1
) (negation-type-type type2
)))
1666 (!def-type-translator not
(typespec)
1667 (type-negation (specifier-type typespec
)))
1671 (!define-type-class number
:enumerable
#'numeric-type-enumerable
1672 :might-contain-other-types nil
)
1674 (declaim (inline numeric-type-equal
))
1675 (defun numeric-type-equal (type1 type2
)
1676 (and (eq (numeric-type-class type1
) (numeric-type-class type2
))
1677 (eq (numeric-type-format type1
) (numeric-type-format type2
))
1678 (eq (numeric-type-complexp type1
) (numeric-type-complexp type2
))))
1680 (!define-type-method
(number :simple-
=) (type1 type2
)
1682 (and (numeric-type-equal type1 type2
)
1683 (equalp (numeric-type-low type1
) (numeric-type-low type2
))
1684 (equalp (numeric-type-high type1
) (numeric-type-high type2
)))
1687 (!define-type-method
(number :negate
) (type)
1688 (if (and (null (numeric-type-low type
)) (null (numeric-type-high type
)))
1689 (make-negation-type :type type
)
1692 :type
(modified-numeric-type type
:low nil
:high nil
))
1694 ((null (numeric-type-low type
))
1695 (modified-numeric-type
1697 :low
(let ((h (numeric-type-high type
)))
1698 (if (consp h
) (car h
) (list h
)))
1700 ((null (numeric-type-high type
))
1701 (modified-numeric-type
1704 :high
(let ((l (numeric-type-low type
)))
1705 (if (consp l
) (car l
) (list l
)))))
1707 (modified-numeric-type
1710 :high
(let ((l (numeric-type-low type
)))
1711 (if (consp l
) (car l
) (list l
))))
1712 (modified-numeric-type
1714 :low
(let ((h (numeric-type-high type
)))
1715 (if (consp h
) (car h
) (list h
)))
1718 (!define-type-method
(number :unparse
) (type)
1719 (let* ((complexp (numeric-type-complexp type
))
1720 (low (numeric-type-low type
))
1721 (high (numeric-type-high type
))
1722 (base (case (numeric-type-class type
)
1724 (rational 'rational
)
1725 (float (or (numeric-type-format type
) 'float
))
1728 (cond ((and (eq base
'integer
) high low
)
1729 (let ((high-count (logcount high
))
1730 (high-length (integer-length high
)))
1732 (cond ((= high
0) '(integer 0 0))
1734 ((and (= high-count high-length
)
1735 (plusp high-length
))
1736 `(unsigned-byte ,high-length
))
1738 `(mod ,(1+ high
)))))
1739 ((and (= low sb
!xc
:most-negative-fixnum
)
1740 (= high sb
!xc
:most-positive-fixnum
))
1742 ((and (= low
(lognot high
))
1743 (= high-count high-length
)
1745 `(signed-byte ,(1+ high-length
)))
1747 `(integer ,low
,high
)))))
1748 (high `(,base
,(or low
'*) ,high
))
1750 (if (and (eq base
'integer
) (= low
0))
1758 (aver (neq base
+bounds
'real
))
1759 `(complex ,base
+bounds
))
1761 (aver (eq base
+bounds
'real
))
1764 (!define-type-method
(number :singleton-p
) (type)
1765 (let ((low (numeric-type-low type
))
1766 (high (numeric-type-high type
)))
1769 (eql (numeric-type-complexp type
) :real
)
1770 (member (numeric-type-class type
) '(integer rational
1771 #-sb-xc-host float
)))
1772 (values t
(numeric-type-low type
))
1775 ;;; Return true if X is "less than or equal" to Y, taking open bounds
1776 ;;; into consideration. CLOSED is the predicate used to test the bound
1777 ;;; on a closed interval (e.g. <=), and OPEN is the predicate used on
1778 ;;; open bounds (e.g. <). Y is considered to be the outside bound, in
1779 ;;; the sense that if it is infinite (NIL), then the test succeeds,
1780 ;;; whereas if X is infinite, then the test fails (unless Y is also
1783 ;;; This is for comparing bounds of the same kind, e.g. upper and
1784 ;;; upper. Use NUMERIC-BOUND-TEST* for different kinds of bounds.
1785 (defmacro numeric-bound-test
(x y closed open
)
1790 (,closed
(car ,x
) (car ,y
))
1791 (,closed
(car ,x
) ,y
)))
1797 ;;; This is used to compare upper and lower bounds. This is different
1798 ;;; from the same-bound case:
1799 ;;; -- Since X = NIL is -infinity, whereas y = NIL is +infinity, we
1800 ;;; return true if *either* arg is NIL.
1801 ;;; -- an open inner bound is "greater" and also squeezes the interval,
1802 ;;; causing us to use the OPEN test for those cases as well.
1803 (defmacro numeric-bound-test
* (x y closed open
)
1808 (,open
(car ,x
) (car ,y
))
1809 (,open
(car ,x
) ,y
)))
1815 ;;; Return whichever of the numeric bounds X and Y is "maximal"
1816 ;;; according to the predicates CLOSED (e.g. >=) and OPEN (e.g. >).
1817 ;;; This is only meaningful for maximizing like bounds, i.e. upper and
1818 ;;; upper. If MAX-P is true, then we return NIL if X or Y is NIL,
1819 ;;; otherwise we return the other arg.
1820 (defmacro numeric-bound-max
(x y closed open max-p
)
1823 `(cond ((not ,n-x
) ,(if max-p nil n-y
))
1824 ((not ,n-y
) ,(if max-p nil n-x
))
1827 (if (,closed
(car ,n-x
) (car ,n-y
)) ,n-x
,n-y
)
1828 (if (,open
(car ,n-x
) ,n-y
) ,n-x
,n-y
)))
1831 (if (,open
(car ,n-y
) ,n-x
) ,n-y
,n-x
)
1832 (if (,closed
,n-y
,n-x
) ,n-y
,n-x
))))))
1834 (!define-type-method
(number :simple-subtypep
) (type1 type2
)
1835 (let ((class1 (numeric-type-class type1
))
1836 (class2 (numeric-type-class type2
))
1837 (complexp2 (numeric-type-complexp type2
))
1838 (format2 (numeric-type-format type2
))
1839 (low1 (numeric-type-low type1
))
1840 (high1 (numeric-type-high type1
))
1841 (low2 (numeric-type-low type2
))
1842 (high2 (numeric-type-high type2
)))
1843 ;; If one is complex and the other isn't, they are disjoint.
1844 (cond ((not (or (eq (numeric-type-complexp type1
) complexp2
)
1847 ;; If the classes are specified and different, the types are
1848 ;; disjoint unless type2 is RATIONAL and type1 is INTEGER.
1849 ;; [ or type1 is INTEGER and type2 is of the form (RATIONAL
1850 ;; X X) for integral X, but this is dealt with in the
1851 ;; canonicalization inside MAKE-NUMERIC-TYPE ]
1852 ((not (or (eq class1 class2
)
1854 (and (eq class1
'integer
) (eq class2
'rational
))))
1856 ;; If the float formats are specified and different, the types
1858 ((not (or (eq (numeric-type-format type1
) format2
)
1861 ;; Check the bounds.
1862 ((and (numeric-bound-test low1 low2
>= >)
1863 (numeric-bound-test high1 high2
<= <))
1868 (!define-superclasses number
((number)) !cold-init-forms
)
1870 ;;; If the high bound of LOW is adjacent to the low bound of HIGH,
1871 ;;; then return true, otherwise NIL.
1872 (defun numeric-types-adjacent (low high
)
1873 (let ((low-bound (numeric-type-high low
))
1874 (high-bound (numeric-type-low high
)))
1875 (cond ((not (and low-bound high-bound
)) nil
)
1876 ((and (consp low-bound
) (consp high-bound
)) nil
)
1878 (let ((low-value (car low-bound
)))
1879 (or (eql low-value high-bound
)
1881 (load-time-value (make-unportable-float
1882 :single-float-negative-zero
)))
1883 (eql high-bound
0f0
))
1884 (and (eql low-value
0f0
)
1886 (load-time-value (make-unportable-float
1887 :single-float-negative-zero
))))
1889 (load-time-value (make-unportable-float
1890 :double-float-negative-zero
)))
1891 (eql high-bound
0d0
))
1892 (and (eql low-value
0d0
)
1894 (load-time-value (make-unportable-float
1895 :double-float-negative-zero
)))))))
1897 (let ((high-value (car high-bound
)))
1898 (or (eql high-value low-bound
)
1899 (and (eql high-value
1900 (load-time-value (make-unportable-float
1901 :single-float-negative-zero
)))
1902 (eql low-bound
0f0
))
1903 (and (eql high-value
0f0
)
1905 (load-time-value (make-unportable-float
1906 :single-float-negative-zero
))))
1907 (and (eql high-value
1908 (load-time-value (make-unportable-float
1909 :double-float-negative-zero
)))
1910 (eql low-bound
0d0
))
1911 (and (eql high-value
0d0
)
1913 (load-time-value (make-unportable-float
1914 :double-float-negative-zero
)))))))
1915 ((and (eq (numeric-type-class low
) 'integer
)
1916 (eq (numeric-type-class high
) 'integer
))
1917 (eql (1+ low-bound
) high-bound
))
1921 ;;; Return a numeric type that is a supertype for both TYPE1 and TYPE2.
1923 ;;; Binding *APPROXIMATE-NUMERIC-UNIONS* to T allows merging non-adjacent
1924 ;;; numeric types, eg (OR (INTEGER 0 12) (INTEGER 20 128)) => (INTEGER 0 128),
1925 ;;; the compiler does this occasionally during type-derivation to avoid
1926 ;;; creating absurdly complex unions of numeric types.
1927 (defvar *approximate-numeric-unions
* nil
)
1929 (!define-type-method
(number :simple-union2
) (type1 type2
)
1930 (declare (type numeric-type type1 type2
))
1931 (cond ((csubtypep type1 type2
) type2
)
1932 ((csubtypep type2 type1
) type1
)
1934 (let ((class1 (numeric-type-class type1
))
1935 (format1 (numeric-type-format type1
))
1936 (complexp1 (numeric-type-complexp type1
))
1937 (class2 (numeric-type-class type2
))
1938 (format2 (numeric-type-format type2
))
1939 (complexp2 (numeric-type-complexp type2
)))
1941 ((and (eq class1 class2
)
1942 (eq format1 format2
)
1943 (eq complexp1 complexp2
)
1944 (or *approximate-numeric-unions
*
1945 (numeric-types-intersect type1 type2
)
1946 (numeric-types-adjacent type1 type2
)
1947 (numeric-types-adjacent type2 type1
)))
1952 :low
(numeric-bound-max (numeric-type-low type1
)
1953 (numeric-type-low type2
)
1955 :high
(numeric-bound-max (numeric-type-high type1
)
1956 (numeric-type-high type2
)
1958 ;; FIXME: These two clauses are almost identical, and the
1959 ;; consequents are in fact identical in every respect.
1960 ((and (eq class1
'rational
)
1961 (eq class2
'integer
)
1962 (eq format1 format2
)
1963 (eq complexp1 complexp2
)
1964 (integerp (numeric-type-low type2
))
1965 (integerp (numeric-type-high type2
))
1966 (= (numeric-type-low type2
) (numeric-type-high type2
))
1967 (or *approximate-numeric-unions
*
1968 (numeric-types-adjacent type1 type2
)
1969 (numeric-types-adjacent type2 type1
)))
1974 :low
(numeric-bound-max (numeric-type-low type1
)
1975 (numeric-type-low type2
)
1977 :high
(numeric-bound-max (numeric-type-high type1
)
1978 (numeric-type-high type2
)
1980 ((and (eq class1
'integer
)
1981 (eq class2
'rational
)
1982 (eq format1 format2
)
1983 (eq complexp1 complexp2
)
1984 (integerp (numeric-type-low type1
))
1985 (integerp (numeric-type-high type1
))
1986 (= (numeric-type-low type1
) (numeric-type-high type1
))
1987 (or *approximate-numeric-unions
*
1988 (numeric-types-adjacent type1 type2
)
1989 (numeric-types-adjacent type2 type1
)))
1994 :low
(numeric-bound-max (numeric-type-low type1
)
1995 (numeric-type-low type2
)
1997 :high
(numeric-bound-max (numeric-type-high type1
)
1998 (numeric-type-high type2
)
2004 (setf (info :type
:kind
'number
)
2005 #+sb-xc-host
:defined
#-sb-xc-host
:primitive
)
2006 (setf (info :type
:builtin
'number
)
2007 (make-numeric-type :complexp nil
)))
2009 (!def-type-translator complex
(&optional
(typespec '*))
2010 (if (eq typespec
'*)
2011 (specifier-type '(complex real
))
2012 (labels ((not-numeric ()
2013 (error "The component type for COMPLEX is not numeric: ~S"
2016 (error "The component type for COMPLEX is not a subtype of REAL: ~S"
2018 (complex1 (component-type)
2019 (unless (numeric-type-p component-type
)
2021 (when (eq (numeric-type-complexp component-type
) :complex
)
2023 (if (csubtypep component-type
(specifier-type '(eql 0)))
2025 (modified-numeric-type component-type
2026 :complexp
:complex
)))
2029 ((eq ctype
*empty-type
*) *empty-type
*)
2030 ((eq ctype
*universal-type
*) (not-real))
2031 ((typep ctype
'numeric-type
) (complex1 ctype
))
2032 ((typep ctype
'union-type
)
2034 (mapcar #'do-complex
(union-type-types ctype
))))
2035 ((typep ctype
'member-type
)
2037 (mapcar-member-type-members
2038 (lambda (x) (do-complex (ctype-of x
)))
2040 ((and (typep ctype
'intersection-type
)
2041 ;; FIXME: This is very much a
2042 ;; not-quite-worst-effort, but we are required to do
2043 ;; something here because of our representation of
2044 ;; RATIO as (AND RATIONAL (NOT INTEGER)): we must
2045 ;; allow users to ask about (COMPLEX RATIO). This
2046 ;; will of course fail to work right on such types
2047 ;; as (AND INTEGER (SATISFIES ZEROP))...
2048 (let ((numbers (remove-if-not
2050 (intersection-type-types ctype
))))
2052 (null (cdr numbers
))
2053 (eq (numeric-type-complexp (car numbers
)) :real
)
2054 (complex1 (car numbers
))))))
2056 (multiple-value-bind (subtypep certainly
)
2057 (csubtypep ctype
(specifier-type 'real
))
2058 (if (and (not subtypep
) certainly
)
2060 ;; ANSI just says that TYPESPEC is any subtype of
2061 ;; type REAL, not necessarily a NUMERIC-TYPE. In
2062 ;; particular, at this point TYPESPEC could legally
2063 ;; be a hairy type like (AND NUMBER (SATISFIES
2064 ;; REALP) (SATISFIES ZEROP)), in which case we fall
2065 ;; through the logic above and end up here,
2067 (bug "~@<(known bug #145): The type ~S is too hairy to be ~
2068 used for a COMPLEX component.~:@>"
2070 (let ((ctype (specifier-type typespec
)))
2071 (do-complex ctype
)))))
2073 ;;; If X is *, return NIL, otherwise return the bound, which must be a
2074 ;;; member of TYPE or a one-element list of a member of TYPE.
2075 #!-sb-fluid
(declaim (inline canonicalized-bound
))
2076 (defun canonicalized-bound (bound type
)
2077 (cond ((eq bound
'*) nil
)
2078 ((or (sb!xc
:typep bound type
)
2080 (sb!xc
:typep
(car bound
) type
)
2081 (null (cdr bound
))))
2084 (error "Bound is not ~S, a ~S or a list of a ~S: ~S"
2090 (!def-type-translator integer
(&optional
(low '*) (high '*))
2091 (let* ((l (canonicalized-bound low
'integer
))
2092 (lb (if (consp l
) (1+ (car l
)) l
))
2093 (h (canonicalized-bound high
'integer
))
2094 (hb (if (consp h
) (1- (car h
)) h
)))
2095 (if (and hb lb
(< hb lb
))
2097 (make-numeric-type :class
'integer
2099 :enumerable
(not (null (and l h
)))
2103 (defmacro !def-bounded-type
(type class format
)
2104 `(!def-type-translator
,type
(&optional
(low '*) (high '*))
2105 (let ((lb (canonicalized-bound low
',type
))
2106 (hb (canonicalized-bound high
',type
)))
2107 (if (not (numeric-bound-test* lb hb
<= <))
2109 (make-numeric-type :class
',class
2114 (!def-bounded-type rational rational nil
)
2116 ;;; Unlike CMU CL, we represent the types FLOAT and REAL as
2117 ;;; UNION-TYPEs of more primitive types, in order to make
2118 ;;; type representation more unique, avoiding problems in the
2119 ;;; simplification of things like
2120 ;;; (subtypep '(or (single-float -1.0 1.0) (single-float 0.1))
2121 ;;; '(or (real -1 7) (single-float 0.1) (single-float -1.0 1.0)))
2122 ;;; When we allowed REAL to remain as a separate NUMERIC-TYPE,
2123 ;;; it was too easy for the first argument to be simplified to
2124 ;;; '(SINGLE-FLOAT -1.0), and for the second argument to be simplified
2125 ;;; to '(OR (REAL -1 7) (SINGLE-FLOAT 0.1)) and then for the
2126 ;;; SUBTYPEP to fail (returning NIL,T instead of T,T) because
2127 ;;; the first argument can't be seen to be a subtype of any of the
2128 ;;; terms in the second argument.
2130 ;;; The old CMU CL way was:
2131 ;;; (!def-bounded-type float float nil)
2132 ;;; (!def-bounded-type real nil nil)
2134 ;;; FIXME: If this new way works for a while with no weird new
2135 ;;; problems, we can go back and rip out support for separate FLOAT
2136 ;;; and REAL flavors of NUMERIC-TYPE. The new way was added in
2137 ;;; sbcl-0.6.11.22, 2001-03-21.
2139 ;;; FIXME: It's probably necessary to do something to fix the
2140 ;;; analogous problem with INTEGER and RATIONAL types. Perhaps
2141 ;;; bounded RATIONAL types should be represented as (OR RATIO INTEGER).
2142 (defun coerce-bound (bound type upperp inner-coerce-bound-fun
)
2143 (declare (type function inner-coerce-bound-fun
))
2146 (funcall inner-coerce-bound-fun bound type upperp
)))
2147 (defun inner-coerce-real-bound (bound type upperp
)
2148 #+sb-xc-host
(declare (ignore upperp
))
2149 (let #+sb-xc-host
()
2151 ((nl (load-time-value (symbol-value 'sb
!xc
:most-negative-long-float
)))
2152 (pl (load-time-value (symbol-value 'sb
!xc
:most-positive-long-float
))))
2153 (let ((nbound (if (consp bound
) (car bound
) bound
))
2154 (consp (consp bound
)))
2158 (list (rational nbound
))
2162 ((floatp nbound
) bound
)
2164 ;; Coerce to the widest float format available, to avoid
2165 ;; unnecessary loss of precision, but don't coerce
2166 ;; unrepresentable numbers, except on the host where we
2167 ;; shouldn't be making these types (but KLUDGE: can't even
2168 ;; assert portably that we're not).
2172 (when (< nbound nl
) (return-from inner-coerce-real-bound nl
)))
2174 (when (> nbound pl
) (return-from inner-coerce-real-bound pl
))))
2175 (let ((result (coerce nbound
'long-float
)))
2176 (if consp
(list result
) result
)))))))))
2177 (defun inner-coerce-float-bound (bound type upperp
)
2178 #+sb-xc-host
(declare (ignore upperp
))
2179 (let #+sb-xc-host
()
2181 ((nd (load-time-value (symbol-value 'sb
!xc
:most-negative-double-float
)))
2182 (pd (load-time-value (symbol-value 'sb
!xc
:most-positive-double-float
)))
2183 (ns (load-time-value (symbol-value 'sb
!xc
:most-negative-single-float
)))
2184 (ps (load-time-value
2185 (symbol-value 'sb
!xc
:most-positive-single-float
))))
2186 (let ((nbound (if (consp bound
) (car bound
) bound
))
2187 (consp (consp bound
)))
2191 ((typep nbound
'single-float
) bound
)
2196 (when (< nbound ns
) (return-from inner-coerce-float-bound ns
)))
2198 (when (> nbound ps
) (return-from inner-coerce-float-bound ps
))))
2199 (let ((result (coerce nbound
'single-float
)))
2200 (if consp
(list result
) result
)))))
2203 ((typep nbound
'double-float
) bound
)
2208 (when (< nbound nd
) (return-from inner-coerce-float-bound nd
)))
2210 (when (> nbound pd
) (return-from inner-coerce-float-bound pd
))))
2211 (let ((result (coerce nbound
'double-float
)))
2212 (if consp
(list result
) result
)))))))))
2213 (defun coerced-real-bound (bound type upperp
)
2214 (coerce-bound bound type upperp
#'inner-coerce-real-bound
))
2215 (defun coerced-float-bound (bound type upperp
)
2216 (coerce-bound bound type upperp
#'inner-coerce-float-bound
))
2217 (!def-type-translator real
(&optional
(low '*) (high '*))
2218 (specifier-type `(or (float ,(coerced-real-bound low
'float nil
)
2219 ,(coerced-real-bound high
'float t
))
2220 (rational ,(coerced-real-bound low
'rational nil
)
2221 ,(coerced-real-bound high
'rational t
)))))
2222 (!def-type-translator float
(&optional
(low '*) (high '*))
2224 `(or (single-float ,(coerced-float-bound low
'single-float nil
)
2225 ,(coerced-float-bound high
'single-float t
))
2226 (double-float ,(coerced-float-bound low
'double-float nil
)
2227 ,(coerced-float-bound high
'double-float t
))
2228 #!+long-float
,(error "stub: no long float support yet"))))
2230 (defmacro !define-float-format
(f)
2231 `(!def-bounded-type
,f float
,f
))
2233 (!define-float-format short-float
)
2234 (!define-float-format single-float
)
2235 (!define-float-format double-float
)
2236 (!define-float-format long-float
)
2238 (defun numeric-types-intersect (type1 type2
)
2239 (declare (type numeric-type type1 type2
))
2240 (let* ((class1 (numeric-type-class type1
))
2241 (class2 (numeric-type-class type2
))
2242 (complexp1 (numeric-type-complexp type1
))
2243 (complexp2 (numeric-type-complexp type2
))
2244 (format1 (numeric-type-format type1
))
2245 (format2 (numeric-type-format type2
))
2246 (low1 (numeric-type-low type1
))
2247 (high1 (numeric-type-high type1
))
2248 (low2 (numeric-type-low type2
))
2249 (high2 (numeric-type-high type2
)))
2250 ;; If one is complex and the other isn't, then they are disjoint.
2251 (cond ((not (or (eq complexp1 complexp2
)
2252 (null complexp1
) (null complexp2
)))
2254 ;; If either type is a float, then the other must either be
2255 ;; specified to be a float or unspecified. Otherwise, they
2257 ((and (eq class1
'float
)
2258 (not (member class2
'(float nil
)))) nil
)
2259 ((and (eq class2
'float
)
2260 (not (member class1
'(float nil
)))) nil
)
2261 ;; If the float formats are specified and different, the
2262 ;; types are disjoint.
2263 ((not (or (eq format1 format2
) (null format1
) (null format2
)))
2266 ;; Check the bounds. This is a bit odd because we must
2267 ;; always have the outer bound of the interval as the
2269 (if (numeric-bound-test high1 high2
<= <)
2270 (or (and (numeric-bound-test low1 low2
>= >)
2271 (numeric-bound-test* low1 high2
<= <))
2272 (and (numeric-bound-test low2 low1
>= >)
2273 (numeric-bound-test* low2 high1
<= <)))
2274 (or (and (numeric-bound-test* low2 high1
<= <)
2275 (numeric-bound-test low2 low1
>= >))
2276 (and (numeric-bound-test high2 high1
<= <)
2277 (numeric-bound-test* high2 low1
>= >))))))))
2279 ;;; Take the numeric bound X and convert it into something that can be
2280 ;;; used as a bound in a numeric type with the specified CLASS and
2281 ;;; FORMAT. If UP-P is true, then we round up as needed, otherwise we
2282 ;;; round down. UP-P true implies that X is a lower bound, i.e. (N) > N.
2284 ;;; This is used by NUMERIC-TYPE-INTERSECTION to mash the bound into
2285 ;;; the appropriate type number. X may only be a float when CLASS is
2288 ;;; ### Note: it is possible for the coercion to a float to overflow
2289 ;;; or underflow. This happens when the bound doesn't fit in the
2290 ;;; specified format. In this case, we should really return the
2291 ;;; appropriate {Most | Least}-{Positive | Negative}-XXX-Float float
2292 ;;; of desired format. But these conditions aren't currently signalled
2293 ;;; in any useful way.
2295 ;;; Also, when converting an open rational bound into a float we
2296 ;;; should probably convert it to a closed bound of the closest float
2297 ;;; in the specified format. KLUDGE: In general, open float bounds are
2298 ;;; screwed up. -- (comment from original CMU CL)
2299 (defun round-numeric-bound (x class format up-p
)
2301 (let ((cx (if (consp x
) (car x
) x
)))
2305 (if (and (consp x
) (integerp cx
))
2306 (if up-p
(1+ cx
) (1- cx
))
2307 (if up-p
(ceiling cx
) (floor cx
))))
2311 ((and format
(subtypep format
'double-float
))
2312 (if (<= most-negative-double-float cx most-positive-double-float
)
2316 (if (<= most-negative-single-float cx most-positive-single-float
)
2318 (coerce cx
(or format
'single-float
))
2320 (if (consp x
) (list res
) res
)))))
2323 ;;; Handle the case of type intersection on two numeric types. We use
2324 ;;; TYPES-EQUAL-OR-INTERSECT to throw out the case of types with no
2325 ;;; intersection. If an attribute in TYPE1 is unspecified, then we use
2326 ;;; TYPE2's attribute, which must be at least as restrictive. If the
2327 ;;; types intersect, then the only attributes that can be specified
2328 ;;; and different are the class and the bounds.
2330 ;;; When the class differs, we use the more restrictive class. The
2331 ;;; only interesting case is RATIONAL/INTEGER, since RATIONAL includes
2334 ;;; We make the result lower (upper) bound the maximum (minimum) of
2335 ;;; the argument lower (upper) bounds. We convert the bounds into the
2336 ;;; appropriate numeric type before maximizing. This avoids possible
2337 ;;; confusion due to mixed-type comparisons (but I think the result is
2339 (!define-type-method
(number :simple-intersection2
) (type1 type2
)
2340 (declare (type numeric-type type1 type2
))
2341 (if (numeric-types-intersect type1 type2
)
2342 (let* ((class1 (numeric-type-class type1
))
2343 (class2 (numeric-type-class type2
))
2344 (class (ecase class1
2346 ((integer float
) class1
)
2347 (rational (if (eq class2
'integer
)
2350 (format (or (numeric-type-format type1
)
2351 (numeric-type-format type2
))))
2355 :complexp
(or (numeric-type-complexp type1
)
2356 (numeric-type-complexp type2
))
2357 :low
(numeric-bound-max
2358 (round-numeric-bound (numeric-type-low type1
)
2360 (round-numeric-bound (numeric-type-low type2
)
2363 :high
(numeric-bound-max
2364 (round-numeric-bound (numeric-type-high type1
)
2366 (round-numeric-bound (numeric-type-high type2
)
2371 ;;; Given two float formats, return the one with more precision. If
2372 ;;; either one is null, return NIL.
2373 (defun float-format-max (f1 f2
)
2375 (dolist (f *float-formats
* (error "bad float format: ~S" f1
))
2376 (when (or (eq f f1
) (eq f f2
))
2379 ;;; Return the result of an operation on TYPE1 and TYPE2 according to
2380 ;;; the rules of numeric contagion. This is always NUMBER, some float
2381 ;;; format (possibly complex) or RATIONAL. Due to rational
2382 ;;; canonicalization, there isn't much we can do here with integers or
2383 ;;; rational complex numbers.
2385 ;;; If either argument is not a NUMERIC-TYPE, then return NUMBER. This
2386 ;;; is useful mainly for allowing types that are technically numbers,
2387 ;;; but not a NUMERIC-TYPE.
2388 (defun numeric-contagion (type1 type2
)
2389 (if (and (numeric-type-p type1
) (numeric-type-p type2
))
2390 (let ((class1 (numeric-type-class type1
))
2391 (class2 (numeric-type-class type2
))
2392 (format1 (numeric-type-format type1
))
2393 (format2 (numeric-type-format type2
))
2394 (complexp1 (numeric-type-complexp type1
))
2395 (complexp2 (numeric-type-complexp type2
)))
2396 (cond ((or (null complexp1
)
2398 (specifier-type 'number
))
2402 :format
(ecase class2
2403 (float (float-format-max format1 format2
))
2404 ((integer rational
) format1
)
2406 ;; A double-float with any real number is a
2409 (if (eq format1
'double-float
)
2412 ;; A long-float with any real number is a
2415 (if (eq format1
'long-float
)
2418 :complexp
(if (or (eq complexp1
:complex
)
2419 (eq complexp2
:complex
))
2422 ((eq class2
'float
) (numeric-contagion type2 type1
))
2423 ((and (eq complexp1
:real
) (eq complexp2
:real
))
2425 :class
(and class1 class2
'rational
)
2428 (specifier-type 'number
))))
2429 (specifier-type 'number
)))
2433 (!define-type-class array
:enumerable nil
2434 :might-contain-other-types nil
)
2436 (!define-type-method
(array :simple-
=) (type1 type2
)
2437 (cond ((not (and (equal (array-type-dimensions type1
)
2438 (array-type-dimensions type2
))
2439 (eq (array-type-complexp type1
)
2440 (array-type-complexp type2
))))
2442 ((or (unknown-type-p (array-type-element-type type1
))
2443 (unknown-type-p (array-type-element-type type2
)))
2444 (type= (array-type-element-type type1
)
2445 (array-type-element-type type2
)))
2447 (values (type= (array-type-specialized-element-type type1
)
2448 (array-type-specialized-element-type type2
))
2451 (!define-type-method
(array :negate
) (type)
2452 ;; FIXME (and hint to PFD): we're vulnerable here to attacks of the
2453 ;; form "are (AND ARRAY (NOT (ARRAY T))) and (OR (ARRAY BIT) (ARRAY
2454 ;; NIL) (ARRAY CHAR) ...) equivalent?" -- CSR, 2003-12-10
2455 ;; A symptom of the aforementioned is that the following are not TYPE=
2456 ;; (AND (VECTOR T) (NOT SIMPLE-ARRAY)) ; an ARRAY-TYPE
2457 ;; (AND (VECTOR T) (NOT SIMPLE-VECTOR)) ; an INTERSECTION-TYPE
2458 ;; even though (VECTOR T) makes it so that the (NOT) clause in each can
2459 ;; only provide one additional bit of information: that the vector
2460 ;; is complex as opposed to simple. The rank and element-type are fixed.
2461 (if (and (eq (array-type-dimensions type
) '*)
2462 (eq (array-type-complexp type
) 't
)
2463 (eq (array-type-element-type type
) *wild-type
*))
2464 ;; (NOT <hairy-array>) = either SIMPLE-ARRAY or (NOT ARRAY).
2465 ;; This is deliberately asymmetric - trying to say that NOT simple-array
2466 ;; equals hairy-array leads to infinite recursion.
2467 (type-union (make-array-type '* :complexp nil
2468 :element-type
*wild-type
*)
2470 :type
(make-array-type '* :element-type
*wild-type
*)))
2471 (make-negation-type :type type
)))
2473 (!define-type-method
(array :unparse
) (type)
2474 (let* ((dims (array-type-dimensions type
))
2475 ;; Compare the specialised element type and the
2476 ;; derived element type. If the derived type
2477 ;; is so small that it jumps to a smaller upgraded
2478 ;; element type, use the specialised element type.
2480 ;; This protects from unparsing
2481 ;; (and (vector (or bit symbol))
2482 ;; (vector (or bit character)))
2483 ;; i.e., the intersection of two T array types,
2485 (stype (array-type-specialized-element-type type
))
2486 (dtype (array-type-element-type type
))
2487 (utype (%upgraded-array-element-type dtype
))
2488 (eltype (type-specifier (if (type= stype utype
)
2491 (complexp (array-type-complexp type
)))
2492 (if (and (eq complexp t
) (not *unparse-allow-negation
*))
2493 (setq complexp
:maybe
))
2497 ((t) '(and array
(not simple-array
)))
2499 ((nil) 'simple-array
))
2501 ((t) `(and (array ,eltype
) (not simple-array
)))
2502 ((:maybe
) `(array ,eltype
))
2503 ((nil) `(simple-array ,eltype
)))))
2504 ((= (length dims
) 1)
2507 (if (eq (car dims
) '*)
2510 ((base-char #!-sb-unicode character
) 'base-string
)
2512 (t `(vector ,eltype
)))
2514 (bit `(bit-vector ,(car dims
)))
2515 ((base-char #!-sb-unicode character
)
2516 `(base-string ,(car dims
)))
2517 (t `(vector ,eltype
,(car dims
)))))))
2518 (if (eql complexp
:maybe
)
2520 `(and ,answer
(not simple-array
))))
2521 (if (eq (car dims
) '*)
2523 (bit 'simple-bit-vector
)
2524 ((base-char #!-sb-unicode character
) 'simple-base-string
)
2525 ((t) 'simple-vector
)
2526 (t `(simple-array ,eltype
(*))))
2528 (bit `(simple-bit-vector ,(car dims
)))
2529 ((base-char #!-sb-unicode character
)
2530 `(simple-base-string ,(car dims
)))
2531 ((t) `(simple-vector ,(car dims
)))
2532 (t `(simple-array ,eltype
,dims
))))))
2535 ((t) `(and (array ,eltype
,dims
) (not simple-array
)))
2536 ((:maybe
) `(array ,eltype
,dims
))
2537 ((nil) `(simple-array ,eltype
,dims
)))))))
2539 (!define-type-method
(array :simple-subtypep
) (type1 type2
)
2540 (let ((dims1 (array-type-dimensions type1
))
2541 (dims2 (array-type-dimensions type2
))
2542 (complexp2 (array-type-complexp type2
)))
2543 (cond (;; not subtypep unless dimensions are compatible
2544 (not (or (eq dims2
'*)
2545 (and (not (eq dims1
'*))
2546 ;; (sbcl-0.6.4 has trouble figuring out that
2547 ;; DIMS1 and DIMS2 must be lists at this
2548 ;; point, and knowing that is important to
2549 ;; compiling EVERY efficiently.)
2550 (= (length (the list dims1
))
2551 (length (the list dims2
)))
2552 (every (lambda (x y
)
2553 (or (eq y
'*) (eql x y
)))
2555 (the list dims2
)))))
2557 ;; not subtypep unless complexness is compatible
2558 ((not (or (eq complexp2
:maybe
)
2559 (eq (array-type-complexp type1
) complexp2
)))
2561 ;; Since we didn't fail any of the tests above, we win
2562 ;; if the TYPE2 element type is wild.
2563 ((eq (array-type-element-type type2
) *wild-type
*)
2565 (;; Since we didn't match any of the special cases above, if
2566 ;; either element type is unknown we can only give a good
2567 ;; answer if they are the same.
2568 (or (unknown-type-p (array-type-element-type type1
))
2569 (unknown-type-p (array-type-element-type type2
)))
2570 (if (type= (array-type-element-type type1
)
2571 (array-type-element-type type2
))
2574 (;; Otherwise, the subtype relationship holds iff the
2575 ;; types are equal, and they're equal iff the specialized
2576 ;; element types are identical.
2578 (values (type= (array-type-specialized-element-type type1
)
2579 (array-type-specialized-element-type type2
))
2582 (!define-superclasses array
2583 ((vector vector
) (array))
2586 (defun array-types-intersect (type1 type2
)
2587 (declare (type array-type type1 type2
))
2588 (let ((dims1 (array-type-dimensions type1
))
2589 (dims2 (array-type-dimensions type2
))
2590 (complexp1 (array-type-complexp type1
))
2591 (complexp2 (array-type-complexp type2
)))
2592 ;; See whether dimensions are compatible.
2593 (cond ((not (or (eq dims1
'*) (eq dims2
'*)
2594 (and (= (length dims1
) (length dims2
))
2595 (every (lambda (x y
)
2596 (or (eq x
'*) (eq y
'*) (= x y
)))
2599 ;; See whether complexpness is compatible.
2600 ((not (or (eq complexp1
:maybe
)
2601 (eq complexp2
:maybe
)
2602 (eq complexp1 complexp2
)))
2606 ;; If either element type is wild, then they intersect.
2607 ;; Otherwise, the types must be identical.
2609 ;; FIXME: There seems to have been a fair amount of
2610 ;; confusion about the distinction between requested element
2611 ;; type and specialized element type; here is one of
2612 ;; them. If we request an array to hold objects of an
2613 ;; unknown type, we can do no better than represent that
2614 ;; type as an array specialized on wild-type. We keep the
2615 ;; requested element-type in the -ELEMENT-TYPE slot, and
2616 ;; *WILD-TYPE* in the -SPECIALIZED-ELEMENT-TYPE. So, here,
2617 ;; we must test for the SPECIALIZED slot being *WILD-TYPE*,
2618 ;; not just the ELEMENT-TYPE slot. Maybe the return value
2619 ;; in that specific case should be T, NIL? Or maybe this
2620 ;; function should really be called
2621 ;; ARRAY-TYPES-COULD-POSSIBLY-INTERSECT? In any case, this
2622 ;; was responsible for bug #123, and this whole issue could
2623 ;; do with a rethink and/or a rewrite. -- CSR, 2002-08-21
2624 ((or (eq (array-type-specialized-element-type type1
) *wild-type
*)
2625 (eq (array-type-specialized-element-type type2
) *wild-type
*)
2626 (type= (array-type-specialized-element-type type1
)
2627 (array-type-specialized-element-type type2
)))
2633 (defun unite-array-types-complexp (type1 type2
)
2634 (let ((complexp1 (array-type-complexp type1
))
2635 (complexp2 (array-type-complexp type2
)))
2637 ((eq complexp1 complexp2
)
2638 ;; both types are the same complexp-ity
2639 (values complexp1 t
))
2640 ((eq complexp1
:maybe
)
2641 ;; type1 is wild-complexp
2642 (values :maybe type1
))
2643 ((eq complexp2
:maybe
)
2644 ;; type2 is wild-complexp
2645 (values :maybe type2
))
2647 ;; both types partition the complexp-space
2648 (values :maybe nil
)))))
2650 (defun unite-array-types-dimensions (type1 type2
)
2651 (let ((dims1 (array-type-dimensions type1
))
2652 (dims2 (array-type-dimensions type2
)))
2653 (cond ((equal dims1 dims2
)
2654 ;; both types are same dimensionality
2657 ;; type1 is wild-dimensions
2660 ;; type2 is wild-dimensions
2662 ((not (= (length dims1
) (length dims2
)))
2663 ;; types have different number of dimensions
2664 (values :incompatible nil
))
2666 ;; we need to check on a per-dimension basis
2667 (let* ((supertype1 t
)
2670 (result (mapcar (lambda (dim1 dim2
)
2675 (setf supertype2 nil
)
2678 (setf supertype1 nil
)
2681 (setf compatible nil
))))
2684 ((or (not compatible
)
2685 (and (not supertype1
)
2687 (values :incompatible nil
))
2688 ((and supertype1 supertype2
)
2689 (values result supertype1
))
2691 (values result
(if supertype1 type1 type2
)))))))))
2693 (defun unite-array-types-element-types (type1 type2
)
2694 ;; FIXME: We'd love to be able to unite the full set of specialized
2695 ;; array element types up to *wild-type*, but :simple-union2 is
2696 ;; performed pairwise, so we don't have a good hook for it and our
2697 ;; representation doesn't allow us to easily detect the situation
2699 ;; But see SIMPLIFY-ARRAY-UNIONS which is able to do something like that.
2700 (let* ((eltype1 (array-type-element-type type1
))
2701 (eltype2 (array-type-element-type type2
))
2702 (stype1 (array-type-specialized-element-type type1
))
2703 (stype2 (array-type-specialized-element-type type2
))
2704 (wild1 (eq eltype1
*wild-type
*))
2705 (wild2 (eq eltype2
*wild-type
*)))
2707 ((type= eltype1 eltype2
)
2708 (values eltype1 stype1 t
))
2710 (values eltype1 stype1 type1
))
2712 (values eltype2 stype2 type2
))
2713 ((not (type= stype1 stype2
))
2714 ;; non-wild types that don't share UAET don't unite
2715 (values :incompatible nil nil
))
2716 ((csubtypep eltype1 eltype2
)
2717 (values eltype2 stype2 type2
))
2718 ((csubtypep eltype2 eltype1
)
2719 (values eltype1 stype1 type1
))
2721 (values :incompatible nil nil
)))))
2723 (defun unite-array-types-supertypes-compatible-p (&rest supertypes
)
2724 ;; supertypes are compatible if they are all T, if there is a single
2725 ;; NIL and all the rest are T, or if all non-T supertypes are the
2726 ;; same and not NIL.
2727 (let ((interesting-supertypes
2728 (remove t supertypes
)))
2729 (or (not interesting-supertypes
)
2730 (equal interesting-supertypes
'(nil))
2731 ;; supertypes are (OR BOOLEAN ARRAY-TYPE), so...
2732 (typep (remove-duplicates interesting-supertypes
)
2733 '(cons array-type null
)))))
2735 (!define-type-method
(array :simple-union2
) (type1 type2
)
2736 (multiple-value-bind
2737 (result-eltype result-stype eltype-supertype
)
2738 (unite-array-types-element-types type1 type2
)
2739 (multiple-value-bind
2740 (result-complexp complexp-supertype
)
2741 (unite-array-types-complexp type1 type2
)
2742 (multiple-value-bind
2743 (result-dimensions dimensions-supertype
)
2744 (unite-array-types-dimensions type1 type2
)
2745 (when (and (not (eq result-dimensions
:incompatible
))
2746 (not (eq result-eltype
:incompatible
))
2747 (unite-array-types-supertypes-compatible-p
2748 eltype-supertype complexp-supertype dimensions-supertype
))
2749 (make-array-type result-dimensions
2750 :complexp result-complexp
2751 :element-type result-eltype
2752 :specialized-element-type result-stype
))))))
2754 (!define-type-method
(array :simple-intersection2
) (type1 type2
)
2755 (declare (type array-type type1 type2
))
2756 (if (array-types-intersect type1 type2
)
2757 (let ((dims1 (array-type-dimensions type1
))
2758 (dims2 (array-type-dimensions type2
))
2759 (complexp1 (array-type-complexp type1
))
2760 (complexp2 (array-type-complexp type2
))
2761 (eltype1 (array-type-element-type type1
))
2762 (eltype2 (array-type-element-type type2
))
2763 (stype1 (array-type-specialized-element-type type1
))
2764 (stype2 (array-type-specialized-element-type type2
)))
2765 (make-array-type (cond ((eq dims1
'*) dims2
)
2766 ((eq dims2
'*) dims1
)
2768 (mapcar (lambda (x y
) (if (eq x
'*) y x
))
2770 :complexp
(if (eq complexp1
:maybe
) complexp2 complexp1
)
2772 ((eq eltype1
*wild-type
*) eltype2
)
2773 ((eq eltype2
*wild-type
*) eltype1
)
2774 (t (type-intersection eltype1 eltype2
)))
2775 :specialized-element-type
(cond
2776 ((eq stype1
*wild-type
*) stype2
)
2777 ((eq stype2
*wild-type
*) stype1
)
2779 (aver (type= stype1 stype2
))
2783 ;;; Check a supplied dimension list to determine whether it is legal,
2784 ;;; and return it in canonical form (as either '* or a list).
2785 (defun canonical-array-dimensions (dims)
2790 (error "Arrays can't have a negative number of dimensions: ~S" dims
))
2791 (when (>= dims sb
!xc
:array-rank-limit
)
2792 (error "array type with too many dimensions: ~S" dims
))
2793 (make-list dims
:initial-element
'*))
2795 (when (>= (length dims
) sb
!xc
:array-rank-limit
)
2796 (error "array type with too many dimensions: ~S" dims
))
2799 (unless (and (integerp dim
)
2801 (< dim sb
!xc
:array-dimension-limit
))
2802 (error "bad dimension in array type: ~S" dim
))))
2805 (error "Array dimensions is not a list, integer or *:~% ~S" dims
))))
2809 (!define-type-class member
:enumerable t
2810 :might-contain-other-types nil
)
2812 ;; this is ridiculously order-sensitive: the DEFSTRUCT is in 'early-type'
2813 ;; as is MAKE-MEMBER-TYPE, the only user of *NULL-TYPE*.
2814 ;; But the type-class is here, and you can't make a CTYPE object
2815 ;; until a type-class exists for it. Type-classes are akin to layouts,
2816 ;; and ought to be as primordial, and dumped during Genesis.
2817 ;; I have a patch to do exactly that, but until then...
2819 (setf *null-type
* (%make-member-type
(xset-from-list '(nil)) nil
)
2820 *boolean-type
* (%make-member-type
(xset-from-list '(t nil
)) nil
)))
2822 (!define-type-method
(member :negate
) (type)
2823 (let ((xset (member-type-xset type
))
2824 (fp-zeroes (member-type-fp-zeroes type
)))
2826 ;; Hairy case, which needs to do a bit of float type
2827 ;; canonicalization.
2828 (apply #'type-intersection
2829 (if (xset-empty-p xset
)
2832 :type
(make-member-type :xset xset
)))
2835 (let* ((opposite (neg-fp-zero x
))
2836 (type (ctype-of opposite
)))
2839 :type
(modified-numeric-type type
:low nil
:high nil
))
2840 (modified-numeric-type type
:low nil
:high
(list opposite
))
2841 (make-member-type :members
(list opposite
))
2842 (modified-numeric-type type
:low
(list opposite
) :high nil
))))
2845 (make-negation-type :type type
))))
2847 (!define-type-method
(member :unparse
) (type)
2848 (let ((members (member-type-members type
)))
2849 (cond ((equal members
'(nil)) 'null
)
2850 (t `(member ,@members
)))))
2852 (!define-type-method
(member :singleton-p
) (type)
2853 (if (eql 1 (member-type-size type
))
2854 (values t
(first (member-type-members type
)))
2857 (!define-type-method
(member :simple-subtypep
) (type1 type2
)
2858 (values (and (xset-subset-p (member-type-xset type1
)
2859 (member-type-xset type2
))
2860 (subsetp (member-type-fp-zeroes type1
)
2861 (member-type-fp-zeroes type2
)))
2864 (!define-type-method
(member :complex-subtypep-arg1
) (type1 type2
)
2866 (mapc-member-type-members
2868 (multiple-value-bind (ok surep
) (ctypep elt type2
)
2870 (return-from punt
(values nil nil
)))
2872 (return-from punt
(values nil t
)))))
2876 ;;; We punt if the odd type is enumerable and intersects with the
2877 ;;; MEMBER type. If not enumerable, then it is definitely not a
2878 ;;; subtype of the MEMBER type.
2879 (!define-type-method
(member :complex-subtypep-arg2
) (type1 type2
)
2880 (cond ((not (type-enumerable type1
)) (values nil t
))
2881 ((types-equal-or-intersect type1 type2
)
2882 (invoke-complex-subtypep-arg1-method type1 type2
))
2883 (t (values nil t
))))
2885 (!define-type-method
(member :simple-intersection2
) (type1 type2
)
2886 (make-member-type :xset
(xset-intersection (member-type-xset type1
)
2887 (member-type-xset type2
))
2888 :fp-zeroes
(intersection (member-type-fp-zeroes type1
)
2889 (member-type-fp-zeroes type2
))))
2891 (!define-type-method
(member :complex-intersection2
) (type1 type2
)
2893 (let ((xset (alloc-xset))
2895 (mapc-member-type-members
2897 (multiple-value-bind (ok sure
) (ctypep member type1
)
2899 (return-from punt nil
))
2901 (if (fp-zero-p member
)
2902 (pushnew member fp-zeroes
)
2903 (add-to-xset member xset
)))))
2905 (if (and (xset-empty-p xset
) (not fp-zeroes
))
2907 (make-member-type :xset xset
:fp-zeroes fp-zeroes
)))))
2909 ;;; We don't need a :COMPLEX-UNION2, since the only interesting case is
2910 ;;; a union type, and the member/union interaction is handled by the
2911 ;;; union type method.
2912 (!define-type-method
(member :simple-union2
) (type1 type2
)
2913 (make-member-type :xset
(xset-union (member-type-xset type1
)
2914 (member-type-xset type2
))
2915 :fp-zeroes
(union (member-type-fp-zeroes type1
)
2916 (member-type-fp-zeroes type2
))))
2918 (!define-type-method
(member :simple-
=) (type1 type2
)
2919 (let ((xset1 (member-type-xset type1
))
2920 (xset2 (member-type-xset type2
))
2921 (l1 (member-type-fp-zeroes type1
))
2922 (l2 (member-type-fp-zeroes type2
)))
2923 (values (and (eql (xset-count xset1
) (xset-count xset2
))
2924 (xset-subset-p xset1 xset2
)
2925 (xset-subset-p xset2 xset1
)
2930 (!define-type-method
(member :complex-
=) (type1 type2
)
2931 (if (type-enumerable type1
)
2932 (multiple-value-bind (val win
) (csubtypep type2 type1
)
2933 (if (or val
(not win
))
2938 (!def-type-translator member
(&rest members
)
2940 (let (ms numbers char-codes
)
2941 (dolist (m (remove-duplicates members
))
2943 (float (if (zerop m
)
2945 (push (ctype-of m
) numbers
)))
2946 (real (push (ctype-of m
) numbers
))
2947 (character (push (sb!xc
:char-code m
) char-codes
))
2951 (make-member-type :members ms
)
2954 (make-character-set-type
2955 :pairs
(mapcar (lambda (x) (cons x x
))
2956 (sort char-codes
#'<)))
2958 (nreverse numbers
)))
2961 ;;;; intersection types
2963 ;;;; Until version 0.6.10.6, SBCL followed the original CMU CL approach
2964 ;;;; of punting on all AND types, not just the unreasonably complicated
2965 ;;;; ones. The change was motivated by trying to get the KEYWORD type
2966 ;;;; to behave sensibly:
2967 ;;;; ;; reasonable definition
2968 ;;;; (DEFTYPE KEYWORD () '(AND SYMBOL (SATISFIES KEYWORDP)))
2969 ;;;; ;; reasonable behavior
2970 ;;;; (AVER (SUBTYPEP 'KEYWORD 'SYMBOL))
2971 ;;;; Without understanding a little about the semantics of AND, we'd
2972 ;;;; get (SUBTYPEP 'KEYWORD 'SYMBOL)=>NIL,NIL and, for entirely
2973 ;;;; parallel reasons, (SUBTYPEP 'RATIO 'NUMBER)=>NIL,NIL. That's
2976 ;;;; We still follow the example of CMU CL to some extent, by punting
2977 ;;;; (to the opaque HAIRY-TYPE) on sufficiently complicated types
2980 (!define-type-class intersection
2981 :enumerable
#'compound-type-enumerable
2982 :might-contain-other-types t
)
2984 (!define-type-method
(intersection :negate
) (type)
2986 (mapcar #'type-negation
(intersection-type-types type
))))
2988 ;;; A few intersection types have special names. The others just get
2989 ;;; mechanically unparsed.
2990 (!define-type-method
(intersection :unparse
) (type)
2991 (declare (type ctype type
))
2992 (or (find type
'(ratio keyword compiled-function
) :key
#'specifier-type
:test
#'type
=)
2993 `(and ,@(mapcar #'type-specifier
(intersection-type-types type
)))))
2995 ;;; shared machinery for type equality: true if every type in the set
2996 ;;; TYPES1 matches a type in the set TYPES2 and vice versa
2997 (defun type=-set
(types1 types2
)
2998 (flet ((type<=-set
(x y
)
2999 (declare (type list x y
))
3000 (every/type
(lambda (x y-element
)
3001 (any/type
#'type
= y-element x
))
3003 (and/type
(type<=-set types1 types2
)
3004 (type<=-set types2 types1
))))
3006 ;;; Two intersection types are equal if their subtypes are equal sets.
3008 ;;; FIXME: Might it be better to use
3009 ;;; (AND (SUBTYPEP X Y) (SUBTYPEP Y X))
3010 ;;; instead, since SUBTYPEP is the usual relationship that we care
3011 ;;; most about, so it would be good to leverage any ingenuity there
3012 ;;; in this more obscure method?
3013 (!define-type-method
(intersection :simple-
=) (type1 type2
)
3014 (type=-set
(intersection-type-types type1
)
3015 (intersection-type-types type2
)))
3017 (defun %intersection-complex-subtypep-arg1
(type1 type2
)
3018 (type= type1
(type-intersection type1 type2
)))
3020 (defun %intersection-simple-subtypep
(type1 type2
)
3021 (every/type
#'%intersection-complex-subtypep-arg1
3023 (intersection-type-types type2
)))
3025 (!define-type-method
(intersection :simple-subtypep
) (type1 type2
)
3026 (%intersection-simple-subtypep type1 type2
))
3028 (!define-type-method
(intersection :complex-subtypep-arg1
) (type1 type2
)
3029 (%intersection-complex-subtypep-arg1 type1 type2
))
3031 (defun %intersection-complex-subtypep-arg2
(type1 type2
)
3032 (every/type
#'csubtypep type1
(intersection-type-types type2
)))
3034 (!define-type-method
(intersection :complex-subtypep-arg2
) (type1 type2
)
3035 (%intersection-complex-subtypep-arg2 type1 type2
))
3037 ;;; FIXME: This will look eeriely familiar to readers of the UNION
3038 ;;; :SIMPLE-INTERSECTION2 :COMPLEX-INTERSECTION2 method. That's
3039 ;;; because it was generated by cut'n'paste methods. Given that
3040 ;;; intersections and unions have all sorts of symmetries known to
3041 ;;; mathematics, it shouldn't be beyond the ken of some programmers to
3042 ;;; reflect those symmetries in code in a way that ties them together
3043 ;;; more strongly than having two independent near-copies :-/
3044 (!define-type-method
(intersection :simple-union2
:complex-union2
)
3046 ;; Within this method, type2 is guaranteed to be an intersection
3048 (aver (intersection-type-p type2
))
3049 ;; Make sure to call only the applicable methods...
3050 (cond ((and (intersection-type-p type1
)
3051 (%intersection-simple-subtypep type1 type2
)) type2
)
3052 ((and (intersection-type-p type1
)
3053 (%intersection-simple-subtypep type2 type1
)) type1
)
3054 ((and (not (intersection-type-p type1
))
3055 (%intersection-complex-subtypep-arg2 type1 type2
))
3057 ((and (not (intersection-type-p type1
))
3058 (%intersection-complex-subtypep-arg1 type2 type1
))
3060 ;; KLUDGE: This special (and somewhat hairy) magic is required
3061 ;; to deal with the RATIONAL/INTEGER special case. The UNION
3062 ;; of (INTEGER * -1) and (AND (RATIONAL * -1/2) (NOT INTEGER))
3063 ;; should be (RATIONAL * -1/2) -- CSR, 2003-02-28
3064 ((and (csubtypep type2
(specifier-type 'ratio
))
3065 (numeric-type-p type1
)
3066 (csubtypep type1
(specifier-type 'integer
))
3071 :low
(if (null (numeric-type-low type1
))
3073 (list (1- (numeric-type-low type1
))))
3074 :high
(if (null (numeric-type-high type1
))
3076 (list (1+ (numeric-type-high type1
)))))))
3077 (let* ((intersected (intersection-type-types type2
))
3078 (remaining (remove (specifier-type '(not integer
))
3081 (and (not (equal intersected remaining
))
3082 (type-union type1
(apply #'type-intersection remaining
)))))
3084 (let ((accumulator *universal-type
*))
3085 (do ((t2s (intersection-type-types type2
) (cdr t2s
)))
3086 ((null t2s
) accumulator
)
3087 (let ((union (type-union type1
(car t2s
))))
3088 (when (union-type-p union
)
3089 ;; we have to give up here -- there are all sorts of
3090 ;; ordering worries, but it's better than before.
3091 ;; Doing exactly the same as in the UNION
3092 ;; :SIMPLE/:COMPLEX-INTERSECTION2 method causes stack
3093 ;; overflow with the mutual recursion never bottoming
3095 (if (and (eq accumulator
*universal-type
*)
3097 ;; KLUDGE: if we get here, we have a partially
3098 ;; simplified result. While this isn't by any
3099 ;; means a universal simplification, including
3100 ;; this logic here means that we can get (OR
3101 ;; KEYWORD (NOT KEYWORD)) canonicalized to T.
3105 (type-intersection accumulator union
))))))))
3107 (!def-type-translator and
(&whole whole
&rest type-specifiers
)
3108 (apply #'type-intersection
3109 (mapcar #'specifier-type type-specifiers
)))
3113 (!define-type-class union
3114 :enumerable
#'compound-type-enumerable
3115 :might-contain-other-types t
)
3117 (!define-type-method
(union :negate
) (type)
3118 (declare (type ctype type
))
3119 (apply #'type-intersection
3120 (mapcar #'type-negation
(union-type-types type
))))
3122 ;;; The LIST, FLOAT and REAL types have special names. Other union
3123 ;;; types just get mechanically unparsed.
3124 (!define-type-method
(union :unparse
) (type)
3125 (declare (type ctype type
))
3127 ((type= type
(specifier-type 'list
)) 'list
)
3128 ((type= type
(specifier-type 'float
)) 'float
)
3129 ((type= type
(specifier-type 'real
)) 'real
)
3130 ((type= type
(specifier-type 'sequence
)) 'sequence
)
3131 ((type= type
(specifier-type 'bignum
)) 'bignum
)
3132 ((type= type
(specifier-type 'simple-string
)) 'simple-string
)
3133 ((type= type
(specifier-type 'string
)) 'string
)
3134 ((type= type
(specifier-type 'complex
)) 'complex
)
3135 ((type= type
(specifier-type 'standard-char
)) 'standard-char
)
3136 (t `(or ,@(mapcar #'type-specifier
(union-type-types type
))))))
3138 ;;; Two union types are equal if they are each subtypes of each
3139 ;;; other. We need to be this clever because our complex subtypep
3140 ;;; methods are now more accurate; we don't get infinite recursion
3141 ;;; because the simple-subtypep method delegates to complex-subtypep
3142 ;;; of the individual types of type1. - CSR, 2002-04-09
3144 ;;; Previous comment, now obsolete, but worth keeping around because
3145 ;;; it is true, though too strong a condition:
3147 ;;; Two union types are equal if their subtypes are equal sets.
3148 (!define-type-method
(union :simple-
=) (type1 type2
)
3149 (multiple-value-bind (subtype certain?
)
3150 (csubtypep type1 type2
)
3152 (csubtypep type2 type1
)
3153 ;; we might as well become as certain as possible.
3156 (multiple-value-bind (subtype certain?
)
3157 (csubtypep type2 type1
)
3158 (declare (ignore subtype
))
3159 (values nil certain?
))))))
3161 (!define-type-method
(union :complex-
=) (type1 type2
)
3162 (declare (ignore type1
))
3163 (if (some #'type-might-contain-other-types-p
3164 (union-type-types type2
))
3168 ;;; Similarly, a union type is a subtype of another if and only if
3169 ;;; every element of TYPE1 is a subtype of TYPE2.
3170 (defun union-simple-subtypep (type1 type2
)
3171 (every/type
(swapped-args-fun #'union-complex-subtypep-arg2
)
3173 (union-type-types type1
)))
3175 (!define-type-method
(union :simple-subtypep
) (type1 type2
)
3176 (union-simple-subtypep type1 type2
))
3178 (defun union-complex-subtypep-arg1 (type1 type2
)
3179 (every/type
(swapped-args-fun #'csubtypep
)
3181 (union-type-types type1
)))
3183 (!define-type-method
(union :complex-subtypep-arg1
) (type1 type2
)
3184 (union-complex-subtypep-arg1 type1 type2
))
3186 (defun union-complex-subtypep-arg2 (type1 type2
)
3187 ;; At this stage, we know that type2 is a union type and type1
3188 ;; isn't. We might as well check this, though:
3189 (aver (union-type-p type2
))
3190 (aver (not (union-type-p type1
)))
3191 ;; was: (any/type #'csubtypep type1 (union-type-types type2)), which
3192 ;; turns out to be too restrictive, causing bug 91.
3194 ;; the following reimplementation might look dodgy. It is dodgy. It
3195 ;; depends on the union :complex-= method not doing very much work
3196 ;; -- certainly, not using subtypep. Reasoning:
3198 ;; A is a subset of (B1 u B2)
3199 ;; <=> A n (B1 u B2) = A
3200 ;; <=> (A n B1) u (A n B2) = A
3202 ;; But, we have to be careful not to delegate this type= to
3203 ;; something that could invoke subtypep, which might get us back
3204 ;; here -> stack explosion. We therefore ensure that the second type
3205 ;; (which is the one that's dispatched on) is either a union type
3206 ;; (where we've ensured that the complex-= method will not call
3207 ;; subtypep) or something with no union types involved, in which
3208 ;; case we'll never come back here.
3210 ;; If we don't do this, then e.g.
3211 ;; (SUBTYPEP '(MEMBER 3) '(OR (SATISFIES FOO) (SATISFIES BAR)))
3212 ;; would loop infinitely, as the member :complex-= method is
3213 ;; implemented in terms of subtypep.
3215 ;; Ouch. - CSR, 2002-04-10
3216 (multiple-value-bind (sub-value sub-certain?
)
3219 (mapcar (lambda (x) (type-intersection type1 x
))
3220 (union-type-types type2
))))
3222 (values sub-value sub-certain?
)
3223 ;; The ANY/TYPE expression above is a sufficient condition for
3224 ;; subsetness, but not a necessary one, so we might get a more
3225 ;; certain answer by this CALL-NEXT-METHOD-ish step when the
3226 ;; ANY/TYPE expression is uncertain.
3227 (invoke-complex-subtypep-arg1-method type1 type2
))))
3229 (!define-type-method
(union :complex-subtypep-arg2
) (type1 type2
)
3230 (union-complex-subtypep-arg2 type1 type2
))
3232 (!define-type-method
(union :simple-intersection2
:complex-intersection2
)
3234 ;; The CSUBTYPEP clauses here let us simplify e.g.
3235 ;; (TYPE-INTERSECTION2 (SPECIFIER-TYPE 'LIST)
3236 ;; (SPECIFIER-TYPE '(OR LIST VECTOR)))
3237 ;; (where LIST is (OR CONS NULL)).
3239 ;; The tests are more or less (CSUBTYPEP TYPE1 TYPE2) and vice
3240 ;; versa, but it's important that we pre-expand them into
3241 ;; specialized operations on individual elements of
3242 ;; UNION-TYPE-TYPES, instead of using the ordinary call to
3243 ;; CSUBTYPEP, in order to avoid possibly invoking any methods which
3244 ;; might in turn invoke (TYPE-INTERSECTION2 TYPE1 TYPE2) and thus
3245 ;; cause infinite recursion.
3247 ;; Within this method, type2 is guaranteed to be a union type:
3248 (aver (union-type-p type2
))
3249 ;; Make sure to call only the applicable methods...
3250 (cond ((and (union-type-p type1
)
3251 (union-simple-subtypep type1 type2
)) type1
)
3252 ((and (union-type-p type1
)
3253 (union-simple-subtypep type2 type1
)) type2
)
3254 ((and (not (union-type-p type1
))
3255 (union-complex-subtypep-arg2 type1 type2
))
3257 ((and (not (union-type-p type1
))
3258 (union-complex-subtypep-arg1 type2 type1
))
3261 ;; KLUDGE: This code accumulates a sequence of TYPE-UNION2
3262 ;; operations in a particular order, and gives up if any of
3263 ;; the sub-unions turn out not to be simple. In other cases
3264 ;; ca. sbcl-0.6.11.15, that approach to taking a union was a
3265 ;; bad idea, since it can overlook simplifications which
3266 ;; might occur if the terms were accumulated in a different
3267 ;; order. It's possible that that will be a problem here too.
3268 ;; However, I can't think of a good example to demonstrate
3269 ;; it, and without an example to demonstrate it I can't write
3270 ;; test cases, and without test cases I don't want to
3271 ;; complicate the code to address what's still a hypothetical
3272 ;; problem. So I punted. -- WHN 2001-03-20
3273 (let ((accumulator *empty-type
*))
3274 (dolist (t2 (union-type-types type2
) accumulator
)
3276 (type-union accumulator
3277 (type-intersection type1 t2
))))))))
3279 (!def-type-translator or
(&rest type-specifiers
)
3280 (let ((type (apply #'type-union
3281 (mapcar #'specifier-type type-specifiers
))))
3282 (if (union-type-p type
)
3283 (sb!kernel
::simplify-array-unions type
)
3288 (!define-type-class cons
:enumerable nil
:might-contain-other-types nil
)
3290 ;; Another order-sensitive form. See related note at MEMBER type-class.
3292 (setf *cons-t-t-type
* (%make-cons-type
*universal-type
* *universal-type
*)))
3294 (!def-type-translator cons
(&optional
(car-type-spec '*) (cdr-type-spec '*))
3295 (let ((car-type (single-value-specifier-type car-type-spec
))
3296 (cdr-type (single-value-specifier-type cdr-type-spec
)))
3297 (make-cons-type car-type cdr-type
)))
3299 (!define-type-method
(cons :negate
) (type)
3300 (if (and (eq (cons-type-car-type type
) *universal-type
*)
3301 (eq (cons-type-cdr-type type
) *universal-type
*))
3302 (make-negation-type :type type
)
3304 (make-negation-type :type
(specifier-type 'cons
))
3306 ((and (not (eq (cons-type-car-type type
) *universal-type
*))
3307 (not (eq (cons-type-cdr-type type
) *universal-type
*)))
3310 (type-negation (cons-type-car-type type
))
3314 (type-negation (cons-type-cdr-type type
)))))
3315 ((not (eq (cons-type-car-type type
) *universal-type
*))
3317 (type-negation (cons-type-car-type type
))
3319 ((not (eq (cons-type-cdr-type type
) *universal-type
*))
3322 (type-negation (cons-type-cdr-type type
))))
3323 (t (bug "Weird CONS type ~S" type
))))))
3325 (!define-type-method
(cons :unparse
) (type)
3326 (let ((car-eltype (type-specifier (cons-type-car-type type
)))
3327 (cdr-eltype (type-specifier (cons-type-cdr-type type
))))
3328 (if (and (member car-eltype
'(t *))
3329 (member cdr-eltype
'(t *)))
3331 `(cons ,car-eltype
,cdr-eltype
))))
3333 (!define-type-method
(cons :simple-
=) (type1 type2
)
3334 (declare (type cons-type type1 type2
))
3335 (multiple-value-bind (car-match car-win
)
3336 (type= (cons-type-car-type type1
) (cons-type-car-type type2
))
3337 (multiple-value-bind (cdr-match cdr-win
)
3338 (type= (cons-type-cdr-type type1
) (cons-type-cdr-type type2
))
3339 (cond ((and car-match cdr-match
)
3340 (aver (and car-win cdr-win
))
3344 ;; FIXME: Ideally we would like to detect and handle
3345 ;; (CONS UNKNOWN INTEGER) (CONS UNKNOWN SYMBOL) => NIL, T
3346 ;; but just returning a secondary true on (and car-win cdr-win)
3347 ;; unfortunately breaks other things. --NS 2006-08-16
3348 (and (or (and (not car-match
) car-win
)
3349 (and (not cdr-match
) cdr-win
))
3350 (not (and (cons-type-might-be-empty-type type1
)
3351 (cons-type-might-be-empty-type type2
))))))))))
3353 (!define-type-method
(cons :simple-subtypep
) (type1 type2
)
3354 (declare (type cons-type type1 type2
))
3355 (multiple-value-bind (val-car win-car
)
3356 (csubtypep (cons-type-car-type type1
) (cons-type-car-type type2
))
3357 (multiple-value-bind (val-cdr win-cdr
)
3358 (csubtypep (cons-type-cdr-type type1
) (cons-type-cdr-type type2
))
3359 (if (and val-car val-cdr
)
3360 (values t
(and win-car win-cdr
))
3361 (values nil
(or (and (not val-car
) win-car
)
3362 (and (not val-cdr
) win-cdr
)))))))
3364 ;;; Give up if a precise type is not possible, to avoid returning
3365 ;;; overly general types.
3366 (!define-type-method
(cons :simple-union2
) (type1 type2
)
3367 (declare (type cons-type type1 type2
))
3368 (let ((car-type1 (cons-type-car-type type1
))
3369 (car-type2 (cons-type-car-type type2
))
3370 (cdr-type1 (cons-type-cdr-type type1
))
3371 (cdr-type2 (cons-type-cdr-type type2
))
3374 ;; UGH. -- CSR, 2003-02-24
3375 (macrolet ((frob-car (car1 car2 cdr1 cdr2
3376 &optional
(not1 nil not1p
))
3378 (make-cons-type ,car1
(type-union ,cdr1
,cdr2
))
3380 (type-intersection ,car2
3383 `(type-negation ,car1
)))
3385 (cond ((type= car-type1 car-type2
)
3386 (make-cons-type car-type1
3387 (type-union cdr-type1 cdr-type2
)))
3388 ((type= cdr-type1 cdr-type2
)
3389 (make-cons-type (type-union car-type1 car-type2
)
3391 ((csubtypep car-type1 car-type2
)
3392 (frob-car car-type1 car-type2 cdr-type1 cdr-type2
))
3393 ((csubtypep car-type2 car-type1
)
3394 (frob-car car-type2 car-type1 cdr-type2 cdr-type1
))
3395 ;; more general case of the above, but harder to compute
3397 (setf car-not1
(type-negation car-type1
))
3398 (multiple-value-bind (yes win
)
3399 (csubtypep car-type2 car-not1
)
3400 (and (not yes
) win
)))
3401 (frob-car car-type1 car-type2 cdr-type1 cdr-type2 car-not1
))
3403 (setf car-not2
(type-negation car-type2
))
3404 (multiple-value-bind (yes win
)
3405 (csubtypep car-type1 car-not2
)
3406 (and (not yes
) win
)))
3407 (frob-car car-type2 car-type1 cdr-type2 cdr-type1 car-not2
))
3408 ;; Don't put these in -- consider the effect of taking the
3409 ;; union of (CONS (INTEGER 0 2) (INTEGER 5 7)) and
3410 ;; (CONS (INTEGER 0 3) (INTEGER 5 6)).
3412 ((csubtypep cdr-type1 cdr-type2
)
3413 (frob-cdr car-type1 car-type2 cdr-type1 cdr-type2
))
3415 ((csubtypep cdr-type2 cdr-type1
)
3416 (frob-cdr car-type2 car-type1 cdr-type2 cdr-type1
))))))
3418 (!define-type-method
(cons :simple-intersection2
) (type1 type2
)
3419 (declare (type cons-type type1 type2
))
3420 (let ((car-int2 (type-intersection2 (cons-type-car-type type1
)
3421 (cons-type-car-type type2
)))
3422 (cdr-int2 (type-intersection2 (cons-type-cdr-type type1
)
3423 (cons-type-cdr-type type2
))))
3425 ((and car-int2 cdr-int2
) (make-cons-type car-int2 cdr-int2
))
3426 (car-int2 (make-cons-type car-int2
3428 (cons-type-cdr-type type1
)
3429 (cons-type-cdr-type type2
))))
3430 (cdr-int2 (make-cons-type
3431 (type-intersection (cons-type-car-type type1
)
3432 (cons-type-car-type type2
))
3435 (!define-superclasses cons
((cons)) !cold-init-forms
)
3437 ;;;; CHARACTER-SET types
3439 ;; all character-set types are enumerable, but it's not possible
3440 ;; for one to be TYPE= to a MEMBER type because (MEMBER #\x)
3441 ;; is not internally represented as a MEMBER type.
3442 ;; So in case it wasn't clear already ENUMERABLE-P does not mean
3443 ;; "possibly a MEMBER type in the Lisp-theoretic sense",
3444 ;; but means "could be implemented in SBCL as a MEMBER type".
3445 (!define-type-class character-set
:enumerable nil
3446 :might-contain-other-types nil
)
3448 (!def-type-translator character-set
3449 (&optional
(pairs '((0 .
#.
(1- sb
!xc
:char-code-limit
)))))
3450 (make-character-set-type :pairs pairs
))
3452 (!define-type-method
(character-set :negate
) (type)
3453 (let ((pairs (character-set-type-pairs type
)))
3454 (if (and (= (length pairs
) 1)
3456 (= (cdar pairs
) (1- sb
!xc
:char-code-limit
)))
3457 (make-negation-type :type type
)
3458 (let ((not-character
3460 :type
(make-character-set-type
3461 :pairs
'((0 .
#.
(1- sb
!xc
:char-code-limit
)))))))
3464 (make-character-set-type
3465 :pairs
(let (not-pairs)
3466 (when (> (caar pairs
) 0)
3467 (push (cons 0 (1- (caar pairs
))) not-pairs
))
3468 (do* ((tail pairs
(cdr tail
))
3469 (high1 (cdar tail
) (cdar tail
))
3470 (low2 (caadr tail
) (caadr tail
)))
3472 (when (< (cdar tail
) (1- sb
!xc
:char-code-limit
))
3473 (push (cons (1+ (cdar tail
))
3474 (1- sb
!xc
:char-code-limit
))
3476 (nreverse not-pairs
))
3477 (push (cons (1+ high1
) (1- low2
)) not-pairs
)))))))))
3479 (!define-type-method
(character-set :unparse
) (type)
3481 ((type= type
(specifier-type 'character
)) 'character
)
3482 ((type= type
(specifier-type 'base-char
)) 'base-char
)
3483 ((type= type
(specifier-type 'extended-char
)) 'extended-char
)
3484 ((type= type
(specifier-type 'standard-char
)) 'standard-char
)
3486 ;; Unparse into either MEMBER or CHARACTER-SET. We use MEMBER if there
3487 ;; are at most as many characters as there are character code ranges.
3488 ;; (basically saying to use MEMBER if each range is one character)
3489 (let* ((pairs (character-set-type-pairs type
))
3490 (count (length pairs
))
3491 (chars (loop named outer
3492 for
(low . high
) in pairs
3493 nconc
(loop for code from low upto high
3494 collect
(sb!xc
:code-char code
)
3495 when
(minusp (decf count
))
3496 do
(return-from outer t
)))))
3498 `(character-set ,pairs
)
3499 `(member ,@chars
))))))
3501 (!define-type-method
(character-set :singleton-p
) (type)
3502 (let* ((pairs (character-set-type-pairs type
))
3503 (pair (first pairs
)))
3504 (if (and (typep pairs
'(cons t null
))
3505 (eql (car pair
) (cdr pair
)))
3506 (values t
(code-char (car pair
)))
3509 (!define-type-method
(character-set :simple-
=) (type1 type2
)
3510 (let ((pairs1 (character-set-type-pairs type1
))
3511 (pairs2 (character-set-type-pairs type2
)))
3512 (values (equal pairs1 pairs2
) t
)))
3514 (!define-type-method
(character-set :simple-subtypep
) (type1 type2
)
3516 (dolist (pair (character-set-type-pairs type1
) t
)
3517 (unless (position pair
(character-set-type-pairs type2
)
3518 :test
(lambda (x y
) (and (>= (car x
) (car y
))
3519 (<= (cdr x
) (cdr y
)))))
3523 (!define-type-method
(character-set :simple-union2
) (type1 type2
)
3524 ;; KLUDGE: the canonizing in the MAKE-CHARACTER-SET-TYPE function
3525 ;; actually does the union for us. It might be a little fragile to
3527 (make-character-set-type
3529 (copy-alist (character-set-type-pairs type1
))
3530 (copy-alist (character-set-type-pairs type2
))
3533 (!define-type-method
(character-set :simple-intersection2
) (type1 type2
)
3534 ;; KLUDGE: brute force.
3537 (dolist (pair1 (character-set-type-pairs type1
)
3538 (make-character-set-type
3539 :pairs
(sort pairs
#'< :key
#'car
)))
3540 (dolist (pair2 (character-set-type-pairs type2
))
3542 ((<= (car pair1
) (car pair2
) (cdr pair1
))
3543 (push (cons (car pair2
) (min (cdr pair1
) (cdr pair2
))) pairs
))
3544 ((<= (car pair2
) (car pair1
) (cdr pair2
))
3545 (push (cons (car pair1
) (min (cdr pair1
) (cdr pair2
))) pairs
))))))
3547 (make-character-set-type
3548 :pairs
(intersect-type-pairs
3549 (character-set-type-pairs type1
)
3550 (character-set-type-pairs type2
))))
3553 ;;; Intersect two ordered lists of pairs
3554 ;;; Each list is of the form ((start1 . end1) ... (startn . endn)),
3555 ;;; where start1 <= end1 < start2 <= end2 < ... < startn <= endn.
3556 ;;; Each pair represents the integer interval start..end.
3558 (defun intersect-type-pairs (alist1 alist2
)
3559 (if (and alist1 alist2
)
3561 (pair1 (pop alist1
))
3562 (pair2 (pop alist2
)))
3564 (when (> (car pair1
) (car pair2
))
3565 (rotatef pair1 pair2
)
3566 (rotatef alist1 alist2
))
3567 (let ((pair1-cdr (cdr pair1
)))
3569 ((> (car pair2
) pair1-cdr
)
3570 ;; No over lap -- discard pair1
3571 (unless alist1
(return))
3572 (setq pair1
(pop alist1
)))
3573 ((<= (cdr pair2
) pair1-cdr
)
3574 (push (cons (car pair2
) (cdr pair2
)) res
)
3576 ((= (cdr pair2
) pair1-cdr
)
3577 (unless alist1
(return))
3578 (unless alist2
(return))
3579 (setq pair1
(pop alist1
)
3580 pair2
(pop alist2
)))
3581 (t ;; (< (cdr pair2) pair1-cdr)
3582 (unless alist2
(return))
3583 (setq pair1
(cons (1+ (cdr pair2
)) pair1-cdr
))
3584 (setq pair2
(pop alist2
)))))
3585 (t ;; (> (cdr pair2) (cdr pair1))
3586 (push (cons (car pair2
) pair1-cdr
) res
)
3587 (unless alist1
(return))
3588 (setq pair2
(cons (1+ pair1-cdr
) (cdr pair2
)))
3589 (setq pair1
(pop alist1
))))))
3594 ;;; Return the type that describes all objects that are in X but not
3595 ;;; in Y. If we can't determine this type, then return NIL.
3597 ;;; For now, we only are clever dealing with union and member types.
3598 ;;; If either type is not a union type, then we pretend that it is a
3599 ;;; union of just one type. What we do is remove from X all the types
3600 ;;; that are a subtype any type in Y. If any type in X intersects with
3601 ;;; a type in Y but is not a subtype, then we give up.
3603 ;;; We must also special-case any member type that appears in the
3604 ;;; union. We remove from X's members all objects that are TYPEP to Y.
3605 ;;; If Y has any members, we must be careful that none of those
3606 ;;; members are CTYPEP to any of Y's non-member types. We give up in
3607 ;;; this case, since to compute that difference we would have to break
3608 ;;; the type from X into some collection of types that represents the
3609 ;;; type without that particular element. This seems too hairy to be
3610 ;;; worthwhile, given its low utility.
3611 (defun type-difference (x y
)
3612 (if (and (numeric-type-p x
) (numeric-type-p y
))
3613 ;; Numeric types are easy. Are there any others we should handle like this?
3614 (type-intersection x
(type-negation y
))
3615 (let ((x-types (if (union-type-p x
) (union-type-types x
) (list x
)))
3616 (y-types (if (union-type-p y
) (union-type-types y
) (list y
))))
3618 (dolist (x-type x-types
)
3619 (if (member-type-p x-type
)
3620 (let ((xset (alloc-xset))
3622 (mapc-member-type-members
3624 (multiple-value-bind (ok sure
) (ctypep elt y
)
3626 (return-from type-difference nil
))
3629 (pushnew elt fp-zeroes
)
3630 (add-to-xset elt xset
)))))
3632 (unless (and (xset-empty-p xset
) (not fp-zeroes
))
3633 (res (make-member-type :xset xset
:fp-zeroes fp-zeroes
))))
3634 (dolist (y-type y-types
(res x-type
))
3635 (multiple-value-bind (val win
) (csubtypep x-type y-type
)
3636 (unless win
(return-from type-difference nil
))
3638 (when (types-equal-or-intersect x-type y-type
)
3639 (return-from type-difference nil
))))))
3640 (let ((y-mem (find-if #'member-type-p y-types
)))
3642 (dolist (x-type x-types
)
3643 (unless (member-type-p x-type
)
3644 (mapc-member-type-members
3646 (multiple-value-bind (ok sure
) (ctypep member x-type
)
3647 (when (or (not sure
) ok
)
3648 (return-from type-difference nil
))))
3650 (apply #'type-union
(res))))))
3652 (!def-type-translator array
(&optional
(element-type '*)
3654 (let ((eltype (if (eq element-type
'*)
3656 (specifier-type element-type
))))
3657 (make-array-type (canonical-array-dimensions dimensions
)
3659 :element-type eltype
3660 :specialized-element-type
(%upgraded-array-element-type
3663 (!def-type-translator simple-array
(&optional
(element-type '*)
3665 (let ((eltype (if (eq element-type
'*)
3667 (specifier-type element-type
))))
3668 (make-array-type (canonical-array-dimensions dimensions
)
3670 :element-type eltype
3671 :specialized-element-type
(%upgraded-array-element-type
3674 ;;;; SIMD-PACK types
3677 (!define-type-class simd-pack
:enumerable nil
3678 :might-contain-other-types nil
)
3680 (!def-type-translator simd-pack
(&optional
(element-type-spec '*))
3681 (if (eql element-type-spec
'*)
3682 (%make-simd-pack-type
*simd-pack-element-types
*)
3683 (make-simd-pack-type (single-value-specifier-type element-type-spec
))))
3685 (!define-type-method
(simd-pack :negate
) (type)
3686 (let ((remaining (set-difference *simd-pack-element-types
*
3687 (simd-pack-type-element-type type
)))
3688 (not-simd-pack (make-negation-type :type
(specifier-type 'simd-pack
))))
3690 (type-union not-simd-pack
(%make-simd-pack-type remaining
))
3693 (!define-type-method
(simd-pack :unparse
) (type)
3694 (let ((eltypes (simd-pack-type-element-type type
)))
3695 (cond ((equal eltypes
*simd-pack-element-types
*)
3697 ((= 1 (length eltypes
))
3698 `(simd-pack ,(first eltypes
)))
3700 `(or ,@(mapcar (lambda (eltype)
3701 `(simd-pack ,eltype
))
3704 (!define-type-method
(simd-pack :simple-
=) (type1 type2
)
3705 (declare (type simd-pack-type type1 type2
))
3706 (null (set-exclusive-or (simd-pack-type-element-type type1
)
3707 (simd-pack-type-element-type type2
))))
3709 (!define-type-method
(simd-pack :simple-subtypep
) (type1 type2
)
3710 (declare (type simd-pack-type type1 type2
))
3711 (subsetp (simd-pack-type-element-type type1
)
3712 (simd-pack-type-element-type type2
)))
3714 (!define-type-method
(simd-pack :simple-union2
) (type1 type2
)
3715 (declare (type simd-pack-type type1 type2
))
3716 (%make-simd-pack-type
(union (simd-pack-type-element-type type1
)
3717 (simd-pack-type-element-type type2
))))
3719 (!define-type-method
(simd-pack :simple-intersection2
) (type1 type2
)
3720 (declare (type simd-pack-type type1 type2
))
3721 (let ((intersection (intersection (simd-pack-type-element-type type1
)
3722 (simd-pack-type-element-type type2
))))
3724 (%make-simd-pack-type intersection
)
3727 (!define-superclasses simd-pack
((simd-pack)) !cold-init-forms
))
3729 ;;;; utilities shared between cross-compiler and target system
3731 ;;; Does the type derived from compilation of an actual function
3732 ;;; definition satisfy declarations of a function's type?
3733 (defun defined-ftype-matches-declared-ftype-p (defined-ftype declared-ftype
)
3734 (declare (type ctype defined-ftype declared-ftype
))
3735 (flet ((is-built-in-class-function-p (ctype)
3736 (and (built-in-classoid-p ctype
)
3737 (eq (built-in-classoid-name ctype
) 'function
))))
3738 (cond (;; DECLARED-FTYPE could certainly be #<BUILT-IN-CLASS FUNCTION>;
3739 ;; that's what happens when we (DECLAIM (FTYPE FUNCTION FOO)).
3740 (is-built-in-class-function-p declared-ftype
)
3741 ;; In that case, any definition satisfies the declaration.
3743 (;; It's not clear whether or how DEFINED-FTYPE might be
3744 ;; #<BUILT-IN-CLASS FUNCTION>, but it's not obviously
3745 ;; invalid, so let's handle that case too, just in case.
3746 (is-built-in-class-function-p defined-ftype
)
3747 ;; No matter what DECLARED-FTYPE might be, we can't prove
3748 ;; that an object of type FUNCTION doesn't satisfy it, so
3749 ;; we return success no matter what.
3751 (;; Otherwise both of them must be FUN-TYPE objects.
3753 ;; FIXME: For now we only check compatibility of the return
3754 ;; type, not argument types, and we don't even check the
3755 ;; return type very precisely (as per bug 94a). It would be
3756 ;; good to do a better job. Perhaps to check the
3757 ;; compatibility of the arguments, we should (1) redo
3758 ;; VALUES-TYPES-EQUAL-OR-INTERSECT as
3759 ;; ARGS-TYPES-EQUAL-OR-INTERSECT, and then (2) apply it to
3760 ;; the ARGS-TYPE slices of the FUN-TYPEs. (ARGS-TYPE
3761 ;; is a base class both of VALUES-TYPE and of FUN-TYPE.)
3762 (values-types-equal-or-intersect
3763 (fun-type-returns defined-ftype
)
3764 (fun-type-returns declared-ftype
))))))
3766 ;;; This messy case of CTYPE for NUMBER is shared between the
3767 ;;; cross-compiler and the target system.
3768 (defun ctype-of-number (x)
3769 (let ((num (if (complexp x
) (realpart x
) x
)))
3770 (multiple-value-bind (complexp low high
)
3772 (let ((imag (imagpart x
)))
3773 (values :complex
(min num imag
) (max num imag
)))
3774 (values :real num num
))
3775 (make-numeric-type :class
(etypecase num
3776 (integer (if (complexp x
)
3777 (if (integerp (imagpart x
))
3781 (rational 'rational
)
3783 :format
(and (floatp num
) (float-format-name num
))
3788 ;;; The following function is a generic driver for approximating
3789 ;;; set-valued functions over types. Putting this here because it'll
3790 ;;; probably be useful for a lot of type analyses.
3792 ;;; Let f be a function from values of type X to Y, e.g., ARRAY-RANK.
3794 ;;; We compute an over or under-approximation of the set
3796 ;;; F(TYPE) = { f(x) : x in TYPE /\ x in X } \subseteq Y
3798 ;;; via set-valued approximations of f, OVER and UNDER.
3800 ;;; These functions must have the property that
3801 ;;; Forall TYPE, OVER(TYPE) \superseteq F(TYPE) and
3802 ;;; Forall TYPE, UNDER(TYPE) \subseteq F(TYPE)
3804 ;;; The driver is also parameterised over the finite set
3807 ;;; Union, intersection and difference are binary functions to compute
3808 ;;; set union, intersection and difference. Top and bottom are the
3809 ;;; concrete representations for the universe and empty sets; we never
3810 ;;; call the set functions on top or bottom, so it's safe to use
3811 ;;; special values there.
3815 ;;; TYPE: the ctype for which we wish to approximate F(TYPE)
3816 ;;; OVERAPPROXIMATE: true if we wish to overapproximate, nil otherwise.
3817 ;;; You usually want T.
3818 ;;; UNION/INTERSECTION/DIFFERENCE: implementations of finite set operations.
3819 ;;; Conform to cl::(union/intersection/set-difference). Passing NIL will
3820 ;;; disable some cleverness and result in quicker computation of coarser
3821 ;;; approximations. However, passing difference without union and intersection
3822 ;;; will probably not end well.
3823 ;;; TOP/BOTTOM: concrete representation of the universe and empty set. Finite
3824 ;;; set operations are never called on TOP/BOTTOM, so it's safe to use special
3826 ;;; OVER/UNDER: the set-valued approximations of F.
3828 ;;; Implementation details.
3830 ;;; It's a straightforward walk down the type.
3831 ;;; Union types -> take the union of children, intersection ->
3832 ;;; intersect. There is some complication for negation types: we must
3833 ;;; not only negate the result, but also flip from overapproximating
3834 ;;; to underapproximating in the children (or vice versa).
3836 ;;; We represent sets as a pair of (negate-p finite-set) in order to
3837 ;;; support negation types.
3839 (declaim (inline generic-abstract-type-function
))
3840 (defun generic-abstract-type-function
3841 (type overapproximate
3842 union intersection difference
3845 (labels ((union* (x y
)
3846 ;; wrappers to avoid calling union/intersection on
3848 (cond ((or (eql x top
)
3854 (funcall union x y
))))
3855 (intersection* (x y
)
3856 (cond ((or (eql x bottom
)
3862 (funcall intersection x y
))))
3863 (unite (not-x-p x not-y-p y
)
3864 ;; if we only have one negated set, it's x.
3866 (rotatef not-x-p not-y-p
)
3868 (cond ((and not-x-p not-y-p
)
3869 ;; -x \/ -y = -(x /\ y)
3870 (normalize t
(intersection* x y
)))
3872 ;; -x \/ y = -(x \ y)
3882 (funcall difference x y
)))))
3884 (values nil
(union* x y
)))))
3885 (intersect (not-x-p x not-y-p y
)
3887 (rotatef not-x-p not-y-p
)
3889 (cond ((and not-x-p not-y-p
)
3890 ;; -x /\ -y = -(x \/ y)
3891 (normalize t
(union* x y
)))
3894 (cond ((or (eql x top
) (eql y bottom
))
3895 (values nil bottom
))
3901 (values nil
(funcall difference y x
)))))
3903 (values nil
(intersection* x y
)))))
3904 (normalize (not-x-p x
)
3905 ;; catch some easy cases of redundant negation.
3906 (cond ((not not-x-p
)
3914 (default (overapproximate)
3916 (if overapproximate top bottom
))
3917 (walk-union (types overapproximate
)
3918 ;; Only do this if union is provided.
3920 (return-from walk-union
(default overapproximate
)))
3921 ;; Reduce/union from bottom.
3922 (let ((not-acc-p nil
)
3924 (dolist (type types
(values not-acc-p acc
))
3925 (multiple-value-bind (not x
)
3926 (walk type overapproximate
)
3927 (setf (values not-acc-p acc
)
3928 (unite not-acc-p acc not x
)))
3929 ;; Early exit on top set.
3930 (when (and (eql acc top
)
3932 (return (values nil top
))))))
3933 (walk-intersection (types overapproximate
)
3934 ;; Skip if we don't know how to intersect sets
3935 (unless intersection
3936 (return-from walk-intersection
(default overapproximate
)))
3937 ;; Reduce/intersection from top
3938 (let ((not-acc-p nil
)
3940 (dolist (type types
(values not-acc-p acc
))
3941 (multiple-value-bind (not x
)
3942 (walk type overapproximate
)
3943 (setf (values not-acc-p acc
)
3944 (intersect not-acc-p acc not x
)))
3945 (when (and (eql acc bottom
)
3947 (return (values nil bottom
))))))
3948 (walk-negate (type overapproximate
)
3949 ;; Don't introduce negated types if we don't know how to
3952 (return-from walk-negate
(default overapproximate
)))
3953 (multiple-value-bind (not x
)
3954 (walk type
(not overapproximate
))
3955 (normalize (not not
) x
)))
3956 (walk (type overapproximate
)
3959 (walk-union (union-type-types type
) overapproximate
))
3960 ((cons (member or union
))
3961 (walk-union (rest type
) overapproximate
))
3963 (walk-intersection (intersection-type-types type
) overapproximate
))
3964 ((cons (member and intersection
))
3965 (walk-intersection (rest type
) overapproximate
))
3967 (walk-negate (negation-type-type type
) overapproximate
))
3969 (walk-negate (second type
) overapproximate
))
3977 (funcall under type
)
3978 (default nil
))))))))
3979 (multiple-value-call #'normalize
(walk type overapproximate
))))
3980 (declaim (notinline generic-abstract-type-function
))
3982 ;;; Standard list representation of sets. Use CL:* for the universe.
3983 (defun list-abstract-type-function (type over
&key under
(overapproximate t
))
3984 (declare (inline generic-abstract-type-function
))
3985 (generic-abstract-type-function
3986 type overapproximate
3987 #'union
#'intersection
#'set-difference
3991 (!defun-from-collected-cold-init-forms
!late-type-cold-init
)
3993 (/show0
"late-type.lisp end of file")