From: Ted Dunning
Subject: type declarations in common lisp
Date: 
Message-ID: <TED.91Nov3223928@pylos.nmsu.edu>
in most functional languages it is possible to declare a value to be a
list of numbers and have the compiler infer that all elements of the
list are numbers.

in common lisp, it is possible to declare the element type of an array
in order to save space, and advanced lisp compilers infer that
dereferenced pieces of this array are of that type.

but it appears that the same cannot be done for lists.  is this true?
or is it possible to write a declare form which will inform the
compiler of the type of the elements of a list.

if this is possible, are there any lisp compilers that would realize
that (reduce #'+ l) could use fixnum arithmetic if we earlier saw that
l was of type (list fixnum)?

From: Barry Margolin
Subject: Re: type declarations in common lisp
Date: 
Message-ID: <khaprvINNhg3@early-bird.think.com>
In article <················@pylos.nmsu.edu> ···@nmsu.edu writes:
>or is it possible to write a declare form which will inform the
>compiler of the type of the elements of a list.

Sorry, this isn't in Common Lisp.

>if this is possible, are there any lisp compilers that would realize
>that (reduce #'+ l) could use fixnum arithmetic if we earlier saw that
>l was of type (list fixnum)?

First of all, you couldn't use fixnum arithmetic for this.  Addition of
fixnums can still overflow.  While the results of the pairwise additions
are limited to one bit larger than a fixnum, L can be arbitrarily long so
the result can be an arbitrarily large bignum.

To get all-fixnum arithmetic, you'd have to write something like:

(reduce #'(lambda (x y) (the fixnum (+ (the fixnum x) (the fixnum y))))
	l)

Even if you could declare L to be a list of fixnums, you'd still have to
write:

(reduce #'(lambda (x y) (the fixnum (+ x y)))
	l)

Finally, you'd have to hope that the compiler optimizes REDUCE
sufficiently.
-- 
Barry Margolin, Thinking Machines Corp.

······@think.com
{uunet,harvard}!think!barmar
From: Trent Lange
Subject: Re: type declarations in common lisp
Date: 
Message-ID: <1991Nov6.130804.436@cs.ucla.edu>
In article <············@early-bird.think.com> ······@think.com (Barry Margolin) writes:
>In article <················@pylos.nmsu.edu> ···@nmsu.edu writes:
>>or is it possible to write a declare form which will inform the
>>compiler of the type of the elements of a list.
>
>Sorry, this isn't in Common Lisp.
>
>[Goes on to show how it wouldn't be very helpful for functions like REDUCE.]
>-- 
>Barry Margolin, Thinking Machines Corp.

Being able to declare the types of a list for REDUCE probably wouldn't be
the best way to show its usefulness, as you point out.  However, there
are many cases where that kind of declaration would be quite useful for
optimization purposes.

A (MAPCAR #'1+ (the (list fixnum) list-of-fixnums)) could be significantly
optimized; and an example in one of my own programs that CMU-LISP almost
handles, but doesn't because there's no way to declare the element-types
of a list:

(MEMBER (the simple-string str) (the (list simple-string) list-of-strings)
        :test #'string=)

Even better, it should be pretty simple for any compiler to optimize
the list accessing functions based on those types, so that numeric
operations such as (> (CAR List-Of-Fixnums) 0) could be optimized.

So it seems that such an ability to declare list element-types would be quite
useful to have as part of the language;  it would be nice of the ongoing(?)
XJ313 negotiations would include it.

- Trent Lange

-- 
·····@cs.ucla.edu
Artificial Intelligence Laboratory
Computer Science Department, UCLA