1 | ;;; subtypep.lisp |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 2003-2005 Peter Graves |
---|
4 | ;;; $Id: subtypep.lisp,v 1.55 2005-02-06 12:48:38 piso Exp $ |
---|
5 | ;;; |
---|
6 | ;;; This program is free software; you can redistribute it and/or |
---|
7 | ;;; modify it under the terms of the GNU General Public License |
---|
8 | ;;; as published by the Free Software Foundation; either version 2 |
---|
9 | ;;; of the License, or (at your option) any later version. |
---|
10 | ;;; |
---|
11 | ;;; This program is distributed in the hope that it will be useful, |
---|
12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | ;;; GNU General Public License for more details. |
---|
15 | ;;; |
---|
16 | ;;; You should have received a copy of the GNU General Public License |
---|
17 | ;;; along with this program; if not, write to the Free Software |
---|
18 | ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
19 | |
---|
20 | ;;; Adapted from GCL. |
---|
21 | |
---|
22 | (in-package #:system) |
---|
23 | |
---|
24 | (defparameter *known-types* (make-hash-table)) |
---|
25 | |
---|
26 | (dolist (i '((ARITHMETIC-ERROR ERROR) |
---|
27 | (ARRAY) |
---|
28 | (BASE-STRING STRING) |
---|
29 | (BIGNUM INTEGER) |
---|
30 | (BIT FIXNUM) |
---|
31 | (BIT-VECTOR VECTOR) |
---|
32 | (BOOLEAN SYMBOL) |
---|
33 | (BUILT-IN-CLASS CLASS) |
---|
34 | (CELL-ERROR ERROR) |
---|
35 | (CHARACTER) |
---|
36 | (CLASS STANDARD-OBJECT) |
---|
37 | (COMPILED-FUNCTION FUNCTION) |
---|
38 | (COMPLEX NUMBER) |
---|
39 | (CONDITION) |
---|
40 | (CONS LIST) |
---|
41 | (CONTROL-ERROR ERROR) |
---|
42 | (DIVISION-BY-ZERO ARITHMETIC-ERROR) |
---|
43 | (END-OF-FILE STREAM-ERROR) |
---|
44 | (ERROR SERIOUS-CONDITION) |
---|
45 | (EXTENDED-CHAR CHARACTER NIL) |
---|
46 | (FILE-ERROR ERROR) |
---|
47 | (FIXNUM INTEGER) |
---|
48 | (FLOAT REAL) |
---|
49 | (FLOATING-POINT-INEXACT ARITHMETIC-ERROR) |
---|
50 | (FLOATING-POINT-INVALID-OPERATION ARITHMETIC-ERROR) |
---|
51 | (FLOATING-POINT-OVERFLOW ARITHMETIC-ERROR) |
---|
52 | (FLOATING-POINT-UNDERFLOW ARITHMETIC-ERROR) |
---|
53 | (FUNCTION) |
---|
54 | (GENERIC-FUNCTION FUNCTION) |
---|
55 | (HASH-TABLE) |
---|
56 | (INTEGER RATIONAL) |
---|
57 | (KEYWORD SYMBOL) |
---|
58 | (LIST SEQUENCE) |
---|
59 | (NIL-VECTOR SIMPLE-STRING) |
---|
60 | (NULL BOOLEAN LIST) |
---|
61 | (NUMBER) |
---|
62 | (PACKAGE) |
---|
63 | (PACKAGE-ERROR ERROR) |
---|
64 | (PARSE-ERROR ERROR) |
---|
65 | (PATHNAME) |
---|
66 | (PRINT-NOT-READABLE ERROR) |
---|
67 | (PROGRAM-ERROR ERROR) |
---|
68 | (RANDOM-STATE) |
---|
69 | (RATIO RATIONAL) |
---|
70 | (RATIONAL REAL) |
---|
71 | (READER-ERROR PARSE-ERROR STREAM-ERROR) |
---|
72 | (READTABLE) |
---|
73 | (REAL NUMBER) |
---|
74 | (RESTART) |
---|
75 | (SERIOUS-CONDITION CONDITION) |
---|
76 | (SIMPLE-ARRAY ARRAY) |
---|
77 | (SIMPLE-BASE-STRING SIMPLE-STRING BASE-STRING) |
---|
78 | (SIMPLE-BIT-VECTOR BIT-VECTOR SIMPLE-ARRAY) |
---|
79 | (SIMPLE-CONDITION CONDITION) |
---|
80 | (SIMPLE-ERROR SIMPLE-CONDITION ERROR) |
---|
81 | (SIMPLE-STRING STRING SIMPLE-ARRAY) |
---|
82 | (SIMPLE-TYPE-ERROR SIMPLE-CONDITION TYPE-ERROR) |
---|
83 | (SIMPLE-VECTOR VECTOR SIMPLE-ARRAY) |
---|
84 | (SIMPLE-WARNING SIMPLE-CONDITION WARNING) |
---|
85 | (STANDARD-CHAR CHARACTER) |
---|
86 | (STANDARD-CLASS CLASS) |
---|
87 | (STANDARD-GENERIC-FUNCTION GENERIC-FUNCTION) |
---|
88 | (STANDARD-OBJECT) |
---|
89 | (STORAGE-CONDITION SERIOUS-CONDITION) |
---|
90 | (STREAM) |
---|
91 | (STREAM-ERROR ERROR) |
---|
92 | (STRING VECTOR) |
---|
93 | (STRUCTURE-CLASS CLASS STANDARD-OBJECT) |
---|
94 | (STYLE-WARNING WARNING) |
---|
95 | (SYMBOL) |
---|
96 | (TWO-WAY-STREAM STREAM) |
---|
97 | (TYPE-ERROR ERROR) |
---|
98 | (UNBOUND-SLOT CELL-ERROR) |
---|
99 | (UNBOUND-VARIABLE CELL-ERROR) |
---|
100 | (UNDEFINED-FUNCTION CELL-ERROR) |
---|
101 | (VECTOR ARRAY SEQUENCE) |
---|
102 | (WARNING CONDITION))) |
---|
103 | (setf (gethash (car i) *known-types*) (cdr i))) |
---|
104 | |
---|
105 | (defun supertypes (type) |
---|
106 | (values (gethash type *known-types*))) |
---|
107 | |
---|
108 | (defun known-type-p (type) |
---|
109 | (multiple-value-bind (value present-p) (gethash type *known-types*) |
---|
110 | present-p)) |
---|
111 | |
---|
112 | (defun sub-interval-p (i1 i2) |
---|
113 | (let (low1 high1 low2 high2) |
---|
114 | (if (null i1) |
---|
115 | (setq low1 '* high1 '*) |
---|
116 | (if (null (cdr i1)) |
---|
117 | (setq low1 (car i1) high1 '*) |
---|
118 | (setq low1 (car i1) high1 (cadr i1)))) |
---|
119 | (if (null i2) |
---|
120 | (setq low2 '* high2 '*) |
---|
121 | (if (null (cdr i2)) |
---|
122 | (setq low2 (car i2) high2 '*) |
---|
123 | (setq low2 (car i2) high2 (cadr i2)))) |
---|
124 | (when (and (consp low1) (integerp (car low1))) |
---|
125 | (setq low1 (1+ (car low1)))) |
---|
126 | (when (and (consp low2) (integerp (car low2))) |
---|
127 | (setq low2 (1+ (car low2)))) |
---|
128 | (when (and (consp high1) (integerp (car high1))) |
---|
129 | (setq high1 (1- (car high1)))) |
---|
130 | (when (and (consp high2) (integerp (car high2))) |
---|
131 | (setq high2 (1- (car high2)))) |
---|
132 | (cond ((eq low1 '*) |
---|
133 | (unless (eq low2 '*) |
---|
134 | (return-from sub-interval-p nil))) |
---|
135 | ((eq low2 '*)) |
---|
136 | ((consp low1) |
---|
137 | (if (consp low2) |
---|
138 | (when (< (car low1) (car low2)) |
---|
139 | (return-from sub-interval-p nil)) |
---|
140 | (when (< (car low1) low2) |
---|
141 | (return-from sub-interval-p nil)))) |
---|
142 | ((if (consp low2) |
---|
143 | (when (<= low1 (car low2)) |
---|
144 | (return-from sub-interval-p nil)) |
---|
145 | (when (< low1 low2) |
---|
146 | (return-from sub-interval-p nil))))) |
---|
147 | (cond ((eq high1 '*) |
---|
148 | (unless (eq high2 '*) |
---|
149 | (return-from sub-interval-p nil))) |
---|
150 | ((eq high2 '*)) |
---|
151 | ((consp high1) |
---|
152 | (if (consp high2) |
---|
153 | (when (> (car high1) (car high2)) |
---|
154 | (return-from sub-interval-p nil)) |
---|
155 | (when (> (car high1) high2) |
---|
156 | (return-from sub-interval-p nil)))) |
---|
157 | ((if (consp high2) |
---|
158 | (when (>= high1 (car high2)) |
---|
159 | (return-from sub-interval-p nil)) |
---|
160 | (when (> high1 high2) |
---|
161 | (return-from sub-interval-p nil))))) |
---|
162 | (return-from sub-interval-p t))) |
---|
163 | |
---|
164 | (defun dimension-subtypep (dim1 dim2) |
---|
165 | (cond ((eq dim2 '*) |
---|
166 | t) |
---|
167 | ((equal dim1 dim2) |
---|
168 | t) |
---|
169 | ((integerp dim2) |
---|
170 | (and (listp dim1) (= (length dim1) dim2))) |
---|
171 | ((eql dim1 0) |
---|
172 | (null dim2)) |
---|
173 | ((integerp dim1) |
---|
174 | (and (consp dim2) |
---|
175 | (= (length dim2) dim1) |
---|
176 | (equal dim2 (make-list dim1 :initial-element '*)))) |
---|
177 | ((and (consp dim1) (consp dim2) (= (length dim1) (length dim2))) |
---|
178 | (do* ((list1 dim1 (cdr list1)) |
---|
179 | (list2 dim2 (cdr list2)) |
---|
180 | (e1 (car list1) (car list1)) |
---|
181 | (e2 (car list2) (car list2))) |
---|
182 | ((null list1) t) |
---|
183 | (unless (or (eq e2 '*) (eql e1 e2)) |
---|
184 | (return nil)))) |
---|
185 | (t |
---|
186 | nil))) |
---|
187 | |
---|
188 | (defun simple-subtypep (type1 type2) |
---|
189 | (if (eq type1 type2) |
---|
190 | t |
---|
191 | (multiple-value-bind (type1-supertypes type1-known-p) |
---|
192 | (gethash type1 *known-types*) |
---|
193 | (if type1-known-p |
---|
194 | (if (memq type2 type1-supertypes) |
---|
195 | t |
---|
196 | (dolist (supertype type1-supertypes) |
---|
197 | (when (simple-subtypep supertype type2) |
---|
198 | (return t)))) |
---|
199 | nil)))) |
---|
200 | |
---|
201 | ;; (defstruct ctype |
---|
202 | ;; ((:constructor make-ctype (super type))) |
---|
203 | ;; super |
---|
204 | ;; type) |
---|
205 | |
---|
206 | (defun make-ctype (super type) |
---|
207 | (cons super type)) |
---|
208 | |
---|
209 | (defun ctype-super (ctype) |
---|
210 | (car ctype)) |
---|
211 | |
---|
212 | (defun ctype-type (ctype) |
---|
213 | (cdr ctype)) |
---|
214 | |
---|
215 | (defun ctype (type) |
---|
216 | (cond ((classp type) |
---|
217 | nil) |
---|
218 | (t |
---|
219 | (let ((tp (if (atom type) type (car type)))) |
---|
220 | (case tp |
---|
221 | ((ARRAY VECTOR STRING SIMPLE-ARRAY SIMPLE-STRING BASE-STRING |
---|
222 | SIMPLE-BASE-STRING BIT-VECTOR SIMPLE-BIT-VECTOR NIL-VECTOR) |
---|
223 | (make-ctype 'ARRAY type)) |
---|
224 | ((REAL INTEGER BIT FIXNUM UNSIGNED-BYTE BIGNUM RATIO |
---|
225 | FLOAT SINGLE-FLOAT DOUBLE-FLOAT SHORT-FLOAT LONG-FLOAT) |
---|
226 | (make-ctype 'REAL type)) |
---|
227 | (COMPLEX |
---|
228 | (make-ctype 'COMPLEX type))))))) |
---|
229 | |
---|
230 | (defun csubtypep-array (ct1 ct2) |
---|
231 | (let ((type1 (normalize-type (ctype-type ct1))) |
---|
232 | (type2 (normalize-type (ctype-type ct2)))) |
---|
233 | (when (eq type1 type2) |
---|
234 | (return-from csubtypep-array (values t t))) |
---|
235 | (let (t1 t2 i1 i2) |
---|
236 | (if (atom type1) |
---|
237 | (setf t1 type1 i1 nil) |
---|
238 | (setf t1 (car type1) i1 (cdr type1))) |
---|
239 | (if (atom type2) |
---|
240 | (setf t2 type2 i2 nil) |
---|
241 | (setf t2 (car type2) i2 (cdr type2))) |
---|
242 | (cond ((and (eq t1 (find-class 'array)) (eq t2 'array)) |
---|
243 | (values (equal i2 '(* *)) t)) |
---|
244 | ((and (memq t1 '(array simple-array)) (eq t2 'array)) |
---|
245 | (let ((e1 (car i1)) |
---|
246 | (e2 (car i2)) |
---|
247 | (d1 (cadr i1)) |
---|
248 | (d2 (cadr i2))) |
---|
249 | (cond ((and (eq e2 '*) (eq d2 '*)) |
---|
250 | (values t t)) |
---|
251 | ((or (eq e2 '*) |
---|
252 | (equal e1 e2) |
---|
253 | (equal (upgraded-array-element-type e1) |
---|
254 | (upgraded-array-element-type e2))) |
---|
255 | (values (dimension-subtypep d1 d2) t)) |
---|
256 | (t |
---|
257 | (values nil t))))) |
---|
258 | ((and (memq t1 '(simple-base-string base-string simple-string string nil-vector)) |
---|
259 | (memq t2 '(simple-base-string base-string simple-string string nil-vector))) |
---|
260 | (if (and (simple-subtypep t1 t2) |
---|
261 | (or (eql (car i1) (car i2)) |
---|
262 | (eq (car i2) '*))) |
---|
263 | (return-from csubtypep-array (values t t)) |
---|
264 | (return-from csubtypep-array (values nil t)))) |
---|
265 | ((and (memq t1 '(array simple-array)) (eq t2 'string)) |
---|
266 | (let ((element-type (car i1)) |
---|
267 | (dim (cadr i1)) |
---|
268 | (size (car i2))) |
---|
269 | (unless (%subtypep element-type 'character) |
---|
270 | (return-from csubtypep-array (values nil t))) |
---|
271 | (when (integerp size) |
---|
272 | (if (and (consp dim) (= (length dim) 1) (eql (car dim) size)) |
---|
273 | (return-from csubtypep-array (values t t)) |
---|
274 | (return-from csubtypep-array (values nil t)))) |
---|
275 | (when (or (null size) (eql size '*)) |
---|
276 | (if (or (eql dim 1) |
---|
277 | (and (consp dim) (= (length dim) 1))) |
---|
278 | (return-from csubtypep-array (values t t)) |
---|
279 | (return-from csubtypep-array (values nil t)))))) |
---|
280 | ((and (eq t1 'simple-array) (eq t2 'simple-string)) |
---|
281 | (let ((element-type (car i1)) |
---|
282 | (dim (cadr i1)) |
---|
283 | (size (car i2))) |
---|
284 | (unless (%subtypep element-type 'character) |
---|
285 | (return-from csubtypep-array (values nil t))) |
---|
286 | (when (integerp size) |
---|
287 | (if (and (consp dim) (= (length dim) 1) (eql (car dim) size)) |
---|
288 | (return-from csubtypep-array (values t t)) |
---|
289 | (return-from csubtypep-array (values nil t)))) |
---|
290 | (when (or (null size) (eql size '*)) |
---|
291 | (if (or (eql dim 1) |
---|
292 | (and (consp dim) (= (length dim) 1))) |
---|
293 | (return-from csubtypep-array (values t t)) |
---|
294 | (return-from csubtypep-array (values nil t)))))) |
---|
295 | ((and (memq t1 '(string simple-string nil-vector)) (eq t2 'array)) |
---|
296 | (let ((element-type (car i2)) |
---|
297 | (dim (cadr i2)) |
---|
298 | (size (car i1))) |
---|
299 | (unless (eq element-type '*) |
---|
300 | (return-from csubtypep-array (values nil t))) |
---|
301 | (when (integerp size) |
---|
302 | (if (or (eq dim '*) |
---|
303 | (eql dim 1) |
---|
304 | (and (consp dim) |
---|
305 | (= (length dim) 1) |
---|
306 | (or (eq (car dim) '*) |
---|
307 | (eql (car dim) size)))) |
---|
308 | (return-from csubtypep-array (values t t)) |
---|
309 | (return-from csubtypep-array (values nil t)))) |
---|
310 | (when (or (null size) (eql size '*)) |
---|
311 | (if (or (eq dim '*) |
---|
312 | (eql dim 1) |
---|
313 | (and (consp dim) (= (length dim) 1))) |
---|
314 | (return-from csubtypep-array (values t t)) |
---|
315 | (return-from csubtypep-array (values nil t)))))) |
---|
316 | ((and (memq t1 '(bit-vector simple-bit-vector)) (eq t2 'array)) |
---|
317 | (let ((element-type (car i2)) |
---|
318 | (dim (cadr i2)) |
---|
319 | (size (car i1))) |
---|
320 | (unless (memq element-type '(bit *)) |
---|
321 | (return-from csubtypep-array (values nil t))) |
---|
322 | (when (integerp size) |
---|
323 | (if (or (eq dim '*) |
---|
324 | (eql dim 1) |
---|
325 | (and (consp dim) |
---|
326 | (= (length dim) 1) |
---|
327 | (or (eq (car dim) '*) |
---|
328 | (eql (car dim) size)))) |
---|
329 | (return-from csubtypep-array (values t t)) |
---|
330 | (return-from csubtypep-array (values nil t)))) |
---|
331 | (when (or (null size) (eql size '*)) |
---|
332 | (if (or (eq dim '*) |
---|
333 | (eql dim 1) |
---|
334 | (and (consp dim) (= (length dim) 1))) |
---|
335 | (return-from csubtypep-array (values t t)) |
---|
336 | (return-from csubtypep-array (values nil t)))))) |
---|
337 | ((eq t2 'simple-array) |
---|
338 | (case t1 |
---|
339 | (simple-array |
---|
340 | (let ((e1 (car i1)) |
---|
341 | (e2 (car i2)) |
---|
342 | (d1 (cadr i1)) |
---|
343 | (d2 (cadr i2))) |
---|
344 | (cond ((and (eq e2 '*) (eq d2 '*)) |
---|
345 | (values t t)) |
---|
346 | ((or (eq e2 '*) |
---|
347 | (equal e1 e2) |
---|
348 | (equal (upgraded-array-element-type e1) |
---|
349 | (upgraded-array-element-type e2))) |
---|
350 | (values (dimension-subtypep d1 d2) t)) |
---|
351 | (t |
---|
352 | (values nil t))))) |
---|
353 | ((simple-string simple-bit-vector nil-vector) |
---|
354 | (let ((element-type (car i2)) |
---|
355 | (dim (cadr i2)) |
---|
356 | (size (car i1))) |
---|
357 | (unless (eq element-type '*) |
---|
358 | (return-from csubtypep-array (values nil t))) |
---|
359 | (when (integerp size) |
---|
360 | (if (or (eq dim '*) |
---|
361 | (and (consp dim) (= (length dim) 1) (eql (car dim) size))) |
---|
362 | (return-from csubtypep-array (values t t)) |
---|
363 | (return-from csubtypep-array (values nil t)))) |
---|
364 | (when (or (null size) (eql size '*)) |
---|
365 | (if (or (eq dim '*) |
---|
366 | (eql dim 1) |
---|
367 | (and (consp dim) (= (length dim) 1))) |
---|
368 | (return-from csubtypep-array (values t t)) |
---|
369 | (return-from csubtypep-array (values nil t)))))) |
---|
370 | (t |
---|
371 | (values nil t)))) |
---|
372 | ((eq t2 'bit-vector) |
---|
373 | (let ((size1 (car i1)) |
---|
374 | (size2 (car i2))) |
---|
375 | (case t1 |
---|
376 | ((bit-vector simple-bit-vector) |
---|
377 | (values (if (or (eq size2 '*) (eql size1 size2)) |
---|
378 | t |
---|
379 | nil) t)) |
---|
380 | (t |
---|
381 | (values nil t))))) |
---|
382 | ((eq t2 'simple-bit-vector) |
---|
383 | (let ((size1 (car i1)) |
---|
384 | (size2 (car i2))) |
---|
385 | (if (and (eq t1 'simple-bit-vector) |
---|
386 | (or (eq size2 '*) |
---|
387 | (eql size1 size2))) |
---|
388 | (values t t) |
---|
389 | (values nil t)))) |
---|
390 | ((classp t2) |
---|
391 | (cond ((eq t2 (find-class t1 nil)) |
---|
392 | (values t t)) |
---|
393 | ((and (eq t2 (find-class 'array)) |
---|
394 | (memq t1 '(array simple-array vector simple-vector string |
---|
395 | simple-string simple-base-string bit-vector |
---|
396 | simple-bit-vector))) |
---|
397 | (values t t)) |
---|
398 | ((eq t2 (find-class 'vector)) |
---|
399 | (cond ((memq t1 '(string simple-string)) |
---|
400 | (values t t)) |
---|
401 | ((eq t1 'array) |
---|
402 | (let ((dim (cadr i1))) |
---|
403 | (if (or (eql dim 1) |
---|
404 | (and (consp dim) (= (length dim) 1))) |
---|
405 | (values t t) |
---|
406 | (values nil t)))) |
---|
407 | (t |
---|
408 | (values nil t)))) |
---|
409 | ((and (eq t2 (find-class 'simple-vector)) |
---|
410 | (eq t1 'simple-array)) |
---|
411 | (let ((dim (cadr i1))) |
---|
412 | (if (or (eql dim 1) |
---|
413 | (and (consp dim) (= (length dim) 1))) |
---|
414 | (values t t) |
---|
415 | (values nil t)))) |
---|
416 | ((and (eq t2 (find-class 'bit-vector)) |
---|
417 | (eq t1 'simple-bit-vector)) |
---|
418 | (values t t)) |
---|
419 | ((and (eq t2 (find-class 'string)) |
---|
420 | (memq t1 '(string simple-string))) |
---|
421 | (values t t)) |
---|
422 | (t |
---|
423 | (values nil nil)))) |
---|
424 | (t |
---|
425 | (values nil nil)))))) |
---|
426 | |
---|
427 | (defun csubtypep (ctype1 ctype2) |
---|
428 | (cond ((null (and ctype1 ctype2)) |
---|
429 | (values nil nil)) |
---|
430 | ((neq (ctype-super ctype1) (ctype-super ctype2)) |
---|
431 | (values nil t)) |
---|
432 | ((eq (ctype-super ctype1) 'array) |
---|
433 | (csubtypep-array ctype1 ctype2)) |
---|
434 | (t |
---|
435 | (values nil nil)))) |
---|
436 | |
---|
437 | (defun %subtypep (type1 type2) |
---|
438 | (when (or (eq type1 type2) |
---|
439 | (null type1) |
---|
440 | (eq type2 t) |
---|
441 | (eq type2 (find-class t))) |
---|
442 | (return-from %subtypep (values t t))) |
---|
443 | (let ((ct1 (ctype type1)) |
---|
444 | (ct2 (ctype type2))) |
---|
445 | (multiple-value-bind (subtype-p valid-p) (csubtypep ct1 ct2) |
---|
446 | (when valid-p |
---|
447 | (return-from %subtypep (values subtype-p valid-p))))) |
---|
448 | (when (and (atom type1) (atom type2)) |
---|
449 | (let* ((classp-1 (classp type1)) |
---|
450 | (classp-2 (classp type2)) |
---|
451 | class1 class2) |
---|
452 | (when (and (setf class1 (if classp-1 |
---|
453 | type1 |
---|
454 | (and (symbolp type1) (find-class type1 nil)))) |
---|
455 | (setf class2 (if classp-2 |
---|
456 | type2 |
---|
457 | (and (symbolp type2) (find-class type2 nil))))) |
---|
458 | (return-from %subtypep |
---|
459 | (values (if (member class2 (class-precedence-list class1)) t nil) t))) |
---|
460 | (when (or classp-1 classp-2) |
---|
461 | (let ((t1 (if classp-1 (%class-name type1) type1)) |
---|
462 | (t2 (if classp-2 (%class-name type2) type2))) |
---|
463 | (return-from %subtypep (values (simple-subtypep t1 t2) t)))))) |
---|
464 | (setf type1 (normalize-type type1) |
---|
465 | type2 (normalize-type type2)) |
---|
466 | (when (eq type1 type2) |
---|
467 | (return-from %subtypep (values t t))) |
---|
468 | (let (t1 t2 i1 i2) |
---|
469 | (if (atom type1) |
---|
470 | (setf t1 type1 i1 nil) |
---|
471 | (setf t1 (car type1) i1 (cdr type1))) |
---|
472 | (if (atom type2) |
---|
473 | (setf t2 type2 i2 nil) |
---|
474 | (setf t2 (car type2) i2 (cdr type2))) |
---|
475 | (cond ((null t1) |
---|
476 | (return-from %subtypep (values t t))) |
---|
477 | ((eq t1 'atom) |
---|
478 | (return-from %subtypep (values (eq t2 t) t))) |
---|
479 | ((eq t2 'atom) |
---|
480 | (return-from %subtypep (cond ((memq t1 '(cons list sequence)) |
---|
481 | (values nil t)) |
---|
482 | (t |
---|
483 | (values t t))))) |
---|
484 | ((eq t1 'member) |
---|
485 | (dolist (e i1) |
---|
486 | (unless (typep e type2) (return-from %subtypep (values nil t)))) |
---|
487 | (return-from %subtypep (values t t))) |
---|
488 | ((eq t1 'eql) |
---|
489 | (case t2 |
---|
490 | (EQL |
---|
491 | (return-from %subtypep (values (eql (car i1) (car i2)) t))) |
---|
492 | (SATISFIES |
---|
493 | (return-from %subtypep (values (funcall (car i2) (car i1)) t))) |
---|
494 | (t |
---|
495 | (return-from %subtypep (values (typep (car i1) type2) t))))) |
---|
496 | ((eq t1 'or) |
---|
497 | (dolist (tt i1) |
---|
498 | (multiple-value-bind (tv flag) (%subtypep tt type2) |
---|
499 | (unless tv (return-from %subtypep (values tv flag))))) |
---|
500 | (return-from %subtypep (values t t))) |
---|
501 | ((eq t1 'and) |
---|
502 | (dolist (tt i1) |
---|
503 | (let ((tv (%subtypep tt type2))) |
---|
504 | (when tv (return-from %subtypep (values t t))))) |
---|
505 | (return-from %subtypep (values nil nil))) |
---|
506 | ((eq t1 'cons) |
---|
507 | (case t2 |
---|
508 | ((LIST SEQUENCE) |
---|
509 | (return-from %subtypep (values t t))) |
---|
510 | (CONS |
---|
511 | (when (and (%subtypep (car i1) (car i2)) |
---|
512 | (%subtypep (cadr i1) (cadr i2))) |
---|
513 | (return-from %subtypep (values t t))))) |
---|
514 | (return-from %subtypep (values nil (known-type-p t2)))) |
---|
515 | ((eq t2 'or) |
---|
516 | (dolist (tt i2) |
---|
517 | (let ((tv (%subtypep type1 tt))) |
---|
518 | (when tv (return-from %subtypep (values t t))))) |
---|
519 | (return-from %subtypep (values nil nil))) |
---|
520 | ((eq t2 'and) |
---|
521 | (dolist (tt i2) |
---|
522 | (multiple-value-bind (tv flag) (%subtypep type1 tt) |
---|
523 | (unless tv (return-from %subtypep (values tv flag))))) |
---|
524 | (return-from %subtypep (values t t))) |
---|
525 | ((null (or i1 i2)) |
---|
526 | (return-from %subtypep (values (simple-subtypep t1 t2) t))) |
---|
527 | ((eq t2 'sequence) |
---|
528 | (cond ((memq t1 '(null cons list)) |
---|
529 | (values t t)) |
---|
530 | ((memq t1 '(simple-base-string base-string simple-string string nil-vector)) |
---|
531 | (values t t)) |
---|
532 | ((memq t1 '(bit-vector simple-bit-vector)) |
---|
533 | (values t t)) |
---|
534 | ((memq t1 '(array simple-array)) |
---|
535 | (if (and (cdr i1) (consp (cadr i1)) (null (cdadr i1))) |
---|
536 | (values t t) |
---|
537 | (values nil t))) |
---|
538 | (t (values nil (known-type-p t1))))) |
---|
539 | ((eq t1 'float) |
---|
540 | (if (memq t2 '(float real number)) |
---|
541 | (values (sub-interval-p i1 i2) t) |
---|
542 | (values nil (known-type-p t2)))) |
---|
543 | ((eq t1 'integer) |
---|
544 | (cond ((memq t2 '(integer rational real number)) |
---|
545 | (values (sub-interval-p i1 i2) t)) |
---|
546 | ((or (eq t2 'bignum) |
---|
547 | (eq t2 (find-class 'bignum))) |
---|
548 | (values |
---|
549 | (or (sub-interval-p i1 (list '* (list most-negative-fixnum))) |
---|
550 | (sub-interval-p i1 (list (list most-positive-fixnum) '*))) |
---|
551 | t)) |
---|
552 | (t |
---|
553 | (values nil (known-type-p t2))))) |
---|
554 | ((eq t1 'rational) |
---|
555 | (if (memq t2 '(rational real number)) |
---|
556 | (values (sub-interval-p i1 i2) t) |
---|
557 | (values nil (known-type-p t2)))) |
---|
558 | ((eq t1 'real) |
---|
559 | (if (memq t2 '(real number)) |
---|
560 | (values (sub-interval-p i1 i2) t) |
---|
561 | (values nil (known-type-p t2)))) |
---|
562 | ((and (eq t1 (find-class 'array)) (eq t2 'array)) |
---|
563 | (values (equal i2 '(* *)) t)) |
---|
564 | ((and (memq t1 '(array simple-array)) (eq t2 'array)) |
---|
565 | (let ((e1 (car i1)) |
---|
566 | (e2 (car i2)) |
---|
567 | (d1 (cadr i1)) |
---|
568 | (d2 (cadr i2))) |
---|
569 | (cond ((and (eq e2 '*) (eq d2 '*)) |
---|
570 | (values t t)) |
---|
571 | ((or (eq e2 '*) |
---|
572 | (equal e1 e2) |
---|
573 | (equal (upgraded-array-element-type e1) |
---|
574 | (upgraded-array-element-type e2))) |
---|
575 | (values (dimension-subtypep d1 d2) t)) |
---|
576 | (t |
---|
577 | (values nil t))))) |
---|
578 | ((and (memq t1 '(array simple-array)) (eq t2 'string)) |
---|
579 | (let ((element-type (car i1)) |
---|
580 | (dim (cadr i1)) |
---|
581 | (size (car i2))) |
---|
582 | (unless (%subtypep element-type 'character) |
---|
583 | (return-from %subtypep (values nil t))) |
---|
584 | (when (integerp size) |
---|
585 | (if (and (consp dim) (= (length dim) 1) (eql (car dim) size)) |
---|
586 | (return-from %subtypep (values t t)) |
---|
587 | (return-from %subtypep (values nil t)))) |
---|
588 | (when (or (null size) (eql size '*)) |
---|
589 | (if (or (eql dim 1) |
---|
590 | (and (consp dim) (= (length dim) 1))) |
---|
591 | (return-from %subtypep (values t t)) |
---|
592 | (return-from %subtypep (values nil t)))))) |
---|
593 | ((and (eq t1 'simple-array) (eq t2 'simple-string)) |
---|
594 | (let ((element-type (car i1)) |
---|
595 | (dim (cadr i1)) |
---|
596 | (size (car i2))) |
---|
597 | (unless (%subtypep element-type 'character) |
---|
598 | (return-from %subtypep (values nil t))) |
---|
599 | (when (integerp size) |
---|
600 | (if (and (consp dim) (= (length dim) 1) (eql (car dim) size)) |
---|
601 | (return-from %subtypep (values t t)) |
---|
602 | (return-from %subtypep (values nil t)))) |
---|
603 | (when (or (null size) (eql size '*)) |
---|
604 | (if (or (eql dim 1) |
---|
605 | (and (consp dim) (= (length dim) 1))) |
---|
606 | (return-from %subtypep (values t t)) |
---|
607 | (return-from %subtypep (values nil t)))))) |
---|
608 | ((and (memq t1 '(string simple-string)) (eq t2 'array)) |
---|
609 | (let ((element-type (car i2)) |
---|
610 | (dim (cadr i2)) |
---|
611 | (size (car i1))) |
---|
612 | (unless (eq element-type '*) |
---|
613 | (return-from %subtypep (values nil t))) |
---|
614 | (when (integerp size) |
---|
615 | (if (or (eq dim '*) |
---|
616 | (and (consp dim) (= (length dim) 1) (eql (car dim) size))) |
---|
617 | (return-from %subtypep (values t t)) |
---|
618 | (return-from %subtypep (values nil t)))) |
---|
619 | (when (or (null size) (eql size '*)) |
---|
620 | (if (or (eq dim '*) |
---|
621 | (eql dim 1) |
---|
622 | (and (consp dim) (= (length dim) 1))) |
---|
623 | (return-from %subtypep (values t t)) |
---|
624 | (return-from %subtypep (values nil t)))))) |
---|
625 | ((eq t2 'simple-array) |
---|
626 | (case t1 |
---|
627 | (simple-array |
---|
628 | (let ((e1 (car i1)) |
---|
629 | (e2 (car i2)) |
---|
630 | (d1 (cadr i1)) |
---|
631 | (d2 (cadr i2))) |
---|
632 | (cond ((and (eq e2 '*) (eq d2 '*)) |
---|
633 | (values t t)) |
---|
634 | ((or (eq e2 '*) |
---|
635 | (equal e1 e2) |
---|
636 | (equal (upgraded-array-element-type e1) |
---|
637 | (upgraded-array-element-type e2))) |
---|
638 | (values (dimension-subtypep d1 d2) t)) |
---|
639 | (t |
---|
640 | (values nil t))))) |
---|
641 | ((simple-string simple-bit-vector) |
---|
642 | (let ((element-type (car i2)) |
---|
643 | (dim (cadr i2)) |
---|
644 | (size (car i1))) |
---|
645 | (unless (eq element-type '*) |
---|
646 | (return-from %subtypep (values nil t))) |
---|
647 | (when (integerp size) |
---|
648 | (if (or (eq dim '*) |
---|
649 | (and (consp dim) (= (length dim) 1) (eql (car dim) size))) |
---|
650 | (return-from %subtypep (values t t)) |
---|
651 | (return-from %subtypep (values nil t)))) |
---|
652 | (when (or (null size) (eql size '*)) |
---|
653 | (if (or (eq dim '*) |
---|
654 | (eql dim 1) |
---|
655 | (and (consp dim) (= (length dim) 1))) |
---|
656 | (return-from %subtypep (values t t)) |
---|
657 | (return-from %subtypep (values nil t)))))) |
---|
658 | (t |
---|
659 | (values nil t)))) |
---|
660 | ((eq t2 'bit-vector) |
---|
661 | (let ((size1 (car i1)) |
---|
662 | (size2 (car i2))) |
---|
663 | (case t1 |
---|
664 | ((bit-vector simple-bit-vector) |
---|
665 | (values (if (or (eq size2 '*) (eql size1 size2)) |
---|
666 | t |
---|
667 | nil) t)) |
---|
668 | (t |
---|
669 | (values nil t))))) |
---|
670 | ((classp t2) |
---|
671 | (cond ((eq t2 (find-class t1 nil)) |
---|
672 | (values t t)) |
---|
673 | ((and (eq t2 (find-class 'array)) |
---|
674 | (memq t1 '(array simple-array vector simple-vector string |
---|
675 | simple-string simple-base-string bit-vector |
---|
676 | simple-bit-vector))) |
---|
677 | (values t t)) |
---|
678 | ((eq t2 (find-class 'vector)) |
---|
679 | (cond ((memq t1 '(string simple-string)) |
---|
680 | (values t t)) |
---|
681 | ((eq t1 'array) |
---|
682 | (let ((dim (cadr i1))) |
---|
683 | (if (or (eql dim 1) |
---|
684 | (and (consp dim) (= (length dim) 1))) |
---|
685 | (values t t) |
---|
686 | (values nil t)))) |
---|
687 | (t |
---|
688 | (values nil t)))) |
---|
689 | ((and (eq t2 (find-class 'simple-vector)) |
---|
690 | (eq t1 'simple-array)) |
---|
691 | (let ((dim (cadr i1))) |
---|
692 | (if (or (eql dim 1) |
---|
693 | (and (consp dim) (= (length dim) 1))) |
---|
694 | (values t t) |
---|
695 | (values nil t)))) |
---|
696 | ((and (eq t2 (find-class 'bit-vector)) |
---|
697 | (eq t1 'simple-bit-vector)) |
---|
698 | (values t t)) |
---|
699 | ((and (eq t2 (find-class 'string)) |
---|
700 | (memq t1 '(string simple-string))) |
---|
701 | (values t t)) |
---|
702 | (t |
---|
703 | (values nil nil)))) |
---|
704 | (t |
---|
705 | (values nil nil))))) |
---|
706 | |
---|
707 | (defun subtypep (type1 type2 &optional environment) |
---|
708 | (%subtypep type1 type2)) |
---|