source: trunk/j/src/org/armedbear/lisp/subtypep.lisp @ 4361

Last change on this file since 4361 was 4361, checked in by piso, 20 years ago

SUBTYPEP-NORMALIZE-TYPE

File size: 12.7 KB
Line 
1;;; subtypep.lisp
2;;;
3;;; Copyright (C) 2003 Peter Graves
4;;; $Id: subtypep.lisp,v 1.21 2003-10-14 02:31:20 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 SIMPLE-STRING)
29             (BIGNUM INTEGER)
30             (BIT INTEGER)
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             (NULL SYMBOL LIST)
60             (NUMBER)
61             (PACKAGE)
62             (PACKAGE-ERROR ERROR)
63             (PARSE-ERROR ERROR)
64             (PATHNAME)
65             (PRINT-NOT-READABLE ERROR)
66             (PROGRAM-ERROR ERROR)
67             (RANDOM-STATE)
68             (RATIO RATIONAL)
69             (RATIONAL REAL)
70             (READER-ERROR PARSE-ERROR STREAM-ERROR)
71             (READTABLE)
72             (REAL NUMBER)
73             (RESTART)
74             (SERIOUS-CONDITION CONDITION)
75             (SIMPLE-ARRAY ARRAY)
76             (SIMPLE-BASE-STRING SIMPLE-STRING BASE-STRING)
77             (SIMPLE-BIT-VECTOR BIT-VECTOR SIMPLE-ARRAY)
78             (SIMPLE-CONDITION CONDITION)
79             (SIMPLE-ERROR SIMPLE-CONDITION ERROR)
80             (SIMPLE-STRING STRING SIMPLE-ARRAY)
81             (SIMPLE-TYPE-ERROR SIMPLE-CONDITION TYPE-ERROR)
82             (SIMPLE-VECTOR VECTOR SIMPLE-ARRAY)
83             (SIMPLE-WARNING SIMPLE-CONDITION WARNING)
84             (STANDARD-CHAR CHARACTER)
85             (STANDARD-CLASS CLASS)
86             (STANDARD-GENERIC-FUNCTION GENERIC-FUNCTION)
87             (STANDARD-OBJECT)
88             (STORAGE-CONDITION SERIOUS-CONDITION)
89             (STREAM)
90             (STREAM-ERROR ERROR)
91             (STRING VECTOR)
92             (STRUCTURE-CLASS CLASS STANDARD-OBJECT)
93             (STYLE-WARNING WARNING)
94             (SYMBOL)
95             (TWO-WAY-STREAM STREAM)
96             (TYPE-ERROR ERROR)
97             (UNBOUND-SLOT CELL-ERROR)
98             (UNBOUND-VARIABLE CELL-ERROR)
99             (UNDEFINED-FUNCTION CELL-ERROR)
100             (VECTOR ARRAY SEQUENCE)
101             (WARNING CONDITION)
102             ))
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 subtypep-normalize-type (type)
113  (when (symbolp type)
114    (case type
115      (FIXNUM
116       (return-from subtypep-normalize-type
117                    '(integer #.most-negative-fixnum #.most-positive-fixnum)))
118      (BASE-CHAR
119       (return-from subtypep-normalize-type 'character))))
120  (let (tp i)
121    (loop
122      (if (consp type)
123          (setq tp (car type) i (cdr type))
124          (setq tp type i nil))
125      (if (and (symbolp tp) (get tp 'deftype-definition))
126          (setq type (apply (get tp 'deftype-definition) i))
127          (return)))
128    (case tp
129      ((ARRAY SIMPLE-ARRAY)
130       (when (and i (eq (car i) nil)) ; Element type is NIL.
131         (if (eq tp 'simple-array)
132             (setq tp 'simple-string)
133             (setq tp 'string))
134         (when (cadr i) ; rank/dimensions
135           (cond ((and (consp (cadr i)) (= (length (cadr i)) 1))
136                  (if (eq (caadr i) '*)
137                      (setq i nil)
138                      (setq i (cadr i))))
139                 ((eql (cadr i) 1)
140                  (setq i nil))
141                 (t
142                  (error "invalid type specifier ~S" type))))))
143      ((SHORT-FLOAT SINGLE-FLOAT DOUBLE-FLOAT LONG-FLOAT)
144       (setq tp 'float)))
145    (if i (cons tp i) tp)))
146
147(defun sub-interval-p (i1 i2)
148  (let (low1 high1 low2 high2)
149    (if (null i1)
150        (setq low1 '* high1 '*)
151        (if (null (cdr i1))
152            (setq low1 (car i1) high1 '*)
153            (setq low1 (car i1) high1 (cadr i1))))
154    (if (null i2)
155        (setq low2 '* high2 '*)
156        (if (null (cdr i2))
157            (setq low2 (car i2) high2 '*)
158            (setq low2 (car i2) high2 (cadr i2))))
159    (when (and (consp low1) (integerp (car low1)))
160      (setq low1 (1+ (car low1))))
161    (when (and (consp low2) (integerp (car low2)))
162      (setq low2 (1+ (car low2))))
163    (when (and (consp high1) (integerp (car high1)))
164      (setq high1 (1- (car high1))))
165    (when (and (consp high2) (integerp (car high2)))
166      (setq high2 (1- (car high2))))
167    (cond ((eq low1 '*)
168     (unless (eq low2 '*)
169             (return-from sub-interval-p nil)))
170          ((eq low2 '*))
171    ((consp low1)
172     (if (consp low2)
173         (when (< (car low1) (car low2))
174         (return-from sub-interval-p nil))
175         (when (< (car low1) low2)
176         (return-from sub-interval-p nil))))
177    ((if (consp low2)
178         (when (<= low1 (car low2))
179         (return-from sub-interval-p nil))
180         (when (< low1 low2)
181         (return-from sub-interval-p nil)))))
182    (cond ((eq high1 '*)
183     (unless (eq high2 '*)
184             (return-from sub-interval-p nil)))
185          ((eq high2 '*))
186    ((consp high1)
187     (if (consp high2)
188         (when (> (car high1) (car high2))
189         (return-from sub-interval-p nil))
190         (when (> (car high1) high2)
191         (return-from sub-interval-p nil))))
192    ((if (consp high2)
193         (when (>= high1 (car high2))
194         (return-from sub-interval-p nil))
195         (when (> high1 high2)
196         (return-from sub-interval-p nil)))))
197    (return-from sub-interval-p t)))
198
199(defun simple-subtypep (type1 type2)
200  (when (and (symbolp type1) (symbolp type2))
201    (multiple-value-bind (type1-supertypes type1-known-p)
202        (gethash type1 *known-types*)
203      (when type1-known-p
204        (return-from simple-subtypep (if (memq type2 type1-supertypes)
205                                         t
206                                         (dolist (supertype type1-supertypes)
207                                           (when (simple-subtypep supertype type2)
208                                             (return (values t)))))))))
209  (let ((c1 (if (classp type1) type1 (find-class type1 nil)))
210        (c2 (if (classp type2) type2 (find-class type2 nil))))
211    (when (and c1 c2)
212      (return-from simple-subtypep
213                   (if (memq c2 (class-precedence-list c1)) t nil))))
214  nil)
215
216(defun subtypep (type1 type2)
217  (when (or (null type1) (eq type2 t))
218    (return-from subtypep (values t t)))
219  (let* ((c1 (classp type1)) (c2 (classp type2))
220         (t1 (if c1
221                 type1
222                 (and (symbolp type1) (find-class type1 nil))))
223         (t2 (if c2
224                 type2
225                 (and (symbolp type2) (find-class type2 nil)))))
226    (when (and t1 t2)
227      (return-from subtypep
228       (if (member t2 (class-precedence-list t1))
229           (values t t) (values nil t))))
230    (when (or c1 c2)
231      (return-from subtypep (values nil t))))
232  (setq type1 (subtypep-normalize-type type1)
233        type2 (subtypep-normalize-type type2))
234  (when (equal type1 type2)
235    (return-from subtypep (values t t)))
236  (let (t1 t2 i1 i2)
237    (if (atom type1)
238        (setq t1 type1 i1 nil)
239        (setq t1 (car type1) i1 (cdr type1)))
240    (if (atom type2)
241        (setq t2 type2 i2 nil)
242        (setq t2 (car type2) i2 (cdr type2)))
243    (when (eq t2 'atom)
244      (return-from subtypep (cond ((memq t1 '(cons list)) (values nil t))
245                                  ((known-type-p t1) (values t t))
246                                  (t (values nil nil)))))
247    (cond ((eq t1 'member)
248           (dolist (e i1)
249             (unless (typep e type2) (return-from subtypep (values nil t))))
250           (return-from subtypep (values t t)))
251          ((eq t1 'or)
252           (dolist (tt i1)
253             (multiple-value-bind (tv flag) (subtypep tt type2)
254               (unless tv (return-from subtypep (values tv flag)))))
255           (return-from subtypep (values t t)))
256          ((eq t1 'and)
257           (dolist (tt i1)
258             (let ((tv (subtypep tt type2)))
259               (when tv (return-from subtypep (values t t)))))
260           (return-from subtypep (values nil nil)))
261          ((eq t2 'or)
262           (dolist (tt i2)
263             (let ((tv (subtypep type1 tt)))
264               (when tv (return-from subtypep (values t t)))))
265           (return-from subtypep (values nil nil)))
266          ((eq t2 'and)
267           (dolist (tt i2)
268             (multiple-value-bind (tv flag) (subtypep type1 tt)
269               (unless tv (return-from subtypep (values tv flag)))))
270           (return-from subtypep (values t t)))
271          ((null (or i1 i2))
272           (return-from subtypep (values (simple-subtypep t1 t2) t)))
273          ((eq t2 'sequence)
274           (cond ((memq t1 '(null cons list))
275                  (values t t))
276                 ((memq t1 '(array simple-array))
277                  (if (and (cdr i1) (consp (cadr i1)) (null (cdadr i1)))
278                      (values t t)
279                      (values nil t)))
280                 (t (values nil (known-type-p t1)))))
281          ((eq t2 'vector)
282           (if (eq t1 'base-string)
283               (if (eq (car i2) 'base-char)
284                   (values t t)
285                   (values nil t))
286               (values nil (known-type-p t2))))
287          ((eq t2 'simple-string)
288           (if (memq t1 '(simple-string simple-base-string))
289               (if (or (null i2) (eq (car i2) '*))
290                   (values t t)
291                   (values nil t))
292               (values nil (known-type-p t2))))
293          ((eq t2 'base-string)
294           (if (eq t1 'vector)
295               (if (eq (car i1) 'base-char)
296                   (values t t)
297                   (values nil t))
298               (values nil (known-type-p t2))))
299          ((eq t2 'string)
300           (if (eq t1 'vector)
301               (if (eq (car i1) 'character)
302                   (values t t)
303                   (values nil t))
304               (values nil (known-type-p t2))))
305          (t
306           (cond ((eq t1 'float)
307                  (if (memq t2 '(float real number))
308                      (values (sub-interval-p i1 i2) t)
309                      (values nil (known-type-p t2))))
310                 ((eq t1 'integer)
311                  (if (memq t2 '(integer rational real number))
312                      (values (sub-interval-p i1 i2) t)
313                      (values nil (known-type-p t2))))
314                 ((eq t1 'rational)
315                  (if (memq t2 '(rational real number))
316                      (values (sub-interval-p i1 i2) t)
317                      (values nil (known-type-p t2))))
318                 ((eq t1 'real)
319                  (if (memq t2 '(real number))
320                      (values (sub-interval-p i1 i2) t)
321                      (values nil (known-type-p t2))))
322                 ((memq t1 '(string simple-string base-string
323                             simple-base-string))
324                  (cond ((eq t2 'string)
325                         (if (or (null i2) (eq (car i2) '*))
326                             (values t t)
327                             (values nil t)))
328                        (t
329                         (values nil (known-type-p t2)))))
330                 (t
331                  (values nil nil)))))))
332
333(when (fboundp 'jvm::jvm-compile)
334  (mapcar #'jvm::jvm-compile '(sys::subtypep-normalize-type
335                               sys::sub-interval-p
336                               sys::simple-subtypep
337                               subtypep)))
Note: See TracBrowser for help on using the repository browser.