/[cmucl]/src/code/type.lisp
ViewVC logotype

Contents of /src/code/type.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.84 - (show annotations)
Tue Apr 20 17:57:45 2010 UTC (4 years ago) by rtoy
Branch: MAIN
CVS Tags: sparc-tramp-assem-base, release-20b-pre1, release-20b-pre2, sparc-tramp-assem-2010-07-19, GIT-CONVERSION, cross-sol-x86-merged, RELEASE_20b, cross-sol-x86-base, snapshot-2010-12, snapshot-2010-11, snapshot-2011-09, snapshot-2011-06, snapshot-2011-07, snapshot-2011-04, snapshot-2011-02, snapshot-2011-03, snapshot-2011-01, snapshot-2010-05, snapshot-2010-07, snapshot-2010-06, snapshot-2010-08, cross-sol-x86-2010-12-20, cross-sparc-branch-base, HEAD
Branch point for: cross-sparc-branch, RELEASE-20B-BRANCH, sparc-tramp-assem-branch, cross-sol-x86-branch
Changes since 1.83: +27 -27 lines
Change uses of _"foo" to (intl:gettext "foo").  This is because slime
may get confused with source locations if the reader macros are
installed.
1 ;;; -*- Mode: Lisp; Package: KERNEL; Log: C.Log -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the CMU Common Lisp project at
5 ;;; Carnegie Mellon University, and has been placed in the public domain.
6 ;;;
7 (ext:file-comment
8 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/type.lisp,v 1.84 2010/04/20 17:57:45 rtoy Rel $")
9 ;;;
10 ;;; **********************************************************************
11 ;;;
12 ;;; This file contains the definition of non-CLASS types (e.g. subtypes of
13 ;;; interesting BUILT-IN-CLASSes) and the interfaces to the type system.
14 ;;; Common Lisp type specifiers are parsed into a somewhat canonical internal
15 ;;; type representation that supports type union, intersection, etc.
16 ;;;
17 ;;; Written by Rob MacLachlan
18 ;;;
19 (in-package "KERNEL")
20 (use-package "ALIEN-INTERNALS")
21
22 (intl:textdomain "cmucl")
23
24 (export '(function-type-nargs code-component code-component-p lra lra-p))
25 (export '(make-alien-type-type alien-type-type
26 alien-type-type-p alien-type-type-alien-type
27 *unparse-function-type-simplify*))
28 (import 'c-call:void)
29 (export 'void)
30
31 (in-package "EXTENSIONS")
32 (export '(constant-argument instance *use-implementation-types*))
33
34 (in-package "KERNEL")
35
36 (export '(extract-function-type))
37
38 (with-cold-load-init-forms)
39
40 ;;; Structures & Type Classes
41
42 (define-type-class values)
43 (define-type-class function)
44 (define-type-class constant values)
45 (define-type-class named)
46 (define-type-class hairy)
47 (define-type-class negation)
48 (define-type-class number)
49 (define-type-class array)
50 (define-type-class member)
51 (define-type-class union)
52 (define-type-class intersection)
53 (define-type-class alien)
54 (define-type-class cons)
55
56 ;;; The Args-Type structure is used both to represent Values types and
57 ;;; and Function types.
58 ;;;
59 (defstruct (args-type (:include ctype)
60 (:print-function %print-type))
61 ;;
62 ;; Lists of the type for each required and optional argument.
63 (required nil :type list)
64 (optional nil :type list)
65 ;;
66 ;; The type for the rest arg. NIL if there is no rest arg.
67 (rest nil :type (or ctype null))
68 ;;
69 ;; True if keyword arguments are specified.
70 (keyp nil :type boolean)
71 ;;
72 ;; List of key-info structures describing the keyword arguments.
73 (keywords nil :type list)
74 ;;
75 ;; True if other keywords are allowed.
76 (allowp nil :type boolean))
77
78 (defstruct (key-info (:pure t))
79 ;;
80 ;; The keyword.
81 (name (required-argument) :type symbol)
82 ;;
83 ;; Type of this argument.
84 (type (required-argument) :type ctype))
85
86 (defstruct (values-type
87 (:include args-type
88 (:class-info (type-class-or-lose 'values)))
89 (:print-function %print-type)))
90
91 (declaim (freeze-type values-type))
92
93 (defstruct (function-type
94 (:include args-type
95 (:class-info (type-class-or-lose 'function)))
96 (:print-function %print-type))
97 ;;
98 ;; True if the arguments are unrestrictive, i.e. *.
99 (wild-args nil :type boolean)
100 ;;
101 ;; Type describing the return values. This is a values type
102 ;; when multiple values were specified for the return.
103 (returns (required-argument) :type ctype))
104
105 ;;; The CONSTANT-TYPE structure represents a use of the CONSTANT-ARGUMENT "type
106 ;;; specifier", which is only meaningful in function argument type specifiers
107 ;;; used within the compiler.
108 ;;;
109 (defstruct (constant-type (:include ctype
110 (:class-info (type-class-or-lose 'constant)))
111 (:print-function %print-type))
112 ;;
113 ;; The type which the argument must be a constant instance of for this type
114 ;; specifier to win.
115 (type (required-argument) :type ctype))
116
117 ;;; The NAMED-TYPE is used to represent *, T and NIL. These types must be
118 ;;; super or sub types of all types, not just classes and * & NIL aren't
119 ;;; classes anyway, so it wouldn't make much sense to make them built-in
120 ;;; classes.
121 ;;;
122 (defstruct (named-type (:include ctype
123 (:class-info (type-class-or-lose 'named)))
124 (:print-function %print-type))
125 (name nil :type symbol))
126
127 ;;; The Hairy-Type represents anything too wierd to be described reasonably or
128 ;;; to be useful, such as AND, NOT and SATISFIES and unknown types. We just
129 ;;; remember the original type spec.
130 ;;;
131 (defstruct (hairy-type (:include ctype
132 (:class-info (type-class-or-lose 'hairy))
133 (:enumerable t))
134 (:print-function %print-type)
135 (:pure nil))
136 ;;
137 ;; The Common Lisp type-specifier.
138 (specifier nil :type t))
139
140 (defstruct (negation-type (:include ctype
141 (:class-info (type-class-or-lose 'negation))
142 ;; FIXME: is this right? It's
143 ;; what they had before, anyway
144 (:enumerable t))
145 (:copier nil)
146 (:pure nil))
147 (type (required-argument) :type ctype))
148
149 ;;; An UNKNOWN-TYPE is a type not known to the type system (not yet defined).
150 ;;; We make this distinction since we don't want to complain about types that
151 ;;; are hairy but defined.
152 ;;;
153 (defstruct (unknown-type (:include hairy-type)))
154
155 ;;; The Numeric-Type is used to represent all numeric types, including things
156 ;;; such as FIXNUM.
157 (defstruct (numeric-type (:include ctype
158 (:class-info (type-class-or-lose 'number)))
159 (:constructor %make-numeric-type)
160 (:print-function %print-type))
161 ;;
162 ;; The kind of numeric type we have. NIL if not specified (just NUMBER or
163 ;; COMPLEX).
164 (class nil :type (member integer rational float nil))
165 ;;
166 ;; Format for a float type. NIL if not specified or not a float. Formats
167 ;; which don't exist in a given implementation don't appear here.
168 (format nil :type (or float-format null))
169 ;;
170 ;; Is this a complex numeric type? Null if unknown (only in NUMBER.)
171 (complexp :real :type (member :real :complex nil))
172 ;;
173 ;; The upper and lower bounds on the value. If null, there is no bound. If
174 ;; a list of a number, the bound is exclusive. Integer types never have
175 ;; exclusive bounds.
176 (low nil :type (or number cons null))
177 (high nil :type (or number cons null)))
178
179 (defun type-bound-number (x)
180 (if (consp x)
181 (destructuring-bind (result) x result)
182 x))
183
184 (defun make-numeric-type (&key class format (complexp :real) low high
185 enumerable)
186 ;; if interval is empty
187 (if (and low
188 high
189 (if (or (consp low) (consp high)) ; if either bound is exclusive
190 (>= (type-bound-number low) (type-bound-number high))
191 (> low high)))
192 *empty-type*
193 (multiple-value-bind (canonical-low canonical-high)
194 (case class
195 (integer
196 ;; INTEGER types always have their LOW and HIGH bounds
197 ;; represented as inclusive, not exclusive values.
198 (values (if (consp low)
199 (1+ (type-bound-number low))
200 low)
201 (if (consp high)
202 (1- (type-bound-number high))
203 high)))
204 (t
205 ;; no canonicalization necessary
206 (values low high)))
207 (when (and (eq class 'rational)
208 (integerp canonical-low)
209 (integerp canonical-high)
210 (= canonical-low canonical-high))
211 (setf class 'integer))
212 (%make-numeric-type :class class
213 :format format
214 :complexp complexp
215 :low canonical-low
216 :high canonical-high
217 :enumerable enumerable))))
218
219 (defun modified-numeric-type (base
220 &key
221 (class (numeric-type-class base))
222 (format (numeric-type-format base))
223 (complexp (numeric-type-complexp base))
224 (low (numeric-type-low base))
225 (high (numeric-type-high base))
226 (enumerable (numeric-type-enumerable base)))
227 (make-numeric-type :class class
228 :format format
229 :complexp complexp
230 :low low
231 :high high
232 :enumerable enumerable))
233
234 ;;; The Array-Type is used to represent all array types, including things such
235 ;;; as SIMPLE-STRING.
236 ;;;
237 (defstruct (array-type (:include ctype
238 (:class-info (type-class-or-lose 'array)))
239 (:print-function %print-type))
240 ;;
241 ;; The dimensions of the array. * if unspecified. If a dimension is
242 ;; unspecified, it is *.
243 (dimensions '* :type (or list (member *)))
244 ;;
245 ;; Is this not a simple array type?
246 (complexp :maybe :type (member t nil :maybe))
247 ;;
248 ;; The element type as originally specified.
249 (element-type (required-argument) :type ctype)
250 ;;
251 ;; The element type as it is specialized in this implementation.
252 (specialized-element-type *wild-type* :type ctype))
253
254 ;;; The Member-Type represents uses of the MEMBER type specifier. We bother
255 ;;; with this at this level because MEMBER types are fairly important and union
256 ;;; and intersection are well defined.
257
258 (defstruct (member-type (:include ctype
259 (:class-info (type-class-or-lose 'member))
260 (:enumerable t))
261 (:constructor %make-member-type (members))
262 (:print-function %print-type)
263 (:pure nil))
264 ;;
265 ;; The things in the set, with no duplications.
266 (members nil :type list))
267
268 (defun make-member-type (&key members)
269 (declare (type list members))
270 ;; make sure that we've removed duplicates
271 (assert (= (length members) (length (remove-duplicates members))))
272 ;; if we have a pair of zeros (e.g. 0.0d0 and -0.0d0), then we can
273 ;; canonicalize to (DOUBLE-FLOAT 0.0d0 0.0d0), because numeric
274 ;; ranges are compared by arithmetic operators (while MEMBERship is
275 ;; compared by EQL). -- CSR, 2003-04-23
276 (let ((singlep (subsetp '(-0.0f0 0.0f0) members))
277 (doublep (subsetp '(-0.0d0 0.0d0) members))
278 #+long-float
279 (longp (subsetp '(-0.0l0 0.0l0) members)))
280 (if (or singlep doublep #+long-float longp)
281 (let (union-types)
282 (when singlep
283 (push (ctype-of 0.0f0) union-types)
284 (setf members (set-difference members '(-0.0f0 0.0f0))))
285 (when doublep
286 (push (ctype-of 0.0d0) union-types)
287 (setf members (set-difference members '(-0.0d0 0.0d0))))
288 #+long-float
289 (when longp
290 (push (ctype-of 0.0l0) union-types)
291 (setf members (set-difference members '(-0.0l0 0.0l0))))
292 (assert (not (null union-types)))
293 (make-union-type (if (null members)
294 union-types
295 (cons (%make-member-type members)
296 union-types))))
297 (%make-member-type members))))
298
299 ;;; The Union-Type represents uses of the OR type specifier which can't be
300 ;;; canonicalized to something simpler. Canonical form:
301 ;;;
302 ;;; 1] There is never more than one Member-Type component.
303 ;;; 2] There are never any Union-Type components.
304 ;;;
305 (defstruct (union-type (:include ctype
306 (:class-info (type-class-or-lose 'union)))
307 (:constructor %make-union-type (enumerable types))
308 (:print-function %print-type))
309 ;;
310 ;; The types in the union.
311 (types nil :type list :read-only t))
312
313 (defun make-union-type (types)
314 (declare (list types))
315 (%make-union-type (every #'type-enumerable types) types))
316
317 (defstruct (intersection-type
318 (:include ctype
319 (:class-info (type-class-or-lose 'intersection)))
320 (:constructor make-intersection-type (enumerable types))
321 (:print-function %print-type))
322 (types nil :type list :read-only t))
323
324 (defstruct (alien-type-type
325 (:include ctype
326 (:class-info (type-class-or-lose 'alien)))
327 (:print-function %print-type)
328 (:constructor %make-alien-type-type (alien-type)))
329 (alien-type nil :type alien-type))
330
331 ;;; The Cons-Type is used to represent cons types.
332 ;;;
333 (defun type-*-to-t (type)
334 (if (type= type *wild-type*)
335 *universal-type*
336 type))
337
338 (defstruct (cons-type (:include ctype
339 (:class-info (type-class-or-lose 'cons)))
340 (:constructor
341 ;; ANSI says that for CAR and CDR subtype
342 ;; specifiers '* is equivalent to T. In order
343 ;; to avoid special cases in SUBTYPEP and
344 ;; possibly elsewhere, we slam all CONS-TYPE
345 ;; objects into canonical form w.r.t. this
346 ;; equivalence at creation time.
347 %make-cons-type (car-raw-type
348 cdr-raw-type
349 &aux
350 (car-type (type-*-to-t car-raw-type))
351 (cdr-type (type-*-to-t cdr-raw-type))))
352 (:print-function %print-type)
353 (:copier nil))
354 ;; the CAR and CDR element types (to support ANSI (CONS FOO BAR) types)
355 ;;
356 ;; FIXME: Most or all other type structure slots could also be :READ-ONLY.
357 (car-type (required-argument) :type ctype :read-only t)
358 (cdr-type (required-argument) :type ctype :read-only t))
359
360 (defun make-cons-type (car-type cdr-type)
361 (if (or (eq car-type *empty-type*)
362 (eq cdr-type *empty-type*))
363 *empty-type*
364 (%make-cons-type car-type cdr-type)))
365
366
367
368 ;;;
369 (defvar *use-implementation-types* t
370 "*Use-Implementation-Types* is a semi-public flag which determines how
371 restrictive we are in determining type membership. If two types are the
372 same in the implementation, then we will consider them them the same when
373 this switch is on. When it is off, we try to be as restrictive as the
374 language allows, allowing us to detect more errors. Currently, this only
375 affects array types.")
376
377 (cold-load-init (setq *use-implementation-types* t))
378 (declaim (type boolean *use-implementation-types*))
379
380 ;;; DELEGATE-COMPLEX-{SUBTYPEP-ARG2,INTERSECTION} -- Interface
381 ;;;
382 ;;; These functions are used as method for types which need a complex
383 ;;; subtypep method to handle some superclasses, but cover a subtree of the
384 ;;; type graph (i.e. there is no simple way for any other type class to be a
385 ;;; subtype.) There are always still complex ways, namely UNION and MEMBER
386 ;;; types, so we must give TYPE1's method a chance to run, instead of
387 ;;; immediately returning NIL, T.
388 ;;;
389 (defun delegate-complex-subtypep-arg2 (type1 type2)
390 (let ((subtypep-arg1
391 (type-class-complex-subtypep-arg1
392 (type-class-info type1))))
393 (if subtypep-arg1
394 (funcall subtypep-arg1 type1 type2)
395 (values nil t))))
396
397 (defun delegate-complex-intersection (type1 type2)
398 (let ((method (type-class-complex-intersection (type-class-info type1))))
399 (if (and method (not (eq method #'delegate-complex-intersection)))
400 (funcall method type2 type1)
401 (hierarchical-intersection2 type1 type2))))
402
403 ;;; HAS-SUPERCLASSES-COMPLEX-SUBTYPEP-ARG1 -- Internal
404 ;;;
405 ;;; Used by DEFINE-SUPERCLASSES to define the SUBTYPE-ARG1 method. Info is
406 ;;; a list of conses (SUPERCLASS-CLASS . {GUARD-TYPE-SPECIFIER | NIL}). Will
407 ;;; never be called with a hairy type as type2, since the hairy type type2
408 ;;; method gets first crack.
409 ;;;
410 (defun has-superclasses-complex-subtypep-arg1 (type1 type2 info)
411 ;; If TYPE2 might be concealing something related to our class
412 ;; hierarchy
413 (if (type-might-contain-other-types-p type2)
414 ;; too confusing, gotta punt
415 (values nil nil)
416 ;; ordinary case expected by old CMU CL code, where the taxonomy
417 ;; of TYPE2's representation accurately reflects the taxonomy of
418 ;; the underlying set
419 (values
420 (and (typep type2 'kernel::class)
421 (dolist (x info nil)
422 (when (or (not (cdr x))
423 (csubtypep type1 (specifier-type (cdr x))))
424 (return
425 (or (eq type2 (car x))
426 (let ((inherits (layout-inherits
427 (%class-layout (car x)))))
428 (dotimes (i (length inherits) nil)
429 (when (eq type2 (layout-class (svref inherits i)))
430 (return t)))))))))
431 t)))
432
433 (eval-when (compile eval)
434 ;;; DEFINE-SUPERCLASSES -- Interface
435 ;;;
436 ;;; Takes a list of specs of the form (superclass &optional guard).
437 ;;; Consider one spec (with no guard): any instance of type-class is also a
438 ;;; subtype of SUPERCLASS and of any of its superclasses. If there are
439 ;;; multiple specs, then some will have guards. We choose the first spec whose
440 ;;; guard is a supertype of TYPE1 and use its superclass. In effect, a
441 ;;; sequence of guards G0, G1, G2 is actually G0, (and G1 (not G0)),
442 ;;; (and G2 (not (or G0 G1))).
443 ;;;
444 (defmacro define-superclasses (type-class &rest specs)
445 (let ((info
446 (mapcar #'(lambda (spec)
447 (destructuring-bind (super &optional guard)
448 spec
449 (cons (kernel::find-class super) guard)))
450 specs)))
451 `(cold-load-init
452 (setf (type-class-complex-subtypep-arg1
453 (type-class-or-lose ',type-class))
454 #'(lambda (type1 type2)
455 (has-superclasses-complex-subtypep-arg1 type1 type2 ',info)))
456
457 (setf (type-class-complex-subtypep-arg2
458 (type-class-or-lose ',type-class))
459 #'delegate-complex-subtypep-arg2)
460
461 (setf (type-class-complex-intersection
462 (type-class-or-lose ',type-class))
463 #'delegate-complex-intersection))))
464
465 ); eval-when (compile eval)
466
467 (declaim (inline reparse-unknown-type))
468 (defun reparse-unknown-type (type)
469 (if (unknown-type-p type)
470 (specifier-type (type-specifier type))
471 type))
472
473 (declaim (inline swapped-args-fun))
474 (defun swapped-args-fun (fun)
475 (declare (type function fun))
476 (lambda (x y)
477 (funcall fun y x)))
478
479 (defun equal-but-no-car-recursion (x y)
480 (cond
481 ((eql x y) t)
482 ((consp x)
483 (and (consp y)
484 (eql (car x) (car y))
485 (equal-but-no-car-recursion (cdr x) (cdr y))))
486 (t nil)))
487
488 (defun any/type (op thing list)
489 (declare (type function op))
490 (let ((certain? t))
491 (dolist (i list (values nil certain?))
492 (multiple-value-bind (sub-value sub-certain?) (funcall op thing i)
493 (if sub-certain?
494 (when sub-value (return (values t t)))
495 (setf certain? nil))))))
496
497 (defun every/type (op thing list)
498 (declare (type function op))
499 (let ((certain? t))
500 (dolist (i list (if certain? (values t t) (values nil nil)))
501 (multiple-value-bind (sub-value sub-certain?) (funcall op thing i)
502 (if sub-certain?
503 (unless sub-value (return (values nil t)))
504 (setf certain? nil))))))
505
506 (defun invoke-complex-=-other-method (type1 type2)
507 (let* ((type-class (type-class-info type1))
508 (method-fun (type-class-complex-= type-class)))
509 (if method-fun
510 (funcall (the function method-fun) type2 type1)
511 (values nil t))))
512
513 (defun invoke-complex-subtypep-arg1-method (type1 type2 &optional subtypep win)
514 (let* ((type-class (type-class-info type1))
515 (method-fun (type-class-complex-subtypep-arg1 type-class)))
516 (if method-fun
517 (funcall (the function method-fun) type1 type2)
518 (values subtypep win))))
519
520 (declaim (inline type-might-contain-other-types-p))
521 (defun type-might-contain-other-types-p (type)
522 (or (hairy-type-p type)
523 (negation-type-p type)
524 (union-type-p type)
525 (intersection-type-p type)))
526
527
528 ;;;; Function and Values types.
529 ;;;
530 ;;; Pretty much all of the general type operations are illegal on VALUES
531 ;;; types, since we can't discriminate using them, do SUBTYPEP, etc. FUNCTION
532 ;;; types are acceptable to the normal type operations, but are generally
533 ;;; considered to be equivalent to FUNCTION. These really aren't true types in
534 ;;; any type theoretic sense, but we still parse them into CTYPE structures for
535 ;;; two reasons:
536 ;;; -- Parsing and unparsing work the same way, and indeed we can't tell
537 ;;; whether a type is a function or values type without parsing it.
538 ;;; -- Many of the places that can be annotated with real types can also be
539 ;;; annotated function or values types.
540
541
542 (define-type-method (values :simple-subtypep :complex-subtypep-arg1)
543 (type1 type2)
544 (declare (ignore type2))
545 (error (intl:gettext "Subtypep is illegal on this type:~% ~S") (type-specifier type1)))
546
547 (define-type-method (values :complex-subtypep-arg2)
548 (type1 type2)
549 (declare (ignore type1))
550 (error (intl:gettext "Subtypep is illegal on this type:~% ~S") (type-specifier type2)))
551
552 (define-type-method (values :unparse) (type)
553 (cons 'values (unparse-args-types type)))
554
555 ;;; TYPE=-LIST -- Internal
556 ;;;
557 ;;; Return true if List1 and List2 have the same elements in the same
558 ;;; positions according to TYPE=. We return NIL, NIL if there is an uncertain
559 ;;; comparison.
560 ;;;
561 (defun type=-list (list1 list2)
562 (declare (list list1 list2))
563 (do ((types1 list1 (cdr types1))
564 (types2 list2 (cdr types2)))
565 ((or (null types1) (null types2))
566 (if (or types1 types2)
567 (values nil t)
568 (values t t)))
569 (multiple-value-bind (val win)
570 (type= (first types1) (first types2))
571 (unless win
572 (return (values nil nil)))
573 (unless val
574 (return (values nil t))))))
575
576 (define-type-method (values :simple-=) (type1 type2)
577 (let ((rest1 (args-type-rest type1))
578 (rest2 (args-type-rest type2)))
579 (cond ((or (args-type-keyp type1) (args-type-keyp type2)
580 (args-type-allowp type1) (args-type-allowp type2))
581 (values nil nil))
582 ((and rest1 rest2 (type/= rest1 rest2))
583 (type= rest1 rest2))
584 ((or rest1 rest2)
585 (values nil t))
586 (t
587 (multiple-value-bind (req-val req-win)
588 (type=-list (values-type-required type1)
589 (values-type-required type2))
590 (multiple-value-bind (opt-val opt-win)
591 (type=-list (values-type-optional type1)
592 (values-type-optional type2))
593 (values (and req-val opt-val) (and req-win opt-win))))))))
594
595 ;;; A flag that we can bind to cause complex function types to be unparsed as
596 ;;; FUNCTION. Useful when we want a type that we can pass to TYPEP.
597 ;;;
598 (defvar *unparse-function-type-simplify*)
599 (cold-load-init (setq *unparse-function-type-simplify* nil))
600
601 (define-type-method (function :unparse) (type)
602 (if *unparse-function-type-simplify*
603 'function
604 (list 'function
605 (if (function-type-wild-args type)
606 '*
607 (unparse-args-types type))
608 (type-specifier
609 (function-type-returns type)))))
610
611 ;;; Since all function types are equivalent to FUNCTION, they are all subtypes
612 ;;; of each other.
613 ;;;
614 (define-type-method (function :simple-subtypep) (type1 type2)
615 (flet ((fun-type-simple-p (type)
616 (not (or (function-type-rest type)
617 (function-type-keyp type))))
618 (every-csubtypep (types1 types2)
619 (loop
620 for a1 in types1
621 for a2 in types2
622 do (multiple-value-bind (res sure-p)
623 (csubtypep a1 a2)
624 (unless res (return (values res sure-p))))
625 finally (return (values t t)))))
626 (macrolet ((3and (x y)
627 `(multiple-value-bind (val1 win1) ,x
628 (if (and (not val1) win1)
629 (values nil t)
630 (multiple-value-bind (val2 win2) ,y
631 (if (and val1 val2)
632 (values t t)
633 (values nil (and win2 (not val2)))))))))
634 (3and (values-subtypep (function-type-returns type1)
635 (function-type-returns type2))
636 (cond ((function-type-wild-args type2) (values t t))
637 ((function-type-wild-args type1)
638 (cond ((function-type-keyp type2) (values nil nil))
639 ((not (function-type-rest type2)) (values nil t))
640 ((not (null (function-type-required type2))) (values nil t))
641 (t (3and (type= *universal-type* (function-type-rest type2))
642 (every/type #'type= *universal-type*
643 (function-type-optional type2))))))
644 ((not (and (fun-type-simple-p type1)
645 (fun-type-simple-p type2)))
646 (values nil nil))
647 (t (multiple-value-bind (min1 max1) (function-type-nargs type1)
648 (multiple-value-bind (min2 max2) (function-type-nargs type2)
649 (cond ((or (> max1 max2) (< min1 min2))
650 (values nil t))
651 ((and (= min1 min2) (= max1 max2))
652 (3and (every-csubtypep (function-type-required type1)
653 (function-type-required type2))
654 (every-csubtypep (function-type-optional type1)
655 (function-type-optional type2))))
656 (t (every-csubtypep
657 (concatenate 'list
658 (function-type-required type1)
659 (function-type-optional type1))
660 (concatenate 'list
661 (function-type-required type2)
662 (function-type-optional type2)))))))))))))
663
664 (define-superclasses function (function))
665
666 ;;; The union or intersection of two FUNCTION types is FUNCTION.
667 ;;;
668 (define-type-method (function :simple-union) (type1 type2)
669 (if (type= type1 type2)
670 type1
671 (specifier-type 'function)))
672
673 (define-type-method (function :simple-intersection) (type1 type2)
674 (if (type= type1 type2)
675 type1
676 (values (specifier-type 'function) t)))
677
678
679 ;;; ### Not very real, but good enough for redefining transforms according to
680 ;;; type:
681 ;;;
682 (define-type-method (function :simple-=) (type1 type2)
683 (values (equalp type1 type2) t))
684
685
686 (define-type-method (constant :unparse) (type)
687 `(constant-argument ,(type-specifier (constant-type-type type))))
688
689 (define-type-method (constant :simple-=) (type1 type2)
690 (type= (constant-type-type type1) (constant-type-type type2)))
691
692 (def-type-translator constant-argument (type)
693 (make-constant-type :type (specifier-type type)))
694
695
696 ;;; Parse-Args-Types -- Internal
697 ;;;
698 ;;; Given a lambda-list like values type specification and a Args-Type
699 ;;; structure, fill in the slots in the structure accordingly. This is used
700 ;;; for both FUNCTION and VALUES types.
701 ;;;
702 (defun parse-args-types (lambda-list result)
703 (declare (list lambda-list) (type args-type result))
704 (multiple-value-bind (required optional restp rest keyp keys allowp aux)
705 (parse-lambda-list lambda-list)
706 (when aux
707 (simple-program-error (intl:gettext "&Aux in a FUNCTION or VALUES type: ~S.")
708 lambda-list))
709 (setf (args-type-required result)
710 (mapcar #'single-value-specifier-type required))
711 (setf (args-type-optional result)
712 (mapcar #'single-value-specifier-type optional))
713 (setf (args-type-rest result)
714 (if restp (single-value-specifier-type rest) nil))
715 (setf (args-type-keyp result) keyp)
716 (collect ((key-info))
717 (dolist (key keys)
718 (when (or (atom key) (/= (length key) 2))
719 (simple-program-error
720 (intl:gettext "Keyword type description is not a two-list: ~S.") key))
721 (let ((kwd (first key)))
722 (when (find kwd (key-info) :key #'key-info-name)
723 (simple-program-error (intl:gettext "Repeated keyword ~S in lambda list: ~S.")
724 kwd lambda-list))
725 (key-info (make-key-info
726 :name kwd
727 :type (single-value-specifier-type (second key))))))
728 (setf (args-type-keywords result) (key-info)))
729 (setf (args-type-allowp result) allowp)))
730
731 ;;; Unparse-Args-Types -- Internal
732 ;;;
733 ;;; Return the lambda-list like type specification corresponding
734 ;;; to a Args-Type.
735 ;;;
736 (defun unparse-args-types (type)
737 (declare (type args-type type) (values list))
738 (collect ((result))
739
740 (dolist (arg (args-type-required type))
741 (result (type-specifier arg)))
742
743 (when (args-type-optional type)
744 (result '&optional)
745 (dolist (arg (args-type-optional type))
746 (result (type-specifier arg))))
747
748 (when (args-type-rest type)
749 (result '&rest)
750 (result (type-specifier (args-type-rest type))))
751
752 (when (args-type-keyp type)
753 (result '&key)
754 (dolist (key (args-type-keywords type))
755 (result (list (key-info-name key)
756 (type-specifier (key-info-type key))))))
757
758 (when (args-type-allowp type)
759 (result '&allow-other-keys))
760
761 (result)))
762
763 (def-type-translator function (&optional (args '*) (result '*))
764 (let ((res (make-function-type :returns (values-specifier-type result))))
765 (if (eq args '*)
766 (setf (function-type-wild-args res) t)
767 (parse-args-types args res))
768 res))
769
770 (def-type-translator values (&rest values)
771 (let ((res (make-values-type)))
772 (parse-args-types values res)
773 ;;
774 ;; Signal an error if the spec has &KEY or &ALLOW-OTHER-KEYS.
775 ;; Actually, CLHS lists &ALLOW-OTHER-KEYS without listing &KEYS,
776 ;; but keys clearly don't make any sense.
777 (when (or (values-type-keyp res) (values-type-allowp res))
778 (simple-program-error (intl:gettext "&KEY or &ALLOW-OTHER-KEYS in values type: ~s")
779 res))
780 res))
781
782
783 ;;;; Values types interfaces:
784 ;;;
785 ;;; We provide a few special operations that can be meaningfully used on
786 ;;; values types (as well as on any other type.)
787 ;;;
788
789 ;;; Single-Value-Type -- Interface
790 ;;;
791 ;;; Return the type of the first value indicated by Type. This is used by
792 ;;; people who don't want to have to deal with values types. If the first
793 ;;; values is an optional or rest argument then return the union with the null
794 ;;; type. If the first values is a keyword then give up and return the
795 ;;; universal type.
796 ;;;
797 (defun single-value-type (type)
798 (declare (type ctype type))
799 (cond ((values-type-p type)
800 (or (car (args-type-required type))
801 (if (args-type-optional type)
802 (type-union (car (args-type-optional type))
803 (specifier-type 'null)))
804 (args-type-rest type)
805 (specifier-type 'null)))
806 ((eq type *wild-type*)
807 *universal-type*)
808 (t
809 type)))
810
811 ;;; FUNCTION-TYPE-NARGS -- Interface
812 ;;;
813 ;;; Return the minmum number of arguments that a function can be called
814 ;;; with, and the maximum number or NIL. If not a function type, return
815 ;;; NIL, NIL.
816 ;;;
817 (defun function-type-nargs (type)
818 (declare (type ctype type))
819 (if (function-type-p type)
820 (let ((fixed (length (args-type-required type))))
821 (if (or (args-type-rest type)
822 (args-type-keyp type)
823 (args-type-allowp type))
824 (values fixed nil)
825 (values fixed (+ fixed (length (args-type-optional type))))))
826 (values nil nil)))
827
828
829 ;;; Values-Types -- Interface
830 ;;;
831 ;;; Determine if Type corresponds to a definite number of values. The first
832 ;;; value is a list of the types for each value, and the second value is the
833 ;;; number of values. If the number of values is not fixed, then return NIL
834 ;;; and :Unknown.
835 ;;;
836 (defun values-types (type)
837 (declare (type ctype type))
838 (cond ((eq type *wild-type*)
839 (values nil :unknown))
840 ((not (values-type-p type))
841 (values (list type) 1))
842 ((or (args-type-optional type)
843 (args-type-rest type)
844 (args-type-keyp type)
845 (args-type-allowp type))
846 (values nil :unknown))
847 (t
848 (let ((req (args-type-required type)))
849 (values (mapcar #'single-value-type req) (length req))))))
850
851
852 ;;; Values-Type-Types -- Internal
853 ;;;
854 ;;; Return two values:
855 ;;; 1] A list of all the positional (fixed and optional) types.
856 ;;; 2] The rest type (if any). If keywords allowed, *universal-type*. If no
857 ;;; keywords or rest, *empty-type*.
858 ;;;
859 (defun values-type-types (type &optional (default-type *empty-type*))
860 (declare (type values-type type))
861 (values (append (args-type-required type)
862 (args-type-optional type))
863 (cond ((args-type-keyp type) *universal-type*)
864 ((args-type-rest type))
865 (t
866 default-type))))
867
868 ;;; Fixed-Values-Op -- Internal
869 ;;;
870 ;;; Return a list of Operation applied to the types in Types1 and Types2,
871 ;;; padding with Rest2 as needed. Types1 must not be shorter than Types2. The
872 ;;; second value is T if Operation always returned a true second value.
873 ;;;
874 (defun fixed-values-op (types1 types2 rest2 operation)
875 (declare (list types1 types2) (type ctype rest2) (type function operation))
876 (let ((exact t))
877 (values (mapcar (lambda (t1 t2)
878 (multiple-value-bind (res win)
879 (funcall operation t1 t2)
880 (unless win
881 (setq exact nil))
882 res))
883 types1
884 (append types2
885 (make-list (- (length types1) (length types2))
886 :initial-element rest2)))
887 exact)))
888
889
890 ;;; Coerce-To-Values -- Internal
891 ;;;
892 ;;; If Type isn't a values type, then make it into one:
893 ;;; <type> ==> (values type)
894 ;;;
895 (defun coerce-to-values (type)
896 (declare (type ctype type))
897 (if (values-type-p type)
898 type
899 (make-values-type :required (list type))))
900
901 ;;; Args-Type-Op -- Internal
902 ;;;
903 ;;; Do the specified Operation on Type1 and Type2, which may be any type,
904 ;;; including Values types. With values types such as:
905 ;;; (values a0 a1)
906 ;;; (values b0 b1)
907 ;;;
908 ;;; We compute the more useful result:
909 ;;; (values (<operation> a0 b0) (<operation> a1 b1))
910 ;;;
911 ;;; Rather than the precise result:
912 ;;; (<operation> (values a0 a1) (values b0 b1))
913 ;;;
914 ;;; This has the virtue of always keeping the values type specifier outermost,
915 ;;; and retains all of the information that is really useful for static type
916 ;;; analysis. We want to know what is always true of each value independently.
917 ;;; It is worthless to know that IF the first value is B0 then the second will
918 ;;; be B1.
919 ;;;
920 ;;; If the values count signatures differ, then we produce result with the
921 ;;; required value count chosen by Nreq when applied to the number of required
922 ;;; values in type1 and type2. Any &key values become &rest T (anyone who uses
923 ;;; keyword values deserves to lose.)
924 ;;;
925 ;;; The second value is true if the result is definitely empty or if Operation
926 ;;; returned true as its second value each time we called it. Since we
927 ;;; approximate the intersection of values types, the second value being true
928 ;;; doesn't mean the result is exact.
929 ;;;
930 (defun args-type-op (type1 type2 operation nreq default-type)
931 (declare (type ctype type1 type2 default-type)
932 (type function operation nreq))
933 (when (eq type1 type2)
934 (values type1 t))
935 (if (or (values-type-p type1) (values-type-p type2))
936 (let ((type1 (coerce-to-values type1))
937 (type2 (coerce-to-values type2)))
938 (multiple-value-bind (types1 rest1)
939 (values-type-types type1 default-type)
940 (multiple-value-bind (types2 rest2)
941 (values-type-types type2 default-type)
942 (multiple-value-bind (rest rest-exact)
943 (funcall operation rest1 rest2)
944 (multiple-value-bind (res res-exact)
945 (if (< (length types1) (length types2))
946 (fixed-values-op types2 types1 rest1 operation)
947 (fixed-values-op types1 types2 rest2 operation))
948 (let* ((req (funcall nreq
949 (length (args-type-required type1))
950 (length (args-type-required type2))))
951 (required (subseq res 0 req))
952 (opt (subseq res req))
953 (opt-last (position rest opt :test-not #'type=
954 :from-end t)))
955 (if (find *empty-type* required :test #'type=)
956 (values *empty-type* t)
957 (values (make-values-type
958 :required required
959 :optional (if opt-last
960 (subseq opt 0 (1+ opt-last))
961 ())
962 :rest (if (eq rest default-type) nil rest))
963 (and rest-exact res-exact)))))))))
964 (funcall operation type1 type2)))
965
966 ;;; Values-Type-Union, Values-Type-Intersection -- Interface
967 ;;;
968 ;;; Do a union or intersection operation on types that might be values
969 ;;; types. The result is optimized for utility rather than exactness, but it
970 ;;; is guaranteed that it will be no smaller (more restrictive) than the
971 ;;; precise result.
972 ;;;
973 (defun-cached (values-type-union :hash-function type-cache-hash
974 :hash-bits 8
975 :default nil
976 :init-form cold-load-init)
977 ((type1 eq) (type2 eq))
978 (declare (type ctype type1 type2))
979 (cond ((or (eq type1 *wild-type*) (eq type2 *wild-type*)) *wild-type*)
980 ((eq type1 *empty-type*) type2)
981 ((eq type2 *empty-type*) type1)
982 (t
983 (values (args-type-op type1 type2 #'type-union #'min *empty-type*)))))
984 ;;;
985 (defun-cached (values-type-intersection :hash-function type-cache-hash
986 :hash-bits 8
987 :values 2
988 :default (values nil :empty)
989 :init-form cold-load-init)
990 ((type1 eq) (type2 eq))
991 (declare (type ctype type1 type2))
992 (cond ((eq type1 *wild-type*) (values type2 t))
993 ((eq type2 *wild-type*) (values type1 t))
994 (t
995 (args-type-op type1 type2
996 #'type-intersection
997 #'max
998 (specifier-type 'null)))))
999
1000
1001 ;;; Values-Types-Intersect -- Interface
1002 ;;;
1003 ;;; Like Types-Intersect, except that it sort of works on values types.
1004 ;;; Note that due to the semantics of Values-Type-Intersection, this might
1005 ;;; return {T, T} when there isn't really any intersection (?).
1006 ;;;
1007 (defun values-types-intersect (type1 type2)
1008 (cond ((or (eq type1 *empty-type*) (eq type2 *empty-type*))
1009 (values t t))
1010 ((or (values-type-p type1) (values-type-p type2))
1011 (multiple-value-bind (res win) (values-type-intersection type1 type2)
1012 (values (not (eq res *empty-type*))
1013 win)))
1014 (t
1015 (types-intersect type1 type2))))
1016
1017
1018 ;;; Values-Subtypep -- Interface
1019 ;;;
1020 ;;; A subtypep-like operation that can be used on any types, including
1021 ;;; values types.
1022 ;;;
1023 (defun-cached (values-subtypep :hash-function type-cache-hash
1024 :hash-bits 8
1025 :values 2
1026 :default (values nil :empty)
1027 :init-form cold-load-init)
1028 ((type1 eq) (type2 eq))
1029 (declare (type ctype type1 type2))
1030 (cond ((eq type2 *wild-type*) (values t t))
1031 ((eq type1 *wild-type*)
1032 (values (eq type2 *universal-type*) t))
1033 ((not (values-types-intersect type1 type2))
1034 (values nil t))
1035 (t
1036 (if (or (values-type-p type1) (values-type-p type2))
1037 (let ((type1 (coerce-to-values type1))
1038 (type2 (coerce-to-values type2)))
1039 (multiple-value-bind (types1 rest1) (values-type-types type1)
1040 (multiple-value-bind (types2 rest2) (values-type-types type2)
1041 (cond ((< (length (values-type-required type1))
1042 (length (values-type-required type2)))
1043 (values nil t))
1044 ((< (length types1) (length types2))
1045 (values nil nil))
1046 ((or (values-type-keyp type1)
1047 (values-type-keyp type2))
1048 (values nil nil))
1049 (t
1050 (do ((t1 types1 (rest t1))
1051 (t2 types2 (rest t2)))
1052 ((null t2)
1053 (csubtypep rest1 rest2))
1054 (multiple-value-bind (res win-p)
1055 (csubtypep (first t1) (first t2))
1056 (unless win-p
1057 (return (values nil nil)))
1058 (unless res
1059 (return (values nil t))))))))))
1060 (csubtypep type1 type2)))))
1061
1062
1063 ;;;; Type method interfaces:
1064
1065 ;;; Csubtypep -- Interface
1066 ;;;
1067 ;;; Like subtypep, only works on Type structures.
1068 ;;;
1069 (defun-cached (csubtypep :hash-function type-cache-hash
1070 :hash-bits 8
1071 :values 2
1072 :default (values nil :empty)
1073 :init-form cold-load-init)
1074 ((type1 eq) (type2 eq))
1075 (declare (type ctype type1 type2))
1076 (cond ((or (eq type1 type2)
1077 (eq type1 *empty-type*)
1078 (eq type2 *wild-type*))
1079 (values t t))
1080 ((eq type1 *wild-type*)
1081 (values nil t))
1082 (t
1083 (invoke-type-method :simple-subtypep :complex-subtypep-arg2
1084 type1 type2
1085 :complex-arg1 :complex-subtypep-arg1))))
1086
1087 (declaim (start-block))
1088
1089 ;;; Type= -- Interface
1090 ;;;
1091 ;;; If two types are definitely equivalent, return true. The second value
1092 ;;; indicates whether the first value is definitely correct. This should only
1093 ;;; fail in the presence of Hairy types.
1094 ;;;
1095 (defun-cached (type= :hash-function type-cache-hash
1096 :hash-bits 8
1097 :values 2
1098 :default (values nil :empty)
1099 :init-form cold-load-init)
1100 ((type1 eq) (type2 eq))
1101 (declare (type ctype type1 type2))
1102 (if (eq type1 type2)
1103 (values t t)
1104 (invoke-type-method :simple-= :complex-= type1 type2)))
1105
1106
1107 ;;; TYPE/= -- Interface
1108 ;;;
1109 ;;; Not exactly the negation of TYPE=, since when the relationship is
1110 ;;; uncertain, we still return NIL, NIL. This is useful in cases where the
1111 ;;; conservative assumption is =.
1112 ;;;
1113 (defun type/= (type1 type2)
1114 (declare (type ctype type1 type2))
1115 (multiple-value-bind (res win) (type= type1 type2)
1116 (if win
1117 (values (not res) t)
1118 (values nil nil))))
1119
1120 (declaim (end-block))
1121
1122 ;;; Type-Union -- Interface
1123 ;;;
1124 ;;; Find a type which includes both types. Any inexactness is represented
1125 ;;; by the fuzzy element types; we return a single value that is precise to the
1126 ;;; best of our knowledge. This result is simplified into the canonical form,
1127 ;;; thus is not a UNION type unless there is no other way to represent the
1128 ;;; result.
1129 ;;;
1130 (defun type-union (&rest input-types)
1131 (%type-union input-types))
1132
1133 (defun-cached (%type-union :hash-bits 8
1134 :hash-function (lambda (x)
1135 (logand (sxhash x) #xff)))
1136 ((input-types equal))
1137 (let ((simplified (simplify-unions input-types)))
1138 (cond ((null simplified) *empty-type*)
1139 ((null (cdr simplified)) (car simplified))
1140 (t (make-union-type simplified)))))
1141
1142 (defun simplify-big-union (type first rest)
1143 ;;
1144 (let ((lowest (numeric-type-low first))
1145 (highest (numeric-type-high first)))
1146 (dolist (type rest)
1147 (multiple-value-bind (type-lo type-hi)
1148 (values (numeric-type-low type)
1149 (numeric-type-high type))
1150 (if (and (numberp lowest) (numberp type-lo))
1151 (setf lowest (min lowest type-lo))
1152 (setf lowest nil))
1153 (if (and (numberp highest) (numberp type-hi))
1154 (setf highest (max highest type-hi))
1155 (setf highest nil))))
1156 (list (specifier-type `(,type ,(or lowest '*) ,(or highest '*))))))
1157
1158
1159 (defparameter *union-length-threshold* 50
1160 "The maximum length of a union of integer types before we take a
1161 short cut and return a simpler union.")
1162
1163 (defun simplify-unions (types)
1164 (when types
1165 (multiple-value-bind (first rest)
1166 (if (union-type-p (car types))
1167 (values (car (union-type-types (car types)))
1168 (append (cdr (union-type-types (car types)))
1169 (cdr types)))
1170 (values (car types) (cdr types)))
1171 (cond
1172 ((and (> (length rest) *union-length-threshold*)
1173 (csubtypep first (specifier-type 'integer))
1174 (every #'(lambda (x)
1175 (and (numeric-type-p x)
1176 (eq (numeric-type-class x) 'integer)))
1177 (cons first rest)))
1178 ;; FIXME: We sometimes spend huge amounts of time computing
1179 ;; the union of a bunch of disjoint numeric types. This is a
1180 ;; hack to shortcut that. If the union is long enough and
1181 ;; they're all of the same type, we give up and try to return
1182 ;; an interval that is a superset of each type.
1183 (simplify-big-union 'integer first rest))
1184 ((and (> (length rest) *union-length-threshold*)
1185 (csubtypep first (specifier-type 'float))
1186 (let ((class (numeric-type-class first))
1187 (format (numeric-type-format first)))
1188 (every #'(lambda (x)
1189 (and (numeric-type-p x)
1190 (eq (numeric-type-class x) class)
1191 (eq (numeric-type-format x) format)))
1192 (cons first rest))))
1193 ;; Same as above, but for floats.
1194 (simplify-big-union (or (numeric-type-format first) 'float) first rest))
1195 (t
1196 (let ((rest (simplify-unions rest)) u)
1197 (dolist (r rest (cons first rest))
1198 (when (setq u (type-union2 first r))
1199 (return (simplify-unions (nsubstitute u r rest)))))))))))
1200
1201 (defun-cached (type-union2 :hash-function type-cache-hash
1202 :hash-bits 8
1203 :init-form cold-load-init)
1204 ((type1 eq) (type2 eq))
1205 (declare (type ctype type1 type2))
1206 (setq type1 (reparse-unknown-type type1))
1207 (setq type2 (reparse-unknown-type type2))
1208 (cond ((eq type1 type2) type1)
1209 ((csubtypep type1 type2) type2)
1210 ((csubtypep type2 type1) type1)
1211 (t
1212 (flet ((1way (x y)
1213 (invoke-type-method :simple-union :complex-union
1214 x y
1215 :default nil)))
1216 (or (1way type1 type2)
1217 (1way type2 type1))))))
1218
1219 ;;; Return as restrictive and simple a type as we can discover that is
1220 ;;; no more restrictive than the intersection of TYPE1 and TYPE2. At
1221 ;;; worst, we arbitrarily return one of the arguments as the first
1222 ;;; value (trying not to return a hairy type).
1223 (defun type-approx-intersection2 (type1 type2)
1224 (cond ((type-intersection2 type1 type2))
1225 ((hairy-type-p type1) type2)
1226 (t type1)))
1227
1228 ;;; Type-Intersection -- Interface
1229 ;;;
1230 ;;; Return as restrictive a type as we can discover that is no more
1231 ;;; restrictive than the intersection of Type1 and Type2. The second value is
1232 ;;; true if the result is exact. At worst, we randomly return one of the
1233 ;;; arguments as the first value (trying not to return a hairy type).
1234 ;;;
1235 (defun type-intersection (&rest input-types)
1236 (%type-intersection input-types))
1237
1238 (defun-cached (%type-intersection :hash-bits 8
1239 :hash-function (lambda (x)
1240 (logand (sxhash x) #xff)))
1241 ((input-types equal))
1242 (let ((simplified (simplify-intersections input-types)))
1243 ;(declare (type (vector ctype) simplified))
1244 ;; We want to have a canonical representation of types (or failing
1245 ;; that, punt to HAIRY-TYPE). Canonical representation would have
1246 ;; intersections inside unions but not vice versa, since you can
1247 ;; always achieve that by the distributive rule. But we don't want
1248 ;; to just apply the distributive rule, since it would be too easy
1249 ;; to end up with unreasonably huge type expressions. So instead
1250 ;; we try to generate a simple type by distributing the union; if
1251 ;; the type can't be made simple, we punt to HAIRY-TYPE.
1252 (if (and (cdr simplified) (some #'union-type-p simplified))
1253 (let* ((first-union (find-if #'union-type-p simplified))
1254 (other-types (remove first-union simplified))
1255 (distributed (maybe-distribute-one-union first-union other-types)))
1256 (if distributed
1257 (apply #'type-union distributed)
1258 (make-hairy-type
1259 :specifier `(and ,@(mapcar #'type-specifier simplified)))))
1260 (cond
1261 ((null simplified) *universal-type*)
1262 ((null (cdr simplified)) (car simplified))
1263 (t (make-intersection-type
1264 (some #'type-enumerable simplified)
1265 simplified))))))
1266
1267 (defun simplify-intersections (types)
1268 (when types
1269 (multiple-value-bind (first rest)
1270 (if (intersection-type-p (car types))
1271 (values (car (intersection-type-types (car types)))
1272 (append (cdr (intersection-type-types (car types)))
1273 (cdr types)))
1274 (values (car types) (cdr types)))
1275 (let ((rest (simplify-intersections rest)) u)
1276 (dolist (r rest (cons first rest))
1277 (when (setq u (type-intersection2 first r))
1278 (return (simplify-intersections (nsubstitute u r rest)))))))))
1279
1280 (defun-cached (type-intersection2 :hash-function type-cache-hash
1281 :hash-bits 8
1282 :init-form cold-load-init)
1283 ((type1 eq) (type2 eq))
1284 (declare (type ctype type1 type2))
1285 (setq type1 (reparse-unknown-type type1))
1286 (setq type2 (reparse-unknown-type type2))
1287 (cond ((eq type1 type2)
1288 ;; FIXME: For some reason, this doesn't catch e.g. type1 =
1289 ;; type2 = (SPECIFIER-TYPE
1290 ;; 'SOME-UNKNOWN-TYPE). Investigate. - CSR, 2002-04-10
1291 type1)
1292 ((or (intersection-type-p type1)
1293 (intersection-type-p type2))
1294 ;; Intersections of INTERSECTION-TYPE should have the
1295 ;; INTERSECTION-TYPE-TYPES values broken out and intersected
1296 ;; separately. The full TYPE-INTERSECTION function knows how
1297 ;; to do that, so let it handle it.
1298 (type-intersection type1 type2))
1299 ;;
1300 ;; (AND (FUNCTION (T) T) GENERIC-FUNCTION) for instance, but
1301 ;; not (AND (FUNCTION (T) T) (FUNCTION (T) T)).
1302 ((let ((function (specifier-type 'function)))
1303 (or (and (function-type-p type1)
1304 (not (or (function-type-p type2) (eq function type2)))
1305 (csubtypep type2 function)
1306 (not (csubtypep function type2)))
1307 (and (function-type-p type2)
1308 (not (or (function-type-p type1) (eq function type1)))
1309 (csubtypep type1 function)
1310 (not (csubtypep function type1)))))
1311 nil)
1312 (t
1313 (flet ((1way (x y)
1314 (invoke-type-method :simple-intersection
1315 :complex-intersection
1316 x y
1317 :default :no-type-method-found)))
1318 (let ((xy (1way type1 type2)))
1319 (or (and (not (eql xy :no-type-method-found)) xy)
1320 (let ((yx (1way type2 type1)))
1321 (or (and (not (eql yx :no-type-method-found)) yx)
1322 (cond ((and (eql xy :no-type-method-found)
1323 (eql yx :no-type-method-found))
1324 *empty-type*)
1325 (t
1326 (assert (and (not xy) (not yx)))
1327 nil))))))))))
1328
1329 (defun maybe-distribute-one-union (union-type types)
1330 (let* ((intersection (apply #'type-intersection types))
1331 (union (mapcar (lambda (x) (type-intersection x intersection))
1332 (union-type-types union-type))))
1333 (if (notany (lambda (x)
1334 (or (hairy-type-p x)
1335 (intersection-type-p x)))
1336 union)
1337 union
1338 nil)))
1339
1340 ;;; Types-Intersect -- Interface
1341 ;;;
1342 ;;; The first value is true unless the types don't intersect. The second
1343 ;;; value is true if the first value is definitely correct. NIL is considered
1344 ;;; to intersect with any type. If T is a subtype of either type, then we also
1345 ;;; return T, T. This way we consider hairy types to intersect with T.
1346 ;;;
1347 (defun types-intersect (type1 type2)
1348 (declare (type ctype type1 type2))
1349 (if (or (eq type1 *empty-type*) (eq type2 *empty-type*))
1350 (values t t)
1351 (let ((intersection2 (type-intersection2 type1 type2)))
1352 (cond ((not intersection2)
1353 (if (or (csubtypep *universal-type* type1)
1354 (csubtypep *universal-type* type2))
1355 (values t t)
1356 (values t nil)))
1357 ((eq intersection2 *empty-type*) (values nil t))
1358 (t (values t t))))))
1359
1360 ;;; Type-Specifier -- Interface
1361 ;;;
1362 ;;; Return a Common Lisp type specifier corresponding to this type.
1363 ;;;
1364 (defun type-specifier (type)
1365 (declare (type ctype type))
1366 (funcall (type-class-unparse (type-class-info type)) type))
1367
1368
1369 ;;; VALUES-SPECIFIER-TYPE -- Interface
1370 ;;;
1371 ;;; Return the type structure corresponding to a type specifier. We pick
1372 ;;; off Structure types as a special case.
1373 ;;;
1374 ;;; Note: VALUES-SPECIFIER-TYPE-CACHE-CLEAR must be called whenever a type is
1375 ;;; defined (or redefined).
1376 ;;;
1377 (defun-cached (values-specifier-type
1378 :hash-function (lambda (x)
1379 (the fixnum
1380 (logand (the fixnum (cache-hash-eq x))
1381 #x3FF)))
1382 :hash-bits 10
1383 :init-form cold-load-init)
1384 ((orig equal-but-no-car-recursion))
1385 (or (info type builtin orig)
1386 (let ((spec (type-expand orig)))
1387 (cond
1388 ((and (not (eq spec orig))
1389 (info type builtin spec)))
1390 ((eq (info type kind spec) :instance)
1391 (kernel::find-class spec))
1392 ((typep spec 'kernel::class)
1393 (if (typep spec 'kernel::built-in-class)
1394 (or (built-in-class-translation spec) spec)
1395 spec))
1396 (t
1397 (let* ((lspec (if (atom spec) (list spec) spec))
1398 (fun (info type translator (car lspec))))
1399 (cond (fun
1400 (funcall fun lspec))
1401 ((or (and (consp spec) (symbolp (car spec)))
1402 (symbolp spec))
1403 (when *type-system-initialized*
1404 (signal 'parse-unknown-type :specifier spec))
1405 ;;
1406 ;; Inhibit caching...
1407 (return-from values-specifier-type
1408 (make-unknown-type :specifier spec)))
1409 (t
1410 (simple-program-error (intl:gettext "Bad thing to be a type specifier: ~S.")
1411 spec)))))))))
1412
1413 ;;; SPECIFIER-TYPE -- Interface
1414 ;;;
1415 ;;; Like VALUES-SPECIFIER-TYPE, except that we guarantee to never return a
1416 ;;; VALUES type.
1417 ;;;
1418 (defun specifier-type (x)
1419 (let ((res (values-specifier-type x)))
1420 (when (values-type-p res)
1421 (simple-program-error (intl:gettext "VALUES type illegal in this context:~% ~S") x))
1422 res))
1423
1424 (defun single-value-specifier-type (x)
1425 (let ((res (specifier-type x)))
1426 (if (eq res *wild-type*)
1427 *universal-type*
1428 res)))
1429
1430 ;;; Type-Expand -- Interface
1431 ;;;
1432 ;;; Similar to Macroexpand, but expands deftypes. We don't bother returning
1433 ;;; a second value.
1434 ;;;
1435 (defun type-expand (form)
1436 (let ((def (cond ((symbolp form)
1437 (info type expander form))
1438 ((and (consp form) (symbolp (car form)))
1439 (info type expander (car form)))
1440 (t nil))))
1441 (if def
1442 (type-expand (funcall def (if (consp form) form (list form))))
1443 form)))
1444
1445
1446 ;;; Precompute-Types -- Interface
1447 ;;;
1448 ;;; Take a list of type specifiers, compute the translation and define it as
1449 ;;; a builtin type.
1450 ;;;
1451 (defun precompute-types (specs)
1452 (declare (list specs))
1453 (dolist (spec specs)
1454 (let ((res (specifier-type spec)))
1455 (unless (unknown-type-p res)
1456 (setf (info type builtin spec) res)
1457 (setf (info type kind spec) :primitive)))))
1458
1459
1460 ;;;; Builtin types.
1461
1462 (defvar *wild-type*)
1463 (defvar *empty-type*)
1464 (defvar *universal-type*)
1465
1466 (cold-load-init
1467 (macrolet ((frob (name var)
1468 `(progn
1469 (setq ,var (make-named-type :name ',name))
1470 (setf (info type kind ',name) :primitive)
1471 (setf (info type builtin ',name) ,var))))
1472 (frob * *wild-type*)
1473 (frob nil *empty-type*)
1474 (frob t *universal-type*)))
1475
1476 (define-type-method (named :simple-=) (type1 type2)
1477 ;; FIXME: BUG 85: This assertion failed when I added it in
1478 ;; sbcl-0.6.11.13. It probably shouldn't fail; but for now it's
1479 ;; just commented out.
1480 ;;(aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1481 (values (eq type1 type2) t))
1482
1483 (define-type-method (named :complex-=) (type1 type2)
1484 (cond
1485 ((and (eq type2 *empty-type*)
1486 (intersection-type-p type1)
1487 ;; not allowed to be unsure on these... FIXME: keep the list
1488 ;; of CL types that are intersection types once and only
1489 ;; once.
1490 (not (or (type= type1 (specifier-type 'ratio))
1491 (type= type1 (specifier-type 'keyword)))))
1492 ;; things like (AND (EQL 0) (SATISFIES ODDP)) or (AND FUNCTION
1493 ;; STREAM) can get here. In general, we can't really tell
1494 ;; whether these are equal to NIL or not, so
1495 (values nil nil))
1496 ((type-might-contain-other-types-p type1)
1497 (invoke-complex-=-other-method type1 type2))
1498 (t (values nil t))))
1499
1500 (define-type-method (named :simple-subtypep) (type1 type2)
1501 (assert (not (eq type1 *wild-type*))) ; * isn't really a type.
1502 (values (or (eq type1 *empty-type*) (eq type2 *wild-type*)) t))
1503
1504 (define-type-method (named :complex-subtypep-arg1) (type1 type2)
1505 ;; This AVER causes problems if we write accurate methods for the
1506 ;; union (and possibly intersection) types which then delegate to
1507 ;; us; while a user shouldn't get here, because of the odd status of
1508 ;; *wild-type* a type-intersection executed by the compiler can. -
1509 ;; CSR, 2002-04-10
1510 ;;
1511 ;; (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1512 (cond ((eq type1 *empty-type*)
1513 t)
1514 (;; When TYPE2 might be the universal type in disguise
1515 (type-might-contain-other-types-p type2)
1516 ;; Now that the UNION and HAIRY COMPLEX-SUBTYPEP-ARG2 methods
1517 ;; can delegate to us (more or less as CALL-NEXT-METHOD) when
1518 ;; they're uncertain, we can't just barf on COMPOUND-TYPE and
1519 ;; HAIRY-TYPEs as we used to. Instead we deal with the
1520 ;; problem (where at least part of the problem is cases like
1521 ;; (SUBTYPEP T '(SATISFIES FOO))
1522 ;; or
1523 ;; (SUBTYPEP T '(AND (SATISFIES FOO) (SATISFIES BAR)))
1524 ;; where the second type is a hairy type like SATISFIES, or
1525 ;; is a compound type which might contain a hairy type) by
1526 ;; returning uncertainty.
1527 (values nil nil))
1528 (t
1529 ;; By elimination, TYPE1 is the universal type.
1530 (assert (or (eq type1 *wild-type*) (eq type1 *universal-type*)))
1531 ;; This case would have been picked off by the SIMPLE-SUBTYPEP
1532 ;; method, and so shouldn't appear here.
1533 (assert (not (eq type2 *universal-type*)))
1534 ;; Since TYPE2 is not EQ *UNIVERSAL-TYPE* and is not the
1535 ;; universal type in disguise, TYPE2 is not a superset of TYPE1.
1536 (values nil t))))
1537
1538 (define-type-method (named :complex-subtypep-arg2) (type1 type2)
1539 (assert (not (eq type2 *wild-type*))) ; * isn't really a type.
1540 (cond ((eq type2 *universal-type*)
1541 (values t t))
1542 ((type-might-contain-other-types-p type1)
1543 ;; those types can be *EMPTY-TYPE* or *UNIVERSAL-TYPE* in
1544 ;; disguise. So we'd better delegate.
1545 (invoke-complex-subtypep-arg1-method type1 type2))
1546 (t
1547 ;; FIXME: This seems to rely on there only being 2 or 3
1548 ;; NAMED-TYPE values, and the exclusion of various
1549 ;; possibilities above. It would be good to explain it and/or
1550 ;; rewrite it so that it's clearer.
1551 (values (not (eq type2 *empty-type*)) t))))
1552
1553 (define-type-method (named :complex-intersection) (type1 type2)
1554 ;; FIXME: This assertion failed when I added it in sbcl-0.6.11.13.
1555 ;; Perhaps when bug 85 is fixed it can be reenabled.
1556 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1557 (hierarchical-intersection2 type1 type2))
1558
1559 (define-type-method (named :complex-union) (type1 type2)
1560 ;; Perhaps when bug 85 is fixed this can be reenabled.
1561 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1562 (hierarchical-union2 type1 type2))
1563
1564 (define-type-method (named :unparse) (x)
1565 (named-type-name x))
1566
1567
1568 ;;;; Hairy and unknown types:
1569
1570 (define-type-method (hairy :unparse) (x)
1571 (hairy-type-specifier x))
1572
1573 (define-type-method (hairy :simple-subtypep) (type1 type2)
1574 (let ((hairy-spec1 (hairy-type-specifier type1))
1575 (hairy-spec2 (hairy-type-specifier type2)))
1576 (cond ((equal-but-no-car-recursion hairy-spec1 hairy-spec2)
1577 (values t t))
1578 (t
1579 (values nil nil)))))
1580
1581 (define-type-method (hairy :complex-subtypep-arg2) (type1 type2)
1582 (invoke-complex-subtypep-arg1-method type1 type2))
1583
1584 (define-type-method (hairy :complex-subtypep-arg1) (type1 type2)
1585 (declare (ignore type1 type2))
1586 (values nil nil))
1587
1588 (define-type-method (hairy :complex-=) (type1 type2)
1589 (if (and (unknown-type-p type2)
1590 (let* ((specifier2 (unknown-type-specifier type2))
1591 (name2 (if (consp specifier2)
1592 (car specifier2)
1593 specifier2)))
1594 (info type kind name2)))
1595 (let ((type2 (specifier-type (unknown-type-specifier type2))))
1596 (if (unknown-type-p type2)
1597 (values nil nil)
1598 (type= type1 type2)))
1599 (values nil nil)))
1600
1601 (define-type-method (hairy :simple-intersection :complex-intersection)
1602 (type1 type2)
1603 (if (type= type1 type2)
1604 type1
1605 nil))
1606
1607 (define-type-method (hairy :simple-union)
1608 (type1 type2)
1609 (if (type= type1 type2)
1610 type1
1611 nil))
1612
1613 (define-type-method (hairy :simple-=) (type1 type2)
1614 (if (equal-but-no-car-recursion (hairy-type-specifier type1)
1615 (hairy-type-specifier type2))
1616 (values t t)
1617 (values nil nil)))
1618
1619
1620 (def-type-translator satisfies (&whole whole fun)
1621 (declare (ignore fun))
1622 ;; Check legality of arguments.
1623 (destructuring-bind (satisfies predicate-name) whole
1624 (declare (ignore satisfies))
1625 (unless (symbolp predicate-name)
1626 (error 'simple-type-error
1627 :datum predicate-name
1628 :expected-type 'symbol
1629 :format-control (intl:gettext "The SATISFIES predicate name is not a symbol: ~S")
1630 :format-arguments (list predicate-name))))
1631 ;; Create object.
1632 (make-hairy-type :specifier whole))
1633
1634
1635 ;;;; Negation Types
1636
1637 (define-type-method (negation :unparse) (x)
1638 `(not ,(type-specifier (negation-type-type x))))
1639
1640 (define-type-method (negation :simple-subtypep) (type1 type2)
1641 (csubtypep (negation-type-type type2) (negation-type-type type1)))
1642
1643 (define-type-method (negation :complex-subtypep-arg2) (type1 type2)
1644 (let* ((complement-type2 (negation-type-type type2))
1645 (intersection2 (type-intersection type1 complement-type2)))
1646 (if intersection2
1647 ;; FIXME: if uncertain, maybe try arg1?
1648 (type= intersection2 *empty-type*)
1649 (invoke-complex-subtypep-arg1-method type1 type2))))
1650
1651 (define-type-method (negation :complex-subtypep-arg1) (type1 type2)
1652 ;; "Incrementally extended heuristic algorithms tend inexorably toward the
1653 ;; incomprehensible." -- http://www.unlambda.com/~james/lambda/lambda.txt
1654 ;;
1655 ;; You may not believe this. I couldn't either. But then I sat down
1656 ;; and drew lots of Venn diagrams. Comments involving a and b refer
1657 ;; to the call (subtypep '(not a) 'b) -- CSR, 2002-02-27.
1658 (block nil
1659 ;; (Several logical truths in this block are true as long as
1660 ;; b/=T. As of sbcl-0.7.1.28, it seems impossible to construct a
1661 ;; case with b=T where we actually reach this type method, but
1662 ;; we'll test for and exclude this case anyway, since future
1663 ;; maintenance might make it possible for it to end up in this
1664 ;; code.)
1665 (multiple-value-bind (equal certain)
1666 (type= type2 *universal-type*)
1667 (unless certain
1668 (return (values nil nil)))
1669 (when equal
1670 (return (values t t))))
1671 (let ((complement-type1 (negation-type-type type1)))
1672 ;; Do the special cases first, in order to give us a chance if
1673 ;; subtype/supertype relationships are hairy.
1674 (multiple-value-bind (equal certain)
1675 (type= complement-type1 type2)
1676 ;; If a = b, ~a is not a subtype of b (unless b=T, which was
1677 ;; excluded above).
1678 (unless certain
1679 (return (values nil nil)))
1680 (when equal
1681 (return (values nil t))))
1682 ;; KLUDGE: ANSI requires that the SUBTYPEP result between any
1683 ;; two built-in atomic type specifiers never be uncertain. This
1684 ;; is hard to do cleanly for the built-in types whose
1685 ;; definitions include (NOT FOO), i.e. CONS and RATIO. However,
1686 ;; we can do it with this hack, which uses our global knowledge
1687 ;; that our implementation of the type system uses disjoint
1688 ;; implementation types to represent disjoint sets (except when
1689 ;; types are contained in other types). (This is a KLUDGE
1690 ;; because it's fragile. Various changes in internal
1691 ;; representation in the type system could make it start
1692 ;; confidently returning incorrect results.) -- WHN 2002-03-08
1693 (unless (or (type-might-contain-other-types-p complement-type1)
1694 (type-might-contain-other-types-p type2))
1695 ;; Because of the way our types which don't contain other
1696 ;; types are disjoint subsets of the space of possible values,
1697 ;; (SUBTYPEP '(NOT AA) 'B)=NIL when AA and B are simple (and B
1698 ;; is not T, as checked above).
1699 (return (values nil t)))
1700 ;; The old (TYPE= TYPE1 TYPE2) branch would never be taken, as
1701 ;; TYPE1 and TYPE2 will only be equal if they're both NOT types,
1702 ;; and then the :SIMPLE-SUBTYPEP method would be used instead.
1703 ;; But a CSUBTYPEP relationship might still hold:
1704 (multiple-value-bind (equal certain)
1705 (csubtypep complement-type1 type2)
1706 ;; If a is a subtype of b, ~a is not a subtype of b (unless
1707 ;; b=T, which was excluded above).
1708 (unless certain
1709 (return (values nil nil)))
1710 (when equal
1711 (return (values nil t))))
1712 (multiple-value-bind (equal certain)
1713 (csubtypep type2 complement-type1)
1714 ;; If b is a subtype of a, ~a is not a subtype of b. (FIXME:
1715 ;; That's not true if a=T. Do we know at this point that a is
1716 ;; not T?)
1717 (unless certain
1718 (return (values nil nil)))
1719 (when equal
1720 (return (values nil t))))
1721 ;; old CSR comment ca. 0.7.2, now obsoleted by the SIMPLE-CTYPE?
1722 ;; KLUDGE case above: Other cases here would rely on being able
1723 ;; to catch all possible cases, which the fragility of this type
1724 ;; system doesn't inspire me; for instance, if a is type= to ~b,
1725 ;; then we want T, T; if this is not the case and the types are
1726 ;; disjoint (have an intersection of *empty-type*) then we want
1727 ;; NIL, T; else if the union of a and b is the *universal-type*
1728 ;; then we want T, T. So currently we still claim to be unsure
1729 ;; about e.g. (subtypep '(not fixnum) 'single-float).
1730 ;;
1731 ;; OTOH we might still get here:
1732 (values nil nil))))
1733
1734 (define-type-method (negation :complex-=) (type1 type2)
1735 ;; (NOT FOO) isn't equivalent to anything that's not a negation
1736 ;; type, except possibly a type that might contain it in disguise.
1737 (declare (ignore type2))
1738 (if (type-might-contain-other-types-p type1)
1739 (values nil nil)
1740 (values nil t)))
1741
1742 (define-type-method (negation :simple-intersection) (type1 type2)
1743 (let ((not1 (negation-type-type type1))
1744 (not2 (negation-type-type type2)))
1745 (cond
1746 ((csubtypep not1 not2) type2)
1747 ((csubtypep not2 not1) type1)
1748 ;; Why no analagous clause to the disjoint in the SIMPLE-UNION2
1749 ;; method, below? The clause would read
1750 ;;
1751 ;; ((EQ (TYPE-UNION NOT1 NOT2) *UNIVERSAL-TYPE*) *EMPTY-TYPE*)
1752 ;;
1753 ;; but with proper canonicalization of negation types, there's
1754 ;; no way of constructing two negation types with union of their
1755 ;; negations being the universal type.
1756 (t
1757 (assert (not (eq (type-union not1 not2) *universal-type*)))
1758 nil))))
1759
1760 (define-type-method (negation :complex-intersection) (type1 type2)
1761 (cond
1762 ((csubtypep type1 (negation-type-type type2)) *empty-type*)
1763 ((eq (type-intersection type1 (negation-type-type type2)) *empty-type*)
1764 type1)
1765 (t nil)))
1766
1767 (define-type-method (negation :simple-union) (type1 type2)
1768 (let ((not1 (negation-type-type type1))
1769 (not2 (negation-type-type type2)))
1770 (cond
1771 ((csubtypep not1 not2) type1)
1772 ((csubtypep not2 not1) type2)
1773 ((eq (type-intersection not1 not2) *empty-type*)
1774 *universal-type*)
1775 (t nil))))
1776
1777 (define-type-method (negation :complex-union) (type1 type2)
1778 (cond
1779 ((csubtypep (negation-type-type type2) type1) *universal-type*)
1780 ((eq (type-intersection type1 (negation-type-type type2)) *empty-type*)
1781 type2)
1782 (t nil)))
1783
1784 (define-type-method (negation :simple-=) (type1 type2)
1785 (type= (negation-type-type type1) (negation-type-type type2)))
1786
1787 (def-type-translator not (typespec)
1788 (let* ((not-type (specifier-type typespec))
1789 (spec (type-specifier not-type)))
1790 (cond
1791 ;; canonicalize (NOT (NOT FOO))
1792 ((and (listp spec) (eq (car spec) 'not))
1793 (specifier-type (cadr spec)))
1794 ;; canonicalize (NOT NIL) and (NOT T)
1795 ((eq not-type *empty-type*) *universal-type*)
1796 ((eq not-type *universal-type*) *empty-type*)
1797 ((and (numeric-type-p not-type)
1798 (null (numeric-type-low not-type))
1799 (null (numeric-type-high not-type)))
1800 (make-negation-type :type not-type))
1801 ((numeric-type-p not-type)
1802 (type-union
1803 (make-negation-type
1804 :type (modified-numeric-type not-type :low nil :high nil))
1805 (cond
1806 ((null (numeric-type-low not-type))
1807 (modified-numeric-type
1808 not-type
1809 :low (let ((h (numeric-type-high not-type)))
1810 (if (consp h) (car h) (list h)))
1811 :high nil))
1812 ((null (numeric-type-high not-type))
1813 (modified-numeric-type
1814 not-type
1815 :low nil
1816 :high (let ((l (numeric-type-low not-type)))
1817 (if (consp l) (car l) (list l)))))
1818 (t (type-union
1819 (modified-numeric-type
1820 not-type
1821 :low nil
1822 :high (let ((l (numeric-type-low not-type)))
1823 (if (consp l) (car l) (list l))))
1824 (modified-numeric-type
1825 not-type
1826 :low (let ((h (numeric-type-high not-type)))
1827 (if (consp h) (car h) (list h)))
1828 :high nil))))))
1829 ((intersection-type-p not-type)
1830 (apply #'type-union
1831 (mapcar #'(lambda (x)
1832 (specifier-type `(not ,(type-specifier x))))
1833 (intersection-type-types not-type))))
1834 ((union-type-p not-type)
1835 (apply #'type-intersection
1836 (mapcar #'(lambda (x)
1837 (specifier-type `(not ,(type-specifier x))))
1838 (union-type-types not-type))))
1839 ((member-type-p not-type)
1840 (let ((members (member-type-members not-type)))
1841 (if (some #'floatp members)
1842 (let (floats)
1843 (dolist (pair '((0.0f0 . -0.0f0) (0.0d0 . -0.0d0)
1844 #+long-float (0.0l0 . -0.0l0)))
1845 (when (member (car pair) members)
1846 (assert (not (member (cdr pair) members)))
1847 (push (cdr pair) floats)
1848 (setf members (remove (car pair) members)))
1849 (when (member (cdr pair) members)
1850 (assert (not (member (car pair) members)))
1851 (push (car pair) floats)
1852 (setf members (remove (cdr pair) members))))
1853 (apply #'type-intersection
1854 (if (null members)
1855 *universal-type*
1856 (make-negation-type
1857 :type (make-member-type :members members)))
1858 (mapcar
1859 (lambda (x)
1860 (let ((type (ctype-of x)))
1861 (type-union
1862 (make-negation-type
1863 :type (modified-numeric-type type
1864 :low nil :high nil))
1865 (modified-numeric-type type
1866 :low nil :high (list x))
1867 (make-member-type :members (list x))
1868 (modified-numeric-type type
1869 :low (list x) :high nil))))
1870 floats)))
1871 (make-negation-type :type not-type))))
1872 ((and (cons-type-p not-type)
1873 (eq (cons-type-car-type not-type) *universal-type*)
1874 (eq (cons-type-cdr-type not-type) *universal-type*))
1875 (make-negation-type :type not-type))
1876 ((cons-type-p not-type)
1877 (type-union
1878 (make-negation-type :type (specifier-type 'cons))
1879 (cond
1880 ((and (not (eq (cons-type-car-type not-type) *universal-type*))
1881 (not (eq (cons-type-cdr-type not-type) *universal-type*)))
1882 (type-union
1883 (make-cons-type
1884 (specifier-type `(not ,(type-specifier
1885 (cons-type-car-type not-type))))
1886 *universal-type*)
1887 (make-cons-type
1888 *universal-type*
1889 (specifier-type `(not ,(type-specifier
1890 (cons-type-cdr-type not-type)))))))
1891 ((not (eq (cons-type-car-type not-type) *universal-type*))
1892 (make-cons-type
1893 (specifier-type `(not ,(type-specifier
1894 (cons-type-car-type not-type))))
1895 *universal-type*))
1896 ((not (eq (cons-type-cdr-type not-type) *universal-type*))
1897 (make-cons-type
1898 *universal-type*
1899 (specifier-type `(not ,(type-specifier
1900 (cons-type-cdr-type not-type))))))
1901 (t (error (intl:gettext "Weird CONS type ~S") not-type)))))
1902 (t (make-negation-type :type not-type)))))
1903
1904
1905 ;;;; Numeric types.
1906
1907 ;;; A list of all the float formats, in order of decreasing precision.
1908 ;;;
1909 (eval-when (compile load eval)
1910 (defconstant float-formats
1911 '(#+double-double double-double-float
1912 long-float double-float single-float short-float)))
1913
1914 ;;; The type of a float format.
1915 ;;;
1916 (deftype float-format () `(member ,@float-formats))
1917
1918
1919 (define-type-method (number :simple-=) (type1 type2)
1920 (values
1921 (and (eq (numeric-type-class type1) (numeric-type-class type2))
1922 (eq (numeric-type-format type1) (numeric-type-format type2))
1923 (eq (numeric-type-complexp type1) (numeric-type-complexp type2))
1924 (equalp (numeric-type-low type1) (numeric-type-low type2))
1925 (equalp (numeric-type-high type1) (numeric-type-high type2)))
1926 t))
1927
1928 (define-type-method (number :unparse) (type)
1929 (let* ((complexp (numeric-type-complexp type))
1930 (low (numeric-type-low type))
1931 (high (numeric-type-high type))
1932 (base (case (numeric-type-class type)
1933 (integer 'integer)
1934 (rational 'rational)
1935 (float (or (numeric-type-format type) 'float))
1936 (t 'real))))
1937 (let ((base+bounds
1938 (cond ((and (eq base 'integer) high low)
1939 (let ((high-count (logcount high))
1940 (high-length (integer-length high)))
1941 (cond ((= low 0)
1942 (cond ((= high 0) '(integer 0 0))
1943 ((= high 1) 'bit)
1944 ((and (= high-count high-length)
1945 (plusp high-length))
1946 `(unsigned-byte ,high-length))
1947 (t
1948 `(mod ,(1+ high)))))
1949 ((and (= low vm:target-most-negative-fixnum)
1950 (= high vm:target-most-positive-fixnum))
1951 'fixnum)
1952 ((and (= low (lognot high))
1953 (= high-count high-length)
1954 (> high-count 0))
1955 `(signed-byte ,(1+ high-length)))
1956 (t
1957 `(integer ,low ,high)))))
1958 (high `(,base ,(or low '*) ,high))
1959 (low
1960 (if (and (eq base 'integer) (= low 0))
1961 'unsigned-byte
1962 `(,base ,low)))
1963 (t base))))
1964 (ecase complexp
1965 (:real
1966 base+bounds)
1967 (:complex
1968 (if (eq base+bounds 'real)
1969 'complex
1970 `(complex ,base+bounds)))
1971 ((nil)
1972 (assert (eq base+bounds 'real))
1973 'number)))))
1974
1975 ;;; Numeric-Bound-Test -- Internal
1976 ;;;
1977 ;;; Return true if X is "less than or equal" to Y, taking open bounds into
1978 ;;; consideration. Closed is the predicate used to test the bound on a closed
1979 ;;; interval (e.g. <=), and Open is the predicate used on open bounds (e.g. <).
1980 ;;; Y is considered to be the outside bound, in the sense that if it is
1981 ;;; infinite (NIL), then the test suceeds, whereas if X is infinite, then the
1982 ;;; test fails (unless Y is also infinite).
1983 ;;;
1984 ;;; This is for comparing bounds of the same kind, e.g. upper and upper.
1985 ;;; Use Numeric-Bound-Test* for different kinds of bounds.
1986 ;;;
1987 (defmacro numeric-bound-test (x y closed open)
1988 `(cond ((not ,y) t)
1989 ((not ,x) nil)
1990 ((consp ,x)
1991 (if (consp ,y)
1992 (,closed (car ,x) (car ,y))
1993 (,closed (car ,x) ,y)))
1994 (t
1995 (if (consp ,y)
1996 (,open ,x (car ,y))
1997 (,closed ,x ,y)))))
1998
1999 ;;; Numeric-Bound-Test* -- Internal
2000 ;;;
2001 ;;; Used to compare upper and lower bounds. This is different from the
2002 ;;; same-bound case:
2003 ;;; -- Since X = NIL is -infinity, whereas y = NIL is +infinity, we return true
2004 ;;; if *either* arg is NIL.
2005 ;;; -- an open inner bound is "greater" and also squeezes the interval, causing
2006 ;;; us to use the Open test for those cases as well.
2007 ;;;
2008 (defmacro numeric-bound-test* (x y closed open)
2009 `(cond ((not ,y) t)
2010 ((not ,x) t)
2011 ((consp ,x)
2012 (if (consp ,y)
2013 (,open (car ,x) (car ,y))
2014 (,open (car ,x) ,y)))
2015 (t
2016 (if (consp ,y)
2017 (,open ,x (car ,y))
2018 (,closed ,x ,y)))))
2019
2020 ;;; Numeric-Bound-Max -- Internal
2021 ;;;
2022 ;;; Return whichever of the numeric bounds X and Y is "maximal" according to
2023 ;;; the predicates Closed (e.g. >=) and Open (e.g. >). This is only meaningful
2024 ;;; for maximizing like bounds, i.e. upper and upper. If Max-P is true, then
2025 ;;; we return NIL if X or Y is NIL, otherwise we return the other arg.
2026 ;;;
2027 (defmacro numeric-bound-max (x y closed open max-p)
2028 (once-only ((n-x x)
2029 (n-y y))
2030 `(cond ((not ,n-x) ,(if max-p nil n-y))
2031 ((not ,n-y) ,(if max-p nil n-x))
2032 ((consp ,n-x)
2033 (if (consp ,n-y)
2034 (if (,closed (car ,n-x) (car ,n-y)) ,n-x ,n-y)
2035 (if (,open (car ,n-x) ,n-y) ,n-x ,n-y)))
2036 (t
2037 (if (consp ,n-y)
2038 (if (,open (car ,n-y) ,n-x) ,n-y ,n-x)
2039 (if (,closed ,n-y ,n-x) ,n-y ,n-x))))))
2040
2041 (define-type-method (number :simple-subtypep) (type1 type2)
2042 (let ((class1 (numeric-type-class type1))
2043 (class2 (numeric-type-class type2))
2044 (complexp2 (numeric-type-complexp type2))
2045 (format2 (numeric-type-format type2))
2046 (low1 (numeric-type-low type1))
2047 (high1 (numeric-type-high type1))
2048 (low2 (numeric-type-low type2))
2049 (high2 (numeric-type-high type2)))
2050 ;; If one is complex and the other isn't, they are disjoint.
2051 (cond ((not (or (eq (numeric-type-complexp type1) complexp2)
2052 (null complexp2)))
2053 (values nil t))
2054 ;; If the classes are specified and different, the types are
2055 ;; disjoint unless type2 is rational and type1 is integer.
2056 ;; [ or type1 is INTEGER and type2 is of the form (RATIONAL
2057 ;; X X) for integral X, but this is dealt with in the
2058 ;; canonicalization inside MAKE-NUMERIC-TYPE ]
2059 ((not (or (eq class1 class2)
2060 (null class2)
2061 (and (eq class1 'integer) (eq class2 'rational))))
2062 (values nil t))
2063 ;; If the float formats are specified and different, the types
2064 ;; are disjoint.
2065 ((not (or (eq (numeric-type-format type1) format2)
2066 (null format2)))
2067 (values nil t))
2068 ;; Check the bounds.
2069 ((and (numeric-bound-test low1 low2 >= >)
2070 (numeric-bound-test high1 high2 <= <))
2071 (values t t))
2072 (t
2073 (values nil t)))))
2074
2075 (define-superclasses number (generic-number))
2076
2077 ;;; NUMERIC-TYPES-ADJACENT -- Internal
2078 ;;;
2079 ;;; If the high bound of Low is adjacent to the low bound of High, then
2080 ;;; return True, otherwise NIL.
2081 ;;;
2082 (defun numeric-types-adjacent (low high)
2083 (let ((low-bound (numeric-type-high low))
2084 (high-bound (numeric-type-low high)))
2085 (cond ((not (and low-bound high-bound)) nil)
2086 ((and (consp low-bound) (consp high-bound)) nil)
2087 ((consp low-bound)
2088 (let ((low-value (car low-bound)))
2089 (or (eql low-value high-bound)
2090 (and (eql low-value -0f0) (eql high-bound 0f0))
2091 (and (eql low-value 0f0) (eql high-bound -0f0))
2092 (and (eql low-value -0d0) (eql high-bound 0d0))
2093 (and (eql low-value 0d0) (eql high-bound -0d0)))))
2094 ((consp high-bound)
2095 (let ((high-value (car high-bound)))
2096 (or (eql high-value low-bound)
2097 (and (eql high-value -0f0) (eql low-bound 0f0))
2098 (and (eql high-value 0f0) (eql low-bound -0f0))
2099 (and (eql high-value -0d0) (eql low-bound 0d0))
2100 (and (eql high-value 0d0) (eql low-bound -0d0)))))
2101 ((and (eq (numeric-type-class low) 'integer)
2102 (eq (numeric-type-class high) 'integer))
2103 (eql (1+ low-bound) high-bound))
2104 (t
2105 nil))))
2106
2107
2108 ;;; NUMBER :SIMPLE-UNION method -- Internal
2109 ;;;
2110 ;;; Return the a numeric type that is a supertype for both type1 and type2.
2111 ;;;
2112 ;;; ### Note: we give up early, so keep from dropping lots of information on
2113 ;;; the floor by returning overly general types.
2114 ;;;
2115 (define-type-method (number :simple-union) (type1 type2)
2116 (declare (type numeric-type type1 type2))
2117 (cond ((csubtypep type1 type2) type2)
2118 ((csubtypep type2 type1) type1)
2119 (t
2120 (let ((class1 (numeric-type-class type1))
2121 (format1 (numeric-type-format type1))
2122 (complexp1 (numeric-type-complexp type1))
2123 (class2 (numeric-type-class type2))
2124 (format2 (numeric-type-format type2))
2125 (complexp2 (numeric-type-complexp type2)))
2126 (cond
2127 ((and (eq class1 class2)
2128 (eq format1 format2)
2129 (eq complexp1 complexp2)
2130 (or (numeric-types-intersect type1 type2)
2131 (numeric-types-adjacent type1 type2)
2132 (numeric-types-adjacent type2 type1)))
2133 (make-numeric-type
2134 :class class1
2135 :format format1
2136 :complexp complexp1
2137 :low (numeric-bound-max (numeric-type-low type1)
2138 (numeric-type-low type2)
2139 <= < t)
2140 :high (numeric-bound-max (numeric-type-high type1)
2141 (numeric-type-high type2)
2142 >= > t)))
2143 ;; FIXME: These two clauses are almost identical, and the
2144 ;; consequents are in fact identical in every respect.
2145 ((and (eq class1 'rational)
2146 (eq class2 'integer)
2147 (eq format1 format2)
2148 (eq complexp1 complexp2)
2149 (integerp (numeric-type-low type2))
2150 (integerp (numeric-type-high type2))
2151 (= (numeric-type-low type2) (numeric-type-high type2))
2152 (or (numeric-types-adjacent type1 type2)
2153 (numeric-types-adjacent type2 type1)))
2154 (make-numeric-type
2155 :class 'rational
2156 :format format1
2157 :complexp complexp1
2158 :low (numeric-bound-max (numeric-type-low type1)
2159 (numeric-type-low type2)
2160 <= < t)
2161 :high (numeric-bound-max (numeric-type-high type1)
2162 (numeric-type-high type2)
2163 >= > t)))
2164 ((and (eq class1 'integer)
2165 (eq class2 'rational)
2166 (eq format1 format2)
2167 (eq complexp1 complexp2)
2168 (integerp (numeric-type-low type1))
2169 (integerp (numeric-type-high type1))
2170 (= (numeric-type-low type1) (numeric-type-high type1))
2171 (or (numeric-types-adjacent type1 type2)
2172 (numeric-types-adjacent type2 type1)))
2173 (make-numeric-type
2174 :class 'rational
2175 :format format1
2176 :complexp complexp1
2177 :low (numeric-bound-max (numeric-type-low type1)
2178 (numeric-type-low type2)
2179 <= < t)
2180 :high (numeric-bound-max (numeric-type-high type1)
2181 (numeric-type-high type2)
2182 >= > t)))
2183 (t nil))))))
2184
2185 (cold-load-init
2186 (setf (info type kind 'number) :primitive)
2187 (setf (info type builtin 'number)
2188 (make-numeric-type :complexp nil)))
2189
2190 (def-type-translator complex (&optional (typespec '*))
2191 (labels ((not-numeric ()
2192 (error (intl:gettext "The component type for COMPLEX is not numeric: ~S")
2193 typespec))
2194 (not-real ()
2195 (error (intl:gettext "The component type for COMPLEX is not real: ~S")
2196 typespec))
2197 (complex1 (component-type)
2198 (unless (numeric-type-p component-type)
2199 (not-numeric))
2200 (when (eq (numeric-type-complexp component-type) :complex)
2201 (not-real))
2202 (modified-numeric-type component-type :complexp :complex))
2203 (complex-union (component)
2204 (unless (numberp component)
2205 (not-numeric))
2206 ;; KLUDGE: This TYPECASE more or less does
2207 ;; (UPGRADED-COMPLEX-PART-TYPE (TYPE-OF COMPONENT)),
2208 ;; (plus a small hack to treat (EQL COMPONENT 0) specially)
2209 ;; but uses logic cut and pasted from the DEFUN of
2210 ;; UPGRADED-COMPLEX-PART-TYPE. That's fragile, because
2211 ;; changing the definition of UPGRADED-COMPLEX-PART-TYPE
2212 ;; would tend to break the code here. Unfortunately,
2213 ;; though, reusing UPGRADED-COMPLEX-PART-TYPE here
2214 ;; would cause another kind of fragility, because
2215 ;; ANSI's definition of TYPE-OF is so weak that e.g.
2216 ;; (UPGRADED-COMPLEX-PART-TYPE (TYPE-OF 1/2)) could
2217 ;; end up being (UPGRADED-COMPLEX-PART-TYPE 'REAL)
2218 ;; instead of (UPGRADED-COMPLEX-PART-TYPE 'RATIONAL).
2219 ;; So using TYPE-OF would mean that ANSI-conforming
2220 ;; maintenance changes in TYPE-OF could break the code here.
2221 ;; It's not clear how best to fix this. -- WHN 2002-01-21,
2222 ;; trying to summarize CSR's concerns in his patch
2223 (typecase component
2224 (complex (error (intl:gettext "The component type for COMPLEX (EQL X) ~
2225 is complex: ~S")
2226 component))
2227 ((eql 0) (specifier-type nil)) ; as required by ANSI
2228 (single-float (specifier-type '(complex single-float)))
2229 (double-float (specifier-type '(complex double-float)))
2230 #+long-float
2231 (long-float (specifier-type '(complex long-float)))
2232 #+double-double
2233 (double-double-float (specifier-type '(complex double-double-float)))
2234 (rational (specifier-type '(complex rational)))
2235 (t (specifier-type '(complex real))))))
2236 (let ((ctype (specifier-type typespec)))
2237 (typecase ctype
2238 (numeric-type
2239 (if (csubtypep ctype (specifier-type 'rational))
2240 (complex1 (specifier-type 'rational))
2241 (complex1 ctype)))
2242 (union-type (apply #'type-union
2243 ;; FIXME: This code could suffer from
2244 ;; (admittedly very obscure) cases of
2245 ;; bug 145 e.g. when TYPE is
2246 ;; (OR (AND INTEGER (SATISFIES ODDP))
2247 ;; (AND FLOAT (SATISFIES FOO))
2248 ;; and not even report the problem very well.
2249 (mapcar #'complex1
2250 (union-type-types ctype))))
2251 (member-type
2252 ;; MEMBER-TYPE is almost the same as UNION-TYPE, but there's
2253 ;; a gotcha: (COMPLEX (EQL 0)) is unclear to me (rtoy). For
2254 ;; now if the typespec is a subtype of rational, we create
2255 ;; (COMPLEX RATIONAL).
2256 (if (csubtypep ctype (specifier-type 'rational))
2257 (complex1 (specifier-type 'rational))
2258 (apply #'type-union
2259 (mapcar #'complex-union
2260 (member-type-members ctype)))))
2261 (named-type
2262 (cond ((eq (named-type-name ctype) '*)
2263 ;; (COMPLEX *) is the same as (COMPLEX REAL) for us.
2264 (apply #'type-union
2265 (mapcar #'complex1
2266 (union-type-types (specifier-type 'real)))))
2267 ((eq (named-type-name ctype) nil)
2268 ;; (COMPLEX NIL) is the NIL type
2269 *empty-type*)
2270 (t
2271 (not-real))))
2272 (t
2273 (multiple-value-bind (subtypep certainly)
2274 (csubtypep ctype (specifier-type 'real))
2275 (if (and (not subtypep) certainly)
2276 (not-real)
2277 ;; ANSI just says that TYPESPEC is any subtype of
2278 ;; type REAL, not necessarily a NUMERIC-TYPE. In
2279 ;; particular, at this point TYPESPEC could legally be
2280 ;; an intersection type like (AND REAL (SATISFIES ODDP)),
2281 ;; in which case we fall through the logic above and
2282 ;; end up here, stumped.
2283 (error (intl:gettext "~@<(known bug #145): The type ~S is too hairy to be
2284 used for a COMPLEX component.~:@>")
2285 typespec))))))))
2286
2287 ;;; Check-Bound -- Internal
2288 ;;;
2289 ;;; Check that X is a well-formed numeric bound of the specified Type.
2290 ;;; If X is *, return NIL, otherwise return the bound.
2291 ;;;
2292 (defmacro check-bound (x type)
2293 `(cond ((eq ,x '*) nil)
2294 ((or (typep ,x ',type)
2295 (and (consp ,x) (typep (car ,x) ',type) (null (cdr ,x))))
2296 ,x)
2297 (t
2298 (simple-program-error (intl:gettext "Bound is not *, a ~A or a list of a ~A: ~S")
2299 ',type ',type ,x))))
2300
2301 (def-type-translator integer (&optional low high)
2302 (let* ((l (check-bound low integer))
2303 (lb (if (consp l) (1+ (car l)) l))
2304 (h (check-bound high integer))
2305 (hb (if (consp h) (1- (car h)) h)))
2306 (if (and hb lb (< hb lb))
2307 ;; This used to signal an error when the lb > hb, but the CLHS
2308 ;; doesn't say that this is an error, so we silently accept it
2309 ;; (as the empty type).
2310 *empty-type*
2311 (make-numeric-type :class 'integer :complexp :real
2312 :enumerable (not (null (and l h)))
2313 :low lb
2314 :high hb))))
2315
2316 (defmacro def-bounded-type (type class format)
2317 `(def-type-translator ,type (&optional low high)
2318 (let ((lb (check-bound low ,type))
2319 (hb (check-bound high ,type)))
2320 ;; We used to signal an error here if the lower bound was
2321 ;; greater then the upper, but the CLHS doesn't say we should,
2322 ;; so we silently accept it as the empty type.
2323 (if (numeric-bound-test* lb hb <= <)
2324 (make-numeric-type :class ',class :format ',format :low lb :high hb)
2325 *empty-type*))))
2326
2327 (def-bounded-type rational rational nil)
2328
2329
2330 (deftype mod (n)
2331 (unless (and (integerp n) (> n 0))
2332 (simple-program-error (intl:gettext "Bad N specified for MOD type specifier: ~S.") n))
2333 `(integer 0 ,(1- n)))
2334
2335 (deftype signed-byte (&optional s)
2336 (cond ((eq s '*) 'integer)
2337 ((and (integerp s) (> s 0))
2338 (let ((bound (ash 1 (1- s))))
2339 `(integer ,(- bound) ,(1- bound))))
2340 (t
2341 (simple-program-error
2342 (intl:gettext "Bad size specified for SIGNED-BYTE type specifier: ~S.") s))))
2343
2344 (deftype unsigned-byte (&optional s)
2345 (cond ((eq s '*) '(integer 0))
2346 ((and (integerp s) (> s 0))
2347 `(integer 0 ,(1- (ash 1 s))))
2348 (t
2349 (simple-program-error
2350 (intl:gettext "Bad size specified for UNSIGNED-BYTE type specifier: ~S.") s))))
2351
2352
2353 ;;; Unlike CMU CL, we represent the types FLOAT and REAL as
2354 ;;; UNION-TYPEs of more primitive types, in order to make
2355 ;;; type representation more unique, avoiding problems in the
2356 ;;; simplification of things like
2357 ;;; (subtypep '(or (single-float -1.0 1.0) (single-float 0.1))
2358 ;;; '(or (real -1 7) (single-float 0.1) (single-float -1.0 1.0)))
2359 ;;; When we allowed REAL to remain as a separate NUMERIC-TYPE,
2360 ;;; it was too easy for the first argument to be simplified to
2361 ;;; '(SINGLE-FLOAT -1.0), and for the second argument to be simplified
2362 ;;; to '(OR (REAL -1 7) (SINGLE-FLOAT 0.1)) and then for the
2363 ;;; SUBTYPEP to fail (returning NIL,T instead of T,T) because
2364 ;;; the first argument can't be seen to be a subtype of any of the
2365 ;;; terms in the second argument.
2366 ;;;
2367 ;;; The old CMU CL way was:
2368 ;;; (!def-bounded-type float float nil)
2369 ;;; (!def-bounded-type real nil nil)
2370 ;;;
2371 ;;; FIXME: If this new way works for a while with no weird new
2372 ;;; problems, we can go back and rip out support for separate FLOAT
2373 ;;; and REAL flavors of NUMERIC-TYPE. The new way was added in
2374 ;;; sbcl-0.6.11.22, 2001-03-21.
2375 ;;;
2376 ;;; FIXME: It's probably necessary to do something to fix the
2377 ;;; analogous problem with INTEGER and RATIONAL types. Perhaps
2378 ;;; bounded RATIONAL types should be represented as (OR RATIO INTEGER).
2379 (defun coerce-bound (bound type inner-coerce-bound-fun)
2380 (declare (type function inner-coerce-bound-fun))
2381 (cond ((eql bound '*)
2382 bound)
2383 ((consp bound)
2384 (destructuring-bind (inner-bound) bound
2385 (list (funcall inner-coerce-bound-fun inner-bound type))))
2386 (t
2387 (funcall inner-coerce-bound-fun bound type))))
2388
2389 (defun inner-coerce-real-bound (bound type)
2390 (ecase type
2391 (rational (rationalize bound))
2392 (float (if (floatp bound)
2393 bound
2394 ;; Coerce to the widest float format available, to
2395 ;; avoid unnecessary loss of precision:
2396 (coerce bound #-double-double 'long-float
2397 #+double-double 'double-double-float)))))
2398
2399 (defun coerced-real-bound (bound type)
2400 (coerce-bound bound type #'inner-coerce-real-bound))
2401
2402 (defun inner-coerce-float-bound (bound type)
2403 (if (and (floatp bound) (not (typep bound type)))
2404 ;; Return NIL if we can't coerce the floating-point bound to the
2405 ;; given type. Typically, a large number that won't fit in a
2406 ;; single-float.
2407 (ignore-errors (coerce bound type))
2408 (coerce bound type)))
2409
2410 (defun coerced-float-bound (bound type)
2411 (let ((bound (coerce-bound bound type #'inner-coerce-float-bound)))
2412 ;; If the resulting bound is NIL or '(NIL), convert that to '* to
2413 ;; mean unbounded.
2414 (if (and (listp bound)
2415 (or (null bound)
2416 (null (car bound))))
2417 '*
2418 bound)))
2419
2420 (def-type-translator real (&optional (low '*) (high '*))
2421 (specifier-type `(or (float ,(coerced-real-bound low 'float)
2422 ,(coerced-real-bound high 'float))
2423 (rational ,(coerced-real-bound low 'rational)
2424 ,(coerced-real-bound high 'rational)))))
2425
2426 (def-type-translator float (&optional (low '*) (high '*))
2427 (check-bound low float)
2428 (check-bound high float)
2429 (specifier-type
2430 `(or (single-float ,(coerced-float-bound low 'single-float)
2431 ,(coerced-float-bound high 'single-float))
2432 (double-float ,(coerced-float-bound low 'double-float)
2433 ,(coerced-float-bound high 'double-float))
2434 #+long-float ,(error "stub: no long float support yet")
2435 #+double-double
2436 (double-double-float
2437 ,(coerced-float-bound low 'double-double-float)
2438 ,(coerced-float-bound high 'double-double-float)))))
2439
2440 (defmacro define-float-format (f)
2441 `(def-bounded-type ,f float ,f))
2442
2443 (define-float-format short-float)
2444 (define-float-format single-float)
2445 (define-float-format double-float)
2446 (define-float-format long-float)
2447 #+double-double
2448 (define-float-format double-double-float)
2449
2450 (defun numeric-types-intersect (type1 type2)
2451 (declare (type numeric-type type1 type2))
2452 (let* ((class1 (numeric-type-class type1))
2453 (class2 (numeric-type-class type2))
2454 (complexp1 (numeric-type-complexp type1))
2455 (complexp2 (numeric-type-complexp type2))
2456 (format1 (numeric-type-format type1))
2457 (format2 (numeric-type-format type2))
2458 (low1 (numeric-type-low type1))
2459 (high1 (numeric-type-high type1))
2460 (low2 (numeric-type-low type2))
2461 (high2 (numeric-type-high type2)))
2462 ;; If one is complex and the other isn't, then they are disjoint.
2463 (cond ((not (or (eq complexp1 complexp2)
2464 (null complexp1) (null complexp2)))
2465 nil)
2466 ;; If either type is a float, then the other must either be
2467 ;; specified to be a float or unspecified. Otherwise, they
2468 ;; are disjoint.
2469 ((and (eq class1 'float)
2470 (not (member class2 '(float nil)))) nil)
2471 ((and (eq class2 'float)
2472 (not (member class1 '(float nil)))) nil)
2473 ;; If the float formats are specified and different, the
2474 ;; types are disjoint.
2475 ((not (or (eq format1 format2) (null format1) (null format2)))
2476 nil)
2477 (t
2478 ;; Check the bounds. This is a bit odd because we must
2479 ;; always have the outer bound of the interval as the
2480 ;; second arg.
2481 (if (numeric-bound-test high1 high2 <= <)
2482 (or (and (numeric-bound-test low1 low2 >= >)
2483 (numeric-bound-test* low1 high2 <= <))
2484 (and (numeric-bound-test low2 low1 >= >)
2485 (numeric-bound-test* low2 high1 <= <)))
2486 (or (and (numeric-bound-test* low2 high1 <= <)
2487 (numeric-bound-test low2 low1 >= >))
2488 (and (numeric-bound-test high2 high1 <= <)
2489 (numeric-bound-test* high2 low1 >= >))))))))
2490
2491 ;;; Round-Numeric-Bound -- Internal
2492 ;;;
2493 ;;; Take the numeric bound X and convert it into something that can be used
2494 ;;; as a bound in a numeric type with the specified Class and Format. If up-p
2495 ;;; is true, then we round up as needed, otherwise we round down. Up-p true
2496 ;;; implies that X is a lower bound, i.e. (N) > N.
2497 ;;;
2498 ;;; This is used by Numeric-Type-Intersection to mash the bound into the
2499 ;;; appropriate type number. X may only be a float when Class is Float.
2500 ;;;
2501 ;;; ### Note: it is possible for the coercion to a float to overflow or
2502 ;;; underflow. This happens when the bound doesn't fit in the specified
2503 ;;; format. In this case, we should really return the appropriate
2504 ;;; {Most | Least}-{Positive | Negative}-XXX-Float float of desired format.
2505 ;;; But these conditions aren't currently signalled in any useful way.
2506 ;;;
2507 ;;; Also, when converting an open rational bound into a float we should
2508 ;;; probably convert it to a closed bound of the closest float in the specified
2509 ;;; format. In general, open float bounds are fucked.
2510 ;;;
2511 (defun round-numeric-bound (x class format up-p)
2512 (if x
2513 (let ((cx (if (consp x) (car x) x)))
2514 (ecase class
2515 ((nil rational) x)
2516 (integer
2517 (if (and (consp x) (integerp cx))
2518 (if up-p (1+ cx) (1- cx))
2519 (if up-p (ceiling cx) (floor cx))))
2520 (float
2521 (let ((res (if format (coerce cx format) (float cx))))
2522 (if (consp x) (list res) res)))))
2523 nil))
2524
2525
2526 ;;; Number :Simple-Intersection type method -- Internal
2527 ;;;
2528 ;;; Handle the case of Type-Intersection on two numeric types. We use
2529 ;;; Types-Intersect to throw out the case of types with no intersection. If an
2530 ;;; attribute in Type1 is unspecified, then we use Type2's attribute, which
2531 ;;; must be at least as restrictive. If the types intersect, then the only
2532 ;;; attributes that can be specified and different are the class and the
2533 ;;; bounds.
2534 ;;;
2535 ;;; When the class differs, we use the more restrictive class. The only
2536 ;;; interesting case is rational/integer, since rational includes integer.
2537 ;;;
2538 ;;; We make the result lower (upper) bound the maximum (minimum) of the
2539 ;;; argument lower (upper) bounds. We convert the bounds into the
2540 ;;; appropriate numeric type before maximizing. This avoids possible confusion
2541 ;;; due to mixed-type comparisons (but I think the result is the same).
2542 ;;;
2543 (define-type-method (number :simple-intersection) (type1 type2)
2544 (declare (type numeric-type type1 type2))
2545 (if (numeric-types-intersect type1 type2)
2546 (let* ((class1 (numeric-type-class type1))
2547 (class2 (numeric-type-class type2))
2548 (class (ecase class1
2549 ((nil) class2)
2550 ((integer float) class1)
2551 (rational (if (eq class2 'integer)
2552 'integer
2553 'rational))))
2554 (format (or (numeric-type-format type1)
2555 (numeric-type-format type2))))
2556 (make-numeric-type
2557 :class class
2558 :format format
2559 :complexp (or (numeric-type-complexp type1)
2560 (numeric-type-complexp type2))
2561 :low (numeric-bound-max
2562 (round-numeric-bound (numeric-type-low type1)
2563 class format t)
2564 (round-numeric-bound (numeric-type-low type2)
2565 class format t)
2566 > >= nil)
2567 :high (numeric-bound-max
2568 (round-numeric-bound (numeric-type-high type1)
2569 class format nil)
2570 (round-numeric-bound (numeric-type-high type2)
2571 class format nil)
2572 < <= nil)))
2573 *empty-type*))
2574
2575 ;;; Float-Format-Max -- Interface
2576 ;;;
2577 ;;; Given two float formats, return the one with more precision. If either
2578 ;;; one is null, return NIL.
2579 ;;;
2580 (defun float-format-max (f1 f2)
2581 (when (and f1 f2)
2582 (dolist (f float-formats (error (intl:gettext "Bad float format: ~S.") f1))
2583 (when (or (eq f f1) (eq f f2))
2584 (return f)))))
2585
2586
2587 ;;; Numeric-Contagion -- Interface
2588