From: Surendra Singhi
Subject: method with same name as function
Date: 
Message-ID: <cue5r4$jcb$1@news.asu.edu>
Hi,
I have a lisp question on methods.
Suppose, I have defined a function by name area, and then later on when 
I try to define a method with the same name, I get an error.
Why is that?
Do I always need to ensure that when I am defining a method I am not 
overriding the definition of an existing function.


Thanks.
-- 
Surendra Singhi

www.public.asu.edu/~sksinghi/

From: Paul F. Dietz
Subject: Re: method with same name as function
Date: 
Message-ID: <9oidna2DJeRyO5ffRVn-sA@dls.net>
Surendra Singhi wrote:
> Hi,
> I have a lisp question on methods.
> Suppose, I have defined a function by name area, and then later on when 
> I try to define a method with the same name, I get an error.
> Why is that?
> Do I always need to ensure that when I am defining a method I am not 
> overriding the definition of an existing function.

The method doesn't have a name.  The method is attached to a generic
function that has a name.  May I hazard a guess that that function
you're trying to add the method to is either not generic, or has
arguments that don't match those of the method?

	Paul
From: Surendra Singhi
Subject: Re: method with same name as function
Date: 
Message-ID: <cuec68$sk2$1@news.asu.edu>
Paul F. Dietz wrote:

> Surendra Singhi wrote:
> 
>> Hi,
>> I have a lisp question on methods.
>> Suppose, I have defined a function by name area, and then later on 
>> when I try to define a method with the same name, I get an error.
>> Why is that?
>> Do I always need to ensure that when I am defining a method I am not 
>> overriding the definition of an existing function.
> 
> 
> The method doesn't have a name.  The method is attached to a generic
> function that has a name.  May I hazard a guess that that function
> you're trying to add the method to is either not generic, or has
> arguments that don't match those of the method?
> 
>     Paul
I wrote a function like this:

(defun area (fig)
   "Function to calculate the area of a figure."
   (cond ((rectangle-p fig) (* (rectangle-length fig)
			      (rectangle-width fig)))
	((circle-p fig) (* pi (circle-radius fig)
			      (circle-radius fig)))))

then I wrote the method:

(defmethod area ((fig rectangle))
   (* (rectangle-length fig) (rectangle-width fig)))

And I was getting the error.
How can make a function generic?
And can you please explain what does a generic function means?

Thanks.
-- 
Surendra Singhi

www.public.asu.edu/~sksinghi/
From: ···············@gmail.com
Subject: Re: method with same name as function
Date: 
Message-ID: <1107999205.160925.179590@z14g2000cwz.googlegroups.com>
The simple answer is this: remove your DEFUN and use DEFMETHOD instead.
Defining a DEFMETHOD automatically creates a generate function.
From: Barry Margolin
Subject: Re: method with same name as function
Date: 
Message-ID: <barmar-33161F.20314609022005@comcast.dca.giganews.com>
In article <············@news.asu.edu>,
 Surendra Singhi <·········@netscape.net> wrote:

> I wrote a function like this:
> 
> (defun area (fig)
>    "Function to calculate the area of a figure."
>    (cond ((rectangle-p fig) (* (rectangle-length fig)
> 			      (rectangle-width fig)))
> 	((circle-p fig) (* pi (circle-radius fig)
> 			      (circle-radius fig)))))
> 
> then I wrote the method:
> 
> (defmethod area ((fig rectangle))
>    (* (rectangle-length fig) (rectangle-width fig)))
> 
> And I was getting the error.
> How can make a function generic?

(defgeneric area (fig))

If you use DEFMETHOD and the function doesn't already exist, it will 
automatically do this for you.

However, DEFGENERIC cannot be used to replace an existing, non-generic 
function.  You need to remove the old definition with (fmakunbound 
'area) or (setf (symbol-function 'area) nil).

> And can you please explain what does a generic function means?

A generic function is one that automatically selects an appropriate 
method based on the classes (or, in some cases, specific identity) of 
its arguments.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
From: Bruno Haible
Subject: Re: method with same name as function
Date: 
Message-ID: <cuflcb$fq1$1@laposte.ilog.fr>
Barry Margolin wrote:
>
> However, DEFGENERIC cannot be used to replace an existing, non-generic 
> function.  You need to remove the old definition with (fmakunbound 'area)

Yes.

> or (setf (symbol-function 'area) nil).

This won't work in ANSI CL compliant implementations:
HyperSpec/Body/acc_symbol-function.html says that the setf'd value
must be a FUNCTION, but nil is a SYMBOL.

FMAKUNBOUND is really the only way to get rid of the function definition
without throwing away the symbol.

         Bruno
From: Kent M Pitman
Subject: Re: method with same name as function
Date: 
Message-ID: <u6511c8tn.fsf@nhplace.com>
Surendra Singhi <·········@netscape.net> writes:

> >> I have a lisp question on methods.
> >> Suppose, I have defined a function by name area, and then later on
> >> when I try to define a method with the same name, I get an error.
> >> Why is that?
> >> Do I always need to ensure that when I am defining a method I am
> >> not overriding the definition of an existing function.

Well, yes, but if you're using packages, that's not hard.  The set of
initial names is fixed and if you start from a fresh namespace (i.e.,
your own package), you'll get used to what names are available and what
not.  (If you need one that is in the initial set, learn about shadowing
symbols in a package.)

> 
> (defun area (fig)
>    "Function to calculate the area of a figure."
>    (cond ((rectangle-p fig) (* (rectangle-length fig)
> 			      (rectangle-width fig)))
> 	((circle-p fig) (* pi (circle-radius fig)
> 			      (circle-radius fig)))))
> 
> then I wrote the method:
> 
> (defmethod area ((fig rectangle))
>    (* (rectangle-length fig) (rectangle-width fig)))

This is redundant with what you wrote above.
 
> And I was getting the error.
> How can make a function generic?
> And can you please explain what does a generic function means?

Generic functions are made explicitly with DEFGENERIC, or implicitly
if you've done DEFMETHOD on something undefined.  DEFMETHOD normally
adds to a generic function, which must already be the definition.
So if you've defined the function to non-generic, DEFMETHOD cannot add
to it.

Start with:

(DEFGENERIC AREA (FIG))

then write your methods:

(DEFMETHOD AREA ((FIG RECTANGLE))
  (* (RECTANGLE-LENGTH FIG) (RECTANGLE-WIDTH FIG)))

(DEFMETHOD CIRCLE ((FIG CIRCLE))
  (* PI (CIRCLE-RADIUS FIG) (CIRCLE-RADIUS FIG)))

The main service that a generic function is buying you is the bookkeeping
involved in managing what would otherwise be the spine of the COND.
That is,  rather than

          +----- this is the backbone part I mentioned
          |
          v

 (COND ((FOO-P X) (DO-FOO-THING))
       ((BAR-P X) (DO-BAR-THING))
       ...)

which necessarily has a static number of clauses, think of the generic
function as approximately being a spongey thing that replaces the backbone,
approximately being like:

  (DOLIST (METHOD-AND-PREDICATE (GET-MY-METHODS-AND-PREDICATES))
    (IF (THIS-IS-THE-RIGHT-ONE METHOD-AND-PREDICATE)
        (RETURN (INVOKE (THIS-IS-THE-RIGHT-ONE METHOD-AND-PREDICATE)))))

But there still has to be a function object that manages this so if you
put something there that is not a generic function, you get whatever 
static-natured function you put there, you don't get the generic.

One other note:  If you've already put a static definition there and you
want to start over without leaving Lisp, the thing to do interactively is
usually just

 (FMAKUNBOUND 'AREA) ; or whatever your generic function is

This will make AREA not be fbound (i.e., not have a function definition),
so then you can cleanly just define it anew.  In practice, though,
when building a system up from scratch not having made the mistake in 
the first place, the call to FMAKUNBOUND is both superfluous and probably 
the wrong thing.