From: Michael Greenwald
Subject: Re: declaring types with variable bits of precision
Date: 
Message-ID: <michaelg.873228485@CS.Stanford.EDU>
"Bruce L. Lambert, Ph.D." <········@uic.edu> writes:

>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)

Try DEFCONSTANT instead of DEFPARAMETER.  (I would also, personally,
use (DEFTYPE MY-UNSIGNED-BYTE (UNSIGNED-BYTE *NUM-BITS*))).  
Note, though, that this will only be useful if each user recompiles
the function when they change the value of *NUM-BITS*.

In the (unlikely?) event that you want to do something like this
dynamically without recompiling, you must remember that the compiler
needs to know how many bits in an unsigned byte at *compile time*.
You can decide on some common values of *NUM-BITS*, and force the user
to choose among those, rather than *any* value (you can put in some
implementation dependent ones, as in MOST-POSITIVE-FIXNUM etc.).
Then, you must make each function that takes in objects of
MY-UNSIGNED-BYTE (or arrays such of things) generic, and dispatch on
that object.  If you always create the objects by calls to a special
function, you can dynamically choose exactly which kind of object to
construct at run-time, and then the generic-dispatch should do the
right thing at run-time, too.  There will be some code bloat, though.

>...
>(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.

>Thanks.

>-bruce

>-- 

>Bruce L. Lambert
>Department of Pharmacy Administration
>University of Illinois at Chicago
>Phone:  (312) 996-2411
>Fax:    (312) 996-3272
>email:  ········@uic.edu
>WWW:    http://ludwig.pmad.uic.edu/~bruce/