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