subtypep.lisp 32.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
;;; subtypep.lisp
;;;
;;; Copyright (C) 2003-2005 Peter Graves
;;; $Id$
;;;
;;; This program is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License
;;; as published by the Free Software Foundation; either version 2
;;; of the License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
;;;
;;; As a special exception, the copyright holders of this library give you
;;; permission to link this library with independent modules to produce an
;;; executable, regardless of the license terms of these independent
;;; modules, and to copy and distribute the resulting executable under
;;; terms of your choice, provided that you also meet, for each linked
;;; independent module, the terms and conditions of the license of that
;;; module.  An independent module is a module which is not derived from
;;; or based on this library.  If you modify this library, you may extend
;;; this exception to your version of the library, but you are not
;;; obligated to do so.  If you do not wish to do so, delete this
;;; exception statement from your version.

(in-package #:system)

(defparameter *known-types* (make-hash-table :test 'eq))

(defun initialize-known-types ()
  (let ((ht (make-hash-table :test 'eq)))
    (dolist (i '((ARITHMETIC-ERROR ERROR)
                 (ARRAY)
                 (BASE-STRING STRING)
                 (BIGNUM INTEGER)
                 (BIT FIXNUM)
                 (BIT-VECTOR VECTOR)
                 (BOOLEAN SYMBOL)
                 (BUILT-IN-CLASS CLASS)
                 (CELL-ERROR ERROR)
                 (CHARACTER)
                 (CLASS STANDARD-OBJECT)
                 (COMPILED-FUNCTION FUNCTION)
                 (COMPLEX NUMBER)
                 (CONDITION)
                 (CONS LIST)
                 (CONTROL-ERROR ERROR)
                 (DIVISION-BY-ZERO ARITHMETIC-ERROR)
                 (DOUBLE-FLOAT FLOAT)
                 (END-OF-FILE STREAM-ERROR)
                 (ERROR SERIOUS-CONDITION)
                 (EXTENDED-CHAR CHARACTER NIL)
                 (FILE-ERROR ERROR)
                 (FIXNUM INTEGER)
                 (FLOAT REAL)
                 (FLOATING-POINT-INEXACT ARITHMETIC-ERROR)
                 (FLOATING-POINT-INVALID-OPERATION ARITHMETIC-ERROR)
                 (FLOATING-POINT-OVERFLOW ARITHMETIC-ERROR)
                 (FLOATING-POINT-UNDERFLOW ARITHMETIC-ERROR)
                 (FUNCTION)
                 (GENERIC-FUNCTION FUNCTION)
                 (HASH-TABLE)
                 (INTEGER RATIONAL)
                 (KEYWORD SYMBOL)
                 (LIST SEQUENCE)
                 (LONG-FLOAT FLOAT)
                 (NIL-VECTOR SIMPLE-STRING)
                 (NULL BOOLEAN LIST)
                 (NUMBER)
                 (PACKAGE)
                 (PACKAGE-ERROR ERROR)
                 (PARSE-ERROR ERROR)
                 (PATHNAME)
                 (PRINT-NOT-READABLE ERROR)
                 (PROGRAM-ERROR ERROR)
                 (RANDOM-STATE)
                 (RATIO RATIONAL)
                 (RATIONAL REAL)
                 (READER-ERROR PARSE-ERROR STREAM-ERROR)
                 (READTABLE)
                 (REAL NUMBER)
                 (RESTART)
                 (SERIOUS-CONDITION CONDITION)
                 (SHORT-FLOAT FLOAT)
                 (SIMPLE-ARRAY ARRAY)
                 (SIMPLE-BASE-STRING SIMPLE-STRING BASE-STRING)
                 (SIMPLE-BIT-VECTOR BIT-VECTOR SIMPLE-ARRAY)
                 (SIMPLE-CONDITION CONDITION)
                 (SIMPLE-ERROR SIMPLE-CONDITION ERROR)
96
                 (SIMPLE-STRING BASE-STRING STRING SIMPLE-ARRAY)
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
                 (SIMPLE-TYPE-ERROR SIMPLE-CONDITION TYPE-ERROR)
                 (SIMPLE-VECTOR VECTOR SIMPLE-ARRAY)
                 (SIMPLE-WARNING SIMPLE-CONDITION WARNING)
                 (SINGLE-FLOAT FLOAT)
                 (STANDARD-CHAR CHARACTER)
                 (STANDARD-CLASS CLASS)
                 (STANDARD-GENERIC-FUNCTION GENERIC-FUNCTION)
                 (STANDARD-OBJECT)
                 (STORAGE-CONDITION SERIOUS-CONDITION)
                 (STREAM)
                 (STREAM-ERROR ERROR)
                 (STRING VECTOR)
                 (STRUCTURE-CLASS CLASS STANDARD-OBJECT)
                 (STYLE-WARNING WARNING)
                 (SYMBOL)
                 (TWO-WAY-STREAM STREAM)
                 (TYPE-ERROR ERROR)
                 (UNBOUND-SLOT CELL-ERROR)
                 (UNBOUND-VARIABLE CELL-ERROR)
                 (UNDEFINED-FUNCTION CELL-ERROR)
                 (VECTOR ARRAY SEQUENCE)
                 (WARNING CONDITION)))
    (setf (gethash (%car i) ht) (%cdr i)))
    (setf *known-types* ht)))

(initialize-known-types)

(defun known-type-p (type)
  (multiple-value-bind (value present-p) (gethash type *known-types*)
    present-p))

(defun sub-interval-p (i1 i2)
  (let (low1 high1 low2 high2)
    (if (null i1)
        (setq low1 '* high1 '*)
        (if (null (cdr i1))
            (setq low1 (car i1) high1 '*)
            (setq low1 (car i1) high1 (cadr i1))))
    (if (null i2)
        (setq low2 '* high2 '*)
        (if (null (cdr i2))
            (setq low2 (car i2) high2 '*)
            (setq low2 (car i2) high2 (cadr i2))))
    (when (and (consp low1) (integerp (%car low1)))
      (setq low1 (1+ (car low1))))
    (when (and (consp low2) (integerp (%car low2)))
      (setq low2 (1+ (car low2))))
    (when (and (consp high1) (integerp (%car high1)))
      (setq high1 (1- (car high1))))
    (when (and (consp high2) (integerp (%car high2)))
      (setq high2 (1- (car high2))))
    (cond ((eq low1 '*)
	   (unless (eq low2 '*)
	           (return-from sub-interval-p nil)))
          ((eq low2 '*))
	  ((consp low1)
	   (if (consp low2)
	       (when (< (%car low1) (%car low2))
		     (return-from sub-interval-p nil))
	       (when (< (%car low1) low2)
		     (return-from sub-interval-p nil))))
	  ((if (consp low2)
	       (when (<= low1 (%car low2))
		     (return-from sub-interval-p nil))
	       (when (< low1 low2)
		     (return-from sub-interval-p nil)))))
    (cond ((eq high1 '*)
	   (unless (eq high2 '*)
	           (return-from sub-interval-p nil)))
          ((eq high2 '*))
	  ((consp high1)
	   (if (consp high2)
	       (when (> (%car high1) (%car high2))
		     (return-from sub-interval-p nil))
	       (when (> (%car high1) high2)
		     (return-from sub-interval-p nil))))
	  ((if (consp high2)
	       (when (>= high1 (%car high2))
		     (return-from sub-interval-p nil))
	       (when (> high1 high2)
		     (return-from sub-interval-p nil)))))
    (return-from sub-interval-p t)))

(defun dimension-subtypep (dim1 dim2)
  (cond ((eq dim2 '*)
         t)
        ((equal dim1 dim2)
         t)
        ((integerp dim2)
         (and (listp dim1) (= (length dim1) dim2)))
        ((eql dim1 0)
         (null dim2))
        ((integerp dim1)
         (and (consp dim2)
              (= (length dim2) dim1)
              (equal dim2 (make-list dim1 :initial-element '*))))
        ((and (consp dim1) (consp dim2) (= (length dim1) (length dim2)))
         (do* ((list1 dim1 (cdr list1))
               (list2 dim2 (cdr list2))
               (e1 (car list1) (car list1))
               (e2 (car list2) (car list2)))
              ((null list1) t)
           (unless (or (eq e2 '*) (eql e1 e2))
              (return nil))))
        (t
         nil)))

(defun simple-subtypep (type1 type2)
  (if (eq type1 type2)
      t
      (multiple-value-bind (type1-supertypes type1-known-p)
          (gethash type1 *known-types*)
        (if type1-known-p
            (if (memq type2 type1-supertypes)
                t
                (dolist (supertype type1-supertypes)
                  (when (simple-subtypep supertype type2)
                    (return t))))
            nil))))

;; (defstruct ctype
;;   ((:constructor make-ctype (super type)))
;;   super
;;   type)

(defun make-ctype (super type)
  (cons super type))

(defun ctype-super (ctype)
  (car ctype))

(defun ctype-type (ctype)
  (cdr ctype))

(defun ctype (type)
  (cond ((classp type)
         nil)
        (t
         (let ((tp (if (atom type) type (car type))))
           (case tp
             ((ARRAY VECTOR STRING SIMPLE-ARRAY SIMPLE-STRING BASE-STRING
               SIMPLE-BASE-STRING BIT-VECTOR SIMPLE-BIT-VECTOR NIL-VECTOR)
              (make-ctype 'ARRAY type))
             ((REAL INTEGER BIT FIXNUM SIGNED-BYTE UNSIGNED-BYTE BIGNUM RATIO
               FLOAT SINGLE-FLOAT DOUBLE-FLOAT SHORT-FLOAT LONG-FLOAT)
              (make-ctype 'REAL type))
             (COMPLEX
              (make-ctype 'COMPLEX
                          (if (atom type) '* (cadr type))))
             (FUNCTION
              (make-ctype 'FUNCTION type)))))))

(defun csubtypep-array (ct1 ct2)
  (let ((type1 (normalize-type (ctype-type ct1)))
        (type2 (normalize-type (ctype-type ct2))))
  (when (eq type1 type2)
    (return-from csubtypep-array (values t t)))
  (let (t1 t2 i1 i2)
    (if (atom type1)
        (setf t1 type1 i1 nil)
        (setf t1 (car type1) i1 (cdr type1)))
    (if (atom type2)
        (setf t2 type2 i2 nil)
        (setf t2 (car type2) i2 (cdr type2)))
    (cond ((and (classp t1) (eq (%class-name t1) 'array) (eq t2 'array))
           (values (equal i2 '(* *)) t))
          ((and (memq t1 '(array simple-array)) (eq t2 'array))
           (let ((e1 (car i1))
                 (e2 (car i2))
                 (d1 (cadr i1))
                 (d2 (cadr i2)))
             (cond ((and (eq e2 '*) (eq d2 '*))
                    (values t t))
                   ((or (eq e2 '*)
                        (equal e1 e2)
                        (equal (upgraded-array-element-type e1)
                               (upgraded-array-element-type e2)))
                    (values (dimension-subtypep d1 d2) t))
                   (t
                    (values nil t)))))
          ((and (memq t1 '(simple-base-string base-string simple-string string nil-vector))
                (memq t2 '(simple-base-string base-string simple-string string nil-vector)))
           (if (and (simple-subtypep t1 t2)
                    (or (eql (car i1) (car i2))
                        (eq (car i2) '*)))
               (return-from csubtypep-array (values t t))
               (return-from csubtypep-array (values nil t))))
          ((and (memq t1 '(array simple-array)) (eq t2 'string))
           (let ((element-type (car i1))
                 (dim (cadr i1))
                 (size (car i2)))
             (unless (%subtypep element-type 'character)
               (return-from csubtypep-array (values nil t)))
             (when (integerp size)
               (if (and (consp dim) (= (length dim) 1) (eql (%car dim) size))
                   (return-from csubtypep-array (values t t))
                   (return-from csubtypep-array (values nil t))))
             (when (or (null size) (eql size '*))
               (if (or (eql dim 1)
                       (and (consp dim) (= (length dim) 1)))
                   (return-from csubtypep-array (values t t))
                   (return-from csubtypep-array (values nil t))))))
          ((and (eq t1 'simple-array) (eq t2 'simple-string))
           (let ((element-type (car i1))
                 (dim (cadr i1))
                 (size (car i2)))
             (unless (%subtypep element-type 'character)
               (return-from csubtypep-array (values nil t)))
             (when (integerp size)
               (if (and (consp dim) (= (length dim) 1) (eql (%car dim) size))
                   (return-from csubtypep-array (values t t))
                   (return-from csubtypep-array (values nil t))))
             (when (or (null size) (eql size '*))
               (if (or (eql dim 1)
                       (and (consp dim) (= (length dim) 1)))
                   (return-from csubtypep-array (values t t))
                   (return-from csubtypep-array (values nil t))))))
          ((and (memq t1 '(string simple-string nil-vector)) (eq t2 'array))
           (let ((element-type (car i2))
                 (dim (cadr i2))
                 (size (car i1)))
             (unless (eq element-type '*)
               (return-from csubtypep-array (values nil t)))
             (when (integerp size)
               (if (or (eq dim '*)
                       (eql dim 1)
                       (and (consp dim)
                            (= (length dim) 1)
                            (or (eq (%car dim) '*)
                                (eql (%car dim) size))))
                   (return-from csubtypep-array (values t t))
                   (return-from csubtypep-array (values nil t))))
             (when (or (null size) (eql size '*))
               (if (or (eq dim '*)
                       (eql dim 1)
                       (and (consp dim) (= (length dim) 1)))
                   (return-from csubtypep-array (values t t))
                   (return-from csubtypep-array (values nil t))))))
          ((and (memq t1 '(bit-vector simple-bit-vector)) (eq t2 'array))
           (let ((element-type (car i2))
                 (dim (cadr i2))
                 (size (car i1)))
             (unless (or (memq element-type '(bit *))
                         (equal element-type '(integer 0 1)))
               (return-from csubtypep-array (values nil t)))
             (when (integerp size)
               (if (or (eq dim '*)
                       (eql dim 1)
                       (and (consp dim)
                            (= (length dim) 1)
                            (or (eq (%car dim) '*)
                                (eql (%car dim) size))))
                   (return-from csubtypep-array (values t t))
                   (return-from csubtypep-array (values nil t))))
             (when (or (null size) (eql size '*))
               (if (or (eq dim '*)
                       (eql dim 1)
                       (and (consp dim) (= (length dim) 1)))
                   (return-from csubtypep-array (values t t))
                   (return-from csubtypep-array (values nil t))))))
          ((eq t2 'simple-array)
           (case t1
             (simple-array
              (let ((e1 (car i1))
                    (e2 (car i2))
                    (d1 (cadr i1))
                    (d2 (cadr i2)))
                (cond ((and (eq e2 '*) (eq d2 '*))
                       (values t t))
                      ((or (eq e2 '*)
                           (equal e1 e2)
                           (equal (upgraded-array-element-type e1)
                                  (upgraded-array-element-type e2)))
                       (values (dimension-subtypep d1 d2) t))
                      (t
                       (values nil t)))))
             ((simple-string simple-bit-vector nil-vector)
              (let ((element-type (car i2))
                    (dim (cadr i2))
                    (size (car i1)))
                (unless (eq element-type '*)
                  (return-from csubtypep-array (values nil t)))
                (when (integerp size)
                  (if (or (eq dim '*)
                          (and (consp dim) (= (length dim) 1) (eql (%car dim) size)))
                      (return-from csubtypep-array (values t t))
                      (return-from csubtypep-array (values nil t))))
                (when (or (null size) (eql size '*))
                  (if (or (eq dim '*)
                          (eql dim 1)
                          (and (consp dim) (= (length dim) 1)))
                      (return-from csubtypep-array (values t t))
                      (return-from csubtypep-array (values nil t))))))
             (t
              (values nil t))))
          ((eq t2 'bit-vector)
           (let ((size1 (car i1))
                 (size2 (car i2)))
             (case t1
               ((bit-vector simple-bit-vector)
                (values (if (or (eq size2 '*) (eql size1 size2))
                            t
                            nil) t))
               (t
                (values nil t)))))
          ((eq t2 'simple-bit-vector)
           (let ((size1 (car i1))
                 (size2 (car i2)))
             (if (and (eq t1 'simple-bit-vector)
                      (or (eq size2 '*)
                          (eql size1 size2)))
                 (values t t)
                 (values nil t))))
          ((classp t2)
           (let ((class-name (%class-name t2)))
             (cond ((eq class-name t1)
                    (values t t))
                   ((and (eq class-name 'array)
                         (memq t1 '(array simple-array vector simple-vector string
                                    simple-string simple-base-string bit-vector
                                    simple-bit-vector)))
                    (values t t))
                   ((eq class-name 'vector)
                    (cond ((memq t1 '(string simple-string))
                           (values t t))
                          ((eq t1 'array)
                           (let ((dim (cadr i1)))
                             (if (or (eql dim 1)
                                     (and (consp dim) (= (length dim) 1)))
                                 (values t t)
                                 (values nil t))))
                          (t
                           (values nil t))))
                   ((and (eq class-name 'simple-vector)
                         (eq t1 'simple-array))
                    (let ((dim (cadr i1)))
                      (if (or (eql dim 1)
                              (and (consp dim) (= (length dim) 1)))
                          (values t t)
                          (values nil t))))
                   ((and (eq class-name 'bit-vector)
                         (eq t1 'simple-bit-vector))
                    (values t t))
                   ((and (eq class-name 'string)
                         (memq t1 '(string simple-string)))
                    (values t t))
                   (t
                    (values nil nil)))))
          (t
           (values nil nil))))))

(defun csubtypep-function (ct1 ct2)
  (let ((type1 (ctype-type ct1))
        (type2 (ctype-type ct2)))
    (cond ((and (listp type1) (atom type2))
           (values t t))
          (t
           (values nil nil)))))

(defun csubtypep-complex (ct1 ct2)
  (let ((type1 (cdr ct1))
        (type2 (cdr ct2)))
    (cond ((or (null type2) (eq type2 '*))
           (values t t))
          ((eq type1 '*)
           (values nil t))
          (t
           (subtypep type1 type2)))))

(defun csubtypep (ctype1 ctype2)
  (cond ((null (and ctype1 ctype2))
         (values nil nil))
        ((neq (ctype-super ctype1) (ctype-super ctype2))
         (values nil t))
        ((eq (ctype-super ctype1) 'array)
         (csubtypep-array ctype1 ctype2))
        ((eq (ctype-super ctype1) 'function)
         (csubtypep-function ctype1 ctype2))
        ((eq (ctype-super ctype1) 'complex)
         (csubtypep-complex ctype1 ctype2))
        (t
         (values nil nil))))

480 481 482 483 484
(defun properly-named-class-p (thing environment)
  (and (classp thing) (class-name thing)
       (eq thing (find-class (class-name thing) nil environment))))

(defun %subtypep (type1 type2 &optional environment)
485 486 487
  (when (or (eq type1 type2)
            (null type1)
            (eq type2 t)
488
            (and (classp type2) (eq type2 (find-class t))))
489
    (return-from %subtypep (values t t)))
490 491 492 493
  (when (properly-named-class-p type1 environment)
    (setf type1 (class-name type1)))
  (when (properly-named-class-p type2 environment)
    (setf type2 (class-name type2)))
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
  (let ((ct1 (ctype type1))
        (ct2 (ctype type2)))
    (multiple-value-bind (subtype-p valid-p)
        (csubtypep ct1 ct2)
      (when valid-p
        (return-from %subtypep (values subtype-p valid-p)))))
  (when (and (atom type1) (atom type2))
    (let* ((classp-1 (classp type1))
           (classp-2 (classp type2))
           class1 class2)
      (when (and (setf class1 (if classp-1
                                  type1
                                  (and (symbolp type1) (find-class type1 nil))))
                 (setf class2 (if classp-2
                                  type2
                                  (and (symbolp type2) (find-class type2 nil)))))
        (return-from %subtypep (values (subclassp class1 class2) t)))
      (when (or classp-1 classp-2)
512 513
        (let ((t1 (if classp-1 (class-name type1) type1))
              (t2 (if classp-2 (class-name type2) type2)))
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
          (return-from %subtypep (values (simple-subtypep t1 t2) t))))))
  (setf type1 (normalize-type type1)
        type2 (normalize-type type2))
  (when (eq type1 type2)
    (return-from %subtypep (values t t)))
  (let (t1 t2 i1 i2)
    (if (atom type1)
        (setf t1 type1 i1 nil)
        (setf t1 (%car type1) i1 (%cdr type1)))
    (if (atom type2)
        (setf t2 type2 i2 nil)
        (setf t2 (%car type2) i2 (%cdr type2)))
    (cond ((null t1)
           (return-from %subtypep (values t t)))
          ((eq t1 'atom)
           (return-from %subtypep (values (eq t2 t) t)))
          ((eq t2 'atom)
           (return-from %subtypep (cond ((memq t1 '(cons list sequence))
                                        (values nil t))
                                       (t
                                        (values t t)))))
          ((eq t1 'member)
           (dolist (e i1)
             (unless (typep e type2) (return-from %subtypep (values nil t))))
           (return-from %subtypep (values t t)))
          ((eq t1 'eql)
           (case t2
             (EQL
              (return-from %subtypep (values (eql (car i1) (car i2)) t)))
             (SATISFIES
              (return-from %subtypep (values (funcall (car i2) (car i1)) t)))
             (t
              (return-from %subtypep (values (typep (car i1) type2) t)))))
          ((eq t1 'or)
           (dolist (tt i1)
             (multiple-value-bind (tv flag) (%subtypep tt type2)
               (unless tv (return-from %subtypep (values tv flag)))))
           (return-from %subtypep (values t t)))
          ((eq t1 'and)
           (dolist (tt i1)
             (let ((tv (%subtypep tt type2)))
               (when tv (return-from %subtypep (values t t)))))
           (return-from %subtypep (values nil nil)))
          ((eq t1 'cons)
           (case t2
             ((LIST SEQUENCE)
              (return-from %subtypep (values t t)))
             (CONS
              (when (and (%subtypep (car i1) (car i2))
                         (%subtypep (cadr i1) (cadr i2)))
                (return-from %subtypep (values t t)))))
           (return-from %subtypep (values nil (known-type-p t2))))
          ((eq t2 'or)
           (dolist (tt i2)
             (let ((tv (%subtypep type1 tt)))
               (when tv (return-from %subtypep (values t t)))))
           (return-from %subtypep (values nil nil)))
          ((eq t2 'and)
           (dolist (tt i2)
             (multiple-value-bind (tv flag) (%subtypep type1 tt)
               (unless tv (return-from %subtypep (values tv flag)))))
           (return-from %subtypep (values t t)))
          ((null (or i1 i2))
           (return-from %subtypep (values (simple-subtypep t1 t2) t)))
          ((eq t2 'SEQUENCE)
           (cond ((memq t1 '(null cons list))
                  (values t t))
                 ((memq t1 '(simple-base-string base-string simple-string string nil-vector))
                  (values t t))
                 ((memq t1 '(bit-vector simple-bit-vector))
                  (values t t))
                 ((memq t1 '(array simple-array))
                  (cond ((and (cdr i1) (consp (cadr i1)) (null (cdadr i1)))
                         (values t t))
                        ((and (cdr i1) (eql (cadr i1) 1))
                         (values t t))
                        (t
                         (values nil t))))
                 (t (values nil (known-type-p t1)))))
          ((eq t1 'integer)
           (cond ((memq t2 '(integer rational real number))
                  (values (sub-interval-p i1 i2) t))
                 ((or (eq t2 'bignum)
597
                      (and (classp t2) (eq (class-name t2) 'bignum)))
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
                  (values
                   (or (sub-interval-p i1 (list '* (list most-negative-fixnum)))
                       (sub-interval-p i1 (list (list most-positive-fixnum) '*)))
                   t))
                 (t
                  (values nil (known-type-p t2)))))
          ((eq t1 'rational)
           (if (memq t2 '(rational real number))
               (values (sub-interval-p i1 i2) t)
               (values nil (known-type-p t2))))
          ((eq t1 'float)
           (if (memq t2 '(float real number))
               (values (sub-interval-p i1 i2) t)
               (values nil (known-type-p t2))))
          ((memq t1 '(single-float short-float))
           (if (memq t2 '(single-float short-float float real number))
               (values (sub-interval-p i1 i2) t)
               (values nil (known-type-p t2))))
          ((memq t1 '(double-float long-float))
           (if (memq t2 '(double-float long-float float real number))
               (values (sub-interval-p i1 i2) t)
               (values nil (known-type-p t2))))
          ((eq t1 'real)
           (if (memq t2 '(real number))
               (values (sub-interval-p i1 i2) t)
               (values nil (known-type-p t2))))
          ((eq t1 'complex)
           (cond ((eq t2 'number)
                  (values t t))
                 ((eq t2 'complex)
                  (cond ((equal i2 '(*))
                         (values t t))
                        ((equal i1 '(*))
                         (values nil t))
                        (t
                         (values (subtypep (car i1) (car i2)) t))))))
          ((and (classp t1)
635
                (eq (class-name t1) 'array)
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
                (eq t2 'array))
           (values (equal i2 '(* *)) t))
          ((and (memq t1 '(array simple-array)) (eq t2 'array))
           (let ((e1 (car i1))
                 (e2 (car i2))
                 (d1 (cadr i1))
                 (d2 (cadr i2)))
             (cond ((and (eq e2 '*) (eq d2 '*))
                    (values t t))
                   ((or (eq e2 '*)
                        (equal e1 e2)
                        (equal (upgraded-array-element-type e1)
                               (upgraded-array-element-type e2)))
                    (values (dimension-subtypep d1 d2) t))
                   (t
                    (values nil t)))))
          ((and (memq t1 '(array simple-array)) (eq t2 'string))
           (let ((element-type (car i1))
                 (dim (cadr i1))
                 (size (car i2)))
             (unless (%subtypep element-type 'character)
               (return-from %subtypep (values nil t)))
             (when (integerp size)
               (if (and (consp dim) (= (length dim) 1) (eql (%car dim) size))
                   (return-from %subtypep (values t t))
                   (return-from %subtypep (values nil t))))
             (when (or (null size) (eql size '*))
               (if (or (eql dim 1)
                       (and (consp dim) (= (length dim) 1)))
                   (return-from %subtypep (values t t))
                   (return-from %subtypep (values nil t))))))
          ((and (eq t1 'simple-array) (eq t2 'simple-string))
           (let ((element-type (car i1))
                 (dim (cadr i1))
                 (size (car i2)))
             (unless (%subtypep element-type 'character)
               (return-from %subtypep (values nil t)))
             (when (integerp size)
               (if (and (consp dim) (= (length dim) 1) (eql (%car dim) size))
                   (return-from %subtypep (values t t))
                   (return-from %subtypep (values nil t))))
             (when (or (null size) (eql size '*))
               (if (or (eql dim 1)
                       (and (consp dim) (= (length dim) 1)))
                   (return-from %subtypep (values t t))
                   (return-from %subtypep (values nil t))))))
          ((and (memq t1 '(string simple-string)) (eq t2 'array))
           (let ((element-type (car i2))
                 (dim (cadr i2))
                 (size (car i1)))
             (unless (eq element-type '*)
               (return-from %subtypep (values nil t)))
             (when (integerp size)
               (if (or (eq dim '*)
                       (and (consp dim) (= (length dim) 1) (eql (%car dim) size)))
                   (return-from %subtypep (values t t))
                   (return-from %subtypep (values nil t))))
             (when (or (null size) (eql size '*))
               (if (or (eq dim '*)
                       (eql dim 1)
                       (and (consp dim) (= (length dim) 1)))
                   (return-from %subtypep (values t t))
                   (return-from %subtypep (values nil t))))))
          ((eq t2 'simple-array)
           (case t1
             (simple-array
              (let ((e1 (car i1))
                    (e2 (car i2))
                    (d1 (cadr i1))
                    (d2 (cadr i2)))
                (cond ((and (eq e2 '*) (eq d2 '*))
                       (values t t))
                      ((or (eq e2 '*)
                           (equal e1 e2)
                           (equal (upgraded-array-element-type e1)
                                  (upgraded-array-element-type e2)))
                       (values (dimension-subtypep d1 d2) t))
                      (t
                       (values nil t)))))
             ((simple-string simple-bit-vector)
              (let ((element-type (car i2))
                    (dim (cadr i2))
                    (size (car i1)))
                (unless (eq element-type '*)
                  (return-from %subtypep (values nil t)))
                (when (integerp size)
                  (if (or (eq dim '*)
                          (and (consp dim) (= (length dim) 1) (eql (%car dim) size)))
                      (return-from %subtypep (values t t))
                      (return-from %subtypep (values nil t))))
                (when (or (null size) (eql size '*))
                  (if (or (eq dim '*)
                          (eql dim 1)
                          (and (consp dim) (= (length dim) 1)))
                      (return-from %subtypep (values t t))
                      (return-from %subtypep (values nil t))))))
             (t
              (values nil t))))
          ((eq t2 'bit-vector)
           (let ((size1 (car i1))
                 (size2 (car i2)))
             (case t1
               ((bit-vector simple-bit-vector)
                (values (if (or (eq size2 '*) (eql size1 size2))
                            t
                            nil) t))
               (t
                (values nil t)))))
          ((classp t2)
745
           (let ((class-name (class-name t2)))
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
             (cond ((eq class-name t1)
                    (values t t))
                   ((and (eq class-name 'array)
                         (memq t1 '(array simple-array vector simple-vector string
                                    simple-string simple-base-string bit-vector
                                    simple-bit-vector)))
                    (values t t))
                   ((eq class-name 'vector)
                    (cond ((memq t1 '(string simple-string))
                           (values t t))
                          ((memq t1 '(array simple-array))
                           (let ((dim (cadr i1)))
                             (if (or (eql dim 1)
                                     (and (consp dim) (= (length dim) 1)))
                                 (values t t)
                                 (values nil t))))
                          (t
                           (values nil t))))
                   ((and (eq class-name 'simple-vector)
                         (eq t1 'simple-array))
                    (let ((dim (cadr i1)))
                      (if (or (eql dim 1)
                              (and (consp dim) (= (length dim) 1)))
                          (values t t)
                          (values nil t))))
                   ((and (eq class-name 'bit-vector)
                         (eq t1 'simple-bit-vector))
                    (values t t))
                   ((and (eq class-name 'string)
                         (memq t1 '(string simple-string)))
                    (values t t))
                   (t
                    (values nil nil)))))
          (t
           (values nil nil)))))

(defun subtypep (type1 type2 &optional environment)
783
  (%subtypep type1 type2 environment))