Changeset 318


Ignore:
Timestamp:
Jan 17, 2004, 7:50:44 PM (21 years ago)
Author:
Gary Byers
Message:

Lots more changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ccl/level-1/l1-typesys.lisp

    r297 r318  
    137137          arglist))
    138138
    139 (eval-when (:compile-toplevel)
    140   (warn "Fix EVAL-WHEN in EXPAND-TYPE-MACRO"))
    141139
    142140(defun expand-type-macro (definer name arglist body env)
     
    144142  (multiple-value-bind (lambda doc)
    145143      (parse-macro-internal name arglist body env '*)
    146       `(eval-when (#|:compile-toplevel|# :load-toplevel :execute)
     144      `(eval-when (:compile-toplevel :load-toplevel :execute)
    147145         (,definer ',name
    148146                   (nfunction ,name ,lambda)
     
    19301928(deftype float-format () `(member ,@float-formats))
    19311929
     1930(defun type-bound-number (x)
     1931  (if (consp x)
     1932      (destructuring-bind (result) x result)
     1933      x))
     1934
    19321935(defun make-numeric-ctype (&key class
    19331936                                format
     
    19371940                                enumerable
    19381941                                predicate)
    1939   (let ((ctype (%istruct 'numeric-ctype
    1940                          (type-class-or-lose 'number)
    1941                          enumerable
    1942                          class
    1943                          format
    1944                          complexp
    1945                          low
    1946                          high
    1947                          predicate)))
    1948     ctype))
     1942  ;; if interval is empty
     1943  (if (and low
     1944           high
     1945           (if (or (consp low) (consp high)) ; if either bound is exclusive
     1946             (>= (type-bound-number low) (type-bound-number high))
     1947             (> low high)))
     1948    *empty-type*
     1949    (multiple-value-bind (canonical-low canonical-high)
     1950        (case class
     1951          (integer
     1952           ;; INTEGER types always have their LOW and HIGH bounds
     1953           ;; represented as inclusive, not exclusive values.
     1954           (values (if (consp low)
     1955                     (1+ (type-bound-number low))
     1956                     low)
     1957                   (if (consp high)
     1958                     (1- (type-bound-number high))
     1959                     high)))
     1960          (t
     1961           ;; no canonicalization necessary
     1962           (values low high)))
     1963      (when (and (eq class 'rational)
     1964                 (integerp canonical-low)
     1965                 (integerp canonical-high)
     1966                 (= canonical-low canonical-high))
     1967        (setf class 'integer))
     1968      (%istruct 'numeric-ctype
     1969                (type-class-or-lose 'number)
     1970                enumerable
     1971                class
     1972                format
     1973                complexp
     1974                canonical-low
     1975                canonical-high
     1976                predicate))))
    19491977   
    19501978
     
    19762004
    19772005(define-type-method (number :unparse) (type)
    1978  (let* ((complexp (numeric-ctype-complexp type))
    1979         (low (numeric-ctype-low type))
    1980         (high (numeric-ctype-high type))
    1981         (base (case (numeric-ctype-class type)
    1982                 (integer 'integer)
    1983                 (rational 'rational)
    1984                 (float (or (numeric-ctype-format type) 'float))
    1985                 (t 'real))))
     2006  (let* ((complexp (numeric-ctype-complexp type))
     2007        (low (numeric-ctype-low type))
     2008        (high (numeric-ctype-high type))
     2009        (base (case (numeric-ctype-class type)
     2010                (integer 'integer)
     2011                (rational 'rational)
     2012                (float (or (numeric-ctype-format type) 'float))
     2013                (t 'real))))
    19862014    (let ((base+bounds
    1987              (cond ((and (eq base 'integer) high low)
    1988                       (let ((high-count (logcount high))
    1989                               (high-length (integer-length high)))
    1990                         (cond ((= low 0)
    1991                                  (cond ((= high 0) '(integer 0 0))
    1992                                          ((= high 1) 'bit)
    1993                                          ((and (= high-count high-length)
    1994                                                (plusp high-length))
    1995                                           `(unsigned-byte ,high-length))
    1996                                          (t
    1997                                           `(mod ,(1+ high)))))
    1998                                 ((and (= low most-negative-fixnum)
    1999                                         (= high most-positive-fixnum))
    2000                                  'fixnum)
    2001                                 ((and (= low (lognot high))
    2002                                         (= high-count high-length)
    2003                                         (> high-count 0))
    2004                                  `(signed-byte ,(1+ high-length)))
    2005                                 (t
    2006                                  `(integer ,low ,high)))))
    2007                      (high `(,base ,(or low '*) ,high))
    2008                      (low
    2009                       (if (and (eq base 'integer) (= low 0))
    2010                         'unsigned-byte
    2011                         `(,base ,low)))
    2012                      (t base))))
     2015           (cond ((and (eq base 'integer) high low)
     2016                  (let ((high-count (logcount high))
     2017                        (high-length (integer-length high)))
     2018                    (cond ((= low 0)
     2019                           (cond ((= high 0) '(integer 0 0))
     2020                                 ((= high 1) 'bit)
     2021                                 ((and (= high-count high-length)
     2022                                       (plusp high-length))
     2023                                  `(unsigned-byte ,high-length))
     2024                                 (t
     2025                                  `(mod ,(1+ high)))))
     2026                          ((and (= low most-negative-fixnum)
     2027                                (= high most-positive-fixnum))
     2028                           'fixnum)
     2029                          ((and (= low (lognot high))
     2030                                (= high-count high-length)
     2031                                (> high-count 0))
     2032                           `(signed-byte ,(1+ high-length)))
     2033                          (t
     2034                           `(integer ,low ,high)))))
     2035                 (high `(,base ,(or low '*) ,high))
     2036                 (low
     2037                  (if (and (eq base 'integer) (= low 0))
     2038                      'unsigned-byte
     2039                      `(,base ,low)))
     2040                 (t base))))
    20132041      (ecase complexp
    2014           (:real
    2015            base+bounds)
    2016           (:complex
    2017            (if (eq base+bounds 'real)
     2042        (:real
     2043         base+bounds)
     2044        (:complex
     2045         (if (eq base+bounds 'real)
    20182046             'complex
    20192047             `(complex ,base+bounds)))
    2020           ((nil)
    2021            (assert (eq base+bounds 'real))
    2022            'number)))))
     2048        ((nil)
     2049         (assert (eq base+bounds 'real))
     2050         'number)))))
    20232051
    20242052;;; Numeric-Bound-Test  --  Internal
     
    20762104(defmacro numeric-bound-max (x y closed open max-p)
    20772105  (once-only ((n-x x)
    2078                 (n-y y))
    2079     `(cond ((not ,n-x) ,(if max-p nil n-y))
    2080              ((not ,n-y) ,(if max-p nil n-x))
    2081              ((consp ,n-x)
    2082               (if (consp ,n-y)
    2083                   (if (,closed (car ,n-x) (car ,n-y)) ,n-x ,n-y)
    2084                   (if (,open (car ,n-x) ,n-y) ,n-x ,n-y)))
    2085              (t
    2086               (if (consp ,n-y)
    2087                   (if (,open (car ,n-y) ,n-x) ,n-y ,n-x)
    2088                   (if (,closed ,n-y ,n-x) ,n-y ,n-x))))))
     2106              (n-y y))
     2107    `(cond
     2108      ((not ,n-x) ,(if max-p nil n-y))
     2109      ((not ,n-y) ,(if max-p nil n-x))
     2110      ((consp ,n-x)
     2111       (if (consp ,n-y)
     2112         (if (,closed (car ,n-x) (car ,n-y)) ,n-x ,n-y)
     2113         (if (,open (car ,n-x) ,n-y) ,n-x ,n-y)))
     2114      (t
     2115       (if (consp ,n-y)
     2116         (if (,open (car ,n-y) ,n-x) ,n-y ,n-x)
     2117         (if (,closed ,n-y ,n-x) ,n-y ,n-x))))))
    20892118
    20902119
     
    21322161(defun numeric-types-adjacent (low high)
    21332162  (let ((low-bound (numeric-ctype-high low))
    2134           (high-bound (numeric-ctype-low high)))
     2163        (high-bound (numeric-ctype-low high)))
    21352164    (cond ((not (and low-bound high-bound)) nil)
    21362165            ((consp low-bound)
     
    21442173             nil))))
    21452174
    2146 ;;; NUMBER :SIMPLE-UNION method  -- Internal
    2147 ;;;
    2148 ;;; Return the a numeric type that is a supertype for both type1 and type2.
     2175;;;
     2176;;; Return a numeric type that is a supertype for both type1 and type2.
    21492177;;;
    2150 ;;; ### Note: we give up early, so keep from dropping lots of information on
    2151 ;;; the floor by returning overly general types.
    2152 ;;;
    21532178(define-type-method (number :simple-union) (type1 type2)
    21542179  (declare (type numeric-ctype type1 type2))
    21552180  (cond ((csubtypep type1 type2) type2)
    2156           ((csubtypep type2 type1) type1)
    2157           (t
    2158            (let ((class1 (numeric-ctype-class type1))
    2159                  (format1 (numeric-ctype-format type1))
    2160                  (complexp1 (numeric-ctype-complexp type1))
    2161                  (class2 (numeric-ctype-class type2))
    2162                  (format2 (numeric-ctype-format type2))
    2163                  (complexp2 (numeric-ctype-complexp type2)))
    2164              (when (and (eq class1 class2)
    2165                           (eq format1 format2)
    2166                           (eq complexp1 complexp2)
    2167                           (or (numeric-types-intersect type1 type2)
    2168                                 (numeric-types-adjacent type1 type2)
    2169                                 (numeric-types-adjacent type2 type1)))
     2181        ((csubtypep type2 type1) type1)
     2182        (t
     2183         (let ((class1 (numeric-ctype-class type1))
     2184               (format1 (numeric-ctype-format type1))
     2185               (complexp1 (numeric-ctype-complexp type1))
     2186               (class2 (numeric-ctype-class type2))
     2187               (format2 (numeric-ctype-format type2))
     2188               (complexp2 (numeric-ctype-complexp type2)))
     2189           (when (and (eq class1 class2)
     2190                      (eq format1 format2)
     2191                      (eq complexp1 complexp2)
     2192                      (or (numeric-types-intersect type1 type2)
     2193                          (numeric-types-adjacent type1 type2)
     2194                          (numeric-types-adjacent type2 type1)))
    21702195               (make-numeric-ctype
    21712196                :class class1
     
    21732198                :complexp complexp1
    21742199                :low (numeric-bound-max (numeric-ctype-low type1)
    2175                                               (numeric-ctype-low type2)
    2176                                               < <= t)
     2200                                        (numeric-ctype-low type2)
     2201                                        <= < t)
    21772202                :high (numeric-bound-max (numeric-ctype-high type1)
    2178                                               (numeric-ctype-high type2)
    2179                                                > >= t)))))))
     2203                                        (numeric-ctype-high type2)
     2204                                         >= > t)))))))
    21802205
    21812206(setf (info-type-kind 'number) :primitive
     
    25342559                     (bit 'bit-vector)
    25352560                     (base-char 'base-string)
    2536                      #|(character 'string)|#
    25372561                     (* 'vector)
    25382562                     (t `(vector ,eltype)))
     
    25402564                     (bit `(bit-vector ,(car dims)))
    25412565                     (base-char `(base-string ,(car dims)))
    2542                      (character `(string ,(car dims)))
    25432566                     (t `(vector ,eltype ,(car dims)))))
    25442567               (if (eq (car dims) '*)
     
    25462569                     (bit 'simple-bit-vector)
    25472570                     (base-char 'simple-base-string)
    2548                      (character 'simple-base-string)
    25492571                     ((t) 'simple-vector)
    25502572                     (t `(simple-array ,eltype (*))))
     
    25522574                     (bit `(simple-bit-vector ,(car dims)))
    25532575                     (base-char `(simple-base-string ,(car dims)))
    2554                      (character `(simple-base-string ,(car dims)))
    25552576                     ((t) `(simple-vector ,(car dims)))
    25562577                     (t `(simple-array ,eltype ,dims))))))
     
    26412662                              (mapcar #'(lambda (x y) (if (eq x '*) y x))
    26422663                                      dims1 dims2)))
    2643            :complexp (if (eq complexp1 '*) complexp2 complexp1)
     2664           :complexp (if (eq complexp1 :maybe) complexp2 complexp1)
    26442665           :element-type (if (eq eltype1 *wild-type*) eltype2 eltype1))))
    26452666    *empty-type*))
     
    26962717              (dolist (stype-name specialized-array-element-types
    26972718                                        (specifier-type 't))
    2698                   (let ((stype (specifier-type stype-name)))
    2699                     (when (csubtypep eltype stype)
    2700                       (return stype))))))
     2719                (let ((stype (specifier-type stype-name)))
     2720                  (when (csubtypep eltype stype)
     2721                    (return stype))))))
    27012722   
    27022723    type))
     
    27102731;;; and intersection are well defined.
    27112732
    2712 (defun make-member-ctype (&key members)
     2733(defun %make-member-ctype (members)
    27132734  (%istruct 'member-ctype
    27142735            (type-class-or-lose 'member)
    27152736            t
    27162737            members))
     2738
     2739(defun make-member-ctype (&key members)
     2740  (let* ((singlep (subsetp '(-0.0f0 0.0f0) members))
     2741         (doublep (subsetp '(-0.0d0 0.0d0) members))
     2742         (union-types
     2743          (if singlep
     2744            (if doublep
     2745              (list *ctype-of-single-float-0* *ctype-of-double-float-0*)
     2746              (list *ctype-of-single-float-0*))
     2747            (if doublep
     2748              (list *ctype-of-single-float-0*)))))
     2749    (if union-types
     2750      (progn
     2751        (if singlep
     2752          (setq members (set-difference '(-0.0f0 0.0f0) members)))
     2753        (if doublep
     2754          (setq members (set-difference '(-0.d00 0.0d0) members)))
     2755        (make-union-ctype (if (null members)
     2756                            union-types
     2757                            (cons (%make-member-ctype members) union-types))))
     2758      (%make-member-ctype members))))
     2759       
    27172760
    27182761(defun member-ctype-p (x) (istruct-typep x 'member-ctype))
     
    32493292            ;;; -could- try to find all such classes, but
    32503293            ;;; punt instead.
    3251             (t (if (and (typep class1 'standard-class)
    3252                         (typep class2 'standard-class))
    3253                  (find-class-intersection class1 class2)
     3294            (t (or (find-class-intersection class1 class2)
    32543295                 *empty-type*)))
    32553296      nil)))
     
    34173458                    (float
    34183459                     (ecase (numeric-ctype-format type)
    3419                        (short-float (typep num 'short-float))
    34203460                       (single-float (typep num 'single-float))
    34213461                       (double-float (typep num 'double-float))
    3422                        (long-float (typep num 'long-float))
    34233462                       ((nil) (floatp num))))
    34243463                    ((nil) t)))
     
    35423581                                   (rational 'rational)
    35433582                                   (float 'float))
    3544                           :format (and (floatp num) (float-format-name num))
     3583                          :format (and (floatp num)
     3584                                       (if (typep num 'double-float)
     3585                                         'double-float
     3586                                         'single-float))
    35453587                          :complexp complexp
    35463588                          :low low
     
    35623604     (%class.ctype (class-of x)))))
    35633605
     3606(defvar *ctype-of-double-float-0* (ctype-of 0.0d0))
     3607(defvar *ctype-of-single-float-0* (ctype-of 0.0f0))
     3608
     3609
    35643610
    35653611
     
    36043650
    36053651(deftype string (&optional size)
    3606   `(base-string ,size))
     3652  `(array character (,size)))
    36073653
    36083654(deftype simple-string (&optional size)
    3609   `(simple-base-string ,size))
     3655  `(simple-array character (,size)))
    36103656
    36113657(deftype extended-string (&optional size)
     
    37033749  `(simple-array single-float (,size)))
    37043750
     3751(deftype short-float (&optional low high)
     3752  `(single-float ,low ,high))
     3753
     3754(deftype long-float (&optional low high)
     3755  `(double-float ,low ,high))
     3756
     3757;;; As empty a type as you're likely to find ...
     3758(deftype extended-char ()
     3759  nil)
    37053760)
     3761
    37063762
    37073763(let* ((builtin-translations
     
    37253781          (double-float . double-float)
    37263782          (long-float . double-float)
    3727           (short-float . short-float)
    3728           (single-float . short-float)
    3729           (rational . rational)   ; why not (or ratio integer)?
    3730           (ratio . (and rational (not integer))) ; why not ratio
     3783          (single-float . single-float)
     3784          (short-float . single-float)
     3785
     3786          (rational . rational)
    37313787          (integer . integer)
     3788          (ratio . (and rational (not integer)))
    37323789          (fixnum . (integer ,most-negative-fixnum ,most-positive-fixnum))
    37333790          (bignum . (or (integer * (,most-negative-fixnum))
     
    37393796          (info-type-builtin (car spec)) (specifier-type (cdr spec)))))
    37403797
     3798
     3799
     3800
     3801
    37413802       
    37423803(precompute-types '((mod 2) (mod 4) (mod 16) (mod #x100) (mod #x10000)
     
    37483809                    ))
    37493810
     3811
    37503812(precompute-types *cl-types*)
     3813
     3814;;; Treat CHARACTER and BASE-CHAR as equivalent.
     3815(setf (info-type-builtin 'character) (info-type-builtin 'base-char))
     3816;;; And EXTENDED-CHAR as empty.
     3817(setf (info-type-builtin 'extended-char) *empty-type*)
     3818
    37513819(defparameter *null-type* (specifier-type 'null))
    37523820
Note: See TracChangeset for help on using the changeset viewer.