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