3

Are symbols and names different? On Lisp by Paul Graham, which focuses on common lisp, has some discussions that seem to imply so, e.g.

Since lambda-expressions are also names of functions, they can also appear first in function calls:
((lambda (x) (* x 2) 3)
6

This makes it sound like symbols are names but names aren't symbols. But I don't understand what sort of Lisp "object" symbols are / could be.

This is also deriving from my question here on the sharp-quote (#') operator v. symbol-function. I'm suspecting the only reason these are different is because not all names are symbols but I don't have enough background to understand those answers yet (hence this question).

I'm also asking for clarification in elisp v. common lisp. I'm assuming this pertains to lexical form, which wasn't introduced in elisp until version 24 (24.1 I think).

Community
  • 1
  • 1
djechlin
  • 59,258
  • 35
  • 162
  • 290
  • 1
    It's worth noting (as it's [caught me](http://stackoverflow.com/a/24739633/1281433) in the past, too), that *On Lisp* predates the Common Lisp standard, so some of Graham's terminology or assumptions about the language don't line up perfectly. That said, it's true that some symbols are names, but that not all names are symbols. – Joshua Taylor Mar 29 '15 at 20:08
  • @JoshuaTaylor: the concept of a Function Name had been introduced into Common Lisp during standardization in 1989. It had been described in CLtL2 (published 1990). On Lisp was published 1993. The wording in On Lisp looks like it is from CLtL1... – Rainer Joswig Mar 29 '15 at 21:05
  • @RainerJoswig Hmmm, I didn't check the dates; I was basing that on [this comment](http://stackoverflow.com/questions/24721676/continuation-in-common-lisp-by-macros-regarding-an-implemetation-in-onlisp/24739633#comment38393774_24739633). – Joshua Taylor Mar 29 '15 at 21:10
  • @JoshuaTaylor: CLtL2 published the language as defined by the ANSI CL effort up to that point - before the standard was finalized. But it was seen necessary to publish the changes/extension, because the users needed an update of CLtL1. Even before, a lambda expression was not a function name. CLtL1 mentions that it is 'treated' as a name in lambda expression forms and inside FUNCTION. But lambda expressions were never function names as such. – Rainer Joswig Mar 29 '15 at 21:15

2 Answers2

5

Lambda Expressions are not names of functions. It's just that ((lambda (...) ...) ...) is allowed in Common Lisp, since it is defined in the standard as legal syntax.

The only allowed function names in Common Lisp are symbols and lists like (setf symbol).

For example one can write

(defun (setf foo) (...) ...)

Here (setf foo) is the function name.

Other function names don't exist in Common Lisp, only symbols and (setf symbol) names.

Common Lisp Hyperspec Glossary: Function Name.

function name n. 1. (in an environment) A symbol or a list (setf symbol) that is the name of a function in that environment. 2. A symbol or a list (setf symbol).

Note: the Common Lisp version of 1984 (as published in CLtL1) did only have symbols as function names. Thus the idea of a function name was not defined. The function to retrieve a function from a symbol was called SYMBOL-FUNCTION. In 1989 the ANSI CL standardization group decided to add setf lists as function names. It also introduced the function FDEFINITION, which is like SYMBOL-FUNCTION but also accepts other function names, besides symbols. See here: Issue FUNCTION-NAME.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
  • The issue says that "Implementations are free to extend the syntax of function-names to include lists beginning with additional symbols other than SETF." But I was unable to find this in the hyperspec proper. – Samuel Edwin Ward Mar 30 '15 at 18:54
  • @SamuelEdwinWard: I don't think that sentence made it into the standard. – Rainer Joswig Mar 30 '15 at 19:09
4

I think Rainer's answer gets this right, but since the question appeared in a comment on my answer to another question, I'll include (an update to) my response from that comment.

A symbol is an actual object. You can inspect it, you can create them with make-symbol, etc. Importantly, symbols are one of the primary components of source code in Common Lisp. Function names, especially in the context that this question arose in (arguments to the function special operator) are either symbols or lists of the form (setf symbol) according to the glossary entry:

function name n. 1. (in an environment) A symbol or a list (setf symbol) that is the name of a function in that environment. 2. A symbol or a list (setf symbol).

Function is a special operator that doesn't evaluate its arguments, so passing a symbol or setf list means something like:

(function car)
(function (setf car))

and not:

(function 'car)
(function '(setf car))

Now, lexical variables, e.g., x in (let ((x 42)) x), while represented by symbols in the source code, don't actually have any connection with the symbol at runtime. The compiled version of (let ((x 42)) x) doesn't need to know anything about the symbol x. Intuitively, this makes sense, because we'd expect the code (let ((y 42)) y) to compile to the same thing. However, when a variable is special, there is a connection with the symbol. The difference is clearest with:

(let ((x 42))
  (symbol-value x))
;=> NIL

(let ((x 42))
  (declare (special x))  ; or (defparameter x ...) or (defvar x ...) earlier
  (symbol-value x))
;=> 42

We'd expect the same thing to be true of lexically scoped functions, e.g., the following code causes an error because there's no connection between the symbol x at runtime and the local function:

(flet ((x () 42)) 
  (symbol-function 'x)) ; ERROR, no function value for symbol x

But even so, we can still do:

(flet ((x () 42))
  (function x))

This is because function is special operator and can access the environment where is occurs. That means that (because it's special, and the implementation makes it work) it can know that x is defined as function here. It may be interesting to note, now, that since flet and labels are defined to take a function name, you can do:

(flet (((setf kar) (value kons)
          ...))
  ...)
Community
  • 1
  • 1
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353