From: ···········@gmail.com
Subject: Renaming a function
Date: 
Message-ID: <31c2237f-d3ee-40fd-9e57-c8a221403b4b@b1g2000pra.googlegroups.com>
Suppose for some reason one needs to rename a global function, would
the following be OK in Common Lisp?

(setf (symbol-function 'foo2) (function foo1))

Rock

From: Rainer Joswig
Subject: Re: Renaming a function
Date: 
Message-ID: <joswig-BF0560.11543809122007@news-europe.giganews.com>
In article 
<····································@b1g2000pra.googlegroups.com>,
 ···········@gmail.com wrote:

> Suppose for some reason one needs to rename a global function, would
> the following be OK in Common Lisp?
> 
> (setf (symbol-function 'foo2) (function foo1))
> 
> Rock

You are not 'renaming' it. You are making a function
available under a different name, additionally.
That's fine.

Minor point: it could be though, that some extra
functionality of the Lisp environment gets lost - depending
on how the Lisp environment stores the information.
For example it could be difficult to locate the 'source
code' for FOO2.

See also here:

? (defun foo () 'bar)
FOO

? (setf (symbol-function 'baz) #'foo)
#<Compiled-function FOO #x300041E22A5F>

? (function-lambda-expression #'baz)
NIL
NIL
FOO

-- 
http://lispm.dyndns.org/
From: ···········@gmail.com
Subject: Re: Renaming a function
Date: 
Message-ID: <280fc6cc-0772-479b-b856-3c8a50c020d8@r1g2000hsg.googlegroups.com>
On Dec 9, 11:54 am, Rainer Joswig <······@lisp.de> wrote:
> In article
> <····································@b1g2000pra.googlegroups.com>,
>
>  ···········@gmail.com wrote:
> > Suppose for some reason one needs to rename a global function, would
> > the following be OK in Common Lisp?
>
> > (setf (symbol-function 'foo2) (function foo1))
>
> > Rock
>
> You are not 'renaming' it. You are making a function
> available under a different name, additionally.
> That's fine.
>

Yeah, you're absolutely right! I didn't express it correctly.

> Minor point: it could be though, that some extra
> functionality of the Lisp environment gets lost - depending
> on how the Lisp environment stores the information.
> For example it could be difficult to locate the 'source
> code' for FOO2.
>

> See also here:
>
> ? (defun foo () 'bar)
> FOO
>
> ? (setf (symbol-function 'baz) #'foo)
> #<Compiled-function FOO #x300041E22A5F>
>
> ? (function-lambda-expression #'baz)
> NIL
> NIL
> FOO

function-lambda-expression ... that is really cool. I didn't even know
there was such a function!

Are there any other alternatives to doing what I did, perhaps even
conserving the information for function-lambda-expression?

Thanks.

Rock
From: Kent M Pitman
Subject: Re: Renaming a function
Date: 
Message-ID: <u7ijn334c.fsf@nhplace.com>
···········@gmail.com writes:

> Are there any other alternatives to doing what I did, perhaps even
> conserving the information for function-lambda-expression?

What most people do is to edit code in a text editor,
and make their changes that way. :)
From: Pascal J. Bourguignon
Subject: Re: Renaming a function
Date: 
Message-ID: <7chci4jaul.fsf@simias.anevia.com>
···········@gmail.com writes:

> On Dec 9, 11:54 am, Rainer Joswig <······@lisp.de> wrote:
>> In article
>> <····································@b1g2000pra.googlegroups.com>,
>>
>>  ···········@gmail.com wrote:
>> > Suppose for some reason one needs to rename a global function, would
>> > the following be OK in Common Lisp?
>>
>> > (setf (symbol-function 'foo2) (function foo1))

You could also copy over the documentation:

(defmacro defalias (new old)
  `(setf (symbol-function ',new)          (symbol-function ',old)
          (documentation ',new 'function) (documentation ',old 'function)
          (documentation ',new 'macro)    (documentation ',old 'macro)))

>> > Rock
>>
>> You are not 'renaming' it. You are making a function
>> available under a different name, additionally.
>> That's fine.
>>
>
> Yeah, you're absolutely right! I didn't express it correctly.
>
>> Minor point: it could be though, that some extra
>> functionality of the Lisp environment gets lost - depending
>> on how the Lisp environment stores the information.
>> For example it could be difficult to locate the 'source
>> code' for FOO2.

Difficult, as in "implementation dependant".

(defmacro defalias (new old)
  `(progn
      (setf (symbol-function ',new)          (symbol-function ',old)
            (documentation ',new 'function) (documentation ',old 'function)
            (documentation ',new 'macro)    (documentation ',old 'macro))
      #+clisp
      (setf (symbol-plist ',new) 
            (list* 'system::doc        (getf (symbol-plist ',old) 'system::doc)
                   'system::definition (getf (symbol-plist ',old) 'system::definition)
                   (symbol-plist ',new)))
      ',new))

C/CL-USER[35]> (defalias gm generate-mapping)
GM
C/CL-USER[36]> (gm)

*** - EVAL/APPLY: too few arguments given to GENERATE-MAPPING
The following restarts are available:
ABORT          :R1      ABORT


Some more could be done with (symbol-function 'gm), for which some
SYSTEM operators are provided to dissect it.  


Note how I defined DEFALIAS, not RENAME-FUNCTION.
To effectively rename a function, you might need more intelligence.

(defun f (x)  (if (zerop x)
                  (incf (getf 'f 'f 0))
                  (f (1- (getf 'f 'f 1)))))
(rename-function 'f 'g)

What should be incremented by (g 1)?




>> See also here:
>>
>> ? (defun foo () 'bar)
>> FOO
>>
>> ? (setf (symbol-function 'baz) #'foo)
>> #<Compiled-function FOO #x300041E22A5F>
>>
>> ? (function-lambda-expression #'baz)
>> NIL
>> NIL
>> FOO
>
> function-lambda-expression ... that is really cool. I didn't even know
> there was such a function!
>
> Are there any other alternatives to doing what I did, perhaps even
> conserving the information for function-lambda-expression?

In case you don't want to keep your code in external text "source"
files, you can also use something like IBCL.

http://www.informatimago.com/develop/lisp/small-cl-pgms/ibcl/

-- 
__Pascal Bourguignon__
From: Paul Donnelly
Subject: Re: Renaming a function
Date: 
Message-ID: <pan.2007.12.10.00.38.42.853137@sbcglobal.net>
On Sun, 09 Dec 2007 01:25:31 -0800, rocco.rossi wrote:

> Suppose for some reason one needs to rename a global function, would
> the following be OK in Common Lisp?
> 
> (setf (symbol-function 'foo2) (function foo1))
> 
> Rock

The usual way to do this would be:

(defun return-two () 3)

Ooops...

(fmakunbound 'return-two)
(defun return-three () 3)

Unless I'm misunderstanding what you're trying to do. Of course, this
requires you to have the function definition sitting around somewhere so
you can re-evaluate it, but you do, don't you?
From: ···········@gmail.com
Subject: Re: Renaming a function
Date: 
Message-ID: <77af6aeb-bcb5-4002-bb91-50694d80761a@q3g2000hsg.googlegroups.com>
On Dec 10, 1:46 am, Paul Donnelly <·············@sbcglobal.net> wrote:
> On Sun, 09 Dec 2007 01:25:31 -0800, rocco.rossi wrote:
> > Suppose for some reason one needs to rename a global function, would
> > the following be OK in Common Lisp?
>
> > (setf (symbol-function 'foo2) (function foo1))
>
> > Rock
>
> The usual way to do this would be:
>
> (defun return-two () 3)
>
> Ooops...
>
> (fmakunbound 'return-two)
> (defun return-three () 3)
>
> Unless I'm misunderstanding what you're trying to do. Of course, this
> requires you to have the function definition sitting around somewhere so
> you can re-evaluate it, but you do, don't you?

Well, actually I was just curious to know what would correspond to
doing something like this in Scheme:

(define add +)

After that, in Scheme, I would be able to use ADD instead of +
whenever I wanted to. I studied Scheme before Common Lisp and after
having read Peter Seibel's and Paul Graham's books, there are certain
key questions that I'm still wondering about.

I think the switch from Lisp-1 to Lisp-2 is a little rough at the
beginning (coming from Scheme), but I actually see the practicality of
it, especially when it comes to macros.

From what I've read in Graham's ANSI Common Lisp, I thought that the
right way to "convert" the above definition would be:

(setf (symbol-function 'add) #'+)

Rock
From: Don Geddis
Subject: Re: Renaming a function
Date: 
Message-ID: <87ve769q48.fsf@geddis.org>
···········@gmail.com wrote on Mon, 10 Dec 2007:
> Well, actually I was just curious to know what would correspond to
> doing something like this in Scheme:
> (define add +)
>
> From what I've read in Graham's ANSI Common Lisp, I thought that the
> right way to "convert" the above definition would be:
> (setf (symbol-function 'add) #'+)

How about
        (defun add (&rest args)
          (reduce #'+ args) )
or
        (defmacro add (args)
          `(+ ,@args) )

_______________________________________________________________________________
Don Geddis                  http://don.geddis.org/               ···@geddis.org
Mistakes:  It could be that the purpose of your life is only to serve as a
warning to others.  -- Despair.com
From: Johannes Weiner
Subject: Re: Renaming a function
Date: 
Message-ID: <x7odcxwq5n.fsf@saeurebad.de>
Hi,

Don Geddis <···@geddis.org> writes:

>         (defun add (&rest args)
>           (reduce #'+ args) )

Or more generic:

	(defun add (&rest args)
          (apply #'+ args))

>         (defmacro add (args)
>           `(+ ,@args) )

Looks like the most straight-forward way for `aliasing' the function.

	Hannes
From: Paul Donnelly
Subject: Re: Renaming a function
Date: 
Message-ID: <pan.2007.12.10.18.46.08.635210@sbcglobal.net>
On Mon, 10 Dec 2007 01:04:05 -0800, rocco.rossi wrote:

> On Dec 10, 1:46 am, Paul Donnelly <·············@sbcglobal.net> wrote:
>> On Sun, 09 Dec 2007 01:25:31 -0800, rocco.rossi wrote:
>> > Suppose for some reason one needs to rename a global function, would
>> > the following be OK in Common Lisp?
>>
>> > (setf (symbol-function 'foo2) (function foo1))
>>
>> > Rock
>>
>> The usual way to do this would be:
>>
>> (defun return-two () 3)
>>
>> Ooops...
>>
>> (fmakunbound 'return-two)
>> (defun return-three () 3)
>>
>> Unless I'm misunderstanding what you're trying to do. Of course, this
>> requires you to have the function definition sitting around somewhere so
>> you can re-evaluate it, but you do, don't you?
> 
> Well, actually I was just curious to know what would correspond to
> doing something like this in Scheme:
> 
> (define add +)

I see. You're wondering how to give a function *additional* names, not how
to replace its name.
From: Steven M. Haflich
Subject: Re: Renaming a function
Date: 
Message-ID: <OXm7j.70139$RX.13820@newssvr11.news.prodigy.net>
Paul Donnelly wrote:

> I see. You're wondering how to give a function *additional* names, not how
> to replace its name.

Of course that's what he wants.  Functions do not have names in Common 
Lisp.  Names may be bound to functions (or macros or special operators) 
in the function namespace.

The original query missed something else, and I don't think any of the 
subsequent posts have picked up on it.  The original query concerned 
naming in the global function namespace.  The following doesn't quite 
accomplish this

   (setf (symbol-function 'foo) #'bar)

because if it is executed in a lexical environment that happens to 
define #'bar, it will find the lexical binding instead of the global 
binding.  More symmetrical would be

   (setf (symbol-function 'foo) (symbol-function 'bar))

Better than symbol-function in both places would be fdefinition, since 
that will also accept global setf function names.
From: ···········@gmail.com
Subject: Re: Renaming a function
Date: 
Message-ID: <4fa1232a-98d1-42e0-a893-e8adb40314c2@e23g2000prf.googlegroups.com>
On Dec 11, 3:52 am, "Steven M. Haflich" <····@alum.mit.edu> wrote:
> Paul Donnelly wrote:
> > I see. You're wondering how to give a function *additional* names, not how
> > to replace its name.
>
> Of course that's what he wants.  Functions do not have names in Common
> Lisp.  Names may be bound to functions (or macros or special operators)
> in the function namespace.
>
> The original query missed something else, and I don't think any of the
> subsequent posts have picked up on it.  The original query concerned
> naming in the global function namespace.  The following doesn't quite
> accomplish this
>
>    (setf (symbol-function 'foo) #'bar)
>
> because if it is executed in a lexical environment that happens to
> define #'bar, it will find the lexical binding instead of the global
> binding.  More symmetrical would be
>
>    (setf (symbol-function 'foo) (symbol-function 'bar))
>
> Better than symbol-function in both places would be fdefinition, since
> that will also accept global setf function names.

Yeah. Thank you so much for that info. I've just looked it up in the
hyperspec. I've also read there that neither SYMBOL-FUNCTION nor
FDEFINITION can access local function symbols defined with LABELS or
FLET. Does that mean that these symbols are completely inaccessible?
Not that that would necessarily be a bad thing of course. Just wanted
to know.

Thanx again.
From: ···········@gmail.com
Subject: Re: Renaming a function
Date: 
Message-ID: <86ba4e9d-df34-4d7d-bcbf-029f12bed601@r1g2000hsg.googlegroups.com>
On Dec 11, 1:16 pm, ···········@gmail.com wrote:
> On Dec 11, 3:52 am, "Steven M. Haflich" <····@alum.mit.edu> wrote:
>
>
>
> > Paul Donnelly wrote:
> > > I see. You're wondering how to give a function *additional* names, not how
> > > to replace its name.
>
> > Of course that's what he wants.  Functions do not have names in Common
> > Lisp.  Names may be bound to functions (or macros or special operators)
> > in the function namespace.
>
> > The original query missed something else, and I don't think any of the
> > subsequent posts have picked up on it.  The original query concerned
> > naming in the global function namespace.  The following doesn't quite
> > accomplish this
>
> >    (setf (symbol-function 'foo) #'bar)
>
> > because if it is executed in a lexical environment that happens to
> > define #'bar, it will find the lexical binding instead of the global
> > binding.  More symmetrical would be
>
> >    (setf (symbol-function 'foo) (symbol-function 'bar))
>
> > Better than symbol-function in both places would be fdefinition, since
> > that will also accept global setf function names.
>
> Yeah. Thank you so much for that info. I've just looked it up in the
> hyperspec. I've also read there that neither SYMBOL-FUNCTION nor
> FDEFINITION can access local function symbols defined with LABELS or
> FLET. Does that mean that these symbols are completely inaccessible?
> Not that that would necessarily be a bad thing of course. Just wanted
> to know.
>
> Thanx again.

I think I've found the answer to that last question (Hyperspec):

"Common Lisp provides no operation on a symbol that can have any
effect on a lexical variable or on a lexical function definition."
From: Rob Warnock
Subject: Re: Renaming a function
Date: 
Message-ID: <Ie6dndw9Y9-00sLanZ2dnUVZ_hisnZ2d@speakeasy.net>
<···········@gmail.com> wrote:
+---------------
| I think I've found the answer to that last question (Hyperspec):
| "Common Lisp provides no operation on a symbol that can have any
| effect on a lexical variable or on a lexical function definition."
+---------------

That's because by the time the code runs lexical variabless don't
even *have* any "symbols" any more, at least not once the code is
compiled [or even just pre-processed for "fast interpretation"].
They're [usually] just converted to stack offsets in the resulting
code. E.g., try this in your favorite CL compiler:

    (disassemble
      (lambda (x)
	(declare (type (integer -1000 1000) x)
		 (optimized (speed 3) (safety 0) (debug 0)))
	(let ((y 5))
	  (+ y (* x 37)))))

See if you can find the "Y" anywhere...  ;-}


-Rob

p.s. In CMUCL on x86, the only trace left of Y is this
single instruction:

    ADD     EDX, 20

[In CMUCL, fixnums are 30-bit signed ints shifted left two,
hence 5 => 20.]


-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Damien Kick
Subject: Re: Renaming a function
Date: 
Message-ID: <13nd501rsjv2j3c@corp.supernews.com>
···········@gmail.com wrote:

> Well, actually I was just curious to know what would correspond to
> doing something like this in Scheme:
> 
> (define add +)

> After that, in Scheme, I would be able to use ADD instead of +
> whenever I wanted to. I studied Scheme before Common Lisp and after
> having read Peter Seibel's and Paul Graham's books, there are certain
> key questions that I'm still wondering about.
> 
> [...]
> 
> From what I've read in Graham's ANSI Common Lisp, I thought that the
> right way to "convert" the above definition would be:
> 
> (setf (symbol-function 'add) #'+)

Of some interest might be the following from Paul Graham's _On Lisp_

(defmacro abbrev (short long)
   �(defmacro ,short (&rest args)
      �(,�,long ,@args)))

(defmacro abbrevs (&rest names)
   �(progn
      ,@(mapcar #�(lambda (pair)
                    �(abbrev ,@pair))
                  (group names 2))))

Which would allow for the following:

(abbrev add +)

It isn't quite the same thing, macros being different than functions. 
And, in this case, "add" is a poor abbreviation for "+".  Perhaps 
DEFSYNONYM might be a better name or DEFSYN to spice it up.