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
(&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 (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 (declaim (ftype (function (list) (values)) !precompute-types
))
1026 (defun !precompute-types
(specs)
1027 (dolist (spec specs
)
1028 (let ((res (specifier-type spec
)))
1029 (unless (unknown-type-p res
)
1030 (setf (info :type
:builtin spec
) res
)
1031 (setf (info :type
:kind spec
) :primitive
))))
1034 ;;;; general TYPE-UNION and TYPE-INTERSECTION operations
1036 ;;;; These are fully general operations on CTYPEs: they'll always
1037 ;;;; return a CTYPE representing the result.
1039 ;;; shared logic for unions and intersections: Return a list of
1040 ;;; types representing the same types as INPUT-TYPES, but with
1041 ;;; COMPOUND-TYPEs satisfying %COMPOUND-TYPE-P broken up into their
1042 ;;; component types, and with any SIMPLY2 simplifications applied.
1044 ((def (name compound-type-p simplify2
)
1045 `(defun ,name
(types)
1047 (multiple-value-bind (first rest
)
1048 (if (,compound-type-p
(car types
))
1049 (values (car (compound-type-types (car types
)))
1050 (append (cdr (compound-type-types (car types
)))
1052 (values (car types
) (cdr types
)))
1053 (let ((rest (,name rest
)) u
)
1054 (dolist (r rest
(cons first rest
))
1055 (when (setq u
(,simplify2 first r
))
1056 (return (,name
(nsubstitute u r rest
)))))))))))
1057 (def simplify-intersections intersection-type-p type-intersection2
)
1058 (def simplify-unions union-type-p type-union2
))
1060 (defun maybe-distribute-one-union (union-type types
)
1061 (let* ((intersection (apply #'type-intersection types
))
1062 (union (mapcar (lambda (x) (type-intersection x intersection
))
1063 (union-type-types union-type
))))
1064 (if (notany (lambda (x) (or (hairy-type-p x
)
1065 (intersection-type-p x
)))
1070 (defun type-intersection (&rest input-types
)
1071 (%type-intersection input-types
))
1072 (defun-cached (%type-intersection
:hash-bits
10 :hash-function
#'type-list-cache-hash
)
1073 ((input-types equal
))
1074 (let ((simplified-types (simplify-intersections input-types
)))
1075 (declare (type list simplified-types
))
1076 ;; We want to have a canonical representation of types (or failing
1077 ;; that, punt to HAIRY-TYPE). Canonical representation would have
1078 ;; intersections inside unions but not vice versa, since you can
1079 ;; always achieve that by the distributive rule. But we don't want
1080 ;; to just apply the distributive rule, since it would be too easy
1081 ;; to end up with unreasonably huge type expressions. So instead
1082 ;; we try to generate a simple type by distributing the union; if
1083 ;; the type can't be made simple, we punt to HAIRY-TYPE.
1084 (if (and (cdr simplified-types
) (some #'union-type-p simplified-types
))
1085 (let* ((first-union (find-if #'union-type-p simplified-types
))
1086 (other-types (coerce (remove first-union simplified-types
)
1088 (distributed (maybe-distribute-one-union first-union
1091 (apply #'type-union distributed
)
1092 (%make-hairy-type
`(and ,@(map 'list
#'type-specifier
1093 simplified-types
)))))
1095 ((null simplified-types
) *universal-type
*)
1096 ((null (cdr simplified-types
)) (car simplified-types
))
1097 (t (%make-intersection-type
1098 (some #'type-enumerable simplified-types
)
1099 simplified-types
))))))
1101 (defun type-union (&rest input-types
)
1102 (%type-union input-types
))
1103 (defun-cached (%type-union
:hash-bits
8 :hash-function
#'type-list-cache-hash
)
1104 ((input-types equal
))
1105 (let ((simplified-types (simplify-unions input-types
)))
1107 ((null simplified-types
) *empty-type
*)
1108 ((null (cdr simplified-types
)) (car simplified-types
))
1110 (every #'type-enumerable simplified-types
)
1111 simplified-types
)))))
1115 (!define-type-class named
:enumerable nil
:might-contain-other-types nil
)
1117 ;; This is used when parsing (SATISFIES KEYWORDP)
1118 ;; so that simplifications can be made when computing intersections,
1119 ;; without which we would see this kind of "empty-type in disguise"
1120 ;; (AND (SATISFIES KEYWORDP) CONS)
1121 ;; This isn't *keyword-type* because KEYWORD is implemented
1122 ;; as the intersection of SYMBOL and (SATISFIES KEYWORDP)
1123 ;; We could also intern the KEYWORD type but that would require
1124 ;; hacking the INTERSECTION logic.
1125 (defglobal *satisfies-keywordp-type
* -
1)
1127 ;; Here too I discovered more than 1000 instances in a particular
1128 ;; Lisp image, when really this is *EMPTY-TYPE*.
1129 ;; (AND (SATISFIES LEGAL-FUN-NAME-P) (SIMPLE-ARRAY CHARACTER (*)))
1130 (defglobal *fun-name-type
* -
1)
1132 ;; !LATE-TYPE-COLD-INIT can't be GCd - there are lambdas in the toplevel code
1133 ;; component that leak out and persist - but everything below is GCable.
1134 ;; This leads to about 20KB of extra code being retained on x86-64.
1135 ;; An educated guess is that DEFINE-SUPERCLASSES is responsible for the problem.
1136 (defun !late-type-cold-init2
()
1137 (macrolet ((frob (name var
)
1140 (mark-ctype-interned (make-named-type :name
',name
)))
1141 (setf (info :type
:kind
',name
) :primitive
)
1142 (setf (info :type
:builtin
',name
) ,var
))))
1143 ;; KLUDGE: In ANSI, * isn't really the name of a type, it's just a
1144 ;; special symbol which can be stuck in some places where an
1145 ;; ordinary type can go, e.g. (ARRAY * 1) instead of (ARRAY T 1).
1146 ;; In SBCL it also used to denote universal VALUES type.
1147 (frob * *wild-type
*)
1148 (frob nil
*empty-type
*)
1149 (frob t
*universal-type
*)
1150 (setf (sb!c
::meta-info-default
(sb!c
::meta-info
:variable
:type
))
1152 ;; new in sbcl-0.9.5: these used to be CLASSOID types, but that
1153 ;; view of them was incompatible with requirements on the MOP
1154 ;; metaobject class hierarchy: the INSTANCE and
1155 ;; FUNCALLABLE-INSTANCE types are disjoint (instances have
1156 ;; instance-pointer-lowtag; funcallable-instances have
1157 ;; fun-pointer-lowtag), while FUNCALLABLE-STANDARD-OBJECT is
1158 ;; required to be a subclass of STANDARD-OBJECT. -- CSR,
1160 (frob instance
*instance-type
*)
1161 (frob funcallable-instance
*funcallable-instance-type
*)
1162 ;; new in sbcl-1.0.3.3: necessary to act as a join point for the
1163 ;; extended sequence hierarchy. (Might be removed later if we use
1164 ;; a dedicated FUNDAMENTAL-SEQUENCE class for this.)
1165 (frob extended-sequence
*extended-sequence-type
*))
1166 (!intern-important-fun-type-instances
)
1167 (!intern-important-member-type-instances
)
1168 (!intern-important-cons-type-instances
)
1169 (!intern-important-numeric-type-instances
)
1170 (!intern-important-character-set-type-instances
)
1171 (!intern-important-array-type-instances
) ; must be after numeric and char
1172 (setf *satisfies-keywordp-type
*
1173 (mark-ctype-interned (%make-hairy-type
'(satisfies keywordp
))))
1174 (setf *fun-name-type
*
1175 (mark-ctype-interned (%make-hairy-type
'(satisfies legal-fun-name-p
))))
1176 ;; This is not an important type- no attempt is made to return exactly this
1177 ;; object when parsing FUNCTION. In fact we return the classoid instead
1178 (setf *universal-fun-type
*
1179 (make-fun-type :wild-args t
:returns
*wild-type
*)))
1181 (!define-type-method
(named :simple-
=) (type1 type2
)
1182 ;;(aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1183 (values (eq type1 type2
) t
))
1185 (defun cons-type-might-be-empty-type (type)
1186 (declare (type cons-type type
))
1187 (let ((car-type (cons-type-car-type type
))
1188 (cdr-type (cons-type-cdr-type type
)))
1190 (if (cons-type-p car-type
)
1191 (cons-type-might-be-empty-type car-type
)
1192 (multiple-value-bind (yes surep
)
1193 (type= car-type
*empty-type
*)
1196 (if (cons-type-p cdr-type
)
1197 (cons-type-might-be-empty-type cdr-type
)
1198 (multiple-value-bind (yes surep
)
1199 (type= cdr-type
*empty-type
*)
1203 (defun cons-type-length-info (type)
1204 (declare (type cons-type type
))
1205 (do ((min 1 (1+ min
))
1206 (cdr (cons-type-cdr-type type
) (cons-type-cdr-type cdr
)))
1207 ((not (cons-type-p cdr
))
1209 ((csubtypep cdr
(specifier-type 'null
))
1211 ((csubtypep *universal-type
* cdr
)
1213 ((type/= (type-intersection (specifier-type 'cons
) cdr
) *empty-type
*)
1215 ((type/= (type-intersection (specifier-type 'null
) cdr
) *empty-type
*)
1217 (t (values min
:maybe
))))
1220 (!define-type-method
(named :complex-
=) (type1 type2
)
1222 ((and (eq type2
*empty-type
*)
1223 (or (and (intersection-type-p type1
)
1224 ;; not allowed to be unsure on these... FIXME: keep
1225 ;; the list of CL types that are intersection types
1226 ;; once and only once.
1227 (not (or (type= type1
(specifier-type 'ratio
))
1228 (type= type1
(specifier-type 'keyword
)))))
1229 (and (cons-type-p type1
)
1230 (cons-type-might-be-empty-type type1
))))
1231 ;; things like (AND (EQL 0) (SATISFIES ODDP)) or (AND FUNCTION
1232 ;; STREAM) can get here. In general, we can't really tell
1233 ;; whether these are equal to NIL or not, so
1235 ((type-might-contain-other-types-p type1
)
1236 (invoke-complex-=-other-method type1 type2
))
1237 (t (values nil t
))))
1239 (!define-type-method
(named :simple-subtypep
) (type1 type2
)
1240 (aver (not (eq type1
*wild-type
*))) ; * isn't really a type.
1241 (aver (not (eq type1 type2
)))
1242 (values (or (eq type1
*empty-type
*)
1243 (eq type2
*wild-type
*)
1244 (eq type2
*universal-type
*)) t
))
1246 (!define-type-method
(named :complex-subtypep-arg1
) (type1 type2
)
1247 ;; This AVER causes problems if we write accurate methods for the
1248 ;; union (and possibly intersection) types which then delegate to
1249 ;; us; while a user shouldn't get here, because of the odd status of
1250 ;; *wild-type* a type-intersection executed by the compiler can. -
1253 ;; (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1254 (cond ((eq type1
*empty-type
*)
1256 (;; When TYPE2 might be the universal type in disguise
1257 (type-might-contain-other-types-p type2
)
1258 ;; Now that the UNION and HAIRY COMPLEX-SUBTYPEP-ARG2 methods
1259 ;; can delegate to us (more or less as CALL-NEXT-METHOD) when
1260 ;; they're uncertain, we can't just barf on COMPOUND-TYPE and
1261 ;; HAIRY-TYPEs as we used to. Instead we deal with the
1262 ;; problem (where at least part of the problem is cases like
1263 ;; (SUBTYPEP T '(SATISFIES FOO))
1265 ;; (SUBTYPEP T '(AND (SATISFIES FOO) (SATISFIES BAR)))
1266 ;; where the second type is a hairy type like SATISFIES, or
1267 ;; is a compound type which might contain a hairy type) by
1268 ;; returning uncertainty.
1270 ((eq type1
*funcallable-instance-type
*)
1271 (values (eq type2
(specifier-type 'function
)) t
))
1273 ;; This case would have been picked off by the SIMPLE-SUBTYPEP
1274 ;; method, and so shouldn't appear here.
1275 (aver (not (named-type-p type2
)))
1276 ;; Since TYPE2 is not EQ *UNIVERSAL-TYPE* and is not another
1277 ;; named type in disguise, TYPE2 is not a superset of TYPE1.
1280 (!define-type-method
(named :complex-subtypep-arg2
) (type1 type2
)
1281 (aver (not (eq type2
*wild-type
*))) ; * isn't really a type.
1282 (cond ((eq type2
*universal-type
*)
1284 ;; some CONS types can conceal danger
1285 ((and (cons-type-p type1
) (cons-type-might-be-empty-type type1
))
1287 ((type-might-contain-other-types-p type1
)
1288 ;; those types can be other types in disguise. So we'd
1290 (invoke-complex-subtypep-arg1-method type1 type2
))
1291 ((and (or (eq type2
*instance-type
*)
1292 (eq type2
*funcallable-instance-type
*))
1293 (member-type-p type1
))
1294 ;; member types can be subtypep INSTANCE and
1295 ;; FUNCALLABLE-INSTANCE in surprising ways.
1296 (invoke-complex-subtypep-arg1-method type1 type2
))
1297 ((and (eq type2
*extended-sequence-type
*) (classoid-p type1
))
1298 (let* ((layout (classoid-layout type1
))
1299 (inherits (layout-inherits layout
))
1300 (sequencep (find (classoid-layout (find-classoid 'sequence
))
1302 (values (if sequencep t nil
) t
)))
1303 ((and (eq type2
*instance-type
*) (classoid-p type1
))
1304 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1306 (let* ((layout (classoid-layout type1
))
1307 (inherits (layout-inherits layout
))
1308 (functionp (find (classoid-layout (find-classoid 'function
))
1313 ((eq type1
(find-classoid 'function
))
1315 ((or (structure-classoid-p type1
)
1317 (condition-classoid-p type1
))
1319 (t (values nil nil
))))))
1320 ((and (eq type2
*funcallable-instance-type
*) (classoid-p type1
))
1321 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1323 (let* ((layout (classoid-layout type1
))
1324 (inherits (layout-inherits layout
))
1325 (functionp (find (classoid-layout (find-classoid 'function
))
1327 (values (if functionp t nil
) t
))))
1329 ;; FIXME: This seems to rely on there only being 4 or 5
1330 ;; NAMED-TYPE values, and the exclusion of various
1331 ;; possibilities above. It would be good to explain it and/or
1332 ;; rewrite it so that it's clearer.
1335 (!define-type-method
(named :complex-intersection2
) (type1 type2
)
1336 ;; FIXME: This assertion failed when I added it in sbcl-0.6.11.13.
1337 ;; Perhaps when bug 85 is fixed it can be reenabled.
1338 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1340 ((eq type2
*extended-sequence-type
*)
1342 (structure-classoid *empty-type
*)
1344 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1346 (if (find (classoid-layout (find-classoid 'sequence
))
1347 (layout-inherits (classoid-layout type1
)))
1351 (if (or (type-might-contain-other-types-p type1
)
1352 (member-type-p type1
))
1355 ((eq type2
*instance-type
*)
1357 (structure-classoid type1
)
1359 (if (and (not (member type1
*non-instance-classoid-types
*
1360 :key
#'find-classoid
))
1361 (not (eq type1
(find-classoid 'function
)))
1362 (not (find (classoid-layout (find-classoid 'function
))
1363 (layout-inherits (classoid-layout type1
)))))
1367 (if (or (type-might-contain-other-types-p type1
)
1368 (member-type-p type1
))
1371 ((eq type2
*funcallable-instance-type
*)
1373 (structure-classoid *empty-type
*)
1375 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1377 (if (find (classoid-layout (find-classoid 'function
))
1378 (layout-inherits (classoid-layout type1
)))
1380 (if (type= type1
(find-classoid 'function
))
1385 (if (or (type-might-contain-other-types-p type1
)
1386 (member-type-p type1
))
1389 (t (hierarchical-intersection2 type1 type2
))))
1391 (!define-type-method
(named :complex-union2
) (type1 type2
)
1392 ;; Perhaps when bug 85 is fixed this can be reenabled.
1393 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1395 ((eq type2
*extended-sequence-type
*)
1396 (if (classoid-p type1
)
1397 (if (or (member type1
*non-instance-classoid-types
*
1398 :key
#'find-classoid
)
1399 (not (find (classoid-layout (find-classoid 'sequence
))
1400 (layout-inherits (classoid-layout type1
)))))
1404 ((eq type2
*instance-type
*)
1405 (if (classoid-p type1
)
1406 (if (or (member type1
*non-instance-classoid-types
*
1407 :key
#'find-classoid
)
1408 (find (classoid-layout (find-classoid 'function
))
1409 (layout-inherits (classoid-layout type1
))))
1413 ((eq type2
*funcallable-instance-type
*)
1414 (if (classoid-p type1
)
1415 (if (or (member type1
*non-instance-classoid-types
*
1416 :key
#'find-classoid
)
1417 (not (find (classoid-layout (find-classoid 'function
))
1418 (layout-inherits (classoid-layout type1
)))))
1420 (if (eq type1
(specifier-type 'function
))
1424 (t (hierarchical-union2 type1 type2
))))
1426 (!define-type-method
(named :negate
) (x)
1427 (aver (not (eq x
*wild-type
*)))
1429 ((eq x
*universal-type
*) *empty-type
*)
1430 ((eq x
*empty-type
*) *universal-type
*)
1431 ((or (eq x
*instance-type
*)
1432 (eq x
*funcallable-instance-type
*)
1433 (eq x
*extended-sequence-type
*))
1434 (make-negation-type :type x
))
1435 (t (bug "NAMED type unexpected: ~S" x
))))
1437 (!define-type-method
(named :unparse
) (x)
1438 (named-type-name x
))
1440 ;;;; hairy and unknown types
1441 ;;;; DEFINE-TYPE-CLASS HAIRY is in 'early-type'
1443 (!define-type-method
(hairy :negate
) (x)
1444 (make-negation-type :type x
))
1446 (!define-type-method
(hairy :unparse
) (x)
1447 (hairy-type-specifier x
))
1449 (!define-type-method
(hairy :simple-subtypep
) (type1 type2
)
1450 (let ((hairy-spec1 (hairy-type-specifier type1
))
1451 (hairy-spec2 (hairy-type-specifier type2
)))
1452 (cond ((equal-but-no-car-recursion hairy-spec1 hairy-spec2
)
1454 ((maybe-reparse-specifier! type1
)
1455 (csubtypep type1 type2
))
1456 ((maybe-reparse-specifier! type2
)
1457 (csubtypep type1 type2
))
1459 (values nil nil
)))))
1461 (!define-type-method
(hairy :complex-subtypep-arg2
) (type1 type2
)
1462 (if (maybe-reparse-specifier! type2
)
1463 (csubtypep type1 type2
)
1464 (let ((specifier (hairy-type-specifier type2
)))
1465 (cond ((and (consp specifier
) (eql (car specifier
) 'satisfies
))
1466 (case (cadr specifier
)
1467 ((keywordp) (if (type= type1
(specifier-type 'symbol
))
1469 (invoke-complex-subtypep-arg1-method type1 type2
)))
1470 (t (invoke-complex-subtypep-arg1-method type1 type2
))))
1472 (invoke-complex-subtypep-arg1-method type1 type2
))))))
1474 (!define-type-method
(hairy :complex-subtypep-arg1
) (type1 type2
)
1475 (if (maybe-reparse-specifier! type1
)
1476 (csubtypep type1 type2
)
1479 (!define-type-method
(hairy :complex-
=) (type1 type2
)
1480 (if (maybe-reparse-specifier! type2
)
1484 (!define-type-method
(hairy :simple-intersection2
:complex-intersection2
)
1486 (cond ((type= type1 type2
)
1488 ((eq type2
*satisfies-keywordp-type
*)
1489 ;; (AND (MEMBER A) (SATISFIES KEYWORDP)) is possibly non-empty
1490 ;; if A is re-homed as :A. However as a special case that really
1491 ;; does occur, (AND (MEMBER NIL) (SATISFIES KEYWORDP))
1492 ;; is empty because of the illegality of changing NIL's package.
1493 (if (eq type1
*null-type
*)
1495 (multiple-value-bind (answer certain
)
1496 (types-equal-or-intersect type1
(specifier-type 'symbol
))
1497 (if (and (not answer
) certain
)
1500 ((eq type2
*fun-name-type
*)
1501 (multiple-value-bind (answer certain
)
1502 (types-equal-or-intersect type1
(specifier-type 'symbol
))
1503 (if (and (not answer
) certain
)
1504 (multiple-value-bind (answer certain
)
1505 (types-equal-or-intersect type1
(specifier-type 'cons
))
1506 (if (and (not answer
) certain
)
1512 (!define-type-method
(hairy :simple-union2
)
1514 (if (type= type1 type2
)
1518 (!define-type-method
(hairy :simple-
=) (type1 type2
)
1519 (if (equal-but-no-car-recursion (hairy-type-specifier type1
)
1520 (hairy-type-specifier type2
))
1524 (!def-type-translator satisfies
(&whole whole fun
)
1525 (declare (ignore fun
))
1526 ;; Check legality of arguments.
1527 (destructuring-bind (satisfies predicate-name
) whole
1528 (declare (ignore satisfies
))
1529 (unless (symbolp predicate-name
)
1530 (error 'simple-type-error
1531 :datum predicate-name
1532 :expected-type
'symbol
1533 :format-control
"The SATISFIES predicate name is not a symbol: ~S"
1534 :format-arguments
(list predicate-name
)))
1536 (case predicate-name
1537 (keywordp *satisfies-keywordp-type
*)
1538 (legal-fun-name-p *fun-name-type
*)
1539 (t (%make-hairy-type whole
)))))
1543 (!define-type-method
(negation :negate
) (x)
1544 (negation-type-type x
))
1546 (!define-type-method
(negation :unparse
) (x)
1547 (if (type= (negation-type-type x
) (specifier-type 'cons
))
1549 `(not ,(type-specifier (negation-type-type x
)))))
1551 (!define-type-method
(negation :simple-subtypep
) (type1 type2
)
1552 (csubtypep (negation-type-type type2
) (negation-type-type type1
)))
1554 (!define-type-method
(negation :complex-subtypep-arg2
) (type1 type2
)
1555 (let* ((complement-type2 (negation-type-type type2
))
1556 (intersection2 (type-intersection2 type1
1559 ;; FIXME: if uncertain, maybe try arg1?
1560 (type= intersection2
*empty-type
*)
1561 (invoke-complex-subtypep-arg1-method type1 type2
))))
1563 (!define-type-method
(negation :complex-subtypep-arg1
) (type1 type2
)
1564 ;; "Incrementally extended heuristic algorithms tend inexorably toward the
1565 ;; incomprehensible." -- http://www.unlambda.com/~james/lambda/lambda.txt
1567 ;; You may not believe this. I couldn't either. But then I sat down
1568 ;; and drew lots of Venn diagrams. Comments involving a and b refer
1569 ;; to the call (subtypep '(not a) 'b) -- CSR, 2002-02-27.
1571 ;; (Several logical truths in this block are true as long as
1572 ;; b/=T. As of sbcl-0.7.1.28, it seems impossible to construct a
1573 ;; case with b=T where we actually reach this type method, but
1574 ;; we'll test for and exclude this case anyway, since future
1575 ;; maintenance might make it possible for it to end up in this
1577 (multiple-value-bind (equal certain
)
1578 (type= type2
*universal-type
*)
1580 (return (values nil nil
)))
1582 (return (values t t
))))
1583 (let ((complement-type1 (negation-type-type type1
)))
1584 ;; Do the special cases first, in order to give us a chance if
1585 ;; subtype/supertype relationships are hairy.
1586 (multiple-value-bind (equal certain
)
1587 (type= complement-type1 type2
)
1588 ;; If a = b, ~a is not a subtype of b (unless b=T, which was
1591 (return (values nil nil
)))
1593 (return (values nil t
))))
1594 ;; KLUDGE: ANSI requires that the SUBTYPEP result between any
1595 ;; two built-in atomic type specifiers never be uncertain. This
1596 ;; is hard to do cleanly for the built-in types whose
1597 ;; definitions include (NOT FOO), i.e. CONS and RATIO. However,
1598 ;; we can do it with this hack, which uses our global knowledge
1599 ;; that our implementation of the type system uses disjoint
1600 ;; implementation types to represent disjoint sets (except when
1601 ;; types are contained in other types). (This is a KLUDGE
1602 ;; because it's fragile. Various changes in internal
1603 ;; representation in the type system could make it start
1604 ;; confidently returning incorrect results.) -- WHN 2002-03-08
1605 (unless (or (type-might-contain-other-types-p complement-type1
)
1606 (type-might-contain-other-types-p type2
))
1607 ;; Because of the way our types which don't contain other
1608 ;; types are disjoint subsets of the space of possible values,
1609 ;; (SUBTYPEP '(NOT AA) 'B)=NIL when AA and B are simple (and B
1610 ;; is not T, as checked above).
1611 (return (values nil t
)))
1612 ;; The old (TYPE= TYPE1 TYPE2) branch would never be taken, as
1613 ;; TYPE1 and TYPE2 will only be equal if they're both NOT types,
1614 ;; and then the :SIMPLE-SUBTYPEP method would be used instead.
1615 ;; But a CSUBTYPEP relationship might still hold:
1616 (multiple-value-bind (equal certain
)
1617 (csubtypep complement-type1 type2
)
1618 ;; If a is a subtype of b, ~a is not a subtype of b (unless
1619 ;; b=T, which was excluded above).
1621 (return (values nil nil
)))
1623 (return (values nil t
))))
1624 (multiple-value-bind (equal certain
)
1625 (csubtypep type2 complement-type1
)
1626 ;; If b is a subtype of a, ~a is not a subtype of b. (FIXME:
1627 ;; That's not true if a=T. Do we know at this point that a is
1630 (return (values nil nil
)))
1632 (return (values nil t
))))
1633 ;; old CSR comment ca. 0.7.2, now obsoleted by the SIMPLE-CTYPE?
1634 ;; KLUDGE case above: Other cases here would rely on being able
1635 ;; to catch all possible cases, which the fragility of this type
1636 ;; system doesn't inspire me; for instance, if a is type= to ~b,
1637 ;; then we want T, T; if this is not the case and the types are
1638 ;; disjoint (have an intersection of *empty-type*) then we want
1639 ;; NIL, T; else if the union of a and b is the *universal-type*
1640 ;; then we want T, T. So currently we still claim to be unsure
1641 ;; about e.g. (subtypep '(not fixnum) 'single-float).
1643 ;; OTOH we might still get here:
1646 (!define-type-method
(negation :complex-
=) (type1 type2
)
1647 ;; (NOT FOO) isn't equivalent to anything that's not a negation
1648 ;; type, except possibly a type that might contain it in disguise.
1649 (declare (ignore type2
))
1650 (if (type-might-contain-other-types-p type1
)
1654 (!define-type-method
(negation :simple-intersection2
) (type1 type2
)
1655 (let ((not1 (negation-type-type type1
))
1656 (not2 (negation-type-type type2
)))
1658 ((csubtypep not1 not2
) type2
)
1659 ((csubtypep not2 not1
) type1
)
1660 ;; Why no analagous clause to the disjoint in the SIMPLE-UNION2
1661 ;; method, below? The clause would read
1663 ;; ((EQ (TYPE-UNION NOT1 NOT2) *UNIVERSAL-TYPE*) *EMPTY-TYPE*)
1665 ;; but with proper canonicalization of negation types, there's
1666 ;; no way of constructing two negation types with union of their
1667 ;; negations being the universal type.
1669 (aver (not (eq (type-union not1 not2
) *universal-type
*)))
1672 (defun maybe-complex-array-refinement (type1 type2
)
1673 (let* ((ntype (negation-type-type type2
))
1674 (ndims (array-type-dimensions ntype
))
1675 (ncomplexp (array-type-complexp ntype
))
1676 (nseltype (array-type-specialized-element-type ntype
))
1677 (neltype (array-type-element-type ntype
)))
1678 (if (and (eql ndims
'*) (null ncomplexp
)
1679 (eq neltype
*wild-type
*) (eq nseltype
*wild-type
*))
1680 (make-array-type (array-type-dimensions type1
)
1682 :element-type
(array-type-element-type type1
)
1683 :specialized-element-type
(array-type-specialized-element-type type1
)))))
1685 (!define-type-method
(negation :complex-intersection2
) (type1 type2
)
1687 ((csubtypep type1
(negation-type-type type2
)) *empty-type
*)
1688 ((eq (type-intersection type1
(negation-type-type type2
)) *empty-type
*)
1690 ((and (array-type-p type1
) (array-type-p (negation-type-type type2
)))
1691 (maybe-complex-array-refinement type1 type2
))
1694 (!define-type-method
(negation :simple-union2
) (type1 type2
)
1695 (let ((not1 (negation-type-type type1
))
1696 (not2 (negation-type-type type2
)))
1698 ((csubtypep not1 not2
) type1
)
1699 ((csubtypep not2 not1
) type2
)
1700 ((eq (type-intersection not1 not2
) *empty-type
*)
1704 (!define-type-method
(negation :complex-union2
) (type1 type2
)
1706 ((csubtypep (negation-type-type type2
) type1
) *universal-type
*)
1707 ((eq (type-intersection type1
(negation-type-type type2
)) *empty-type
*)
1711 (!define-type-method
(negation :simple-
=) (type1 type2
)
1712 (type= (negation-type-type type1
) (negation-type-type type2
)))
1714 (!def-type-translator not
(typespec)
1715 (type-negation (specifier-type typespec
)))
1719 (!define-type-class number
:enumerable
#'numeric-type-enumerable
1720 :might-contain-other-types nil
)
1722 (declaim (inline numeric-type-equal
))
1723 (defun numeric-type-equal (type1 type2
)
1724 (and (eq (numeric-type-class type1
) (numeric-type-class type2
))
1725 (eq (numeric-type-format type1
) (numeric-type-format type2
))
1726 (eq (numeric-type-complexp type1
) (numeric-type-complexp type2
))))
1728 (!define-type-method
(number :simple-
=) (type1 type2
)
1730 (and (numeric-type-equal type1 type2
)
1731 (equalp (numeric-type-low type1
) (numeric-type-low type2
))
1732 (equalp (numeric-type-high type1
) (numeric-type-high type2
)))
1735 (!define-type-method
(number :negate
) (type)
1736 (if (and (null (numeric-type-low type
)) (null (numeric-type-high type
)))
1737 (make-negation-type :type type
)
1740 :type
(modified-numeric-type type
:low nil
:high nil
))
1742 ((null (numeric-type-low type
))
1743 (modified-numeric-type
1745 :low
(let ((h (numeric-type-high type
)))
1746 (if (consp h
) (car h
) (list h
)))
1748 ((null (numeric-type-high type
))
1749 (modified-numeric-type
1752 :high
(let ((l (numeric-type-low type
)))
1753 (if (consp l
) (car l
) (list l
)))))
1755 (modified-numeric-type
1758 :high
(let ((l (numeric-type-low type
)))
1759 (if (consp l
) (car l
) (list l
))))
1760 (modified-numeric-type
1762 :low
(let ((h (numeric-type-high type
)))
1763 (if (consp h
) (car h
) (list h
)))
1766 (!define-type-method
(number :unparse
) (type)
1767 (let* ((complexp (numeric-type-complexp type
))
1768 (low (numeric-type-low type
))
1769 (high (numeric-type-high type
))
1770 (base (case (numeric-type-class type
)
1772 (rational 'rational
)
1773 (float (or (numeric-type-format type
) 'float
))
1776 (cond ((and (eq base
'integer
) high low
)
1777 (let ((high-count (logcount high
))
1778 (high-length (integer-length high
)))
1780 (cond ((= high
0) '(integer 0 0))
1782 ((and (= high-count high-length
)
1783 (plusp high-length
))
1784 `(unsigned-byte ,high-length
))
1786 `(mod ,(1+ high
)))))
1787 ((and (= low sb
!xc
:most-negative-fixnum
)
1788 (= high sb
!xc
:most-positive-fixnum
))
1790 ((and (= low
(lognot high
))
1791 (= high-count high-length
)
1793 `(signed-byte ,(1+ high-length
)))
1795 `(integer ,low
,high
)))))
1796 (high `(,base
,(or low
'*) ,high
))
1798 (if (and (eq base
'integer
) (= low
0))
1806 (aver (neq base
+bounds
'real
))
1807 `(complex ,base
+bounds
))
1809 (aver (eq base
+bounds
'real
))
1812 (!define-type-method
(number :singleton-p
) (type)
1813 (let ((low (numeric-type-low type
))
1814 (high (numeric-type-high type
)))
1817 (eql (numeric-type-complexp type
) :real
)
1818 (member (numeric-type-class type
) '(integer rational
1819 #-sb-xc-host float
)))
1820 (values t
(numeric-type-low type
))
1823 ;;; Return true if X is "less than or equal" to Y, taking open bounds
1824 ;;; into consideration. CLOSED is the predicate used to test the bound
1825 ;;; on a closed interval (e.g. <=), and OPEN is the predicate used on
1826 ;;; open bounds (e.g. <). Y is considered to be the outside bound, in
1827 ;;; the sense that if it is infinite (NIL), then the test succeeds,
1828 ;;; whereas if X is infinite, then the test fails (unless Y is also
1831 ;;; This is for comparing bounds of the same kind, e.g. upper and
1832 ;;; upper. Use NUMERIC-BOUND-TEST* for different kinds of bounds.
1833 (defmacro numeric-bound-test
(x y closed open
)
1838 (,closed
(car ,x
) (car ,y
))
1839 (,closed
(car ,x
) ,y
)))
1845 ;;; This is used to compare upper and lower bounds. This is different
1846 ;;; from the same-bound case:
1847 ;;; -- Since X = NIL is -infinity, whereas y = NIL is +infinity, we
1848 ;;; return true if *either* arg is NIL.
1849 ;;; -- an open inner bound is "greater" and also squeezes the interval,
1850 ;;; causing us to use the OPEN test for those cases as well.
1851 (defmacro numeric-bound-test
* (x y closed open
)
1856 (,open
(car ,x
) (car ,y
))
1857 (,open
(car ,x
) ,y
)))
1863 ;;; Return whichever of the numeric bounds X and Y is "maximal"
1864 ;;; according to the predicates CLOSED (e.g. >=) and OPEN (e.g. >).
1865 ;;; This is only meaningful for maximizing like bounds, i.e. upper and
1866 ;;; upper. If MAX-P is true, then we return NIL if X or Y is NIL,
1867 ;;; otherwise we return the other arg.
1868 (defmacro numeric-bound-max
(x y closed open max-p
)
1871 `(cond ((not ,n-x
) ,(if max-p nil n-y
))
1872 ((not ,n-y
) ,(if max-p nil n-x
))
1875 (if (,closed
(car ,n-x
) (car ,n-y
)) ,n-x
,n-y
)
1876 (if (,open
(car ,n-x
) ,n-y
) ,n-x
,n-y
)))
1879 (if (,open
(car ,n-y
) ,n-x
) ,n-y
,n-x
)
1880 (if (,closed
,n-y
,n-x
) ,n-y
,n-x
))))))
1882 (!define-type-method
(number :simple-subtypep
) (type1 type2
)
1883 (let ((class1 (numeric-type-class type1
))
1884 (class2 (numeric-type-class type2
))
1885 (complexp2 (numeric-type-complexp type2
))
1886 (format2 (numeric-type-format type2
))
1887 (low1 (numeric-type-low type1
))
1888 (high1 (numeric-type-high type1
))
1889 (low2 (numeric-type-low type2
))
1890 (high2 (numeric-type-high type2
)))
1891 ;; If one is complex and the other isn't, they are disjoint.
1892 (cond ((not (or (eq (numeric-type-complexp type1
) complexp2
)
1895 ;; If the classes are specified and different, the types are
1896 ;; disjoint unless type2 is RATIONAL and type1 is INTEGER.
1897 ;; [ or type1 is INTEGER and type2 is of the form (RATIONAL
1898 ;; X X) for integral X, but this is dealt with in the
1899 ;; canonicalization inside MAKE-NUMERIC-TYPE ]
1900 ((not (or (eq class1 class2
)
1902 (and (eq class1
'integer
) (eq class2
'rational
))))
1904 ;; If the float formats are specified and different, the types
1906 ((not (or (eq (numeric-type-format type1
) format2
)
1909 ;; Check the bounds.
1910 ((and (numeric-bound-test low1 low2
>= >)
1911 (numeric-bound-test high1 high2
<= <))
1916 (!define-superclasses number
((number)) !cold-init-forms
)
1918 ;;; If the high bound of LOW is adjacent to the low bound of HIGH,
1919 ;;; then return true, otherwise NIL.
1920 (defun numeric-types-adjacent (low high
)
1921 (let ((low-bound (numeric-type-high low
))
1922 (high-bound (numeric-type-low high
)))
1923 (cond ((not (and low-bound high-bound
)) nil
)
1924 ((and (consp low-bound
) (consp high-bound
)) nil
)
1926 (let ((low-value (car low-bound
)))
1927 (or (eql low-value high-bound
)
1929 (load-time-value (make-unportable-float
1930 :single-float-negative-zero
)))
1931 (eql high-bound
0f0
))
1932 (and (eql low-value
0f0
)
1934 (load-time-value (make-unportable-float
1935 :single-float-negative-zero
))))
1937 (load-time-value (make-unportable-float
1938 :double-float-negative-zero
)))
1939 (eql high-bound
0d0
))
1940 (and (eql low-value
0d0
)
1942 (load-time-value (make-unportable-float
1943 :double-float-negative-zero
)))))))
1945 (let ((high-value (car high-bound
)))
1946 (or (eql high-value low-bound
)
1947 (and (eql high-value
1948 (load-time-value (make-unportable-float
1949 :single-float-negative-zero
)))
1950 (eql low-bound
0f0
))
1951 (and (eql high-value
0f0
)
1953 (load-time-value (make-unportable-float
1954 :single-float-negative-zero
))))
1955 (and (eql high-value
1956 (load-time-value (make-unportable-float
1957 :double-float-negative-zero
)))
1958 (eql low-bound
0d0
))
1959 (and (eql high-value
0d0
)
1961 (load-time-value (make-unportable-float
1962 :double-float-negative-zero
)))))))
1963 ((and (eq (numeric-type-class low
) 'integer
)
1964 (eq (numeric-type-class high
) 'integer
))
1965 (eql (1+ low-bound
) high-bound
))
1969 ;;; Return a numeric type that is a supertype for both TYPE1 and TYPE2.
1971 ;;; Binding *APPROXIMATE-NUMERIC-UNIONS* to T allows merging non-adjacent
1972 ;;; numeric types, eg (OR (INTEGER 0 12) (INTEGER 20 128)) => (INTEGER 0 128),
1973 ;;; the compiler does this occasionally during type-derivation to avoid
1974 ;;; creating absurdly complex unions of numeric types.
1975 (defvar *approximate-numeric-unions
* nil
)
1977 (!define-type-method
(number :simple-union2
) (type1 type2
)
1978 (declare (type numeric-type type1 type2
))
1979 (cond ((csubtypep type1 type2
) type2
)
1980 ((csubtypep type2 type1
) type1
)
1982 (let ((class1 (numeric-type-class type1
))
1983 (format1 (numeric-type-format type1
))
1984 (complexp1 (numeric-type-complexp type1
))
1985 (class2 (numeric-type-class type2
))
1986 (format2 (numeric-type-format type2
))
1987 (complexp2 (numeric-type-complexp type2
)))
1989 ((and (eq class1 class2
)
1990 (eq format1 format2
)
1991 (eq complexp1 complexp2
)
1992 (or *approximate-numeric-unions
*
1993 (numeric-types-intersect type1 type2
)
1994 (numeric-types-adjacent type1 type2
)
1995 (numeric-types-adjacent type2 type1
)))
2000 :low
(numeric-bound-max (numeric-type-low type1
)
2001 (numeric-type-low type2
)
2003 :high
(numeric-bound-max (numeric-type-high type1
)
2004 (numeric-type-high type2
)
2006 ;; FIXME: These two clauses are almost identical, and the
2007 ;; consequents are in fact identical in every respect.
2008 ((and (eq class1
'rational
)
2009 (eq class2
'integer
)
2010 (eq format1 format2
)
2011 (eq complexp1 complexp2
)
2012 (integerp (numeric-type-low type2
))
2013 (integerp (numeric-type-high type2
))
2014 (= (numeric-type-low type2
) (numeric-type-high type2
))
2015 (or *approximate-numeric-unions
*
2016 (numeric-types-adjacent type1 type2
)
2017 (numeric-types-adjacent type2 type1
)))
2022 :low
(numeric-bound-max (numeric-type-low type1
)
2023 (numeric-type-low type2
)
2025 :high
(numeric-bound-max (numeric-type-high type1
)
2026 (numeric-type-high type2
)
2028 ((and (eq class1
'integer
)
2029 (eq class2
'rational
)
2030 (eq format1 format2
)
2031 (eq complexp1 complexp2
)
2032 (integerp (numeric-type-low type1
))
2033 (integerp (numeric-type-high type1
))
2034 (= (numeric-type-low type1
) (numeric-type-high type1
))
2035 (or *approximate-numeric-unions
*
2036 (numeric-types-adjacent type1 type2
)
2037 (numeric-types-adjacent type2 type1
)))
2042 :low
(numeric-bound-max (numeric-type-low type1
)
2043 (numeric-type-low type2
)
2045 :high
(numeric-bound-max (numeric-type-high type1
)
2046 (numeric-type-high type2
)
2051 (!cold-init-forms
;; is !PRECOMPUTE-TYPES not doing the right thing?
2052 (setf (info :type
:kind
'number
) :primitive
)
2053 (setf (info :type
:builtin
'number
)
2054 (make-numeric-type :complexp nil
)))
2056 (!def-type-translator complex
(&optional
(typespec '*))
2057 (if (eq typespec
'*)
2058 (specifier-type '(complex real
))
2059 (labels ((not-numeric ()
2060 (error "The component type for COMPLEX is not numeric: ~S"
2063 (error "The component type for COMPLEX is not a subtype of REAL: ~S"
2065 (complex1 (component-type)
2066 (unless (numeric-type-p component-type
)
2068 (when (eq (numeric-type-complexp component-type
) :complex
)
2070 (if (csubtypep component-type
(specifier-type '(eql 0)))
2072 (modified-numeric-type component-type
2073 :complexp
:complex
)))
2076 ((eq ctype
*empty-type
*) *empty-type
*)
2077 ((eq ctype
*universal-type
*) (not-real))
2078 ((typep ctype
'numeric-type
) (complex1 ctype
))
2079 ((typep ctype
'union-type
)
2081 (mapcar #'do-complex
(union-type-types ctype
))))
2082 ((typep ctype
'member-type
)
2084 (mapcar-member-type-members
2085 (lambda (x) (do-complex (ctype-of x
)))
2087 ((and (typep ctype
'intersection-type
)
2088 ;; FIXME: This is very much a
2089 ;; not-quite-worst-effort, but we are required to do
2090 ;; something here because of our representation of
2091 ;; RATIO as (AND RATIONAL (NOT INTEGER)): we must
2092 ;; allow users to ask about (COMPLEX RATIO). This
2093 ;; will of course fail to work right on such types
2094 ;; as (AND INTEGER (SATISFIES ZEROP))...
2095 (let ((numbers (remove-if-not
2097 (intersection-type-types ctype
))))
2099 (null (cdr numbers
))
2100 (eq (numeric-type-complexp (car numbers
)) :real
)
2101 (complex1 (car numbers
))))))
2103 (multiple-value-bind (subtypep certainly
)
2104 (csubtypep ctype
(specifier-type 'real
))
2105 (if (and (not subtypep
) certainly
)
2107 ;; ANSI just says that TYPESPEC is any subtype of
2108 ;; type REAL, not necessarily a NUMERIC-TYPE. In
2109 ;; particular, at this point TYPESPEC could legally
2110 ;; be a hairy type like (AND NUMBER (SATISFIES
2111 ;; REALP) (SATISFIES ZEROP)), in which case we fall
2112 ;; through the logic above and end up here,
2114 ;; FIXME: (COMPLEX NUMBER) is not rejected but should
2115 ;; be, as NUMBER is clearly not a subtype of real.
2116 (bug "~@<(known bug #145): The type ~S is too hairy to be ~
2117 used for a COMPLEX component.~:@>"
2119 (let ((ctype (specifier-type typespec
)))
2120 (do-complex ctype
)))))
2122 ;;; If X is *, return NIL, otherwise return the bound, which must be a
2123 ;;; member of TYPE or a one-element list of a member of TYPE.
2124 #!-sb-fluid
(declaim (inline canonicalized-bound
))
2125 (defun canonicalized-bound (bound type
)
2126 (cond ((eq bound
'*) nil
)
2127 ((or (sb!xc
:typep bound type
)
2129 (sb!xc
:typep
(car bound
) type
)
2130 (null (cdr bound
))))
2133 (error "Bound is not ~S, a ~S or a list of a ~S: ~S"
2139 (!def-type-translator integer
(&optional
(low '*) (high '*))
2140 (let* ((l (canonicalized-bound low
'integer
))
2141 (lb (if (consp l
) (1+ (car l
)) l
))
2142 (h (canonicalized-bound high
'integer
))
2143 (hb (if (consp h
) (1- (car h
)) h
)))
2144 (if (and hb lb
(< hb lb
))
2146 (make-numeric-type :class
'integer
2148 :enumerable
(not (null (and l h
)))
2152 (defmacro !def-bounded-type
(type class format
)
2153 `(!def-type-translator
,type
(&optional
(low '*) (high '*))
2154 (let ((lb (canonicalized-bound low
',type
))
2155 (hb (canonicalized-bound high
',type
)))
2156 (if (not (numeric-bound-test* lb hb
<= <))
2158 (make-numeric-type :class
',class
2163 (!def-bounded-type rational rational nil
)
2165 ;;; Unlike CMU CL, we represent the types FLOAT and REAL as
2166 ;;; UNION-TYPEs of more primitive types, in order to make
2167 ;;; type representation more unique, avoiding problems in the
2168 ;;; simplification of things like
2169 ;;; (subtypep '(or (single-float -1.0 1.0) (single-float 0.1))
2170 ;;; '(or (real -1 7) (single-float 0.1) (single-float -1.0 1.0)))
2171 ;;; When we allowed REAL to remain as a separate NUMERIC-TYPE,
2172 ;;; it was too easy for the first argument to be simplified to
2173 ;;; '(SINGLE-FLOAT -1.0), and for the second argument to be simplified
2174 ;;; to '(OR (REAL -1 7) (SINGLE-FLOAT 0.1)) and then for the
2175 ;;; SUBTYPEP to fail (returning NIL,T instead of T,T) because
2176 ;;; the first argument can't be seen to be a subtype of any of the
2177 ;;; terms in the second argument.
2179 ;;; The old CMU CL way was:
2180 ;;; (!def-bounded-type float float nil)
2181 ;;; (!def-bounded-type real nil nil)
2183 ;;; FIXME: If this new way works for a while with no weird new
2184 ;;; problems, we can go back and rip out support for separate FLOAT
2185 ;;; and REAL flavors of NUMERIC-TYPE. The new way was added in
2186 ;;; sbcl-0.6.11.22, 2001-03-21.
2188 ;;; FIXME: It's probably necessary to do something to fix the
2189 ;;; analogous problem with INTEGER and RATIONAL types. Perhaps
2190 ;;; bounded RATIONAL types should be represented as (OR RATIO INTEGER).
2191 (defun coerce-bound (bound type upperp inner-coerce-bound-fun
)
2192 (declare (type function inner-coerce-bound-fun
))
2195 (funcall inner-coerce-bound-fun bound type upperp
)))
2196 (defun inner-coerce-real-bound (bound type upperp
)
2197 #+sb-xc-host
(declare (ignore upperp
))
2198 (let #+sb-xc-host
()
2200 ((nl (load-time-value (symbol-value 'sb
!xc
:most-negative-long-float
)))
2201 (pl (load-time-value (symbol-value 'sb
!xc
:most-positive-long-float
))))
2202 (let ((nbound (if (consp bound
) (car bound
) bound
))
2203 (consp (consp bound
)))
2207 (list (rational nbound
))
2211 ((floatp nbound
) bound
)
2213 ;; Coerce to the widest float format available, to avoid
2214 ;; unnecessary loss of precision, but don't coerce
2215 ;; unrepresentable numbers, except on the host where we
2216 ;; shouldn't be making these types (but KLUDGE: can't even
2217 ;; assert portably that we're not).
2221 (when (< nbound nl
) (return-from inner-coerce-real-bound nl
)))
2223 (when (> nbound pl
) (return-from inner-coerce-real-bound pl
))))
2224 (let ((result (coerce nbound
'long-float
)))
2225 (if consp
(list result
) result
)))))))))
2226 (defun inner-coerce-float-bound (bound type upperp
)
2227 #+sb-xc-host
(declare (ignore upperp
))
2228 (let #+sb-xc-host
()
2230 ((nd (load-time-value (symbol-value 'sb
!xc
:most-negative-double-float
)))
2231 (pd (load-time-value (symbol-value 'sb
!xc
:most-positive-double-float
)))
2232 (ns (load-time-value (symbol-value 'sb
!xc
:most-negative-single-float
)))
2233 (ps (load-time-value
2234 (symbol-value 'sb
!xc
:most-positive-single-float
))))
2235 (let ((nbound (if (consp bound
) (car bound
) bound
))
2236 (consp (consp bound
)))
2240 ((typep nbound
'single-float
) bound
)
2245 (when (< nbound ns
) (return-from inner-coerce-float-bound ns
)))
2247 (when (> nbound ps
) (return-from inner-coerce-float-bound ps
))))
2248 (let ((result (coerce nbound
'single-float
)))
2249 (if consp
(list result
) result
)))))
2252 ((typep nbound
'double-float
) bound
)
2257 (when (< nbound nd
) (return-from inner-coerce-float-bound nd
)))
2259 (when (> nbound pd
) (return-from inner-coerce-float-bound pd
))))
2260 (let ((result (coerce nbound
'double-float
)))
2261 (if consp
(list result
) result
)))))))))
2262 (defun coerced-real-bound (bound type upperp
)
2263 (coerce-bound bound type upperp
#'inner-coerce-real-bound
))
2264 (defun coerced-float-bound (bound type upperp
)
2265 (coerce-bound bound type upperp
#'inner-coerce-float-bound
))
2266 (!def-type-translator real
(&optional
(low '*) (high '*))
2267 (specifier-type `(or (float ,(coerced-real-bound low
'float nil
)
2268 ,(coerced-real-bound high
'float t
))
2269 (rational ,(coerced-real-bound low
'rational nil
)
2270 ,(coerced-real-bound high
'rational t
)))))
2271 (!def-type-translator float
(&optional
(low '*) (high '*))
2273 `(or (single-float ,(coerced-float-bound low
'single-float nil
)
2274 ,(coerced-float-bound high
'single-float t
))
2275 (double-float ,(coerced-float-bound low
'double-float nil
)
2276 ,(coerced-float-bound high
'double-float t
))
2277 #!+long-float
,(error "stub: no long float support yet"))))
2279 (defmacro !define-float-format
(f)
2280 `(!def-bounded-type
,f float
,f
))
2282 ;; (!define-float-format short-float) ; it's a DEFTYPE
2283 (!define-float-format single-float
)
2284 (!define-float-format double-float
)
2285 ;; long-float support is dead.
2286 ;; (!define-float-format long-float) ; also a DEFTYPE
2288 (defun numeric-types-intersect (type1 type2
)
2289 (declare (type numeric-type type1 type2
))
2290 (let* ((class1 (numeric-type-class type1
))
2291 (class2 (numeric-type-class type2
))
2292 (complexp1 (numeric-type-complexp type1
))
2293 (complexp2 (numeric-type-complexp type2
))
2294 (format1 (numeric-type-format type1
))
2295 (format2 (numeric-type-format type2
))
2296 (low1 (numeric-type-low type1
))
2297 (high1 (numeric-type-high type1
))
2298 (low2 (numeric-type-low type2
))
2299 (high2 (numeric-type-high type2
)))
2300 ;; If one is complex and the other isn't, then they are disjoint.
2301 (cond ((not (or (eq complexp1 complexp2
)
2302 (null complexp1
) (null complexp2
)))
2304 ;; If either type is a float, then the other must either be
2305 ;; specified to be a float or unspecified. Otherwise, they
2307 ((and (eq class1
'float
)
2308 (not (member class2
'(float nil
)))) nil
)
2309 ((and (eq class2
'float
)
2310 (not (member class1
'(float nil
)))) nil
)
2311 ;; If the float formats are specified and different, the
2312 ;; types are disjoint.
2313 ((not (or (eq format1 format2
) (null format1
) (null format2
)))
2316 ;; Check the bounds. This is a bit odd because we must
2317 ;; always have the outer bound of the interval as the
2319 (if (numeric-bound-test high1 high2
<= <)
2320 (or (and (numeric-bound-test low1 low2
>= >)
2321 (numeric-bound-test* low1 high2
<= <))
2322 (and (numeric-bound-test low2 low1
>= >)
2323 (numeric-bound-test* low2 high1
<= <)))
2324 (or (and (numeric-bound-test* low2 high1
<= <)
2325 (numeric-bound-test low2 low1
>= >))
2326 (and (numeric-bound-test high2 high1
<= <)
2327 (numeric-bound-test* high2 low1
>= >))))))))
2329 ;;; Take the numeric bound X and convert it into something that can be
2330 ;;; used as a bound in a numeric type with the specified CLASS and
2331 ;;; FORMAT. If UP-P is true, then we round up as needed, otherwise we
2332 ;;; round down. UP-P true implies that X is a lower bound, i.e. (N) > N.
2334 ;;; This is used by NUMERIC-TYPE-INTERSECTION to mash the bound into
2335 ;;; the appropriate type number. X may only be a float when CLASS is
2338 ;;; ### Note: it is possible for the coercion to a float to overflow
2339 ;;; or underflow. This happens when the bound doesn't fit in the
2340 ;;; specified format. In this case, we should really return the
2341 ;;; appropriate {Most | Least}-{Positive | Negative}-XXX-Float float
2342 ;;; of desired format. But these conditions aren't currently signalled
2343 ;;; in any useful way.
2345 ;;; Also, when converting an open rational bound into a float we
2346 ;;; should probably convert it to a closed bound of the closest float
2347 ;;; in the specified format. KLUDGE: In general, open float bounds are
2348 ;;; screwed up. -- (comment from original CMU CL)
2349 (defun round-numeric-bound (x class format up-p
)
2351 (let ((cx (if (consp x
) (car x
) x
)))
2355 (if (and (consp x
) (integerp cx
))
2356 (if up-p
(1+ cx
) (1- cx
))
2357 (if up-p
(ceiling cx
) (floor cx
))))
2361 ((and format
(subtypep format
'double-float
))
2362 (if (<= most-negative-double-float cx most-positive-double-float
)
2366 (if (<= most-negative-single-float cx most-positive-single-float
)
2368 (coerce cx
(or format
'single-float
))
2370 (if (consp x
) (list res
) res
)))))
2373 ;;; Handle the case of type intersection on two numeric types. We use
2374 ;;; TYPES-EQUAL-OR-INTERSECT to throw out the case of types with no
2375 ;;; intersection. If an attribute in TYPE1 is unspecified, then we use
2376 ;;; TYPE2's attribute, which must be at least as restrictive. If the
2377 ;;; types intersect, then the only attributes that can be specified
2378 ;;; and different are the class and the bounds.
2380 ;;; When the class differs, we use the more restrictive class. The
2381 ;;; only interesting case is RATIONAL/INTEGER, since RATIONAL includes
2384 ;;; We make the result lower (upper) bound the maximum (minimum) of
2385 ;;; the argument lower (upper) bounds. We convert the bounds into the
2386 ;;; appropriate numeric type before maximizing. This avoids possible
2387 ;;; confusion due to mixed-type comparisons (but I think the result is
2389 (!define-type-method
(number :simple-intersection2
) (type1 type2
)
2390 (declare (type numeric-type type1 type2
))
2391 (if (numeric-types-intersect type1 type2
)
2392 (let* ((class1 (numeric-type-class type1
))
2393 (class2 (numeric-type-class type2
))
2394 (class (ecase class1
2396 ((integer float
) class1
)
2397 (rational (if (eq class2
'integer
)
2400 (format (or (numeric-type-format type1
)
2401 (numeric-type-format type2
))))
2405 :complexp
(or (numeric-type-complexp type1
)
2406 (numeric-type-complexp type2
))
2407 :low
(numeric-bound-max
2408 (round-numeric-bound (numeric-type-low type1
)
2410 (round-numeric-bound (numeric-type-low type2
)
2413 :high
(numeric-bound-max
2414 (round-numeric-bound (numeric-type-high type1
)
2416 (round-numeric-bound (numeric-type-high type2
)
2421 ;;; Given two float formats, return the one with more precision. If
2422 ;;; either one is null, return NIL.
2423 (defun float-format-max (f1 f2
)
2425 (dolist (f *float-formats
* (error "bad float format: ~S" f1
))
2426 (when (or (eq f f1
) (eq f f2
))
2429 ;;; Return the result of an operation on TYPE1 and TYPE2 according to
2430 ;;; the rules of numeric contagion. This is always NUMBER, some float
2431 ;;; format (possibly complex) or RATIONAL. Due to rational
2432 ;;; canonicalization, there isn't much we can do here with integers or
2433 ;;; rational complex numbers.
2435 ;;; If either argument is not a NUMERIC-TYPE, then return NUMBER. This
2436 ;;; is useful mainly for allowing types that are technically numbers,
2437 ;;; but not a NUMERIC-TYPE.
2438 (defun numeric-contagion (type1 type2
)
2439 (if (and (numeric-type-p type1
) (numeric-type-p type2
))
2440 (let ((class1 (numeric-type-class type1
))
2441 (class2 (numeric-type-class type2
))
2442 (format1 (numeric-type-format type1
))
2443 (format2 (numeric-type-format type2
))
2444 (complexp1 (numeric-type-complexp type1
))
2445 (complexp2 (numeric-type-complexp type2
)))
2446 (cond ((or (null complexp1
)
2448 (specifier-type 'number
))
2452 :format
(ecase class2
2453 (float (float-format-max format1 format2
))
2454 ((integer rational
) format1
)
2456 ;; A double-float with any real number is a
2459 (if (eq format1
'double-float
)
2462 ;; A long-float with any real number is a
2465 (if (eq format1
'long-float
)
2468 :complexp
(if (or (eq complexp1
:complex
)
2469 (eq complexp2
:complex
))
2472 ((eq class2
'float
) (numeric-contagion type2 type1
))
2473 ((and (eq complexp1
:real
) (eq complexp2
:real
))
2475 :class
(and class1 class2
'rational
)
2478 (specifier-type 'number
))))
2479 (specifier-type 'number
)))
2483 (!define-type-class array
:enumerable nil
2484 :might-contain-other-types nil
)
2486 (!define-type-method
(array :simple-
=) (type1 type2
)
2487 (cond ((not (and (equal (array-type-dimensions type1
)
2488 (array-type-dimensions type2
))
2489 (eq (array-type-complexp type1
)
2490 (array-type-complexp type2
))))
2492 ((or (unknown-type-p (array-type-element-type type1
))
2493 (unknown-type-p (array-type-element-type type2
)))
2494 (type= (array-type-element-type type1
)
2495 (array-type-element-type type2
)))
2497 (values (type= (array-type-specialized-element-type type1
)
2498 (array-type-specialized-element-type type2
))
2501 (!define-type-method
(array :negate
) (type)
2502 ;; FIXME (and hint to PFD): we're vulnerable here to attacks of the
2503 ;; form "are (AND ARRAY (NOT (ARRAY T))) and (OR (ARRAY BIT) (ARRAY
2504 ;; NIL) (ARRAY CHAR) ...) equivalent?" -- CSR, 2003-12-10
2505 ;; A symptom of the aforementioned is that the following are not TYPE=
2506 ;; (AND (VECTOR T) (NOT SIMPLE-ARRAY)) ; an ARRAY-TYPE
2507 ;; (AND (VECTOR T) (NOT SIMPLE-VECTOR)) ; an INTERSECTION-TYPE
2508 ;; even though (VECTOR T) makes it so that the (NOT) clause in each can
2509 ;; only provide one additional bit of information: that the vector
2510 ;; is complex as opposed to simple. The rank and element-type are fixed.
2511 (if (and (eq (array-type-dimensions type
) '*)
2512 (eq (array-type-complexp type
) 't
)
2513 (eq (array-type-element-type type
) *wild-type
*))
2514 ;; (NOT <hairy-array>) = either SIMPLE-ARRAY or (NOT ARRAY).
2515 ;; This is deliberately asymmetric - trying to say that NOT simple-array
2516 ;; equals hairy-array leads to infinite recursion.
2517 (type-union (make-array-type '* :complexp nil
2518 :element-type
*wild-type
*)
2520 :type
(make-array-type '* :element-type
*wild-type
*)))
2521 (make-negation-type :type type
)))
2523 (!define-type-method
(array :unparse
) (type)
2524 (let* ((dims (array-type-dimensions type
))
2525 ;; Compare the specialised element type and the
2526 ;; derived element type. If the derived type
2527 ;; is so small that it jumps to a smaller upgraded
2528 ;; element type, use the specialised element type.
2530 ;; This protects from unparsing
2531 ;; (and (vector (or bit symbol))
2532 ;; (vector (or bit character)))
2533 ;; i.e., the intersection of two T array types,
2535 (stype (array-type-specialized-element-type type
))
2536 (dtype (array-type-element-type type
))
2537 (utype (%upgraded-array-element-type dtype
))
2538 (eltype (type-specifier (if (type= stype utype
)
2541 (complexp (array-type-complexp type
)))
2542 (if (and (eq complexp t
) (not *unparse-allow-negation
*))
2543 (setq complexp
:maybe
))
2547 ((t) '(and array
(not simple-array
)))
2549 ((nil) 'simple-array
))
2551 ((t) `(and (array ,eltype
) (not simple-array
)))
2552 ((:maybe
) `(array ,eltype
))
2553 ((nil) `(simple-array ,eltype
)))))
2554 ((= (length dims
) 1)
2557 (if (eq (car dims
) '*)
2560 ((base-char #!-sb-unicode character
) 'base-string
)
2562 (t `(vector ,eltype
)))
2564 (bit `(bit-vector ,(car dims
)))
2565 ((base-char #!-sb-unicode character
)
2566 `(base-string ,(car dims
)))
2567 (t `(vector ,eltype
,(car dims
)))))))
2568 (if (eql complexp
:maybe
)
2570 `(and ,answer
(not simple-array
))))
2571 (if (eq (car dims
) '*)
2573 (bit 'simple-bit-vector
)
2574 ((base-char #!-sb-unicode character
) 'simple-base-string
)
2575 ((t) 'simple-vector
)
2576 (t `(simple-array ,eltype
(*))))
2578 (bit `(simple-bit-vector ,(car dims
)))
2579 ((base-char #!-sb-unicode character
)
2580 `(simple-base-string ,(car dims
)))
2581 ((t) `(simple-vector ,(car dims
)))
2582 (t `(simple-array ,eltype
,dims
))))))
2585 ((t) `(and (array ,eltype
,dims
) (not simple-array
)))
2586 ((:maybe
) `(array ,eltype
,dims
))
2587 ((nil) `(simple-array ,eltype
,dims
)))))))
2589 (!define-type-method
(array :simple-subtypep
) (type1 type2
)
2590 (let ((dims1 (array-type-dimensions type1
))
2591 (dims2 (array-type-dimensions type2
))
2592 (complexp2 (array-type-complexp type2
)))
2593 (cond (;; not subtypep unless dimensions are compatible
2594 (not (or (eq dims2
'*)
2595 (and (not (eq dims1
'*))
2596 ;; (sbcl-0.6.4 has trouble figuring out that
2597 ;; DIMS1 and DIMS2 must be lists at this
2598 ;; point, and knowing that is important to
2599 ;; compiling EVERY efficiently.)
2600 (= (length (the list dims1
))
2601 (length (the list dims2
)))
2602 (every (lambda (x y
)
2603 (or (eq y
'*) (eql x y
)))
2605 (the list dims2
)))))
2607 ;; not subtypep unless complexness is compatible
2608 ((not (or (eq complexp2
:maybe
)
2609 (eq (array-type-complexp type1
) complexp2
)))
2611 ;; Since we didn't fail any of the tests above, we win
2612 ;; if the TYPE2 element type is wild.
2613 ((eq (array-type-element-type type2
) *wild-type
*)
2615 (;; Since we didn't match any of the special cases above, if
2616 ;; either element type is unknown we can only give a good
2617 ;; answer if they are the same.
2618 (or (unknown-type-p (array-type-element-type type1
))
2619 (unknown-type-p (array-type-element-type type2
)))
2620 (if (type= (array-type-element-type type1
)
2621 (array-type-element-type type2
))
2624 (;; Otherwise, the subtype relationship holds iff the
2625 ;; types are equal, and they're equal iff the specialized
2626 ;; element types are identical.
2628 (values (type= (array-type-specialized-element-type type1
)
2629 (array-type-specialized-element-type type2
))
2632 (!define-superclasses array
2633 ((vector vector
) (array))
2636 (defun array-types-intersect (type1 type2
)
2637 (declare (type array-type type1 type2
))
2638 (let ((dims1 (array-type-dimensions type1
))
2639 (dims2 (array-type-dimensions type2
))
2640 (complexp1 (array-type-complexp type1
))
2641 (complexp2 (array-type-complexp type2
)))
2642 ;; See whether dimensions are compatible.
2643 (cond ((not (or (eq dims1
'*) (eq dims2
'*)
2644 (and (= (length dims1
) (length dims2
))
2645 (every (lambda (x y
)
2646 (or (eq x
'*) (eq y
'*) (= x y
)))
2649 ;; See whether complexpness is compatible.
2650 ((not (or (eq complexp1
:maybe
)
2651 (eq complexp2
:maybe
)
2652 (eq complexp1 complexp2
)))
2656 ;; If either element type is wild, then they intersect.
2657 ;; Otherwise, the types must be identical.
2659 ;; FIXME: There seems to have been a fair amount of
2660 ;; confusion about the distinction between requested element
2661 ;; type and specialized element type; here is one of
2662 ;; them. If we request an array to hold objects of an
2663 ;; unknown type, we can do no better than represent that
2664 ;; type as an array specialized on wild-type. We keep the
2665 ;; requested element-type in the -ELEMENT-TYPE slot, and
2666 ;; *WILD-TYPE* in the -SPECIALIZED-ELEMENT-TYPE. So, here,
2667 ;; we must test for the SPECIALIZED slot being *WILD-TYPE*,
2668 ;; not just the ELEMENT-TYPE slot. Maybe the return value
2669 ;; in that specific case should be T, NIL? Or maybe this
2670 ;; function should really be called
2671 ;; ARRAY-TYPES-COULD-POSSIBLY-INTERSECT? In any case, this
2672 ;; was responsible for bug #123, and this whole issue could
2673 ;; do with a rethink and/or a rewrite. -- CSR, 2002-08-21
2674 ((or (eq (array-type-specialized-element-type type1
) *wild-type
*)
2675 (eq (array-type-specialized-element-type type2
) *wild-type
*)
2676 (type= (array-type-specialized-element-type type1
)
2677 (array-type-specialized-element-type type2
)))
2683 (defun unite-array-types-complexp (type1 type2
)
2684 (let ((complexp1 (array-type-complexp type1
))
2685 (complexp2 (array-type-complexp type2
)))
2687 ((eq complexp1 complexp2
)
2688 ;; both types are the same complexp-ity
2689 (values complexp1 t
))
2690 ((eq complexp1
:maybe
)
2691 ;; type1 is wild-complexp
2692 (values :maybe type1
))
2693 ((eq complexp2
:maybe
)
2694 ;; type2 is wild-complexp
2695 (values :maybe type2
))
2697 ;; both types partition the complexp-space
2698 (values :maybe nil
)))))
2700 (defun unite-array-types-dimensions (type1 type2
)
2701 (let ((dims1 (array-type-dimensions type1
))
2702 (dims2 (array-type-dimensions type2
)))
2703 (cond ((equal dims1 dims2
)
2704 ;; both types are same dimensionality
2707 ;; type1 is wild-dimensions
2710 ;; type2 is wild-dimensions
2712 ((not (= (length dims1
) (length dims2
)))
2713 ;; types have different number of dimensions
2714 (values :incompatible nil
))
2716 ;; we need to check on a per-dimension basis
2717 (let* ((supertype1 t
)
2720 (result (mapcar (lambda (dim1 dim2
)
2725 (setf supertype2 nil
)
2728 (setf supertype1 nil
)
2731 (setf compatible nil
))))
2734 ((or (not compatible
)
2735 (and (not supertype1
)
2737 (values :incompatible nil
))
2738 ((and supertype1 supertype2
)
2739 (values result supertype1
))
2741 (values result
(if supertype1 type1 type2
)))))))))
2743 (defun unite-array-types-element-types (type1 type2
)
2744 ;; FIXME: We'd love to be able to unite the full set of specialized
2745 ;; array element types up to *wild-type*, but :simple-union2 is
2746 ;; performed pairwise, so we don't have a good hook for it and our
2747 ;; representation doesn't allow us to easily detect the situation
2749 ;; But see SIMPLIFY-ARRAY-UNIONS which is able to do something like that.
2750 (let* ((eltype1 (array-type-element-type type1
))
2751 (eltype2 (array-type-element-type type2
))
2752 (stype1 (array-type-specialized-element-type type1
))
2753 (stype2 (array-type-specialized-element-type type2
))
2754 (wild1 (eq eltype1
*wild-type
*))
2755 (wild2 (eq eltype2
*wild-type
*)))
2757 ((type= eltype1 eltype2
)
2758 (values eltype1 stype1 t
))
2760 (values eltype1 stype1 type1
))
2762 (values eltype2 stype2 type2
))
2763 ((not (type= stype1 stype2
))
2764 ;; non-wild types that don't share UAET don't unite
2765 (values :incompatible nil nil
))
2766 ((csubtypep eltype1 eltype2
)
2767 (values eltype2 stype2 type2
))
2768 ((csubtypep eltype2 eltype1
)
2769 (values eltype1 stype1 type1
))
2771 (values :incompatible nil nil
)))))
2773 (defun unite-array-types-supertypes-compatible-p (&rest supertypes
)
2774 ;; supertypes are compatible if they are all T, if there is a single
2775 ;; NIL and all the rest are T, or if all non-T supertypes are the
2776 ;; same and not NIL.
2777 (let ((interesting-supertypes
2778 (remove t supertypes
)))
2779 (or (not interesting-supertypes
)
2780 (equal interesting-supertypes
'(nil))
2781 ;; supertypes are (OR BOOLEAN ARRAY-TYPE), so...
2782 (typep (remove-duplicates interesting-supertypes
)
2783 '(cons array-type null
)))))
2785 (!define-type-method
(array :simple-union2
) (type1 type2
)
2786 (multiple-value-bind
2787 (result-eltype result-stype eltype-supertype
)
2788 (unite-array-types-element-types type1 type2
)
2789 (multiple-value-bind
2790 (result-complexp complexp-supertype
)
2791 (unite-array-types-complexp type1 type2
)
2792 (multiple-value-bind
2793 (result-dimensions dimensions-supertype
)
2794 (unite-array-types-dimensions type1 type2
)
2795 (when (and (not (eq result-dimensions
:incompatible
))
2796 (not (eq result-eltype
:incompatible
))
2797 (unite-array-types-supertypes-compatible-p
2798 eltype-supertype complexp-supertype dimensions-supertype
))
2799 (make-array-type result-dimensions
2800 :complexp result-complexp
2801 :element-type result-eltype
2802 :specialized-element-type result-stype
))))))
2804 (!define-type-method
(array :simple-intersection2
) (type1 type2
)
2805 (declare (type array-type type1 type2
))
2806 (if (array-types-intersect type1 type2
)
2807 (let ((dims1 (array-type-dimensions type1
))
2808 (dims2 (array-type-dimensions type2
))
2809 (complexp1 (array-type-complexp type1
))
2810 (complexp2 (array-type-complexp type2
))
2811 (eltype1 (array-type-element-type type1
))
2812 (eltype2 (array-type-element-type type2
))
2813 (stype1 (array-type-specialized-element-type type1
))
2814 (stype2 (array-type-specialized-element-type type2
)))
2815 (make-array-type (cond ((eq dims1
'*) dims2
)
2816 ((eq dims2
'*) dims1
)
2818 (mapcar (lambda (x y
) (if (eq x
'*) y x
))
2820 :complexp
(if (eq complexp1
:maybe
) complexp2 complexp1
)
2822 ((eq eltype1
*wild-type
*) eltype2
)
2823 ((eq eltype2
*wild-type
*) eltype1
)
2824 (t (type-intersection eltype1 eltype2
)))
2825 :specialized-element-type
(cond
2826 ((eq stype1
*wild-type
*) stype2
)
2827 ((eq stype2
*wild-type
*) stype1
)
2829 (aver (type= stype1 stype2
))
2833 ;;; Check a supplied dimension list to determine whether it is legal,
2834 ;;; and return it in canonical form (as either '* or a list).
2835 (defun canonical-array-dimensions (dims)
2840 (error "Arrays can't have a negative number of dimensions: ~S" dims
))
2841 (when (>= dims sb
!xc
:array-rank-limit
)
2842 (error "array type with too many dimensions: ~S" dims
))
2843 (make-list dims
:initial-element
'*))
2845 (when (>= (length dims
) sb
!xc
:array-rank-limit
)
2846 (error "array type with too many dimensions: ~S" dims
))
2849 (unless (and (integerp dim
)
2851 (< dim sb
!xc
:array-dimension-limit
))
2852 (error "bad dimension in array type: ~S" dim
))))
2855 (error "Array dimensions is not a list, integer or *:~% ~S" dims
))))
2859 (!define-type-class member
:enumerable t
2860 :might-contain-other-types nil
)
2862 (!define-type-method
(member :negate
) (type)
2863 (let ((xset (member-type-xset type
))
2864 (fp-zeroes (member-type-fp-zeroes type
)))
2866 ;; Hairy case, which needs to do a bit of float type
2867 ;; canonicalization.
2868 (apply #'type-intersection
2869 (if (xset-empty-p xset
)
2872 :type
(make-member-type :xset xset
)))
2875 (let* ((opposite (neg-fp-zero x
))
2876 (type (ctype-of opposite
)))
2879 :type
(modified-numeric-type type
:low nil
:high nil
))
2880 (modified-numeric-type type
:low nil
:high
(list opposite
))
2881 (make-member-type :members
(list opposite
))
2882 (modified-numeric-type type
:low
(list opposite
) :high nil
))))
2885 (make-negation-type :type type
))))
2887 (!define-type-method
(member :unparse
) (type)
2888 (let ((members (member-type-members type
)))
2889 (cond ((equal members
'(nil)) 'null
)
2890 (t `(member ,@members
)))))
2892 (!define-type-method
(member :singleton-p
) (type)
2893 (if (eql 1 (member-type-size type
))
2894 (values t
(first (member-type-members type
)))
2897 (!define-type-method
(member :simple-subtypep
) (type1 type2
)
2898 (values (and (xset-subset-p (member-type-xset type1
)
2899 (member-type-xset type2
))
2900 (subsetp (member-type-fp-zeroes type1
)
2901 (member-type-fp-zeroes type2
)))
2904 (!define-type-method
(member :complex-subtypep-arg1
) (type1 type2
)
2906 (mapc-member-type-members
2908 (multiple-value-bind (ok surep
) (ctypep elt type2
)
2910 (return-from punt
(values nil nil
)))
2912 (return-from punt
(values nil t
)))))
2916 ;;; We punt if the odd type is enumerable and intersects with the
2917 ;;; MEMBER type. If not enumerable, then it is definitely not a
2918 ;;; subtype of the MEMBER type.
2919 (!define-type-method
(member :complex-subtypep-arg2
) (type1 type2
)
2920 (cond ((not (type-enumerable type1
)) (values nil t
))
2921 ((types-equal-or-intersect type1 type2
)
2922 (invoke-complex-subtypep-arg1-method type1 type2
))
2923 (t (values nil t
))))
2925 (!define-type-method
(member :simple-intersection2
) (type1 type2
)
2926 (make-member-type :xset
(xset-intersection (member-type-xset type1
)
2927 (member-type-xset type2
))
2928 :fp-zeroes
(intersection (member-type-fp-zeroes type1
)
2929 (member-type-fp-zeroes type2
))))
2931 (!define-type-method
(member :complex-intersection2
) (type1 type2
)
2933 (let ((xset (alloc-xset))
2935 (mapc-member-type-members
2937 (multiple-value-bind (ok sure
) (ctypep member type1
)
2939 (return-from punt nil
))
2941 (if (fp-zero-p member
)
2942 (pushnew member fp-zeroes
)
2943 (add-to-xset member xset
)))))
2945 (if (and (xset-empty-p xset
) (not fp-zeroes
))
2947 (make-member-type :xset xset
:fp-zeroes fp-zeroes
)))))
2949 ;;; We don't need a :COMPLEX-UNION2, since the only interesting case is
2950 ;;; a union type, and the member/union interaction is handled by the
2951 ;;; union type method.
2952 (!define-type-method
(member :simple-union2
) (type1 type2
)
2953 (make-member-type :xset
(xset-union (member-type-xset type1
)
2954 (member-type-xset type2
))
2955 :fp-zeroes
(union (member-type-fp-zeroes type1
)
2956 (member-type-fp-zeroes type2
))))
2958 (!define-type-method
(member :simple-
=) (type1 type2
)
2959 (let ((xset1 (member-type-xset type1
))
2960 (xset2 (member-type-xset type2
))
2961 (l1 (member-type-fp-zeroes type1
))
2962 (l2 (member-type-fp-zeroes type2
)))
2963 (values (and (eql (xset-count xset1
) (xset-count xset2
))
2964 (xset-subset-p xset1 xset2
)
2965 (xset-subset-p xset2 xset1
)
2970 (!define-type-method
(member :complex-
=) (type1 type2
)
2971 (if (type-enumerable type1
)
2972 (multiple-value-bind (val win
) (csubtypep type2 type1
)
2973 (if (or val
(not win
))
2978 (!def-type-translator member
(&rest members
)
2980 (let (ms numbers char-codes
)
2981 (dolist (m (remove-duplicates members
))
2983 (float (if (zerop m
)
2985 (push (ctype-of m
) numbers
)))
2986 (real (push (ctype-of m
) numbers
))
2987 (character (push (sb!xc
:char-code m
) char-codes
))
2991 (make-member-type :members ms
)
2994 (make-character-set-type
2995 :pairs
(mapcar (lambda (x) (cons x x
))
2996 (sort char-codes
#'<)))
2998 (nreverse numbers
)))
3001 ;;;; intersection types
3003 ;;;; Until version 0.6.10.6, SBCL followed the original CMU CL approach
3004 ;;;; of punting on all AND types, not just the unreasonably complicated
3005 ;;;; ones. The change was motivated by trying to get the KEYWORD type
3006 ;;;; to behave sensibly:
3007 ;;;; ;; reasonable definition
3008 ;;;; (DEFTYPE KEYWORD () '(AND SYMBOL (SATISFIES KEYWORDP)))
3009 ;;;; ;; reasonable behavior
3010 ;;;; (AVER (SUBTYPEP 'KEYWORD 'SYMBOL))
3011 ;;;; Without understanding a little about the semantics of AND, we'd
3012 ;;;; get (SUBTYPEP 'KEYWORD 'SYMBOL)=>NIL,NIL and, for entirely
3013 ;;;; parallel reasons, (SUBTYPEP 'RATIO 'NUMBER)=>NIL,NIL. That's
3016 ;;;; We still follow the example of CMU CL to some extent, by punting
3017 ;;;; (to the opaque HAIRY-TYPE) on sufficiently complicated types
3020 (!define-type-class intersection
3021 :enumerable
#'compound-type-enumerable
3022 :might-contain-other-types t
)
3024 (!define-type-method
(intersection :negate
) (type)
3026 (mapcar #'type-negation
(intersection-type-types type
))))
3028 ;;; A few intersection types have special names. The others just get
3029 ;;; mechanically unparsed.
3030 (!define-type-method
(intersection :unparse
) (type)
3031 (declare (type ctype type
))
3032 (or (find type
'(ratio keyword compiled-function
) :key
#'specifier-type
:test
#'type
=)
3033 `(and ,@(mapcar #'type-specifier
(intersection-type-types type
)))))
3035 ;;; shared machinery for type equality: true if every type in the set
3036 ;;; TYPES1 matches a type in the set TYPES2 and vice versa
3037 (defun type=-set
(types1 types2
)
3038 (flet ((type<=-set
(x y
)
3039 (declare (type list x y
))
3040 (every/type
(lambda (x y-element
)
3041 (any/type
#'type
= y-element x
))
3043 (and/type
(type<=-set types1 types2
)
3044 (type<=-set types2 types1
))))
3046 ;;; Two intersection types are equal if their subtypes are equal sets.
3048 ;;; FIXME: Might it be better to use
3049 ;;; (AND (SUBTYPEP X Y) (SUBTYPEP Y X))
3050 ;;; instead, since SUBTYPEP is the usual relationship that we care
3051 ;;; most about, so it would be good to leverage any ingenuity there
3052 ;;; in this more obscure method?
3053 (!define-type-method
(intersection :simple-
=) (type1 type2
)
3054 (type=-set
(intersection-type-types type1
)
3055 (intersection-type-types type2
)))
3057 (defun %intersection-complex-subtypep-arg1
(type1 type2
)
3058 (type= type1
(type-intersection type1 type2
)))
3060 (defun %intersection-simple-subtypep
(type1 type2
)
3061 (every/type
#'%intersection-complex-subtypep-arg1
3063 (intersection-type-types type2
)))
3065 (!define-type-method
(intersection :simple-subtypep
) (type1 type2
)
3066 (%intersection-simple-subtypep type1 type2
))
3068 (!define-type-method
(intersection :complex-subtypep-arg1
) (type1 type2
)
3069 (%intersection-complex-subtypep-arg1 type1 type2
))
3071 (defun %intersection-complex-subtypep-arg2
(type1 type2
)
3072 (every/type
#'csubtypep type1
(intersection-type-types type2
)))
3074 (!define-type-method
(intersection :complex-subtypep-arg2
) (type1 type2
)
3075 (%intersection-complex-subtypep-arg2 type1 type2
))
3077 ;;; FIXME: This will look eeriely familiar to readers of the UNION
3078 ;;; :SIMPLE-INTERSECTION2 :COMPLEX-INTERSECTION2 method. That's
3079 ;;; because it was generated by cut'n'paste methods. Given that
3080 ;;; intersections and unions have all sorts of symmetries known to
3081 ;;; mathematics, it shouldn't be beyond the ken of some programmers to
3082 ;;; reflect those symmetries in code in a way that ties them together
3083 ;;; more strongly than having two independent near-copies :-/
3084 (!define-type-method
(intersection :simple-union2
:complex-union2
)
3086 ;; Within this method, type2 is guaranteed to be an intersection
3088 (aver (intersection-type-p type2
))
3089 ;; Make sure to call only the applicable methods...
3090 (cond ((and (intersection-type-p type1
)
3091 (%intersection-simple-subtypep type1 type2
)) type2
)
3092 ((and (intersection-type-p type1
)
3093 (%intersection-simple-subtypep type2 type1
)) type1
)
3094 ((and (not (intersection-type-p type1
))
3095 (%intersection-complex-subtypep-arg2 type1 type2
))
3097 ((and (not (intersection-type-p type1
))
3098 (%intersection-complex-subtypep-arg1 type2 type1
))
3100 ;; KLUDGE: This special (and somewhat hairy) magic is required
3101 ;; to deal with the RATIONAL/INTEGER special case. The UNION
3102 ;; of (INTEGER * -1) and (AND (RATIONAL * -1/2) (NOT INTEGER))
3103 ;; should be (RATIONAL * -1/2) -- CSR, 2003-02-28
3104 ((and (csubtypep type2
(specifier-type 'ratio
))
3105 (numeric-type-p type1
)
3106 (csubtypep type1
(specifier-type 'integer
))
3111 :low
(if (null (numeric-type-low type1
))
3113 (list (1- (numeric-type-low type1
))))
3114 :high
(if (null (numeric-type-high type1
))
3116 (list (1+ (numeric-type-high type1
)))))))
3117 (let* ((intersected (intersection-type-types type2
))
3118 (remaining (remove (specifier-type '(not integer
))
3121 (and (not (equal intersected remaining
))
3122 (type-union type1
(apply #'type-intersection remaining
)))))
3124 (let ((accumulator *universal-type
*))
3125 (do ((t2s (intersection-type-types type2
) (cdr t2s
)))
3126 ((null t2s
) accumulator
)
3127 (let ((union (type-union type1
(car t2s
))))
3128 (when (union-type-p union
)
3129 ;; we have to give up here -- there are all sorts of
3130 ;; ordering worries, but it's better than before.
3131 ;; Doing exactly the same as in the UNION
3132 ;; :SIMPLE/:COMPLEX-INTERSECTION2 method causes stack
3133 ;; overflow with the mutual recursion never bottoming
3135 (if (and (eq accumulator
*universal-type
*)
3137 ;; KLUDGE: if we get here, we have a partially
3138 ;; simplified result. While this isn't by any
3139 ;; means a universal simplification, including
3140 ;; this logic here means that we can get (OR
3141 ;; KEYWORD (NOT KEYWORD)) canonicalized to T.
3145 (type-intersection accumulator union
))))))))
3147 (!def-type-translator and
(&rest type-specifiers
)
3148 (apply #'type-intersection
3149 (mapcar #'specifier-type type-specifiers
)))
3153 (!define-type-class union
3154 :enumerable
#'compound-type-enumerable
3155 :might-contain-other-types t
)
3157 (!define-type-method
(union :negate
) (type)
3158 (declare (type ctype type
))
3159 (apply #'type-intersection
3160 (mapcar #'type-negation
(union-type-types type
))))
3162 ;;; The LIST, FLOAT and REAL types have special names. Other union
3163 ;;; types just get mechanically unparsed.
3164 (!define-type-method
(union :unparse
) (type)
3165 (declare (type ctype type
))
3167 ((type= type
(specifier-type 'list
)) 'list
)
3168 ((type= type
(specifier-type 'float
)) 'float
)
3169 ((type= type
(specifier-type 'real
)) 'real
)
3170 ((type= type
(specifier-type 'sequence
)) 'sequence
)
3171 ((type= type
(specifier-type 'bignum
)) 'bignum
)
3172 ((type= type
(specifier-type 'simple-string
)) 'simple-string
)
3173 ((type= type
(specifier-type 'string
)) 'string
)
3174 ((type= type
(specifier-type 'complex
)) 'complex
)
3175 ((type= type
(specifier-type 'standard-char
)) 'standard-char
)
3176 (t `(or ,@(mapcar #'type-specifier
(union-type-types type
))))))
3178 ;;; Two union types are equal if they are each subtypes of each
3179 ;;; other. We need to be this clever because our complex subtypep
3180 ;;; methods are now more accurate; we don't get infinite recursion
3181 ;;; because the simple-subtypep method delegates to complex-subtypep
3182 ;;; of the individual types of type1. - CSR, 2002-04-09
3184 ;;; Previous comment, now obsolete, but worth keeping around because
3185 ;;; it is true, though too strong a condition:
3187 ;;; Two union types are equal if their subtypes are equal sets.
3188 (!define-type-method
(union :simple-
=) (type1 type2
)
3189 (multiple-value-bind (subtype certain?
)
3190 (csubtypep type1 type2
)
3192 (csubtypep type2 type1
)
3193 ;; we might as well become as certain as possible.
3196 (multiple-value-bind (subtype certain?
)
3197 (csubtypep type2 type1
)
3198 (declare (ignore subtype
))
3199 (values nil certain?
))))))
3201 (!define-type-method
(union :complex-
=) (type1 type2
)
3202 (declare (ignore type1
))
3203 (if (some #'type-might-contain-other-types-p
3204 (union-type-types type2
))
3208 ;;; Similarly, a union type is a subtype of another if and only if
3209 ;;; every element of TYPE1 is a subtype of TYPE2.
3210 (defun union-simple-subtypep (type1 type2
)
3211 (every/type
(swapped-args-fun #'union-complex-subtypep-arg2
)
3213 (union-type-types type1
)))
3215 (!define-type-method
(union :simple-subtypep
) (type1 type2
)
3216 (union-simple-subtypep type1 type2
))
3218 (defun union-complex-subtypep-arg1 (type1 type2
)
3219 (every/type
(swapped-args-fun #'csubtypep
)
3221 (union-type-types type1
)))
3223 (!define-type-method
(union :complex-subtypep-arg1
) (type1 type2
)
3224 (union-complex-subtypep-arg1 type1 type2
))
3226 (defun union-complex-subtypep-arg2 (type1 type2
)
3227 ;; At this stage, we know that type2 is a union type and type1
3228 ;; isn't. We might as well check this, though:
3229 (aver (union-type-p type2
))
3230 (aver (not (union-type-p type1
)))
3231 ;; was: (any/type #'csubtypep type1 (union-type-types type2)), which
3232 ;; turns out to be too restrictive, causing bug 91.
3234 ;; the following reimplementation might look dodgy. It is dodgy. It
3235 ;; depends on the union :complex-= method not doing very much work
3236 ;; -- certainly, not using subtypep. Reasoning:
3238 ;; A is a subset of (B1 u B2)
3239 ;; <=> A n (B1 u B2) = A
3240 ;; <=> (A n B1) u (A n B2) = A
3242 ;; But, we have to be careful not to delegate this type= to
3243 ;; something that could invoke subtypep, which might get us back
3244 ;; here -> stack explosion. We therefore ensure that the second type
3245 ;; (which is the one that's dispatched on) is either a union type
3246 ;; (where we've ensured that the complex-= method will not call
3247 ;; subtypep) or something with no union types involved, in which
3248 ;; case we'll never come back here.
3250 ;; If we don't do this, then e.g.
3251 ;; (SUBTYPEP '(MEMBER 3) '(OR (SATISFIES FOO) (SATISFIES BAR)))
3252 ;; would loop infinitely, as the member :complex-= method is
3253 ;; implemented in terms of subtypep.
3255 ;; Ouch. - CSR, 2002-04-10
3256 (multiple-value-bind (sub-value sub-certain?
)
3259 (mapcar (lambda (x) (type-intersection type1 x
))
3260 (union-type-types type2
))))
3262 (values sub-value sub-certain?
)
3263 ;; The ANY/TYPE expression above is a sufficient condition for
3264 ;; subsetness, but not a necessary one, so we might get a more
3265 ;; certain answer by this CALL-NEXT-METHOD-ish step when the
3266 ;; ANY/TYPE expression is uncertain.
3267 (invoke-complex-subtypep-arg1-method type1 type2
))))
3269 (!define-type-method
(union :complex-subtypep-arg2
) (type1 type2
)
3270 (union-complex-subtypep-arg2 type1 type2
))
3272 (!define-type-method
(union :simple-intersection2
:complex-intersection2
)
3274 ;; The CSUBTYPEP clauses here let us simplify e.g.
3275 ;; (TYPE-INTERSECTION2 (SPECIFIER-TYPE 'LIST)
3276 ;; (SPECIFIER-TYPE '(OR LIST VECTOR)))
3277 ;; (where LIST is (OR CONS NULL)).
3279 ;; The tests are more or less (CSUBTYPEP TYPE1 TYPE2) and vice
3280 ;; versa, but it's important that we pre-expand them into
3281 ;; specialized operations on individual elements of
3282 ;; UNION-TYPE-TYPES, instead of using the ordinary call to
3283 ;; CSUBTYPEP, in order to avoid possibly invoking any methods which
3284 ;; might in turn invoke (TYPE-INTERSECTION2 TYPE1 TYPE2) and thus
3285 ;; cause infinite recursion.
3287 ;; Within this method, type2 is guaranteed to be a union type:
3288 (aver (union-type-p type2
))
3289 ;; Make sure to call only the applicable methods...
3290 (cond ((and (union-type-p type1
)
3291 (union-simple-subtypep type1 type2
)) type1
)
3292 ((and (union-type-p type1
)
3293 (union-simple-subtypep type2 type1
)) type2
)
3294 ((and (not (union-type-p type1
))
3295 (union-complex-subtypep-arg2 type1 type2
))
3297 ((and (not (union-type-p type1
))
3298 (union-complex-subtypep-arg1 type2 type1
))
3301 ;; KLUDGE: This code accumulates a sequence of TYPE-UNION2
3302 ;; operations in a particular order, and gives up if any of
3303 ;; the sub-unions turn out not to be simple. In other cases
3304 ;; ca. sbcl-0.6.11.15, that approach to taking a union was a
3305 ;; bad idea, since it can overlook simplifications which
3306 ;; might occur if the terms were accumulated in a different
3307 ;; order. It's possible that that will be a problem here too.
3308 ;; However, I can't think of a good example to demonstrate
3309 ;; it, and without an example to demonstrate it I can't write
3310 ;; test cases, and without test cases I don't want to
3311 ;; complicate the code to address what's still a hypothetical
3312 ;; problem. So I punted. -- WHN 2001-03-20
3313 (let ((accumulator *empty-type
*))
3314 (dolist (t2 (union-type-types type2
) accumulator
)
3316 (type-union accumulator
3317 (type-intersection type1 t2
))))))))
3319 (!def-type-translator or
(&rest type-specifiers
)
3320 (let ((type (apply #'type-union
3321 (mapcar #'specifier-type type-specifiers
))))
3322 (if (union-type-p type
)
3323 (sb!kernel
::simplify-array-unions type
)
3328 (!define-type-class cons
:enumerable nil
:might-contain-other-types nil
)
3330 (!def-type-translator cons
(&optional
(car-type-spec '*) (cdr-type-spec '*))
3331 (let ((car-type (single-value-specifier-type car-type-spec
))
3332 (cdr-type (single-value-specifier-type cdr-type-spec
)))
3333 (make-cons-type car-type cdr-type
)))
3335 (!define-type-method
(cons :negate
) (type)
3336 (if (and (eq (cons-type-car-type type
) *universal-type
*)
3337 (eq (cons-type-cdr-type type
) *universal-type
*))
3338 (make-negation-type :type type
)
3340 (make-negation-type :type
(specifier-type 'cons
))
3342 ((and (not (eq (cons-type-car-type type
) *universal-type
*))
3343 (not (eq (cons-type-cdr-type type
) *universal-type
*)))
3346 (type-negation (cons-type-car-type type
))
3350 (type-negation (cons-type-cdr-type type
)))))
3351 ((not (eq (cons-type-car-type type
) *universal-type
*))
3353 (type-negation (cons-type-car-type type
))
3355 ((not (eq (cons-type-cdr-type type
) *universal-type
*))
3358 (type-negation (cons-type-cdr-type type
))))
3359 (t (bug "Weird CONS type ~S" type
))))))
3361 (!define-type-method
(cons :unparse
) (type)
3362 (let ((car-eltype (type-specifier (cons-type-car-type type
)))
3363 (cdr-eltype (type-specifier (cons-type-cdr-type type
))))
3364 (if (and (member car-eltype
'(t *))
3365 (member cdr-eltype
'(t *)))
3367 `(cons ,car-eltype
,cdr-eltype
))))
3369 (!define-type-method
(cons :simple-
=) (type1 type2
)
3370 (declare (type cons-type type1 type2
))
3371 (multiple-value-bind (car-match car-win
)
3372 (type= (cons-type-car-type type1
) (cons-type-car-type type2
))
3373 (multiple-value-bind (cdr-match cdr-win
)
3374 (type= (cons-type-cdr-type type1
) (cons-type-cdr-type type2
))
3375 (cond ((and car-match cdr-match
)
3376 (aver (and car-win cdr-win
))
3380 ;; FIXME: Ideally we would like to detect and handle
3381 ;; (CONS UNKNOWN INTEGER) (CONS UNKNOWN SYMBOL) => NIL, T
3382 ;; but just returning a secondary true on (and car-win cdr-win)
3383 ;; unfortunately breaks other things. --NS 2006-08-16
3384 (and (or (and (not car-match
) car-win
)
3385 (and (not cdr-match
) cdr-win
))
3386 (not (and (cons-type-might-be-empty-type type1
)
3387 (cons-type-might-be-empty-type type2
))))))))))
3389 (!define-type-method
(cons :simple-subtypep
) (type1 type2
)
3390 (declare (type cons-type type1 type2
))
3391 (multiple-value-bind (val-car win-car
)
3392 (csubtypep (cons-type-car-type type1
) (cons-type-car-type type2
))
3393 (multiple-value-bind (val-cdr win-cdr
)
3394 (csubtypep (cons-type-cdr-type type1
) (cons-type-cdr-type type2
))
3395 (if (and val-car val-cdr
)
3396 (values t
(and win-car win-cdr
))
3397 (values nil
(or (and (not val-car
) win-car
)
3398 (and (not val-cdr
) win-cdr
)))))))
3400 ;;; Give up if a precise type is not possible, to avoid returning
3401 ;;; overly general types.
3402 (!define-type-method
(cons :simple-union2
) (type1 type2
)
3403 (declare (type cons-type type1 type2
))
3404 (let ((car-type1 (cons-type-car-type type1
))
3405 (car-type2 (cons-type-car-type type2
))
3406 (cdr-type1 (cons-type-cdr-type type1
))
3407 (cdr-type2 (cons-type-cdr-type type2
))
3410 ;; UGH. -- CSR, 2003-02-24
3411 (macrolet ((frob-car (car1 car2 cdr1 cdr2
3412 &optional
(not1 nil not1p
))
3414 (make-cons-type ,car1
(type-union ,cdr1
,cdr2
))
3416 (type-intersection ,car2
3419 `(type-negation ,car1
)))
3421 (cond ((type= car-type1 car-type2
)
3422 (make-cons-type car-type1
3423 (type-union cdr-type1 cdr-type2
)))
3424 ((type= cdr-type1 cdr-type2
)
3425 (make-cons-type (type-union car-type1 car-type2
)
3427 ((csubtypep car-type1 car-type2
)
3428 (frob-car car-type1 car-type2 cdr-type1 cdr-type2
))
3429 ((csubtypep car-type2 car-type1
)
3430 (frob-car car-type2 car-type1 cdr-type2 cdr-type1
))
3431 ;; more general case of the above, but harder to compute
3433 (setf car-not1
(type-negation car-type1
))
3434 (multiple-value-bind (yes win
)
3435 (csubtypep car-type2 car-not1
)
3436 (and (not yes
) win
)))
3437 (frob-car car-type1 car-type2 cdr-type1 cdr-type2 car-not1
))
3439 (setf car-not2
(type-negation car-type2
))
3440 (multiple-value-bind (yes win
)
3441 (csubtypep car-type1 car-not2
)
3442 (and (not yes
) win
)))
3443 (frob-car car-type2 car-type1 cdr-type2 cdr-type1 car-not2
))
3444 ;; Don't put these in -- consider the effect of taking the
3445 ;; union of (CONS (INTEGER 0 2) (INTEGER 5 7)) and
3446 ;; (CONS (INTEGER 0 3) (INTEGER 5 6)).
3448 ((csubtypep cdr-type1 cdr-type2
)
3449 (frob-cdr car-type1 car-type2 cdr-type1 cdr-type2
))
3451 ((csubtypep cdr-type2 cdr-type1
)
3452 (frob-cdr car-type2 car-type1 cdr-type2 cdr-type1
))))))
3454 (!define-type-method
(cons :simple-intersection2
) (type1 type2
)
3455 (declare (type cons-type type1 type2
))
3456 (let ((car-int2 (type-intersection2 (cons-type-car-type type1
)
3457 (cons-type-car-type type2
)))
3458 (cdr-int2 (type-intersection2 (cons-type-cdr-type type1
)
3459 (cons-type-cdr-type type2
))))
3461 ((and car-int2 cdr-int2
) (make-cons-type car-int2 cdr-int2
))
3462 (car-int2 (make-cons-type car-int2
3464 (cons-type-cdr-type type1
)
3465 (cons-type-cdr-type type2
))))
3466 (cdr-int2 (make-cons-type
3467 (type-intersection (cons-type-car-type type1
)
3468 (cons-type-car-type type2
))
3471 (!define-superclasses cons
((cons)) !cold-init-forms
)
3473 ;;;; CHARACTER-SET types
3475 ;; all character-set types are enumerable, but it's not possible
3476 ;; for one to be TYPE= to a MEMBER type because (MEMBER #\x)
3477 ;; is not internally represented as a MEMBER type.
3478 ;; So in case it wasn't clear already ENUMERABLE-P does not mean
3479 ;; "possibly a MEMBER type in the Lisp-theoretic sense",
3480 ;; but means "could be implemented in SBCL as a MEMBER type".
3481 (!define-type-class character-set
:enumerable nil
3482 :might-contain-other-types nil
)
3484 (!def-type-translator character-set
3485 (&optional
(pairs '((0 .
#.
(1- sb
!xc
:char-code-limit
)))))
3486 (make-character-set-type :pairs pairs
))
3488 (!define-type-method
(character-set :negate
) (type)
3489 (let ((pairs (character-set-type-pairs type
)))
3490 (if (and (= (length pairs
) 1)
3492 (= (cdar pairs
) (1- sb
!xc
:char-code-limit
)))
3493 (make-negation-type :type type
)
3494 (let ((not-character
3496 :type
(make-character-set-type
3497 :pairs
'((0 .
#.
(1- sb
!xc
:char-code-limit
)))))))
3500 (make-character-set-type
3501 :pairs
(let (not-pairs)
3502 (when (> (caar pairs
) 0)
3503 (push (cons 0 (1- (caar pairs
))) not-pairs
))
3504 (do* ((tail pairs
(cdr tail
))
3505 (high1 (cdar tail
) (cdar tail
))
3506 (low2 (caadr tail
) (caadr tail
)))
3508 (when (< (cdar tail
) (1- sb
!xc
:char-code-limit
))
3509 (push (cons (1+ (cdar tail
))
3510 (1- sb
!xc
:char-code-limit
))
3512 (nreverse not-pairs
))
3513 (push (cons (1+ high1
) (1- low2
)) not-pairs
)))))))))
3515 (!define-type-method
(character-set :unparse
) (type)
3517 ((type= type
(specifier-type 'character
)) 'character
)
3518 ((type= type
(specifier-type 'base-char
)) 'base-char
)
3519 ((type= type
(specifier-type 'extended-char
)) 'extended-char
)
3520 ((type= type
(specifier-type 'standard-char
)) 'standard-char
)
3522 ;; Unparse into either MEMBER or CHARACTER-SET. We use MEMBER if there
3523 ;; are at most as many characters as there are character code ranges.
3524 ;; (basically saying to use MEMBER if each range is one character)
3525 (let* ((pairs (character-set-type-pairs type
))
3526 (count (length pairs
))
3527 (chars (loop named outer
3528 for
(low . high
) in pairs
3529 nconc
(loop for code from low upto high
3530 collect
(sb!xc
:code-char code
)
3531 when
(minusp (decf count
))
3532 do
(return-from outer t
)))))
3534 `(character-set ,pairs
)
3535 `(member ,@chars
))))))
3537 (!define-type-method
(character-set :singleton-p
) (type)
3538 (let* ((pairs (character-set-type-pairs type
))
3539 (pair (first pairs
)))
3540 (if (and (typep pairs
'(cons t null
))
3541 (eql (car pair
) (cdr pair
)))
3542 (values t
(code-char (car pair
)))
3545 (!define-type-method
(character-set :simple-
=) (type1 type2
)
3546 (let ((pairs1 (character-set-type-pairs type1
))
3547 (pairs2 (character-set-type-pairs type2
)))
3548 (values (equal pairs1 pairs2
) t
)))
3550 (!define-type-method
(character-set :simple-subtypep
) (type1 type2
)
3552 (dolist (pair (character-set-type-pairs type1
) t
)
3553 (unless (position pair
(character-set-type-pairs type2
)
3554 :test
(lambda (x y
) (and (>= (car x
) (car y
))
3555 (<= (cdr x
) (cdr y
)))))
3559 (!define-type-method
(character-set :simple-union2
) (type1 type2
)
3560 ;; KLUDGE: the canonizing in the MAKE-CHARACTER-SET-TYPE function
3561 ;; actually does the union for us. It might be a little fragile to
3563 (make-character-set-type
3565 (copy-alist (character-set-type-pairs type1
))
3566 (copy-alist (character-set-type-pairs type2
))
3569 (!define-type-method
(character-set :simple-intersection2
) (type1 type2
)
3570 ;; KLUDGE: brute force.
3573 (dolist (pair1 (character-set-type-pairs type1
)
3574 (make-character-set-type
3575 :pairs
(sort pairs
#'< :key
#'car
)))
3576 (dolist (pair2 (character-set-type-pairs type2
))
3578 ((<= (car pair1
) (car pair2
) (cdr pair1
))
3579 (push (cons (car pair2
) (min (cdr pair1
) (cdr pair2
))) pairs
))
3580 ((<= (car pair2
) (car pair1
) (cdr pair2
))
3581 (push (cons (car pair1
) (min (cdr pair1
) (cdr pair2
))) pairs
))))))
3583 (make-character-set-type
3584 :pairs
(intersect-type-pairs
3585 (character-set-type-pairs type1
)
3586 (character-set-type-pairs type2
))))
3589 ;;; Intersect two ordered lists of pairs
3590 ;;; Each list is of the form ((start1 . end1) ... (startn . endn)),
3591 ;;; where start1 <= end1 < start2 <= end2 < ... < startn <= endn.
3592 ;;; Each pair represents the integer interval start..end.
3594 (defun intersect-type-pairs (alist1 alist2
)
3595 (if (and alist1 alist2
)
3597 (pair1 (pop alist1
))
3598 (pair2 (pop alist2
)))
3600 (when (> (car pair1
) (car pair2
))
3601 (rotatef pair1 pair2
)
3602 (rotatef alist1 alist2
))
3603 (let ((pair1-cdr (cdr pair1
)))
3605 ((> (car pair2
) pair1-cdr
)
3606 ;; No over lap -- discard pair1
3607 (unless alist1
(return))
3608 (setq pair1
(pop alist1
)))
3609 ((<= (cdr pair2
) pair1-cdr
)
3610 (push (cons (car pair2
) (cdr pair2
)) res
)
3612 ((= (cdr pair2
) pair1-cdr
)
3613 (unless alist1
(return))
3614 (unless alist2
(return))
3615 (setq pair1
(pop alist1
)
3616 pair2
(pop alist2
)))
3617 (t ;; (< (cdr pair2) pair1-cdr)
3618 (unless alist2
(return))
3619 (setq pair1
(cons (1+ (cdr pair2
)) pair1-cdr
))
3620 (setq pair2
(pop alist2
)))))
3621 (t ;; (> (cdr pair2) (cdr pair1))
3622 (push (cons (car pair2
) pair1-cdr
) res
)
3623 (unless alist1
(return))
3624 (setq pair2
(cons (1+ pair1-cdr
) (cdr pair2
)))
3625 (setq pair1
(pop alist1
))))))
3630 ;;; Return the type that describes all objects that are in X but not
3631 ;;; in Y. If we can't determine this type, then return NIL.
3633 ;;; For now, we only are clever dealing with union and member types.
3634 ;;; If either type is not a union type, then we pretend that it is a
3635 ;;; union of just one type. What we do is remove from X all the types
3636 ;;; that are a subtype any type in Y. If any type in X intersects with
3637 ;;; a type in Y but is not a subtype, then we give up.
3639 ;;; We must also special-case any member type that appears in the
3640 ;;; union. We remove from X's members all objects that are TYPEP to Y.
3641 ;;; If Y has any members, we must be careful that none of those
3642 ;;; members are CTYPEP to any of Y's non-member types. We give up in
3643 ;;; this case, since to compute that difference we would have to break
3644 ;;; the type from X into some collection of types that represents the
3645 ;;; type without that particular element. This seems too hairy to be
3646 ;;; worthwhile, given its low utility.
3647 (defun type-difference (x y
)
3648 (if (and (numeric-type-p x
) (numeric-type-p y
))
3649 ;; Numeric types are easy. Are there any others we should handle like this?
3650 (type-intersection x
(type-negation y
))
3651 (let ((x-types (if (union-type-p x
) (union-type-types x
) (list x
)))
3652 (y-types (if (union-type-p y
) (union-type-types y
) (list y
))))
3654 (dolist (x-type x-types
)
3655 (if (member-type-p x-type
)
3656 (let ((xset (alloc-xset))
3658 (mapc-member-type-members
3660 (multiple-value-bind (ok sure
) (ctypep elt y
)
3662 (return-from type-difference nil
))
3665 (pushnew elt fp-zeroes
)
3666 (add-to-xset elt xset
)))))
3668 (unless (and (xset-empty-p xset
) (not fp-zeroes
))
3669 (res (make-member-type :xset xset
:fp-zeroes fp-zeroes
))))
3670 (dolist (y-type y-types
(res x-type
))
3671 (multiple-value-bind (val win
) (csubtypep x-type y-type
)
3672 (unless win
(return-from type-difference nil
))
3674 (when (types-equal-or-intersect x-type y-type
)
3675 (return-from type-difference nil
))))))
3676 (let ((y-mem (find-if #'member-type-p y-types
)))
3678 (dolist (x-type x-types
)
3679 (unless (member-type-p x-type
)
3680 (mapc-member-type-members
3682 (multiple-value-bind (ok sure
) (ctypep member x-type
)
3683 (when (or (not sure
) ok
)
3684 (return-from type-difference nil
))))
3686 (apply #'type-union
(res))))))
3688 (!def-type-translator array
(&optional
(element-type '*)
3690 (let ((eltype (if (eq element-type
'*)
3692 (specifier-type element-type
))))
3693 (make-array-type (canonical-array-dimensions dimensions
)
3695 :element-type eltype
3696 :specialized-element-type
(%upgraded-array-element-type
3699 (!def-type-translator simple-array
(&optional
(element-type '*)
3701 (let ((eltype (if (eq element-type
'*)
3703 (specifier-type element-type
))))
3704 (make-array-type (canonical-array-dimensions dimensions
)
3706 :element-type eltype
3707 :specialized-element-type
(%upgraded-array-element-type
3710 ;;;; SIMD-PACK types
3713 (!define-type-class simd-pack
:enumerable nil
3714 :might-contain-other-types nil
)
3716 (!def-type-translator simd-pack
(&optional
(element-type-spec '*))
3717 (if (eql element-type-spec
'*)
3718 (%make-simd-pack-type
*simd-pack-element-types
*)
3719 (make-simd-pack-type (single-value-specifier-type element-type-spec
))))
3721 (!define-type-method
(simd-pack :negate
) (type)
3722 (let ((remaining (set-difference *simd-pack-element-types
*
3723 (simd-pack-type-element-type type
)))
3724 (not-simd-pack (make-negation-type :type
(specifier-type 'simd-pack
))))
3726 (type-union not-simd-pack
(%make-simd-pack-type remaining
))
3729 (!define-type-method
(simd-pack :unparse
) (type)
3730 (let ((eltypes (simd-pack-type-element-type type
)))
3731 (cond ((equal eltypes
*simd-pack-element-types
*)
3733 ((= 1 (length eltypes
))
3734 `(simd-pack ,(first eltypes
)))
3736 `(or ,@(mapcar (lambda (eltype)
3737 `(simd-pack ,eltype
))
3740 (!define-type-method
(simd-pack :simple-
=) (type1 type2
)
3741 (declare (type simd-pack-type type1 type2
))
3742 (null (set-exclusive-or (simd-pack-type-element-type type1
)
3743 (simd-pack-type-element-type type2
))))
3745 (!define-type-method
(simd-pack :simple-subtypep
) (type1 type2
)
3746 (declare (type simd-pack-type type1 type2
))
3747 (subsetp (simd-pack-type-element-type type1
)
3748 (simd-pack-type-element-type type2
)))
3750 (!define-type-method
(simd-pack :simple-union2
) (type1 type2
)
3751 (declare (type simd-pack-type type1 type2
))
3752 (%make-simd-pack-type
(union (simd-pack-type-element-type type1
)
3753 (simd-pack-type-element-type type2
))))
3755 (!define-type-method
(simd-pack :simple-intersection2
) (type1 type2
)
3756 (declare (type simd-pack-type type1 type2
))
3757 (let ((intersection (intersection (simd-pack-type-element-type type1
)
3758 (simd-pack-type-element-type type2
))))
3760 (%make-simd-pack-type intersection
)
3763 (!define-superclasses simd-pack
((simd-pack)) !cold-init-forms
))
3765 ;;;; utilities shared between cross-compiler and target system
3767 ;;; Does the type derived from compilation of an actual function
3768 ;;; definition satisfy declarations of a function's type?
3769 (defun defined-ftype-matches-declared-ftype-p (defined-ftype declared-ftype
)
3770 (declare (type ctype defined-ftype declared-ftype
))
3771 (flet ((is-built-in-class-function-p (ctype)
3772 (and (built-in-classoid-p ctype
)
3773 (eq (built-in-classoid-name ctype
) 'function
))))
3774 (cond (;; DECLARED-FTYPE could certainly be #<BUILT-IN-CLASS FUNCTION>;
3775 ;; that's what happens when we (DECLAIM (FTYPE FUNCTION FOO)).
3776 (is-built-in-class-function-p declared-ftype
)
3777 ;; In that case, any definition satisfies the declaration.
3779 (;; It's not clear whether or how DEFINED-FTYPE might be
3780 ;; #<BUILT-IN-CLASS FUNCTION>, but it's not obviously
3781 ;; invalid, so let's handle that case too, just in case.
3782 (is-built-in-class-function-p defined-ftype
)
3783 ;; No matter what DECLARED-FTYPE might be, we can't prove
3784 ;; that an object of type FUNCTION doesn't satisfy it, so
3785 ;; we return success no matter what.
3787 (;; Otherwise both of them must be FUN-TYPE objects.
3789 ;; FIXME: For now we only check compatibility of the return
3790 ;; type, not argument types, and we don't even check the
3791 ;; return type very precisely (as per bug 94a). It would be
3792 ;; good to do a better job. Perhaps to check the
3793 ;; compatibility of the arguments, we should (1) redo
3794 ;; VALUES-TYPES-EQUAL-OR-INTERSECT as
3795 ;; ARGS-TYPES-EQUAL-OR-INTERSECT, and then (2) apply it to
3796 ;; the ARGS-TYPE slices of the FUN-TYPEs. (ARGS-TYPE
3797 ;; is a base class both of VALUES-TYPE and of FUN-TYPE.)
3798 (values-types-equal-or-intersect
3799 (fun-type-returns defined-ftype
)
3800 (fun-type-returns declared-ftype
))))))
3802 ;;; This messy case of CTYPE for NUMBER is shared between the
3803 ;;; cross-compiler and the target system.
3804 (defun ctype-of-number (x)
3805 (let ((num (if (complexp x
) (realpart x
) x
)))
3806 (multiple-value-bind (complexp low high
)
3808 (let ((imag (imagpart x
)))
3809 (values :complex
(min num imag
) (max num imag
)))
3810 (values :real num num
))
3811 (make-numeric-type :class
(etypecase num
3812 (integer (if (complexp x
)
3813 (if (integerp (imagpart x
))
3817 (rational 'rational
)
3819 :format
(and (floatp num
) (float-format-name num
))
3824 ;;; The following function is a generic driver for approximating
3825 ;;; set-valued functions over types. Putting this here because it'll
3826 ;;; probably be useful for a lot of type analyses.
3828 ;;; Let f be a function from values of type X to Y, e.g., ARRAY-RANK.
3830 ;;; We compute an over or under-approximation of the set
3832 ;;; F(TYPE) = { f(x) : x in TYPE /\ x in X } \subseteq Y
3834 ;;; via set-valued approximations of f, OVER and UNDER.
3836 ;;; These functions must have the property that
3837 ;;; Forall TYPE, OVER(TYPE) \superseteq F(TYPE) and
3838 ;;; Forall TYPE, UNDER(TYPE) \subseteq F(TYPE)
3840 ;;; The driver is also parameterised over the finite set
3843 ;;; Union, intersection and difference are binary functions to compute
3844 ;;; set union, intersection and difference. Top and bottom are the
3845 ;;; concrete representations for the universe and empty sets; we never
3846 ;;; call the set functions on top or bottom, so it's safe to use
3847 ;;; special values there.
3851 ;;; TYPE: the ctype for which we wish to approximate F(TYPE)
3852 ;;; OVERAPPROXIMATE: true if we wish to overapproximate, nil otherwise.
3853 ;;; You usually want T.
3854 ;;; UNION/INTERSECTION/DIFFERENCE: implementations of finite set operations.
3855 ;;; Conform to cl::(union/intersection/set-difference). Passing NIL will
3856 ;;; disable some cleverness and result in quicker computation of coarser
3857 ;;; approximations. However, passing difference without union and intersection
3858 ;;; will probably not end well.
3859 ;;; TOP/BOTTOM: concrete representation of the universe and empty set. Finite
3860 ;;; set operations are never called on TOP/BOTTOM, so it's safe to use special
3862 ;;; OVER/UNDER: the set-valued approximations of F.
3864 ;;; Implementation details.
3866 ;;; It's a straightforward walk down the type.
3867 ;;; Union types -> take the union of children, intersection ->
3868 ;;; intersect. There is some complication for negation types: we must
3869 ;;; not only negate the result, but also flip from overapproximating
3870 ;;; to underapproximating in the children (or vice versa).
3872 ;;; We represent sets as a pair of (negate-p finite-set) in order to
3873 ;;; support negation types.
3875 (declaim (inline generic-abstract-type-function
))
3876 (defun generic-abstract-type-function
3877 (type overapproximate
3878 union intersection difference
3881 (labels ((union* (x y
)
3882 ;; wrappers to avoid calling union/intersection on
3884 (cond ((or (eql x top
)
3890 (funcall union x y
))))
3891 (intersection* (x y
)
3892 (cond ((or (eql x bottom
)
3898 (funcall intersection x y
))))
3899 (unite (not-x-p x not-y-p y
)
3900 ;; if we only have one negated set, it's x.
3902 (rotatef not-x-p not-y-p
)
3904 (cond ((and not-x-p not-y-p
)
3905 ;; -x \/ -y = -(x /\ y)
3906 (normalize t
(intersection* x y
)))
3908 ;; -x \/ y = -(x \ y)
3918 (funcall difference x y
)))))
3920 (values nil
(union* x y
)))))
3921 (intersect (not-x-p x not-y-p y
)
3923 (rotatef not-x-p not-y-p
)
3925 (cond ((and not-x-p not-y-p
)
3926 ;; -x /\ -y = -(x \/ y)
3927 (normalize t
(union* x y
)))
3930 (cond ((or (eql x top
) (eql y bottom
))
3931 (values nil bottom
))
3937 (values nil
(funcall difference y x
)))))
3939 (values nil
(intersection* x y
)))))
3940 (normalize (not-x-p x
)
3941 ;; catch some easy cases of redundant negation.
3942 (cond ((not not-x-p
)
3950 (default (overapproximate)
3952 (if overapproximate top bottom
))
3953 (walk-union (types overapproximate
)
3954 ;; Only do this if union is provided.
3956 (return-from walk-union
(default overapproximate
)))
3957 ;; Reduce/union from bottom.
3958 (let ((not-acc-p nil
)
3960 (dolist (type types
(values not-acc-p acc
))
3961 (multiple-value-bind (not x
)
3962 (walk type overapproximate
)
3963 (setf (values not-acc-p acc
)
3964 (unite not-acc-p acc not x
)))
3965 ;; Early exit on top set.
3966 (when (and (eql acc top
)
3968 (return (values nil top
))))))
3969 (walk-intersection (types overapproximate
)
3970 ;; Skip if we don't know how to intersect sets
3971 (unless intersection
3972 (return-from walk-intersection
(default overapproximate
)))
3973 ;; Reduce/intersection from top
3974 (let ((not-acc-p nil
)
3976 (dolist (type types
(values not-acc-p acc
))
3977 (multiple-value-bind (not x
)
3978 (walk type overapproximate
)
3979 (setf (values not-acc-p acc
)
3980 (intersect not-acc-p acc not x
)))
3981 (when (and (eql acc bottom
)
3983 (return (values nil bottom
))))))
3984 (walk-negate (type overapproximate
)
3985 ;; Don't introduce negated types if we don't know how to
3988 (return-from walk-negate
(default overapproximate
)))
3989 (multiple-value-bind (not x
)
3990 (walk type
(not overapproximate
))
3991 (normalize (not not
) x
)))
3992 (walk (type overapproximate
)
3995 (walk-union (union-type-types type
) overapproximate
))
3996 ((cons (member or union
))
3997 (walk-union (rest type
) overapproximate
))
3999 (walk-intersection (intersection-type-types type
) overapproximate
))
4000 ((cons (member and intersection
))
4001 (walk-intersection (rest type
) overapproximate
))
4003 (walk-negate (negation-type-type type
) overapproximate
))
4005 (walk-negate (second type
) overapproximate
))
4013 (funcall under type
)
4014 (default nil
))))))))
4015 (multiple-value-call #'normalize
(walk type overapproximate
))))
4016 (declaim (notinline generic-abstract-type-function
))
4018 ;;; Standard list representation of sets. Use CL:* for the universe.
4019 (defun list-abstract-type-function (type over
&key under
(overapproximate t
))
4020 (declare (inline generic-abstract-type-function
))
4021 (generic-abstract-type-function
4022 type overapproximate
4023 #'union
#'intersection
#'set-difference
4027 (!defun-from-collected-cold-init-forms
!late-type-cold-init
)
4029 #-sb-xc
(!late-type-cold-init2
)
4031 (/show0
"late-type.lisp end of file")