From: KanZen
Subject: (setf #'x 42)
Date: 
Message-ID: <10eb079f.0404151204.5cbb4c7c@posting.google.com>
I noticed that if I have some function x that I can't seem to manually overwrite
the function slot with:

(setf #'x 42) or (setf (function x) 42)

Is the function slot not "seteffable"? (This is with CLisp or CMUCL)

KanZen

From: Sashank Varma
Subject: Re: (setf #'x 42)
Date: 
Message-ID: <none-334F28.15235115042004@news.vanderbilt.edu>
In article <····························@posting.google.com>,
 ······@mail.com (KanZen) wrote:

> I noticed that if I have some function x that I can't seem to manually 
> overwrite
> the function slot with:
> 
> (setf #'x 42) or (setf (function x) 42)
> 
> Is the function slot not "seteffable"? (This is with CLisp or CMUCL)

[In MCL]

? (setf (function x) 42)
;Compiler warnings :
;   Undeclared free variable X, in an anonymous lambda form.
;   Undefined function SETF::|COMMON-LISP::FUNCTION|, in an anonymous 
lambda form.
> Error: Unbound variable: X
> While executing: #<Anonymous Function #xA39E6E>
> Type Command-/ to continue, Command-. to abort.
> If continued: Retry getting the value of X.
See the Restarts� menu item for further choices.
1 > 

Ah, perhaps you meant to try:

? (setf (symbol-function 'foo) 42)
> Error: value 42 is not of the expected type (SATISFIES FUNCTIONP).
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This error message is instructive.

> While executing: FSET
> Type Command-. to abort.
See the Restarts� menu item for further choices.
1 > 
Aborted
? (setf (symbol-function 'foo) (symbol-function 'car))
#<Compiled-function CAR #x94D1D6>
? (foo '(bar baz))
BAR
? 

So the function slot of a symbol can be SETFed, but the value must
be of the required type.
From: KanZen
Subject: Re: (setf #'x 42)
Date: 
Message-ID: <10eb079f.0404152017.44a81eb4@posting.google.com>
Sashank Varma <····@vanderbilt.edu> wrote in message news:<··························@news.vanderbilt.edu>...


> ? (setf (symbol-function 'foo) (symbol-function 'car))
> #<Compiled-function CAR #x94D1D6>

Strange that this works, but this fails:

[34]> (setf #'foo #'car)

*** - EVAL: variable FOO has no value
The following restarts are available:
STORE-VALUE    :R1      You may input a new value for FOO.
USE-VALUE      :R2      You may input a value to be used instead of FOO.

I don't understand the difference between #' and symbol-function

KanZen
From: Frode Vatvedt Fjeld
Subject: Re: (setf #'x 42)
Date: 
Message-ID: <2hd6685m4l.fsf@vserver.cs.uit.no>
······@mail.com (KanZen) writes:

> I don't understand the difference between #' and symbol-function

First of all, #'foo is the same as (function foo). Secondly, (setf
foo) is not magically able to figure out how to set whatever foo
normally reads. (setf (symbol-function 'bar) #'car) works because the
operator named (setf symbol-function) is defined. There is no operator
named (setf function) defined in Common Lisp, hence there is no reason
to expect it to work.

-- 
Frode Vatvedt Fjeld
From: Hannah Schroeter
Subject: Re: (setf #'x 42)
Date: 
Message-ID: <c5mqg1$hnj$1@c3po.use.schlund.de>
Hello!

KanZen <······@mail.com> wrote:
>I noticed that if I have some function x that I can't seem to manually overwrite
>the function slot with:

>(setf #'x 42) or (setf (function x) 42)

These are exactly identical after read-time.

>Is the function slot not "seteffable"? (This is with CLisp or CMUCL)

function is a special form.

Use (setf (symbol-function 'x) 42) instead.

Kind regards,

Hannah.
From: Steven M. Haflich
Subject: Re: (setf #'x 42)
Date: 
Message-ID: <qGIfc.23166$Ak1.21981@newssvr27.news.prodigy.com>
Hannah Schroeter wrote:

>>Is the function slot not "seteffable"? (This is with CLisp or CMUCL) 
> 
> function is a special form.
> 
> Use (setf (symbol-function 'x) 42) instead.

This deserves a little more explanation why the language definition
defines symbol-function to be setfable, but not the function special
operator.

Observe these results:

  cl-user(2): (defun foo (x) x)
  foo
  cl-user(3): (flet ((foo (y) (1+ y)))
                (values (funcall (function foo) 1)
                        (funcall (symbol-function 'foo) 1)))
  2
  1

The function special form sees the lexical environment.  The
function symbol-function does not, referring instead to a global
attribute of the symbol.

In order to make lexical analysis tractible, the language definition
severly restricts the ways you can affect the lexical environment.
This is a good thing, oherwise the language would be vwery hard to
implement efficiently, and even harder to understand when reading
code.
From: Marcin 'Qrczak' Kowalczyk
Subject: Re: (setf #'x 42)
Date: 
Message-ID: <pan.2004.04.16.11.59.33.327749@knm.org.pl>
On Fri, 16 Apr 2004 03:46:30 +0000, Steven M. Haflich wrote:

> The function special form sees the lexical environment.  The
> function symbol-function does not, referring instead to a global
> attribute of the symbol.
> 
> In order to make lexical analysis tractible, the language definition
> severly restricts the ways you can affect the lexical environment.

Isn't this analogous to setq vs. set? The language does allow to setq
lexical variables, and it doesn't affect other variables because the
variable is known at compile time. Similarly setf (function foo) could
be allowed without sacrificing efficiency, and without requiring
sophisticated analysis - the compiler would just have to check whether
the given lexical function is ever assigned to in its scope.

-- 
   __("<         Marcin Kowalczyk
   \__/       ······@knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/
From: Pascal Costanza
Subject: Re: (setf #'x 42)
Date: 
Message-ID: <c5ojhr$t7u$1@f1node01.rhrz.uni-bonn.de>
Marcin 'Qrczak' Kowalczyk wrote:

> On Fri, 16 Apr 2004 03:46:30 +0000, Steven M. Haflich wrote:
> 
>>The function special form sees the lexical environment.  The
>>function symbol-function does not, referring instead to a global
>>attribute of the symbol.
>>
>>In order to make lexical analysis tractible, the language definition
>>severly restricts the ways you can affect the lexical environment.
> 
> Isn't this analogous to setq vs. set? The language does allow to setq
> lexical variables, and it doesn't affect other variables because the
> variable is known at compile time. Similarly setf (function foo) could
> be allowed without sacrificing efficiency, and without requiring
> sophisticated analysis - the compiler would just have to check whether
> the given lexical function is ever assigned to in its scope.

You mean:

(let ((f (lambda (...) ...)))
   (funcall f ...)
   (setf f (lambda (...) ...))
   (apply f ...))

?


How often do you need this?

Pascal

-- 
ECOOP 2004 Workshops - Oslo, Norway
*1st European Lisp and Scheme Workshop, June 13*
http://www.cs.uni-bonn.de/~costanza/lisp-ecoop/
*2nd Post-Java Workshop, June 14*
http://prog.vub.ac.be/~wdmeuter/PostJava04/
From: Marcin 'Qrczak' Kowalczyk
Subject: Re: (setf #'x 42)
Date: 
Message-ID: <pan.2004.04.17.12.41.01.347796@knm.org.pl>
On Fri, 16 Apr 2004 14:28:41 +0200, Pascal Costanza wrote:

>> Isn't this analogous to setq vs. set? The language does allow to setq
>> lexical variables, and it doesn't affect other variables because the
>> variable is known at compile time. Similarly setf (function foo) could
>> be allowed without sacrificing efficiency, and without requiring
>> sophisticated analysis - the compiler would just have to check whether
>> the given lexical function is ever assigned to in its scope.
> 
> You mean:
> 
> (let ((f (lambda (...) ...)))
>    (funcall f ...)
>    (setf f (lambda (...) ...))
>    (apply f ...))
> 
> ?

No. I mean that allowing setf (function foo) would not prevent any
optimizations which are currently possible. It's not true that it would
"make lexical analysis less tractible".

We know that the presence of setq doesn't prevent the compiler from
knowing which variable bindings are lexical and which are not, and it
doesn't prevent statically knowing the value of local variables which
are not being rebound - because the variable to rebind is known
statically. The same would apply to function bindings.

> How often do you need this?

I don't say I want to assign local function bindings often, but allowing
it would be consistent with other uses of setf. Now I must remember that
#'foo is not setfable, even though foo is, and both (symbol-value 'foo)
and (symbol-function 'foo) are. I must use setf (symbol-function 'foo)
to set what I refer to as #'foo when reading, instead of the more obvious
setf #'foo. Why is this inconsistency?

-- 
   __("<         Marcin Kowalczyk
   \__/       ······@knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/
From: Pascal Costanza
Subject: Re: (setf #'x 42)
Date: 
Message-ID: <c5ra13$om2$1@newsreader2.netcologne.de>
Marcin 'Qrczak' Kowalczyk wrote:

> I don't say I want to assign local function bindings often, but allowing
> it would be consistent with other uses of setf. Now I must remember that
> #'foo is not setfable, even though foo is, and both (symbol-value 'foo)
> and (symbol-function 'foo) are. I must use setf (symbol-function 'foo)
> to set what I refer to as #'foo when reading, instead of the more obvious
> setf #'foo. Why is this inconsistency?

symbol-function and function already behave differently, so it's not 
true that you "have" to say (setf (symbol-function 'foo) ...) when you 
actually "want" (setf #'foo ...). You simply cannot do the latter, and 
the former means something else.

CL already treats the function and value namespaces differently, so this 
isn't any more inconsistent than CL already is. There are lots of other 
forms that are not setfable in CL, so what's the point?


Pascal

-- 
1st European Lisp and Scheme Workshop
June 13 - Oslo, Norway - co-located with ECOOP 2004
http://www.cs.uni-bonn.de/~costanza/lisp-ecoop/
From: Edi Weitz
Subject: Re: (setf #'x 42)
Date: 
Message-ID: <m3ekqpc85t.fsf@bird.agharta.de>
On 15 Apr 2004 13:04:44 -0700, ······@mail.com (KanZen) wrote:

> I noticed that if I have some function x that I can't seem to
> manually overwrite the function slot with:
>
> (setf #'x 42) or (setf (function x) 42)
>
> Is the function slot not "seteffable"? (This is with CLisp or CMUCL)

Use SYMBOL-FUNCTION.

  <http://www.lispworks.com/reference/HyperSpec/Body/f_symb_1.htm>

Edi.
From: Thomas F. Burdick
Subject: Re: (setf #'x 42)
Date: 
Message-ID: <xcvr7up7zm5.fsf@famine.OCF.Berkeley.EDU>
Edi Weitz <···@agharta.de> writes:

> On 15 Apr 2004 13:04:44 -0700, ······@mail.com (KanZen) wrote:
> 
> > I noticed that if I have some function x that I can't seem to
> > manually overwrite the function slot with:
> >
> > (setf #'x 42) or (setf (function x) 42)
> >
> > Is the function slot not "seteffable"? (This is with CLisp or CMUCL)
> 
> Use SYMBOL-FUNCTION.

But don't use it to set a symbol's function definition to 42.

-- 
           /|_     .-----------------------.                        
         ,'  .\  / | No to Imperialist war |                        
     ,--'    _,'   | Wage class war!       |                        
    /       /      `-----------------------'                        
   (   -.  |                               
   |     ) |                               
  (`-.  '--.)                              
   `. )----'                               
From: Karl A. Krueger
Subject: Re: (setf #'x 42)
Date: 
Message-ID: <c5mrli$cpq$1@baldur.whoi.edu>
Edi Weitz <···@agharta.de> wrote:
> On 15 Apr 2004 13:04:44 -0700, ······@mail.com (KanZen) wrote:
>> I noticed that if I have some function x that I can't seem to
>> manually overwrite the function slot with:
>>
>> (setf #'x 42) or (setf (function x) 42)
>>
>> Is the function slot not "seteffable"? (This is with CLisp or CMUCL)
> 
> Use SYMBOL-FUNCTION.
> 
>  <http://www.lispworks.com/reference/HyperSpec/Body/f_symb_1.htm>


You still can't (SETF (SYMBOL-FUNCTION 'X) 42) though:

http://www.lispworks.com/reference/HyperSpec/Issues/iss175_w.htm :

#    5b. SETF of SYMBOL-FUNCTION requires a FUNCTION as the new value.
#        It is an error to set the SYMBOL-FUNCTION of a symbol to a
#        symbol or a list or the value returned by SYMBOL-FUNCTION on
#        the name of a macro or a special form.


That latter clause also, incidentally, bans (SETF (SYMBOL-FUNCTION 'X)
(SYMBOL-FUNCTION 'WHEN)), since WHEN is a special form.

CLISP allows it, though, and X appears subsequently to work like WHEN.

CMUCL and SBCL allow the SETF, but break nastily when you try to use it.

Weird.

-- 
Karl A. Krueger <········@example.edu>
Woods Hole Oceanographic Institution
Email address is spamtrapped.  s/example/whoi/
"Outlook not so good." -- Magic 8-Ball Software Reviews
From: Harald Hanche-Olsen
Subject: Re: (setf #'x 42)
Date: 
Message-ID: <pcozn9dugr2.fsf@shuttle.math.ntnu.no>
+ ······@mail.com (KanZen):

| I noticed that if I have some function x that I can't seem to
| manually overwrite the function slot with:
| 
| (setf #'x 42) or (setf (function x) 42)
| 
| Is the function slot not "seteffable"? (This is with CLisp or CMUCL)

FUNCTION is a special form, not an accessor.  Hence no SETF on it.

The proper form for what you are trying to do is

(setf (symbol-function) ...)

I don't know if you really wanted to assign 42 to the function slot of
symbol x, though.  I don't believe you're allowed to assign
non-functions to the function slot.

You really should learn to use the Hyperspec.
Then you can answer many of these questions yourself.

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- Debating gives most of us much more psychological satisfaction
  than thinking does: but it deprives us of whatever chance there is
  of getting closer to the truth.  -- C.P. Snow
From: Kaz Kylheku
Subject: Re: (setf #'x 42)
Date: 
Message-ID: <cf333042.0404160936.3d3eb186@posting.google.com>
······@mail.com (KanZen) wrote in message news:<····························@posting.google.com>...
> I noticed that if I have some function x that I can't seem to manually overwrite
> the function slot with:
> 
> (setf #'x 42) or (setf (function x) 42)
> 
> Is the function slot not "seteffable"?

(FUNCTION X) refers to the function slot of symbol X only if there is
no lexical function binding in scope! X could be a LABELS or FLET, in
which case there is no such slot.

You can think of FUNCTION as generating a function object, rather than
just retrieving it from a binding. In Lisp, functions aren't really
objects until they are called upon to be. If you have (FLET ((FUNC ()
...)) ...), FUNC isn't really an object, but (FUNCTION FUNC) produces
one.

Alternately, it may help to think of FUNCTION as a special kind of
quoting. You can't treat (QUOTE X) as an assignment place either! You
can't make X be 42 using (SETF 'X 42).