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 (!define-type-class named
:enumerable nil
:might-contain-other-types nil
)
1154 ;; This is used when parsing (SATISFIES KEYWORDP)
1155 ;; so that simplifications can be made when computing intersections,
1156 ;; without which we would see this kind of "empty-type in disguise"
1157 ;; (AND (SATISFIES KEYWORDP) CONS)
1158 ;; This isn't *keyword-type* because KEYWORD is implemented
1159 ;; as the intersection of SYMBOL and (SATISFIES KEYWORDP)
1160 ;; We could also intern the KEYWORD type but that would require
1161 ;; hacking the INTERSECTION logic.
1162 (defglobal *satisfies-keywordp-type
* -
1)
1164 ;; Here too I discovered more than 1000 instances in a particular
1165 ;; Lisp image, when really this is *EMPTY-TYPE*.
1166 ;; (AND (SATISFIES LEGAL-FUN-NAME-P) (SIMPLE-ARRAY CHARACTER (*)))
1167 (defglobal *fun-name-type
* -
1)
1169 ;; !LATE-TYPE-COLD-INIT can't be GCd - there are lambdas in the toplevel code
1170 ;; component that leak out and persist - but everything below is GCable.
1171 ;; This leads to about 20KB of extra code being retained on x86-64.
1172 ;; An educated guess is that DEFINE-SUPERCLASSES is responsible for the problem.
1173 (defun !late-type-cold-init2
()
1174 (macrolet ((frob (name var
)
1177 (mark-ctype-interned (make-named-type :name
',name
)))
1178 (setf (info :type
:kind
',name
) :primitive
)
1179 (setf (info :type
:builtin
',name
) ,var
))))
1180 ;; KLUDGE: In ANSI, * isn't really the name of a type, it's just a
1181 ;; special symbol which can be stuck in some places where an
1182 ;; ordinary type can go, e.g. (ARRAY * 1) instead of (ARRAY T 1).
1183 ;; In SBCL it also used to denote universal VALUES type.
1184 (frob * *wild-type
*)
1185 (frob nil
*empty-type
*)
1186 (frob t
*universal-type
*)
1187 (setf (sb!c
::meta-info-default
(sb!c
::meta-info
:variable
:type
))
1189 ;; new in sbcl-0.9.5: these used to be CLASSOID types, but that
1190 ;; view of them was incompatible with requirements on the MOP
1191 ;; metaobject class hierarchy: the INSTANCE and
1192 ;; FUNCALLABLE-INSTANCE types are disjoint (instances have
1193 ;; instance-pointer-lowtag; funcallable-instances have
1194 ;; fun-pointer-lowtag), while FUNCALLABLE-STANDARD-OBJECT is
1195 ;; required to be a subclass of STANDARD-OBJECT. -- CSR,
1197 (frob instance
*instance-type
*)
1198 (frob funcallable-instance
*funcallable-instance-type
*)
1199 ;; new in sbcl-1.0.3.3: necessary to act as a join point for the
1200 ;; extended sequence hierarchy. (Might be removed later if we use
1201 ;; a dedicated FUNDAMENTAL-SEQUENCE class for this.)
1202 (frob extended-sequence
*extended-sequence-type
*))
1203 (!intern-important-fun-type-instances
)
1204 (!intern-important-member-type-instances
)
1205 (!intern-important-cons-type-instances
)
1206 (!intern-important-numeric-type-instances
)
1207 (!intern-important-character-set-type-instances
)
1208 (!intern-important-array-type-instances
) ; must be after numeric and char
1209 (setf *satisfies-keywordp-type
*
1210 (mark-ctype-interned (%make-hairy-type
'(satisfies keywordp
))))
1211 (setf *fun-name-type
*
1212 (mark-ctype-interned (%make-hairy-type
'(satisfies legal-fun-name-p
))))
1213 ;; This is not an important type- no attempt is made to return exactly this
1214 ;; object when parsing FUNCTION. In fact we return the classoid instead
1215 (setf *universal-fun-type
*
1216 (make-fun-type :wild-args t
:returns
*wild-type
*)))
1218 (!define-type-method
(named :simple-
=) (type1 type2
)
1219 ;;(aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1220 (values (eq type1 type2
) t
))
1222 (defun cons-type-might-be-empty-type (type)
1223 (declare (type cons-type type
))
1224 (let ((car-type (cons-type-car-type type
))
1225 (cdr-type (cons-type-cdr-type type
)))
1227 (if (cons-type-p car-type
)
1228 (cons-type-might-be-empty-type car-type
)
1229 (multiple-value-bind (yes surep
)
1230 (type= car-type
*empty-type
*)
1233 (if (cons-type-p cdr-type
)
1234 (cons-type-might-be-empty-type cdr-type
)
1235 (multiple-value-bind (yes surep
)
1236 (type= cdr-type
*empty-type
*)
1240 (defun cons-type-length-info (type)
1241 (declare (type cons-type type
))
1242 (do ((min 1 (1+ min
))
1243 (cdr (cons-type-cdr-type type
) (cons-type-cdr-type cdr
)))
1244 ((not (cons-type-p cdr
))
1246 ((csubtypep cdr
(specifier-type 'null
))
1248 ((csubtypep *universal-type
* cdr
)
1250 ((type/= (type-intersection (specifier-type 'cons
) cdr
) *empty-type
*)
1252 ((type/= (type-intersection (specifier-type 'null
) cdr
) *empty-type
*)
1254 (t (values min
:maybe
))))
1257 (!define-type-method
(named :complex-
=) (type1 type2
)
1259 ((and (eq type2
*empty-type
*)
1260 (or (and (intersection-type-p type1
)
1261 ;; not allowed to be unsure on these... FIXME: keep
1262 ;; the list of CL types that are intersection types
1263 ;; once and only once.
1264 (not (or (type= type1
(specifier-type 'ratio
))
1265 (type= type1
(specifier-type 'keyword
)))))
1266 (and (cons-type-p type1
)
1267 (cons-type-might-be-empty-type type1
))))
1268 ;; things like (AND (EQL 0) (SATISFIES ODDP)) or (AND FUNCTION
1269 ;; STREAM) can get here. In general, we can't really tell
1270 ;; whether these are equal to NIL or not, so
1272 ((type-might-contain-other-types-p type1
)
1273 (invoke-complex-=-other-method type1 type2
))
1274 (t (values nil t
))))
1276 (!define-type-method
(named :simple-subtypep
) (type1 type2
)
1277 (aver (not (eq type1
*wild-type
*))) ; * isn't really a type.
1278 (aver (not (eq type1 type2
)))
1279 (values (or (eq type1
*empty-type
*)
1280 (eq type2
*wild-type
*)
1281 (eq type2
*universal-type
*)) t
))
1283 (!define-type-method
(named :complex-subtypep-arg1
) (type1 type2
)
1284 ;; This AVER causes problems if we write accurate methods for the
1285 ;; union (and possibly intersection) types which then delegate to
1286 ;; us; while a user shouldn't get here, because of the odd status of
1287 ;; *wild-type* a type-intersection executed by the compiler can. -
1290 ;; (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1291 (cond ((eq type1
*empty-type
*)
1293 (;; When TYPE2 might be the universal type in disguise
1294 (type-might-contain-other-types-p type2
)
1295 ;; Now that the UNION and HAIRY COMPLEX-SUBTYPEP-ARG2 methods
1296 ;; can delegate to us (more or less as CALL-NEXT-METHOD) when
1297 ;; they're uncertain, we can't just barf on COMPOUND-TYPE and
1298 ;; HAIRY-TYPEs as we used to. Instead we deal with the
1299 ;; problem (where at least part of the problem is cases like
1300 ;; (SUBTYPEP T '(SATISFIES FOO))
1302 ;; (SUBTYPEP T '(AND (SATISFIES FOO) (SATISFIES BAR)))
1303 ;; where the second type is a hairy type like SATISFIES, or
1304 ;; is a compound type which might contain a hairy type) by
1305 ;; returning uncertainty.
1307 ((eq type1
*funcallable-instance-type
*)
1308 (values (eq type2
(specifier-type 'function
)) t
))
1310 ;; This case would have been picked off by the SIMPLE-SUBTYPEP
1311 ;; method, and so shouldn't appear here.
1312 (aver (not (named-type-p type2
)))
1313 ;; Since TYPE2 is not EQ *UNIVERSAL-TYPE* and is not another
1314 ;; named type in disguise, TYPE2 is not a superset of TYPE1.
1317 (!define-type-method
(named :complex-subtypep-arg2
) (type1 type2
)
1318 (aver (not (eq type2
*wild-type
*))) ; * isn't really a type.
1319 (cond ((eq type2
*universal-type
*)
1321 ;; some CONS types can conceal danger
1322 ((and (cons-type-p type1
) (cons-type-might-be-empty-type type1
))
1324 ((type-might-contain-other-types-p type1
)
1325 ;; those types can be other types in disguise. So we'd
1327 (invoke-complex-subtypep-arg1-method type1 type2
))
1328 ((and (or (eq type2
*instance-type
*)
1329 (eq type2
*funcallable-instance-type
*))
1330 (member-type-p type1
))
1331 ;; member types can be subtypep INSTANCE and
1332 ;; FUNCALLABLE-INSTANCE in surprising ways.
1333 (invoke-complex-subtypep-arg1-method type1 type2
))
1334 ((and (eq type2
*extended-sequence-type
*) (classoid-p type1
))
1335 (let* ((layout (classoid-layout type1
))
1336 (inherits (layout-inherits layout
))
1337 (sequencep (find (classoid-layout (find-classoid 'sequence
))
1339 (values (if sequencep t nil
) t
)))
1340 ((and (eq type2
*instance-type
*) (classoid-p type1
))
1341 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1343 (let* ((layout (classoid-layout type1
))
1344 (inherits (layout-inherits layout
))
1345 (functionp (find (classoid-layout (find-classoid 'function
))
1350 ((eq type1
(find-classoid 'function
))
1352 ((or (structure-classoid-p type1
)
1354 (condition-classoid-p type1
))
1356 (t (values nil nil
))))))
1357 ((and (eq type2
*funcallable-instance-type
*) (classoid-p type1
))
1358 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1360 (let* ((layout (classoid-layout type1
))
1361 (inherits (layout-inherits layout
))
1362 (functionp (find (classoid-layout (find-classoid 'function
))
1364 (values (if functionp t nil
) t
))))
1366 ;; FIXME: This seems to rely on there only being 4 or 5
1367 ;; NAMED-TYPE values, and the exclusion of various
1368 ;; possibilities above. It would be good to explain it and/or
1369 ;; rewrite it so that it's clearer.
1372 (!define-type-method
(named :complex-intersection2
) (type1 type2
)
1373 ;; FIXME: This assertion failed when I added it in sbcl-0.6.11.13.
1374 ;; Perhaps when bug 85 is fixed it can be reenabled.
1375 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1377 ((eq type2
*extended-sequence-type
*)
1379 (structure-classoid *empty-type
*)
1381 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1383 (if (find (classoid-layout (find-classoid 'sequence
))
1384 (layout-inherits (classoid-layout type1
)))
1388 (if (or (type-might-contain-other-types-p type1
)
1389 (member-type-p type1
))
1392 ((eq type2
*instance-type
*)
1394 (structure-classoid type1
)
1396 (if (and (not (member type1
*non-instance-classoid-types
*
1397 :key
#'find-classoid
))
1398 (not (eq type1
(find-classoid 'function
)))
1399 (not (find (classoid-layout (find-classoid 'function
))
1400 (layout-inherits (classoid-layout type1
)))))
1404 (if (or (type-might-contain-other-types-p type1
)
1405 (member-type-p type1
))
1408 ((eq type2
*funcallable-instance-type
*)
1410 (structure-classoid *empty-type
*)
1412 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1414 (if (find (classoid-layout (find-classoid 'function
))
1415 (layout-inherits (classoid-layout type1
)))
1417 (if (type= type1
(find-classoid 'function
))
1422 (if (or (type-might-contain-other-types-p type1
)
1423 (member-type-p type1
))
1426 (t (hierarchical-intersection2 type1 type2
))))
1428 (!define-type-method
(named :complex-union2
) (type1 type2
)
1429 ;; Perhaps when bug 85 is fixed this can be reenabled.
1430 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1432 ((eq type2
*extended-sequence-type
*)
1433 (if (classoid-p type1
)
1434 (if (or (member type1
*non-instance-classoid-types
*
1435 :key
#'find-classoid
)
1436 (not (find (classoid-layout (find-classoid 'sequence
))
1437 (layout-inherits (classoid-layout type1
)))))
1441 ((eq type2
*instance-type
*)
1442 (if (classoid-p type1
)
1443 (if (or (member type1
*non-instance-classoid-types
*
1444 :key
#'find-classoid
)
1445 (find (classoid-layout (find-classoid 'function
))
1446 (layout-inherits (classoid-layout type1
))))
1450 ((eq type2
*funcallable-instance-type
*)
1451 (if (classoid-p type1
)
1452 (if (or (member type1
*non-instance-classoid-types
*
1453 :key
#'find-classoid
)
1454 (not (find (classoid-layout (find-classoid 'function
))
1455 (layout-inherits (classoid-layout type1
)))))
1457 (if (eq type1
(specifier-type 'function
))
1461 (t (hierarchical-union2 type1 type2
))))
1463 (!define-type-method
(named :negate
) (x)
1464 (aver (not (eq x
*wild-type
*)))
1466 ((eq x
*universal-type
*) *empty-type
*)
1467 ((eq x
*empty-type
*) *universal-type
*)
1468 ((or (eq x
*instance-type
*)
1469 (eq x
*funcallable-instance-type
*)
1470 (eq x
*extended-sequence-type
*))
1471 (make-negation-type x
))
1472 (t (bug "NAMED type unexpected: ~S" x
))))
1474 (!define-type-method
(named :unparse
) (x)
1475 (named-type-name x
))
1477 ;;;; hairy and unknown types
1478 ;;;; DEFINE-TYPE-CLASS HAIRY is in 'early-type'
1480 (!define-type-method
(hairy :negate
) (x) (make-negation-type x
))
1482 (!define-type-method
(hairy :unparse
) (x)
1483 (hairy-type-specifier x
))
1485 (!define-type-method
(hairy :simple-subtypep
) (type1 type2
)
1486 (let ((hairy-spec1 (hairy-type-specifier type1
))
1487 (hairy-spec2 (hairy-type-specifier type2
)))
1488 (cond ((equal-but-no-car-recursion hairy-spec1 hairy-spec2
)
1490 ((maybe-reparse-specifier! type1
)
1491 (csubtypep type1 type2
))
1492 ((maybe-reparse-specifier! type2
)
1493 (csubtypep type1 type2
))
1495 (values nil nil
)))))
1497 (!define-type-method
(hairy :complex-subtypep-arg2
) (type1 type2
)
1498 (if (maybe-reparse-specifier! type2
)
1499 (csubtypep type1 type2
)
1500 (let ((specifier (hairy-type-specifier type2
)))
1501 (cond ((and (consp specifier
) (eql (car specifier
) 'satisfies
))
1502 (case (cadr specifier
)
1503 ((keywordp) (if (type= type1
(specifier-type 'symbol
))
1505 (invoke-complex-subtypep-arg1-method type1 type2
)))
1506 (t (invoke-complex-subtypep-arg1-method type1 type2
))))
1508 (invoke-complex-subtypep-arg1-method type1 type2
))))))
1510 (!define-type-method
(hairy :complex-subtypep-arg1
) (type1 type2
)
1511 (if (maybe-reparse-specifier! type1
)
1512 (csubtypep type1 type2
)
1515 (!define-type-method
(hairy :complex-
=) (type1 type2
)
1516 (if (maybe-reparse-specifier! type2
)
1520 (!define-type-method
(hairy :simple-intersection2
:complex-intersection2
)
1522 (acond ((type= type1 type2
)
1524 ((eq type2
*satisfies-keywordp-type
*)
1525 ;; (AND (MEMBER A) (SATISFIES KEYWORDP)) is possibly non-empty
1526 ;; if A is re-homed as :A. However as a special case that really
1527 ;; does occur, (AND (MEMBER NIL) (SATISFIES KEYWORDP))
1528 ;; is empty because of the illegality of changing NIL's package.
1529 (if (eq type1
*null-type
*)
1531 (multiple-value-bind (answer certain
)
1532 (types-equal-or-intersect type1
(specifier-type 'symbol
))
1533 (and (not answer
) certain
*empty-type
*))))
1534 ((eq type2
*fun-name-type
*)
1535 (multiple-value-bind (answer certain
)
1536 (types-equal-or-intersect type1
(specifier-type 'symbol
))
1539 (multiple-value-bind (answer certain
)
1540 (types-equal-or-intersect type1
(specifier-type 'cons
))
1541 (and (not answer
) certain
*empty-type
*)))))
1542 ((and (typep (hairy-type-specifier type2
) '(cons (eql satisfies
)))
1543 (info :function
:predicate-truth-constraint
1544 (cadr (hairy-type-specifier type2
))))
1545 (multiple-value-bind (answer certain
)
1546 (types-equal-or-intersect type1
(specifier-type it
))
1547 (and (not answer
) certain
*empty-type
*)))))
1549 (!define-type-method
(hairy :simple-union2
)
1551 (if (type= type1 type2
)
1555 (!define-type-method
(hairy :simple-
=) (type1 type2
)
1556 (if (equal-but-no-car-recursion (hairy-type-specifier type1
)
1557 (hairy-type-specifier type2
))
1561 (!def-type-translator satisfies
:list
(&whole whole predicate-name
)
1562 (unless (symbolp predicate-name
)
1563 (error 'simple-type-error
1564 :datum predicate-name
1565 :expected-type
'symbol
1566 :format-control
"The SATISFIES predicate name is not a symbol: ~S"
1567 :format-arguments
(list predicate-name
)))
1568 (case predicate-name
1569 (keywordp *satisfies-keywordp-type
*)
1570 (legal-fun-name-p *fun-name-type
*)
1571 (t (%make-hairy-type whole
))))
1575 (!define-type-method
(negation :negate
) (x)
1576 (negation-type-type x
))
1578 (!define-type-method
(negation :unparse
) (x)
1579 (if (type= (negation-type-type x
) (specifier-type 'cons
))
1581 `(not ,(type-specifier (negation-type-type x
)))))
1583 (!define-type-method
(negation :simple-subtypep
) (type1 type2
)
1584 (csubtypep (negation-type-type type2
) (negation-type-type type1
)))
1586 (!define-type-method
(negation :complex-subtypep-arg2
) (type1 type2
)
1587 (let* ((complement-type2 (negation-type-type type2
))
1588 (intersection2 (type-intersection2 type1
1591 ;; FIXME: if uncertain, maybe try arg1?
1592 (type= intersection2
*empty-type
*)
1593 (invoke-complex-subtypep-arg1-method type1 type2
))))
1595 (!define-type-method
(negation :complex-subtypep-arg1
) (type1 type2
)
1596 ;; "Incrementally extended heuristic algorithms tend inexorably toward the
1597 ;; incomprehensible." -- http://www.unlambda.com/~james/lambda/lambda.txt
1599 ;; You may not believe this. I couldn't either. But then I sat down
1600 ;; and drew lots of Venn diagrams. Comments involving a and b refer
1601 ;; to the call (subtypep '(not a) 'b) -- CSR, 2002-02-27.
1603 ;; (Several logical truths in this block are true as long as
1604 ;; b/=T. As of sbcl-0.7.1.28, it seems impossible to construct a
1605 ;; case with b=T where we actually reach this type method, but
1606 ;; we'll test for and exclude this case anyway, since future
1607 ;; maintenance might make it possible for it to end up in this
1609 (multiple-value-bind (equal certain
)
1610 (type= type2
*universal-type
*)
1612 (return (values nil nil
)))
1614 (return (values t t
))))
1615 (let ((complement-type1 (negation-type-type type1
)))
1616 ;; Do the special cases first, in order to give us a chance if
1617 ;; subtype/supertype relationships are hairy.
1618 (multiple-value-bind (equal certain
)
1619 (type= complement-type1 type2
)
1620 ;; If a = b, ~a is not a subtype of b (unless b=T, which was
1623 (return (values nil nil
)))
1625 (return (values nil t
))))
1626 ;; KLUDGE: ANSI requires that the SUBTYPEP result between any
1627 ;; two built-in atomic type specifiers never be uncertain. This
1628 ;; is hard to do cleanly for the built-in types whose
1629 ;; definitions include (NOT FOO), i.e. CONS and RATIO. However,
1630 ;; we can do it with this hack, which uses our global knowledge
1631 ;; that our implementation of the type system uses disjoint
1632 ;; implementation types to represent disjoint sets (except when
1633 ;; types are contained in other types). (This is a KLUDGE
1634 ;; because it's fragile. Various changes in internal
1635 ;; representation in the type system could make it start
1636 ;; confidently returning incorrect results.) -- WHN 2002-03-08
1637 (unless (or (type-might-contain-other-types-p complement-type1
)
1638 (type-might-contain-other-types-p type2
))
1639 ;; Because of the way our types which don't contain other
1640 ;; types are disjoint subsets of the space of possible values,
1641 ;; (SUBTYPEP '(NOT AA) 'B)=NIL when AA and B are simple (and B
1642 ;; is not T, as checked above).
1643 (return (values nil t
)))
1644 ;; The old (TYPE= TYPE1 TYPE2) branch would never be taken, as
1645 ;; TYPE1 and TYPE2 will only be equal if they're both NOT types,
1646 ;; and then the :SIMPLE-SUBTYPEP method would be used instead.
1647 ;; But a CSUBTYPEP relationship might still hold:
1648 (multiple-value-bind (equal certain
)
1649 (csubtypep complement-type1 type2
)
1650 ;; If a is a subtype of b, ~a is not a subtype of b (unless
1651 ;; b=T, which was excluded above).
1653 (return (values nil nil
)))
1655 (return (values nil t
))))
1656 (multiple-value-bind (equal certain
)
1657 (csubtypep type2 complement-type1
)
1658 ;; If b is a subtype of a, ~a is not a subtype of b. (FIXME:
1659 ;; That's not true if a=T. Do we know at this point that a is
1662 (return (values nil nil
)))
1664 (return (values nil t
))))
1665 ;; old CSR comment ca. 0.7.2, now obsoleted by the SIMPLE-CTYPE?
1666 ;; KLUDGE case above: Other cases here would rely on being able
1667 ;; to catch all possible cases, which the fragility of this type
1668 ;; system doesn't inspire me; for instance, if a is type= to ~b,
1669 ;; then we want T, T; if this is not the case and the types are
1670 ;; disjoint (have an intersection of *empty-type*) then we want
1671 ;; NIL, T; else if the union of a and b is the *universal-type*
1672 ;; then we want T, T. So currently we still claim to be unsure
1673 ;; about e.g. (subtypep '(not fixnum) 'single-float).
1675 ;; OTOH we might still get here:
1678 (!define-type-method
(negation :complex-
=) (type1 type2
)
1679 ;; (NOT FOO) isn't equivalent to anything that's not a negation
1680 ;; type, except possibly a type that might contain it in disguise.
1681 (declare (ignore type2
))
1682 (if (type-might-contain-other-types-p type1
)
1686 (!define-type-method
(negation :simple-intersection2
) (type1 type2
)
1687 (let ((not1 (negation-type-type type1
))
1688 (not2 (negation-type-type type2
)))
1690 ((csubtypep not1 not2
) type2
)
1691 ((csubtypep not2 not1
) type1
)
1692 ;; Why no analagous clause to the disjoint in the SIMPLE-UNION2
1693 ;; method, below? The clause would read
1695 ;; ((EQ (TYPE-UNION NOT1 NOT2) *UNIVERSAL-TYPE*) *EMPTY-TYPE*)
1697 ;; but with proper canonicalization of negation types, there's
1698 ;; no way of constructing two negation types with union of their
1699 ;; negations being the universal type.
1701 (aver (not (eq (type-union not1 not2
) *universal-type
*)))
1704 (defun maybe-complex-array-refinement (type1 type2
)
1705 (let* ((ntype (negation-type-type type2
))
1706 (ndims (array-type-dimensions ntype
))
1707 (ncomplexp (array-type-complexp ntype
))
1708 (nseltype (array-type-specialized-element-type ntype
))
1709 (neltype (array-type-element-type ntype
)))
1710 (if (and (eql ndims
'*) (null ncomplexp
)
1711 (eq neltype
*wild-type
*) (eq nseltype
*wild-type
*))
1712 (make-array-type (array-type-dimensions type1
)
1714 :element-type
(array-type-element-type type1
)
1715 :specialized-element-type
(array-type-specialized-element-type type1
)))))
1717 (!define-type-method
(negation :complex-intersection2
) (type1 type2
)
1719 ((csubtypep type1
(negation-type-type type2
)) *empty-type
*)
1720 ((eq (type-intersection type1
(negation-type-type type2
)) *empty-type
*)
1722 ((and (array-type-p type1
) (array-type-p (negation-type-type type2
)))
1723 (maybe-complex-array-refinement type1 type2
))
1726 (!define-type-method
(negation :simple-union2
) (type1 type2
)
1727 (let ((not1 (negation-type-type type1
))
1728 (not2 (negation-type-type type2
)))
1730 ((csubtypep not1 not2
) type1
)
1731 ((csubtypep not2 not1
) type2
)
1732 ((eq (type-intersection not1 not2
) *empty-type
*)
1736 (!define-type-method
(negation :complex-union2
) (type1 type2
)
1738 ((csubtypep (negation-type-type type2
) type1
) *universal-type
*)
1739 ((eq (type-intersection type1
(negation-type-type type2
)) *empty-type
*)
1743 (!define-type-method
(negation :simple-
=) (type1 type2
)
1744 (type= (negation-type-type type1
) (negation-type-type type2
)))
1746 (!def-type-translator not
:list
((:context context
) typespec
)
1747 (type-negation (specifier-type-r context typespec
)))
1751 (!define-type-class number
:enumerable
#'numeric-type-enumerable
1752 :might-contain-other-types nil
)
1754 (declaim (inline numeric-type-equal
))
1755 (defun numeric-type-equal (type1 type2
)
1756 (and (eq (numeric-type-class type1
) (numeric-type-class type2
))
1757 (eq (numeric-type-format type1
) (numeric-type-format type2
))
1758 (eq (numeric-type-complexp type1
) (numeric-type-complexp type2
))))
1760 (!define-type-method
(number :simple-
=) (type1 type2
)
1762 (and (numeric-type-equal type1 type2
)
1763 (equalp (numeric-type-low type1
) (numeric-type-low type2
))
1764 (equalp (numeric-type-high type1
) (numeric-type-high type2
)))
1767 (!define-type-method
(number :negate
) (type)
1768 (if (and (null (numeric-type-low type
)) (null (numeric-type-high type
)))
1769 (make-negation-type type
)
1771 (make-negation-type (modified-numeric-type type
:low nil
:high nil
))
1773 ((null (numeric-type-low type
))
1774 (modified-numeric-type
1776 :low
(let ((h (numeric-type-high type
)))
1777 (if (consp h
) (car h
) (list h
)))
1779 ((null (numeric-type-high type
))
1780 (modified-numeric-type
1783 :high
(let ((l (numeric-type-low type
)))
1784 (if (consp l
) (car l
) (list l
)))))
1786 (modified-numeric-type
1789 :high
(let ((l (numeric-type-low type
)))
1790 (if (consp l
) (car l
) (list l
))))
1791 (modified-numeric-type
1793 :low
(let ((h (numeric-type-high type
)))
1794 (if (consp h
) (car h
) (list h
)))
1797 (!define-type-method
(number :unparse
) (type)
1798 (let* ((complexp (numeric-type-complexp type
))
1799 (low (numeric-type-low type
))
1800 (high (numeric-type-high type
))
1801 (base (case (numeric-type-class type
)
1803 (rational 'rational
)
1804 (float (or (numeric-type-format type
) 'float
))
1807 (cond ((and (eq base
'integer
) high low
)
1808 (let ((high-count (logcount high
))
1809 (high-length (integer-length high
)))
1811 (cond ((= high
0) '(integer 0 0))
1813 ((and (= high-count high-length
)
1814 (plusp high-length
))
1815 `(unsigned-byte ,high-length
))
1817 `(mod ,(1+ high
)))))
1818 ((and (= low sb
!xc
:most-negative-fixnum
)
1819 (= high sb
!xc
:most-positive-fixnum
))
1821 ((and (= low
(lognot high
))
1822 (= high-count high-length
)
1824 `(signed-byte ,(1+ high-length
)))
1826 `(integer ,low
,high
)))))
1827 (high `(,base
,(or low
'*) ,high
))
1829 (if (and (eq base
'integer
) (= low
0))
1837 (aver (neq base
+bounds
'real
))
1838 `(complex ,base
+bounds
))
1840 (aver (eq base
+bounds
'real
))
1843 (!define-type-method
(number :singleton-p
) (type)
1844 (let ((low (numeric-type-low type
))
1845 (high (numeric-type-high type
)))
1848 (eql (numeric-type-complexp type
) :real
)
1849 (member (numeric-type-class type
) '(integer rational
1850 #-sb-xc-host float
)))
1851 (values t
(numeric-type-low type
))
1854 ;;; Return true if X is "less than or equal" to Y, taking open bounds
1855 ;;; into consideration. CLOSED is the predicate used to test the bound
1856 ;;; on a closed interval (e.g. <=), and OPEN is the predicate used on
1857 ;;; open bounds (e.g. <). Y is considered to be the outside bound, in
1858 ;;; the sense that if it is infinite (NIL), then the test succeeds,
1859 ;;; whereas if X is infinite, then the test fails (unless Y is also
1862 ;;; This is for comparing bounds of the same kind, e.g. upper and
1863 ;;; upper. Use NUMERIC-BOUND-TEST* for different kinds of bounds.
1864 (defmacro numeric-bound-test
(x y closed open
)
1869 (,closed
(car ,x
) (car ,y
))
1870 (,closed
(car ,x
) ,y
)))
1876 ;;; This is used to compare upper and lower bounds. This is different
1877 ;;; from the same-bound case:
1878 ;;; -- Since X = NIL is -infinity, whereas y = NIL is +infinity, we
1879 ;;; return true if *either* arg is NIL.
1880 ;;; -- an open inner bound is "greater" and also squeezes the interval,
1881 ;;; causing us to use the OPEN test for those cases as well.
1882 (defmacro numeric-bound-test
* (x y closed open
)
1887 (,open
(car ,x
) (car ,y
))
1888 (,open
(car ,x
) ,y
)))
1894 ;;; Return whichever of the numeric bounds X and Y is "maximal"
1895 ;;; according to the predicates CLOSED (e.g. >=) and OPEN (e.g. >).
1896 ;;; This is only meaningful for maximizing like bounds, i.e. upper and
1897 ;;; upper. If MAX-P is true, then we return NIL if X or Y is NIL,
1898 ;;; otherwise we return the other arg.
1899 (defmacro numeric-bound-max
(x y closed open max-p
)
1902 `(cond ((not ,n-x
) ,(if max-p nil n-y
))
1903 ((not ,n-y
) ,(if max-p nil n-x
))
1906 (if (,closed
(car ,n-x
) (car ,n-y
)) ,n-x
,n-y
)
1907 (if (,open
(car ,n-x
) ,n-y
) ,n-x
,n-y
)))
1910 (if (,open
(car ,n-y
) ,n-x
) ,n-y
,n-x
)
1911 (if (,closed
,n-y
,n-x
) ,n-y
,n-x
))))))
1913 (!define-type-method
(number :simple-subtypep
) (type1 type2
)
1914 (let ((class1 (numeric-type-class type1
))
1915 (class2 (numeric-type-class type2
))
1916 (complexp2 (numeric-type-complexp type2
))
1917 (format2 (numeric-type-format type2
))
1918 (low1 (numeric-type-low type1
))
1919 (high1 (numeric-type-high type1
))
1920 (low2 (numeric-type-low type2
))
1921 (high2 (numeric-type-high type2
)))
1922 ;; If one is complex and the other isn't, they are disjoint.
1923 (cond ((not (or (eq (numeric-type-complexp type1
) complexp2
)
1926 ;; If the classes are specified and different, the types are
1927 ;; disjoint unless type2 is RATIONAL and type1 is INTEGER.
1928 ;; [ or type1 is INTEGER and type2 is of the form (RATIONAL
1929 ;; X X) for integral X, but this is dealt with in the
1930 ;; canonicalization inside MAKE-NUMERIC-TYPE ]
1931 ((not (or (eq class1 class2
)
1933 (and (eq class1
'integer
) (eq class2
'rational
))))
1935 ;; If the float formats are specified and different, the types
1937 ((not (or (eq (numeric-type-format type1
) format2
)
1940 ;; Check the bounds.
1941 ((and (numeric-bound-test low1 low2
>= >)
1942 (numeric-bound-test high1 high2
<= <))
1947 (!define-superclasses number
((number)) !cold-init-forms
)
1949 ;;; If the high bound of LOW is adjacent to the low bound of HIGH,
1950 ;;; then return true, otherwise NIL.
1951 (defun numeric-types-adjacent (low high
)
1952 (let ((low-bound (numeric-type-high low
))
1953 (high-bound (numeric-type-low high
)))
1954 (cond ((not (and low-bound high-bound
)) nil
)
1955 ((and (consp low-bound
) (consp high-bound
)) nil
)
1957 (let ((low-value (car low-bound
)))
1958 (or (eql low-value high-bound
)
1960 (load-time-value (make-unportable-float
1961 :single-float-negative-zero
)))
1962 (eql high-bound
0f0
))
1963 (and (eql low-value
0f0
)
1965 (load-time-value (make-unportable-float
1966 :single-float-negative-zero
))))
1968 (load-time-value (make-unportable-float
1969 :double-float-negative-zero
)))
1970 (eql high-bound
0d0
))
1971 (and (eql low-value
0d0
)
1973 (load-time-value (make-unportable-float
1974 :double-float-negative-zero
)))))))
1976 (let ((high-value (car high-bound
)))
1977 (or (eql high-value low-bound
)
1978 (and (eql high-value
1979 (load-time-value (make-unportable-float
1980 :single-float-negative-zero
)))
1981 (eql low-bound
0f0
))
1982 (and (eql high-value
0f0
)
1984 (load-time-value (make-unportable-float
1985 :single-float-negative-zero
))))
1986 (and (eql high-value
1987 (load-time-value (make-unportable-float
1988 :double-float-negative-zero
)))
1989 (eql low-bound
0d0
))
1990 (and (eql high-value
0d0
)
1992 (load-time-value (make-unportable-float
1993 :double-float-negative-zero
)))))))
1994 ((and (eq (numeric-type-class low
) 'integer
)
1995 (eq (numeric-type-class high
) 'integer
))
1996 (eql (1+ low-bound
) high-bound
))
2000 ;;; Return a numeric type that is a supertype for both TYPE1 and TYPE2.
2002 ;;; Binding *APPROXIMATE-NUMERIC-UNIONS* to T allows merging non-adjacent
2003 ;;; numeric types, eg (OR (INTEGER 0 12) (INTEGER 20 128)) => (INTEGER 0 128),
2004 ;;; the compiler does this occasionally during type-derivation to avoid
2005 ;;; creating absurdly complex unions of numeric types.
2006 (defvar *approximate-numeric-unions
* nil
)
2008 (!define-type-method
(number :simple-union2
) (type1 type2
)
2009 (declare (type numeric-type type1 type2
))
2010 (cond ((csubtypep type1 type2
) type2
)
2011 ((csubtypep type2 type1
) type1
)
2013 (let ((class1 (numeric-type-class type1
))
2014 (format1 (numeric-type-format type1
))
2015 (complexp1 (numeric-type-complexp type1
))
2016 (class2 (numeric-type-class type2
))
2017 (format2 (numeric-type-format type2
))
2018 (complexp2 (numeric-type-complexp type2
)))
2020 ((and (eq class1 class2
)
2021 (eq format1 format2
)
2022 (eq complexp1 complexp2
)
2023 (or *approximate-numeric-unions
*
2024 (numeric-types-intersect type1 type2
)
2025 (numeric-types-adjacent type1 type2
)
2026 (numeric-types-adjacent type2 type1
)))
2031 :low
(numeric-bound-max (numeric-type-low type1
)
2032 (numeric-type-low type2
)
2034 :high
(numeric-bound-max (numeric-type-high type1
)
2035 (numeric-type-high type2
)
2037 ;; FIXME: These two clauses are almost identical, and the
2038 ;; consequents are in fact identical in every respect.
2039 ((and (eq class1
'rational
)
2040 (eq class2
'integer
)
2041 (eq format1 format2
)
2042 (eq complexp1 complexp2
)
2043 (integerp (numeric-type-low type2
))
2044 (integerp (numeric-type-high type2
))
2045 (= (numeric-type-low type2
) (numeric-type-high type2
))
2046 (or *approximate-numeric-unions
*
2047 (numeric-types-adjacent type1 type2
)
2048 (numeric-types-adjacent type2 type1
)))
2053 :low
(numeric-bound-max (numeric-type-low type1
)
2054 (numeric-type-low type2
)
2056 :high
(numeric-bound-max (numeric-type-high type1
)
2057 (numeric-type-high type2
)
2059 ((and (eq class1
'integer
)
2060 (eq class2
'rational
)
2061 (eq format1 format2
)
2062 (eq complexp1 complexp2
)
2063 (integerp (numeric-type-low type1
))
2064 (integerp (numeric-type-high type1
))
2065 (= (numeric-type-low type1
) (numeric-type-high type1
))
2066 (or *approximate-numeric-unions
*
2067 (numeric-types-adjacent type1 type2
)
2068 (numeric-types-adjacent type2 type1
)))
2073 :low
(numeric-bound-max (numeric-type-low type1
)
2074 (numeric-type-low type2
)
2076 :high
(numeric-bound-max (numeric-type-high type1
)
2077 (numeric-type-high type2
)
2082 (!cold-init-forms
;; is !PRECOMPUTE-TYPES not doing the right thing?
2083 (setf (info :type
:kind
'number
) :primitive
)
2084 (setf (info :type
:builtin
'number
)
2085 (make-numeric-type :complexp nil
)))
2087 (!def-type-translator complex
((:context context
) &optional
(typespec '*))
2088 (if (eq typespec
'*)
2089 (specifier-type '(complex real
))
2090 (labels ((not-numeric ()
2091 (error "The component type for COMPLEX is not numeric: ~S"
2094 (error "The component type for COMPLEX is not a subtype of REAL: ~S"
2096 (complex1 (component-type)
2097 (unless (numeric-type-p component-type
)
2099 (when (eq (numeric-type-complexp component-type
) :complex
)
2101 (if (csubtypep component-type
(specifier-type '(eql 0)))
2103 (modified-numeric-type component-type
2104 :complexp
:complex
)))
2107 ((eq ctype
*empty-type
*) *empty-type
*)
2108 ((eq ctype
*universal-type
*) (not-real))
2109 ((typep ctype
'numeric-type
) (complex1 ctype
))
2110 ((typep ctype
'union-type
)
2112 (mapcar #'do-complex
(union-type-types ctype
))))
2113 ((typep ctype
'member-type
)
2115 (mapcar-member-type-members
2116 (lambda (x) (do-complex (ctype-of x
)))
2118 ((and (typep ctype
'intersection-type
)
2119 ;; FIXME: This is very much a
2120 ;; not-quite-worst-effort, but we are required to do
2121 ;; something here because of our representation of
2122 ;; RATIO as (AND RATIONAL (NOT INTEGER)): we must
2123 ;; allow users to ask about (COMPLEX RATIO). This
2124 ;; will of course fail to work right on such types
2125 ;; as (AND INTEGER (SATISFIES ZEROP))...
2126 (let ((numbers (remove-if-not
2128 (intersection-type-types ctype
))))
2130 (null (cdr numbers
))
2131 (eq (numeric-type-complexp (car numbers
)) :real
)
2132 (complex1 (car numbers
))))))
2134 (multiple-value-bind (subtypep certainly
)
2135 (csubtypep ctype
(specifier-type 'real
))
2136 (if (and (not subtypep
) certainly
)
2138 ;; ANSI just says that TYPESPEC is any subtype of
2139 ;; type REAL, not necessarily a NUMERIC-TYPE. In
2140 ;; particular, at this point TYPESPEC could legally
2141 ;; be a hairy type like (AND NUMBER (SATISFIES
2142 ;; REALP) (SATISFIES ZEROP)), in which case we fall
2143 ;; through the logic above and end up here,
2145 ;; FIXME: (COMPLEX NUMBER) is not rejected but should
2146 ;; be, as NUMBER is clearly not a subtype of real.
2147 (bug "~@<(known bug #145): The type ~S is too hairy to be ~
2148 used for a COMPLEX component.~:@>"
2150 (let ((ctype (specifier-type-r context typespec
)))
2151 (do-complex ctype
)))))
2153 ;;; If X is *, return NIL, otherwise return the bound, which must be a
2154 ;;; member of TYPE or a one-element list of a member of TYPE.
2155 #!-sb-fluid
(declaim (inline canonicalized-bound
))
2156 (defun canonicalized-bound (bound type
)
2157 (cond ((eq bound
'*) nil
)
2158 ((or (sb!xc
:typep bound type
)
2160 (sb!xc
:typep
(car bound
) type
)
2161 (null (cdr bound
))))
2164 (error "Bound is not ~S, a ~S or a list of a ~S: ~S"
2170 (!def-type-translator integer
(&optional
(low '*) (high '*))
2171 (let* ((l (canonicalized-bound low
'integer
))
2172 (lb (if (consp l
) (1+ (car l
)) l
))
2173 (h (canonicalized-bound high
'integer
))
2174 (hb (if (consp h
) (1- (car h
)) h
)))
2175 (if (and hb lb
(< hb lb
))
2177 (make-numeric-type :class
'integer
2179 :enumerable
(not (null (and l h
)))
2183 (defmacro !def-bounded-type
(type class format
)
2184 `(!def-type-translator
,type
(&optional
(low '*) (high '*))
2185 (let ((lb (canonicalized-bound low
',type
))
2186 (hb (canonicalized-bound high
',type
)))
2187 (if (not (numeric-bound-test* lb hb
<= <))
2189 (make-numeric-type :class
',class
2194 (!def-bounded-type rational rational nil
)
2196 ;;; Unlike CMU CL, we represent the types FLOAT and REAL as
2197 ;;; UNION-TYPEs of more primitive types, in order to make
2198 ;;; type representation more unique, avoiding problems in the
2199 ;;; simplification of things like
2200 ;;; (subtypep '(or (single-float -1.0 1.0) (single-float 0.1))
2201 ;;; '(or (real -1 7) (single-float 0.1) (single-float -1.0 1.0)))
2202 ;;; When we allowed REAL to remain as a separate NUMERIC-TYPE,
2203 ;;; it was too easy for the first argument to be simplified to
2204 ;;; '(SINGLE-FLOAT -1.0), and for the second argument to be simplified
2205 ;;; to '(OR (REAL -1 7) (SINGLE-FLOAT 0.1)) and then for the
2206 ;;; SUBTYPEP to fail (returning NIL,T instead of T,T) because
2207 ;;; the first argument can't be seen to be a subtype of any of the
2208 ;;; terms in the second argument.
2210 ;;; The old CMU CL way was:
2211 ;;; (!def-bounded-type float float nil)
2212 ;;; (!def-bounded-type real nil nil)
2214 ;;; FIXME: If this new way works for a while with no weird new
2215 ;;; problems, we can go back and rip out support for separate FLOAT
2216 ;;; and REAL flavors of NUMERIC-TYPE. The new way was added in
2217 ;;; sbcl-0.6.11.22, 2001-03-21.
2219 ;;; FIXME: It's probably necessary to do something to fix the
2220 ;;; analogous problem with INTEGER and RATIONAL types. Perhaps
2221 ;;; bounded RATIONAL types should be represented as (OR RATIO INTEGER).
2222 (defun coerce-bound (bound type upperp inner-coerce-bound-fun
)
2223 (declare (type function inner-coerce-bound-fun
))
2226 (funcall inner-coerce-bound-fun bound type upperp
)))
2227 (defun inner-coerce-real-bound (bound type upperp
)
2228 #+sb-xc-host
(declare (ignore upperp
))
2229 (let #+sb-xc-host
()
2231 ((nl (load-time-value (symbol-value 'sb
!xc
:most-negative-long-float
)))
2232 (pl (load-time-value (symbol-value 'sb
!xc
:most-positive-long-float
))))
2233 (let ((nbound (if (consp bound
) (car bound
) bound
))
2234 (consp (consp bound
)))
2238 (list (rational nbound
))
2242 ((floatp nbound
) bound
)
2244 ;; Coerce to the widest float format available, to avoid
2245 ;; unnecessary loss of precision, but don't coerce
2246 ;; unrepresentable numbers, except on the host where we
2247 ;; shouldn't be making these types (but KLUDGE: can't even
2248 ;; assert portably that we're not).
2252 (when (< nbound nl
) (return-from inner-coerce-real-bound nl
)))
2254 (when (> nbound pl
) (return-from inner-coerce-real-bound pl
))))
2255 (let ((result (coerce nbound
'long-float
)))
2256 (if consp
(list result
) result
)))))))))
2257 (defun inner-coerce-float-bound (bound type upperp
)
2258 #+sb-xc-host
(declare (ignore upperp
))
2259 (let #+sb-xc-host
()
2261 ((nd (load-time-value (symbol-value 'sb
!xc
:most-negative-double-float
)))
2262 (pd (load-time-value (symbol-value 'sb
!xc
:most-positive-double-float
)))
2263 (ns (load-time-value (symbol-value 'sb
!xc
:most-negative-single-float
)))
2264 (ps (load-time-value
2265 (symbol-value 'sb
!xc
:most-positive-single-float
))))
2266 (let ((nbound (if (consp bound
) (car bound
) bound
))
2267 (consp (consp bound
)))
2271 ((typep nbound
'single-float
) bound
)
2276 (when (< nbound ns
) (return-from inner-coerce-float-bound ns
)))
2278 (when (> nbound ps
) (return-from inner-coerce-float-bound ps
))))
2279 (let ((result (coerce nbound
'single-float
)))
2280 (if consp
(list result
) result
)))))
2283 ((typep nbound
'double-float
) bound
)
2288 (when (< nbound nd
) (return-from inner-coerce-float-bound nd
)))
2290 (when (> nbound pd
) (return-from inner-coerce-float-bound pd
))))
2291 (let ((result (coerce nbound
'double-float
)))
2292 (if consp
(list result
) result
)))))))))
2293 (defun coerced-real-bound (bound type upperp
)
2294 (coerce-bound bound type upperp
#'inner-coerce-real-bound
))
2295 (defun coerced-float-bound (bound type upperp
)
2296 (coerce-bound bound type upperp
#'inner-coerce-float-bound
))
2297 (!def-type-translator real
(&optional
(low '*) (high '*))
2298 (specifier-type `(or (float ,(coerced-real-bound low
'float nil
)
2299 ,(coerced-real-bound high
'float t
))
2300 (rational ,(coerced-real-bound low
'rational nil
)
2301 ,(coerced-real-bound high
'rational t
)))))
2302 (!def-type-translator float
(&optional
(low '*) (high '*))
2304 `(or (single-float ,(coerced-float-bound low
'single-float nil
)
2305 ,(coerced-float-bound high
'single-float t
))
2306 (double-float ,(coerced-float-bound low
'double-float nil
)
2307 ,(coerced-float-bound high
'double-float t
))
2308 #!+long-float
,(error "stub: no long float support yet"))))
2310 (defmacro !define-float-format
(f)
2311 `(!def-bounded-type
,f float
,f
))
2313 ;; (!define-float-format short-float) ; it's a DEFTYPE
2314 (!define-float-format single-float
)
2315 (!define-float-format double-float
)
2316 ;; long-float support is dead.
2317 ;; (!define-float-format long-float) ; also a DEFTYPE
2319 (defun numeric-types-intersect (type1 type2
)
2320 (declare (type numeric-type type1 type2
))
2321 (let* ((class1 (numeric-type-class type1
))
2322 (class2 (numeric-type-class type2
))
2323 (complexp1 (numeric-type-complexp type1
))
2324 (complexp2 (numeric-type-complexp type2
))
2325 (format1 (numeric-type-format type1
))
2326 (format2 (numeric-type-format type2
))
2327 (low1 (numeric-type-low type1
))
2328 (high1 (numeric-type-high type1
))
2329 (low2 (numeric-type-low type2
))
2330 (high2 (numeric-type-high type2
)))
2331 ;; If one is complex and the other isn't, then they are disjoint.
2332 (cond ((not (or (eq complexp1 complexp2
)
2333 (null complexp1
) (null complexp2
)))
2335 ;; If either type is a float, then the other must either be
2336 ;; specified to be a float or unspecified. Otherwise, they
2338 ((and (eq class1
'float
)
2339 (not (member class2
'(float nil
)))) nil
)
2340 ((and (eq class2
'float
)
2341 (not (member class1
'(float nil
)))) nil
)
2342 ;; If the float formats are specified and different, the
2343 ;; types are disjoint.
2344 ((not (or (eq format1 format2
) (null format1
) (null format2
)))
2347 ;; Check the bounds. This is a bit odd because we must
2348 ;; always have the outer bound of the interval as the
2350 (if (numeric-bound-test high1 high2
<= <)
2351 (or (and (numeric-bound-test low1 low2
>= >)
2352 (numeric-bound-test* low1 high2
<= <))
2353 (and (numeric-bound-test low2 low1
>= >)
2354 (numeric-bound-test* low2 high1
<= <)))
2355 (or (and (numeric-bound-test* low2 high1
<= <)
2356 (numeric-bound-test low2 low1
>= >))
2357 (and (numeric-bound-test high2 high1
<= <)
2358 (numeric-bound-test* high2 low1
>= >))))))))
2360 ;;; Take the numeric bound X and convert it into something that can be
2361 ;;; used as a bound in a numeric type with the specified CLASS and
2362 ;;; FORMAT. If UP-P is true, then we round up as needed, otherwise we
2363 ;;; round down. UP-P true implies that X is a lower bound, i.e. (N) > N.
2365 ;;; This is used by NUMERIC-TYPE-INTERSECTION to mash the bound into
2366 ;;; the appropriate type number. X may only be a float when CLASS is
2369 ;;; ### Note: it is possible for the coercion to a float to overflow
2370 ;;; or underflow. This happens when the bound doesn't fit in the
2371 ;;; specified format. In this case, we should really return the
2372 ;;; appropriate {Most | Least}-{Positive | Negative}-XXX-Float float
2373 ;;; of desired format. But these conditions aren't currently signalled
2374 ;;; in any useful way.
2376 ;;; Also, when converting an open rational bound into a float we
2377 ;;; should probably convert it to a closed bound of the closest float
2378 ;;; in the specified format. KLUDGE: In general, open float bounds are
2379 ;;; screwed up. -- (comment from original CMU CL)
2380 (defun round-numeric-bound (x class format up-p
)
2382 (let ((cx (if (consp x
) (car x
) x
)))
2386 (if (and (consp x
) (integerp cx
))
2387 (if up-p
(1+ cx
) (1- cx
))
2388 (if up-p
(ceiling cx
) (floor cx
))))
2392 ((and format
(subtypep format
'double-float
))
2393 (if (<= most-negative-double-float cx most-positive-double-float
)
2397 (if (<= most-negative-single-float cx most-positive-single-float
)
2399 (coerce cx
(or format
'single-float
))
2401 (if (consp x
) (list res
) res
)))))
2404 ;;; Handle the case of type intersection on two numeric types. We use
2405 ;;; TYPES-EQUAL-OR-INTERSECT to throw out the case of types with no
2406 ;;; intersection. If an attribute in TYPE1 is unspecified, then we use
2407 ;;; TYPE2's attribute, which must be at least as restrictive. If the
2408 ;;; types intersect, then the only attributes that can be specified
2409 ;;; and different are the class and the bounds.
2411 ;;; When the class differs, we use the more restrictive class. The
2412 ;;; only interesting case is RATIONAL/INTEGER, since RATIONAL includes
2415 ;;; We make the result lower (upper) bound the maximum (minimum) of
2416 ;;; the argument lower (upper) bounds. We convert the bounds into the
2417 ;;; appropriate numeric type before maximizing. This avoids possible
2418 ;;; confusion due to mixed-type comparisons (but I think the result is
2420 (!define-type-method
(number :simple-intersection2
) (type1 type2
)
2421 (declare (type numeric-type type1 type2
))
2422 (if (numeric-types-intersect type1 type2
)
2423 (let* ((class1 (numeric-type-class type1
))
2424 (class2 (numeric-type-class type2
))
2425 (class (ecase class1
2427 ((integer float
) class1
)
2428 (rational (if (eq class2
'integer
)
2431 (format (or (numeric-type-format type1
)
2432 (numeric-type-format type2
))))
2436 :complexp
(or (numeric-type-complexp type1
)
2437 (numeric-type-complexp type2
))
2438 :low
(numeric-bound-max
2439 (round-numeric-bound (numeric-type-low type1
)
2441 (round-numeric-bound (numeric-type-low type2
)
2444 :high
(numeric-bound-max
2445 (round-numeric-bound (numeric-type-high type1
)
2447 (round-numeric-bound (numeric-type-high type2
)
2452 ;;; Given two float formats, return the one with more precision. If
2453 ;;; either one is null, return NIL.
2454 (defun float-format-max (f1 f2
)
2456 (dolist (f *float-formats
* (error "bad float format: ~S" f1
))
2457 (when (or (eq f f1
) (eq f f2
))
2460 ;;; Return the result of an operation on TYPE1 and TYPE2 according to
2461 ;;; the rules of numeric contagion. This is always NUMBER, some float
2462 ;;; format (possibly complex) or RATIONAL. Due to rational
2463 ;;; canonicalization, there isn't much we can do here with integers or
2464 ;;; rational complex numbers.
2466 ;;; If either argument is not a NUMERIC-TYPE, then return NUMBER. This
2467 ;;; is useful mainly for allowing types that are technically numbers,
2468 ;;; but not a NUMERIC-TYPE.
2469 (defun numeric-contagion (type1 type2
)
2470 (if (and (numeric-type-p type1
) (numeric-type-p type2
))
2471 (let ((class1 (numeric-type-class type1
))
2472 (class2 (numeric-type-class type2
))
2473 (format1 (numeric-type-format type1
))
2474 (format2 (numeric-type-format type2
))
2475 (complexp1 (numeric-type-complexp type1
))
2476 (complexp2 (numeric-type-complexp type2
)))
2477 (cond ((or (null complexp1
)
2479 (specifier-type 'number
))
2483 :format
(ecase class2
2484 (float (float-format-max format1 format2
))
2485 ((integer rational
) format1
)
2487 ;; A double-float with any real number is a
2490 (if (eq format1
'double-float
)
2493 ;; A long-float with any real number is a
2496 (if (eq format1
'long-float
)
2499 :complexp
(if (or (eq complexp1
:complex
)
2500 (eq complexp2
:complex
))
2503 ((eq class2
'float
) (numeric-contagion type2 type1
))
2504 ((and (eq complexp1
:real
) (eq complexp2
:real
))
2506 :class
(and class1 class2
'rational
)
2509 (specifier-type 'number
))))
2510 (specifier-type 'number
)))
2514 (!define-type-class array
:enumerable nil
2515 :might-contain-other-types nil
)
2517 (!define-type-method
(array :simple-
=) (type1 type2
)
2518 (cond ((not (and (equal (array-type-dimensions type1
)
2519 (array-type-dimensions type2
))
2520 (eq (array-type-complexp type1
)
2521 (array-type-complexp type2
))))
2523 ((or (unknown-type-p (array-type-element-type type1
))
2524 (unknown-type-p (array-type-element-type type2
)))
2525 (type= (array-type-element-type type1
)
2526 (array-type-element-type type2
)))
2528 (values (type= (array-type-specialized-element-type type1
)
2529 (array-type-specialized-element-type type2
))
2532 (!define-type-method
(array :negate
) (type)
2533 ;; FIXME (and hint to PFD): we're vulnerable here to attacks of the
2534 ;; form "are (AND ARRAY (NOT (ARRAY T))) and (OR (ARRAY BIT) (ARRAY
2535 ;; NIL) (ARRAY CHAR) ...) equivalent?" -- CSR, 2003-12-10
2536 ;; A symptom of the aforementioned is that the following are not TYPE=
2537 ;; (AND (VECTOR T) (NOT SIMPLE-ARRAY)) ; an ARRAY-TYPE
2538 ;; (AND (VECTOR T) (NOT SIMPLE-VECTOR)) ; an INTERSECTION-TYPE
2539 ;; even though (VECTOR T) makes it so that the (NOT) clause in each can
2540 ;; only provide one additional bit of information: that the vector
2541 ;; is complex as opposed to simple. The rank and element-type are fixed.
2542 (if (and (eq (array-type-dimensions type
) '*)
2543 (eq (array-type-complexp type
) 't
)
2544 (eq (array-type-element-type type
) *wild-type
*))
2545 ;; (NOT <hairy-array>) = either SIMPLE-ARRAY or (NOT ARRAY).
2546 ;; This is deliberately asymmetric - trying to say that NOT simple-array
2547 ;; equals hairy-array leads to infinite recursion.
2548 (type-union (make-array-type '* :complexp nil
2549 :element-type
*wild-type
*)
2551 (make-array-type '* :element-type
*wild-type
*)))
2552 (make-negation-type type
)))
2554 (!define-type-method
(array :unparse
) (type)
2555 (let* ((dims (array-type-dimensions type
))
2556 ;; Compare the specialised element type and the
2557 ;; derived element type. If the derived type
2558 ;; is so small that it jumps to a smaller upgraded
2559 ;; element type, use the specialised element type.
2561 ;; This protects from unparsing
2562 ;; (and (vector (or bit symbol))
2563 ;; (vector (or bit character)))
2564 ;; i.e., the intersection of two T array types,
2566 (stype (array-type-specialized-element-type type
))
2567 (dtype (array-type-element-type type
))
2568 (utype (%upgraded-array-element-type dtype
))
2569 (eltype (type-specifier (if (type= stype utype
)
2572 (complexp (array-type-complexp type
)))
2573 (if (and (eq complexp t
) (not *unparse-allow-negation
*))
2574 (setq complexp
:maybe
))
2578 ((t) '(and array
(not simple-array
)))
2580 ((nil) 'simple-array
))
2582 ((t) `(and (array ,eltype
) (not simple-array
)))
2583 ((:maybe
) `(array ,eltype
))
2584 ((nil) `(simple-array ,eltype
)))))
2585 ((= (length dims
) 1)
2588 (if (eq (car dims
) '*)
2591 ((base-char #!-sb-unicode character
) 'base-string
)
2593 (t `(vector ,eltype
)))
2595 (bit `(bit-vector ,(car dims
)))
2596 ((base-char #!-sb-unicode character
)
2597 `(base-string ,(car dims
)))
2598 (t `(vector ,eltype
,(car dims
)))))))
2599 (if (eql complexp
:maybe
)
2601 `(and ,answer
(not simple-array
))))
2602 (if (eq (car dims
) '*)
2604 (bit 'simple-bit-vector
)
2605 ((base-char #!-sb-unicode character
) 'simple-base-string
)
2606 ((t) 'simple-vector
)
2607 (t `(simple-array ,eltype
(*))))
2609 (bit `(simple-bit-vector ,(car dims
)))
2610 ((base-char #!-sb-unicode character
)
2611 `(simple-base-string ,(car dims
)))
2612 ((t) `(simple-vector ,(car dims
)))
2613 (t `(simple-array ,eltype
,dims
))))))
2616 ((t) `(and (array ,eltype
,dims
) (not simple-array
)))
2617 ((:maybe
) `(array ,eltype
,dims
))
2618 ((nil) `(simple-array ,eltype
,dims
)))))))
2620 (!define-type-method
(array :simple-subtypep
) (type1 type2
)
2621 (let ((dims1 (array-type-dimensions type1
))
2622 (dims2 (array-type-dimensions type2
))
2623 (complexp2 (array-type-complexp type2
)))
2624 (cond (;; not subtypep unless dimensions are compatible
2625 (not (or (eq dims2
'*)
2626 (and (not (eq dims1
'*))
2627 ;; (sbcl-0.6.4 has trouble figuring out that
2628 ;; DIMS1 and DIMS2 must be lists at this
2629 ;; point, and knowing that is important to
2630 ;; compiling EVERY efficiently.)
2631 (= (length (the list dims1
))
2632 (length (the list dims2
)))
2633 (every (lambda (x y
)
2634 (or (eq y
'*) (eql x y
)))
2636 (the list dims2
)))))
2638 ;; not subtypep unless complexness is compatible
2639 ((not (or (eq complexp2
:maybe
)
2640 (eq (array-type-complexp type1
) complexp2
)))
2642 ;; Since we didn't fail any of the tests above, we win
2643 ;; if the TYPE2 element type is wild.
2644 ((eq (array-type-element-type type2
) *wild-type
*)
2646 (;; Since we didn't match any of the special cases above, if
2647 ;; either element type is unknown we can only give a good
2648 ;; answer if they are the same.
2649 (or (unknown-type-p (array-type-element-type type1
))
2650 (unknown-type-p (array-type-element-type type2
)))
2651 (if (type= (array-type-element-type type1
)
2652 (array-type-element-type type2
))
2655 (;; Otherwise, the subtype relationship holds iff the
2656 ;; types are equal, and they're equal iff the specialized
2657 ;; element types are identical.
2659 (values (type= (array-type-specialized-element-type type1
)
2660 (array-type-specialized-element-type type2
))
2663 (!define-superclasses array
2664 ((vector vector
) (array))
2667 (defun array-types-intersect (type1 type2
)
2668 (declare (type array-type type1 type2
))
2669 (let ((dims1 (array-type-dimensions type1
))
2670 (dims2 (array-type-dimensions type2
))
2671 (complexp1 (array-type-complexp type1
))
2672 (complexp2 (array-type-complexp type2
)))
2673 ;; See whether dimensions are compatible.
2674 (cond ((not (or (eq dims1
'*) (eq dims2
'*)
2675 (and (= (length dims1
) (length dims2
))
2676 (every (lambda (x y
)
2677 (or (eq x
'*) (eq y
'*) (= x y
)))
2680 ;; See whether complexpness is compatible.
2681 ((not (or (eq complexp1
:maybe
)
2682 (eq complexp2
:maybe
)
2683 (eq complexp1 complexp2
)))
2687 ;; If either element type is wild, then they intersect.
2688 ;; Otherwise, the types must be identical.
2690 ;; FIXME: There seems to have been a fair amount of
2691 ;; confusion about the distinction between requested element
2692 ;; type and specialized element type; here is one of
2693 ;; them. If we request an array to hold objects of an
2694 ;; unknown type, we can do no better than represent that
2695 ;; type as an array specialized on wild-type. We keep the
2696 ;; requested element-type in the -ELEMENT-TYPE slot, and
2697 ;; *WILD-TYPE* in the -SPECIALIZED-ELEMENT-TYPE. So, here,
2698 ;; we must test for the SPECIALIZED slot being *WILD-TYPE*,
2699 ;; not just the ELEMENT-TYPE slot. Maybe the return value
2700 ;; in that specific case should be T, NIL? Or maybe this
2701 ;; function should really be called
2702 ;; ARRAY-TYPES-COULD-POSSIBLY-INTERSECT? In any case, this
2703 ;; was responsible for bug #123, and this whole issue could
2704 ;; do with a rethink and/or a rewrite. -- CSR, 2002-08-21
2705 ((or (eq (array-type-specialized-element-type type1
) *wild-type
*)
2706 (eq (array-type-specialized-element-type type2
) *wild-type
*)
2707 (type= (array-type-specialized-element-type type1
)
2708 (array-type-specialized-element-type type2
)))
2714 (defun unite-array-types-complexp (type1 type2
)
2715 (let ((complexp1 (array-type-complexp type1
))
2716 (complexp2 (array-type-complexp type2
)))
2718 ((eq complexp1 complexp2
)
2719 ;; both types are the same complexp-ity
2720 (values complexp1 t
))
2721 ((eq complexp1
:maybe
)
2722 ;; type1 is wild-complexp
2723 (values :maybe type1
))
2724 ((eq complexp2
:maybe
)
2725 ;; type2 is wild-complexp
2726 (values :maybe type2
))
2728 ;; both types partition the complexp-space
2729 (values :maybe nil
)))))
2731 (defun unite-array-types-dimensions (type1 type2
)
2732 (let ((dims1 (array-type-dimensions type1
))
2733 (dims2 (array-type-dimensions type2
)))
2734 (cond ((equal dims1 dims2
)
2735 ;; both types are same dimensionality
2738 ;; type1 is wild-dimensions
2741 ;; type2 is wild-dimensions
2743 ((not (= (length dims1
) (length dims2
)))
2744 ;; types have different number of dimensions
2745 (values :incompatible nil
))
2747 ;; we need to check on a per-dimension basis
2748 (let* ((supertype1 t
)
2751 (result (mapcar (lambda (dim1 dim2
)
2756 (setf supertype2 nil
)
2759 (setf supertype1 nil
)
2762 (setf compatible nil
))))
2765 ((or (not compatible
)
2766 (and (not supertype1
)
2768 (values :incompatible nil
))
2769 ((and supertype1 supertype2
)
2770 (values result supertype1
))
2772 (values result
(if supertype1 type1 type2
)))))))))
2774 (defun unite-array-types-element-types (type1 type2
)
2775 ;; FIXME: We'd love to be able to unite the full set of specialized
2776 ;; array element types up to *wild-type*, but :simple-union2 is
2777 ;; performed pairwise, so we don't have a good hook for it and our
2778 ;; representation doesn't allow us to easily detect the situation
2780 ;; But see SIMPLIFY-ARRAY-UNIONS which is able to do something like that.
2781 (let* ((eltype1 (array-type-element-type type1
))
2782 (eltype2 (array-type-element-type type2
))
2783 (stype1 (array-type-specialized-element-type type1
))
2784 (stype2 (array-type-specialized-element-type type2
))
2785 (wild1 (eq eltype1
*wild-type
*))
2786 (wild2 (eq eltype2
*wild-type
*)))
2788 ((type= eltype1 eltype2
)
2789 (values eltype1 stype1 t
))
2791 (values eltype1 stype1 type1
))
2793 (values eltype2 stype2 type2
))
2794 ((not (type= stype1 stype2
))
2795 ;; non-wild types that don't share UAET don't unite
2796 (values :incompatible nil nil
))
2797 ((csubtypep eltype1 eltype2
)
2798 (values eltype2 stype2 type2
))
2799 ((csubtypep eltype2 eltype1
)
2800 (values eltype1 stype1 type1
))
2802 (values :incompatible nil nil
)))))
2804 (defun unite-array-types-supertypes-compatible-p (&rest supertypes
)
2805 ;; supertypes are compatible if they are all T, if there is a single
2806 ;; NIL and all the rest are T, or if all non-T supertypes are the
2807 ;; same and not NIL.
2808 (let ((interesting-supertypes
2809 (remove t supertypes
)))
2810 (or (not interesting-supertypes
)
2811 (equal interesting-supertypes
'(nil))
2812 ;; supertypes are (OR BOOLEAN ARRAY-TYPE), so...
2813 (typep (remove-duplicates interesting-supertypes
)
2814 '(cons array-type null
)))))
2816 (!define-type-method
(array :simple-union2
) (type1 type2
)
2817 (multiple-value-bind
2818 (result-eltype result-stype eltype-supertype
)
2819 (unite-array-types-element-types type1 type2
)
2820 (multiple-value-bind
2821 (result-complexp complexp-supertype
)
2822 (unite-array-types-complexp type1 type2
)
2823 (multiple-value-bind
2824 (result-dimensions dimensions-supertype
)
2825 (unite-array-types-dimensions type1 type2
)
2826 (when (and (not (eq result-dimensions
:incompatible
))
2827 (not (eq result-eltype
:incompatible
))
2828 (unite-array-types-supertypes-compatible-p
2829 eltype-supertype complexp-supertype dimensions-supertype
))
2830 (make-array-type result-dimensions
2831 :complexp result-complexp
2832 :element-type result-eltype
2833 :specialized-element-type result-stype
))))))
2835 (!define-type-method
(array :simple-intersection2
) (type1 type2
)
2836 (declare (type array-type type1 type2
))
2837 (if (array-types-intersect type1 type2
)
2838 (let ((dims1 (array-type-dimensions type1
))
2839 (dims2 (array-type-dimensions type2
))
2840 (complexp1 (array-type-complexp type1
))
2841 (complexp2 (array-type-complexp type2
))
2842 (eltype1 (array-type-element-type type1
))
2843 (eltype2 (array-type-element-type type2
))
2844 (stype1 (array-type-specialized-element-type type1
))
2845 (stype2 (array-type-specialized-element-type type2
)))
2846 (make-array-type (cond ((eq dims1
'*) dims2
)
2847 ((eq dims2
'*) dims1
)
2849 (mapcar (lambda (x y
) (if (eq x
'*) y x
))
2851 :complexp
(if (eq complexp1
:maybe
) complexp2 complexp1
)
2853 ((eq eltype1
*wild-type
*) eltype2
)
2854 ((eq eltype2
*wild-type
*) eltype1
)
2855 (t (type-intersection eltype1 eltype2
)))
2856 :specialized-element-type
(cond
2857 ((eq stype1
*wild-type
*) stype2
)
2858 ((eq stype2
*wild-type
*) stype1
)
2860 (aver (type= stype1 stype2
))
2864 ;;; Check a supplied dimension list to determine whether it is legal,
2865 ;;; and return it in canonical form (as either '* or a list).
2866 (defun canonical-array-dimensions (dims)
2871 (error "Arrays can't have a negative number of dimensions: ~S" dims
))
2872 (when (>= dims sb
!xc
:array-rank-limit
)
2873 (error "array type with too many dimensions: ~S" dims
))
2874 (make-list dims
:initial-element
'*))
2876 (when (>= (length dims
) sb
!xc
:array-rank-limit
)
2877 (error "array type with too many dimensions: ~S" dims
))
2880 (unless (and (integerp dim
)
2882 (< dim sb
!xc
:array-dimension-limit
))
2883 (error "bad dimension in array type: ~S" dim
))))
2886 (error "Array dimensions is not a list, integer or *:~% ~S" dims
))))
2890 (!define-type-class member
:enumerable t
2891 :might-contain-other-types nil
)
2893 (!define-type-method
(member :negate
) (type)
2894 (let ((xset (member-type-xset type
))
2895 (fp-zeroes (member-type-fp-zeroes type
)))
2897 ;; Hairy case, which needs to do a bit of float type
2898 ;; canonicalization.
2899 (apply #'type-intersection
2900 (if (xset-empty-p xset
)
2902 (make-negation-type (make-member-type xset nil
)))
2905 (let* ((opposite (neg-fp-zero x
))
2906 (type (ctype-of opposite
)))
2909 (modified-numeric-type type
:low nil
:high nil
))
2910 (modified-numeric-type type
:low nil
:high
(list opposite
))
2911 (make-eql-type opposite
)
2912 (modified-numeric-type type
:low
(list opposite
) :high nil
))))
2915 (make-negation-type type
))))
2917 (!define-type-method
(member :unparse
) (type)
2918 (let ((members (member-type-members type
)))
2919 (cond ((equal members
'(nil)) 'null
)
2920 (t `(member ,@members
)))))
2922 (!define-type-method
(member :singleton-p
) (type)
2923 (if (eql 1 (member-type-size type
))
2924 (values t
(first (member-type-members type
)))
2927 (!define-type-method
(member :simple-subtypep
) (type1 type2
)
2928 (values (and (xset-subset-p (member-type-xset type1
)
2929 (member-type-xset type2
))
2930 (subsetp (member-type-fp-zeroes type1
)
2931 (member-type-fp-zeroes type2
)))
2934 (!define-type-method
(member :complex-subtypep-arg1
) (type1 type2
)
2936 (mapc-member-type-members
2938 (multiple-value-bind (ok surep
) (ctypep elt type2
)
2940 (return-from punt
(values nil nil
)))
2942 (return-from punt
(values nil t
)))))
2946 ;;; We punt if the odd type is enumerable and intersects with the
2947 ;;; MEMBER type. If not enumerable, then it is definitely not a
2948 ;;; subtype of the MEMBER type.
2949 (!define-type-method
(member :complex-subtypep-arg2
) (type1 type2
)
2950 (cond ((not (type-enumerable type1
)) (values nil t
))
2951 ((types-equal-or-intersect type1 type2
)
2952 (invoke-complex-subtypep-arg1-method type1 type2
))
2953 (t (values nil t
))))
2955 (!define-type-method
(member :simple-intersection2
) (type1 type2
)
2956 (make-member-type (xset-intersection (member-type-xset type1
)
2957 (member-type-xset type2
))
2958 (intersection (member-type-fp-zeroes type1
)
2959 (member-type-fp-zeroes type2
))))
2961 (!define-type-method
(member :complex-intersection2
) (type1 type2
)
2963 (let ((xset (alloc-xset))
2965 (mapc-member-type-members
2967 (multiple-value-bind (ok sure
) (ctypep member type1
)
2969 (return-from punt nil
))
2971 (if (fp-zero-p member
)
2972 (pushnew member fp-zeroes
)
2973 (add-to-xset member xset
)))))
2975 (if (and (xset-empty-p xset
) (not fp-zeroes
))
2977 (make-member-type xset fp-zeroes
)))))
2979 ;;; We don't need a :COMPLEX-UNION2, since the only interesting case is
2980 ;;; a union type, and the member/union interaction is handled by the
2981 ;;; union type method.
2982 (!define-type-method
(member :simple-union2
) (type1 type2
)
2983 (make-member-type (xset-union (member-type-xset type1
)
2984 (member-type-xset type2
))
2985 (union (member-type-fp-zeroes type1
)
2986 (member-type-fp-zeroes type2
))))
2988 (!define-type-method
(member :simple-
=) (type1 type2
)
2989 (let ((xset1 (member-type-xset type1
))
2990 (xset2 (member-type-xset type2
))
2991 (l1 (member-type-fp-zeroes type1
))
2992 (l2 (member-type-fp-zeroes type2
)))
2993 (values (and (eql (xset-count xset1
) (xset-count xset2
))
2994 (xset-subset-p xset1 xset2
)
2995 (xset-subset-p xset2 xset1
)
3000 (!define-type-method
(member :complex-
=) (type1 type2
)
3001 (if (type-enumerable type1
)
3002 (multiple-value-bind (val win
) (csubtypep type2 type1
)
3003 (if (or val
(not win
))
3008 (!def-type-translator member
:list
(&rest members
)
3010 (let (ms numbers char-codes
)
3011 (dolist (m (remove-duplicates members
))
3013 (character (push (sb!xc
:char-code m
) char-codes
))
3014 (real (if (and (floatp m
) (zerop m
))
3016 (push (ctype-of m
) numbers
)))
3019 (member-type-from-list ms
)
3020 (make-character-set-type (mapcar (lambda (x) (cons x x
))
3021 (sort char-codes
#'<)))
3022 (nreverse numbers
)))
3025 ;;;; intersection types
3027 ;;;; Until version 0.6.10.6, SBCL followed the original CMU CL approach
3028 ;;;; of punting on all AND types, not just the unreasonably complicated
3029 ;;;; ones. The change was motivated by trying to get the KEYWORD type
3030 ;;;; to behave sensibly:
3031 ;;;; ;; reasonable definition
3032 ;;;; (DEFTYPE KEYWORD () '(AND SYMBOL (SATISFIES KEYWORDP)))
3033 ;;;; ;; reasonable behavior
3034 ;;;; (AVER (SUBTYPEP 'KEYWORD 'SYMBOL))
3035 ;;;; Without understanding a little about the semantics of AND, we'd
3036 ;;;; get (SUBTYPEP 'KEYWORD 'SYMBOL)=>NIL,NIL and, for entirely
3037 ;;;; parallel reasons, (SUBTYPEP 'RATIO 'NUMBER)=>NIL,NIL. That's
3040 ;;;; We still follow the example of CMU CL to some extent, by punting
3041 ;;;; (to the opaque HAIRY-TYPE) on sufficiently complicated types
3044 (!define-type-class intersection
3045 :enumerable
#'compound-type-enumerable
3046 :might-contain-other-types t
)
3048 (!define-type-method
(intersection :negate
) (type)
3050 (mapcar #'type-negation
(intersection-type-types type
))))
3052 ;;; A few intersection types have special names. The others just get
3053 ;;; mechanically unparsed.
3054 (!define-type-method
(intersection :unparse
) (type)
3055 (declare (type ctype type
))
3056 (or (find type
'(ratio keyword compiled-function
) :key
#'specifier-type
:test
#'type
=)
3057 `(and ,@(mapcar #'type-specifier
(intersection-type-types type
)))))
3059 ;;; shared machinery for type equality: true if every type in the set
3060 ;;; TYPES1 matches a type in the set TYPES2 and vice versa
3061 (defun type=-set
(types1 types2
)
3062 (flet ((type<=-set
(x y
)
3063 (declare (type list x y
))
3064 (every/type
(lambda (x y-element
)
3065 (any/type
#'type
= y-element x
))
3067 (and/type
(type<=-set types1 types2
)
3068 (type<=-set types2 types1
))))
3070 ;;; Two intersection types are equal if their subtypes are equal sets.
3072 ;;; FIXME: Might it be better to use
3073 ;;; (AND (SUBTYPEP X Y) (SUBTYPEP Y X))
3074 ;;; instead, since SUBTYPEP is the usual relationship that we care
3075 ;;; most about, so it would be good to leverage any ingenuity there
3076 ;;; in this more obscure method?
3077 (!define-type-method
(intersection :simple-
=) (type1 type2
)
3078 (type=-set
(intersection-type-types type1
)
3079 (intersection-type-types type2
)))
3081 (defun %intersection-complex-subtypep-arg1
(type1 type2
)
3082 (type= type1
(type-intersection type1 type2
)))
3084 (defun %intersection-simple-subtypep
(type1 type2
)
3085 (every/type
#'%intersection-complex-subtypep-arg1
3087 (intersection-type-types type2
)))
3089 (!define-type-method
(intersection :simple-subtypep
) (type1 type2
)
3090 (%intersection-simple-subtypep type1 type2
))
3092 (!define-type-method
(intersection :complex-subtypep-arg1
) (type1 type2
)
3093 (%intersection-complex-subtypep-arg1 type1 type2
))
3095 (defun %intersection-complex-subtypep-arg2
(type1 type2
)
3096 (every/type
#'csubtypep type1
(intersection-type-types type2
)))
3098 (!define-type-method
(intersection :complex-subtypep-arg2
) (type1 type2
)
3099 (%intersection-complex-subtypep-arg2 type1 type2
))
3101 ;;; FIXME: This will look eeriely familiar to readers of the UNION
3102 ;;; :SIMPLE-INTERSECTION2 :COMPLEX-INTERSECTION2 method. That's
3103 ;;; because it was generated by cut'n'paste methods. Given that
3104 ;;; intersections and unions have all sorts of symmetries known to
3105 ;;; mathematics, it shouldn't be beyond the ken of some programmers to
3106 ;;; reflect those symmetries in code in a way that ties them together
3107 ;;; more strongly than having two independent near-copies :-/
3108 (!define-type-method
(intersection :simple-union2
:complex-union2
)
3110 ;; Within this method, type2 is guaranteed to be an intersection
3112 (aver (intersection-type-p type2
))
3113 ;; Make sure to call only the applicable methods...
3114 (cond ((and (intersection-type-p type1
)
3115 (%intersection-simple-subtypep type1 type2
)) type2
)
3116 ((and (intersection-type-p type1
)
3117 (%intersection-simple-subtypep type2 type1
)) type1
)
3118 ((and (not (intersection-type-p type1
))
3119 (%intersection-complex-subtypep-arg2 type1 type2
))
3121 ((and (not (intersection-type-p type1
))
3122 (%intersection-complex-subtypep-arg1 type2 type1
))
3124 ;; KLUDGE: This special (and somewhat hairy) magic is required
3125 ;; to deal with the RATIONAL/INTEGER special case. The UNION
3126 ;; of (INTEGER * -1) and (AND (RATIONAL * -1/2) (NOT INTEGER))
3127 ;; should be (RATIONAL * -1/2) -- CSR, 2003-02-28
3128 ((and (csubtypep type2
(specifier-type 'ratio
))
3129 (numeric-type-p type1
)
3130 (csubtypep type1
(specifier-type 'integer
))
3135 :low
(if (null (numeric-type-low type1
))
3137 (list (1- (numeric-type-low type1
))))
3138 :high
(if (null (numeric-type-high type1
))
3140 (list (1+ (numeric-type-high type1
)))))))
3141 (let* ((intersected (intersection-type-types type2
))
3142 (remaining (remove (specifier-type '(not integer
))
3145 (and (not (equal intersected remaining
))
3146 (type-union type1
(apply #'type-intersection remaining
)))))
3148 (let ((accumulator *universal-type
*))
3149 (do ((t2s (intersection-type-types type2
) (cdr t2s
)))
3150 ((null t2s
) accumulator
)
3151 (let ((union (type-union type1
(car t2s
))))
3152 (when (union-type-p union
)
3153 ;; we have to give up here -- there are all sorts of
3154 ;; ordering worries, but it's better than before.
3155 ;; Doing exactly the same as in the UNION
3156 ;; :SIMPLE/:COMPLEX-INTERSECTION2 method causes stack
3157 ;; overflow with the mutual recursion never bottoming
3159 (if (and (eq accumulator
*universal-type
*)
3161 ;; KLUDGE: if we get here, we have a partially
3162 ;; simplified result. While this isn't by any
3163 ;; means a universal simplification, including
3164 ;; this logic here means that we can get (OR
3165 ;; KEYWORD (NOT KEYWORD)) canonicalized to T.
3169 (type-intersection accumulator union
))))))))
3171 (!def-type-translator and
:list
((:context context
) &rest type-specifiers
)
3172 (apply #'type-intersection
3173 (mapcar (lambda (x) (specifier-type-r context x
))
3178 (!define-type-class union
3179 :enumerable
#'compound-type-enumerable
3180 :might-contain-other-types t
)
3182 (!define-type-method
(union :negate
) (type)
3183 (declare (type ctype type
))
3184 (apply #'type-intersection
3185 (mapcar #'type-negation
(union-type-types type
))))
3187 ;;; The LIST, FLOAT and REAL types have special names. Other union
3188 ;;; types just get mechanically unparsed.
3189 (!define-type-method
(union :unparse
) (type)
3190 (declare (type ctype type
))
3192 ((type= type
(specifier-type 'list
)) 'list
)
3193 ((type= type
(specifier-type 'float
)) 'float
)
3194 ((type= type
(specifier-type 'real
)) 'real
)
3195 ((type= type
(specifier-type 'sequence
)) 'sequence
)
3196 ((type= type
(specifier-type 'bignum
)) 'bignum
)
3197 ((type= type
(specifier-type 'simple-string
)) 'simple-string
)
3198 ((type= type
(specifier-type 'string
)) 'string
)
3199 ((type= type
(specifier-type 'complex
)) 'complex
)
3200 (t `(or ,@(mapcar #'type-specifier
(union-type-types type
))))))
3202 ;;; Two union types are equal if they are each subtypes of each
3203 ;;; other. We need to be this clever because our complex subtypep
3204 ;;; methods are now more accurate; we don't get infinite recursion
3205 ;;; because the simple-subtypep method delegates to complex-subtypep
3206 ;;; of the individual types of type1. - CSR, 2002-04-09
3208 ;;; Previous comment, now obsolete, but worth keeping around because
3209 ;;; it is true, though too strong a condition:
3211 ;;; Two union types are equal if their subtypes are equal sets.
3212 (!define-type-method
(union :simple-
=) (type1 type2
)
3213 (multiple-value-bind (subtype certain?
)
3214 (csubtypep type1 type2
)
3216 (csubtypep type2 type1
)
3217 ;; we might as well become as certain as possible.
3220 (multiple-value-bind (subtype certain?
)
3221 (csubtypep type2 type1
)
3222 (declare (ignore subtype
))
3223 (values nil certain?
))))))
3225 (!define-type-method
(union :complex-
=) (type1 type2
)
3226 (declare (ignore type1
))
3227 (if (some #'type-might-contain-other-types-p
3228 (union-type-types type2
))
3232 ;;; Similarly, a union type is a subtype of another if and only if
3233 ;;; every element of TYPE1 is a subtype of TYPE2.
3234 (defun union-simple-subtypep (type1 type2
)
3235 (every/type
(swapped-args-fun #'union-complex-subtypep-arg2
)
3237 (union-type-types type1
)))
3239 (!define-type-method
(union :simple-subtypep
) (type1 type2
)
3240 (union-simple-subtypep type1 type2
))
3242 (defun union-complex-subtypep-arg1 (type1 type2
)
3243 (every/type
(swapped-args-fun #'csubtypep
)
3245 (union-type-types type1
)))
3247 (!define-type-method
(union :complex-subtypep-arg1
) (type1 type2
)
3248 (union-complex-subtypep-arg1 type1 type2
))
3250 (defun union-complex-subtypep-arg2 (type1 type2
)
3251 ;; At this stage, we know that type2 is a union type and type1
3252 ;; isn't. We might as well check this, though:
3253 (aver (union-type-p type2
))
3254 (aver (not (union-type-p type1
)))
3255 ;; was: (any/type #'csubtypep type1 (union-type-types type2)), which
3256 ;; turns out to be too restrictive, causing bug 91.
3258 ;; the following reimplementation might look dodgy. It is dodgy. It
3259 ;; depends on the union :complex-= method not doing very much work
3260 ;; -- certainly, not using subtypep. Reasoning:
3262 ;; A is a subset of (B1 u B2)
3263 ;; <=> A n (B1 u B2) = A
3264 ;; <=> (A n B1) u (A n B2) = A
3266 ;; But, we have to be careful not to delegate this type= to
3267 ;; something that could invoke subtypep, which might get us back
3268 ;; here -> stack explosion. We therefore ensure that the second type
3269 ;; (which is the one that's dispatched on) is either a union type
3270 ;; (where we've ensured that the complex-= method will not call
3271 ;; subtypep) or something with no union types involved, in which
3272 ;; case we'll never come back here.
3274 ;; If we don't do this, then e.g.
3275 ;; (SUBTYPEP '(MEMBER 3) '(OR (SATISFIES FOO) (SATISFIES BAR)))
3276 ;; would loop infinitely, as the member :complex-= method is
3277 ;; implemented in terms of subtypep.
3279 ;; Ouch. - CSR, 2002-04-10
3280 (multiple-value-bind (sub-value sub-certain?
)
3283 (mapcar (lambda (x) (type-intersection type1 x
))
3284 (union-type-types type2
))))
3286 (values sub-value sub-certain?
)
3287 ;; The ANY/TYPE expression above is a sufficient condition for
3288 ;; subsetness, but not a necessary one, so we might get a more
3289 ;; certain answer by this CALL-NEXT-METHOD-ish step when the
3290 ;; ANY/TYPE expression is uncertain.
3291 (invoke-complex-subtypep-arg1-method type1 type2
))))
3293 (!define-type-method
(union :complex-subtypep-arg2
) (type1 type2
)
3294 (union-complex-subtypep-arg2 type1 type2
))
3296 (!define-type-method
(union :simple-intersection2
:complex-intersection2
)
3298 ;; The CSUBTYPEP clauses here let us simplify e.g.
3299 ;; (TYPE-INTERSECTION2 (SPECIFIER-TYPE 'LIST)
3300 ;; (SPECIFIER-TYPE '(OR LIST VECTOR)))
3301 ;; (where LIST is (OR CONS NULL)).
3303 ;; The tests are more or less (CSUBTYPEP TYPE1 TYPE2) and vice
3304 ;; versa, but it's important that we pre-expand them into
3305 ;; specialized operations on individual elements of
3306 ;; UNION-TYPE-TYPES, instead of using the ordinary call to
3307 ;; CSUBTYPEP, in order to avoid possibly invoking any methods which
3308 ;; might in turn invoke (TYPE-INTERSECTION2 TYPE1 TYPE2) and thus
3309 ;; cause infinite recursion.
3311 ;; Within this method, type2 is guaranteed to be a union type:
3312 (aver (union-type-p type2
))
3313 ;; Make sure to call only the applicable methods...
3314 (cond ((and (union-type-p type1
)
3315 (union-simple-subtypep type1 type2
)) type1
)
3316 ((and (union-type-p type1
)
3317 (union-simple-subtypep type2 type1
)) type2
)
3318 ((and (not (union-type-p type1
))
3319 (union-complex-subtypep-arg2 type1 type2
))
3321 ((and (not (union-type-p type1
))
3322 (union-complex-subtypep-arg1 type2 type1
))
3325 ;; KLUDGE: This code accumulates a sequence of TYPE-UNION2
3326 ;; operations in a particular order, and gives up if any of
3327 ;; the sub-unions turn out not to be simple. In other cases
3328 ;; ca. sbcl-0.6.11.15, that approach to taking a union was a
3329 ;; bad idea, since it can overlook simplifications which
3330 ;; might occur if the terms were accumulated in a different
3331 ;; order. It's possible that that will be a problem here too.
3332 ;; However, I can't think of a good example to demonstrate
3333 ;; it, and without an example to demonstrate it I can't write
3334 ;; test cases, and without test cases I don't want to
3335 ;; complicate the code to address what's still a hypothetical
3336 ;; problem. So I punted. -- WHN 2001-03-20
3337 (let ((accumulator *empty-type
*))
3338 (dolist (t2 (union-type-types type2
) accumulator
)
3340 (type-union accumulator
3341 (type-intersection type1 t2
))))))))
3343 (!def-type-translator or
:list
((:context context
) &rest type-specifiers
)
3344 (let ((type (apply #'type-union
3345 (mapcar (lambda (x) (specifier-type-r context x
))
3347 (if (union-type-p type
)
3348 (sb!kernel
::simplify-array-unions type
)
3353 (!define-type-class cons
:enumerable nil
:might-contain-other-types nil
)
3355 (!def-type-translator cons
((:context context
)
3356 &optional
(car-type-spec '*) (cdr-type-spec '*))
3357 (let ((car-type (single-value-specifier-type-r context car-type-spec
))
3358 (cdr-type (single-value-specifier-type-r context cdr-type-spec
)))
3359 (make-cons-type car-type cdr-type
)))
3361 (!define-type-method
(cons :negate
) (type)
3362 (if (and (eq (cons-type-car-type type
) *universal-type
*)
3363 (eq (cons-type-cdr-type type
) *universal-type
*))
3364 (make-negation-type type
)
3366 (make-negation-type (specifier-type 'cons
))
3368 ((and (not (eq (cons-type-car-type type
) *universal-type
*))
3369 (not (eq (cons-type-cdr-type type
) *universal-type
*)))
3372 (type-negation (cons-type-car-type type
))
3376 (type-negation (cons-type-cdr-type type
)))))
3377 ((not (eq (cons-type-car-type type
) *universal-type
*))
3379 (type-negation (cons-type-car-type type
))
3381 ((not (eq (cons-type-cdr-type type
) *universal-type
*))
3384 (type-negation (cons-type-cdr-type type
))))
3385 (t (bug "Weird CONS type ~S" type
))))))
3387 (!define-type-method
(cons :unparse
) (type)
3388 (let ((car-eltype (type-specifier (cons-type-car-type type
)))
3389 (cdr-eltype (type-specifier (cons-type-cdr-type type
))))
3390 (if (and (member car-eltype
'(t *))
3391 (member cdr-eltype
'(t *)))
3393 `(cons ,car-eltype
,cdr-eltype
))))
3395 (!define-type-method
(cons :simple-
=) (type1 type2
)
3396 (declare (type cons-type type1 type2
))
3397 (multiple-value-bind (car-match car-win
)
3398 (type= (cons-type-car-type type1
) (cons-type-car-type type2
))
3399 (multiple-value-bind (cdr-match cdr-win
)
3400 (type= (cons-type-cdr-type type1
) (cons-type-cdr-type type2
))
3401 (cond ((and car-match cdr-match
)
3402 (aver (and car-win cdr-win
))
3406 ;; FIXME: Ideally we would like to detect and handle
3407 ;; (CONS UNKNOWN INTEGER) (CONS UNKNOWN SYMBOL) => NIL, T
3408 ;; but just returning a secondary true on (and car-win cdr-win)
3409 ;; unfortunately breaks other things. --NS 2006-08-16
3410 (and (or (and (not car-match
) car-win
)
3411 (and (not cdr-match
) cdr-win
))
3412 (not (and (cons-type-might-be-empty-type type1
)
3413 (cons-type-might-be-empty-type type2
))))))))))
3415 (!define-type-method
(cons :simple-subtypep
) (type1 type2
)
3416 (declare (type cons-type type1 type2
))
3417 (multiple-value-bind (val-car win-car
)
3418 (csubtypep (cons-type-car-type type1
) (cons-type-car-type type2
))
3419 (multiple-value-bind (val-cdr win-cdr
)
3420 (csubtypep (cons-type-cdr-type type1
) (cons-type-cdr-type type2
))
3421 (if (and val-car val-cdr
)
3422 (values t
(and win-car win-cdr
))
3423 (values nil
(or (and (not val-car
) win-car
)
3424 (and (not val-cdr
) win-cdr
)))))))
3426 ;;; Give up if a precise type is not possible, to avoid returning
3427 ;;; overly general types.
3428 (!define-type-method
(cons :simple-union2
) (type1 type2
)
3429 (declare (type cons-type type1 type2
))
3430 (let ((car-type1 (cons-type-car-type type1
))
3431 (car-type2 (cons-type-car-type type2
))
3432 (cdr-type1 (cons-type-cdr-type type1
))
3433 (cdr-type2 (cons-type-cdr-type type2
))
3436 ;; UGH. -- CSR, 2003-02-24
3437 (macrolet ((frob-car (car1 car2 cdr1 cdr2
3438 &optional
(not1 nil not1p
))
3440 (make-cons-type ,car1
(type-union ,cdr1
,cdr2
))
3442 (type-intersection ,car2
3445 `(type-negation ,car1
)))
3447 (cond ((type= car-type1 car-type2
)
3448 (make-cons-type car-type1
3449 (type-union cdr-type1 cdr-type2
)))
3450 ((type= cdr-type1 cdr-type2
)
3451 (make-cons-type (type-union car-type1 car-type2
)
3453 ((csubtypep car-type1 car-type2
)
3454 (frob-car car-type1 car-type2 cdr-type1 cdr-type2
))
3455 ((csubtypep car-type2 car-type1
)
3456 (frob-car car-type2 car-type1 cdr-type2 cdr-type1
))
3457 ;; more general case of the above, but harder to compute
3459 (setf car-not1
(type-negation car-type1
))
3460 (multiple-value-bind (yes win
)
3461 (csubtypep car-type2 car-not1
)
3462 (and (not yes
) win
)))
3463 (frob-car car-type1 car-type2 cdr-type1 cdr-type2 car-not1
))
3465 (setf car-not2
(type-negation car-type2
))
3466 (multiple-value-bind (yes win
)
3467 (csubtypep car-type1 car-not2
)
3468 (and (not yes
) win
)))
3469 (frob-car car-type2 car-type1 cdr-type2 cdr-type1 car-not2
))
3470 ;; Don't put these in -- consider the effect of taking the
3471 ;; union of (CONS (INTEGER 0 2) (INTEGER 5 7)) and
3472 ;; (CONS (INTEGER 0 3) (INTEGER 5 6)).
3474 ((csubtypep cdr-type1 cdr-type2
)
3475 (frob-cdr car-type1 car-type2 cdr-type1 cdr-type2
))
3477 ((csubtypep cdr-type2 cdr-type1
)
3478 (frob-cdr car-type2 car-type1 cdr-type2 cdr-type1
))))))
3480 (!define-type-method
(cons :simple-intersection2
) (type1 type2
)
3481 (declare (type cons-type type1 type2
))
3482 (let ((car-int2 (type-intersection2 (cons-type-car-type type1
)
3483 (cons-type-car-type type2
)))
3484 (cdr-int2 (type-intersection2 (cons-type-cdr-type type1
)
3485 (cons-type-cdr-type type2
))))
3487 ((and car-int2 cdr-int2
) (make-cons-type car-int2 cdr-int2
))
3488 (car-int2 (make-cons-type car-int2
3490 (cons-type-cdr-type type1
)
3491 (cons-type-cdr-type type2
))))
3492 (cdr-int2 (make-cons-type
3493 (type-intersection (cons-type-car-type type1
)
3494 (cons-type-car-type type2
))
3497 (!define-superclasses cons
((cons)) !cold-init-forms
)
3499 ;;;; CHARACTER-SET types
3501 ;; all character-set types are enumerable, but it's not possible
3502 ;; for one to be TYPE= to a MEMBER type because (MEMBER #\x)
3503 ;; is not internally represented as a MEMBER type.
3504 ;; So in case it wasn't clear already ENUMERABLE-P does not mean
3505 ;; "possibly a MEMBER type in the Lisp-theoretic sense",
3506 ;; but means "could be implemented in SBCL as a MEMBER type".
3507 (!define-type-class character-set
:enumerable nil
3508 :might-contain-other-types nil
)
3510 (!def-type-translator character-set
3511 (&optional
(pairs '((0 .
#.
(1- sb
!xc
:char-code-limit
)))))
3512 (make-character-set-type pairs
))
3514 (!define-type-method
(character-set :negate
) (type)
3515 (let ((pairs (character-set-type-pairs type
)))
3516 (if (and (= (length pairs
) 1)
3518 (= (cdar pairs
) (1- sb
!xc
:char-code-limit
)))
3519 (make-negation-type type
)
3520 (let ((not-character
3522 (make-character-set-type
3523 '((0 .
#.
(1- sb
!xc
:char-code-limit
)))))))
3526 (make-character-set-type
3528 (when (> (caar pairs
) 0)
3529 (push (cons 0 (1- (caar pairs
))) not-pairs
))
3530 (do* ((tail pairs
(cdr tail
))
3531 (high1 (cdar tail
) (cdar tail
))
3532 (low2 (caadr tail
) (caadr tail
)))
3534 (when (< (cdar tail
) (1- sb
!xc
:char-code-limit
))
3535 (push (cons (1+ (cdar tail
))
3536 (1- sb
!xc
:char-code-limit
))
3538 (nreverse not-pairs
))
3539 (push (cons (1+ high1
) (1- low2
)) not-pairs
)))))))))
3541 (!define-type-method
(character-set :unparse
) (type)
3543 ((type= type
(specifier-type 'character
)) 'character
)
3544 ((type= type
(specifier-type 'base-char
)) 'base-char
)
3545 ((type= type
(specifier-type 'extended-char
)) 'extended-char
)
3546 ((type= type
(specifier-type 'standard-char
)) 'standard-char
)
3548 ;; Unparse into either MEMBER or CHARACTER-SET. We use MEMBER if there
3549 ;; are at most as many characters as there are character code ranges.
3550 ;; (basically saying to use MEMBER if each range is one character)
3551 (let* ((pairs (character-set-type-pairs type
))
3552 (count (length pairs
))
3553 (chars (loop named outer
3554 for
(low . high
) in pairs
3555 nconc
(loop for code from low upto high
3556 collect
(sb!xc
:code-char code
)
3557 when
(minusp (decf count
))
3558 do
(return-from outer t
)))))
3560 `(character-set ,pairs
)
3561 `(member ,@chars
))))))
3563 (!define-type-method
(character-set :singleton-p
) (type)
3564 (let* ((pairs (character-set-type-pairs type
))
3565 (pair (first pairs
)))
3566 (if (and (typep pairs
'(cons t null
))
3567 (eql (car pair
) (cdr pair
)))
3568 (values t
(code-char (car pair
)))
3571 (!define-type-method
(character-set :simple-
=) (type1 type2
)
3572 (let ((pairs1 (character-set-type-pairs type1
))
3573 (pairs2 (character-set-type-pairs type2
)))
3574 (values (equal pairs1 pairs2
) t
)))
3576 (!define-type-method
(character-set :simple-subtypep
) (type1 type2
)
3578 (dolist (pair (character-set-type-pairs type1
) t
)
3579 (unless (position pair
(character-set-type-pairs type2
)
3580 :test
(lambda (x y
) (and (>= (car x
) (car y
))
3581 (<= (cdr x
) (cdr y
)))))
3585 (!define-type-method
(character-set :simple-union2
) (type1 type2
)
3586 ;; KLUDGE: the canonizing in the MAKE-CHARACTER-SET-TYPE function
3587 ;; actually does the union for us. It might be a little fragile to
3589 (make-character-set-type
3591 (copy-alist (character-set-type-pairs type1
))
3592 (copy-alist (character-set-type-pairs type2
))
3595 (!define-type-method
(character-set :simple-intersection2
) (type1 type2
)
3596 ;; KLUDGE: brute force.
3599 (dolist (pair1 (character-set-type-pairs type1
)
3600 (make-character-set-type
3601 (sort pairs
#'< :key
#'car
)))
3602 (dolist (pair2 (character-set-type-pairs type2
))
3604 ((<= (car pair1
) (car pair2
) (cdr pair1
))
3605 (push (cons (car pair2
) (min (cdr pair1
) (cdr pair2
))) pairs
))
3606 ((<= (car pair2
) (car pair1
) (cdr pair2
))
3607 (push (cons (car pair1
) (min (cdr pair1
) (cdr pair2
))) pairs
))))))
3609 (make-character-set-type
3610 (intersect-type-pairs
3611 (character-set-type-pairs type1
)
3612 (character-set-type-pairs type2
))))
3615 ;;; Intersect two ordered lists of pairs
3616 ;;; Each list is of the form ((start1 . end1) ... (startn . endn)),
3617 ;;; where start1 <= end1 < start2 <= end2 < ... < startn <= endn.
3618 ;;; Each pair represents the integer interval start..end.
3620 (defun intersect-type-pairs (alist1 alist2
)
3621 (if (and alist1 alist2
)
3623 (pair1 (pop alist1
))
3624 (pair2 (pop alist2
)))
3626 (when (> (car pair1
) (car pair2
))
3627 (rotatef pair1 pair2
)
3628 (rotatef alist1 alist2
))
3629 (let ((pair1-cdr (cdr pair1
)))
3631 ((> (car pair2
) pair1-cdr
)
3632 ;; No over lap -- discard pair1
3633 (unless alist1
(return))
3634 (setq pair1
(pop alist1
)))
3635 ((<= (cdr pair2
) pair1-cdr
)
3636 (push (cons (car pair2
) (cdr pair2
)) res
)
3638 ((= (cdr pair2
) pair1-cdr
)
3639 (unless alist1
(return))
3640 (unless alist2
(return))
3641 (setq pair1
(pop alist1
)
3642 pair2
(pop alist2
)))
3643 (t ;; (< (cdr pair2) pair1-cdr)
3644 (unless alist2
(return))
3645 (setq pair1
(cons (1+ (cdr pair2
)) pair1-cdr
))
3646 (setq pair2
(pop alist2
)))))
3647 (t ;; (> (cdr pair2) (cdr pair1))
3648 (push (cons (car pair2
) pair1-cdr
) res
)
3649 (unless alist1
(return))
3650 (setq pair2
(cons (1+ pair1-cdr
) (cdr pair2
)))
3651 (setq pair1
(pop alist1
))))))
3656 ;;; Return the type that describes all objects that are in X but not
3657 ;;; in Y. If we can't determine this type, then return NIL.
3659 ;;; For now, we only are clever dealing with union and member types.
3660 ;;; If either type is not a union type, then we pretend that it is a
3661 ;;; union of just one type. What we do is remove from X all the types
3662 ;;; that are a subtype any type in Y. If any type in X intersects with
3663 ;;; a type in Y but is not a subtype, then we give up.
3665 ;;; We must also special-case any member type that appears in the
3666 ;;; union. We remove from X's members all objects that are TYPEP to Y.
3667 ;;; If Y has any members, we must be careful that none of those
3668 ;;; members are CTYPEP to any of Y's non-member types. We give up in
3669 ;;; this case, since to compute that difference we would have to break
3670 ;;; the type from X into some collection of types that represents the
3671 ;;; type without that particular element. This seems too hairy to be
3672 ;;; worthwhile, given its low utility.
3673 (defun type-difference (x y
)
3674 (if (and (numeric-type-p x
) (numeric-type-p y
))
3675 ;; Numeric types are easy. Are there any others we should handle like this?
3676 (type-intersection x
(type-negation y
))
3677 (let ((x-types (if (union-type-p x
) (union-type-types x
) (list x
)))
3678 (y-types (if (union-type-p y
) (union-type-types y
) (list y
))))
3680 (dolist (x-type x-types
)
3681 (if (member-type-p x-type
)
3682 (let ((xset (alloc-xset))
3684 (mapc-member-type-members
3686 (multiple-value-bind (ok sure
) (ctypep elt y
)
3688 (return-from type-difference nil
))
3691 (pushnew elt fp-zeroes
)
3692 (add-to-xset elt xset
)))))
3694 (unless (and (xset-empty-p xset
) (not fp-zeroes
))
3695 (res (make-member-type xset fp-zeroes
))))
3696 (dolist (y-type y-types
(res x-type
))
3697 (multiple-value-bind (val win
) (csubtypep x-type y-type
)
3698 (unless win
(return-from type-difference nil
))
3700 (when (types-equal-or-intersect x-type y-type
)
3701 (return-from type-difference nil
))))))
3702 (let ((y-mem (find-if #'member-type-p y-types
)))
3704 (dolist (x-type x-types
)
3705 (unless (member-type-p x-type
)
3706 (mapc-member-type-members
3708 (multiple-value-bind (ok sure
) (ctypep member x-type
)
3709 (when (or (not sure
) ok
)
3710 (return-from type-difference nil
))))
3712 (apply #'type-union
(res))))))
3714 (!def-type-translator array
((:context context
)
3715 &optional
(element-type '*)
3717 (let ((eltype (if (eq element-type
'*)
3719 (specifier-type-r context element-type
))))
3720 (make-array-type (canonical-array-dimensions dimensions
)
3722 :element-type eltype
3723 :specialized-element-type
(%upgraded-array-element-type
3726 (!def-type-translator simple-array
((:context context
)
3727 &optional
(element-type '*)
3729 (let ((eltype (if (eq element-type
'*)
3731 (specifier-type-r context element-type
))))
3732 (make-array-type (canonical-array-dimensions dimensions
)
3734 :element-type eltype
3735 :specialized-element-type
(%upgraded-array-element-type
3738 ;;;; SIMD-PACK types
3741 (!define-type-class simd-pack
:enumerable nil
3742 :might-contain-other-types nil
)
3744 ;; Though this involves a recursive call to parser, parsing context need not
3745 ;; be passed down, because an unknown-type condition is an immediate failure.
3746 (!def-type-translator simd-pack
(&optional
(element-type-spec '*))
3747 (if (eql element-type-spec
'*)
3748 (%make-simd-pack-type
*simd-pack-element-types
*)
3749 (make-simd-pack-type (single-value-specifier-type element-type-spec
))))
3751 (!define-type-method
(simd-pack :negate
) (type)
3752 (let ((remaining (set-difference *simd-pack-element-types
*
3753 (simd-pack-type-element-type type
)))
3754 (not-simd-pack (make-negation-type (specifier-type 'simd-pack
))))
3756 (type-union not-simd-pack
(%make-simd-pack-type remaining
))
3759 (!define-type-method
(simd-pack :unparse
) (type)
3760 (let ((eltypes (simd-pack-type-element-type type
)))
3761 (cond ((equal eltypes
*simd-pack-element-types
*)
3763 ((= 1 (length eltypes
))
3764 `(simd-pack ,(first eltypes
)))
3766 `(or ,@(mapcar (lambda (eltype)
3767 `(simd-pack ,eltype
))
3770 (!define-type-method
(simd-pack :simple-
=) (type1 type2
)
3771 (declare (type simd-pack-type type1 type2
))
3772 (null (set-exclusive-or (simd-pack-type-element-type type1
)
3773 (simd-pack-type-element-type type2
))))
3775 (!define-type-method
(simd-pack :simple-subtypep
) (type1 type2
)
3776 (declare (type simd-pack-type type1 type2
))
3777 (subsetp (simd-pack-type-element-type type1
)
3778 (simd-pack-type-element-type type2
)))
3780 (!define-type-method
(simd-pack :simple-union2
) (type1 type2
)
3781 (declare (type simd-pack-type type1 type2
))
3782 (%make-simd-pack-type
(union (simd-pack-type-element-type type1
)
3783 (simd-pack-type-element-type type2
))))
3785 (!define-type-method
(simd-pack :simple-intersection2
) (type1 type2
)
3786 (declare (type simd-pack-type type1 type2
))
3787 (let ((intersection (intersection (simd-pack-type-element-type type1
)
3788 (simd-pack-type-element-type type2
))))
3790 (%make-simd-pack-type intersection
)
3793 (!define-superclasses simd-pack
((simd-pack)) !cold-init-forms
))
3795 ;;;; utilities shared between cross-compiler and target system
3797 ;;; Does the type derived from compilation of an actual function
3798 ;;; definition satisfy declarations of a function's type?
3799 (defun defined-ftype-matches-declared-ftype-p (defined-ftype declared-ftype
)
3800 (declare (type ctype defined-ftype declared-ftype
))
3801 (flet ((is-built-in-class-function-p (ctype)
3802 (and (built-in-classoid-p ctype
)
3803 (eq (built-in-classoid-name ctype
) 'function
))))
3804 (cond (;; DECLARED-FTYPE could certainly be #<BUILT-IN-CLASS FUNCTION>;
3805 ;; that's what happens when we (DECLAIM (FTYPE FUNCTION FOO)).
3806 (is-built-in-class-function-p declared-ftype
)
3807 ;; In that case, any definition satisfies the declaration.
3809 (;; It's not clear whether or how DEFINED-FTYPE might be
3810 ;; #<BUILT-IN-CLASS FUNCTION>, but it's not obviously
3811 ;; invalid, so let's handle that case too, just in case.
3812 (is-built-in-class-function-p defined-ftype
)
3813 ;; No matter what DECLARED-FTYPE might be, we can't prove
3814 ;; that an object of type FUNCTION doesn't satisfy it, so
3815 ;; we return success no matter what.
3817 (;; Otherwise both of them must be FUN-TYPE objects.
3819 ;; FIXME: For now we only check compatibility of the return
3820 ;; type, not argument types, and we don't even check the
3821 ;; return type very precisely (as per bug 94a). It would be
3822 ;; good to do a better job. Perhaps to check the
3823 ;; compatibility of the arguments, we should (1) redo
3824 ;; VALUES-TYPES-EQUAL-OR-INTERSECT as
3825 ;; ARGS-TYPES-EQUAL-OR-INTERSECT, and then (2) apply it to
3826 ;; the ARGS-TYPE slices of the FUN-TYPEs. (ARGS-TYPE
3827 ;; is a base class both of VALUES-TYPE and of FUN-TYPE.)
3828 (values-types-equal-or-intersect
3829 (fun-type-returns defined-ftype
)
3830 (fun-type-returns declared-ftype
))))))
3832 ;;; This messy case of CTYPE for NUMBER is shared between the
3833 ;;; cross-compiler and the target system.
3834 (defun ctype-of-number (x)
3835 (let ((num (if (complexp x
) (realpart x
) x
)))
3836 (multiple-value-bind (complexp low high
)
3838 (let ((imag (imagpart x
)))
3839 (values :complex
(min num imag
) (max num imag
)))
3840 (values :real num num
))
3841 (make-numeric-type :class
(etypecase num
3842 (integer (if (complexp x
)
3843 (if (integerp (imagpart x
))
3847 (rational 'rational
)
3849 :format
(and (floatp num
) (float-format-name num
))
3854 ;;; The following function is a generic driver for approximating
3855 ;;; set-valued functions over types. Putting this here because it'll
3856 ;;; probably be useful for a lot of type analyses.
3858 ;;; Let f be a function from values of type X to Y, e.g., ARRAY-RANK.
3860 ;;; We compute an over or under-approximation of the set
3862 ;;; F(TYPE) = { f(x) : x in TYPE /\ x in X } \subseteq Y
3864 ;;; via set-valued approximations of f, OVER and UNDER.
3866 ;;; These functions must have the property that
3867 ;;; Forall TYPE, OVER(TYPE) \superseteq F(TYPE) and
3868 ;;; Forall TYPE, UNDER(TYPE) \subseteq F(TYPE)
3870 ;;; The driver is also parameterised over the finite set
3873 ;;; Union, intersection and difference are binary functions to compute
3874 ;;; set union, intersection and difference. Top and bottom are the
3875 ;;; concrete representations for the universe and empty sets; we never
3876 ;;; call the set functions on top or bottom, so it's safe to use
3877 ;;; special values there.
3881 ;;; TYPE: the ctype for which we wish to approximate F(TYPE)
3882 ;;; OVERAPPROXIMATE: true if we wish to overapproximate, nil otherwise.
3883 ;;; You usually want T.
3884 ;;; UNION/INTERSECTION/DIFFERENCE: implementations of finite set operations.
3885 ;;; Conform to cl::(union/intersection/set-difference). Passing NIL will
3886 ;;; disable some cleverness and result in quicker computation of coarser
3887 ;;; approximations. However, passing difference without union and intersection
3888 ;;; will probably not end well.
3889 ;;; TOP/BOTTOM: concrete representation of the universe and empty set. Finite
3890 ;;; set operations are never called on TOP/BOTTOM, so it's safe to use special
3892 ;;; OVER/UNDER: the set-valued approximations of F.
3894 ;;; Implementation details.
3896 ;;; It's a straightforward walk down the type.
3897 ;;; Union types -> take the union of children, intersection ->
3898 ;;; intersect. There is some complication for negation types: we must
3899 ;;; not only negate the result, but also flip from overapproximating
3900 ;;; to underapproximating in the children (or vice versa).
3902 ;;; We represent sets as a pair of (negate-p finite-set) in order to
3903 ;;; support negation types.
3905 (declaim (inline generic-abstract-type-function
))
3906 (defun generic-abstract-type-function
3907 (type overapproximate
3908 union intersection difference
3911 (labels ((union* (x y
)
3912 ;; wrappers to avoid calling union/intersection on
3914 (cond ((or (eql x top
)
3920 (funcall union x y
))))
3921 (intersection* (x y
)
3922 (cond ((or (eql x bottom
)
3928 (funcall intersection x y
))))
3929 (unite (not-x-p x not-y-p y
)
3930 ;; if we only have one negated set, it's x.
3932 (rotatef not-x-p not-y-p
)
3934 (cond ((and not-x-p not-y-p
)
3935 ;; -x \/ -y = -(x /\ y)
3936 (normalize t
(intersection* x y
)))
3938 ;; -x \/ y = -(x \ y)
3948 (funcall difference x y
)))))
3950 (values nil
(union* x y
)))))
3951 (intersect (not-x-p x not-y-p y
)
3953 (rotatef not-x-p not-y-p
)
3955 (cond ((and not-x-p not-y-p
)
3956 ;; -x /\ -y = -(x \/ y)
3957 (normalize t
(union* x y
)))
3960 (cond ((or (eql x top
) (eql y bottom
))
3961 (values nil bottom
))
3967 (values nil
(funcall difference y x
)))))
3969 (values nil
(intersection* x y
)))))
3970 (normalize (not-x-p x
)
3971 ;; catch some easy cases of redundant negation.
3972 (cond ((not not-x-p
)
3980 (default (overapproximate)
3982 (if overapproximate top bottom
))
3983 (walk-union (types overapproximate
)
3984 ;; Only do this if union is provided.
3986 (return-from walk-union
(default overapproximate
)))
3987 ;; Reduce/union from bottom.
3988 (let ((not-acc-p nil
)
3990 (dolist (type types
(values not-acc-p acc
))
3991 (multiple-value-bind (not x
)
3992 (walk type overapproximate
)
3993 (setf (values not-acc-p acc
)
3994 (unite not-acc-p acc not x
)))
3995 ;; Early exit on top set.
3996 (when (and (eql acc top
)
3998 (return (values nil top
))))))
3999 (walk-intersection (types overapproximate
)
4000 ;; Skip if we don't know how to intersect sets
4001 (unless intersection
4002 (return-from walk-intersection
(default overapproximate
)))
4003 ;; Reduce/intersection from top
4004 (let ((not-acc-p nil
)
4006 (dolist (type types
(values not-acc-p acc
))
4007 (multiple-value-bind (not x
)
4008 (walk type overapproximate
)
4009 (setf (values not-acc-p acc
)
4010 (intersect not-acc-p acc not x
)))
4011 (when (and (eql acc bottom
)
4013 (return (values nil bottom
))))))
4014 (walk-negate (type overapproximate
)
4015 ;; Don't introduce negated types if we don't know how to
4018 (return-from walk-negate
(default overapproximate
)))
4019 (multiple-value-bind (not x
)
4020 (walk type
(not overapproximate
))
4021 (normalize (not not
) x
)))
4022 (walk (type overapproximate
)
4025 (walk-union (union-type-types type
) overapproximate
))
4026 ((cons (member or union
))
4027 (walk-union (rest type
) overapproximate
))
4029 (walk-intersection (intersection-type-types type
) overapproximate
))
4030 ((cons (member and intersection
))
4031 (walk-intersection (rest type
) overapproximate
))
4033 (walk-negate (negation-type-type type
) overapproximate
))
4035 (walk-negate (second type
) overapproximate
))
4043 (funcall under type
)
4044 (default nil
))))))))
4045 (multiple-value-call #'normalize
(walk type overapproximate
))))
4046 (declaim (notinline generic-abstract-type-function
))
4048 ;;; Standard list representation of sets. Use CL:* for the universe.
4049 (defun list-abstract-type-function (type over
&key under
(overapproximate t
))
4050 (declare (inline generic-abstract-type-function
))
4051 (generic-abstract-type-function
4052 type overapproximate
4053 #'union
#'intersection
#'set-difference
4057 (!defun-from-collected-cold-init-forms
!late-type-cold-init
)
4059 #-sb-xc
(!late-type-cold-init2
)
4061 (/show0
"late-type.lisp end of file")