[cdr-discuss] Issues with DEFTYPE

Nikodemus Siivola nikodemus at random-state.net
Tue Aug 12 07:48:41 UTC 2008


On Mon, Aug 11, 2008 at 9:56 PM, Pascal Costanza <pc at p-cos.net> wrote:

> Maybe I'm missing something...

The ability to derive the type of both CAR and CDR of an INT-LIST (or whatever).

That said,

 (let ((list (get-list)))
   (declare (type (cons integer list) list))
   ...)

gives compiler as much information as a recursive type, assuming that
the CDR is always
assigned back to LIST.

Likewise,

 (defstruct listoid (int 0 :type integer) (tail nil :type (or null listoid)))

gives the compiler just as much information (though the representation
is almost certain to be 4 words on the heap as opposed to 2 for a
CONS. DEFSTRUCT also gives you the ability to define what amount to
mutually
recursive types:

 (defstruct one (two nil :type (or null two)))
 (defstruct two (one nil :type (or null one)))

All in all, I would be perhaps more interested in a SIMPLE-LIST type,
(where CAR is always of the same type), then recursive DEFTYPE in
general:

 (deftype int-list ()
   '(simple-list integer))

In terms of what implmenentations do, what does Allegro do for

 (compile nil '(lambda (x) (typep x 'int-list)))

and

 (deftype fixnum-list () '(or null (cons fixnum fixnum-list)))

 (compile nil '(lambda (x) (declare (fixnum-list x) (optimize speed))
(let ((a (first x)) (b (second x))) (when (and a b) (+ a b))))

? (That is, does it implement recursive types for the compiler as
well, or do they just work in full calls to TYPEP?)

Cheers,

 -- Nikodemus



More information about the cdr-discuss mailing list