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)
61 (cond ((unknown-type-p ctype
) t
)
62 ((compound-type-p ctype
)
63 (some #'contains-unknown-type-p
(compound-type-types ctype
)))
64 ((negation-type-p ctype
)
65 (contains-unknown-type-p (negation-type-type ctype
)))))
67 ;;; This is used by !DEFINE-SUPERCLASSES to define the SUBTYPE-ARG1
68 ;;; method. INFO is a list of conses
69 ;;; (SUPERCLASS-CLASS . {GUARD-TYPE-SPECIFIER | NIL}).
70 (defun has-superclasses-complex-subtypep-arg1 (type1 type2 info
)
71 ;; If TYPE2 might be concealing something related to our class
73 (if (type-might-contain-other-types-p type2
)
74 ;; too confusing, gotta punt
76 ;; ordinary case expected by old CMU CL code, where the taxonomy
77 ;; of TYPE2's representation accurately reflects the taxonomy of
80 ;; FIXME: This old CMU CL code probably deserves a comment
81 ;; explaining to us mere mortals how it works...
82 (and (sb!xc
:typep type2
'classoid
)
84 (when (or (not (cdr x
))
85 (csubtypep type1
(specifier-type (cdr x
))))
87 (or (eq type2
(car x
))
88 (let ((inherits (layout-inherits
89 (classoid-layout (car x
)))))
90 (dotimes (i (length inherits
) nil
)
91 (when (eq type2
(layout-classoid (svref inherits i
)))
95 ;;; This function takes a list of specs, each of the form
96 ;;; (SUPERCLASS-NAME &OPTIONAL GUARD).
97 ;;; Consider one spec (with no guard): any instance of the named
98 ;;; TYPE-CLASS is also a subtype of the named superclass and of any of
99 ;;; its superclasses. If there are multiple specs, then some will have
100 ;;; guards. We choose the first spec whose guard is a supertype of
101 ;;; TYPE1 and use its superclass. In effect, a sequence of guards
104 ;;; G0,(and G1 (not G0)), (and G2 (not (or G0 G1))).
106 ;;; WHEN controls when the forms are executed.
107 (defmacro !define-superclasses
(type-class-name specs when
)
108 (with-unique-names (type-class info
)
110 (let ((,type-class
(type-class-or-lose ',type-class-name
))
111 (,info
(mapcar (lambda (spec)
113 (super &optional guard
)
115 (cons (find-classoid super
) guard
)))
117 (setf (type-class-complex-subtypep-arg1 ,type-class
)
118 (lambda (type1 type2
)
119 (has-superclasses-complex-subtypep-arg1 type1 type2
,info
)))
120 (setf (type-class-complex-subtypep-arg2 ,type-class
)
121 #'delegate-complex-subtypep-arg2
)
122 (setf (type-class-complex-intersection2 ,type-class
)
123 #'delegate-complex-intersection2
)))))
125 ;;;; FUNCTION and VALUES types
127 ;;;; Pretty much all of the general type operations are illegal on
128 ;;;; VALUES types, since we can't discriminate using them, do
129 ;;;; SUBTYPEP, etc. FUNCTION types are acceptable to the normal type
130 ;;;; operations, but are generally considered to be equivalent to
131 ;;;; FUNCTION. These really aren't true types in any type theoretic
132 ;;;; sense, but we still parse them into CTYPE structures for two
135 ;;;; -- Parsing and unparsing work the same way, and indeed we can't
136 ;;;; tell whether a type is a function or values type without
138 ;;;; -- Many of the places that can be annotated with real types can
139 ;;;; also be annotated with function or values types.
141 (!define-type-method
(values :simple-subtypep
:complex-subtypep-arg1
)
143 (declare (ignore type2
))
144 ;; FIXME: should be TYPE-ERROR, here and in next method
145 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type1
)))
147 (!define-type-method
(values :complex-subtypep-arg2
)
149 (declare (ignore type1
))
150 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type2
)))
152 (!define-type-method
(values :negate
) (type)
153 (error "NOT VALUES too confusing on ~S" (type-specifier type
)))
155 (!define-type-method
(values :unparse
) (type)
157 (let ((unparsed (unparse-args-types type
)))
158 (if (or (values-type-optional type
)
159 (values-type-rest type
)
160 (values-type-allowp type
))
162 (nconc unparsed
'(&optional
))))))
164 ;;; Return true if LIST1 and LIST2 have the same elements in the same
165 ;;; positions according to TYPE=. We return NIL, NIL if there is an
166 ;;; uncertain comparison.
167 (defun type=-list
(list1 list2
)
168 (declare (list list1 list2
))
169 (do ((types1 list1
(cdr types1
))
170 (types2 list2
(cdr types2
)))
171 ((or (null types1
) (null types2
))
172 (if (or types1 types2
)
175 (multiple-value-bind (val win
)
176 (type= (first types1
) (first types2
))
178 (return (values nil nil
)))
180 (return (values nil t
))))))
182 (!define-type-method
(values :simple-
=) (type1 type2
)
183 (type=-args type1 type2
))
185 (!define-type-class function
:enumerable nil
186 :might-contain-other-types nil
)
188 ;;; a flag that we can bind to cause complex function types to be
189 ;;; unparsed as FUNCTION. This is useful when we want a type that we
190 ;;; can pass to TYPEP.
191 (!defvar
*unparse-fun-type-simplify
* nil
)
192 ;;; A flag to prevent TYPE-OF calls by user applications from returning
193 ;;; (NOT x). TYPE-SPECIFIER usually allows it to preserve information.
194 (!defvar
*unparse-allow-negation
* t
)
196 (!define-type-method
(function :negate
) (type)
197 (make-negation-type :type type
))
199 (!define-type-method
(function :unparse
) (type)
200 (if *unparse-fun-type-simplify
*
203 (if (fun-type-wild-args type
)
205 (unparse-args-types type
))
207 (fun-type-returns type
)))))
209 ;;; The meaning of this is a little confused. On the one hand, all
210 ;;; function objects are represented the same way regardless of the
211 ;;; arglists and return values, and apps don't get to ask things like
212 ;;; (TYPEP #'FOO (FUNCTION (FIXNUM) *)) in any meaningful way. On the
213 ;;; other hand, Python wants to reason about function types. So...
214 (!define-type-method
(function :simple-subtypep
) (type1 type2
)
215 (flet ((fun-type-simple-p (type)
216 (not (or (fun-type-rest type
)
217 (fun-type-keyp type
))))
218 (every-csubtypep (types1 types2
)
222 do
(multiple-value-bind (res sure-p
)
224 (unless res
(return (values res sure-p
))))
225 finally
(return (values t t
)))))
226 (and/type
(values-subtypep (fun-type-returns type1
)
227 (fun-type-returns type2
))
228 (cond ((fun-type-wild-args type2
) (values t t
))
229 ((fun-type-wild-args type1
)
230 (cond ((fun-type-keyp type2
) (values nil nil
))
231 ((not (fun-type-rest type2
)) (values nil t
))
232 ((not (null (fun-type-required type2
)))
234 (t (and/type
(type= *universal-type
*
235 (fun-type-rest type2
))
240 ((not (and (fun-type-simple-p type1
)
241 (fun-type-simple-p type2
)))
243 (t (multiple-value-bind (min1 max1
) (fun-type-nargs type1
)
244 (multiple-value-bind (min2 max2
) (fun-type-nargs type2
)
245 (cond ((or (> max1 max2
) (< min1 min2
))
247 ((and (= min1 min2
) (= max1 max2
))
248 (and/type
(every-csubtypep
249 (fun-type-required type1
)
250 (fun-type-required type2
))
252 (fun-type-optional type1
)
253 (fun-type-optional type2
))))
256 (fun-type-required type1
)
257 (fun-type-optional type1
))
259 (fun-type-required type2
)
260 (fun-type-optional type2
))))))))))))
262 (!define-superclasses function
((function)) !cold-init-forms
)
264 ;;; The union or intersection of two FUNCTION types is FUNCTION.
265 (!define-type-method
(function :simple-union2
) (type1 type2
)
266 (declare (ignore type1 type2
))
267 (specifier-type 'function
))
268 (!define-type-method
(function :simple-intersection2
) (type1 type2
)
269 (let ((ftype (specifier-type 'function
)))
270 (cond ((eq type1 ftype
) type2
)
271 ((eq type2 ftype
) type1
)
272 (t (let ((rtype (values-type-intersection (fun-type-returns type1
)
273 (fun-type-returns type2
))))
274 (flet ((change-returns (ftype rtype
)
275 (declare (type fun-type ftype
) (type ctype rtype
))
276 (make-fun-type :required
(fun-type-required ftype
)
277 :optional
(fun-type-optional ftype
)
278 :keyp
(fun-type-keyp ftype
)
279 :keywords
(fun-type-keywords ftype
)
280 :allowp
(fun-type-allowp ftype
)
283 ((fun-type-wild-args type1
)
284 (if (fun-type-wild-args type2
)
285 (make-fun-type :wild-args t
287 (change-returns type2 rtype
)))
288 ((fun-type-wild-args type2
)
289 (change-returns type1 rtype
))
290 (t (multiple-value-bind (req opt rest
)
291 (args-type-op type1 type2
#'type-intersection
#'max
)
292 (make-fun-type :required req
296 :allowp
(and (fun-type-allowp type1
)
297 (fun-type-allowp type2
))
298 :returns rtype
))))))))))
300 ;;; The union or intersection of a subclass of FUNCTION with a
301 ;;; FUNCTION type is somewhat complicated.
302 (!define-type-method
(function :complex-intersection2
) (type1 type2
)
304 ((type= type1
(specifier-type 'function
)) type2
)
305 ((csubtypep type1
(specifier-type 'function
)) nil
)
306 (t :call-other-method
)))
307 (!define-type-method
(function :complex-union2
) (type1 type2
)
308 (declare (ignore type2
))
309 ;; TYPE2 is a FUNCTION type. If TYPE1 is a classoid type naming
310 ;; FUNCTION, then it is the union of the two; otherwise, there is no
313 ((type= type1
(specifier-type 'function
)) type1
)
316 (!define-type-method
(function :simple-
=) (type1 type2
)
317 (macrolet ((compare (comparator field
)
318 (let ((reader (symbolicate '#:fun-type- field
)))
319 `(,comparator
(,reader type1
) (,reader type2
)))))
320 (and/type
(compare type
= returns
)
321 (cond ((neq (fun-type-wild-args type1
) (fun-type-wild-args type2
))
323 ((eq (fun-type-wild-args type1
) t
)
325 (t (type=-args type1 type2
))))))
327 (!define-type-class constant
:inherits values
)
329 (!define-type-method
(constant :negate
) (type)
330 (error "NOT CONSTANT too confusing on ~S" (type-specifier type
)))
332 (!define-type-method
(constant :unparse
) (type)
333 `(constant-arg ,(type-specifier (constant-type-type type
))))
335 (!define-type-method
(constant :simple-
=) (type1 type2
)
336 (type= (constant-type-type type1
) (constant-type-type type2
)))
338 (!def-type-translator constant-arg
(type)
339 (make-constant-type :type
(single-value-specifier-type type
)))
341 ;;; Return the lambda-list-like type specification corresponding
343 (declaim (ftype (function (args-type) list
) unparse-args-types
))
344 (defun unparse-args-types (type)
347 (dolist (arg (args-type-required type
))
348 (result (type-specifier arg
)))
350 (when (args-type-optional type
)
352 (dolist (arg (args-type-optional type
))
353 (result (type-specifier arg
))))
355 (when (args-type-rest type
)
357 (result (type-specifier (args-type-rest type
))))
359 (when (args-type-keyp type
)
361 (dolist (key (args-type-keywords type
))
362 (result (list (key-info-name key
)
363 (type-specifier (key-info-type key
))))))
365 (when (args-type-allowp type
)
366 (result '&allow-other-keys
))
370 (!def-type-translator function
(&optional
(args '*) (result '*))
371 (let ((result (coerce-to-values (values-specifier-type result
))))
373 (if (eq result
*wild-type
*)
374 (specifier-type 'function
)
375 (make-fun-type :wild-args t
:returns result
))
376 (multiple-value-bind (llks required optional rest keywords
)
377 (parse-args-types args
:function-type
)
378 (if (and (null required
)
380 (eq rest
*universal-type
*)
381 (not (ll-kwds-keyp llks
)))
382 (if (eq result
*wild-type
*)
383 (specifier-type 'function
)
384 (make-fun-type :wild-args t
:returns result
))
385 (make-fun-type :required required
388 :keyp
(ll-kwds-keyp llks
)
390 :allowp
(ll-kwds-allowp llks
)
391 :returns result
))))))
393 (!def-type-translator values
:list
(&rest values
)
396 (multiple-value-bind (llks required optional rest
)
397 (parse-args-types values
:values-type
)
399 (make-values-type :required required
:optional optional
:rest rest
)
400 (make-short-values-type required
)))))
402 ;;;; VALUES types interfaces
404 ;;;; We provide a few special operations that can be meaningfully used
405 ;;;; on VALUES types (as well as on any other type).
407 ;;; Return the minimum number of values possibly matching VALUES type
409 (defun values-type-min-value-count (type)
412 (ecase (named-type-name type
)
416 (length (values-type-required type
)))))
418 ;;; Return the maximum number of values possibly matching VALUES type
420 (defun values-type-max-value-count (type)
423 (ecase (named-type-name type
)
424 ((t *) call-arguments-limit
)
427 (if (values-type-rest type
)
429 (+ (length (values-type-optional type
))
430 (length (values-type-required type
)))))))
432 (defun values-type-may-be-single-value-p (type)
433 (<= (values-type-min-value-count type
)
435 (values-type-max-value-count type
)))
437 ;;; VALUES type with a single value.
438 (defun type-single-value-p (type)
439 (and (%values-type-p type
)
440 (not (values-type-rest type
))
441 (null (values-type-optional type
))
442 (singleton-p (values-type-required type
))))
444 ;;; Return the type of the first value indicated by TYPE. This is used
445 ;;; by people who don't want to have to deal with VALUES types.
446 #!-sb-fluid
(declaim (freeze-type values-type
))
447 ; (inline single-value-type))
448 (defun single-value-type (type)
449 (declare (type ctype type
))
450 (cond ((eq type
*wild-type
*)
452 ((eq type
*empty-type
*)
454 ((not (values-type-p type
))
456 ((car (args-type-required type
)))
457 (t (type-union (specifier-type 'null
)
458 (or (car (args-type-optional type
))
459 (args-type-rest type
)
460 (specifier-type 'null
))))))
462 ;;; Return the minimum number of arguments that a function can be
463 ;;; called with, and the maximum number or NIL. If not a function
464 ;;; type, return NIL, NIL.
465 (defun fun-type-nargs (type)
466 (declare (type ctype type
))
467 (if (and (fun-type-p type
) (not (fun-type-wild-args type
)))
468 (let ((fixed (length (args-type-required type
))))
469 (if (or (args-type-rest type
)
470 (args-type-keyp type
)
471 (args-type-allowp type
))
473 (values fixed
(+ fixed
(length (args-type-optional type
))))))
476 ;;; Determine whether TYPE corresponds to a definite number of values.
477 ;;; The first value is a list of the types for each value, and the
478 ;;; second value is the number of values. If the number of values is
479 ;;; not fixed, then return NIL and :UNKNOWN.
480 (defun values-types (type)
481 (declare (type ctype type
))
482 (cond ((or (eq type
*wild-type
*) (eq type
*empty-type
*))
483 (values nil
:unknown
))
484 ((or (args-type-optional type
)
485 (args-type-rest type
))
486 (values nil
:unknown
))
488 (let ((req (args-type-required type
)))
489 (values req
(length req
))))))
491 ;;; Return two values:
492 ;;; 1. A list of all the positional (fixed and optional) types.
493 ;;; 2. The &REST type (if any). If no &REST, then the DEFAULT-TYPE.
494 (defun values-type-types (type &optional
(default-type *empty-type
*))
495 (declare (type ctype type
))
496 (if (eq type
*wild-type
*)
497 (values nil
*universal-type
*)
498 (values (append (args-type-required type
)
499 (args-type-optional type
))
500 (cond ((args-type-rest type
))
503 ;;; types of values in (the <type> (values o_1 ... o_n))
504 (defun values-type-out (type count
)
505 (declare (type ctype type
) (type unsigned-byte count
))
506 (if (eq type
*wild-type
*)
507 (make-list count
:initial-element
*universal-type
*)
509 (flet ((process-types (types)
510 (loop for type in types
514 (process-types (values-type-required type
))
515 (process-types (values-type-optional type
))
517 (loop with rest
= (the ctype
(values-type-rest type
))
522 ;;; types of variable in (m-v-bind (v_1 ... v_n) (the <type> ...
523 (defun values-type-in (type count
)
524 (declare (type ctype type
) (type unsigned-byte count
))
525 (if (eq type
*wild-type
*)
526 (make-list count
:initial-element
*universal-type
*)
528 (let ((null-type (specifier-type 'null
)))
529 (loop for type in
(values-type-required type
)
533 (loop for type in
(values-type-optional type
)
536 do
(res (type-union type null-type
)))
538 (loop with rest
= (acond ((values-type-rest type
)
539 (type-union it null-type
))
545 ;;; Return a list of OPERATION applied to the types in TYPES1 and
546 ;;; TYPES2, padding with REST2 as needed. TYPES1 must not be shorter
547 ;;; than TYPES2. The second value is T if OPERATION always returned a
548 ;;; true second value.
549 (defun fixed-values-op (types1 types2 rest2 operation
)
550 (declare (list types1 types2
) (type ctype rest2
) (type function operation
))
552 (values (mapcar (lambda (t1 t2
)
553 (multiple-value-bind (res win
)
554 (funcall operation t1 t2
)
560 (make-list (- (length types1
) (length types2
))
561 :initial-element rest2
)))
564 ;;; If TYPE isn't a values type, then make it into one.
565 (defun-cached (%coerce-to-values
:hash-bits
8 :hash-function
#'type-hash-value
)
567 (cond ((multiple-value-bind (res sure
)
568 (csubtypep (specifier-type 'null
) type
)
569 (and (not res
) sure
))
570 ;; FIXME: What should we do with (NOT SURE)?
571 (make-values-type :required
(list type
) :rest
*universal-type
*))
573 (make-values-type :optional
(list type
) :rest
*universal-type
*))))
575 (defun coerce-to-values (type)
576 (declare (type ctype type
))
577 (cond ((or (eq type
*universal-type
*)
578 (eq type
*wild-type
*))
580 ((values-type-p type
)
582 (t (%coerce-to-values type
))))
584 ;;; Return type, corresponding to ANSI short form of VALUES type
586 (defun make-short-values-type (types)
587 (declare (list types
))
588 (let ((last-required (position-if
590 (not/type
(csubtypep (specifier-type 'null
) type
)))
594 (make-values-type :required
(subseq types
0 (1+ last-required
))
595 :optional
(subseq types
(1+ last-required
))
596 :rest
*universal-type
*)
597 (make-values-type :optional types
:rest
*universal-type
*))))
599 (defun make-single-value-type (type)
600 (make-values-type :required
(list type
)))
602 ;;; Do the specified OPERATION on TYPE1 and TYPE2, which may be any
603 ;;; type, including VALUES types. With VALUES types such as:
606 ;;; we compute the more useful result
607 ;;; (VALUES (<operation> a0 b0) (<operation> a1 b1))
608 ;;; rather than the precise result
609 ;;; (<operation> (values a0 a1) (values b0 b1))
610 ;;; This has the virtue of always keeping the VALUES type specifier
611 ;;; outermost, and retains all of the information that is really
612 ;;; useful for static type analysis. We want to know what is always
613 ;;; true of each value independently. It is worthless to know that if
614 ;;; the first value is B0 then the second will be B1.
616 ;;; If the VALUES count signatures differ, then we produce a result with
617 ;;; the required VALUE count chosen by NREQ when applied to the number
618 ;;; of required values in TYPE1 and TYPE2. Any &KEY values become
619 ;;; &REST T (anyone who uses keyword values deserves to lose.)
621 ;;; The second value is true if the result is definitely empty or if
622 ;;; OPERATION returned true as its second value each time we called
623 ;;; it. Since we approximate the intersection of VALUES types, the
624 ;;; second value being true doesn't mean the result is exact.
625 (defun args-type-op (type1 type2 operation nreq
)
626 (declare (type ctype type1 type2
)
627 (type function operation nreq
))
628 (when (eq type1 type2
)
630 (multiple-value-bind (types1 rest1
)
631 (values-type-types type1
)
632 (multiple-value-bind (types2 rest2
)
633 (values-type-types type2
)
634 (multiple-value-bind (rest rest-exact
)
635 (funcall operation rest1 rest2
)
636 (multiple-value-bind (res res-exact
)
637 (if (< (length types1
) (length types2
))
638 (fixed-values-op types2 types1 rest1 operation
)
639 (fixed-values-op types1 types2 rest2 operation
))
640 (let* ((req (funcall nreq
641 (length (args-type-required type1
))
642 (length (args-type-required type2
))))
643 (required (subseq res
0 req
))
644 (opt (subseq res req
)))
645 (values required opt rest
646 (and rest-exact res-exact
))))))))
648 (defun values-type-op (type1 type2 operation nreq
)
649 (multiple-value-bind (required optional rest exactp
)
650 (args-type-op type1 type2 operation nreq
)
651 (values (make-values-type :required required
656 (defun compare-key-args (type1 type2
)
657 (let ((keys1 (args-type-keywords type1
))
658 (keys2 (args-type-keywords type2
)))
659 (and (= (length keys1
) (length keys2
))
660 (eq (args-type-allowp type1
)
661 (args-type-allowp type2
))
662 (loop for key1 in keys1
663 for match
= (find (key-info-name key1
)
664 keys2
:key
#'key-info-name
)
666 (type= (key-info-type key1
)
667 (key-info-type match
)))))))
669 (defun type=-args
(type1 type2
)
670 (macrolet ((compare (comparator field
)
671 (let ((reader (symbolicate '#:args-type- field
)))
672 `(,comparator
(,reader type1
) (,reader type2
)))))
674 (cond ((null (args-type-rest type1
))
675 (values (null (args-type-rest type2
)) t
))
676 ((null (args-type-rest type2
))
679 (compare type
= rest
)))
680 (and/type
(and/type
(compare type
=-list required
)
681 (compare type
=-list optional
))
682 (if (or (args-type-keyp type1
) (args-type-keyp type2
))
683 (values (compare-key-args type1 type2
) t
)
686 ;;; Do a union or intersection operation on types that might be values
687 ;;; types. The result is optimized for utility rather than exactness,
688 ;;; but it is guaranteed that it will be no smaller (more restrictive)
689 ;;; than the precise result.
691 ;;; The return convention seems to be analogous to
692 ;;; TYPES-EQUAL-OR-INTERSECT. -- WHN 19990910.
693 (defun-cached (values-type-union :hash-function
#'type-cache-hash
695 ((type1 eq
) (type2 eq
))
696 (declare (type ctype type1 type2
))
697 (cond ((or (eq type1
*wild-type
*) (eq type2
*wild-type
*)) *wild-type
*)
698 ((eq type1
*empty-type
*) type2
)
699 ((eq type2
*empty-type
*) type1
)
701 (values (values-type-op type1 type2
#'type-union
#'min
)))))
703 (defun-cached (values-type-intersection :hash-function
#'type-cache-hash
705 ((type1 eq
) (type2 eq
))
706 (declare (type ctype type1 type2
))
707 (cond ((eq type1
*wild-type
*)
708 (coerce-to-values type2
))
709 ((or (eq type2
*wild-type
*) (eq type2
*universal-type
*))
711 ((or (eq type1
*empty-type
*) (eq type2
*empty-type
*))
713 ((and (not (values-type-p type2
))
714 (values-type-required type1
))
715 (let ((req1 (values-type-required type1
)))
716 (make-values-type :required
(cons (type-intersection (first req1
) type2
)
718 :optional
(values-type-optional type1
)
719 :rest
(values-type-rest type1
)
720 :allowp
(values-type-allowp type1
))))
722 (values (values-type-op type1
(coerce-to-values type2
)
726 ;;; This is like TYPES-EQUAL-OR-INTERSECT, except that it sort of
727 ;;; works on VALUES types. Note that due to the semantics of
728 ;;; VALUES-TYPE-INTERSECTION, this might return (VALUES T T) when
729 ;;; there isn't really any intersection.
730 (defun values-types-equal-or-intersect (type1 type2
)
731 (cond ((or (eq type1
*empty-type
*) (eq type2
*empty-type
*))
733 ((or (eq type1
*wild-type
*) (eq type2
*wild-type
*))
736 (let ((res (values-type-intersection type1 type2
)))
737 (values (not (eq res
*empty-type
*))
740 ;;; a SUBTYPEP-like operation that can be used on any types, including
742 (defun-cached (values-subtypep :hash-function
#'type-cache-hash
745 ((type1 eq
) (type2 eq
))
746 (declare (type ctype type1 type2
))
747 (cond ((or (eq type2
*wild-type
*) (eq type2
*universal-type
*)
748 (eq type1
*empty-type
*))
750 ((eq type1
*wild-type
*)
751 (values (eq type2
*wild-type
*) t
))
752 ((or (eq type2
*empty-type
*)
753 (not (values-types-equal-or-intersect type1 type2
)))
755 ((and (not (values-type-p type2
))
756 (values-type-required type1
))
757 (csubtypep (first (values-type-required type1
))
759 (t (setq type2
(coerce-to-values type2
))
760 (multiple-value-bind (types1 rest1
) (values-type-types type1
)
761 (multiple-value-bind (types2 rest2
) (values-type-types type2
)
762 (cond ((< (length (values-type-required type1
))
763 (length (values-type-required type2
)))
765 ((< (length types1
) (length types2
))
768 (do ((t1 types1
(rest t1
))
769 (t2 types2
(rest t2
)))
771 (csubtypep rest1 rest2
))
772 (multiple-value-bind (res win-p
)
773 (csubtypep (first t1
) (first t2
))
775 (return (values nil nil
)))
777 (return (values nil t
))))))))))))
779 ;;;; type method interfaces
781 ;;; like SUBTYPEP, only works on CTYPE structures
782 (defun-cached (csubtypep :hash-function
#'type-cache-hash
786 ((type1 eq
) (type2 eq
))
787 (declare (type ctype type1 type2
))
788 (cond ((or (eq type1 type2
)
789 (eq type1
*empty-type
*)
790 (eq type2
*universal-type
*))
793 ((eq type1
*universal-type
*)
797 (!invoke-type-method
:simple-subtypep
:complex-subtypep-arg2
799 :complex-arg1
:complex-subtypep-arg1
)))))
801 ;;; Just parse the type specifiers and call CSUBTYPE.
802 (defun sb!xc
:subtypep
(type1 type2
&optional environment
)
804 "Return two values indicating the relationship between type1 and type2.
805 If values are T and T, type1 definitely is a subtype of type2.
806 If values are NIL and T, type1 definitely is not a subtype of type2.
807 If values are NIL and NIL, it couldn't be determined."
808 (declare (type lexenv-designator environment
) (ignore environment
))
809 (csubtypep (specifier-type type1
) (specifier-type type2
)))
811 ;;; If two types are definitely equivalent, return true. The second
812 ;;; value indicates whether the first value is definitely correct.
813 ;;; This should only fail in the presence of HAIRY types.
814 (defun-cached (type= :hash-function
#'type-cache-hash
818 ((type1 eq
) (type2 eq
))
819 (declare (type ctype type1 type2
))
820 (cond ((eq type1 type2
)
822 ;; If args are not EQ, but both allow TYPE= optimization,
823 ;; and at least one is interned, then return no and certainty.
824 ((and (minusp (logior (type-hash-value type1
) (type-hash-value type2
)))
825 (logtest (logand (type-hash-value type1
) (type-hash-value type2
))
826 +type-admits-type
=-optimization
+))
829 (memoize (!invoke-type-method
:simple-
= :complex-
= type1 type2
)))))
831 ;;; Not exactly the negation of TYPE=, since when the relationship is
832 ;;; uncertain, we still return NIL, NIL. This is useful in cases where
833 ;;; the conservative assumption is =.
834 (defun type/= (type1 type2
)
835 (declare (type ctype type1 type2
))
836 (multiple-value-bind (res win
) (type= type1 type2
)
841 ;;; the type method dispatch case of TYPE-UNION2
842 (defun %type-union2
(type1 type2
)
843 ;; As in %TYPE-INTERSECTION2, it seems to be a good idea to give
844 ;; both argument orders a chance at COMPLEX-INTERSECTION2. Unlike
845 ;; %TYPE-INTERSECTION2, though, I don't have a specific case which
846 ;; demonstrates this is actually necessary. Also unlike
847 ;; %TYPE-INTERSECTION2, there seems to be no need to distinguish
848 ;; between not finding a method and having a method return NIL.
850 (!invoke-type-method
:simple-union2
:complex-union2
853 (declare (inline 1way
))
854 (or (1way type1 type2
)
855 (1way type2 type1
))))
857 ;;; Find a type which includes both types. Any inexactness is
858 ;;; represented by the fuzzy element types; we return a single value
859 ;;; that is precise to the best of our knowledge. This result is
860 ;;; simplified into the canonical form, thus is not a UNION-TYPE
861 ;;; unless we find no other way to represent the result.
862 (defun-cached (type-union2 :hash-function
#'type-cache-hash
865 ((type1 eq
) (type2 eq
))
866 ;; KLUDGE: This was generated from TYPE-INTERSECTION2 by Ye Olde Cut And
867 ;; Paste technique of programming. If it stays around (as opposed to
868 ;; e.g. fading away in favor of some CLOS solution) the shared logic
869 ;; should probably become shared code. -- WHN 2001-03-16
870 (declare (type ctype type1 type2
))
876 ;; CSUBTYPEP for array-types answers questions about the
877 ;; specialized type, yet for union we want to take the
878 ;; expressed type in account too.
879 ((and (not (and (array-type-p type1
) (array-type-p type2
)))
880 (or (setf t2
(csubtypep type1 type2
))
881 (csubtypep type2 type1
)))
883 ((or (union-type-p type1
)
884 (union-type-p type2
))
885 ;; Unions of UNION-TYPE should have the UNION-TYPE-TYPES
886 ;; values broken out and united separately. The full TYPE-UNION
887 ;; function knows how to do this, so let it handle it.
888 (type-union type1 type2
))
890 ;; the ordinary case: we dispatch to type methods
891 (%type-union2 type1 type2
)))))))
893 ;;; the type method dispatch case of TYPE-INTERSECTION2
894 (defun %type-intersection2
(type1 type2
)
895 ;; We want to give both argument orders a chance at
896 ;; COMPLEX-INTERSECTION2. Without that, the old CMU CL type
897 ;; methods could give noncommutative results, e.g.
898 ;; (TYPE-INTERSECTION2 *EMPTY-TYPE* SOME-HAIRY-TYPE)
900 ;; (TYPE-INTERSECTION2 SOME-HAIRY-TYPE *EMPTY-TYPE*)
901 ;; => #<NAMED-TYPE NIL>, T
902 ;; We also need to distinguish between the case where we found a
903 ;; type method, and it returned NIL, and the case where we fell
904 ;; through without finding any type method. An example of the first
905 ;; case is the intersection of a HAIRY-TYPE with some ordinary type.
906 ;; An example of the second case is the intersection of two
907 ;; completely-unrelated types, e.g. CONS and NUMBER, or SYMBOL and
910 ;; (Why yes, CLOS probably *would* be nicer..)
912 (!invoke-type-method
:simple-intersection2
:complex-intersection2
914 :default
:call-other-method
)))
915 (declare (inline 1way
))
916 (let ((xy (1way type1 type2
)))
917 (or (and (not (eql xy
:call-other-method
)) xy
)
918 (let ((yx (1way type2 type1
)))
919 (or (and (not (eql yx
:call-other-method
)) yx
)
920 (cond ((and (eql xy
:call-other-method
)
921 (eql yx
:call-other-method
))
926 (defun-cached (type-intersection2 :hash-function
#'type-cache-hash
930 ((type1 eq
) (type2 eq
))
931 (declare (type ctype type1 type2
))
933 ;; FIXME: For some reason, this doesn't catch e.g. type1 =
934 ;; type2 = (SPECIFIER-TYPE
935 ;; 'SOME-UNKNOWN-TYPE). Investigate. - CSR, 2002-04-10
939 ((or (intersection-type-p type1
)
940 (intersection-type-p type2
))
941 ;; Intersections of INTERSECTION-TYPE should have the
942 ;; INTERSECTION-TYPE-TYPES values broken out and intersected
943 ;; separately. The full TYPE-INTERSECTION function knows how
944 ;; to do that, so let it handle it.
945 (type-intersection type1 type2
))
947 ;; the ordinary case: we dispatch to type methods
948 (%type-intersection2 type1 type2
))))))
950 ;;; Return as restrictive and simple a type as we can discover that is
951 ;;; no more restrictive than the intersection of TYPE1 and TYPE2. At
952 ;;; worst, we arbitrarily return one of the arguments as the first
953 ;;; value (trying not to return a hairy type).
954 (defun type-approx-intersection2 (type1 type2
)
955 (cond ((type-intersection2 type1 type2
))
956 ((hairy-type-p type1
) type2
)
959 ;;; a test useful for checking whether a derived type matches a
962 ;;; The first value is true unless the types don't intersect and
963 ;;; aren't equal. The second value is true if the first value is
964 ;;; definitely correct. NIL is considered to intersect with any type.
965 ;;; If T is a subtype of either type, then we also return T, T. This
966 ;;; way we recognize that hairy types might intersect with T.
968 ;;; Well now given the statement above that this is "useful for ..."
969 ;;; a particular thing, I see how treating *empty-type* magically could
970 ;;; be useful, however given all the _other_ calls to this function within
971 ;;; this file, it seems suboptimal, because logically it is wrong.
972 (defun types-equal-or-intersect (type1 type2
)
973 (declare (type ctype type1 type2
))
974 (if (or (eq type1
*empty-type
*) (eq type2
*empty-type
*))
976 (let ((intersection2 (type-intersection2 type1 type2
)))
977 (cond ((not intersection2
)
978 (if (or (csubtypep *universal-type
* type1
)
979 (csubtypep *universal-type
* type2
))
982 ((eq intersection2
*empty-type
*) (values nil t
))
985 ;;; Return a Common Lisp type specifier corresponding to the TYPE
987 (defun type-specifier (type)
988 (declare (type ctype type
))
989 (funcall (type-class-unparse (type-class-info type
)) type
))
991 ;;; Don't try to define a print method until it's actually gonna work!
992 ;;; (Otherwise this would be near the DEFSTRUCT)
993 (def!method print-object
((ctype ctype
) stream
)
994 (print-unreadable-object (ctype stream
:type t
)
995 (prin1 (type-specifier ctype
) stream
)))
998 ;;; Just dump it as a specifier. (We'll convert it back upon loading.)
999 (defun make-type-load-form (type)
1000 (declare (type ctype type
))
1001 `(specifier-type ',(type-specifier type
)))
1003 (defun-cached (type-negation :hash-function
#'type-hash-value
1007 (declare (type ctype type
))
1008 (funcall (type-class-negate (type-class-info type
)) type
))
1010 (defun-cached (type-singleton-p :hash-function
#'type-hash-value
1014 (declare (type ctype type
))
1015 (let ((function (type-class-singleton-p (type-class-info type
))))
1017 (funcall function type
)
1020 ;;; (VALUES-SPECIFIER-TYPE and SPECIFIER-TYPE moved from here to
1021 ;;; early-type.lisp by WHN ca. 19990201.)
1023 ;;; Take a list of type specifiers, computing the translation of each
1024 ;;; specifier and defining it as a builtin type.
1025 ;;; Seee the comments in 'type-init' for why this is a slightly
1026 ;;; screwy way to go about it.
1027 (declaim (ftype (function (list) (values)) !precompute-types
))
1028 (defun !precompute-types
(specs)
1029 (dolist (spec specs
)
1030 (let ((res (handler-bind
1031 ((parse-unknown-type
1033 (declare (ignore c
))
1034 ;; We can handle conditions at this point,
1035 ;; but win32 can not perform i/o here because
1036 ;; !MAKE-COLD-STDERR-STREAM has no implementation.
1038 (progn (write-string "//caught: parse-unknown ")
1041 (specifier-type spec
))))
1042 (unless (unknown-type-p res
)
1043 (setf (info :type
:builtin spec
) res
)
1044 (setf (info :type
:kind spec
) :primitive
))))
1047 ;;;; general TYPE-UNION and TYPE-INTERSECTION operations
1049 ;;;; These are fully general operations on CTYPEs: they'll always
1050 ;;;; return a CTYPE representing the result.
1052 ;;; shared logic for unions and intersections: Return a list of
1053 ;;; types representing the same types as INPUT-TYPES, but with
1054 ;;; COMPOUND-TYPEs satisfying %COMPOUND-TYPE-P broken up into their
1055 ;;; component types, and with any SIMPLY2 simplifications applied.
1057 ((def (name compound-type-p simplify2
)
1058 `(defun ,name
(types)
1060 (multiple-value-bind (first rest
)
1061 (if (,compound-type-p
(car types
))
1062 (values (car (compound-type-types (car types
)))
1063 (append (cdr (compound-type-types (car types
)))
1065 (values (car types
) (cdr types
)))
1066 (let ((rest (,name rest
)) u
)
1067 (dolist (r rest
(cons first rest
))
1068 (when (setq u
(,simplify2 first r
))
1069 (return (,name
(nsubstitute u r rest
)))))))))))
1070 (def simplify-intersections intersection-type-p type-intersection2
)
1071 (def simplify-unions union-type-p type-union2
))
1073 (defun maybe-distribute-one-union (union-type types
)
1074 (let* ((intersection (apply #'type-intersection types
))
1075 (union (mapcar (lambda (x) (type-intersection x intersection
))
1076 (union-type-types union-type
))))
1077 (if (notany (lambda (x) (or (hairy-type-p x
)
1078 (intersection-type-p x
)))
1083 (defun type-intersection (&rest input-types
)
1084 (%type-intersection input-types
))
1085 (defun-cached (%type-intersection
:hash-bits
10 :hash-function
#'type-list-cache-hash
)
1086 ((input-types equal
))
1087 (let ((simplified-types (simplify-intersections input-types
)))
1088 (declare (type list simplified-types
))
1089 ;; We want to have a canonical representation of types (or failing
1090 ;; that, punt to HAIRY-TYPE). Canonical representation would have
1091 ;; intersections inside unions but not vice versa, since you can
1092 ;; always achieve that by the distributive rule. But we don't want
1093 ;; to just apply the distributive rule, since it would be too easy
1094 ;; to end up with unreasonably huge type expressions. So instead
1095 ;; we try to generate a simple type by distributing the union; if
1096 ;; the type can't be made simple, we punt to HAIRY-TYPE.
1097 (if (and (cdr simplified-types
) (some #'union-type-p simplified-types
))
1098 (let* ((first-union (find-if #'union-type-p simplified-types
))
1099 (other-types (coerce (remove first-union simplified-types
)
1101 (distributed (maybe-distribute-one-union first-union
1104 (apply #'type-union distributed
)
1105 (%make-hairy-type
`(and ,@(map 'list
#'type-specifier
1106 simplified-types
)))))
1108 ((null simplified-types
) *universal-type
*)
1109 ((null (cdr simplified-types
)) (car simplified-types
))
1110 (t (%make-intersection-type
1111 (some #'type-enumerable simplified-types
)
1112 simplified-types
))))))
1114 (defun type-union (&rest input-types
)
1115 (%type-union input-types
))
1116 (defun-cached (%type-union
:hash-bits
8 :hash-function
#'type-list-cache-hash
)
1117 ((input-types equal
))
1118 (let ((simplified-types (simplify-unions input-types
)))
1120 ((null simplified-types
) *empty-type
*)
1121 ((null (cdr simplified-types
)) (car simplified-types
))
1123 (every #'type-enumerable simplified-types
)
1124 simplified-types
)))))
1128 (!define-type-class named
:enumerable nil
:might-contain-other-types nil
)
1130 ;; This is used when parsing (SATISFIES KEYWORDP)
1131 ;; so that simplifications can be made when computing intersections,
1132 ;; without which we would see this kind of "empty-type in disguise"
1133 ;; (AND (SATISFIES KEYWORDP) CONS)
1134 ;; This isn't *keyword-type* because KEYWORD is implemented
1135 ;; as the intersection of SYMBOL and (SATISFIES KEYWORDP)
1136 ;; We could also intern the KEYWORD type but that would require
1137 ;; hacking the INTERSECTION logic.
1138 (defglobal *satisfies-keywordp-type
* -
1)
1140 ;; Here too I discovered more than 1000 instances in a particular
1141 ;; Lisp image, when really this is *EMPTY-TYPE*.
1142 ;; (AND (SATISFIES LEGAL-FUN-NAME-P) (SIMPLE-ARRAY CHARACTER (*)))
1143 (defglobal *fun-name-type
* -
1)
1145 ;; !LATE-TYPE-COLD-INIT can't be GCd - there are lambdas in the toplevel code
1146 ;; component that leak out and persist - but everything below is GCable.
1147 ;; This leads to about 20KB of extra code being retained on x86-64.
1148 ;; An educated guess is that DEFINE-SUPERCLASSES is responsible for the problem.
1149 (defun !late-type-cold-init2
()
1150 (macrolet ((frob (name var
)
1153 (mark-ctype-interned (make-named-type :name
',name
)))
1154 (setf (info :type
:kind
',name
) :primitive
)
1155 (setf (info :type
:builtin
',name
) ,var
))))
1156 ;; KLUDGE: In ANSI, * isn't really the name of a type, it's just a
1157 ;; special symbol which can be stuck in some places where an
1158 ;; ordinary type can go, e.g. (ARRAY * 1) instead of (ARRAY T 1).
1159 ;; In SBCL it also used to denote universal VALUES type.
1160 (frob * *wild-type
*)
1161 (frob nil
*empty-type
*)
1162 (frob t
*universal-type
*)
1163 (setf (sb!c
::meta-info-default
(sb!c
::meta-info
:variable
:type
))
1165 ;; new in sbcl-0.9.5: these used to be CLASSOID types, but that
1166 ;; view of them was incompatible with requirements on the MOP
1167 ;; metaobject class hierarchy: the INSTANCE and
1168 ;; FUNCALLABLE-INSTANCE types are disjoint (instances have
1169 ;; instance-pointer-lowtag; funcallable-instances have
1170 ;; fun-pointer-lowtag), while FUNCALLABLE-STANDARD-OBJECT is
1171 ;; required to be a subclass of STANDARD-OBJECT. -- CSR,
1173 (frob instance
*instance-type
*)
1174 (frob funcallable-instance
*funcallable-instance-type
*)
1175 ;; new in sbcl-1.0.3.3: necessary to act as a join point for the
1176 ;; extended sequence hierarchy. (Might be removed later if we use
1177 ;; a dedicated FUNDAMENTAL-SEQUENCE class for this.)
1178 (frob extended-sequence
*extended-sequence-type
*))
1179 (!intern-important-fun-type-instances
)
1180 (!intern-important-member-type-instances
)
1181 (!intern-important-cons-type-instances
)
1182 (!intern-important-numeric-type-instances
)
1183 (!intern-important-character-set-type-instances
)
1184 (!intern-important-array-type-instances
) ; must be after numeric and char
1185 (setf *satisfies-keywordp-type
*
1186 (mark-ctype-interned (%make-hairy-type
'(satisfies keywordp
))))
1187 (setf *fun-name-type
*
1188 (mark-ctype-interned (%make-hairy-type
'(satisfies legal-fun-name-p
))))
1189 ;; This is not an important type- no attempt is made to return exactly this
1190 ;; object when parsing FUNCTION. In fact we return the classoid instead
1191 (setf *universal-fun-type
*
1192 (make-fun-type :wild-args t
:returns
*wild-type
*)))
1194 (!define-type-method
(named :simple-
=) (type1 type2
)
1195 ;;(aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1196 (values (eq type1 type2
) t
))
1198 (defun cons-type-might-be-empty-type (type)
1199 (declare (type cons-type type
))
1200 (let ((car-type (cons-type-car-type type
))
1201 (cdr-type (cons-type-cdr-type type
)))
1203 (if (cons-type-p car-type
)
1204 (cons-type-might-be-empty-type car-type
)
1205 (multiple-value-bind (yes surep
)
1206 (type= car-type
*empty-type
*)
1209 (if (cons-type-p cdr-type
)
1210 (cons-type-might-be-empty-type cdr-type
)
1211 (multiple-value-bind (yes surep
)
1212 (type= cdr-type
*empty-type
*)
1216 (defun cons-type-length-info (type)
1217 (declare (type cons-type type
))
1218 (do ((min 1 (1+ min
))
1219 (cdr (cons-type-cdr-type type
) (cons-type-cdr-type cdr
)))
1220 ((not (cons-type-p cdr
))
1222 ((csubtypep cdr
(specifier-type 'null
))
1224 ((csubtypep *universal-type
* cdr
)
1226 ((type/= (type-intersection (specifier-type 'cons
) cdr
) *empty-type
*)
1228 ((type/= (type-intersection (specifier-type 'null
) cdr
) *empty-type
*)
1230 (t (values min
:maybe
))))
1233 (!define-type-method
(named :complex-
=) (type1 type2
)
1235 ((and (eq type2
*empty-type
*)
1236 (or (and (intersection-type-p type1
)
1237 ;; not allowed to be unsure on these... FIXME: keep
1238 ;; the list of CL types that are intersection types
1239 ;; once and only once.
1240 (not (or (type= type1
(specifier-type 'ratio
))
1241 (type= type1
(specifier-type 'keyword
)))))
1242 (and (cons-type-p type1
)
1243 (cons-type-might-be-empty-type type1
))))
1244 ;; things like (AND (EQL 0) (SATISFIES ODDP)) or (AND FUNCTION
1245 ;; STREAM) can get here. In general, we can't really tell
1246 ;; whether these are equal to NIL or not, so
1248 ((type-might-contain-other-types-p type1
)
1249 (invoke-complex-=-other-method type1 type2
))
1250 (t (values nil t
))))
1252 (!define-type-method
(named :simple-subtypep
) (type1 type2
)
1253 (aver (not (eq type1
*wild-type
*))) ; * isn't really a type.
1254 (aver (not (eq type1 type2
)))
1255 (values (or (eq type1
*empty-type
*)
1256 (eq type2
*wild-type
*)
1257 (eq type2
*universal-type
*)) t
))
1259 (!define-type-method
(named :complex-subtypep-arg1
) (type1 type2
)
1260 ;; This AVER causes problems if we write accurate methods for the
1261 ;; union (and possibly intersection) types which then delegate to
1262 ;; us; while a user shouldn't get here, because of the odd status of
1263 ;; *wild-type* a type-intersection executed by the compiler can. -
1266 ;; (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1267 (cond ((eq type1
*empty-type
*)
1269 (;; When TYPE2 might be the universal type in disguise
1270 (type-might-contain-other-types-p type2
)
1271 ;; Now that the UNION and HAIRY COMPLEX-SUBTYPEP-ARG2 methods
1272 ;; can delegate to us (more or less as CALL-NEXT-METHOD) when
1273 ;; they're uncertain, we can't just barf on COMPOUND-TYPE and
1274 ;; HAIRY-TYPEs as we used to. Instead we deal with the
1275 ;; problem (where at least part of the problem is cases like
1276 ;; (SUBTYPEP T '(SATISFIES FOO))
1278 ;; (SUBTYPEP T '(AND (SATISFIES FOO) (SATISFIES BAR)))
1279 ;; where the second type is a hairy type like SATISFIES, or
1280 ;; is a compound type which might contain a hairy type) by
1281 ;; returning uncertainty.
1283 ((eq type1
*funcallable-instance-type
*)
1284 (values (eq type2
(specifier-type 'function
)) t
))
1286 ;; This case would have been picked off by the SIMPLE-SUBTYPEP
1287 ;; method, and so shouldn't appear here.
1288 (aver (not (named-type-p type2
)))
1289 ;; Since TYPE2 is not EQ *UNIVERSAL-TYPE* and is not another
1290 ;; named type in disguise, TYPE2 is not a superset of TYPE1.
1293 (!define-type-method
(named :complex-subtypep-arg2
) (type1 type2
)
1294 (aver (not (eq type2
*wild-type
*))) ; * isn't really a type.
1295 (cond ((eq type2
*universal-type
*)
1297 ;; some CONS types can conceal danger
1298 ((and (cons-type-p type1
) (cons-type-might-be-empty-type type1
))
1300 ((type-might-contain-other-types-p type1
)
1301 ;; those types can be other types in disguise. So we'd
1303 (invoke-complex-subtypep-arg1-method type1 type2
))
1304 ((and (or (eq type2
*instance-type
*)
1305 (eq type2
*funcallable-instance-type
*))
1306 (member-type-p type1
))
1307 ;; member types can be subtypep INSTANCE and
1308 ;; FUNCALLABLE-INSTANCE in surprising ways.
1309 (invoke-complex-subtypep-arg1-method type1 type2
))
1310 ((and (eq type2
*extended-sequence-type
*) (classoid-p type1
))
1311 (let* ((layout (classoid-layout type1
))
1312 (inherits (layout-inherits layout
))
1313 (sequencep (find (classoid-layout (find-classoid 'sequence
))
1315 (values (if sequencep t nil
) t
)))
1316 ((and (eq type2
*instance-type
*) (classoid-p type1
))
1317 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1319 (let* ((layout (classoid-layout type1
))
1320 (inherits (layout-inherits layout
))
1321 (functionp (find (classoid-layout (find-classoid 'function
))
1326 ((eq type1
(find-classoid 'function
))
1328 ((or (structure-classoid-p type1
)
1330 (condition-classoid-p type1
))
1332 (t (values nil nil
))))))
1333 ((and (eq type2
*funcallable-instance-type
*) (classoid-p type1
))
1334 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1336 (let* ((layout (classoid-layout type1
))
1337 (inherits (layout-inherits layout
))
1338 (functionp (find (classoid-layout (find-classoid 'function
))
1340 (values (if functionp t nil
) t
))))
1342 ;; FIXME: This seems to rely on there only being 4 or 5
1343 ;; NAMED-TYPE values, and the exclusion of various
1344 ;; possibilities above. It would be good to explain it and/or
1345 ;; rewrite it so that it's clearer.
1348 (!define-type-method
(named :complex-intersection2
) (type1 type2
)
1349 ;; FIXME: This assertion failed when I added it in sbcl-0.6.11.13.
1350 ;; Perhaps when bug 85 is fixed it can be reenabled.
1351 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1353 ((eq type2
*extended-sequence-type
*)
1355 (structure-classoid *empty-type
*)
1357 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1359 (if (find (classoid-layout (find-classoid 'sequence
))
1360 (layout-inherits (classoid-layout type1
)))
1364 (if (or (type-might-contain-other-types-p type1
)
1365 (member-type-p type1
))
1368 ((eq type2
*instance-type
*)
1370 (structure-classoid type1
)
1372 (if (and (not (member type1
*non-instance-classoid-types
*
1373 :key
#'find-classoid
))
1374 (not (eq type1
(find-classoid 'function
)))
1375 (not (find (classoid-layout (find-classoid 'function
))
1376 (layout-inherits (classoid-layout type1
)))))
1380 (if (or (type-might-contain-other-types-p type1
)
1381 (member-type-p type1
))
1384 ((eq type2
*funcallable-instance-type
*)
1386 (structure-classoid *empty-type
*)
1388 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1390 (if (find (classoid-layout (find-classoid 'function
))
1391 (layout-inherits (classoid-layout type1
)))
1393 (if (type= type1
(find-classoid 'function
))
1398 (if (or (type-might-contain-other-types-p type1
)
1399 (member-type-p type1
))
1402 (t (hierarchical-intersection2 type1 type2
))))
1404 (!define-type-method
(named :complex-union2
) (type1 type2
)
1405 ;; Perhaps when bug 85 is fixed this can be reenabled.
1406 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1408 ((eq type2
*extended-sequence-type
*)
1409 (if (classoid-p type1
)
1410 (if (or (member type1
*non-instance-classoid-types
*
1411 :key
#'find-classoid
)
1412 (not (find (classoid-layout (find-classoid 'sequence
))
1413 (layout-inherits (classoid-layout type1
)))))
1417 ((eq type2
*instance-type
*)
1418 (if (classoid-p type1
)
1419 (if (or (member type1
*non-instance-classoid-types
*
1420 :key
#'find-classoid
)
1421 (find (classoid-layout (find-classoid 'function
))
1422 (layout-inherits (classoid-layout type1
))))
1426 ((eq type2
*funcallable-instance-type
*)
1427 (if (classoid-p type1
)
1428 (if (or (member type1
*non-instance-classoid-types
*
1429 :key
#'find-classoid
)
1430 (not (find (classoid-layout (find-classoid 'function
))
1431 (layout-inherits (classoid-layout type1
)))))
1433 (if (eq type1
(specifier-type 'function
))
1437 (t (hierarchical-union2 type1 type2
))))
1439 (!define-type-method
(named :negate
) (x)
1440 (aver (not (eq x
*wild-type
*)))
1442 ((eq x
*universal-type
*) *empty-type
*)
1443 ((eq x
*empty-type
*) *universal-type
*)
1444 ((or (eq x
*instance-type
*)
1445 (eq x
*funcallable-instance-type
*)
1446 (eq x
*extended-sequence-type
*))
1447 (make-negation-type :type x
))
1448 (t (bug "NAMED type unexpected: ~S" x
))))
1450 (!define-type-method
(named :unparse
) (x)
1451 (named-type-name x
))
1453 ;;;; hairy and unknown types
1454 ;;;; DEFINE-TYPE-CLASS HAIRY is in 'early-type'
1456 (!define-type-method
(hairy :negate
) (x)
1457 (make-negation-type :type x
))
1459 (!define-type-method
(hairy :unparse
) (x)
1460 (hairy-type-specifier x
))
1462 (!define-type-method
(hairy :simple-subtypep
) (type1 type2
)
1463 (let ((hairy-spec1 (hairy-type-specifier type1
))
1464 (hairy-spec2 (hairy-type-specifier type2
)))
1465 (cond ((equal-but-no-car-recursion hairy-spec1 hairy-spec2
)
1467 ((maybe-reparse-specifier! type1
)
1468 (csubtypep type1 type2
))
1469 ((maybe-reparse-specifier! type2
)
1470 (csubtypep type1 type2
))
1472 (values nil nil
)))))
1474 (!define-type-method
(hairy :complex-subtypep-arg2
) (type1 type2
)
1475 (if (maybe-reparse-specifier! type2
)
1476 (csubtypep type1 type2
)
1477 (let ((specifier (hairy-type-specifier type2
)))
1478 (cond ((and (consp specifier
) (eql (car specifier
) 'satisfies
))
1479 (case (cadr specifier
)
1480 ((keywordp) (if (type= type1
(specifier-type 'symbol
))
1482 (invoke-complex-subtypep-arg1-method type1 type2
)))
1483 (t (invoke-complex-subtypep-arg1-method type1 type2
))))
1485 (invoke-complex-subtypep-arg1-method type1 type2
))))))
1487 (!define-type-method
(hairy :complex-subtypep-arg1
) (type1 type2
)
1488 (if (maybe-reparse-specifier! type1
)
1489 (csubtypep type1 type2
)
1492 (!define-type-method
(hairy :complex-
=) (type1 type2
)
1493 (if (maybe-reparse-specifier! type2
)
1497 (!define-type-method
(hairy :simple-intersection2
:complex-intersection2
)
1499 (acond ((type= type1 type2
)
1501 ((eq type2
*satisfies-keywordp-type
*)
1502 ;; (AND (MEMBER A) (SATISFIES KEYWORDP)) is possibly non-empty
1503 ;; if A is re-homed as :A. However as a special case that really
1504 ;; does occur, (AND (MEMBER NIL) (SATISFIES KEYWORDP))
1505 ;; is empty because of the illegality of changing NIL's package.
1506 (if (eq type1
*null-type
*)
1508 (multiple-value-bind (answer certain
)
1509 (types-equal-or-intersect type1
(specifier-type 'symbol
))
1510 (and (not answer
) certain
*empty-type
*))))
1511 ((eq type2
*fun-name-type
*)
1512 (multiple-value-bind (answer certain
)
1513 (types-equal-or-intersect type1
(specifier-type 'symbol
))
1516 (multiple-value-bind (answer certain
)
1517 (types-equal-or-intersect type1
(specifier-type 'cons
))
1518 (and (not answer
) certain
*empty-type
*)))))
1519 ((and (typep (hairy-type-specifier type2
) '(cons (eql satisfies
)))
1520 (info :function
:predicate-truth-constraint
1521 (cadr (hairy-type-specifier type2
))))
1522 (multiple-value-bind (answer certain
)
1523 (types-equal-or-intersect type1
(specifier-type it
))
1524 (and (not answer
) certain
*empty-type
*)))))
1526 (!define-type-method
(hairy :simple-union2
)
1528 (if (type= type1 type2
)
1532 (!define-type-method
(hairy :simple-
=) (type1 type2
)
1533 (if (equal-but-no-car-recursion (hairy-type-specifier type1
)
1534 (hairy-type-specifier type2
))
1538 (!def-type-translator satisfies
:list
(&whole whole predicate-name
)
1539 (unless (symbolp predicate-name
)
1540 (error 'simple-type-error
1541 :datum predicate-name
1542 :expected-type
'symbol
1543 :format-control
"The SATISFIES predicate name is not a symbol: ~S"
1544 :format-arguments
(list predicate-name
)))
1545 (case predicate-name
1546 (keywordp *satisfies-keywordp-type
*)
1547 (legal-fun-name-p *fun-name-type
*)
1548 (t (%make-hairy-type whole
))))
1552 (!define-type-method
(negation :negate
) (x)
1553 (negation-type-type x
))
1555 (!define-type-method
(negation :unparse
) (x)
1556 (if (type= (negation-type-type x
) (specifier-type 'cons
))
1558 `(not ,(type-specifier (negation-type-type x
)))))
1560 (!define-type-method
(negation :simple-subtypep
) (type1 type2
)
1561 (csubtypep (negation-type-type type2
) (negation-type-type type1
)))
1563 (!define-type-method
(negation :complex-subtypep-arg2
) (type1 type2
)
1564 (let* ((complement-type2 (negation-type-type type2
))
1565 (intersection2 (type-intersection2 type1
1568 ;; FIXME: if uncertain, maybe try arg1?
1569 (type= intersection2
*empty-type
*)
1570 (invoke-complex-subtypep-arg1-method type1 type2
))))
1572 (!define-type-method
(negation :complex-subtypep-arg1
) (type1 type2
)
1573 ;; "Incrementally extended heuristic algorithms tend inexorably toward the
1574 ;; incomprehensible." -- http://www.unlambda.com/~james/lambda/lambda.txt
1576 ;; You may not believe this. I couldn't either. But then I sat down
1577 ;; and drew lots of Venn diagrams. Comments involving a and b refer
1578 ;; to the call (subtypep '(not a) 'b) -- CSR, 2002-02-27.
1580 ;; (Several logical truths in this block are true as long as
1581 ;; b/=T. As of sbcl-0.7.1.28, it seems impossible to construct a
1582 ;; case with b=T where we actually reach this type method, but
1583 ;; we'll test for and exclude this case anyway, since future
1584 ;; maintenance might make it possible for it to end up in this
1586 (multiple-value-bind (equal certain
)
1587 (type= type2
*universal-type
*)
1589 (return (values nil nil
)))
1591 (return (values t t
))))
1592 (let ((complement-type1 (negation-type-type type1
)))
1593 ;; Do the special cases first, in order to give us a chance if
1594 ;; subtype/supertype relationships are hairy.
1595 (multiple-value-bind (equal certain
)
1596 (type= complement-type1 type2
)
1597 ;; If a = b, ~a is not a subtype of b (unless b=T, which was
1600 (return (values nil nil
)))
1602 (return (values nil t
))))
1603 ;; KLUDGE: ANSI requires that the SUBTYPEP result between any
1604 ;; two built-in atomic type specifiers never be uncertain. This
1605 ;; is hard to do cleanly for the built-in types whose
1606 ;; definitions include (NOT FOO), i.e. CONS and RATIO. However,
1607 ;; we can do it with this hack, which uses our global knowledge
1608 ;; that our implementation of the type system uses disjoint
1609 ;; implementation types to represent disjoint sets (except when
1610 ;; types are contained in other types). (This is a KLUDGE
1611 ;; because it's fragile. Various changes in internal
1612 ;; representation in the type system could make it start
1613 ;; confidently returning incorrect results.) -- WHN 2002-03-08
1614 (unless (or (type-might-contain-other-types-p complement-type1
)
1615 (type-might-contain-other-types-p type2
))
1616 ;; Because of the way our types which don't contain other
1617 ;; types are disjoint subsets of the space of possible values,
1618 ;; (SUBTYPEP '(NOT AA) 'B)=NIL when AA and B are simple (and B
1619 ;; is not T, as checked above).
1620 (return (values nil t
)))
1621 ;; The old (TYPE= TYPE1 TYPE2) branch would never be taken, as
1622 ;; TYPE1 and TYPE2 will only be equal if they're both NOT types,
1623 ;; and then the :SIMPLE-SUBTYPEP method would be used instead.
1624 ;; But a CSUBTYPEP relationship might still hold:
1625 (multiple-value-bind (equal certain
)
1626 (csubtypep complement-type1 type2
)
1627 ;; If a is a subtype of b, ~a is not a subtype of b (unless
1628 ;; b=T, which was excluded above).
1630 (return (values nil nil
)))
1632 (return (values nil t
))))
1633 (multiple-value-bind (equal certain
)
1634 (csubtypep type2 complement-type1
)
1635 ;; If b is a subtype of a, ~a is not a subtype of b. (FIXME:
1636 ;; That's not true if a=T. Do we know at this point that a is
1639 (return (values nil nil
)))
1641 (return (values nil t
))))
1642 ;; old CSR comment ca. 0.7.2, now obsoleted by the SIMPLE-CTYPE?
1643 ;; KLUDGE case above: Other cases here would rely on being able
1644 ;; to catch all possible cases, which the fragility of this type
1645 ;; system doesn't inspire me; for instance, if a is type= to ~b,
1646 ;; then we want T, T; if this is not the case and the types are
1647 ;; disjoint (have an intersection of *empty-type*) then we want
1648 ;; NIL, T; else if the union of a and b is the *universal-type*
1649 ;; then we want T, T. So currently we still claim to be unsure
1650 ;; about e.g. (subtypep '(not fixnum) 'single-float).
1652 ;; OTOH we might still get here:
1655 (!define-type-method
(negation :complex-
=) (type1 type2
)
1656 ;; (NOT FOO) isn't equivalent to anything that's not a negation
1657 ;; type, except possibly a type that might contain it in disguise.
1658 (declare (ignore type2
))
1659 (if (type-might-contain-other-types-p type1
)
1663 (!define-type-method
(negation :simple-intersection2
) (type1 type2
)
1664 (let ((not1 (negation-type-type type1
))
1665 (not2 (negation-type-type type2
)))
1667 ((csubtypep not1 not2
) type2
)
1668 ((csubtypep not2 not1
) type1
)
1669 ;; Why no analagous clause to the disjoint in the SIMPLE-UNION2
1670 ;; method, below? The clause would read
1672 ;; ((EQ (TYPE-UNION NOT1 NOT2) *UNIVERSAL-TYPE*) *EMPTY-TYPE*)
1674 ;; but with proper canonicalization of negation types, there's
1675 ;; no way of constructing two negation types with union of their
1676 ;; negations being the universal type.
1678 (aver (not (eq (type-union not1 not2
) *universal-type
*)))
1681 (defun maybe-complex-array-refinement (type1 type2
)
1682 (let* ((ntype (negation-type-type type2
))
1683 (ndims (array-type-dimensions ntype
))
1684 (ncomplexp (array-type-complexp ntype
))
1685 (nseltype (array-type-specialized-element-type ntype
))
1686 (neltype (array-type-element-type ntype
)))
1687 (if (and (eql ndims
'*) (null ncomplexp
)
1688 (eq neltype
*wild-type
*) (eq nseltype
*wild-type
*))
1689 (make-array-type (array-type-dimensions type1
)
1691 :element-type
(array-type-element-type type1
)
1692 :specialized-element-type
(array-type-specialized-element-type type1
)))))
1694 (!define-type-method
(negation :complex-intersection2
) (type1 type2
)
1696 ((csubtypep type1
(negation-type-type type2
)) *empty-type
*)
1697 ((eq (type-intersection type1
(negation-type-type type2
)) *empty-type
*)
1699 ((and (array-type-p type1
) (array-type-p (negation-type-type type2
)))
1700 (maybe-complex-array-refinement type1 type2
))
1703 (!define-type-method
(negation :simple-union2
) (type1 type2
)
1704 (let ((not1 (negation-type-type type1
))
1705 (not2 (negation-type-type type2
)))
1707 ((csubtypep not1 not2
) type1
)
1708 ((csubtypep not2 not1
) type2
)
1709 ((eq (type-intersection not1 not2
) *empty-type
*)
1713 (!define-type-method
(negation :complex-union2
) (type1 type2
)
1715 ((csubtypep (negation-type-type type2
) type1
) *universal-type
*)
1716 ((eq (type-intersection type1
(negation-type-type type2
)) *empty-type
*)
1720 (!define-type-method
(negation :simple-
=) (type1 type2
)
1721 (type= (negation-type-type type1
) (negation-type-type type2
)))
1723 (!def-type-translator not
:list
(typespec)
1724 (type-negation (specifier-type typespec
)))
1728 (!define-type-class number
:enumerable
#'numeric-type-enumerable
1729 :might-contain-other-types nil
)
1731 (declaim (inline numeric-type-equal
))
1732 (defun numeric-type-equal (type1 type2
)
1733 (and (eq (numeric-type-class type1
) (numeric-type-class type2
))
1734 (eq (numeric-type-format type1
) (numeric-type-format type2
))
1735 (eq (numeric-type-complexp type1
) (numeric-type-complexp type2
))))
1737 (!define-type-method
(number :simple-
=) (type1 type2
)
1739 (and (numeric-type-equal type1 type2
)
1740 (equalp (numeric-type-low type1
) (numeric-type-low type2
))
1741 (equalp (numeric-type-high type1
) (numeric-type-high type2
)))
1744 (!define-type-method
(number :negate
) (type)
1745 (if (and (null (numeric-type-low type
)) (null (numeric-type-high type
)))
1746 (make-negation-type :type type
)
1749 :type
(modified-numeric-type type
:low nil
:high nil
))
1751 ((null (numeric-type-low type
))
1752 (modified-numeric-type
1754 :low
(let ((h (numeric-type-high type
)))
1755 (if (consp h
) (car h
) (list h
)))
1757 ((null (numeric-type-high type
))
1758 (modified-numeric-type
1761 :high
(let ((l (numeric-type-low type
)))
1762 (if (consp l
) (car l
) (list l
)))))
1764 (modified-numeric-type
1767 :high
(let ((l (numeric-type-low type
)))
1768 (if (consp l
) (car l
) (list l
))))
1769 (modified-numeric-type
1771 :low
(let ((h (numeric-type-high type
)))
1772 (if (consp h
) (car h
) (list h
)))
1775 (!define-type-method
(number :unparse
) (type)
1776 (let* ((complexp (numeric-type-complexp type
))
1777 (low (numeric-type-low type
))
1778 (high (numeric-type-high type
))
1779 (base (case (numeric-type-class type
)
1781 (rational 'rational
)
1782 (float (or (numeric-type-format type
) 'float
))
1785 (cond ((and (eq base
'integer
) high low
)
1786 (let ((high-count (logcount high
))
1787 (high-length (integer-length high
)))
1789 (cond ((= high
0) '(integer 0 0))
1791 ((and (= high-count high-length
)
1792 (plusp high-length
))
1793 `(unsigned-byte ,high-length
))
1795 `(mod ,(1+ high
)))))
1796 ((and (= low sb
!xc
:most-negative-fixnum
)
1797 (= high sb
!xc
:most-positive-fixnum
))
1799 ((and (= low
(lognot high
))
1800 (= high-count high-length
)
1802 `(signed-byte ,(1+ high-length
)))
1804 `(integer ,low
,high
)))))
1805 (high `(,base
,(or low
'*) ,high
))
1807 (if (and (eq base
'integer
) (= low
0))
1815 (aver (neq base
+bounds
'real
))
1816 `(complex ,base
+bounds
))
1818 (aver (eq base
+bounds
'real
))
1821 (!define-type-method
(number :singleton-p
) (type)
1822 (let ((low (numeric-type-low type
))
1823 (high (numeric-type-high type
)))
1826 (eql (numeric-type-complexp type
) :real
)
1827 (member (numeric-type-class type
) '(integer rational
1828 #-sb-xc-host float
)))
1829 (values t
(numeric-type-low type
))
1832 ;;; Return true if X is "less than or equal" to Y, taking open bounds
1833 ;;; into consideration. CLOSED is the predicate used to test the bound
1834 ;;; on a closed interval (e.g. <=), and OPEN is the predicate used on
1835 ;;; open bounds (e.g. <). Y is considered to be the outside bound, in
1836 ;;; the sense that if it is infinite (NIL), then the test succeeds,
1837 ;;; whereas if X is infinite, then the test fails (unless Y is also
1840 ;;; This is for comparing bounds of the same kind, e.g. upper and
1841 ;;; upper. Use NUMERIC-BOUND-TEST* for different kinds of bounds.
1842 (defmacro numeric-bound-test
(x y closed open
)
1847 (,closed
(car ,x
) (car ,y
))
1848 (,closed
(car ,x
) ,y
)))
1854 ;;; This is used to compare upper and lower bounds. This is different
1855 ;;; from the same-bound case:
1856 ;;; -- Since X = NIL is -infinity, whereas y = NIL is +infinity, we
1857 ;;; return true if *either* arg is NIL.
1858 ;;; -- an open inner bound is "greater" and also squeezes the interval,
1859 ;;; causing us to use the OPEN test for those cases as well.
1860 (defmacro numeric-bound-test
* (x y closed open
)
1865 (,open
(car ,x
) (car ,y
))
1866 (,open
(car ,x
) ,y
)))
1872 ;;; Return whichever of the numeric bounds X and Y is "maximal"
1873 ;;; according to the predicates CLOSED (e.g. >=) and OPEN (e.g. >).
1874 ;;; This is only meaningful for maximizing like bounds, i.e. upper and
1875 ;;; upper. If MAX-P is true, then we return NIL if X or Y is NIL,
1876 ;;; otherwise we return the other arg.
1877 (defmacro numeric-bound-max
(x y closed open max-p
)
1880 `(cond ((not ,n-x
) ,(if max-p nil n-y
))
1881 ((not ,n-y
) ,(if max-p nil n-x
))
1884 (if (,closed
(car ,n-x
) (car ,n-y
)) ,n-x
,n-y
)
1885 (if (,open
(car ,n-x
) ,n-y
) ,n-x
,n-y
)))
1888 (if (,open
(car ,n-y
) ,n-x
) ,n-y
,n-x
)
1889 (if (,closed
,n-y
,n-x
) ,n-y
,n-x
))))))
1891 (!define-type-method
(number :simple-subtypep
) (type1 type2
)
1892 (let ((class1 (numeric-type-class type1
))
1893 (class2 (numeric-type-class type2
))
1894 (complexp2 (numeric-type-complexp type2
))
1895 (format2 (numeric-type-format type2
))
1896 (low1 (numeric-type-low type1
))
1897 (high1 (numeric-type-high type1
))
1898 (low2 (numeric-type-low type2
))
1899 (high2 (numeric-type-high type2
)))
1900 ;; If one is complex and the other isn't, they are disjoint.
1901 (cond ((not (or (eq (numeric-type-complexp type1
) complexp2
)
1904 ;; If the classes are specified and different, the types are
1905 ;; disjoint unless type2 is RATIONAL and type1 is INTEGER.
1906 ;; [ or type1 is INTEGER and type2 is of the form (RATIONAL
1907 ;; X X) for integral X, but this is dealt with in the
1908 ;; canonicalization inside MAKE-NUMERIC-TYPE ]
1909 ((not (or (eq class1 class2
)
1911 (and (eq class1
'integer
) (eq class2
'rational
))))
1913 ;; If the float formats are specified and different, the types
1915 ((not (or (eq (numeric-type-format type1
) format2
)
1918 ;; Check the bounds.
1919 ((and (numeric-bound-test low1 low2
>= >)
1920 (numeric-bound-test high1 high2
<= <))
1925 (!define-superclasses number
((number)) !cold-init-forms
)
1927 ;;; If the high bound of LOW is adjacent to the low bound of HIGH,
1928 ;;; then return true, otherwise NIL.
1929 (defun numeric-types-adjacent (low high
)
1930 (let ((low-bound (numeric-type-high low
))
1931 (high-bound (numeric-type-low high
)))
1932 (cond ((not (and low-bound high-bound
)) nil
)
1933 ((and (consp low-bound
) (consp high-bound
)) nil
)
1935 (let ((low-value (car low-bound
)))
1936 (or (eql low-value high-bound
)
1938 (load-time-value (make-unportable-float
1939 :single-float-negative-zero
)))
1940 (eql high-bound
0f0
))
1941 (and (eql low-value
0f0
)
1943 (load-time-value (make-unportable-float
1944 :single-float-negative-zero
))))
1946 (load-time-value (make-unportable-float
1947 :double-float-negative-zero
)))
1948 (eql high-bound
0d0
))
1949 (and (eql low-value
0d0
)
1951 (load-time-value (make-unportable-float
1952 :double-float-negative-zero
)))))))
1954 (let ((high-value (car high-bound
)))
1955 (or (eql high-value low-bound
)
1956 (and (eql high-value
1957 (load-time-value (make-unportable-float
1958 :single-float-negative-zero
)))
1959 (eql low-bound
0f0
))
1960 (and (eql high-value
0f0
)
1962 (load-time-value (make-unportable-float
1963 :single-float-negative-zero
))))
1964 (and (eql high-value
1965 (load-time-value (make-unportable-float
1966 :double-float-negative-zero
)))
1967 (eql low-bound
0d0
))
1968 (and (eql high-value
0d0
)
1970 (load-time-value (make-unportable-float
1971 :double-float-negative-zero
)))))))
1972 ((and (eq (numeric-type-class low
) 'integer
)
1973 (eq (numeric-type-class high
) 'integer
))
1974 (eql (1+ low-bound
) high-bound
))
1978 ;;; Return a numeric type that is a supertype for both TYPE1 and TYPE2.
1980 ;;; Binding *APPROXIMATE-NUMERIC-UNIONS* to T allows merging non-adjacent
1981 ;;; numeric types, eg (OR (INTEGER 0 12) (INTEGER 20 128)) => (INTEGER 0 128),
1982 ;;; the compiler does this occasionally during type-derivation to avoid
1983 ;;; creating absurdly complex unions of numeric types.
1984 (defvar *approximate-numeric-unions
* nil
)
1986 (!define-type-method
(number :simple-union2
) (type1 type2
)
1987 (declare (type numeric-type type1 type2
))
1988 (cond ((csubtypep type1 type2
) type2
)
1989 ((csubtypep type2 type1
) type1
)
1991 (let ((class1 (numeric-type-class type1
))
1992 (format1 (numeric-type-format type1
))
1993 (complexp1 (numeric-type-complexp type1
))
1994 (class2 (numeric-type-class type2
))
1995 (format2 (numeric-type-format type2
))
1996 (complexp2 (numeric-type-complexp type2
)))
1998 ((and (eq class1 class2
)
1999 (eq format1 format2
)
2000 (eq complexp1 complexp2
)
2001 (or *approximate-numeric-unions
*
2002 (numeric-types-intersect type1 type2
)
2003 (numeric-types-adjacent type1 type2
)
2004 (numeric-types-adjacent type2 type1
)))
2009 :low
(numeric-bound-max (numeric-type-low type1
)
2010 (numeric-type-low type2
)
2012 :high
(numeric-bound-max (numeric-type-high type1
)
2013 (numeric-type-high type2
)
2015 ;; FIXME: These two clauses are almost identical, and the
2016 ;; consequents are in fact identical in every respect.
2017 ((and (eq class1
'rational
)
2018 (eq class2
'integer
)
2019 (eq format1 format2
)
2020 (eq complexp1 complexp2
)
2021 (integerp (numeric-type-low type2
))
2022 (integerp (numeric-type-high type2
))
2023 (= (numeric-type-low type2
) (numeric-type-high type2
))
2024 (or *approximate-numeric-unions
*
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 ((and (eq class1
'integer
)
2038 (eq class2
'rational
)
2039 (eq format1 format2
)
2040 (eq complexp1 complexp2
)
2041 (integerp (numeric-type-low type1
))
2042 (integerp (numeric-type-high type1
))
2043 (= (numeric-type-low type1
) (numeric-type-high type1
))
2044 (or *approximate-numeric-unions
*
2045 (numeric-types-adjacent type1 type2
)
2046 (numeric-types-adjacent type2 type1
)))
2051 :low
(numeric-bound-max (numeric-type-low type1
)
2052 (numeric-type-low type2
)
2054 :high
(numeric-bound-max (numeric-type-high type1
)
2055 (numeric-type-high type2
)
2060 (!cold-init-forms
;; is !PRECOMPUTE-TYPES not doing the right thing?
2061 (setf (info :type
:kind
'number
) :primitive
)
2062 (setf (info :type
:builtin
'number
)
2063 (make-numeric-type :complexp nil
)))
2065 (!def-type-translator complex
(&optional
(typespec '*))
2066 (if (eq typespec
'*)
2067 (specifier-type '(complex real
))
2068 (labels ((not-numeric ()
2069 (error "The component type for COMPLEX is not numeric: ~S"
2072 (error "The component type for COMPLEX is not a subtype of REAL: ~S"
2074 (complex1 (component-type)
2075 (unless (numeric-type-p component-type
)
2077 (when (eq (numeric-type-complexp component-type
) :complex
)
2079 (if (csubtypep component-type
(specifier-type '(eql 0)))
2081 (modified-numeric-type component-type
2082 :complexp
:complex
)))
2085 ((eq ctype
*empty-type
*) *empty-type
*)
2086 ((eq ctype
*universal-type
*) (not-real))
2087 ((typep ctype
'numeric-type
) (complex1 ctype
))
2088 ((typep ctype
'union-type
)
2090 (mapcar #'do-complex
(union-type-types ctype
))))
2091 ((typep ctype
'member-type
)
2093 (mapcar-member-type-members
2094 (lambda (x) (do-complex (ctype-of x
)))
2096 ((and (typep ctype
'intersection-type
)
2097 ;; FIXME: This is very much a
2098 ;; not-quite-worst-effort, but we are required to do
2099 ;; something here because of our representation of
2100 ;; RATIO as (AND RATIONAL (NOT INTEGER)): we must
2101 ;; allow users to ask about (COMPLEX RATIO). This
2102 ;; will of course fail to work right on such types
2103 ;; as (AND INTEGER (SATISFIES ZEROP))...
2104 (let ((numbers (remove-if-not
2106 (intersection-type-types ctype
))))
2108 (null (cdr numbers
))
2109 (eq (numeric-type-complexp (car numbers
)) :real
)
2110 (complex1 (car numbers
))))))
2112 (multiple-value-bind (subtypep certainly
)
2113 (csubtypep ctype
(specifier-type 'real
))
2114 (if (and (not subtypep
) certainly
)
2116 ;; ANSI just says that TYPESPEC is any subtype of
2117 ;; type REAL, not necessarily a NUMERIC-TYPE. In
2118 ;; particular, at this point TYPESPEC could legally
2119 ;; be a hairy type like (AND NUMBER (SATISFIES
2120 ;; REALP) (SATISFIES ZEROP)), in which case we fall
2121 ;; through the logic above and end up here,
2123 ;; FIXME: (COMPLEX NUMBER) is not rejected but should
2124 ;; be, as NUMBER is clearly not a subtype of real.
2125 (bug "~@<(known bug #145): The type ~S is too hairy to be ~
2126 used for a COMPLEX component.~:@>"
2128 (let ((ctype (specifier-type typespec
)))
2129 (do-complex ctype
)))))
2131 ;;; If X is *, return NIL, otherwise return the bound, which must be a
2132 ;;; member of TYPE or a one-element list of a member of TYPE.
2133 #!-sb-fluid
(declaim (inline canonicalized-bound
))
2134 (defun canonicalized-bound (bound type
)
2135 (cond ((eq bound
'*) nil
)
2136 ((or (sb!xc
:typep bound type
)
2138 (sb!xc
:typep
(car bound
) type
)
2139 (null (cdr bound
))))
2142 (error "Bound is not ~S, a ~S or a list of a ~S: ~S"
2148 (!def-type-translator integer
(&optional
(low '*) (high '*))
2149 (let* ((l (canonicalized-bound low
'integer
))
2150 (lb (if (consp l
) (1+ (car l
)) l
))
2151 (h (canonicalized-bound high
'integer
))
2152 (hb (if (consp h
) (1- (car h
)) h
)))
2153 (if (and hb lb
(< hb lb
))
2155 (make-numeric-type :class
'integer
2157 :enumerable
(not (null (and l h
)))
2161 (defmacro !def-bounded-type
(type class format
)
2162 `(!def-type-translator
,type
(&optional
(low '*) (high '*))
2163 (let ((lb (canonicalized-bound low
',type
))
2164 (hb (canonicalized-bound high
',type
)))
2165 (if (not (numeric-bound-test* lb hb
<= <))
2167 (make-numeric-type :class
',class
2172 (!def-bounded-type rational rational nil
)
2174 ;;; Unlike CMU CL, we represent the types FLOAT and REAL as
2175 ;;; UNION-TYPEs of more primitive types, in order to make
2176 ;;; type representation more unique, avoiding problems in the
2177 ;;; simplification of things like
2178 ;;; (subtypep '(or (single-float -1.0 1.0) (single-float 0.1))
2179 ;;; '(or (real -1 7) (single-float 0.1) (single-float -1.0 1.0)))
2180 ;;; When we allowed REAL to remain as a separate NUMERIC-TYPE,
2181 ;;; it was too easy for the first argument to be simplified to
2182 ;;; '(SINGLE-FLOAT -1.0), and for the second argument to be simplified
2183 ;;; to '(OR (REAL -1 7) (SINGLE-FLOAT 0.1)) and then for the
2184 ;;; SUBTYPEP to fail (returning NIL,T instead of T,T) because
2185 ;;; the first argument can't be seen to be a subtype of any of the
2186 ;;; terms in the second argument.
2188 ;;; The old CMU CL way was:
2189 ;;; (!def-bounded-type float float nil)
2190 ;;; (!def-bounded-type real nil nil)
2192 ;;; FIXME: If this new way works for a while with no weird new
2193 ;;; problems, we can go back and rip out support for separate FLOAT
2194 ;;; and REAL flavors of NUMERIC-TYPE. The new way was added in
2195 ;;; sbcl-0.6.11.22, 2001-03-21.
2197 ;;; FIXME: It's probably necessary to do something to fix the
2198 ;;; analogous problem with INTEGER and RATIONAL types. Perhaps
2199 ;;; bounded RATIONAL types should be represented as (OR RATIO INTEGER).
2200 (defun coerce-bound (bound type upperp inner-coerce-bound-fun
)
2201 (declare (type function inner-coerce-bound-fun
))
2204 (funcall inner-coerce-bound-fun bound type upperp
)))
2205 (defun inner-coerce-real-bound (bound type upperp
)
2206 #+sb-xc-host
(declare (ignore upperp
))
2207 (let #+sb-xc-host
()
2209 ((nl (load-time-value (symbol-value 'sb
!xc
:most-negative-long-float
)))
2210 (pl (load-time-value (symbol-value 'sb
!xc
:most-positive-long-float
))))
2211 (let ((nbound (if (consp bound
) (car bound
) bound
))
2212 (consp (consp bound
)))
2216 (list (rational nbound
))
2220 ((floatp nbound
) bound
)
2222 ;; Coerce to the widest float format available, to avoid
2223 ;; unnecessary loss of precision, but don't coerce
2224 ;; unrepresentable numbers, except on the host where we
2225 ;; shouldn't be making these types (but KLUDGE: can't even
2226 ;; assert portably that we're not).
2230 (when (< nbound nl
) (return-from inner-coerce-real-bound nl
)))
2232 (when (> nbound pl
) (return-from inner-coerce-real-bound pl
))))
2233 (let ((result (coerce nbound
'long-float
)))
2234 (if consp
(list result
) result
)))))))))
2235 (defun inner-coerce-float-bound (bound type upperp
)
2236 #+sb-xc-host
(declare (ignore upperp
))
2237 (let #+sb-xc-host
()
2239 ((nd (load-time-value (symbol-value 'sb
!xc
:most-negative-double-float
)))
2240 (pd (load-time-value (symbol-value 'sb
!xc
:most-positive-double-float
)))
2241 (ns (load-time-value (symbol-value 'sb
!xc
:most-negative-single-float
)))
2242 (ps (load-time-value
2243 (symbol-value 'sb
!xc
:most-positive-single-float
))))
2244 (let ((nbound (if (consp bound
) (car bound
) bound
))
2245 (consp (consp bound
)))
2249 ((typep nbound
'single-float
) bound
)
2254 (when (< nbound ns
) (return-from inner-coerce-float-bound ns
)))
2256 (when (> nbound ps
) (return-from inner-coerce-float-bound ps
))))
2257 (let ((result (coerce nbound
'single-float
)))
2258 (if consp
(list result
) result
)))))
2261 ((typep nbound
'double-float
) bound
)
2266 (when (< nbound nd
) (return-from inner-coerce-float-bound nd
)))
2268 (when (> nbound pd
) (return-from inner-coerce-float-bound pd
))))
2269 (let ((result (coerce nbound
'double-float
)))
2270 (if consp
(list result
) result
)))))))))
2271 (defun coerced-real-bound (bound type upperp
)
2272 (coerce-bound bound type upperp
#'inner-coerce-real-bound
))
2273 (defun coerced-float-bound (bound type upperp
)
2274 (coerce-bound bound type upperp
#'inner-coerce-float-bound
))
2275 (!def-type-translator real
(&optional
(low '*) (high '*))
2276 (specifier-type `(or (float ,(coerced-real-bound low
'float nil
)
2277 ,(coerced-real-bound high
'float t
))
2278 (rational ,(coerced-real-bound low
'rational nil
)
2279 ,(coerced-real-bound high
'rational t
)))))
2280 (!def-type-translator float
(&optional
(low '*) (high '*))
2282 `(or (single-float ,(coerced-float-bound low
'single-float nil
)
2283 ,(coerced-float-bound high
'single-float t
))
2284 (double-float ,(coerced-float-bound low
'double-float nil
)
2285 ,(coerced-float-bound high
'double-float t
))
2286 #!+long-float
,(error "stub: no long float support yet"))))
2288 (defmacro !define-float-format
(f)
2289 `(!def-bounded-type
,f float
,f
))
2291 ;; (!define-float-format short-float) ; it's a DEFTYPE
2292 (!define-float-format single-float
)
2293 (!define-float-format double-float
)
2294 ;; long-float support is dead.
2295 ;; (!define-float-format long-float) ; also a DEFTYPE
2297 (defun numeric-types-intersect (type1 type2
)
2298 (declare (type numeric-type type1 type2
))
2299 (let* ((class1 (numeric-type-class type1
))
2300 (class2 (numeric-type-class type2
))
2301 (complexp1 (numeric-type-complexp type1
))
2302 (complexp2 (numeric-type-complexp type2
))
2303 (format1 (numeric-type-format type1
))
2304 (format2 (numeric-type-format type2
))
2305 (low1 (numeric-type-low type1
))
2306 (high1 (numeric-type-high type1
))
2307 (low2 (numeric-type-low type2
))
2308 (high2 (numeric-type-high type2
)))
2309 ;; If one is complex and the other isn't, then they are disjoint.
2310 (cond ((not (or (eq complexp1 complexp2
)
2311 (null complexp1
) (null complexp2
)))
2313 ;; If either type is a float, then the other must either be
2314 ;; specified to be a float or unspecified. Otherwise, they
2316 ((and (eq class1
'float
)
2317 (not (member class2
'(float nil
)))) nil
)
2318 ((and (eq class2
'float
)
2319 (not (member class1
'(float nil
)))) nil
)
2320 ;; If the float formats are specified and different, the
2321 ;; types are disjoint.
2322 ((not (or (eq format1 format2
) (null format1
) (null format2
)))
2325 ;; Check the bounds. This is a bit odd because we must
2326 ;; always have the outer bound of the interval as the
2328 (if (numeric-bound-test high1 high2
<= <)
2329 (or (and (numeric-bound-test low1 low2
>= >)
2330 (numeric-bound-test* low1 high2
<= <))
2331 (and (numeric-bound-test low2 low1
>= >)
2332 (numeric-bound-test* low2 high1
<= <)))
2333 (or (and (numeric-bound-test* low2 high1
<= <)
2334 (numeric-bound-test low2 low1
>= >))
2335 (and (numeric-bound-test high2 high1
<= <)
2336 (numeric-bound-test* high2 low1
>= >))))))))
2338 ;;; Take the numeric bound X and convert it into something that can be
2339 ;;; used as a bound in a numeric type with the specified CLASS and
2340 ;;; FORMAT. If UP-P is true, then we round up as needed, otherwise we
2341 ;;; round down. UP-P true implies that X is a lower bound, i.e. (N) > N.
2343 ;;; This is used by NUMERIC-TYPE-INTERSECTION to mash the bound into
2344 ;;; the appropriate type number. X may only be a float when CLASS is
2347 ;;; ### Note: it is possible for the coercion to a float to overflow
2348 ;;; or underflow. This happens when the bound doesn't fit in the
2349 ;;; specified format. In this case, we should really return the
2350 ;;; appropriate {Most | Least}-{Positive | Negative}-XXX-Float float
2351 ;;; of desired format. But these conditions aren't currently signalled
2352 ;;; in any useful way.
2354 ;;; Also, when converting an open rational bound into a float we
2355 ;;; should probably convert it to a closed bound of the closest float
2356 ;;; in the specified format. KLUDGE: In general, open float bounds are
2357 ;;; screwed up. -- (comment from original CMU CL)
2358 (defun round-numeric-bound (x class format up-p
)
2360 (let ((cx (if (consp x
) (car x
) x
)))
2364 (if (and (consp x
) (integerp cx
))
2365 (if up-p
(1+ cx
) (1- cx
))
2366 (if up-p
(ceiling cx
) (floor cx
))))
2370 ((and format
(subtypep format
'double-float
))
2371 (if (<= most-negative-double-float cx most-positive-double-float
)
2375 (if (<= most-negative-single-float cx most-positive-single-float
)
2377 (coerce cx
(or format
'single-float
))
2379 (if (consp x
) (list res
) res
)))))
2382 ;;; Handle the case of type intersection on two numeric types. We use
2383 ;;; TYPES-EQUAL-OR-INTERSECT to throw out the case of types with no
2384 ;;; intersection. If an attribute in TYPE1 is unspecified, then we use
2385 ;;; TYPE2's attribute, which must be at least as restrictive. If the
2386 ;;; types intersect, then the only attributes that can be specified
2387 ;;; and different are the class and the bounds.
2389 ;;; When the class differs, we use the more restrictive class. The
2390 ;;; only interesting case is RATIONAL/INTEGER, since RATIONAL includes
2393 ;;; We make the result lower (upper) bound the maximum (minimum) of
2394 ;;; the argument lower (upper) bounds. We convert the bounds into the
2395 ;;; appropriate numeric type before maximizing. This avoids possible
2396 ;;; confusion due to mixed-type comparisons (but I think the result is
2398 (!define-type-method
(number :simple-intersection2
) (type1 type2
)
2399 (declare (type numeric-type type1 type2
))
2400 (if (numeric-types-intersect type1 type2
)
2401 (let* ((class1 (numeric-type-class type1
))
2402 (class2 (numeric-type-class type2
))
2403 (class (ecase class1
2405 ((integer float
) class1
)
2406 (rational (if (eq class2
'integer
)
2409 (format (or (numeric-type-format type1
)
2410 (numeric-type-format type2
))))
2414 :complexp
(or (numeric-type-complexp type1
)
2415 (numeric-type-complexp type2
))
2416 :low
(numeric-bound-max
2417 (round-numeric-bound (numeric-type-low type1
)
2419 (round-numeric-bound (numeric-type-low type2
)
2422 :high
(numeric-bound-max
2423 (round-numeric-bound (numeric-type-high type1
)
2425 (round-numeric-bound (numeric-type-high type2
)
2430 ;;; Given two float formats, return the one with more precision. If
2431 ;;; either one is null, return NIL.
2432 (defun float-format-max (f1 f2
)
2434 (dolist (f *float-formats
* (error "bad float format: ~S" f1
))
2435 (when (or (eq f f1
) (eq f f2
))
2438 ;;; Return the result of an operation on TYPE1 and TYPE2 according to
2439 ;;; the rules of numeric contagion. This is always NUMBER, some float
2440 ;;; format (possibly complex) or RATIONAL. Due to rational
2441 ;;; canonicalization, there isn't much we can do here with integers or
2442 ;;; rational complex numbers.
2444 ;;; If either argument is not a NUMERIC-TYPE, then return NUMBER. This
2445 ;;; is useful mainly for allowing types that are technically numbers,
2446 ;;; but not a NUMERIC-TYPE.
2447 (defun numeric-contagion (type1 type2
)
2448 (if (and (numeric-type-p type1
) (numeric-type-p type2
))
2449 (let ((class1 (numeric-type-class type1
))
2450 (class2 (numeric-type-class type2
))
2451 (format1 (numeric-type-format type1
))
2452 (format2 (numeric-type-format type2
))
2453 (complexp1 (numeric-type-complexp type1
))
2454 (complexp2 (numeric-type-complexp type2
)))
2455 (cond ((or (null complexp1
)
2457 (specifier-type 'number
))
2461 :format
(ecase class2
2462 (float (float-format-max format1 format2
))
2463 ((integer rational
) format1
)
2465 ;; A double-float with any real number is a
2468 (if (eq format1
'double-float
)
2471 ;; A long-float with any real number is a
2474 (if (eq format1
'long-float
)
2477 :complexp
(if (or (eq complexp1
:complex
)
2478 (eq complexp2
:complex
))
2481 ((eq class2
'float
) (numeric-contagion type2 type1
))
2482 ((and (eq complexp1
:real
) (eq complexp2
:real
))
2484 :class
(and class1 class2
'rational
)
2487 (specifier-type 'number
))))
2488 (specifier-type 'number
)))
2492 (!define-type-class array
:enumerable nil
2493 :might-contain-other-types nil
)
2495 (!define-type-method
(array :simple-
=) (type1 type2
)
2496 (cond ((not (and (equal (array-type-dimensions type1
)
2497 (array-type-dimensions type2
))
2498 (eq (array-type-complexp type1
)
2499 (array-type-complexp type2
))))
2501 ((or (unknown-type-p (array-type-element-type type1
))
2502 (unknown-type-p (array-type-element-type type2
)))
2503 (type= (array-type-element-type type1
)
2504 (array-type-element-type type2
)))
2506 (values (type= (array-type-specialized-element-type type1
)
2507 (array-type-specialized-element-type type2
))
2510 (!define-type-method
(array :negate
) (type)
2511 ;; FIXME (and hint to PFD): we're vulnerable here to attacks of the
2512 ;; form "are (AND ARRAY (NOT (ARRAY T))) and (OR (ARRAY BIT) (ARRAY
2513 ;; NIL) (ARRAY CHAR) ...) equivalent?" -- CSR, 2003-12-10
2514 ;; A symptom of the aforementioned is that the following are not TYPE=
2515 ;; (AND (VECTOR T) (NOT SIMPLE-ARRAY)) ; an ARRAY-TYPE
2516 ;; (AND (VECTOR T) (NOT SIMPLE-VECTOR)) ; an INTERSECTION-TYPE
2517 ;; even though (VECTOR T) makes it so that the (NOT) clause in each can
2518 ;; only provide one additional bit of information: that the vector
2519 ;; is complex as opposed to simple. The rank and element-type are fixed.
2520 (if (and (eq (array-type-dimensions type
) '*)
2521 (eq (array-type-complexp type
) 't
)
2522 (eq (array-type-element-type type
) *wild-type
*))
2523 ;; (NOT <hairy-array>) = either SIMPLE-ARRAY or (NOT ARRAY).
2524 ;; This is deliberately asymmetric - trying to say that NOT simple-array
2525 ;; equals hairy-array leads to infinite recursion.
2526 (type-union (make-array-type '* :complexp nil
2527 :element-type
*wild-type
*)
2529 :type
(make-array-type '* :element-type
*wild-type
*)))
2530 (make-negation-type :type type
)))
2532 (!define-type-method
(array :unparse
) (type)
2533 (let* ((dims (array-type-dimensions type
))
2534 ;; Compare the specialised element type and the
2535 ;; derived element type. If the derived type
2536 ;; is so small that it jumps to a smaller upgraded
2537 ;; element type, use the specialised element type.
2539 ;; This protects from unparsing
2540 ;; (and (vector (or bit symbol))
2541 ;; (vector (or bit character)))
2542 ;; i.e., the intersection of two T array types,
2544 (stype (array-type-specialized-element-type type
))
2545 (dtype (array-type-element-type type
))
2546 (utype (%upgraded-array-element-type dtype
))
2547 (eltype (type-specifier (if (type= stype utype
)
2550 (complexp (array-type-complexp type
)))
2551 (if (and (eq complexp t
) (not *unparse-allow-negation
*))
2552 (setq complexp
:maybe
))
2556 ((t) '(and array
(not simple-array
)))
2558 ((nil) 'simple-array
))
2560 ((t) `(and (array ,eltype
) (not simple-array
)))
2561 ((:maybe
) `(array ,eltype
))
2562 ((nil) `(simple-array ,eltype
)))))
2563 ((= (length dims
) 1)
2566 (if (eq (car dims
) '*)
2569 ((base-char #!-sb-unicode character
) 'base-string
)
2571 (t `(vector ,eltype
)))
2573 (bit `(bit-vector ,(car dims
)))
2574 ((base-char #!-sb-unicode character
)
2575 `(base-string ,(car dims
)))
2576 (t `(vector ,eltype
,(car dims
)))))))
2577 (if (eql complexp
:maybe
)
2579 `(and ,answer
(not simple-array
))))
2580 (if (eq (car dims
) '*)
2582 (bit 'simple-bit-vector
)
2583 ((base-char #!-sb-unicode character
) 'simple-base-string
)
2584 ((t) 'simple-vector
)
2585 (t `(simple-array ,eltype
(*))))
2587 (bit `(simple-bit-vector ,(car dims
)))
2588 ((base-char #!-sb-unicode character
)
2589 `(simple-base-string ,(car dims
)))
2590 ((t) `(simple-vector ,(car dims
)))
2591 (t `(simple-array ,eltype
,dims
))))))
2594 ((t) `(and (array ,eltype
,dims
) (not simple-array
)))
2595 ((:maybe
) `(array ,eltype
,dims
))
2596 ((nil) `(simple-array ,eltype
,dims
)))))))
2598 (!define-type-method
(array :simple-subtypep
) (type1 type2
)
2599 (let ((dims1 (array-type-dimensions type1
))
2600 (dims2 (array-type-dimensions type2
))
2601 (complexp2 (array-type-complexp type2
)))
2602 (cond (;; not subtypep unless dimensions are compatible
2603 (not (or (eq dims2
'*)
2604 (and (not (eq dims1
'*))
2605 ;; (sbcl-0.6.4 has trouble figuring out that
2606 ;; DIMS1 and DIMS2 must be lists at this
2607 ;; point, and knowing that is important to
2608 ;; compiling EVERY efficiently.)
2609 (= (length (the list dims1
))
2610 (length (the list dims2
)))
2611 (every (lambda (x y
)
2612 (or (eq y
'*) (eql x y
)))
2614 (the list dims2
)))))
2616 ;; not subtypep unless complexness is compatible
2617 ((not (or (eq complexp2
:maybe
)
2618 (eq (array-type-complexp type1
) complexp2
)))
2620 ;; Since we didn't fail any of the tests above, we win
2621 ;; if the TYPE2 element type is wild.
2622 ((eq (array-type-element-type type2
) *wild-type
*)
2624 (;; Since we didn't match any of the special cases above, if
2625 ;; either element type is unknown we can only give a good
2626 ;; answer if they are the same.
2627 (or (unknown-type-p (array-type-element-type type1
))
2628 (unknown-type-p (array-type-element-type type2
)))
2629 (if (type= (array-type-element-type type1
)
2630 (array-type-element-type type2
))
2633 (;; Otherwise, the subtype relationship holds iff the
2634 ;; types are equal, and they're equal iff the specialized
2635 ;; element types are identical.
2637 (values (type= (array-type-specialized-element-type type1
)
2638 (array-type-specialized-element-type type2
))
2641 (!define-superclasses array
2642 ((vector vector
) (array))
2645 (defun array-types-intersect (type1 type2
)
2646 (declare (type array-type type1 type2
))
2647 (let ((dims1 (array-type-dimensions type1
))
2648 (dims2 (array-type-dimensions type2
))
2649 (complexp1 (array-type-complexp type1
))
2650 (complexp2 (array-type-complexp type2
)))
2651 ;; See whether dimensions are compatible.
2652 (cond ((not (or (eq dims1
'*) (eq dims2
'*)
2653 (and (= (length dims1
) (length dims2
))
2654 (every (lambda (x y
)
2655 (or (eq x
'*) (eq y
'*) (= x y
)))
2658 ;; See whether complexpness is compatible.
2659 ((not (or (eq complexp1
:maybe
)
2660 (eq complexp2
:maybe
)
2661 (eq complexp1 complexp2
)))
2665 ;; If either element type is wild, then they intersect.
2666 ;; Otherwise, the types must be identical.
2668 ;; FIXME: There seems to have been a fair amount of
2669 ;; confusion about the distinction between requested element
2670 ;; type and specialized element type; here is one of
2671 ;; them. If we request an array to hold objects of an
2672 ;; unknown type, we can do no better than represent that
2673 ;; type as an array specialized on wild-type. We keep the
2674 ;; requested element-type in the -ELEMENT-TYPE slot, and
2675 ;; *WILD-TYPE* in the -SPECIALIZED-ELEMENT-TYPE. So, here,
2676 ;; we must test for the SPECIALIZED slot being *WILD-TYPE*,
2677 ;; not just the ELEMENT-TYPE slot. Maybe the return value
2678 ;; in that specific case should be T, NIL? Or maybe this
2679 ;; function should really be called
2680 ;; ARRAY-TYPES-COULD-POSSIBLY-INTERSECT? In any case, this
2681 ;; was responsible for bug #123, and this whole issue could
2682 ;; do with a rethink and/or a rewrite. -- CSR, 2002-08-21
2683 ((or (eq (array-type-specialized-element-type type1
) *wild-type
*)
2684 (eq (array-type-specialized-element-type type2
) *wild-type
*)
2685 (type= (array-type-specialized-element-type type1
)
2686 (array-type-specialized-element-type type2
)))
2692 (defun unite-array-types-complexp (type1 type2
)
2693 (let ((complexp1 (array-type-complexp type1
))
2694 (complexp2 (array-type-complexp type2
)))
2696 ((eq complexp1 complexp2
)
2697 ;; both types are the same complexp-ity
2698 (values complexp1 t
))
2699 ((eq complexp1
:maybe
)
2700 ;; type1 is wild-complexp
2701 (values :maybe type1
))
2702 ((eq complexp2
:maybe
)
2703 ;; type2 is wild-complexp
2704 (values :maybe type2
))
2706 ;; both types partition the complexp-space
2707 (values :maybe nil
)))))
2709 (defun unite-array-types-dimensions (type1 type2
)
2710 (let ((dims1 (array-type-dimensions type1
))
2711 (dims2 (array-type-dimensions type2
)))
2712 (cond ((equal dims1 dims2
)
2713 ;; both types are same dimensionality
2716 ;; type1 is wild-dimensions
2719 ;; type2 is wild-dimensions
2721 ((not (= (length dims1
) (length dims2
)))
2722 ;; types have different number of dimensions
2723 (values :incompatible nil
))
2725 ;; we need to check on a per-dimension basis
2726 (let* ((supertype1 t
)
2729 (result (mapcar (lambda (dim1 dim2
)
2734 (setf supertype2 nil
)
2737 (setf supertype1 nil
)
2740 (setf compatible nil
))))
2743 ((or (not compatible
)
2744 (and (not supertype1
)
2746 (values :incompatible nil
))
2747 ((and supertype1 supertype2
)
2748 (values result supertype1
))
2750 (values result
(if supertype1 type1 type2
)))))))))
2752 (defun unite-array-types-element-types (type1 type2
)
2753 ;; FIXME: We'd love to be able to unite the full set of specialized
2754 ;; array element types up to *wild-type*, but :simple-union2 is
2755 ;; performed pairwise, so we don't have a good hook for it and our
2756 ;; representation doesn't allow us to easily detect the situation
2758 ;; But see SIMPLIFY-ARRAY-UNIONS which is able to do something like that.
2759 (let* ((eltype1 (array-type-element-type type1
))
2760 (eltype2 (array-type-element-type type2
))
2761 (stype1 (array-type-specialized-element-type type1
))
2762 (stype2 (array-type-specialized-element-type type2
))
2763 (wild1 (eq eltype1
*wild-type
*))
2764 (wild2 (eq eltype2
*wild-type
*)))
2766 ((type= eltype1 eltype2
)
2767 (values eltype1 stype1 t
))
2769 (values eltype1 stype1 type1
))
2771 (values eltype2 stype2 type2
))
2772 ((not (type= stype1 stype2
))
2773 ;; non-wild types that don't share UAET don't unite
2774 (values :incompatible nil nil
))
2775 ((csubtypep eltype1 eltype2
)
2776 (values eltype2 stype2 type2
))
2777 ((csubtypep eltype2 eltype1
)
2778 (values eltype1 stype1 type1
))
2780 (values :incompatible nil nil
)))))
2782 (defun unite-array-types-supertypes-compatible-p (&rest supertypes
)
2783 ;; supertypes are compatible if they are all T, if there is a single
2784 ;; NIL and all the rest are T, or if all non-T supertypes are the
2785 ;; same and not NIL.
2786 (let ((interesting-supertypes
2787 (remove t supertypes
)))
2788 (or (not interesting-supertypes
)
2789 (equal interesting-supertypes
'(nil))
2790 ;; supertypes are (OR BOOLEAN ARRAY-TYPE), so...
2791 (typep (remove-duplicates interesting-supertypes
)
2792 '(cons array-type null
)))))
2794 (!define-type-method
(array :simple-union2
) (type1 type2
)
2795 (multiple-value-bind
2796 (result-eltype result-stype eltype-supertype
)
2797 (unite-array-types-element-types type1 type2
)
2798 (multiple-value-bind
2799 (result-complexp complexp-supertype
)
2800 (unite-array-types-complexp type1 type2
)
2801 (multiple-value-bind
2802 (result-dimensions dimensions-supertype
)
2803 (unite-array-types-dimensions type1 type2
)
2804 (when (and (not (eq result-dimensions
:incompatible
))
2805 (not (eq result-eltype
:incompatible
))
2806 (unite-array-types-supertypes-compatible-p
2807 eltype-supertype complexp-supertype dimensions-supertype
))
2808 (make-array-type result-dimensions
2809 :complexp result-complexp
2810 :element-type result-eltype
2811 :specialized-element-type result-stype
))))))
2813 (!define-type-method
(array :simple-intersection2
) (type1 type2
)
2814 (declare (type array-type type1 type2
))
2815 (if (array-types-intersect type1 type2
)
2816 (let ((dims1 (array-type-dimensions type1
))
2817 (dims2 (array-type-dimensions type2
))
2818 (complexp1 (array-type-complexp type1
))
2819 (complexp2 (array-type-complexp type2
))
2820 (eltype1 (array-type-element-type type1
))
2821 (eltype2 (array-type-element-type type2
))
2822 (stype1 (array-type-specialized-element-type type1
))
2823 (stype2 (array-type-specialized-element-type type2
)))
2824 (make-array-type (cond ((eq dims1
'*) dims2
)
2825 ((eq dims2
'*) dims1
)
2827 (mapcar (lambda (x y
) (if (eq x
'*) y x
))
2829 :complexp
(if (eq complexp1
:maybe
) complexp2 complexp1
)
2831 ((eq eltype1
*wild-type
*) eltype2
)
2832 ((eq eltype2
*wild-type
*) eltype1
)
2833 (t (type-intersection eltype1 eltype2
)))
2834 :specialized-element-type
(cond
2835 ((eq stype1
*wild-type
*) stype2
)
2836 ((eq stype2
*wild-type
*) stype1
)
2838 (aver (type= stype1 stype2
))
2842 ;;; Check a supplied dimension list to determine whether it is legal,
2843 ;;; and return it in canonical form (as either '* or a list).
2844 (defun canonical-array-dimensions (dims)
2849 (error "Arrays can't have a negative number of dimensions: ~S" dims
))
2850 (when (>= dims sb
!xc
:array-rank-limit
)
2851 (error "array type with too many dimensions: ~S" dims
))
2852 (make-list dims
:initial-element
'*))
2854 (when (>= (length dims
) sb
!xc
:array-rank-limit
)
2855 (error "array type with too many dimensions: ~S" dims
))
2858 (unless (and (integerp dim
)
2860 (< dim sb
!xc
:array-dimension-limit
))
2861 (error "bad dimension in array type: ~S" dim
))))
2864 (error "Array dimensions is not a list, integer or *:~% ~S" dims
))))
2868 (!define-type-class member
:enumerable t
2869 :might-contain-other-types nil
)
2871 (!define-type-method
(member :negate
) (type)
2872 (let ((xset (member-type-xset type
))
2873 (fp-zeroes (member-type-fp-zeroes type
)))
2875 ;; Hairy case, which needs to do a bit of float type
2876 ;; canonicalization.
2877 (apply #'type-intersection
2878 (if (xset-empty-p xset
)
2880 (make-negation-type :type
(make-member-type xset nil
)))
2883 (let* ((opposite (neg-fp-zero x
))
2884 (type (ctype-of opposite
)))
2887 :type
(modified-numeric-type type
:low nil
:high nil
))
2888 (modified-numeric-type type
:low nil
:high
(list opposite
))
2889 (make-eql-type opposite
)
2890 (modified-numeric-type type
:low
(list opposite
) :high nil
))))
2893 (make-negation-type :type type
))))
2895 (!define-type-method
(member :unparse
) (type)
2896 (let ((members (member-type-members type
)))
2897 (cond ((equal members
'(nil)) 'null
)
2898 (t `(member ,@members
)))))
2900 (!define-type-method
(member :singleton-p
) (type)
2901 (if (eql 1 (member-type-size type
))
2902 (values t
(first (member-type-members type
)))
2905 (!define-type-method
(member :simple-subtypep
) (type1 type2
)
2906 (values (and (xset-subset-p (member-type-xset type1
)
2907 (member-type-xset type2
))
2908 (subsetp (member-type-fp-zeroes type1
)
2909 (member-type-fp-zeroes type2
)))
2912 (!define-type-method
(member :complex-subtypep-arg1
) (type1 type2
)
2914 (mapc-member-type-members
2916 (multiple-value-bind (ok surep
) (ctypep elt type2
)
2918 (return-from punt
(values nil nil
)))
2920 (return-from punt
(values nil t
)))))
2924 ;;; We punt if the odd type is enumerable and intersects with the
2925 ;;; MEMBER type. If not enumerable, then it is definitely not a
2926 ;;; subtype of the MEMBER type.
2927 (!define-type-method
(member :complex-subtypep-arg2
) (type1 type2
)
2928 (cond ((not (type-enumerable type1
)) (values nil t
))
2929 ((types-equal-or-intersect type1 type2
)
2930 (invoke-complex-subtypep-arg1-method type1 type2
))
2931 (t (values nil t
))))
2933 (!define-type-method
(member :simple-intersection2
) (type1 type2
)
2934 (make-member-type (xset-intersection (member-type-xset type1
)
2935 (member-type-xset type2
))
2936 (intersection (member-type-fp-zeroes type1
)
2937 (member-type-fp-zeroes type2
))))
2939 (!define-type-method
(member :complex-intersection2
) (type1 type2
)
2941 (let ((xset (alloc-xset))
2943 (mapc-member-type-members
2945 (multiple-value-bind (ok sure
) (ctypep member type1
)
2947 (return-from punt nil
))
2949 (if (fp-zero-p member
)
2950 (pushnew member fp-zeroes
)
2951 (add-to-xset member xset
)))))
2953 (if (and (xset-empty-p xset
) (not fp-zeroes
))
2955 (make-member-type xset fp-zeroes
)))))
2957 ;;; We don't need a :COMPLEX-UNION2, since the only interesting case is
2958 ;;; a union type, and the member/union interaction is handled by the
2959 ;;; union type method.
2960 (!define-type-method
(member :simple-union2
) (type1 type2
)
2961 (make-member-type (xset-union (member-type-xset type1
)
2962 (member-type-xset type2
))
2963 (union (member-type-fp-zeroes type1
)
2964 (member-type-fp-zeroes type2
))))
2966 (!define-type-method
(member :simple-
=) (type1 type2
)
2967 (let ((xset1 (member-type-xset type1
))
2968 (xset2 (member-type-xset type2
))
2969 (l1 (member-type-fp-zeroes type1
))
2970 (l2 (member-type-fp-zeroes type2
)))
2971 (values (and (eql (xset-count xset1
) (xset-count xset2
))
2972 (xset-subset-p xset1 xset2
)
2973 (xset-subset-p xset2 xset1
)
2978 (!define-type-method
(member :complex-
=) (type1 type2
)
2979 (if (type-enumerable type1
)
2980 (multiple-value-bind (val win
) (csubtypep type2 type1
)
2981 (if (or val
(not win
))
2986 (!def-type-translator member
:list
(&rest members
)
2988 (let (ms numbers char-codes
)
2989 (dolist (m (remove-duplicates members
))
2991 (character (push (sb!xc
:char-code m
) char-codes
))
2992 (real (if (and (floatp m
) (zerop m
))
2994 (push (ctype-of m
) numbers
)))
2997 (member-type-from-list ms
)
2998 (make-character-set-type
2999 :pairs
(mapcar (lambda (x) (cons x x
))
3000 (sort char-codes
#'<)))
3001 (nreverse numbers
)))
3004 ;;;; intersection types
3006 ;;;; Until version 0.6.10.6, SBCL followed the original CMU CL approach
3007 ;;;; of punting on all AND types, not just the unreasonably complicated
3008 ;;;; ones. The change was motivated by trying to get the KEYWORD type
3009 ;;;; to behave sensibly:
3010 ;;;; ;; reasonable definition
3011 ;;;; (DEFTYPE KEYWORD () '(AND SYMBOL (SATISFIES KEYWORDP)))
3012 ;;;; ;; reasonable behavior
3013 ;;;; (AVER (SUBTYPEP 'KEYWORD 'SYMBOL))
3014 ;;;; Without understanding a little about the semantics of AND, we'd
3015 ;;;; get (SUBTYPEP 'KEYWORD 'SYMBOL)=>NIL,NIL and, for entirely
3016 ;;;; parallel reasons, (SUBTYPEP 'RATIO 'NUMBER)=>NIL,NIL. That's
3019 ;;;; We still follow the example of CMU CL to some extent, by punting
3020 ;;;; (to the opaque HAIRY-TYPE) on sufficiently complicated types
3023 (!define-type-class intersection
3024 :enumerable
#'compound-type-enumerable
3025 :might-contain-other-types t
)
3027 (!define-type-method
(intersection :negate
) (type)
3029 (mapcar #'type-negation
(intersection-type-types type
))))
3031 ;;; A few intersection types have special names. The others just get
3032 ;;; mechanically unparsed.
3033 (!define-type-method
(intersection :unparse
) (type)
3034 (declare (type ctype type
))
3035 (or (find type
'(ratio keyword compiled-function
) :key
#'specifier-type
:test
#'type
=)
3036 `(and ,@(mapcar #'type-specifier
(intersection-type-types type
)))))
3038 ;;; shared machinery for type equality: true if every type in the set
3039 ;;; TYPES1 matches a type in the set TYPES2 and vice versa
3040 (defun type=-set
(types1 types2
)
3041 (flet ((type<=-set
(x y
)
3042 (declare (type list x y
))
3043 (every/type
(lambda (x y-element
)
3044 (any/type
#'type
= y-element x
))
3046 (and/type
(type<=-set types1 types2
)
3047 (type<=-set types2 types1
))))
3049 ;;; Two intersection types are equal if their subtypes are equal sets.
3051 ;;; FIXME: Might it be better to use
3052 ;;; (AND (SUBTYPEP X Y) (SUBTYPEP Y X))
3053 ;;; instead, since SUBTYPEP is the usual relationship that we care
3054 ;;; most about, so it would be good to leverage any ingenuity there
3055 ;;; in this more obscure method?
3056 (!define-type-method
(intersection :simple-
=) (type1 type2
)
3057 (type=-set
(intersection-type-types type1
)
3058 (intersection-type-types type2
)))
3060 (defun %intersection-complex-subtypep-arg1
(type1 type2
)
3061 (type= type1
(type-intersection type1 type2
)))
3063 (defun %intersection-simple-subtypep
(type1 type2
)
3064 (every/type
#'%intersection-complex-subtypep-arg1
3066 (intersection-type-types type2
)))
3068 (!define-type-method
(intersection :simple-subtypep
) (type1 type2
)
3069 (%intersection-simple-subtypep type1 type2
))
3071 (!define-type-method
(intersection :complex-subtypep-arg1
) (type1 type2
)
3072 (%intersection-complex-subtypep-arg1 type1 type2
))
3074 (defun %intersection-complex-subtypep-arg2
(type1 type2
)
3075 (every/type
#'csubtypep type1
(intersection-type-types type2
)))
3077 (!define-type-method
(intersection :complex-subtypep-arg2
) (type1 type2
)
3078 (%intersection-complex-subtypep-arg2 type1 type2
))
3080 ;;; FIXME: This will look eeriely familiar to readers of the UNION
3081 ;;; :SIMPLE-INTERSECTION2 :COMPLEX-INTERSECTION2 method. That's
3082 ;;; because it was generated by cut'n'paste methods. Given that
3083 ;;; intersections and unions have all sorts of symmetries known to
3084 ;;; mathematics, it shouldn't be beyond the ken of some programmers to
3085 ;;; reflect those symmetries in code in a way that ties them together
3086 ;;; more strongly than having two independent near-copies :-/
3087 (!define-type-method
(intersection :simple-union2
:complex-union2
)
3089 ;; Within this method, type2 is guaranteed to be an intersection
3091 (aver (intersection-type-p type2
))
3092 ;; Make sure to call only the applicable methods...
3093 (cond ((and (intersection-type-p type1
)
3094 (%intersection-simple-subtypep type1 type2
)) type2
)
3095 ((and (intersection-type-p type1
)
3096 (%intersection-simple-subtypep type2 type1
)) type1
)
3097 ((and (not (intersection-type-p type1
))
3098 (%intersection-complex-subtypep-arg2 type1 type2
))
3100 ((and (not (intersection-type-p type1
))
3101 (%intersection-complex-subtypep-arg1 type2 type1
))
3103 ;; KLUDGE: This special (and somewhat hairy) magic is required
3104 ;; to deal with the RATIONAL/INTEGER special case. The UNION
3105 ;; of (INTEGER * -1) and (AND (RATIONAL * -1/2) (NOT INTEGER))
3106 ;; should be (RATIONAL * -1/2) -- CSR, 2003-02-28
3107 ((and (csubtypep type2
(specifier-type 'ratio
))
3108 (numeric-type-p type1
)
3109 (csubtypep type1
(specifier-type 'integer
))
3114 :low
(if (null (numeric-type-low type1
))
3116 (list (1- (numeric-type-low type1
))))
3117 :high
(if (null (numeric-type-high type1
))
3119 (list (1+ (numeric-type-high type1
)))))))
3120 (let* ((intersected (intersection-type-types type2
))
3121 (remaining (remove (specifier-type '(not integer
))
3124 (and (not (equal intersected remaining
))
3125 (type-union type1
(apply #'type-intersection remaining
)))))
3127 (let ((accumulator *universal-type
*))
3128 (do ((t2s (intersection-type-types type2
) (cdr t2s
)))
3129 ((null t2s
) accumulator
)
3130 (let ((union (type-union type1
(car t2s
))))
3131 (when (union-type-p union
)
3132 ;; we have to give up here -- there are all sorts of
3133 ;; ordering worries, but it's better than before.
3134 ;; Doing exactly the same as in the UNION
3135 ;; :SIMPLE/:COMPLEX-INTERSECTION2 method causes stack
3136 ;; overflow with the mutual recursion never bottoming
3138 (if (and (eq accumulator
*universal-type
*)
3140 ;; KLUDGE: if we get here, we have a partially
3141 ;; simplified result. While this isn't by any
3142 ;; means a universal simplification, including
3143 ;; this logic here means that we can get (OR
3144 ;; KEYWORD (NOT KEYWORD)) canonicalized to T.
3148 (type-intersection accumulator union
))))))))
3150 (!def-type-translator and
:list
(&rest type-specifiers
)
3151 (apply #'type-intersection
3152 (mapcar #'specifier-type type-specifiers
)))
3156 (!define-type-class union
3157 :enumerable
#'compound-type-enumerable
3158 :might-contain-other-types t
)
3160 (!define-type-method
(union :negate
) (type)
3161 (declare (type ctype type
))
3162 (apply #'type-intersection
3163 (mapcar #'type-negation
(union-type-types type
))))
3165 ;;; The LIST, FLOAT and REAL types have special names. Other union
3166 ;;; types just get mechanically unparsed.
3167 (!define-type-method
(union :unparse
) (type)
3168 (declare (type ctype type
))
3170 ((type= type
(specifier-type 'list
)) 'list
)
3171 ((type= type
(specifier-type 'float
)) 'float
)
3172 ((type= type
(specifier-type 'real
)) 'real
)
3173 ((type= type
(specifier-type 'sequence
)) 'sequence
)
3174 ((type= type
(specifier-type 'bignum
)) 'bignum
)
3175 ((type= type
(specifier-type 'simple-string
)) 'simple-string
)
3176 ((type= type
(specifier-type 'string
)) 'string
)
3177 ((type= type
(specifier-type 'complex
)) 'complex
)
3178 ((type= type
(specifier-type 'standard-char
)) 'standard-char
)
3179 (t `(or ,@(mapcar #'type-specifier
(union-type-types type
))))))
3181 ;;; Two union types are equal if they are each subtypes of each
3182 ;;; other. We need to be this clever because our complex subtypep
3183 ;;; methods are now more accurate; we don't get infinite recursion
3184 ;;; because the simple-subtypep method delegates to complex-subtypep
3185 ;;; of the individual types of type1. - CSR, 2002-04-09
3187 ;;; Previous comment, now obsolete, but worth keeping around because
3188 ;;; it is true, though too strong a condition:
3190 ;;; Two union types are equal if their subtypes are equal sets.
3191 (!define-type-method
(union :simple-
=) (type1 type2
)
3192 (multiple-value-bind (subtype certain?
)
3193 (csubtypep type1 type2
)
3195 (csubtypep type2 type1
)
3196 ;; we might as well become as certain as possible.
3199 (multiple-value-bind (subtype certain?
)
3200 (csubtypep type2 type1
)
3201 (declare (ignore subtype
))
3202 (values nil certain?
))))))
3204 (!define-type-method
(union :complex-
=) (type1 type2
)
3205 (declare (ignore type1
))
3206 (if (some #'type-might-contain-other-types-p
3207 (union-type-types type2
))
3211 ;;; Similarly, a union type is a subtype of another if and only if
3212 ;;; every element of TYPE1 is a subtype of TYPE2.
3213 (defun union-simple-subtypep (type1 type2
)
3214 (every/type
(swapped-args-fun #'union-complex-subtypep-arg2
)
3216 (union-type-types type1
)))
3218 (!define-type-method
(union :simple-subtypep
) (type1 type2
)
3219 (union-simple-subtypep type1 type2
))
3221 (defun union-complex-subtypep-arg1 (type1 type2
)
3222 (every/type
(swapped-args-fun #'csubtypep
)
3224 (union-type-types type1
)))
3226 (!define-type-method
(union :complex-subtypep-arg1
) (type1 type2
)
3227 (union-complex-subtypep-arg1 type1 type2
))
3229 (defun union-complex-subtypep-arg2 (type1 type2
)
3230 ;; At this stage, we know that type2 is a union type and type1
3231 ;; isn't. We might as well check this, though:
3232 (aver (union-type-p type2
))
3233 (aver (not (union-type-p type1
)))
3234 ;; was: (any/type #'csubtypep type1 (union-type-types type2)), which
3235 ;; turns out to be too restrictive, causing bug 91.
3237 ;; the following reimplementation might look dodgy. It is dodgy. It
3238 ;; depends on the union :complex-= method not doing very much work
3239 ;; -- certainly, not using subtypep. Reasoning:
3241 ;; A is a subset of (B1 u B2)
3242 ;; <=> A n (B1 u B2) = A
3243 ;; <=> (A n B1) u (A n B2) = A
3245 ;; But, we have to be careful not to delegate this type= to
3246 ;; something that could invoke subtypep, which might get us back
3247 ;; here -> stack explosion. We therefore ensure that the second type
3248 ;; (which is the one that's dispatched on) is either a union type
3249 ;; (where we've ensured that the complex-= method will not call
3250 ;; subtypep) or something with no union types involved, in which
3251 ;; case we'll never come back here.
3253 ;; If we don't do this, then e.g.
3254 ;; (SUBTYPEP '(MEMBER 3) '(OR (SATISFIES FOO) (SATISFIES BAR)))
3255 ;; would loop infinitely, as the member :complex-= method is
3256 ;; implemented in terms of subtypep.
3258 ;; Ouch. - CSR, 2002-04-10
3259 (multiple-value-bind (sub-value sub-certain?
)
3262 (mapcar (lambda (x) (type-intersection type1 x
))
3263 (union-type-types type2
))))
3265 (values sub-value sub-certain?
)
3266 ;; The ANY/TYPE expression above is a sufficient condition for
3267 ;; subsetness, but not a necessary one, so we might get a more
3268 ;; certain answer by this CALL-NEXT-METHOD-ish step when the
3269 ;; ANY/TYPE expression is uncertain.
3270 (invoke-complex-subtypep-arg1-method type1 type2
))))
3272 (!define-type-method
(union :complex-subtypep-arg2
) (type1 type2
)
3273 (union-complex-subtypep-arg2 type1 type2
))
3275 (!define-type-method
(union :simple-intersection2
:complex-intersection2
)
3277 ;; The CSUBTYPEP clauses here let us simplify e.g.
3278 ;; (TYPE-INTERSECTION2 (SPECIFIER-TYPE 'LIST)
3279 ;; (SPECIFIER-TYPE '(OR LIST VECTOR)))
3280 ;; (where LIST is (OR CONS NULL)).
3282 ;; The tests are more or less (CSUBTYPEP TYPE1 TYPE2) and vice
3283 ;; versa, but it's important that we pre-expand them into
3284 ;; specialized operations on individual elements of
3285 ;; UNION-TYPE-TYPES, instead of using the ordinary call to
3286 ;; CSUBTYPEP, in order to avoid possibly invoking any methods which
3287 ;; might in turn invoke (TYPE-INTERSECTION2 TYPE1 TYPE2) and thus
3288 ;; cause infinite recursion.
3290 ;; Within this method, type2 is guaranteed to be a union type:
3291 (aver (union-type-p type2
))
3292 ;; Make sure to call only the applicable methods...
3293 (cond ((and (union-type-p type1
)
3294 (union-simple-subtypep type1 type2
)) type1
)
3295 ((and (union-type-p type1
)
3296 (union-simple-subtypep type2 type1
)) type2
)
3297 ((and (not (union-type-p type1
))
3298 (union-complex-subtypep-arg2 type1 type2
))
3300 ((and (not (union-type-p type1
))
3301 (union-complex-subtypep-arg1 type2 type1
))
3304 ;; KLUDGE: This code accumulates a sequence of TYPE-UNION2
3305 ;; operations in a particular order, and gives up if any of
3306 ;; the sub-unions turn out not to be simple. In other cases
3307 ;; ca. sbcl-0.6.11.15, that approach to taking a union was a
3308 ;; bad idea, since it can overlook simplifications which
3309 ;; might occur if the terms were accumulated in a different
3310 ;; order. It's possible that that will be a problem here too.
3311 ;; However, I can't think of a good example to demonstrate
3312 ;; it, and without an example to demonstrate it I can't write
3313 ;; test cases, and without test cases I don't want to
3314 ;; complicate the code to address what's still a hypothetical
3315 ;; problem. So I punted. -- WHN 2001-03-20
3316 (let ((accumulator *empty-type
*))
3317 (dolist (t2 (union-type-types type2
) accumulator
)
3319 (type-union accumulator
3320 (type-intersection type1 t2
))))))))
3322 (!def-type-translator or
:list
(&rest type-specifiers
)
3323 (let ((type (apply #'type-union
3324 (mapcar #'specifier-type type-specifiers
))))
3325 (if (union-type-p type
)
3326 (sb!kernel
::simplify-array-unions type
)
3331 (!define-type-class cons
:enumerable nil
:might-contain-other-types nil
)
3333 (!def-type-translator cons
(&optional
(car-type-spec '*) (cdr-type-spec '*))
3334 (let ((car-type (single-value-specifier-type car-type-spec
))
3335 (cdr-type (single-value-specifier-type cdr-type-spec
)))
3336 (make-cons-type car-type cdr-type
)))
3338 (!define-type-method
(cons :negate
) (type)
3339 (if (and (eq (cons-type-car-type type
) *universal-type
*)
3340 (eq (cons-type-cdr-type type
) *universal-type
*))
3341 (make-negation-type :type type
)
3343 (make-negation-type :type
(specifier-type 'cons
))
3345 ((and (not (eq (cons-type-car-type type
) *universal-type
*))
3346 (not (eq (cons-type-cdr-type type
) *universal-type
*)))
3349 (type-negation (cons-type-car-type type
))
3353 (type-negation (cons-type-cdr-type type
)))))
3354 ((not (eq (cons-type-car-type type
) *universal-type
*))
3356 (type-negation (cons-type-car-type type
))
3358 ((not (eq (cons-type-cdr-type type
) *universal-type
*))
3361 (type-negation (cons-type-cdr-type type
))))
3362 (t (bug "Weird CONS type ~S" type
))))))
3364 (!define-type-method
(cons :unparse
) (type)
3365 (let ((car-eltype (type-specifier (cons-type-car-type type
)))
3366 (cdr-eltype (type-specifier (cons-type-cdr-type type
))))
3367 (if (and (member car-eltype
'(t *))
3368 (member cdr-eltype
'(t *)))
3370 `(cons ,car-eltype
,cdr-eltype
))))
3372 (!define-type-method
(cons :simple-
=) (type1 type2
)
3373 (declare (type cons-type type1 type2
))
3374 (multiple-value-bind (car-match car-win
)
3375 (type= (cons-type-car-type type1
) (cons-type-car-type type2
))
3376 (multiple-value-bind (cdr-match cdr-win
)
3377 (type= (cons-type-cdr-type type1
) (cons-type-cdr-type type2
))
3378 (cond ((and car-match cdr-match
)
3379 (aver (and car-win cdr-win
))
3383 ;; FIXME: Ideally we would like to detect and handle
3384 ;; (CONS UNKNOWN INTEGER) (CONS UNKNOWN SYMBOL) => NIL, T
3385 ;; but just returning a secondary true on (and car-win cdr-win)
3386 ;; unfortunately breaks other things. --NS 2006-08-16
3387 (and (or (and (not car-match
) car-win
)
3388 (and (not cdr-match
) cdr-win
))
3389 (not (and (cons-type-might-be-empty-type type1
)
3390 (cons-type-might-be-empty-type type2
))))))))))
3392 (!define-type-method
(cons :simple-subtypep
) (type1 type2
)
3393 (declare (type cons-type type1 type2
))
3394 (multiple-value-bind (val-car win-car
)
3395 (csubtypep (cons-type-car-type type1
) (cons-type-car-type type2
))
3396 (multiple-value-bind (val-cdr win-cdr
)
3397 (csubtypep (cons-type-cdr-type type1
) (cons-type-cdr-type type2
))
3398 (if (and val-car val-cdr
)
3399 (values t
(and win-car win-cdr
))
3400 (values nil
(or (and (not val-car
) win-car
)
3401 (and (not val-cdr
) win-cdr
)))))))
3403 ;;; Give up if a precise type is not possible, to avoid returning
3404 ;;; overly general types.
3405 (!define-type-method
(cons :simple-union2
) (type1 type2
)
3406 (declare (type cons-type type1 type2
))
3407 (let ((car-type1 (cons-type-car-type type1
))
3408 (car-type2 (cons-type-car-type type2
))
3409 (cdr-type1 (cons-type-cdr-type type1
))
3410 (cdr-type2 (cons-type-cdr-type type2
))
3413 ;; UGH. -- CSR, 2003-02-24
3414 (macrolet ((frob-car (car1 car2 cdr1 cdr2
3415 &optional
(not1 nil not1p
))
3417 (make-cons-type ,car1
(type-union ,cdr1
,cdr2
))
3419 (type-intersection ,car2
3422 `(type-negation ,car1
)))
3424 (cond ((type= car-type1 car-type2
)
3425 (make-cons-type car-type1
3426 (type-union cdr-type1 cdr-type2
)))
3427 ((type= cdr-type1 cdr-type2
)
3428 (make-cons-type (type-union car-type1 car-type2
)
3430 ((csubtypep car-type1 car-type2
)
3431 (frob-car car-type1 car-type2 cdr-type1 cdr-type2
))
3432 ((csubtypep car-type2 car-type1
)
3433 (frob-car car-type2 car-type1 cdr-type2 cdr-type1
))
3434 ;; more general case of the above, but harder to compute
3436 (setf car-not1
(type-negation car-type1
))
3437 (multiple-value-bind (yes win
)
3438 (csubtypep car-type2 car-not1
)
3439 (and (not yes
) win
)))
3440 (frob-car car-type1 car-type2 cdr-type1 cdr-type2 car-not1
))
3442 (setf car-not2
(type-negation car-type2
))
3443 (multiple-value-bind (yes win
)
3444 (csubtypep car-type1 car-not2
)
3445 (and (not yes
) win
)))
3446 (frob-car car-type2 car-type1 cdr-type2 cdr-type1 car-not2
))
3447 ;; Don't put these in -- consider the effect of taking the
3448 ;; union of (CONS (INTEGER 0 2) (INTEGER 5 7)) and
3449 ;; (CONS (INTEGER 0 3) (INTEGER 5 6)).
3451 ((csubtypep cdr-type1 cdr-type2
)
3452 (frob-cdr car-type1 car-type2 cdr-type1 cdr-type2
))
3454 ((csubtypep cdr-type2 cdr-type1
)
3455 (frob-cdr car-type2 car-type1 cdr-type2 cdr-type1
))))))
3457 (!define-type-method
(cons :simple-intersection2
) (type1 type2
)
3458 (declare (type cons-type type1 type2
))
3459 (let ((car-int2 (type-intersection2 (cons-type-car-type type1
)
3460 (cons-type-car-type type2
)))
3461 (cdr-int2 (type-intersection2 (cons-type-cdr-type type1
)
3462 (cons-type-cdr-type type2
))))
3464 ((and car-int2 cdr-int2
) (make-cons-type car-int2 cdr-int2
))
3465 (car-int2 (make-cons-type car-int2
3467 (cons-type-cdr-type type1
)
3468 (cons-type-cdr-type type2
))))
3469 (cdr-int2 (make-cons-type
3470 (type-intersection (cons-type-car-type type1
)
3471 (cons-type-car-type type2
))
3474 (!define-superclasses cons
((cons)) !cold-init-forms
)
3476 ;;;; CHARACTER-SET types
3478 ;; all character-set types are enumerable, but it's not possible
3479 ;; for one to be TYPE= to a MEMBER type because (MEMBER #\x)
3480 ;; is not internally represented as a MEMBER type.
3481 ;; So in case it wasn't clear already ENUMERABLE-P does not mean
3482 ;; "possibly a MEMBER type in the Lisp-theoretic sense",
3483 ;; but means "could be implemented in SBCL as a MEMBER type".
3484 (!define-type-class character-set
:enumerable nil
3485 :might-contain-other-types nil
)
3487 (!def-type-translator character-set
3488 (&optional
(pairs '((0 .
#.
(1- sb
!xc
:char-code-limit
)))))
3489 (make-character-set-type :pairs pairs
))
3491 (!define-type-method
(character-set :negate
) (type)
3492 (let ((pairs (character-set-type-pairs type
)))
3493 (if (and (= (length pairs
) 1)
3495 (= (cdar pairs
) (1- sb
!xc
:char-code-limit
)))
3496 (make-negation-type :type type
)
3497 (let ((not-character
3499 :type
(make-character-set-type
3500 :pairs
'((0 .
#.
(1- sb
!xc
:char-code-limit
)))))))
3503 (make-character-set-type
3504 :pairs
(let (not-pairs)
3505 (when (> (caar pairs
) 0)
3506 (push (cons 0 (1- (caar pairs
))) not-pairs
))
3507 (do* ((tail pairs
(cdr tail
))
3508 (high1 (cdar tail
) (cdar tail
))
3509 (low2 (caadr tail
) (caadr tail
)))
3511 (when (< (cdar tail
) (1- sb
!xc
:char-code-limit
))
3512 (push (cons (1+ (cdar tail
))
3513 (1- sb
!xc
:char-code-limit
))
3515 (nreverse not-pairs
))
3516 (push (cons (1+ high1
) (1- low2
)) not-pairs
)))))))))
3518 (!define-type-method
(character-set :unparse
) (type)
3520 ((type= type
(specifier-type 'character
)) 'character
)
3521 ((type= type
(specifier-type 'base-char
)) 'base-char
)
3522 ((type= type
(specifier-type 'extended-char
)) 'extended-char
)
3523 ((type= type
(specifier-type 'standard-char
)) 'standard-char
)
3525 ;; Unparse into either MEMBER or CHARACTER-SET. We use MEMBER if there
3526 ;; are at most as many characters as there are character code ranges.
3527 ;; (basically saying to use MEMBER if each range is one character)
3528 (let* ((pairs (character-set-type-pairs type
))
3529 (count (length pairs
))
3530 (chars (loop named outer
3531 for
(low . high
) in pairs
3532 nconc
(loop for code from low upto high
3533 collect
(sb!xc
:code-char code
)
3534 when
(minusp (decf count
))
3535 do
(return-from outer t
)))))
3537 `(character-set ,pairs
)
3538 `(member ,@chars
))))))
3540 (!define-type-method
(character-set :singleton-p
) (type)
3541 (let* ((pairs (character-set-type-pairs type
))
3542 (pair (first pairs
)))
3543 (if (and (typep pairs
'(cons t null
))
3544 (eql (car pair
) (cdr pair
)))
3545 (values t
(code-char (car pair
)))
3548 (!define-type-method
(character-set :simple-
=) (type1 type2
)
3549 (let ((pairs1 (character-set-type-pairs type1
))
3550 (pairs2 (character-set-type-pairs type2
)))
3551 (values (equal pairs1 pairs2
) t
)))
3553 (!define-type-method
(character-set :simple-subtypep
) (type1 type2
)
3555 (dolist (pair (character-set-type-pairs type1
) t
)
3556 (unless (position pair
(character-set-type-pairs type2
)
3557 :test
(lambda (x y
) (and (>= (car x
) (car y
))
3558 (<= (cdr x
) (cdr y
)))))
3562 (!define-type-method
(character-set :simple-union2
) (type1 type2
)
3563 ;; KLUDGE: the canonizing in the MAKE-CHARACTER-SET-TYPE function
3564 ;; actually does the union for us. It might be a little fragile to
3566 (make-character-set-type
3568 (copy-alist (character-set-type-pairs type1
))
3569 (copy-alist (character-set-type-pairs type2
))
3572 (!define-type-method
(character-set :simple-intersection2
) (type1 type2
)
3573 ;; KLUDGE: brute force.
3576 (dolist (pair1 (character-set-type-pairs type1
)
3577 (make-character-set-type
3578 :pairs
(sort pairs
#'< :key
#'car
)))
3579 (dolist (pair2 (character-set-type-pairs type2
))
3581 ((<= (car pair1
) (car pair2
) (cdr pair1
))
3582 (push (cons (car pair2
) (min (cdr pair1
) (cdr pair2
))) pairs
))
3583 ((<= (car pair2
) (car pair1
) (cdr pair2
))
3584 (push (cons (car pair1
) (min (cdr pair1
) (cdr pair2
))) pairs
))))))
3586 (make-character-set-type
3587 :pairs
(intersect-type-pairs
3588 (character-set-type-pairs type1
)
3589 (character-set-type-pairs type2
))))
3592 ;;; Intersect two ordered lists of pairs
3593 ;;; Each list is of the form ((start1 . end1) ... (startn . endn)),
3594 ;;; where start1 <= end1 < start2 <= end2 < ... < startn <= endn.
3595 ;;; Each pair represents the integer interval start..end.
3597 (defun intersect-type-pairs (alist1 alist2
)
3598 (if (and alist1 alist2
)
3600 (pair1 (pop alist1
))
3601 (pair2 (pop alist2
)))
3603 (when (> (car pair1
) (car pair2
))
3604 (rotatef pair1 pair2
)
3605 (rotatef alist1 alist2
))
3606 (let ((pair1-cdr (cdr pair1
)))
3608 ((> (car pair2
) pair1-cdr
)
3609 ;; No over lap -- discard pair1
3610 (unless alist1
(return))
3611 (setq pair1
(pop alist1
)))
3612 ((<= (cdr pair2
) pair1-cdr
)
3613 (push (cons (car pair2
) (cdr pair2
)) res
)
3615 ((= (cdr pair2
) pair1-cdr
)
3616 (unless alist1
(return))
3617 (unless alist2
(return))
3618 (setq pair1
(pop alist1
)
3619 pair2
(pop alist2
)))
3620 (t ;; (< (cdr pair2) pair1-cdr)
3621 (unless alist2
(return))
3622 (setq pair1
(cons (1+ (cdr pair2
)) pair1-cdr
))
3623 (setq pair2
(pop alist2
)))))
3624 (t ;; (> (cdr pair2) (cdr pair1))
3625 (push (cons (car pair2
) pair1-cdr
) res
)
3626 (unless alist1
(return))
3627 (setq pair2
(cons (1+ pair1-cdr
) (cdr pair2
)))
3628 (setq pair1
(pop alist1
))))))
3633 ;;; Return the type that describes all objects that are in X but not
3634 ;;; in Y. If we can't determine this type, then return NIL.
3636 ;;; For now, we only are clever dealing with union and member types.
3637 ;;; If either type is not a union type, then we pretend that it is a
3638 ;;; union of just one type. What we do is remove from X all the types
3639 ;;; that are a subtype any type in Y. If any type in X intersects with
3640 ;;; a type in Y but is not a subtype, then we give up.
3642 ;;; We must also special-case any member type that appears in the
3643 ;;; union. We remove from X's members all objects that are TYPEP to Y.
3644 ;;; If Y has any members, we must be careful that none of those
3645 ;;; members are CTYPEP to any of Y's non-member types. We give up in
3646 ;;; this case, since to compute that difference we would have to break
3647 ;;; the type from X into some collection of types that represents the
3648 ;;; type without that particular element. This seems too hairy to be
3649 ;;; worthwhile, given its low utility.
3650 (defun type-difference (x y
)
3651 (if (and (numeric-type-p x
) (numeric-type-p y
))
3652 ;; Numeric types are easy. Are there any others we should handle like this?
3653 (type-intersection x
(type-negation y
))
3654 (let ((x-types (if (union-type-p x
) (union-type-types x
) (list x
)))
3655 (y-types (if (union-type-p y
) (union-type-types y
) (list y
))))
3657 (dolist (x-type x-types
)
3658 (if (member-type-p x-type
)
3659 (let ((xset (alloc-xset))
3661 (mapc-member-type-members
3663 (multiple-value-bind (ok sure
) (ctypep elt y
)
3665 (return-from type-difference nil
))
3668 (pushnew elt fp-zeroes
)
3669 (add-to-xset elt xset
)))))
3671 (unless (and (xset-empty-p xset
) (not fp-zeroes
))
3672 (res (make-member-type xset fp-zeroes
))))
3673 (dolist (y-type y-types
(res x-type
))
3674 (multiple-value-bind (val win
) (csubtypep x-type y-type
)
3675 (unless win
(return-from type-difference nil
))
3677 (when (types-equal-or-intersect x-type y-type
)
3678 (return-from type-difference nil
))))))
3679 (let ((y-mem (find-if #'member-type-p y-types
)))
3681 (dolist (x-type x-types
)
3682 (unless (member-type-p x-type
)
3683 (mapc-member-type-members
3685 (multiple-value-bind (ok sure
) (ctypep member x-type
)
3686 (when (or (not sure
) ok
)
3687 (return-from type-difference nil
))))
3689 (apply #'type-union
(res))))))
3691 (!def-type-translator array
(&optional
(element-type '*)
3693 (let ((eltype (if (eq element-type
'*)
3695 (specifier-type element-type
))))
3696 (make-array-type (canonical-array-dimensions dimensions
)
3698 :element-type eltype
3699 :specialized-element-type
(%upgraded-array-element-type
3702 (!def-type-translator simple-array
(&optional
(element-type '*)
3704 (let ((eltype (if (eq element-type
'*)
3706 (specifier-type element-type
))))
3707 (make-array-type (canonical-array-dimensions dimensions
)
3709 :element-type eltype
3710 :specialized-element-type
(%upgraded-array-element-type
3713 ;;;; SIMD-PACK types
3716 (!define-type-class simd-pack
:enumerable nil
3717 :might-contain-other-types nil
)
3719 (!def-type-translator simd-pack
(&optional
(element-type-spec '*))
3720 (if (eql element-type-spec
'*)
3721 (%make-simd-pack-type
*simd-pack-element-types
*)
3722 (make-simd-pack-type (single-value-specifier-type element-type-spec
))))
3724 (!define-type-method
(simd-pack :negate
) (type)
3725 (let ((remaining (set-difference *simd-pack-element-types
*
3726 (simd-pack-type-element-type type
)))
3727 (not-simd-pack (make-negation-type :type
(specifier-type 'simd-pack
))))
3729 (type-union not-simd-pack
(%make-simd-pack-type remaining
))
3732 (!define-type-method
(simd-pack :unparse
) (type)
3733 (let ((eltypes (simd-pack-type-element-type type
)))
3734 (cond ((equal eltypes
*simd-pack-element-types
*)
3736 ((= 1 (length eltypes
))
3737 `(simd-pack ,(first eltypes
)))
3739 `(or ,@(mapcar (lambda (eltype)
3740 `(simd-pack ,eltype
))
3743 (!define-type-method
(simd-pack :simple-
=) (type1 type2
)
3744 (declare (type simd-pack-type type1 type2
))
3745 (null (set-exclusive-or (simd-pack-type-element-type type1
)
3746 (simd-pack-type-element-type type2
))))
3748 (!define-type-method
(simd-pack :simple-subtypep
) (type1 type2
)
3749 (declare (type simd-pack-type type1 type2
))
3750 (subsetp (simd-pack-type-element-type type1
)
3751 (simd-pack-type-element-type type2
)))
3753 (!define-type-method
(simd-pack :simple-union2
) (type1 type2
)
3754 (declare (type simd-pack-type type1 type2
))
3755 (%make-simd-pack-type
(union (simd-pack-type-element-type type1
)
3756 (simd-pack-type-element-type type2
))))
3758 (!define-type-method
(simd-pack :simple-intersection2
) (type1 type2
)
3759 (declare (type simd-pack-type type1 type2
))
3760 (let ((intersection (intersection (simd-pack-type-element-type type1
)
3761 (simd-pack-type-element-type type2
))))
3763 (%make-simd-pack-type intersection
)
3766 (!define-superclasses simd-pack
((simd-pack)) !cold-init-forms
))
3768 ;;;; utilities shared between cross-compiler and target system
3770 ;;; Does the type derived from compilation of an actual function
3771 ;;; definition satisfy declarations of a function's type?
3772 (defun defined-ftype-matches-declared-ftype-p (defined-ftype declared-ftype
)
3773 (declare (type ctype defined-ftype declared-ftype
))
3774 (flet ((is-built-in-class-function-p (ctype)
3775 (and (built-in-classoid-p ctype
)
3776 (eq (built-in-classoid-name ctype
) 'function
))))
3777 (cond (;; DECLARED-FTYPE could certainly be #<BUILT-IN-CLASS FUNCTION>;
3778 ;; that's what happens when we (DECLAIM (FTYPE FUNCTION FOO)).
3779 (is-built-in-class-function-p declared-ftype
)
3780 ;; In that case, any definition satisfies the declaration.
3782 (;; It's not clear whether or how DEFINED-FTYPE might be
3783 ;; #<BUILT-IN-CLASS FUNCTION>, but it's not obviously
3784 ;; invalid, so let's handle that case too, just in case.
3785 (is-built-in-class-function-p defined-ftype
)
3786 ;; No matter what DECLARED-FTYPE might be, we can't prove
3787 ;; that an object of type FUNCTION doesn't satisfy it, so
3788 ;; we return success no matter what.
3790 (;; Otherwise both of them must be FUN-TYPE objects.
3792 ;; FIXME: For now we only check compatibility of the return
3793 ;; type, not argument types, and we don't even check the
3794 ;; return type very precisely (as per bug 94a). It would be
3795 ;; good to do a better job. Perhaps to check the
3796 ;; compatibility of the arguments, we should (1) redo
3797 ;; VALUES-TYPES-EQUAL-OR-INTERSECT as
3798 ;; ARGS-TYPES-EQUAL-OR-INTERSECT, and then (2) apply it to
3799 ;; the ARGS-TYPE slices of the FUN-TYPEs. (ARGS-TYPE
3800 ;; is a base class both of VALUES-TYPE and of FUN-TYPE.)
3801 (values-types-equal-or-intersect
3802 (fun-type-returns defined-ftype
)
3803 (fun-type-returns declared-ftype
))))))
3805 ;;; This messy case of CTYPE for NUMBER is shared between the
3806 ;;; cross-compiler and the target system.
3807 (defun ctype-of-number (x)
3808 (let ((num (if (complexp x
) (realpart x
) x
)))
3809 (multiple-value-bind (complexp low high
)
3811 (let ((imag (imagpart x
)))
3812 (values :complex
(min num imag
) (max num imag
)))
3813 (values :real num num
))
3814 (make-numeric-type :class
(etypecase num
3815 (integer (if (complexp x
)
3816 (if (integerp (imagpart x
))
3820 (rational 'rational
)
3822 :format
(and (floatp num
) (float-format-name num
))
3827 ;;; The following function is a generic driver for approximating
3828 ;;; set-valued functions over types. Putting this here because it'll
3829 ;;; probably be useful for a lot of type analyses.
3831 ;;; Let f be a function from values of type X to Y, e.g., ARRAY-RANK.
3833 ;;; We compute an over or under-approximation of the set
3835 ;;; F(TYPE) = { f(x) : x in TYPE /\ x in X } \subseteq Y
3837 ;;; via set-valued approximations of f, OVER and UNDER.
3839 ;;; These functions must have the property that
3840 ;;; Forall TYPE, OVER(TYPE) \superseteq F(TYPE) and
3841 ;;; Forall TYPE, UNDER(TYPE) \subseteq F(TYPE)
3843 ;;; The driver is also parameterised over the finite set
3846 ;;; Union, intersection and difference are binary functions to compute
3847 ;;; set union, intersection and difference. Top and bottom are the
3848 ;;; concrete representations for the universe and empty sets; we never
3849 ;;; call the set functions on top or bottom, so it's safe to use
3850 ;;; special values there.
3854 ;;; TYPE: the ctype for which we wish to approximate F(TYPE)
3855 ;;; OVERAPPROXIMATE: true if we wish to overapproximate, nil otherwise.
3856 ;;; You usually want T.
3857 ;;; UNION/INTERSECTION/DIFFERENCE: implementations of finite set operations.
3858 ;;; Conform to cl::(union/intersection/set-difference). Passing NIL will
3859 ;;; disable some cleverness and result in quicker computation of coarser
3860 ;;; approximations. However, passing difference without union and intersection
3861 ;;; will probably not end well.
3862 ;;; TOP/BOTTOM: concrete representation of the universe and empty set. Finite
3863 ;;; set operations are never called on TOP/BOTTOM, so it's safe to use special
3865 ;;; OVER/UNDER: the set-valued approximations of F.
3867 ;;; Implementation details.
3869 ;;; It's a straightforward walk down the type.
3870 ;;; Union types -> take the union of children, intersection ->
3871 ;;; intersect. There is some complication for negation types: we must
3872 ;;; not only negate the result, but also flip from overapproximating
3873 ;;; to underapproximating in the children (or vice versa).
3875 ;;; We represent sets as a pair of (negate-p finite-set) in order to
3876 ;;; support negation types.
3878 (declaim (inline generic-abstract-type-function
))
3879 (defun generic-abstract-type-function
3880 (type overapproximate
3881 union intersection difference
3884 (labels ((union* (x y
)
3885 ;; wrappers to avoid calling union/intersection on
3887 (cond ((or (eql x top
)
3893 (funcall union x y
))))
3894 (intersection* (x y
)
3895 (cond ((or (eql x bottom
)
3901 (funcall intersection x y
))))
3902 (unite (not-x-p x not-y-p y
)
3903 ;; if we only have one negated set, it's x.
3905 (rotatef not-x-p not-y-p
)
3907 (cond ((and not-x-p not-y-p
)
3908 ;; -x \/ -y = -(x /\ y)
3909 (normalize t
(intersection* x y
)))
3911 ;; -x \/ y = -(x \ y)
3921 (funcall difference x y
)))))
3923 (values nil
(union* x y
)))))
3924 (intersect (not-x-p x not-y-p y
)
3926 (rotatef not-x-p not-y-p
)
3928 (cond ((and not-x-p not-y-p
)
3929 ;; -x /\ -y = -(x \/ y)
3930 (normalize t
(union* x y
)))
3933 (cond ((or (eql x top
) (eql y bottom
))
3934 (values nil bottom
))
3940 (values nil
(funcall difference y x
)))))
3942 (values nil
(intersection* x y
)))))
3943 (normalize (not-x-p x
)
3944 ;; catch some easy cases of redundant negation.
3945 (cond ((not not-x-p
)
3953 (default (overapproximate)
3955 (if overapproximate top bottom
))
3956 (walk-union (types overapproximate
)
3957 ;; Only do this if union is provided.
3959 (return-from walk-union
(default overapproximate
)))
3960 ;; Reduce/union from bottom.
3961 (let ((not-acc-p nil
)
3963 (dolist (type types
(values not-acc-p acc
))
3964 (multiple-value-bind (not x
)
3965 (walk type overapproximate
)
3966 (setf (values not-acc-p acc
)
3967 (unite not-acc-p acc not x
)))
3968 ;; Early exit on top set.
3969 (when (and (eql acc top
)
3971 (return (values nil top
))))))
3972 (walk-intersection (types overapproximate
)
3973 ;; Skip if we don't know how to intersect sets
3974 (unless intersection
3975 (return-from walk-intersection
(default overapproximate
)))
3976 ;; Reduce/intersection from top
3977 (let ((not-acc-p nil
)
3979 (dolist (type types
(values not-acc-p acc
))
3980 (multiple-value-bind (not x
)
3981 (walk type overapproximate
)
3982 (setf (values not-acc-p acc
)
3983 (intersect not-acc-p acc not x
)))
3984 (when (and (eql acc bottom
)
3986 (return (values nil bottom
))))))
3987 (walk-negate (type overapproximate
)
3988 ;; Don't introduce negated types if we don't know how to
3991 (return-from walk-negate
(default overapproximate
)))
3992 (multiple-value-bind (not x
)
3993 (walk type
(not overapproximate
))
3994 (normalize (not not
) x
)))
3995 (walk (type overapproximate
)
3998 (walk-union (union-type-types type
) overapproximate
))
3999 ((cons (member or union
))
4000 (walk-union (rest type
) overapproximate
))
4002 (walk-intersection (intersection-type-types type
) overapproximate
))
4003 ((cons (member and intersection
))
4004 (walk-intersection (rest type
) overapproximate
))
4006 (walk-negate (negation-type-type type
) overapproximate
))
4008 (walk-negate (second type
) overapproximate
))
4016 (funcall under type
)
4017 (default nil
))))))))
4018 (multiple-value-call #'normalize
(walk type overapproximate
))))
4019 (declaim (notinline generic-abstract-type-function
))
4021 ;;; Standard list representation of sets. Use CL:* for the universe.
4022 (defun list-abstract-type-function (type over
&key under
(overapproximate t
))
4023 (declare (inline generic-abstract-type-function
))
4024 (generic-abstract-type-function
4025 type overapproximate
4026 #'union
#'intersection
#'set-difference
4030 (!defun-from-collected-cold-init-forms
!late-type-cold-init
)
4032 #-sb-xc
(!late-type-cold-init2
)
4034 (/show0
"late-type.lisp end of file")