523f792f17766979a86312486fbc8a7b09956362
[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 (/show0 "late-type.lisp 19")
20
21 (!begin-collecting-cold-init-forms)
22
23 ;;; ### Remaining incorrectnesses:
24 ;;;
25 ;;; TYPE-UNION (and the OR type) doesn't properly canonicalize an
26 ;;; exhaustive partition or coalesce contiguous ranges of numeric
27 ;;; types.
28 ;;;
29 ;;; There are all sorts of nasty problems with open bounds on FLOAT
30 ;;; types (and probably FLOAT types in general.)
31 ;;;
32 ;;; RATIO and BIGNUM are not recognized as numeric types.
33
34 ;;; FIXME: It seems to me that this should be set to NIL by default,
35 ;;; and perhaps not even optionally set to T.
36 (defvar *use-implementation-types* t
37   #!+sb-doc
38   "*USE-IMPLEMENTATION-TYPES* is a semi-public flag which determines how
39    restrictive we are in determining type membership. If two types are the
40    same in the implementation, then we will consider them them the same when
41    this switch is on. When it is off, we try to be as restrictive as the
42    language allows, allowing us to detect more errors. Currently, this only
43    affects array types.")
44
45 (!cold-init-forms (setq *use-implementation-types* t))
46
47 ;;; These functions are used as method for types which need a complex
48 ;;; subtypep method to handle some superclasses, but cover a subtree
49 ;;; of the type graph (i.e. there is no simple way for any other type
50 ;;; class to be a subtype.) There are always still complex ways,
51 ;;; namely UNION and MEMBER types, so we must give TYPE1's method a
52 ;;; chance to run, instead of immediately returning NIL, T.
53 (defun delegate-complex-subtypep-arg2 (type1 type2)
54   (let ((subtypep-arg1
55          (type-class-complex-subtypep-arg1
56           (type-class-info type1))))
57     (if subtypep-arg1
58         (funcall subtypep-arg1 type1 type2)
59         (values nil t))))
60 (defun delegate-complex-intersection2 (type1 type2)
61   (let ((method (type-class-complex-intersection2 (type-class-info type1))))
62     (if (and method (not (eq method #'delegate-complex-intersection2)))
63         (funcall method type2 type1)
64         (hierarchical-intersection2 type1 type2))))
65
66 ;;; This is used by !DEFINE-SUPERCLASSES to define the SUBTYPE-ARG1
67 ;;; method. INFO is a list of conses
68 ;;;   (SUPERCLASS-CLASS . {GUARD-TYPE-SPECIFIER | NIL}).
69 ;;; This will never be called with a hairy type as TYPE2, since the
70 ;;; hairy type TYPE2 method gets first crack.
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-intersection2 ,type-class)
114                #'delegate-complex-intersection2)))))
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 &KEY argument
133 (defstruct (key-info #-sb-xc-host (:pure t)
134                      (:copier nil))
135   ;; the key (not necessarily a keyword in ANSI)
136   (name (required-argument) :type symbol)
137   ;; the type of the argument value
138   (type (required-argument) :type ctype))
139
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)))
145
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)))
150
151 (!define-type-method (values :unparse) (type)
152   (cons 'values (unparse-args-types type)))
153
154 ;;; Return true if LIST1 and LIST2 have the same elements in the same
155 ;;; positions according to TYPE=. We return NIL, NIL if there is an
156 ;;; uncertain comparison.
157 (defun type=-list (list1 list2)
158   (declare (list list1 list2))
159   (do ((types1 list1 (cdr types1))
160        (types2 list2 (cdr types2)))
161       ((or (null types1) (null types2))
162        (if (or types1 types2)
163            (values nil t)
164            (values t t)))
165     (multiple-value-bind (val win)
166         (type= (first types1) (first types2))
167       (unless win
168         (return (values nil nil)))
169       (unless val
170         (return (values nil t))))))
171
172 (!define-type-method (values :simple-=) (type1 type2)
173   (let ((rest1 (args-type-rest type1))
174         (rest2 (args-type-rest type2)))
175     (cond ((or (args-type-keyp type1) (args-type-keyp type2)
176                (args-type-allowp type1) (args-type-allowp type2))
177            (values nil nil))
178           ((and rest1 rest2 (type/= rest1 rest2))
179            (type= rest1 rest2))
180           ((or rest1 rest2)
181            (values nil t))
182           (t
183            (multiple-value-bind (req-val req-win)
184                (type=-list (values-type-required type1)
185                            (values-type-required type2))
186              (multiple-value-bind (opt-val opt-win)
187                  (type=-list (values-type-optional type1)
188                              (values-type-optional type2))
189                (values (and req-val opt-val) (and req-win opt-win))))))))
190
191 (!define-type-class function)
192
193 ;;; a flag that we can bind to cause complex function types to be
194 ;;; unparsed as FUNCTION. This is useful when we want a type that we
195 ;;; can pass to TYPEP.
196 (defvar *unparse-function-type-simplify*)
197 (!cold-init-forms (setq *unparse-function-type-simplify* nil))
198
199 (!define-type-method (function :unparse) (type)
200   (if *unparse-function-type-simplify*
201       'function
202       (list 'function
203             (if (function-type-wild-args type)
204                 '*
205                 (unparse-args-types type))
206             (type-specifier
207              (function-type-returns type)))))
208
209 ;;; Since all function types are equivalent to FUNCTION, they are all
210 ;;; subtypes of each other.
211 (!define-type-method (function :simple-subtypep) (type1 type2)
212   (declare (ignore type1 type2))
213   (values t t))
214
215 (!define-superclasses function ((function)) !cold-init-forms)
216
217 ;;; The union or intersection of two FUNCTION types is FUNCTION.
218 (!define-type-method (function :simple-union2) (type1 type2)
219   (declare (ignore type1 type2))
220   (specifier-type 'function))
221 (!define-type-method (function :simple-intersection2) (type1 type2)
222   (declare (ignore type1 type2))
223   (specifier-type 'function))
224
225 ;;; ### Not very real, but good enough for redefining transforms
226 ;;; according to type:
227 (!define-type-method (function :simple-=) (type1 type2)
228   (values (equalp type1 type2) t))
229
230 (!define-type-class constant :inherits values)
231
232 (!define-type-method (constant :unparse) (type)
233   `(constant-argument ,(type-specifier (constant-type-type type))))
234
235 (!define-type-method (constant :simple-=) (type1 type2)
236   (type= (constant-type-type type1) (constant-type-type type2)))
237
238 (!def-type-translator constant-argument (type)
239   (make-constant-type :type (specifier-type type)))
240
241 ;;; Given a LAMBDA-LIST-like values type specification and an ARGS-TYPE
242 ;;; structure, fill in the slots in the structure accordingly. This is
243 ;;; used for both FUNCTION and VALUES types.
244 (declaim (ftype (function (list args-type) (values)) parse-args-types))
245 (defun parse-args-types (lambda-list result)
246   (multiple-value-bind (required optional restp rest keyp keys allowp aux)
247       (parse-lambda-list lambda-list)
248     (when aux
249       (error "&AUX in a FUNCTION or VALUES type: ~S." lambda-list))
250     (setf (args-type-required result) (mapcar #'specifier-type required))
251     (setf (args-type-optional result) (mapcar #'specifier-type optional))
252     (setf (args-type-rest result) (if restp (specifier-type rest) nil))
253     (setf (args-type-keyp result) keyp)
254     (collect ((key-info))
255       (dolist (key keys)
256         (unless (proper-list-of-length-p key 2)
257           (error "Keyword type description is not a two-list: ~S." key))
258         (let ((kwd (first key)))
259           (when (find kwd (key-info) :key #'key-info-name)
260             (error "~@<repeated keyword ~S in lambda list: ~2I~_~S~:>"
261                    kwd lambda-list))
262           (key-info (make-key-info :name kwd
263                                    :type (specifier-type (second key))))))
264       (setf (args-type-keywords result) (key-info)))
265     (setf (args-type-allowp result) allowp)
266     (values)))
267
268 ;;; Return the lambda-list-like type specification corresponding
269 ;;; to an ARGS-TYPE.
270 (declaim (ftype (function (args-type) list) unparse-args-types))
271 (defun unparse-args-types (type)
272   (collect ((result))
273
274     (dolist (arg (args-type-required type))
275       (result (type-specifier arg)))
276
277     (when (args-type-optional type)
278       (result '&optional)
279       (dolist (arg (args-type-optional type))
280         (result (type-specifier arg))))
281
282     (when (args-type-rest type)
283       (result '&rest)
284       (result (type-specifier (args-type-rest type))))
285
286     (when (args-type-keyp type)
287       (result '&key)
288       (dolist (key (args-type-keywords type))
289         (result (list (key-info-name key)
290                       (type-specifier (key-info-type key))))))
291
292     (when (args-type-allowp type)
293       (result '&allow-other-keys))
294
295     (result)))
296
297 (!def-type-translator function (&optional (args '*) (result '*))
298   (let ((res (make-function-type
299               :returns (values-specifier-type result))))
300     (if (eq args '*)
301         (setf (function-type-wild-args res) t)
302         (parse-args-types args res))
303     res))
304
305 (!def-type-translator values (&rest values)
306   (let ((res (make-values-type)))
307     (parse-args-types values res)
308     res))
309 \f
310 ;;;; VALUES types interfaces
311 ;;;;
312 ;;;; We provide a few special operations that can be meaningfully used
313 ;;;; on VALUES types (as well as on any other type).
314
315 ;;; Return the type of the first value indicated by TYPE. This is used
316 ;;; by people who don't want to have to deal with VALUES types.
317 #!-sb-fluid (declaim (freeze-type values-type))
318 ; (inline single-value-type))
319 (defun single-value-type (type)
320   (declare (type ctype type))
321   (cond ((values-type-p type)
322          (or (car (args-type-required type))
323              (if (args-type-optional type)
324                  (type-union (car (args-type-optional type))
325                              (specifier-type 'null)))
326              (args-type-rest type)
327              (specifier-type 'null)))
328         ((eq type *wild-type*)
329          *universal-type*)
330         (t
331          type)))
332
333 ;;; Return the minimum number of arguments that a function can be
334 ;;; called with, and the maximum number or NIL. If not a function
335 ;;; type, return NIL, NIL.
336 (defun function-type-nargs (type)
337   (declare (type ctype type))
338   (if (function-type-p type)
339       (let ((fixed (length (args-type-required type))))
340         (if (or (args-type-rest type)
341                 (args-type-keyp type)
342                 (args-type-allowp type))
343             (values fixed nil)
344             (values fixed (+ fixed (length (args-type-optional type))))))
345       (values nil nil)))
346
347 ;;; Determine whether TYPE corresponds to a definite number of values.
348 ;;; The first value is a list of the types for each value, and the
349 ;;; second value is the number of values. If the number of values is
350 ;;; not fixed, then return NIL and :UNKNOWN.
351 (defun values-types (type)
352   (declare (type ctype type))
353   (cond ((eq type *wild-type*)
354          (values nil :unknown))
355         ((not (values-type-p type))
356          (values (list type) 1))
357         ((or (args-type-optional type)
358              (args-type-rest type)
359              (args-type-keyp type)
360              (args-type-allowp type))
361          (values nil :unknown))
362         (t
363          (let ((req (args-type-required type)))
364            (values (mapcar #'single-value-type req) (length req))))))
365
366 ;;; Return two values:
367 ;;; 1. A list of all the positional (fixed and optional) types.
368 ;;; 2. The &REST type (if any). If keywords allowed, *UNIVERSAL-TYPE*.
369 ;;;    If no keywords or &REST, then the DEFAULT-TYPE.
370 (defun values-type-types (type &optional (default-type *empty-type*))
371   (declare (type values-type type))
372   (values (append (args-type-required type)
373                   (args-type-optional type))
374           (cond ((args-type-keyp type) *universal-type*)
375                 ((args-type-rest type))
376                 (t
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 (defun args-type-op (type1 type2 operation nreq default-type)
430   (declare (type ctype type1 type2 default-type)
431            (type function operation nreq))
432   (if (or (values-type-p type1) (values-type-p type2))
433       (let ((type1 (coerce-to-values type1))
434             (type2 (coerce-to-values type2)))
435         (multiple-value-bind (types1 rest1)
436             (values-type-types type1 default-type)
437           (multiple-value-bind (types2 rest2)
438               (values-type-types type2 default-type)
439             (multiple-value-bind (rest rest-exact)
440                 (funcall operation rest1 rest2)
441               (multiple-value-bind (res res-exact)
442                   (if (< (length types1) (length types2))
443                       (fixed-values-op types2 types1 rest1 operation)
444                       (fixed-values-op types1 types2 rest2 operation))
445                 (let* ((req (funcall nreq
446                                      (length (args-type-required type1))
447                                      (length (args-type-required type2))))
448                        (required (subseq res 0 req))
449                        (opt (subseq res req))
450                        (opt-last (position rest opt :test-not #'type=
451                                            :from-end t)))
452                   (if (find *empty-type* required :test #'type=)
453                       (values *empty-type* t)
454                       (values (make-values-type
455                                :required required
456                                :optional (if opt-last
457                                              (subseq opt 0 (1+ opt-last))
458                                              ())
459                                :rest (if (eq rest default-type) nil rest))
460                               (and rest-exact res-exact)))))))))
461       (funcall operation type1 type2)))
462
463 ;;; Do a union or intersection operation on types that might be values
464 ;;; types. The result is optimized for utility rather than exactness,
465 ;;; but it is guaranteed that it will be no smaller (more restrictive)
466 ;;; than the precise result.
467 ;;;
468 ;;; The return convention seems to be analogous to
469 ;;; TYPES-EQUAL-OR-INTERSECT. -- WHN 19990910.
470 (defun-cached (values-type-union :hash-function type-cache-hash
471                                  :hash-bits 8
472                                  :default nil
473                                  :init-wrapper !cold-init-forms)
474               ((type1 eq) (type2 eq))
475   (declare (type ctype type1 type2))
476   (cond ((or (eq type1 *wild-type*) (eq type2 *wild-type*)) *wild-type*)
477         ((eq type1 *empty-type*) type2)
478         ((eq type2 *empty-type*) type1)
479         (t
480          (values (args-type-op type1 type2 #'type-union #'min *empty-type*)))))
481 (defun-cached (values-type-intersection :hash-function type-cache-hash
482                                         :hash-bits 8
483                                         :values 2
484                                         :default (values nil :empty)
485                                         :init-wrapper !cold-init-forms)
486               ((type1 eq) (type2 eq))
487   (declare (type ctype type1 type2))
488   (cond ((eq type1 *wild-type*) (values type2 t))
489         ((eq type2 *wild-type*) (values type1 t))
490         (t
491          (args-type-op type1 type2
492                        #'type-intersection
493                        #'max
494                        (specifier-type 'null)))))
495
496 ;;; This is like TYPES-EQUAL-OR-INTERSECT, except that it sort of
497 ;;; works on VALUES types. Note that due to the semantics of
498 ;;; VALUES-TYPE-INTERSECTION, this might return (VALUES T T) when
499 ;;; there isn't really any intersection.
500 (defun values-types-equal-or-intersect (type1 type2)
501   (cond ((or (eq type1 *empty-type*) (eq type2 *empty-type*))
502          (values t t))
503         ((or (values-type-p type1) (values-type-p type2))
504          (multiple-value-bind (res win) (values-type-intersection type1 type2)
505            (values (not (eq res *empty-type*))
506                    win)))
507         (t
508          (types-equal-or-intersect type1 type2))))
509
510 ;;; a SUBTYPEP-like operation that can be used on any types, including
511 ;;; VALUES types
512 (defun-cached (values-subtypep :hash-function type-cache-hash
513                                :hash-bits 8
514                                :values 2
515                                :default (values nil :empty)
516                                :init-wrapper !cold-init-forms)
517               ((type1 eq) (type2 eq))
518   (declare (type ctype type1 type2))
519   (cond ((eq type2 *wild-type*) (values t t))
520         ((eq type1 *wild-type*)
521          (values (eq type2 *universal-type*) t))
522         ((not (values-types-equal-or-intersect type1 type2))
523          (values nil t))
524         (t
525          (if (or (values-type-p type1) (values-type-p type2))
526              (let ((type1 (coerce-to-values type1))
527                    (type2 (coerce-to-values type2)))
528                (multiple-value-bind (types1 rest1) (values-type-types type1)
529                  (multiple-value-bind (types2 rest2) (values-type-types type2)
530                    (cond ((< (length (values-type-required type1))
531                              (length (values-type-required type2)))
532                           (values nil t))
533                          ((< (length types1) (length types2))
534                           (values nil nil))
535                          ((or (values-type-keyp type1)
536                               (values-type-keyp type2))
537                           (values nil nil))
538                          (t
539                           (do ((t1 types1 (rest t1))
540                                (t2 types2 (rest t2)))
541                               ((null t2)
542                                (csubtypep rest1 rest2))
543                             (multiple-value-bind (res win-p)
544                                 (csubtypep (first t1) (first t2))
545                               (unless win-p
546                                 (return (values nil nil)))
547                               (unless res
548                                 (return (values nil t))))))))))
549              (csubtypep type1 type2)))))
550 \f
551 ;;;; type method interfaces
552
553 ;;; like SUBTYPEP, only works on CTYPE structures
554 (defun-cached (csubtypep :hash-function type-cache-hash
555                          :hash-bits 8
556                          :values 2
557                          :default (values nil :empty)
558                          :init-wrapper !cold-init-forms)
559               ((type1 eq) (type2 eq))
560   (declare (type ctype type1 type2))
561   (cond ((or (eq type1 type2)
562              (eq type1 *empty-type*)
563              (eq type2 *wild-type*))
564          (values t t))
565         ((or (eq type1 *wild-type*)
566              (eq type2 *empty-type*))
567          (values nil t))
568         (t
569          (!invoke-type-method :simple-subtypep :complex-subtypep-arg2
570                               type1 type2
571                               :complex-arg1 :complex-subtypep-arg1))))
572
573 ;;; Just parse the type specifiers and call CSUBTYPE.
574 (defun sb!xc:subtypep (type1 type2)
575   #!+sb-doc
576   "Return two values indicating the relationship between type1 and type2.
577   If values are T and T, type1 definitely is a subtype of type2.
578   If values are NIL and T, type1 definitely is not a subtype of type2.
579   If values are NIL and NIL, it couldn't be determined."
580   (csubtypep (specifier-type type1) (specifier-type type2)))
581
582 ;;; If two types are definitely equivalent, return true. The second
583 ;;; value indicates whether the first value is definitely correct.
584 ;;; This should only fail in the presence of HAIRY types.
585 (defun-cached (type= :hash-function type-cache-hash
586                      :hash-bits 8
587                      :values 2
588                      :default (values nil :empty)
589                      :init-wrapper !cold-init-forms)
590               ((type1 eq) (type2 eq))
591   (declare (type ctype type1 type2))
592   (if (eq type1 type2)
593       (values t t)
594       (!invoke-type-method :simple-= :complex-= type1 type2)))
595
596 ;;; Not exactly the negation of TYPE=, since when the relationship is
597 ;;; uncertain, we still return NIL, NIL. This is useful in cases where
598 ;;; the conservative assumption is =.
599 (defun type/= (type1 type2)
600   (declare (type ctype type1 type2))
601   (multiple-value-bind (res win) (type= type1 type2)
602     (if win
603         (values (not res) t)
604         (values nil nil))))
605
606 ;;; the type method dispatch case of TYPE-UNION2
607 (defun %type-union2 (type1 type2)
608   ;; As in %TYPE-INTERSECTION2, it seems to be a good idea to give
609   ;; both argument orders a chance at COMPLEX-INTERSECTION2. Unlike
610   ;; %TYPE-INTERSECTION2, though, I don't have a specific case which
611   ;; demonstrates this is actually necessary. Also unlike
612   ;; %TYPE-INTERSECTION2, there seems to be no need to distinguish
613   ;; between not finding a method and having a method return NIL.
614   (flet ((1way (x y)
615            (!invoke-type-method :simple-union2 :complex-union2
616                                 x y
617                                 :default nil)))
618     (declare (inline 1way))
619     (or (1way type1 type2)
620         (1way type2 type1))))
621
622 ;;; Find a type which includes both types. Any inexactness is
623 ;;; represented by the fuzzy element types; we return a single value
624 ;;; that is precise to the best of our knowledge. This result is
625 ;;; simplified into the canonical form, thus is not a UNION-TYPE
626 ;;; unless we find no other way to represent the result.
627 (defun-cached (type-union2 :hash-function type-cache-hash
628                            :hash-bits 8
629                            :init-wrapper !cold-init-forms)
630               ((type1 eq) (type2 eq))
631   ;; KLUDGE: This was generated from TYPE-INTERSECTION2 by Ye Olde Cut And
632   ;; Paste technique of programming. If it stays around (as opposed to
633   ;; e.g. fading away in favor of some CLOS solution) the shared logic
634   ;; should probably become shared code. -- WHN 2001-03-16
635   (declare (type ctype type1 type2))
636   (cond ((eq type1 type2)
637          type1)
638         ((or (union-type-p type1)
639              (union-type-p type2))
640          ;; Unions of UNION-TYPE should have the UNION-TYPE-TYPES
641          ;; values broken out and united separately. The full TYPE-UNION
642          ;; function knows how to do this, so let it handle it.
643          (type-union type1 type2))
644         (t
645          ;; the ordinary case: we dispatch to type methods
646          (%type-union2 type1 type2))))
647
648 ;;; the type method dispatch case of TYPE-INTERSECTION2
649 (defun %type-intersection2 (type1 type2)
650   ;; We want to give both argument orders a chance at
651   ;; COMPLEX-INTERSECTION2. Without that, the old CMU CL type
652   ;; methods could give noncommutative results, e.g.
653   ;;   (TYPE-INTERSECTION2 *EMPTY-TYPE* SOME-HAIRY-TYPE)
654   ;;     => NIL, NIL
655   ;;   (TYPE-INTERSECTION2 SOME-HAIRY-TYPE *EMPTY-TYPE*)
656   ;;     => #<NAMED-TYPE NIL>, T
657   ;; We also need to distinguish between the case where we found a
658   ;; type method, and it returned NIL, and the case where we fell
659   ;; through without finding any type method. An example of the first
660   ;; case is the intersection of a HAIRY-TYPE with some ordinary type.
661   ;; An example of the second case is the intersection of two
662   ;; completely-unrelated types, e.g. CONS and NUMBER, or SYMBOL and
663   ;; ARRAY.
664   ;;
665   ;; (Why yes, CLOS probably *would* be nicer..)
666   (flet ((1way (x y)
667            (!invoke-type-method :simple-intersection2 :complex-intersection2
668                                 x y
669                                 :default :no-type-method-found)))
670     (declare (inline 1way))
671     (let ((xy (1way type1 type2)))
672       (or (and (not (eql xy :no-type-method-found)) xy)
673           (let ((yx (1way type2 type1)))
674             (or (and (not (eql yx :no-type-method-found)) yx)
675                 (cond ((and (eql xy :no-type-method-found)
676                             (eql yx :no-type-method-found))
677                        *empty-type*)
678                       (t
679                        (aver (and (not xy) (not yx))) ; else handled above
680                        nil))))))))
681
682 (defun-cached (type-intersection2 :hash-function type-cache-hash
683                                   :hash-bits 8
684                                   :values 1
685                                   :default nil
686                                   :init-wrapper !cold-init-forms)
687               ((type1 eq) (type2 eq))
688   (declare (type ctype type1 type2))
689   (cond ((eq type1 type2)
690          type1)
691         ((or (intersection-type-p type1)
692              (intersection-type-p type2))
693          ;; Intersections of INTERSECTION-TYPE should have the
694          ;; INTERSECTION-TYPE-TYPES values broken out and intersected
695          ;; separately. The full TYPE-INTERSECTION function knows how
696          ;; to do that, so let it handle it.
697          (type-intersection type1 type2))
698         (t
699          ;; the ordinary case: we dispatch to type methods
700          (%type-intersection2 type1 type2))))
701
702 ;;; Return as restrictive and simple a type as we can discover that is
703 ;;; no more restrictive than the intersection of TYPE1 and TYPE2. At
704 ;;; worst, we arbitrarily return one of the arguments as the first
705 ;;; value (trying not to return a hairy type).
706 (defun type-approx-intersection2 (type1 type2)
707   (cond ((type-intersection2 type1 type2))
708         ((hairy-type-p type1) type2)
709         (t type1)))
710
711 ;;; a test useful for checking whether a derived type matches a
712 ;;; declared type
713 ;;;
714 ;;; The first value is true unless the types don't intersect and
715 ;;; aren't equal. The second value is true if the first value is
716 ;;; definitely correct. NIL is considered to intersect with any type.
717 ;;; If T is a subtype of either type, then we also return T, T. This
718 ;;; way we recognize that hairy types might intersect with T.
719 (defun types-equal-or-intersect (type1 type2)
720   (declare (type ctype type1 type2))
721   (if (or (eq type1 *empty-type*) (eq type2 *empty-type*))
722       (values t t)
723       (let ((intersection2 (type-intersection2 type1 type2)))
724         (cond ((not intersection2)
725                (if (or (csubtypep *universal-type* type1)
726                        (csubtypep *universal-type* type2))
727                    (values t t)
728                    (values t nil)))
729               ((eq intersection2 *empty-type*) (values nil t))
730               (t (values t t))))))
731
732 ;;; Return a Common Lisp type specifier corresponding to the TYPE
733 ;;; object.
734 (defun type-specifier (type)
735   (declare (type ctype type))
736   (funcall (type-class-unparse (type-class-info type)) type))
737
738 ;;; (VALUES-SPECIFIER-TYPE and SPECIFIER-TYPE moved from here to
739 ;;; early-type.lisp by WHN ca. 19990201.)
740
741 ;;; Take a list of type specifiers, computing the translation of each
742 ;;; specifier and defining it as a builtin type.
743 (declaim (ftype (function (list) (values)) precompute-types))
744 (defun precompute-types (specs)
745   (dolist (spec specs)
746     (let ((res (specifier-type spec)))
747       (unless (unknown-type-p res)
748         (setf (info :type :builtin spec) res)
749         (setf (info :type :kind spec) :primitive))))
750   (values))
751 \f
752 ;;;; general TYPE-UNION and TYPE-INTERSECTION operations
753 ;;;;
754 ;;;; These are fully general operations on CTYPEs: they'll always
755 ;;;; return a CTYPE representing the result.
756
757 ;;; shared logic for unions and intersections: Stuff TYPE into the
758 ;;; vector TYPES, finding pairs of types which can be simplified by
759 ;;; SIMPLIFY2 (TYPE-UNION2 or TYPE-INTERSECTION2) and replacing them
760 ;;; by their simplified forms.
761 (defun accumulate1-compound-type (type types %compound-type-p simplify2)
762   (declare (type ctype type))
763   (declare (type (vector ctype) types))
764   (declare (type function simplify2))
765   ;; Any input object satisfying %COMPOUND-TYPE-P should've been
766   ;; broken into components before it reached us.
767   (aver (not (funcall %compound-type-p type)))
768   (dotimes (i (length types) (vector-push-extend type types))
769     (let ((simplified2 (funcall simplify2 type (aref types i))))
770       (when simplified2
771         ;; Discard the old (AREF TYPES I).
772         (setf (aref types i) (vector-pop types))
773         ;; Merge the new SIMPLIFIED2 into TYPES, by tail recursing.
774         ;; (Note that the tail recursion is indirect: we go through
775         ;; ACCUMULATE, not ACCUMULATE1, so that if SIMPLIFIED2 is
776         ;; handled properly if it satisfies %COMPOUND-TYPE-P.)
777         (return (accumulate-compound-type simplified2
778                                           types
779                                           %compound-type-p
780                                           simplify2)))))
781   ;; Voila.
782   (values))
783
784 ;;; shared logic for unions and intersections: Use
785 ;;; ACCUMULATE1-COMPOUND-TYPE to merge TYPE into TYPES, either
786 ;;; all in one step or, if %COMPOUND-TYPE-P is satisfied,
787 ;;; component by component.
788 (defun accumulate-compound-type (type types %compound-type-p simplify2)
789   (declare (type function %compound-type-p simplify2))
790   (flet ((accumulate1 (x)
791            (accumulate1-compound-type x types %compound-type-p simplify2)))
792     (declare (inline accumulate1))
793     (if (funcall %compound-type-p type)
794         (map nil #'accumulate1 (compound-type-types type))
795         (accumulate1 type)))
796   (values))
797
798 ;;; shared logic for unions and intersections: Return a vector of
799 ;;; types representing the same types as INPUT-TYPES, but with 
800 ;;; COMPOUND-TYPEs satisfying %COMPOUND-TYPE-P broken up into their
801 ;;; component types, and with any SIMPLY2 simplifications applied.
802 (defun simplified-compound-types (input-types %compound-type-p simplify2)
803   (let ((simplified-types (make-array (length input-types)
804                                       :fill-pointer 0
805                                       :element-type 'ctype
806                                       ;; (This INITIAL-ELEMENT shouldn't
807                                       ;; matter, but helps avoid type
808                                       ;; warnings at compile time.)
809                                       :initial-element *empty-type*)))
810     (dolist (input-type input-types)
811       (accumulate-compound-type input-type
812                                 simplified-types
813                                 %compound-type-p
814                                 simplify2))
815     simplified-types))
816
817 ;;; shared logic for unions and intersections: Make a COMPOUND-TYPE
818 ;;; object whose components are the types in TYPES, or skip to special
819 ;;; cases when TYPES is short.
820 (defun make-compound-type-or-something (constructor types enumerable identity)
821   (declare (type function constructor))
822   (declare (type (vector ctype) types))
823   (declare (type ctype identity))
824   (case (length types)
825     (0 identity)
826     (1 (aref types 0))
827     (t (funcall constructor
828                 enumerable
829                 ;; FIXME: This should be just (COERCE TYPES 'LIST), but as
830                 ;; of sbcl-0.6.11.17 the COERCE optimizer is really
831                 ;; brain-dead, so that would generate a full call to
832                 ;; SPECIFIER-TYPE at runtime, so we get into bootstrap
833                 ;; problems in cold init because 'LIST is a compound
834                 ;; type, so we need to MAKE-COMPOUND-TYPE-OR-SOMETHING
835                 ;; before we know what 'LIST is. Once the COERCE
836                 ;; optimizer is less brain-dead, we can make this
837                 ;; (COERCE TYPES 'LIST) again.
838                 #+sb-xc-host (coerce types 'list)
839                 #-sb-xc-host (coerce-to-list types)))))
840
841 (defun type-intersection (&rest input-types)
842   (let ((simplified-types (simplified-compound-types input-types
843                                                      #'intersection-type-p
844                                                      #'type-intersection2)))
845     (declare (type (vector ctype) simplified-types))
846     ;; We want to have a canonical representation of types (or failing
847     ;; that, punt to HAIRY-TYPE). Canonical representation would have
848     ;; intersections inside unions but not vice versa, since you can
849     ;; always achieve that by the distributive rule. But we don't want
850     ;; to just apply the distributive rule, since it would be too easy
851     ;; to end up with unreasonably huge type expressions. So instead
852     ;; we punt to HAIRY-TYPE when this comes up.
853     (if (and (> (length simplified-types) 1)
854              (some #'union-type-p simplified-types))
855         (make-hairy-type
856          :specifier `(and ,@(map 'list #'type-specifier simplified-types)))
857         (make-compound-type-or-something #'%make-intersection-type
858                                          simplified-types
859                                          (some #'type-enumerable
860                                                simplified-types)
861                                          *universal-type*))))
862
863 (defun type-union (&rest input-types)
864   (let ((simplified-types (simplified-compound-types input-types
865                                                      #'union-type-p
866                                                      #'type-union2)))
867     (make-compound-type-or-something #'%make-union-type
868                                      simplified-types
869                                      (every #'type-enumerable simplified-types)
870                                      *empty-type*)))
871 \f
872 ;;;; built-in types
873
874 (!define-type-class named)
875
876 (defvar *wild-type*)
877 (defvar *empty-type*)
878 (defvar *universal-type*)
879
880 (!cold-init-forms
881  (macrolet ((frob (name var)
882               `(progn
883                  (setq ,var (make-named-type :name ',name))
884                  (setf (info :type :kind ',name) :primitive)
885                  (setf (info :type :builtin ',name) ,var))))
886    ;; KLUDGE: In ANSI, * isn't really the name of a type, it's just a
887    ;; special symbol which can be stuck in some places where an
888    ;; ordinary type can go, e.g. (ARRAY * 1) instead of (ARRAY T 1).
889    ;; At some point, in order to become more standard, we should
890    ;; convert all the classic CMU CL legacy *s and *WILD-TYPE*s into
891    ;; Ts and *UNIVERSAL-TYPE*s.
892    (frob * *wild-type*)
893    (frob nil *empty-type*)
894    (frob t *universal-type*)))
895
896 (!define-type-method (named :simple-=) (type1 type2)
897   ;; FIXME: BUG 85: This assertion failed when I added it in
898   ;; sbcl-0.6.11.13. It probably shouldn't fail; but for now it's
899   ;; just commented out.
900   ;;(aver (not (eq type1 *wild-type*))) ; * isn't really a type.
901   (values (eq type1 type2) t))
902
903 (!define-type-method (named :simple-subtypep) (type1 type2)
904   (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
905   (values (or (eq type1 *empty-type*) (eq type2 *wild-type*)) t))
906
907 (!define-type-method (named :complex-subtypep-arg1) (type1 type2)
908   (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
909   ;; FIXME: Why does this (old CMU CL) assertion hold? Perhaps 'cause
910   ;; the HAIRY-TYPE COMPLEX-SUBTYPEP-ARG2 method takes precedence over
911   ;; this COMPLEX-SUBTYPE-ARG1 method? (I miss CLOS..)
912   (aver (not (hairy-type-p type2))) 
913   ;; Besides the old CMU CL assertion above, we also need to avoid
914   ;; compound types, else we could get into trouble with
915   ;;   (SUBTYPEP T '(OR (SATISFIES FOO) (SATISFIES BAR)))
916   ;; or
917   ;;   (SUBTYPEP T '(AND (SATISFIES FOO) (SATISFIES BAR))).
918   (aver (not (compound-type-p type2))) 
919   ;; Then, since TYPE2 is reasonably tractable, we're good to go.
920   (values (eq type1 *empty-type*) t))
921
922 (!define-type-method (named :complex-subtypep-arg2) (type1 type2)
923   (aver (not (eq type2 *wild-type*))) ; * isn't really a type.
924   (cond ((eq type2 *universal-type*)
925          (values t t))
926         ((hairy-type-p type1)
927          (values nil nil))
928         (t
929          ;; FIXME: This seems to rely on there only being 2 or 3
930          ;; HAIRY-TYPE values, and the exclusion of various
931          ;; possibilities above. It would be good to explain it and/or
932          ;; rewrite it so that it's clearer.
933          (values (not (eq type2 *empty-type*)) t))))
934
935 (!define-type-method (named :complex-intersection2) (type1 type2)
936   ;; FIXME: This assertion failed when I added it in sbcl-0.6.11.13.
937   ;; Perhaps when bug 85 is fixed it can be reenabled.
938   ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
939   (hierarchical-intersection2 type1 type2))
940
941 (!define-type-method (named :complex-union2) (type1 type2)
942   ;; Perhaps when bug 85 is fixed this can be reenabled.
943   ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
944   (hierarchical-union2 type1 type2))
945
946 (!define-type-method (named :unparse) (x)
947   (named-type-name x))
948 \f
949 ;;;; hairy and unknown types
950
951 (!define-type-method (hairy :unparse) (x) (hairy-type-specifier x))
952
953 (!define-type-method (hairy :simple-subtypep) (type1 type2)
954   (let ((hairy-spec1 (hairy-type-specifier type1))
955         (hairy-spec2 (hairy-type-specifier type2)))
956     (cond ((and (consp hairy-spec1) (eq (car hairy-spec1) 'not)
957                 (consp hairy-spec2) (eq (car hairy-spec2) 'not))
958            (csubtypep (specifier-type (cadr hairy-spec2))
959                       (specifier-type (cadr hairy-spec1))))
960           ((equal hairy-spec1 hairy-spec2)
961            (values t t))
962           (t
963            (values nil nil)))))
964
965 (!define-type-method (hairy :complex-subtypep-arg2) (type1 type2)
966   (let ((hairy-spec (hairy-type-specifier type2)))
967     (cond ((and (consp hairy-spec) (eq (car hairy-spec) 'not))
968            (let* ((complement-type2 (specifier-type (cadr hairy-spec)))
969                   (intersection2 (type-intersection2 type1
970                                                      complement-type2)))
971              (if intersection2
972                  (values (eq intersection2 *empty-type*) t)
973                  (values nil nil))))
974           (t
975            (values nil nil)))))
976
977 (!define-type-method (hairy :complex-subtypep-arg1 :complex-=) (type1 type2)
978   (declare (ignore type1 type2))
979   (values nil nil))
980
981 (!define-type-method (hairy :simple-intersection2 :complex-intersection2)
982                      (type1 type2)
983   (declare (ignore type1 type2))
984   nil)
985
986 (!define-type-method (hairy :simple-=) (type1 type2)
987   (if (equal (hairy-type-specifier type1)
988              (hairy-type-specifier type2))
989       (values t t)
990       (values nil nil)))
991
992 (!def-type-translator not (&whole whole type)
993   (declare (ignore type))
994   ;; Check legality of arguments.
995   (destructuring-bind (not typespec) whole
996     (declare (ignore not))
997     (specifier-type typespec)) ; must be legal typespec
998   ;; Create object.
999   (make-hairy-type :specifier whole))
1000
1001 (!def-type-translator satisfies (&whole whole fun)
1002   (declare (ignore fun))
1003   ;; Check legality of arguments.
1004   (destructuring-bind (satisfies predicate-name) whole
1005     (declare (ignore satisfies))
1006     (unless (symbolp predicate-name)
1007       (error 'simple-type-error
1008              :datum predicate-name
1009              :expected-type 'symbol
1010              :format-control "~S is not a symbol."
1011              :format-arguments (list predicate-name))))
1012   ;; Create object.
1013   (make-hairy-type :specifier whole))
1014 \f
1015 ;;;; numeric types
1016
1017 (!define-type-class number)
1018
1019 (!define-type-method (number :simple-=) (type1 type2)
1020   (values
1021    (and (eq (numeric-type-class type1) (numeric-type-class type2))
1022         (eq (numeric-type-format type1) (numeric-type-format type2))
1023         (eq (numeric-type-complexp type1) (numeric-type-complexp type2))
1024         (equal (numeric-type-low type1) (numeric-type-low type2))
1025         (equal (numeric-type-high type1) (numeric-type-high type2)))
1026    t))
1027
1028 (!define-type-method (number :unparse) (type)
1029   (let* ((complexp (numeric-type-complexp type))
1030          (low (numeric-type-low type))
1031          (high (numeric-type-high type))
1032          (base (case (numeric-type-class type)
1033                  (integer 'integer)
1034                  (rational 'rational)
1035                  (float (or (numeric-type-format type) 'float))
1036                  (t 'real))))
1037     (let ((base+bounds
1038            (cond ((and (eq base 'integer) high low)
1039                   (let ((high-count (logcount high))
1040                         (high-length (integer-length high)))
1041                     (cond ((= low 0)
1042                            (cond ((= high 0) '(integer 0 0))
1043                                  ((= high 1) 'bit)
1044                                  ((and (= high-count high-length)
1045                                        (plusp high-length))
1046                                   `(unsigned-byte ,high-length))
1047                                  (t
1048                                   `(mod ,(1+ high)))))
1049                           ((and (= low sb!vm:*target-most-negative-fixnum*)
1050                                 (= high sb!vm:*target-most-positive-fixnum*))
1051                            'fixnum)
1052                           ((and (= low (lognot high))
1053                                 (= high-count high-length)
1054                                 (> high-count 0))
1055                            `(signed-byte ,(1+ high-length)))
1056                           (t
1057                            `(integer ,low ,high)))))
1058                  (high `(,base ,(or low '*) ,high))
1059                  (low
1060                   (if (and (eq base 'integer) (= low 0))
1061                       'unsigned-byte
1062                       `(,base ,low)))
1063                  (t base))))
1064       (ecase complexp
1065         (:real
1066          base+bounds)
1067         (:complex
1068          (if (eq base+bounds 'real)
1069              'complex
1070              `(complex ,base+bounds)))
1071         ((nil)
1072          (aver (eq base+bounds 'real))
1073          'number)))))
1074
1075 ;;; Return true if X is "less than or equal" to Y, taking open bounds
1076 ;;; into consideration. CLOSED is the predicate used to test the bound
1077 ;;; on a closed interval (e.g. <=), and OPEN is the predicate used on
1078 ;;; open bounds (e.g. <). Y is considered to be the outside bound, in
1079 ;;; the sense that if it is infinite (NIL), then the test succeeds,
1080 ;;; whereas if X is infinite, then the test fails (unless Y is also
1081 ;;; infinite).
1082 ;;;
1083 ;;; This is for comparing bounds of the same kind, e.g. upper and
1084 ;;; upper. Use NUMERIC-BOUND-TEST* for different kinds of bounds.
1085 #!-negative-zero-is-not-zero
1086 (defmacro numeric-bound-test (x y closed open)
1087   `(cond ((not ,y) t)
1088          ((not ,x) nil)
1089          ((consp ,x)
1090           (if (consp ,y)
1091               (,closed (car ,x) (car ,y))
1092               (,closed (car ,x) ,y)))
1093          (t
1094           (if (consp ,y)
1095               (,open ,x (car ,y))
1096               (,closed ,x ,y)))))
1097
1098 #!+negative-zero-is-not-zero
1099 (defmacro numeric-bound-test-zero (op x y)
1100   `(if (and (zerop ,x) (zerop ,y) (floatp ,x) (floatp ,y))
1101        (,op (float-sign ,x) (float-sign ,y))
1102        (,op ,x ,y)))
1103
1104 #!+negative-zero-is-not-zero
1105 (defmacro numeric-bound-test (x y closed open)
1106   `(cond ((not ,y) t)
1107          ((not ,x) nil)
1108          ((consp ,x)
1109           (if (consp ,y)
1110               (numeric-bound-test-zero ,closed (car ,x) (car ,y))
1111               (numeric-bound-test-zero ,closed (car ,x) ,y)))
1112          (t
1113           (if (consp ,y)
1114               (numeric-bound-test-zero ,open ,x (car ,y))
1115               (numeric-bound-test-zero ,closed ,x ,y)))))
1116
1117 ;;; This is used to compare upper and lower bounds. This is different
1118 ;;; from the same-bound case:
1119 ;;; -- Since X = NIL is -infinity, whereas y = NIL is +infinity, we
1120 ;;;    return true if *either* arg is NIL.
1121 ;;; -- an open inner bound is "greater" and also squeezes the interval,
1122 ;;;    causing us to use the OPEN test for those cases as well.
1123 #!-negative-zero-is-not-zero
1124 (defmacro numeric-bound-test* (x y closed open)
1125   `(cond ((not ,y) t)
1126          ((not ,x) t)
1127          ((consp ,x)
1128           (if (consp ,y)
1129               (,open (car ,x) (car ,y))
1130               (,open (car ,x) ,y)))
1131          (t
1132           (if (consp ,y)
1133               (,open ,x (car ,y))
1134               (,closed ,x ,y)))))
1135
1136 #!+negative-zero-is-not-zero
1137 (defmacro numeric-bound-test* (x y closed open)
1138   `(cond ((not ,y) t)
1139          ((not ,x) t)
1140          ((consp ,x)
1141           (if (consp ,y)
1142               (numeric-bound-test-zero ,open (car ,x) (car ,y))
1143               (numeric-bound-test-zero ,open (car ,x) ,y)))
1144          (t
1145           (if (consp ,y)
1146               (numeric-bound-test-zero ,open ,x (car ,y))
1147               (numeric-bound-test-zero ,closed ,x ,y)))))
1148
1149 ;;; Return whichever of the numeric bounds X and Y is "maximal"
1150 ;;; according to the predicates CLOSED (e.g. >=) and OPEN (e.g. >).
1151 ;;; This is only meaningful for maximizing like bounds, i.e. upper and
1152 ;;; upper. If MAX-P is true, then we return NIL if X or Y is NIL,
1153 ;;; otherwise we return the other arg.
1154 (defmacro numeric-bound-max (x y closed open max-p)
1155   (once-only ((n-x x)
1156               (n-y y))
1157     `(cond ((not ,n-x) ,(if max-p nil n-y))
1158            ((not ,n-y) ,(if max-p nil n-x))
1159            ((consp ,n-x)
1160             (if (consp ,n-y)
1161                 (if (,closed (car ,n-x) (car ,n-y)) ,n-x ,n-y)
1162                 (if (,open (car ,n-x) ,n-y) ,n-x ,n-y)))
1163            (t
1164             (if (consp ,n-y)
1165                 (if (,open (car ,n-y) ,n-x) ,n-y ,n-x)
1166                 (if (,closed ,n-y ,n-x) ,n-y ,n-x))))))
1167
1168 (!define-type-method (number :simple-subtypep) (type1 type2)
1169   (let ((class1 (numeric-type-class type1))
1170         (class2 (numeric-type-class type2))
1171         (complexp2 (numeric-type-complexp type2))
1172         (format2 (numeric-type-format type2))
1173         (low1 (numeric-type-low type1))
1174         (high1 (numeric-type-high type1))
1175         (low2 (numeric-type-low type2))
1176         (high2 (numeric-type-high type2)))
1177     ;; If one is complex and the other isn't, they are disjoint.
1178     (cond ((not (or (eq (numeric-type-complexp type1) complexp2)
1179                     (null complexp2)))
1180            (values nil t))
1181           ;; If the classes are specified and different, the types are
1182           ;; disjoint unless type2 is rational and type1 is integer.
1183           ((not (or (eq class1 class2)
1184                     (null class2)
1185                     (and (eq class1 'integer)
1186                          (eq class2 'rational))))
1187            (values nil t))
1188           ;; If the float formats are specified and different, the types
1189           ;; are disjoint.
1190           ((not (or (eq (numeric-type-format type1) format2)
1191                     (null format2)))
1192            (values nil t))
1193           ;; Check the bounds.
1194           ((and (numeric-bound-test low1 low2 >= >)
1195                 (numeric-bound-test high1 high2 <= <))
1196            (values t t))
1197           (t
1198            (values nil t)))))
1199
1200 (!define-superclasses number ((generic-number)) !cold-init-forms)
1201
1202 ;;; If the high bound of LOW is adjacent to the low bound of HIGH,
1203 ;;; then return true, otherwise NIL.
1204 (defun numeric-types-adjacent (low high)
1205   (let ((low-bound (numeric-type-high low))
1206         (high-bound (numeric-type-low high)))
1207     (cond ((not (and low-bound high-bound)) nil)
1208           ((and (consp low-bound) (consp high-bound)) nil)
1209           ((consp low-bound)
1210            #!-negative-zero-is-not-zero
1211            (let ((low-value (car low-bound)))
1212              (or (eql low-value high-bound)
1213                  (and (eql low-value -0f0) (eql high-bound 0f0))
1214                  (and (eql low-value 0f0) (eql high-bound -0f0))
1215                  (and (eql low-value -0d0) (eql high-bound 0d0))
1216                  (and (eql low-value 0d0) (eql high-bound -0d0))))
1217            #!+negative-zero-is-not-zero
1218            (eql (car low-bound) high-bound))
1219           ((consp high-bound)
1220            #!-negative-zero-is-not-zero
1221            (let ((high-value (car high-bound)))
1222              (or (eql high-value low-bound)
1223                  (and (eql high-value -0f0) (eql low-bound 0f0))
1224                  (and (eql high-value 0f0) (eql low-bound -0f0))
1225                  (and (eql high-value -0d0) (eql low-bound 0d0))
1226                  (and (eql high-value 0d0) (eql low-bound -0d0))))
1227            #!+negative-zero-is-not-zero
1228            (eql (car high-bound) low-bound))
1229           #!+negative-zero-is-not-zero
1230           ((or (and (eql low-bound -0f0) (eql high-bound 0f0))
1231                (and (eql low-bound -0d0) (eql high-bound 0d0))))
1232           ((and (eq (numeric-type-class low) 'integer)
1233                 (eq (numeric-type-class high) 'integer))
1234            (eql (1+ low-bound) high-bound))
1235           (t
1236            nil))))
1237
1238 ;;; Return a numeric type that is a supertype for both TYPE1 and TYPE2.
1239 ;;;
1240 ;;; ### Note: we give up early to keep from dropping lots of information on
1241 ;;; the floor by returning overly general types.
1242 (!define-type-method (number :simple-union2) (type1 type2)
1243   (declare (type numeric-type type1 type2))
1244   (cond ((csubtypep type1 type2) type2)
1245         ((csubtypep type2 type1) type1)
1246         (t
1247          (let ((class1 (numeric-type-class type1))
1248                (format1 (numeric-type-format type1))
1249                (complexp1 (numeric-type-complexp type1))
1250                (class2 (numeric-type-class type2))
1251                (format2 (numeric-type-format type2))
1252                (complexp2 (numeric-type-complexp type2)))
1253            (when (and (eq class1 class2)
1254                       (eq format1 format2)
1255                       (eq complexp1 complexp2)
1256                       (or (numeric-types-intersect type1 type2)
1257                           (numeric-types-adjacent type1 type2)
1258                           (numeric-types-adjacent type2 type1)))
1259              (make-numeric-type
1260               :class class1
1261               :format format1
1262               :complexp complexp1
1263               :low (numeric-bound-max (numeric-type-low type1)
1264                                       (numeric-type-low type2)
1265                                       <= < t)
1266               :high (numeric-bound-max (numeric-type-high type1)
1267                                        (numeric-type-high type2)
1268                                        >= > t)))))))
1269
1270 (!cold-init-forms
1271   (setf (info :type :kind 'number) :primitive)
1272   (setf (info :type :builtin 'number)
1273         (make-numeric-type :complexp nil)))
1274
1275 (!def-type-translator complex (&optional (typespec '*))
1276   (if (eq typespec '*)
1277       (make-numeric-type :complexp :complex)
1278       (labels ((not-numeric ()
1279                  ;; FIXME: should probably be TYPE-ERROR
1280                  (error "The component type for COMPLEX is not numeric: ~S"
1281                         typespec))
1282                (complex1 (component-type)
1283                  (unless (numeric-type-p component-type)
1284                    ;; FIXME: As per the FIXME below, ANSI says we're
1285                    ;; supposed to handle any subtype of REAL, not only
1286                    ;; those which can be represented as NUMERIC-TYPE.
1287                    (not-numeric))
1288                  (when (eq (numeric-type-complexp component-type) :complex)
1289                    (error "The component type for COMPLEX is complex: ~S"
1290                           typespec))
1291                  (modified-numeric-type component-type :complexp :complex)))
1292         (let ((type (specifier-type typespec)))
1293           (typecase type
1294             ;; This is all that CMU CL handled.
1295             (numeric-type (complex1 type))
1296             ;; We need to handle UNION-TYPEs in order to deal with
1297             ;; REAL and FLOAT being represented as UNION-TYPEs of more
1298             ;; primitive types.
1299             (union-type (apply #'type-union
1300                                (mapcar #'complex1
1301                                        (union-type-types type))))
1302             ;; FIXME: ANSI just says that TYPESPEC is a subtype of type
1303             ;; REAL, not necessarily a NUMERIC-TYPE. E.g. TYPESPEC could
1304             ;; legally be (AND REAL (SATISFIES ODDP))! But like the old
1305             ;; CMU CL code, we're still not nearly that general.
1306             (t (not-numeric)))))))
1307
1308 ;;; If X is *, return NIL, otherwise return the bound, which must be a
1309 ;;; member of TYPE or a one-element list of a member of TYPE.
1310 #!-sb-fluid (declaim (inline canonicalized-bound))
1311 (defun canonicalized-bound (bound type)
1312   (cond ((eq bound '*) nil)
1313         ((or (sb!xc:typep bound type)
1314              (and (consp bound)
1315                   (sb!xc:typep (car bound) type)
1316                   (null (cdr bound))))
1317           bound)
1318         (t
1319          (error "Bound is not ~S, a ~S or a list of a ~S: ~S"
1320                 '*
1321                 type
1322                 type
1323                 bound))))
1324
1325 (!def-type-translator integer (&optional (low '*) (high '*))
1326   (let* ((l (canonicalized-bound low 'integer))
1327          (lb (if (consp l) (1+ (car l)) l))
1328          (h (canonicalized-bound high 'integer))
1329          (hb (if (consp h) (1- (car h)) h)))
1330     (when (and hb lb (< hb lb))
1331       (error "Lower bound ~S is greater than upper bound ~S." l h))
1332     (make-numeric-type :class 'integer
1333                        :complexp :real
1334                        :enumerable (not (null (and l h)))
1335                        :low lb
1336                        :high hb)))
1337
1338 (defmacro !def-bounded-type (type class format)
1339   `(!def-type-translator ,type (&optional (low '*) (high '*))
1340      (let ((lb (canonicalized-bound low ',type))
1341            (hb (canonicalized-bound high ',type)))
1342        (unless (numeric-bound-test* lb hb <= <)
1343          (error "Lower bound ~S is not less than upper bound ~S." low high))
1344        (make-numeric-type :class ',class :format ',format :low lb :high hb))))
1345
1346 (!def-bounded-type rational rational nil)
1347
1348 ;;; Unlike CMU CL, we represent the types FLOAT and REAL as
1349 ;;; UNION-TYPEs of more primitive types, in order to make
1350 ;;; type representation more unique, avoiding problems in the
1351 ;;; simplification of things like
1352 ;;;   (subtypep '(or (single-float -1.0 1.0) (single-float 0.1))
1353 ;;;             '(or (real -1 7) (single-float 0.1) (single-float -1.0 1.0)))
1354 ;;; When we allowed REAL to remain as a separate NUMERIC-TYPE,
1355 ;;; it was too easy for the first argument to be simplified to
1356 ;;; '(SINGLE-FLOAT -1.0), and for the second argument to be simplified
1357 ;;; to '(OR (REAL -1 7) (SINGLE-FLOAT 0.1)) and then for the
1358 ;;; SUBTYPEP to fail (returning NIL,T instead of T,T) because
1359 ;;; the first argument can't be seen to be a subtype of any of the
1360 ;;; terms in the second argument.
1361 ;;;
1362 ;;; The old CMU CL way was:
1363 ;;;   (!def-bounded-type float float nil)
1364 ;;;   (!def-bounded-type real nil nil)
1365 ;;;
1366 ;;; FIXME: If this new way works for a while with no weird new
1367 ;;; problems, we can go back and rip out support for separate FLOAT
1368 ;;; and REAL flavors of NUMERIC-TYPE. The new way was added in
1369 ;;; sbcl-0.6.11.22, 2001-03-21.
1370 ;;;
1371 ;;; FIXME: It's probably necessary to do something to fix the
1372 ;;; analogous problem with INTEGER and RATIONAL types. Perhaps
1373 ;;; bounded RATIONAL types should be represented as (OR RATIO INTEGER).
1374 (defun coerce-bound (bound type inner-coerce-bound-fun)
1375   (declare (type function inner-coerce-bound-fun))
1376   (cond ((eql bound '*)
1377          bound)
1378         ((consp bound)
1379          (destructuring-bind (inner-bound) bound
1380            (list (funcall inner-coerce-bound-fun inner-bound type))))
1381         (t
1382          (funcall inner-coerce-bound-fun bound type))))
1383 (defun inner-coerce-real-bound (bound type)
1384   (ecase type
1385     (rational (rationalize bound))
1386     (float (if (floatp bound)
1387                bound
1388                ;; Coerce to the widest float format available, to
1389                ;; avoid unnecessary loss of precision:
1390                (coerce bound 'long-float)))))
1391 (defun coerced-real-bound (bound type)
1392   (coerce-bound bound type #'inner-coerce-real-bound))
1393 (defun coerced-float-bound (bound type)
1394   (coerce-bound bound type #'coerce))
1395 (!def-type-translator real (&optional (low '*) (high '*))
1396   (specifier-type `(or (float ,(coerced-real-bound  low 'float)
1397                               ,(coerced-real-bound high 'float))
1398                        (rational ,(coerced-real-bound  low 'rational)
1399                                  ,(coerced-real-bound high 'rational)))))
1400 (!def-type-translator float (&optional (low '*) (high '*))
1401   (specifier-type 
1402    `(or (single-float ,(coerced-float-bound  low 'single-float)
1403                       ,(coerced-float-bound high 'single-float))
1404         (double-float ,(coerced-float-bound  low 'double-float)
1405                       ,(coerced-float-bound high 'double-float))
1406         #!+long-float ,(error "stub: no long float support yet"))))
1407
1408 (defmacro !define-float-format (f)
1409   `(!def-bounded-type ,f float ,f))
1410
1411 (!define-float-format short-float)
1412 (!define-float-format single-float)
1413 (!define-float-format double-float)
1414 (!define-float-format long-float)
1415
1416 (defun numeric-types-intersect (type1 type2)
1417   (declare (type numeric-type type1 type2))
1418   (let* ((class1 (numeric-type-class type1))
1419          (class2 (numeric-type-class type2))
1420          (complexp1 (numeric-type-complexp type1))
1421          (complexp2 (numeric-type-complexp type2))
1422          (format1 (numeric-type-format type1))
1423          (format2 (numeric-type-format type2))
1424          (low1 (numeric-type-low type1))
1425          (high1 (numeric-type-high type1))
1426          (low2 (numeric-type-low type2))
1427          (high2 (numeric-type-high type2)))
1428     ;; If one is complex and the other isn't, then they are disjoint.
1429     (cond ((not (or (eq complexp1 complexp2)
1430                     (null complexp1) (null complexp2)))
1431            nil)
1432           ;; If either type is a float, then the other must either be
1433           ;; specified to be a float or unspecified. Otherwise, they
1434           ;; are disjoint.
1435           ((and (eq class1 'float)
1436                 (not (member class2 '(float nil)))) nil)
1437           ((and (eq class2 'float)
1438                 (not (member class1 '(float nil)))) nil)
1439           ;; If the float formats are specified and different, the
1440           ;; types are disjoint.
1441           ((not (or (eq format1 format2) (null format1) (null format2)))
1442            nil)
1443           (t
1444            ;; Check the bounds. This is a bit odd because we must
1445            ;; always have the outer bound of the interval as the
1446            ;; second arg.
1447            (if (numeric-bound-test high1 high2 <= <)
1448                (or (and (numeric-bound-test low1 low2 >= >)
1449                         (numeric-bound-test* low1 high2 <= <))
1450                    (and (numeric-bound-test low2 low1 >= >)
1451                         (numeric-bound-test* low2 high1 <= <)))
1452                (or (and (numeric-bound-test* low2 high1 <= <)
1453                         (numeric-bound-test low2 low1 >= >))
1454                    (and (numeric-bound-test high2 high1 <= <)
1455                         (numeric-bound-test* high2 low1 >= >))))))))
1456
1457 ;;; Take the numeric bound X and convert it into something that can be
1458 ;;; used as a bound in a numeric type with the specified CLASS and
1459 ;;; FORMAT. If UP-P is true, then we round up as needed, otherwise we
1460 ;;; round down. UP-P true implies that X is a lower bound, i.e. (N) > N.
1461 ;;;
1462 ;;; This is used by NUMERIC-TYPE-INTERSECTION to mash the bound into
1463 ;;; the appropriate type number. X may only be a float when CLASS is
1464 ;;; FLOAT.
1465 ;;;
1466 ;;; ### Note: it is possible for the coercion to a float to overflow
1467 ;;; or underflow. This happens when the bound doesn't fit in the
1468 ;;; specified format. In this case, we should really return the
1469 ;;; appropriate {Most | Least}-{Positive | Negative}-XXX-Float float
1470 ;;; of desired format. But these conditions aren't currently signalled
1471 ;;; in any useful way.
1472 ;;;
1473 ;;; Also, when converting an open rational bound into a float we
1474 ;;; should probably convert it to a closed bound of the closest float
1475 ;;; in the specified format. KLUDGE: In general, open float bounds are
1476 ;;; screwed up. -- (comment from original CMU CL)
1477 (defun round-numeric-bound (x class format up-p)
1478   (if x
1479       (let ((cx (if (consp x) (car x) x)))
1480         (ecase class
1481           ((nil rational) x)
1482           (integer
1483            (if (and (consp x) (integerp cx))
1484                (if up-p (1+ cx) (1- cx))
1485                (if up-p (ceiling cx) (floor cx))))
1486           (float
1487            (let ((res (if format (coerce cx format) (float cx))))
1488              (if (consp x) (list res) res)))))
1489       nil))
1490
1491 ;;; Handle the case of type intersection on two numeric types. We use
1492 ;;; TYPES-EQUAL-OR-INTERSECT to throw out the case of types with no
1493 ;;; intersection. If an attribute in TYPE1 is unspecified, then we use
1494 ;;; TYPE2's attribute, which must be at least as restrictive. If the
1495 ;;; types intersect, then the only attributes that can be specified
1496 ;;; and different are the class and the bounds.
1497 ;;;
1498 ;;; When the class differs, we use the more restrictive class. The
1499 ;;; only interesting case is RATIONAL/INTEGER, since RATIONAL includes
1500 ;;; INTEGER.
1501 ;;;
1502 ;;; We make the result lower (upper) bound the maximum (minimum) of
1503 ;;; the argument lower (upper) bounds. We convert the bounds into the
1504 ;;; appropriate numeric type before maximizing. This avoids possible
1505 ;;; confusion due to mixed-type comparisons (but I think the result is
1506 ;;; the same).
1507 (!define-type-method (number :simple-intersection2) (type1 type2)
1508   (declare (type numeric-type type1 type2))
1509   (if (numeric-types-intersect type1 type2)
1510       (let* ((class1 (numeric-type-class type1))
1511              (class2 (numeric-type-class type2))
1512              (class (ecase class1
1513                       ((nil) class2)
1514                       ((integer float) class1)
1515                       (rational (if (eq class2 'integer)
1516                                        'integer
1517                                        'rational))))
1518              (format (or (numeric-type-format type1)
1519                          (numeric-type-format type2))))
1520         (make-numeric-type
1521          :class class
1522          :format format
1523          :complexp (or (numeric-type-complexp type1)
1524                        (numeric-type-complexp type2))
1525          :low (numeric-bound-max
1526                (round-numeric-bound (numeric-type-low type1)
1527                                     class format t)
1528                (round-numeric-bound (numeric-type-low type2)
1529                                     class format t)
1530                > >= nil)
1531          :high (numeric-bound-max
1532                 (round-numeric-bound (numeric-type-high type1)
1533                                      class format nil)
1534                 (round-numeric-bound (numeric-type-high type2)
1535                                      class format nil)
1536                 < <= nil)))
1537       *empty-type*))
1538
1539 ;;; Given two float formats, return the one with more precision. If
1540 ;;; either one is null, return NIL.
1541 (defun float-format-max (f1 f2)
1542   (when (and f1 f2)
1543     (dolist (f *float-formats* (error "bad float format: ~S" f1))
1544       (when (or (eq f f1) (eq f f2))
1545         (return f)))))
1546
1547 ;;; Return the result of an operation on TYPE1 and TYPE2 according to
1548 ;;; the rules of numeric contagion. This is always NUMBER, some float
1549 ;;; format (possibly complex) or RATIONAL. Due to rational
1550 ;;; canonicalization, there isn't much we can do here with integers or
1551 ;;; rational complex numbers.
1552 ;;;
1553 ;;; If either argument is not a NUMERIC-TYPE, then return NUMBER. This
1554 ;;; is useful mainly for allowing types that are technically numbers,
1555 ;;; but not a NUMERIC-TYPE.
1556 (defun numeric-contagion (type1 type2)
1557   (if (and (numeric-type-p type1) (numeric-type-p type2))
1558       (let ((class1 (numeric-type-class type1))
1559             (class2 (numeric-type-class type2))
1560             (format1 (numeric-type-format type1))
1561             (format2 (numeric-type-format type2))
1562             (complexp1 (numeric-type-complexp type1))
1563             (complexp2 (numeric-type-complexp type2)))
1564         (cond ((or (null complexp1)
1565                    (null complexp2))
1566                (specifier-type 'number))
1567               ((eq class1 'float)
1568                (make-numeric-type
1569                 :class 'float
1570                 :format (ecase class2
1571                           (float (float-format-max format1 format2))
1572                           ((integer rational) format1)
1573                           ((nil)
1574                            ;; A double-float with any real number is a
1575                            ;; double-float.
1576                            #!-long-float
1577                            (if (eq format1 'double-float)
1578                              'double-float
1579                              nil)
1580                            ;; A long-float with any real number is a
1581                            ;; long-float.
1582                            #!+long-float
1583                            (if (eq format1 'long-float)
1584                              'long-float
1585                              nil)))
1586                 :complexp (if (or (eq complexp1 :complex)
1587                                   (eq complexp2 :complex))
1588                               :complex
1589                               :real)))
1590               ((eq class2 'float) (numeric-contagion type2 type1))
1591               ((and (eq complexp1 :real) (eq complexp2 :real))
1592                (make-numeric-type
1593                 :class (and class1 class2 'rational)
1594                 :complexp :real))
1595               (t
1596                (specifier-type 'number))))
1597       (specifier-type 'number)))
1598 \f
1599 ;;;; array types
1600
1601 (!define-type-class array)
1602
1603 ;;; What this does depends on the setting of the
1604 ;;; *USE-IMPLEMENTATION-TYPES* switch. If true, return the specialized
1605 ;;; element type, otherwise return the original element type.
1606 (defun specialized-element-type-maybe (type)
1607   (declare (type array-type type))
1608   (if *use-implementation-types*
1609       (array-type-specialized-element-type type)
1610       (array-type-element-type type)))
1611
1612 (!define-type-method (array :simple-=) (type1 type2)
1613   (values (and (equal (array-type-dimensions type1)
1614                       (array-type-dimensions type2))
1615                (eq (array-type-complexp type1)
1616                    (array-type-complexp type2))
1617                (type= (specialized-element-type-maybe type1)
1618                       (specialized-element-type-maybe type2)))
1619           t))
1620
1621 (!define-type-method (array :unparse) (type)
1622   (let ((dims (array-type-dimensions type))
1623         (eltype (type-specifier (array-type-element-type type)))
1624         (complexp (array-type-complexp type)))
1625     (cond ((eq dims '*)
1626            (if (eq eltype '*)
1627                (if complexp 'array 'simple-array)
1628                (if complexp `(array ,eltype) `(simple-array ,eltype))))
1629           ((= (length dims) 1)
1630            (if complexp
1631                (if (eq (car dims) '*)
1632                    (case eltype
1633                      (bit 'bit-vector)
1634                      (base-char 'base-string)
1635                      (character 'string)
1636                      (* 'vector)
1637                      (t `(vector ,eltype)))
1638                    (case eltype
1639                      (bit `(bit-vector ,(car dims)))
1640                      (base-char `(base-string ,(car dims)))
1641                      (character `(string ,(car dims)))
1642                      (t `(vector ,eltype ,(car dims)))))
1643                (if (eq (car dims) '*)
1644                    (case eltype
1645                      (bit 'simple-bit-vector)
1646                      (base-char 'simple-base-string)
1647                      (character 'simple-string)
1648                      ((t) 'simple-vector)
1649                      (t `(simple-array ,eltype (*))))
1650                    (case eltype
1651                      (bit `(simple-bit-vector ,(car dims)))
1652                      (base-char `(simple-base-string ,(car dims)))
1653                      (character `(simple-string ,(car dims)))
1654                      ((t) `(simple-vector ,(car dims)))
1655                      (t `(simple-array ,eltype ,dims))))))
1656           (t
1657            (if complexp
1658                `(array ,eltype ,dims)
1659                `(simple-array ,eltype ,dims))))))
1660
1661 (!define-type-method (array :simple-subtypep) (type1 type2)
1662   (let ((dims1 (array-type-dimensions type1))
1663         (dims2 (array-type-dimensions type2))
1664         (complexp2 (array-type-complexp type2)))
1665     (cond (;; not subtypep unless dimensions are compatible
1666            (not (or (eq dims2 '*)
1667                     (and (not (eq dims1 '*))
1668                          ;; (sbcl-0.6.4 has trouble figuring out that
1669                          ;; DIMS1 and DIMS2 must be lists at this
1670                          ;; point, and knowing that is important to
1671                          ;; compiling EVERY efficiently.)
1672                          (= (length (the list dims1))
1673                             (length (the list dims2)))
1674                          (every (lambda (x y)
1675                                   (or (eq y '*) (eql x y)))
1676                                 (the list dims1)
1677                                 (the list dims2)))))
1678            (values nil t))
1679           ;; not subtypep unless complexness is compatible
1680           ((not (or (eq complexp2 :maybe)
1681                     (eq (array-type-complexp type1) complexp2)))
1682            (values nil t))
1683           ;; Since we didn't fail any of the tests above, we win
1684           ;; if the TYPE2 element type is wild.
1685           ((eq (array-type-element-type type2) *wild-type*)
1686            (values t t))
1687           (;; Since we didn't match any of the special cases above, we
1688            ;; can't give a good answer unless both the element types
1689            ;; have been defined.
1690            (or (unknown-type-p (array-type-element-type type1))
1691                (unknown-type-p (array-type-element-type type2)))
1692            (values nil nil))
1693           (;; Otherwise, the subtype relationship holds iff the
1694            ;; types are equal, and they're equal iff the specialized
1695            ;; element types are identical.
1696            t
1697            (values (type= (specialized-element-type-maybe type1)
1698                           (specialized-element-type-maybe type2))
1699                    t)))))
1700
1701 (!define-superclasses array
1702   ((string string)
1703    (vector vector)
1704    (array))
1705   !cold-init-forms)
1706
1707 (defun array-types-intersect (type1 type2)
1708   (declare (type array-type type1 type2))
1709   (let ((dims1 (array-type-dimensions type1))
1710         (dims2 (array-type-dimensions type2))
1711         (complexp1 (array-type-complexp type1))
1712         (complexp2 (array-type-complexp type2)))
1713     ;; See whether dimensions are compatible.
1714     (cond ((not (or (eq dims1 '*) (eq dims2 '*)
1715                     (and (= (length dims1) (length dims2))
1716                          (every #'(lambda (x y)
1717                                     (or (eq x '*) (eq y '*) (= x y)))
1718                                 dims1 dims2))))
1719            (values nil t))
1720           ;; See whether complexpness is compatible.
1721           ((not (or (eq complexp1 :maybe)
1722                     (eq complexp2 :maybe)
1723                     (eq complexp1 complexp2)))
1724            (values nil t))
1725           ;; If either element type is wild, then they intersect.
1726           ;; Otherwise, the types must be identical.
1727           ((or (eq (array-type-element-type type1) *wild-type*)
1728                (eq (array-type-element-type type2) *wild-type*)
1729                (type= (specialized-element-type-maybe type1)
1730                       (specialized-element-type-maybe type2)))
1731
1732            (values t t))
1733           (t
1734            (values nil t)))))
1735
1736 (!define-type-method (array :simple-intersection2) (type1 type2)
1737   (declare (type array-type type1 type2))
1738   (if (array-types-intersect type1 type2)
1739       (let ((dims1 (array-type-dimensions type1))
1740             (dims2 (array-type-dimensions type2))
1741             (complexp1 (array-type-complexp type1))
1742             (complexp2 (array-type-complexp type2))
1743             (eltype1 (array-type-element-type type1))
1744             (eltype2 (array-type-element-type type2)))
1745         (specialize-array-type
1746          (make-array-type
1747           :dimensions (cond ((eq dims1 '*) dims2)
1748                             ((eq dims2 '*) dims1)
1749                             (t
1750                              (mapcar (lambda (x y) (if (eq x '*) y x))
1751                                      dims1 dims2)))
1752           :complexp (if (eq complexp1 :maybe) complexp2 complexp1)
1753           :element-type (if (eq eltype1 *wild-type*) eltype2 eltype1))))
1754       *empty-type*))
1755
1756 ;;; Check a supplied dimension list to determine whether it is legal,
1757 ;;; and return it in canonical form (as either '* or a list).
1758 (defun canonical-array-dimensions (dims)
1759   (typecase dims
1760     ((member *) dims)
1761     (integer
1762      (when (minusp dims)
1763        (error "Arrays can't have a negative number of dimensions: ~S" dims))
1764      (when (>= dims sb!xc:array-rank-limit)
1765        (error "array type with too many dimensions: ~S" dims))
1766      (make-list dims :initial-element '*))
1767     (list
1768      (when (>= (length dims) sb!xc:array-rank-limit)
1769        (error "array type with too many dimensions: ~S" dims))
1770      (dolist (dim dims)
1771        (unless (eq dim '*)
1772          (unless (and (integerp dim)
1773                       (>= dim 0)
1774                       (< dim sb!xc:array-dimension-limit))
1775            (error "bad dimension in array type: ~S" dim))))
1776      dims)
1777     (t
1778      (error "Array dimensions is not a list, integer or *:~%  ~S" dims))))
1779 \f
1780 ;;;; MEMBER types
1781
1782 (!define-type-class member)
1783
1784 (!define-type-method (member :unparse) (type)
1785   (let ((members (member-type-members type)))
1786     (if (equal members '(nil))
1787         'null
1788         `(member ,@members))))
1789
1790 (!define-type-method (member :simple-subtypep) (type1 type2)
1791   (values (subsetp (member-type-members type1) (member-type-members type2))
1792           t))
1793
1794 (!define-type-method (member :complex-subtypep-arg1) (type1 type2)
1795   (every/type (swapped-args-fun #'ctypep)
1796               type2
1797               (member-type-members type1)))
1798
1799 ;;; We punt if the odd type is enumerable and intersects with the
1800 ;;; MEMBER type. If not enumerable, then it is definitely not a
1801 ;;; subtype of the MEMBER type.
1802 (!define-type-method (member :complex-subtypep-arg2) (type1 type2)
1803   (cond ((not (type-enumerable type1)) (values nil t))
1804         ((types-equal-or-intersect type1 type2) (values nil nil))
1805         (t (values nil t))))
1806
1807 (!define-type-method (member :simple-intersection2) (type1 type2)
1808   (let ((mem1 (member-type-members type1))
1809         (mem2 (member-type-members type2)))
1810     (cond ((subsetp mem1 mem2) type1)
1811           ((subsetp mem2 mem1) type2)
1812           (t
1813            (let ((res (intersection mem1 mem2)))
1814              (if res
1815                  (make-member-type :members res)
1816                  *empty-type*))))))
1817
1818 (!define-type-method (member :complex-intersection2) (type1 type2)
1819   (block punt                
1820     (collect ((members))
1821       (let ((mem2 (member-type-members type2)))
1822         (dolist (member mem2)
1823           (multiple-value-bind (val win) (ctypep member type1)
1824             (unless win
1825               (return-from punt nil))
1826             (when val (members member))))
1827         (cond ((subsetp mem2 (members)) type2)
1828               ((null (members)) *empty-type*)
1829               (t
1830                (make-member-type :members (members))))))))
1831
1832 ;;; We don't need a :COMPLEX-UNION2, since the only interesting case is
1833 ;;; a union type, and the member/union interaction is handled by the
1834 ;;; union type method.
1835 (!define-type-method (member :simple-union2) (type1 type2)
1836   (let ((mem1 (member-type-members type1))
1837         (mem2 (member-type-members type2)))
1838     (cond ((subsetp mem1 mem2) type2)
1839           ((subsetp mem2 mem1) type1)
1840           (t
1841            (make-member-type :members (union mem1 mem2))))))
1842
1843 (!define-type-method (member :simple-=) (type1 type2)
1844   (let ((mem1 (member-type-members type1))
1845         (mem2 (member-type-members type2)))
1846     (values (and (subsetp mem1 mem2)
1847                  (subsetp mem2 mem1))
1848             t)))
1849
1850 (!define-type-method (member :complex-=) (type1 type2)
1851   (if (type-enumerable type1)
1852       (multiple-value-bind (val win) (csubtypep type2 type1)
1853         (if (or val (not win))
1854             (values nil nil)
1855             (values nil t)))
1856       (values nil t)))
1857
1858 (!def-type-translator member (&rest members)
1859   (if members
1860     (make-member-type :members (remove-duplicates members))
1861     *empty-type*))
1862 \f
1863 ;;;; intersection types
1864 ;;;;
1865 ;;;; Until version 0.6.10.6, SBCL followed the original CMU CL approach
1866 ;;;; of punting on all AND types, not just the unreasonably complicated
1867 ;;;; ones. The change was motivated by trying to get the KEYWORD type
1868 ;;;; to behave sensibly:
1869 ;;;;    ;; reasonable definition
1870 ;;;;    (DEFTYPE KEYWORD () '(AND SYMBOL (SATISFIES KEYWORDP)))
1871 ;;;;    ;; reasonable behavior
1872 ;;;;    (AVER (SUBTYPEP 'KEYWORD 'SYMBOL))
1873 ;;;; Without understanding a little about the semantics of AND, we'd
1874 ;;;; get (SUBTYPEP 'KEYWORD 'SYMBOL)=>NIL,NIL and, for entirely
1875 ;;;; parallel reasons, (SUBTYPEP 'RATIO 'NUMBER)=>NIL,NIL. That's
1876 ;;;; not so good..)
1877 ;;;;
1878 ;;;; We still follow the example of CMU CL to some extent, by punting
1879 ;;;; (to the opaque HAIRY-TYPE) on sufficiently complicated types
1880 ;;;; involving AND.
1881
1882 (!define-type-class intersection)
1883
1884 ;;; A few intersection types have special names. The others just get
1885 ;;; mechanically unparsed.
1886 (!define-type-method (intersection :unparse) (type)
1887   (declare (type ctype type))
1888   (or (find type '(ratio bignum keyword) :key #'specifier-type :test #'type=)
1889       `(and ,@(mapcar #'type-specifier (intersection-type-types type)))))
1890
1891 ;;; shared machinery for type equality: true if every type in the set
1892 ;;; TYPES1 matches a type in the set TYPES2 and vice versa
1893 (defun type=-set (types1 types2)
1894   (flet (;; true if every type in the set X matches a type in the set Y
1895          (type<=-set (x y)
1896            (declare (type list x y))
1897            (every (lambda (xelement)
1898                     (position xelement y :test #'type=))
1899                   x)))
1900     (values (and (type<=-set types1 types2)
1901                  (type<=-set types2 types1))
1902             t)))
1903
1904 ;;; Two intersection types are equal if their subtypes are equal sets.
1905 ;;;
1906 ;;; FIXME: Might it be better to use
1907 ;;;   (AND (SUBTYPEP X Y) (SUBTYPEP Y X))
1908 ;;; instead, since SUBTYPEP is the usual relationship that we care
1909 ;;; most about, so it would be good to leverage any ingenuity there
1910 ;;; in this more obscure method?
1911 (!define-type-method (intersection :simple-=) (type1 type2)
1912   (type=-set (intersection-type-types type1)
1913              (intersection-type-types type2)))
1914
1915 (flet ((intersection-complex-subtypep-arg1 (type1 type2)
1916          (any/type (swapped-args-fun #'csubtypep)
1917                    type2
1918                    (intersection-type-types type1))))
1919   (!define-type-method (intersection :simple-subtypep) (type1 type2)
1920     (every/type #'intersection-complex-subtypep-arg1
1921                 type1
1922                 (intersection-type-types type2)))
1923   (!define-type-method (intersection :complex-subtypep-arg1) (type1 type2)
1924     (intersection-complex-subtypep-arg1 type1 type2)))
1925
1926 (!define-type-method (intersection :complex-subtypep-arg2) (type1 type2)
1927   (every/type #'csubtypep type1 (intersection-type-types type2)))
1928
1929 (!def-type-translator and (&whole whole &rest type-specifiers)
1930   (apply #'type-intersection
1931          (mapcar #'specifier-type
1932                  type-specifiers)))
1933 \f
1934 ;;;; union types
1935
1936 (!define-type-class union)
1937
1938 ;;; The LIST type has a special name. Other union types just get
1939 ;;; mechanically unparsed.
1940 (!define-type-method (union :unparse) (type)
1941   (declare (type ctype type))
1942   (if (type= type (specifier-type 'list))
1943       'list
1944       `(or ,@(mapcar #'type-specifier (union-type-types type)))))
1945
1946 ;;; Two union types are equal if their subtypes are equal sets.
1947 (!define-type-method (union :simple-=) (type1 type2)
1948   (type=-set (union-type-types type1)
1949              (union-type-types type2)))
1950
1951 ;;; Similarly, a union type is a subtype of another if every element
1952 ;;; of TYPE1 is a subtype of some element of TYPE2.
1953 (!define-type-method (union :simple-subtypep) (type1 type2)
1954   (every/type (swapped-args-fun #'union-complex-subtypep-arg2)
1955               type2
1956               (union-type-types type1)))
1957
1958 (defun union-complex-subtypep-arg1 (type1 type2)
1959   (every/type (swapped-args-fun #'csubtypep)
1960               type2
1961               (union-type-types type1)))
1962 (!define-type-method (union :complex-subtypep-arg1) (type1 type2)
1963   (union-complex-subtypep-arg1 type1 type2))
1964
1965 (defun union-complex-subtypep-arg2 (type1 type2)
1966   (any/type #'csubtypep type1 (union-type-types type2)))
1967 (!define-type-method (union :complex-subtypep-arg2) (type1 type2)
1968   (union-complex-subtypep-arg2 type1 type2))
1969
1970 (!define-type-method (union :simple-intersection2 :complex-intersection2)
1971                      (type1 type2)
1972   ;; The CSUBTYPEP clauses here let us simplify e.g.
1973   ;;   (TYPE-INTERSECTION2 (SPECIFIER-TYPE 'LIST)
1974   ;;                       (SPECIFIER-TYPE '(OR LIST VECTOR)))
1975   ;; (where LIST is (OR CONS NULL)).
1976   ;;
1977   ;; The tests are more or less (CSUBTYPEP TYPE1 TYPE2) and vice
1978   ;; versa, but it's important that we pre-expand them into
1979   ;; specialized operations on individual elements of
1980   ;; UNION-TYPE-TYPES, instead of using the ordinary call to
1981   ;; CSUBTYPEP, in order to avoid possibly invoking any methods which
1982   ;; might in turn invoke (TYPE-INTERSECTION2 TYPE1 TYPE2) and thus
1983   ;; cause infinite recursion.
1984   (cond ((union-complex-subtypep-arg2 type1 type2)
1985          type1)
1986         ((union-complex-subtypep-arg1 type2 type1)
1987          type2)
1988         (t 
1989          ;; KLUDGE: This code accumulates a sequence of TYPE-UNION2
1990          ;; operations in a particular order, and gives up if any of
1991          ;; the sub-unions turn out not to be simple. In other cases
1992          ;; ca. sbcl-0.6.11.15, that approach to taking a union was a
1993          ;; bad idea, since it can overlook simplifications which
1994          ;; might occur if the terms were accumulated in a different
1995          ;; order. It's possible that that will be a problem here too.
1996          ;; However, I can't think of a good example to demonstrate
1997          ;; it, and without an example to demonstrate it I can't write
1998          ;; test cases, and without test cases I don't want to
1999          ;; complicate the code to address what's still a hypothetical
2000          ;; problem. So I punted. -- WHN 2001-03-20
2001          (let ((accumulator *empty-type*))
2002            (dolist (t2 (union-type-types type2) accumulator)
2003              (setf accumulator
2004                    (type-union2 accumulator
2005                                 (type-intersection type1 t2)))
2006              ;; When our result isn't simple any more (because
2007              ;; TYPE-UNION2 was unable to give us a simple result)
2008              (unless accumulator
2009                (return nil)))))))
2010
2011 (!def-type-translator or (&rest type-specifiers)
2012   (apply #'type-union
2013          (mapcar #'specifier-type
2014                  type-specifiers)))
2015 \f
2016 ;;;; CONS types
2017
2018 (!define-type-class cons)
2019
2020 (!def-type-translator cons (&optional (car-type-spec '*) (cdr-type-spec '*))
2021   (make-cons-type (specifier-type car-type-spec)
2022                   (specifier-type cdr-type-spec)))
2023  
2024 (!define-type-method (cons :unparse) (type)
2025   (let ((car-eltype (type-specifier (cons-type-car-type type)))
2026         (cdr-eltype (type-specifier (cons-type-cdr-type type))))
2027     (if (and (member car-eltype '(t *))
2028              (member cdr-eltype '(t *)))
2029         'cons
2030         `(cons ,car-eltype ,cdr-eltype))))
2031  
2032 (!define-type-method (cons :simple-=) (type1 type2)
2033   (declare (type cons-type type1 type2))
2034   (and (type= (cons-type-car-type type1) (cons-type-car-type type2))
2035        (type= (cons-type-cdr-type type1) (cons-type-cdr-type type2))))
2036  
2037 (!define-type-method (cons :simple-subtypep) (type1 type2)
2038   (declare (type cons-type type1 type2))
2039   (multiple-value-bind (val-car win-car)
2040       (csubtypep (cons-type-car-type type1) (cons-type-car-type type2))
2041     (multiple-value-bind (val-cdr win-cdr)
2042         (csubtypep (cons-type-cdr-type type1) (cons-type-cdr-type type2))
2043       (if (and val-car val-cdr)
2044           (values t (and win-car win-cdr))
2045           (values nil (or win-car win-cdr))))))
2046  
2047 ;;; Give up if a precise type is not possible, to avoid returning
2048 ;;; overly general types.
2049 (!define-type-method (cons :simple-union2) (type1 type2)
2050   (declare (type cons-type type1 type2))
2051   (let ((car-type1 (cons-type-car-type type1))
2052         (car-type2 (cons-type-car-type type2))
2053         (cdr-type1 (cons-type-cdr-type type1))
2054         (cdr-type2 (cons-type-cdr-type type2)))
2055     (cond ((type= car-type1 car-type2)
2056            (make-cons-type car-type1
2057                            (type-union cdr-type1 cdr-type2)))
2058           ((type= cdr-type1 cdr-type2)
2059            (make-cons-type (type-union cdr-type1 cdr-type2)
2060                            cdr-type1)))))
2061
2062 (!define-type-method (cons :simple-intersection2) (type1 type2)
2063   (declare (type cons-type type1 type2))
2064   (let (car-int2
2065         cdr-int2)
2066     (and (setf car-int2 (type-intersection2 (cons-type-car-type type1)
2067                                             (cons-type-car-type type2)))
2068          (setf cdr-int2 (type-intersection2 (cons-type-cdr-type type1)
2069                                             (cons-type-cdr-type type2)))
2070          (make-cons-type car-int2 cdr-int2))))
2071 \f
2072 ;;; Return the type that describes all objects that are in X but not
2073 ;;; in Y. If we can't determine this type, then return NIL.
2074 ;;;
2075 ;;; For now, we only are clever dealing with union and member types.
2076 ;;; If either type is not a union type, then we pretend that it is a
2077 ;;; union of just one type. What we do is remove from X all the types
2078 ;;; that are a subtype any type in Y. If any type in X intersects with
2079 ;;; a type in Y but is not a subtype, then we give up.
2080 ;;;
2081 ;;; We must also special-case any member type that appears in the
2082 ;;; union. We remove from X's members all objects that are TYPEP to Y.
2083 ;;; If Y has any members, we must be careful that none of those
2084 ;;; members are CTYPEP to any of Y's non-member types. We give up in
2085 ;;; this case, since to compute that difference we would have to break
2086 ;;; the type from X into some collection of types that represents the
2087 ;;; type without that particular element. This seems too hairy to be
2088 ;;; worthwhile, given its low utility.
2089 (defun type-difference (x y)
2090   (let ((x-types (if (union-type-p x) (union-type-types x) (list x)))
2091         (y-types (if (union-type-p y) (union-type-types y) (list y))))
2092     (collect ((res))
2093       (dolist (x-type x-types)
2094         (if (member-type-p x-type)
2095             (collect ((members))
2096               (dolist (mem (member-type-members x-type))
2097                 (multiple-value-bind (val win) (ctypep mem y)
2098                   (unless win (return-from type-difference nil))
2099                   (unless val
2100                     (members mem))))
2101               (when (members)
2102                 (res (make-member-type :members (members)))))
2103             (dolist (y-type y-types (res x-type))
2104               (multiple-value-bind (val win) (csubtypep x-type y-type)
2105                 (unless win (return-from type-difference nil))
2106                 (when val (return))
2107                 (when (types-equal-or-intersect x-type y-type)
2108                   (return-from type-difference nil))))))
2109       (let ((y-mem (find-if #'member-type-p y-types)))
2110         (when y-mem
2111           (let ((members (member-type-members y-mem)))
2112             (dolist (x-type x-types)
2113               (unless (member-type-p x-type)
2114                 (dolist (member members)
2115                   (multiple-value-bind (val win) (ctypep member x-type)
2116                     (when (or (not win) val)
2117                       (return-from type-difference nil)))))))))
2118       (apply #'type-union (res)))))
2119 \f
2120 (!def-type-translator array (&optional (element-type '*)
2121                                        (dimensions '*))
2122   (specialize-array-type
2123    (make-array-type :dimensions (canonical-array-dimensions dimensions)
2124                     :element-type (specifier-type element-type))))
2125
2126 (!def-type-translator simple-array (&optional (element-type '*)
2127                                               (dimensions '*))
2128   (specialize-array-type
2129    (make-array-type :dimensions (canonical-array-dimensions dimensions)
2130                     :element-type (specifier-type element-type)
2131                     :complexp nil)))
2132 \f
2133 ;;;; utilities shared between cross-compiler and target system
2134
2135 ;;; Does the type derived from compilation of an actual function
2136 ;;; definition satisfy declarations of a function's type?
2137 (defun defined-ftype-matches-declared-ftype-p (defined-ftype declared-ftype)
2138   (declare (type ctype defined-ftype declared-ftype))
2139   (flet ((is-built-in-class-function-p (ctype)
2140            (and (built-in-class-p ctype)
2141                 (eq (built-in-class-%name ctype) 'function))))
2142     (cond (;; DECLARED-FTYPE could certainly be #<BUILT-IN-CLASS FUNCTION>;
2143            ;; that's what happens when we (DECLAIM (FTYPE FUNCTION FOO)).
2144            (is-built-in-class-function-p declared-ftype)
2145            ;; In that case, any definition satisfies the declaration.
2146            t)
2147           (;; It's not clear whether or how DEFINED-FTYPE might be
2148            ;; #<BUILT-IN-CLASS FUNCTION>, but it's not obviously
2149            ;; invalid, so let's handle that case too, just in case.
2150            (is-built-in-class-function-p defined-ftype)
2151            ;; No matter what DECLARED-FTYPE might be, we can't prove
2152            ;; that an object of type FUNCTION doesn't satisfy it, so
2153            ;; we return success no matter what.
2154            t)
2155           (;; Otherwise both of them must be FUNCTION-TYPE objects.
2156            t
2157            ;; FIXME: For now we only check compatibility of the return
2158            ;; type, not argument types, and we don't even check the
2159            ;; return type very precisely (as per bug 94a). It would be
2160            ;; good to do a better job. Perhaps to check the
2161            ;; compatibility of the arguments, we should (1) redo
2162            ;; VALUES-TYPES-EQUAL-OR-INTERSECT as
2163            ;; ARGS-TYPES-EQUAL-OR-INTERSECT, and then (2) apply it to
2164            ;; the ARGS-TYPE slices of the FUNCTION-TYPEs. (ARGS-TYPE
2165            ;; is a base class both of VALUES-TYPE and of FUNCTION-TYPE.)
2166            (values-types-equal-or-intersect
2167             (function-type-returns defined-ftype)
2168             (function-type-returns declared-ftype))))))
2169            
2170 ;;; This messy case of CTYPE for NUMBER is shared between the
2171 ;;; cross-compiler and the target system.
2172 (defun ctype-of-number (x)
2173   (let ((num (if (complexp x) (realpart x) x)))
2174     (multiple-value-bind (complexp low high)
2175         (if (complexp x)
2176             (let ((imag (imagpart x)))
2177               (values :complex (min num imag) (max num imag)))
2178             (values :real num num))
2179       (make-numeric-type :class (etypecase num
2180                                   (integer 'integer)
2181                                   (rational 'rational)
2182                                   (float 'float))
2183                          :format (and (floatp num) (float-format-name num))
2184                          :complexp complexp
2185                          :low low
2186                          :high high))))
2187 \f
2188 (!defun-from-collected-cold-init-forms !late-type-cold-init)
2189
2190 (/show0 "late-type.lisp end of file")