From: Barry Margolin
Subject: Re: declaring types with variable bits of precision
Date: 
Message-ID: <5u7na5$ajk@tools.bbnplanet.com>
In article <············@uic.edu>,
Bruce L. Lambert, Ph.D. <········@uic.edu> wrote:
>I want to write a fucntion that uses an array of unsigned-bytes, but I
>want to let the user decide how many bits of precision to use in each
>unsigned-byte. How do I write a declaration that allows me to do this?
>
>For example:
>(defparameter *num-bits* 8)
>...
>(defun foo (an-array)
>  (declare (type (simple-array (unsigned-byte *num-bits*) (*)) 
>                 an-array))
>  (dotimes (i (length an-array))
>  
>   ...))
>
>The CLISP compiler chokes on this declaration. I could rewrite it as a
>macro, and then it will work. But in my system, many functions need to
>know how many bits are going to be in an unsigned-byte. Must I rewrite
>all these functions as macros? There must be a better way.

You could implement it using SATISFIES:

(defun has-num-bits (x)
  (typep x `(unsigned-byte ,*num-bits*)))

(deftype num-bits () '(satisfies has-num-bits))

(defun foo (an-array)
  (declare (type (simple-array num-bits (*))
		 an-array))
  ...)

Note that you're not likely to get any benefit from a declaration like
this; it's essentially just documentation.  The compiler can't do much
optimization if the precision isn't known at compile time.  Type specifiers
that use SATISFIES are generally only useful with TYPEP, not DECLARE.

-- 
Barry Margolin, ······@bbnplanet.com
BBN Corporation, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>
Please don't send technical questions directly to me, post them to newsgroups.