Make INFO's compiler-macro more forgiving.
[sbcl.git] / src / code / late-type.lisp
blob85efd51c261e698323c639518312baec8fd4bffe
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
6 ;;;; moved out..)
8 ;;;; This software is part of the SBCL system. See the README file for
9 ;;;; more information.
10 ;;;;
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:
24 ;;;
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 ;;; These functions are used as method for types which need a complex
34 ;;; subtypep method to handle some superclasses, but cover a subtree
35 ;;; of the type graph (i.e. there is no simple way for any other type
36 ;;; class to be a subtype.) There are always still complex ways,
37 ;;; namely UNION and MEMBER types, so we must give TYPE1's method a
38 ;;; chance to run, instead of immediately returning NIL, T.
39 (defun delegate-complex-subtypep-arg2 (type1 type2)
40 (let ((subtypep-arg1
41 (type-class-complex-subtypep-arg1 (type-class-info type1))))
42 (if subtypep-arg1
43 (funcall subtypep-arg1 type1 type2)
44 (values nil t))))
45 (defun delegate-complex-intersection2 (type1 type2)
46 (let ((method (type-class-complex-intersection2 (type-class-info type1))))
47 (if (and method (not (eq method #'delegate-complex-intersection2)))
48 (funcall method type2 type1)
49 (hierarchical-intersection2 type1 type2))))
51 (defun contains-unknown-type-p (ctype)
52 (cond ((unknown-type-p ctype) t)
53 ((compound-type-p ctype)
54 (some #'contains-unknown-type-p (compound-type-types ctype)))
55 ((negation-type-p ctype)
56 (contains-unknown-type-p (negation-type-type ctype)))))
58 ;;; This is used by !DEFINE-SUPERCLASSES to define the SUBTYPE-ARG1
59 ;;; method. INFO is a list of conses
60 ;;; (SUPERCLASS-CLASS . {GUARD-TYPE-SPECIFIER | NIL}).
61 (defun has-superclasses-complex-subtypep-arg1 (type1 type2 info)
62 ;; If TYPE2 might be concealing something related to our class
63 ;; hierarchy
64 (if (type-might-contain-other-types-p type2)
65 ;; too confusing, gotta punt
66 (values nil nil)
67 ;; ordinary case expected by old CMU CL code, where the taxonomy
68 ;; of TYPE2's representation accurately reflects the taxonomy of
69 ;; the underlying set
70 (values
71 ;; FIXME: This old CMU CL code probably deserves a comment
72 ;; explaining to us mere mortals how it works...
73 (and (sb!xc:typep type2 'classoid)
74 (dolist (x info nil)
75 (when (or (not (cdr x))
76 (csubtypep type1 (specifier-type (cdr x))))
77 (return
78 (or (eq type2 (car x))
79 (let ((inherits (layout-inherits
80 (classoid-layout (car x)))))
81 (dotimes (i (length inherits) nil)
82 (when (eq type2 (layout-classoid (svref inherits i)))
83 (return t)))))))))
84 t)))
86 ;;; This function takes a list of specs, each of the form
87 ;;; (SUPERCLASS-NAME &OPTIONAL GUARD).
88 ;;; Consider one spec (with no guard): any instance of the named
89 ;;; TYPE-CLASS is also a subtype of the named superclass and of any of
90 ;;; its superclasses. If there are multiple specs, then some will have
91 ;;; guards. We choose the first spec whose guard is a supertype of
92 ;;; TYPE1 and use its superclass. In effect, a sequence of guards
93 ;;; G0, G1, G2
94 ;;; is actually
95 ;;; G0,(and G1 (not G0)), (and G2 (not (or G0 G1))).
96 ;;;
97 ;;; WHEN controls when the forms are executed.
98 (defmacro !define-superclasses (type-class-name specs when)
99 (with-unique-names (type-class info)
100 `(,when
101 (let ((,type-class (type-class-or-lose ',type-class-name))
102 (,info (mapcar (lambda (spec)
103 (destructuring-bind
104 (super &optional guard)
105 spec
106 (cons (find-classoid super) guard)))
107 ',specs)))
108 (setf (type-class-complex-subtypep-arg1 ,type-class)
109 (lambda (type1 type2)
110 (has-superclasses-complex-subtypep-arg1 type1 type2 ,info)))
111 (setf (type-class-complex-subtypep-arg2 ,type-class)
112 #'delegate-complex-subtypep-arg2)
113 (setf (type-class-complex-intersection2 ,type-class)
114 #'delegate-complex-intersection2)))))
116 ;;;; FUNCTION and VALUES types
117 ;;;;
118 ;;;; Pretty much all of the general type operations are illegal on
119 ;;;; VALUES types, since we can't discriminate using them, do
120 ;;;; SUBTYPEP, etc. FUNCTION types are acceptable to the normal type
121 ;;;; operations, but are generally considered to be equivalent to
122 ;;;; FUNCTION. These really aren't true types in any type theoretic
123 ;;;; sense, but we still parse them into CTYPE structures for two
124 ;;;; reasons:
126 ;;;; -- Parsing and unparsing work the same way, and indeed we can't
127 ;;;; tell whether a type is a function or values type without
128 ;;;; parsing it.
129 ;;;; -- Many of the places that can be annotated with real types can
130 ;;;; also be annotated with function or values types.
132 ;;; the description of a &KEY argument
133 (defstruct (key-info #-sb-xc-host (:pure t)
134 (:copier nil))
135 ;; the key (not necessarily a keyword in ANSI Common Lisp)
136 (name (missing-arg) :type symbol :read-only t)
137 ;; the type of the argument value
138 (type (missing-arg) :type ctype :read-only t))
140 (!define-type-method (values :simple-subtypep :complex-subtypep-arg1)
141 (type1 type2)
142 (declare (ignore type2))
143 ;; FIXME: should be TYPE-ERROR, here and in next method
144 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type1)))
146 (!define-type-method (values :complex-subtypep-arg2)
147 (type1 type2)
148 (declare (ignore type1))
149 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type2)))
151 (!define-type-method (values :negate) (type)
152 (error "NOT VALUES too confusing on ~S" (type-specifier type)))
154 (!define-type-method (values :unparse) (type)
155 (cons 'values
156 (let ((unparsed (unparse-args-types type)))
157 (if (or (values-type-optional type)
158 (values-type-rest type)
159 (values-type-allowp type))
160 unparsed
161 (nconc unparsed '(&optional))))))
163 ;;; Return true if LIST1 and LIST2 have the same elements in the same
164 ;;; positions according to TYPE=. We return NIL, NIL if there is an
165 ;;; uncertain comparison.
166 (defun type=-list (list1 list2)
167 (declare (list list1 list2))
168 (do ((types1 list1 (cdr types1))
169 (types2 list2 (cdr types2)))
170 ((or (null types1) (null types2))
171 (if (or types1 types2)
172 (values nil t)
173 (values t t)))
174 (multiple-value-bind (val win)
175 (type= (first types1) (first types2))
176 (unless win
177 (return (values nil nil)))
178 (unless val
179 (return (values nil t))))))
181 (!define-type-method (values :simple-=) (type1 type2)
182 (type=-args type1 type2))
184 (!define-type-class function :enumerable nil
185 :might-contain-other-types nil)
187 ;;; a flag that we can bind to cause complex function types to be
188 ;;; unparsed as FUNCTION. This is useful when we want a type that we
189 ;;; can pass to TYPEP.
190 (!defvar *unparse-fun-type-simplify* nil)
191 ;;; A flag to prevent TYPE-OF calls by user applications from returning
192 ;;; (NOT x). TYPE-SPECIFIER usually allows it to preserve information.
193 (!defvar *unparse-allow-negation* t)
195 (!define-type-method (function :negate) (type)
196 (make-negation-type :type type))
198 (!define-type-method (function :unparse) (type)
199 (if *unparse-fun-type-simplify*
200 'function
201 (list 'function
202 (if (fun-type-wild-args type)
204 (unparse-args-types type))
205 (type-specifier
206 (fun-type-returns type)))))
208 ;;; The meaning of this is a little confused. On the one hand, all
209 ;;; function objects are represented the same way regardless of the
210 ;;; arglists and return values, and apps don't get to ask things like
211 ;;; (TYPEP #'FOO (FUNCTION (FIXNUM) *)) in any meaningful way. On the
212 ;;; other hand, Python wants to reason about function types. So...
213 (!define-type-method (function :simple-subtypep) (type1 type2)
214 (flet ((fun-type-simple-p (type)
215 (not (or (fun-type-rest type)
216 (fun-type-keyp type))))
217 (every-csubtypep (types1 types2)
218 (loop
219 for a1 in types1
220 for a2 in types2
221 do (multiple-value-bind (res sure-p)
222 (csubtypep a1 a2)
223 (unless res (return (values res sure-p))))
224 finally (return (values t t)))))
225 (and/type (values-subtypep (fun-type-returns type1)
226 (fun-type-returns type2))
227 (cond ((fun-type-wild-args type2) (values t t))
228 ((fun-type-wild-args type1)
229 (cond ((fun-type-keyp type2) (values nil nil))
230 ((not (fun-type-rest type2)) (values nil t))
231 ((not (null (fun-type-required type2)))
232 (values nil t))
233 (t (and/type (type= *universal-type*
234 (fun-type-rest type2))
235 (every/type #'type=
236 *universal-type*
237 (fun-type-optional
238 type2))))))
239 ((not (and (fun-type-simple-p type1)
240 (fun-type-simple-p type2)))
241 (values nil nil))
242 (t (multiple-value-bind (min1 max1) (fun-type-nargs type1)
243 (multiple-value-bind (min2 max2) (fun-type-nargs type2)
244 (cond ((or (> max1 max2) (< min1 min2))
245 (values nil t))
246 ((and (= min1 min2) (= max1 max2))
247 (and/type (every-csubtypep
248 (fun-type-required type1)
249 (fun-type-required type2))
250 (every-csubtypep
251 (fun-type-optional type1)
252 (fun-type-optional type2))))
253 (t (every-csubtypep
254 (concatenate 'list
255 (fun-type-required type1)
256 (fun-type-optional type1))
257 (concatenate 'list
258 (fun-type-required type2)
259 (fun-type-optional type2))))))))))))
261 (!define-superclasses function ((function)) !cold-init-forms)
263 ;;; The union or intersection of two FUNCTION types is FUNCTION.
264 (!define-type-method (function :simple-union2) (type1 type2)
265 (declare (ignore type1 type2))
266 (specifier-type 'function))
267 (!define-type-method (function :simple-intersection2) (type1 type2)
268 (let ((ftype (specifier-type 'function)))
269 (cond ((eq type1 ftype) type2)
270 ((eq type2 ftype) type1)
271 (t (let ((rtype (values-type-intersection (fun-type-returns type1)
272 (fun-type-returns type2))))
273 (flet ((change-returns (ftype rtype)
274 (declare (type fun-type ftype) (type ctype rtype))
275 (make-fun-type :required (fun-type-required ftype)
276 :optional (fun-type-optional ftype)
277 :keyp (fun-type-keyp ftype)
278 :keywords (fun-type-keywords ftype)
279 :allowp (fun-type-allowp ftype)
280 :returns rtype)))
281 (cond
282 ((fun-type-wild-args type1)
283 (if (fun-type-wild-args type2)
284 (make-fun-type :wild-args t
285 :returns rtype)
286 (change-returns type2 rtype)))
287 ((fun-type-wild-args type2)
288 (change-returns type1 rtype))
289 (t (multiple-value-bind (req opt rest)
290 (args-type-op type1 type2 #'type-intersection #'max)
291 (make-fun-type :required req
292 :optional opt
293 :rest rest
294 ;; FIXME: :keys
295 :allowp (and (fun-type-allowp type1)
296 (fun-type-allowp type2))
297 :returns rtype))))))))))
299 ;;; The union or intersection of a subclass of FUNCTION with a
300 ;;; FUNCTION type is somewhat complicated.
301 (!define-type-method (function :complex-intersection2) (type1 type2)
302 (cond
303 ((type= type1 (specifier-type 'function)) type2)
304 ((csubtypep type1 (specifier-type 'function)) nil)
305 (t :call-other-method)))
306 (!define-type-method (function :complex-union2) (type1 type2)
307 (declare (ignore type2))
308 ;; TYPE2 is a FUNCTION type. If TYPE1 is a classoid type naming
309 ;; FUNCTION, then it is the union of the two; otherwise, there is no
310 ;; special union.
311 (cond
312 ((type= type1 (specifier-type 'function)) type1)
313 (t nil)))
315 (!define-type-method (function :simple-=) (type1 type2)
316 (macrolet ((compare (comparator field)
317 (let ((reader (symbolicate '#:fun-type- field)))
318 `(,comparator (,reader type1) (,reader type2)))))
319 (and/type (compare type= returns)
320 (cond ((neq (fun-type-wild-args type1) (fun-type-wild-args type2))
321 (values nil t))
322 ((eq (fun-type-wild-args type1) t)
323 (values t t))
324 (t (type=-args type1 type2))))))
326 (!define-type-class constant :inherits values)
328 (!define-type-method (constant :negate) (type)
329 (error "NOT CONSTANT too confusing on ~S" (type-specifier type)))
331 (!define-type-method (constant :unparse) (type)
332 `(constant-arg ,(type-specifier (constant-type-type type))))
334 (!define-type-method (constant :simple-=) (type1 type2)
335 (type= (constant-type-type type1) (constant-type-type type2)))
337 (!def-type-translator constant-arg (type)
338 (make-constant-type :type (single-value-specifier-type type)))
340 ;;; Return the lambda-list-like type specification corresponding
341 ;;; to an ARGS-TYPE.
342 (declaim (ftype (function (args-type) list) unparse-args-types))
343 (defun unparse-args-types (type)
344 (collect ((result))
346 (dolist (arg (args-type-required type))
347 (result (type-specifier arg)))
349 (when (args-type-optional type)
350 (result '&optional)
351 (dolist (arg (args-type-optional type))
352 (result (type-specifier arg))))
354 (when (args-type-rest type)
355 (result '&rest)
356 (result (type-specifier (args-type-rest type))))
358 (when (args-type-keyp type)
359 (result '&key)
360 (dolist (key (args-type-keywords type))
361 (result (list (key-info-name key)
362 (type-specifier (key-info-type key))))))
364 (when (args-type-allowp type)
365 (result '&allow-other-keys))
367 (result)))
369 (!def-type-translator function (&optional (args '*) (result '*))
370 (let ((result (coerce-to-values (values-specifier-type result))))
371 (if (eq args '*)
372 (if (eq result *wild-type*)
373 (specifier-type 'function)
374 (make-fun-type :wild-args t :returns result))
375 (multiple-value-bind (required optional rest keyp keywords allowp)
376 (parse-args-types args)
377 (if (and (null required)
378 (null optional)
379 (eq rest *universal-type*)
380 (not keyp))
381 (if (eq result *wild-type*)
382 (specifier-type 'function)
383 (make-fun-type :wild-args t :returns result))
384 (make-fun-type :required required
385 :optional optional
386 :rest rest
387 :keyp keyp
388 :keywords keywords
389 :allowp allowp
390 :returns result))))))
392 (!def-type-translator values (&rest values)
393 (if (eq values '*)
394 *wild-type*
395 (multiple-value-bind (required optional rest keyp keywords allowp llk-p)
396 (parse-args-types values)
397 (declare (ignore keywords))
398 (cond (keyp
399 (error "&KEY appeared in a VALUES type specifier ~S."
400 `(values ,@values)))
401 (llk-p
402 (make-values-type :required required
403 :optional optional
404 :rest rest
405 :allowp allowp))
407 (make-short-values-type required))))))
409 ;;;; VALUES types interfaces
410 ;;;;
411 ;;;; We provide a few special operations that can be meaningfully used
412 ;;;; on VALUES types (as well as on any other type).
414 ;;; Return the minimum number of values possibly matching VALUES type
415 ;;; TYPE.
416 (defun values-type-min-value-count (type)
417 (etypecase type
418 (named-type
419 (ecase (named-type-name type)
420 ((t *) 0)
421 ((nil) 0)))
422 (values-type
423 (length (values-type-required type)))))
425 ;;; Return the maximum number of values possibly matching VALUES type
426 ;;; TYPE.
427 (defun values-type-max-value-count (type)
428 (etypecase type
429 (named-type
430 (ecase (named-type-name type)
431 ((t *) call-arguments-limit)
432 ((nil) 0)))
433 (values-type
434 (if (values-type-rest type)
435 call-arguments-limit
436 (+ (length (values-type-optional type))
437 (length (values-type-required type)))))))
439 (defun values-type-may-be-single-value-p (type)
440 (<= (values-type-min-value-count type)
442 (values-type-max-value-count type)))
444 ;;; VALUES type with a single value.
445 (defun type-single-value-p (type)
446 (and (%values-type-p type)
447 (not (values-type-rest type))
448 (null (values-type-optional type))
449 (singleton-p (values-type-required type))))
451 ;;; Return the type of the first value indicated by TYPE. This is used
452 ;;; by people who don't want to have to deal with VALUES types.
453 #!-sb-fluid (declaim (freeze-type values-type))
454 ; (inline single-value-type))
455 (defun single-value-type (type)
456 (declare (type ctype type))
457 (cond ((eq type *wild-type*)
458 *universal-type*)
459 ((eq type *empty-type*)
460 *empty-type*)
461 ((not (values-type-p type))
462 type)
463 ((car (args-type-required type)))
464 (t (type-union (specifier-type 'null)
465 (or (car (args-type-optional type))
466 (args-type-rest type)
467 (specifier-type 'null))))))
469 ;;; Return the minimum number of arguments that a function can be
470 ;;; called with, and the maximum number or NIL. If not a function
471 ;;; type, return NIL, NIL.
472 (defun fun-type-nargs (type)
473 (declare (type ctype type))
474 (if (and (fun-type-p type) (not (fun-type-wild-args type)))
475 (let ((fixed (length (args-type-required type))))
476 (if (or (args-type-rest type)
477 (args-type-keyp type)
478 (args-type-allowp type))
479 (values fixed nil)
480 (values fixed (+ fixed (length (args-type-optional type))))))
481 (values nil nil)))
483 ;;; Determine whether TYPE corresponds to a definite number of values.
484 ;;; The first value is a list of the types for each value, and the
485 ;;; second value is the number of values. If the number of values is
486 ;;; not fixed, then return NIL and :UNKNOWN.
487 (defun values-types (type)
488 (declare (type ctype type))
489 (cond ((or (eq type *wild-type*) (eq type *empty-type*))
490 (values nil :unknown))
491 ((or (args-type-optional type)
492 (args-type-rest type))
493 (values nil :unknown))
495 (let ((req (args-type-required type)))
496 (values req (length req))))))
498 ;;; Return two values:
499 ;;; 1. A list of all the positional (fixed and optional) types.
500 ;;; 2. The &REST type (if any). If no &REST, then the DEFAULT-TYPE.
501 (defun values-type-types (type &optional (default-type *empty-type*))
502 (declare (type ctype type))
503 (if (eq type *wild-type*)
504 (values nil *universal-type*)
505 (values (append (args-type-required type)
506 (args-type-optional type))
507 (cond ((args-type-rest type))
508 (t default-type)))))
510 ;;; types of values in (the <type> (values o_1 ... o_n))
511 (defun values-type-out (type count)
512 (declare (type ctype type) (type unsigned-byte count))
513 (if (eq type *wild-type*)
514 (make-list count :initial-element *universal-type*)
515 (collect ((res))
516 (flet ((process-types (types)
517 (loop for type in types
518 while (plusp count)
519 do (decf count)
520 do (res type))))
521 (process-types (values-type-required type))
522 (process-types (values-type-optional type))
523 (when (plusp count)
524 (loop with rest = (the ctype (values-type-rest type))
525 repeat count
526 do (res rest))))
527 (res))))
529 ;;; types of variable in (m-v-bind (v_1 ... v_n) (the <type> ...
530 (defun values-type-in (type count)
531 (declare (type ctype type) (type unsigned-byte count))
532 (if (eq type *wild-type*)
533 (make-list count :initial-element *universal-type*)
534 (collect ((res))
535 (let ((null-type (specifier-type 'null)))
536 (loop for type in (values-type-required type)
537 while (plusp count)
538 do (decf count)
539 do (res type))
540 (loop for type in (values-type-optional type)
541 while (plusp count)
542 do (decf count)
543 do (res (type-union type null-type)))
544 (when (plusp count)
545 (loop with rest = (acond ((values-type-rest type)
546 (type-union it null-type))
547 (t null-type))
548 repeat count
549 do (res rest))))
550 (res))))
552 ;;; Return a list of OPERATION applied to the types in TYPES1 and
553 ;;; TYPES2, padding with REST2 as needed. TYPES1 must not be shorter
554 ;;; than TYPES2. The second value is T if OPERATION always returned a
555 ;;; true second value.
556 (defun fixed-values-op (types1 types2 rest2 operation)
557 (declare (list types1 types2) (type ctype rest2) (type function operation))
558 (let ((exact t))
559 (values (mapcar (lambda (t1 t2)
560 (multiple-value-bind (res win)
561 (funcall operation t1 t2)
562 (unless win
563 (setq exact nil))
564 res))
565 types1
566 (append types2
567 (make-list (- (length types1) (length types2))
568 :initial-element rest2)))
569 exact)))
571 ;;; If TYPE isn't a values type, then make it into one.
572 (defun-cached (%coerce-to-values :hash-bits 8 :hash-function #'type-hash-value)
573 ((type eq))
574 (cond ((multiple-value-bind (res sure)
575 (csubtypep (specifier-type 'null) type)
576 (and (not res) sure))
577 ;; FIXME: What should we do with (NOT SURE)?
578 (make-values-type :required (list type) :rest *universal-type*))
580 (make-values-type :optional (list type) :rest *universal-type*))))
582 (defun coerce-to-values (type)
583 (declare (type ctype type))
584 (cond ((or (eq type *universal-type*)
585 (eq type *wild-type*))
586 *wild-type*)
587 ((values-type-p type)
588 type)
589 (t (%coerce-to-values type))))
591 ;;; Return type, corresponding to ANSI short form of VALUES type
592 ;;; specifier.
593 (defun make-short-values-type (types)
594 (declare (list types))
595 (let ((last-required (position-if
596 (lambda (type)
597 (not/type (csubtypep (specifier-type 'null) type)))
598 types
599 :from-end t)))
600 (if last-required
601 (make-values-type :required (subseq types 0 (1+ last-required))
602 :optional (subseq types (1+ last-required))
603 :rest *universal-type*)
604 (make-values-type :optional types :rest *universal-type*))))
606 (defun make-single-value-type (type)
607 (make-values-type :required (list type)))
609 ;;; Do the specified OPERATION on TYPE1 and TYPE2, which may be any
610 ;;; type, including VALUES types. With VALUES types such as:
611 ;;; (VALUES a0 a1)
612 ;;; (VALUES b0 b1)
613 ;;; we compute the more useful result
614 ;;; (VALUES (<operation> a0 b0) (<operation> a1 b1))
615 ;;; rather than the precise result
616 ;;; (<operation> (values a0 a1) (values b0 b1))
617 ;;; This has the virtue of always keeping the VALUES type specifier
618 ;;; outermost, and retains all of the information that is really
619 ;;; useful for static type analysis. We want to know what is always
620 ;;; true of each value independently. It is worthless to know that if
621 ;;; the first value is B0 then the second will be B1.
623 ;;; If the VALUES count signatures differ, then we produce a result with
624 ;;; the required VALUE count chosen by NREQ when applied to the number
625 ;;; of required values in TYPE1 and TYPE2. Any &KEY values become
626 ;;; &REST T (anyone who uses keyword values deserves to lose.)
628 ;;; The second value is true if the result is definitely empty or if
629 ;;; OPERATION returned true as its second value each time we called
630 ;;; it. Since we approximate the intersection of VALUES types, the
631 ;;; second value being true doesn't mean the result is exact.
632 (defun args-type-op (type1 type2 operation nreq)
633 (declare (type ctype type1 type2)
634 (type function operation nreq))
635 (when (eq type1 type2)
636 (values type1 t))
637 (multiple-value-bind (types1 rest1)
638 (values-type-types type1)
639 (multiple-value-bind (types2 rest2)
640 (values-type-types type2)
641 (multiple-value-bind (rest rest-exact)
642 (funcall operation rest1 rest2)
643 (multiple-value-bind (res res-exact)
644 (if (< (length types1) (length types2))
645 (fixed-values-op types2 types1 rest1 operation)
646 (fixed-values-op types1 types2 rest2 operation))
647 (let* ((req (funcall nreq
648 (length (args-type-required type1))
649 (length (args-type-required type2))))
650 (required (subseq res 0 req))
651 (opt (subseq res req)))
652 (values required opt rest
653 (and rest-exact res-exact))))))))
655 (defun values-type-op (type1 type2 operation nreq)
656 (multiple-value-bind (required optional rest exactp)
657 (args-type-op type1 type2 operation nreq)
658 (values (make-values-type :required required
659 :optional optional
660 :rest rest)
661 exactp)))
663 (defun compare-key-args (type1 type2)
664 (let ((keys1 (args-type-keywords type1))
665 (keys2 (args-type-keywords type2)))
666 (and (= (length keys1) (length keys2))
667 (eq (args-type-allowp type1)
668 (args-type-allowp type2))
669 (loop for key1 in keys1
670 for match = (find (key-info-name key1)
671 keys2 :key #'key-info-name)
672 always (and match
673 (type= (key-info-type key1)
674 (key-info-type match)))))))
676 (defun type=-args (type1 type2)
677 (macrolet ((compare (comparator field)
678 (let ((reader (symbolicate '#:args-type- field)))
679 `(,comparator (,reader type1) (,reader type2)))))
680 (and/type
681 (cond ((null (args-type-rest type1))
682 (values (null (args-type-rest type2)) t))
683 ((null (args-type-rest type2))
684 (values nil t))
686 (compare type= rest)))
687 (and/type (and/type (compare type=-list required)
688 (compare type=-list optional))
689 (if (or (args-type-keyp type1) (args-type-keyp type2))
690 (values (compare-key-args type1 type2) t)
691 (values t t))))))
693 ;;; Do a union or intersection operation on types that might be values
694 ;;; types. The result is optimized for utility rather than exactness,
695 ;;; but it is guaranteed that it will be no smaller (more restrictive)
696 ;;; than the precise result.
698 ;;; The return convention seems to be analogous to
699 ;;; TYPES-EQUAL-OR-INTERSECT. -- WHN 19990910.
700 (defun-cached (values-type-union :hash-function #'type-cache-hash
701 :hash-bits 8)
702 ((type1 eq) (type2 eq))
703 (declare (type ctype type1 type2))
704 (cond ((or (eq type1 *wild-type*) (eq type2 *wild-type*)) *wild-type*)
705 ((eq type1 *empty-type*) type2)
706 ((eq type2 *empty-type*) type1)
708 (values (values-type-op type1 type2 #'type-union #'min)))))
710 (defun-cached (values-type-intersection :hash-function #'type-cache-hash
711 :hash-bits 8)
712 ((type1 eq) (type2 eq))
713 (declare (type ctype type1 type2))
714 (cond ((eq type1 *wild-type*)
715 (coerce-to-values type2))
716 ((or (eq type2 *wild-type*) (eq type2 *universal-type*))
717 type1)
718 ((or (eq type1 *empty-type*) (eq type2 *empty-type*))
719 *empty-type*)
720 ((and (not (values-type-p type2))
721 (values-type-required type1))
722 (let ((req1 (values-type-required type1)))
723 (make-values-type :required (cons (type-intersection (first req1) type2)
724 (rest req1))
725 :optional (values-type-optional type1)
726 :rest (values-type-rest type1)
727 :allowp (values-type-allowp type1))))
729 (values (values-type-op type1 (coerce-to-values type2)
730 #'type-intersection
731 #'max)))))
733 ;;; This is like TYPES-EQUAL-OR-INTERSECT, except that it sort of
734 ;;; works on VALUES types. Note that due to the semantics of
735 ;;; VALUES-TYPE-INTERSECTION, this might return (VALUES T T) when
736 ;;; there isn't really any intersection.
737 (defun values-types-equal-or-intersect (type1 type2)
738 (cond ((or (eq type1 *empty-type*) (eq type2 *empty-type*))
739 (values t t))
740 ((or (eq type1 *wild-type*) (eq type2 *wild-type*))
741 (values t t))
743 (let ((res (values-type-intersection type1 type2)))
744 (values (not (eq res *empty-type*))
745 t)))))
747 ;;; a SUBTYPEP-like operation that can be used on any types, including
748 ;;; VALUES types
749 (defun-cached (values-subtypep :hash-function #'type-cache-hash
750 :hash-bits 8
751 :values 2)
752 ((type1 eq) (type2 eq))
753 (declare (type ctype type1 type2))
754 (cond ((or (eq type2 *wild-type*) (eq type2 *universal-type*)
755 (eq type1 *empty-type*))
756 (values t t))
757 ((eq type1 *wild-type*)
758 (values (eq type2 *wild-type*) t))
759 ((or (eq type2 *empty-type*)
760 (not (values-types-equal-or-intersect type1 type2)))
761 (values nil t))
762 ((and (not (values-type-p type2))
763 (values-type-required type1))
764 (csubtypep (first (values-type-required type1))
765 type2))
766 (t (setq type2 (coerce-to-values type2))
767 (multiple-value-bind (types1 rest1) (values-type-types type1)
768 (multiple-value-bind (types2 rest2) (values-type-types type2)
769 (cond ((< (length (values-type-required type1))
770 (length (values-type-required type2)))
771 (values nil t))
772 ((< (length types1) (length types2))
773 (values nil nil))
775 (do ((t1 types1 (rest t1))
776 (t2 types2 (rest t2)))
777 ((null t2)
778 (csubtypep rest1 rest2))
779 (multiple-value-bind (res win-p)
780 (csubtypep (first t1) (first t2))
781 (unless win-p
782 (return (values nil nil)))
783 (unless res
784 (return (values nil t))))))))))))
786 ;;;; type method interfaces
788 ;;; like SUBTYPEP, only works on CTYPE structures
789 (defun-cached (csubtypep :hash-function #'type-cache-hash
790 :hash-bits 10
791 :memoizer memoize
792 :values 2)
793 ((type1 eq) (type2 eq))
794 (declare (type ctype type1 type2))
795 (cond ((or (eq type1 type2)
796 (eq type1 *empty-type*)
797 (eq type2 *universal-type*))
798 (values t t))
799 #+nil
800 ((eq type1 *universal-type*)
801 (values nil t))
803 (memoize
804 (!invoke-type-method :simple-subtypep :complex-subtypep-arg2
805 type1 type2
806 :complex-arg1 :complex-subtypep-arg1)))))
808 ;;; Just parse the type specifiers and call CSUBTYPE.
809 (defun sb!xc:subtypep (type1 type2 &optional environment)
810 #!+sb-doc
811 "Return two values indicating the relationship between type1 and type2.
812 If values are T and T, type1 definitely is a subtype of type2.
813 If values are NIL and T, type1 definitely is not a subtype of type2.
814 If values are NIL and NIL, it couldn't be determined."
815 (declare (ignore environment))
816 (csubtypep (specifier-type type1) (specifier-type type2)))
818 ;;; If two types are definitely equivalent, return true. The second
819 ;;; value indicates whether the first value is definitely correct.
820 ;;; This should only fail in the presence of HAIRY types.
821 (defun-cached (type= :hash-function #'type-cache-hash
822 :hash-bits 11
823 :memoizer memoize
824 :values 2)
825 ((type1 eq) (type2 eq))
826 (declare (type ctype type1 type2))
827 (cond ((eq type1 type2)
828 (values t t))
829 ;; If args are not EQ, but both allow TYPE= optimization,
830 ;; and at least one is interned, then return no and certainty.
831 ((and (minusp (logior (type-hash-value type1) (type-hash-value type2)))
832 (logtest (logand (type-hash-value type1) (type-hash-value type2))
833 +type-admits-type=-optimization+))
834 (values nil t))
836 (memoize (!invoke-type-method :simple-= :complex-= type1 type2)))))
838 ;;; Not exactly the negation of TYPE=, since when the relationship is
839 ;;; uncertain, we still return NIL, NIL. This is useful in cases where
840 ;;; the conservative assumption is =.
841 (defun type/= (type1 type2)
842 (declare (type ctype type1 type2))
843 (multiple-value-bind (res win) (type= type1 type2)
844 (if win
845 (values (not res) t)
846 (values nil nil))))
848 ;;; the type method dispatch case of TYPE-UNION2
849 (defun %type-union2 (type1 type2)
850 ;; As in %TYPE-INTERSECTION2, it seems to be a good idea to give
851 ;; both argument orders a chance at COMPLEX-INTERSECTION2. Unlike
852 ;; %TYPE-INTERSECTION2, though, I don't have a specific case which
853 ;; demonstrates this is actually necessary. Also unlike
854 ;; %TYPE-INTERSECTION2, there seems to be no need to distinguish
855 ;; between not finding a method and having a method return NIL.
856 (flet ((1way (x y)
857 (!invoke-type-method :simple-union2 :complex-union2
859 :default nil)))
860 (declare (inline 1way))
861 (or (1way type1 type2)
862 (1way type2 type1))))
864 ;;; Find a type which includes both types. Any inexactness is
865 ;;; represented by the fuzzy element types; we return a single value
866 ;;; that is precise to the best of our knowledge. This result is
867 ;;; simplified into the canonical form, thus is not a UNION-TYPE
868 ;;; unless we find no other way to represent the result.
869 (defun-cached (type-union2 :hash-function #'type-cache-hash
870 :hash-bits 11
871 :memoizer memoize)
872 ((type1 eq) (type2 eq))
873 ;; KLUDGE: This was generated from TYPE-INTERSECTION2 by Ye Olde Cut And
874 ;; Paste technique of programming. If it stays around (as opposed to
875 ;; e.g. fading away in favor of some CLOS solution) the shared logic
876 ;; should probably become shared code. -- WHN 2001-03-16
877 (declare (type ctype type1 type2))
878 (let ((t2 nil))
879 (if (eq type1 type2)
880 type1
881 (memoize
882 (cond
883 ;; CSUBTYPEP for array-types answers questions about the
884 ;; specialized type, yet for union we want to take the
885 ;; expressed type in account too.
886 ((and (not (and (array-type-p type1) (array-type-p type2)))
887 (or (setf t2 (csubtypep type1 type2))
888 (csubtypep type2 type1)))
889 (if t2 type2 type1))
890 ((or (union-type-p type1)
891 (union-type-p type2))
892 ;; Unions of UNION-TYPE should have the UNION-TYPE-TYPES
893 ;; values broken out and united separately. The full TYPE-UNION
894 ;; function knows how to do this, so let it handle it.
895 (type-union type1 type2))
897 ;; the ordinary case: we dispatch to type methods
898 (%type-union2 type1 type2)))))))
900 ;;; the type method dispatch case of TYPE-INTERSECTION2
901 (defun %type-intersection2 (type1 type2)
902 ;; We want to give both argument orders a chance at
903 ;; COMPLEX-INTERSECTION2. Without that, the old CMU CL type
904 ;; methods could give noncommutative results, e.g.
905 ;; (TYPE-INTERSECTION2 *EMPTY-TYPE* SOME-HAIRY-TYPE)
906 ;; => NIL, NIL
907 ;; (TYPE-INTERSECTION2 SOME-HAIRY-TYPE *EMPTY-TYPE*)
908 ;; => #<NAMED-TYPE NIL>, T
909 ;; We also need to distinguish between the case where we found a
910 ;; type method, and it returned NIL, and the case where we fell
911 ;; through without finding any type method. An example of the first
912 ;; case is the intersection of a HAIRY-TYPE with some ordinary type.
913 ;; An example of the second case is the intersection of two
914 ;; completely-unrelated types, e.g. CONS and NUMBER, or SYMBOL and
915 ;; ARRAY.
917 ;; (Why yes, CLOS probably *would* be nicer..)
918 (flet ((1way (x y)
919 (!invoke-type-method :simple-intersection2 :complex-intersection2
921 :default :call-other-method)))
922 (declare (inline 1way))
923 (let ((xy (1way type1 type2)))
924 (or (and (not (eql xy :call-other-method)) xy)
925 (let ((yx (1way type2 type1)))
926 (or (and (not (eql yx :call-other-method)) yx)
927 (cond ((and (eql xy :call-other-method)
928 (eql yx :call-other-method))
929 *empty-type*)
931 nil))))))))
933 (defun-cached (type-intersection2 :hash-function #'type-cache-hash
934 :hash-bits 11
935 :memoizer memoize
936 :values 1)
937 ((type1 eq) (type2 eq))
938 (declare (type ctype type1 type2))
939 (if (eq type1 type2)
940 ;; FIXME: For some reason, this doesn't catch e.g. type1 =
941 ;; type2 = (SPECIFIER-TYPE
942 ;; 'SOME-UNKNOWN-TYPE). Investigate. - CSR, 2002-04-10
943 type1
944 (memoize
945 (cond
946 ((or (intersection-type-p type1)
947 (intersection-type-p type2))
948 ;; Intersections of INTERSECTION-TYPE should have the
949 ;; INTERSECTION-TYPE-TYPES values broken out and intersected
950 ;; separately. The full TYPE-INTERSECTION function knows how
951 ;; to do that, so let it handle it.
952 (type-intersection type1 type2))
954 ;; the ordinary case: we dispatch to type methods
955 (%type-intersection2 type1 type2))))))
957 ;;; Return as restrictive and simple a type as we can discover that is
958 ;;; no more restrictive than the intersection of TYPE1 and TYPE2. At
959 ;;; worst, we arbitrarily return one of the arguments as the first
960 ;;; value (trying not to return a hairy type).
961 (defun type-approx-intersection2 (type1 type2)
962 (cond ((type-intersection2 type1 type2))
963 ((hairy-type-p type1) type2)
964 (t type1)))
966 ;;; a test useful for checking whether a derived type matches a
967 ;;; declared type
969 ;;; The first value is true unless the types don't intersect and
970 ;;; aren't equal. The second value is true if the first value is
971 ;;; definitely correct. NIL is considered to intersect with any type.
972 ;;; If T is a subtype of either type, then we also return T, T. This
973 ;;; way we recognize that hairy types might intersect with T.
975 ;;; Well now given the statement above that this is "useful for ..."
976 ;;; a particular thing, I see how treating *empty-type* magically could
977 ;;; be useful, however given all the _other_ calls to this function within
978 ;;; this file, it seems suboptimal, because logically it is wrong.
979 (defun types-equal-or-intersect (type1 type2)
980 (declare (type ctype type1 type2))
981 (if (or (eq type1 *empty-type*) (eq type2 *empty-type*))
982 (values t t)
983 (let ((intersection2 (type-intersection2 type1 type2)))
984 (cond ((not intersection2)
985 (if (or (csubtypep *universal-type* type1)
986 (csubtypep *universal-type* type2))
987 (values t t)
988 (values t nil)))
989 ((eq intersection2 *empty-type*) (values nil t))
990 (t (values t t))))))
992 ;;; Return a Common Lisp type specifier corresponding to the TYPE
993 ;;; object.
994 (defun type-specifier (type)
995 (declare (type ctype type))
996 (funcall (type-class-unparse (type-class-info type)) type))
998 (defun-cached (type-negation :hash-function #'type-hash-value
999 :hash-bits 8
1000 :values 1)
1001 ((type eq))
1002 (declare (type ctype type))
1003 (funcall (type-class-negate (type-class-info type)) type))
1005 (defun-cached (type-singleton-p :hash-function #'type-hash-value
1006 :hash-bits 8
1007 :values 2)
1008 ((type eq))
1009 (declare (type ctype type))
1010 (let ((function (type-class-singleton-p (type-class-info type))))
1011 (if function
1012 (funcall function type)
1013 (values nil nil))))
1015 ;;; (VALUES-SPECIFIER-TYPE and SPECIFIER-TYPE moved from here to
1016 ;;; early-type.lisp by WHN ca. 19990201.)
1018 ;;; Take a list of type specifiers, computing the translation of each
1019 ;;; specifier and defining it as a builtin type.
1020 (declaim (ftype (function (list) (values)) !precompute-types))
1021 (defun !precompute-types (specs)
1022 (dolist (spec specs)
1023 (let ((res (specifier-type spec)))
1024 (unless (unknown-type-p res)
1025 (setf (info :type :builtin spec) res)
1026 (setf (info :type :kind spec) :primitive))))
1027 (values))
1029 ;;;; general TYPE-UNION and TYPE-INTERSECTION operations
1030 ;;;;
1031 ;;;; These are fully general operations on CTYPEs: they'll always
1032 ;;;; return a CTYPE representing the result.
1034 ;;; shared logic for unions and intersections: Return a list of
1035 ;;; types representing the same types as INPUT-TYPES, but with
1036 ;;; COMPOUND-TYPEs satisfying %COMPOUND-TYPE-P broken up into their
1037 ;;; component types, and with any SIMPLY2 simplifications applied.
1038 (macrolet
1039 ((def (name compound-type-p simplify2)
1040 `(defun ,name (types)
1041 (when types
1042 (multiple-value-bind (first rest)
1043 (if (,compound-type-p (car types))
1044 (values (car (compound-type-types (car types)))
1045 (append (cdr (compound-type-types (car types)))
1046 (cdr types)))
1047 (values (car types) (cdr types)))
1048 (let ((rest (,name rest)) u)
1049 (dolist (r rest (cons first rest))
1050 (when (setq u (,simplify2 first r))
1051 (return (,name (nsubstitute u r rest)))))))))))
1052 (def simplify-intersections intersection-type-p type-intersection2)
1053 (def simplify-unions union-type-p type-union2))
1055 (defun maybe-distribute-one-union (union-type types)
1056 (let* ((intersection (apply #'type-intersection types))
1057 (union (mapcar (lambda (x) (type-intersection x intersection))
1058 (union-type-types union-type))))
1059 (if (notany (lambda (x) (or (hairy-type-p x)
1060 (intersection-type-p x)))
1061 union)
1062 union
1063 nil)))
1065 (defun type-intersection (&rest input-types)
1066 (%type-intersection input-types))
1067 (defun-cached (%type-intersection :hash-bits 10 :hash-function #'type-list-cache-hash)
1068 ((input-types equal))
1069 (let ((simplified-types (simplify-intersections input-types)))
1070 (declare (type list simplified-types))
1071 ;; We want to have a canonical representation of types (or failing
1072 ;; that, punt to HAIRY-TYPE). Canonical representation would have
1073 ;; intersections inside unions but not vice versa, since you can
1074 ;; always achieve that by the distributive rule. But we don't want
1075 ;; to just apply the distributive rule, since it would be too easy
1076 ;; to end up with unreasonably huge type expressions. So instead
1077 ;; we try to generate a simple type by distributing the union; if
1078 ;; the type can't be made simple, we punt to HAIRY-TYPE.
1079 (if (and (cdr simplified-types) (some #'union-type-p simplified-types))
1080 (let* ((first-union (find-if #'union-type-p simplified-types))
1081 (other-types (coerce (remove first-union simplified-types)
1082 'list))
1083 (distributed (maybe-distribute-one-union first-union
1084 other-types)))
1085 (if distributed
1086 (apply #'type-union distributed)
1087 (%make-hairy-type `(and ,@(map 'list #'type-specifier
1088 simplified-types)))))
1089 (cond
1090 ((null simplified-types) *universal-type*)
1091 ((null (cdr simplified-types)) (car simplified-types))
1092 (t (%make-intersection-type
1093 (some #'type-enumerable simplified-types)
1094 simplified-types))))))
1096 (defun type-union (&rest input-types)
1097 (%type-union input-types))
1098 (defun-cached (%type-union :hash-bits 8 :hash-function #'type-list-cache-hash)
1099 ((input-types equal))
1100 (let ((simplified-types (simplify-unions input-types)))
1101 (cond
1102 ((null simplified-types) *empty-type*)
1103 ((null (cdr simplified-types)) (car simplified-types))
1104 (t (make-union-type
1105 (every #'type-enumerable simplified-types)
1106 simplified-types)))))
1108 ;;;; built-in types
1110 (!define-type-class named :enumerable nil :might-contain-other-types nil)
1112 ;; This is used when parsing (SATISFIES KEYWORDP)
1113 ;; so that simplifications can be made when computing intersections,
1114 ;; without which we would see this kind of "empty-type in disguise"
1115 ;; (AND (SATISFIES KEYWORDP) CONS)
1116 ;; This isn't *keyword-type* because KEYWORD is implemented
1117 ;; as the intersection of SYMBOL and (SATISFIES KEYWORDP)
1118 ;; We could also intern the KEYWORD type but that would require
1119 ;; hacking the INTERSECTION logic.
1120 (defglobal *satisfies-keywordp-type* -1)
1122 ;; Here too I discovered more than 1000 instances in a particular
1123 ;; Lisp image, when really this is *EMPTY-TYPE*.
1124 ;; (AND (SATISFIES LEGAL-FUN-NAME-P) (SIMPLE-ARRAY CHARACTER (*)))
1125 (defglobal *fun-name-type* -1)
1127 ;; !LATE-TYPE-COLD-INIT can't be GCd - there are lambdas in the toplevel code
1128 ;; component that leak out and persist - but everything below is GCable.
1129 ;; This leads to about 20KB of extra code being retained on x86-64.
1130 ;; An educated guess is that DEFINE-SUPERCLASSES is responsible for the problem.
1131 (defun !late-type-cold-init2 ()
1132 (macrolet ((frob (name var)
1133 `(progn
1134 (setq ,var
1135 (mark-ctype-interned (make-named-type :name ',name)))
1136 (setf (info :type :kind ',name) :primitive)
1137 (setf (info :type :builtin ',name) ,var))))
1138 ;; KLUDGE: In ANSI, * isn't really the name of a type, it's just a
1139 ;; special symbol which can be stuck in some places where an
1140 ;; ordinary type can go, e.g. (ARRAY * 1) instead of (ARRAY T 1).
1141 ;; In SBCL it also used to denote universal VALUES type.
1142 (frob * *wild-type*)
1143 (frob nil *empty-type*)
1144 (frob t *universal-type*)
1145 (setf (sb!c::meta-info-default (sb!c::meta-info :variable :type))
1146 *universal-type*)
1147 ;; new in sbcl-0.9.5: these used to be CLASSOID types, but that
1148 ;; view of them was incompatible with requirements on the MOP
1149 ;; metaobject class hierarchy: the INSTANCE and
1150 ;; FUNCALLABLE-INSTANCE types are disjoint (instances have
1151 ;; instance-pointer-lowtag; funcallable-instances have
1152 ;; fun-pointer-lowtag), while FUNCALLABLE-STANDARD-OBJECT is
1153 ;; required to be a subclass of STANDARD-OBJECT. -- CSR,
1154 ;; 2005-09-09
1155 (frob instance *instance-type*)
1156 (frob funcallable-instance *funcallable-instance-type*)
1157 ;; new in sbcl-1.0.3.3: necessary to act as a join point for the
1158 ;; extended sequence hierarchy. (Might be removed later if we use
1159 ;; a dedicated FUNDAMENTAL-SEQUENCE class for this.)
1160 (frob extended-sequence *extended-sequence-type*))
1161 (!intern-important-fun-type-instances)
1162 (!intern-important-member-type-instances)
1163 (!intern-important-cons-type-instances)
1164 (!intern-important-numeric-type-instances)
1165 (!intern-important-character-set-type-instances)
1166 (!intern-important-array-type-instances) ; must be after numeric and char
1167 (setf *satisfies-keywordp-type*
1168 (mark-ctype-interned (%make-hairy-type '(satisfies keywordp))))
1169 (setf *fun-name-type*
1170 (mark-ctype-interned (%make-hairy-type '(satisfies legal-fun-name-p))))
1171 ;; This is not an important type- no attempt is made to return exactly this
1172 ;; object when parsing FUNCTION. In fact we return the classoid instead
1173 (setf *universal-fun-type*
1174 (make-fun-type :wild-args t :returns *wild-type*)))
1176 (!define-type-method (named :simple-=) (type1 type2)
1177 ;;(aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1178 (values (eq type1 type2) t))
1180 (defun cons-type-might-be-empty-type (type)
1181 (declare (type cons-type type))
1182 (let ((car-type (cons-type-car-type type))
1183 (cdr-type (cons-type-cdr-type type)))
1185 (if (cons-type-p car-type)
1186 (cons-type-might-be-empty-type car-type)
1187 (multiple-value-bind (yes surep)
1188 (type= car-type *empty-type*)
1189 (aver (not yes))
1190 (not surep)))
1191 (if (cons-type-p cdr-type)
1192 (cons-type-might-be-empty-type cdr-type)
1193 (multiple-value-bind (yes surep)
1194 (type= cdr-type *empty-type*)
1195 (aver (not yes))
1196 (not surep))))))
1198 (!define-type-method (named :complex-=) (type1 type2)
1199 (cond
1200 ((and (eq type2 *empty-type*)
1201 (or (and (intersection-type-p type1)
1202 ;; not allowed to be unsure on these... FIXME: keep
1203 ;; the list of CL types that are intersection types
1204 ;; once and only once.
1205 (not (or (type= type1 (specifier-type 'ratio))
1206 (type= type1 (specifier-type 'keyword)))))
1207 (and (cons-type-p type1)
1208 (cons-type-might-be-empty-type type1))))
1209 ;; things like (AND (EQL 0) (SATISFIES ODDP)) or (AND FUNCTION
1210 ;; STREAM) can get here. In general, we can't really tell
1211 ;; whether these are equal to NIL or not, so
1212 (values nil nil))
1213 ((type-might-contain-other-types-p type1)
1214 (invoke-complex-=-other-method type1 type2))
1215 (t (values nil t))))
1217 (!define-type-method (named :simple-subtypep) (type1 type2)
1218 (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1219 (aver (not (eq type1 type2)))
1220 (values (or (eq type1 *empty-type*)
1221 (eq type2 *wild-type*)
1222 (eq type2 *universal-type*)) t))
1224 (!define-type-method (named :complex-subtypep-arg1) (type1 type2)
1225 ;; This AVER causes problems if we write accurate methods for the
1226 ;; union (and possibly intersection) types which then delegate to
1227 ;; us; while a user shouldn't get here, because of the odd status of
1228 ;; *wild-type* a type-intersection executed by the compiler can. -
1229 ;; CSR, 2002-04-10
1231 ;; (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1232 (cond ((eq type1 *empty-type*)
1234 (;; When TYPE2 might be the universal type in disguise
1235 (type-might-contain-other-types-p type2)
1236 ;; Now that the UNION and HAIRY COMPLEX-SUBTYPEP-ARG2 methods
1237 ;; can delegate to us (more or less as CALL-NEXT-METHOD) when
1238 ;; they're uncertain, we can't just barf on COMPOUND-TYPE and
1239 ;; HAIRY-TYPEs as we used to. Instead we deal with the
1240 ;; problem (where at least part of the problem is cases like
1241 ;; (SUBTYPEP T '(SATISFIES FOO))
1242 ;; or
1243 ;; (SUBTYPEP T '(AND (SATISFIES FOO) (SATISFIES BAR)))
1244 ;; where the second type is a hairy type like SATISFIES, or
1245 ;; is a compound type which might contain a hairy type) by
1246 ;; returning uncertainty.
1247 (values nil nil))
1248 ((eq type1 *funcallable-instance-type*)
1249 (values (eq type2 (specifier-type 'function)) t))
1251 ;; This case would have been picked off by the SIMPLE-SUBTYPEP
1252 ;; method, and so shouldn't appear here.
1253 (aver (not (named-type-p type2)))
1254 ;; Since TYPE2 is not EQ *UNIVERSAL-TYPE* and is not another
1255 ;; named type in disguise, TYPE2 is not a superset of TYPE1.
1256 (values nil t))))
1258 (!define-type-method (named :complex-subtypep-arg2) (type1 type2)
1259 (aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1260 (cond ((eq type2 *universal-type*)
1261 (values t t))
1262 ;; some CONS types can conceal danger
1263 ((and (cons-type-p type1) (cons-type-might-be-empty-type type1))
1264 (values nil nil))
1265 ((type-might-contain-other-types-p type1)
1266 ;; those types can be other types in disguise. So we'd
1267 ;; better delegate.
1268 (invoke-complex-subtypep-arg1-method type1 type2))
1269 ((and (or (eq type2 *instance-type*)
1270 (eq type2 *funcallable-instance-type*))
1271 (member-type-p type1))
1272 ;; member types can be subtypep INSTANCE and
1273 ;; FUNCALLABLE-INSTANCE in surprising ways.
1274 (invoke-complex-subtypep-arg1-method type1 type2))
1275 ((and (eq type2 *extended-sequence-type*) (classoid-p type1))
1276 (let* ((layout (classoid-layout type1))
1277 (inherits (layout-inherits layout))
1278 (sequencep (find (classoid-layout (find-classoid 'sequence))
1279 inherits)))
1280 (values (if sequencep t nil) t)))
1281 ((and (eq type2 *instance-type*) (classoid-p type1))
1282 (if (member type1 *non-instance-classoid-types* :key #'find-classoid)
1283 (values nil t)
1284 (let* ((layout (classoid-layout type1))
1285 (inherits (layout-inherits layout))
1286 (functionp (find (classoid-layout (find-classoid 'function))
1287 inherits)))
1288 (cond
1289 (functionp
1290 (values nil t))
1291 ((eq type1 (find-classoid 'function))
1292 (values nil t))
1293 ((or (structure-classoid-p type1)
1294 #+nil
1295 (condition-classoid-p type1))
1296 (values t t))
1297 (t (values nil nil))))))
1298 ((and (eq type2 *funcallable-instance-type*) (classoid-p type1))
1299 (if (member type1 *non-instance-classoid-types* :key #'find-classoid)
1300 (values nil t)
1301 (let* ((layout (classoid-layout type1))
1302 (inherits (layout-inherits layout))
1303 (functionp (find (classoid-layout (find-classoid 'function))
1304 inherits)))
1305 (values (if functionp t nil) t))))
1307 ;; FIXME: This seems to rely on there only being 4 or 5
1308 ;; NAMED-TYPE values, and the exclusion of various
1309 ;; possibilities above. It would be good to explain it and/or
1310 ;; rewrite it so that it's clearer.
1311 (values nil t))))
1313 (!define-type-method (named :complex-intersection2) (type1 type2)
1314 ;; FIXME: This assertion failed when I added it in sbcl-0.6.11.13.
1315 ;; Perhaps when bug 85 is fixed it can be reenabled.
1316 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1317 (cond
1318 ((eq type2 *extended-sequence-type*)
1319 (typecase type1
1320 (structure-classoid *empty-type*)
1321 (classoid
1322 (if (member type1 *non-instance-classoid-types* :key #'find-classoid)
1323 *empty-type*
1324 (if (find (classoid-layout (find-classoid 'sequence))
1325 (layout-inherits (classoid-layout type1)))
1326 type1
1327 nil)))
1329 (if (or (type-might-contain-other-types-p type1)
1330 (member-type-p type1))
1332 *empty-type*))))
1333 ((eq type2 *instance-type*)
1334 (typecase type1
1335 (structure-classoid type1)
1336 (classoid
1337 (if (and (not (member type1 *non-instance-classoid-types*
1338 :key #'find-classoid))
1339 (not (eq type1 (find-classoid 'function)))
1340 (not (find (classoid-layout (find-classoid 'function))
1341 (layout-inherits (classoid-layout type1)))))
1343 *empty-type*))
1345 (if (or (type-might-contain-other-types-p type1)
1346 (member-type-p type1))
1348 *empty-type*))))
1349 ((eq type2 *funcallable-instance-type*)
1350 (typecase type1
1351 (structure-classoid *empty-type*)
1352 (classoid
1353 (if (member type1 *non-instance-classoid-types* :key #'find-classoid)
1354 *empty-type*
1355 (if (find (classoid-layout (find-classoid 'function))
1356 (layout-inherits (classoid-layout type1)))
1357 type1
1358 (if (type= type1 (find-classoid 'function))
1359 type2
1360 nil))))
1361 (fun-type nil)
1363 (if (or (type-might-contain-other-types-p type1)
1364 (member-type-p type1))
1366 *empty-type*))))
1367 (t (hierarchical-intersection2 type1 type2))))
1369 (!define-type-method (named :complex-union2) (type1 type2)
1370 ;; Perhaps when bug 85 is fixed this can be reenabled.
1371 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1372 (cond
1373 ((eq type2 *extended-sequence-type*)
1374 (if (classoid-p type1)
1375 (if (or (member type1 *non-instance-classoid-types*
1376 :key #'find-classoid)
1377 (not (find (classoid-layout (find-classoid 'sequence))
1378 (layout-inherits (classoid-layout type1)))))
1380 type2)
1381 nil))
1382 ((eq type2 *instance-type*)
1383 (if (classoid-p type1)
1384 (if (or (member type1 *non-instance-classoid-types*
1385 :key #'find-classoid)
1386 (find (classoid-layout (find-classoid 'function))
1387 (layout-inherits (classoid-layout type1))))
1389 type2)
1390 nil))
1391 ((eq type2 *funcallable-instance-type*)
1392 (if (classoid-p type1)
1393 (if (or (member type1 *non-instance-classoid-types*
1394 :key #'find-classoid)
1395 (not (find (classoid-layout (find-classoid 'function))
1396 (layout-inherits (classoid-layout type1)))))
1398 (if (eq type1 (specifier-type 'function))
1399 type1
1400 type2))
1401 nil))
1402 (t (hierarchical-union2 type1 type2))))
1404 (!define-type-method (named :negate) (x)
1405 (aver (not (eq x *wild-type*)))
1406 (cond
1407 ((eq x *universal-type*) *empty-type*)
1408 ((eq x *empty-type*) *universal-type*)
1409 ((or (eq x *instance-type*)
1410 (eq x *funcallable-instance-type*)
1411 (eq x *extended-sequence-type*))
1412 (make-negation-type :type x))
1413 (t (bug "NAMED type unexpected: ~S" x))))
1415 (!define-type-method (named :unparse) (x)
1416 (named-type-name x))
1418 ;;;; hairy and unknown types
1419 ;;;; DEFINE-TYPE-CLASS HAIRY is in 'early-type'
1421 (!define-type-method (hairy :negate) (x)
1422 (make-negation-type :type x))
1424 (!define-type-method (hairy :unparse) (x)
1425 (hairy-type-specifier x))
1427 (!define-type-method (hairy :simple-subtypep) (type1 type2)
1428 (let ((hairy-spec1 (hairy-type-specifier type1))
1429 (hairy-spec2 (hairy-type-specifier type2)))
1430 (cond ((equal-but-no-car-recursion hairy-spec1 hairy-spec2)
1431 (values t t))
1432 ((maybe-reparse-specifier! type1)
1433 (csubtypep type1 type2))
1434 ((maybe-reparse-specifier! type2)
1435 (csubtypep type1 type2))
1437 (values nil nil)))))
1439 (!define-type-method (hairy :complex-subtypep-arg2) (type1 type2)
1440 (if (maybe-reparse-specifier! type2)
1441 (csubtypep type1 type2)
1442 (let ((specifier (hairy-type-specifier type2)))
1443 (cond ((and (consp specifier) (eql (car specifier) 'satisfies))
1444 (case (cadr specifier)
1445 ((keywordp) (if (type= type1 (specifier-type 'symbol))
1446 (values nil t)
1447 (invoke-complex-subtypep-arg1-method type1 type2)))
1448 (t (invoke-complex-subtypep-arg1-method type1 type2))))
1450 (invoke-complex-subtypep-arg1-method type1 type2))))))
1452 (!define-type-method (hairy :complex-subtypep-arg1) (type1 type2)
1453 (if (maybe-reparse-specifier! type1)
1454 (csubtypep type1 type2)
1455 (values nil nil)))
1457 (!define-type-method (hairy :complex-=) (type1 type2)
1458 (if (maybe-reparse-specifier! type2)
1459 (type= type1 type2)
1460 (values nil nil)))
1462 (!define-type-method (hairy :simple-intersection2 :complex-intersection2)
1463 (type1 type2)
1464 (cond ((type= type1 type2)
1465 type1)
1466 ((eq type2 *satisfies-keywordp-type*)
1467 ;; (AND (MEMBER A) (SATISFIES KEYWORDP)) is possibly non-empty
1468 ;; if A is re-homed as :A. However as a special case that really
1469 ;; does occur, (AND (MEMBER NIL) (SATISFIES KEYWORDP))
1470 ;; is empty because of the illegality of changing NIL's package.
1471 (if (eq type1 *null-type*)
1472 *empty-type*
1473 (multiple-value-bind (answer certain)
1474 (types-equal-or-intersect type1 (specifier-type 'symbol))
1475 (if (and (not answer) certain)
1476 *empty-type*
1477 nil))))
1478 ((eq type2 *fun-name-type*)
1479 (multiple-value-bind (answer certain)
1480 (types-equal-or-intersect type1 (specifier-type 'symbol))
1481 (if (and (not answer) certain)
1482 (multiple-value-bind (answer certain)
1483 (types-equal-or-intersect type1 (specifier-type 'cons))
1484 (if (and (not answer) certain)
1485 *empty-type*
1486 nil))
1487 nil)))
1488 (t nil)))
1490 (!define-type-method (hairy :simple-union2)
1491 (type1 type2)
1492 (if (type= type1 type2)
1493 type1
1494 nil))
1496 (!define-type-method (hairy :simple-=) (type1 type2)
1497 (if (equal-but-no-car-recursion (hairy-type-specifier type1)
1498 (hairy-type-specifier type2))
1499 (values t t)
1500 (values nil nil)))
1502 (!def-type-translator satisfies (&whole whole fun)
1503 (declare (ignore fun))
1504 ;; Check legality of arguments.
1505 (destructuring-bind (satisfies predicate-name) whole
1506 (declare (ignore satisfies))
1507 (unless (symbolp predicate-name)
1508 (error 'simple-type-error
1509 :datum predicate-name
1510 :expected-type 'symbol
1511 :format-control "The SATISFIES predicate name is not a symbol: ~S"
1512 :format-arguments (list predicate-name)))
1513 ;; Create object.
1514 (case predicate-name
1515 (keywordp *satisfies-keywordp-type*)
1516 (legal-fun-name-p *fun-name-type*)
1517 (t (%make-hairy-type whole)))))
1519 ;;;; negation types
1521 (!define-type-method (negation :negate) (x)
1522 (negation-type-type x))
1524 (!define-type-method (negation :unparse) (x)
1525 (if (type= (negation-type-type x) (specifier-type 'cons))
1526 'atom
1527 `(not ,(type-specifier (negation-type-type x)))))
1529 (!define-type-method (negation :simple-subtypep) (type1 type2)
1530 (csubtypep (negation-type-type type2) (negation-type-type type1)))
1532 (!define-type-method (negation :complex-subtypep-arg2) (type1 type2)
1533 (let* ((complement-type2 (negation-type-type type2))
1534 (intersection2 (type-intersection2 type1
1535 complement-type2)))
1536 (if intersection2
1537 ;; FIXME: if uncertain, maybe try arg1?
1538 (type= intersection2 *empty-type*)
1539 (invoke-complex-subtypep-arg1-method type1 type2))))
1541 (!define-type-method (negation :complex-subtypep-arg1) (type1 type2)
1542 ;; "Incrementally extended heuristic algorithms tend inexorably toward the
1543 ;; incomprehensible." -- http://www.unlambda.com/~james/lambda/lambda.txt
1545 ;; You may not believe this. I couldn't either. But then I sat down
1546 ;; and drew lots of Venn diagrams. Comments involving a and b refer
1547 ;; to the call (subtypep '(not a) 'b) -- CSR, 2002-02-27.
1548 (block nil
1549 ;; (Several logical truths in this block are true as long as
1550 ;; b/=T. As of sbcl-0.7.1.28, it seems impossible to construct a
1551 ;; case with b=T where we actually reach this type method, but
1552 ;; we'll test for and exclude this case anyway, since future
1553 ;; maintenance might make it possible for it to end up in this
1554 ;; code.)
1555 (multiple-value-bind (equal certain)
1556 (type= type2 *universal-type*)
1557 (unless certain
1558 (return (values nil nil)))
1559 (when equal
1560 (return (values t t))))
1561 (let ((complement-type1 (negation-type-type type1)))
1562 ;; Do the special cases first, in order to give us a chance if
1563 ;; subtype/supertype relationships are hairy.
1564 (multiple-value-bind (equal certain)
1565 (type= complement-type1 type2)
1566 ;; If a = b, ~a is not a subtype of b (unless b=T, which was
1567 ;; excluded above).
1568 (unless certain
1569 (return (values nil nil)))
1570 (when equal
1571 (return (values nil t))))
1572 ;; KLUDGE: ANSI requires that the SUBTYPEP result between any
1573 ;; two built-in atomic type specifiers never be uncertain. This
1574 ;; is hard to do cleanly for the built-in types whose
1575 ;; definitions include (NOT FOO), i.e. CONS and RATIO. However,
1576 ;; we can do it with this hack, which uses our global knowledge
1577 ;; that our implementation of the type system uses disjoint
1578 ;; implementation types to represent disjoint sets (except when
1579 ;; types are contained in other types). (This is a KLUDGE
1580 ;; because it's fragile. Various changes in internal
1581 ;; representation in the type system could make it start
1582 ;; confidently returning incorrect results.) -- WHN 2002-03-08
1583 (unless (or (type-might-contain-other-types-p complement-type1)
1584 (type-might-contain-other-types-p type2))
1585 ;; Because of the way our types which don't contain other
1586 ;; types are disjoint subsets of the space of possible values,
1587 ;; (SUBTYPEP '(NOT AA) 'B)=NIL when AA and B are simple (and B
1588 ;; is not T, as checked above).
1589 (return (values nil t)))
1590 ;; The old (TYPE= TYPE1 TYPE2) branch would never be taken, as
1591 ;; TYPE1 and TYPE2 will only be equal if they're both NOT types,
1592 ;; and then the :SIMPLE-SUBTYPEP method would be used instead.
1593 ;; But a CSUBTYPEP relationship might still hold:
1594 (multiple-value-bind (equal certain)
1595 (csubtypep complement-type1 type2)
1596 ;; If a is a subtype of b, ~a is not a subtype of b (unless
1597 ;; b=T, which was excluded above).
1598 (unless certain
1599 (return (values nil nil)))
1600 (when equal
1601 (return (values nil t))))
1602 (multiple-value-bind (equal certain)
1603 (csubtypep type2 complement-type1)
1604 ;; If b is a subtype of a, ~a is not a subtype of b. (FIXME:
1605 ;; That's not true if a=T. Do we know at this point that a is
1606 ;; not T?)
1607 (unless certain
1608 (return (values nil nil)))
1609 (when equal
1610 (return (values nil t))))
1611 ;; old CSR comment ca. 0.7.2, now obsoleted by the SIMPLE-CTYPE?
1612 ;; KLUDGE case above: Other cases here would rely on being able
1613 ;; to catch all possible cases, which the fragility of this type
1614 ;; system doesn't inspire me; for instance, if a is type= to ~b,
1615 ;; then we want T, T; if this is not the case and the types are
1616 ;; disjoint (have an intersection of *empty-type*) then we want
1617 ;; NIL, T; else if the union of a and b is the *universal-type*
1618 ;; then we want T, T. So currently we still claim to be unsure
1619 ;; about e.g. (subtypep '(not fixnum) 'single-float).
1621 ;; OTOH we might still get here:
1622 (values nil nil))))
1624 (!define-type-method (negation :complex-=) (type1 type2)
1625 ;; (NOT FOO) isn't equivalent to anything that's not a negation
1626 ;; type, except possibly a type that might contain it in disguise.
1627 (declare (ignore type2))
1628 (if (type-might-contain-other-types-p type1)
1629 (values nil nil)
1630 (values nil t)))
1632 (!define-type-method (negation :simple-intersection2) (type1 type2)
1633 (let ((not1 (negation-type-type type1))
1634 (not2 (negation-type-type type2)))
1635 (cond
1636 ((csubtypep not1 not2) type2)
1637 ((csubtypep not2 not1) type1)
1638 ;; Why no analagous clause to the disjoint in the SIMPLE-UNION2
1639 ;; method, below? The clause would read
1641 ;; ((EQ (TYPE-UNION NOT1 NOT2) *UNIVERSAL-TYPE*) *EMPTY-TYPE*)
1643 ;; but with proper canonicalization of negation types, there's
1644 ;; no way of constructing two negation types with union of their
1645 ;; negations being the universal type.
1647 (aver (not (eq (type-union not1 not2) *universal-type*)))
1648 nil))))
1650 (defun maybe-complex-array-refinement (type1 type2)
1651 (let* ((ntype (negation-type-type type2))
1652 (ndims (array-type-dimensions ntype))
1653 (ncomplexp (array-type-complexp ntype))
1654 (nseltype (array-type-specialized-element-type ntype))
1655 (neltype (array-type-element-type ntype)))
1656 (if (and (eql ndims '*) (null ncomplexp)
1657 (eql neltype *wild-type*) (eql nseltype *wild-type*))
1658 (make-array-type (array-type-dimensions type1)
1659 :complexp t
1660 :element-type (array-type-element-type type1)
1661 :specialized-element-type (array-type-specialized-element-type type1)))))
1663 (!define-type-method (negation :complex-intersection2) (type1 type2)
1664 (cond
1665 ((csubtypep type1 (negation-type-type type2)) *empty-type*)
1666 ((eq (type-intersection type1 (negation-type-type type2)) *empty-type*)
1667 type1)
1668 ((and (array-type-p type1) (array-type-p (negation-type-type type2)))
1669 (maybe-complex-array-refinement type1 type2))
1670 (t nil)))
1672 (!define-type-method (negation :simple-union2) (type1 type2)
1673 (let ((not1 (negation-type-type type1))
1674 (not2 (negation-type-type type2)))
1675 (cond
1676 ((csubtypep not1 not2) type1)
1677 ((csubtypep not2 not1) type2)
1678 ((eq (type-intersection not1 not2) *empty-type*)
1679 *universal-type*)
1680 (t nil))))
1682 (!define-type-method (negation :complex-union2) (type1 type2)
1683 (cond
1684 ((csubtypep (negation-type-type type2) type1) *universal-type*)
1685 ((eq (type-intersection type1 (negation-type-type type2)) *empty-type*)
1686 type2)
1687 (t nil)))
1689 (!define-type-method (negation :simple-=) (type1 type2)
1690 (type= (negation-type-type type1) (negation-type-type type2)))
1692 (!def-type-translator not (typespec)
1693 (type-negation (specifier-type typespec)))
1695 ;;;; numeric types
1697 (!define-type-class number :enumerable #'numeric-type-enumerable
1698 :might-contain-other-types nil)
1700 (declaim (inline numeric-type-equal))
1701 (defun numeric-type-equal (type1 type2)
1702 (and (eq (numeric-type-class type1) (numeric-type-class type2))
1703 (eq (numeric-type-format type1) (numeric-type-format type2))
1704 (eq (numeric-type-complexp type1) (numeric-type-complexp type2))))
1706 (!define-type-method (number :simple-=) (type1 type2)
1707 (values
1708 (and (numeric-type-equal type1 type2)
1709 (equalp (numeric-type-low type1) (numeric-type-low type2))
1710 (equalp (numeric-type-high type1) (numeric-type-high type2)))
1713 (!define-type-method (number :negate) (type)
1714 (if (and (null (numeric-type-low type)) (null (numeric-type-high type)))
1715 (make-negation-type :type type)
1716 (type-union
1717 (make-negation-type
1718 :type (modified-numeric-type type :low nil :high nil))
1719 (cond
1720 ((null (numeric-type-low type))
1721 (modified-numeric-type
1722 type
1723 :low (let ((h (numeric-type-high type)))
1724 (if (consp h) (car h) (list h)))
1725 :high nil))
1726 ((null (numeric-type-high type))
1727 (modified-numeric-type
1728 type
1729 :low nil
1730 :high (let ((l (numeric-type-low type)))
1731 (if (consp l) (car l) (list l)))))
1732 (t (type-union
1733 (modified-numeric-type
1734 type
1735 :low nil
1736 :high (let ((l (numeric-type-low type)))
1737 (if (consp l) (car l) (list l))))
1738 (modified-numeric-type
1739 type
1740 :low (let ((h (numeric-type-high type)))
1741 (if (consp h) (car h) (list h)))
1742 :high nil)))))))
1744 (!define-type-method (number :unparse) (type)
1745 (let* ((complexp (numeric-type-complexp type))
1746 (low (numeric-type-low type))
1747 (high (numeric-type-high type))
1748 (base (case (numeric-type-class type)
1749 (integer 'integer)
1750 (rational 'rational)
1751 (float (or (numeric-type-format type) 'float))
1752 (t 'real))))
1753 (let ((base+bounds
1754 (cond ((and (eq base 'integer) high low)
1755 (let ((high-count (logcount high))
1756 (high-length (integer-length high)))
1757 (cond ((= low 0)
1758 (cond ((= high 0) '(integer 0 0))
1759 ((= high 1) 'bit)
1760 ((and (= high-count high-length)
1761 (plusp high-length))
1762 `(unsigned-byte ,high-length))
1764 `(mod ,(1+ high)))))
1765 ((and (= low sb!xc:most-negative-fixnum)
1766 (= high sb!xc:most-positive-fixnum))
1767 'fixnum)
1768 ((and (= low (lognot high))
1769 (= high-count high-length)
1770 (> high-count 0))
1771 `(signed-byte ,(1+ high-length)))
1773 `(integer ,low ,high)))))
1774 (high `(,base ,(or low '*) ,high))
1775 (low
1776 (if (and (eq base 'integer) (= low 0))
1777 'unsigned-byte
1778 `(,base ,low)))
1779 (t base))))
1780 (ecase complexp
1781 (:real
1782 base+bounds)
1783 (:complex
1784 (aver (neq base+bounds 'real))
1785 `(complex ,base+bounds))
1786 ((nil)
1787 (aver (eq base+bounds 'real))
1788 'number)))))
1790 (!define-type-method (number :singleton-p) (type)
1791 (let ((low (numeric-type-low type))
1792 (high (numeric-type-high type)))
1793 (if (and low
1794 (eql low high)
1795 (eql (numeric-type-complexp type) :real)
1796 (member (numeric-type-class type) '(integer rational
1797 #-sb-xc-host float)))
1798 (values t (numeric-type-low type))
1799 (values nil nil))))
1801 ;;; Return true if X is "less than or equal" to Y, taking open bounds
1802 ;;; into consideration. CLOSED is the predicate used to test the bound
1803 ;;; on a closed interval (e.g. <=), and OPEN is the predicate used on
1804 ;;; open bounds (e.g. <). Y is considered to be the outside bound, in
1805 ;;; the sense that if it is infinite (NIL), then the test succeeds,
1806 ;;; whereas if X is infinite, then the test fails (unless Y is also
1807 ;;; infinite).
1809 ;;; This is for comparing bounds of the same kind, e.g. upper and
1810 ;;; upper. Use NUMERIC-BOUND-TEST* for different kinds of bounds.
1811 (defmacro numeric-bound-test (x y closed open)
1812 `(cond ((not ,y) t)
1813 ((not ,x) nil)
1814 ((consp ,x)
1815 (if (consp ,y)
1816 (,closed (car ,x) (car ,y))
1817 (,closed (car ,x) ,y)))
1819 (if (consp ,y)
1820 (,open ,x (car ,y))
1821 (,closed ,x ,y)))))
1823 ;;; This is used to compare upper and lower bounds. This is different
1824 ;;; from the same-bound case:
1825 ;;; -- Since X = NIL is -infinity, whereas y = NIL is +infinity, we
1826 ;;; return true if *either* arg is NIL.
1827 ;;; -- an open inner bound is "greater" and also squeezes the interval,
1828 ;;; causing us to use the OPEN test for those cases as well.
1829 (defmacro numeric-bound-test* (x y closed open)
1830 `(cond ((not ,y) t)
1831 ((not ,x) t)
1832 ((consp ,x)
1833 (if (consp ,y)
1834 (,open (car ,x) (car ,y))
1835 (,open (car ,x) ,y)))
1837 (if (consp ,y)
1838 (,open ,x (car ,y))
1839 (,closed ,x ,y)))))
1841 ;;; Return whichever of the numeric bounds X and Y is "maximal"
1842 ;;; according to the predicates CLOSED (e.g. >=) and OPEN (e.g. >).
1843 ;;; This is only meaningful for maximizing like bounds, i.e. upper and
1844 ;;; upper. If MAX-P is true, then we return NIL if X or Y is NIL,
1845 ;;; otherwise we return the other arg.
1846 (defmacro numeric-bound-max (x y closed open max-p)
1847 (once-only ((n-x x)
1848 (n-y y))
1849 `(cond ((not ,n-x) ,(if max-p nil n-y))
1850 ((not ,n-y) ,(if max-p nil n-x))
1851 ((consp ,n-x)
1852 (if (consp ,n-y)
1853 (if (,closed (car ,n-x) (car ,n-y)) ,n-x ,n-y)
1854 (if (,open (car ,n-x) ,n-y) ,n-x ,n-y)))
1856 (if (consp ,n-y)
1857 (if (,open (car ,n-y) ,n-x) ,n-y ,n-x)
1858 (if (,closed ,n-y ,n-x) ,n-y ,n-x))))))
1860 (!define-type-method (number :simple-subtypep) (type1 type2)
1861 (let ((class1 (numeric-type-class type1))
1862 (class2 (numeric-type-class type2))
1863 (complexp2 (numeric-type-complexp type2))
1864 (format2 (numeric-type-format type2))
1865 (low1 (numeric-type-low type1))
1866 (high1 (numeric-type-high type1))
1867 (low2 (numeric-type-low type2))
1868 (high2 (numeric-type-high type2)))
1869 ;; If one is complex and the other isn't, they are disjoint.
1870 (cond ((not (or (eq (numeric-type-complexp type1) complexp2)
1871 (null complexp2)))
1872 (values nil t))
1873 ;; If the classes are specified and different, the types are
1874 ;; disjoint unless type2 is RATIONAL and type1 is INTEGER.
1875 ;; [ or type1 is INTEGER and type2 is of the form (RATIONAL
1876 ;; X X) for integral X, but this is dealt with in the
1877 ;; canonicalization inside MAKE-NUMERIC-TYPE ]
1878 ((not (or (eq class1 class2)
1879 (null class2)
1880 (and (eq class1 'integer) (eq class2 'rational))))
1881 (values nil t))
1882 ;; If the float formats are specified and different, the types
1883 ;; are disjoint.
1884 ((not (or (eq (numeric-type-format type1) format2)
1885 (null format2)))
1886 (values nil t))
1887 ;; Check the bounds.
1888 ((and (numeric-bound-test low1 low2 >= >)
1889 (numeric-bound-test high1 high2 <= <))
1890 (values t t))
1892 (values nil t)))))
1894 (!define-superclasses number ((number)) !cold-init-forms)
1896 ;;; If the high bound of LOW is adjacent to the low bound of HIGH,
1897 ;;; then return true, otherwise NIL.
1898 (defun numeric-types-adjacent (low high)
1899 (let ((low-bound (numeric-type-high low))
1900 (high-bound (numeric-type-low high)))
1901 (cond ((not (and low-bound high-bound)) nil)
1902 ((and (consp low-bound) (consp high-bound)) nil)
1903 ((consp low-bound)
1904 (let ((low-value (car low-bound)))
1905 (or (eql low-value high-bound)
1906 (and (eql low-value
1907 (load-time-value (make-unportable-float
1908 :single-float-negative-zero)))
1909 (eql high-bound 0f0))
1910 (and (eql low-value 0f0)
1911 (eql high-bound
1912 (load-time-value (make-unportable-float
1913 :single-float-negative-zero))))
1914 (and (eql low-value
1915 (load-time-value (make-unportable-float
1916 :double-float-negative-zero)))
1917 (eql high-bound 0d0))
1918 (and (eql low-value 0d0)
1919 (eql high-bound
1920 (load-time-value (make-unportable-float
1921 :double-float-negative-zero)))))))
1922 ((consp high-bound)
1923 (let ((high-value (car high-bound)))
1924 (or (eql high-value low-bound)
1925 (and (eql high-value
1926 (load-time-value (make-unportable-float
1927 :single-float-negative-zero)))
1928 (eql low-bound 0f0))
1929 (and (eql high-value 0f0)
1930 (eql low-bound
1931 (load-time-value (make-unportable-float
1932 :single-float-negative-zero))))
1933 (and (eql high-value
1934 (load-time-value (make-unportable-float
1935 :double-float-negative-zero)))
1936 (eql low-bound 0d0))
1937 (and (eql high-value 0d0)
1938 (eql low-bound
1939 (load-time-value (make-unportable-float
1940 :double-float-negative-zero)))))))
1941 ((and (eq (numeric-type-class low) 'integer)
1942 (eq (numeric-type-class high) 'integer))
1943 (eql (1+ low-bound) high-bound))
1945 nil))))
1947 ;;; Return a numeric type that is a supertype for both TYPE1 and TYPE2.
1949 ;;; Binding *APPROXIMATE-NUMERIC-UNIONS* to T allows merging non-adjacent
1950 ;;; numeric types, eg (OR (INTEGER 0 12) (INTEGER 20 128)) => (INTEGER 0 128),
1951 ;;; the compiler does this occasionally during type-derivation to avoid
1952 ;;; creating absurdly complex unions of numeric types.
1953 (defvar *approximate-numeric-unions* nil)
1955 (!define-type-method (number :simple-union2) (type1 type2)
1956 (declare (type numeric-type type1 type2))
1957 (cond ((csubtypep type1 type2) type2)
1958 ((csubtypep type2 type1) type1)
1960 (let ((class1 (numeric-type-class type1))
1961 (format1 (numeric-type-format type1))
1962 (complexp1 (numeric-type-complexp type1))
1963 (class2 (numeric-type-class type2))
1964 (format2 (numeric-type-format type2))
1965 (complexp2 (numeric-type-complexp type2)))
1966 (cond
1967 ((and (eq class1 class2)
1968 (eq format1 format2)
1969 (eq complexp1 complexp2)
1970 (or *approximate-numeric-unions*
1971 (numeric-types-intersect type1 type2)
1972 (numeric-types-adjacent type1 type2)
1973 (numeric-types-adjacent type2 type1)))
1974 (make-numeric-type
1975 :class class1
1976 :format format1
1977 :complexp complexp1
1978 :low (numeric-bound-max (numeric-type-low type1)
1979 (numeric-type-low type2)
1980 <= < t)
1981 :high (numeric-bound-max (numeric-type-high type1)
1982 (numeric-type-high type2)
1983 >= > t)))
1984 ;; FIXME: These two clauses are almost identical, and the
1985 ;; consequents are in fact identical in every respect.
1986 ((and (eq class1 'rational)
1987 (eq class2 'integer)
1988 (eq format1 format2)
1989 (eq complexp1 complexp2)
1990 (integerp (numeric-type-low type2))
1991 (integerp (numeric-type-high type2))
1992 (= (numeric-type-low type2) (numeric-type-high type2))
1993 (or *approximate-numeric-unions*
1994 (numeric-types-adjacent type1 type2)
1995 (numeric-types-adjacent type2 type1)))
1996 (make-numeric-type
1997 :class 'rational
1998 :format format1
1999 :complexp complexp1
2000 :low (numeric-bound-max (numeric-type-low type1)
2001 (numeric-type-low type2)
2002 <= < t)
2003 :high (numeric-bound-max (numeric-type-high type1)
2004 (numeric-type-high type2)
2005 >= > t)))
2006 ((and (eq class1 'integer)
2007 (eq class2 'rational)
2008 (eq format1 format2)
2009 (eq complexp1 complexp2)
2010 (integerp (numeric-type-low type1))
2011 (integerp (numeric-type-high type1))
2012 (= (numeric-type-low type1) (numeric-type-high type1))
2013 (or *approximate-numeric-unions*
2014 (numeric-types-adjacent type1 type2)
2015 (numeric-types-adjacent type2 type1)))
2016 (make-numeric-type
2017 :class 'rational
2018 :format format1
2019 :complexp complexp1
2020 :low (numeric-bound-max (numeric-type-low type1)
2021 (numeric-type-low type2)
2022 <= < t)
2023 :high (numeric-bound-max (numeric-type-high type1)
2024 (numeric-type-high type2)
2025 >= > t)))
2026 (t nil))))))
2029 (!cold-init-forms ;; is !PRECOMPUTE-TYPES not doing the right thing?
2030 (setf (info :type :kind 'number) :primitive)
2031 (setf (info :type :builtin 'number)
2032 (make-numeric-type :complexp nil)))
2034 (!def-type-translator complex (&optional (typespec '*))
2035 (if (eq typespec '*)
2036 (specifier-type '(complex real))
2037 (labels ((not-numeric ()
2038 (error "The component type for COMPLEX is not numeric: ~S"
2039 typespec))
2040 (not-real ()
2041 (error "The component type for COMPLEX is not a subtype of REAL: ~S"
2042 typespec))
2043 (complex1 (component-type)
2044 (unless (numeric-type-p component-type)
2045 (not-numeric))
2046 (when (eq (numeric-type-complexp component-type) :complex)
2047 (not-real))
2048 (if (csubtypep component-type (specifier-type '(eql 0)))
2049 *empty-type*
2050 (modified-numeric-type component-type
2051 :complexp :complex)))
2052 (do-complex (ctype)
2053 (cond
2054 ((eq ctype *empty-type*) *empty-type*)
2055 ((eq ctype *universal-type*) (not-real))
2056 ((typep ctype 'numeric-type) (complex1 ctype))
2057 ((typep ctype 'union-type)
2058 (apply #'type-union
2059 (mapcar #'do-complex (union-type-types ctype))))
2060 ((typep ctype 'member-type)
2061 (apply #'type-union
2062 (mapcar-member-type-members
2063 (lambda (x) (do-complex (ctype-of x)))
2064 ctype)))
2065 ((and (typep ctype 'intersection-type)
2066 ;; FIXME: This is very much a
2067 ;; not-quite-worst-effort, but we are required to do
2068 ;; something here because of our representation of
2069 ;; RATIO as (AND RATIONAL (NOT INTEGER)): we must
2070 ;; allow users to ask about (COMPLEX RATIO). This
2071 ;; will of course fail to work right on such types
2072 ;; as (AND INTEGER (SATISFIES ZEROP))...
2073 (let ((numbers (remove-if-not
2074 #'numeric-type-p
2075 (intersection-type-types ctype))))
2076 (and (car numbers)
2077 (null (cdr numbers))
2078 (eq (numeric-type-complexp (car numbers)) :real)
2079 (complex1 (car numbers))))))
2081 (multiple-value-bind (subtypep certainly)
2082 (csubtypep ctype (specifier-type 'real))
2083 (if (and (not subtypep) certainly)
2084 (not-real)
2085 ;; ANSI just says that TYPESPEC is any subtype of
2086 ;; type REAL, not necessarily a NUMERIC-TYPE. In
2087 ;; particular, at this point TYPESPEC could legally
2088 ;; be a hairy type like (AND NUMBER (SATISFIES
2089 ;; REALP) (SATISFIES ZEROP)), in which case we fall
2090 ;; through the logic above and end up here,
2091 ;; stumped.
2092 ;; FIXME: (COMPLEX NUMBER) is not rejected but should
2093 ;; be, as NUMBER is clearly not a subtype of real.
2094 (bug "~@<(known bug #145): The type ~S is too hairy to be ~
2095 used for a COMPLEX component.~:@>"
2096 typespec)))))))
2097 (let ((ctype (specifier-type typespec)))
2098 (do-complex ctype)))))
2100 ;;; If X is *, return NIL, otherwise return the bound, which must be a
2101 ;;; member of TYPE or a one-element list of a member of TYPE.
2102 #!-sb-fluid (declaim (inline canonicalized-bound))
2103 (defun canonicalized-bound (bound type)
2104 (cond ((eq bound '*) nil)
2105 ((or (sb!xc:typep bound type)
2106 (and (consp bound)
2107 (sb!xc:typep (car bound) type)
2108 (null (cdr bound))))
2109 bound)
2111 (error "Bound is not ~S, a ~S or a list of a ~S: ~S"
2113 type
2114 type
2115 bound))))
2117 (!def-type-translator integer (&optional (low '*) (high '*))
2118 (let* ((l (canonicalized-bound low 'integer))
2119 (lb (if (consp l) (1+ (car l)) l))
2120 (h (canonicalized-bound high 'integer))
2121 (hb (if (consp h) (1- (car h)) h)))
2122 (if (and hb lb (< hb lb))
2123 *empty-type*
2124 (make-numeric-type :class 'integer
2125 :complexp :real
2126 :enumerable (not (null (and l h)))
2127 :low lb
2128 :high hb))))
2130 (defmacro !def-bounded-type (type class format)
2131 `(!def-type-translator ,type (&optional (low '*) (high '*))
2132 (let ((lb (canonicalized-bound low ',type))
2133 (hb (canonicalized-bound high ',type)))
2134 (if (not (numeric-bound-test* lb hb <= <))
2135 *empty-type*
2136 (make-numeric-type :class ',class
2137 :format ',format
2138 :low lb
2139 :high hb)))))
2141 (!def-bounded-type rational rational nil)
2143 ;;; Unlike CMU CL, we represent the types FLOAT and REAL as
2144 ;;; UNION-TYPEs of more primitive types, in order to make
2145 ;;; type representation more unique, avoiding problems in the
2146 ;;; simplification of things like
2147 ;;; (subtypep '(or (single-float -1.0 1.0) (single-float 0.1))
2148 ;;; '(or (real -1 7) (single-float 0.1) (single-float -1.0 1.0)))
2149 ;;; When we allowed REAL to remain as a separate NUMERIC-TYPE,
2150 ;;; it was too easy for the first argument to be simplified to
2151 ;;; '(SINGLE-FLOAT -1.0), and for the second argument to be simplified
2152 ;;; to '(OR (REAL -1 7) (SINGLE-FLOAT 0.1)) and then for the
2153 ;;; SUBTYPEP to fail (returning NIL,T instead of T,T) because
2154 ;;; the first argument can't be seen to be a subtype of any of the
2155 ;;; terms in the second argument.
2157 ;;; The old CMU CL way was:
2158 ;;; (!def-bounded-type float float nil)
2159 ;;; (!def-bounded-type real nil nil)
2161 ;;; FIXME: If this new way works for a while with no weird new
2162 ;;; problems, we can go back and rip out support for separate FLOAT
2163 ;;; and REAL flavors of NUMERIC-TYPE. The new way was added in
2164 ;;; sbcl-0.6.11.22, 2001-03-21.
2166 ;;; FIXME: It's probably necessary to do something to fix the
2167 ;;; analogous problem with INTEGER and RATIONAL types. Perhaps
2168 ;;; bounded RATIONAL types should be represented as (OR RATIO INTEGER).
2169 (defun coerce-bound (bound type upperp inner-coerce-bound-fun)
2170 (declare (type function inner-coerce-bound-fun))
2171 (if (eql bound '*)
2172 bound
2173 (funcall inner-coerce-bound-fun bound type upperp)))
2174 (defun inner-coerce-real-bound (bound type upperp)
2175 #+sb-xc-host (declare (ignore upperp))
2176 (let #+sb-xc-host ()
2177 #-sb-xc-host
2178 ((nl (load-time-value (symbol-value 'sb!xc:most-negative-long-float)))
2179 (pl (load-time-value (symbol-value 'sb!xc:most-positive-long-float))))
2180 (let ((nbound (if (consp bound) (car bound) bound))
2181 (consp (consp bound)))
2182 (ecase type
2183 (rational
2184 (if consp
2185 (list (rational nbound))
2186 (rational nbound)))
2187 (float
2188 (cond
2189 ((floatp nbound) bound)
2191 ;; Coerce to the widest float format available, to avoid
2192 ;; unnecessary loss of precision, but don't coerce
2193 ;; unrepresentable numbers, except on the host where we
2194 ;; shouldn't be making these types (but KLUDGE: can't even
2195 ;; assert portably that we're not).
2196 #-sb-xc-host
2197 (ecase upperp
2198 ((nil)
2199 (when (< nbound nl) (return-from inner-coerce-real-bound nl)))
2200 ((t)
2201 (when (> nbound pl) (return-from inner-coerce-real-bound pl))))
2202 (let ((result (coerce nbound 'long-float)))
2203 (if consp (list result) result)))))))))
2204 (defun inner-coerce-float-bound (bound type upperp)
2205 #+sb-xc-host (declare (ignore upperp))
2206 (let #+sb-xc-host ()
2207 #-sb-xc-host
2208 ((nd (load-time-value (symbol-value 'sb!xc:most-negative-double-float)))
2209 (pd (load-time-value (symbol-value 'sb!xc:most-positive-double-float)))
2210 (ns (load-time-value (symbol-value 'sb!xc:most-negative-single-float)))
2211 (ps (load-time-value
2212 (symbol-value 'sb!xc:most-positive-single-float))))
2213 (let ((nbound (if (consp bound) (car bound) bound))
2214 (consp (consp bound)))
2215 (ecase type
2216 (single-float
2217 (cond
2218 ((typep nbound 'single-float) bound)
2220 #-sb-xc-host
2221 (ecase upperp
2222 ((nil)
2223 (when (< nbound ns) (return-from inner-coerce-float-bound ns)))
2224 ((t)
2225 (when (> nbound ps) (return-from inner-coerce-float-bound ps))))
2226 (let ((result (coerce nbound 'single-float)))
2227 (if consp (list result) result)))))
2228 (double-float
2229 (cond
2230 ((typep nbound 'double-float) bound)
2232 #-sb-xc-host
2233 (ecase upperp
2234 ((nil)
2235 (when (< nbound nd) (return-from inner-coerce-float-bound nd)))
2236 ((t)
2237 (when (> nbound pd) (return-from inner-coerce-float-bound pd))))
2238 (let ((result (coerce nbound 'double-float)))
2239 (if consp (list result) result)))))))))
2240 (defun coerced-real-bound (bound type upperp)
2241 (coerce-bound bound type upperp #'inner-coerce-real-bound))
2242 (defun coerced-float-bound (bound type upperp)
2243 (coerce-bound bound type upperp #'inner-coerce-float-bound))
2244 (!def-type-translator real (&optional (low '*) (high '*))
2245 (specifier-type `(or (float ,(coerced-real-bound low 'float nil)
2246 ,(coerced-real-bound high 'float t))
2247 (rational ,(coerced-real-bound low 'rational nil)
2248 ,(coerced-real-bound high 'rational t)))))
2249 (!def-type-translator float (&optional (low '*) (high '*))
2250 (specifier-type
2251 `(or (single-float ,(coerced-float-bound low 'single-float nil)
2252 ,(coerced-float-bound high 'single-float t))
2253 (double-float ,(coerced-float-bound low 'double-float nil)
2254 ,(coerced-float-bound high 'double-float t))
2255 #!+long-float ,(error "stub: no long float support yet"))))
2257 (defmacro !define-float-format (f)
2258 `(!def-bounded-type ,f float ,f))
2260 ;; (!define-float-format short-float) ; it's a DEFTYPE
2261 (!define-float-format single-float)
2262 (!define-float-format double-float)
2263 ;; long-float support is dead.
2264 ;; (!define-float-format long-float) ; also a DEFTYPE
2266 (defun numeric-types-intersect (type1 type2)
2267 (declare (type numeric-type type1 type2))
2268 (let* ((class1 (numeric-type-class type1))
2269 (class2 (numeric-type-class type2))
2270 (complexp1 (numeric-type-complexp type1))
2271 (complexp2 (numeric-type-complexp type2))
2272 (format1 (numeric-type-format type1))
2273 (format2 (numeric-type-format type2))
2274 (low1 (numeric-type-low type1))
2275 (high1 (numeric-type-high type1))
2276 (low2 (numeric-type-low type2))
2277 (high2 (numeric-type-high type2)))
2278 ;; If one is complex and the other isn't, then they are disjoint.
2279 (cond ((not (or (eq complexp1 complexp2)
2280 (null complexp1) (null complexp2)))
2281 nil)
2282 ;; If either type is a float, then the other must either be
2283 ;; specified to be a float or unspecified. Otherwise, they
2284 ;; are disjoint.
2285 ((and (eq class1 'float)
2286 (not (member class2 '(float nil)))) nil)
2287 ((and (eq class2 'float)
2288 (not (member class1 '(float nil)))) nil)
2289 ;; If the float formats are specified and different, the
2290 ;; types are disjoint.
2291 ((not (or (eq format1 format2) (null format1) (null format2)))
2292 nil)
2294 ;; Check the bounds. This is a bit odd because we must
2295 ;; always have the outer bound of the interval as the
2296 ;; second arg.
2297 (if (numeric-bound-test high1 high2 <= <)
2298 (or (and (numeric-bound-test low1 low2 >= >)
2299 (numeric-bound-test* low1 high2 <= <))
2300 (and (numeric-bound-test low2 low1 >= >)
2301 (numeric-bound-test* low2 high1 <= <)))
2302 (or (and (numeric-bound-test* low2 high1 <= <)
2303 (numeric-bound-test low2 low1 >= >))
2304 (and (numeric-bound-test high2 high1 <= <)
2305 (numeric-bound-test* high2 low1 >= >))))))))
2307 ;;; Take the numeric bound X and convert it into something that can be
2308 ;;; used as a bound in a numeric type with the specified CLASS and
2309 ;;; FORMAT. If UP-P is true, then we round up as needed, otherwise we
2310 ;;; round down. UP-P true implies that X is a lower bound, i.e. (N) > N.
2312 ;;; This is used by NUMERIC-TYPE-INTERSECTION to mash the bound into
2313 ;;; the appropriate type number. X may only be a float when CLASS is
2314 ;;; FLOAT.
2316 ;;; ### Note: it is possible for the coercion to a float to overflow
2317 ;;; or underflow. This happens when the bound doesn't fit in the
2318 ;;; specified format. In this case, we should really return the
2319 ;;; appropriate {Most | Least}-{Positive | Negative}-XXX-Float float
2320 ;;; of desired format. But these conditions aren't currently signalled
2321 ;;; in any useful way.
2323 ;;; Also, when converting an open rational bound into a float we
2324 ;;; should probably convert it to a closed bound of the closest float
2325 ;;; in the specified format. KLUDGE: In general, open float bounds are
2326 ;;; screwed up. -- (comment from original CMU CL)
2327 (defun round-numeric-bound (x class format up-p)
2328 (if x
2329 (let ((cx (if (consp x) (car x) x)))
2330 (ecase class
2331 ((nil rational) x)
2332 (integer
2333 (if (and (consp x) (integerp cx))
2334 (if up-p (1+ cx) (1- cx))
2335 (if up-p (ceiling cx) (floor cx))))
2336 (float
2337 (let ((res
2338 (cond
2339 ((and format (subtypep format 'double-float))
2340 (if (<= most-negative-double-float cx most-positive-double-float)
2341 (coerce cx format)
2342 nil))
2344 (if (<= most-negative-single-float cx most-positive-single-float)
2345 ;; FIXME: bug #389
2346 (coerce cx (or format 'single-float))
2347 nil)))))
2348 (if (consp x) (list res) res)))))
2349 nil))
2351 ;;; Handle the case of type intersection on two numeric types. We use
2352 ;;; TYPES-EQUAL-OR-INTERSECT to throw out the case of types with no
2353 ;;; intersection. If an attribute in TYPE1 is unspecified, then we use
2354 ;;; TYPE2's attribute, which must be at least as restrictive. If the
2355 ;;; types intersect, then the only attributes that can be specified
2356 ;;; and different are the class and the bounds.
2358 ;;; When the class differs, we use the more restrictive class. The
2359 ;;; only interesting case is RATIONAL/INTEGER, since RATIONAL includes
2360 ;;; INTEGER.
2362 ;;; We make the result lower (upper) bound the maximum (minimum) of
2363 ;;; the argument lower (upper) bounds. We convert the bounds into the
2364 ;;; appropriate numeric type before maximizing. This avoids possible
2365 ;;; confusion due to mixed-type comparisons (but I think the result is
2366 ;;; the same).
2367 (!define-type-method (number :simple-intersection2) (type1 type2)
2368 (declare (type numeric-type type1 type2))
2369 (if (numeric-types-intersect type1 type2)
2370 (let* ((class1 (numeric-type-class type1))
2371 (class2 (numeric-type-class type2))
2372 (class (ecase class1
2373 ((nil) class2)
2374 ((integer float) class1)
2375 (rational (if (eq class2 'integer)
2376 'integer
2377 'rational))))
2378 (format (or (numeric-type-format type1)
2379 (numeric-type-format type2))))
2380 (make-numeric-type
2381 :class class
2382 :format format
2383 :complexp (or (numeric-type-complexp type1)
2384 (numeric-type-complexp type2))
2385 :low (numeric-bound-max
2386 (round-numeric-bound (numeric-type-low type1)
2387 class format t)
2388 (round-numeric-bound (numeric-type-low type2)
2389 class format t)
2390 > >= nil)
2391 :high (numeric-bound-max
2392 (round-numeric-bound (numeric-type-high type1)
2393 class format nil)
2394 (round-numeric-bound (numeric-type-high type2)
2395 class format nil)
2396 < <= nil)))
2397 *empty-type*))
2399 ;;; Given two float formats, return the one with more precision. If
2400 ;;; either one is null, return NIL.
2401 (defun float-format-max (f1 f2)
2402 (when (and f1 f2)
2403 (dolist (f *float-formats* (error "bad float format: ~S" f1))
2404 (when (or (eq f f1) (eq f f2))
2405 (return f)))))
2407 ;;; Return the result of an operation on TYPE1 and TYPE2 according to
2408 ;;; the rules of numeric contagion. This is always NUMBER, some float
2409 ;;; format (possibly complex) or RATIONAL. Due to rational
2410 ;;; canonicalization, there isn't much we can do here with integers or
2411 ;;; rational complex numbers.
2413 ;;; If either argument is not a NUMERIC-TYPE, then return NUMBER. This
2414 ;;; is useful mainly for allowing types that are technically numbers,
2415 ;;; but not a NUMERIC-TYPE.
2416 (defun numeric-contagion (type1 type2)
2417 (if (and (numeric-type-p type1) (numeric-type-p type2))
2418 (let ((class1 (numeric-type-class type1))
2419 (class2 (numeric-type-class type2))
2420 (format1 (numeric-type-format type1))
2421 (format2 (numeric-type-format type2))
2422 (complexp1 (numeric-type-complexp type1))
2423 (complexp2 (numeric-type-complexp type2)))
2424 (cond ((or (null complexp1)
2425 (null complexp2))
2426 (specifier-type 'number))
2427 ((eq class1 'float)
2428 (make-numeric-type
2429 :class 'float
2430 :format (ecase class2
2431 (float (float-format-max format1 format2))
2432 ((integer rational) format1)
2433 ((nil)
2434 ;; A double-float with any real number is a
2435 ;; double-float.
2436 #!-long-float
2437 (if (eq format1 'double-float)
2438 'double-float
2439 nil)
2440 ;; A long-float with any real number is a
2441 ;; long-float.
2442 #!+long-float
2443 (if (eq format1 'long-float)
2444 'long-float
2445 nil)))
2446 :complexp (if (or (eq complexp1 :complex)
2447 (eq complexp2 :complex))
2448 :complex
2449 :real)))
2450 ((eq class2 'float) (numeric-contagion type2 type1))
2451 ((and (eq complexp1 :real) (eq complexp2 :real))
2452 (make-numeric-type
2453 :class (and class1 class2 'rational)
2454 :complexp :real))
2456 (specifier-type 'number))))
2457 (specifier-type 'number)))
2459 ;;;; array types
2461 (!define-type-class array :enumerable nil
2462 :might-contain-other-types nil)
2464 (!define-type-method (array :simple-=) (type1 type2)
2465 (cond ((not (and (equal (array-type-dimensions type1)
2466 (array-type-dimensions type2))
2467 (eq (array-type-complexp type1)
2468 (array-type-complexp type2))))
2469 (values nil t))
2470 ((or (unknown-type-p (array-type-element-type type1))
2471 (unknown-type-p (array-type-element-type type2)))
2472 (type= (array-type-element-type type1)
2473 (array-type-element-type type2)))
2475 (values (type= (array-type-specialized-element-type type1)
2476 (array-type-specialized-element-type type2))
2477 t))))
2479 (!define-type-method (array :negate) (type)
2480 ;; FIXME (and hint to PFD): we're vulnerable here to attacks of the
2481 ;; form "are (AND ARRAY (NOT (ARRAY T))) and (OR (ARRAY BIT) (ARRAY
2482 ;; NIL) (ARRAY CHAR) ...) equivalent?" -- CSR, 2003-12-10
2483 ;; A symptom of the aforementioned is that the following are not TYPE=
2484 ;; (AND (VECTOR T) (NOT SIMPLE-ARRAY)) ; an ARRAY-TYPE
2485 ;; (AND (VECTOR T) (NOT SIMPLE-VECTOR)) ; an INTERSECTION-TYPE
2486 ;; even though (VECTOR T) makes it so that the (NOT) clause in each can
2487 ;; only provide one additional bit of information: that the vector
2488 ;; is complex as opposed to simple. The rank and element-type are fixed.
2489 (if (and (eq (array-type-dimensions type) '*)
2490 (eq (array-type-complexp type) 't)
2491 (eq (array-type-element-type type) *wild-type*))
2492 ;; (NOT <hairy-array>) = either SIMPLE-ARRAY or (NOT ARRAY).
2493 ;; This is deliberately asymmetric - trying to say that NOT simple-array
2494 ;; equals hairy-array leads to infinite recursion.
2495 (type-union (make-array-type '* :complexp nil
2496 :element-type *wild-type*)
2497 (make-negation-type
2498 :type (make-array-type '* :element-type *wild-type*)))
2499 (make-negation-type :type type)))
2501 (!define-type-method (array :unparse) (type)
2502 (let* ((dims (array-type-dimensions type))
2503 ;; Compare the specialised element type and the
2504 ;; derived element type. If the derived type
2505 ;; is so small that it jumps to a smaller upgraded
2506 ;; element type, use the specialised element type.
2508 ;; This protects from unparsing
2509 ;; (and (vector (or bit symbol))
2510 ;; (vector (or bit character)))
2511 ;; i.e., the intersection of two T array types,
2512 ;; as a bit vector.
2513 (stype (array-type-specialized-element-type type))
2514 (dtype (array-type-element-type type))
2515 (utype (%upgraded-array-element-type dtype))
2516 (eltype (type-specifier (if (type= stype utype)
2517 dtype
2518 stype)))
2519 (complexp (array-type-complexp type)))
2520 (if (and (eq complexp t) (not *unparse-allow-negation*))
2521 (setq complexp :maybe))
2522 (cond ((eq dims '*)
2523 (if (eq eltype '*)
2524 (ecase complexp
2525 ((t) '(and array (not simple-array)))
2526 ((:maybe) 'array)
2527 ((nil) 'simple-array))
2528 (ecase complexp
2529 ((t) `(and (array ,eltype) (not simple-array)))
2530 ((:maybe) `(array ,eltype))
2531 ((nil) `(simple-array ,eltype)))))
2532 ((= (length dims) 1)
2533 (if complexp
2534 (let ((answer
2535 (if (eq (car dims) '*)
2536 (case eltype
2537 (bit 'bit-vector)
2538 ((base-char #!-sb-unicode character) 'base-string)
2539 (* 'vector)
2540 (t `(vector ,eltype)))
2541 (case eltype
2542 (bit `(bit-vector ,(car dims)))
2543 ((base-char #!-sb-unicode character)
2544 `(base-string ,(car dims)))
2545 (t `(vector ,eltype ,(car dims)))))))
2546 (if (eql complexp :maybe)
2547 answer
2548 `(and ,answer (not simple-array))))
2549 (if (eq (car dims) '*)
2550 (case eltype
2551 (bit 'simple-bit-vector)
2552 ((base-char #!-sb-unicode character) 'simple-base-string)
2553 ((t) 'simple-vector)
2554 (t `(simple-array ,eltype (*))))
2555 (case eltype
2556 (bit `(simple-bit-vector ,(car dims)))
2557 ((base-char #!-sb-unicode character)
2558 `(simple-base-string ,(car dims)))
2559 ((t) `(simple-vector ,(car dims)))
2560 (t `(simple-array ,eltype ,dims))))))
2562 (ecase complexp
2563 ((t) `(and (array ,eltype ,dims) (not simple-array)))
2564 ((:maybe) `(array ,eltype ,dims))
2565 ((nil) `(simple-array ,eltype ,dims)))))))
2567 (!define-type-method (array :simple-subtypep) (type1 type2)
2568 (let ((dims1 (array-type-dimensions type1))
2569 (dims2 (array-type-dimensions type2))
2570 (complexp2 (array-type-complexp type2)))
2571 (cond (;; not subtypep unless dimensions are compatible
2572 (not (or (eq dims2 '*)
2573 (and (not (eq dims1 '*))
2574 ;; (sbcl-0.6.4 has trouble figuring out that
2575 ;; DIMS1 and DIMS2 must be lists at this
2576 ;; point, and knowing that is important to
2577 ;; compiling EVERY efficiently.)
2578 (= (length (the list dims1))
2579 (length (the list dims2)))
2580 (every (lambda (x y)
2581 (or (eq y '*) (eql x y)))
2582 (the list dims1)
2583 (the list dims2)))))
2584 (values nil t))
2585 ;; not subtypep unless complexness is compatible
2586 ((not (or (eq complexp2 :maybe)
2587 (eq (array-type-complexp type1) complexp2)))
2588 (values nil t))
2589 ;; Since we didn't fail any of the tests above, we win
2590 ;; if the TYPE2 element type is wild.
2591 ((eq (array-type-element-type type2) *wild-type*)
2592 (values t t))
2593 (;; Since we didn't match any of the special cases above, if
2594 ;; either element type is unknown we can only give a good
2595 ;; answer if they are the same.
2596 (or (unknown-type-p (array-type-element-type type1))
2597 (unknown-type-p (array-type-element-type type2)))
2598 (if (type= (array-type-element-type type1)
2599 (array-type-element-type type2))
2600 (values t t)
2601 (values nil nil)))
2602 (;; Otherwise, the subtype relationship holds iff the
2603 ;; types are equal, and they're equal iff the specialized
2604 ;; element types are identical.
2606 (values (type= (array-type-specialized-element-type type1)
2607 (array-type-specialized-element-type type2))
2608 t)))))
2610 (!define-superclasses array
2611 ((vector vector) (array))
2612 !cold-init-forms)
2614 (defun array-types-intersect (type1 type2)
2615 (declare (type array-type type1 type2))
2616 (let ((dims1 (array-type-dimensions type1))
2617 (dims2 (array-type-dimensions type2))
2618 (complexp1 (array-type-complexp type1))
2619 (complexp2 (array-type-complexp type2)))
2620 ;; See whether dimensions are compatible.
2621 (cond ((not (or (eq dims1 '*) (eq dims2 '*)
2622 (and (= (length dims1) (length dims2))
2623 (every (lambda (x y)
2624 (or (eq x '*) (eq y '*) (= x y)))
2625 dims1 dims2))))
2626 (values nil t))
2627 ;; See whether complexpness is compatible.
2628 ((not (or (eq complexp1 :maybe)
2629 (eq complexp2 :maybe)
2630 (eq complexp1 complexp2)))
2631 (values nil t))
2632 ;; Old comment:
2634 ;; If either element type is wild, then they intersect.
2635 ;; Otherwise, the types must be identical.
2637 ;; FIXME: There seems to have been a fair amount of
2638 ;; confusion about the distinction between requested element
2639 ;; type and specialized element type; here is one of
2640 ;; them. If we request an array to hold objects of an
2641 ;; unknown type, we can do no better than represent that
2642 ;; type as an array specialized on wild-type. We keep the
2643 ;; requested element-type in the -ELEMENT-TYPE slot, and
2644 ;; *WILD-TYPE* in the -SPECIALIZED-ELEMENT-TYPE. So, here,
2645 ;; we must test for the SPECIALIZED slot being *WILD-TYPE*,
2646 ;; not just the ELEMENT-TYPE slot. Maybe the return value
2647 ;; in that specific case should be T, NIL? Or maybe this
2648 ;; function should really be called
2649 ;; ARRAY-TYPES-COULD-POSSIBLY-INTERSECT? In any case, this
2650 ;; was responsible for bug #123, and this whole issue could
2651 ;; do with a rethink and/or a rewrite. -- CSR, 2002-08-21
2652 ((or (eq (array-type-specialized-element-type type1) *wild-type*)
2653 (eq (array-type-specialized-element-type type2) *wild-type*)
2654 (type= (array-type-specialized-element-type type1)
2655 (array-type-specialized-element-type type2)))
2657 (values t t))
2659 (values nil t)))))
2661 (defun unite-array-types-complexp (type1 type2)
2662 (let ((complexp1 (array-type-complexp type1))
2663 (complexp2 (array-type-complexp type2)))
2664 (cond
2665 ((eq complexp1 complexp2)
2666 ;; both types are the same complexp-ity
2667 (values complexp1 t))
2668 ((eq complexp1 :maybe)
2669 ;; type1 is wild-complexp
2670 (values :maybe type1))
2671 ((eq complexp2 :maybe)
2672 ;; type2 is wild-complexp
2673 (values :maybe type2))
2675 ;; both types partition the complexp-space
2676 (values :maybe nil)))))
2678 (defun unite-array-types-dimensions (type1 type2)
2679 (let ((dims1 (array-type-dimensions type1))
2680 (dims2 (array-type-dimensions type2)))
2681 (cond ((equal dims1 dims2)
2682 ;; both types are same dimensionality
2683 (values dims1 t))
2684 ((eq dims1 '*)
2685 ;; type1 is wild-dimensions
2686 (values '* type1))
2687 ((eq dims2 '*)
2688 ;; type2 is wild-dimensions
2689 (values '* type2))
2690 ((not (= (length dims1) (length dims2)))
2691 ;; types have different number of dimensions
2692 (values :incompatible nil))
2694 ;; we need to check on a per-dimension basis
2695 (let* ((supertype1 t)
2696 (supertype2 t)
2697 (compatible t)
2698 (result (mapcar (lambda (dim1 dim2)
2699 (cond
2700 ((equal dim1 dim2)
2701 dim1)
2702 ((eq dim1 '*)
2703 (setf supertype2 nil)
2705 ((eq dim2 '*)
2706 (setf supertype1 nil)
2709 (setf compatible nil))))
2710 dims1 dims2)))
2711 (cond
2712 ((or (not compatible)
2713 (and (not supertype1)
2714 (not supertype2)))
2715 (values :incompatible nil))
2716 ((and supertype1 supertype2)
2717 (values result supertype1))
2719 (values result (if supertype1 type1 type2)))))))))
2721 (defun unite-array-types-element-types (type1 type2)
2722 ;; FIXME: We'd love to be able to unite the full set of specialized
2723 ;; array element types up to *wild-type*, but :simple-union2 is
2724 ;; performed pairwise, so we don't have a good hook for it and our
2725 ;; representation doesn't allow us to easily detect the situation
2726 ;; anyway.
2727 ;; But see SIMPLIFY-ARRAY-UNIONS which is able to do something like that.
2728 (let* ((eltype1 (array-type-element-type type1))
2729 (eltype2 (array-type-element-type type2))
2730 (stype1 (array-type-specialized-element-type type1))
2731 (stype2 (array-type-specialized-element-type type2))
2732 (wild1 (eq eltype1 *wild-type*))
2733 (wild2 (eq eltype2 *wild-type*)))
2734 (cond
2735 ((type= eltype1 eltype2)
2736 (values eltype1 stype1 t))
2737 (wild1
2738 (values eltype1 stype1 type1))
2739 (wild2
2740 (values eltype2 stype2 type2))
2741 ((not (type= stype1 stype2))
2742 ;; non-wild types that don't share UAET don't unite
2743 (values :incompatible nil nil))
2744 ((csubtypep eltype1 eltype2)
2745 (values eltype2 stype2 type2))
2746 ((csubtypep eltype2 eltype1)
2747 (values eltype1 stype1 type1))
2749 (values :incompatible nil nil)))))
2751 (defun unite-array-types-supertypes-compatible-p (&rest supertypes)
2752 ;; supertypes are compatible if they are all T, if there is a single
2753 ;; NIL and all the rest are T, or if all non-T supertypes are the
2754 ;; same and not NIL.
2755 (let ((interesting-supertypes
2756 (remove t supertypes)))
2757 (or (not interesting-supertypes)
2758 (equal interesting-supertypes '(nil))
2759 ;; supertypes are (OR BOOLEAN ARRAY-TYPE), so...
2760 (typep (remove-duplicates interesting-supertypes)
2761 '(cons array-type null)))))
2763 (!define-type-method (array :simple-union2) (type1 type2)
2764 (multiple-value-bind
2765 (result-eltype result-stype eltype-supertype)
2766 (unite-array-types-element-types type1 type2)
2767 (multiple-value-bind
2768 (result-complexp complexp-supertype)
2769 (unite-array-types-complexp type1 type2)
2770 (multiple-value-bind
2771 (result-dimensions dimensions-supertype)
2772 (unite-array-types-dimensions type1 type2)
2773 (when (and (not (eq result-dimensions :incompatible))
2774 (not (eq result-eltype :incompatible))
2775 (unite-array-types-supertypes-compatible-p
2776 eltype-supertype complexp-supertype dimensions-supertype))
2777 (make-array-type result-dimensions
2778 :complexp result-complexp
2779 :element-type result-eltype
2780 :specialized-element-type result-stype))))))
2782 (!define-type-method (array :simple-intersection2) (type1 type2)
2783 (declare (type array-type type1 type2))
2784 (if (array-types-intersect type1 type2)
2785 (let ((dims1 (array-type-dimensions type1))
2786 (dims2 (array-type-dimensions type2))
2787 (complexp1 (array-type-complexp type1))
2788 (complexp2 (array-type-complexp type2))
2789 (eltype1 (array-type-element-type type1))
2790 (eltype2 (array-type-element-type type2))
2791 (stype1 (array-type-specialized-element-type type1))
2792 (stype2 (array-type-specialized-element-type type2)))
2793 (make-array-type (cond ((eq dims1 '*) dims2)
2794 ((eq dims2 '*) dims1)
2796 (mapcar (lambda (x y) (if (eq x '*) y x))
2797 dims1 dims2)))
2798 :complexp (if (eq complexp1 :maybe) complexp2 complexp1)
2799 :element-type (cond
2800 ((eq eltype1 *wild-type*) eltype2)
2801 ((eq eltype2 *wild-type*) eltype1)
2802 (t (type-intersection eltype1 eltype2)))
2803 :specialized-element-type (cond
2804 ((eq stype1 *wild-type*) stype2)
2805 ((eq stype2 *wild-type*) stype1)
2807 (aver (type= stype1 stype2))
2808 stype1))))
2809 *empty-type*))
2811 ;;; Check a supplied dimension list to determine whether it is legal,
2812 ;;; and return it in canonical form (as either '* or a list).
2813 (defun canonical-array-dimensions (dims)
2814 (typecase dims
2815 ((member *) dims)
2816 (integer
2817 (when (minusp dims)
2818 (error "Arrays can't have a negative number of dimensions: ~S" dims))
2819 (when (>= dims sb!xc:array-rank-limit)
2820 (error "array type with too many dimensions: ~S" dims))
2821 (make-list dims :initial-element '*))
2822 (list
2823 (when (>= (length dims) sb!xc:array-rank-limit)
2824 (error "array type with too many dimensions: ~S" dims))
2825 (dolist (dim dims)
2826 (unless (eq dim '*)
2827 (unless (and (integerp dim)
2828 (>= dim 0)
2829 (< dim sb!xc:array-dimension-limit))
2830 (error "bad dimension in array type: ~S" dim))))
2831 dims)
2833 (error "Array dimensions is not a list, integer or *:~% ~S" dims))))
2835 ;;;; MEMBER types
2837 (!define-type-class member :enumerable t
2838 :might-contain-other-types nil)
2840 (!define-type-method (member :negate) (type)
2841 (let ((xset (member-type-xset type))
2842 (fp-zeroes (member-type-fp-zeroes type)))
2843 (if fp-zeroes
2844 ;; Hairy case, which needs to do a bit of float type
2845 ;; canonicalization.
2846 (apply #'type-intersection
2847 (if (xset-empty-p xset)
2848 *universal-type*
2849 (make-negation-type
2850 :type (make-member-type :xset xset)))
2851 (mapcar
2852 (lambda (x)
2853 (let* ((opposite (neg-fp-zero x))
2854 (type (ctype-of opposite)))
2855 (type-union
2856 (make-negation-type
2857 :type (modified-numeric-type type :low nil :high nil))
2858 (modified-numeric-type type :low nil :high (list opposite))
2859 (make-member-type :members (list opposite))
2860 (modified-numeric-type type :low (list opposite) :high nil))))
2861 fp-zeroes))
2862 ;; Easy case
2863 (make-negation-type :type type))))
2865 (!define-type-method (member :unparse) (type)
2866 (let ((members (member-type-members type)))
2867 (cond ((equal members '(nil)) 'null)
2868 (t `(member ,@members)))))
2870 (!define-type-method (member :singleton-p) (type)
2871 (if (eql 1 (member-type-size type))
2872 (values t (first (member-type-members type)))
2873 (values nil nil)))
2875 (!define-type-method (member :simple-subtypep) (type1 type2)
2876 (values (and (xset-subset-p (member-type-xset type1)
2877 (member-type-xset type2))
2878 (subsetp (member-type-fp-zeroes type1)
2879 (member-type-fp-zeroes type2)))
2882 (!define-type-method (member :complex-subtypep-arg1) (type1 type2)
2883 (block punt
2884 (mapc-member-type-members
2885 (lambda (elt)
2886 (multiple-value-bind (ok surep) (ctypep elt type2)
2887 (unless surep
2888 (return-from punt (values nil nil)))
2889 (unless ok
2890 (return-from punt (values nil t)))))
2891 type1)
2892 (values t t)))
2894 ;;; We punt if the odd type is enumerable and intersects with the
2895 ;;; MEMBER type. If not enumerable, then it is definitely not a
2896 ;;; subtype of the MEMBER type.
2897 (!define-type-method (member :complex-subtypep-arg2) (type1 type2)
2898 (cond ((not (type-enumerable type1)) (values nil t))
2899 ((types-equal-or-intersect type1 type2)
2900 (invoke-complex-subtypep-arg1-method type1 type2))
2901 (t (values nil t))))
2903 (!define-type-method (member :simple-intersection2) (type1 type2)
2904 (make-member-type :xset (xset-intersection (member-type-xset type1)
2905 (member-type-xset type2))
2906 :fp-zeroes (intersection (member-type-fp-zeroes type1)
2907 (member-type-fp-zeroes type2))))
2909 (!define-type-method (member :complex-intersection2) (type1 type2)
2910 (block punt
2911 (let ((xset (alloc-xset))
2912 (fp-zeroes nil))
2913 (mapc-member-type-members
2914 (lambda (member)
2915 (multiple-value-bind (ok sure) (ctypep member type1)
2916 (unless sure
2917 (return-from punt nil))
2918 (when ok
2919 (if (fp-zero-p member)
2920 (pushnew member fp-zeroes)
2921 (add-to-xset member xset)))))
2922 type2)
2923 (if (and (xset-empty-p xset) (not fp-zeroes))
2924 *empty-type*
2925 (make-member-type :xset xset :fp-zeroes fp-zeroes)))))
2927 ;;; We don't need a :COMPLEX-UNION2, since the only interesting case is
2928 ;;; a union type, and the member/union interaction is handled by the
2929 ;;; union type method.
2930 (!define-type-method (member :simple-union2) (type1 type2)
2931 (make-member-type :xset (xset-union (member-type-xset type1)
2932 (member-type-xset type2))
2933 :fp-zeroes (union (member-type-fp-zeroes type1)
2934 (member-type-fp-zeroes type2))))
2936 (!define-type-method (member :simple-=) (type1 type2)
2937 (let ((xset1 (member-type-xset type1))
2938 (xset2 (member-type-xset type2))
2939 (l1 (member-type-fp-zeroes type1))
2940 (l2 (member-type-fp-zeroes type2)))
2941 (values (and (eql (xset-count xset1) (xset-count xset2))
2942 (xset-subset-p xset1 xset2)
2943 (xset-subset-p xset2 xset1)
2944 (subsetp l1 l2)
2945 (subsetp l2 l1))
2946 t)))
2948 (!define-type-method (member :complex-=) (type1 type2)
2949 (if (type-enumerable type1)
2950 (multiple-value-bind (val win) (csubtypep type2 type1)
2951 (if (or val (not win))
2952 (values nil nil)
2953 (values nil t)))
2954 (values nil t)))
2956 (!def-type-translator member (&rest members)
2957 (if members
2958 (let (ms numbers char-codes)
2959 (dolist (m (remove-duplicates members))
2960 (typecase m
2961 (float (if (zerop m)
2962 (push m ms)
2963 (push (ctype-of m) numbers)))
2964 (real (push (ctype-of m) numbers))
2965 (character (push (sb!xc:char-code m) char-codes))
2966 (t (push m ms))))
2967 (apply #'type-union
2968 (if ms
2969 (make-member-type :members ms)
2970 *empty-type*)
2971 (if char-codes
2972 (make-character-set-type
2973 :pairs (mapcar (lambda (x) (cons x x))
2974 (sort char-codes #'<)))
2975 *empty-type*)
2976 (nreverse numbers)))
2977 *empty-type*))
2979 ;;;; intersection types
2980 ;;;;
2981 ;;;; Until version 0.6.10.6, SBCL followed the original CMU CL approach
2982 ;;;; of punting on all AND types, not just the unreasonably complicated
2983 ;;;; ones. The change was motivated by trying to get the KEYWORD type
2984 ;;;; to behave sensibly:
2985 ;;;; ;; reasonable definition
2986 ;;;; (DEFTYPE KEYWORD () '(AND SYMBOL (SATISFIES KEYWORDP)))
2987 ;;;; ;; reasonable behavior
2988 ;;;; (AVER (SUBTYPEP 'KEYWORD 'SYMBOL))
2989 ;;;; Without understanding a little about the semantics of AND, we'd
2990 ;;;; get (SUBTYPEP 'KEYWORD 'SYMBOL)=>NIL,NIL and, for entirely
2991 ;;;; parallel reasons, (SUBTYPEP 'RATIO 'NUMBER)=>NIL,NIL. That's
2992 ;;;; not so good..)
2993 ;;;;
2994 ;;;; We still follow the example of CMU CL to some extent, by punting
2995 ;;;; (to the opaque HAIRY-TYPE) on sufficiently complicated types
2996 ;;;; involving AND.
2998 (!define-type-class intersection
2999 :enumerable #'compound-type-enumerable
3000 :might-contain-other-types t)
3002 (!define-type-method (intersection :negate) (type)
3003 (apply #'type-union
3004 (mapcar #'type-negation (intersection-type-types type))))
3006 ;;; A few intersection types have special names. The others just get
3007 ;;; mechanically unparsed.
3008 (!define-type-method (intersection :unparse) (type)
3009 (declare (type ctype type))
3010 (or (find type '(ratio keyword compiled-function) :key #'specifier-type :test #'type=)
3011 `(and ,@(mapcar #'type-specifier (intersection-type-types type)))))
3013 ;;; shared machinery for type equality: true if every type in the set
3014 ;;; TYPES1 matches a type in the set TYPES2 and vice versa
3015 (defun type=-set (types1 types2)
3016 (flet ((type<=-set (x y)
3017 (declare (type list x y))
3018 (every/type (lambda (x y-element)
3019 (any/type #'type= y-element x))
3020 x y)))
3021 (and/type (type<=-set types1 types2)
3022 (type<=-set types2 types1))))
3024 ;;; Two intersection types are equal if their subtypes are equal sets.
3026 ;;; FIXME: Might it be better to use
3027 ;;; (AND (SUBTYPEP X Y) (SUBTYPEP Y X))
3028 ;;; instead, since SUBTYPEP is the usual relationship that we care
3029 ;;; most about, so it would be good to leverage any ingenuity there
3030 ;;; in this more obscure method?
3031 (!define-type-method (intersection :simple-=) (type1 type2)
3032 (type=-set (intersection-type-types type1)
3033 (intersection-type-types type2)))
3035 (defun %intersection-complex-subtypep-arg1 (type1 type2)
3036 (type= type1 (type-intersection type1 type2)))
3038 (defun %intersection-simple-subtypep (type1 type2)
3039 (every/type #'%intersection-complex-subtypep-arg1
3040 type1
3041 (intersection-type-types type2)))
3043 (!define-type-method (intersection :simple-subtypep) (type1 type2)
3044 (%intersection-simple-subtypep type1 type2))
3046 (!define-type-method (intersection :complex-subtypep-arg1) (type1 type2)
3047 (%intersection-complex-subtypep-arg1 type1 type2))
3049 (defun %intersection-complex-subtypep-arg2 (type1 type2)
3050 (every/type #'csubtypep type1 (intersection-type-types type2)))
3052 (!define-type-method (intersection :complex-subtypep-arg2) (type1 type2)
3053 (%intersection-complex-subtypep-arg2 type1 type2))
3055 ;;; FIXME: This will look eeriely familiar to readers of the UNION
3056 ;;; :SIMPLE-INTERSECTION2 :COMPLEX-INTERSECTION2 method. That's
3057 ;;; because it was generated by cut'n'paste methods. Given that
3058 ;;; intersections and unions have all sorts of symmetries known to
3059 ;;; mathematics, it shouldn't be beyond the ken of some programmers to
3060 ;;; reflect those symmetries in code in a way that ties them together
3061 ;;; more strongly than having two independent near-copies :-/
3062 (!define-type-method (intersection :simple-union2 :complex-union2)
3063 (type1 type2)
3064 ;; Within this method, type2 is guaranteed to be an intersection
3065 ;; type:
3066 (aver (intersection-type-p type2))
3067 ;; Make sure to call only the applicable methods...
3068 (cond ((and (intersection-type-p type1)
3069 (%intersection-simple-subtypep type1 type2)) type2)
3070 ((and (intersection-type-p type1)
3071 (%intersection-simple-subtypep type2 type1)) type1)
3072 ((and (not (intersection-type-p type1))
3073 (%intersection-complex-subtypep-arg2 type1 type2))
3074 type2)
3075 ((and (not (intersection-type-p type1))
3076 (%intersection-complex-subtypep-arg1 type2 type1))
3077 type1)
3078 ;; KLUDGE: This special (and somewhat hairy) magic is required
3079 ;; to deal with the RATIONAL/INTEGER special case. The UNION
3080 ;; of (INTEGER * -1) and (AND (RATIONAL * -1/2) (NOT INTEGER))
3081 ;; should be (RATIONAL * -1/2) -- CSR, 2003-02-28
3082 ((and (csubtypep type2 (specifier-type 'ratio))
3083 (numeric-type-p type1)
3084 (csubtypep type1 (specifier-type 'integer))
3085 (csubtypep type2
3086 (make-numeric-type
3087 :class 'rational
3088 :complexp nil
3089 :low (if (null (numeric-type-low type1))
3091 (list (1- (numeric-type-low type1))))
3092 :high (if (null (numeric-type-high type1))
3094 (list (1+ (numeric-type-high type1)))))))
3095 (let* ((intersected (intersection-type-types type2))
3096 (remaining (remove (specifier-type '(not integer))
3097 intersected
3098 :test #'type=)))
3099 (and (not (equal intersected remaining))
3100 (type-union type1 (apply #'type-intersection remaining)))))
3102 (let ((accumulator *universal-type*))
3103 (do ((t2s (intersection-type-types type2) (cdr t2s)))
3104 ((null t2s) accumulator)
3105 (let ((union (type-union type1 (car t2s))))
3106 (when (union-type-p union)
3107 ;; we have to give up here -- there are all sorts of
3108 ;; ordering worries, but it's better than before.
3109 ;; Doing exactly the same as in the UNION
3110 ;; :SIMPLE/:COMPLEX-INTERSECTION2 method causes stack
3111 ;; overflow with the mutual recursion never bottoming
3112 ;; out.
3113 (if (and (eq accumulator *universal-type*)
3114 (null (cdr t2s)))
3115 ;; KLUDGE: if we get here, we have a partially
3116 ;; simplified result. While this isn't by any
3117 ;; means a universal simplification, including
3118 ;; this logic here means that we can get (OR
3119 ;; KEYWORD (NOT KEYWORD)) canonicalized to T.
3120 (return union)
3121 (return nil)))
3122 (setf accumulator
3123 (type-intersection accumulator union))))))))
3125 (!def-type-translator and (&whole whole &rest type-specifiers)
3126 (apply #'type-intersection
3127 (mapcar #'specifier-type type-specifiers)))
3129 ;;;; union types
3131 (!define-type-class union
3132 :enumerable #'compound-type-enumerable
3133 :might-contain-other-types t)
3135 (!define-type-method (union :negate) (type)
3136 (declare (type ctype type))
3137 (apply #'type-intersection
3138 (mapcar #'type-negation (union-type-types type))))
3140 ;;; The LIST, FLOAT and REAL types have special names. Other union
3141 ;;; types just get mechanically unparsed.
3142 (!define-type-method (union :unparse) (type)
3143 (declare (type ctype type))
3144 (cond
3145 ((type= type (specifier-type 'list)) 'list)
3146 ((type= type (specifier-type 'float)) 'float)
3147 ((type= type (specifier-type 'real)) 'real)
3148 ((type= type (specifier-type 'sequence)) 'sequence)
3149 ((type= type (specifier-type 'bignum)) 'bignum)
3150 ((type= type (specifier-type 'simple-string)) 'simple-string)
3151 ((type= type (specifier-type 'string)) 'string)
3152 ((type= type (specifier-type 'complex)) 'complex)
3153 ((type= type (specifier-type 'standard-char)) 'standard-char)
3154 (t `(or ,@(mapcar #'type-specifier (union-type-types type))))))
3156 ;;; Two union types are equal if they are each subtypes of each
3157 ;;; other. We need to be this clever because our complex subtypep
3158 ;;; methods are now more accurate; we don't get infinite recursion
3159 ;;; because the simple-subtypep method delegates to complex-subtypep
3160 ;;; of the individual types of type1. - CSR, 2002-04-09
3162 ;;; Previous comment, now obsolete, but worth keeping around because
3163 ;;; it is true, though too strong a condition:
3165 ;;; Two union types are equal if their subtypes are equal sets.
3166 (!define-type-method (union :simple-=) (type1 type2)
3167 (multiple-value-bind (subtype certain?)
3168 (csubtypep type1 type2)
3169 (if subtype
3170 (csubtypep type2 type1)
3171 ;; we might as well become as certain as possible.
3172 (if certain?
3173 (values nil t)
3174 (multiple-value-bind (subtype certain?)
3175 (csubtypep type2 type1)
3176 (declare (ignore subtype))
3177 (values nil certain?))))))
3179 (!define-type-method (union :complex-=) (type1 type2)
3180 (declare (ignore type1))
3181 (if (some #'type-might-contain-other-types-p
3182 (union-type-types type2))
3183 (values nil nil)
3184 (values nil t)))
3186 ;;; Similarly, a union type is a subtype of another if and only if
3187 ;;; every element of TYPE1 is a subtype of TYPE2.
3188 (defun union-simple-subtypep (type1 type2)
3189 (every/type (swapped-args-fun #'union-complex-subtypep-arg2)
3190 type2
3191 (union-type-types type1)))
3193 (!define-type-method (union :simple-subtypep) (type1 type2)
3194 (union-simple-subtypep type1 type2))
3196 (defun union-complex-subtypep-arg1 (type1 type2)
3197 (every/type (swapped-args-fun #'csubtypep)
3198 type2
3199 (union-type-types type1)))
3201 (!define-type-method (union :complex-subtypep-arg1) (type1 type2)
3202 (union-complex-subtypep-arg1 type1 type2))
3204 (defun union-complex-subtypep-arg2 (type1 type2)
3205 ;; At this stage, we know that type2 is a union type and type1
3206 ;; isn't. We might as well check this, though:
3207 (aver (union-type-p type2))
3208 (aver (not (union-type-p type1)))
3209 ;; was: (any/type #'csubtypep type1 (union-type-types type2)), which
3210 ;; turns out to be too restrictive, causing bug 91.
3212 ;; the following reimplementation might look dodgy. It is dodgy. It
3213 ;; depends on the union :complex-= method not doing very much work
3214 ;; -- certainly, not using subtypep. Reasoning:
3216 ;; A is a subset of (B1 u B2)
3217 ;; <=> A n (B1 u B2) = A
3218 ;; <=> (A n B1) u (A n B2) = A
3220 ;; But, we have to be careful not to delegate this type= to
3221 ;; something that could invoke subtypep, which might get us back
3222 ;; here -> stack explosion. We therefore ensure that the second type
3223 ;; (which is the one that's dispatched on) is either a union type
3224 ;; (where we've ensured that the complex-= method will not call
3225 ;; subtypep) or something with no union types involved, in which
3226 ;; case we'll never come back here.
3228 ;; If we don't do this, then e.g.
3229 ;; (SUBTYPEP '(MEMBER 3) '(OR (SATISFIES FOO) (SATISFIES BAR)))
3230 ;; would loop infinitely, as the member :complex-= method is
3231 ;; implemented in terms of subtypep.
3233 ;; Ouch. - CSR, 2002-04-10
3234 (multiple-value-bind (sub-value sub-certain?)
3235 (type= type1
3236 (apply #'type-union
3237 (mapcar (lambda (x) (type-intersection type1 x))
3238 (union-type-types type2))))
3239 (if sub-certain?
3240 (values sub-value sub-certain?)
3241 ;; The ANY/TYPE expression above is a sufficient condition for
3242 ;; subsetness, but not a necessary one, so we might get a more
3243 ;; certain answer by this CALL-NEXT-METHOD-ish step when the
3244 ;; ANY/TYPE expression is uncertain.
3245 (invoke-complex-subtypep-arg1-method type1 type2))))
3247 (!define-type-method (union :complex-subtypep-arg2) (type1 type2)
3248 (union-complex-subtypep-arg2 type1 type2))
3250 (!define-type-method (union :simple-intersection2 :complex-intersection2)
3251 (type1 type2)
3252 ;; The CSUBTYPEP clauses here let us simplify e.g.
3253 ;; (TYPE-INTERSECTION2 (SPECIFIER-TYPE 'LIST)
3254 ;; (SPECIFIER-TYPE '(OR LIST VECTOR)))
3255 ;; (where LIST is (OR CONS NULL)).
3257 ;; The tests are more or less (CSUBTYPEP TYPE1 TYPE2) and vice
3258 ;; versa, but it's important that we pre-expand them into
3259 ;; specialized operations on individual elements of
3260 ;; UNION-TYPE-TYPES, instead of using the ordinary call to
3261 ;; CSUBTYPEP, in order to avoid possibly invoking any methods which
3262 ;; might in turn invoke (TYPE-INTERSECTION2 TYPE1 TYPE2) and thus
3263 ;; cause infinite recursion.
3265 ;; Within this method, type2 is guaranteed to be a union type:
3266 (aver (union-type-p type2))
3267 ;; Make sure to call only the applicable methods...
3268 (cond ((and (union-type-p type1)
3269 (union-simple-subtypep type1 type2)) type1)
3270 ((and (union-type-p type1)
3271 (union-simple-subtypep type2 type1)) type2)
3272 ((and (not (union-type-p type1))
3273 (union-complex-subtypep-arg2 type1 type2))
3274 type1)
3275 ((and (not (union-type-p type1))
3276 (union-complex-subtypep-arg1 type2 type1))
3277 type2)
3279 ;; KLUDGE: This code accumulates a sequence of TYPE-UNION2
3280 ;; operations in a particular order, and gives up if any of
3281 ;; the sub-unions turn out not to be simple. In other cases
3282 ;; ca. sbcl-0.6.11.15, that approach to taking a union was a
3283 ;; bad idea, since it can overlook simplifications which
3284 ;; might occur if the terms were accumulated in a different
3285 ;; order. It's possible that that will be a problem here too.
3286 ;; However, I can't think of a good example to demonstrate
3287 ;; it, and without an example to demonstrate it I can't write
3288 ;; test cases, and without test cases I don't want to
3289 ;; complicate the code to address what's still a hypothetical
3290 ;; problem. So I punted. -- WHN 2001-03-20
3291 (let ((accumulator *empty-type*))
3292 (dolist (t2 (union-type-types type2) accumulator)
3293 (setf accumulator
3294 (type-union accumulator
3295 (type-intersection type1 t2))))))))
3297 (!def-type-translator or (&rest type-specifiers)
3298 (let ((type (apply #'type-union
3299 (mapcar #'specifier-type type-specifiers))))
3300 (if (union-type-p type)
3301 (sb!kernel::simplify-array-unions type)
3302 type)))
3304 ;;;; CONS types
3306 (!define-type-class cons :enumerable nil :might-contain-other-types nil)
3308 (!def-type-translator cons (&optional (car-type-spec '*) (cdr-type-spec '*))
3309 (let ((car-type (single-value-specifier-type car-type-spec))
3310 (cdr-type (single-value-specifier-type cdr-type-spec)))
3311 (make-cons-type car-type cdr-type)))
3313 (!define-type-method (cons :negate) (type)
3314 (if (and (eq (cons-type-car-type type) *universal-type*)
3315 (eq (cons-type-cdr-type type) *universal-type*))
3316 (make-negation-type :type type)
3317 (type-union
3318 (make-negation-type :type (specifier-type 'cons))
3319 (cond
3320 ((and (not (eq (cons-type-car-type type) *universal-type*))
3321 (not (eq (cons-type-cdr-type type) *universal-type*)))
3322 (type-union
3323 (make-cons-type
3324 (type-negation (cons-type-car-type type))
3325 *universal-type*)
3326 (make-cons-type
3327 *universal-type*
3328 (type-negation (cons-type-cdr-type type)))))
3329 ((not (eq (cons-type-car-type type) *universal-type*))
3330 (make-cons-type
3331 (type-negation (cons-type-car-type type))
3332 *universal-type*))
3333 ((not (eq (cons-type-cdr-type type) *universal-type*))
3334 (make-cons-type
3335 *universal-type*
3336 (type-negation (cons-type-cdr-type type))))
3337 (t (bug "Weird CONS type ~S" type))))))
3339 (!define-type-method (cons :unparse) (type)
3340 (let ((car-eltype (type-specifier (cons-type-car-type type)))
3341 (cdr-eltype (type-specifier (cons-type-cdr-type type))))
3342 (if (and (member car-eltype '(t *))
3343 (member cdr-eltype '(t *)))
3344 'cons
3345 `(cons ,car-eltype ,cdr-eltype))))
3347 (!define-type-method (cons :simple-=) (type1 type2)
3348 (declare (type cons-type type1 type2))
3349 (multiple-value-bind (car-match car-win)
3350 (type= (cons-type-car-type type1) (cons-type-car-type type2))
3351 (multiple-value-bind (cdr-match cdr-win)
3352 (type= (cons-type-cdr-type type1) (cons-type-cdr-type type2))
3353 (cond ((and car-match cdr-match)
3354 (aver (and car-win cdr-win))
3355 (values t t))
3357 (values nil
3358 ;; FIXME: Ideally we would like to detect and handle
3359 ;; (CONS UNKNOWN INTEGER) (CONS UNKNOWN SYMBOL) => NIL, T
3360 ;; but just returning a secondary true on (and car-win cdr-win)
3361 ;; unfortunately breaks other things. --NS 2006-08-16
3362 (and (or (and (not car-match) car-win)
3363 (and (not cdr-match) cdr-win))
3364 (not (and (cons-type-might-be-empty-type type1)
3365 (cons-type-might-be-empty-type type2))))))))))
3367 (!define-type-method (cons :simple-subtypep) (type1 type2)
3368 (declare (type cons-type type1 type2))
3369 (multiple-value-bind (val-car win-car)
3370 (csubtypep (cons-type-car-type type1) (cons-type-car-type type2))
3371 (multiple-value-bind (val-cdr win-cdr)
3372 (csubtypep (cons-type-cdr-type type1) (cons-type-cdr-type type2))
3373 (if (and val-car val-cdr)
3374 (values t (and win-car win-cdr))
3375 (values nil (or (and (not val-car) win-car)
3376 (and (not val-cdr) win-cdr)))))))
3378 ;;; Give up if a precise type is not possible, to avoid returning
3379 ;;; overly general types.
3380 (!define-type-method (cons :simple-union2) (type1 type2)
3381 (declare (type cons-type type1 type2))
3382 (let ((car-type1 (cons-type-car-type type1))
3383 (car-type2 (cons-type-car-type type2))
3384 (cdr-type1 (cons-type-cdr-type type1))
3385 (cdr-type2 (cons-type-cdr-type type2))
3386 car-not1
3387 car-not2)
3388 ;; UGH. -- CSR, 2003-02-24
3389 (macrolet ((frob-car (car1 car2 cdr1 cdr2
3390 &optional (not1 nil not1p))
3391 `(type-union
3392 (make-cons-type ,car1 (type-union ,cdr1 ,cdr2))
3393 (make-cons-type
3394 (type-intersection ,car2
3395 ,(if not1p
3396 not1
3397 `(type-negation ,car1)))
3398 ,cdr2))))
3399 (cond ((type= car-type1 car-type2)
3400 (make-cons-type car-type1
3401 (type-union cdr-type1 cdr-type2)))
3402 ((type= cdr-type1 cdr-type2)
3403 (make-cons-type (type-union car-type1 car-type2)
3404 cdr-type1))
3405 ((csubtypep car-type1 car-type2)
3406 (frob-car car-type1 car-type2 cdr-type1 cdr-type2))
3407 ((csubtypep car-type2 car-type1)
3408 (frob-car car-type2 car-type1 cdr-type2 cdr-type1))
3409 ;; more general case of the above, but harder to compute
3410 ((progn
3411 (setf car-not1 (type-negation car-type1))
3412 (multiple-value-bind (yes win)
3413 (csubtypep car-type2 car-not1)
3414 (and (not yes) win)))
3415 (frob-car car-type1 car-type2 cdr-type1 cdr-type2 car-not1))
3416 ((progn
3417 (setf car-not2 (type-negation car-type2))
3418 (multiple-value-bind (yes win)
3419 (csubtypep car-type1 car-not2)
3420 (and (not yes) win)))
3421 (frob-car car-type2 car-type1 cdr-type2 cdr-type1 car-not2))
3422 ;; Don't put these in -- consider the effect of taking the
3423 ;; union of (CONS (INTEGER 0 2) (INTEGER 5 7)) and
3424 ;; (CONS (INTEGER 0 3) (INTEGER 5 6)).
3425 #+nil
3426 ((csubtypep cdr-type1 cdr-type2)
3427 (frob-cdr car-type1 car-type2 cdr-type1 cdr-type2))
3428 #+nil
3429 ((csubtypep cdr-type2 cdr-type1)
3430 (frob-cdr car-type2 car-type1 cdr-type2 cdr-type1))))))
3432 (!define-type-method (cons :simple-intersection2) (type1 type2)
3433 (declare (type cons-type type1 type2))
3434 (let ((car-int2 (type-intersection2 (cons-type-car-type type1)
3435 (cons-type-car-type type2)))
3436 (cdr-int2 (type-intersection2 (cons-type-cdr-type type1)
3437 (cons-type-cdr-type type2))))
3438 (cond
3439 ((and car-int2 cdr-int2) (make-cons-type car-int2 cdr-int2))
3440 (car-int2 (make-cons-type car-int2
3441 (type-intersection
3442 (cons-type-cdr-type type1)
3443 (cons-type-cdr-type type2))))
3444 (cdr-int2 (make-cons-type
3445 (type-intersection (cons-type-car-type type1)
3446 (cons-type-car-type type2))
3447 cdr-int2)))))
3449 (!define-superclasses cons ((cons)) !cold-init-forms)
3451 ;;;; CHARACTER-SET types
3453 ;; all character-set types are enumerable, but it's not possible
3454 ;; for one to be TYPE= to a MEMBER type because (MEMBER #\x)
3455 ;; is not internally represented as a MEMBER type.
3456 ;; So in case it wasn't clear already ENUMERABLE-P does not mean
3457 ;; "possibly a MEMBER type in the Lisp-theoretic sense",
3458 ;; but means "could be implemented in SBCL as a MEMBER type".
3459 (!define-type-class character-set :enumerable nil
3460 :might-contain-other-types nil)
3462 (!def-type-translator character-set
3463 (&optional (pairs '((0 . #.(1- sb!xc:char-code-limit)))))
3464 (make-character-set-type :pairs pairs))
3466 (!define-type-method (character-set :negate) (type)
3467 (let ((pairs (character-set-type-pairs type)))
3468 (if (and (= (length pairs) 1)
3469 (= (caar pairs) 0)
3470 (= (cdar pairs) (1- sb!xc:char-code-limit)))
3471 (make-negation-type :type type)
3472 (let ((not-character
3473 (make-negation-type
3474 :type (make-character-set-type
3475 :pairs '((0 . #.(1- sb!xc:char-code-limit)))))))
3476 (type-union
3477 not-character
3478 (make-character-set-type
3479 :pairs (let (not-pairs)
3480 (when (> (caar pairs) 0)
3481 (push (cons 0 (1- (caar pairs))) not-pairs))
3482 (do* ((tail pairs (cdr tail))
3483 (high1 (cdar tail) (cdar tail))
3484 (low2 (caadr tail) (caadr tail)))
3485 ((null (cdr tail))
3486 (when (< (cdar tail) (1- sb!xc:char-code-limit))
3487 (push (cons (1+ (cdar tail))
3488 (1- sb!xc:char-code-limit))
3489 not-pairs))
3490 (nreverse not-pairs))
3491 (push (cons (1+ high1) (1- low2)) not-pairs)))))))))
3493 (!define-type-method (character-set :unparse) (type)
3494 (cond
3495 ((type= type (specifier-type 'character)) 'character)
3496 ((type= type (specifier-type 'base-char)) 'base-char)
3497 ((type= type (specifier-type 'extended-char)) 'extended-char)
3498 ((type= type (specifier-type 'standard-char)) 'standard-char)
3500 ;; Unparse into either MEMBER or CHARACTER-SET. We use MEMBER if there
3501 ;; are at most as many characters as there are character code ranges.
3502 ;; (basically saying to use MEMBER if each range is one character)
3503 (let* ((pairs (character-set-type-pairs type))
3504 (count (length pairs))
3505 (chars (loop named outer
3506 for (low . high) in pairs
3507 nconc (loop for code from low upto high
3508 collect (sb!xc:code-char code)
3509 when (minusp (decf count))
3510 do (return-from outer t)))))
3511 (if (eq chars t)
3512 `(character-set ,pairs)
3513 `(member ,@chars))))))
3515 (!define-type-method (character-set :singleton-p) (type)
3516 (let* ((pairs (character-set-type-pairs type))
3517 (pair (first pairs)))
3518 (if (and (typep pairs '(cons t null))
3519 (eql (car pair) (cdr pair)))
3520 (values t (code-char (car pair)))
3521 (values nil nil))))
3523 (!define-type-method (character-set :simple-=) (type1 type2)
3524 (let ((pairs1 (character-set-type-pairs type1))
3525 (pairs2 (character-set-type-pairs type2)))
3526 (values (equal pairs1 pairs2) t)))
3528 (!define-type-method (character-set :simple-subtypep) (type1 type2)
3529 (values
3530 (dolist (pair (character-set-type-pairs type1) t)
3531 (unless (position pair (character-set-type-pairs type2)
3532 :test (lambda (x y) (and (>= (car x) (car y))
3533 (<= (cdr x) (cdr y)))))
3534 (return nil)))
3537 (!define-type-method (character-set :simple-union2) (type1 type2)
3538 ;; KLUDGE: the canonizing in the MAKE-CHARACTER-SET-TYPE function
3539 ;; actually does the union for us. It might be a little fragile to
3540 ;; rely on it.
3541 (make-character-set-type
3542 :pairs (merge 'list
3543 (copy-alist (character-set-type-pairs type1))
3544 (copy-alist (character-set-type-pairs type2))
3545 #'< :key #'car)))
3547 (!define-type-method (character-set :simple-intersection2) (type1 type2)
3548 ;; KLUDGE: brute force.
3550 (let (pairs)
3551 (dolist (pair1 (character-set-type-pairs type1)
3552 (make-character-set-type
3553 :pairs (sort pairs #'< :key #'car)))
3554 (dolist (pair2 (character-set-type-pairs type2))
3555 (cond
3556 ((<= (car pair1) (car pair2) (cdr pair1))
3557 (push (cons (car pair2) (min (cdr pair1) (cdr pair2))) pairs))
3558 ((<= (car pair2) (car pair1) (cdr pair2))
3559 (push (cons (car pair1) (min (cdr pair1) (cdr pair2))) pairs))))))
3561 (make-character-set-type
3562 :pairs (intersect-type-pairs
3563 (character-set-type-pairs type1)
3564 (character-set-type-pairs type2))))
3567 ;;; Intersect two ordered lists of pairs
3568 ;;; Each list is of the form ((start1 . end1) ... (startn . endn)),
3569 ;;; where start1 <= end1 < start2 <= end2 < ... < startn <= endn.
3570 ;;; Each pair represents the integer interval start..end.
3572 (defun intersect-type-pairs (alist1 alist2)
3573 (if (and alist1 alist2)
3574 (let ((res nil)
3575 (pair1 (pop alist1))
3576 (pair2 (pop alist2)))
3577 (loop
3578 (when (> (car pair1) (car pair2))
3579 (rotatef pair1 pair2)
3580 (rotatef alist1 alist2))
3581 (let ((pair1-cdr (cdr pair1)))
3582 (cond
3583 ((> (car pair2) pair1-cdr)
3584 ;; No over lap -- discard pair1
3585 (unless alist1 (return))
3586 (setq pair1 (pop alist1)))
3587 ((<= (cdr pair2) pair1-cdr)
3588 (push (cons (car pair2) (cdr pair2)) res)
3589 (cond
3590 ((= (cdr pair2) pair1-cdr)
3591 (unless alist1 (return))
3592 (unless alist2 (return))
3593 (setq pair1 (pop alist1)
3594 pair2 (pop alist2)))
3595 (t ;; (< (cdr pair2) pair1-cdr)
3596 (unless alist2 (return))
3597 (setq pair1 (cons (1+ (cdr pair2)) pair1-cdr))
3598 (setq pair2 (pop alist2)))))
3599 (t ;; (> (cdr pair2) (cdr pair1))
3600 (push (cons (car pair2) pair1-cdr) res)
3601 (unless alist1 (return))
3602 (setq pair2 (cons (1+ pair1-cdr) (cdr pair2)))
3603 (setq pair1 (pop alist1))))))
3604 (nreverse res))
3605 nil))
3608 ;;; Return the type that describes all objects that are in X but not
3609 ;;; in Y. If we can't determine this type, then return NIL.
3611 ;;; For now, we only are clever dealing with union and member types.
3612 ;;; If either type is not a union type, then we pretend that it is a
3613 ;;; union of just one type. What we do is remove from X all the types
3614 ;;; that are a subtype any type in Y. If any type in X intersects with
3615 ;;; a type in Y but is not a subtype, then we give up.
3617 ;;; We must also special-case any member type that appears in the
3618 ;;; union. We remove from X's members all objects that are TYPEP to Y.
3619 ;;; If Y has any members, we must be careful that none of those
3620 ;;; members are CTYPEP to any of Y's non-member types. We give up in
3621 ;;; this case, since to compute that difference we would have to break
3622 ;;; the type from X into some collection of types that represents the
3623 ;;; type without that particular element. This seems too hairy to be
3624 ;;; worthwhile, given its low utility.
3625 (defun type-difference (x y)
3626 (if (and (numeric-type-p x) (numeric-type-p y))
3627 ;; Numeric types are easy. Are there any others we should handle like this?
3628 (type-intersection x (type-negation y))
3629 (let ((x-types (if (union-type-p x) (union-type-types x) (list x)))
3630 (y-types (if (union-type-p y) (union-type-types y) (list y))))
3631 (collect ((res))
3632 (dolist (x-type x-types)
3633 (if (member-type-p x-type)
3634 (let ((xset (alloc-xset))
3635 (fp-zeroes nil))
3636 (mapc-member-type-members
3637 (lambda (elt)
3638 (multiple-value-bind (ok sure) (ctypep elt y)
3639 (unless sure
3640 (return-from type-difference nil))
3641 (unless ok
3642 (if (fp-zero-p elt)
3643 (pushnew elt fp-zeroes)
3644 (add-to-xset elt xset)))))
3645 x-type)
3646 (unless (and (xset-empty-p xset) (not fp-zeroes))
3647 (res (make-member-type :xset xset :fp-zeroes fp-zeroes))))
3648 (dolist (y-type y-types (res x-type))
3649 (multiple-value-bind (val win) (csubtypep x-type y-type)
3650 (unless win (return-from type-difference nil))
3651 (when val (return))
3652 (when (types-equal-or-intersect x-type y-type)
3653 (return-from type-difference nil))))))
3654 (let ((y-mem (find-if #'member-type-p y-types)))
3655 (when y-mem
3656 (dolist (x-type x-types)
3657 (unless (member-type-p x-type)
3658 (mapc-member-type-members
3659 (lambda (member)
3660 (multiple-value-bind (ok sure) (ctypep member x-type)
3661 (when (or (not sure) ok)
3662 (return-from type-difference nil))))
3663 y-mem)))))
3664 (apply #'type-union (res))))))
3666 (!def-type-translator array (&optional (element-type '*)
3667 (dimensions '*))
3668 (let ((eltype (if (eq element-type '*)
3669 *wild-type*
3670 (specifier-type element-type))))
3671 (make-array-type (canonical-array-dimensions dimensions)
3672 :complexp :maybe
3673 :element-type eltype
3674 :specialized-element-type (%upgraded-array-element-type
3675 eltype))))
3677 (!def-type-translator simple-array (&optional (element-type '*)
3678 (dimensions '*))
3679 (let ((eltype (if (eq element-type '*)
3680 *wild-type*
3681 (specifier-type element-type))))
3682 (make-array-type (canonical-array-dimensions dimensions)
3683 :complexp nil
3684 :element-type eltype
3685 :specialized-element-type (%upgraded-array-element-type
3686 eltype))))
3688 ;;;; SIMD-PACK types
3689 #!+sb-simd-pack
3690 (progn
3691 (!define-type-class simd-pack :enumerable nil
3692 :might-contain-other-types nil)
3694 (!def-type-translator simd-pack (&optional (element-type-spec '*))
3695 (if (eql element-type-spec '*)
3696 (%make-simd-pack-type *simd-pack-element-types*)
3697 (make-simd-pack-type (single-value-specifier-type element-type-spec))))
3699 (!define-type-method (simd-pack :negate) (type)
3700 (let ((remaining (set-difference *simd-pack-element-types*
3701 (simd-pack-type-element-type type)))
3702 (not-simd-pack (make-negation-type :type (specifier-type 'simd-pack))))
3703 (if remaining
3704 (type-union not-simd-pack (%make-simd-pack-type remaining))
3705 not-simd-pack)))
3707 (!define-type-method (simd-pack :unparse) (type)
3708 (let ((eltypes (simd-pack-type-element-type type)))
3709 (cond ((equal eltypes *simd-pack-element-types*)
3710 'simd-pack)
3711 ((= 1 (length eltypes))
3712 `(simd-pack ,(first eltypes)))
3714 `(or ,@(mapcar (lambda (eltype)
3715 `(simd-pack ,eltype))
3716 eltypes))))))
3718 (!define-type-method (simd-pack :simple-=) (type1 type2)
3719 (declare (type simd-pack-type type1 type2))
3720 (null (set-exclusive-or (simd-pack-type-element-type type1)
3721 (simd-pack-type-element-type type2))))
3723 (!define-type-method (simd-pack :simple-subtypep) (type1 type2)
3724 (declare (type simd-pack-type type1 type2))
3725 (subsetp (simd-pack-type-element-type type1)
3726 (simd-pack-type-element-type type2)))
3728 (!define-type-method (simd-pack :simple-union2) (type1 type2)
3729 (declare (type simd-pack-type type1 type2))
3730 (%make-simd-pack-type (union (simd-pack-type-element-type type1)
3731 (simd-pack-type-element-type type2))))
3733 (!define-type-method (simd-pack :simple-intersection2) (type1 type2)
3734 (declare (type simd-pack-type type1 type2))
3735 (let ((intersection (intersection (simd-pack-type-element-type type1)
3736 (simd-pack-type-element-type type2))))
3737 (if intersection
3738 (%make-simd-pack-type intersection)
3739 *empty-type*)))
3741 (!define-superclasses simd-pack ((simd-pack)) !cold-init-forms))
3743 ;;;; utilities shared between cross-compiler and target system
3745 ;;; Does the type derived from compilation of an actual function
3746 ;;; definition satisfy declarations of a function's type?
3747 (defun defined-ftype-matches-declared-ftype-p (defined-ftype declared-ftype)
3748 (declare (type ctype defined-ftype declared-ftype))
3749 (flet ((is-built-in-class-function-p (ctype)
3750 (and (built-in-classoid-p ctype)
3751 (eq (built-in-classoid-name ctype) 'function))))
3752 (cond (;; DECLARED-FTYPE could certainly be #<BUILT-IN-CLASS FUNCTION>;
3753 ;; that's what happens when we (DECLAIM (FTYPE FUNCTION FOO)).
3754 (is-built-in-class-function-p declared-ftype)
3755 ;; In that case, any definition satisfies the declaration.
3757 (;; It's not clear whether or how DEFINED-FTYPE might be
3758 ;; #<BUILT-IN-CLASS FUNCTION>, but it's not obviously
3759 ;; invalid, so let's handle that case too, just in case.
3760 (is-built-in-class-function-p defined-ftype)
3761 ;; No matter what DECLARED-FTYPE might be, we can't prove
3762 ;; that an object of type FUNCTION doesn't satisfy it, so
3763 ;; we return success no matter what.
3765 (;; Otherwise both of them must be FUN-TYPE objects.
3767 ;; FIXME: For now we only check compatibility of the return
3768 ;; type, not argument types, and we don't even check the
3769 ;; return type very precisely (as per bug 94a). It would be
3770 ;; good to do a better job. Perhaps to check the
3771 ;; compatibility of the arguments, we should (1) redo
3772 ;; VALUES-TYPES-EQUAL-OR-INTERSECT as
3773 ;; ARGS-TYPES-EQUAL-OR-INTERSECT, and then (2) apply it to
3774 ;; the ARGS-TYPE slices of the FUN-TYPEs. (ARGS-TYPE
3775 ;; is a base class both of VALUES-TYPE and of FUN-TYPE.)
3776 (values-types-equal-or-intersect
3777 (fun-type-returns defined-ftype)
3778 (fun-type-returns declared-ftype))))))
3780 ;;; This messy case of CTYPE for NUMBER is shared between the
3781 ;;; cross-compiler and the target system.
3782 (defun ctype-of-number (x)
3783 (let ((num (if (complexp x) (realpart x) x)))
3784 (multiple-value-bind (complexp low high)
3785 (if (complexp x)
3786 (let ((imag (imagpart x)))
3787 (values :complex (min num imag) (max num imag)))
3788 (values :real num num))
3789 (make-numeric-type :class (etypecase num
3790 (integer (if (complexp x)
3791 (if (integerp (imagpart x))
3792 'integer
3793 'rational)
3794 'integer))
3795 (rational 'rational)
3796 (float 'float))
3797 :format (and (floatp num) (float-format-name num))
3798 :complexp complexp
3799 :low low
3800 :high high))))
3802 ;;; The following function is a generic driver for approximating
3803 ;;; set-valued functions over types. Putting this here because it'll
3804 ;;; probably be useful for a lot of type analyses.
3806 ;;; Let f be a function from values of type X to Y, e.g., ARRAY-RANK.
3808 ;;; We compute an over or under-approximation of the set
3810 ;;; F(TYPE) = { f(x) : x in TYPE /\ x in X } \subseteq Y
3812 ;;; via set-valued approximations of f, OVER and UNDER.
3814 ;;; These functions must have the property that
3815 ;;; Forall TYPE, OVER(TYPE) \superseteq F(TYPE) and
3816 ;;; Forall TYPE, UNDER(TYPE) \subseteq F(TYPE)
3818 ;;; The driver is also parameterised over the finite set
3819 ;;; representation.
3821 ;;; Union, intersection and difference are binary functions to compute
3822 ;;; set union, intersection and difference. Top and bottom are the
3823 ;;; concrete representations for the universe and empty sets; we never
3824 ;;; call the set functions on top or bottom, so it's safe to use
3825 ;;; special values there.
3827 ;;; Arguments:
3829 ;;; TYPE: the ctype for which we wish to approximate F(TYPE)
3830 ;;; OVERAPPROXIMATE: true if we wish to overapproximate, nil otherwise.
3831 ;;; You usually want T.
3832 ;;; UNION/INTERSECTION/DIFFERENCE: implementations of finite set operations.
3833 ;;; Conform to cl::(union/intersection/set-difference). Passing NIL will
3834 ;;; disable some cleverness and result in quicker computation of coarser
3835 ;;; approximations. However, passing difference without union and intersection
3836 ;;; will probably not end well.
3837 ;;; TOP/BOTTOM: concrete representation of the universe and empty set. Finite
3838 ;;; set operations are never called on TOP/BOTTOM, so it's safe to use special
3839 ;;; values there.
3840 ;;; OVER/UNDER: the set-valued approximations of F.
3842 ;;; Implementation details.
3844 ;;; It's a straightforward walk down the type.
3845 ;;; Union types -> take the union of children, intersection ->
3846 ;;; intersect. There is some complication for negation types: we must
3847 ;;; not only negate the result, but also flip from overapproximating
3848 ;;; to underapproximating in the children (or vice versa).
3850 ;;; We represent sets as a pair of (negate-p finite-set) in order to
3851 ;;; support negation types.
3853 (declaim (inline generic-abstract-type-function))
3854 (defun generic-abstract-type-function
3855 (type overapproximate
3856 union intersection difference
3857 top bottom
3858 over under)
3859 (labels ((union* (x y)
3860 ;; wrappers to avoid calling union/intersection on
3861 ;; top/bottom.
3862 (cond ((or (eql x top)
3863 (eql y top))
3864 top)
3865 ((eql x bottom) y)
3866 ((eql y bottom) x)
3868 (funcall union x y))))
3869 (intersection* (x y)
3870 (cond ((or (eql x bottom)
3871 (eql y bottom))
3872 bottom)
3873 ((eql x top) y)
3874 ((eql y top) x)
3876 (funcall intersection x y))))
3877 (unite (not-x-p x not-y-p y)
3878 ;; if we only have one negated set, it's x.
3879 (when not-y-p
3880 (rotatef not-x-p not-y-p)
3881 (rotatef x y))
3882 (cond ((and not-x-p not-y-p)
3883 ;; -x \/ -y = -(x /\ y)
3884 (normalize t (intersection* x y)))
3885 (not-x-p
3886 ;; -x \/ y = -(x \ y)
3887 (cond ((eql x top)
3888 (values nil y))
3889 ((or (eql y top)
3890 (eql x bottom))
3891 (values nil top))
3892 ((eql y bottom)
3893 (values t x))
3895 (normalize t
3896 (funcall difference x y)))))
3898 (values nil (union* x y)))))
3899 (intersect (not-x-p x not-y-p y)
3900 (when not-y-p
3901 (rotatef not-x-p not-y-p)
3902 (rotatef x y))
3903 (cond ((and not-x-p not-y-p)
3904 ;; -x /\ -y = -(x \/ y)
3905 (normalize t (union* x y)))
3906 (not-x-p
3907 ;; -x /\ y = y \ x
3908 (cond ((or (eql x top) (eql y bottom))
3909 (values nil bottom))
3910 ((eql x bottom)
3911 (values nil y))
3912 ((eql y top)
3913 (values t x))
3915 (values nil (funcall difference y x)))))
3917 (values nil (intersection* x y)))))
3918 (normalize (not-x-p x)
3919 ;; catch some easy cases of redundant negation.
3920 (cond ((not not-x-p)
3921 (values nil x))
3922 ((eql x top)
3923 bottom)
3924 ((eql x bottom)
3925 top)
3927 (values t x))))
3928 (default (overapproximate)
3929 ;; default value
3930 (if overapproximate top bottom))
3931 (walk-union (types overapproximate)
3932 ;; Only do this if union is provided.
3933 (unless union
3934 (return-from walk-union (default overapproximate)))
3935 ;; Reduce/union from bottom.
3936 (let ((not-acc-p nil)
3937 (acc bottom))
3938 (dolist (type types (values not-acc-p acc))
3939 (multiple-value-bind (not x)
3940 (walk type overapproximate)
3941 (setf (values not-acc-p acc)
3942 (unite not-acc-p acc not x)))
3943 ;; Early exit on top set.
3944 (when (and (eql acc top)
3945 (not not-acc-p))
3946 (return (values nil top))))))
3947 (walk-intersection (types overapproximate)
3948 ;; Skip if we don't know how to intersect sets
3949 (unless intersection
3950 (return-from walk-intersection (default overapproximate)))
3951 ;; Reduce/intersection from top
3952 (let ((not-acc-p nil)
3953 (acc top))
3954 (dolist (type types (values not-acc-p acc))
3955 (multiple-value-bind (not x)
3956 (walk type overapproximate)
3957 (setf (values not-acc-p acc)
3958 (intersect not-acc-p acc not x)))
3959 (when (and (eql acc bottom)
3960 (not not-acc-p))
3961 (return (values nil bottom))))))
3962 (walk-negate (type overapproximate)
3963 ;; Don't introduce negated types if we don't know how to
3964 ;; subtract sets.
3965 (unless difference
3966 (return-from walk-negate (default overapproximate)))
3967 (multiple-value-bind (not x)
3968 (walk type (not overapproximate))
3969 (normalize (not not) x)))
3970 (walk (type overapproximate)
3971 (typecase type
3972 (union-type
3973 (walk-union (union-type-types type) overapproximate))
3974 ((cons (member or union))
3975 (walk-union (rest type) overapproximate))
3976 (intersection-type
3977 (walk-intersection (intersection-type-types type) overapproximate))
3978 ((cons (member and intersection))
3979 (walk-intersection (rest type) overapproximate))
3980 (negation-type
3981 (walk-negate (negation-type-type type) overapproximate))
3982 ((cons (eql not))
3983 (walk-negate (second type) overapproximate))
3985 (values nil
3986 (if overapproximate
3987 (if over
3988 (funcall over type)
3989 (default t))
3990 (if under
3991 (funcall under type)
3992 (default nil))))))))
3993 (multiple-value-call #'normalize (walk type overapproximate))))
3994 (declaim (notinline generic-abstract-type-function))
3996 ;;; Standard list representation of sets. Use CL:* for the universe.
3997 (defun list-abstract-type-function (type over &key under (overapproximate t))
3998 (declare (inline generic-abstract-type-function))
3999 (generic-abstract-type-function
4000 type overapproximate
4001 #'union #'intersection #'set-difference
4002 '* nil
4003 over under))
4005 (!defun-from-collected-cold-init-forms !late-type-cold-init)
4007 #-sb-xc (!late-type-cold-init2)
4009 (/show0 "late-type.lisp end of file")