From: ·········@hotmail.com
Subject: defmacro and type-of - why doesn't this work
Date: 
Message-ID: <f1d0648f-7902-4cad-97b3-e53c2c44ce2b@w5g2000prd.googlegroups.com>
Hi all,

I was wondering why the following doesn't work:

(defmacro create-inc-method (value)
  (let ((vt (type-of value)))
    `(defmethod inc ((self ,vt) a)
       (+ self a))))

(create-inc-method 23) => #<STANDARD-METHOD INC NIL (T T) 200B536F>
(create-inc-method 23.0) => #<STANDARD-METHOD INC NIL (T T) 2009B25B>

I am using Lispworks for this experiment.

The output shows two methods created with identical parameters. I was
expecting two distinct methods to be generated. One with a float
initial argument and another with a fixnum initial argument.

I don't have a clue what I'm doing wrong.

Thanks in advance.

Matthew

From: ······@gmail.com
Subject: Re: defmacro and type-of - why doesn't this work
Date: 
Message-ID: <9abf42f2-5577-4b26-a039-6f7f8d92cfc1@m45g2000hsb.googlegroups.com>
On Jun 2, 3:17 pm, ·········@hotmail.com wrote:
> Hi all,
>
> I was wondering why the following doesn't work:
>
> (defmacro create-inc-method (value)
>   (let ((vt (type-of value)))
>     `(defmethod inc ((self ,vt) a)
>        (+ self a))))
>
> (create-inc-method 23) => #<STANDARD-METHOD INC NIL (T T) 200B536F>
> (create-inc-method 23.0) => #<STANDARD-METHOD INC NIL (T T) 2009B25B>
>
> I am using Lispworks for this experiment.
>
> The output shows two methods created with identical parameters. I was
> expecting two distinct methods to be generated. One with a float
> initial argument and another with a fixnum initial argument.
>
> I don't have a clue what I'm doing wrong.
>
> Thanks in advance.
>
> Matthew

This does not work on sbcl type-of return cons in some cases
you should use (class-name (class-of value)) probably
best,milan
From: Pascal Costanza
Subject: Re: defmacro and type-of - why doesn't this work
Date: 
Message-ID: <6aibo9F36uv5lU3@mid.individual.net>
······@gmail.com wrote:
> On Jun 2, 3:17 pm, ·········@hotmail.com wrote:
>> Hi all,
>>
>> I was wondering why the following doesn't work:
>>
>> (defmacro create-inc-method (value)
>>   (let ((vt (type-of value)))
>>     `(defmethod inc ((self ,vt) a)
>>        (+ self a))))
>>
>> (create-inc-method 23) => #<STANDARD-METHOD INC NIL (T T) 200B536F>
>> (create-inc-method 23.0) => #<STANDARD-METHOD INC NIL (T T) 2009B25B>
>>
>> I am using Lispworks for this experiment.
>>
>> The output shows two methods created with identical parameters. I was
>> expecting two distinct methods to be generated. One with a float
>> initial argument and another with a fixnum initial argument.
>>
>> I don't have a clue what I'm doing wrong.
>>
>> Thanks in advance.
>>
>> Matthew
> 
> This does not work on sbcl type-of return cons in some cases
> you should use (class-name (class-of value)) probably

(class-of value) should be sufficient.


Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Didier Verna
Subject: Re: defmacro and type-of - why doesn't this work
Date: 
Message-ID: <muxej7g2b52.fsf@uzeb.lrde.epita.fr>
·······@gmail.com" <······@gmail.com> wrote:

> On Jun 2, 3:17 pm, ·········@hotmail.com wrote:
>> Hi all,
>>
>> I was wondering why the following doesn't work:
>>
>> (defmacro create-inc-method (value)
>>   (let ((vt (type-of value)))
>>     `(defmethod inc ((self ,vt) a)
>>        (+ self a))))
>
> This does not work on sbcl type-of return cons in some cases
> you should use (class-name (class-of value)) probably
> best,milan

  Actually, using just class-of is OK. Section 7.6.2 "Introduction to
Methods" of the HyperSpec sez:

| A specialized parameter is a list (variable-name
| parameter-specializer-name), where parameter-specializer-name is one of
| the following:
| 
| a symbol
| 
|     denotes a parameter specializer which is the class named by that symbol.
| 
| a class
| 
|     denotes a parameter specializer which is the class itself.
| 
| (eql form)
| 
|     blah blah


On the other hand, the defmethod macro description sez:

| A specialized parameter is a list of the form (var
| parameter-specializer-name). Only required parameters can be
| specialized. If parameter-specializer-name is a symbol it names a class;
| if it is a list, it is of the form (eql eql-specializer-form).

... and omits to mention the "class" case. But then, it goes:

| For further discussion, see Section 7.6.2 (Introduction to Methods)

... so I guess that's OK.


-- 
5th European Lisp Workshop at ECOOP 2008, July 7: http://elw.bknr.net/2008/

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

EPITA / LRDE, 14-16 rue Voltaire   Tel.+33 (0)1 44 08 01 85
94276 Le Kremlin-Bic�tre, France   Fax.+33 (0)1 53 14 59 22  ······@xemacs.org
From: Pascal Costanza
Subject: Re: defmacro and type-of - why doesn't this work
Date: 
Message-ID: <6aibmpF36uv5lU2@mid.individual.net>
·········@hotmail.com wrote:
> Hi all,
> 
> I was wondering why the following doesn't work:
> 
> (defmacro create-inc-method (value)
>   (let ((vt (type-of value)))
>     `(defmethod inc ((self ,vt) a)
>        (+ self a))))
> 
> (create-inc-method 23) => #<STANDARD-METHOD INC NIL (T T) 200B536F>
> (create-inc-method 23.0) => #<STANDARD-METHOD INC NIL (T T) 2009B25B>
> 
> I am using Lispworks for this experiment.
> 
> The output shows two methods created with identical parameters. I was
> expecting two distinct methods to be generated. One with a float
> initial argument and another with a fixnum initial argument.
> 
> I don't have a clue what I'm doing wrong.

Methods specialize on classes, not on types. So try class-of instead of 
type-of.


Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Thomas A. Russ
Subject: Re: defmacro and type-of - why doesn't this work
Date: 
Message-ID: <ymibq2jcxj4.fsf@blackcat.isi.edu>
Pascal Costanza <··@p-cos.net> writes:

> ·········@hotmail.com wrote:
> > Hi all,
> > I was wondering why the following doesn't work:
> > (defmacro create-inc-method (value)
> >   (let ((vt (type-of value)))
> >     `(defmethod inc ((self ,vt) a)
> >        (+ self a))))
> > (create-inc-method 23) => #<STANDARD-METHOD INC NIL (T T) 200B536F>
> > (create-inc-method 23.0) => #<STANDARD-METHOD INC NIL (T T) 2009B25B>
> > I am using Lispworks for this experiment.
> > The output shows two methods created with identical parameters. I was
> > expecting two distinct methods to be generated. One with a float
> > initial argument and another with a fixnum initial argument.
> > I don't have a clue what I'm doing wrong.
> 
> Methods specialize on classes, not on types. So try class-of instead of
> type-of.

Also you should be aware that in general this will not be particularly
portable, but in this specific case it may only result in you having
multiple definitions for the same method.

That is because is no requirement in the standard that all of the
numeric types will be represented by a class.  The only required numeric
classes are 
  NUMBER
   COMPLEX
   REAL
     FLOAT
     RATIONAL
       INTEGER
       RATIO

So, you aren't (for example) guaranteed to have both FIXNUM and BIGNUM
as classes for method dispatch.

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: ·········@hotmail.com
Subject: Re: defmacro and type-of - why doesn't this work
Date: 
Message-ID: <1613c181-0146-4081-89df-7216fcf6d3a4@s33g2000pri.googlegroups.com>
Thanks all,

Using "class-of" did the trick.

However I fear the answer has only exposed a gaping hole in my
knowledge of lisp.

When I try the following I get different results.

CL-USER> (class-of 3) => #<BUILT-IN-CLASS INTEGER>
CL-USER> (type-of 3)  => (INTEGER 0 16777215)

This tells me that types and classes are different. I guess I was
expecting the types and classes to be unified (my Python background
showing).

I will have to do some more digging around but at least it makes sense
now.

Thanks again,

Matthew
From: Carl Taylor
Subject: Re: defmacro and type-of - why doesn't this work
Date: 
Message-ID: <Chf1k.29275$SV4.2052@bgtnsc04-news.ops.worldnet.att.net>
<·········@hotmail.com> wrote in message 
·········································@s33g2000pri.googlegroups.com...
> Thanks all,
>
> Using "class-of" did the trick.
>
> However I fear the answer has only exposed a gaping hole in my
> knowledge of lisp.
>
> When I try the following I get different results.
>
> CL-USER> (class-of 3) => #<BUILT-IN-CLASS INTEGER>
> CL-USER> (type-of 3)  => (INTEGER 0 16777215)
>
> This tells me that types and classes are different. I guess I was
> expecting the types and classes to be unified (my Python background
> showing).

You can precede the (class-of ...) with (class-name ... )) to extract 
the name from the unreadable #<.....> form.

An example using LispWorks:

CL-USER 4 > (class-of 3)
#<BUILT-IN-CLASS FIXNUM 20A062DF>

CL-USER 5 > (class-name (class-of 3))
FIXNUM

Carl Taylor
From: Thomas A. Russ
Subject: Re: defmacro and type-of - why doesn't this work
Date: 
Message-ID: <ymid4mqbhcd.fsf@blackcat.isi.edu>
·········@hotmail.com writes:

> Thanks all,
> 
> Using "class-of" did the trick.
> 
> However I fear the answer has only exposed a gaping hole in my
> knowledge of lisp.
> 
> When I try the following I get different results.
> 
> CL-USER> (class-of 3) => #<BUILT-IN-CLASS INTEGER>
> CL-USER> (type-of 3)  => (INTEGER 0 16777215)
> 
> This tells me that types and classes are different. I guess I was
> expecting the types and classes to be unified (my Python background
> showing).
> 
> I will have to do some more digging around but at least it makes sense
> now.

Well, that is bit of an historic artifact.  The CLOS system was added to
Lisp and Common Lisp long after the type system had been designed.
There was work on a unification of the class and type system, but for
various pragmatic reasons the unification is not complete.  Some of
these are also rather function, namely the ability to describe
sub-ranges of integer:

   (integer 1 10)

for which one would not expect there to be a corresponing class.

The quick summary is that all classes are types, but not all types have
classes associated with them.

One place to look for some background would be in CLTL-II, which has a
section about the integration of types and classes.  [Although the
figure that goes with that section is not an accurate representation of
the final standard.]

-- 
Thomas A. Russ,  USC/Information Sciences Institute