Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the X in (LET ((x ...) a fully fleshed symbol?

Tags:

common-lisp

Or in other words: Is it possible for a variable in CL not to be (part of) a symbol?

I think I may have a profound misconception about variables in CL.

I always thought CL has no variables, only symbols, and symbols have (among other properties) a name and a value cell (which is the variable).
And when someone said "variable x has the value 42" I thought it was short for "the value cell of the symbol named x stores the value 42".

But this is probably wrong.

When I type

> (let ((a 42))
       (type-of 'a))
SYMBOL
; caught STYLE-WARNING:
;   The variable A is defined but never used.

is the lexical variable a in this example a fully fleshed symbol whose value cell has been set to 42?

Because the warning The variable A is defined but never used suggests otherwise and it appears that the lexical variable is not the same thing as the symbol a in the following form (type-of 'a).

like image 688
Frank Avatar asked Dec 07 '25 17:12

Frank


1 Answers

Common Lisp has two data types which have a special meaning for evaluation:

  • cons cells / lists -> used in Lisp source code, lists are Lisp forms
  • symbols -> used as names for various purposes

If you want to use them as data in Lisp code, then you have to quote them.

Both are used in the Lisp source code, but once you compile code, they may disappear.

Variables are written as symbols in the source code. But in compiled code they may go away - when they are lexical variables.

Example using SBCL:

a file with

(defun test (foo)
  (+ foo foo))

Now we do:

CL-USER> (proclaim '(optimize (debug 0)))  ; the compiler saves no debug info
; No value
CL-USER> (compile-file "/tmp/test.lisp")
; compiling file "/private/tmp/test.lisp" (written 23 MAY 2017 09:06:51 PM):
; compiling (DEFUN TEST ...)

; /tmp/test.fasl written
; compilation finished in 0:00:00.013
#P"/private/tmp/test.fasl"
NIL
NIL
CL-USER> (find-symbol "FOO")
FOO
:INTERNAL

The compiler has read the source code and created a compiled FASL file. We see that the symbol FOO is now in the current package. FOO names the variable in our source code.

Now quit SBCL and restart it.

Let's load the machine code:

CL-USER> (load "/tmp/test")
T
CL-USER> (find-symbol "FOO")
NIL
NIL

There is no symbol FOO anymore. It's also not possible to retrieve the lexical value of the variable FOO using the symbol FOO. There is no mapping (like some kind of explicit lexical environment) from symbols to lexical values.

like image 154
Rainer Joswig Avatar answered Dec 10 '25 14:12

Rainer Joswig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!