Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symbol names undefined variable

Tags:

common-lisp

I have a question concerning the relation between symbols and global variables.

The hyperspec states for the value attribute of a symbol:

"If a symbol has a value attribute, it is said to be bound, and that fact can be detected by the function boundp. The object contained in the value cell of a bound symbol is the value of the global variable named by that symbol, and can be accessed by the function symbol-value."

If I apply the following steps:

CL-USER> (intern "*X*")
*X*
NIL
CL-USER> (boundp '*x*)
NIL
CL-USER> (setf (symbol-value '*x*) 1)
1
CL-USER> (boundp '*x*)
T

as I understand the above cited conditions are fulfilled. There should be a global variable named by the symbol and the value of the variable is the symbol-value. But this is wrong.

CL-USER> (describe '*x*)
COMMON-LISP-USER::*X*
  [symbol]

*X* names an undefined variable:
  Value: 1
; No value
CL-USER> 

It has to be proclaimed special.

CL-USER> (proclaim '(special *x*))
; No value
CL-USER> (describe '*x*)
COMMON-LISP-USER::*X*
  [symbol]

*X* names a special variable:
  Value: 1
; No value
CL-USER> 

Can you please explain this behaviour. What means an "undefined variable", I did not find this term in the hyperspec.

(I use SBCL 1.3.15.)

Thank you for your answers.

Edit:

(Since this comment applies to both answers below (user Svante and coredump), I write it as an Edit and not as a comment to both answers).

I agree with the answers that * x* is a global variable.

The hyperspec states for global variable:

"global variable n. a dynamic variable or a constant variable."

Therefore I think now, the reason why SBCL says it is "undefined" is not whether is special, but whether it is a dynamic (special) variable or a constant variable (hyperspec:"constant variable n. a variable, the value of which can never change").

The third definition which is mentioned in the below answers (maybe I understand the answers wrong), that it is a global variable which is not special (and not a constant) does not exist according to the hyperspec.

Can you agree with that?

Edit 2:

Ok, in summary, I thought, since the hyperspec does not define undefined global variables, they do not exist.

But the correct answer is, they do exist and are undefined, that means it is implementation dependent how it is dealt with them.

Thank you for your answers, I accept all three of them, but I can only mark one.

like image 256
N. Böck Avatar asked Aug 01 '26 23:08

N. Böck


1 Answers

There is a global variable named by the symbol, and its value is the symbol-value. That is what the output tells you. The thing that is undefined is the status of the variable: whether it is special. I agree that the wording of the output is a bit special.

If you set the value of a variable without creating it first (which is what a naked setq would do as well), it is undefined whether it becomes special or not.

Conventionally, one does not use global variables that are not special. That's why you should use defvar, defparameter etc..

like image 138
Svante Avatar answered Aug 10 '26 06:08

Svante



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!