1 ;;;; This file contains the definition of non-CLASS types (e.g.
2 ;;;; subtypes of interesting BUILT-IN-CLASSes) and the interfaces to
3 ;;;; the type system. Common Lisp type specifiers are parsed into a
4 ;;;; somewhat canonical internal type representation that supports
5 ;;;; type union, intersection, etc. (Except that ALIEN types have
8 ;;;; This software is part of the SBCL system. See the README file for
11 ;;;; This software is derived from the CMU CL system, which was
12 ;;;; written at Carnegie Mellon University and released into the
13 ;;;; public domain. The software is in the public domain and is
14 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
15 ;;;; files for more information.
17 (in-package "SB!KERNEL")
19 (/show0
"late-type.lisp 19")
21 (!begin-collecting-cold-init-forms
)
23 ;;; ### Remaining incorrectnesses:
25 ;;; There are all sorts of nasty problems with open bounds on FLOAT
26 ;;; types (and probably FLOAT types in general.)
28 ;;; This condition is signalled whenever we make a UNKNOWN-TYPE so that
29 ;;; compiler warnings can be emitted as appropriate.
30 (define-condition parse-unknown-type
(condition)
31 ((specifier :reader parse-unknown-type-specifier
:initarg
:specifier
))
33 :specifier
(missing-arg)))
35 ;;; This condition is signalled whenever we encounter a type (DEFTYPE,
36 ;;; structure, condition, class) that has been marked as deprecated.
37 (define-condition parse-deprecated-type
(condition)
38 ((specifier :reader parse-deprecated-type-specifier
:initarg
:specifier
))
40 :specifier
(missing-arg)))
42 ;;; These functions are used as method for types which need a complex
43 ;;; subtypep method to handle some superclasses, but cover a subtree
44 ;;; of the type graph (i.e. there is no simple way for any other type
45 ;;; class to be a subtype.) There are always still complex ways,
46 ;;; namely UNION and MEMBER types, so we must give TYPE1's method a
47 ;;; chance to run, instead of immediately returning NIL, T.
48 (defun delegate-complex-subtypep-arg2 (type1 type2
)
50 (type-class-complex-subtypep-arg1 (type-class-info type1
))))
52 (funcall subtypep-arg1 type1 type2
)
54 (defun delegate-complex-intersection2 (type1 type2
)
55 (let ((method (type-class-complex-intersection2 (type-class-info type1
))))
56 (if (and method
(not (eq method
#'delegate-complex-intersection2
)))
57 (funcall method type2 type1
)
58 (hierarchical-intersection2 type1 type2
))))
60 (defun contains-unknown-type-p (ctype)
63 (compound-type (some #'contains-unknown-type-p
(compound-type-types ctype
)))
64 (negation-type (contains-unknown-type-p (negation-type-type ctype
)))
65 (cons-type (or (contains-unknown-type-p (cons-type-car-type ctype
))
66 (contains-unknown-type-p (cons-type-cdr-type ctype
))))
67 (array-type (contains-unknown-type-p (array-type-element-type ctype
)))
69 (or (some #'contains-unknown-type-p
(args-type-required ctype
))
70 (some #'contains-unknown-type-p
(args-type-optional ctype
))
71 (acond ((args-type-rest ctype
) (contains-unknown-type-p it
)))
72 (some (lambda (x) (contains-unknown-type-p (key-info-type x
)))
73 (args-type-keywords ctype
))
74 (and (fun-type-p ctype
)
75 (contains-unknown-type-p (fun-type-returns ctype
)))))))
77 ;; Similar to (NOT CONTAINS-UNKNOWN-TYPE-P), but report that (SATISFIES F)
78 ;; is not a testable type unless F is currently bound.
79 (defun testable-type-p (ctype)
81 (unknown-type nil
) ; must precede HAIRY because an unknown is HAIRY
83 (let ((spec (hairy-type-specifier ctype
)))
84 ;; Anything other than (SATISFIES ...) is testable
85 ;; because there's no reason to suppose that it isn't.
86 (or (neq (car spec
) 'satisfies
) (fboundp (cadr spec
)))))
87 (compound-type (every #'testable-type-p
(compound-type-types ctype
)))
88 (negation-type (testable-type-p (negation-type-type ctype
)))
89 (cons-type (and (testable-type-p (cons-type-car-type ctype
))
90 (testable-type-p (cons-type-cdr-type ctype
))))
91 ;; This case could be too strict. I think an array type is testable
92 ;; if the upgraded type is testable. Probably nobody cares though.
93 (array-type (testable-type-p (array-type-element-type ctype
)))
96 ;;; This is used by !DEFINE-SUPERCLASSES to define the SUBTYPE-ARG1
97 ;;; method. INFO is a list of conses
98 ;;; (SUPERCLASS-CLASS . {GUARD-TYPE-SPECIFIER | NIL}).
99 (defun has-superclasses-complex-subtypep-arg1 (type1 type2 info
)
100 ;; If TYPE2 might be concealing something related to our class
102 (if (type-might-contain-other-types-p type2
)
103 ;; too confusing, gotta punt
105 ;; ordinary case expected by old CMU CL code, where the taxonomy
106 ;; of TYPE2's representation accurately reflects the taxonomy of
107 ;; the underlying set
109 ;; FIXME: This old CMU CL code probably deserves a comment
110 ;; explaining to us mere mortals how it works...
111 (and (sb!xc
:typep type2
'classoid
)
113 (let ((guard (cdr x
)))
114 (when (or (not guard
)
115 (csubtypep type1
(if (%instancep guard
)
118 (specifier-type guard
)))))
120 (or (eq type2
(car x
))
121 (let ((inherits (layout-inherits
122 (classoid-layout (car x
)))))
123 (dotimes (i (length inherits
) nil
)
124 (when (eq type2
(layout-classoid (svref inherits i
)))
128 ;;; This function takes a list of specs, each of the form
129 ;;; (SUPERCLASS-NAME &OPTIONAL GUARD).
130 ;;; Consider one spec (with no guard): any instance of the named
131 ;;; TYPE-CLASS is also a subtype of the named superclass and of any of
132 ;;; its superclasses. If there are multiple specs, then some will have
133 ;;; guards. We choose the first spec whose guard is a supertype of
134 ;;; TYPE1 and use its superclass. In effect, a sequence of guards
137 ;;; G0,(and G1 (not G0)), (and G2 (not (or G0 G1))).
139 ;;; WHEN controls when the forms are executed.
140 (defmacro !define-superclasses
(type-class-name specs progn-oid
)
141 (let ((defun-name (symbolicate type-class-name
"-COMPLEX-SUBTYPEP-ARG1")))
143 (defun ,defun-name
(type1 type2
)
144 (has-superclasses-complex-subtypep-arg1
147 (list ,@(mapcar (lambda (spec)
148 (destructuring-bind (super &optional guard
) spec
149 `(cons (find-classoid ',super
) ',guard
)))
150 specs
)) #-sb-xc-host t
)))
152 (let ((type-class (type-class-or-lose ',type-class-name
)))
153 (setf (type-class-complex-subtypep-arg1 type-class
) #',defun-name
)
154 (setf (type-class-complex-subtypep-arg2 type-class
)
155 #'delegate-complex-subtypep-arg2
)
156 (setf (type-class-complex-intersection2 type-class
)
157 #'delegate-complex-intersection2
))))))
159 ;;;; FUNCTION and VALUES types
161 ;;;; Pretty much all of the general type operations are illegal on
162 ;;;; VALUES types, since we can't discriminate using them, do
163 ;;;; SUBTYPEP, etc. FUNCTION types are acceptable to the normal type
164 ;;;; operations, but are generally considered to be equivalent to
165 ;;;; FUNCTION. These really aren't true types in any type theoretic
166 ;;;; sense, but we still parse them into CTYPE structures for two
169 ;;;; -- Parsing and unparsing work the same way, and indeed we can't
170 ;;;; tell whether a type is a function or values type without
172 ;;;; -- Many of the places that can be annotated with real types can
173 ;;;; also be annotated with function or values types.
175 (!define-type-method
(values :simple-subtypep
:complex-subtypep-arg1
)
177 (declare (ignore type2
))
178 ;; FIXME: should be TYPE-ERROR, here and in next method
179 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type1
)))
181 (!define-type-method
(values :complex-subtypep-arg2
)
183 (declare (ignore type1
))
184 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type2
)))
186 (!define-type-method
(values :negate
) (type)
187 (error "NOT VALUES too confusing on ~S" (type-specifier type
)))
189 (!define-type-method
(values :unparse
) (type)
191 (let ((unparsed (unparse-args-types type
)))
192 (if (or (values-type-optional type
)
193 (values-type-rest type
)
194 (values-type-allowp type
))
196 (nconc unparsed
'(&optional
))))))
198 ;;; Return true if LIST1 and LIST2 have the same elements in the same
199 ;;; positions according to TYPE=. We return NIL, NIL if there is an
200 ;;; uncertain comparison.
201 (defun type=-list
(list1 list2
)
202 (declare (list list1 list2
))
203 (do ((types1 list1
(cdr types1
))
204 (types2 list2
(cdr types2
)))
205 ((or (null types1
) (null types2
))
206 (if (or types1 types2
)
209 (multiple-value-bind (val win
)
210 (type= (first types1
) (first types2
))
212 (return (values nil nil
)))
214 (return (values nil t
))))))
216 (!define-type-method
(values :simple-
=) (type1 type2
)
217 (type=-args type1 type2
))
219 ;;; a flag that we can bind to cause complex function types to be
220 ;;; unparsed as FUNCTION. This is useful when we want a type that we
221 ;;; can pass to TYPEP.
222 (!defvar
*unparse-fun-type-simplify
* nil
)
223 ;;; A flag to prevent TYPE-OF calls by user applications from returning
224 ;;; (NOT x). TYPE-SPECIFIER usually allows it to preserve information.
225 (!defvar
*unparse-allow-negation
* t
)
227 (!define-type-method
(function :negate
) (type) (make-negation-type type
))
229 (!define-type-method
(function :unparse
) (type)
230 (if *unparse-fun-type-simplify
*
233 (if (fun-type-wild-args type
)
235 (unparse-args-types type
))
237 (fun-type-returns type
)))))
239 ;;; The meaning of this is a little confused. On the one hand, all
240 ;;; function objects are represented the same way regardless of the
241 ;;; arglists and return values, and apps don't get to ask things like
242 ;;; (TYPEP #'FOO (FUNCTION (FIXNUM) *)) in any meaningful way. On the
243 ;;; other hand, Python wants to reason about function types. So...
244 (!define-type-method
(function :simple-subtypep
) (type1 type2
)
245 (flet ((fun-type-simple-p (type)
246 (not (or (fun-type-rest type
)
247 (fun-type-keyp type
))))
248 (every-csubtypep (types1 types2
)
252 do
(multiple-value-bind (res sure-p
)
254 (unless res
(return (values res sure-p
))))
255 finally
(return (values t t
)))))
256 (and/type
(values-subtypep (fun-type-returns type1
)
257 (fun-type-returns type2
))
258 (cond ((fun-type-wild-args type2
) (values t t
))
259 ((fun-type-wild-args type1
)
260 (cond ((fun-type-keyp type2
) (values nil nil
))
261 ((not (fun-type-rest type2
)) (values nil t
))
262 ((not (null (fun-type-required type2
)))
264 (t (and/type
(type= *universal-type
*
265 (fun-type-rest type2
))
270 ((not (and (fun-type-simple-p type1
)
271 (fun-type-simple-p type2
)))
273 (t (multiple-value-bind (min1 max1
) (fun-type-nargs type1
)
274 (multiple-value-bind (min2 max2
) (fun-type-nargs type2
)
275 (cond ((or (> max1 max2
) (< min1 min2
))
277 ((and (= min1 min2
) (= max1 max2
))
278 (and/type
(every-csubtypep
279 (fun-type-required type1
)
280 (fun-type-required type2
))
282 (fun-type-optional type1
)
283 (fun-type-optional type2
))))
286 (fun-type-required type1
)
287 (fun-type-optional type1
))
289 (fun-type-required type2
)
290 (fun-type-optional type2
))))))))))))
292 (!define-superclasses function
((function)) !cold-init-forms
)
294 ;;; The union or intersection of two FUNCTION types is FUNCTION.
295 (!define-type-method
(function :simple-union2
) (type1 type2
)
296 (declare (ignore type1 type2
))
297 (specifier-type 'function
))
298 (!define-type-method
(function :simple-intersection2
) (type1 type2
)
299 (let ((ftype (specifier-type 'function
)))
300 (cond ((eq type1 ftype
) type2
)
301 ((eq type2 ftype
) type1
)
302 (t (let ((rtype (values-type-intersection (fun-type-returns type1
)
303 (fun-type-returns type2
))))
304 (flet ((change-returns (ftype rtype
)
305 (declare (type fun-type ftype
) (type ctype rtype
))
306 (make-fun-type :required
(fun-type-required ftype
)
307 :optional
(fun-type-optional ftype
)
308 :keyp
(fun-type-keyp ftype
)
309 :keywords
(fun-type-keywords ftype
)
310 :allowp
(fun-type-allowp ftype
)
313 ((fun-type-wild-args type1
)
314 (if (fun-type-wild-args type2
)
315 (make-fun-type :wild-args t
317 (change-returns type2 rtype
)))
318 ((fun-type-wild-args type2
)
319 (change-returns type1 rtype
))
320 (t (multiple-value-bind (req opt rest
)
321 (args-type-op type1 type2
#'type-intersection
#'max
)
322 (make-fun-type :required req
326 :allowp
(and (fun-type-allowp type1
)
327 (fun-type-allowp type2
))
328 :returns rtype
))))))))))
330 ;;; The union or intersection of a subclass of FUNCTION with a
331 ;;; FUNCTION type is somewhat complicated.
332 (!define-type-method
(function :complex-intersection2
) (type1 type2
)
334 ((type= type1
(specifier-type 'function
)) type2
)
335 ((csubtypep type1
(specifier-type 'function
)) nil
)
336 (t :call-other-method
)))
337 (!define-type-method
(function :complex-union2
) (type1 type2
)
338 (declare (ignore type2
))
339 ;; TYPE2 is a FUNCTION type. If TYPE1 is a classoid type naming
340 ;; FUNCTION, then it is the union of the two; otherwise, there is no
343 ((type= type1
(specifier-type 'function
)) type1
)
346 (!define-type-method
(function :simple-
=) (type1 type2
)
347 (macrolet ((compare (comparator field
)
348 (let ((reader (symbolicate '#:fun-type- field
)))
349 `(,comparator
(,reader type1
) (,reader type2
)))))
350 (and/type
(compare type
= returns
)
351 (cond ((neq (fun-type-wild-args type1
) (fun-type-wild-args type2
))
353 ((eq (fun-type-wild-args type1
) t
)
355 (t (type=-args type1 type2
))))))
357 (!define-type-class constant
:inherits values
)
359 (!define-type-method
(constant :negate
) (type)
360 (error "NOT CONSTANT too confusing on ~S" (type-specifier type
)))
362 (!define-type-method
(constant :unparse
) (type)
363 `(constant-arg ,(type-specifier (constant-type-type type
))))
365 (!define-type-method
(constant :simple-
=) (type1 type2
)
366 (type= (constant-type-type type1
) (constant-type-type type2
)))
368 (!def-type-translator constant-arg
((:context context
) type
)
369 (make-constant-type :type
(single-value-specifier-type-r context type
)))
371 ;;; Return the lambda-list-like type specification corresponding
373 (declaim (ftype (function (args-type) list
) unparse-args-types
))
374 (defun unparse-args-types (type)
377 (dolist (arg (args-type-required type
))
378 (result (type-specifier arg
)))
380 (when (args-type-optional type
)
382 (dolist (arg (args-type-optional type
))
383 (result (type-specifier arg
))))
385 (when (args-type-rest type
)
387 (result (type-specifier (args-type-rest type
))))
389 (when (args-type-keyp type
)
391 (dolist (key (args-type-keywords type
))
392 (result (list (key-info-name key
)
393 (type-specifier (key-info-type key
))))))
395 (when (args-type-allowp type
)
396 (result '&allow-other-keys
))
400 (!def-type-translator function
((:context context
)
401 &optional
(args '*) (result '*))
402 (let ((result (coerce-to-values (values-specifier-type-r context result
))))
404 (if (eq result
*wild-type
*)
405 (specifier-type 'function
)
406 (make-fun-type :wild-args t
:returns result
))
407 (multiple-value-bind (llks required optional rest keywords
)
408 (parse-args-types context args
:function-type
)
409 (if (and (null required
)
411 (eq rest
*universal-type
*)
412 (not (ll-kwds-keyp llks
)))
413 (if (eq result
*wild-type
*)
414 (specifier-type 'function
)
415 (make-fun-type :wild-args t
:returns result
))
416 (make-fun-type :required required
419 :keyp
(ll-kwds-keyp llks
)
421 :allowp
(ll-kwds-allowp llks
)
422 :returns result
))))))
424 (!def-type-translator values
:list
((:context context
) &rest values
)
427 (multiple-value-bind (llks required optional rest
)
428 (parse-args-types context values
:values-type
)
430 (make-values-type :required required
:optional optional
:rest rest
)
431 (make-short-values-type required
)))))
433 ;;;; VALUES types interfaces
435 ;;;; We provide a few special operations that can be meaningfully used
436 ;;;; on VALUES types (as well as on any other type).
438 ;;; Return the minimum number of values possibly matching VALUES type
440 (defun values-type-min-value-count (type)
443 (ecase (named-type-name type
)
447 (length (values-type-required type
)))))
449 ;;; Return the maximum number of values possibly matching VALUES type
451 (defun values-type-max-value-count (type)
454 (ecase (named-type-name type
)
455 ((t *) call-arguments-limit
)
458 (if (values-type-rest type
)
460 (+ (length (values-type-optional type
))
461 (length (values-type-required type
)))))))
463 (defun values-type-may-be-single-value-p (type)
464 (<= (values-type-min-value-count type
)
466 (values-type-max-value-count type
)))
468 ;;; VALUES type with a single value.
469 (defun type-single-value-p (type)
470 (and (%values-type-p type
)
471 (not (values-type-rest type
))
472 (null (values-type-optional type
))
473 (singleton-p (values-type-required type
))))
475 ;;; Return the type of the first value indicated by TYPE. This is used
476 ;;; by people who don't want to have to deal with VALUES types.
477 #!-sb-fluid
(declaim (freeze-type values-type
))
478 ; (inline single-value-type))
479 (defun single-value-type (type)
480 (declare (type ctype type
))
481 (cond ((eq type
*wild-type
*)
483 ((eq type
*empty-type
*)
485 ((not (values-type-p type
))
487 ((car (args-type-required type
)))
488 (t (type-union (specifier-type 'null
)
489 (or (car (args-type-optional type
))
490 (args-type-rest type
)
491 (specifier-type 'null
))))))
493 ;;; Return the minimum number of arguments that a function can be
494 ;;; called with, and the maximum number or NIL. If not a function
495 ;;; type, return NIL, NIL.
496 (defun fun-type-nargs (type)
497 (declare (type ctype type
))
498 (if (and (fun-type-p type
) (not (fun-type-wild-args type
)))
499 (let ((fixed (length (args-type-required type
))))
500 (if (or (args-type-rest type
)
501 (args-type-keyp type
)
502 (args-type-allowp type
))
504 (values fixed
(+ fixed
(length (args-type-optional type
))))))
507 ;;; Determine whether TYPE corresponds to a definite number of values.
508 ;;; The first value is a list of the types for each value, and the
509 ;;; second value is the number of values. If the number of values is
510 ;;; not fixed, then return NIL and :UNKNOWN.
511 (defun values-types (type)
512 (declare (type ctype type
))
513 (cond ((or (eq type
*wild-type
*) (eq type
*empty-type
*))
514 (values nil
:unknown
))
515 ((or (args-type-optional type
)
516 (args-type-rest type
))
517 (values nil
:unknown
))
519 (let ((req (args-type-required type
)))
520 (values req
(length req
))))))
522 ;;; Return two values:
523 ;;; 1. A list of all the positional (fixed and optional) types.
524 ;;; 2. The &REST type (if any). If no &REST, then the DEFAULT-TYPE.
525 (defun values-type-types (type &optional
(default-type *empty-type
*))
526 (declare (type ctype type
))
527 (if (eq type
*wild-type
*)
528 (values nil
*universal-type
*)
529 (values (append (args-type-required type
)
530 (args-type-optional type
))
531 (cond ((args-type-rest type
))
534 ;;; types of values in (the <type> (values o_1 ... o_n))
535 (defun values-type-out (type count
)
536 (declare (type ctype type
) (type unsigned-byte count
))
537 (if (eq type
*wild-type
*)
538 (make-list count
:initial-element
*universal-type
*)
540 (flet ((process-types (types)
541 (loop for type in types
545 (process-types (values-type-required type
))
546 (process-types (values-type-optional type
))
548 (loop with rest
= (the ctype
(values-type-rest type
))
553 ;;; types of variable in (m-v-bind (v_1 ... v_n) (the <type> ...
554 (defun values-type-in (type count
)
555 (declare (type ctype type
) (type unsigned-byte count
))
556 (if (eq type
*wild-type
*)
557 (make-list count
:initial-element
*universal-type
*)
559 (let ((null-type (specifier-type 'null
)))
560 (loop for type in
(values-type-required type
)
564 (loop for type in
(values-type-optional type
)
567 do
(res (type-union type null-type
)))
569 (loop with rest
= (acond ((values-type-rest type
)
570 (type-union it null-type
))
576 ;;; Return a list of OPERATION applied to the types in TYPES1 and
577 ;;; TYPES2, padding with REST2 as needed. TYPES1 must not be shorter
578 ;;; than TYPES2. The second value is T if OPERATION always returned a
579 ;;; true second value.
580 (defun fixed-values-op (types1 types2 rest2 operation
)
581 (declare (list types1 types2
) (type ctype rest2
) (type function operation
))
583 (values (mapcar (lambda (t1 t2
)
584 (multiple-value-bind (res win
)
585 (funcall operation t1 t2
)
591 (make-list (- (length types1
) (length types2
))
592 :initial-element rest2
)))
595 ;;; If TYPE isn't a values type, then make it into one.
596 (defun-cached (%coerce-to-values
:hash-bits
8 :hash-function
#'type-hash-value
)
598 (cond ((multiple-value-bind (res sure
)
599 (csubtypep (specifier-type 'null
) type
)
600 (and (not res
) sure
))
601 ;; FIXME: What should we do with (NOT SURE)?
602 (make-values-type :required
(list type
) :rest
*universal-type
*))
604 (make-values-type :optional
(list type
) :rest
*universal-type
*))))
606 (defun coerce-to-values (type)
607 (declare (type ctype type
))
608 (cond ((or (eq type
*universal-type
*)
609 (eq type
*wild-type
*))
611 ((values-type-p type
)
613 (t (%coerce-to-values type
))))
615 ;;; Return type, corresponding to ANSI short form of VALUES type
617 (defun make-short-values-type (types)
618 (declare (list types
))
619 (let ((last-required (position-if
621 (not/type
(csubtypep (specifier-type 'null
) type
)))
625 (make-values-type :required
(subseq types
0 (1+ last-required
))
626 :optional
(subseq types
(1+ last-required
))
627 :rest
*universal-type
*)
628 (make-values-type :optional types
:rest
*universal-type
*))))
630 (defun make-single-value-type (type)
631 (make-values-type :required
(list type
)))
633 ;;; Do the specified OPERATION on TYPE1 and TYPE2, which may be any
634 ;;; type, including VALUES types. With VALUES types such as:
637 ;;; we compute the more useful result
638 ;;; (VALUES (<operation> a0 b0) (<operation> a1 b1))
639 ;;; rather than the precise result
640 ;;; (<operation> (values a0 a1) (values b0 b1))
641 ;;; This has the virtue of always keeping the VALUES type specifier
642 ;;; outermost, and retains all of the information that is really
643 ;;; useful for static type analysis. We want to know what is always
644 ;;; true of each value independently. It is worthless to know that if
645 ;;; the first value is B0 then the second will be B1.
647 ;;; If the VALUES count signatures differ, then we produce a result with
648 ;;; the required VALUE count chosen by NREQ when applied to the number
649 ;;; of required values in TYPE1 and TYPE2. Any &KEY values become
650 ;;; &REST T (anyone who uses keyword values deserves to lose.)
652 ;;; The second value is true if the result is definitely empty or if
653 ;;; OPERATION returned true as its second value each time we called
654 ;;; it. Since we approximate the intersection of VALUES types, the
655 ;;; second value being true doesn't mean the result is exact.
656 (defun args-type-op (type1 type2 operation nreq
)
657 (declare (type ctype type1 type2
)
658 (type function operation nreq
))
659 (when (eq type1 type2
)
661 (multiple-value-bind (types1 rest1
)
662 (values-type-types type1
)
663 (multiple-value-bind (types2 rest2
)
664 (values-type-types type2
)
665 (multiple-value-bind (rest rest-exact
)
666 (funcall operation rest1 rest2
)
667 (multiple-value-bind (res res-exact
)
668 (if (< (length types1
) (length types2
))
669 (fixed-values-op types2 types1 rest1 operation
)
670 (fixed-values-op types1 types2 rest2 operation
))
671 (let* ((req (funcall nreq
672 (length (args-type-required type1
))
673 (length (args-type-required type2
))))
674 (required (subseq res
0 req
))
675 (opt (subseq res req
)))
676 (values required opt rest
677 (and rest-exact res-exact
))))))))
679 (defun values-type-op (type1 type2 operation nreq
)
680 (multiple-value-bind (required optional rest exactp
)
681 (args-type-op type1 type2 operation nreq
)
682 (values (make-values-type :required required
687 (defun compare-key-args (type1 type2
)
688 (let ((keys1 (args-type-keywords type1
))
689 (keys2 (args-type-keywords type2
)))
690 (and (= (length keys1
) (length keys2
))
691 (eq (args-type-allowp type1
)
692 (args-type-allowp type2
))
693 (loop for key1 in keys1
694 for match
= (find (key-info-name key1
)
695 keys2
:key
#'key-info-name
)
697 (type= (key-info-type key1
)
698 (key-info-type match
)))))))
700 (defun type=-args
(type1 type2
)
701 (macrolet ((compare (comparator field
)
702 (let ((reader (symbolicate '#:args-type- field
)))
703 `(,comparator
(,reader type1
) (,reader type2
)))))
705 (cond ((null (args-type-rest type1
))
706 (values (null (args-type-rest type2
)) t
))
707 ((null (args-type-rest type2
))
710 (compare type
= rest
)))
711 (and/type
(and/type
(compare type
=-list required
)
712 (compare type
=-list optional
))
713 (if (or (args-type-keyp type1
) (args-type-keyp type2
))
714 (values (compare-key-args type1 type2
) t
)
717 ;;; Do a union or intersection operation on types that might be values
718 ;;; types. The result is optimized for utility rather than exactness,
719 ;;; but it is guaranteed that it will be no smaller (more restrictive)
720 ;;; than the precise result.
722 ;;; The return convention seems to be analogous to
723 ;;; TYPES-EQUAL-OR-INTERSECT. -- WHN 19990910.
724 (defun-cached (values-type-union :hash-function
#'type-cache-hash
726 ((type1 eq
) (type2 eq
))
727 (declare (type ctype type1 type2
))
728 (cond ((or (eq type1
*wild-type
*) (eq type2
*wild-type
*)) *wild-type
*)
729 ((eq type1
*empty-type
*) type2
)
730 ((eq type2
*empty-type
*) type1
)
732 (values (values-type-op type1 type2
#'type-union
#'min
)))))
734 (defun-cached (values-type-intersection :hash-function
#'type-cache-hash
736 ((type1 eq
) (type2 eq
))
737 (declare (type ctype type1 type2
))
738 (cond ((eq type1
*wild-type
*)
739 (coerce-to-values type2
))
740 ((or (eq type2
*wild-type
*) (eq type2
*universal-type
*))
742 ((or (eq type1
*empty-type
*) (eq type2
*empty-type
*))
744 ((and (not (values-type-p type2
))
745 (values-type-required type1
))
746 (let ((req1 (values-type-required type1
)))
747 (make-values-type :required
(cons (type-intersection (first req1
) type2
)
749 :optional
(values-type-optional type1
)
750 :rest
(values-type-rest type1
)
751 :allowp
(values-type-allowp type1
))))
753 (values (values-type-op type1
(coerce-to-values type2
)
757 ;;; This is like TYPES-EQUAL-OR-INTERSECT, except that it sort of
758 ;;; works on VALUES types. Note that due to the semantics of
759 ;;; VALUES-TYPE-INTERSECTION, this might return (VALUES T T) when
760 ;;; there isn't really any intersection.
761 (defun values-types-equal-or-intersect (type1 type2
)
762 (cond ((or (eq type1
*empty-type
*) (eq type2
*empty-type
*))
764 ((or (eq type1
*wild-type
*) (eq type2
*wild-type
*))
767 (let ((res (values-type-intersection type1 type2
)))
768 (values (not (eq res
*empty-type
*))
771 ;;; a SUBTYPEP-like operation that can be used on any types, including
773 (defun-cached (values-subtypep :hash-function
#'type-cache-hash
776 ((type1 eq
) (type2 eq
))
777 (declare (type ctype type1 type2
))
778 (cond ((or (eq type2
*wild-type
*) (eq type2
*universal-type
*)
779 (eq type1
*empty-type
*))
781 ((eq type1
*wild-type
*)
782 (values (eq type2
*wild-type
*) t
))
783 ((or (eq type2
*empty-type
*)
784 (not (values-types-equal-or-intersect type1 type2
)))
786 ((and (not (values-type-p type2
))
787 (values-type-required type1
))
788 (csubtypep (first (values-type-required type1
))
790 (t (setq type2
(coerce-to-values type2
))
791 (multiple-value-bind (types1 rest1
) (values-type-types type1
)
792 (multiple-value-bind (types2 rest2
) (values-type-types type2
)
793 (cond ((< (length (values-type-required type1
))
794 (length (values-type-required type2
)))
796 ((< (length types1
) (length types2
))
799 (do ((t1 types1
(rest t1
))
800 (t2 types2
(rest t2
)))
802 (csubtypep rest1 rest2
))
803 (multiple-value-bind (res win-p
)
804 (csubtypep (first t1
) (first t2
))
806 (return (values nil nil
)))
808 (return (values nil t
))))))))))))
810 ;;;; type method interfaces
812 ;;; like SUBTYPEP, only works on CTYPE structures
813 (defun-cached (csubtypep :hash-function
#'type-cache-hash
817 ((type1 eq
) (type2 eq
))
818 (declare (type ctype type1 type2
))
819 (cond ((or (eq type1 type2
)
820 (eq type1
*empty-type
*)
821 (eq type2
*universal-type
*))
824 ((eq type1
*universal-type
*)
828 (!invoke-type-method
:simple-subtypep
:complex-subtypep-arg2
830 :complex-arg1
:complex-subtypep-arg1
)))))
832 ;;; Just parse the type specifiers and call CSUBTYPE.
833 (defun sb!xc
:subtypep
(type1 type2
&optional environment
)
835 "Return two values indicating the relationship between type1 and type2.
836 If values are T and T, type1 definitely is a subtype of type2.
837 If values are NIL and T, type1 definitely is not a subtype of type2.
838 If values are NIL and NIL, it couldn't be determined."
839 (declare (type lexenv-designator environment
) (ignore environment
))
840 (declare (explicit-check))
841 (csubtypep (specifier-type type1
) (specifier-type type2
)))
843 ;;; If two types are definitely equivalent, return true. The second
844 ;;; value indicates whether the first value is definitely correct.
845 ;;; This should only fail in the presence of HAIRY types.
846 (defun-cached (type= :hash-function
#'type-cache-hash
850 ((type1 eq
) (type2 eq
))
851 (declare (type ctype type1 type2
))
852 (cond ((eq type1 type2
)
854 ;; If args are not EQ, but both allow TYPE= optimization,
855 ;; and at least one is interned, then return no and certainty.
856 ;; Most of the interned CTYPEs admit this optimization,
857 ;; NUMERIC and MEMBER types do as well.
858 ((and (minusp (logior (type-hash-value type1
) (type-hash-value type2
)))
859 (logtest (logand (type-hash-value type1
) (type-hash-value type2
))
860 +type-admits-type
=-optimization
+))
863 (memoize (!invoke-type-method
:simple-
= :complex-
= type1 type2
)))))
865 ;;; Not exactly the negation of TYPE=, since when the relationship is
866 ;;; uncertain, we still return NIL, NIL. This is useful in cases where
867 ;;; the conservative assumption is =.
868 (defun type/= (type1 type2
)
869 (declare (type ctype type1 type2
))
870 (multiple-value-bind (res win
) (type= type1 type2
)
875 ;;; the type method dispatch case of TYPE-UNION2
876 (defun %type-union2
(type1 type2
)
877 ;; As in %TYPE-INTERSECTION2, it seems to be a good idea to give
878 ;; both argument orders a chance at COMPLEX-INTERSECTION2. Unlike
879 ;; %TYPE-INTERSECTION2, though, I don't have a specific case which
880 ;; demonstrates this is actually necessary. Also unlike
881 ;; %TYPE-INTERSECTION2, there seems to be no need to distinguish
882 ;; between not finding a method and having a method return NIL.
884 (!invoke-type-method
:simple-union2
:complex-union2
887 (declare (inline 1way
))
888 (or (1way type1 type2
)
889 (1way type2 type1
))))
891 ;;; Find a type which includes both types. Any inexactness is
892 ;;; represented by the fuzzy element types; we return a single value
893 ;;; that is precise to the best of our knowledge. This result is
894 ;;; simplified into the canonical form, thus is not a UNION-TYPE
895 ;;; unless we find no other way to represent the result.
896 (defun-cached (type-union2 :hash-function
#'type-cache-hash
899 ((type1 eq
) (type2 eq
))
900 ;; KLUDGE: This was generated from TYPE-INTERSECTION2 by Ye Olde Cut And
901 ;; Paste technique of programming. If it stays around (as opposed to
902 ;; e.g. fading away in favor of some CLOS solution) the shared logic
903 ;; should probably become shared code. -- WHN 2001-03-16
904 (declare (type ctype type1 type2
))
910 ;; CSUBTYPEP for array-types answers questions about the
911 ;; specialized type, yet for union we want to take the
912 ;; expressed type in account too.
913 ((and (not (and (array-type-p type1
) (array-type-p type2
)))
914 (or (setf t2
(csubtypep type1 type2
))
915 (csubtypep type2 type1
)))
917 ((or (union-type-p type1
)
918 (union-type-p type2
))
919 ;; Unions of UNION-TYPE should have the UNION-TYPE-TYPES
920 ;; values broken out and united separately. The full TYPE-UNION
921 ;; function knows how to do this, so let it handle it.
922 (type-union type1 type2
))
924 ;; the ordinary case: we dispatch to type methods
925 (%type-union2 type1 type2
)))))))
927 ;;; the type method dispatch case of TYPE-INTERSECTION2
928 (defun %type-intersection2
(type1 type2
)
929 ;; We want to give both argument orders a chance at
930 ;; COMPLEX-INTERSECTION2. Without that, the old CMU CL type
931 ;; methods could give noncommutative results, e.g.
932 ;; (TYPE-INTERSECTION2 *EMPTY-TYPE* SOME-HAIRY-TYPE)
934 ;; (TYPE-INTERSECTION2 SOME-HAIRY-TYPE *EMPTY-TYPE*)
935 ;; => #<NAMED-TYPE NIL>, T
936 ;; We also need to distinguish between the case where we found a
937 ;; type method, and it returned NIL, and the case where we fell
938 ;; through without finding any type method. An example of the first
939 ;; case is the intersection of a HAIRY-TYPE with some ordinary type.
940 ;; An example of the second case is the intersection of two
941 ;; completely-unrelated types, e.g. CONS and NUMBER, or SYMBOL and
944 ;; (Why yes, CLOS probably *would* be nicer..)
946 (!invoke-type-method
:simple-intersection2
:complex-intersection2
948 :default
:call-other-method
)))
949 (declare (inline 1way
))
950 (let ((xy (1way type1 type2
)))
951 (or (and (not (eql xy
:call-other-method
)) xy
)
952 (let ((yx (1way type2 type1
)))
953 (or (and (not (eql yx
:call-other-method
)) yx
)
954 (cond ((and (eql xy
:call-other-method
)
955 (eql yx
:call-other-method
))
960 (defun-cached (type-intersection2 :hash-function
#'type-cache-hash
964 ((type1 eq
) (type2 eq
))
965 (declare (type ctype type1 type2
))
967 ;; FIXME: For some reason, this doesn't catch e.g. type1 =
968 ;; type2 = (SPECIFIER-TYPE
969 ;; 'SOME-UNKNOWN-TYPE). Investigate. - CSR, 2002-04-10
973 ((or (intersection-type-p type1
)
974 (intersection-type-p type2
))
975 ;; Intersections of INTERSECTION-TYPE should have the
976 ;; INTERSECTION-TYPE-TYPES values broken out and intersected
977 ;; separately. The full TYPE-INTERSECTION function knows how
978 ;; to do that, so let it handle it.
979 (type-intersection type1 type2
))
981 ;; the ordinary case: we dispatch to type methods
982 (%type-intersection2 type1 type2
))))))
984 ;;; Return as restrictive and simple a type as we can discover that is
985 ;;; no more restrictive than the intersection of TYPE1 and TYPE2. At
986 ;;; worst, we arbitrarily return one of the arguments as the first
987 ;;; value (trying not to return a hairy type).
988 (defun type-approx-intersection2 (type1 type2
)
989 (cond ((type-intersection2 type1 type2
))
990 ((hairy-type-p type1
) type2
)
993 ;;; a test useful for checking whether a derived type matches a
996 ;;; The first value is true unless the types don't intersect and
997 ;;; aren't equal. The second value is true if the first value is
998 ;;; definitely correct. NIL is considered to intersect with any type.
999 ;;; If T is a subtype of either type, then we also return T, T. This
1000 ;;; way we recognize that hairy types might intersect with T.
1002 ;;; Well now given the statement above that this is "useful for ..."
1003 ;;; a particular thing, I see how treating *empty-type* magically could
1004 ;;; be useful, however given all the _other_ calls to this function within
1005 ;;; this file, it seems suboptimal, because logically it is wrong.
1006 (defun types-equal-or-intersect (type1 type2
)
1007 (declare (type ctype type1 type2
))
1008 (if (or (eq type1
*empty-type
*) (eq type2
*empty-type
*))
1010 (let ((intersection2 (type-intersection2 type1 type2
)))
1011 (cond ((not intersection2
)
1012 (if (or (csubtypep *universal-type
* type1
)
1013 (csubtypep *universal-type
* type2
))
1016 ((eq intersection2
*empty-type
*) (values nil t
))
1017 (t (values t t
))))))
1019 ;;; Return a Common Lisp type specifier corresponding to the TYPE
1021 (defun type-specifier (type)
1022 (declare (type ctype type
))
1023 (funcall (type-class-unparse (type-class-info type
)) type
))
1025 ;;; Don't try to define a print method until it's actually gonna work!
1026 ;;; (Otherwise this would be near the DEFSTRUCT)
1027 (def!method print-object
((ctype ctype
) stream
)
1028 (print-unreadable-object (ctype stream
:type t
)
1029 (prin1 (type-specifier ctype
) stream
)))
1032 ;;; Just dump it as a specifier. (We'll convert it back upon loading.)
1033 (defun make-type-load-form (type)
1034 (declare (type ctype type
))
1035 `(specifier-type ',(type-specifier type
)))
1037 (defun-cached (type-negation :hash-function
#'type-hash-value
1041 (declare (type ctype type
))
1042 (funcall (type-class-negate (type-class-info type
)) type
))
1044 (defun-cached (type-singleton-p :hash-function
#'type-hash-value
1048 (declare (type ctype type
))
1049 (let ((function (type-class-singleton-p (type-class-info type
))))
1051 (funcall function type
)
1054 ;;; (VALUES-SPECIFIER-TYPE and SPECIFIER-TYPE moved from here to
1055 ;;; early-type.lisp by WHN ca. 19990201.)
1057 ;;; Take a list of type specifiers, computing the translation of each
1058 ;;; specifier and defining it as a builtin type.
1059 ;;; Seee the comments in 'type-init' for why this is a slightly
1060 ;;; screwy way to go about it.
1061 (declaim (ftype (function (list) (values)) !precompute-types
))
1062 (defun !precompute-types
(specs)
1063 (dolist (spec specs
)
1064 (let ((res (handler-bind
1065 ((parse-unknown-type
1067 (declare (ignore c
))
1068 ;; We can handle conditions at this point,
1069 ;; but win32 can not perform i/o here because
1070 ;; !MAKE-COLD-STDERR-STREAM has no implementation.
1072 (progn (write-string "//caught: parse-unknown ")
1075 (specifier-type spec
))))
1076 (unless (unknown-type-p res
)
1077 (setf (info :type
:builtin spec
) res
)
1078 (setf (info :type
:kind spec
) :primitive
))))
1081 ;;;; general TYPE-UNION and TYPE-INTERSECTION operations
1083 ;;;; These are fully general operations on CTYPEs: they'll always
1084 ;;;; return a CTYPE representing the result.
1086 ;;; shared logic for unions and intersections: Return a list of
1087 ;;; types representing the same types as INPUT-TYPES, but with
1088 ;;; COMPOUND-TYPEs satisfying %COMPOUND-TYPE-P broken up into their
1089 ;;; component types, and with any SIMPLY2 simplifications applied.
1091 ((def (name compound-type-p simplify2
)
1092 `(defun ,name
(types)
1094 (multiple-value-bind (first rest
)
1095 (if (,compound-type-p
(car types
))
1096 (values (car (compound-type-types (car types
)))
1097 (append (cdr (compound-type-types (car types
)))
1099 (values (car types
) (cdr types
)))
1100 (let ((rest (,name rest
)) u
)
1101 (dolist (r rest
(cons first rest
))
1102 (when (setq u
(,simplify2 first r
))
1103 (return (,name
(nsubstitute u r rest
)))))))))))
1104 (def simplify-intersections intersection-type-p type-intersection2
)
1105 (def simplify-unions union-type-p type-union2
))
1107 (defun maybe-distribute-one-union (union-type types
)
1108 (let* ((intersection (apply #'type-intersection types
))
1109 (union (mapcar (lambda (x) (type-intersection x intersection
))
1110 (union-type-types union-type
))))
1111 (if (notany (lambda (x) (or (hairy-type-p x
)
1112 (intersection-type-p x
)))
1117 (defun type-intersection (&rest input-types
)
1118 (%type-intersection input-types
))
1119 (defun-cached (%type-intersection
:hash-bits
10 :hash-function
#'type-list-cache-hash
)
1120 ((input-types equal
))
1121 (let ((simplified-types (simplify-intersections input-types
)))
1122 (declare (type list simplified-types
))
1123 ;; We want to have a canonical representation of types (or failing
1124 ;; that, punt to HAIRY-TYPE). Canonical representation would have
1125 ;; intersections inside unions but not vice versa, since you can
1126 ;; always achieve that by the distributive rule. But we don't want
1127 ;; to just apply the distributive rule, since it would be too easy
1128 ;; to end up with unreasonably huge type expressions. So instead
1129 ;; we try to generate a simple type by distributing the union; if
1130 ;; the type can't be made simple, we punt to HAIRY-TYPE.
1131 (if (and (cdr simplified-types
) (some #'union-type-p simplified-types
))
1132 (let* ((first-union (find-if #'union-type-p simplified-types
))
1133 (other-types (coerce (remove first-union simplified-types
)
1135 (distributed (maybe-distribute-one-union first-union
1138 (apply #'type-union distributed
)
1139 (%make-hairy-type
`(and ,@(map 'list
#'type-specifier
1140 simplified-types
)))))
1142 ((null simplified-types
) *universal-type
*)
1143 ((null (cdr simplified-types
)) (car simplified-types
))
1144 (t (%make-intersection-type
1145 (some #'type-enumerable simplified-types
)
1146 simplified-types
))))))
1148 (defun type-union (&rest input-types
)
1149 (%type-union input-types
))
1150 (defun-cached (%type-union
:hash-bits
8 :hash-function
#'type-list-cache-hash
)
1151 ((input-types equal
))
1152 (let ((simplified-types (simplify-unions input-types
)))
1154 ((null simplified-types
) *empty-type
*)
1155 ((null (cdr simplified-types
)) (car simplified-types
))
1157 (every #'type-enumerable simplified-types
)
1158 simplified-types
)))))
1162 (!define-type-method
(named :simple-
=) (type1 type2
)
1163 ;;(aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1164 (values (eq type1 type2
) t
))
1166 (defun cons-type-might-be-empty-type (type)
1167 (declare (type cons-type type
))
1168 (let ((car-type (cons-type-car-type type
))
1169 (cdr-type (cons-type-cdr-type type
)))
1171 (if (cons-type-p car-type
)
1172 (cons-type-might-be-empty-type car-type
)
1173 (multiple-value-bind (yes surep
)
1174 (type= car-type
*empty-type
*)
1177 (if (cons-type-p cdr-type
)
1178 (cons-type-might-be-empty-type cdr-type
)
1179 (multiple-value-bind (yes surep
)
1180 (type= cdr-type
*empty-type
*)
1184 (defun cons-type-length-info (type)
1185 (declare (type cons-type type
))
1186 (do ((min 1 (1+ min
))
1187 (cdr (cons-type-cdr-type type
) (cons-type-cdr-type cdr
)))
1188 ((not (cons-type-p cdr
))
1190 ((csubtypep cdr
(specifier-type 'null
))
1192 ((csubtypep *universal-type
* cdr
)
1194 ((type/= (type-intersection (specifier-type 'cons
) cdr
) *empty-type
*)
1196 ((type/= (type-intersection (specifier-type 'null
) cdr
) *empty-type
*)
1198 (t (values min
:maybe
))))
1201 (!define-type-method
(named :complex-
=) (type1 type2
)
1203 ((and (eq type2
*empty-type
*)
1204 (or (and (intersection-type-p type1
)
1205 ;; not allowed to be unsure on these... FIXME: keep
1206 ;; the list of CL types that are intersection types
1207 ;; once and only once.
1208 (not (or (type= type1
(specifier-type 'ratio
))
1209 (type= type1
(specifier-type 'keyword
)))))
1210 (and (cons-type-p type1
)
1211 (cons-type-might-be-empty-type type1
))))
1212 ;; things like (AND (EQL 0) (SATISFIES ODDP)) or (AND FUNCTION
1213 ;; STREAM) can get here. In general, we can't really tell
1214 ;; whether these are equal to NIL or not, so
1216 ((type-might-contain-other-types-p type1
)
1217 (invoke-complex-=-other-method type1 type2
))
1218 (t (values nil t
))))
1220 (!define-type-method
(named :simple-subtypep
) (type1 type2
)
1221 (aver (not (eq type1
*wild-type
*))) ; * isn't really a type.
1222 (aver (not (eq type1 type2
)))
1223 (values (or (eq type1
*empty-type
*)
1224 (eq type2
*wild-type
*)
1225 (eq type2
*universal-type
*)) t
))
1227 (!define-type-method
(named :complex-subtypep-arg1
) (type1 type2
)
1228 ;; This AVER causes problems if we write accurate methods for the
1229 ;; union (and possibly intersection) types which then delegate to
1230 ;; us; while a user shouldn't get here, because of the odd status of
1231 ;; *wild-type* a type-intersection executed by the compiler can. -
1234 ;; (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1235 (cond ((eq type1
*empty-type
*)
1237 (;; When TYPE2 might be the universal type in disguise
1238 (type-might-contain-other-types-p type2
)
1239 ;; Now that the UNION and HAIRY COMPLEX-SUBTYPEP-ARG2 methods
1240 ;; can delegate to us (more or less as CALL-NEXT-METHOD) when
1241 ;; they're uncertain, we can't just barf on COMPOUND-TYPE and
1242 ;; HAIRY-TYPEs as we used to. Instead we deal with the
1243 ;; problem (where at least part of the problem is cases like
1244 ;; (SUBTYPEP T '(SATISFIES FOO))
1246 ;; (SUBTYPEP T '(AND (SATISFIES FOO) (SATISFIES BAR)))
1247 ;; where the second type is a hairy type like SATISFIES, or
1248 ;; is a compound type which might contain a hairy type) by
1249 ;; returning uncertainty.
1251 ((eq type1
*funcallable-instance-type
*)
1252 (values (eq type2
(specifier-type 'function
)) t
))
1254 ;; This case would have been picked off by the SIMPLE-SUBTYPEP
1255 ;; method, and so shouldn't appear here.
1256 (aver (not (named-type-p type2
)))
1257 ;; Since TYPE2 is not EQ *UNIVERSAL-TYPE* and is not another
1258 ;; named type in disguise, TYPE2 is not a superset of TYPE1.
1261 (!define-type-method
(named :complex-subtypep-arg2
) (type1 type2
)
1262 (aver (not (eq type2
*wild-type
*))) ; * isn't really a type.
1263 (cond ((eq type2
*universal-type
*)
1265 ;; some CONS types can conceal danger
1266 ((and (cons-type-p type1
) (cons-type-might-be-empty-type type1
))
1268 ((type-might-contain-other-types-p type1
)
1269 ;; those types can be other types in disguise. So we'd
1271 (invoke-complex-subtypep-arg1-method type1 type2
))
1272 ((and (or (eq type2
*instance-type
*)
1273 (eq type2
*funcallable-instance-type
*))
1274 (member-type-p type1
))
1275 ;; member types can be subtypep INSTANCE and
1276 ;; FUNCALLABLE-INSTANCE in surprising ways.
1277 (invoke-complex-subtypep-arg1-method type1 type2
))
1278 ((and (eq type2
*extended-sequence-type
*) (classoid-p type1
))
1279 (let* ((layout (classoid-layout type1
))
1280 (inherits (layout-inherits layout
))
1281 (sequencep (find (classoid-layout (find-classoid 'sequence
))
1283 (values (if sequencep t nil
) t
)))
1284 ((and (eq type2
*instance-type
*) (classoid-p type1
))
1285 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1287 (let* ((layout (classoid-layout type1
))
1288 (inherits (layout-inherits layout
))
1289 (functionp (find (classoid-layout (find-classoid 'function
))
1294 ((eq type1
(find-classoid 'function
))
1296 ((or (structure-classoid-p type1
)
1298 (condition-classoid-p type1
))
1300 (t (values nil nil
))))))
1301 ((and (eq type2
*funcallable-instance-type
*) (classoid-p type1
))
1302 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1304 (let* ((layout (classoid-layout type1
))
1305 (inherits (layout-inherits layout
))
1306 (functionp (find (classoid-layout (find-classoid 'function
))
1308 (values (if functionp t nil
) t
))))
1310 ;; FIXME: This seems to rely on there only being 4 or 5
1311 ;; NAMED-TYPE values, and the exclusion of various
1312 ;; possibilities above. It would be good to explain it and/or
1313 ;; rewrite it so that it's clearer.
1316 (!define-type-method
(named :simple-intersection2
) (type1 type2
)
1318 ((and (eq type1
*extended-sequence-type
*)
1319 (or (eq type2
*instance-type
*)
1320 (eq type2
*funcallable-instance-type
*)))
1322 ((and (or (eq type1
*instance-type
*)
1323 (eq type1
*funcallable-instance-type
*))
1324 (eq type2
*extended-sequence-type
*))
1327 (hierarchical-intersection2 type1 type2
))))
1329 (!define-type-method
(named :complex-intersection2
) (type1 type2
)
1330 ;; FIXME: This assertion failed when I added it in sbcl-0.6.11.13.
1331 ;; Perhaps when bug 85 is fixed it can be reenabled.
1332 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1334 ((eq type2
*extended-sequence-type
*)
1336 (structure-classoid *empty-type
*)
1338 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1340 (if (find (classoid-layout (find-classoid 'sequence
))
1341 (layout-inherits (classoid-layout type1
)))
1345 (if (or (type-might-contain-other-types-p type1
)
1346 (member-type-p type1
))
1349 ((eq type2
*instance-type
*)
1351 (structure-classoid type1
)
1353 (if (and (not (member type1
*non-instance-classoid-types
*
1354 :key
#'find-classoid
))
1355 (not (eq type1
(find-classoid 'function
)))
1356 (not (find (classoid-layout (find-classoid 'function
))
1357 (layout-inherits (classoid-layout type1
)))))
1361 (if (or (type-might-contain-other-types-p type1
)
1362 (member-type-p type1
))
1365 ((eq type2
*funcallable-instance-type
*)
1367 (structure-classoid *empty-type
*)
1369 (if (member type1
*non-instance-classoid-types
* :key
#'find-classoid
)
1371 (if (find (classoid-layout (find-classoid 'function
))
1372 (layout-inherits (classoid-layout type1
)))
1374 (if (type= type1
(find-classoid 'function
))
1379 (if (or (type-might-contain-other-types-p type1
)
1380 (member-type-p type1
))
1383 (t (hierarchical-intersection2 type1 type2
))))
1385 (!define-type-method
(named :complex-union2
) (type1 type2
)
1386 ;; Perhaps when bug 85 is fixed this can be reenabled.
1387 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1389 ((eq type2
*extended-sequence-type
*)
1390 (if (classoid-p type1
)
1391 (if (or (member type1
*non-instance-classoid-types
*
1392 :key
#'find-classoid
)
1393 (not (find (classoid-layout (find-classoid 'sequence
))
1394 (layout-inherits (classoid-layout type1
)))))
1398 ((eq type2
*instance-type
*)
1399 (if (classoid-p type1
)
1400 (if (or (member type1
*non-instance-classoid-types
*
1401 :key
#'find-classoid
)
1402 (find (classoid-layout (find-classoid 'function
))
1403 (layout-inherits (classoid-layout type1
))))
1407 ((eq type2
*funcallable-instance-type
*)
1408 (if (classoid-p type1
)
1409 (if (or (member type1
*non-instance-classoid-types
*
1410 :key
#'find-classoid
)
1411 (not (find (classoid-layout (find-classoid 'function
))
1412 (layout-inherits (classoid-layout type1
)))))
1414 (if (eq type1
(specifier-type 'function
))
1418 (t (hierarchical-union2 type1 type2
))))
1420 (!define-type-method
(named :negate
) (x)
1421 (aver (not (eq x
*wild-type
*)))
1423 ((eq x
*universal-type
*) *empty-type
*)
1424 ((eq x
*empty-type
*) *universal-type
*)
1425 ((or (eq x
*instance-type
*)
1426 (eq x
*funcallable-instance-type
*)
1427 (eq x
*extended-sequence-type
*))
1428 (make-negation-type x
))
1429 (t (bug "NAMED type unexpected: ~S" x
))))
1431 (!define-type-method
(named :unparse
) (x)
1432 (named-type-name x
))
1434 ;;;; hairy and unknown types
1435 ;;;; DEFINE-TYPE-CLASS HAIRY is in 'early-type'
1437 (!define-type-method
(hairy :negate
) (x) (make-negation-type x
))
1439 (!define-type-method
(hairy :unparse
) (x)
1440 (hairy-type-specifier x
))
1442 (!define-type-method
(hairy :simple-subtypep
) (type1 type2
)
1443 (let ((hairy-spec1 (hairy-type-specifier type1
))
1444 (hairy-spec2 (hairy-type-specifier type2
)))
1445 (cond ((equal-but-no-car-recursion hairy-spec1 hairy-spec2
)
1447 ((maybe-reparse-specifier! type1
)
1448 (csubtypep type1 type2
))
1449 ((maybe-reparse-specifier! type2
)
1450 (csubtypep type1 type2
))
1452 (values nil nil
)))))
1454 (!define-type-method
(hairy :complex-subtypep-arg2
) (type1 type2
)
1455 (if (maybe-reparse-specifier! type2
)
1456 (csubtypep type1 type2
)
1457 (let ((specifier (hairy-type-specifier type2
)))
1458 (cond ((and (consp specifier
) (eql (car specifier
) 'satisfies
))
1459 (case (cadr specifier
)
1460 ((keywordp) (if (type= type1
(specifier-type 'symbol
))
1462 (invoke-complex-subtypep-arg1-method type1 type2
)))
1463 (t (invoke-complex-subtypep-arg1-method type1 type2
))))
1465 (invoke-complex-subtypep-arg1-method type1 type2
))))))
1467 (!define-type-method
(hairy :complex-subtypep-arg1
) (type1 type2
)
1468 (if (maybe-reparse-specifier! type1
)
1469 (csubtypep type1 type2
)
1472 (!define-type-method
(hairy :complex-
=) (type1 type2
)
1473 (if (maybe-reparse-specifier! type2
)
1477 (!define-type-method
(hairy :simple-intersection2
:complex-intersection2
)
1479 (acond ((type= type1 type2
)
1481 ((eq type2
(literal-ctype *satisfies-keywordp-type
*))
1482 ;; (AND (MEMBER A) (SATISFIES KEYWORDP)) is possibly non-empty
1483 ;; if A is re-homed as :A. However as a special case that really
1484 ;; does occur, (AND (MEMBER NIL) (SATISFIES KEYWORDP))
1485 ;; is empty because of the illegality of changing NIL's package.
1486 (if (eq type1
(specifier-type 'null
))
1488 (multiple-value-bind (answer certain
)
1489 (types-equal-or-intersect type1
(specifier-type 'symbol
))
1490 (and (not answer
) certain
*empty-type
*))))
1491 ((eq type2
(literal-ctype *fun-name-type
*))
1492 (multiple-value-bind (answer certain
)
1493 (types-equal-or-intersect type1
(specifier-type 'symbol
))
1496 (multiple-value-bind (answer certain
)
1497 (types-equal-or-intersect type1
(specifier-type 'cons
))
1498 (and (not answer
) certain
*empty-type
*)))))
1499 ((and (typep (hairy-type-specifier type2
) '(cons (eql satisfies
)))
1500 (info :function
:predicate-truth-constraint
1501 (cadr (hairy-type-specifier type2
))))
1502 (multiple-value-bind (answer certain
)
1503 (types-equal-or-intersect type1
(specifier-type it
))
1504 (and (not answer
) certain
*empty-type
*)))))
1506 (!define-type-method
(hairy :simple-union2
)
1508 (if (type= type1 type2
)
1512 (!define-type-method
(hairy :simple-
=) (type1 type2
)
1513 (if (equal-but-no-car-recursion (hairy-type-specifier type1
)
1514 (hairy-type-specifier type2
))
1518 (!def-type-translator satisfies
:list
(&whole whole predicate-name
)
1519 (unless (symbolp predicate-name
)
1520 (error 'simple-type-error
1521 :datum predicate-name
1522 :expected-type
'symbol
1523 :format-control
"The SATISFIES predicate name is not a symbol: ~S"
1524 :format-arguments
(list predicate-name
)))
1525 (case predicate-name
1526 (keywordp (literal-ctype *satisfies-keywordp-type
*))
1527 (legal-fun-name-p (literal-ctype *fun-name-type
*))
1528 (t (%make-hairy-type whole
))))
1532 (!define-type-method
(negation :negate
) (x)
1533 (negation-type-type x
))
1535 (!define-type-method
(negation :unparse
) (x)
1536 (if (type= (negation-type-type x
) (specifier-type 'cons
))
1538 `(not ,(type-specifier (negation-type-type x
)))))
1540 (!define-type-method
(negation :simple-subtypep
) (type1 type2
)
1541 (csubtypep (negation-type-type type2
) (negation-type-type type1
)))
1543 (!define-type-method
(negation :complex-subtypep-arg2
) (type1 type2
)
1544 (let* ((complement-type2 (negation-type-type type2
))
1545 (intersection2 (type-intersection2 type1
1548 ;; FIXME: if uncertain, maybe try arg1?
1549 (type= intersection2
*empty-type
*)
1550 (invoke-complex-subtypep-arg1-method type1 type2
))))
1552 (!define-type-method
(negation :complex-subtypep-arg1
) (type1 type2
)
1553 ;; "Incrementally extended heuristic algorithms tend inexorably toward the
1554 ;; incomprehensible." -- http://www.unlambda.com/~james/lambda/lambda.txt
1556 ;; You may not believe this. I couldn't either. But then I sat down
1557 ;; and drew lots of Venn diagrams. Comments involving a and b refer
1558 ;; to the call (subtypep '(not a) 'b) -- CSR, 2002-02-27.
1560 ;; (Several logical truths in this block are true as long as
1561 ;; b/=T. As of sbcl-0.7.1.28, it seems impossible to construct a
1562 ;; case with b=T where we actually reach this type method, but
1563 ;; we'll test for and exclude this case anyway, since future
1564 ;; maintenance might make it possible for it to end up in this
1566 (multiple-value-bind (equal certain
)
1567 (type= type2
*universal-type
*)
1569 (return (values nil nil
)))
1571 (return (values t t
))))
1572 (let ((complement-type1 (negation-type-type type1
)))
1573 ;; Do the special cases first, in order to give us a chance if
1574 ;; subtype/supertype relationships are hairy.
1575 (multiple-value-bind (equal certain
)
1576 (type= complement-type1 type2
)
1577 ;; If a = b, ~a is not a subtype of b (unless b=T, which was
1580 (return (values nil nil
)))
1582 (return (values nil t
))))
1583 ;; KLUDGE: ANSI requires that the SUBTYPEP result between any
1584 ;; two built-in atomic type specifiers never be uncertain. This
1585 ;; is hard to do cleanly for the built-in types whose
1586 ;; definitions include (NOT FOO), i.e. CONS and RATIO. However,
1587 ;; we can do it with this hack, which uses our global knowledge
1588 ;; that our implementation of the type system uses disjoint
1589 ;; implementation types to represent disjoint sets (except when
1590 ;; types are contained in other types). (This is a KLUDGE
1591 ;; because it's fragile. Various changes in internal
1592 ;; representation in the type system could make it start
1593 ;; confidently returning incorrect results.) -- WHN 2002-03-08
1594 (unless (or (type-might-contain-other-types-p complement-type1
)
1595 (type-might-contain-other-types-p type2
))
1596 ;; Because of the way our types which don't contain other
1597 ;; types are disjoint subsets of the space of possible values,
1598 ;; (SUBTYPEP '(NOT AA) 'B)=NIL when AA and B are simple (and B
1599 ;; is not T, as checked above).
1600 (return (values nil t
)))
1601 ;; The old (TYPE= TYPE1 TYPE2) branch would never be taken, as
1602 ;; TYPE1 and TYPE2 will only be equal if they're both NOT types,
1603 ;; and then the :SIMPLE-SUBTYPEP method would be used instead.
1604 ;; But a CSUBTYPEP relationship might still hold:
1605 (multiple-value-bind (equal certain
)
1606 (csubtypep complement-type1 type2
)
1607 ;; If a is a subtype of b, ~a is not a subtype of b (unless
1608 ;; b=T, which was excluded above).
1610 (return (values nil nil
)))
1612 (return (values nil t
))))
1613 (multiple-value-bind (equal certain
)
1614 (csubtypep type2 complement-type1
)
1615 ;; If b is a subtype of a, ~a is not a subtype of b. (FIXME:
1616 ;; That's not true if a=T. Do we know at this point that a is
1619 (return (values nil nil
)))
1621 (return (values nil t
))))
1622 ;; old CSR comment ca. 0.7.2, now obsoleted by the SIMPLE-CTYPE?
1623 ;; KLUDGE case above: Other cases here would rely on being able
1624 ;; to catch all possible cases, which the fragility of this type
1625 ;; system doesn't inspire me; for instance, if a is type= to ~b,
1626 ;; then we want T, T; if this is not the case and the types are
1627 ;; disjoint (have an intersection of *empty-type*) then we want
1628 ;; NIL, T; else if the union of a and b is the *universal-type*
1629 ;; then we want T, T. So currently we still claim to be unsure
1630 ;; about e.g. (subtypep '(not fixnum) 'single-float).
1632 ;; OTOH we might still get here:
1635 (!define-type-method
(negation :complex-
=) (type1 type2
)
1636 ;; (NOT FOO) isn't equivalent to anything that's not a negation
1637 ;; type, except possibly a type that might contain it in disguise.
1638 (declare (ignore type2
))
1639 (if (type-might-contain-other-types-p type1
)
1643 (!define-type-method
(negation :simple-intersection2
) (type1 type2
)
1644 (let ((not1 (negation-type-type type1
))
1645 (not2 (negation-type-type type2
)))
1647 ((csubtypep not1 not2
) type2
)
1648 ((csubtypep not2 not1
) type1
)
1649 ;; Why no analagous clause to the disjoint in the SIMPLE-UNION2
1650 ;; method, below? The clause would read
1652 ;; ((EQ (TYPE-UNION NOT1 NOT2) *UNIVERSAL-TYPE*) *EMPTY-TYPE*)
1654 ;; but with proper canonicalization of negation types, there's
1655 ;; no way of constructing two negation types with union of their
1656 ;; negations being the universal type.
1658 (aver (not (eq (type-union not1 not2
) *universal-type
*)))
1661 (defun maybe-complex-array-refinement (type1 type2
)
1662 (let* ((ntype (negation-type-type type2
))
1663 (ndims (array-type-dimensions ntype
))
1664 (ncomplexp (array-type-complexp ntype
))
1665 (nseltype (array-type-specialized-element-type ntype
))
1666 (neltype (array-type-element-type ntype
)))
1667 (if (and (eql ndims
'*) (null ncomplexp
)
1668 (eq neltype
*wild-type
*) (eq nseltype
*wild-type
*))
1669 (make-array-type (array-type-dimensions type1
)
1671 :element-type
(array-type-element-type type1
)
1672 :specialized-element-type
(array-type-specialized-element-type type1
)))))
1674 (!define-type-method
(negation :complex-intersection2
) (type1 type2
)
1676 ((csubtypep type1
(negation-type-type type2
)) *empty-type
*)
1677 ((eq (type-intersection type1
(negation-type-type type2
)) *empty-type
*)
1679 ((and (array-type-p type1
) (array-type-p (negation-type-type type2
)))
1680 (maybe-complex-array-refinement type1 type2
))
1683 (!define-type-method
(negation :simple-union2
) (type1 type2
)
1684 (let ((not1 (negation-type-type type1
))
1685 (not2 (negation-type-type type2
)))
1687 ((csubtypep not1 not2
) type1
)
1688 ((csubtypep not2 not1
) type2
)
1689 ((eq (type-intersection not1 not2
) *empty-type
*)
1693 (!define-type-method
(negation :complex-union2
) (type1 type2
)
1695 ((csubtypep (negation-type-type type2
) type1
) *universal-type
*)
1696 ((eq (type-intersection type1
(negation-type-type type2
)) *empty-type
*)
1700 (!define-type-method
(negation :simple-
=) (type1 type2
)
1701 (type= (negation-type-type type1
) (negation-type-type type2
)))
1703 (!def-type-translator not
:list
((:context context
) typespec
)
1704 (type-negation (specifier-type-r context typespec
)))
1708 (declaim (inline numeric-type-equal
))
1709 (defun numeric-type-equal (type1 type2
)
1710 (and (eq (numeric-type-class type1
) (numeric-type-class type2
))
1711 (eq (numeric-type-format type1
) (numeric-type-format type2
))
1712 (eq (numeric-type-complexp type1
) (numeric-type-complexp type2
))))
1714 (!define-type-method
(number :simple-
=) (type1 type2
)
1716 (and (numeric-type-equal type1 type2
)
1717 (equalp (numeric-type-low type1
) (numeric-type-low type2
))
1718 (equalp (numeric-type-high type1
) (numeric-type-high type2
)))
1721 (!define-type-method
(number :negate
) (type)
1722 (if (and (null (numeric-type-low type
)) (null (numeric-type-high type
)))
1723 (make-negation-type type
)
1725 (make-negation-type (modified-numeric-type type
:low nil
:high nil
))
1727 ((null (numeric-type-low type
))
1728 (modified-numeric-type
1730 :low
(let ((h (numeric-type-high type
)))
1731 (if (consp h
) (car h
) (list h
)))
1733 ((null (numeric-type-high type
))
1734 (modified-numeric-type
1737 :high
(let ((l (numeric-type-low type
)))
1738 (if (consp l
) (car l
) (list l
)))))
1740 (modified-numeric-type
1743 :high
(let ((l (numeric-type-low type
)))
1744 (if (consp l
) (car l
) (list l
))))
1745 (modified-numeric-type
1747 :low
(let ((h (numeric-type-high type
)))
1748 (if (consp h
) (car h
) (list h
)))
1751 (!define-type-method
(number :unparse
) (type)
1752 (let* ((complexp (numeric-type-complexp type
))
1753 (low (numeric-type-low type
))
1754 (high (numeric-type-high type
))
1755 (base (case (numeric-type-class type
)
1757 (rational 'rational
)
1758 (float (or (numeric-type-format type
) 'float
))
1761 (cond ((and (eq base
'integer
) high low
)
1762 (let ((high-count (logcount high
))
1763 (high-length (integer-length high
)))
1765 (cond ((= high
0) '(integer 0 0))
1767 ((and (= high-count high-length
)
1768 (plusp high-length
))
1769 `(unsigned-byte ,high-length
))
1771 `(mod ,(1+ high
)))))
1772 ((and (= low sb
!xc
:most-negative-fixnum
)
1773 (= high sb
!xc
:most-positive-fixnum
))
1775 ((and (= low
(lognot high
))
1776 (= high-count high-length
)
1778 `(signed-byte ,(1+ high-length
)))
1780 `(integer ,low
,high
)))))
1781 (high `(,base
,(or low
'*) ,high
))
1783 (if (and (eq base
'integer
) (= low
0))
1791 (aver (neq base
+bounds
'real
))
1792 `(complex ,base
+bounds
))
1794 (aver (eq base
+bounds
'real
))
1797 (!define-type-method
(number :singleton-p
) (type)
1798 (let ((low (numeric-type-low type
))
1799 (high (numeric-type-high type
)))
1802 (eql (numeric-type-complexp type
) :real
)
1803 (member (numeric-type-class type
) '(integer rational
1804 #-sb-xc-host float
)))
1805 (values t
(numeric-type-low type
))
1808 ;;; Return true if X is "less than or equal" to Y, taking open bounds
1809 ;;; into consideration. CLOSED is the predicate used to test the bound
1810 ;;; on a closed interval (e.g. <=), and OPEN is the predicate used on
1811 ;;; open bounds (e.g. <). Y is considered to be the outside bound, in
1812 ;;; the sense that if it is infinite (NIL), then the test succeeds,
1813 ;;; whereas if X is infinite, then the test fails (unless Y is also
1816 ;;; This is for comparing bounds of the same kind, e.g. upper and
1817 ;;; upper. Use NUMERIC-BOUND-TEST* for different kinds of bounds.
1818 (defmacro numeric-bound-test
(x y closed open
)
1823 (,closed
(car ,x
) (car ,y
))
1824 (,closed
(car ,x
) ,y
)))
1830 ;;; This is used to compare upper and lower bounds. This is different
1831 ;;; from the same-bound case:
1832 ;;; -- Since X = NIL is -infinity, whereas y = NIL is +infinity, we
1833 ;;; return true if *either* arg is NIL.
1834 ;;; -- an open inner bound is "greater" and also squeezes the interval,
1835 ;;; causing us to use the OPEN test for those cases as well.
1836 (defmacro numeric-bound-test
* (x y closed open
)
1841 (,open
(car ,x
) (car ,y
))
1842 (,open
(car ,x
) ,y
)))
1848 ;;; Return whichever of the numeric bounds X and Y is "maximal"
1849 ;;; according to the predicates CLOSED (e.g. >=) and OPEN (e.g. >).
1850 ;;; This is only meaningful for maximizing like bounds, i.e. upper and
1851 ;;; upper. If MAX-P is true, then we return NIL if X or Y is NIL,
1852 ;;; otherwise we return the other arg.
1853 (defmacro numeric-bound-max
(x y closed open max-p
)
1856 `(cond ((not ,n-x
) ,(if max-p nil n-y
))
1857 ((not ,n-y
) ,(if max-p nil n-x
))
1860 (if (,closed
(car ,n-x
) (car ,n-y
)) ,n-x
,n-y
)
1861 (if (,open
(car ,n-x
) ,n-y
) ,n-x
,n-y
)))
1864 (if (,open
(car ,n-y
) ,n-x
) ,n-y
,n-x
)
1865 (if (,closed
,n-y
,n-x
) ,n-y
,n-x
))))))
1867 (!define-type-method
(number :simple-subtypep
) (type1 type2
)
1868 (let ((class1 (numeric-type-class type1
))
1869 (class2 (numeric-type-class type2
))
1870 (complexp2 (numeric-type-complexp type2
))
1871 (format2 (numeric-type-format type2
))
1872 (low1 (numeric-type-low type1
))
1873 (high1 (numeric-type-high type1
))
1874 (low2 (numeric-type-low type2
))
1875 (high2 (numeric-type-high type2
)))
1876 ;; If one is complex and the other isn't, they are disjoint.
1877 (cond ((not (or (eq (numeric-type-complexp type1
) complexp2
)
1880 ;; If the classes are specified and different, the types are
1881 ;; disjoint unless type2 is RATIONAL and type1 is INTEGER.
1882 ;; [ or type1 is INTEGER and type2 is of the form (RATIONAL
1883 ;; X X) for integral X, but this is dealt with in the
1884 ;; canonicalization inside MAKE-NUMERIC-TYPE ]
1885 ((not (or (eq class1 class2
)
1887 (and (eq class1
'integer
) (eq class2
'rational
))))
1889 ;; If the float formats are specified and different, the types
1891 ((not (or (eq (numeric-type-format type1
) format2
)
1894 ;; Check the bounds.
1895 ((and (numeric-bound-test low1 low2
>= >)
1896 (numeric-bound-test high1 high2
<= <))
1901 (!define-superclasses number
((number)) !cold-init-forms
)
1903 ;;; If the high bound of LOW is adjacent to the low bound of HIGH,
1904 ;;; then return true, otherwise NIL.
1905 (defun numeric-types-adjacent (low high
)
1906 (let ((low-bound (numeric-type-high low
))
1907 (high-bound (numeric-type-low high
)))
1908 (cond ((not (and low-bound high-bound
)) nil
)
1909 ((and (consp low-bound
) (consp high-bound
)) nil
)
1911 (let ((low-value (car low-bound
)))
1912 (or (eql low-value high-bound
)
1914 (load-time-value (make-unportable-float
1915 :single-float-negative-zero
) t
))
1916 (eql high-bound
0f0
))
1917 (and (eql low-value
0f0
)
1919 (load-time-value (make-unportable-float
1920 :single-float-negative-zero
) t
)))
1922 (load-time-value (make-unportable-float
1923 :double-float-negative-zero
) t
))
1924 (eql high-bound
0d0
))
1925 (and (eql low-value
0d0
)
1927 (load-time-value (make-unportable-float
1928 :double-float-negative-zero
) t
))))))
1930 (let ((high-value (car high-bound
)))
1931 (or (eql high-value low-bound
)
1932 (and (eql high-value
1933 (load-time-value (make-unportable-float
1934 :single-float-negative-zero
) t
))
1935 (eql low-bound
0f0
))
1936 (and (eql high-value
0f0
)
1938 (load-time-value (make-unportable-float
1939 :single-float-negative-zero
) t
)))
1940 (and (eql high-value
1941 (load-time-value (make-unportable-float
1942 :double-float-negative-zero
) t
))
1943 (eql low-bound
0d0
))
1944 (and (eql high-value
0d0
)
1946 (load-time-value (make-unportable-float
1947 :double-float-negative-zero
) t
))))))
1948 ((and (eq (numeric-type-class low
) 'integer
)
1949 (eq (numeric-type-class high
) 'integer
))
1950 (eql (1+ low-bound
) high-bound
))
1954 ;;; Return a numeric type that is a supertype for both TYPE1 and TYPE2.
1956 ;;; Binding *APPROXIMATE-NUMERIC-UNIONS* to T allows merging non-adjacent
1957 ;;; numeric types, eg (OR (INTEGER 0 12) (INTEGER 20 128)) => (INTEGER 0 128),
1958 ;;; the compiler does this occasionally during type-derivation to avoid
1959 ;;; creating absurdly complex unions of numeric types.
1960 (defvar *approximate-numeric-unions
* nil
)
1962 (!define-type-method
(number :simple-union2
) (type1 type2
)
1963 (declare (type numeric-type type1 type2
))
1964 (cond ((csubtypep type1 type2
) type2
)
1965 ((csubtypep type2 type1
) type1
)
1967 (let ((class1 (numeric-type-class type1
))
1968 (format1 (numeric-type-format type1
))
1969 (complexp1 (numeric-type-complexp type1
))
1970 (class2 (numeric-type-class type2
))
1971 (format2 (numeric-type-format type2
))
1972 (complexp2 (numeric-type-complexp type2
)))
1974 ((and (eq class1 class2
)
1975 (eq format1 format2
)
1976 (eq complexp1 complexp2
)
1977 (or *approximate-numeric-unions
*
1978 (numeric-types-intersect type1 type2
)
1979 (numeric-types-adjacent type1 type2
)
1980 (numeric-types-adjacent type2 type1
)))
1985 :low
(numeric-bound-max (numeric-type-low type1
)
1986 (numeric-type-low type2
)
1988 :high
(numeric-bound-max (numeric-type-high type1
)
1989 (numeric-type-high type2
)
1991 ;; FIXME: These two clauses are almost identical, and the
1992 ;; consequents are in fact identical in every respect.
1993 ((and (eq class1
'rational
)
1994 (eq class2
'integer
)
1995 (eq format1 format2
)
1996 (eq complexp1 complexp2
)
1997 (integerp (numeric-type-low type2
))
1998 (integerp (numeric-type-high type2
))
1999 (= (numeric-type-low type2
) (numeric-type-high type2
))
2000 (or *approximate-numeric-unions
*
2001 (numeric-types-adjacent type1 type2
)
2002 (numeric-types-adjacent type2 type1
)))
2007 :low
(numeric-bound-max (numeric-type-low type1
)
2008 (numeric-type-low type2
)
2010 :high
(numeric-bound-max (numeric-type-high type1
)
2011 (numeric-type-high type2
)
2013 ((and (eq class1
'integer
)
2014 (eq class2
'rational
)
2015 (eq format1 format2
)
2016 (eq complexp1 complexp2
)
2017 (integerp (numeric-type-low type1
))
2018 (integerp (numeric-type-high type1
))
2019 (= (numeric-type-low type1
) (numeric-type-high type1
))
2020 (or *approximate-numeric-unions
*
2021 (numeric-types-adjacent type1 type2
)
2022 (numeric-types-adjacent type2 type1
)))
2027 :low
(numeric-bound-max (numeric-type-low type1
)
2028 (numeric-type-low type2
)
2030 :high
(numeric-bound-max (numeric-type-high type1
)
2031 (numeric-type-high type2
)
2036 (!cold-init-forms
;; is !PRECOMPUTE-TYPES not doing the right thing?
2037 (setf (info :type
:kind
'number
) :primitive
)
2038 (setf (info :type
:builtin
'number
)
2039 (make-numeric-type :complexp nil
)))
2041 (!def-type-translator complex
((:context context
) &optional
(typespec '*))
2042 (if (eq typespec
'*)
2043 (specifier-type '(complex real
))
2044 (labels ((not-numeric ()
2045 (error "The component type for COMPLEX is not numeric: ~S"
2048 (error "The component type for COMPLEX is not a subtype of REAL: ~S"
2050 (complex1 (component-type)
2051 (unless (numeric-type-p component-type
)
2053 (when (eq (numeric-type-complexp component-type
) :complex
)
2055 (if (csubtypep component-type
(specifier-type '(eql 0)))
2057 (modified-numeric-type component-type
2058 :complexp
:complex
)))
2061 ((eq ctype
*empty-type
*) *empty-type
*)
2062 ((eq ctype
*universal-type
*) (not-real))
2063 ((typep ctype
'numeric-type
) (complex1 ctype
))
2064 ((typep ctype
'union-type
)
2066 (mapcar #'do-complex
(union-type-types ctype
))))
2067 ((typep ctype
'member-type
)
2069 (mapcar-member-type-members
2070 (lambda (x) (do-complex (ctype-of x
)))
2072 ((and (typep ctype
'intersection-type
)
2073 ;; FIXME: This is very much a
2074 ;; not-quite-worst-effort, but we are required to do
2075 ;; something here because of our representation of
2076 ;; RATIO as (AND RATIONAL (NOT INTEGER)): we must
2077 ;; allow users to ask about (COMPLEX RATIO). This
2078 ;; will of course fail to work right on such types
2079 ;; as (AND INTEGER (SATISFIES ZEROP))...
2080 (let ((numbers (remove-if-not
2082 (intersection-type-types ctype
))))
2084 (null (cdr numbers
))
2085 (eq (numeric-type-complexp (car numbers
)) :real
)
2086 (complex1 (car numbers
))))))
2088 (multiple-value-bind (subtypep certainly
)
2089 (csubtypep ctype
(specifier-type 'real
))
2090 (if (and (not subtypep
) certainly
)
2092 ;; ANSI just says that TYPESPEC is any subtype of
2093 ;; type REAL, not necessarily a NUMERIC-TYPE. In
2094 ;; particular, at this point TYPESPEC could legally
2095 ;; be a hairy type like (AND NUMBER (SATISFIES
2096 ;; REALP) (SATISFIES ZEROP)), in which case we fall
2097 ;; through the logic above and end up here,
2099 ;; FIXME: (COMPLEX NUMBER) is not rejected but should
2100 ;; be, as NUMBER is clearly not a subtype of real.
2101 (bug "~@<(known bug #145): The type ~S is too hairy to be ~
2102 used for a COMPLEX component.~:@>"
2104 (let ((ctype (specifier-type-r context typespec
)))
2105 (do-complex ctype
)))))
2107 ;;; If X is *, return NIL, otherwise return the bound, which must be a
2108 ;;; member of TYPE or a one-element list of a member of TYPE.
2109 ;;; This is not necessarily the canonical bound. An integer bound
2110 ;;; should always be an atom, which we'll enforce later if needed.
2111 #!-sb-fluid
(declaim (inline valid-bound
))
2112 (defun valid-bound (bound type
)
2113 (cond ((eq bound
'*) nil
)
2114 ((sb!xc
:typep
(if (singleton-p bound
) (car bound
) bound
) type
) bound
)
2116 (error "Bound is not * or ~A ~S or list of one ~:*~S: ~S"
2117 (if (eq type
'integer
) "an" "a") type bound
))))
2119 (!def-type-translator integer
(&optional
(low '*) (high '*))
2120 (let ((lb (valid-bound low
'integer
))
2121 (hb (valid-bound high
'integer
)))
2122 (make-numeric-type :class
'integer
:complexp
:real
2123 :enumerable
(not (null (and lb hb
)))
2126 (defmacro !def-bounded-type
(type class format
)
2127 `(!def-type-translator
,type
(&optional
(low '*) (high '*))
2128 (let ((lb (valid-bound low
',type
))
2129 (hb (valid-bound high
',type
)))
2130 (make-numeric-type :class
',class
:format
',format
2131 :low lb
:high hb
))))
2133 (!def-bounded-type rational rational nil
)
2135 ;;; Unlike CMU CL, we represent the types FLOAT and REAL as
2136 ;;; UNION-TYPEs of more primitive types, in order to make
2137 ;;; type representation more unique, avoiding problems in the
2138 ;;; simplification of things like
2139 ;;; (subtypep '(or (single-float -1.0 1.0) (single-float 0.1))
2140 ;;; '(or (real -1 7) (single-float 0.1) (single-float -1.0 1.0)))
2141 ;;; When we allowed REAL to remain as a separate NUMERIC-TYPE,
2142 ;;; it was too easy for the first argument to be simplified to
2143 ;;; '(SINGLE-FLOAT -1.0), and for the second argument to be simplified
2144 ;;; to '(OR (REAL -1 7) (SINGLE-FLOAT 0.1)) and then for the
2145 ;;; SUBTYPEP to fail (returning NIL,T instead of T,T) because
2146 ;;; the first argument can't be seen to be a subtype of any of the
2147 ;;; terms in the second argument.
2149 ;;; The old CMU CL way was:
2150 ;;; (!def-bounded-type float float nil)
2151 ;;; (!def-bounded-type real nil nil)
2153 ;;; FIXME: If this new way works for a while with no weird new
2154 ;;; problems, we can go back and rip out support for separate FLOAT
2155 ;;; and REAL flavors of NUMERIC-TYPE. The new way was added in
2156 ;;; sbcl-0.6.11.22, 2001-03-21.
2158 ;;; FIXME: It's probably necessary to do something to fix the
2159 ;;; analogous problem with INTEGER and RATIONAL types. Perhaps
2160 ;;; bounded RATIONAL types should be represented as (OR RATIO INTEGER).
2161 (defun coerce-bound (bound type upperp inner-coerce-bound-fun
)
2162 (declare (type function inner-coerce-bound-fun
))
2165 (funcall inner-coerce-bound-fun bound type upperp
)))
2166 (defun inner-coerce-real-bound (bound type upperp
)
2167 #+sb-xc-host
(declare (ignore upperp
))
2168 (let #+sb-xc-host
()
2170 ((nl (load-time-value (symbol-value 'sb
!xc
:most-negative-long-float
) t
))
2171 (pl (load-time-value (symbol-value 'sb
!xc
:most-positive-long-float
) t
)))
2172 (let ((nbound (if (consp bound
) (car bound
) bound
))
2173 (consp (consp bound
)))
2177 (list (rational nbound
))
2181 ((floatp nbound
) bound
)
2183 ;; Coerce to the widest float format available, to avoid
2184 ;; unnecessary loss of precision, but don't coerce
2185 ;; unrepresentable numbers, except on the host where we
2186 ;; shouldn't be making these types (but KLUDGE: can't even
2187 ;; assert portably that we're not).
2191 (when (< nbound nl
) (return-from inner-coerce-real-bound nl
)))
2193 (when (> nbound pl
) (return-from inner-coerce-real-bound pl
))))
2194 (let ((result (coerce nbound
'long-float
)))
2195 (if consp
(list result
) result
)))))))))
2196 (defun inner-coerce-float-bound (bound type upperp
)
2197 #+sb-xc-host
(declare (ignore upperp
))
2198 (let #+sb-xc-host
()
2200 ((nd (load-time-value (symbol-value 'sb
!xc
:most-negative-double-float
) t
))
2201 (pd (load-time-value (symbol-value 'sb
!xc
:most-positive-double-float
) t
))
2202 (ns (load-time-value (symbol-value 'sb
!xc
:most-negative-single-float
) t
))
2203 (ps (load-time-value (symbol-value 'sb
!xc
:most-positive-single-float
) t
)))
2204 (let ((nbound (if (consp bound
) (car bound
) bound
))
2205 (consp (consp bound
)))
2209 ((typep nbound
'single-float
) bound
)
2214 (when (< nbound ns
) (return-from inner-coerce-float-bound ns
)))
2216 (when (> nbound ps
) (return-from inner-coerce-float-bound ps
))))
2217 (let ((result (coerce nbound
'single-float
)))
2218 (if consp
(list result
) result
)))))
2221 ((typep nbound
'double-float
) bound
)
2226 (when (< nbound nd
) (return-from inner-coerce-float-bound nd
)))
2228 (when (> nbound pd
) (return-from inner-coerce-float-bound pd
))))
2229 (let ((result (coerce nbound
'double-float
)))
2230 (if consp
(list result
) result
)))))))))
2231 (defun coerced-real-bound (bound type upperp
)
2232 (coerce-bound bound type upperp
#'inner-coerce-real-bound
))
2233 (defun coerced-float-bound (bound type upperp
)
2234 (coerce-bound bound type upperp
#'inner-coerce-float-bound
))
2235 (!def-type-translator real
(&optional
(low '*) (high '*))
2236 (specifier-type `(or (float ,(coerced-real-bound low
'float nil
)
2237 ,(coerced-real-bound high
'float t
))
2238 (rational ,(coerced-real-bound low
'rational nil
)
2239 ,(coerced-real-bound high
'rational t
)))))
2240 (!def-type-translator float
(&optional
(low '*) (high '*))
2242 `(or (single-float ,(coerced-float-bound low
'single-float nil
)
2243 ,(coerced-float-bound high
'single-float t
))
2244 (double-float ,(coerced-float-bound low
'double-float nil
)
2245 ,(coerced-float-bound high
'double-float t
))
2246 #!+long-float
,(error "stub: no long float support yet"))))
2248 (macrolet ((define-float-format (f) `(!def-bounded-type
,f float
,f
)))
2249 (define-float-format single-float
)
2250 (define-float-format double-float
))
2252 (defun numeric-types-intersect (type1 type2
)
2253 (declare (type numeric-type type1 type2
))
2254 (let* ((class1 (numeric-type-class type1
))
2255 (class2 (numeric-type-class type2
))
2256 (complexp1 (numeric-type-complexp type1
))
2257 (complexp2 (numeric-type-complexp type2
))
2258 (format1 (numeric-type-format type1
))
2259 (format2 (numeric-type-format type2
))
2260 (low1 (numeric-type-low type1
))
2261 (high1 (numeric-type-high type1
))
2262 (low2 (numeric-type-low type2
))
2263 (high2 (numeric-type-high type2
)))
2264 ;; If one is complex and the other isn't, then they are disjoint.
2265 (cond ((not (or (eq complexp1 complexp2
)
2266 (null complexp1
) (null complexp2
)))
2268 ;; If either type is a float, then the other must either be
2269 ;; specified to be a float or unspecified. Otherwise, they
2271 ((and (eq class1
'float
)
2272 (not (member class2
'(float nil
)))) nil
)
2273 ((and (eq class2
'float
)
2274 (not (member class1
'(float nil
)))) nil
)
2275 ;; If the float formats are specified and different, the
2276 ;; types are disjoint.
2277 ((not (or (eq format1 format2
) (null format1
) (null format2
)))
2280 ;; Check the bounds. This is a bit odd because we must
2281 ;; always have the outer bound of the interval as the
2283 (if (numeric-bound-test high1 high2
<= <)
2284 (or (and (numeric-bound-test low1 low2
>= >)
2285 (numeric-bound-test* low1 high2
<= <))
2286 (and (numeric-bound-test low2 low1
>= >)
2287 (numeric-bound-test* low2 high1
<= <)))
2288 (or (and (numeric-bound-test* low2 high1
<= <)
2289 (numeric-bound-test low2 low1
>= >))
2290 (and (numeric-bound-test high2 high1
<= <)
2291 (numeric-bound-test* high2 low1
>= >))))))))
2293 ;;; Take the numeric bound X and convert it into something that can be
2294 ;;; used as a bound in a numeric type with the specified CLASS and
2295 ;;; FORMAT. If UP-P is true, then we round up as needed, otherwise we
2296 ;;; round down. UP-P true implies that X is a lower bound, i.e. (N) > N.
2298 ;;; This is used by NUMERIC-TYPE-INTERSECTION to mash the bound into
2299 ;;; the appropriate type number. X may only be a float when CLASS is
2302 ;;; ### Note: it is possible for the coercion to a float to overflow
2303 ;;; or underflow. This happens when the bound doesn't fit in the
2304 ;;; specified format. In this case, we should really return the
2305 ;;; appropriate {Most | Least}-{Positive | Negative}-XXX-Float float
2306 ;;; of desired format. But these conditions aren't currently signalled
2307 ;;; in any useful way.
2309 ;;; Also, when converting an open rational bound into a float we
2310 ;;; should probably convert it to a closed bound of the closest float
2311 ;;; in the specified format. KLUDGE: In general, open float bounds are
2312 ;;; screwed up. -- (comment from original CMU CL)
2313 (defun round-numeric-bound (x class format up-p
)
2315 (let ((cx (if (consp x
) (car x
) x
)))
2319 (if (and (consp x
) (integerp cx
))
2320 (if up-p
(1+ cx
) (1- cx
))
2321 (if up-p
(ceiling cx
) (floor cx
))))
2325 ((and format
(subtypep format
'double-float
))
2326 (if (<= most-negative-double-float cx most-positive-double-float
)
2330 (if (<= most-negative-single-float cx most-positive-single-float
)
2332 (coerce cx
(or format
'single-float
))
2334 (if (consp x
) (list res
) res
)))))
2337 ;;; Handle the case of type intersection on two numeric types. We use
2338 ;;; TYPES-EQUAL-OR-INTERSECT to throw out the case of types with no
2339 ;;; intersection. If an attribute in TYPE1 is unspecified, then we use
2340 ;;; TYPE2's attribute, which must be at least as restrictive. If the
2341 ;;; types intersect, then the only attributes that can be specified
2342 ;;; and different are the class and the bounds.
2344 ;;; When the class differs, we use the more restrictive class. The
2345 ;;; only interesting case is RATIONAL/INTEGER, since RATIONAL includes
2348 ;;; We make the result lower (upper) bound the maximum (minimum) of
2349 ;;; the argument lower (upper) bounds. We convert the bounds into the
2350 ;;; appropriate numeric type before maximizing. This avoids possible
2351 ;;; confusion due to mixed-type comparisons (but I think the result is
2353 (!define-type-method
(number :simple-intersection2
) (type1 type2
)
2354 (declare (type numeric-type type1 type2
))
2355 (if (numeric-types-intersect type1 type2
)
2356 (let* ((class1 (numeric-type-class type1
))
2357 (class2 (numeric-type-class type2
))
2358 (class (ecase class1
2360 ((integer float
) class1
)
2361 (rational (if (eq class2
'integer
)
2364 (format (or (numeric-type-format type1
)
2365 (numeric-type-format type2
))))
2369 :complexp
(or (numeric-type-complexp type1
)
2370 (numeric-type-complexp type2
))
2371 :low
(numeric-bound-max
2372 (round-numeric-bound (numeric-type-low type1
)
2374 (round-numeric-bound (numeric-type-low type2
)
2377 :high
(numeric-bound-max
2378 (round-numeric-bound (numeric-type-high type1
)
2380 (round-numeric-bound (numeric-type-high type2
)
2385 ;;; Given two float formats, return the one with more precision. If
2386 ;;; either one is null, return NIL.
2387 (defun float-format-max (f1 f2
)
2389 (dolist (f *float-formats
* (error "bad float format: ~S" f1
))
2390 (when (or (eq f f1
) (eq f f2
))
2393 ;;; Return the result of an operation on TYPE1 and TYPE2 according to
2394 ;;; the rules of numeric contagion. This is always NUMBER, some float
2395 ;;; format (possibly complex) or RATIONAL. Due to rational
2396 ;;; canonicalization, there isn't much we can do here with integers or
2397 ;;; rational complex numbers.
2399 ;;; If either argument is not a NUMERIC-TYPE, then return NUMBER. This
2400 ;;; is useful mainly for allowing types that are technically numbers,
2401 ;;; but not a NUMERIC-TYPE.
2402 (defun numeric-contagion (type1 type2
)
2403 (if (and (numeric-type-p type1
) (numeric-type-p type2
))
2404 (let ((class1 (numeric-type-class type1
))
2405 (class2 (numeric-type-class type2
))
2406 (format1 (numeric-type-format type1
))
2407 (format2 (numeric-type-format type2
))
2408 (complexp1 (numeric-type-complexp type1
))
2409 (complexp2 (numeric-type-complexp type2
)))
2410 (cond ((or (null complexp1
)
2412 (specifier-type 'number
))
2416 :format
(ecase class2
2417 (float (float-format-max format1 format2
))
2418 ((integer rational
) format1
)
2420 ;; A double-float with any real number is a
2423 (if (eq format1
'double-float
)
2426 ;; A long-float with any real number is a
2429 (if (eq format1
'long-float
)
2432 :complexp
(if (or (eq complexp1
:complex
)
2433 (eq complexp2
:complex
))
2436 ((eq class2
'float
) (numeric-contagion type2 type1
))
2437 ((and (eq complexp1
:real
) (eq complexp2
:real
))
2439 :class
(and class1 class2
'rational
)
2442 (specifier-type 'number
))))
2443 (specifier-type 'number
)))
2447 (!define-type-method
(array :simple-
=) (type1 type2
)
2448 (cond ((not (and (equal (array-type-dimensions type1
)
2449 (array-type-dimensions type2
))
2450 (eq (array-type-complexp type1
)
2451 (array-type-complexp type2
))))
2453 ((or (unknown-type-p (array-type-element-type type1
))
2454 (unknown-type-p (array-type-element-type type2
)))
2455 (type= (array-type-element-type type1
)
2456 (array-type-element-type type2
)))
2458 (values (type= (array-type-specialized-element-type type1
)
2459 (array-type-specialized-element-type type2
))
2462 (!define-type-method
(array :negate
) (type)
2463 ;; FIXME (and hint to PFD): we're vulnerable here to attacks of the
2464 ;; form "are (AND ARRAY (NOT (ARRAY T))) and (OR (ARRAY BIT) (ARRAY
2465 ;; NIL) (ARRAY CHAR) ...) equivalent?" -- CSR, 2003-12-10
2466 ;; A symptom of the aforementioned is that the following are not TYPE=
2467 ;; (AND (VECTOR T) (NOT SIMPLE-ARRAY)) ; an ARRAY-TYPE
2468 ;; (AND (VECTOR T) (NOT SIMPLE-VECTOR)) ; an INTERSECTION-TYPE
2469 ;; even though (VECTOR T) makes it so that the (NOT) clause in each can
2470 ;; only provide one additional bit of information: that the vector
2471 ;; is complex as opposed to simple. The rank and element-type are fixed.
2472 (if (and (eq (array-type-dimensions type
) '*)
2473 (eq (array-type-complexp type
) 't
)
2474 (eq (array-type-element-type type
) *wild-type
*))
2475 ;; (NOT <hairy-array>) = either SIMPLE-ARRAY or (NOT ARRAY).
2476 ;; This is deliberately asymmetric - trying to say that NOT simple-array
2477 ;; equals hairy-array leads to infinite recursion.
2478 (type-union (make-array-type '* :complexp nil
2479 :element-type
*wild-type
*)
2481 (make-array-type '* :element-type
*wild-type
*)))
2482 (make-negation-type type
)))
2484 (!define-type-method
(array :unparse
) (type)
2485 (let* ((dims (array-type-dimensions type
))
2486 ;; Compare the specialised element type and the
2487 ;; derived element type. If the derived type
2488 ;; is so small that it jumps to a smaller upgraded
2489 ;; element type, use the specialised element type.
2491 ;; This protects from unparsing
2492 ;; (and (vector (or bit symbol))
2493 ;; (vector (or bit character)))
2494 ;; i.e., the intersection of two T array types,
2496 (stype (array-type-specialized-element-type type
))
2497 (dtype (array-type-element-type type
))
2498 (utype (%upgraded-array-element-type dtype
))
2499 (eltype (type-specifier (if (type= stype utype
)
2502 (complexp (array-type-complexp type
)))
2503 (if (and (eq complexp t
) (not *unparse-allow-negation
*))
2504 (setq complexp
:maybe
))
2508 ((t) '(and array
(not simple-array
)))
2510 ((nil) 'simple-array
))
2512 ((t) `(and (array ,eltype
) (not simple-array
)))
2513 ((:maybe
) `(array ,eltype
))
2514 ((nil) `(simple-array ,eltype
)))))
2515 ((= (length dims
) 1)
2518 (if (eq (car dims
) '*)
2521 ((base-char #!-sb-unicode character
) 'base-string
)
2523 (t `(vector ,eltype
)))
2525 (bit `(bit-vector ,(car dims
)))
2526 ((base-char #!-sb-unicode character
)
2527 `(base-string ,(car dims
)))
2528 (t `(vector ,eltype
,(car dims
)))))))
2529 (if (eql complexp
:maybe
)
2531 `(and ,answer
(not simple-array
))))
2532 (if (eq (car dims
) '*)
2534 (bit 'simple-bit-vector
)
2535 ((base-char #!-sb-unicode character
) 'simple-base-string
)
2536 ((t) 'simple-vector
)
2537 (t `(simple-array ,eltype
(*))))
2539 (bit `(simple-bit-vector ,(car dims
)))
2540 ((base-char #!-sb-unicode character
)
2541 `(simple-base-string ,(car dims
)))
2542 ((t) `(simple-vector ,(car dims
)))
2543 (t `(simple-array ,eltype
,dims
))))))
2546 ((t) `(and (array ,eltype
,dims
) (not simple-array
)))
2547 ((:maybe
) `(array ,eltype
,dims
))
2548 ((nil) `(simple-array ,eltype
,dims
)))))))
2550 (!define-type-method
(array :simple-subtypep
) (type1 type2
)
2551 (let ((dims1 (array-type-dimensions type1
))
2552 (dims2 (array-type-dimensions type2
))
2553 (complexp2 (array-type-complexp type2
)))
2554 (cond (;; not subtypep unless dimensions are compatible
2555 (not (or (eq dims2
'*)
2556 (and (not (eq dims1
'*))
2557 ;; (sbcl-0.6.4 has trouble figuring out that
2558 ;; DIMS1 and DIMS2 must be lists at this
2559 ;; point, and knowing that is important to
2560 ;; compiling EVERY efficiently.)
2561 (= (length (the list dims1
))
2562 (length (the list dims2
)))
2563 (every (lambda (x y
)
2564 (or (eq y
'*) (eql x y
)))
2566 (the list dims2
)))))
2568 ;; not subtypep unless complexness is compatible
2569 ((not (or (eq complexp2
:maybe
)
2570 (eq (array-type-complexp type1
) complexp2
)))
2572 ;; Since we didn't fail any of the tests above, we win
2573 ;; if the TYPE2 element type is wild.
2574 ((eq (array-type-element-type type2
) *wild-type
*)
2576 (;; Since we didn't match any of the special cases above, if
2577 ;; either element type is unknown we can only give a good
2578 ;; answer if they are the same.
2579 (or (unknown-type-p (array-type-element-type type1
))
2580 (unknown-type-p (array-type-element-type type2
)))
2581 (if (type= (array-type-element-type type1
)
2582 (array-type-element-type type2
))
2585 (;; Otherwise, the subtype relationship holds iff the
2586 ;; types are equal, and they're equal iff the specialized
2587 ;; element types are identical.
2589 (values (type= (array-type-specialized-element-type type1
)
2590 (array-type-specialized-element-type type2
))
2593 (!define-superclasses array
((vector vector
) (array)) !cold-init-forms
)
2595 (defun array-types-intersect (type1 type2
)
2596 (declare (type array-type type1 type2
))
2597 (let ((dims1 (array-type-dimensions type1
))
2598 (dims2 (array-type-dimensions type2
))
2599 (complexp1 (array-type-complexp type1
))
2600 (complexp2 (array-type-complexp type2
)))
2601 ;; See whether dimensions are compatible.
2602 (cond ((not (or (eq dims1
'*) (eq dims2
'*)
2603 (and (= (length dims1
) (length dims2
))
2604 (every (lambda (x y
)
2605 (or (eq x
'*) (eq y
'*) (= x y
)))
2608 ;; See whether complexpness is compatible.
2609 ((not (or (eq complexp1
:maybe
)
2610 (eq complexp2
:maybe
)
2611 (eq complexp1 complexp2
)))
2615 ;; If either element type is wild, then they intersect.
2616 ;; Otherwise, the types must be identical.
2618 ;; FIXME: There seems to have been a fair amount of
2619 ;; confusion about the distinction between requested element
2620 ;; type and specialized element type; here is one of
2621 ;; them. If we request an array to hold objects of an
2622 ;; unknown type, we can do no better than represent that
2623 ;; type as an array specialized on wild-type. We keep the
2624 ;; requested element-type in the -ELEMENT-TYPE slot, and
2625 ;; *WILD-TYPE* in the -SPECIALIZED-ELEMENT-TYPE. So, here,
2626 ;; we must test for the SPECIALIZED slot being *WILD-TYPE*,
2627 ;; not just the ELEMENT-TYPE slot. Maybe the return value
2628 ;; in that specific case should be T, NIL? Or maybe this
2629 ;; function should really be called
2630 ;; ARRAY-TYPES-COULD-POSSIBLY-INTERSECT? In any case, this
2631 ;; was responsible for bug #123, and this whole issue could
2632 ;; do with a rethink and/or a rewrite. -- CSR, 2002-08-21
2633 ((or (eq (array-type-specialized-element-type type1
) *wild-type
*)
2634 (eq (array-type-specialized-element-type type2
) *wild-type
*)
2635 (type= (array-type-specialized-element-type type1
)
2636 (array-type-specialized-element-type type2
)))
2642 (defun unite-array-types-complexp (type1 type2
)
2643 (let ((complexp1 (array-type-complexp type1
))
2644 (complexp2 (array-type-complexp type2
)))
2646 ((eq complexp1 complexp2
)
2647 ;; both types are the same complexp-ity
2648 (values complexp1 t
))
2649 ((eq complexp1
:maybe
)
2650 ;; type1 is wild-complexp
2651 (values :maybe type1
))
2652 ((eq complexp2
:maybe
)
2653 ;; type2 is wild-complexp
2654 (values :maybe type2
))
2656 ;; both types partition the complexp-space
2657 (values :maybe nil
)))))
2659 (defun unite-array-types-dimensions (type1 type2
)
2660 (let ((dims1 (array-type-dimensions type1
))
2661 (dims2 (array-type-dimensions type2
)))
2662 (cond ((equal dims1 dims2
)
2663 ;; both types are same dimensionality
2666 ;; type1 is wild-dimensions
2669 ;; type2 is wild-dimensions
2671 ((not (= (length dims1
) (length dims2
)))
2672 ;; types have different number of dimensions
2673 (values :incompatible nil
))
2675 ;; we need to check on a per-dimension basis
2676 (let* ((supertype1 t
)
2679 (result (mapcar (lambda (dim1 dim2
)
2684 (setf supertype2 nil
)
2687 (setf supertype1 nil
)
2690 (setf compatible nil
))))
2693 ((or (not compatible
)
2694 (and (not supertype1
)
2696 (values :incompatible nil
))
2697 ((and supertype1 supertype2
)
2698 (values result supertype1
))
2700 (values result
(if supertype1 type1 type2
)))))))))
2702 (defun unite-array-types-element-types (type1 type2
)
2703 ;; FIXME: We'd love to be able to unite the full set of specialized
2704 ;; array element types up to *wild-type*, but :simple-union2 is
2705 ;; performed pairwise, so we don't have a good hook for it and our
2706 ;; representation doesn't allow us to easily detect the situation
2708 ;; But see SIMPLIFY-ARRAY-UNIONS which is able to do something like that.
2709 (let* ((eltype1 (array-type-element-type type1
))
2710 (eltype2 (array-type-element-type type2
))
2711 (stype1 (array-type-specialized-element-type type1
))
2712 (stype2 (array-type-specialized-element-type type2
))
2713 (wild1 (eq eltype1
*wild-type
*))
2714 (wild2 (eq eltype2
*wild-type
*)))
2716 ((type= eltype1 eltype2
)
2717 (values eltype1 stype1 t
))
2719 (values eltype1 stype1 type1
))
2721 (values eltype2 stype2 type2
))
2722 ((not (type= stype1 stype2
))
2723 ;; non-wild types that don't share UAET don't unite
2724 (values :incompatible nil nil
))
2725 ((csubtypep eltype1 eltype2
)
2726 (values eltype2 stype2 type2
))
2727 ((csubtypep eltype2 eltype1
)
2728 (values eltype1 stype1 type1
))
2730 (values :incompatible nil nil
)))))
2732 (defun unite-array-types-supertypes-compatible-p (&rest supertypes
)
2733 ;; supertypes are compatible if they are all T, if there is a single
2734 ;; NIL and all the rest are T, or if all non-T supertypes are the
2735 ;; same and not NIL.
2736 (let ((interesting-supertypes
2737 (remove t supertypes
)))
2738 (or (not interesting-supertypes
)
2739 (equal interesting-supertypes
'(nil))
2740 ;; supertypes are (OR BOOLEAN ARRAY-TYPE), so...
2741 (typep (remove-duplicates interesting-supertypes
)
2742 '(cons array-type null
)))))
2744 (!define-type-method
(array :simple-union2
) (type1 type2
)
2745 (multiple-value-bind
2746 (result-eltype result-stype eltype-supertype
)
2747 (unite-array-types-element-types type1 type2
)
2748 (multiple-value-bind
2749 (result-complexp complexp-supertype
)
2750 (unite-array-types-complexp type1 type2
)
2751 (multiple-value-bind
2752 (result-dimensions dimensions-supertype
)
2753 (unite-array-types-dimensions type1 type2
)
2754 (when (and (not (eq result-dimensions
:incompatible
))
2755 (not (eq result-eltype
:incompatible
))
2756 (unite-array-types-supertypes-compatible-p
2757 eltype-supertype complexp-supertype dimensions-supertype
))
2758 (make-array-type result-dimensions
2759 :complexp result-complexp
2760 :element-type result-eltype
2761 :specialized-element-type result-stype
))))))
2763 (!define-type-method
(array :simple-intersection2
) (type1 type2
)
2764 (declare (type array-type type1 type2
))
2765 (if (array-types-intersect type1 type2
)
2766 (let ((dims1 (array-type-dimensions type1
))
2767 (dims2 (array-type-dimensions type2
))
2768 (complexp1 (array-type-complexp type1
))
2769 (complexp2 (array-type-complexp type2
))
2770 (eltype1 (array-type-element-type type1
))
2771 (eltype2 (array-type-element-type type2
))
2772 (stype1 (array-type-specialized-element-type type1
))
2773 (stype2 (array-type-specialized-element-type type2
)))
2774 (make-array-type (cond ((eq dims1
'*) dims2
)
2775 ((eq dims2
'*) dims1
)
2777 (mapcar (lambda (x y
) (if (eq x
'*) y x
))
2779 :complexp
(if (eq complexp1
:maybe
) complexp2 complexp1
)
2781 ((eq eltype1
*wild-type
*) eltype2
)
2782 ((eq eltype2
*wild-type
*) eltype1
)
2783 (t (type-intersection eltype1 eltype2
)))
2784 :specialized-element-type
(cond
2785 ((eq stype1
*wild-type
*) stype2
)
2786 ((eq stype2
*wild-type
*) stype1
)
2788 (aver (type= stype1 stype2
))
2792 ;;; Check a supplied dimension list to determine whether it is legal,
2793 ;;; and return it in canonical form (as either '* or a list).
2794 (defun canonical-array-dimensions (dims)
2799 (error "Arrays can't have a negative number of dimensions: ~S" dims
))
2800 (when (>= dims sb
!xc
:array-rank-limit
)
2801 (error "array type with too many dimensions: ~S" dims
))
2802 (make-list dims
:initial-element
'*))
2804 (when (>= (length dims
) sb
!xc
:array-rank-limit
)
2805 (error "array type with too many dimensions: ~S" dims
))
2808 (unless (and (integerp dim
)
2810 (< dim sb
!xc
:array-dimension-limit
))
2811 (error "bad dimension in array type: ~S" dim
))))
2814 (error "Array dimensions is not a list, integer or *:~% ~S" dims
))))
2818 (!define-type-method
(member :negate
) (type)
2819 (let ((xset (member-type-xset type
))
2820 (fp-zeroes (member-type-fp-zeroes type
)))
2822 ;; Hairy case, which needs to do a bit of float type
2823 ;; canonicalization.
2824 (apply #'type-intersection
2825 (if (xset-empty-p xset
)
2827 (make-negation-type (make-member-type xset nil
)))
2830 (let* ((opposite (neg-fp-zero x
))
2831 (type (ctype-of opposite
)))
2834 (modified-numeric-type type
:low nil
:high nil
))
2835 (modified-numeric-type type
:low nil
:high
(list opposite
))
2836 (make-eql-type opposite
)
2837 (modified-numeric-type type
:low
(list opposite
) :high nil
))))
2840 (make-negation-type type
))))
2842 (!define-type-method
(member :unparse
) (type)
2843 (cond ((eq type
(specifier-type 'null
)) 'null
) ; NULL type is EQ-comparable
2844 ((eq type
(specifier-type 'boolean
)) 'boolean
) ; so is BOOLEAN
2845 (t `(member ,@(member-type-members type
)))))
2847 (!define-type-method
(member :singleton-p
) (type)
2848 (if (eql 1 (member-type-size type
))
2849 (values t
(first (member-type-members type
)))
2852 (!define-type-method
(member :simple-subtypep
) (type1 type2
)
2853 (values (and (xset-subset-p (member-type-xset type1
)
2854 (member-type-xset type2
))
2855 (subsetp (member-type-fp-zeroes type1
)
2856 (member-type-fp-zeroes type2
)))
2859 (!define-type-method
(member :complex-subtypep-arg1
) (type1 type2
)
2861 (mapc-member-type-members
2863 (multiple-value-bind (ok surep
) (ctypep elt type2
)
2865 (return-from punt
(values nil nil
)))
2867 (return-from punt
(values nil t
)))))
2871 ;;; We punt if the odd type is enumerable and intersects with the
2872 ;;; MEMBER type. If not enumerable, then it is definitely not a
2873 ;;; subtype of the MEMBER type.
2874 (!define-type-method
(member :complex-subtypep-arg2
) (type1 type2
)
2875 (cond ((not (type-enumerable type1
)) (values nil t
))
2876 ((types-equal-or-intersect type1 type2
)
2877 (invoke-complex-subtypep-arg1-method type1 type2
))
2878 (t (values nil t
))))
2880 (!define-type-method
(member :simple-intersection2
) (type1 type2
)
2881 (make-member-type (xset-intersection (member-type-xset type1
)
2882 (member-type-xset type2
))
2883 (intersection (member-type-fp-zeroes type1
)
2884 (member-type-fp-zeroes type2
))))
2886 (!define-type-method
(member :complex-intersection2
) (type1 type2
)
2888 (let ((xset (alloc-xset))
2890 (mapc-member-type-members
2892 (multiple-value-bind (ok sure
) (ctypep member type1
)
2894 (return-from punt nil
))
2896 (if (fp-zero-p member
)
2897 (pushnew member fp-zeroes
)
2898 (add-to-xset member xset
)))))
2900 (if (and (xset-empty-p xset
) (not fp-zeroes
))
2902 (make-member-type xset fp-zeroes
)))))
2904 ;;; We don't need a :COMPLEX-UNION2, since the only interesting case is
2905 ;;; a union type, and the member/union interaction is handled by the
2906 ;;; union type method.
2907 (!define-type-method
(member :simple-union2
) (type1 type2
)
2908 (make-member-type (xset-union (member-type-xset type1
)
2909 (member-type-xset type2
))
2910 (union (member-type-fp-zeroes type1
)
2911 (member-type-fp-zeroes type2
))))
2913 (!define-type-method
(member :simple-
=) (type1 type2
)
2914 (let ((xset1 (member-type-xset type1
))
2915 (xset2 (member-type-xset type2
))
2916 (l1 (member-type-fp-zeroes type1
))
2917 (l2 (member-type-fp-zeroes type2
)))
2918 (values (and (eql (xset-count xset1
) (xset-count xset2
))
2919 (xset-subset-p xset1 xset2
)
2920 (xset-subset-p xset2 xset1
)
2925 (!define-type-method
(member :complex-
=) (type1 type2
)
2926 (if (type-enumerable type1
)
2927 (multiple-value-bind (val win
) (csubtypep type2 type1
)
2928 (if (or val
(not win
))
2933 (!def-type-translator member
:list
(&rest members
)
2935 (let (ms numbers char-codes
)
2936 (dolist (m (remove-duplicates members
))
2938 (character (push (sb!xc
:char-code m
) char-codes
))
2939 (real (if (and (floatp m
) (zerop m
))
2941 (push (ctype-of m
) numbers
)))
2944 (member-type-from-list ms
)
2945 (make-character-set-type (mapcar (lambda (x) (cons x x
))
2946 (sort char-codes
#'<)))
2947 (nreverse numbers
)))
2950 ;;;; intersection types
2952 ;;;; Until version 0.6.10.6, SBCL followed the original CMU CL approach
2953 ;;;; of punting on all AND types, not just the unreasonably complicated
2954 ;;;; ones. The change was motivated by trying to get the KEYWORD type
2955 ;;;; to behave sensibly:
2956 ;;;; ;; reasonable definition
2957 ;;;; (DEFTYPE KEYWORD () '(AND SYMBOL (SATISFIES KEYWORDP)))
2958 ;;;; ;; reasonable behavior
2959 ;;;; (AVER (SUBTYPEP 'KEYWORD 'SYMBOL))
2960 ;;;; Without understanding a little about the semantics of AND, we'd
2961 ;;;; get (SUBTYPEP 'KEYWORD 'SYMBOL)=>NIL,NIL and, for entirely
2962 ;;;; parallel reasons, (SUBTYPEP 'RATIO 'NUMBER)=>NIL,NIL. That's
2965 ;;;; We still follow the example of CMU CL to some extent, by punting
2966 ;;;; (to the opaque HAIRY-TYPE) on sufficiently complicated types
2969 (!define-type-class intersection
2970 :enumerable
#'compound-type-enumerable
2971 :might-contain-other-types t
)
2973 (!define-type-method
(intersection :negate
) (type)
2975 (mapcar #'type-negation
(intersection-type-types type
))))
2977 ;;; A few intersection types have special names. The others just get
2978 ;;; mechanically unparsed.
2979 (!define-type-method
(intersection :unparse
) (type)
2980 (declare (type ctype type
))
2981 (or (find type
'(ratio keyword compiled-function
) :key
#'specifier-type
:test
#'type
=)
2982 `(and ,@(mapcar #'type-specifier
(intersection-type-types type
)))))
2984 ;;; shared machinery for type equality: true if every type in the set
2985 ;;; TYPES1 matches a type in the set TYPES2 and vice versa
2986 (defun type=-set
(types1 types2
)
2987 (flet ((type<=-set
(x y
)
2988 (declare (type list x y
))
2989 (every/type
(lambda (x y-element
)
2990 (any/type
#'type
= y-element x
))
2992 (and/type
(type<=-set types1 types2
)
2993 (type<=-set types2 types1
))))
2995 ;;; Two intersection types are equal if their subtypes are equal sets.
2997 ;;; FIXME: Might it be better to use
2998 ;;; (AND (SUBTYPEP X Y) (SUBTYPEP Y X))
2999 ;;; instead, since SUBTYPEP is the usual relationship that we care
3000 ;;; most about, so it would be good to leverage any ingenuity there
3001 ;;; in this more obscure method?
3002 (!define-type-method
(intersection :simple-
=) (type1 type2
)
3003 (type=-set
(intersection-type-types type1
)
3004 (intersection-type-types type2
)))
3006 (defun %intersection-complex-subtypep-arg1
(type1 type2
)
3007 (type= type1
(type-intersection type1 type2
)))
3009 (defun %intersection-simple-subtypep
(type1 type2
)
3010 (every/type
#'%intersection-complex-subtypep-arg1
3012 (intersection-type-types type2
)))
3014 (!define-type-method
(intersection :simple-subtypep
) (type1 type2
)
3015 (%intersection-simple-subtypep type1 type2
))
3017 (!define-type-method
(intersection :complex-subtypep-arg1
) (type1 type2
)
3018 (%intersection-complex-subtypep-arg1 type1 type2
))
3020 (defun %intersection-complex-subtypep-arg2
(type1 type2
)
3021 (every/type
#'csubtypep type1
(intersection-type-types type2
)))
3023 (!define-type-method
(intersection :complex-subtypep-arg2
) (type1 type2
)
3024 (%intersection-complex-subtypep-arg2 type1 type2
))
3026 ;;; FIXME: This will look eeriely familiar to readers of the UNION
3027 ;;; :SIMPLE-INTERSECTION2 :COMPLEX-INTERSECTION2 method. That's
3028 ;;; because it was generated by cut'n'paste methods. Given that
3029 ;;; intersections and unions have all sorts of symmetries known to
3030 ;;; mathematics, it shouldn't be beyond the ken of some programmers to
3031 ;;; reflect those symmetries in code in a way that ties them together
3032 ;;; more strongly than having two independent near-copies :-/
3033 (!define-type-method
(intersection :simple-union2
:complex-union2
)
3035 ;; Within this method, type2 is guaranteed to be an intersection
3037 (aver (intersection-type-p type2
))
3038 ;; Make sure to call only the applicable methods...
3039 (cond ((and (intersection-type-p type1
)
3040 (%intersection-simple-subtypep type1 type2
)) type2
)
3041 ((and (intersection-type-p type1
)
3042 (%intersection-simple-subtypep type2 type1
)) type1
)
3043 ((and (not (intersection-type-p type1
))
3044 (%intersection-complex-subtypep-arg2 type1 type2
))
3046 ((and (not (intersection-type-p type1
))
3047 (%intersection-complex-subtypep-arg1 type2 type1
))
3049 ;; KLUDGE: This special (and somewhat hairy) magic is required
3050 ;; to deal with the RATIONAL/INTEGER special case. The UNION
3051 ;; of (INTEGER * -1) and (AND (RATIONAL * -1/2) (NOT INTEGER))
3052 ;; should be (RATIONAL * -1/2) -- CSR, 2003-02-28
3053 ((and (csubtypep type2
(specifier-type 'ratio
))
3054 (numeric-type-p type1
)
3055 (csubtypep type1
(specifier-type 'integer
))
3060 :low
(if (null (numeric-type-low type1
))
3062 (list (1- (numeric-type-low type1
))))
3063 :high
(if (null (numeric-type-high type1
))
3065 (list (1+ (numeric-type-high type1
)))))))
3066 (let* ((intersected (intersection-type-types type2
))
3067 (remaining (remove (specifier-type '(not integer
))
3070 (and (not (equal intersected remaining
))
3071 (type-union type1
(apply #'type-intersection remaining
)))))
3073 (let ((accumulator *universal-type
*))
3074 (do ((t2s (intersection-type-types type2
) (cdr t2s
)))
3075 ((null t2s
) accumulator
)
3076 (let ((union (type-union type1
(car t2s
))))
3077 (when (union-type-p union
)
3078 ;; we have to give up here -- there are all sorts of
3079 ;; ordering worries, but it's better than before.
3080 ;; Doing exactly the same as in the UNION
3081 ;; :SIMPLE/:COMPLEX-INTERSECTION2 method causes stack
3082 ;; overflow with the mutual recursion never bottoming
3084 (if (and (eq accumulator
*universal-type
*)
3086 ;; KLUDGE: if we get here, we have a partially
3087 ;; simplified result. While this isn't by any
3088 ;; means a universal simplification, including
3089 ;; this logic here means that we can get (OR
3090 ;; KEYWORD (NOT KEYWORD)) canonicalized to T.
3094 (type-intersection accumulator union
))))))))
3096 (!def-type-translator and
:list
((:context context
) &rest type-specifiers
)
3097 (apply #'type-intersection
3098 (mapcar (lambda (x) (specifier-type-r context x
))
3103 (!define-type-class union
3104 :enumerable
#'compound-type-enumerable
3105 :might-contain-other-types t
)
3107 (!define-type-method
(union :negate
) (type)
3108 (declare (type ctype type
))
3109 (apply #'type-intersection
3110 (mapcar #'type-negation
(union-type-types type
))))
3112 ;;; The LIST, FLOAT and REAL types have special names. Other union
3113 ;;; types just get mechanically unparsed.
3114 (!define-type-method
(union :unparse
) (type)
3115 (declare (type ctype type
))
3117 ((type= type
(specifier-type 'list
)) 'list
)
3118 ((type= type
(specifier-type 'float
)) 'float
)
3119 ((type= type
(specifier-type 'real
)) 'real
)
3120 ((type= type
(specifier-type 'sequence
)) 'sequence
)
3121 ((type= type
(specifier-type 'bignum
)) 'bignum
)
3122 ((type= type
(specifier-type 'simple-string
)) 'simple-string
)
3123 ((type= type
(specifier-type 'string
)) 'string
)
3124 ((type= type
(specifier-type 'complex
)) 'complex
)
3125 (t `(or ,@(mapcar #'type-specifier
(union-type-types type
))))))
3127 ;;; Two union types are equal if they are each subtypes of each
3128 ;;; other. We need to be this clever because our complex subtypep
3129 ;;; methods are now more accurate; we don't get infinite recursion
3130 ;;; because the simple-subtypep method delegates to complex-subtypep
3131 ;;; of the individual types of type1. - CSR, 2002-04-09
3133 ;;; Previous comment, now obsolete, but worth keeping around because
3134 ;;; it is true, though too strong a condition:
3136 ;;; Two union types are equal if their subtypes are equal sets.
3137 (!define-type-method
(union :simple-
=) (type1 type2
)
3138 (multiple-value-bind (subtype certain?
)
3139 (csubtypep type1 type2
)
3141 (csubtypep type2 type1
)
3142 ;; we might as well become as certain as possible.
3145 (multiple-value-bind (subtype certain?
)
3146 (csubtypep type2 type1
)
3147 (declare (ignore subtype
))
3148 (values nil certain?
))))))
3150 (!define-type-method
(union :complex-
=) (type1 type2
)
3151 (declare (ignore type1
))
3152 (if (some #'type-might-contain-other-types-p
3153 (union-type-types type2
))
3157 ;;; Similarly, a union type is a subtype of another if and only if
3158 ;;; every element of TYPE1 is a subtype of TYPE2.
3159 (defun union-simple-subtypep (type1 type2
)
3160 (every/type
(swapped-args-fun #'union-complex-subtypep-arg2
)
3162 (union-type-types type1
)))
3164 (!define-type-method
(union :simple-subtypep
) (type1 type2
)
3165 (union-simple-subtypep type1 type2
))
3167 (defun union-complex-subtypep-arg1 (type1 type2
)
3168 (every/type
(swapped-args-fun #'csubtypep
)
3170 (union-type-types type1
)))
3172 (!define-type-method
(union :complex-subtypep-arg1
) (type1 type2
)
3173 (union-complex-subtypep-arg1 type1 type2
))
3175 (defun union-complex-subtypep-arg2 (type1 type2
)
3176 ;; At this stage, we know that type2 is a union type and type1
3177 ;; isn't. We might as well check this, though:
3178 (aver (union-type-p type2
))
3179 (aver (not (union-type-p type1
)))
3180 ;; was: (any/type #'csubtypep type1 (union-type-types type2)), which
3181 ;; turns out to be too restrictive, causing bug 91.
3183 ;; the following reimplementation might look dodgy. It is dodgy. It
3184 ;; depends on the union :complex-= method not doing very much work
3185 ;; -- certainly, not using subtypep. Reasoning:
3187 ;; A is a subset of (B1 u B2)
3188 ;; <=> A n (B1 u B2) = A
3189 ;; <=> (A n B1) u (A n B2) = A
3191 ;; But, we have to be careful not to delegate this type= to
3192 ;; something that could invoke subtypep, which might get us back
3193 ;; here -> stack explosion. We therefore ensure that the second type
3194 ;; (which is the one that's dispatched on) is either a union type
3195 ;; (where we've ensured that the complex-= method will not call
3196 ;; subtypep) or something with no union types involved, in which
3197 ;; case we'll never come back here.
3199 ;; If we don't do this, then e.g.
3200 ;; (SUBTYPEP '(MEMBER 3) '(OR (SATISFIES FOO) (SATISFIES BAR)))
3201 ;; would loop infinitely, as the member :complex-= method is
3202 ;; implemented in terms of subtypep.
3204 ;; Ouch. - CSR, 2002-04-10
3205 (multiple-value-bind (sub-value sub-certain?
)
3208 (mapcar (lambda (x) (type-intersection type1 x
))
3209 (union-type-types type2
))))
3211 (values sub-value sub-certain?
)
3212 ;; The ANY/TYPE expression above is a sufficient condition for
3213 ;; subsetness, but not a necessary one, so we might get a more
3214 ;; certain answer by this CALL-NEXT-METHOD-ish step when the
3215 ;; ANY/TYPE expression is uncertain.
3216 (invoke-complex-subtypep-arg1-method type1 type2
))))
3218 (!define-type-method
(union :complex-subtypep-arg2
) (type1 type2
)
3219 (union-complex-subtypep-arg2 type1 type2
))
3221 (!define-type-method
(union :simple-intersection2
:complex-intersection2
)
3223 ;; The CSUBTYPEP clauses here let us simplify e.g.
3224 ;; (TYPE-INTERSECTION2 (SPECIFIER-TYPE 'LIST)
3225 ;; (SPECIFIER-TYPE '(OR LIST VECTOR)))
3226 ;; (where LIST is (OR CONS NULL)).
3228 ;; The tests are more or less (CSUBTYPEP TYPE1 TYPE2) and vice
3229 ;; versa, but it's important that we pre-expand them into
3230 ;; specialized operations on individual elements of
3231 ;; UNION-TYPE-TYPES, instead of using the ordinary call to
3232 ;; CSUBTYPEP, in order to avoid possibly invoking any methods which
3233 ;; might in turn invoke (TYPE-INTERSECTION2 TYPE1 TYPE2) and thus
3234 ;; cause infinite recursion.
3236 ;; Within this method, type2 is guaranteed to be a union type:
3237 (aver (union-type-p type2
))
3238 ;; Make sure to call only the applicable methods...
3239 (cond ((and (union-type-p type1
)
3240 (union-simple-subtypep type1 type2
)) type1
)
3241 ((and (union-type-p type1
)
3242 (union-simple-subtypep type2 type1
)) type2
)
3243 ((and (not (union-type-p type1
))
3244 (union-complex-subtypep-arg2 type1 type2
))
3246 ((and (not (union-type-p type1
))
3247 (union-complex-subtypep-arg1 type2 type1
))
3250 ;; KLUDGE: This code accumulates a sequence of TYPE-UNION2
3251 ;; operations in a particular order, and gives up if any of
3252 ;; the sub-unions turn out not to be simple. In other cases
3253 ;; ca. sbcl-0.6.11.15, that approach to taking a union was a
3254 ;; bad idea, since it can overlook simplifications which
3255 ;; might occur if the terms were accumulated in a different
3256 ;; order. It's possible that that will be a problem here too.
3257 ;; However, I can't think of a good example to demonstrate
3258 ;; it, and without an example to demonstrate it I can't write
3259 ;; test cases, and without test cases I don't want to
3260 ;; complicate the code to address what's still a hypothetical
3261 ;; problem. So I punted. -- WHN 2001-03-20
3262 (let ((accumulator *empty-type
*))
3263 (dolist (t2 (union-type-types type2
) accumulator
)
3265 (type-union accumulator
3266 (type-intersection type1 t2
))))))))
3268 (!def-type-translator or
:list
((:context context
) &rest type-specifiers
)
3269 (let ((type (apply #'type-union
3270 (mapcar (lambda (x) (specifier-type-r context x
))
3272 (if (union-type-p type
)
3273 (sb!kernel
::simplify-array-unions type
)
3278 (!def-type-translator cons
((:context context
)
3279 &optional
(car-type-spec '*) (cdr-type-spec '*))
3280 (let ((car-type (single-value-specifier-type-r context car-type-spec
))
3281 (cdr-type (single-value-specifier-type-r context cdr-type-spec
)))
3282 (make-cons-type car-type cdr-type
)))
3284 (!define-type-method
(cons :negate
) (type)
3285 (if (and (eq (cons-type-car-type type
) *universal-type
*)
3286 (eq (cons-type-cdr-type type
) *universal-type
*))
3287 (make-negation-type type
)
3289 (make-negation-type (specifier-type 'cons
))
3291 ((and (not (eq (cons-type-car-type type
) *universal-type
*))
3292 (not (eq (cons-type-cdr-type type
) *universal-type
*)))
3295 (type-negation (cons-type-car-type type
))
3299 (type-negation (cons-type-cdr-type type
)))))
3300 ((not (eq (cons-type-car-type type
) *universal-type
*))
3302 (type-negation (cons-type-car-type type
))
3304 ((not (eq (cons-type-cdr-type type
) *universal-type
*))
3307 (type-negation (cons-type-cdr-type type
))))
3308 (t (bug "Weird CONS type ~S" type
))))))
3310 (!define-type-method
(cons :unparse
) (type)
3311 (if (eq type
(specifier-type 'cons
))
3313 `(cons ,(type-specifier (cons-type-car-type type
))
3314 ,(type-specifier (cons-type-cdr-type type
)))))
3316 (!define-type-method
(cons :simple-
=) (type1 type2
)
3317 (declare (type cons-type type1 type2
))
3318 (multiple-value-bind (car-match car-win
)
3319 (type= (cons-type-car-type type1
) (cons-type-car-type type2
))
3320 (multiple-value-bind (cdr-match cdr-win
)
3321 (type= (cons-type-cdr-type type1
) (cons-type-cdr-type type2
))
3322 (cond ((and car-match cdr-match
)
3323 (aver (and car-win cdr-win
))
3327 ;; FIXME: Ideally we would like to detect and handle
3328 ;; (CONS UNKNOWN INTEGER) (CONS UNKNOWN SYMBOL) => NIL, T
3329 ;; but just returning a secondary true on (and car-win cdr-win)
3330 ;; unfortunately breaks other things. --NS 2006-08-16
3331 (and (or (and (not car-match
) car-win
)
3332 (and (not cdr-match
) cdr-win
))
3333 (not (and (cons-type-might-be-empty-type type1
)
3334 (cons-type-might-be-empty-type type2
))))))))))
3336 (!define-type-method
(cons :simple-subtypep
) (type1 type2
)
3337 (declare (type cons-type type1 type2
))
3338 (multiple-value-bind (val-car win-car
)
3339 (csubtypep (cons-type-car-type type1
) (cons-type-car-type type2
))
3340 (multiple-value-bind (val-cdr win-cdr
)
3341 (csubtypep (cons-type-cdr-type type1
) (cons-type-cdr-type type2
))
3342 (if (and val-car val-cdr
)
3343 (values t
(and win-car win-cdr
))
3344 (values nil
(or (and (not val-car
) win-car
)
3345 (and (not val-cdr
) win-cdr
)))))))
3347 ;;; Give up if a precise type is not possible, to avoid returning
3348 ;;; overly general types.
3349 (!define-type-method
(cons :simple-union2
) (type1 type2
)
3350 (declare (type cons-type type1 type2
))
3351 (let ((car-type1 (cons-type-car-type type1
))
3352 (car-type2 (cons-type-car-type type2
))
3353 (cdr-type1 (cons-type-cdr-type type1
))
3354 (cdr-type2 (cons-type-cdr-type type2
))
3357 ;; UGH. -- CSR, 2003-02-24
3358 (macrolet ((frob-car (car1 car2 cdr1 cdr2
3359 &optional
(not1 nil not1p
))
3361 (make-cons-type ,car1
(type-union ,cdr1
,cdr2
))
3363 (type-intersection ,car2
3366 `(type-negation ,car1
)))
3368 (cond ((type= car-type1 car-type2
)
3369 (make-cons-type car-type1
3370 (type-union cdr-type1 cdr-type2
)))
3371 ((type= cdr-type1 cdr-type2
)
3372 (make-cons-type (type-union car-type1 car-type2
)
3374 ((csubtypep car-type1 car-type2
)
3375 (frob-car car-type1 car-type2 cdr-type1 cdr-type2
))
3376 ((csubtypep car-type2 car-type1
)
3377 (frob-car car-type2 car-type1 cdr-type2 cdr-type1
))
3378 ;; more general case of the above, but harder to compute
3380 (setf car-not1
(type-negation car-type1
))
3381 (multiple-value-bind (yes win
)
3382 (csubtypep car-type2 car-not1
)
3383 (and (not yes
) win
)))
3384 (frob-car car-type1 car-type2 cdr-type1 cdr-type2 car-not1
))
3386 (setf car-not2
(type-negation car-type2
))
3387 (multiple-value-bind (yes win
)
3388 (csubtypep car-type1 car-not2
)
3389 (and (not yes
) win
)))
3390 (frob-car car-type2 car-type1 cdr-type2 cdr-type1 car-not2
))
3391 ;; Don't put these in -- consider the effect of taking the
3392 ;; union of (CONS (INTEGER 0 2) (INTEGER 5 7)) and
3393 ;; (CONS (INTEGER 0 3) (INTEGER 5 6)).
3395 ((csubtypep cdr-type1 cdr-type2
)
3396 (frob-cdr car-type1 car-type2 cdr-type1 cdr-type2
))
3398 ((csubtypep cdr-type2 cdr-type1
)
3399 (frob-cdr car-type2 car-type1 cdr-type2 cdr-type1
))))))
3401 (!define-type-method
(cons :simple-intersection2
) (type1 type2
)
3402 (declare (type cons-type type1 type2
))
3403 (let ((car-int2 (type-intersection2 (cons-type-car-type type1
)
3404 (cons-type-car-type type2
)))
3405 (cdr-int2 (type-intersection2 (cons-type-cdr-type type1
)
3406 (cons-type-cdr-type type2
))))
3408 ((and car-int2 cdr-int2
) (make-cons-type car-int2 cdr-int2
))
3409 (car-int2 (make-cons-type car-int2
3411 (cons-type-cdr-type type1
)
3412 (cons-type-cdr-type type2
))))
3413 (cdr-int2 (make-cons-type
3414 (type-intersection (cons-type-car-type type1
)
3415 (cons-type-car-type type2
))
3418 (!define-superclasses cons
((cons)) !cold-init-forms
)
3420 ;;;; CHARACTER-SET types
3422 (!def-type-translator character-set
3423 (&optional
(pairs '((0 .
#.
(1- sb
!xc
:char-code-limit
)))))
3424 (make-character-set-type pairs
))
3426 (!define-type-method
(character-set :negate
) (type)
3427 (let ((pairs (character-set-type-pairs type
)))
3428 (if (and (= (length pairs
) 1)
3430 (= (cdar pairs
) (1- sb
!xc
:char-code-limit
)))
3431 (make-negation-type type
)
3432 (let ((not-character
3434 (make-character-set-type
3435 '((0 .
#.
(1- sb
!xc
:char-code-limit
)))))))
3438 (make-character-set-type
3440 (when (> (caar pairs
) 0)
3441 (push (cons 0 (1- (caar pairs
))) not-pairs
))
3442 (do* ((tail pairs
(cdr tail
))
3443 (high1 (cdar tail
) (cdar tail
))
3444 (low2 (caadr tail
) (caadr tail
)))
3446 (when (< (cdar tail
) (1- sb
!xc
:char-code-limit
))
3447 (push (cons (1+ (cdar tail
))
3448 (1- sb
!xc
:char-code-limit
))
3450 (nreverse not-pairs
))
3451 (push (cons (1+ high1
) (1- low2
)) not-pairs
)))))))))
3453 (!define-type-method
(character-set :unparse
) (type)
3455 ((eq type
(specifier-type 'character
)) 'character
)
3456 ((eq type
(specifier-type 'base-char
)) 'base-char
)
3457 ((eq type
(specifier-type 'extended-char
)) 'extended-char
)
3458 ;; standard-char is not an interned type
3459 ((type= type
(specifier-type 'standard-char
)) 'standard-char
)
3461 ;; Unparse into either MEMBER or CHARACTER-SET. We use MEMBER if there
3462 ;; are at most as many characters as there are character code ranges.
3463 ;; (basically saying to use MEMBER if each range is one character)
3464 (let* ((pairs (character-set-type-pairs type
))
3465 (count (length pairs
))
3466 (chars (loop named outer
3467 for
(low . high
) in pairs
3468 nconc
(loop for code from low upto high
3469 collect
(sb!xc
:code-char code
)
3470 when
(minusp (decf count
))
3471 do
(return-from outer t
)))))
3473 `(character-set ,pairs
)
3474 `(member ,@chars
))))))
3476 (!define-type-method
(character-set :singleton-p
) (type)
3477 (let* ((pairs (character-set-type-pairs type
))
3478 (pair (first pairs
)))
3479 (if (and (typep pairs
'(cons t null
))
3480 (eql (car pair
) (cdr pair
)))
3481 (values t
(code-char (car pair
)))
3484 (!define-type-method
(character-set :simple-
=) (type1 type2
)
3485 (let ((pairs1 (character-set-type-pairs type1
))
3486 (pairs2 (character-set-type-pairs type2
)))
3487 (values (equal pairs1 pairs2
) t
)))
3489 (!define-type-method
(character-set :simple-subtypep
) (type1 type2
)
3491 (dolist (pair (character-set-type-pairs type1
) t
)
3492 (unless (position pair
(character-set-type-pairs type2
)
3493 :test
(lambda (x y
) (and (>= (car x
) (car y
))
3494 (<= (cdr x
) (cdr y
)))))
3498 (!define-type-method
(character-set :simple-union2
) (type1 type2
)
3499 ;; KLUDGE: the canonizing in the MAKE-CHARACTER-SET-TYPE function
3500 ;; actually does the union for us. It might be a little fragile to
3502 (make-character-set-type
3504 (copy-alist (character-set-type-pairs type1
))
3505 (copy-alist (character-set-type-pairs type2
))
3508 (!define-type-method
(character-set :simple-intersection2
) (type1 type2
)
3509 ;; KLUDGE: brute force.
3512 (dolist (pair1 (character-set-type-pairs type1
)
3513 (make-character-set-type
3514 (sort pairs
#'< :key
#'car
)))
3515 (dolist (pair2 (character-set-type-pairs type2
))
3517 ((<= (car pair1
) (car pair2
) (cdr pair1
))
3518 (push (cons (car pair2
) (min (cdr pair1
) (cdr pair2
))) pairs
))
3519 ((<= (car pair2
) (car pair1
) (cdr pair2
))
3520 (push (cons (car pair1
) (min (cdr pair1
) (cdr pair2
))) pairs
))))))
3522 (make-character-set-type
3523 (intersect-type-pairs
3524 (character-set-type-pairs type1
)
3525 (character-set-type-pairs type2
))))
3528 ;;; Intersect two ordered lists of pairs
3529 ;;; Each list is of the form ((start1 . end1) ... (startn . endn)),
3530 ;;; where start1 <= end1 < start2 <= end2 < ... < startn <= endn.
3531 ;;; Each pair represents the integer interval start..end.
3533 (defun intersect-type-pairs (alist1 alist2
)
3534 (if (and alist1 alist2
)
3536 (pair1 (pop alist1
))
3537 (pair2 (pop alist2
)))
3539 (when (> (car pair1
) (car pair2
))
3540 (rotatef pair1 pair2
)
3541 (rotatef alist1 alist2
))
3542 (let ((pair1-cdr (cdr pair1
)))
3544 ((> (car pair2
) pair1-cdr
)
3545 ;; No over lap -- discard pair1
3546 (unless alist1
(return))
3547 (setq pair1
(pop alist1
)))
3548 ((<= (cdr pair2
) pair1-cdr
)
3549 (push (cons (car pair2
) (cdr pair2
)) res
)
3551 ((= (cdr pair2
) pair1-cdr
)
3552 (unless alist1
(return))
3553 (unless alist2
(return))
3554 (setq pair1
(pop alist1
)
3555 pair2
(pop alist2
)))
3556 (t ;; (< (cdr pair2) pair1-cdr)
3557 (unless alist2
(return))
3558 (setq pair1
(cons (1+ (cdr pair2
)) pair1-cdr
))
3559 (setq pair2
(pop alist2
)))))
3560 (t ;; (> (cdr pair2) (cdr pair1))
3561 (push (cons (car pair2
) pair1-cdr
) res
)
3562 (unless alist1
(return))
3563 (setq pair2
(cons (1+ pair1-cdr
) (cdr pair2
)))
3564 (setq pair1
(pop alist1
))))))
3569 ;;; Return the type that describes all objects that are in X but not
3570 ;;; in Y. If we can't determine this type, then return NIL.
3572 ;;; For now, we only are clever dealing with union and member types.
3573 ;;; If either type is not a union type, then we pretend that it is a
3574 ;;; union of just one type. What we do is remove from X all the types
3575 ;;; that are a subtype any type in Y. If any type in X intersects with
3576 ;;; a type in Y but is not a subtype, then we give up.
3578 ;;; We must also special-case any member type that appears in the
3579 ;;; union. We remove from X's members all objects that are TYPEP to Y.
3580 ;;; If Y has any members, we must be careful that none of those
3581 ;;; members are CTYPEP to any of Y's non-member types. We give up in
3582 ;;; this case, since to compute that difference we would have to break
3583 ;;; the type from X into some collection of types that represents the
3584 ;;; type without that particular element. This seems too hairy to be
3585 ;;; worthwhile, given its low utility.
3586 (defun type-difference (x y
)
3587 (if (and (numeric-type-p x
) (numeric-type-p y
))
3588 ;; Numeric types are easy. Are there any others we should handle like this?
3589 (type-intersection x
(type-negation y
))
3590 (let ((x-types (if (union-type-p x
) (union-type-types x
) (list x
)))
3591 (y-types (if (union-type-p y
) (union-type-types y
) (list y
))))
3593 (dolist (x-type x-types
)
3594 (if (member-type-p x-type
)
3595 (let ((xset (alloc-xset))
3597 (mapc-member-type-members
3599 (multiple-value-bind (ok sure
) (ctypep elt y
)
3601 (return-from type-difference nil
))
3604 (pushnew elt fp-zeroes
)
3605 (add-to-xset elt xset
)))))
3607 (unless (and (xset-empty-p xset
) (not fp-zeroes
))
3608 (res (make-member-type xset fp-zeroes
))))
3609 (dolist (y-type y-types
(res x-type
))
3610 (multiple-value-bind (val win
) (csubtypep x-type y-type
)
3611 (unless win
(return-from type-difference nil
))
3613 (when (types-equal-or-intersect x-type y-type
)
3614 (return-from type-difference nil
))))))
3615 (let ((y-mem (find-if #'member-type-p y-types
)))
3617 (dolist (x-type x-types
)
3618 (unless (member-type-p x-type
)
3619 (mapc-member-type-members
3621 (multiple-value-bind (ok sure
) (ctypep member x-type
)
3622 (when (or (not sure
) ok
)
3623 (return-from type-difference nil
))))
3625 (apply #'type-union
(res))))))
3627 (!def-type-translator array
((:context context
)
3628 &optional
(element-type '*)
3630 (let ((eltype (if (eq element-type
'*)
3632 (specifier-type-r context element-type
))))
3633 (make-array-type (canonical-array-dimensions dimensions
)
3635 :element-type eltype
3636 :specialized-element-type
(%upgraded-array-element-type
3639 (!def-type-translator simple-array
((:context context
)
3640 &optional
(element-type '*)
3642 (let ((eltype (if (eq element-type
'*)
3644 (specifier-type-r context element-type
))))
3645 (make-array-type (canonical-array-dimensions dimensions
)
3647 :element-type eltype
3648 :specialized-element-type
(%upgraded-array-element-type
3651 ;;;; SIMD-PACK types
3654 (!define-type-class simd-pack
:enumerable nil
3655 :might-contain-other-types nil
)
3657 ;; Though this involves a recursive call to parser, parsing context need not
3658 ;; be passed down, because an unknown-type condition is an immediate failure.
3659 (!def-type-translator simd-pack
(&optional
(element-type-spec '*))
3660 (if (eql element-type-spec
'*)
3661 (%make-simd-pack-type
*simd-pack-element-types
*)
3662 (make-simd-pack-type (single-value-specifier-type element-type-spec
))))
3664 (!define-type-method
(simd-pack :negate
) (type)
3665 (let ((remaining (set-difference *simd-pack-element-types
*
3666 (simd-pack-type-element-type type
)))
3667 (not-simd-pack (make-negation-type (specifier-type 'simd-pack
))))
3669 (type-union not-simd-pack
(%make-simd-pack-type remaining
))
3672 (!define-type-method
(simd-pack :unparse
) (type)
3673 (let ((eltypes (simd-pack-type-element-type type
)))
3674 (cond ((equal eltypes
*simd-pack-element-types
*)
3676 ((= 1 (length eltypes
))
3677 `(simd-pack ,(first eltypes
)))
3679 `(or ,@(mapcar (lambda (eltype)
3680 `(simd-pack ,eltype
))
3683 (!define-type-method
(simd-pack :simple-
=) (type1 type2
)
3684 (declare (type simd-pack-type type1 type2
))
3685 (null (set-exclusive-or (simd-pack-type-element-type type1
)
3686 (simd-pack-type-element-type type2
))))
3688 (!define-type-method
(simd-pack :simple-subtypep
) (type1 type2
)
3689 (declare (type simd-pack-type type1 type2
))
3690 (subsetp (simd-pack-type-element-type type1
)
3691 (simd-pack-type-element-type type2
)))
3693 (!define-type-method
(simd-pack :simple-union2
) (type1 type2
)
3694 (declare (type simd-pack-type type1 type2
))
3695 (%make-simd-pack-type
(union (simd-pack-type-element-type type1
)
3696 (simd-pack-type-element-type type2
))))
3698 (!define-type-method
(simd-pack :simple-intersection2
) (type1 type2
)
3699 (declare (type simd-pack-type type1 type2
))
3700 (let ((intersection (intersection (simd-pack-type-element-type type1
)
3701 (simd-pack-type-element-type type2
))))
3703 (%make-simd-pack-type intersection
)
3706 (!define-superclasses simd-pack
((simd-pack)) !cold-init-forms
))
3708 ;;;; utilities shared between cross-compiler and target system
3710 ;;; Does the type derived from compilation of an actual function
3711 ;;; definition satisfy declarations of a function's type?
3712 (defun defined-ftype-matches-declared-ftype-p (defined-ftype declared-ftype
)
3713 (declare (type ctype defined-ftype declared-ftype
))
3714 (flet ((is-built-in-class-function-p (ctype)
3715 (and (built-in-classoid-p ctype
)
3716 (eq (built-in-classoid-name ctype
) 'function
))))
3717 (cond (;; DECLARED-FTYPE could certainly be #<BUILT-IN-CLASS FUNCTION>;
3718 ;; that's what happens when we (DECLAIM (FTYPE FUNCTION FOO)).
3719 (is-built-in-class-function-p declared-ftype
)
3720 ;; In that case, any definition satisfies the declaration.
3722 (;; It's not clear whether or how DEFINED-FTYPE might be
3723 ;; #<BUILT-IN-CLASS FUNCTION>, but it's not obviously
3724 ;; invalid, so let's handle that case too, just in case.
3725 (is-built-in-class-function-p defined-ftype
)
3726 ;; No matter what DECLARED-FTYPE might be, we can't prove
3727 ;; that an object of type FUNCTION doesn't satisfy it, so
3728 ;; we return success no matter what.
3730 (;; Otherwise both of them must be FUN-TYPE objects.
3732 ;; FIXME: For now we only check compatibility of the return
3733 ;; type, not argument types, and we don't even check the
3734 ;; return type very precisely (as per bug 94a). It would be
3735 ;; good to do a better job. Perhaps to check the
3736 ;; compatibility of the arguments, we should (1) redo
3737 ;; VALUES-TYPES-EQUAL-OR-INTERSECT as
3738 ;; ARGS-TYPES-EQUAL-OR-INTERSECT, and then (2) apply it to
3739 ;; the ARGS-TYPE slices of the FUN-TYPEs. (ARGS-TYPE
3740 ;; is a base class both of VALUES-TYPE and of FUN-TYPE.)
3741 (values-types-equal-or-intersect
3742 (fun-type-returns defined-ftype
)
3743 (fun-type-returns declared-ftype
))))))
3745 ;;; This messy case of CTYPE for NUMBER is shared between the
3746 ;;; cross-compiler and the target system.
3747 (defun ctype-of-number (x)
3748 (let ((num (if (complexp x
) (realpart x
) x
)))
3749 (multiple-value-bind (complexp low high
)
3751 (let ((imag (imagpart x
)))
3752 (values :complex
(min num imag
) (max num imag
)))
3753 (values :real num num
))
3754 (make-numeric-type :class
(etypecase num
3755 (integer (if (complexp x
)
3756 (if (integerp (imagpart x
))
3760 (rational 'rational
)
3762 :format
(and (floatp num
) (float-format-name num
))
3767 ;;; The following function is a generic driver for approximating
3768 ;;; set-valued functions over types. Putting this here because it'll
3769 ;;; probably be useful for a lot of type analyses.
3771 ;;; Let f be a function from values of type X to Y, e.g., ARRAY-RANK.
3773 ;;; We compute an over or under-approximation of the set
3775 ;;; F(TYPE) = { f(x) : x in TYPE /\ x in X } \subseteq Y
3777 ;;; via set-valued approximations of f, OVER and UNDER.
3779 ;;; These functions must have the property that
3780 ;;; Forall TYPE, OVER(TYPE) \superseteq F(TYPE) and
3781 ;;; Forall TYPE, UNDER(TYPE) \subseteq F(TYPE)
3783 ;;; The driver is also parameterised over the finite set
3786 ;;; Union, intersection and difference are binary functions to compute
3787 ;;; set union, intersection and difference. Top and bottom are the
3788 ;;; concrete representations for the universe and empty sets; we never
3789 ;;; call the set functions on top or bottom, so it's safe to use
3790 ;;; special values there.
3794 ;;; TYPE: the ctype for which we wish to approximate F(TYPE)
3795 ;;; OVERAPPROXIMATE: true if we wish to overapproximate, nil otherwise.
3796 ;;; You usually want T.
3797 ;;; UNION/INTERSECTION/DIFFERENCE: implementations of finite set operations.
3798 ;;; Conform to cl::(union/intersection/set-difference). Passing NIL will
3799 ;;; disable some cleverness and result in quicker computation of coarser
3800 ;;; approximations. However, passing difference without union and intersection
3801 ;;; will probably not end well.
3802 ;;; TOP/BOTTOM: concrete representation of the universe and empty set. Finite
3803 ;;; set operations are never called on TOP/BOTTOM, so it's safe to use special
3805 ;;; OVER/UNDER: the set-valued approximations of F.
3807 ;;; Implementation details.
3809 ;;; It's a straightforward walk down the type.
3810 ;;; Union types -> take the union of children, intersection ->
3811 ;;; intersect. There is some complication for negation types: we must
3812 ;;; not only negate the result, but also flip from overapproximating
3813 ;;; to underapproximating in the children (or vice versa).
3815 ;;; We represent sets as a pair of (negate-p finite-set) in order to
3816 ;;; support negation types.
3818 (declaim (inline generic-abstract-type-function
))
3819 (defun generic-abstract-type-function
3820 (type overapproximate
3821 union intersection difference
3824 (labels ((union* (x y
)
3825 ;; wrappers to avoid calling union/intersection on
3827 (cond ((or (eql x top
)
3833 (funcall union x y
))))
3834 (intersection* (x y
)
3835 (cond ((or (eql x bottom
)
3841 (funcall intersection x y
))))
3842 (unite (not-x-p x not-y-p y
)
3843 ;; if we only have one negated set, it's x.
3845 (rotatef not-x-p not-y-p
)
3847 (cond ((and not-x-p not-y-p
)
3848 ;; -x \/ -y = -(x /\ y)
3849 (normalize t
(intersection* x y
)))
3851 ;; -x \/ y = -(x \ y)
3861 (funcall difference x y
)))))
3863 (values nil
(union* x y
)))))
3864 (intersect (not-x-p x not-y-p y
)
3866 (rotatef not-x-p not-y-p
)
3868 (cond ((and not-x-p not-y-p
)
3869 ;; -x /\ -y = -(x \/ y)
3870 (normalize t
(union* x y
)))
3873 (cond ((or (eql x top
) (eql y bottom
))
3874 (values nil bottom
))
3880 (values nil
(funcall difference y x
)))))
3882 (values nil
(intersection* x y
)))))
3883 (normalize (not-x-p x
)
3884 ;; catch some easy cases of redundant negation.
3885 (cond ((not not-x-p
)
3893 (default (overapproximate)
3895 (if overapproximate top bottom
))
3896 (walk-union (types overapproximate
)
3897 ;; Only do this if union is provided.
3899 (return-from walk-union
(default overapproximate
)))
3900 ;; Reduce/union from bottom.
3901 (let ((not-acc-p nil
)
3903 (dolist (type types
(values not-acc-p acc
))
3904 (multiple-value-bind (not x
)
3905 (walk type overapproximate
)
3906 (setf (values not-acc-p acc
)
3907 (unite not-acc-p acc not x
)))
3908 ;; Early exit on top set.
3909 (when (and (eql acc top
)
3911 (return (values nil top
))))))
3912 (walk-intersection (types overapproximate
)
3913 ;; Skip if we don't know how to intersect sets
3914 (unless intersection
3915 (return-from walk-intersection
(default overapproximate
)))
3916 ;; Reduce/intersection from top
3917 (let ((not-acc-p nil
)
3919 (dolist (type types
(values not-acc-p acc
))
3920 (multiple-value-bind (not x
)
3921 (walk type overapproximate
)
3922 (setf (values not-acc-p acc
)
3923 (intersect not-acc-p acc not x
)))
3924 (when (and (eql acc bottom
)
3926 (return (values nil bottom
))))))
3927 (walk-negate (type overapproximate
)
3928 ;; Don't introduce negated types if we don't know how to
3931 (return-from walk-negate
(default overapproximate
)))
3932 (multiple-value-bind (not x
)
3933 (walk type
(not overapproximate
))
3934 (normalize (not not
) x
)))
3935 (walk (type overapproximate
)
3938 (walk-union (union-type-types type
) overapproximate
))
3939 ((cons (member or union
))
3940 (walk-union (rest type
) overapproximate
))
3942 (walk-intersection (intersection-type-types type
) overapproximate
))
3943 ((cons (member and intersection
))
3944 (walk-intersection (rest type
) overapproximate
))
3946 (walk-negate (negation-type-type type
) overapproximate
))
3948 (walk-negate (second type
) overapproximate
))
3956 (funcall under type
)
3957 (default nil
))))))))
3958 (multiple-value-call #'normalize
(walk type overapproximate
))))
3959 (declaim (notinline generic-abstract-type-function
))
3961 ;;; Standard list representation of sets. Use CL:* for the universe.
3962 (defun list-abstract-type-function (type over
&key under
(overapproximate t
))
3963 (declare (inline generic-abstract-type-function
))
3964 (generic-abstract-type-function
3965 type overapproximate
3966 #'union
#'intersection
#'set-difference
3970 (!defun-from-collected-cold-init-forms
!late-type-cold-init
)
3972 (/show0
"late-type.lisp end of file")