0.6.8.9:
[sbcl.git] / src / code / late-type.lisp
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..)
7
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.
16
17 (in-package "SB!KERNEL")
18
19 (!begin-collecting-cold-init-forms)
20
21 ;;; ### Remaining incorrectnesses:
22 ;;;
23 ;;; TYPE-UNION (and the OR type) doesn't properly canonicalize an
24 ;;; exhaustive partition or coalesce contiguous ranges of numeric
25 ;;; types.
26 ;;;
27 ;;; There are all sorts of nasty problems with open bounds on FLOAT
28 ;;; types (and probably FLOAT types in general.)
29 ;;;
30 ;;; RATIO and BIGNUM are not recognized as numeric types.
31
32 ;;; FIXME: It seems to me that this should be set to NIL by default,
33 ;;; and perhaps not even optionally set to T.
34 (defvar *use-implementation-types* t
35   #!+sb-doc
36   "*USE-IMPLEMENTATION-TYPES* is a semi-public flag which determines how
37    restrictive we are in determining type membership. If two types are the
38    same in the implementation, then we will consider them them the same when
39    this switch is on. When it is off, we try to be as restrictive as the
40    language allows, allowing us to detect more errors. Currently, this only
41    affects array types.")
42
43 (!cold-init-forms (setq *use-implementation-types* t))
44
45 ;;; These functions are used as method for types which need a complex
46 ;;; subtypep method to handle some superclasses, but cover a subtree
47 ;;; of the type graph (i.e. there is no simple way for any other type
48 ;;; class to be a subtype.) There are always still complex ways,
49 ;;; namely UNION and MEMBER types, so we must give TYPE1's method a
50 ;;; chance to run, instead of immediately returning NIL, T.
51 (defun delegate-complex-subtypep-arg2 (type1 type2)
52   (let ((subtypep-arg1
53          (type-class-complex-subtypep-arg1
54           (type-class-info type1))))
55     (if subtypep-arg1
56         (funcall subtypep-arg1 type1 type2)
57         (values nil t))))
58 (defun delegate-complex-intersection (type1 type2)
59   (let ((method (type-class-complex-intersection (type-class-info type1))))
60     (if (and method (not (eq method #'delegate-complex-intersection)))
61         (funcall method type2 type1)
62         (vanilla-intersection type1 type2))))
63
64 ;;; This is used by DEFINE-SUPERCLASSES to define the SUBTYPE-ARG1
65 ;;; method. INFO is a list of conses (SUPERCLASS-CLASS .
66 ;;; {GUARD-TYPE-SPECIFIER | NIL}). This will never be called with a
67 ;;; hairy type as TYPE2, since the hairy type TYPE2 method gets first
68 ;;; crack.
69 ;;;
70 ;;; FIXME: Declare this as INLINE, since it's only used in one place.
71 (defun has-superclasses-complex-subtypep-arg1 (type1 type2 info)
72   (values
73    (and (sb!xc:typep type2 'sb!xc:class)
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 (class-layout (car x)))))
80                    (dotimes (i (length inherits) nil)
81                      (when (eq type2 (layout-class (svref inherits i)))
82                        (return t)))))))))
83    t))
84
85 ;;; This function takes a list of specs, each of the form
86 ;;;    (SUPERCLASS-NAME &OPTIONAL GUARD).
87 ;;; Consider one spec (with no guard): any instance of the named
88 ;;; TYPE-CLASS is also a subtype of the named superclass and of any of
89 ;;; its superclasses. If there are multiple specs, then some will have
90 ;;; guards. We choose the first spec whose guard is a supertype of
91 ;;; TYPE1 and use its superclass. In effect, a sequence of guards
92 ;;;    G0, G1, G2
93 ;;; is actually
94 ;;;    G0,(and G1 (not G0)), (and G2 (not (or G0 G1))).
95 ;;;
96 ;;; WHEN controls when the forms are executed.
97 (defmacro define-superclasses (type-class-name specs when)
98   (let ((type-class (gensym "TYPE-CLASS-"))
99         (info (gensym "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 (sb!xc:find-class 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-intersection ,type-class)
114                #'delegate-complex-intersection)))))
115 \f
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:
125
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.
131
132 ;;; the description of a keyword argument
133 (defstruct (key-info #-sb-xc-host (:pure t))
134   ;; the keyword
135   (name (required-argument) :type keyword)
136   ;; the type of the argument value
137   (type (required-argument) :type ctype))
138
139 (define-type-method (values :simple-subtypep :complex-subtypep-arg1)
140                     (type1 type2)
141   (declare (ignore type2))
142   (error "Subtypep is illegal on this type:~%  ~S" (type-specifier type1)))
143
144 (define-type-method (values :complex-subtypep-arg2)
145                     (type1 type2)
146   (declare (ignore type1))
147   (error "Subtypep is illegal on this type:~%  ~S" (type-specifier type2)))
148
149 (define-type-method (values :unparse) (type)
150   (cons 'values (unparse-args-types type)))
151
152 ;;; Return true if LIST1 and LIST2 have the same elements in the same
153 ;;; positions according to TYPE=. We return NIL, NIL if there is an
154 ;;; uncertain comparison.
155 (defun type=-list (list1 list2)
156   (declare (list list1 list2))
157   (do ((types1 list1 (cdr types1))
158        (types2 list2 (cdr types2)))
159       ((or (null types1) (null types2))
160        (if (or types1 types2)
161            (values nil t)
162            (values t t)))
163     (multiple-value-bind (val win)
164         (type= (first types1) (first types2))
165       (unless win
166         (return (values nil nil)))
167       (unless val
168         (return (values nil t))))))
169
170 (define-type-method (values :simple-=) (type1 type2)
171   (let ((rest1 (args-type-rest type1))
172         (rest2 (args-type-rest type2)))
173     (cond ((or (args-type-keyp type1) (args-type-keyp type2)
174                (args-type-allowp type1) (args-type-allowp type2))
175            (values nil nil))
176           ((and rest1 rest2 (type/= rest1 rest2))
177            (type= rest1 rest2))
178           ((or rest1 rest2)
179            (values nil t))
180           (t
181            (multiple-value-bind (req-val req-win)
182                (type=-list (values-type-required type1)
183                            (values-type-required type2))
184              (multiple-value-bind (opt-val opt-win)
185                  (type=-list (values-type-optional type1)
186                              (values-type-optional type2))
187                (values (and req-val opt-val) (and req-win opt-win))))))))
188
189 (define-type-class function)
190
191 ;;; a flag that we can bind to cause complex function types to be
192 ;;; unparsed as FUNCTION. This is useful when we want a type that we
193 ;;; can pass to TYPEP.
194 (defvar *unparse-function-type-simplify*)
195 (!cold-init-forms (setq *unparse-function-type-simplify* nil))
196
197 (define-type-method (function :unparse) (type)
198   (if *unparse-function-type-simplify*
199       'function
200       (list 'function
201             (if (function-type-wild-args type)
202                 '*
203                 (unparse-args-types type))
204             (type-specifier
205              (function-type-returns type)))))
206
207 ;;; Since all function types are equivalent to FUNCTION, they are all
208 ;;; subtypes of each other.
209 (define-type-method (function :simple-subtypep) (type1 type2)
210   (declare (ignore type1 type2))
211   (values t t))
212
213 (define-superclasses function ((function)) !cold-init-forms)
214
215 ;;; The union or intersection of two FUNCTION types is FUNCTION.
216 (define-type-method (function :simple-union) (type1 type2)
217   (declare (ignore type1 type2))
218   (specifier-type 'function))
219 (define-type-method (function :simple-intersection) (type1 type2)
220   (declare (ignore type1 type2))
221   (values (specifier-type 'function) t))
222
223 ;;; ### Not very real, but good enough for redefining transforms
224 ;;; according to type:
225 (define-type-method (function :simple-=) (type1 type2)
226   (values (equalp type1 type2) t))
227
228 (define-type-class constant :inherits values)
229
230 (define-type-method (constant :unparse) (type)
231   `(constant-argument ,(type-specifier (constant-type-type type))))
232
233 (define-type-method (constant :simple-=) (type1 type2)
234   (type= (constant-type-type type1) (constant-type-type type2)))
235
236 (def-type-translator constant-argument (type)
237   (make-constant-type :type (specifier-type type)))
238
239 ;;; Given a LAMBDA-LIST-like values type specification and an ARGS-TYPE
240 ;;; structure, fill in the slots in the structure accordingly. This is
241 ;;; used for both FUNCTION and VALUES types.
242 (declaim (ftype (function (list args-type) (values)) parse-args-types))
243 (defun parse-args-types (lambda-list result)
244   (multiple-value-bind (required optional restp rest keyp keys allowp aux)
245       (parse-lambda-list lambda-list)
246     (when aux
247       (error "&Aux in a FUNCTION or VALUES type: ~S." lambda-list))
248     (setf (args-type-required result) (mapcar #'specifier-type required))
249     (setf (args-type-optional result) (mapcar #'specifier-type optional))
250     (setf (args-type-rest result) (if restp (specifier-type rest) nil))
251     (setf (args-type-keyp result) keyp)
252     (collect ((key-info))
253       (dolist (key keys)
254         (unless (proper-list-of-length-p key 2)
255           (error "Keyword type description is not a two-list: ~S." key))
256         (let ((kwd (first key)))
257           (when (find kwd (key-info) :key #'key-info-name)
258             (error "Repeated keyword ~S in lambda list: ~S." kwd lambda-list))
259           (key-info (make-key-info :name kwd
260                                    :type (specifier-type (second key))))))
261       (setf (args-type-keywords result) (key-info)))
262     (setf (args-type-allowp result) allowp)
263     (values)))
264
265 ;;; Return the lambda-list-like type specification corresponding
266 ;;; to an ARGS-TYPE.
267 (declaim (ftype (function (args-type) list) unparse-args-types))
268 (defun unparse-args-types (type)
269   (collect ((result))
270
271     (dolist (arg (args-type-required type))
272       (result (type-specifier arg)))
273
274     (when (args-type-optional type)
275       (result '&optional)
276       (dolist (arg (args-type-optional type))
277         (result (type-specifier arg))))
278
279     (when (args-type-rest type)
280       (result '&rest)
281       (result (type-specifier (args-type-rest type))))
282
283     (when (args-type-keyp type)
284       (result '&key)
285       (dolist (key (args-type-keywords type))
286         (result (list (key-info-name key)
287                       (type-specifier (key-info-type key))))))
288
289     (when (args-type-allowp type)
290       (result '&allow-other-keys))
291
292     (result)))
293
294 (def-type-translator function (&optional (args '*) (result '*))
295   (let ((res (make-function-type
296               :returns (values-specifier-type result))))
297     (if (eq args '*)
298         (setf (function-type-wild-args res) t)
299         (parse-args-types args res))
300     res))
301
302 (def-type-translator values (&rest values)
303   (let ((res (make-values-type)))
304     (parse-args-types values res)
305     res))
306 \f
307 ;;;; VALUES types interfaces
308 ;;;;
309 ;;;; We provide a few special operations that can be meaningfully used
310 ;;;; on VALUES types (as well as on any other type).
311
312 ;;; Return the type of the first value indicated by Type. This is used
313 ;;; by people who don't want to have to deal with values types.
314
315 ;;; MNA: fix-instance-typep-call patch
316 #!-sb-fluid (declaim (freeze-type values-type))
317 ; (inline single-value-type))
318 (defun single-value-type (type)
319   (declare (type ctype type))
320   (cond ((values-type-p type)
321          (or (car (args-type-required type))
322              (if (args-type-optional type)
323                  (type-union (car (args-type-optional type)) (specifier-type 'null)))
324              (args-type-rest type)
325              (specifier-type 'null)))
326         ((eq type *wild-type*)
327          *universal-type*)
328         (t
329          type)))
330
331 ;;; Return the minmum number of arguments that a function can be
332 ;;; called with, and the maximum number or NIL. If not a function
333 ;;; type, return NIL, NIL.
334 (defun function-type-nargs (type)
335   (declare (type ctype type))
336   (if (function-type-p type)
337       (let ((fixed (length (args-type-required type))))
338         (if (or (args-type-rest type)
339                 (args-type-keyp type)
340                 (args-type-allowp type))
341             (values fixed nil)
342             (values fixed (+ fixed (length (args-type-optional type))))))
343       (values nil nil)))
344
345 ;;; Determine if Type corresponds to a definite number of values. The
346 ;;; first value is a list of the types for each value, and the second
347 ;;; value is the number of values. If the number of values is not
348 ;;; fixed, then return NIL and :Unknown.
349 (defun values-types (type)
350   (declare (type ctype type))
351   (cond ((eq type *wild-type*)
352          (values nil :unknown))
353         ((not (values-type-p type))
354          (values (list type) 1))
355         ((or (args-type-optional type)
356              (args-type-rest type)
357              (args-type-keyp type)
358              (args-type-allowp type))
359          (values nil :unknown))
360         (t
361          (let ((req (args-type-required type)))
362            (values (mapcar #'single-value-type req) (length req))))))
363
364 ;;; Return two values:
365 ;;; MNA: fix-instance-typep-call patch
366 ;;; 1. A list of all the positional (fixed and optional) types.
367 ;;; 2] The rest type (if any).  If keywords allowed, *universal-type*.
368 ;;;    If no keywords or rest then the default-type.
369 (defun values-type-types (type &optional (default-type *empty-type*))
370   (declare (type values-type type))
371   (values (append (args-type-required type)
372                   (args-type-optional type))
373           (cond ((args-type-keyp type) *universal-type*)
374                 ((args-type-rest type))
375                 (t
376                   ;; MNA: fix-instance-typep-call patch
377                   default-type))))
378
379 ;;; Return a list of OPERATION applied to the types in TYPES1 and
380 ;;; TYPES2, padding with REST2 as needed. TYPES1 must not be shorter
381 ;;; than TYPES2. The second value is T if OPERATION always returned a
382 ;;; true second value.
383 (defun fixed-values-op (types1 types2 rest2 operation)
384   (declare (list types1 types2) (type ctype rest2) (type function operation))
385   (let ((exact t))
386     (values (mapcar #'(lambda (t1 t2)
387                         (multiple-value-bind (res win)
388                             (funcall operation t1 t2)
389                           (unless win
390                             (setq exact nil))
391                           res))
392                     types1
393                     (append types2
394                             (make-list (- (length types1) (length types2))
395                                        :initial-element rest2)))
396             exact)))
397
398 ;;; If Type isn't a values type, then make it into one:
399 ;;;    <type>  ==>  (values type &rest t)
400 (defun coerce-to-values (type)
401   (declare (type ctype type))
402   (if (values-type-p type)
403       type
404       (make-values-type :required (list type) :rest *universal-type*)))
405
406 ;;; Do the specified OPERATION on TYPE1 and TYPE2, which may be any
407 ;;; type, including VALUES types. With VALUES types such as:
408 ;;;    (VALUES a0 a1)
409 ;;;    (VALUES b0 b1)
410 ;;; we compute the more useful result
411 ;;;    (VALUES (<operation> a0 b0) (<operation> a1 b1))
412 ;;; rather than the precise result
413 ;;;    (<operation> (values a0 a1) (values b0 b1))
414 ;;; This has the virtue of always keeping the VALUES type specifier
415 ;;; outermost, and retains all of the information that is really
416 ;;; useful for static type analysis. We want to know what is always
417 ;;; true of each value independently. It is worthless to know that IF
418 ;;; the first value is B0 then the second will be B1.
419 ;;;
420 ;;; If the VALUES count signatures differ, then we produce a result with
421 ;;; the required VALUE count chosen by NREQ when applied to the number
422 ;;; of required values in TYPE1 and TYPE2. Any &KEY values become
423 ;;; &REST T (anyone who uses keyword values deserves to lose.)
424 ;;;
425 ;;; The second value is true if the result is definitely empty or if
426 ;;; OPERATION returned true as its second value each time we called
427 ;;; it. Since we approximate the intersection of VALUES types, the
428 ;;; second value being true doesn't mean the result is exact.
429 ;;; MNA: fix-instance-typep-call patch
430 (defun args-type-op (type1 type2 operation nreq default-type)
431   ;;; MNA: fix-instance-typep-call patch
432   (declare (type ctype type1 type2 default-type)
433            (type function operation nreq))
434   (if (or (values-type-p type1) (values-type-p type2))
435       (let ((type1 (coerce-to-values type1))
436             (type2 (coerce-to-values type2)))
437         (multiple-value-bind (types1 rest1)
438             ;;; MNA: fix-instance-typep-call patch
439             (values-type-types type1 default-type)
440           (multiple-value-bind (types2 rest2)
441               ;;; MNA: fix-instance-typep-call patch
442               (values-type-types type2 default-type)
443             (multiple-value-bind (rest rest-exact)
444                 (funcall operation rest1 rest2)
445               (multiple-value-bind (res res-exact)
446                   (if (< (length types1) (length types2))
447                       (fixed-values-op types2 types1 rest1 operation)
448                       (fixed-values-op types1 types2 rest2 operation))
449                 (let* ((req (funcall nreq
450                                      (length (args-type-required type1))
451                                      (length (args-type-required type2))))
452                        (required (subseq res 0 req))
453                        (opt (subseq res req))
454                        (opt-last (position rest opt :test-not #'type=
455                                            :from-end t)))
456                   (if (find *empty-type* required :test #'type=)
457                       (values *empty-type* t)
458                       (values (make-values-type
459                                :required required
460                                :optional (if opt-last
461                                              (subseq opt 0 (1+ opt-last))
462                                              ())
463                                ;; MNA fix-instance-typep-call patch
464                                :rest (if (eq rest default-type) nil rest))
465                               (and rest-exact res-exact)))))))))
466       (funcall operation type1 type2)))
467
468 ;;; Do a union or intersection operation on types that might be values
469 ;;; types. The result is optimized for utility rather than exactness,
470 ;;; but it is guaranteed that it will be no smaller (more restrictive)
471 ;;; than the precise result.
472 ;;;
473 ;;; The return convention seems to be analogous to
474 ;;; TYPES-INTERSECT. -- WHN 19990910.
475 (defun-cached (values-type-union :hash-function type-cache-hash
476                                  :hash-bits 8
477                                  :default nil
478                                  :init-wrapper !cold-init-forms)
479               ((type1 eq) (type2 eq))
480   (declare (type ctype type1 type2))
481   (cond ((or (eq type1 *wild-type*) (eq type2 *wild-type*)) *wild-type*)
482         ((eq type1 *empty-type*) type2)
483         ((eq type2 *empty-type*) type1)
484         (t
485           ;;; MNA: fix-instance-typep-call patch
486          (values (args-type-op type1 type2 #'type-union #'min *empty-type*)))))
487 ;;;
488 (defun-cached (values-type-intersection :hash-function type-cache-hash
489                                         :hash-bits 8
490                                         :values 2
491                                         :default (values nil :empty)
492                                         :init-wrapper !cold-init-forms)
493               ((type1 eq) (type2 eq))
494   (declare (type ctype type1 type2))
495   (cond ((eq type1 *wild-type*) (values type2 t))
496         ((eq type2 *wild-type*) (values type1 t))
497         (t
498          (args-type-op type1 type2 #'type-intersection #'max (specifier-type 'null)))))
499
500 ;;; This is like TYPES-INTERSECT, except that it sort of works on
501 ;;; VALUES types. Note that due to the semantics of
502 ;;; VALUES-TYPE-INTERSECTION, this might return (VALUES T T) when
503 ;;; there isn't really any intersection (?).
504 ;;;
505 ;;; The return convention seems to be analogous to
506 ;;; TYPES-INTERSECT. -- WHN 19990910.
507 (defun values-types-intersect (type1 type2)
508   (cond ((or (eq type1 *empty-type*) (eq type2 *empty-type*))
509          (values 't t))
510         ((or (values-type-p type1) (values-type-p type2))
511          (multiple-value-bind (res win) (values-type-intersection type1 type2)
512            (values (not (eq res *empty-type*))
513                    win)))
514         (t
515          (types-intersect type1 type2))))
516
517 ;;; a SUBTYPEP-like operation that can be used on any types, including
518 ;;; VALUES types
519 (defun-cached (values-subtypep :hash-function type-cache-hash
520                                :hash-bits 8
521                                :values 2
522                                :default (values nil :empty)
523                                :init-wrapper !cold-init-forms)
524               ((type1 eq) (type2 eq))
525   (declare (type ctype type1 type2))
526   (cond ((eq type2 *wild-type*) (values t t))
527         ((eq type1 *wild-type*)
528          (values (eq type2 *universal-type*) t))
529         ((not (values-types-intersect type1 type2))
530          (values nil t))
531         (t
532          (if (or (values-type-p type1) (values-type-p type2))
533              (let ((type1 (coerce-to-values type1))
534                    (type2 (coerce-to-values type2)))
535                (multiple-value-bind (types1 rest1) (values-type-types type1)
536                  (multiple-value-bind (types2 rest2) (values-type-types type2)
537                    (cond ((< (length (values-type-required type1))
538                              (length (values-type-required type2)))
539                           (values nil t))
540                          ((< (length types1) (length types2))
541                           (values nil nil))
542                          ((or (values-type-keyp type1)
543                               (values-type-keyp type2))
544                           (values nil nil))
545                          (t
546                           (do ((t1 types1 (rest t1))
547                                (t2 types2 (rest t2)))
548                               ((null t2)
549                                (csubtypep rest1 rest2))
550                             (multiple-value-bind (res win-p)
551                                 (csubtypep (first t1) (first t2))
552                               (unless win-p
553                                 (return (values nil nil)))
554                               (unless res
555                                 (return (values nil t))))))))))
556              (csubtypep type1 type2)))))
557 \f
558 ;;;; type method interfaces
559
560 ;;; like SUBTYPEP, only works on CTYPE structures
561 (defun-cached (csubtypep :hash-function type-cache-hash
562                          :hash-bits 8
563                          :values 2
564                          :default (values nil :empty)
565                          :init-wrapper !cold-init-forms)
566               ((type1 eq) (type2 eq))
567   (declare (type ctype type1 type2))
568   (cond ((or (eq type1 type2)
569              (eq type1 *empty-type*)
570              (eq type2 *wild-type*))
571          (values t t))
572         ((or (eq type1 *wild-type*)
573              (eq type2 *empty-type*))
574          (values nil t))
575         (t
576          (invoke-type-method :simple-subtypep :complex-subtypep-arg2
577                              type1 type2
578                              :complex-arg1 :complex-subtypep-arg1))))
579
580 ;;; Just parse the type specifiers and call CSUBTYPE.
581 (defun sb!xc:subtypep (type1 type2)
582   #!+sb-doc
583   "Return two values indicating the relationship between type1 and type2.
584   If values are T and T, type1 definitely is a subtype of type2.
585   If values are NIL and T, type1 definitely is not a subtype of type2.
586   If values are NIL and NIL, it couldn't be determined."
587   (csubtypep (specifier-type type1) (specifier-type type2)))
588
589 ;;; If two types are definitely equivalent, return true. The second
590 ;;; value indicates whether the first value is definitely correct.
591 ;;; This should only fail in the presence of HAIRY types.
592 (defun-cached (type= :hash-function type-cache-hash
593                      :hash-bits 8
594                      :values 2
595                      :default (values nil :empty)
596                      :init-wrapper !cold-init-forms)
597               ((type1 eq) (type2 eq))
598   (declare (type ctype type1 type2))
599   (if (eq type1 type2)
600       (values t t)
601       (invoke-type-method :simple-= :complex-= type1 type2)))
602
603 ;;; Not exactly the negation of TYPE=, since when the relationship is
604 ;;; uncertain, we still return NIL, NIL. This is useful in cases where
605 ;;; the conservative assumption is =.
606 (defun type/= (type1 type2)
607   (declare (type ctype type1 type2))
608   (multiple-value-bind (res win) (type= type1 type2)
609     (if win
610         (values (not res) t)
611         (values nil nil))))
612
613 ;;; Find a type which includes both types. Any inexactness is
614 ;;; represented by the fuzzy element types; we return a single value
615 ;;; that is precise to the best of our knowledge. This result is
616 ;;; simplified into the canonical form, thus is not a UNION type
617 ;;; unless there is no other way to represent the result.
618 (defun-cached (type-union :hash-function type-cache-hash
619                           :hash-bits 8
620                           :init-wrapper !cold-init-forms)
621               ((type1 eq) (type2 eq))
622   (declare (type ctype type1 type2))
623   (if (eq type1 type2)
624       type1
625       (let ((res (invoke-type-method :simple-union :complex-union
626                                      type1 type2
627                                      :default :vanilla)))
628         (cond ((eq res :vanilla)
629                (or (vanilla-union type1 type2)
630                    (make-union-type (list type1 type2))))
631               (res)
632               (t
633                (make-union-type (list type1 type2)))))))
634
635 ;;; Return as restrictive a type as we can discover that is no more
636 ;;; restrictive than the intersection of Type1 and Type2. The second
637 ;;; value is true if the result is exact. At worst, we randomly return
638 ;;; one of the arguments as the first value (trying not to return a
639 ;;; hairy type).
640 (defun-cached (type-intersection :hash-function type-cache-hash
641                                  :hash-bits 8
642                                  :values 2
643                                  :default (values nil :empty)
644                                  :init-wrapper !cold-init-forms)
645               ((type1 eq) (type2 eq))
646   (declare (type ctype type1 type2))
647   (if (eq type1 type2)
648       (values type1 t)
649       (invoke-type-method :simple-intersection :complex-intersection
650                           type1 type2
651                           :default (values *empty-type* t))))
652
653 ;;; The first value is true unless the types don't intersect. The
654 ;;; second value is true if the first value is definitely correct. NIL
655 ;;; is considered to intersect with any type. If T is a subtype of
656 ;;; either type, then we also return T, T. This way we consider hairy
657 ;;; types to intersect with T.
658 (defun types-intersect (type1 type2)
659   (declare (type ctype type1 type2))
660   (if (or (eq type1 *empty-type*) (eq type2 *empty-type*))
661       (values t t)
662       (multiple-value-bind (val winp) (type-intersection type1 type2)
663         (cond ((not winp)
664                (if (or (csubtypep *universal-type* type1)
665                        (csubtypep *universal-type* type2))
666                    (values t t)
667                    (values t nil)))
668               ((eq val *empty-type*) (values nil t))
669               (t (values t t))))))
670
671 ;;; Return a Common Lisp type specifier corresponding to the TYPE
672 ;;; object.
673 (defun type-specifier (type)
674   (declare (type ctype type))
675   (funcall (type-class-unparse (type-class-info type)) type))
676
677 ;;; (VALUES-SPECIFIER-TYPE and SPECIFIER-TYPE moved from here to
678 ;;; early-type.lisp by WHN ca. 19990201.)
679
680 ;;; Take a list of type specifiers, compute the translation and define
681 ;;; it as a builtin type.
682 (declaim (ftype (function (list) (values)) precompute-types))
683 (defun precompute-types (specs)
684   (dolist (spec specs)
685     (let ((res (specifier-type spec)))
686       (unless (unknown-type-p res)
687         (setf (info :type :builtin spec) res)
688         (setf (info :type :kind spec) :primitive))))
689   (values))
690 \f
691 ;;;; built-in types
692
693 (define-type-class named)
694
695 (defvar *wild-type*)
696 (defvar *empty-type*)
697 (defvar *universal-type*)
698
699 (!cold-init-forms
700  (macrolet ((frob (name var)
701               `(progn
702                  (setq ,var (make-named-type :name ',name))
703                  (setf (info :type :kind ',name) :primitive)
704                  (setf (info :type :builtin ',name) ,var))))
705    ;; KLUDGE: In ANSI, * isn't really the name of a type, it's just a
706    ;; special symbol which can be stuck in some places where an
707    ;; ordinary type can go, e.g. (ARRAY * 1) instead of (ARRAY T 1).
708    ;; At some point, in order to become more standard, we should
709    ;; convert all the classic CMU CL legacy *s and *WILD-TYPE*s into
710    ;; Ts and *UNIVERSAL-TYPE*s.
711    (frob * *wild-type*)
712    (frob nil *empty-type*)
713    (frob t *universal-type*)))
714
715 (define-type-method (named :simple-=) (type1 type2)
716   (values (eq type1 type2) t))
717
718 (define-type-method (named :simple-subtypep) (type1 type2)
719   (values (or (eq type1 *empty-type*) (eq type2 *wild-type*)) t))
720
721 (define-type-method (named :complex-subtypep-arg1) (type1 type2)
722   (assert (not (hairy-type-p type2)))
723   (values (eq type1 *empty-type*) t))
724
725 (define-type-method (named :complex-subtypep-arg2) (type1 type2)
726   (if (hairy-type-p type1)
727       (values nil nil)
728       (values (not (eq type2 *empty-type*)) t)))
729
730 (define-type-method (named :complex-intersection) (type1 type2)
731   (vanilla-intersection type1 type2))
732
733 (define-type-method (named :unparse) (x)
734   (named-type-name x))
735 \f
736 ;;;; hairy and unknown types
737
738 (define-type-method (hairy :unparse) (x) (hairy-type-specifier x))
739
740 (define-type-method (hairy :simple-subtypep) (type1 type2)
741   (let ((hairy-spec1 (hairy-type-specifier type1))
742         (hairy-spec2 (hairy-type-specifier type2)))
743     (cond ((and (consp hairy-spec1) (eq (car hairy-spec1) 'not)
744                 (consp hairy-spec2) (eq (car hairy-spec2) 'not))
745            (csubtypep (specifier-type (cadr hairy-spec2))
746                       (specifier-type (cadr hairy-spec1))))
747           ((equal hairy-spec1 hairy-spec2)
748            (values t t))
749           (t
750            (values nil nil)))))
751
752 (define-type-method (hairy :complex-subtypep-arg2) (type1 type2)
753   (let ((hairy-spec (hairy-type-specifier type2)))
754     (cond ((and (consp hairy-spec) (eq (car hairy-spec) 'not))
755            (multiple-value-bind (val win)
756                (type-intersection type1 (specifier-type (cadr hairy-spec)))
757              (if win
758                  (values (eq val *empty-type*) t)
759                  (values nil nil))))
760           (t
761            (values nil nil)))))
762
763 (define-type-method (hairy :complex-subtypep-arg1 :complex-=) (type1 type2)
764   (declare (ignore type1 type2))
765   (values nil nil))
766
767 (define-type-method (hairy :simple-intersection :complex-intersection)
768                     (type1 type2)
769   (declare (ignore type2))
770   (values type1 nil))
771
772 (define-type-method (hairy :complex-union) (type1 type2)
773   (make-union-type (list type1 type2)))
774
775 (define-type-method (hairy :simple-=) (type1 type2)
776   (if (equal (hairy-type-specifier type1)
777              (hairy-type-specifier type2))
778       (values t t)
779       (values nil nil)))
780
781 (def-type-translator not (&whole whole type)
782   (declare (ignore type))
783   (make-hairy-type :specifier whole))
784
785 (def-type-translator satisfies (&whole whole fun)
786   (declare (ignore fun))
787   (make-hairy-type :specifier whole))
788 \f
789 ;;;; numeric types
790
791 ;;; A list of all the float formats, in order of decreasing precision.
792 (eval-when (:compile-toplevel :load-toplevel :execute)
793   (defparameter *float-formats*
794     '(long-float double-float single-float short-float)))
795
796 ;;; The type of a float format.
797 (deftype float-format () `(member ,@*float-formats*))
798
799 #!+negative-zero-is-not-zero
800 (defun make-numeric-type (&key class format (complexp :real) low high
801                                enumerable)
802   (flet ((canonicalise-low-bound (x)
803            ;; Canonicalise a low bound of (-0.0) to 0.0.
804            (if (and (consp x) (floatp (car x)) (zerop (car x))
805                     (minusp (float-sign (car x))))
806                (float 0.0 (car x))
807                x))
808          (canonicalise-high-bound (x)
809            ;; Canonicalise a high bound of (+0.0) to -0.0.
810            (if (and (consp x) (floatp (car x)) (zerop (car x))
811                     (plusp (float-sign (car x))))
812                (float -0.0 (car x))
813                x)))
814     (%make-numeric-type :class class
815                         :format format
816                         :complexp complexp
817                         :low (canonicalise-low-bound low)
818                         :high (canonicalise-high-bound high)
819                         :enumerable enumerable)))
820
821 (define-type-class number)
822
823 (define-type-method (number :simple-=) (type1 type2)
824   (values
825    (and (eq (numeric-type-class type1) (numeric-type-class type2))
826         (eq (numeric-type-format type1) (numeric-type-format type2))
827         (eq (numeric-type-complexp type1) (numeric-type-complexp type2))
828         (equal (numeric-type-low type1) (numeric-type-low type2))
829         (equal (numeric-type-high type1) (numeric-type-high type2)))
830    t))
831
832 (define-type-method (number :unparse) (type)
833   (let* ((complexp (numeric-type-complexp type))
834          (low (numeric-type-low type))
835          (high (numeric-type-high type))
836          (base (case (numeric-type-class type)
837                  (integer 'integer)
838                  (rational 'rational)
839                  (float (or (numeric-type-format type) 'float))
840                  (t 'real))))
841     (let ((base+bounds
842            (cond ((and (eq base 'integer) high low)
843                   (let ((high-count (logcount high))
844                         (high-length (integer-length high)))
845                     (cond ((= low 0)
846                            (cond ((= high 0) '(integer 0 0))
847                                  ((= high 1) 'bit)
848                                  ((and (= high-count high-length)
849                                        (plusp high-length))
850                                   `(unsigned-byte ,high-length))
851                                  (t
852                                   `(mod ,(1+ high)))))
853                           ((and (= low sb!vm:*target-most-negative-fixnum*)
854                                 (= high sb!vm:*target-most-positive-fixnum*))
855                            'fixnum)
856                           ((and (= low (lognot high))
857                                 (= high-count high-length)
858                                 (> high-count 0))
859                            `(signed-byte ,(1+ high-length)))
860                           (t
861                            `(integer ,low ,high)))))
862                  (high `(,base ,(or low '*) ,high))
863                  (low
864                   (if (and (eq base 'integer) (= low 0))
865                       'unsigned-byte
866                       `(,base ,low)))
867                  (t base))))
868       (ecase complexp
869         (:real
870          base+bounds)
871         (:complex
872          (if (eq base+bounds 'real)
873              'complex
874              `(complex ,base+bounds)))
875         ((nil)
876          (assert (eq base+bounds 'real))
877          'number)))))
878
879 ;;; Return true if X is "less than or equal" to Y, taking open bounds
880 ;;; into consideration. CLOSED is the predicate used to test the bound
881 ;;; on a closed interval (e.g. <=), and OPEN is the predicate used on
882 ;;; open bounds (e.g. <). Y is considered to be the outside bound, in
883 ;;; the sense that if it is infinite (NIL), then the test succeeds,
884 ;;; whereas if X is infinite, then the test fails (unless Y is also
885 ;;; infinite).
886 ;;;
887 ;;; This is for comparing bounds of the same kind, e.g. upper and
888 ;;; upper. Use NUMERIC-BOUND-TEST* for different kinds of bounds.
889 #!-negative-zero-is-not-zero
890 (defmacro numeric-bound-test (x y closed open)
891   `(cond ((not ,y) t)
892          ((not ,x) nil)
893          ((consp ,x)
894           (if (consp ,y)
895               (,closed (car ,x) (car ,y))
896               (,closed (car ,x) ,y)))
897          (t
898           (if (consp ,y)
899               (,open ,x (car ,y))
900               (,closed ,x ,y)))))
901
902 #!+negative-zero-is-not-zero
903 (defmacro numeric-bound-test-zero (op x y)
904   `(if (and (zerop ,x) (zerop ,y) (floatp ,x) (floatp ,y))
905        (,op (float-sign ,x) (float-sign ,y))
906        (,op ,x ,y)))
907
908 #!+negative-zero-is-not-zero
909 (defmacro numeric-bound-test (x y closed open)
910   `(cond ((not ,y) t)
911          ((not ,x) nil)
912          ((consp ,x)
913           (if (consp ,y)
914               (numeric-bound-test-zero ,closed (car ,x) (car ,y))
915               (numeric-bound-test-zero ,closed (car ,x) ,y)))
916          (t
917           (if (consp ,y)
918               (numeric-bound-test-zero ,open ,x (car ,y))
919               (numeric-bound-test-zero ,closed ,x ,y)))))
920
921 ;;; This is used to compare upper and lower bounds. This is different
922 ;;; from the same-bound case:
923 ;;; -- Since X = NIL is -infinity, whereas y = NIL is +infinity, we
924 ;;;    return true if *either* arg is NIL.
925 ;;; -- an open inner bound is "greater" and also squeezes the interval,
926 ;;;    causing us to use the OPEN test for those cases as well.
927 #!-negative-zero-is-not-zero
928 (defmacro numeric-bound-test* (x y closed open)
929   `(cond ((not ,y) t)
930          ((not ,x) t)
931          ((consp ,x)
932           (if (consp ,y)
933               (,open (car ,x) (car ,y))
934               (,open (car ,x) ,y)))
935          (t
936           (if (consp ,y)
937               (,open ,x (car ,y))
938               (,closed ,x ,y)))))
939
940 #!+negative-zero-is-not-zero
941 (defmacro numeric-bound-test* (x y closed open)
942   `(cond ((not ,y) t)
943          ((not ,x) t)
944          ((consp ,x)
945           (if (consp ,y)
946               (numeric-bound-test-zero ,open (car ,x) (car ,y))
947               (numeric-bound-test-zero ,open (car ,x) ,y)))
948          (t
949           (if (consp ,y)
950               (numeric-bound-test-zero ,open ,x (car ,y))
951               (numeric-bound-test-zero ,closed ,x ,y)))))
952
953 ;;; Return whichever of the numeric bounds X and Y is "maximal"
954 ;;; according to the predicates CLOSED (e.g. >=) and OPEN (e.g. >).
955 ;;; This is only meaningful for maximizing like bounds, i.e. upper and
956 ;;; upper. If MAX-P is true, then we return NIL if X or Y is NIL,
957 ;;; otherwise we return the other arg.
958 (defmacro numeric-bound-max (x y closed open max-p)
959   (once-only ((n-x x)
960               (n-y y))
961     `(cond ((not ,n-x) ,(if max-p nil n-y))
962            ((not ,n-y) ,(if max-p nil n-x))
963            ((consp ,n-x)
964             (if (consp ,n-y)
965                 (if (,closed (car ,n-x) (car ,n-y)) ,n-x ,n-y)
966                 (if (,open (car ,n-x) ,n-y) ,n-x ,n-y)))
967            (t
968             (if (consp ,n-y)
969                 (if (,open (car ,n-y) ,n-x) ,n-y ,n-x)
970                 (if (,closed ,n-y ,n-x) ,n-y ,n-x))))))
971
972 (define-type-method (number :simple-subtypep) (type1 type2)
973   (let ((class1 (numeric-type-class type1))
974         (class2 (numeric-type-class type2))
975         (complexp2 (numeric-type-complexp type2))
976         (format2 (numeric-type-format type2))
977         (low1 (numeric-type-low type1))
978         (high1 (numeric-type-high type1))
979         (low2 (numeric-type-low type2))
980         (high2 (numeric-type-high type2)))
981     ;; If one is complex and the other isn't, they are disjoint.
982     (cond ((not (or (eq (numeric-type-complexp type1) complexp2)
983                     (null complexp2)))
984            (values nil t))
985           ;; If the classes are specified and different, the types are
986           ;; disjoint unless type2 is rational and type1 is integer.
987           ((not (or (eq class1 class2)
988                     (null class2)
989                     (and (eq class1 'integer)
990                          (eq class2 'rational))))
991            (values nil t))
992           ;; If the float formats are specified and different, the types
993           ;; are disjoint.
994           ((not (or (eq (numeric-type-format type1) format2)
995                     (null format2)))
996            (values nil t))
997           ;; Check the bounds.
998           ((and (numeric-bound-test low1 low2 >= >)
999                 (numeric-bound-test high1 high2 <= <))
1000            (values t t))
1001           (t
1002            (values nil t)))))
1003
1004 (define-superclasses number ((generic-number)) !cold-init-forms)
1005
1006 ;;; If the high bound of LOW is adjacent to the low bound of HIGH,
1007 ;;; then return true, otherwise NIL.
1008 (defun numeric-types-adjacent (low high)
1009   (let ((low-bound (numeric-type-high low))
1010         (high-bound (numeric-type-low high)))
1011     (cond ((not (and low-bound high-bound)) nil)
1012           ((and (consp low-bound) (consp high-bound)) nil)
1013           ((consp low-bound)
1014            #!-negative-zero-is-not-zero
1015            (let ((low-value (car low-bound)))
1016              (or (eql low-value high-bound)
1017                  (and (eql low-value -0f0) (eql high-bound 0f0))
1018                  (and (eql low-value 0f0) (eql high-bound -0f0))
1019                  (and (eql low-value -0d0) (eql high-bound 0d0))
1020                  (and (eql low-value 0d0) (eql high-bound -0d0))))
1021            #!+negative-zero-is-not-zero
1022            (eql (car low-bound) high-bound))
1023           ((consp high-bound)
1024            #!-negative-zero-is-not-zero
1025            (let ((high-value (car high-bound)))
1026              (or (eql high-value low-bound)
1027                  (and (eql high-value -0f0) (eql low-bound 0f0))
1028                  (and (eql high-value 0f0) (eql low-bound -0f0))
1029                  (and (eql high-value -0d0) (eql low-bound 0d0))
1030                  (and (eql high-value 0d0) (eql low-bound -0d0))))
1031            #!+negative-zero-is-not-zero
1032            (eql (car high-bound) low-bound))
1033           #!+negative-zero-is-not-zero
1034           ((or (and (eql low-bound -0f0) (eql high-bound 0f0))
1035                (and (eql low-bound -0d0) (eql high-bound 0d0))))
1036           ((and (eq (numeric-type-class low) 'integer)
1037                 (eq (numeric-type-class high) 'integer))
1038            (eql (1+ low-bound) high-bound))
1039           (t
1040            nil))))
1041
1042 ;;; Return a numeric type that is a supertype for both TYPE1 and TYPE2.
1043 ;;;
1044 ;;; ### Note: we give up early, so keep from dropping lots of information on
1045 ;;; the floor by returning overly general types.
1046 (define-type-method (number :simple-union) (type1 type2)
1047   (declare (type numeric-type type1 type2))
1048   (cond ((csubtypep type1 type2) type2)
1049         ((csubtypep type2 type1) type1)
1050         (t
1051          (let ((class1 (numeric-type-class type1))
1052                (format1 (numeric-type-format type1))
1053                (complexp1 (numeric-type-complexp type1))
1054                (class2 (numeric-type-class type2))
1055                (format2 (numeric-type-format type2))
1056                (complexp2 (numeric-type-complexp type2)))
1057            (when (and (eq class1 class2)
1058                       (eq format1 format2)
1059                       (eq complexp1 complexp2)
1060                       (or (numeric-types-intersect type1 type2)
1061                           (numeric-types-adjacent type1 type2)
1062                           (numeric-types-adjacent type2 type1)))
1063              (make-numeric-type
1064               :class class1
1065               :format format1
1066               :complexp complexp1
1067               :low (numeric-bound-max (numeric-type-low type1)
1068                                       (numeric-type-low type2)
1069                                       <= < t)
1070               :high (numeric-bound-max (numeric-type-high type1)
1071                                        (numeric-type-high type2)
1072                                        >= > t)))))))
1073
1074 (!cold-init-forms
1075   (setf (info :type :kind 'number) :primitive)
1076   (setf (info :type :builtin 'number)
1077         (make-numeric-type :complexp nil)))
1078
1079 (def-type-translator complex (&optional (spec '*))
1080   (if (eq spec '*)
1081       (make-numeric-type :complexp :complex)
1082       (let ((type (specifier-type spec)))
1083         (unless (numeric-type-p type)
1084           (error "Component type for Complex is not numeric: ~S." spec))
1085         (when (eq (numeric-type-complexp type) :complex)
1086           (error "Component type for Complex is complex: ~S." spec))
1087         (let ((res (copy-numeric-type type)))
1088           (setf (numeric-type-complexp res) :complex)
1089           res))))
1090
1091 ;;; If X is *, return NIL, otherwise return the bound, which must be a
1092 ;;; member of TYPE or a one-element list of a member of TYPE.
1093 #!-sb-fluid (declaim (inline canonicalized-bound))
1094 (defun canonicalized-bound (bound type)
1095   (cond ((eq bound '*) nil)
1096         ((or (sb!xc:typep bound type)
1097              (and (consp bound)
1098                   (sb!xc:typep (car bound) type)
1099                   (null (cdr bound))))
1100           bound)
1101         (t
1102          (error "Bound is not ~S, a ~S or a list of a ~S: ~S"
1103                 '*
1104                 type
1105                 type
1106                 bound))))
1107
1108 (def-type-translator integer (&optional (low '*) (high '*))
1109   (let* ((l (canonicalized-bound low 'integer))
1110          (lb (if (consp l) (1+ (car l)) l))
1111          (h (canonicalized-bound high 'integer))
1112          (hb (if (consp h) (1- (car h)) h)))
1113     (when (and hb lb (< hb lb))
1114       (error "Lower bound ~S is greater than upper bound ~S." l h))
1115     (make-numeric-type :class 'integer
1116                        :complexp :real
1117                        :enumerable (not (null (and l h)))
1118                        :low lb
1119                        :high hb)))
1120
1121 (defmacro def-bounded-type (type class format)
1122   `(def-type-translator ,type (&optional (low '*) (high '*))
1123      (let ((lb (canonicalized-bound low ',type))
1124            (hb (canonicalized-bound high ',type)))
1125        (unless (numeric-bound-test* lb hb <= <)
1126          (error "Lower bound ~S is not less than upper bound ~S." low high))
1127        (make-numeric-type :class ',class :format ',format :low lb :high hb))))
1128
1129 (def-bounded-type rational rational nil)
1130 (def-bounded-type float float nil)
1131 (def-bounded-type real nil nil)
1132
1133 (defmacro define-float-format (f)
1134   `(def-bounded-type ,f float ,f))
1135
1136 (define-float-format short-float)
1137 (define-float-format single-float)
1138 (define-float-format double-float)
1139 (define-float-format long-float)
1140
1141 (defun numeric-types-intersect (type1 type2)
1142   (declare (type numeric-type type1 type2))
1143   (let* ((class1 (numeric-type-class type1))
1144          (class2 (numeric-type-class type2))
1145          (complexp1 (numeric-type-complexp type1))
1146          (complexp2 (numeric-type-complexp type2))
1147          (format1 (numeric-type-format type1))
1148          (format2 (numeric-type-format type2))
1149          (low1 (numeric-type-low type1))
1150          (high1 (numeric-type-high type1))
1151          (low2 (numeric-type-low type2))
1152          (high2 (numeric-type-high type2)))
1153     ;; If one is complex and the other isn't, then they are disjoint.
1154     (cond ((not (or (eq complexp1 complexp2)
1155                     (null complexp1) (null complexp2)))
1156            nil)
1157           ;; If either type is a float, then the other must either be
1158           ;; specified to be a float or unspecified. Otherwise, they
1159           ;; are disjoint.
1160           ((and (eq class1 'float)
1161                 (not (member class2 '(float nil)))) nil)
1162           ((and (eq class2 'float)
1163                 (not (member class1 '(float nil)))) nil)
1164           ;; If the float formats are specified and different, the
1165           ;; types are disjoint.
1166           ((not (or (eq format1 format2) (null format1) (null format2)))
1167            nil)
1168           (t
1169            ;; Check the bounds. This is a bit odd because we must
1170            ;; always have the outer bound of the interval as the
1171            ;; second arg.
1172            (if (numeric-bound-test high1 high2 <= <)
1173                (or (and (numeric-bound-test low1 low2 >= >)
1174                         (numeric-bound-test* low1 high2 <= <))
1175                    (and (numeric-bound-test low2 low1 >= >)
1176                         (numeric-bound-test* low2 high1 <= <)))
1177                (or (and (numeric-bound-test* low2 high1 <= <)
1178                         (numeric-bound-test low2 low1 >= >))
1179                    (and (numeric-bound-test high2 high1 <= <)
1180                         (numeric-bound-test* high2 low1 >= >))))))))
1181
1182 ;;; Take the numeric bound X and convert it into something that can be
1183 ;;; used as a bound in a numeric type with the specified CLASS and
1184 ;;; FORMAT. If UP-P is true, then we round up as needed, otherwise we
1185 ;;; round down. UP-P true implies that X is a lower bound, i.e. (N) > N.
1186 ;;;
1187 ;;; This is used by NUMERIC-TYPE-INTERSECTION to mash the bound into
1188 ;;; the appropriate type number. X may only be a float when CLASS is
1189 ;;; FLOAT.
1190 ;;;
1191 ;;; ### Note: it is possible for the coercion to a float to overflow
1192 ;;; or underflow. This happens when the bound doesn't fit in the
1193 ;;; specified format. In this case, we should really return the
1194 ;;; appropriate {Most | Least}-{Positive | Negative}-XXX-Float float
1195 ;;; of desired format. But these conditions aren't currently signalled
1196 ;;; in any useful way.
1197 ;;;
1198 ;;; Also, when converting an open rational bound into a float we
1199 ;;; should probably convert it to a closed bound of the closest float
1200 ;;; in the specified format. KLUDGE: In general, open float bounds are
1201 ;;; screwed up. -- (comment from original CMU CL)
1202 (defun round-numeric-bound (x class format up-p)
1203   (if x
1204       (let ((cx (if (consp x) (car x) x)))
1205         (ecase class
1206           ((nil rational) x)
1207           (integer
1208            (if (and (consp x) (integerp cx))
1209                (if up-p (1+ cx) (1- cx))
1210                (if up-p (ceiling cx) (floor cx))))
1211           (float
1212            (let ((res (if format (coerce cx format) (float cx))))
1213              (if (consp x) (list res) res)))))
1214       nil))
1215
1216 ;;; Handle the case of TYPE-INTERSECTION on two numeric types. We use
1217 ;;; TYPES-INTERSECT to throw out the case of types with no
1218 ;;; intersection. If an attribute in TYPE1 is unspecified, then we use
1219 ;;; TYPE2's attribute, which must be at least as restrictive. If the
1220 ;;; types intersect, then the only attributes that can be specified
1221 ;;; and different are the class and the bounds.
1222 ;;;
1223 ;;; When the class differs, we use the more restrictive class. The
1224 ;;; only interesting case is RATIONAL/INTEGER, since RATIONAL includes
1225 ;;; INTEGER.
1226 ;;;
1227 ;;; We make the result lower (upper) bound the maximum (minimum) of
1228 ;;; the argument lower (upper) bounds. We convert the bounds into the
1229 ;;; appropriate numeric type before maximizing. This avoids possible
1230 ;;; confusion due to mixed-type comparisons (but I think the result is
1231 ;;; the same).
1232 (define-type-method (number :simple-intersection) (type1 type2)
1233   (declare (type numeric-type type1 type2))
1234   (if (numeric-types-intersect type1 type2)
1235       (let* ((class1 (numeric-type-class type1))
1236              (class2 (numeric-type-class type2))
1237              (class (ecase class1
1238                       ((nil) class2)
1239                       ((integer float) class1)
1240                       (rational (if (eq class2 'integer)
1241                                        'integer
1242                                        'rational))))
1243              (format (or (numeric-type-format type1)
1244                          (numeric-type-format type2))))
1245         (values
1246          (make-numeric-type
1247           :class class
1248           :format format
1249           :complexp (or (numeric-type-complexp type1)
1250                         (numeric-type-complexp type2))
1251           :low (numeric-bound-max
1252                 (round-numeric-bound (numeric-type-low type1)
1253                                      class format t)
1254                 (round-numeric-bound (numeric-type-low type2)
1255                                      class format t)
1256                 > >= nil)
1257           :high (numeric-bound-max
1258                  (round-numeric-bound (numeric-type-high type1)
1259                                       class format nil)
1260                  (round-numeric-bound (numeric-type-high type2)
1261                                       class format nil)
1262                  < <= nil))
1263          t))
1264       (values *empty-type* t)))
1265
1266 ;;; Given two float formats, return the one with more precision. If
1267 ;;; either one is null, return NIL.
1268 (defun float-format-max (f1 f2)
1269   (when (and f1 f2)
1270     (dolist (f *float-formats* (error "bad float format: ~S" f1))
1271       (when (or (eq f f1) (eq f f2))
1272         (return f)))))
1273
1274 ;;; Return the result of an operation on TYPE1 and TYPE2 according to
1275 ;;; the rules of numeric contagion. This is always NUMBER, some float
1276 ;;; format (possibly complex) or RATIONAL. Due to rational
1277 ;;; canonicalization, there isn't much we can do here with integers or
1278 ;;; rational complex numbers.
1279 ;;;
1280 ;;; If either argument is not a NUMERIC-TYPE, then return NUMBER. This
1281 ;;; is useful mainly for allowing types that are technically numbers,
1282 ;;; but not a NUMERIC-TYPE.
1283 (defun numeric-contagion (type1 type2)
1284   (if (and (numeric-type-p type1) (numeric-type-p type2))
1285       (let ((class1 (numeric-type-class type1))
1286             (class2 (numeric-type-class type2))
1287             (format1 (numeric-type-format type1))
1288             (format2 (numeric-type-format type2))
1289             (complexp1 (numeric-type-complexp type1))
1290             (complexp2 (numeric-type-complexp type2)))
1291         (cond ((or (null complexp1)
1292                    (null complexp2))
1293                (specifier-type 'number))
1294               ((eq class1 'float)
1295                (make-numeric-type
1296                 :class 'float
1297                 :format (ecase class2
1298                           (float (float-format-max format1 format2))
1299                           ((integer rational) format1)
1300                           ((nil)
1301                            ;; A double-float with any real number is a
1302                            ;; double-float.
1303                            #!-long-float
1304                            (if (eq format1 'double-float)
1305                              'double-float
1306                              nil)
1307                            ;; A long-float with any real number is a
1308                            ;; long-float.
1309                            #!+long-float
1310                            (if (eq format1 'long-float)
1311                              'long-float
1312                              nil)))
1313                 :complexp (if (or (eq complexp1 :complex)
1314                                   (eq complexp2 :complex))
1315                               :complex
1316                               :real)))
1317               ((eq class2 'float) (numeric-contagion type2 type1))
1318               ((and (eq complexp1 :real) (eq complexp2 :real))
1319                (make-numeric-type
1320                 :class (and class1 class2 'rational)
1321                 :complexp :real))
1322               (t
1323                (specifier-type 'number))))
1324       (specifier-type 'number)))
1325 \f
1326 ;;;; array types
1327
1328 (define-type-class array)
1329
1330 ;;; What this does depends on the setting of the
1331 ;;; *USE-IMPLEMENTATION-TYPES* switch. If true, return the specialized
1332 ;;; element type, otherwise return the original element type.
1333 (defun specialized-element-type-maybe (type)
1334   (declare (type array-type type))
1335   (if *use-implementation-types*
1336       (array-type-specialized-element-type type)
1337       (array-type-element-type type)))
1338
1339 (define-type-method (array :simple-=) (type1 type2)
1340   (values (and (equal (array-type-dimensions type1)
1341                       (array-type-dimensions type2))
1342                (eq (array-type-complexp type1)
1343                    (array-type-complexp type2))
1344                (type= (specialized-element-type-maybe type1)
1345                       (specialized-element-type-maybe type2)))
1346           t))
1347
1348 (define-type-method (array :unparse) (type)
1349   (let ((dims (array-type-dimensions type))
1350         (eltype (type-specifier (array-type-element-type type)))
1351         (complexp (array-type-complexp type)))
1352     (cond ((eq dims '*)
1353            (if (eq eltype '*)
1354                (if complexp 'array 'simple-array)
1355                (if complexp `(array ,eltype) `(simple-array ,eltype))))
1356           ((= (length dims) 1)
1357            (if complexp
1358                (if (eq (car dims) '*)
1359                    (case eltype
1360                      (bit 'bit-vector)
1361                      (base-char 'base-string)
1362                      (character 'string)
1363                      (* 'vector)
1364                      (t `(vector ,eltype)))
1365                    (case eltype
1366                      (bit `(bit-vector ,(car dims)))
1367                      (base-char `(base-string ,(car dims)))
1368                      (character `(string ,(car dims)))
1369                      (t `(vector ,eltype ,(car dims)))))
1370                (if (eq (car dims) '*)
1371                    (case eltype
1372                      (bit 'simple-bit-vector)
1373                      (base-char 'simple-base-string)
1374                      (character 'simple-string)
1375                      ((t) 'simple-vector)
1376                      (t `(simple-array ,eltype (*))))
1377                    (case eltype
1378                      (bit `(simple-bit-vector ,(car dims)))
1379                      (base-char `(simple-base-string ,(car dims)))
1380                      (character `(simple-string ,(car dims)))
1381                      ((t) `(simple-vector ,(car dims)))
1382                      (t `(simple-array ,eltype ,dims))))))
1383           (t
1384            (if complexp
1385                `(array ,eltype ,dims)
1386                `(simple-array ,eltype ,dims))))))
1387
1388 (define-type-method (array :simple-subtypep) (type1 type2)
1389   (let ((dims1 (array-type-dimensions type1))
1390         (dims2 (array-type-dimensions type2))
1391         (complexp2 (array-type-complexp type2)))
1392     ;; See whether dimensions are compatible.
1393     (cond ((not (or (eq dims2 '*)
1394                     (and (not (eq dims1 '*))
1395                          ;; (sbcl-0.6.4 has trouble figuring out that
1396                          ;; DIMS1 and DIMS2 must be lists at this
1397                          ;; point, and knowing that is important to
1398                          ;; compiling EVERY efficiently.)
1399                          (= (length (the list dims1))
1400                             (length (the list dims2)))
1401                          (every (lambda (x y)
1402                                   (or (eq y '*) (eql x y)))
1403                                 (the list dims1)
1404                                 (the list dims2)))))
1405            (values nil t))
1406           ;; See whether complexpness is compatible.
1407           ((not (or (eq complexp2 :maybe)
1408                     (eq (array-type-complexp type1) complexp2)))
1409            (values nil t))
1410           ;; If the TYPE2 eltype is wild, we win. Otherwise, the types
1411           ;; must be identical.
1412           ((or (eq (array-type-element-type type2) *wild-type*)
1413                (type= (specialized-element-type-maybe type1)
1414                       (specialized-element-type-maybe type2)))
1415            (values t t))
1416           (t
1417            (values nil t)))))
1418
1419 (define-superclasses array
1420   ((string string)
1421    (vector vector)
1422    (array))
1423   !cold-init-forms)
1424
1425 (defun array-types-intersect (type1 type2)
1426   (declare (type array-type type1 type2))
1427   (let ((dims1 (array-type-dimensions type1))
1428         (dims2 (array-type-dimensions type2))
1429         (complexp1 (array-type-complexp type1))
1430         (complexp2 (array-type-complexp type2)))
1431     ;; See whether dimensions are compatible.
1432     (cond ((not (or (eq dims1 '*) (eq dims2 '*)
1433                     (and (= (length dims1) (length dims2))
1434                          (every #'(lambda (x y)
1435                                     (or (eq x '*) (eq y '*) (= x y)))
1436                                 dims1 dims2))))
1437            (values nil t))
1438           ;; See whether complexpness is compatible.
1439           ((not (or (eq complexp1 :maybe)
1440                     (eq complexp2 :maybe)
1441                     (eq complexp1 complexp2)))
1442            (values nil t))
1443           ;; If either element type is wild, then they intersect.
1444           ;; Otherwise, the types must be identical.
1445           ((or (eq (array-type-element-type type1) *wild-type*)
1446                (eq (array-type-element-type type2) *wild-type*)
1447                (type= (specialized-element-type-maybe type1)
1448                       (specialized-element-type-maybe type2)))
1449
1450            (values t t))
1451           (t
1452            (values nil t)))))
1453
1454 (define-type-method (array :simple-intersection) (type1 type2)
1455   (declare (type array-type type1 type2))
1456   (if (array-types-intersect type1 type2)
1457       (let ((dims1 (array-type-dimensions type1))
1458             (dims2 (array-type-dimensions type2))
1459             (complexp1 (array-type-complexp type1))
1460             (complexp2 (array-type-complexp type2))
1461             (eltype1 (array-type-element-type type1))
1462             (eltype2 (array-type-element-type type2)))
1463         (values
1464          (specialize-array-type
1465           (make-array-type
1466            :dimensions (cond ((eq dims1 '*) dims2)
1467                              ((eq dims2 '*) dims1)
1468                              (t
1469                               (mapcar (lambda (x y) (if (eq x '*) y x))
1470                                       dims1 dims2)))
1471            :complexp (if (eq complexp1 :maybe) complexp2 complexp1)
1472            :element-type (if (eq eltype1 *wild-type*) eltype2 eltype1)))
1473          t))
1474       (values *empty-type* t)))
1475
1476 ;;; Check a supplied dimension list to determine whether it is legal,
1477 ;;; and return it in canonical form (as either '* or a list).
1478 (defun canonical-array-dimensions (dims)
1479   (typecase dims
1480     ((member *) dims)
1481     (integer
1482      (when (minusp dims)
1483        (error "Arrays can't have a negative number of dimensions: ~S" dims))
1484      (when (>= dims sb!xc:array-rank-limit)
1485        (error "array type with too many dimensions: ~S" dims))
1486      (make-list dims :initial-element '*))
1487     (list
1488      (when (>= (length dims) sb!xc:array-rank-limit)
1489        (error "array type with too many dimensions: ~S" dims))
1490      (dolist (dim dims)
1491        (unless (eq dim '*)
1492          (unless (and (integerp dim)
1493                       (>= dim 0)
1494                       (< dim sb!xc:array-dimension-limit))
1495            (error "bad dimension in array type: ~S" dim))))
1496      dims)
1497     (t
1498      (error "Array dimensions is not a list, integer or *:~%  ~S" dims))))
1499 \f
1500 ;;;; MEMBER types
1501
1502 (define-type-class member)
1503
1504 (define-type-method (member :unparse) (type)
1505   (let ((members (member-type-members type)))
1506     (if (equal members '(nil))
1507         'null
1508         `(member ,@members))))
1509
1510 (define-type-method (member :simple-subtypep) (type1 type2)
1511   (values (subsetp (member-type-members type1) (member-type-members type2))
1512           t))
1513
1514 (define-type-method (member :complex-subtypep-arg1) (type1 type2)
1515   (block PUNT
1516     (values (every-type-op ctypep type2 (member-type-members type1)
1517                            :list-first t)
1518             t)))
1519
1520 ;;; We punt if the odd type is enumerable and intersects with the
1521 ;;; MEMBER type. If not enumerable, then it is definitely not a
1522 ;;; subtype of the MEMBER type.
1523 (define-type-method (member :complex-subtypep-arg2) (type1 type2)
1524   (cond ((not (type-enumerable type1)) (values nil t))
1525         ((types-intersect type1 type2) (values nil nil))
1526         (t
1527          (values nil t))))
1528
1529 (define-type-method (member :simple-intersection) (type1 type2)
1530   (let ((mem1 (member-type-members type1))
1531         (mem2 (member-type-members type2)))
1532     (values (cond ((subsetp mem1 mem2) type1)
1533                   ((subsetp mem2 mem1) type2)
1534                   (t
1535                    (let ((res (intersection mem1 mem2)))
1536                      (if res
1537                          (make-member-type :members res)
1538                          *empty-type*))))
1539             t)))
1540
1541 (define-type-method (member :complex-intersection) (type1 type2)
1542   (block PUNT
1543     (collect ((members))
1544       (let ((mem2 (member-type-members type2)))
1545         (dolist (member mem2)
1546           (multiple-value-bind (val win) (ctypep member type1)
1547             (unless win
1548               (return-from PUNT (values type2 nil)))
1549             (when val (members member))))
1550
1551         (values (cond ((subsetp mem2 (members)) type2)
1552                       ((null (members)) *empty-type*)
1553                       (t
1554                        (make-member-type :members (members))))
1555                 t)))))
1556
1557 ;;; We don't need a :COMPLEX-UNION, since the only interesting case is a union
1558 ;;; type, and the member/union interaction is handled by the union type
1559 ;;; method.
1560 (define-type-method (member :simple-union) (type1 type2)
1561   (let ((mem1 (member-type-members type1))
1562         (mem2 (member-type-members type2)))
1563     (cond ((subsetp mem1 mem2) type2)
1564           ((subsetp mem2 mem1) type1)
1565           (t
1566            (make-member-type :members (union mem1 mem2))))))
1567
1568 (define-type-method (member :simple-=) (type1 type2)
1569   (let ((mem1 (member-type-members type1))
1570         (mem2 (member-type-members type2)))
1571     (values (and (subsetp mem1 mem2) (subsetp mem2 mem1))
1572             t)))
1573
1574 (define-type-method (member :complex-=) (type1 type2)
1575   (if (type-enumerable type1)
1576       (multiple-value-bind (val win) (csubtypep type2 type1)
1577         (if (or val (not win))
1578             (values nil nil)
1579             (values nil t)))
1580       (values nil t)))
1581
1582 (def-type-translator member (&rest members)
1583   (if members
1584     (make-member-type :members (remove-duplicates members))
1585     *empty-type*))
1586 \f
1587 ;;;; union types
1588
1589 ;;; Make a union type from the specifier types, setting ENUMERABLE in
1590 ;;; the result if all are enumerable.
1591 (defun make-union-type (types)
1592   (declare (list types))
1593   (%make-union-type (every #'type-enumerable types) types))
1594
1595 (define-type-class union)
1596
1597 ;;; If LIST, then return that, otherwise the OR of the component types.
1598 (define-type-method (union :unparse) (type)
1599   (declare (type ctype type))
1600   (if (type= type (specifier-type 'list))
1601       'list
1602       `(or ,@(mapcar #'type-specifier (union-type-types type)))))
1603
1604 ;;; Two union types are equal if every type in one is equal to some
1605 ;;; type in the other.
1606 (define-type-method (union :simple-=) (type1 type2)
1607   (block PUNT
1608     (let ((types1 (union-type-types type1))
1609           (types2 (union-type-types type2)))
1610       (values (and (dolist (type1 types1 t)
1611                      (unless (any-type-op type= type1 types2)
1612                        (return nil)))
1613                    (dolist (type2 types2 t)
1614                      (unless (any-type-op type= type2 types1)
1615                        (return nil))))
1616               t))))
1617
1618 ;;; Similarly, a union type is a subtype of another if every element
1619 ;;; of TYPE1 is a subtype of some element of TYPE2.
1620 (define-type-method (union :simple-subtypep) (type1 type2)
1621   (block PUNT
1622     (let ((types2 (union-type-types type2)))
1623       (values (dolist (type1 (union-type-types type1) t)
1624                 (unless (any-type-op csubtypep type1 types2)
1625                   (return nil)))
1626               t))))
1627
1628 (define-type-method (union :complex-subtypep-arg1) (type1 type2)
1629   (block PUNT
1630     (values (every-type-op csubtypep type2 (union-type-types type1)
1631                            :list-first t)
1632             t)))
1633
1634 (define-type-method (union :complex-subtypep-arg2) (type1 type2)
1635   (block PUNT
1636     (values (any-type-op csubtypep type1 (union-type-types type2)) t)))
1637
1638 (define-type-method (union :complex-union) (type1 type2)
1639   (let* ((class1 (type-class-info type1)))
1640     (collect ((res))
1641       (let ((this-type type1))
1642         (dolist (type (union-type-types type2)
1643                       (if (res)
1644                           (make-union-type (cons this-type (res)))
1645                           this-type))
1646           (cond ((eq (type-class-info type) class1)
1647                  (let ((union (funcall (type-class-simple-union class1)
1648                                        this-type type)))
1649                    (if union
1650                        (setq this-type union)
1651                        (res type))))
1652                 ((csubtypep type this-type))
1653                 ((csubtypep type1 type) (return type2))
1654                 (t
1655                  (res type))))))))
1656
1657 ;;; For the union of union types, we let the :COMPLEX-UNION method do
1658 ;;; the work.
1659 (define-type-method (union :simple-union) (type1 type2)
1660   (let ((res type1))
1661     (dolist (t2 (union-type-types type2) res)
1662       (setq res (type-union res t2)))))
1663
1664 (define-type-method (union :simple-intersection :complex-intersection)
1665                     (type1 type2)
1666   (let ((res *empty-type*)
1667         (win t))
1668     (dolist (type (union-type-types type2) (values res win))
1669       (multiple-value-bind (int w) (type-intersection type1 type)
1670         (setq res (type-union res int))
1671         (unless w (setq win nil))))))
1672
1673 (def-type-translator or (&rest types)
1674   (reduce #'type-union
1675           (mapcar #'specifier-type types)
1676           :initial-value *empty-type*))
1677
1678 ;;; We don't actually have intersection types, since the result of
1679 ;;; reasonable type intersections is always describable as a union of
1680 ;;; simple types. If something is too hairy to fit this mold, then we
1681 ;;; make a hairy type.
1682 (def-type-translator and (&whole spec &rest types)
1683   (let ((res *wild-type*))
1684     (dolist (type types res)
1685       (let ((ctype (specifier-type type)))
1686         (multiple-value-bind (int win) (type-intersection res ctype)
1687           (unless win
1688             (return (make-hairy-type :specifier spec)))
1689           (setq res int))))))
1690 \f
1691
1692 ;;; MNA: cons compound-type patch
1693 ;;; FIXIT: all commented out
1694
1695 ; (define-type-class cons)
1696  
1697 ; (def-type-translator cons (&optional car-type cdr-type)
1698 ;   (make-cons-type :car-type (specifier-type car-type)
1699 ;                 :cdr-type (specifier-type cdr-type)))
1700  
1701 ; (define-type-method (cons :unparse) (type)
1702 ;   (let ((car-eltype (type-specifier (cons-type-car-type type)))
1703 ;       (cdr-eltype (type-specifier (cons-type-cdr-type type))))
1704 ;     (cond ((and (eq car-eltype '*) (eq cdr-eltype '*))
1705 ;          'cons)
1706 ;         (t
1707 ;          `(cons ,car-eltype ,cdr-eltype)))))
1708  
1709 ; (define-type-method (cons :simple-=) (type1 type2)
1710 ;   (declare (type cons-type type1 type2))
1711 ;   (and (type= (cons-type-car-type type1) (cons-type-car-type type2))
1712 ;        (type= (cons-type-cdr-type type1) (cons-type-cdr-type type2))))
1713  
1714 ; (define-type-method (cons :simple-subtypep) (type1 type2)
1715 ;   (declare (type cons-type type1 type2))
1716 ;   (multiple-value-bind (val-car win-car)
1717 ;       (csubtypep (cons-type-car-type type1) (cons-type-car-type type2))
1718 ;     (multiple-value-bind (val-cdr win-cdr)
1719 ;       (csubtypep (cons-type-cdr-type type1) (cons-type-cdr-type type2))
1720 ;       (if (and val-car val-cdr)
1721 ;         (values t (and win-car win-cdr))
1722 ;         (values nil (or win-car win-cdr))))))
1723  
1724 ; ;;; CONS :simple-union method  -- Internal
1725 ; ;;;
1726 ; ;;; Give up if a precise type in not possible, to avoid returning overly
1727 ; ;;; general types.
1728 ; ;;;
1729 ; (define-type-method (cons :simple-union) (type1 type2)
1730 ;   (declare (type cons-type type1 type2))
1731 ;   (let ((car-type1 (cons-type-car-type type1))
1732 ;       (car-type2 (cons-type-car-type type2))
1733 ;       (cdr-type1 (cons-type-cdr-type type1))
1734 ;       (cdr-type2 (cons-type-cdr-type type2)))
1735 ;     (cond ((type= car-type1 car-type2)
1736 ;          (make-cons-type :car-type car-type1
1737 ;                          :cdr-type (type-union cdr-type1 cdr-type2)))
1738 ;         ((type= cdr-type1 cdr-type2)
1739 ;          (make-cons-type :car-type (type-union cdr-type1 cdr-type2)
1740 ;                          :cdr-type cdr-type1)))))
1741  
1742 ; (define-type-method (cons :simple-intersection) (type1 type2)
1743 ;   (declare (type cons-type type1 type2))
1744 ;   (multiple-value-bind (int-car win-car)
1745 ;       (type-intersection (cons-type-car-type type1) (cons-type-car-type type2))
1746 ;     (multiple-value-bind (int-cdr win-cdr)
1747 ;       (type-intersection (cons-type-cdr-type type1) (cons-type-cdr-type type2))
1748 ;       (values (make-cons-type :car-type int-car :cdr-type int-cdr)
1749 ;             (and win-car win-cdr)))))
1750
1751
1752
1753 ;;; Return the type that describes all objects that are in X but not
1754 ;;; in Y. If we can't determine this type, then return NIL.
1755 ;;;
1756 ;;; For now, we only are clever dealing with union and member types.
1757 ;;; If either type is not a union type, then we pretend that it is a
1758 ;;; union of just one type. What we do is remove from X all the types
1759 ;;; that are a subtype any type in Y. If any type in X intersects with
1760 ;;; a type in Y but is not a subtype, then we give up.
1761 ;;;
1762 ;;; We must also special-case any member type that appears in the
1763 ;;; union. We remove from X's members all objects that are TYPEP to Y.
1764 ;;; If Y has any members, we must be careful that none of those
1765 ;;; members are CTYPEP to any of Y's non-member types. We give up in
1766 ;;; this case, since to compute that difference we would have to break
1767 ;;; the type from X into some collection of types that represents the
1768 ;;; type without that particular element. This seems too hairy to be
1769 ;;; worthwhile, given its low utility.
1770 (defun type-difference (x y)
1771   (let ((x-types (if (union-type-p x) (union-type-types x) (list x)))
1772         (y-types (if (union-type-p y) (union-type-types y) (list y))))
1773     (collect ((res))
1774       (dolist (x-type x-types)
1775         (if (member-type-p x-type)
1776             (collect ((members))
1777               (dolist (mem (member-type-members x-type))
1778                 (multiple-value-bind (val win) (ctypep mem y)
1779                   (unless win (return-from type-difference nil))
1780                   (unless val
1781                     (members mem))))
1782               (when (members)
1783                 (res (make-member-type :members (members)))))
1784             (dolist (y-type y-types (res x-type))
1785               (multiple-value-bind (val win) (csubtypep x-type y-type)
1786                 (unless win (return-from type-difference nil))
1787                 (when val (return))
1788                 (when (types-intersect x-type y-type)
1789                   (return-from type-difference nil))))))
1790
1791       (let ((y-mem (find-if #'member-type-p y-types)))
1792         (when y-mem
1793           (let ((members (member-type-members y-mem)))
1794             (dolist (x-type x-types)
1795               (unless (member-type-p x-type)
1796                 (dolist (member members)
1797                   (multiple-value-bind (val win) (ctypep member x-type)
1798                     (when (or (not win) val)
1799                       (return-from type-difference nil)))))))))
1800
1801       (cond ((null (res)) *empty-type*)
1802             ((null (rest (res))) (first (res)))
1803             (t
1804              (make-union-type (res)))))))
1805 \f
1806 (def-type-translator array (&optional (element-type '*)
1807                                       (dimensions '*))
1808   (specialize-array-type
1809    (make-array-type :dimensions (canonical-array-dimensions dimensions)
1810                     :element-type (specifier-type element-type))))
1811
1812 (def-type-translator simple-array (&optional (element-type '*)
1813                                              (dimensions '*))
1814   (specialize-array-type
1815    (make-array-type :dimensions (canonical-array-dimensions dimensions)
1816                     :element-type (specifier-type element-type)
1817                     :complexp nil)))
1818 \f
1819 (!defun-from-collected-cold-init-forms !late-type-cold-init)