Debian Codegraph @

src/lsp/predlib.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: SYSTEM -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya.
6 ;;;; Copyright (c) 1990, Giuseppe Attardi.
7 ;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll.
8 ;;;;
9 ;;;; See file 'LICENSE' for the copyright details.
10
11 ;;;; Predicate routines.
12
13 (in-package "SYSTEM")
14
15 (defun constantly-t (&rest foo)
16 (declare (ignore foo))
17 t)
18
19 (defun constantly-nil (&rest foo)
20 (declare (ignore foo))
21 nil)
22
23 (defun constantly (n)
24 "Args: (n)
25 Builds a new function which accepts any number of arguments but always outputs N."
26 (case n
27 ((nil) #'constantly-nil)
28 ((t) #'constantly-t)
29 (t #'(lambda (&rest x) (declare (ignore x)) n))))
30
31 (defparameter *subtypep-cache* (si:make-vector t 256 nil nil nil 0))
32
33 (defparameter *upgraded-array-element-type-cache* (si:make-vector t 128 nil nil nil 0))
34
35 (defun subtypep-clear-cache ()
36 (ext:fill-array-with-elt *subtypep-cache* nil 0 nil)
37 (ext:fill-array-with-elt *upgraded-array-element-type-cache* nil 0 nil))
38
39 (defun create-type-name (name)
40 (when (member name *alien-declarations*)
41 (error "Symbol ~s is a declaration specifier and cannot be used to name a new type" name)))
42
43 (defun do-deftype (name form function)
44 (unless (symbolp name)
45 (error-type-specifier name))
46 (create-type-name name)
47 (put-sysprop name 'DEFTYPE-FORM form)
48 (put-sysprop name 'DEFTYPE-DEFINITION
49 (if (functionp function) function (constantly function)))
50 (subtypep-clear-cache)
51 name)
52
53 ;;; DEFTYPE macro.
54 (defmacro deftype (name lambda-list &rest body &environment env)
55 "Syntax: (deftype name macro-lambda-list {decl | doc}* {form}*)
56 Defines a new type-specifier abbreviation in terms of an 'expansion'
57 function
58
59 (lambda (whole) {DECL}* {FORM}*)
60
61 where WHOLE is identical to MACRO-LAMBDA-LIST except that all optional
62 parameters with no default value specified in LAMBDA-LIST defaults to
63 the symbol '*', but not to NIL. When the type system of ECL encounters
64 a type specifier (NAME arg1 ... argn), it calls the expansion function
65 with the argument (ARG1 ... ARGn), and uses the returned value instead
66 of the original type specifier. When the symbol NAME is used as a
67 type specifier, the expansion function is called with no argument.
68 The doc-string DOC, if supplied, is saved as a TYPE doc and can be
69 retrieved by (documentation 'NAME 'type)."
70 (labels ((set-default (list*)
71 "Sets default value for optional arguments to *. Doesn't
72 modify arguments which happen to be in lambda-list
73 keywords."
74 (if (consp list*)
75 (let ((variable (car list*)))
76 (cons
77 (if (and (symbolp variable)
78 (not (member variable lambda-list-keywords)))
79 `(,variable '*)
80 variable)
81 (set-default (cdr list*))))
82 list*))
83 (verify-tree (elt)
84 "Vefrifies if ELT is the list containing optional arg."
85 (and (consp elt)
86 (member (car elt)
87 '(&key &optional))))
88 (maptree (function tree test)
89 "Applies FUNCTION to branches for which TEST resolves to
90 true. MAPTREE doesn't traverse this branch further. It
91 is correct in this context, because we can't create
92 nested lambda-list after both &key and &optional, since
93 it would be considered as default value or an error."
94 (cond ((funcall test tree)
95 (funcall function tree))
96 ((consp tree)
97 (cons
98 (maptree function (car tree) test)
99 (maptree function (cdr tree) test)))
100 (T tree))))
101 (setf lambda-list
102 (maptree #'set-default lambda-list #'verify-tree)))
103 (multiple-value-bind (function ppn documentation)
104 (expand-defmacro name lambda-list body 'deftype)
105 (declare (ignore ppn))
106 (when (and (null lambda-list)
107 (consp body)
108 (null (rest body)))
109 (let ((form (first body)))
110 (when (constantp form env)
111 (setf function (ext:maybe-quote (ext:constant-form-value form env))))))
112 `(eval-when (:compile-toplevel :load-toplevel :execute)
113 ,@(si::expand-set-documentation name 'type documentation)
114 (do-deftype ',name '(DEFTYPE ,name ,lambda-list ,@body)
115 ,function))))
116
117 ;;; Some DEFTYPE definitions.
118 (deftype boolean ()
119 "A BOOLEAN is an object which is either NIL or T."
120 '(member nil t))
121
122 (deftype index ()
123 '(INTEGER 0 #.array-dimension-limit))
124
125 (deftype fixnum ()
126 "A FIXNUM is an integer between MOST-NEGATIVE-FIXNUM and
127 MOST-POSITIVE-FIXNUM inclusive. Other integers are bignums."
128 '(INTEGER #.most-negative-fixnum #.most-positive-fixnum))
129 (deftype bignum ()
130 '(OR (INTEGER * (#.most-negative-fixnum)) (INTEGER (#.most-positive-fixnum) *)))
131
132 (deftype ext:byte8 () '(INTEGER 0 255))
133 (deftype ext:integer8 () '(INTEGER -128 127))
134 (deftype ext:byte16 () '(INTEGER 0 #xFFFF))
135 (deftype ext:integer16 () '(INTEGER #x-8000 #x7FFF))
136 (deftype ext:byte32 () '(INTEGER 0 #xFFFFFFFF))
137 (deftype ext:integer32 () '(INTEGER #x-80000000 #x7FFFFFFF))
138 (deftype ext:byte64 () '(INTEGER 0 #xFFFFFFFFFFFFFFFF))
139 (deftype ext:integer64 () '(INTEGER #x-8000000000000000 #x7FFFFFFFFFFFFFFF))
140 (deftype ext:cl-fixnum () '(SIGNED-BYTE #.CL-FIXNUM-BITS))
141 (deftype ext:cl-index () '(UNSIGNED-BYTE #.CL-FIXNUM-BITS))
142
143 (deftype real (&optional (start '* start-p) (end '*))
144 (if start-p
145 (let (rat-start
146 real-start
147 rat-end
148 real-end)
149 (cond ((consp start)
150 (setf start (first start)
151 rat-start (list (rational start))
152 real-start (list (float start))))
153 ((numberp start)
154 (setf rat-start (rational start)
155 real-start (float start)))
156 (t
157 (setf rat-start start
158 real-start start)))
159 (cond ((consp end)
160 (setf end (first end)
161 rat-end (list (rational end))
162 real-end (list (float end))))
163 ((numberp end)
164 (setf rat-end (rational end)
165 real-end (float end)))
166 (t
167 (setf rat-end end
168 real-end end)))
169 `(OR (RATIONAL ,rat-start ,rat-end) (FLOAT ,real-start ,real-end)))
170 '(OR RATIONAL FLOAT)))
171
172 #-short-float
173 (deftype short-float (&rest args)
174 (if args
175 `(single-float ,@args)
176 'single-float))
177
178 (deftype bit ()
179 "A BIT is either integer 0 or 1."
180 '(INTEGER 0 1))
181
182 (deftype mod (n)
183 `(INTEGER 0 ,(1- n)))
184
185 (deftype signed-byte (&optional s)
186 "As a type specifier, (SIGNED-BYTE n) specifies those integers that can be
187 represented with N bits in 2's complement representation."
188 (if (or (null s) (eq s '*))
189 '(INTEGER * *)
190 `(INTEGER ,(- (expt 2 (1- s))) ,(1- (expt 2 (1- s))))))
191
192 (deftype unsigned-byte (&optional s)
193 "As a type specifier, (UNSIGNED-BYTE n) specifies non-negative integers that
194 can be represented with N bits."
195 (if (or (null s) (eq s '*))
196 '(INTEGER 0 *)
197 `(INTEGER 0 ,(1- (expt 2 s)))))
198
199 (deftype null ()
200 "The type to which only NIL belongs."
201 '(MEMBER NIL))
202
203 (deftype sequence ()
204 "A sequence is either a list or a vector."
205 '(OR CONS NULL (ARRAY * (*))))
206
207 (deftype list ()
208 "As a type specifier, LIST is used to specify the type consisting of NIL and
209 cons objects. In our ordinary life with Lisp, however, a list is either NIL
210 or a cons whose cdr is a list, and is notated by its elements surrounded with
211 parentheses.
212 The backquote macro is sometimes useful to construct a complicated list
213 structure. When evaluating `(...)
214 ,form embeds the value of FORM,
215 ,@form and ,.form embed all elements of the list value of FORM,
216 and other things embed itself
217 into the structure at their position. For example,
218 `(a b ,c d e) expands to (list* 'a 'b c '(d e))
219 `(a b ,@c d e) expands to (list* 'a 'b (append c '(d e)))
220 `(a b ,.c d e) expands to (list* 'a 'b (nconc c '(d e)))"
221 '(OR CONS NULL))
222
223 (deftype proper-list ()
224 '(OR (CONS T PROPER-LIST) NULL))
225
226 (deftype property-list ()
227 '(OR (CONS T (CONS T PROPERTY-LIST)) NULL))
228
229 (deftype atom ()
230 "An ATOM is an object that is not a CONS."
231 '(NOT CONS))
232 ;(deftype null () '(MEMBER NIL))
233
234 (deftype vector (&optional (element-type '*) (size '*))
235 "A vector is a one-dimensional array. Strings and bit-vectors are kinds of
236 vectors. Other vectors are called general vectors and are notated as
237 #(elem ... elem)
238 Some vectors may be displaced to another array, may have a fill-pointer, or
239 may be adjustable. Other vectors are called simple-vectors."
240 `(array ,element-type (,size)))
241
242 (deftype extended-char ()
243 "A character which is not of type BASE-CHAR."
244 '(and character (not base-char)))
245
246 (deftype string (&optional size)
247 "A string is a vector of characters. A string is notated by surrounding the
248 characters with double quotes. Some strings may be displaced to another
249 string, may have a fill-pointer, or may be adjustable. Other strings are
250 called simple-strings."
251 #-unicode
252 (if (eq size '*)
253 '(array character (*))
254 `(array character (,size)))
255 #+unicode
256 (if (eq size '*)
257 '(or (array base-char (*)) (array character (*)))
258 `(or (array base-char (,size))
259 (array character (,size)))))
260
261 (deftype base-string (&optional (size '*))
262 "A string which is made of BASE-CHAR."
263 (if (eq size '*) '(array base-char (*)) `(array base-char (,size))))
264
265 (deftype extended-string (&optional (size '*))
266 "A string which is nt a base string"
267 #-unicode
268 NIL
269 #+unicode
270 (if (eq size '*) '(array character (*)) `(array character (,size))))
271
272 (deftype bit-vector (&optional (size '*))
273 "A bit-vector is a vector of bits. A bit-vector is notated by '#*' followed
274 by its elements (0 or 1). Bit-vectors may be displaced to another array, may
275 have a fill-pointer, or may be adjustable. Other bit-vectors are called
276 simple-bit-vectors. Only simple-bit-vectors can be input in the above format
277 using '#*'."
278 (if (eq size '*) '(array bit (*)) `(array bit (,size))))
279
280 (deftype simple-vector (&optional (size '*))
281 "A simple-vector is a vector that is not displaced to another array, has no
282 fill-pointer, and is not adjustable."
283 (if (eq size '*) '(simple-array t (*)) `(simple-array t (,size))))
284
285 (deftype simple-string (&optional size)
286 "A simple-string is a string that is not displaced to another array, has no
287 fill-pointer, and is not adjustable."
288 #-unicode
289 (if size
290 `(simple-array character (,size))
291 '(simple-array character (*)))
292 #+unicode
293 (if size
294 `(or (simple-array base-char (,size))
295 (simple-array character (,size)))
296 '(or (simple-array base-char (*)) (simple-array character (*)))))
297
298 (deftype simple-base-string (&optional size)
299 "A base-string which cannot be adjusted nor displaced."
300 (if size `(simple-array base-char (,size)) '(simple-array base-char (*))))
301
302 (deftype simple-bit-vector (&optional size)
303 "A bit-vector that is not displaced to another array, has no fill-pointer,
304 and is not adjustable."
305 (if size `(simple-array bit (,size)) '(simple-array bit (*))))
306
307 (deftype array-index ()
308 '(integer 0 #.(1- array-dimension-limit)))
309
310 (deftype ext:virtual-stream ()
311 '(or string-stream
312 #+clos-streams gray:fundamental-stream))
313
314 ;;; ----------------------------------------------------------------------------
315 ;;; TYPEP
316 ;;; ----------------------------------------------------------------------------
317
318 (defun simple-array-p (x)
319 (and (arrayp x)
320 (not (adjustable-array-p x))
321 (not (array-has-fill-pointer-p x))
322 (not (array-displacement x))))
323
324 (defun complex-array-p (x)
325 (and (arrayp x)
326 (or (adjustable-array-p x)
327 (array-has-fill-pointer-p x)
328 (array-displacement x))))
329
330 (defun ratiop (x)
331 #-ecl-min
332 (ffi::c-inline (x) (t) :bool "ecl_t_of(#0) == t_ratio" :one-liner t)
333 #+ecl-min
334 (and (rationalp x) (not (integerp x))))
335
336 #+short-float
337 (defun short-float-p (x)
338 #-ecl-min
339 (ffi::c-inline (x) (t) :bool "ecl_t_of(#0) == t_shortfloat" :one-liner t)
340 #+ecl-min
341 (eq (type-of x) 'short-float))
342
343 #-short-float
344 (defun short-float-p (x)
345 #-ecl-min
346 (ffi::c-inline (x) (t) :bool "ecl_t_of(#0) == t_singlefloat" :one-liner t)
347 #+ecl-min
348 (eq (type-of x) 'single-float))
349
350 (defun single-float-p (x)
351 #-ecl-min
352 (ffi::c-inline (x) (t) :bool "ecl_t_of(#0) == t_singlefloat" :one-liner t)
353 #+ecl-min
354 (eq (type-of x) 'single-float))
355
356 (defun double-float-p (x)
357 #-ecl-min
358 (ffi::c-inline (x) (t) :bool "ecl_t_of(#0) == t_doublefloat" :one-liner t)
359 #+ecl-min
360 (eq (type-of x) 'double-float))
361
362 (defun long-float-p (x)
363 #-ecl-min
364 (ffi::c-inline (x) (t) :bool "ecl_t_of(#0) == t_longfloat" :one-liner t)
365 #+ecl-min
366 (eq (type-of x) 'long-float))
367
368 #+complex-float
369 (defun complex-single-float-p (x)
370 #-ecl-min
371 (ffi::c-inline (x) (t) :bool "ecl_t_of(#0) == t_csfloat" :one-liner t)
372 #+ecl-min
373 (equal (type-of x) '(complex single-float)))
374
375 #+complex-float
376 (defun complex-double-float-p (x)
377 #-ecl-min
378 (ffi::c-inline (x) (t) :bool "ecl_t_of(#0) == t_cdfloat" :one-liner t)
379 #+ecl-min
380 (equal (type-of x) '(complex double-float)))
381
382 #+complex-float
383 (defun complex-long-float-p (x)
384 #-ecl-min
385 (ffi::c-inline (x) (t) :bool "ecl_t_of(#0) == t_clfloat" :one-liner t)
386 #+ecl-min
387 (equal (type-of x) '(complex long-float)))
388
389
390 (eval-when (:execute :load-toplevel :compile-toplevel)
391 (defconstant +known-typep-predicates+
392 '((ARRAY . ARRAYP)
393 (ATOM . ATOM)
394 #-unicode
395 (EXTENDED-CHAR . CONSTANTLY-NIL)
396 (BASE-CHAR . BASE-CHAR-P)
397 (BASE-STRING . BASE-STRING-P)
398 (BIT-VECTOR . BIT-VECTOR-P)
399 (CHARACTER . CHARACTERP)
400 (COMPILED-FUNCTION . COMPILED-FUNCTION-P)
401 (COMPLEX . COMPLEXP)
402 #+complex-float(SI:COMPLEX-SINGLE-FLOAT . COMPLEX-SINGLE-FLOAT-P)
403 #+complex-float(SI:COMPLEX-DOUBLE-FLOAT . COMPLEX-DOUBLE-FLOAT-P)
404 #+complex-float(SI:COMPLEX-LONG-FLOAT . COMPLEX-LONG-FLOAT-P)
405 (COMPLEX-ARRAY . COMPLEX-ARRAY-P)
406 (CONS . CONSP)
407 (DOUBLE-FLOAT . SI:DOUBLE-FLOAT-P)
408 (FLOAT . FLOATP)
409 (SI:FOREIGN-DATA . SI:FOREIGN-DATA-P)
410 (FUNCTION . FUNCTIONP)
411 (HASH-TABLE . HASH-TABLE-P)
412 (INTEGER . INTEGERP)
413 (FIXNUM . SI::FIXNUMP)
414 (KEYWORD . KEYWORDP)
415 (LIST . LISTP)
416 (LOGICAL-PATHNAME . LOGICAL-PATHNAME-P)
417 (LONG-FLOAT . SI:LONG-FLOAT-P)
418 (NIL . CONSTANTLY-NIL)
419 (NULL . NULL)
420 (NUMBER . NUMBERP)
421 (PACKAGE . PACKAGEP)
422 (RATIO . SI:RATIOP)
423 (RANDOM-STATE . RANDOM-STATE-P)
424 (RATIONAL . RATIONALP)
425 (PATHNAME . PATHNAMEP)
426 (READTABLE . READTABLEP)
427 (REAL . REALP)
428 (SHORT-FLOAT . SI:SHORT-FLOAT-P)
429 (SIMPLE-ARRAY . SIMPLE-ARRAY-P)
430 (SIMPLE-STRING . SIMPLE-STRING-P)
431 (SIMPLE-VECTOR . SIMPLE-VECTOR-P)
432 (SINGLE-FLOAT . SI:SINGLE-FLOAT-P)
433 (STREAM . STREAMP)
434 (STRING . STRINGP)
435 (STRUCTURE . SYS:STRUCTUREP)
436 (SYMBOL . SYMBOLP)
437 #+sse2 (EXT:SSE-PACK . EXT:SSE-PACK-P)
438 #+sse2 (EXT:INT-SSE-PACK . EXT:SSE-PACK-P)
439 #+sse2 (EXT:FLOAT-SSE-PACK . EXT:SSE-PACK-P)
440 #+sse2 (EXT:DOUBLE-SSE-PACK . EXT:SSE-PACK-P)
441 (T . CONSTANTLY-T)
442 (VECTOR . VECTORP))))
443
444 (dolist (l +known-typep-predicates+)
445 (put-sysprop (car l) 'TYPE-PREDICATE (cdr l)))
446
447 (defconstant +upgraded-array-element-types+
448 '#.(append '(NIL BASE-CHAR #+unicode CHARACTER BIT EXT:BYTE8 EXT:INTEGER8)
449 #+:uint16-t '(EXT:BYTE16 EXT:INTEGER16)
450 #+:uint32-t '(EXT:BYTE32 EXT:INTEGER32)
451 (when (< 32 cl-fixnum-bits 64)
452 '(EXT::CL-INDEX FIXNUM))
453 #+:uint64-t '(EXT:BYTE64 EXT:INTEGER64)
454 (when (< 64 cl-fixnum-bits)
455 '(EXT::CL-INDEX FIXNUM))
456 '(SINGLE-FLOAT DOUBLE-FLOAT LONG-FLOAT)
457 #+complex-float '(si:complex-single-float
458 si:complex-double-float
459 si:complex-long-float)
460 '(t)))
461
462 (defun upgraded-array-element-type (element-type &optional env)
463 (let* ((hash (logand 127 (si:hash-eql element-type)))
464 (record (aref *upgraded-array-element-type-cache* hash)))
465 (declare (type (integer 0 127) hash))
466 (if (and record (eq (car record) element-type))
467 (cdr record)
468 (let ((answer (if (member element-type +upgraded-array-element-types+
469 :test #'eq)
470 element-type
471 (dolist (v +upgraded-array-element-types+ 'T)
472 (when (subtypep element-type v env)
473 (return v))))))
474 (setf (aref *upgraded-array-element-type-cache* hash)
475 (cons element-type answer))
476 answer))))
477
478 (defun upgraded-complex-part-type (real-type &optional env)
479 ;; ECL does not have specialized complex types. If we had them, the
480 ;; code would look as follows
481 ;; (dolist (v '(INTEGER RATIO RATIONAL SINGLE-FLOAT DOUBLE-FLOAT FLOAT REAL)
482 ;; (error "~S is not a valid part type for a complex." real-type))
483 ;; (when (subtypep real-type v)
484 ;; (return v))))
485 #+complex-float
486 (cond ((subtypep real-type 'null env) nil)
487 ((subtypep real-type 'rational env) 'rational)
488 ((subtypep real-type 'single-float env) 'single-float)
489 ((subtypep real-type 'double-float env) 'double-float)
490 ((subtypep real-type 'long-float env) 'long-float)
491 ((subtypep real-type 'float env) 'float)
492 ((subtypep real-type 'real env) 'real)
493 (t (error "~S is not a valid part type for a complex." real-type)))
494 #-complex-float
495 (cond ((subtypep real-type 'null env) nil)
496 ((subtypep real-type 'real env) 'real)
497 (t (error "~S is not a valid part type for a complex." real-type))))
498
499 (defun in-interval-p (x interval)
500 (declare (si::c-local))
501 (let* (low high)
502 (if (endp interval)
503 (setq low '* high '*)
504 (if (endp (cdr interval))
505 (setq low (car interval) high '*)
506 (setq low (car interval) high (second interval))))
507 (cond ((eq low '*))
508 ((consp low)
509 (when (<= x (car low)) (return-from in-interval-p nil)))
510 ((when (< x low) (return-from in-interval-p nil))))
511 (cond ((eq high '*))
512 ((consp high)
513 (when (>= x (car high)) (return-from in-interval-p nil)))
514 ((when (> x high) (return-from in-interval-p nil))))
515 (return-from in-interval-p t)))
516
517 (defun error-type-specifier (type)
518 (declare (si::c-local))
519 (error "~S is not a valid type specifier." type))
520
521 (defun match-dimensions (array pat)
522 (declare (si::c-local))
523 (or (eq pat '*)
524 (let ((rank (array-rank array)))
525 (cond ((numberp pat) (= rank pat))
526 ((listp pat)
527 (dotimes (i rank (null pat))
528 (unless (and (consp pat)
529 (or (eq (car pat) '*)
530 (eql (array-dimension array i) (car pat))))
531 (return nil))
532 (setq pat (cdr pat))))
533 ((atom pat)
534 (error "~S does not describe array dimensions." pat))))))
535
536 (defun typep (object type &optional env &aux tp i c)
537 "Args: (object type)
538 Returns T if X belongs to TYPE; NIL otherwise."
539 (when env
540 (setf type (search-type-in-env type env)))
541 (cond ((symbolp type)
542 (let ((f (get-sysprop type 'TYPE-PREDICATE)))
543 (if f
544 (return-from typep (funcall f object))
545 (setq tp type i nil))))
546 ((consp type)
547 (setq tp (car type) i (cdr type)))
548 ((sys:instancep type)
549 (return-from typep (si::subclassp (class-of object) type)))
550 (t
551 (error-type-specifier type)))
552 (case tp
553 ((EQL MEMBER) (and (member object i) t))
554 (NOT (not (typep object (car i) env)))
555 (OR (dolist (e i)
556 (when (typep object e env) (return t))))
557 (AND (dolist (e i t)
558 (unless (typep object e env) (return nil))))
559 (SATISFIES (funcall (car i) object))
560 ((T *) t)
561 ((NIL) nil)
562 (BIGNUM (and (integerp object) (not (si::fixnump object))))
563 (STANDARD-CHAR
564 (and (characterp object) (standard-char-p object)))
565 (INTEGER
566 (and (integerp object) (in-interval-p object i)))
567 (RATIO
568 (and (ratiop object) (in-interval-p object i)))
569 (RATIONAL
570 (and (rationalp object) (in-interval-p object i)))
571 (FLOAT
572 (and (floatp object) (in-interval-p object i)))
573 (REAL
574 (and (or (rationalp object) (floatp object)) (in-interval-p object i)))
575 (SHORT-FLOAT
576 (and (si:short-float-p object) (in-interval-p object i)))
577 (SINGLE-FLOAT
578 (and (si:single-float-p object) (in-interval-p object i)))
579 (DOUBLE-FLOAT
580 (and (si:double-float-p object) (in-interval-p object i)))
581 (LONG-FLOAT
582 (and (si:long-float-p object) (in-interval-p object i)))
583 (COMPLEX
584 (and (complexp object)
585 (or (null i)
586 ;; type specifier may be i.e (complex integer) so we
587 ;; should check both real and imag part (disregarding
588 ;; the fact that both have the same upgraded type).
589 (and (typep (realpart object) (car i) env)
590 (typep (imagpart object) (car i) env)))
591 ))
592 (SEQUENCE (or (listp object) (vectorp object)))
593 (CONS (and (consp object)
594 (or (endp i)
595 (let ((car-type (first i)))
596 (or (eq car-type '*) (typep (car object) car-type env))))
597 (or (endp (cdr i))
598 (let ((cdr-type (second i)))
599 (or (eq cdr-type '*) (typep (cdr object) cdr-type env))))))
600 (BASE-STRING
601 (and (base-string-p object)
602 (or (null i) (match-dimensions object i))))
603 (STRING
604 (and (stringp object)
605 (or (null i) (match-dimensions object i))))
606 (BIT-VECTOR
607 (and (bit-vector-p object)
608 (or (null i) (match-dimensions object i))))
609 (SIMPLE-BASE-STRING
610 (and (base-string-p object)
611 (simple-string-p object)
612 (or (null i) (match-dimensions object i))))
613 (SIMPLE-STRING
614 (and (simple-string-p object)
615 (or (null i) (match-dimensions object i))))
616 (SIMPLE-BIT-VECTOR
617 (and (simple-bit-vector-p object)
618 (or (null i) (match-dimensions object i))))
619 (SIMPLE-VECTOR
620 (and (simple-vector-p object)
621 (or (null i) (match-dimensions object i))))
622 (COMPLEX-ARRAY
623 (and (complex-array-p object)
624 (or (endp i) (eq (car i) '*)
625 ;; (car i) needs expansion
626 (eq (array-element-type object)
627 (upgraded-array-element-type (car i))))
628 (or (endp (cdr i)) (match-dimensions object (second i)))))
629 (SIMPLE-ARRAY
630 (and (simple-array-p object)
631 (or (endp i) (eq (car i) '*)
632 ;; (car i) needs expansion
633 (eq (array-element-type object)
634 (upgraded-array-element-type (car i))))
635 (or (endp (cdr i)) (match-dimensions object (second i)))))
636 (ARRAY
637 (and (arrayp object)
638 (or (endp i) (eq (car i) '*)
639 ;; Or the element type of object should be EQUAL to (car i).
640 ;; Is this too strict?
641 (eq (array-element-type object)
642 (upgraded-array-element-type (car i))))
643 (or (endp (cdr i)) (match-dimensions object (second i)))))
644 (t
645 (cond ((get-sysprop tp 'DEFTYPE-DEFINITION)
646 (typep object (funcall (get-sysprop tp 'DEFTYPE-DEFINITION) (cons tp i) env)))
647 ((consp i)
648 (error-type-specifier type))
649 ((setq c (find-class type nil))
650 ;; Follow the inheritance chain
651 (si::subclassp (class-of object) c))
652 (t
653 (error-type-specifier type))))))
654
655 (defun subclassp (low high)
656 (or (eq low high)
657 (member high (sys:instance-ref low clos::+class-precedence-list-ndx+)
658 :test #'eq))) ; (class-precedence-list low)
659
660 (defun of-class-p (object class)
661 (declare (optimize (speed 3) (safety 0)))
662 (macrolet ((class-precedence-list (x)
663 `(si::instance-ref ,x clos::+class-precedence-list-ndx+))
664 (class-name (x)
665 `(si::instance-ref ,x clos::+class-name-ndx+)))
666 (let* ((x-class (class-of object)))
667 (declare (class x-class))
668 (if (eq x-class class)
669 t
670 (let ((x-cpl (class-precedence-list x-class)))
671 (if (instancep class)
672 (member class x-cpl :test #'eq)
673 (dolist (c x-cpl)
674 (declare (class c))
675 (when (eq (class-name c) class)
676 (return t)))))))))
677
678 #+(and clos ecl-min)
679 (defun clos::classp (foo)
680 (declare (ignore foo))
681 nil)
682
683 (defun flatten-function-types (type env)
684 "Replace all compound function types by the atomic function type.
685 For example (flatten-function-types '(function (symbol) symbol)) ->
686 'function."
687 (cond ((and (consp type) (eq (first type) 'FUNCTION))
688 'FUNCTION)
689 ((and (consp type) (member (first type) '(NOT OR AND CONS)))
690 (list* (first type) (mapcar #'(lambda (tp) (flatten-function-types tp env))
691 (rest type))))
692 (t
693 (let ((type-name (if (consp type) (first type) type))
694 (type-args (if (consp type) (rest type) nil)))
695 (ext:if-let ((fd (get-sysprop type-name 'DEFTYPE-DEFINITION)))
696 ;; In the case of custom types, we expand the type
697 ;; definition only if necessary.
698 (let* ((type-alias (funcall fd (cons type-name type-args) env))
699 (flattened-type (flatten-function-types type-alias env)))
700 (if (eq flattened-type type-alias)
701 type
702 flattened-type))
703 type)))))
704
705 ;;; ----------------------------------------------------------------------------
706 ;;; NORMALIZE-TYPE
707 ;;; ----------------------------------------------------------------------------
708 ;;; NORMALIZE-TYPE normalizes the type using the DEFTYPE definitions.
709 ;;; The result is a pair of values
710 ;;; VALUE-1 = normalized type name or object
711 ;;; VALUE-2 = normalized type arguments or nil
712 (defun normalize-type (type env &aux tp i fd)
713 ;; Loops until the car of type has no DEFTYPE definition.
714 (cond ((symbolp type)
715 (if (setq fd (get-sysprop type 'DEFTYPE-DEFINITION))
716 (normalize-type (funcall fd (list type) env) env)
717 (values type nil)))
718 ((clos::classp type) (values type nil))
719 ((atom type)
720 (error-type-specifier type))
721 ((progn
722 (setq tp (car type) i (cdr type))
723 (setq fd (get-sysprop tp 'DEFTYPE-DEFINITION)))
724 (normalize-type (funcall fd type env) env))
725 ((and (eq tp 'INTEGER) (consp (cadr i)))
726 (values tp (list (car i) (1- (caadr i)))))
727 (t (values tp i))))
728
729 (defun expand-deftype (type env)
730 (let (base args)
731 (if (atom type)
732 (setf base type
733 args nil)
734 (setf base (car type)
735 args (cdr type)))
736 (let ((fn (get-sysprop base 'DEFTYPE-DEFINITION)))
737 (if fn
738 (expand-deftype (funcall fn (cons base args) env) env)
739 type))))
740
741 ;;; ----------------------------------------------------------------------------
742 ;;; COERCE
743 ;;; ----------------------------------------------------------------------------
744
745 (defun coerce (object type &aux aux)
746 "Args: (x type)
747 Coerces X to an object of the specified type, if possible. Signals an error
748 if not possible."
749 (when (typep object type)
750 ;; Just return as it is.
751 (return-from coerce object))
752 (flet ((fail ()
753 (error "Cannot coerce ~S to type ~S." object type)))
754 (let ((type (expand-deftype type nil)))
755 (cond ((atom type)
756 (case type
757 ((T) object)
758 (LIST
759 (do ((io (make-seq-iterator object) (seq-iterator-next object io))
760 (l nil (cons (seq-iterator-ref object io) l)))
761 ((null io) l)))
762 ((CHARACTER BASE-CHAR) (character object))
763 (FLOAT (float object))
764 (SHORT-FLOAT (float object 0.0S0))
765 (SINGLE-FLOAT (float object 0.0F0))
766 (DOUBLE-FLOAT (float object 0.0D0))
767 (LONG-FLOAT (float object 0.0L0))
768 (COMPLEX (complex (realpart object) (imagpart object)))
769 #+complex-float
770 (si:complex-single-float
771 (complex (coerce (realpart object) 'single-float)
772 (coerce (imagpart object) 'single-float)))
773 #+complex-float
774 (si:complex-double-float
775 (complex (coerce (realpart object) 'double-float)
776 (coerce (imagpart object) 'double-float)))
777 #+complex-float
778 (si:complex-long-float
779 (complex (coerce (realpart object) 'long-float)
780 (coerce (imagpart object) 'long-float)))
781 (FUNCTION (coerce-to-function object))
782 ((VECTOR SIMPLE-VECTOR #+unicode SIMPLE-BASE-STRING SIMPLE-STRING
783 #+unicode BASE-STRING STRING BIT-VECTOR SIMPLE-BIT-VECTOR)
784 (concatenate type object))
785 (t
786 (if (or (listp object) (vectorp object))
787 (concatenate type object)
788 (fail)))))
789 ((eq (setq aux (first type)) 'COMPLEX)
790 (if type
791 (complex (coerce (realpart object) (second type))
792 (coerce (imagpart object) (second type)))
793 (complex (realpart object) (imagpart object))))
794 ((member aux '(SHORT-FLOAT SINGLE-FLOAT DOUBLE-FLOAT LONG-FLOAT FLOAT))
795 (setq aux (coerce object aux))
796 (unless (typep aux type)
797 (fail))
798 aux)
799 ((eq aux 'AND)
800 (dolist (type (rest type))
801 (setq aux (coerce aux type)))
802 (unless (typep aux type)
803 (fail))
804 aux)
805 ((or (listp object) (vectorp object))
806 (concatenate type object))
807 (t
808 (fail))))))
809
810 ;;; ----------------------------------------------------------------------------
811 ;;; SUBTYPEP
812 ;;; ----------------------------------------------------------------------------
813 ;;;
814 ;;; TYPES LATTICE
815 ;;;
816 ;;; Following the paper written by Henry G. Baker: "A Decision Procedure for
817 ;;; Common Lisp's SUBTYPEP Predicate".
818 ;;;
819 ;;; The algorithm works as follows. Types are identified with sets. Some sets
820 ;;; are elementary, in the sense that other types may be expressed as
821 ;;; combination of them. We partition these sets into FAMILIES (kingdoms):
822 ;;;
823 ;;; Built-in objects --- Hash tables, etc
824 ;;; Intervals --- (INTEGER a b), (REAL a b), etc
825 ;;; Arrays --- (ARRAY * (2)), etc
826 ;;; Classes
827 ;;;
828 ;;; When passed a type specifier, ECL canonicalizes it: it decomposes the type
829 ;;; into the most elementary sets, assigns a unique bit pattern (TAG) to each
830 ;;; of these sets, and builds a composite tag for the type by LOGIOR.
831 ;;; Operations between these sets reduce to logical operations between these
832 ;;; bit patterns. Given types T1, T2 and a function which produces tags f(T)
833 ;;;
834 ;;; f((AND T1 T2)) = (LOGIAND f(T1) f(T2))
835 ;;; f((OR T1 T2)) = (LOGIOR f(T1) f(T2))
836 ;;; f((NOT T1)) = (LOGNOT f(T1))
837 ;;;
838 ;;; However, tags are not permanent: whenever a new type is registered, the
839 ;;; tag associated to a type may be changed (for instance, because new
840 ;;; elementary sets are discovered, which also belong to existing types).
841 ;;; ----------------------------------------------------------------------------
842
843 (defparameter *save-types-database* nil)
844
845 (defparameter *highest-type-tag*
846 #+ecl-min #B1
847 #-ecl-min '#.*highest-type-tag*)
848
849 ;;; Built-in tags for the top and the bottom types.
850 (defconstant +built-in-tag-t+ -1)
851 (defconstant +built-in-tag-nil+ 0)
852 (defparameter *intervals-mask* #B1)
853
854 (defparameter *member-types*
855 #+ecl-min NIL
856 #-ecl-min '#.*member-types*)
857
858 (defparameter *elementary-types*
859 #+ecl-min
860 '()
861 #-ecl-min
862 '#.*elementary-types*)
863
864 ;;; The definition is commented out because DEFSTRUCT is not available yet
865 ;;; during the bootstrap procedure, so we open-code the definition below.
866 #+ (or)
867 (defstruct (member-type (:type list))
868 (object (error "Argument :OBJECT is required.") :read-only t)
869 (tag (error "Argument :TAG is required.") :type integer))
870
871 (defun make-member-type (&key object tag)
872 (list object tag))
873
874 (defun find-member-type (object)
875 (assoc object *member-types*))
876
877 (setf (fdefinition 'member-type-object) #'first)
878 (setf (fdefinition 'member-type-tag) #'second)
879 (defsetf member-type-tag (mtype) (new-tag)
880 `(rplaca (cdr ,mtype) ,new-tag))
881
882 ;;; The definition is commented out because DEFSTRUCT is not available yet
883 ;;; during the bootstrap procedure, so we open-code the definition below.
884 #+ (or)
885 (defstruct (elementary-type (:type list))
886 (spec (error "Argument :SPEC is required.") :read-only t)
887 (tag (error "Argument :TAG is required.") :type integer))
888
889 (defun make-elementary-type (&key spec tag)
890 (declare (si::c-local))
891 (list spec tag))
892
893 (defun find-elementary-type (spec test)
894 (declare (si::c-local))
895 (find spec *elementary-types* :key #'elementary-type-spec :test test))
896
897 (setf (fdefinition 'elementary-type-spec) #'first)
898 (setf (fdefinition 'elementary-type-tag) #'second)
899 (defsetf elementary-type-tag (etype) (new-tag)
900 `(rplaca (cdr ,etype) ,new-tag))
901
902 ;;; INV The function MAYBE-SAVE-TYPES ensures that we operate on fresh conses
903 ;;; instead of modifying *MEMBER-TYPES* and *ELEMENTARY-TYPES*.
904 (defmacro with-type-database (() &body body)
905 `(let ((*highest-type-tag* *highest-type-tag*)
906 (*save-types-database* t)
907 (*member-types* *member-types*)
908 (*elementary-types* *elementary-types*))
909 ,@body))
910
911 (defun new-type-tag ()
912 (declare (si::c-local))
913 (prog1 *highest-type-tag*
914 (setq *highest-type-tag* (ash *highest-type-tag* 1))))
915
916 (defun push-new-type (type tag)
917 (declare (si::c-local)
918 (ext:assume-no-errors))
919 (dolist (i *member-types*)
920 (declare (cons i))
921 (when (typep (member-type-object i) type)
922 (setq tag (logior tag (member-type-tag i)))))
923 (push (make-elementary-type :spec type :tag tag) *elementary-types*)
924 tag)
925
926 ;; Find out the tag for a certain type, if it has been already registered.
927 ;;
928 (defun find-registered-tag (type &optional (test #'equal))
929 (declare (si::c-local))
930 (when-let ((etype (find-elementary-type type test)))
931 (elementary-type-tag etype)))
932
933 ;;; Make and register a new tag for a certain type.
934 (defun make-registered-tag (type same-kingdom-p type-<=)
935 (multiple-value-bind (tag-super tag-sub)
936 (find-type-bounds type same-kingdom-p type-<=)
937 (if (null tag-super)
938 (push-new-type type tag-sub)
939 (let ((tag (new-type-tag)))
940 (update-types tag-super tag)
941 (setf tag (logior tag tag-sub))
942 (push-new-type type tag)))))
943
944 ;; We are going to make changes in the types database. Save a copy if this
945 ;; will cause trouble.
946 ;;
947 (defun maybe-save-types ()
948 (declare (si::c-local))
949 (when *save-types-database*
950 (setf *save-types-database* nil
951 *elementary-types* (copy-tree *elementary-types*)
952 *member-types* (copy-tree *member-types*))))
953
954 ;; We have created and tagged a new type (NEW-TAG). However, there are
955 ;; composite and synonym types registered around which are supertypes of
956 ;; this type and need to be tagged. TYPE-MASK is a bit pattern which helps
957 ;; us in recognizing these supertypes.
958 ;;
959 (defun update-types (type-mask new-tag)
960 (declare (ext:assume-no-errors))
961 (maybe-save-types)
962 (dolist (i *elementary-types*)
963 (unless (zerop (logand (elementary-type-tag i) type-mask))
964 (setf (elementary-type-tag i)
965 (logior new-tag (elementary-type-tag i))))))
966
967 ;;; FIND-TYPE-BOUNDS => (VALUES TAG-SUPER TAG-SUB)
968 ;;;
969 ;;; This function computes two tags:
970 ;;;
971 ;;; TAG-SUPER is the union-type which is a supertype of the supplied one within
972 ;;; its own kingdom. To achieve that we compute the union of all supertypes and
973 ;;; then remove from it unions of all subtypes and all disjoint types.
974 ;;;
975 ;;; TAG-SUB is the union-type which is a subtype of the supplied one within its
976 ;;; own kingdom.
977 ;;;
978 ;;; If the function finds an equivalent type with a different name, then it
979 ;;; returns (VALUES NIL EQUIVALENT-TYPE-TAG). This is a clue that there is no
980 ;;; need to extend the type's bit-vector.
981 ;;;
982 ;;; All types in the family must be disjoint (sub-family wise) or have a total
983 ;;; order to avoid aliasing problem in the binary vector.
984 (defun find-type-bounds (type in-our-family-p type-<=)
985 (declare (si::c-local)
986 (optimize (safety 0))
987 (function in-our-family-p type-<=))
988 (let ((subtype-tag +built-in-tag-nil+)
989 (disjoint-tag +built-in-tag-nil+)
990 (supertype-tag +built-in-tag-nil+))
991 (dolist (i *elementary-types*)
992 (declare (cons i))
993 (let ((other-type (elementary-type-spec i))
994 (other-tag (elementary-type-tag i)))
995 (when (funcall in-our-family-p other-type)
996 (let ((other-sup-p (funcall type-<= type other-type))
997 (other-sub-p (funcall type-<= other-type type)))
998 (cond ((and other-sup-p other-sub-p)
999 (return-from find-type-bounds
1000 (values nil other-tag)))
1001 (other-sup-p
1002 (setq supertype-tag (logior other-tag supertype-tag)))
1003 (other-sub-p
1004 (setq subtype-tag (logior other-tag subtype-tag)))
1005 (t
1006 (setq disjoint-tag (logior disjoint-tag other-tag))))))))
1007 (unless (logand disjoint-tag subtype-tag)
1008 (error "Some types in the family does not have a strict total order."))
1009 (values (logandc2 supertype-tag (logior disjoint-tag subtype-tag))
1010 subtype-tag)))
1011
1012 ;;; A new type is to be registered, which is not simply a composition of
1013 ;;; previous types. A new tag has to be created, and all supertypes are to be
1014 ;;; tagged. Here we have to distinguish two possibilities: first, a supertype
1015 ;;; may belong to the same family (intervals, arrays, etc); second, some
1016 ;;; supertypes may be basic types (NUMBER is a supertype for (INTEGER 0 2),
1017 ;;; for instance). The first possibility is detected with the comparison
1018 ;;; procedure, TYPE-<=; the second possibility is detected by means of tags.
1019 ;;;
1020 (defun register-type (type in-our-family-p type-<=)
1021 (declare (si::c-local)
1022 (optimize (safety 0))
1023 (function in-our-family-p type-<=))
1024 (or (find-registered-tag type)
1025 (make-registered-tag type in-our-family-p type-<=)))
1026
1027 ;;; ----------------------------------------------------------------------------
1028 ;;; MEMBER types.
1029 ;;;
1030 ;;; We register this object in a separate list, *MEMBER-TYPES*, and tag all
1031 ;;; types to which it belongs. We need to treat three cases separately:
1032 ;;;
1033 ;;; 1. Ordinary types, via simple-member-type, check the objects against all
1034 ;;; pre-registered types, adding their tags.
1035 ;;;
1036 ;;; 2. Ordinary numbers, are translated into intervals.
1037 ;;;
1038 ;;; 3. Floating point zeros, have to be treated separately. This
1039 ;;; is done by assigning a special tag to -0.0 and translating
1040 ;;;
1041 ;;; (MEMBER 0.0) = (AND (float-type 0.0 0.0) (NOT (MEMBER -0.0)))
1042 ;;;
1043 (defun register-member-type (object)
1044 ;(declare (si::c-local))
1045 (let ((mtype (find-member-type object)))
1046 (cond (mtype
1047 (member-type-tag mtype))
1048 ((not (realp object))
1049 (simple-member-type object))
1050 ((and (floatp object) (zerop object))
1051 #.(if (eql (- 0.0) 0.0)
1052 '(number-member-type object)
1053 '(if (minusp (float-sign object))
1054 (simple-member-type object)
1055 (logandc2 (number-member-type object)
1056 (register-member-type (- object))))))
1057 (t
1058 (number-member-type object)))))
1059
1060 (defun simple-member-type (object)
1061 (declare (si::c-local)
1062 (ext:assume-no-errors))
1063 (let ((tag (new-type-tag)))
1064 (maybe-save-types)
1065 (push (make-member-type :object object :tag tag) *member-types*)
1066 (dolist (i *elementary-types*)
1067 (let ((type (elementary-type-spec i)))
1068 (when (typep object type)
1069 (setf (elementary-type-tag i)
1070 (logior tag (elementary-type-tag i))))))
1071 tag))
1072
1073 ;;; We convert number into intervals, so that (AND INTEGER (NOT (EQL 10))) is
1074 ;;; detected as a subtype of (OR (INTEGER * 9) (INTEGER 11 *)).
1075 (defun number-member-type (object)
1076 (let* ((base-type (if (integerp object) 'INTEGER (type-of object)))
1077 (type (list base-type object object)))
1078 (or (find-registered-tag type)
1079 (canonical-interval-type type))))
1080
1081 ;;; ----------------------------------------------------------------------------
1082 ;;; SATISFIES types. Here we should signal some error which is caught somewhere
1083 ;;; up, to denote failure of the decision procedure.
1084 ;;;
1085 (defun register-satisfies-type (type)
1086 (declare (si::c-local)
1087 (ignore type))
1088 (throw '+canonical-type-failure+ 'satisfies))
1089
1090 ;;; ----------------------------------------------------------------------------
1091 ;;; CLOS classes and structures.
1092 ;;;
1093 (defun register-class (class env)
1094 (declare (si::c-local)
1095 (notinline class-name))
1096 (or (find-registered-tag class)
1097 ;; We do not need to register classes which belong to the core type
1098 ;; system of LISP (ARRAY, NUMBER, etc).
1099 (let ((name (class-name class)))
1100 (and name
1101 (eq class (find-class name 'nil))
1102 (or (find-registered-tag name)
1103 (find-built-in-tag name env))))
1104 (and (not (clos::class-finalized-p class))
1105 (throw '+canonical-type-failure+ nil))
1106 (register-type class
1107 #'(lambda (c)
1108 (or (si::instancep c)
1109 (symbolp c)))
1110 #'(lambda (c1 c2)
1111 (when (symbolp c1)
1112 (setq c1 (find-class c1 nil)))
1113 (when (symbolp c2)
1114 (setq c2 (find-class c2 nil)))
1115 (and c1 c2 (si::subclassp c1 c2))))))
1116
1117 ;;; ----------------------------------------------------------------------------
1118 ;;; ARRAY types.
1119 ;;;
1120 (defun register-array-type (type env)
1121 (declare (si::c-local))
1122 (multiple-value-bind (array-class elt-type dimensions)
1123 (parse-array-type type env)
1124 (cond ((eq elt-type '*)
1125 (canonical-type `(OR ,@(mapcar #'(lambda (type)
1126 `(,array-class ,type ,dimensions))
1127 +upgraded-array-element-types+))
1128 env))
1129 ((find-registered-tag (setq type (list array-class elt-type dimensions))))
1130 (t
1131 #+ (or)
1132 (when (and (consp dimensions) (> (count-if #'numberp dimensions) 1))
1133 (dotimes (i (length dimensions))
1134 (when (numberp (elt dimensions i))
1135 (let ((dims (make-list (length dimensions) :initial-element '*)))
1136 (setf (elt dims i) (elt dimensions i))
1137 (register-type (list array-class elt-type dims)
1138 #'array-type-p #'array-type-<=)))))
1139 (register-type type #'array-type-p #'array-type-<=)))))
1140
1141 ;;;
1142 ;;; We look for the most specialized type which is capable of containing this
1143 ;;; object. LIST always contains 'T, so that this procedure never fails. It is
1144 ;;; faster than UPGRADED-... because we use the tags of types that have been
1145 ;;; already registered.
1146 ;;;
1147 (defun fast-upgraded-array-element-type (type env)
1148 (declare (si::c-local))
1149 (cond ((eql type '*) '*)
1150 ((member type +upgraded-array-element-types+ :test #'eq)
1151 type)
1152 (t
1153 (dolist (other-type +upgraded-array-element-types+ 'T)
1154 (when (fast-subtypep type other-type env)
1155 (return other-type))))))
1156
1157 ;;;
1158 ;;; This canonicalizes the array type into the form
1159 ;;;
1160 ;;; ({COMPLEX-ARRAY | SIMPLE-ARRAY} {elt-type | '*} {'* | (['*]*)})
1161 ;;;
1162 ;;; ELT-TYPE is the upgraded element type of the input.
1163 ;;;
1164 (defun parse-array-type (input env)
1165 (declare (si::c-local))
1166 (let* ((type input)
1167 (name (pop type))
1168 (elt-type (fast-upgraded-array-element-type (if type (pop type) '*) env))
1169 (dims (if type (pop type) '*)))
1170 (when type
1171 (error "Wrong array type designator ~S." input))
1172 (cond ((numberp dims)
1173 (unless (< -1 dims array-rank-limit)
1174 (error "Wrong rank size array type ~S." input))
1175 (setq dims (nthcdr (- array-rank-limit dims)
1176 '#.(make-list array-rank-limit :initial-element '*))))
1177 ((consp dims)
1178 (dolist (i dims)
1179 (unless (or (eq i '*)
1180 (and (integerp i) (< -1 i array-dimension-limit)))
1181 (error "Wrong dimension size in array type ~S." input)))))
1182 (values name elt-type dims)))
1183
1184 ;;;
1185 ;;; This function checks whether the array type T1 is a subtype of the array
1186 ;;; type T2.
1187 ;;;
1188 (defun array-type-<= (t1 t2)
1189 (unless (and (eq (first t1) (first t2))
1190 (eq (second t1) (second t2)))
1191 (return-from array-type-<= nil))
1192 (let ((dim (third t1))
1193 (pat (third t2)))
1194 (cond ((eq pat '*) t)
1195 ((eq dim '*) nil)
1196 (t (do ((a dim (cdr a))
1197 (b pat (cdr b)))
1198 ((or (endp a)
1199 (endp b)
1200 (not (or (eq (car b) '*)
1201 (eql (car b) (car a)))))
1202 (and (null a) (null b)))
1203 )))))
1204
1205 (defun array-type-p (type)
1206 (and (consp type)
1207 (member (first type) '(COMPLEX-ARRAY SIMPLE-ARRAY))))
1208
1209 ;;; ----------------------------------------------------------------------------
1210 ;;; INTERVALS:
1211 ;;;
1212 ;;; Arbitrary intervals may be defined as the union or intersection of intervals
1213 ;;; that are semi-infinite, of the form (NUMBER-TYPE B *), where B is either a
1214 ;;; real number, a list with one real number or a symbol *.
1215 ;;;
1216 ;;; Any other interval, may be defined using these. For instance:
1217 ;;;
1218 ;;; (INTEGER 0 2) = (AND (INTEGER 0 *) (NOT (INTEGER (2) *)))
1219 ;;; (SHORT-FLOAT (0.2) (2)) = (AND (SHORT-FLOAT (0.2) *) (NOT (SHORT-FLOAT 2 *)))
1220 ;;;
1221
1222 (defun numeric-range-p (type)
1223 (and (consp type)
1224 (member (car type)
1225 '(integer ratio short-float single-float double-float long-float))
1226 (or (null (cddr type))
1227 (error "NUMERIC-RANGE-P: ~s is not in the canonical form (TYPE B)." type))))
1228
1229 ;;; Numeric ranges are decided separately depending on the type actual type.
1230 ;;; When ranges belong to different sub-families, then they are disjoint and
1231 ;;; can't be ordered.
1232 (defun numeric-range-<= (i1 i2)
1233 (and (eq (first i1) (first i2))
1234 (bounds-<= (second i2) (second i1))))
1235
1236 (defun canonical-interval-type (interval)
1237 (declare (si::c-local))
1238 (destructuring-bind (type &optional (low '*) (high '*)) interval
1239 (let ((tag-high
1240 (cond ((eq high '*)
1241 +built-in-tag-nil+)
1242 ((eq type 'INTEGER)
1243 (setq high (if (consp high)
1244 (ceiling (first high))
1245 (floor (1+ high))))
1246 (register-interval-type type high))
1247 ((consp high)
1248 (register-interval-type type (first high)))
1249 (t
1250 (register-interval-type type (list high)))))
1251 (tag-low
1252 (cond ((eq low '*)
1253 (register-interval-type type low))
1254 ((eq type 'INTEGER)
1255 (setq low (if (consp low)
1256 (floor (1+ (first low)))
1257 (ceiling low)))
1258 (register-interval-type type low))
1259 (t
1260 (register-interval-type type low)))))
1261 (logandc2 tag-low tag-high))))
1262
1263 (defun register-interval-type (type b)
1264 (declare (si::c-local))
1265 (setq type (list type b))
1266 (or (find-registered-tag type #'equalp)
1267 (make-registered-tag type #'numeric-range-p #'numeric-range-<=)))
1268
1269 ;;; All comparisons between intervals operations may be defined in terms of
1270 ;;;
1271 ;;; (BOUNDS-<= b1 b2)
1272 ;;;
1273 ;;; that checks whether (REAL b2 *) is contained in (REAL b1 *).
1274 (defun bounds-<= (b1 b2)
1275 (cond ((eq b1 '*) t)
1276 ((eq b2 '*) nil)
1277 ((consp b1)
1278 (if (consp b2)
1279 (<= (first b1) (first b2))
1280 (< (first b1) b2)))
1281 ((consp b2)
1282 (<= b1 (first b2)))
1283 (t
1284 (<= b1 b2))))
1285
1286 ;;; ----------------------------------------------------------------------------
1287 ;;; COMPLEX types.
1288 ;;;
1289 ;;; We do not need to register anything, because all possibilities have been
1290 ;;; covered by the definitions above. We only have to bring the type to
1291 ;;; canonical form, which is a union of all specialized complex types that can
1292 ;;; store an element of the corresponding type.
1293 ;;;
1294 ;;; Don't be tempted to do "better" than that. CANONICAL-COMPLEX-TYPE yields
1295 ;;; results for use of SUBTYPEP which has clearly specified to return true when:
1296 ;;; T1 is a subtype of T2 or when the upgraded type specifiers refer to the same
1297 ;;; sets of objects. TYPEP has a different specification and TYPECASE should use
1298 ;;; it. -- jd 2019-04-19
1299 ;;;
1300 (defun canonical-complex-type (complex-type)
1301 (declare (si::c-local))
1302 ;; UPGRADE-COMPLEX-PART-TYPE signals condition when REAL-TYPE is not a
1303 ;; subtype of REAL.
1304 (destructuring-bind (&optional (real-type 'real)) (rest complex-type)
1305 (when (eq real-type '*)
1306 (setq real-type 'real))
1307 (let* ((upgraded-real (upgraded-complex-part-type real-type))
1308 (upgraded-type `(complex ,upgraded-real)))
1309 (or (find-registered-tag upgraded-type)
1310 #+complex-float
1311 (case upgraded-real
1312 (real
1313 (logior (canonical-complex-type '(complex single-float))
1314 (canonical-complex-type '(complex double-float))
1315 (canonical-complex-type '(complex long-float))
1316 (canonical-complex-type '(complex rational))))
1317 (float
1318 (logior (canonical-complex-type '(complex single-float))
1319 (canonical-complex-type '(complex double-float))
1320 (canonical-complex-type '(complex long-float)))))
1321 (register-complex-type upgraded-type)))))
1322
1323 (defun register-complex-type (upgraded-type)
1324 (declare (si::c-local))
1325 (let ((tag (new-type-tag)))
1326 (push-new-type upgraded-type tag)))
1327
1328 ;;----------------------------------------------------------------------
1329 ;; CONS types. Only (CONS T T) and variants, as well as (CONS NIL *), etc
1330 ;; are strictly supported.
1331 ;;
1332 (defun register-cons-type (env &optional (car-type '*) (cdr-type '*))
1333 ;; The problem with the code below is that it does not suport infinite
1334 ;; recursion. Instead we just canonicalize everything to CONS, irrespective
1335 ;; of whether the arguments are valid types or not!
1336 #+(or)
1337 (canonical-type 'CONS env)
1338 (let ((car-tag (if (eq car-type '*) +built-in-tag-t+ (canonical-type car-type env)))
1339 (cdr-tag (if (eq cdr-type '*) +built-in-tag-t+ (canonical-type cdr-type env))))
1340 (cond ((or (= car-tag +built-in-tag-nil+) (= cdr-tag +built-in-tag-nil+))
1341 +built-in-tag-nil+)
1342 ((and (= car-tag +built-in-tag-t+) (= cdr-tag +built-in-tag-t+))
1343 (canonical-type 'CONS env))
1344 (t
1345 (throw '+canonical-type-failure+ 'CONS)))))
1346
1347 ;;; ----------------------------------------------------------------------------
1348 ;;; FIND-BUILT-IN-TAG
1349 ;;;
1350 ;;; This function computes the tags for all builtin types. We used to do this
1351 ;;; computation and save it. However, for most cases it seems faster if we just
1352 ;;; repeat it every time we need it, because the list of *elementary-types* is
1353 ;;; kept smaller and *highest-type-tag* may be just a fixnum.
1354 ;;;
1355 ;;; Note 1: There is some redundancy between this and the built-in classes
1356 ;;; definitions. REGISTER-CLASS knows this and calls FIND-BUILT-IN-TAG, which
1357 ;;; has priority. This is because some built-in classes are also interpreted as
1358 ;;; intervals, arrays, etc.
1359 ;;;
1360 ;;; Note 2: All built in types listed here have to be symbols.
1361 ;;;
1362 ;;; Note 3: Each element of +BUILT-IN-TYPE-LIST+ is:
1363 ;;;
1364 ;;; (TYPE-NAME &optional ALIAS-TO SUPERTYPE)
1365 ;;;
1366 ;;; Note 4: The function FIND-BUILT-IN-TAG is always called _after_ the function
1367 ;;; FIND-REGISTERED-TAG. This invariant implies that FIND-BUILT-IN-TAG won't add
1368 ;;; the same TYPE twice to *ELEMENTARY-TYPES*.
1369 ;;;
1370 #+ecl-min
1371 (defconstant +built-in-type-list+
1372 '((SYMBOL)
1373 (KEYWORD NIL SYMBOL)
1374 (PACKAGE)
1375 (COMPILED-FUNCTION)
1376 (FUNCTION (OR COMPILED-FUNCTION GENERIC-FUNCTION))
1377
1378 (INTEGER (INTEGER * *))
1379 (FIXNUM (INTEGER #.most-negative-fixnum #.most-positive-fixnum))
1380 (BIGNUM (OR (INTEGER * (#.most-negative-fixnum))
1381 (INTEGER (#.most-positive-fixnum) *)))
1382 #+short-float (SHORT-FLOAT (SHORT-FLOAT * *))
1383 (SINGLE-FLOAT (SINGLE-FLOAT * *))
1384 (DOUBLE-FLOAT (DOUBLE-FLOAT * *))
1385 (LONG-FLOAT (LONG-FLOAT * *))
1386 (RATIO (RATIO * *))
1387
1388 (RATIONAL (OR INTEGER RATIO))
1389 (FLOAT (OR #+short-float SHORT-FLOAT
1390 SINGLE-FLOAT
1391 DOUBLE-FLOAT
1392 LONG-FLOAT))
1393
1394 (REAL (OR RATIONAL FLOAT))
1395
1396 #+complex-float(SI:COMPLEX-SINGLE-FLOAT (COMPLEX SINGLE-FLOAT))
1397 #+complex-float(SI:COMPLEX-DOUBLE-FLOAT (COMPLEX DOUBLE-FLOAT))
1398 #+complex-float(SI:COMPLEX-LONG-FLOAT (COMPLEX LONG-FLOAT))
1399 #+complex-float(SI:COMPLEX-FLOAT (COMPLEX FLOAT))
1400
1401 (COMPLEX (COMPLEX *))
1402 (NUMBER (OR REAL COMPLEX))
1403
1404 (CHARACTER)
1405 #-unicode
1406 (BASE-CHAR CHARACTER)
1407 #+unicode
1408 (BASE-CHAR NIL CHARACTER)
1409 (STANDARD-CHAR NIL BASE-CHAR)
1410
1411 (CONS)
1412 (NULL (MEMBER NIL))
1413 (LIST (OR CONS (MEMBER NIL)))
1414
1415 (ARRAY (ARRAY * *))
1416 (SIMPLE-ARRAY (SIMPLE-ARRAY * *))
1417 (SIMPLE-VECTOR (SIMPLE-ARRAY T (*)))
1418 (SIMPLE-BIT-VECTOR (SIMPLE-ARRAY BIT (*)))
1419 (VECTOR (ARRAY * (*)))
1420 #-unicode (STRING (ARRAY CHARACTER (*)))
1421 #+unicode (STRING (OR (ARRAY CHARACTER (*)) (ARRAY BASE-CHAR (*))))
1422 #+unicode (BASE-STRING (ARRAY BASE-CHAR (*)))
1423 (SIMPLE-STRING (SIMPLE-ARRAY CHARACTER (*)))
1424 #+unicode (SIMPLE-BASE-STRING (SIMPLE-ARRAY BASE-CHAR (*)))
1425 (BIT-VECTOR (ARRAY BIT (*)))
1426
1427 (SEQUENCE (OR CONS (MEMBER NIL) (ARRAY * (*))))
1428
1429 (HASH-TABLE)
1430 (PATHNAME)
1431 (LOGICAL-PATHNAME NIL PATHNAME)
1432
1433 (BROADCAST-STREAM)
1434 (CONCATENATED-STREAM)
1435 (ECHO-STREAM)
1436 (FILE-STREAM)
1437 (STRING-STREAM)
1438 (SYNONYM-STREAM)
1439 (TWO-WAY-STREAM)
1440 (EXT:SEQUENCE-STREAM)
1441 (EXT:ANSI-STREAM (OR BROADCAST-STREAM CONCATENATED-STREAM ECHO-STREAM
1442 FILE-STREAM STRING-STREAM SYNONYM-STREAM TWO-WAY-STREAM
1443 EXT:SEQUENCE-STREAM))
1444 (STREAM (OR EXT:ANSI-STREAM #+clos-streams GRAY:FUNDAMENTAL-STREAM))
1445 (EXT:VIRTUAL-STREAM (OR STRING-STREAM #+clos-streams GRAY:FUNDAMENTAL-STREAM))
1446
1447 (READTABLE)
1448 #+threads (MP::PROCESS)
1449 #+threads (MP::LOCK)
1450 #+threads (MP::RWLOCK)
1451 #+threads (MP::CONDITION-VARIABLE)
1452 #+threads (MP::SEMAPHORE)
1453 #+threads (MP::BARRIER)
1454 #+threads (MP::MAILBOX)
1455 #+ffi (FOREIGN-DATA)
1456 #+sse2 (EXT:SSE-PACK (OR EXT:INT-SSE-PACK
1457 EXT:FLOAT-SSE-PACK
1458 EXT:DOUBLE-SSE-PACK))
1459 #+sse2 (EXT:INT-SSE-PACK)
1460 #+sse2 (EXT:FLOAT-SSE-PACK)
1461 #+sse2 (EXT:DOUBLE-SSE-PACK)
1462 (CODE-BLOCK)
1463 ))
1464
1465 (defconstant +built-in-types+
1466 (ext:hash-table-fill
1467 (make-hash-table :test 'eq :size 128)
1468 '#.+built-in-type-list+))
1469
1470 (defun find-built-in-tag (name env)
1471 (declare (si::c-local))
1472 ;(assert (null (find-registered-tag name)))
1473 (cond
1474 ((eq name 'T) +built-in-tag-t+)
1475 ((eq name 'NIL) +built-in-tag-nil+)
1476 ((multiple-value-bind (record foundp)
1477 (gethash name +built-in-types+)
1478 (when (null foundp)
1479 (return-from find-built-in-tag))
1480 (ext:if-let ((alias (pop record)))
1481 (canonical-type alias env)
1482 (let* ((strict-supertype (or (first record) 'T))
1483 (strict-supertype-tag (canonical-type strict-supertype env))
1484 (new-type-tag (new-type-tag)))
1485 (unless (eq strict-supertype 't)
1486 (extend-type-tag new-type-tag strict-supertype-tag))
1487 (push-new-type name new-type-tag)))))))
1488
1489 (defun extend-type-tag (tag minimal-supertype-tag)
1490 (declare (si::c-local)
1491 (ext:assume-no-errors))
1492 (dolist (type *elementary-types*)
1493 (let ((other-tag (elementary-type-tag type)))
1494 (when (zerop (logandc2 minimal-supertype-tag other-tag))
1495 (setf (elementary-type-tag type)
1496 (logior tag other-tag))))))
1497
1498 ;;; ----------------------------------------------------------------------------
1499 ;;; CANONICALIZE (removed)
1500 ;;;
1501 ;;; This function takes a type tag and produces a more or less human
1502 ;;; readable representation of the type in terms of elementary types,
1503 ;;; intervals, arrays and classes.
1504 ;;;
1505 #+ (or)
1506 (defun canonicalize (type env)
1507 (with-type-database ()
1508 (let ((tag (canonical-type type env))
1509 (out))
1510 (setq tag (canonical-type type env))
1511 ;;(print-types-database *elementary-types*)
1512 ;;(print-types-database *member-types*)
1513 (dolist (i *member-types*)
1514 (unless (zerop (logand (member-type-tag i) tag))
1515 (push (member-type-object i) out)))
1516 (when out
1517 (setq out `((MEMBER ,@out))))
1518 (dolist (i *elementary-types*)
1519 (unless (zerop (logand (elementary-type-tag i) tag))
1520 ;;(print (list tag (elementary-type-tag i) (logand tag (elementary-type-tag i))))
1521 (push (elementary-type-spec i) out)))
1522 (values tag `(OR ,@out)))))
1523
1524 ;;; ----------------------------------------------------------------------------
1525 ;;; (CANONICAL-TYPE TYPE)
1526 ;;;
1527 ;;; This function registers all types mentioned in the given expression, and
1528 ;;; outputs a code corresponding to the represented type. This function has side
1529 ;;; effects: it destructively modifies the content of *ELEMENTARY-TYPES* and
1530 ;;; *MEMBER-TYPES*.
1531 ;;;
1532 (defun canonical-type (type env)
1533 (declare (notinline clos::classp))
1534 (when env
1535 (setf type (search-type-in-env type env)))
1536 (cond ((find-registered-tag type))
1537 ((eq type 'T) +built-in-tag-t+)
1538 ((eq type 'NIL) +built-in-tag-nil+)
1539 ((symbolp type)
1540 (let ((expander (get-sysprop type 'DEFTYPE-DEFINITION)))
1541 (cond (expander
1542 (canonical-type (funcall expander (list type) env) env))
1543 ((find-built-in-tag type env))
1544 (t (let ((class (find-class type nil)))
1545 (if class
1546 (register-class class env)
1547 (throw '+canonical-type-failure+ nil)))))))
1548 ((consp type)
1549 (case (first type)
1550 (AND (apply #'logand (mapcar #'(lambda (tp) (canonical-type tp env)) (rest type))))
1551 (OR (apply #'logior (mapcar #'(lambda (tp) (canonical-type tp env)) (rest type))))
1552 (NOT (lognot (canonical-type (second type) env)))
1553 ((EQL MEMBER) (apply #'logior (mapcar #'register-member-type (rest type))))
1554 (SATISFIES (register-satisfies-type type))
1555 ((INTEGER RATIO
1556 #+short-float SHORT-FLOAT SINGLE-FLOAT DOUBLE-FLOAT LONG-FLOAT)
1557 (canonical-interval-type type))
1558 ((FLOAT)
1559 (canonical-type `(OR #+short-float
1560 (SHORT-FLOAT ,@(rest type))
1561 (SINGLE-FLOAT ,@(rest type))
1562 (DOUBLE-FLOAT ,@(rest type))
1563 (LONG-FLOAT ,@(rest type)))
1564 env))
1565 ((REAL)
1566 (canonical-type `(OR (INTEGER ,@(rest type))
1567 (RATIO ,@(rest type))
1568 #+short-float
1569 (SHORT-FLOAT ,@(rest type))
1570 (SINGLE-FLOAT ,@(rest type))
1571 (DOUBLE-FLOAT ,@(rest type))
1572 (LONG-FLOAT ,@(rest type)))
1573 env))
1574 ((RATIONAL)
1575 (canonical-type `(OR (INTEGER ,@(rest type))
1576 (RATIO ,@(rest type)))
1577 env))
1578 (COMPLEX
1579 (canonical-complex-type type))
1580 (CONS (apply #'register-cons-type env (rest type)))
1581 (ARRAY (logior (register-array-type `(COMPLEX-ARRAY ,@(rest type)) env)
1582 (register-array-type `(SIMPLE-ARRAY ,@(rest type)) env)))
1583 ((COMPLEX-ARRAY SIMPLE-ARRAY) (register-array-type type env))
1584 ;;(FUNCTION (register-function-type type))
1585 ;;(VALUES (register-values-type type))
1586 (FUNCTION (canonical-type 'FUNCTION env))
1587 (t
1588 (ext:if-let ((expander (get-sysprop (first type) 'DEFTYPE-DEFINITION)))
1589 (canonical-type (funcall expander type env) env)
1590 (or (find-registered-tag (first type) #'eql)
1591 (throw '+canonical-type-failure+ nil))))))
1592 ((clos::classp type)
1593 (register-class type env))
1594 ((and (fboundp 'function-type-p) (function-type-p type))
1595 (register-function-type type))
1596 ((and (fboundp 'values-type-p) (values-type-p type))
1597 (register-values-type type))
1598 (t
1599 (error-type-specifier type))))
1600
1601 (defun safe-canonical-type (type env)
1602 (catch '+canonical-type-failure+
1603 (canonical-type type env)))
1604
1605 (defun fast-subtypep (t1 t2 env)
1606 (declare (si::c-local))
1607 (when (eq t1 t2)
1608 (return-from fast-subtypep (values t t)))
1609 (let* ((tag1 (safe-canonical-type t1 env))
1610 (tag2 (safe-canonical-type t2 env)))
1611 (when (and (numberp tag1) (numberp tag2))
1612 ;; We must call safe-canonical-type again because one of
1613 ;; the calls above could have called UPDATE-TYPES.
1614 (setf tag1 (safe-canonical-type t1 env)
1615 tag2 (safe-canonical-type t2 env)))
1616 (cond ((and (numberp tag1) (numberp tag2))
1617 (values (zerop (logandc2 tag1 tag2)) t))
1618 #+nil
1619 ((null tag1)
1620 (error "Unknown type specifier ~S." t1))
1621 #+nil
1622 ((null tag2)
1623 (error "Unknown type specifier ~S." t2))
1624 (t
1625 (values nil nil)))))
1626
1627 (defun subtypep (t1 t2 &optional env)
1628 ;; One easy case: types are equal
1629 (when (eq t1 t2)
1630 (return-from subtypep (values t t)))
1631 ;; Another easy case: types are classes.
1632 (when (and (instancep t1) (instancep t2)
1633 (clos::classp t1) (clos::classp t2))
1634 (return-from subtypep (values (subclassp t1 t2) t)))
1635 ;; Finally, cached results.
1636 (let* ((cache *subtypep-cache*)
1637 (hash (truly-the (integer 0 255) (logand (hash-eql t1 t2) 255)))
1638 (elt (aref cache hash)))
1639 (when (and elt (eq (caar elt) t1) (eq (cdar elt) t2))
1640 (setf elt (cdr elt))
1641 (return-from subtypep (values (car elt) (cdr elt))))
1642 (with-type-database ()
1643 (multiple-value-bind (test confident)
1644 (fast-subtypep t1 t2 env)
1645 (setf (aref cache hash) (cons (cons t1 t2) (cons test confident)))
1646 (values test confident)))))
1647
1648 (defun fast-type= (t1 t2 env)
1649 (declare (si::c-local))
1650 (when (eq t1 t2)
1651 (return-from fast-type= (values t t)))
1652 (let ((tag1 (safe-canonical-type t1 env))
1653 (tag2 (safe-canonical-type t2 env))
1654 (tag3 (safe-canonical-type 'complex env)))
1655 ;; FAST-TYPE= can't rely on the CANONICAL-TYPE in case of complex
1656 ;; numbers which have an exceptional behavior define for TYPEP not
1657 ;; being consistent with SUBTYPEP. -- jd 2019-04-19
1658 (when (and (numberp tag1) (numberp tag2) (/= tag2 tag3))
1659 ;; We must call safe-canonical-type again because one of
1660 ;; the calls above could have called UPDATE-TYPES.
1661 (setf tag1 (safe-canonical-type t1 env)
1662 tag2 (safe-canonical-type t2 env)))
1663 (cond ((and (numberp tag1) (numberp tag2) (/= tag2 tag3))
1664 (values (= tag1 tag2) t))
1665 #+nil
1666 ((null tag1)
1667 (error "Unknown type specifier ~S." t1))
1668 #+nil
1669 ((null tag2)
1670 (error "Unknown type specifier ~S." t2))
1671 (t
1672 (values nil nil)))))
1673
1674 (defun type= (t1 t2 &optional env)
1675 (with-type-database ()
1676 (fast-type= t1 t2 env)))
1677
1678 (defun search-type-in-env (type env)
1679 (let ((type-name type)
1680 (type-args nil))
1681 (when (consp type)
1682 (setf type-name (first type)
1683 type-args (rest type)))
1684 (dolist (record (car env))
1685 (when (and (consp record)
1686 (eq (first record) :type)
1687 (eq (second record) type-name))
1688 (return-from search-type-in-env
1689 (if (typep (third record) 'function)
1690 (funcall (third record) type-args)
1691 (third record))))))
1692 type)