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