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 :specifier
(missing-arg)))
35 ;;; This condition is signalled whenever we encounter a type (DEFTYPE,
36 ;;; structure, condition, class) that has been marked as deprecated.
37 (define-condition parse-deprecated-type
(condition)
38 ((specifier :reader parse-deprecated-type-specifier
:initarg
:specifier
))
40 :specifier
(missing-arg)))
42 ;;; These functions are used as method for types which need a complex
43 ;;; subtypep method to handle some superclasses, but cover a subtree
44 ;;; of the type graph (i.e. there is no simple way for any other type
45 ;;; class to be a subtype.) There are always still complex ways,
46 ;;; namely UNION and MEMBER types, so we must give TYPE1's method a
47 ;;; chance to run, instead of immediately returning NIL, T.
48 (defun delegate-complex-subtypep-arg2 (type1 type2
)
50 (type-class-complex-subtypep-arg1 (type-class-info type1
))))
52 (funcall subtypep-arg1 type1 type2
)
54 (defun delegate-complex-intersection2 (type1 type2
)
55 (let ((method (type-class-complex-intersection2 (type-class-info type1
))))
56 (if (and method
(not (eq method
#'delegate-complex-intersection2
)))
57 (funcall method type2 type1
)
58 (hierarchical-intersection2 type1 type2
))))
60 (defun contains-unknown-type-p (ctype)
63 (compound-type (some #'contains-unknown-type-p
(compound-type-types ctype
)))
64 (negation-type (contains-unknown-type-p (negation-type-type ctype
)))
65 (cons-type (or (contains-unknown-type-p (cons-type-car-type ctype
))
66 (contains-unknown-type-p (cons-type-cdr-type ctype
))))
67 (array-type (contains-unknown-type-p (array-type-element-type ctype
)))))
69 ;; Similar to (NOT CONTAINS-UNKNOWN-TYPE-P), but report that (SATISFIES F)
70 ;; is not a testable type unless F is currently bound.
71 (defun testable-type-p (ctype)
73 (unknown-type nil
) ; must precede HAIRY because an unknown is HAIRY
75 (let ((spec (hairy-type-specifier ctype
)))
76 ;; Anything other than (SATISFIES ...) is testable
77 ;; because there's no reason to suppose that it isn't.
78 (or (neq (car spec
) 'satisfies
) (fboundp (cadr spec
)))))
79 (compound-type (every #'testable-type-p
(compound-type-types ctype
)))
80 (negation-type (testable-type-p (negation-type-type ctype
)))
81 (cons-type (and (testable-type-p (cons-type-car-type ctype
))
82 (testable-type-p (cons-type-cdr-type ctype
))))
83 ;; This case could be too strict. I think an array type is testable
84 ;; if the upgraded type is testable. Probably nobody cares though.
85 (array-type (testable-type-p (array-type-element-type ctype
)))
88 ;;; This is used by !DEFINE-SUPERCLASSES to define the SUBTYPE-ARG1
89 ;;; method. INFO is a list of conses
90 ;;; (SUPERCLASS-CLASS . {GUARD-TYPE-SPECIFIER | NIL}).
91 (defun has-superclasses-complex-subtypep-arg1 (type1 type2 info
)
92 ;; If TYPE2 might be concealing something related to our class
94 (if (type-might-contain-other-types-p type2
)
95 ;; too confusing, gotta punt
97 ;; ordinary case expected by old CMU CL code, where the taxonomy
98 ;; of TYPE2's representation accurately reflects the taxonomy of
101 ;; FIXME: This old CMU CL code probably deserves a comment
102 ;; explaining to us mere mortals how it works...
103 (and (sb!xc
:typep type2
'classoid
)
105 (when (or (not (cdr x
))
106 (csubtypep type1
(specifier-type (cdr x
))))
108 (or (eq type2
(car x
))
109 (let ((inherits (layout-inherits
110 (classoid-layout (car x
)))))
111 (dotimes (i (length inherits
) nil
)
112 (when (eq type2
(layout-classoid (svref inherits i
)))
116 ;;; This function takes a list of specs, each of the form
117 ;;; (SUPERCLASS-NAME &OPTIONAL GUARD).
118 ;;; Consider one spec (with no guard): any instance of the named
119 ;;; TYPE-CLASS is also a subtype of the named superclass and of any of
120 ;;; its superclasses. If there are multiple specs, then some will have
121 ;;; guards. We choose the first spec whose guard is a supertype of
122 ;;; TYPE1 and use its superclass. In effect, a sequence of guards
125 ;;; G0,(and G1 (not G0)), (and G2 (not (or G0 G1))).
127 ;;; WHEN controls when the forms are executed.
128 (defmacro !define-superclasses
(type-class-name specs when
)
129 (with-unique-names (type-class info
)
131 (let ((,type-class
(type-class-or-lose ',type-class-name
))
132 (,info
(mapcar (lambda (spec)
134 (super &optional guard
)
136 (cons (find-classoid super
) guard
)))
138 (setf (type-class-complex-subtypep-arg1 ,type-class
)
139 (lambda (type1 type2
)
140 (has-superclasses-complex-subtypep-arg1 type1 type2
,info
)))
141 (setf (type-class-complex-subtypep-arg2 ,type-class
)
142 #'delegate-complex-subtypep-arg2
)
143 (setf (type-class-complex-intersection2 ,type-class
)
144 #'delegate-complex-intersection2
)))))
146 ;;;; FUNCTION and VALUES types
148 ;;;; Pretty much all of the general type operations are illegal on
149 ;;;; VALUES types, since we can't discriminate using them, do
150 ;;;; SUBTYPEP, etc. FUNCTION types are acceptable to the normal type
151 ;;;; operations, but are generally considered to be equivalent to
152 ;;;; FUNCTION. These really aren't true types in any type theoretic
153 ;;;; sense, but we still parse them into CTYPE structures for two
156 ;;;; -- Parsing and unparsing work the same way, and indeed we can't
157 ;;;; tell whether a type is a function or values type without
159 ;;;; -- Many of the places that can be annotated with real types can
160 ;;;; also be annotated with function or values types.
162 (!define-type-method
(values :simple-subtypep
:complex-subtypep-arg1
)
164 (declare (ignore type2
))
165 ;; FIXME: should be TYPE-ERROR, here and in next method
166 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type1
)))
168 (!define-type-method
(values :complex-subtypep-arg2
)
170 (declare (ignore type1
))
171 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type2
)))
173 (!define-type-method
(values :negate
) (type)
174 (error "NOT VALUES too confusing on ~S" (type-specifier type
)))
176 (!define-type-method
(values :unparse
) (type)
178 (let ((unparsed (unparse-args-types type
)))
179 (if (or (values-type-optional type
)
180 (values-type-rest type
)
181 (values-type-allowp type
))
183 (nconc unparsed
'(&optional
))))))
185 ;;; Return true if LIST1 and LIST2 have the same elements in the same
186 ;;; positions according to TYPE=. We return NIL, NIL if there is an
187 ;;; uncertain comparison.
188 (defun type=-list
(list1 list2
)
189 (declare (list list1 list2
))
190 (do ((types1 list1
(cdr types1
))
191 (types2 list2
(cdr types2
)))
192 ((or (null types1
) (null types2
))
193 (if (or types1 types2
)
196 (multiple-value-bind (val win
)
197 (type= (first types1
) (first types2
))
199 (return (values nil nil
)))
201 (return (values nil t
))))))
203 (!define-type-method
(values :simple-
=) (type1 type2
)
204 (type=-args type1 type2
))
206 (!define-type-class function
:enumerable nil
207 :might-contain-other-types nil
)
209 ;;; a flag that we can bind to cause complex function types to be
210 ;;; unparsed as FUNCTION. This is useful when we want a type that we
211 ;;; can pass to TYPEP.
212 (!defvar
*unparse-fun-type-simplify
* nil
)
213 ;;; A flag to prevent TYPE-OF calls by user applications from returning
214 ;;; (NOT x). TYPE-SPECIFIER usually allows it to preserve information.
215 (!defvar
*unparse-allow-negation
* t
)
217 (!define-type-method
(function :negate
) (type) (make-negation-type type
))
219 (!define-type-method
(function :unparse
) (type)
220 (if *unparse-fun-type-simplify
*
223 (if (fun-type-wild-args type
)
225 (unparse-args-types type
))
227 (fun-type-returns type
)))))
229 ;;; The meaning of this is a little confused. On the one hand, all
230 ;;; function objects are represented the same way regardless of the
231 ;;; arglists and return values, and apps don't get to ask things like
232 ;;; (TYPEP #'FOO (FUNCTION (FIXNUM) *)) in any meaningful way. On the
233 ;;; other hand, Python wants to reason about function types. So...
234 (!define-type-method
(function :simple-subtypep
) (type1 type2
)
235 (flet ((fun-type-simple-p (type)
236 (not (or (fun-type-rest type
)
237 (fun-type-keyp type
))))
238 (every-csubtypep (types1 types2
)
242 do
(multiple-value-bind (res sure-p
)
244 (unless res
(return (values res sure-p
))))
245 finally
(return (values t t
)))))
246 (and/type
(values-subtypep (fun-type-returns type1
)
247 (fun-type-returns type2
))
248 (cond ((fun-type-wild-args type2
) (values t t
))
249 ((fun-type-wild-args type1
)
250 (cond ((fun-type-keyp type2
) (values nil nil
))
251 ((not (fun-type-rest type2
)) (values nil t
))
252 ((not (null (fun-type-required type2
)))
254 (t (and/type
(type= *universal-type
*
255 (fun-type-rest type2
))
260 ((not (and (fun-type-simple-p type1
)
261 (fun-type-simple-p type2
)))
263 (t (multiple-value-bind (min1 max1
) (fun-type-nargs type1
)
264 (multiple-value-bind (min2 max2
) (fun-type-nargs type2
)
265 (cond ((or (> max1 max2
) (< min1 min2
))
267 ((and (= min1 min2
) (= max1 max2
))
268 (and/type
(every-csubtypep
269 (fun-type-required type1
)
270 (fun-type-required type2
))
272 (fun-type-optional type1
)
273 (fun-type-optional type2
))))
276 (fun-type-required type1
)
277 (fun-type-optional type1
))
279 (fun-type-required type2
)
280 (fun-type-optional type2
))))))))))))
282 (!define-superclasses function
((function)) !cold-init-forms
)
284 ;;; The union or intersection of two FUNCTION types is FUNCTION.
285 (!define-type-method
(function :simple-union2
) (type1 type2
)
286 (declare (ignore type1 type2
))
287 (specifier-type 'function
))
288 (!define-type-method
(function :simple-intersection2
) (type1 type2
)
289 (let ((ftype (specifier-type 'function
)))
290 (cond ((eq type1 ftype
) type2
)
291 ((eq type2 ftype
) type1
)
292 (t (let ((rtype (values-type-intersection (fun-type-returns type1
)
293 (fun-type-returns type2
))))
294 (flet ((change-returns (ftype rtype
)
295 (declare (type fun-type ftype
) (type ctype rtype
))
296 (make-fun-type :required
(fun-type-required ftype
)
297 :optional
(fun-type-optional ftype
)
298 :keyp
(fun-type-keyp ftype
)
299 :keywords
(fun-type-keywords ftype
)
300 :allowp
(fun-type-allowp ftype
)
303 ((fun-type-wild-args type1
)
304 (if (fun-type-wild-args type2
)
305 (make-fun-type :wild-args t
307 (change-returns type2 rtype
)))
308 ((fun-type-wild-args type2
)
309 (change-returns type1 rtype
))
310 (t (multiple-value-bind (req opt rest
)
311 (args-type-op type1 type2
#'type-intersection
#'max
)
312 (make-fun-type :required req
316 :allowp
(and (fun-type-allowp type1
)
317 (fun-type-allowp type2
))
318 :returns rtype
))))))))))
320 ;;; The union or intersection of a subclass of FUNCTION with a
321 ;;; FUNCTION type is somewhat complicated.
322 (!define-type-method
(function :complex-intersection2
) (type1 type2
)
324 ((type= type1
(specifier-type 'function
)) type2
)
325 ((csubtypep type1
(specifier-type 'function
)) nil
)
326 (t :call-other-method
)))
327 (!define-type-method
(function :complex-union2
) (type1 type2
)
328 (declare (ignore type2
))
329 ;; TYPE2 is a FUNCTION type. If TYPE1 is a classoid type naming
330 ;; FUNCTION, then it is the union of the two; otherwise, there is no
333 ((type= type1
(specifier-type 'function
)) type1
)
336 (!define-type-method
(function :simple-
=) (type1 type2
)
337 (macrolet ((compare (comparator field
)
338 (let ((reader (symbolicate '#:fun-type- field
)))
339 `(,comparator
(,reader type1
) (,reader type2
)))))
340 (and/type
(compare type
= returns
)
341 (cond ((neq (fun-type-wild-args type1
) (fun-type-wild-args type2
))
343 ((eq (fun-type-wild-args type1
) t
)
345 (t (type=-args type1 type2
))))))
347 (!define-type-class constant
:inherits values
)
349 (!define-type-method
(constant :negate
) (type)
350 (error "NOT CONSTANT too confusing on ~S" (type-specifier type
)))
352 (!define-type-method
(constant :unparse
) (type)
353 `(constant-arg ,(type-specifier (constant-type-type type
))))
355 (!define-type-method
(constant :simple-
=) (type1 type2
)
356 (type= (constant-type-type type1
) (constant-type-type type2
)))
358 (!def-type-translator constant-arg
((:context context
) type
)
359 (make-constant-type :type
(single-value-specifier-type-r context type
)))
361 ;;; Return the lambda-list-like type specification corresponding
363 (declaim (ftype (function (args-type) list
) unparse-args-types
))
364 (defun unparse-args-types (type)
367 (dolist (arg (args-type-required type
))
368 (result (type-specifier arg
)))
370 (when (args-type-optional type
)
372 (dolist (arg (args-type-optional type
))
373 (result (type-specifier arg
))))
375 (when (args-type-rest type
)
377 (result (type-specifier (args-type-rest type
))))
379 (when (args-type-keyp type
)
381 (dolist (key (args-type-keywords type
))
382 (result (list (key-info-name key
)
383 (type-specifier (key-info-type key
))))))
385 (when (args-type-allowp type
)
386 (result '&allow-other-keys
))
390 (!def-type-translator function
((:context context
)
391 &optional
(args '*) (result '*))
392 (let ((result (coerce-to-values (values-specifier-type-r context result
))))
394 (if (eq result
*wild-type
*)
395 (specifier-type 'function
)
396 (make-fun-type :wild-args t
:returns result
))
397 (multiple-value-bind (llks required optional rest keywords
)
398 (parse-args-types context args
:function-type
)
399 (if (and (null required
)
401 (eq rest
*universal-type
*)
402 (not (ll-kwds-keyp llks
)))
403 (if (eq result
*wild-type
*)
404 (specifier-type 'function
)
405 (make-fun-type :wild-args t
:returns result
))
406 (make-fun-type :required required
409 :keyp
(ll-kwds-keyp llks
)
411 :allowp
(ll-kwds-allowp llks
)
412 :returns result
))))))
414 (!def-type-translator values
:list
((:context context
) &rest values
)
417 (multiple-value-bind (llks required optional rest
)
418 (parse-args-types context values
:values-type
)
420 (make-values-type :required required
:optional optional
:rest rest
)
421 (make-short-values-type required
)))))
423 ;;;; VALUES types interfaces
425 ;;;; We provide a few special operations that can be meaningfully used
426 ;;;; on VALUES types (as well as on any other type).
428 ;;; Return the minimum number of values possibly matching VALUES type
430 (defun values-type-min-value-count (type)
433 (ecase (named-type-name type
)
437 (length (values-type-required type
)))))
439 ;;; Return the maximum number of values possibly matching VALUES type
441 (defun values-type-max-value-count (type)
444 (ecase (named-type-name type
)
445 ((t *) call-arguments-limit
)
448 (if (values-type-rest type
)
450 (+ (length (values-type-optional type
))
451 (length (values-type-required type
)))))))
453 (defun values-type-may-be-single-value-p (type)
454 (<= (values-type-min-value-count type
)
456 (values-type-max-value-count type
)))
458 ;;; VALUES type with a single value.
459 (defun type-single-value-p (type)
460 (and (%values-type-p type
)
461 (not (values-type-rest type
))
462 (null (values-type-optional type
))
463 (singleton-p (values-type-required type
))))
465 ;;; Return the type of the first value indicated by TYPE. This is used
466 ;;; by people who don't want to have to deal with VALUES types.
467 #!-sb-fluid
(declaim (freeze-type values-type
))
468 ; (inline single-value-type))
469 (defun single-value-type (type)
470 (declare (type ctype type
))
471 (cond ((eq type
*wild-type
*)
473 ((eq type
*empty-type
*)
475 ((not (values-type-p type
))
477 ((car (args-type-required type
)))
478 (t (type-union (specifier-type 'null
)
479 (or (car (args-type-optional type
))
480 (args-type-rest type
)
481 (specifier-type 'null
))))))
483 ;;; Return the minimum number of arguments that a function can be
484 ;;; called with, and the maximum number or NIL. If not a function
485 ;;; type, return NIL, NIL.
486 (defun fun-type-nargs (type)
487 (declare (type ctype type
))
488 (if (and (fun-type-p type
) (not (fun-type-wild-args type
)))
489 (let ((fixed (length (args-type-required type
))))
490 (if (or (args-type-rest type
)
491 (args-type-keyp type
)
492 (args-type-allowp type
))
494 (values fixed
(+ fixed
(length (args-type-optional type
))))))
497 ;;; Determine whether TYPE corresponds to a definite number of values.
498 ;;; The first value is a list of the types for each value, and the
499 ;;; second value is the number of values. If the number of values is
500 ;;; not fixed, then return NIL and :UNKNOWN.
501 (defun values-types (type)
502 (declare (type ctype type
))
503 (cond ((or (eq type
*wild-type
*) (eq type
*empty-type
*))
504 (values nil
:unknown
))
505 ((or (args-type-optional type
)
506 (args-type-rest type
))
507 (values nil
:unknown
))
509 (let ((req (args-type-required type
)))
510 (values req
(length req
))))))
512 ;;; Return two values:
513 ;;; 1. A list of all the positional (fixed and optional) types.
514 ;;; 2. The &REST type (if any). If no &REST, then the DEFAULT-TYPE.
515 (defun values-type-types (type &optional
(default-type *empty-type
*))
516 (declare (type ctype type
))
517 (if (eq type
*wild-type
*)
518 (values nil
*universal-type
*)
519 (values (append (args-type-required type
)
520 (args-type-optional type
))
521 (cond ((args-type-rest type
))
524 ;;; types of values in (the <type> (values o_1 ... o_n))
525 (defun values-type-out (type count
)
526 (declare (type ctype type
) (type unsigned-byte count
))
527 (if (eq type
*wild-type
*)
528 (make-list count
:initial-element
*universal-type
*)
530 (flet ((process-types (types)
531 (loop for type in types
535 (process-types (values-type-required type
))
536 (process-types (values-type-optional type
))
538 (loop with rest
= (the ctype
(values-type-rest type
))
543 ;;; types of variable in (m-v-bind (v_1 ... v_n) (the <type> ...
544 (defun values-type-in (type count
)
545 (declare (type ctype type
) (type unsigned-byte count
))
546 (if (eq type
*wild-type
*)
547 (make-list count
:initial-element
*universal-type
*)
549 (let ((null-type (specifier-type 'null
)))
550 (loop for type in
(values-type-required type
)
554 (loop for type in
(values-type-optional type
)
557 do
(res (type-union type null-type
)))
559 (loop with rest
= (acond ((values-type-rest type
)
560 (type-union it null-type
))
566 ;;; Return a list of OPERATION applied to the types in TYPES1 and
567 ;;; TYPES2, padding with REST2 as needed. TYPES1 must not be shorter
568 ;;; than TYPES2. The second value is T if OPERATION always returned a
569 ;;; true second value.
570 (defun fixed-values-op (types1 types2 rest2 operation
)
571 (declare (list types1 types2
) (type ctype rest2
) (type function operation
))
573 (values (mapcar (lambda (t1 t2
)
574 (multiple-value-bind (res win
)
575 (funcall operation t1 t2
)
581 (make-list (- (length types1
) (length types2
))
582 :initial-element rest2
)))
585 ;;; If TYPE isn't a values type, then make it into one.
586 (defun-cached (%coerce-to-values
:hash-bits
8 :hash-function
#'type-hash-value
)
588 (cond ((multiple-value-bind (res sure
)
589 (csubtypep (specifier-type 'null
) type
)
590 (and (not res
) sure
))
591 ;; FIXME: What should we do with (NOT SURE)?
592 (make-values-type :required
(list type
) :rest
*universal-type
*))
594 (make-values-type :optional
(list type
) :rest
*universal-type
*))))
596 (defun coerce-to-values (type)
597 (declare (type ctype type
))
598 (cond ((or (eq type
*universal-type
*)
599 (eq type
*wild-type
*))
601 ((values-type-p type
)
603 (t (%coerce-to-values type
))))
605 ;;; Return type, corresponding to ANSI short form of VALUES type
607 (defun make-short-values-type (types)
608 (declare (list types
))
609 (let ((last-required (position-if
611 (not/type
(csubtypep (specifier-type 'null
) type
)))
615 (make-values-type :required
(subseq types
0 (1+ last-required
))
616 :optional
(subseq types
(1+ last-required
))
617 :rest
*universal-type
*)
618 (make-values-type :optional types
:rest
*universal-type
*))))
620 (defun make-single-value-type (type)
621 (make-values-type :required
(list type
)))
623 ;;; Do the specified OPERATION on TYPE1 and TYPE2, which may be any
624 ;;; type, including VALUES types. With VALUES types such as:
627 ;;; we compute the more useful result
628 ;;; (VALUES (<operation> a0 b0) (<operation> a1 b1))
629 ;;; rather than the precise result
630 ;;; (<operation> (values a0 a1) (values b0 b1))
631 ;;; This has the virtue of always keeping the VALUES type specifier
632 ;;; outermost, and retains all of the information that is really
633 ;;; useful for static type analysis. We want to know what is always
634 ;;; true of each value independently. It is worthless to know that if
635 ;;; the first value is B0 then the second will be B1.
637 ;;; If the VALUES count signatures differ, then we produce a result with
638 ;;; the required VALUE count chosen by NREQ when applied to the number
639 ;;; of required values in TYPE1 and TYPE2. Any &KEY values become
640 ;;; &REST T (anyone who uses keyword values deserves to lose.)
642 ;;; The second value is true if the result is definitely empty or if
643 ;;; OPERATION returned true as its second value each time we called
644 ;;; it. Since we approximate the intersection of VALUES types, the
645 ;;; second value being true doesn't mean the result is exact.
646 (defun args-type-op (type1 type2 operation nreq
)
647 (declare (type ctype type1 type2
)
648 (type function operation nreq
))
649 (when (eq type1 type2
)
651 (multiple-value-bind (types1 rest1
)
652 (values-type-types type1
)
653 (multiple-value-bind (types2 rest2
)
654 (values-type-types type2
)
655 (multiple-value-bind (rest rest-exact
)
656 (funcall operation rest1 rest2
)
657 (multiple-value-bind (res res-exact
)
658 (if (< (length types1
) (length types2
))
659 (fixed-values-op types2 types1 rest1 operation
)
660 (fixed-values-op types1 types2 rest2 operation
))
661 (let* ((req (funcall nreq
662 (length (args-type-required type1
))
663 (length (args-type-required type2
))))
664 (required (subseq res
0 req
))
665 (opt (subseq res req
)))
666 (values required opt rest
667 (and rest-exact res-exact
))))))))
669 (defun values-type-op (type1 type2 operation nreq
)
670 (multiple-value-bind (required optional rest exactp
)
671 (args-type-op type1 type2 operation nreq
)
672 (values (make-values-type :required required
677 (defun compare-key-args (type1 type2
)
678 (let ((keys1 (args-type-keywords type1
))
679 (keys2 (args-type-keywords type2
)))
680 (and (= (length keys1
) (length keys2
))
681 (eq (args-type-allowp type1
)
682 (args-type-allowp type2
))
683 (loop for key1 in keys1
684 for match
= (find (key-info-name key1
)
685 keys2
:key
#'key-info-name
)
687 (type= (key-info-type key1
)
688 (key-info-type match
)))))))
690 (defun type=-args
(type1 type2
)
691 (macrolet ((compare (comparator field
)
692 (let ((reader (symbolicate '#:args-type- field
)))
693 `(,comparator
(,reader type1
) (,reader type2
)))))
695 (cond ((null (args-type-rest type1
))
696 (values (null (args-type-rest type2
)) t
))
697 ((null (args-type-rest type2
))
700 (compare type
= rest
)))
701 (and/type
(and/type
(compare type
=-list required
)
702 (compare type
=-list optional
))
703 (if (or (args-type-keyp type1
) (args-type-keyp type2
))
704 (values (compare-key-args type1 type2
) t
)
707 ;;; Do a union or intersection operation on types that might be values
708 ;;; types. The result is optimized for utility rather than exactness,
709 ;;; but it is guaranteed that it will be no smaller (more restrictive)
710 ;;; than the precise result.
712 ;;; The return convention seems to be analogous to
713 ;;; TYPES-EQUAL-OR-INTERSECT. -- WHN 19990910.
714 (defun-cached (values-type-union :hash-function
#'type-cache-hash
716 ((type1 eq
) (type2 eq
))
717 (declare (type ctype type1 type2
))
718 (cond ((or (eq type1
*wild-type
*) (eq type2
*wild-type
*)) *wild-type
*)
719 ((eq type1
*empty-type
*) type2
)
720 ((eq type2
*empty-type
*) type1
)
722 (values (values-type-op type1 type2
#'type-union
#'min
)))))
724 (defun-cached (values-type-intersection :hash-function
#'type-cache-hash
726 ((type1 eq
) (type2 eq
))
727 (declare (type ctype type1 type2
))
728 (cond ((eq type1
*wild-type
*)
729 (coerce-to-values type2
))
730 ((or (eq type2
*wild-type
*) (eq type2
*universal-type
*))
732 ((or (eq type1
*empty-type
*) (eq type2
*empty-type
*))
734 ((and (not (values-type-p type2
))
735 (values-type-required type1
))
736 (let ((req1 (values-type-required type1
)))
737 (make-values-type :required
(cons (type-intersection (first req1
) type2
)
739 :optional
(values-type-optional type1
)
740 :rest
(values-type-rest type1
)
741 :allowp
(values-type-allowp type1
))))
743 (values (values-type-op type1
(coerce-to-values type2
)
747 ;;; This is like TYPES-EQUAL-OR-INTERSECT, except that it sort of
748 ;;; works on VALUES types. Note that due to the semantics of
749 ;;; VALUES-TYPE-INTERSECTION, this might return (VALUES T T) when
750 ;;; there isn't really any intersection.
751 (defun values-types-equal-or-intersect (type1 type2
)
752 (cond ((or (eq type1
*empty-type
*) (eq type2
*empty-type
*))
754 ((or (eq type1
*wild-type
*) (eq type2
*wild-type
*))
757 (let ((res (values-type-intersection type1 type2
)))
758 (values (not (eq res
*empty-type
*))
761 ;;; a SUBTYPEP-like operation that can be used on any types, including
763 (defun-cached (values-subtypep :hash-function
#'type-cache-hash
766 ((type1 eq
) (type2 eq
))
767 (declare (type ctype type1 type2
))
768 (cond ((or (eq type2
*wild-type
*) (eq type2
*universal-type
*)
769 (eq type1
*empty-type
*))
771 ((eq type1
*wild-type
*)
772 (values (eq type2
*wild-type
*) t
))
773 ((or (eq type2
*empty-type
*)
774 (not (values-types-equal-or-intersect type1 type2
)))
776 ((and (not (values-type-p type2
))
777 (values-type-required type1
))
778 (csubtypep (first (values-type-required type1
))
780 (t (setq type2
(coerce-to-values type2
))
781 (multiple-value-bind (types1 rest1
) (values-type-types type1
)
782 (multiple-value-bind (types2 rest2
) (values-type-types type2
)
783 (cond ((< (length (values-type-required type1
))
784 (length (values-type-required type2
)))
786 ((< (length types1
) (length types2
))
789 (do ((t1 types1
(rest t1
))
790 (t2 types2
(rest t2
)))
792 (csubtypep rest1 rest2
))
793 (multiple-value-bind (res win-p
)
794 (csubtypep (first t1
) (first t2
))
796 (return (values nil nil
)))
798 (return (values nil t
))))))))))))
800 ;;;; type method interfaces
802 ;;; like SUBTYPEP, only works on CTYPE structures
803 (defun-cached (csubtypep :hash-function
#'type-cache-hash
807 ((type1 eq
) (type2 eq
))
808 (declare (type ctype type1 type2
))
809 (cond ((or (eq type1 type2
)
810 (eq type1
*empty-type
*)
811 (eq type2
*universal-type
*))
814 ((eq type1
*universal-type
*)
818 (!invoke-type-method
:simple-subtypep
:complex-subtypep-arg2
820 :complex-arg1
:complex-subtypep-arg1
)))))
822 ;;; Just parse the type specifiers and call CSUBTYPE.
823 (defun sb!xc
:subtypep
(type1 type2
&optional environment
)
825 "Return two values indicating the relationship between type1 and type2.
826 If values are T and T, type1 definitely is a subtype of type2.
827 If values are NIL and T, type1 definitely is not a subtype of type2.
828 If values are NIL and NIL, it couldn't be determined."
829 (declare (type lexenv-designator environment
) (ignore environment
))
830 (declare (explicit-check))
831 (csubtypep (specifier-type type1
) (specifier-type type2
)))
833 ;;; If two types are definitely equivalent, return true. The second
834 ;;; value indicates whether the first value is definitely correct.
835 ;;; This should only fail in the presence of HAIRY types.
836 (defun-cached (type= :hash-function
#'type-cache-hash
840 ((type1 eq
) (type2 eq
))
841 (declare (type ctype type1 type2
))
842 (cond ((eq type1 type2
)
844 ;; If args are not EQ, but both allow TYPE= optimization,
845 ;; and at least one is interned, then return no and certainty.
846 ;; Most of the interned CTYPEs admit this optimization,
847 ;; NUMERIC and MEMBER types do as well.
848 ((and (minusp (logior (type-hash-value type1
) (type-hash-value type2
)))
849 (logtest (logand (type-hash-value type1
) (type-hash-value type2
))
850 +type-admits-type
=-optimization
+))
853 (memoize (!invoke-type-method
:simple-
= :complex-
= type1 type2
)))))
855 ;;; Not exactly the negation of TYPE=, since when the relationship is
856 ;;; uncertain, we still return NIL, NIL. This is useful in cases where
857 ;;; the conservative assumption is =.
858 (defun type/= (type1 type2
)
859 (declare (type ctype type1 type2
))
860 (multiple-value-bind (res win
) (type= type1 type2
)
865 ;;; the type method dispatch case of TYPE-UNION2
866 (defun %type-union2
(type1 type2
)
867 ;; As in %TYPE-INTERSECTION2, it seems to be a good idea to give
868 ;; both argument orders a chance at COMPLEX-INTERSECTION2. Unlike
869 ;; %TYPE-INTERSECTION2, though, I don't have a specific case which
870 ;; demonstrates this is actually necessary. Also unlike
871 ;; %TYPE-INTERSECTION2, there seems to be no need to distinguish
872 ;; between not finding a method and having a method return NIL.
874 (!invoke-type-method
:simple-union2
:complex-union2
877 (declare (inline 1way
))
878 (or (1way type1 type2
)
879 (1way type2 type1
))))
881 ;;; Find a type which includes both types. Any inexactness is
882 ;;; represented by the fuzzy element types; we return a single value
883 ;;; that is precise to the best of our knowledge. This result is
884 ;;; simplified into the canonical form, thus is not a UNION-TYPE
885 ;;; unless we find no other way to represent the result.
886 (defun-cached (type-union2 :hash-function
#'type-cache-hash
889 ((type1 eq
) (type2 eq
))
890 ;; KLUDGE: This was generated from TYPE-INTERSECTION2 by Ye Olde Cut And
891 ;; Paste technique of programming. If it stays around (as opposed to
892 ;; e.g. fading away in favor of some CLOS solution) the shared logic
893 ;; should probably become shared code. -- WHN 2001-03-16
894 (declare (type ctype type1 type2
))
900 ;; CSUBTYPEP for array-types answers questions about the
901 ;; specialized type, yet for union we want to take the
902 ;; expressed type in account too.
903 ((and (not (and (array-type-p type1
) (array-type-p type2
)))
904 (or (setf t2
(csubtypep type1 type2
))
905 (csubtypep type2 type1
)))
907 ((or (union-type-p type1
)
908 (union-type-p type2
))
909 ;; Unions of UNION-TYPE should have the UNION-TYPE-TYPES
910 ;; values broken out and united separately. The full TYPE-UNION
911 ;; function knows how to do this, so let it handle it.
912 (type-union type1 type2
))
914 ;; the ordinary case: we dispatch to type methods
915 (%type-union2 type1 type2
)))))))
917 ;;; the type method dispatch case of TYPE-INTERSECTION2
918 (defun %type-intersection2
(type1 type2
)
919 ;; We want to give both argument orders a chance at
920 ;; COMPLEX-INTERSECTION2. Without that, the old CMU CL type
921 ;; methods could give noncommutative results, e.g.
922 ;; (TYPE-INTERSECTION2 *EMPTY-TYPE* SOME-HAIRY-TYPE)
924 ;; (TYPE-INTERSECTION2 SOME-HAIRY-TYPE *EMPTY-TYPE*)
925 ;; => #<NAMED-TYPE NIL>, T
926 ;; We also need to distinguish between the case where we found a
927 ;; type method, and it returned NIL, and the case where we fell
928 ;; through without finding any type method. An example of the first
929 ;; case is the intersection of a HAIRY-TYPE with some ordinary type.
930 ;; An example of the second case is the intersection of two
931 ;; completely-unrelated types, e.g. CONS and NUMBER, or SYMBOL and
934 ;; (Why yes, CLOS probably *would* be nicer..)
936 (!invoke-type-method
:simple-intersection2
:complex-intersection2
938 :default
:call-other-method
)))
939 (declare (inline 1way
))
940 (let ((xy (1way type1 type2
)))
941 (or (and (not (eql xy
:call-other-method
)) xy
)
942 (let ((yx (1way type2 type1
)))
943 (or (and (not (eql yx
:call-other-method
)) yx
)
944 (cond ((and (eql xy
:call-other-method
)
945 (eql yx
:call-other-method
))
950 (defun-cached (type-intersection2 :hash-function
#'type-cache-hash
954 ((type1 eq
) (type2 eq
))
955 (declare (type ctype type1 type2
))
957 ;; FIXME: For some reason, this doesn't catch e.g. type1 =
958 ;; type2 = (SPECIFIER-TYPE
959 ;; 'SOME-UNKNOWN-TYPE). Investigate. - CSR, 2002-04-10
963 ((or (intersection-type-p type1
)
964 (intersection-type-p type2
))
965 ;; Intersections of INTERSECTION-TYPE should have the
966 ;; INTERSECTION-TYPE-TYPES values broken out and intersected
967 ;; separately. The full TYPE-INTERSECTION function knows how
968 ;; to do that, so let it handle it.
969 (type-intersection type1 type2
))
971 ;; the ordinary case: we dispatch to type methods
972 (%type-intersection2 type1 type2
))))))
974 ;;; Return as restrictive and simple a type as we can discover that is
975 ;;; no more restrictive than the intersection of TYPE1 and TYPE2. At
976 ;;; worst, we arbitrarily return one of the arguments as the first
977 ;;; value (trying not to return a hairy type).
978 (defun type-approx-intersection2 (type1 type2
)
979 (cond ((type-intersection2 type1 type2
))
980 ((hairy-type-p type1
) type2
)
983 ;;; a test useful for checking whether a derived type matches a
986 ;;; The first value is true unless the types don't intersect and
987 ;;; aren't equal. The second value is true if the first value is
988 ;;; definitely correct. NIL is considered to intersect with any type.
989 ;;; If T is a subtype of either type, then we also return T, T. This
990 ;;; way we recognize that hairy types might intersect with T.
992 ;;; Well now given the statement above that this is "useful for ..."
993 ;;; a particular thing, I see how treating *empty-type* magically could
994 ;;; be useful, however given all the _other_ calls to this function within
995 ;;; this file, it seems suboptimal, because logically it is wrong.
996 (defun types-equal-or-intersect (type1 type2
)
997 (declare (type ctype type1 type2
))
998 (if (or (eq type1
*empty-type
*) (eq type2
*empty-type
*))
1000 (let ((intersection2 (type-intersection2 type1 type2
)))
1001 (cond ((not intersection2
)
1002 (if (or (csubtypep *universal-type
* type1
)
1003 (csubtypep *universal-type
* type2
))
1006 ((eq intersection2
*empty-type
*) (values nil t
))
1007 (t (values t t
))))))
1009 ;;; Return a Common Lisp type specifier corresponding to the TYPE
1011 (defun type-specifier (type)
1012 (declare (type ctype type
))
1013 (funcall (type-class-unparse (type-class-info type
)) type
))
1015 ;;; Don't try to define a print method until it's actually gonna work!
1016 ;;; (Otherwise this would be near the DEFSTRUCT)
1017 (def!method print-object
((ctype ctype
) stream
)
1018 (print-unreadable-object (ctype stream
:type t
)
1019 (prin1 (type-specifier ctype
) stream
)))
1022 ;;; Just dump it as a specifier. (We'll convert it back upon loading.)
1023 (defun make-type-load-form (type)
1024 (declare (type ctype type
))
1025 `(specifier-type ',(type-specifier type
)))
1027 (defun-cached (type-negation :hash-function
#'type-hash-value
1031 (declare (type ctype type
))
1032 (funcall (type-class-negate (type-class-info type
)) type
))
1034 (defun-cached (type-singleton-p :hash-function
#'type-hash-value
1038 (declare (type ctype type
))
1039 (let ((function (type-class-singleton-p (type-class-info type
))))
1041 (funcall function type
)
1044 ;;; (VALUES-SPECIFIER-TYPE and SPECIFIER-TYPE moved from here to
1045 ;;; early-type.lisp by WHN ca. 19990201.)
1047 ;;; Take a list of type specifiers, computing the translation of each
1048 ;;; specifier and defining it as a builtin type.
1049 ;;; Seee the comments in 'type-init' for why this is a slightly
1050 ;;; screwy way to go about it.
1051 (declaim (ftype (function (list) (values)) !precompute-types
))
1052 (defun !precompute-types
(specs)
1053 (dolist (spec specs
)
1054 (let ((res (handler-bind
1055 ((parse-unknown-type
1057 (declare (ignore c
))
1058 ;; We can handle conditions at this point,
1059 ;; but win32 can not perform i/o here because
1060 ;; !MAKE-COLD-STDERR-STREAM has no implementation.
1062 (progn (write-string "//caught: parse-unknown ")
1065 (specifier-type spec
))))
1066 (unless (unknown-type-p res
)
1067 (setf (info :type
:builtin spec
) res
)
1068 (setf (info :type
:kind spec
) :primitive
))))
1071 ;;;; general TYPE-UNION and TYPE-INTERSECTION operations
1073 ;;;; These are fully general operations on CTYPEs: they'll always
1074 ;;;; return a CTYPE representing the result.
1076 ;;; shared logic for unions and intersections: Return a list of
1077 ;;; types representing the same types as INPUT-TYPES, but with
1078 ;;; COMPOUND-TYPEs satisfying %COMPOUND-TYPE-P broken up into their
1079 ;;; component types, and with any SIMPLY2 simplifications applied.
1081 ((def (name compound-type-p simplify2
)
1082 `(defun ,name
(types)
1084 (multiple-value-bind (first rest
)
1085 (if (,compound-type-p
(car types
))
1086 (values (car (compound-type-types (car types
)))
1087 (append (cdr (compound-type-types (car types
)))
1089 (values (car types
) (cdr types
)))
1090 (let ((rest (,name rest
)) u
)
1091 (dolist (r rest
(cons first rest
))
1092 (when (setq u
(,simplify2 first r
))
1093 (return (,name
(nsubstitute u r rest
)))))))))))
1094 (def simplify-intersections intersection-type-p type-intersection2
)
1095 (def simplify-unions union-type-p type-union2
))
1097 (defun maybe-distribute-one-union (union-type types
)
1098 (let* ((intersection (apply #'type-intersection types
))
1099 (union (mapcar (lambda (x) (type-intersection x intersection
))
1100 (union-type-types union-type
))))
1101 (if (notany (lambda (x) (or (hairy-type-p x
)
1102 (intersection-type-p x
)))
1107 (defun type-intersection (&rest input-types
)
1108 (%type-intersection input-types
))
1109 (defun-cached (%type-intersection
:hash-bits
10 :hash-function
#'type-list-cache-hash
)
1110 ((input-types equal
))
1111 (let ((simplified-types (simplify-intersections input-types
)))
1112 (declare (type list simplified-types
))
1113 ;; We want to have a canonical representation of types (or failing
1114 ;; that, punt to HAIRY-TYPE). Canonical representation would have
1115 ;; intersections inside unions but not vice versa, since you can
1116 ;; always achieve that by the distributive rule. But we don't want
1117 ;; to just apply the distributive rule, since it would be too easy
1118 ;; to end up with unreasonably huge type expressions. So instead
1119 ;; we try to generate a simple type by distributing the union; if
1120 ;; the type can't be made simple, we punt to HAIRY-TYPE.
1121 (if (and (cdr simplified-types
) (some #'union-type-p simplified-types
))
1122 (let* ((first-union (find-if #'union-type-p simplified-types
))
1123 (other-types (coerce (remove first-union simplified-types
)
1125 (distributed (maybe-distribute-one-union first-union
1128 (apply #'type-union distributed
)
1129 (%make-hairy-type
`(and ,@(map 'list
#'type-specifier
1130 simplified-types
)))))
1132 ((null simplified-types
) *universal-type
*)
1133 ((null (cdr simplified-types
)) (car simplified-types
))
1134 (t (%make-intersection-type
1135 (some #'type-enumerable simplified-types
)
1136 simplified-types
))))))
1138 (defun type-union (&rest input-types
)
1139 (%type-union input-types
))
1140 (defun-cached (%type-union
:hash-bits
8 :hash-function
#'type-list-cache-hash
)
1141 ((input-types equal
))
1142 (let ((simplified-types (simplify-unions input-types
)))
1144 ((null simplified-types
) *empty-type
*)
1145 ((null (cdr simplified-types
)) (car simplified-types
))
1147 (every #'type-enumerable simplified-types
)
1148 simplified-types
)))))
1152 ;; This is used when parsing (SATISFIES KEYWORDP)
1153 ;; so that simplifications can be made when computing intersections,
1154 ;; without which we would see this kind of "empty-type in disguise"
1155 ;; (AND (SATISFIES KEYWORDP) CONS)
1156 ;; This isn't *keyword-type* because KEYWORD is implemented
1157 ;; as the intersection of SYMBOL and (SATISFIES KEYWORDP)
1158 ;; We could also intern the KEYWORD type but that would require
1159 ;; hacking the INTERSECTION logic.
1160 (defglobal *satisfies-keywordp-type
* -
1)
1162 ;; Here too I discovered more than 1000 instances in a particular
1163 ;; Lisp image, when really this is *EMPTY-TYPE*.
1164 ;; (AND (SATISFIES LEGAL-FUN-NAME-P) (SIMPLE-ARRAY CHARACTER (*)))
1165 (defglobal *fun-name-type
* -
1)
1167 ;; !LATE-TYPE-COLD-INIT can't be GCd - there are lambdas in the toplevel code
1168 ;; component that leak out and persist - but everything below is GCable.
1169 ;; This leads to about 20KB of extra code being retained on x86-64.
1170 ;; An educated guess is that DEFINE-SUPERCLASSES is responsible for the problem.
1171 (defun !late-type-cold-init2
()
1172 (!intern-important-fun-type-instances
)
1173 (!intern-important-member-type-instances
)
1174 (!intern-important-cons-type-instances
)
1175 (setf *satisfies-keywordp-type
*
1176 (mark-ctype-interned (%make-hairy-type
'(satisfies keywordp
))))
1177 (setf *fun-name-type
*
1178 (mark-ctype-interned (%make-hairy-type
'(satisfies legal-fun-name-p
))))
1179 ;; This is not an important type- no attempt is made to return exactly this
1180 ;; object when parsing FUNCTION. In fact we return the classoid instead
1181 (setf *universal-fun-type
*
1182 (make-fun-type :wild-args t
:returns
*wild-type
*)))
1184 (!define-type-method
(named :simple-
=) (type1 type2
)
1185 ;;(aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1186 (values (eq type1 type2
) t
))
1188 (defun cons-type-might-be-empty-type (type)
1189 (declare (type cons-type type
))
1190 (let ((car-type (cons-type-car-type type
))
1191 (cdr-type (cons-type-cdr-type type
)))
1193 (if (cons-type-p car-type
)
1194 (cons-type-might-be-empty-type car-type
)
1195 (multiple-value-bind (yes surep
)
1196 (type= car-type
*empty-type
*)
1199 (if (cons-type-p cdr-type
)
1200 (cons-type-might-be-empty-type cdr-type
)
1201 (multiple-value-bind (yes surep
)
1202 (type= cdr-type
*empty-type
*)
1206 (defun cons-type-length-info (type)
1207 (declare (type cons-type type
))
1208 (do ((min 1 (1+ min
))
1209 (cdr (cons-type-cdr-type type
) (cons-type-cdr-type cdr
)))
1210 ((not (cons-type-p cdr
))
1212 ((csubtypep cdr
(specifier-type 'null
))
1214 ((csubtypep *universal-type
* cdr
)
1216 ((type/= (type-intersection (specifier-type 'cons
) cdr
) *empty-type
*)
1218 ((type/= (type-intersection (specifier-type 'null
) cdr
) *empty-type
*)
1220 (t (values min
:maybe
))))
1223 (!define-type-method
(named :complex-
=) (type1 type2
)
1225 ((and (eq type2
*empty-type
*)
1226 (or (and (intersection-type-p type1
)
1227 ;; not allowed to be unsure on these... FIXME: keep
1228 ;; the list of CL types that are intersection types
1229 ;; once and only once.
1230 (not (or (type= type1
(specifier-type 'ratio
))
1231 (type= type1
(specifier-type 'keyword
)))))
1232 (and (cons-type-p type1
)
1233 (cons-type-might-be-empty-type type1
))))
1234 ;; things like (AND (EQL 0) (SATISFIES ODDP)) or (AND FUNCTION
1235 ;; STREAM) can get here. In general, we can't really tell
1236 ;; whether these are equal to NIL or not, so
1238 ((type-might-contain-other-types-p type1
)
1239 (invoke-complex-=-other-method type1 type2
))
1240 (t (values nil t
))))
1242 (!define-type-method
(named :simple-subtypep
) (type1 type2
)
1243 (aver (not (eq type1
*wild-type
*))) ; * isn't really a type.
1244 (aver (not (eq type1 type2
)))
1245 (values (or (eq type1
*empty-type
*)
1246 (eq type2
*wild-type
*)
1247 (eq type2
*universal-type
*)) t
))
1249 (!define-type-method
(named :complex-subtypep-arg1
) (type1 type2
)
1250 ;; This AVER causes problems if we write accurate methods for the
1251 ;; union (and possibly intersection) types which then delegate to
1252 ;; us; while a user shouldn't get here, because of the odd status of
1253 ;; *wild-type* a type-intersection executed by the compiler can. -
1256 ;; (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1257 (cond ((eq type1
*empty-type
*)
1259 (;; When TYPE2 might be the universal type in disguise
1260 (type-might-contain-other-types-p type2
)
1261 ;; Now that the UNION and HAIRY COMPLEX-SUBTYPEP-ARG2 methods
1262 ;; can delegate to us (more or less as CALL-NEXT-METHOD) when
1263 ;; they're uncertain, we can't just barf on COMPOUND-TYPE and
1264 ;; HAIRY-TYPEs as we used to. Instead we deal with the
1265 ;; problem (where at least part of the problem is cases like
1266 ;; (SUBTYPEP T '(SATISFIES FOO))
1268 ;; (SUBTYPEP T '(AND (SATISFIES FOO) (SATISFIES BAR)))
1269 ;; where the second type is a hairy type like SATISFIES, or
1270 ;; is a compound type which might contain a hairy type) by
1271 ;; returning uncertainty.
1273 ((eq type1
*funcallable-instance-type
*)
1274 (values (eq type2
(specifier-type 'function
)) t
))
1276 ;; This case would have been picked off by the SIMPLE-SUBTYPEP
1277 ;; method, and so shouldn't appear here.
1278 (aver (not (named-type-p type2
)))
1279 ;; Since TYPE2 is not EQ *UNIVERSAL-TYPE* and is not another
1280 ;; named type in disguise, TYPE2 is not a superset of TYPE1.
1283 (!define-type-method
(named :complex-subtypep-arg2
) (type1 type2
)
1284 (aver (not (eq type2
*wild-type
*))) ; * isn't really a type.
1285 (cond ((eq type2
*universal-type
*)
1287 ;; some CONS types can conceal danger
1288 ((and (cons-type-p type1
) (cons-type-might-be-empty-type type1
))
1290 ((type-might-contain-other-types-p type1
)
1291 ;; those types can be other types in disguise. So we'd
1293 (invoke-complex-subtypep-arg1-method type1 type2
))
1294 ((and (or (eq type2
*instance-type
*)
1295 (eq type2
*funcallable-instance-type
*))
1296 (member-type-p type1
))
1297 ;; member types can be subtypep INSTANCE and
1298 ;; FUNCALLABLE-INSTANCE in surprising ways.
1299 (invoke-complex-subtypep-arg1-method type1 type2
))
1300 ((and (eq type2
*extended-sequence-type
*) (classoid-p type1
))
1301 (let* ((layout (classoid-layout type1
))
1302 (inherits (layout-inherits layout
))
1303 (sequencep (find (classoid-layout (find-classoid 'sequence
))
1305 (values (if sequencep t nil
) t
)))
1306 ((and (eq type2
*instance-type
*) (classoid-p type1
))
1307 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1309 (let* ((layout (classoid-layout type1
))
1310 (inherits (layout-inherits layout
))
1311 (functionp (find (classoid-layout (find-classoid 'function
))
1316 ((eq type1
(find-classoid 'function
))
1318 ((or (structure-classoid-p type1
)
1320 (condition-classoid-p type1
))
1322 (t (values nil nil
))))))
1323 ((and (eq type2
*funcallable-instance-type
*) (classoid-p type1
))
1324 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1326 (let* ((layout (classoid-layout type1
))
1327 (inherits (layout-inherits layout
))
1328 (functionp (find (classoid-layout (find-classoid 'function
))
1330 (values (if functionp t nil
) t
))))
1332 ;; FIXME: This seems to rely on there only being 4 or 5
1333 ;; NAMED-TYPE values, and the exclusion of various
1334 ;; possibilities above. It would be good to explain it and/or
1335 ;; rewrite it so that it's clearer.
1338 (!define-type-method
(named :complex-intersection2
) (type1 type2
)
1339 ;; FIXME: This assertion failed when I added it in sbcl-0.6.11.13.
1340 ;; Perhaps when bug 85 is fixed it can be reenabled.
1341 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1343 ((eq type2
*extended-sequence-type
*)
1345 (structure-classoid *empty-type
*)
1347 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1349 (if (find (classoid-layout (find-classoid 'sequence
))
1350 (layout-inherits (classoid-layout type1
)))
1354 (if (or (type-might-contain-other-types-p type1
)
1355 (member-type-p type1
))
1358 ((eq type2
*instance-type
*)
1360 (structure-classoid type1
)
1362 (if (and (not (member type1
*non-instance-classoid-types
*
1363 :key
#'find-classoid
))
1364 (not (eq type1
(find-classoid 'function
)))
1365 (not (find (classoid-layout (find-classoid 'function
))
1366 (layout-inherits (classoid-layout type1
)))))
1370 (if (or (type-might-contain-other-types-p type1
)
1371 (member-type-p type1
))
1374 ((eq type2
*funcallable-instance-type
*)
1376 (structure-classoid *empty-type
*)
1378 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1380 (if (find (classoid-layout (find-classoid 'function
))
1381 (layout-inherits (classoid-layout type1
)))
1383 (if (type= type1
(find-classoid 'function
))
1388 (if (or (type-might-contain-other-types-p type1
)
1389 (member-type-p type1
))
1392 (t (hierarchical-intersection2 type1 type2
))))
1394 (!define-type-method
(named :complex-union2
) (type1 type2
)
1395 ;; Perhaps when bug 85 is fixed this can be reenabled.
1396 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1398 ((eq type2
*extended-sequence-type
*)
1399 (if (classoid-p type1
)
1400 (if (or (member type1
*non-instance-classoid-types
*
1401 :key
#'find-classoid
)
1402 (not (find (classoid-layout (find-classoid 'sequence
))
1403 (layout-inherits (classoid-layout type1
)))))
1407 ((eq type2
*instance-type
*)
1408 (if (classoid-p type1
)
1409 (if (or (member type1
*non-instance-classoid-types
*
1410 :key
#'find-classoid
)
1411 (find (classoid-layout (find-classoid 'function
))
1412 (layout-inherits (classoid-layout type1
))))
1416 ((eq type2
*funcallable-instance-type
*)
1417 (if (classoid-p type1
)
1418 (if (or (member type1
*non-instance-classoid-types
*
1419 :key
#'find-classoid
)
1420 (not (find (classoid-layout (find-classoid 'function
))
1421 (layout-inherits (classoid-layout type1
)))))
1423 (if (eq type1
(specifier-type 'function
))
1427 (t (hierarchical-union2 type1 type2
))))
1429 (!define-type-method
(named :negate
) (x)
1430 (aver (not (eq x
*wild-type
*)))
1432 ((eq x
*universal-type
*) *empty-type
*)
1433 ((eq x
*empty-type
*) *universal-type
*)
1434 ((or (eq x
*instance-type
*)
1435 (eq x
*funcallable-instance-type
*)
1436 (eq x
*extended-sequence-type
*))
1437 (make-negation-type x
))
1438 (t (bug "NAMED type unexpected: ~S" x
))))
1440 (!define-type-method
(named :unparse
) (x)
1441 (named-type-name x
))
1443 ;;;; hairy and unknown types
1444 ;;;; DEFINE-TYPE-CLASS HAIRY is in 'early-type'
1446 (!define-type-method
(hairy :negate
) (x) (make-negation-type x
))
1448 (!define-type-method
(hairy :unparse
) (x)
1449 (hairy-type-specifier x
))
1451 (!define-type-method
(hairy :simple-subtypep
) (type1 type2
)
1452 (let ((hairy-spec1 (hairy-type-specifier type1
))
1453 (hairy-spec2 (hairy-type-specifier type2
)))
1454 (cond ((equal-but-no-car-recursion hairy-spec1 hairy-spec2
)
1456 ((maybe-reparse-specifier! type1
)
1457 (csubtypep type1 type2
))
1458 ((maybe-reparse-specifier! type2
)
1459 (csubtypep type1 type2
))
1461 (values nil nil
)))))
1463 (!define-type-method
(hairy :complex-subtypep-arg2
) (type1 type2
)
1464 (if (maybe-reparse-specifier! type2
)
1465 (csubtypep type1 type2
)
1466 (let ((specifier (hairy-type-specifier type2
)))
1467 (cond ((and (consp specifier
) (eql (car specifier
) 'satisfies
))
1468 (case (cadr specifier
)
1469 ((keywordp) (if (type= type1
(specifier-type 'symbol
))
1471 (invoke-complex-subtypep-arg1-method type1 type2
)))
1472 (t (invoke-complex-subtypep-arg1-method type1 type2
))))
1474 (invoke-complex-subtypep-arg1-method type1 type2
))))))
1476 (!define-type-method
(hairy :complex-subtypep-arg1
) (type1 type2
)
1477 (if (maybe-reparse-specifier! type1
)
1478 (csubtypep type1 type2
)
1481 (!define-type-method
(hairy :complex-
=) (type1 type2
)
1482 (if (maybe-reparse-specifier! type2
)
1486 (!define-type-method
(hairy :simple-intersection2
:complex-intersection2
)
1488 (acond ((type= type1 type2
)
1490 ((eq type2
*satisfies-keywordp-type
*)
1491 ;; (AND (MEMBER A) (SATISFIES KEYWORDP)) is possibly non-empty
1492 ;; if A is re-homed as :A. However as a special case that really
1493 ;; does occur, (AND (MEMBER NIL) (SATISFIES KEYWORDP))
1494 ;; is empty because of the illegality of changing NIL's package.
1495 (if (eq type1
*null-type
*)
1497 (multiple-value-bind (answer certain
)
1498 (types-equal-or-intersect type1
(specifier-type 'symbol
))
1499 (and (not answer
) certain
*empty-type
*))))
1500 ((eq type2
*fun-name-type
*)
1501 (multiple-value-bind (answer certain
)
1502 (types-equal-or-intersect type1
(specifier-type 'symbol
))
1505 (multiple-value-bind (answer certain
)
1506 (types-equal-or-intersect type1
(specifier-type 'cons
))
1507 (and (not answer
) certain
*empty-type
*)))))
1508 ((and (typep (hairy-type-specifier type2
) '(cons (eql satisfies
)))
1509 (info :function
:predicate-truth-constraint
1510 (cadr (hairy-type-specifier type2
))))
1511 (multiple-value-bind (answer certain
)
1512 (types-equal-or-intersect type1
(specifier-type it
))
1513 (and (not answer
) certain
*empty-type
*)))))
1515 (!define-type-method
(hairy :simple-union2
)
1517 (if (type= type1 type2
)
1521 (!define-type-method
(hairy :simple-
=) (type1 type2
)
1522 (if (equal-but-no-car-recursion (hairy-type-specifier type1
)
1523 (hairy-type-specifier type2
))
1527 (!def-type-translator satisfies
:list
(&whole whole predicate-name
)
1528 (unless (symbolp predicate-name
)
1529 (error 'simple-type-error
1530 :datum predicate-name
1531 :expected-type
'symbol
1532 :format-control
"The SATISFIES predicate name is not a symbol: ~S"
1533 :format-arguments
(list predicate-name
)))
1534 (case predicate-name
1535 (keywordp *satisfies-keywordp-type
*)
1536 (legal-fun-name-p *fun-name-type
*)
1537 (t (%make-hairy-type whole
))))
1541 (!define-type-method
(negation :negate
) (x)
1542 (negation-type-type x
))
1544 (!define-type-method
(negation :unparse
) (x)
1545 (if (type= (negation-type-type x
) (specifier-type 'cons
))
1547 `(not ,(type-specifier (negation-type-type x
)))))
1549 (!define-type-method
(negation :simple-subtypep
) (type1 type2
)
1550 (csubtypep (negation-type-type type2
) (negation-type-type type1
)))
1552 (!define-type-method
(negation :complex-subtypep-arg2
) (type1 type2
)
1553 (let* ((complement-type2 (negation-type-type type2
))
1554 (intersection2 (type-intersection2 type1
1557 ;; FIXME: if uncertain, maybe try arg1?
1558 (type= intersection2
*empty-type
*)
1559 (invoke-complex-subtypep-arg1-method type1 type2
))))
1561 (!define-type-method
(negation :complex-subtypep-arg1
) (type1 type2
)
1562 ;; "Incrementally extended heuristic algorithms tend inexorably toward the
1563 ;; incomprehensible." -- http://www.unlambda.com/~james/lambda/lambda.txt
1565 ;; You may not believe this. I couldn't either. But then I sat down
1566 ;; and drew lots of Venn diagrams. Comments involving a and b refer
1567 ;; to the call (subtypep '(not a) 'b) -- CSR, 2002-02-27.
1569 ;; (Several logical truths in this block are true as long as
1570 ;; b/=T. As of sbcl-0.7.1.28, it seems impossible to construct a
1571 ;; case with b=T where we actually reach this type method, but
1572 ;; we'll test for and exclude this case anyway, since future
1573 ;; maintenance might make it possible for it to end up in this
1575 (multiple-value-bind (equal certain
)
1576 (type= type2
*universal-type
*)
1578 (return (values nil nil
)))
1580 (return (values t t
))))
1581 (let ((complement-type1 (negation-type-type type1
)))
1582 ;; Do the special cases first, in order to give us a chance if
1583 ;; subtype/supertype relationships are hairy.
1584 (multiple-value-bind (equal certain
)
1585 (type= complement-type1 type2
)
1586 ;; If a = b, ~a is not a subtype of b (unless b=T, which was
1589 (return (values nil nil
)))
1591 (return (values nil t
))))
1592 ;; KLUDGE: ANSI requires that the SUBTYPEP result between any
1593 ;; two built-in atomic type specifiers never be uncertain. This
1594 ;; is hard to do cleanly for the built-in types whose
1595 ;; definitions include (NOT FOO), i.e. CONS and RATIO. However,
1596 ;; we can do it with this hack, which uses our global knowledge
1597 ;; that our implementation of the type system uses disjoint
1598 ;; implementation types to represent disjoint sets (except when
1599 ;; types are contained in other types). (This is a KLUDGE
1600 ;; because it's fragile. Various changes in internal
1601 ;; representation in the type system could make it start
1602 ;; confidently returning incorrect results.) -- WHN 2002-03-08
1603 (unless (or (type-might-contain-other-types-p complement-type1
)
1604 (type-might-contain-other-types-p type2
))
1605 ;; Because of the way our types which don't contain other
1606 ;; types are disjoint subsets of the space of possible values,
1607 ;; (SUBTYPEP '(NOT AA) 'B)=NIL when AA and B are simple (and B
1608 ;; is not T, as checked above).
1609 (return (values nil t
)))
1610 ;; The old (TYPE= TYPE1 TYPE2) branch would never be taken, as
1611 ;; TYPE1 and TYPE2 will only be equal if they're both NOT types,
1612 ;; and then the :SIMPLE-SUBTYPEP method would be used instead.
1613 ;; But a CSUBTYPEP relationship might still hold:
1614 (multiple-value-bind (equal certain
)
1615 (csubtypep complement-type1 type2
)
1616 ;; If a is a subtype of b, ~a is not a subtype of b (unless
1617 ;; b=T, which was excluded above).
1619 (return (values nil nil
)))
1621 (return (values nil t
))))
1622 (multiple-value-bind (equal certain
)
1623 (csubtypep type2 complement-type1
)
1624 ;; If b is a subtype of a, ~a is not a subtype of b. (FIXME:
1625 ;; That's not true if a=T. Do we know at this point that a is
1628 (return (values nil nil
)))
1630 (return (values nil t
))))
1631 ;; old CSR comment ca. 0.7.2, now obsoleted by the SIMPLE-CTYPE?
1632 ;; KLUDGE case above: Other cases here would rely on being able
1633 ;; to catch all possible cases, which the fragility of this type
1634 ;; system doesn't inspire me; for instance, if a is type= to ~b,
1635 ;; then we want T, T; if this is not the case and the types are
1636 ;; disjoint (have an intersection of *empty-type*) then we want
1637 ;; NIL, T; else if the union of a and b is the *universal-type*
1638 ;; then we want T, T. So currently we still claim to be unsure
1639 ;; about e.g. (subtypep '(not fixnum) 'single-float).
1641 ;; OTOH we might still get here:
1644 (!define-type-method
(negation :complex-
=) (type1 type2
)
1645 ;; (NOT FOO) isn't equivalent to anything that's not a negation
1646 ;; type, except possibly a type that might contain it in disguise.
1647 (declare (ignore type2
))
1648 (if (type-might-contain-other-types-p type1
)
1652 (!define-type-method
(negation :simple-intersection2
) (type1 type2
)
1653 (let ((not1 (negation-type-type type1
))
1654 (not2 (negation-type-type type2
)))
1656 ((csubtypep not1 not2
) type2
)
1657 ((csubtypep not2 not1
) type1
)
1658 ;; Why no analagous clause to the disjoint in the SIMPLE-UNION2
1659 ;; method, below? The clause would read
1661 ;; ((EQ (TYPE-UNION NOT1 NOT2) *UNIVERSAL-TYPE*) *EMPTY-TYPE*)
1663 ;; but with proper canonicalization of negation types, there's
1664 ;; no way of constructing two negation types with union of their
1665 ;; negations being the universal type.
1667 (aver (not (eq (type-union not1 not2
) *universal-type
*)))
1670 (defun maybe-complex-array-refinement (type1 type2
)
1671 (let* ((ntype (negation-type-type type2
))
1672 (ndims (array-type-dimensions ntype
))
1673 (ncomplexp (array-type-complexp ntype
))
1674 (nseltype (array-type-specialized-element-type ntype
))
1675 (neltype (array-type-element-type ntype
)))
1676 (if (and (eql ndims
'*) (null ncomplexp
)
1677 (eq neltype
*wild-type
*) (eq nseltype
*wild-type
*))
1678 (make-array-type (array-type-dimensions type1
)
1680 :element-type
(array-type-element-type type1
)
1681 :specialized-element-type
(array-type-specialized-element-type type1
)))))
1683 (!define-type-method
(negation :complex-intersection2
) (type1 type2
)
1685 ((csubtypep type1
(negation-type-type type2
)) *empty-type
*)
1686 ((eq (type-intersection type1
(negation-type-type type2
)) *empty-type
*)
1688 ((and (array-type-p type1
) (array-type-p (negation-type-type type2
)))
1689 (maybe-complex-array-refinement type1 type2
))
1692 (!define-type-method
(negation :simple-union2
) (type1 type2
)
1693 (let ((not1 (negation-type-type type1
))
1694 (not2 (negation-type-type type2
)))
1696 ((csubtypep not1 not2
) type1
)
1697 ((csubtypep not2 not1
) type2
)
1698 ((eq (type-intersection not1 not2
) *empty-type
*)
1702 (!define-type-method
(negation :complex-union2
) (type1 type2
)
1704 ((csubtypep (negation-type-type type2
) type1
) *universal-type
*)
1705 ((eq (type-intersection type1
(negation-type-type type2
)) *empty-type
*)
1709 (!define-type-method
(negation :simple-
=) (type1 type2
)
1710 (type= (negation-type-type type1
) (negation-type-type type2
)))
1712 (!def-type-translator not
:list
((:context context
) typespec
)
1713 (type-negation (specifier-type-r context typespec
)))
1717 (declaim (inline numeric-type-equal
))
1718 (defun numeric-type-equal (type1 type2
)
1719 (and (eq (numeric-type-class type1
) (numeric-type-class type2
))
1720 (eq (numeric-type-format type1
) (numeric-type-format type2
))
1721 (eq (numeric-type-complexp type1
) (numeric-type-complexp type2
))))
1723 (!define-type-method
(number :simple-
=) (type1 type2
)
1725 (and (numeric-type-equal type1 type2
)
1726 (equalp (numeric-type-low type1
) (numeric-type-low type2
))
1727 (equalp (numeric-type-high type1
) (numeric-type-high type2
)))
1730 (!define-type-method
(number :negate
) (type)
1731 (if (and (null (numeric-type-low type
)) (null (numeric-type-high type
)))
1732 (make-negation-type type
)
1734 (make-negation-type (modified-numeric-type type
:low nil
:high nil
))
1736 ((null (numeric-type-low type
))
1737 (modified-numeric-type
1739 :low
(let ((h (numeric-type-high type
)))
1740 (if (consp h
) (car h
) (list h
)))
1742 ((null (numeric-type-high type
))
1743 (modified-numeric-type
1746 :high
(let ((l (numeric-type-low type
)))
1747 (if (consp l
) (car l
) (list l
)))))
1749 (modified-numeric-type
1752 :high
(let ((l (numeric-type-low type
)))
1753 (if (consp l
) (car l
) (list l
))))
1754 (modified-numeric-type
1756 :low
(let ((h (numeric-type-high type
)))
1757 (if (consp h
) (car h
) (list h
)))
1760 (!define-type-method
(number :unparse
) (type)
1761 (let* ((complexp (numeric-type-complexp type
))
1762 (low (numeric-type-low type
))
1763 (high (numeric-type-high type
))
1764 (base (case (numeric-type-class type
)
1766 (rational 'rational
)
1767 (float (or (numeric-type-format type
) 'float
))
1770 (cond ((and (eq base
'integer
) high low
)
1771 (let ((high-count (logcount high
))
1772 (high-length (integer-length high
)))
1774 (cond ((= high
0) '(integer 0 0))
1776 ((and (= high-count high-length
)
1777 (plusp high-length
))
1778 `(unsigned-byte ,high-length
))
1780 `(mod ,(1+ high
)))))
1781 ((and (= low sb
!xc
:most-negative-fixnum
)
1782 (= high sb
!xc
:most-positive-fixnum
))
1784 ((and (= low
(lognot high
))
1785 (= high-count high-length
)
1787 `(signed-byte ,(1+ high-length
)))
1789 `(integer ,low
,high
)))))
1790 (high `(,base
,(or low
'*) ,high
))
1792 (if (and (eq base
'integer
) (= low
0))
1800 (aver (neq base
+bounds
'real
))
1801 `(complex ,base
+bounds
))
1803 (aver (eq base
+bounds
'real
))
1806 (!define-type-method
(number :singleton-p
) (type)
1807 (let ((low (numeric-type-low type
))
1808 (high (numeric-type-high type
)))
1811 (eql (numeric-type-complexp type
) :real
)
1812 (member (numeric-type-class type
) '(integer rational
1813 #-sb-xc-host float
)))
1814 (values t
(numeric-type-low type
))
1817 ;;; Return true if X is "less than or equal" to Y, taking open bounds
1818 ;;; into consideration. CLOSED is the predicate used to test the bound
1819 ;;; on a closed interval (e.g. <=), and OPEN is the predicate used on
1820 ;;; open bounds (e.g. <). Y is considered to be the outside bound, in
1821 ;;; the sense that if it is infinite (NIL), then the test succeeds,
1822 ;;; whereas if X is infinite, then the test fails (unless Y is also
1825 ;;; This is for comparing bounds of the same kind, e.g. upper and
1826 ;;; upper. Use NUMERIC-BOUND-TEST* for different kinds of bounds.
1827 (defmacro numeric-bound-test
(x y closed open
)
1832 (,closed
(car ,x
) (car ,y
))
1833 (,closed
(car ,x
) ,y
)))
1839 ;;; This is used to compare upper and lower bounds. This is different
1840 ;;; from the same-bound case:
1841 ;;; -- Since X = NIL is -infinity, whereas y = NIL is +infinity, we
1842 ;;; return true if *either* arg is NIL.
1843 ;;; -- an open inner bound is "greater" and also squeezes the interval,
1844 ;;; causing us to use the OPEN test for those cases as well.
1845 (defmacro numeric-bound-test
* (x y closed open
)
1850 (,open
(car ,x
) (car ,y
))
1851 (,open
(car ,x
) ,y
)))
1857 ;;; Return whichever of the numeric bounds X and Y is "maximal"
1858 ;;; according to the predicates CLOSED (e.g. >=) and OPEN (e.g. >).
1859 ;;; This is only meaningful for maximizing like bounds, i.e. upper and
1860 ;;; upper. If MAX-P is true, then we return NIL if X or Y is NIL,
1861 ;;; otherwise we return the other arg.
1862 (defmacro numeric-bound-max
(x y closed open max-p
)
1865 `(cond ((not ,n-x
) ,(if max-p nil n-y
))
1866 ((not ,n-y
) ,(if max-p nil n-x
))
1869 (if (,closed
(car ,n-x
) (car ,n-y
)) ,n-x
,n-y
)
1870 (if (,open
(car ,n-x
) ,n-y
) ,n-x
,n-y
)))
1873 (if (,open
(car ,n-y
) ,n-x
) ,n-y
,n-x
)
1874 (if (,closed
,n-y
,n-x
) ,n-y
,n-x
))))))
1876 (!define-type-method
(number :simple-subtypep
) (type1 type2
)
1877 (let ((class1 (numeric-type-class type1
))
1878 (class2 (numeric-type-class type2
))
1879 (complexp2 (numeric-type-complexp type2
))
1880 (format2 (numeric-type-format type2
))
1881 (low1 (numeric-type-low type1
))
1882 (high1 (numeric-type-high type1
))
1883 (low2 (numeric-type-low type2
))
1884 (high2 (numeric-type-high type2
)))
1885 ;; If one is complex and the other isn't, they are disjoint.
1886 (cond ((not (or (eq (numeric-type-complexp type1
) complexp2
)
1889 ;; If the classes are specified and different, the types are
1890 ;; disjoint unless type2 is RATIONAL and type1 is INTEGER.
1891 ;; [ or type1 is INTEGER and type2 is of the form (RATIONAL
1892 ;; X X) for integral X, but this is dealt with in the
1893 ;; canonicalization inside MAKE-NUMERIC-TYPE ]
1894 ((not (or (eq class1 class2
)
1896 (and (eq class1
'integer
) (eq class2
'rational
))))
1898 ;; If the float formats are specified and different, the types
1900 ((not (or (eq (numeric-type-format type1
) format2
)
1903 ;; Check the bounds.
1904 ((and (numeric-bound-test low1 low2
>= >)
1905 (numeric-bound-test high1 high2
<= <))
1910 (!define-superclasses number
((number)) !cold-init-forms
)
1912 ;;; If the high bound of LOW is adjacent to the low bound of HIGH,
1913 ;;; then return true, otherwise NIL.
1914 (defun numeric-types-adjacent (low high
)
1915 (let ((low-bound (numeric-type-high low
))
1916 (high-bound (numeric-type-low high
)))
1917 (cond ((not (and low-bound high-bound
)) nil
)
1918 ((and (consp low-bound
) (consp high-bound
)) nil
)
1920 (let ((low-value (car low-bound
)))
1921 (or (eql low-value high-bound
)
1923 (load-time-value (make-unportable-float
1924 :single-float-negative-zero
)))
1925 (eql high-bound
0f0
))
1926 (and (eql low-value
0f0
)
1928 (load-time-value (make-unportable-float
1929 :single-float-negative-zero
))))
1931 (load-time-value (make-unportable-float
1932 :double-float-negative-zero
)))
1933 (eql high-bound
0d0
))
1934 (and (eql low-value
0d0
)
1936 (load-time-value (make-unportable-float
1937 :double-float-negative-zero
)))))))
1939 (let ((high-value (car high-bound
)))
1940 (or (eql high-value low-bound
)
1941 (and (eql high-value
1942 (load-time-value (make-unportable-float
1943 :single-float-negative-zero
)))
1944 (eql low-bound
0f0
))
1945 (and (eql high-value
0f0
)
1947 (load-time-value (make-unportable-float
1948 :single-float-negative-zero
))))
1949 (and (eql high-value
1950 (load-time-value (make-unportable-float
1951 :double-float-negative-zero
)))
1952 (eql low-bound
0d0
))
1953 (and (eql high-value
0d0
)
1955 (load-time-value (make-unportable-float
1956 :double-float-negative-zero
)))))))
1957 ((and (eq (numeric-type-class low
) 'integer
)
1958 (eq (numeric-type-class high
) 'integer
))
1959 (eql (1+ low-bound
) high-bound
))
1963 ;;; Return a numeric type that is a supertype for both TYPE1 and TYPE2.
1965 ;;; Binding *APPROXIMATE-NUMERIC-UNIONS* to T allows merging non-adjacent
1966 ;;; numeric types, eg (OR (INTEGER 0 12) (INTEGER 20 128)) => (INTEGER 0 128),
1967 ;;; the compiler does this occasionally during type-derivation to avoid
1968 ;;; creating absurdly complex unions of numeric types.
1969 (defvar *approximate-numeric-unions
* nil
)
1971 (!define-type-method
(number :simple-union2
) (type1 type2
)
1972 (declare (type numeric-type type1 type2
))
1973 (cond ((csubtypep type1 type2
) type2
)
1974 ((csubtypep type2 type1
) type1
)
1976 (let ((class1 (numeric-type-class type1
))
1977 (format1 (numeric-type-format type1
))
1978 (complexp1 (numeric-type-complexp type1
))
1979 (class2 (numeric-type-class type2
))
1980 (format2 (numeric-type-format type2
))
1981 (complexp2 (numeric-type-complexp type2
)))
1983 ((and (eq class1 class2
)
1984 (eq format1 format2
)
1985 (eq complexp1 complexp2
)
1986 (or *approximate-numeric-unions
*
1987 (numeric-types-intersect type1 type2
)
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
)
2000 ;; FIXME: These two clauses are almost identical, and the
2001 ;; consequents are in fact identical in every respect.
2002 ((and (eq class1
'rational
)
2003 (eq class2
'integer
)
2004 (eq format1 format2
)
2005 (eq complexp1 complexp2
)
2006 (integerp (numeric-type-low type2
))
2007 (integerp (numeric-type-high type2
))
2008 (= (numeric-type-low type2
) (numeric-type-high type2
))
2009 (or *approximate-numeric-unions
*
2010 (numeric-types-adjacent type1 type2
)
2011 (numeric-types-adjacent type2 type1
)))
2016 :low
(numeric-bound-max (numeric-type-low type1
)
2017 (numeric-type-low type2
)
2019 :high
(numeric-bound-max (numeric-type-high type1
)
2020 (numeric-type-high type2
)
2022 ((and (eq class1
'integer
)
2023 (eq class2
'rational
)
2024 (eq format1 format2
)
2025 (eq complexp1 complexp2
)
2026 (integerp (numeric-type-low type1
))
2027 (integerp (numeric-type-high type1
))
2028 (= (numeric-type-low type1
) (numeric-type-high type1
))
2029 (or *approximate-numeric-unions
*
2030 (numeric-types-adjacent type1 type2
)
2031 (numeric-types-adjacent type2 type1
)))
2036 :low
(numeric-bound-max (numeric-type-low type1
)
2037 (numeric-type-low type2
)
2039 :high
(numeric-bound-max (numeric-type-high type1
)
2040 (numeric-type-high type2
)
2045 (!cold-init-forms
;; is !PRECOMPUTE-TYPES not doing the right thing?
2046 (setf (info :type
:kind
'number
) :primitive
)
2047 (setf (info :type
:builtin
'number
)
2048 (make-numeric-type :complexp nil
)))
2050 (!def-type-translator complex
((:context context
) &optional
(typespec '*))
2051 (if (eq typespec
'*)
2052 (specifier-type '(complex real
))
2053 (labels ((not-numeric ()
2054 (error "The component type for COMPLEX is not numeric: ~S"
2057 (error "The component type for COMPLEX is not a subtype of REAL: ~S"
2059 (complex1 (component-type)
2060 (unless (numeric-type-p component-type
)
2062 (when (eq (numeric-type-complexp component-type
) :complex
)
2064 (if (csubtypep component-type
(specifier-type '(eql 0)))
2066 (modified-numeric-type component-type
2067 :complexp
:complex
)))
2070 ((eq ctype
*empty-type
*) *empty-type
*)
2071 ((eq ctype
*universal-type
*) (not-real))
2072 ((typep ctype
'numeric-type
) (complex1 ctype
))
2073 ((typep ctype
'union-type
)
2075 (mapcar #'do-complex
(union-type-types ctype
))))
2076 ((typep ctype
'member-type
)
2078 (mapcar-member-type-members
2079 (lambda (x) (do-complex (ctype-of x
)))
2081 ((and (typep ctype
'intersection-type
)
2082 ;; FIXME: This is very much a
2083 ;; not-quite-worst-effort, but we are required to do
2084 ;; something here because of our representation of
2085 ;; RATIO as (AND RATIONAL (NOT INTEGER)): we must
2086 ;; allow users to ask about (COMPLEX RATIO). This
2087 ;; will of course fail to work right on such types
2088 ;; as (AND INTEGER (SATISFIES ZEROP))...
2089 (let ((numbers (remove-if-not
2091 (intersection-type-types ctype
))))
2093 (null (cdr numbers
))
2094 (eq (numeric-type-complexp (car numbers
)) :real
)
2095 (complex1 (car numbers
))))))
2097 (multiple-value-bind (subtypep certainly
)
2098 (csubtypep ctype
(specifier-type 'real
))
2099 (if (and (not subtypep
) certainly
)
2101 ;; ANSI just says that TYPESPEC is any subtype of
2102 ;; type REAL, not necessarily a NUMERIC-TYPE. In
2103 ;; particular, at this point TYPESPEC could legally
2104 ;; be a hairy type like (AND NUMBER (SATISFIES
2105 ;; REALP) (SATISFIES ZEROP)), in which case we fall
2106 ;; through the logic above and end up here,
2108 ;; FIXME: (COMPLEX NUMBER) is not rejected but should
2109 ;; be, as NUMBER is clearly not a subtype of real.
2110 (bug "~@<(known bug #145): The type ~S is too hairy to be ~
2111 used for a COMPLEX component.~:@>"
2113 (let ((ctype (specifier-type-r context typespec
)))
2114 (do-complex ctype
)))))
2116 ;;; If X is *, return NIL, otherwise return the bound, which must be a
2117 ;;; member of TYPE or a one-element list of a member of TYPE.
2118 #!-sb-fluid
(declaim (inline canonicalized-bound
))
2119 (defun canonicalized-bound (bound type
)
2120 (cond ((eq bound
'*) nil
)
2121 ((or (sb!xc
:typep bound type
)
2123 (sb!xc
:typep
(car bound
) type
)
2124 (null (cdr bound
))))
2127 (error "Bound is not ~S, a ~S or a list of a ~S: ~S"
2133 (!def-type-translator integer
(&optional
(low '*) (high '*))
2134 (let* ((l (canonicalized-bound low
'integer
))
2135 (lb (if (consp l
) (1+ (car l
)) l
))
2136 (h (canonicalized-bound high
'integer
))
2137 (hb (if (consp h
) (1- (car h
)) h
)))
2138 (if (and hb lb
(< hb lb
))
2140 (make-numeric-type :class
'integer
2142 :enumerable
(not (null (and l h
)))
2146 (defmacro !def-bounded-type
(type class format
)
2147 `(!def-type-translator
,type
(&optional
(low '*) (high '*))
2148 (let ((lb (canonicalized-bound low
',type
))
2149 (hb (canonicalized-bound high
',type
)))
2150 (if (not (numeric-bound-test* lb hb
<= <))
2152 (make-numeric-type :class
',class
2157 (!def-bounded-type rational rational nil
)
2159 ;;; Unlike CMU CL, we represent the types FLOAT and REAL as
2160 ;;; UNION-TYPEs of more primitive types, in order to make
2161 ;;; type representation more unique, avoiding problems in the
2162 ;;; simplification of things like
2163 ;;; (subtypep '(or (single-float -1.0 1.0) (single-float 0.1))
2164 ;;; '(or (real -1 7) (single-float 0.1) (single-float -1.0 1.0)))
2165 ;;; When we allowed REAL to remain as a separate NUMERIC-TYPE,
2166 ;;; it was too easy for the first argument to be simplified to
2167 ;;; '(SINGLE-FLOAT -1.0), and for the second argument to be simplified
2168 ;;; to '(OR (REAL -1 7) (SINGLE-FLOAT 0.1)) and then for the
2169 ;;; SUBTYPEP to fail (returning NIL,T instead of T,T) because
2170 ;;; the first argument can't be seen to be a subtype of any of the
2171 ;;; terms in the second argument.
2173 ;;; The old CMU CL way was:
2174 ;;; (!def-bounded-type float float nil)
2175 ;;; (!def-bounded-type real nil nil)
2177 ;;; FIXME: If this new way works for a while with no weird new
2178 ;;; problems, we can go back and rip out support for separate FLOAT
2179 ;;; and REAL flavors of NUMERIC-TYPE. The new way was added in
2180 ;;; sbcl-0.6.11.22, 2001-03-21.
2182 ;;; FIXME: It's probably necessary to do something to fix the
2183 ;;; analogous problem with INTEGER and RATIONAL types. Perhaps
2184 ;;; bounded RATIONAL types should be represented as (OR RATIO INTEGER).
2185 (defun coerce-bound (bound type upperp inner-coerce-bound-fun
)
2186 (declare (type function inner-coerce-bound-fun
))
2189 (funcall inner-coerce-bound-fun bound type upperp
)))
2190 (defun inner-coerce-real-bound (bound type upperp
)
2191 #+sb-xc-host
(declare (ignore upperp
))
2192 (let #+sb-xc-host
()
2194 ((nl (load-time-value (symbol-value 'sb
!xc
:most-negative-long-float
)))
2195 (pl (load-time-value (symbol-value 'sb
!xc
:most-positive-long-float
))))
2196 (let ((nbound (if (consp bound
) (car bound
) bound
))
2197 (consp (consp bound
)))
2201 (list (rational nbound
))
2205 ((floatp nbound
) bound
)
2207 ;; Coerce to the widest float format available, to avoid
2208 ;; unnecessary loss of precision, but don't coerce
2209 ;; unrepresentable numbers, except on the host where we
2210 ;; shouldn't be making these types (but KLUDGE: can't even
2211 ;; assert portably that we're not).
2215 (when (< nbound nl
) (return-from inner-coerce-real-bound nl
)))
2217 (when (> nbound pl
) (return-from inner-coerce-real-bound pl
))))
2218 (let ((result (coerce nbound
'long-float
)))
2219 (if consp
(list result
) result
)))))))))
2220 (defun inner-coerce-float-bound (bound type upperp
)
2221 #+sb-xc-host
(declare (ignore upperp
))
2222 (let #+sb-xc-host
()
2224 ((nd (load-time-value (symbol-value 'sb
!xc
:most-negative-double-float
)))
2225 (pd (load-time-value (symbol-value 'sb
!xc
:most-positive-double-float
)))
2226 (ns (load-time-value (symbol-value 'sb
!xc
:most-negative-single-float
)))
2227 (ps (load-time-value
2228 (symbol-value 'sb
!xc
:most-positive-single-float
))))
2229 (let ((nbound (if (consp bound
) (car bound
) bound
))
2230 (consp (consp bound
)))
2234 ((typep nbound
'single-float
) bound
)
2239 (when (< nbound ns
) (return-from inner-coerce-float-bound ns
)))
2241 (when (> nbound ps
) (return-from inner-coerce-float-bound ps
))))
2242 (let ((result (coerce nbound
'single-float
)))
2243 (if consp
(list result
) result
)))))
2246 ((typep nbound
'double-float
) bound
)
2251 (when (< nbound nd
) (return-from inner-coerce-float-bound nd
)))
2253 (when (> nbound pd
) (return-from inner-coerce-float-bound pd
))))
2254 (let ((result (coerce nbound
'double-float
)))
2255 (if consp
(list result
) result
)))))))))
2256 (defun coerced-real-bound (bound type upperp
)
2257 (coerce-bound bound type upperp
#'inner-coerce-real-bound
))
2258 (defun coerced-float-bound (bound type upperp
)
2259 (coerce-bound bound type upperp
#'inner-coerce-float-bound
))
2260 (!def-type-translator real
(&optional
(low '*) (high '*))
2261 (specifier-type `(or (float ,(coerced-real-bound low
'float nil
)
2262 ,(coerced-real-bound high
'float t
))
2263 (rational ,(coerced-real-bound low
'rational nil
)
2264 ,(coerced-real-bound high
'rational t
)))))
2265 (!def-type-translator float
(&optional
(low '*) (high '*))
2267 `(or (single-float ,(coerced-float-bound low
'single-float nil
)
2268 ,(coerced-float-bound high
'single-float t
))
2269 (double-float ,(coerced-float-bound low
'double-float nil
)
2270 ,(coerced-float-bound high
'double-float t
))
2271 #!+long-float
,(error "stub: no long float support yet"))))
2273 (defmacro !define-float-format
(f)
2274 `(!def-bounded-type
,f float
,f
))
2276 ;; (!define-float-format short-float) ; it's a DEFTYPE
2277 (!define-float-format single-float
)
2278 (!define-float-format double-float
)
2279 ;; long-float support is dead.
2280 ;; (!define-float-format long-float) ; also a DEFTYPE
2282 (defun numeric-types-intersect (type1 type2
)
2283 (declare (type numeric-type type1 type2
))
2284 (let* ((class1 (numeric-type-class type1
))
2285 (class2 (numeric-type-class type2
))
2286 (complexp1 (numeric-type-complexp type1
))
2287 (complexp2 (numeric-type-complexp type2
))
2288 (format1 (numeric-type-format type1
))
2289 (format2 (numeric-type-format type2
))
2290 (low1 (numeric-type-low type1
))
2291 (high1 (numeric-type-high type1
))
2292 (low2 (numeric-type-low type2
))
2293 (high2 (numeric-type-high type2
)))
2294 ;; If one is complex and the other isn't, then they are disjoint.
2295 (cond ((not (or (eq complexp1 complexp2
)
2296 (null complexp1
) (null complexp2
)))
2298 ;; If either type is a float, then the other must either be
2299 ;; specified to be a float or unspecified. Otherwise, they
2301 ((and (eq class1
'float
)
2302 (not (member class2
'(float nil
)))) nil
)
2303 ((and (eq class2
'float
)
2304 (not (member class1
'(float nil
)))) nil
)
2305 ;; If the float formats are specified and different, the
2306 ;; types are disjoint.
2307 ((not (or (eq format1 format2
) (null format1
) (null format2
)))
2310 ;; Check the bounds. This is a bit odd because we must
2311 ;; always have the outer bound of the interval as the
2313 (if (numeric-bound-test high1 high2
<= <)
2314 (or (and (numeric-bound-test low1 low2
>= >)
2315 (numeric-bound-test* low1 high2
<= <))
2316 (and (numeric-bound-test low2 low1
>= >)
2317 (numeric-bound-test* low2 high1
<= <)))
2318 (or (and (numeric-bound-test* low2 high1
<= <)
2319 (numeric-bound-test low2 low1
>= >))
2320 (and (numeric-bound-test high2 high1
<= <)
2321 (numeric-bound-test* high2 low1
>= >))))))))
2323 ;;; Take the numeric bound X and convert it into something that can be
2324 ;;; used as a bound in a numeric type with the specified CLASS and
2325 ;;; FORMAT. If UP-P is true, then we round up as needed, otherwise we
2326 ;;; round down. UP-P true implies that X is a lower bound, i.e. (N) > N.
2328 ;;; This is used by NUMERIC-TYPE-INTERSECTION to mash the bound into
2329 ;;; the appropriate type number. X may only be a float when CLASS is
2332 ;;; ### Note: it is possible for the coercion to a float to overflow
2333 ;;; or underflow. This happens when the bound doesn't fit in the
2334 ;;; specified format. In this case, we should really return the
2335 ;;; appropriate {Most | Least}-{Positive | Negative}-XXX-Float float
2336 ;;; of desired format. But these conditions aren't currently signalled
2337 ;;; in any useful way.
2339 ;;; Also, when converting an open rational bound into a float we
2340 ;;; should probably convert it to a closed bound of the closest float
2341 ;;; in the specified format. KLUDGE: In general, open float bounds are
2342 ;;; screwed up. -- (comment from original CMU CL)
2343 (defun round-numeric-bound (x class format up-p
)
2345 (let ((cx (if (consp x
) (car x
) x
)))
2349 (if (and (consp x
) (integerp cx
))
2350 (if up-p
(1+ cx
) (1- cx
))
2351 (if up-p
(ceiling cx
) (floor cx
))))
2355 ((and format
(subtypep format
'double-float
))
2356 (if (<= most-negative-double-float cx most-positive-double-float
)
2360 (if (<= most-negative-single-float cx most-positive-single-float
)
2362 (coerce cx
(or format
'single-float
))
2364 (if (consp x
) (list res
) res
)))))
2367 ;;; Handle the case of type intersection on two numeric types. We use
2368 ;;; TYPES-EQUAL-OR-INTERSECT to throw out the case of types with no
2369 ;;; intersection. If an attribute in TYPE1 is unspecified, then we use
2370 ;;; TYPE2's attribute, which must be at least as restrictive. If the
2371 ;;; types intersect, then the only attributes that can be specified
2372 ;;; and different are the class and the bounds.
2374 ;;; When the class differs, we use the more restrictive class. The
2375 ;;; only interesting case is RATIONAL/INTEGER, since RATIONAL includes
2378 ;;; We make the result lower (upper) bound the maximum (minimum) of
2379 ;;; the argument lower (upper) bounds. We convert the bounds into the
2380 ;;; appropriate numeric type before maximizing. This avoids possible
2381 ;;; confusion due to mixed-type comparisons (but I think the result is
2383 (!define-type-method
(number :simple-intersection2
) (type1 type2
)
2384 (declare (type numeric-type type1 type2
))
2385 (if (numeric-types-intersect type1 type2
)
2386 (let* ((class1 (numeric-type-class type1
))
2387 (class2 (numeric-type-class type2
))
2388 (class (ecase class1
2390 ((integer float
) class1
)
2391 (rational (if (eq class2
'integer
)
2394 (format (or (numeric-type-format type1
)
2395 (numeric-type-format type2
))))
2399 :complexp
(or (numeric-type-complexp type1
)
2400 (numeric-type-complexp type2
))
2401 :low
(numeric-bound-max
2402 (round-numeric-bound (numeric-type-low type1
)
2404 (round-numeric-bound (numeric-type-low type2
)
2407 :high
(numeric-bound-max
2408 (round-numeric-bound (numeric-type-high type1
)
2410 (round-numeric-bound (numeric-type-high type2
)
2415 ;;; Given two float formats, return the one with more precision. If
2416 ;;; either one is null, return NIL.
2417 (defun float-format-max (f1 f2
)
2419 (dolist (f *float-formats
* (error "bad float format: ~S" f1
))
2420 (when (or (eq f f1
) (eq f f2
))
2423 ;;; Return the result of an operation on TYPE1 and TYPE2 according to
2424 ;;; the rules of numeric contagion. This is always NUMBER, some float
2425 ;;; format (possibly complex) or RATIONAL. Due to rational
2426 ;;; canonicalization, there isn't much we can do here with integers or
2427 ;;; rational complex numbers.
2429 ;;; If either argument is not a NUMERIC-TYPE, then return NUMBER. This
2430 ;;; is useful mainly for allowing types that are technically numbers,
2431 ;;; but not a NUMERIC-TYPE.
2432 (defun numeric-contagion (type1 type2
)
2433 (if (and (numeric-type-p type1
) (numeric-type-p type2
))
2434 (let ((class1 (numeric-type-class type1
))
2435 (class2 (numeric-type-class type2
))
2436 (format1 (numeric-type-format type1
))
2437 (format2 (numeric-type-format type2
))
2438 (complexp1 (numeric-type-complexp type1
))
2439 (complexp2 (numeric-type-complexp type2
)))
2440 (cond ((or (null complexp1
)
2442 (specifier-type 'number
))
2446 :format
(ecase class2
2447 (float (float-format-max format1 format2
))
2448 ((integer rational
) format1
)
2450 ;; A double-float with any real number is a
2453 (if (eq format1
'double-float
)
2456 ;; A long-float with any real number is a
2459 (if (eq format1
'long-float
)
2462 :complexp
(if (or (eq complexp1
:complex
)
2463 (eq complexp2
:complex
))
2466 ((eq class2
'float
) (numeric-contagion type2 type1
))
2467 ((and (eq complexp1
:real
) (eq complexp2
:real
))
2469 :class
(and class1 class2
'rational
)
2472 (specifier-type 'number
))))
2473 (specifier-type 'number
)))
2477 (!define-type-method
(array :simple-
=) (type1 type2
)
2478 (cond ((not (and (equal (array-type-dimensions type1
)
2479 (array-type-dimensions type2
))
2480 (eq (array-type-complexp type1
)
2481 (array-type-complexp type2
))))
2483 ((or (unknown-type-p (array-type-element-type type1
))
2484 (unknown-type-p (array-type-element-type type2
)))
2485 (type= (array-type-element-type type1
)
2486 (array-type-element-type type2
)))
2488 (values (type= (array-type-specialized-element-type type1
)
2489 (array-type-specialized-element-type type2
))
2492 (!define-type-method
(array :negate
) (type)
2493 ;; FIXME (and hint to PFD): we're vulnerable here to attacks of the
2494 ;; form "are (AND ARRAY (NOT (ARRAY T))) and (OR (ARRAY BIT) (ARRAY
2495 ;; NIL) (ARRAY CHAR) ...) equivalent?" -- CSR, 2003-12-10
2496 ;; A symptom of the aforementioned is that the following are not TYPE=
2497 ;; (AND (VECTOR T) (NOT SIMPLE-ARRAY)) ; an ARRAY-TYPE
2498 ;; (AND (VECTOR T) (NOT SIMPLE-VECTOR)) ; an INTERSECTION-TYPE
2499 ;; even though (VECTOR T) makes it so that the (NOT) clause in each can
2500 ;; only provide one additional bit of information: that the vector
2501 ;; is complex as opposed to simple. The rank and element-type are fixed.
2502 (if (and (eq (array-type-dimensions type
) '*)
2503 (eq (array-type-complexp type
) 't
)
2504 (eq (array-type-element-type type
) *wild-type
*))
2505 ;; (NOT <hairy-array>) = either SIMPLE-ARRAY or (NOT ARRAY).
2506 ;; This is deliberately asymmetric - trying to say that NOT simple-array
2507 ;; equals hairy-array leads to infinite recursion.
2508 (type-union (make-array-type '* :complexp nil
2509 :element-type
*wild-type
*)
2511 (make-array-type '* :element-type
*wild-type
*)))
2512 (make-negation-type type
)))
2514 (!define-type-method
(array :unparse
) (type)
2515 (let* ((dims (array-type-dimensions type
))
2516 ;; Compare the specialised element type and the
2517 ;; derived element type. If the derived type
2518 ;; is so small that it jumps to a smaller upgraded
2519 ;; element type, use the specialised element type.
2521 ;; This protects from unparsing
2522 ;; (and (vector (or bit symbol))
2523 ;; (vector (or bit character)))
2524 ;; i.e., the intersection of two T array types,
2526 (stype (array-type-specialized-element-type type
))
2527 (dtype (array-type-element-type type
))
2528 (utype (%upgraded-array-element-type dtype
))
2529 (eltype (type-specifier (if (type= stype utype
)
2532 (complexp (array-type-complexp type
)))
2533 (if (and (eq complexp t
) (not *unparse-allow-negation
*))
2534 (setq complexp
:maybe
))
2538 ((t) '(and array
(not simple-array
)))
2540 ((nil) 'simple-array
))
2542 ((t) `(and (array ,eltype
) (not simple-array
)))
2543 ((:maybe
) `(array ,eltype
))
2544 ((nil) `(simple-array ,eltype
)))))
2545 ((= (length dims
) 1)
2548 (if (eq (car dims
) '*)
2551 ((base-char #!-sb-unicode character
) 'base-string
)
2553 (t `(vector ,eltype
)))
2555 (bit `(bit-vector ,(car dims
)))
2556 ((base-char #!-sb-unicode character
)
2557 `(base-string ,(car dims
)))
2558 (t `(vector ,eltype
,(car dims
)))))))
2559 (if (eql complexp
:maybe
)
2561 `(and ,answer
(not simple-array
))))
2562 (if (eq (car dims
) '*)
2564 (bit 'simple-bit-vector
)
2565 ((base-char #!-sb-unicode character
) 'simple-base-string
)
2566 ((t) 'simple-vector
)
2567 (t `(simple-array ,eltype
(*))))
2569 (bit `(simple-bit-vector ,(car dims
)))
2570 ((base-char #!-sb-unicode character
)
2571 `(simple-base-string ,(car dims
)))
2572 ((t) `(simple-vector ,(car dims
)))
2573 (t `(simple-array ,eltype
,dims
))))))
2576 ((t) `(and (array ,eltype
,dims
) (not simple-array
)))
2577 ((:maybe
) `(array ,eltype
,dims
))
2578 ((nil) `(simple-array ,eltype
,dims
)))))))
2580 (!define-type-method
(array :simple-subtypep
) (type1 type2
)
2581 (let ((dims1 (array-type-dimensions type1
))
2582 (dims2 (array-type-dimensions type2
))
2583 (complexp2 (array-type-complexp type2
)))
2584 (cond (;; not subtypep unless dimensions are compatible
2585 (not (or (eq dims2
'*)
2586 (and (not (eq dims1
'*))
2587 ;; (sbcl-0.6.4 has trouble figuring out that
2588 ;; DIMS1 and DIMS2 must be lists at this
2589 ;; point, and knowing that is important to
2590 ;; compiling EVERY efficiently.)
2591 (= (length (the list dims1
))
2592 (length (the list dims2
)))
2593 (every (lambda (x y
)
2594 (or (eq y
'*) (eql x y
)))
2596 (the list dims2
)))))
2598 ;; not subtypep unless complexness is compatible
2599 ((not (or (eq complexp2
:maybe
)
2600 (eq (array-type-complexp type1
) complexp2
)))
2602 ;; Since we didn't fail any of the tests above, we win
2603 ;; if the TYPE2 element type is wild.
2604 ((eq (array-type-element-type type2
) *wild-type
*)
2606 (;; Since we didn't match any of the special cases above, if
2607 ;; either element type is unknown we can only give a good
2608 ;; answer if they are the same.
2609 (or (unknown-type-p (array-type-element-type type1
))
2610 (unknown-type-p (array-type-element-type type2
)))
2611 (if (type= (array-type-element-type type1
)
2612 (array-type-element-type type2
))
2615 (;; Otherwise, the subtype relationship holds iff the
2616 ;; types are equal, and they're equal iff the specialized
2617 ;; element types are identical.
2619 (values (type= (array-type-specialized-element-type type1
)
2620 (array-type-specialized-element-type type2
))
2623 (!define-superclasses array
2624 ((vector vector
) (array))
2627 (defun array-types-intersect (type1 type2
)
2628 (declare (type array-type type1 type2
))
2629 (let ((dims1 (array-type-dimensions type1
))
2630 (dims2 (array-type-dimensions type2
))
2631 (complexp1 (array-type-complexp type1
))
2632 (complexp2 (array-type-complexp type2
)))
2633 ;; See whether dimensions are compatible.
2634 (cond ((not (or (eq dims1
'*) (eq dims2
'*)
2635 (and (= (length dims1
) (length dims2
))
2636 (every (lambda (x y
)
2637 (or (eq x
'*) (eq y
'*) (= x y
)))
2640 ;; See whether complexpness is compatible.
2641 ((not (or (eq complexp1
:maybe
)
2642 (eq complexp2
:maybe
)
2643 (eq complexp1 complexp2
)))
2647 ;; If either element type is wild, then they intersect.
2648 ;; Otherwise, the types must be identical.
2650 ;; FIXME: There seems to have been a fair amount of
2651 ;; confusion about the distinction between requested element
2652 ;; type and specialized element type; here is one of
2653 ;; them. If we request an array to hold objects of an
2654 ;; unknown type, we can do no better than represent that
2655 ;; type as an array specialized on wild-type. We keep the
2656 ;; requested element-type in the -ELEMENT-TYPE slot, and
2657 ;; *WILD-TYPE* in the -SPECIALIZED-ELEMENT-TYPE. So, here,
2658 ;; we must test for the SPECIALIZED slot being *WILD-TYPE*,
2659 ;; not just the ELEMENT-TYPE slot. Maybe the return value
2660 ;; in that specific case should be T, NIL? Or maybe this
2661 ;; function should really be called
2662 ;; ARRAY-TYPES-COULD-POSSIBLY-INTERSECT? In any case, this
2663 ;; was responsible for bug #123, and this whole issue could
2664 ;; do with a rethink and/or a rewrite. -- CSR, 2002-08-21
2665 ((or (eq (array-type-specialized-element-type type1
) *wild-type
*)
2666 (eq (array-type-specialized-element-type type2
) *wild-type
*)
2667 (type= (array-type-specialized-element-type type1
)
2668 (array-type-specialized-element-type type2
)))
2674 (defun unite-array-types-complexp (type1 type2
)
2675 (let ((complexp1 (array-type-complexp type1
))
2676 (complexp2 (array-type-complexp type2
)))
2678 ((eq complexp1 complexp2
)
2679 ;; both types are the same complexp-ity
2680 (values complexp1 t
))
2681 ((eq complexp1
:maybe
)
2682 ;; type1 is wild-complexp
2683 (values :maybe type1
))
2684 ((eq complexp2
:maybe
)
2685 ;; type2 is wild-complexp
2686 (values :maybe type2
))
2688 ;; both types partition the complexp-space
2689 (values :maybe nil
)))))
2691 (defun unite-array-types-dimensions (type1 type2
)
2692 (let ((dims1 (array-type-dimensions type1
))
2693 (dims2 (array-type-dimensions type2
)))
2694 (cond ((equal dims1 dims2
)
2695 ;; both types are same dimensionality
2698 ;; type1 is wild-dimensions
2701 ;; type2 is wild-dimensions
2703 ((not (= (length dims1
) (length dims2
)))
2704 ;; types have different number of dimensions
2705 (values :incompatible nil
))
2707 ;; we need to check on a per-dimension basis
2708 (let* ((supertype1 t
)
2711 (result (mapcar (lambda (dim1 dim2
)
2716 (setf supertype2 nil
)
2719 (setf supertype1 nil
)
2722 (setf compatible nil
))))
2725 ((or (not compatible
)
2726 (and (not supertype1
)
2728 (values :incompatible nil
))
2729 ((and supertype1 supertype2
)
2730 (values result supertype1
))
2732 (values result
(if supertype1 type1 type2
)))))))))
2734 (defun unite-array-types-element-types (type1 type2
)
2735 ;; FIXME: We'd love to be able to unite the full set of specialized
2736 ;; array element types up to *wild-type*, but :simple-union2 is
2737 ;; performed pairwise, so we don't have a good hook for it and our
2738 ;; representation doesn't allow us to easily detect the situation
2740 ;; But see SIMPLIFY-ARRAY-UNIONS which is able to do something like that.
2741 (let* ((eltype1 (array-type-element-type type1
))
2742 (eltype2 (array-type-element-type type2
))
2743 (stype1 (array-type-specialized-element-type type1
))
2744 (stype2 (array-type-specialized-element-type type2
))
2745 (wild1 (eq eltype1
*wild-type
*))
2746 (wild2 (eq eltype2
*wild-type
*)))
2748 ((type= eltype1 eltype2
)
2749 (values eltype1 stype1 t
))
2751 (values eltype1 stype1 type1
))
2753 (values eltype2 stype2 type2
))
2754 ((not (type= stype1 stype2
))
2755 ;; non-wild types that don't share UAET don't unite
2756 (values :incompatible nil nil
))
2757 ((csubtypep eltype1 eltype2
)
2758 (values eltype2 stype2 type2
))
2759 ((csubtypep eltype2 eltype1
)
2760 (values eltype1 stype1 type1
))
2762 (values :incompatible nil nil
)))))
2764 (defun unite-array-types-supertypes-compatible-p (&rest supertypes
)
2765 ;; supertypes are compatible if they are all T, if there is a single
2766 ;; NIL and all the rest are T, or if all non-T supertypes are the
2767 ;; same and not NIL.
2768 (let ((interesting-supertypes
2769 (remove t supertypes
)))
2770 (or (not interesting-supertypes
)
2771 (equal interesting-supertypes
'(nil))
2772 ;; supertypes are (OR BOOLEAN ARRAY-TYPE), so...
2773 (typep (remove-duplicates interesting-supertypes
)
2774 '(cons array-type null
)))))
2776 (!define-type-method
(array :simple-union2
) (type1 type2
)
2777 (multiple-value-bind
2778 (result-eltype result-stype eltype-supertype
)
2779 (unite-array-types-element-types type1 type2
)
2780 (multiple-value-bind
2781 (result-complexp complexp-supertype
)
2782 (unite-array-types-complexp type1 type2
)
2783 (multiple-value-bind
2784 (result-dimensions dimensions-supertype
)
2785 (unite-array-types-dimensions type1 type2
)
2786 (when (and (not (eq result-dimensions
:incompatible
))
2787 (not (eq result-eltype
:incompatible
))
2788 (unite-array-types-supertypes-compatible-p
2789 eltype-supertype complexp-supertype dimensions-supertype
))
2790 (make-array-type result-dimensions
2791 :complexp result-complexp
2792 :element-type result-eltype
2793 :specialized-element-type result-stype
))))))
2795 (!define-type-method
(array :simple-intersection2
) (type1 type2
)
2796 (declare (type array-type type1 type2
))
2797 (if (array-types-intersect type1 type2
)
2798 (let ((dims1 (array-type-dimensions type1
))
2799 (dims2 (array-type-dimensions type2
))
2800 (complexp1 (array-type-complexp type1
))
2801 (complexp2 (array-type-complexp type2
))
2802 (eltype1 (array-type-element-type type1
))
2803 (eltype2 (array-type-element-type type2
))
2804 (stype1 (array-type-specialized-element-type type1
))
2805 (stype2 (array-type-specialized-element-type type2
)))
2806 (make-array-type (cond ((eq dims1
'*) dims2
)
2807 ((eq dims2
'*) dims1
)
2809 (mapcar (lambda (x y
) (if (eq x
'*) y x
))
2811 :complexp
(if (eq complexp1
:maybe
) complexp2 complexp1
)
2813 ((eq eltype1
*wild-type
*) eltype2
)
2814 ((eq eltype2
*wild-type
*) eltype1
)
2815 (t (type-intersection eltype1 eltype2
)))
2816 :specialized-element-type
(cond
2817 ((eq stype1
*wild-type
*) stype2
)
2818 ((eq stype2
*wild-type
*) stype1
)
2820 (aver (type= stype1 stype2
))
2824 ;;; Check a supplied dimension list to determine whether it is legal,
2825 ;;; and return it in canonical form (as either '* or a list).
2826 (defun canonical-array-dimensions (dims)
2831 (error "Arrays can't have a negative number of dimensions: ~S" dims
))
2832 (when (>= dims sb
!xc
:array-rank-limit
)
2833 (error "array type with too many dimensions: ~S" dims
))
2834 (make-list dims
:initial-element
'*))
2836 (when (>= (length dims
) sb
!xc
:array-rank-limit
)
2837 (error "array type with too many dimensions: ~S" dims
))
2840 (unless (and (integerp dim
)
2842 (< dim sb
!xc
:array-dimension-limit
))
2843 (error "bad dimension in array type: ~S" dim
))))
2846 (error "Array dimensions is not a list, integer or *:~% ~S" dims
))))
2850 (!define-type-class member
:enumerable t
2851 :might-contain-other-types nil
)
2853 (!define-type-method
(member :negate
) (type)
2854 (let ((xset (member-type-xset type
))
2855 (fp-zeroes (member-type-fp-zeroes type
)))
2857 ;; Hairy case, which needs to do a bit of float type
2858 ;; canonicalization.
2859 (apply #'type-intersection
2860 (if (xset-empty-p xset
)
2862 (make-negation-type (make-member-type xset nil
)))
2865 (let* ((opposite (neg-fp-zero x
))
2866 (type (ctype-of opposite
)))
2869 (modified-numeric-type type
:low nil
:high nil
))
2870 (modified-numeric-type type
:low nil
:high
(list opposite
))
2871 (make-eql-type opposite
)
2872 (modified-numeric-type type
:low
(list opposite
) :high nil
))))
2875 (make-negation-type type
))))
2877 (!define-type-method
(member :unparse
) (type)
2878 (let ((members (member-type-members type
)))
2879 (cond ((equal members
'(nil)) 'null
)
2880 (t `(member ,@members
)))))
2882 (!define-type-method
(member :singleton-p
) (type)
2883 (if (eql 1 (member-type-size type
))
2884 (values t
(first (member-type-members type
)))
2887 (!define-type-method
(member :simple-subtypep
) (type1 type2
)
2888 (values (and (xset-subset-p (member-type-xset type1
)
2889 (member-type-xset type2
))
2890 (subsetp (member-type-fp-zeroes type1
)
2891 (member-type-fp-zeroes type2
)))
2894 (!define-type-method
(member :complex-subtypep-arg1
) (type1 type2
)
2896 (mapc-member-type-members
2898 (multiple-value-bind (ok surep
) (ctypep elt type2
)
2900 (return-from punt
(values nil nil
)))
2902 (return-from punt
(values nil t
)))))
2906 ;;; We punt if the odd type is enumerable and intersects with the
2907 ;;; MEMBER type. If not enumerable, then it is definitely not a
2908 ;;; subtype of the MEMBER type.
2909 (!define-type-method
(member :complex-subtypep-arg2
) (type1 type2
)
2910 (cond ((not (type-enumerable type1
)) (values nil t
))
2911 ((types-equal-or-intersect type1 type2
)
2912 (invoke-complex-subtypep-arg1-method type1 type2
))
2913 (t (values nil t
))))
2915 (!define-type-method
(member :simple-intersection2
) (type1 type2
)
2916 (make-member-type (xset-intersection (member-type-xset type1
)
2917 (member-type-xset type2
))
2918 (intersection (member-type-fp-zeroes type1
)
2919 (member-type-fp-zeroes type2
))))
2921 (!define-type-method
(member :complex-intersection2
) (type1 type2
)
2923 (let ((xset (alloc-xset))
2925 (mapc-member-type-members
2927 (multiple-value-bind (ok sure
) (ctypep member type1
)
2929 (return-from punt nil
))
2931 (if (fp-zero-p member
)
2932 (pushnew member fp-zeroes
)
2933 (add-to-xset member xset
)))))
2935 (if (and (xset-empty-p xset
) (not fp-zeroes
))
2937 (make-member-type xset fp-zeroes
)))))
2939 ;;; We don't need a :COMPLEX-UNION2, since the only interesting case is
2940 ;;; a union type, and the member/union interaction is handled by the
2941 ;;; union type method.
2942 (!define-type-method
(member :simple-union2
) (type1 type2
)
2943 (make-member-type (xset-union (member-type-xset type1
)
2944 (member-type-xset type2
))
2945 (union (member-type-fp-zeroes type1
)
2946 (member-type-fp-zeroes type2
))))
2948 (!define-type-method
(member :simple-
=) (type1 type2
)
2949 (let ((xset1 (member-type-xset type1
))
2950 (xset2 (member-type-xset type2
))
2951 (l1 (member-type-fp-zeroes type1
))
2952 (l2 (member-type-fp-zeroes type2
)))
2953 (values (and (eql (xset-count xset1
) (xset-count xset2
))
2954 (xset-subset-p xset1 xset2
)
2955 (xset-subset-p xset2 xset1
)
2960 (!define-type-method
(member :complex-
=) (type1 type2
)
2961 (if (type-enumerable type1
)
2962 (multiple-value-bind (val win
) (csubtypep type2 type1
)
2963 (if (or val
(not win
))
2968 (!def-type-translator member
:list
(&rest members
)
2970 (let (ms numbers char-codes
)
2971 (dolist (m (remove-duplicates members
))
2973 (character (push (sb!xc
:char-code m
) char-codes
))
2974 (real (if (and (floatp m
) (zerop m
))
2976 (push (ctype-of m
) numbers
)))
2979 (member-type-from-list ms
)
2980 (make-character-set-type (mapcar (lambda (x) (cons x x
))
2981 (sort char-codes
#'<)))
2982 (nreverse numbers
)))
2985 ;;;; intersection types
2987 ;;;; Until version 0.6.10.6, SBCL followed the original CMU CL approach
2988 ;;;; of punting on all AND types, not just the unreasonably complicated
2989 ;;;; ones. The change was motivated by trying to get the KEYWORD type
2990 ;;;; to behave sensibly:
2991 ;;;; ;; reasonable definition
2992 ;;;; (DEFTYPE KEYWORD () '(AND SYMBOL (SATISFIES KEYWORDP)))
2993 ;;;; ;; reasonable behavior
2994 ;;;; (AVER (SUBTYPEP 'KEYWORD 'SYMBOL))
2995 ;;;; Without understanding a little about the semantics of AND, we'd
2996 ;;;; get (SUBTYPEP 'KEYWORD 'SYMBOL)=>NIL,NIL and, for entirely
2997 ;;;; parallel reasons, (SUBTYPEP 'RATIO 'NUMBER)=>NIL,NIL. That's
3000 ;;;; We still follow the example of CMU CL to some extent, by punting
3001 ;;;; (to the opaque HAIRY-TYPE) on sufficiently complicated types
3004 (!define-type-class intersection
3005 :enumerable
#'compound-type-enumerable
3006 :might-contain-other-types t
)
3008 (!define-type-method
(intersection :negate
) (type)
3010 (mapcar #'type-negation
(intersection-type-types type
))))
3012 ;;; A few intersection types have special names. The others just get
3013 ;;; mechanically unparsed.
3014 (!define-type-method
(intersection :unparse
) (type)
3015 (declare (type ctype type
))
3016 (or (find type
'(ratio keyword compiled-function
) :key
#'specifier-type
:test
#'type
=)
3017 `(and ,@(mapcar #'type-specifier
(intersection-type-types type
)))))
3019 ;;; shared machinery for type equality: true if every type in the set
3020 ;;; TYPES1 matches a type in the set TYPES2 and vice versa
3021 (defun type=-set
(types1 types2
)
3022 (flet ((type<=-set
(x y
)
3023 (declare (type list x y
))
3024 (every/type
(lambda (x y-element
)
3025 (any/type
#'type
= y-element x
))
3027 (and/type
(type<=-set types1 types2
)
3028 (type<=-set types2 types1
))))
3030 ;;; Two intersection types are equal if their subtypes are equal sets.
3032 ;;; FIXME: Might it be better to use
3033 ;;; (AND (SUBTYPEP X Y) (SUBTYPEP Y X))
3034 ;;; instead, since SUBTYPEP is the usual relationship that we care
3035 ;;; most about, so it would be good to leverage any ingenuity there
3036 ;;; in this more obscure method?
3037 (!define-type-method
(intersection :simple-
=) (type1 type2
)
3038 (type=-set
(intersection-type-types type1
)
3039 (intersection-type-types type2
)))
3041 (defun %intersection-complex-subtypep-arg1
(type1 type2
)
3042 (type= type1
(type-intersection type1 type2
)))
3044 (defun %intersection-simple-subtypep
(type1 type2
)
3045 (every/type
#'%intersection-complex-subtypep-arg1
3047 (intersection-type-types type2
)))
3049 (!define-type-method
(intersection :simple-subtypep
) (type1 type2
)
3050 (%intersection-simple-subtypep type1 type2
))
3052 (!define-type-method
(intersection :complex-subtypep-arg1
) (type1 type2
)
3053 (%intersection-complex-subtypep-arg1 type1 type2
))
3055 (defun %intersection-complex-subtypep-arg2
(type1 type2
)
3056 (every/type
#'csubtypep type1
(intersection-type-types type2
)))
3058 (!define-type-method
(intersection :complex-subtypep-arg2
) (type1 type2
)
3059 (%intersection-complex-subtypep-arg2 type1 type2
))
3061 ;;; FIXME: This will look eeriely familiar to readers of the UNION
3062 ;;; :SIMPLE-INTERSECTION2 :COMPLEX-INTERSECTION2 method. That's
3063 ;;; because it was generated by cut'n'paste methods. Given that
3064 ;;; intersections and unions have all sorts of symmetries known to
3065 ;;; mathematics, it shouldn't be beyond the ken of some programmers to
3066 ;;; reflect those symmetries in code in a way that ties them together
3067 ;;; more strongly than having two independent near-copies :-/
3068 (!define-type-method
(intersection :simple-union2
:complex-union2
)
3070 ;; Within this method, type2 is guaranteed to be an intersection
3072 (aver (intersection-type-p type2
))
3073 ;; Make sure to call only the applicable methods...
3074 (cond ((and (intersection-type-p type1
)
3075 (%intersection-simple-subtypep type1 type2
)) type2
)
3076 ((and (intersection-type-p type1
)
3077 (%intersection-simple-subtypep type2 type1
)) type1
)
3078 ((and (not (intersection-type-p type1
))
3079 (%intersection-complex-subtypep-arg2 type1 type2
))
3081 ((and (not (intersection-type-p type1
))
3082 (%intersection-complex-subtypep-arg1 type2 type1
))
3084 ;; KLUDGE: This special (and somewhat hairy) magic is required
3085 ;; to deal with the RATIONAL/INTEGER special case. The UNION
3086 ;; of (INTEGER * -1) and (AND (RATIONAL * -1/2) (NOT INTEGER))
3087 ;; should be (RATIONAL * -1/2) -- CSR, 2003-02-28
3088 ((and (csubtypep type2
(specifier-type 'ratio
))
3089 (numeric-type-p type1
)
3090 (csubtypep type1
(specifier-type 'integer
))
3095 :low
(if (null (numeric-type-low type1
))
3097 (list (1- (numeric-type-low type1
))))
3098 :high
(if (null (numeric-type-high type1
))
3100 (list (1+ (numeric-type-high type1
)))))))
3101 (let* ((intersected (intersection-type-types type2
))
3102 (remaining (remove (specifier-type '(not integer
))
3105 (and (not (equal intersected remaining
))
3106 (type-union type1
(apply #'type-intersection remaining
)))))
3108 (let ((accumulator *universal-type
*))
3109 (do ((t2s (intersection-type-types type2
) (cdr t2s
)))
3110 ((null t2s
) accumulator
)
3111 (let ((union (type-union type1
(car t2s
))))
3112 (when (union-type-p union
)
3113 ;; we have to give up here -- there are all sorts of
3114 ;; ordering worries, but it's better than before.
3115 ;; Doing exactly the same as in the UNION
3116 ;; :SIMPLE/:COMPLEX-INTERSECTION2 method causes stack
3117 ;; overflow with the mutual recursion never bottoming
3119 (if (and (eq accumulator
*universal-type
*)
3121 ;; KLUDGE: if we get here, we have a partially
3122 ;; simplified result. While this isn't by any
3123 ;; means a universal simplification, including
3124 ;; this logic here means that we can get (OR
3125 ;; KEYWORD (NOT KEYWORD)) canonicalized to T.
3129 (type-intersection accumulator union
))))))))
3131 (!def-type-translator and
:list
((:context context
) &rest type-specifiers
)
3132 (apply #'type-intersection
3133 (mapcar (lambda (x) (specifier-type-r context x
))
3138 (!define-type-class union
3139 :enumerable
#'compound-type-enumerable
3140 :might-contain-other-types t
)
3142 (!define-type-method
(union :negate
) (type)
3143 (declare (type ctype type
))
3144 (apply #'type-intersection
3145 (mapcar #'type-negation
(union-type-types type
))))
3147 ;;; The LIST, FLOAT and REAL types have special names. Other union
3148 ;;; types just get mechanically unparsed.
3149 (!define-type-method
(union :unparse
) (type)
3150 (declare (type ctype type
))
3152 ((type= type
(specifier-type 'list
)) 'list
)
3153 ((type= type
(specifier-type 'float
)) 'float
)
3154 ((type= type
(specifier-type 'real
)) 'real
)
3155 ((type= type
(specifier-type 'sequence
)) 'sequence
)
3156 ((type= type
(specifier-type 'bignum
)) 'bignum
)
3157 ((type= type
(specifier-type 'simple-string
)) 'simple-string
)
3158 ((type= type
(specifier-type 'string
)) 'string
)
3159 ((type= type
(specifier-type 'complex
)) 'complex
)
3160 (t `(or ,@(mapcar #'type-specifier
(union-type-types type
))))))
3162 ;;; Two union types are equal if they are each subtypes of each
3163 ;;; other. We need to be this clever because our complex subtypep
3164 ;;; methods are now more accurate; we don't get infinite recursion
3165 ;;; because the simple-subtypep method delegates to complex-subtypep
3166 ;;; of the individual types of type1. - CSR, 2002-04-09
3168 ;;; Previous comment, now obsolete, but worth keeping around because
3169 ;;; it is true, though too strong a condition:
3171 ;;; Two union types are equal if their subtypes are equal sets.
3172 (!define-type-method
(union :simple-
=) (type1 type2
)
3173 (multiple-value-bind (subtype certain?
)
3174 (csubtypep type1 type2
)
3176 (csubtypep type2 type1
)
3177 ;; we might as well become as certain as possible.
3180 (multiple-value-bind (subtype certain?
)
3181 (csubtypep type2 type1
)
3182 (declare (ignore subtype
))
3183 (values nil certain?
))))))
3185 (!define-type-method
(union :complex-
=) (type1 type2
)
3186 (declare (ignore type1
))
3187 (if (some #'type-might-contain-other-types-p
3188 (union-type-types type2
))
3192 ;;; Similarly, a union type is a subtype of another if and only if
3193 ;;; every element of TYPE1 is a subtype of TYPE2.
3194 (defun union-simple-subtypep (type1 type2
)
3195 (every/type
(swapped-args-fun #'union-complex-subtypep-arg2
)
3197 (union-type-types type1
)))
3199 (!define-type-method
(union :simple-subtypep
) (type1 type2
)
3200 (union-simple-subtypep type1 type2
))
3202 (defun union-complex-subtypep-arg1 (type1 type2
)
3203 (every/type
(swapped-args-fun #'csubtypep
)
3205 (union-type-types type1
)))
3207 (!define-type-method
(union :complex-subtypep-arg1
) (type1 type2
)
3208 (union-complex-subtypep-arg1 type1 type2
))
3210 (defun union-complex-subtypep-arg2 (type1 type2
)
3211 ;; At this stage, we know that type2 is a union type and type1
3212 ;; isn't. We might as well check this, though:
3213 (aver (union-type-p type2
))
3214 (aver (not (union-type-p type1
)))
3215 ;; was: (any/type #'csubtypep type1 (union-type-types type2)), which
3216 ;; turns out to be too restrictive, causing bug 91.
3218 ;; the following reimplementation might look dodgy. It is dodgy. It
3219 ;; depends on the union :complex-= method not doing very much work
3220 ;; -- certainly, not using subtypep. Reasoning:
3222 ;; A is a subset of (B1 u B2)
3223 ;; <=> A n (B1 u B2) = A
3224 ;; <=> (A n B1) u (A n B2) = A
3226 ;; But, we have to be careful not to delegate this type= to
3227 ;; something that could invoke subtypep, which might get us back
3228 ;; here -> stack explosion. We therefore ensure that the second type
3229 ;; (which is the one that's dispatched on) is either a union type
3230 ;; (where we've ensured that the complex-= method will not call
3231 ;; subtypep) or something with no union types involved, in which
3232 ;; case we'll never come back here.
3234 ;; If we don't do this, then e.g.
3235 ;; (SUBTYPEP '(MEMBER 3) '(OR (SATISFIES FOO) (SATISFIES BAR)))
3236 ;; would loop infinitely, as the member :complex-= method is
3237 ;; implemented in terms of subtypep.
3239 ;; Ouch. - CSR, 2002-04-10
3240 (multiple-value-bind (sub-value sub-certain?
)
3243 (mapcar (lambda (x) (type-intersection type1 x
))
3244 (union-type-types type2
))))
3246 (values sub-value sub-certain?
)
3247 ;; The ANY/TYPE expression above is a sufficient condition for
3248 ;; subsetness, but not a necessary one, so we might get a more
3249 ;; certain answer by this CALL-NEXT-METHOD-ish step when the
3250 ;; ANY/TYPE expression is uncertain.
3251 (invoke-complex-subtypep-arg1-method type1 type2
))))
3253 (!define-type-method
(union :complex-subtypep-arg2
) (type1 type2
)
3254 (union-complex-subtypep-arg2 type1 type2
))
3256 (!define-type-method
(union :simple-intersection2
:complex-intersection2
)
3258 ;; The CSUBTYPEP clauses here let us simplify e.g.
3259 ;; (TYPE-INTERSECTION2 (SPECIFIER-TYPE 'LIST)
3260 ;; (SPECIFIER-TYPE '(OR LIST VECTOR)))
3261 ;; (where LIST is (OR CONS NULL)).
3263 ;; The tests are more or less (CSUBTYPEP TYPE1 TYPE2) and vice
3264 ;; versa, but it's important that we pre-expand them into
3265 ;; specialized operations on individual elements of
3266 ;; UNION-TYPE-TYPES, instead of using the ordinary call to
3267 ;; CSUBTYPEP, in order to avoid possibly invoking any methods which
3268 ;; might in turn invoke (TYPE-INTERSECTION2 TYPE1 TYPE2) and thus
3269 ;; cause infinite recursion.
3271 ;; Within this method, type2 is guaranteed to be a union type:
3272 (aver (union-type-p type2
))
3273 ;; Make sure to call only the applicable methods...
3274 (cond ((and (union-type-p type1
)
3275 (union-simple-subtypep type1 type2
)) type1
)
3276 ((and (union-type-p type1
)
3277 (union-simple-subtypep type2 type1
)) type2
)
3278 ((and (not (union-type-p type1
))
3279 (union-complex-subtypep-arg2 type1 type2
))
3281 ((and (not (union-type-p type1
))
3282 (union-complex-subtypep-arg1 type2 type1
))
3285 ;; KLUDGE: This code accumulates a sequence of TYPE-UNION2
3286 ;; operations in a particular order, and gives up if any of
3287 ;; the sub-unions turn out not to be simple. In other cases
3288 ;; ca. sbcl-0.6.11.15, that approach to taking a union was a
3289 ;; bad idea, since it can overlook simplifications which
3290 ;; might occur if the terms were accumulated in a different
3291 ;; order. It's possible that that will be a problem here too.
3292 ;; However, I can't think of a good example to demonstrate
3293 ;; it, and without an example to demonstrate it I can't write
3294 ;; test cases, and without test cases I don't want to
3295 ;; complicate the code to address what's still a hypothetical
3296 ;; problem. So I punted. -- WHN 2001-03-20
3297 (let ((accumulator *empty-type
*))
3298 (dolist (t2 (union-type-types type2
) accumulator
)
3300 (type-union accumulator
3301 (type-intersection type1 t2
))))))))
3303 (!def-type-translator or
:list
((:context context
) &rest type-specifiers
)
3304 (let ((type (apply #'type-union
3305 (mapcar (lambda (x) (specifier-type-r context x
))
3307 (if (union-type-p type
)
3308 (sb!kernel
::simplify-array-unions type
)
3313 (!define-type-class cons
:enumerable nil
:might-contain-other-types nil
)
3315 (!def-type-translator cons
((:context context
)
3316 &optional
(car-type-spec '*) (cdr-type-spec '*))
3317 (let ((car-type (single-value-specifier-type-r context car-type-spec
))
3318 (cdr-type (single-value-specifier-type-r context cdr-type-spec
)))
3319 (make-cons-type car-type cdr-type
)))
3321 (!define-type-method
(cons :negate
) (type)
3322 (if (and (eq (cons-type-car-type type
) *universal-type
*)
3323 (eq (cons-type-cdr-type type
) *universal-type
*))
3324 (make-negation-type type
)
3326 (make-negation-type (specifier-type 'cons
))
3328 ((and (not (eq (cons-type-car-type type
) *universal-type
*))
3329 (not (eq (cons-type-cdr-type type
) *universal-type
*)))
3332 (type-negation (cons-type-car-type type
))
3336 (type-negation (cons-type-cdr-type type
)))))
3337 ((not (eq (cons-type-car-type type
) *universal-type
*))
3339 (type-negation (cons-type-car-type type
))
3341 ((not (eq (cons-type-cdr-type type
) *universal-type
*))
3344 (type-negation (cons-type-cdr-type type
))))
3345 (t (bug "Weird CONS type ~S" type
))))))
3347 (!define-type-method
(cons :unparse
) (type)
3348 (let ((car-eltype (type-specifier (cons-type-car-type type
)))
3349 (cdr-eltype (type-specifier (cons-type-cdr-type type
))))
3350 (if (and (member car-eltype
'(t *))
3351 (member cdr-eltype
'(t *)))
3353 `(cons ,car-eltype
,cdr-eltype
))))
3355 (!define-type-method
(cons :simple-
=) (type1 type2
)
3356 (declare (type cons-type type1 type2
))
3357 (multiple-value-bind (car-match car-win
)
3358 (type= (cons-type-car-type type1
) (cons-type-car-type type2
))
3359 (multiple-value-bind (cdr-match cdr-win
)
3360 (type= (cons-type-cdr-type type1
) (cons-type-cdr-type type2
))
3361 (cond ((and car-match cdr-match
)
3362 (aver (and car-win cdr-win
))
3366 ;; FIXME: Ideally we would like to detect and handle
3367 ;; (CONS UNKNOWN INTEGER) (CONS UNKNOWN SYMBOL) => NIL, T
3368 ;; but just returning a secondary true on (and car-win cdr-win)
3369 ;; unfortunately breaks other things. --NS 2006-08-16
3370 (and (or (and (not car-match
) car-win
)
3371 (and (not cdr-match
) cdr-win
))
3372 (not (and (cons-type-might-be-empty-type type1
)
3373 (cons-type-might-be-empty-type type2
))))))))))
3375 (!define-type-method
(cons :simple-subtypep
) (type1 type2
)
3376 (declare (type cons-type type1 type2
))
3377 (multiple-value-bind (val-car win-car
)
3378 (csubtypep (cons-type-car-type type1
) (cons-type-car-type type2
))
3379 (multiple-value-bind (val-cdr win-cdr
)
3380 (csubtypep (cons-type-cdr-type type1
) (cons-type-cdr-type type2
))
3381 (if (and val-car val-cdr
)
3382 (values t
(and win-car win-cdr
))
3383 (values nil
(or (and (not val-car
) win-car
)
3384 (and (not val-cdr
) win-cdr
)))))))
3386 ;;; Give up if a precise type is not possible, to avoid returning
3387 ;;; overly general types.
3388 (!define-type-method
(cons :simple-union2
) (type1 type2
)
3389 (declare (type cons-type type1 type2
))
3390 (let ((car-type1 (cons-type-car-type type1
))
3391 (car-type2 (cons-type-car-type type2
))
3392 (cdr-type1 (cons-type-cdr-type type1
))
3393 (cdr-type2 (cons-type-cdr-type type2
))
3396 ;; UGH. -- CSR, 2003-02-24
3397 (macrolet ((frob-car (car1 car2 cdr1 cdr2
3398 &optional
(not1 nil not1p
))
3400 (make-cons-type ,car1
(type-union ,cdr1
,cdr2
))
3402 (type-intersection ,car2
3405 `(type-negation ,car1
)))
3407 (cond ((type= car-type1 car-type2
)
3408 (make-cons-type car-type1
3409 (type-union cdr-type1 cdr-type2
)))
3410 ((type= cdr-type1 cdr-type2
)
3411 (make-cons-type (type-union car-type1 car-type2
)
3413 ((csubtypep car-type1 car-type2
)
3414 (frob-car car-type1 car-type2 cdr-type1 cdr-type2
))
3415 ((csubtypep car-type2 car-type1
)
3416 (frob-car car-type2 car-type1 cdr-type2 cdr-type1
))
3417 ;; more general case of the above, but harder to compute
3419 (setf car-not1
(type-negation car-type1
))
3420 (multiple-value-bind (yes win
)
3421 (csubtypep car-type2 car-not1
)
3422 (and (not yes
) win
)))
3423 (frob-car car-type1 car-type2 cdr-type1 cdr-type2 car-not1
))
3425 (setf car-not2
(type-negation car-type2
))
3426 (multiple-value-bind (yes win
)
3427 (csubtypep car-type1 car-not2
)
3428 (and (not yes
) win
)))
3429 (frob-car car-type2 car-type1 cdr-type2 cdr-type1 car-not2
))
3430 ;; Don't put these in -- consider the effect of taking the
3431 ;; union of (CONS (INTEGER 0 2) (INTEGER 5 7)) and
3432 ;; (CONS (INTEGER 0 3) (INTEGER 5 6)).
3434 ((csubtypep cdr-type1 cdr-type2
)
3435 (frob-cdr car-type1 car-type2 cdr-type1 cdr-type2
))
3437 ((csubtypep cdr-type2 cdr-type1
)
3438 (frob-cdr car-type2 car-type1 cdr-type2 cdr-type1
))))))
3440 (!define-type-method
(cons :simple-intersection2
) (type1 type2
)
3441 (declare (type cons-type type1 type2
))
3442 (let ((car-int2 (type-intersection2 (cons-type-car-type type1
)
3443 (cons-type-car-type type2
)))
3444 (cdr-int2 (type-intersection2 (cons-type-cdr-type type1
)
3445 (cons-type-cdr-type type2
))))
3447 ((and car-int2 cdr-int2
) (make-cons-type car-int2 cdr-int2
))
3448 (car-int2 (make-cons-type car-int2
3450 (cons-type-cdr-type type1
)
3451 (cons-type-cdr-type type2
))))
3452 (cdr-int2 (make-cons-type
3453 (type-intersection (cons-type-car-type type1
)
3454 (cons-type-car-type type2
))
3457 (!define-superclasses cons
((cons)) !cold-init-forms
)
3459 ;;;; CHARACTER-SET types
3461 (!def-type-translator character-set
3462 (&optional
(pairs '((0 .
#.
(1- sb
!xc
:char-code-limit
)))))
3463 (make-character-set-type pairs
))
3465 (!define-type-method
(character-set :negate
) (type)
3466 (let ((pairs (character-set-type-pairs type
)))
3467 (if (and (= (length pairs
) 1)
3469 (= (cdar pairs
) (1- sb
!xc
:char-code-limit
)))
3470 (make-negation-type type
)
3471 (let ((not-character
3473 (make-character-set-type
3474 '((0 .
#.
(1- sb
!xc
:char-code-limit
)))))))
3477 (make-character-set-type
3479 (when (> (caar pairs
) 0)
3480 (push (cons 0 (1- (caar pairs
))) not-pairs
))
3481 (do* ((tail pairs
(cdr tail
))
3482 (high1 (cdar tail
) (cdar tail
))
3483 (low2 (caadr tail
) (caadr tail
)))
3485 (when (< (cdar tail
) (1- sb
!xc
:char-code-limit
))
3486 (push (cons (1+ (cdar tail
))
3487 (1- sb
!xc
:char-code-limit
))
3489 (nreverse not-pairs
))
3490 (push (cons (1+ high1
) (1- low2
)) not-pairs
)))))))))
3492 (!define-type-method
(character-set :unparse
) (type)
3494 ((type= type
(specifier-type 'character
)) 'character
)
3495 ((type= type
(specifier-type 'base-char
)) 'base-char
)
3496 ((type= type
(specifier-type 'extended-char
)) 'extended-char
)
3497 ((type= type
(specifier-type 'standard-char
)) 'standard-char
)
3499 ;; Unparse into either MEMBER or CHARACTER-SET. We use MEMBER if there
3500 ;; are at most as many characters as there are character code ranges.
3501 ;; (basically saying to use MEMBER if each range is one character)
3502 (let* ((pairs (character-set-type-pairs type
))
3503 (count (length pairs
))
3504 (chars (loop named outer
3505 for
(low . high
) in pairs
3506 nconc
(loop for code from low upto high
3507 collect
(sb!xc
:code-char code
)
3508 when
(minusp (decf count
))
3509 do
(return-from outer t
)))))
3511 `(character-set ,pairs
)
3512 `(member ,@chars
))))))
3514 (!define-type-method
(character-set :singleton-p
) (type)
3515 (let* ((pairs (character-set-type-pairs type
))
3516 (pair (first pairs
)))
3517 (if (and (typep pairs
'(cons t null
))
3518 (eql (car pair
) (cdr pair
)))
3519 (values t
(code-char (car pair
)))
3522 (!define-type-method
(character-set :simple-
=) (type1 type2
)
3523 (let ((pairs1 (character-set-type-pairs type1
))
3524 (pairs2 (character-set-type-pairs type2
)))
3525 (values (equal pairs1 pairs2
) t
)))
3527 (!define-type-method
(character-set :simple-subtypep
) (type1 type2
)
3529 (dolist (pair (character-set-type-pairs type1
) t
)
3530 (unless (position pair
(character-set-type-pairs type2
)
3531 :test
(lambda (x y
) (and (>= (car x
) (car y
))
3532 (<= (cdr x
) (cdr y
)))))
3536 (!define-type-method
(character-set :simple-union2
) (type1 type2
)
3537 ;; KLUDGE: the canonizing in the MAKE-CHARACTER-SET-TYPE function
3538 ;; actually does the union for us. It might be a little fragile to
3540 (make-character-set-type
3542 (copy-alist (character-set-type-pairs type1
))
3543 (copy-alist (character-set-type-pairs type2
))
3546 (!define-type-method
(character-set :simple-intersection2
) (type1 type2
)
3547 ;; KLUDGE: brute force.
3550 (dolist (pair1 (character-set-type-pairs type1
)
3551 (make-character-set-type
3552 (sort pairs
#'< :key
#'car
)))
3553 (dolist (pair2 (character-set-type-pairs type2
))
3555 ((<= (car pair1
) (car pair2
) (cdr pair1
))
3556 (push (cons (car pair2
) (min (cdr pair1
) (cdr pair2
))) pairs
))
3557 ((<= (car pair2
) (car pair1
) (cdr pair2
))
3558 (push (cons (car pair1
) (min (cdr pair1
) (cdr pair2
))) pairs
))))))
3560 (make-character-set-type
3561 (intersect-type-pairs
3562 (character-set-type-pairs type1
)
3563 (character-set-type-pairs type2
))))
3566 ;;; Intersect two ordered lists of pairs
3567 ;;; Each list is of the form ((start1 . end1) ... (startn . endn)),
3568 ;;; where start1 <= end1 < start2 <= end2 < ... < startn <= endn.
3569 ;;; Each pair represents the integer interval start..end.
3571 (defun intersect-type-pairs (alist1 alist2
)
3572 (if (and alist1 alist2
)
3574 (pair1 (pop alist1
))
3575 (pair2 (pop alist2
)))
3577 (when (> (car pair1
) (car pair2
))
3578 (rotatef pair1 pair2
)
3579 (rotatef alist1 alist2
))
3580 (let ((pair1-cdr (cdr pair1
)))
3582 ((> (car pair2
) pair1-cdr
)
3583 ;; No over lap -- discard pair1
3584 (unless alist1
(return))
3585 (setq pair1
(pop alist1
)))
3586 ((<= (cdr pair2
) pair1-cdr
)
3587 (push (cons (car pair2
) (cdr pair2
)) res
)
3589 ((= (cdr pair2
) pair1-cdr
)
3590 (unless alist1
(return))
3591 (unless alist2
(return))
3592 (setq pair1
(pop alist1
)
3593 pair2
(pop alist2
)))
3594 (t ;; (< (cdr pair2) pair1-cdr)
3595 (unless alist2
(return))
3596 (setq pair1
(cons (1+ (cdr pair2
)) pair1-cdr
))
3597 (setq pair2
(pop alist2
)))))
3598 (t ;; (> (cdr pair2) (cdr pair1))
3599 (push (cons (car pair2
) pair1-cdr
) res
)
3600 (unless alist1
(return))
3601 (setq pair2
(cons (1+ pair1-cdr
) (cdr pair2
)))
3602 (setq pair1
(pop alist1
))))))
3607 ;;; Return the type that describes all objects that are in X but not
3608 ;;; in Y. If we can't determine this type, then return NIL.
3610 ;;; For now, we only are clever dealing with union and member types.
3611 ;;; If either type is not a union type, then we pretend that it is a
3612 ;;; union of just one type. What we do is remove from X all the types
3613 ;;; that are a subtype any type in Y. If any type in X intersects with
3614 ;;; a type in Y but is not a subtype, then we give up.
3616 ;;; We must also special-case any member type that appears in the
3617 ;;; union. We remove from X's members all objects that are TYPEP to Y.
3618 ;;; If Y has any members, we must be careful that none of those
3619 ;;; members are CTYPEP to any of Y's non-member types. We give up in
3620 ;;; this case, since to compute that difference we would have to break
3621 ;;; the type from X into some collection of types that represents the
3622 ;;; type without that particular element. This seems too hairy to be
3623 ;;; worthwhile, given its low utility.
3624 (defun type-difference (x y
)
3625 (if (and (numeric-type-p x
) (numeric-type-p y
))
3626 ;; Numeric types are easy. Are there any others we should handle like this?
3627 (type-intersection x
(type-negation y
))
3628 (let ((x-types (if (union-type-p x
) (union-type-types x
) (list x
)))
3629 (y-types (if (union-type-p y
) (union-type-types y
) (list y
))))
3631 (dolist (x-type x-types
)
3632 (if (member-type-p x-type
)
3633 (let ((xset (alloc-xset))
3635 (mapc-member-type-members
3637 (multiple-value-bind (ok sure
) (ctypep elt y
)
3639 (return-from type-difference nil
))
3642 (pushnew elt fp-zeroes
)
3643 (add-to-xset elt xset
)))))
3645 (unless (and (xset-empty-p xset
) (not fp-zeroes
))
3646 (res (make-member-type xset fp-zeroes
))))
3647 (dolist (y-type y-types
(res x-type
))
3648 (multiple-value-bind (val win
) (csubtypep x-type y-type
)
3649 (unless win
(return-from type-difference nil
))
3651 (when (types-equal-or-intersect x-type y-type
)
3652 (return-from type-difference nil
))))))
3653 (let ((y-mem (find-if #'member-type-p y-types
)))
3655 (dolist (x-type x-types
)
3656 (unless (member-type-p x-type
)
3657 (mapc-member-type-members
3659 (multiple-value-bind (ok sure
) (ctypep member x-type
)
3660 (when (or (not sure
) ok
)
3661 (return-from type-difference nil
))))
3663 (apply #'type-union
(res))))))
3665 (!def-type-translator array
((:context context
)
3666 &optional
(element-type '*)
3668 (let ((eltype (if (eq element-type
'*)
3670 (specifier-type-r context element-type
))))
3671 (make-array-type (canonical-array-dimensions dimensions
)
3673 :element-type eltype
3674 :specialized-element-type
(%upgraded-array-element-type
3677 (!def-type-translator simple-array
((:context context
)
3678 &optional
(element-type '*)
3680 (let ((eltype (if (eq element-type
'*)
3682 (specifier-type-r context element-type
))))
3683 (make-array-type (canonical-array-dimensions dimensions
)
3685 :element-type eltype
3686 :specialized-element-type
(%upgraded-array-element-type
3689 ;;;; SIMD-PACK types
3692 (!define-type-class simd-pack
:enumerable nil
3693 :might-contain-other-types nil
)
3695 ;; Though this involves a recursive call to parser, parsing context need not
3696 ;; be passed down, because an unknown-type condition is an immediate failure.
3697 (!def-type-translator simd-pack
(&optional
(element-type-spec '*))
3698 (if (eql element-type-spec
'*)
3699 (%make-simd-pack-type
*simd-pack-element-types
*)
3700 (make-simd-pack-type (single-value-specifier-type element-type-spec
))))
3702 (!define-type-method
(simd-pack :negate
) (type)
3703 (let ((remaining (set-difference *simd-pack-element-types
*
3704 (simd-pack-type-element-type type
)))
3705 (not-simd-pack (make-negation-type (specifier-type 'simd-pack
))))
3707 (type-union not-simd-pack
(%make-simd-pack-type remaining
))
3710 (!define-type-method
(simd-pack :unparse
) (type)
3711 (let ((eltypes (simd-pack-type-element-type type
)))
3712 (cond ((equal eltypes
*simd-pack-element-types
*)
3714 ((= 1 (length eltypes
))
3715 `(simd-pack ,(first eltypes
)))
3717 `(or ,@(mapcar (lambda (eltype)
3718 `(simd-pack ,eltype
))
3721 (!define-type-method
(simd-pack :simple-
=) (type1 type2
)
3722 (declare (type simd-pack-type type1 type2
))
3723 (null (set-exclusive-or (simd-pack-type-element-type type1
)
3724 (simd-pack-type-element-type type2
))))
3726 (!define-type-method
(simd-pack :simple-subtypep
) (type1 type2
)
3727 (declare (type simd-pack-type type1 type2
))
3728 (subsetp (simd-pack-type-element-type type1
)
3729 (simd-pack-type-element-type type2
)))
3731 (!define-type-method
(simd-pack :simple-union2
) (type1 type2
)
3732 (declare (type simd-pack-type type1 type2
))
3733 (%make-simd-pack-type
(union (simd-pack-type-element-type type1
)
3734 (simd-pack-type-element-type type2
))))
3736 (!define-type-method
(simd-pack :simple-intersection2
) (type1 type2
)
3737 (declare (type simd-pack-type type1 type2
))
3738 (let ((intersection (intersection (simd-pack-type-element-type type1
)
3739 (simd-pack-type-element-type type2
))))
3741 (%make-simd-pack-type intersection
)
3744 (!define-superclasses simd-pack
((simd-pack)) !cold-init-forms
))
3746 ;;;; utilities shared between cross-compiler and target system
3748 ;;; Does the type derived from compilation of an actual function
3749 ;;; definition satisfy declarations of a function's type?
3750 (defun defined-ftype-matches-declared-ftype-p (defined-ftype declared-ftype
)
3751 (declare (type ctype defined-ftype declared-ftype
))
3752 (flet ((is-built-in-class-function-p (ctype)
3753 (and (built-in-classoid-p ctype
)
3754 (eq (built-in-classoid-name ctype
) 'function
))))
3755 (cond (;; DECLARED-FTYPE could certainly be #<BUILT-IN-CLASS FUNCTION>;
3756 ;; that's what happens when we (DECLAIM (FTYPE FUNCTION FOO)).
3757 (is-built-in-class-function-p declared-ftype
)
3758 ;; In that case, any definition satisfies the declaration.
3760 (;; It's not clear whether or how DEFINED-FTYPE might be
3761 ;; #<BUILT-IN-CLASS FUNCTION>, but it's not obviously
3762 ;; invalid, so let's handle that case too, just in case.
3763 (is-built-in-class-function-p defined-ftype
)
3764 ;; No matter what DECLARED-FTYPE might be, we can't prove
3765 ;; that an object of type FUNCTION doesn't satisfy it, so
3766 ;; we return success no matter what.
3768 (;; Otherwise both of them must be FUN-TYPE objects.
3770 ;; FIXME: For now we only check compatibility of the return
3771 ;; type, not argument types, and we don't even check the
3772 ;; return type very precisely (as per bug 94a). It would be
3773 ;; good to do a better job. Perhaps to check the
3774 ;; compatibility of the arguments, we should (1) redo
3775 ;; VALUES-TYPES-EQUAL-OR-INTERSECT as
3776 ;; ARGS-TYPES-EQUAL-OR-INTERSECT, and then (2) apply it to
3777 ;; the ARGS-TYPE slices of the FUN-TYPEs. (ARGS-TYPE
3778 ;; is a base class both of VALUES-TYPE and of FUN-TYPE.)
3779 (values-types-equal-or-intersect
3780 (fun-type-returns defined-ftype
)
3781 (fun-type-returns declared-ftype
))))))
3783 ;;; This messy case of CTYPE for NUMBER is shared between the
3784 ;;; cross-compiler and the target system.
3785 (defun ctype-of-number (x)
3786 (let ((num (if (complexp x
) (realpart x
) x
)))
3787 (multiple-value-bind (complexp low high
)
3789 (let ((imag (imagpart x
)))
3790 (values :complex
(min num imag
) (max num imag
)))
3791 (values :real num num
))
3792 (make-numeric-type :class
(etypecase num
3793 (integer (if (complexp x
)
3794 (if (integerp (imagpart x
))
3798 (rational 'rational
)
3800 :format
(and (floatp num
) (float-format-name num
))
3805 ;;; The following function is a generic driver for approximating
3806 ;;; set-valued functions over types. Putting this here because it'll
3807 ;;; probably be useful for a lot of type analyses.
3809 ;;; Let f be a function from values of type X to Y, e.g., ARRAY-RANK.
3811 ;;; We compute an over or under-approximation of the set
3813 ;;; F(TYPE) = { f(x) : x in TYPE /\ x in X } \subseteq Y
3815 ;;; via set-valued approximations of f, OVER and UNDER.
3817 ;;; These functions must have the property that
3818 ;;; Forall TYPE, OVER(TYPE) \superseteq F(TYPE) and
3819 ;;; Forall TYPE, UNDER(TYPE) \subseteq F(TYPE)
3821 ;;; The driver is also parameterised over the finite set
3824 ;;; Union, intersection and difference are binary functions to compute
3825 ;;; set union, intersection and difference. Top and bottom are the
3826 ;;; concrete representations for the universe and empty sets; we never
3827 ;;; call the set functions on top or bottom, so it's safe to use
3828 ;;; special values there.
3832 ;;; TYPE: the ctype for which we wish to approximate F(TYPE)
3833 ;;; OVERAPPROXIMATE: true if we wish to overapproximate, nil otherwise.
3834 ;;; You usually want T.
3835 ;;; UNION/INTERSECTION/DIFFERENCE: implementations of finite set operations.
3836 ;;; Conform to cl::(union/intersection/set-difference). Passing NIL will
3837 ;;; disable some cleverness and result in quicker computation of coarser
3838 ;;; approximations. However, passing difference without union and intersection
3839 ;;; will probably not end well.
3840 ;;; TOP/BOTTOM: concrete representation of the universe and empty set. Finite
3841 ;;; set operations are never called on TOP/BOTTOM, so it's safe to use special
3843 ;;; OVER/UNDER: the set-valued approximations of F.
3845 ;;; Implementation details.
3847 ;;; It's a straightforward walk down the type.
3848 ;;; Union types -> take the union of children, intersection ->
3849 ;;; intersect. There is some complication for negation types: we must
3850 ;;; not only negate the result, but also flip from overapproximating
3851 ;;; to underapproximating in the children (or vice versa).
3853 ;;; We represent sets as a pair of (negate-p finite-set) in order to
3854 ;;; support negation types.
3856 (declaim (inline generic-abstract-type-function
))
3857 (defun generic-abstract-type-function
3858 (type overapproximate
3859 union intersection difference
3862 (labels ((union* (x y
)
3863 ;; wrappers to avoid calling union/intersection on
3865 (cond ((or (eql x top
)
3871 (funcall union x y
))))
3872 (intersection* (x y
)
3873 (cond ((or (eql x bottom
)
3879 (funcall intersection x y
))))
3880 (unite (not-x-p x not-y-p y
)
3881 ;; if we only have one negated set, it's x.
3883 (rotatef not-x-p not-y-p
)
3885 (cond ((and not-x-p not-y-p
)
3886 ;; -x \/ -y = -(x /\ y)
3887 (normalize t
(intersection* x y
)))
3889 ;; -x \/ y = -(x \ y)
3899 (funcall difference x y
)))))
3901 (values nil
(union* x y
)))))
3902 (intersect (not-x-p x not-y-p y
)
3904 (rotatef not-x-p not-y-p
)
3906 (cond ((and not-x-p not-y-p
)
3907 ;; -x /\ -y = -(x \/ y)
3908 (normalize t
(union* x y
)))
3911 (cond ((or (eql x top
) (eql y bottom
))
3912 (values nil bottom
))
3918 (values nil
(funcall difference y x
)))))
3920 (values nil
(intersection* x y
)))))
3921 (normalize (not-x-p x
)
3922 ;; catch some easy cases of redundant negation.
3923 (cond ((not not-x-p
)
3931 (default (overapproximate)
3933 (if overapproximate top bottom
))
3934 (walk-union (types overapproximate
)
3935 ;; Only do this if union is provided.
3937 (return-from walk-union
(default overapproximate
)))
3938 ;; Reduce/union from bottom.
3939 (let ((not-acc-p nil
)
3941 (dolist (type types
(values not-acc-p acc
))
3942 (multiple-value-bind (not x
)
3943 (walk type overapproximate
)
3944 (setf (values not-acc-p acc
)
3945 (unite not-acc-p acc not x
)))
3946 ;; Early exit on top set.
3947 (when (and (eql acc top
)
3949 (return (values nil top
))))))
3950 (walk-intersection (types overapproximate
)
3951 ;; Skip if we don't know how to intersect sets
3952 (unless intersection
3953 (return-from walk-intersection
(default overapproximate
)))
3954 ;; Reduce/intersection from top
3955 (let ((not-acc-p nil
)
3957 (dolist (type types
(values not-acc-p acc
))
3958 (multiple-value-bind (not x
)
3959 (walk type overapproximate
)
3960 (setf (values not-acc-p acc
)
3961 (intersect not-acc-p acc not x
)))
3962 (when (and (eql acc bottom
)
3964 (return (values nil bottom
))))))
3965 (walk-negate (type overapproximate
)
3966 ;; Don't introduce negated types if we don't know how to
3969 (return-from walk-negate
(default overapproximate
)))
3970 (multiple-value-bind (not x
)
3971 (walk type
(not overapproximate
))
3972 (normalize (not not
) x
)))
3973 (walk (type overapproximate
)
3976 (walk-union (union-type-types type
) overapproximate
))
3977 ((cons (member or union
))
3978 (walk-union (rest type
) overapproximate
))
3980 (walk-intersection (intersection-type-types type
) overapproximate
))
3981 ((cons (member and intersection
))
3982 (walk-intersection (rest type
) overapproximate
))
3984 (walk-negate (negation-type-type type
) overapproximate
))
3986 (walk-negate (second type
) overapproximate
))
3994 (funcall under type
)
3995 (default nil
))))))))
3996 (multiple-value-call #'normalize
(walk type overapproximate
))))
3997 (declaim (notinline generic-abstract-type-function
))
3999 ;;; Standard list representation of sets. Use CL:* for the universe.
4000 (defun list-abstract-type-function (type over
&key under
(overapproximate t
))
4001 (declare (inline generic-abstract-type-function
))
4002 (generic-abstract-type-function
4003 type overapproximate
4004 #'union
#'intersection
#'set-difference
4008 (!defun-from-collected-cold-init-forms
!late-type-cold-init
)
4010 #-sb-xc
(!late-type-cold-init2
)
4012 (/show0
"late-type.lisp end of file")