From: zoav1602 on
Hi!
I see this:

CL-USER> (defclass my-class () ((a :type (simple-array fixnum (3))
:initform #(1 2 3))))
#<STANDARD-CLASS MY-CLASS>
CL-USER> (type-of (slot-value (make-instance 'my-class) 'a))
(SIMPLE-VECTOR 3)

I expect it to mention fixnums somehow but it's not there. What's
going on here? The problem arises when I try to process slots like
this:

(with-slots (a) my-instance
(declare ((simple-array fixnum (3)) a)
blah-blah-blah)

I get a run-time error about type mismatch. Can somebody show me the
way around? I want to give the compiler as many information as
possible for not having these 'unable to optimize' notes from SBCL.

AZ
From: Tamas K Papp on
On Sun, 04 Apr 2010 07:40:23 -0700, zoav1602 wrote:

> Hi!
> I see this:
>
> CL-USER> (defclass my-class () ((a :type (simple-array fixnum (3))
> :initform #(1 2 3))))
> #<STANDARD-CLASS MY-CLASS>
> CL-USER> (type-of (slot-value (make-instance 'my-class) 'a))
> (SIMPLE-VECTOR 3)
>
> I expect it to mention fixnums somehow but it's not there. What's going
> on here? The problem arises when I try to process slots like this:
>
> (with-slots (a) my-instance
> (declare ((simple-array fixnum (3)) a) blah-blah-blah)
>
> I get a run-time error about type mismatch. Can somebody show me the way
> around? I want to give the compiler as many information as possible for
> not having these 'unable to optimize' notes from SBCL.

That :TYPE in the DEFCLASS is a promise that _you_ are making about
the type (which, BTW, most compilers usually just ignore). You are
already violating that promise: the #( read macro creates a simple
_general_ vector (element type t). Look up 2.4.8.3 in the HS.

I would suggest that you drop that :TYPE declaration in DEFCLASS
altogether, or just keep it for documentation purposes, and create that
vector with

(make-array 3 :element-type 'fixnum :initial-contents '(1 2 3))

If you find this verbose, just write a function that does this.

And a general remark (which you can of course ignore): you appear to be a
Lisp newbie, and you are already optimizing your code. That is generally
unwise: it will distract you from learning the language, and good CL
implementations are fast enough anyway. Optimizations are useful, but
usually at a later stage. That said, I did exactly the same when I was
learning CL, until I understood its performance model a little better.
Now I am using declarations like that only sparingly.

Best,

Tamas
From: joswig on
On 4 Apr., 17:10, Tamas K Papp <tkp...(a)gmail.com> wrote:
> On Sun, 04 Apr 2010 07:40:23 -0700, zoav1602 wrote:
> > Hi!
> > I see this:
>
> > CL-USER> (defclass my-class () ((a :type (simple-array fixnum (3))
> >                               :initform #(1 2 3))))
> > #<STANDARD-CLASS MY-CLASS>
> > CL-USER> (type-of (slot-value (make-instance 'my-class) 'a))
> > (SIMPLE-VECTOR 3)
>
> > I expect it to mention fixnums somehow but it's not there. What's going
> > on here? The problem arises when I try to process slots like this:
>
> > (with-slots (a) my-instance
> >   (declare ((simple-array fixnum (3)) a) blah-blah-blah)
>
> > I get a run-time error about type mismatch. Can somebody show me the way
> > around? I want to give the compiler as many information as possible for
> > not having these 'unable to optimize' notes from SBCL.
>
> That :TYPE in the DEFCLASS is a promise that _you_ are making about
> the type (which, BTW, most compilers usually just ignore).

It is not that unusual that setting a slot in safe mode will be type
checked at runtime.

>  You are
> already violating that promise: the #( read macro creates a simple
> _general_ vector (element type t).  Look up 2.4.8.3 in the HS.
>
> I would suggest that you drop that :TYPE declaration in DEFCLASS
> altogether, or just keep it for documentation purposes, and create that
> vector with
>
> (make-array 3 :element-type 'fixnum :initial-contents '(1 2 3))
>
> If you find this verbose, just write a function that does this.
>
> And a general remark (which you can of course ignore): you appear to be a
> Lisp newbie, and you are already optimizing your code.  That is generally
> unwise: it will distract you from learning the language, and good CL
> implementations are fast enough anyway.  Optimizations are useful, but
> usually at a later stage.  That said, I did exactly the same when I was
> learning CL, until I understood its performance model a little better.  
> Now I am using declarations like that only sparingly.
>
> Best,
>
> Tamas

From: Rob Warnock on
Tamas K Papp <tkpapp(a)gmail.com> wrote:
+---------------
| zoav1602 wrote:
| > CL-USER> (defclass my-class () ((a :type (simple-array fixnum (3))
| > :initform #(1 2 3))))
| > #<STANDARD-CLASS MY-CLASS>
| > CL-USER> (type-of (slot-value (make-instance 'my-class) 'a))
| > (SIMPLE-VECTOR 3)
| >
| > I expect it to mention fixnums somehow but it's not there. What's going
| > on here?
....
| I would suggest that you drop that :TYPE declaration in DEFCLASS
| altogether, or just keep it for documentation purposes, and create that
| vector with
|
| (make-array 3 :element-type 'fixnum :initial-contents '(1 2 3))
+---------------

And bear in mind that you *still* might end up with a general array
[or perhaps an unspecialized SIMPLE-VECTOR], even in that case. It all
depends upon what UPGRADED-ARRAY-ELEMENT-TYPE returns when given FIXNUM
in your implementation. E.g., in CLISP:

> (upgraded-array-element-type 'fixnum)
T
>

This is reasonable, since fixnums are typically the same size as general
lisp opject pointers, and therefore there is little reason to specialize
arrays to hold fixnums.


-Rob

-----
Rob Warnock <rpw3(a)rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607

 | 
Pages: 1
Prev: Help with LISP functions
Next: [ANN] ECL 10.4.1