From: Didier Verna
Subject: [Q] Parametrized type declarations
Date: 
Message-ID: <muxd5mw5jk2.fsf@uzeb.lrde.epita.fr>
        Hi !

Suppose I have a function starting like this:

(defun foo (bar)
  (declare (type (simple-array fixnum (1024 1024)) bar))
  ;; ...
  )


Now, suppose that instead of hard-wiring the size, I have a defconstant at the
top of the file. I'd like to use it in the type declaration, but I can't seem
to find a way to do so (because declare expressions are not forms).

What should I do ?

Thanks.

-- 
Didier Verna, ······@lrde.epita.fr, http://www.lrde.epita.fr/~didier

EPITA / LRDE, 14-16 rue Voltaire   Tel.+33 (1) 44 08 01 85
94276 Le Kremlin-Bic�tre, France   Fax.+33 (1) 53 14 59 22   ······@xemacs.org

From: Marco Baringer
Subject: Re: [Q] Parametrized type declarations
Date: 
Message-ID: <m28xxkw4sj.fsf@soma.local>
Didier Verna <······@lrde.epita.fr> writes:

>         Hi !
>
> Suppose I have a function starting like this:
>
> (defun foo (bar)
>   (declare (type (simple-array fixnum (1024 1024)) bar))
>   ;; ...
>   )
>
>
> Now, suppose that instead of hard-wiring the size, I have a defconstant at the
> top of the file. I'd like to use it in the type declaration, but I can't seem
> to find a way to do so (because declare expressions are not forms).
>
> What should I do ?

(defconstant +rows+ 1024)

(defconstant +cols+ 1024)

(defun foo (bar)
  (declare (type (simple-array fixnum (#.+rows+ #.+cols+)) bar))
  ...)

-- 
-Marco
Ring the bells that still can ring.
Forget the perfect offering.
There is a crack in everything.
That's how the light gets in.
	-Leonard Cohen
From: Steven M. Haflich
Subject: Re: [Q] Parametrized type declarations
Date: 
Message-ID: <fm3_e.864$Fi3.314@newssvr29.news.prodigy.net>
Didier Verna wrote:

> Now, suppose that instead of hard-wiring the size, I have a defconstant at the
> top of the file. I'd like to use it in the type declaration, but I can't seem
> to find a way to do so (because declare expressions are not forms).
> 
> What should I do ?

Two choices.

  One would be to switch to C++ programming and spend the next six months
of your life convincing yourself you understand C++ parameterized types.
I don't recommend this approach, because even if you convince yourself,
you'll still face many more months of arguments with yourself that
you're wrong.

  The other would be to read abuot CL deftype.  deftype provides for
parameterized types, although the facility isn't really complete.
For example, you can write

   (deftype square-simple-arrat (n) `(simple-array (,n ,n))

which will work for a specific n.  But CL does not suppor paraeterized
types with unbound parameters in the type, e.g.:

   (typep #2a((1 2) (3 4) (5 6)) '(square-simple-array *))

This returns true because * matcches anything, not necessarily the same
value matched but the _other _ *.

You could write a tyep declaration that uses cl:satisfies to make the
above typep do the correct thing, but no existing compiler will made
use of a declaration involving satisfies.  Turing almost requires it.
From: Marco Antoniotti
Subject: Re: [Q] Parametrized type declarations
Date: 
Message-ID: <F7d_e.18$pa3.8203@typhoon.nyu.edu>
Didier Verna wrote:
>         Hi !
> 
> Suppose I have a function starting like this:
> 
> (defun foo (bar)
>   (declare (type (simple-array fixnum (1024 1024)) bar))
>   ;; ...
>   )
> 
> 
> Now, suppose that instead of hard-wiring the size, I have a defconstant at the
> top of the file. I'd like to use it in the type declaration, but I can't seem
> to find a way to do so (because declare expressions are not forms).
> 
> What should I do ?


As Marco Baringer said, you can use read-time evaluation to achieve this 
effect.  Otherwise you need to define your own "template" language. 
Something along the lines of

(def template (n) function foo (bar)
     (declare (type (simple-array fixnum (n n)) bar)
     ;; ...
     )

and then define the actual specialized function as

(def parameterized-function foo (1024))

which may yield a function named foo$1024, which you can call as

	(foo$1024 (make-array '(1024 1024)))

Variations on this theme abound.  You could in principle construct a 
program transformation package that recognized things like

	(defun baz (q w e)
	  ((psi foo 1024) (make-array '(1024 1024))))

i.e. the PSI form becomes a kind of LAMBDA.  And then the code gets 
translated into

	(defun baz (q w e)
	   (labels ((foo$1024 (bar)
	               (declare (type (simple-array fixnum (1024 1024)) bar)))
	            ))
               (foo$1024 (make-array '(1024 1024))))

All the fun in this case is that the above can be implemented in CL 
itself (not easy, but doable).

The DEFINER package from common-lisp.net may help (shameless plug :) )

Cheers
--
Marco
From: Marco Antoniotti
Subject: Re: [Q] Parametrized type declarations
Date: 
Message-ID: <Cod_e.22$pa3.7816@typhoon.nyu.edu>
Marco Antoniotti wrote:
> 
> 
> Didier Verna wrote:
> 
>>         Hi !
>>
>> Suppose I have a function starting like this:
>>
>> (defun foo (bar)
>>   (declare (type (simple-array fixnum (1024 1024)) bar))
>>   ;; ...
>>   )
>>
>>
>> Now, suppose that instead of hard-wiring the size, I have a 
>> defconstant at the
>> top of the file. I'd like to use it in the type declaration, but I 
>> can't seem
>> to find a way to do so (because declare expressions are not forms).
>>
>> What should I do ?
> 
> 
> 
> As Marco Baringer said, you can use read-time evaluation to achieve this 
> effect.  Otherwise you need to define your own "template" language. 
> Something along the lines of
> 
> (def template (n) function foo (bar)
>     (declare (type (simple-array fixnum (n n)) bar)
>     ;; ...
>     )
> 
> and then define the actual specialized function as
> 
> (def parameterized-function foo (1024))
> 
> which may yield a function named foo$1024, which you can call as
> 
>     (foo$1024 (make-array '(1024 1024)))
> 
> Variations on this theme abound.  You could in principle construct a 
> program transformation package that recognized things like
> 
>     (defun baz (q w e)
>       ((psi foo 1024) (make-array '(1024 1024))))
> 
> i.e. the PSI form becomes a kind of LAMBDA.  And then the code gets 
> translated into
> 
>     (defun baz (q w e)
>        (labels ((foo$1024 (bar)
>                    (declare (type (simple-array fixnum (1024 1024)) bar)))
>                 ))
>               (foo$1024 (make-array '(1024 1024))))
> 
> All the fun in this case is that the above can be implemented in CL 
> itself (not easy, but doable).
> 

Or maybe you could have something like

	(defun baz (q w e)
	  (pfuncall foo (1024) (make-array '(1024 1024)))

where PFUNCALL is a macro that expands directly into the LABELS form. 
The FEMLISP package (www.femlisp.org) by Nicolas Neuss does similar tricks.

Cheers
--
Marco