2
$\begingroup$

Suppose it's possible for a symbol to escape the scope in which it is defined. What are considered the possible policies for handling that? I mean possible in the sense of what choices of language design that could be implemented -- different choices yield languages which differ in this respect.

For example, it's conceivable that two lexical scopes might each contain a symbol named "x". If these symbols escape from their respective scopes, what can we say about that? I can see three possibilities: all symbols named "x" are the same, so the symbol leaked out of the first scope is the same as that leaked out of the second; or each symbol named "x" is actually a distinct symbol, so the leaked symbols are not the same; or when a symbol escapes from its scope, something about that lexical context is carried along with the symbol, so that the result of the leakage is not a symbol but a more complex object representing the lexical context or environment and the symbol together. No doubt there are other possibilities.

I know Common Lisp implements the first policy -- (eq (let (x) 'x) (let (x) 'x)) yields T. I think Scheme does too. I don't know enough about other languages to say what might happen. I'm not aware of languages which implement the second or third ideas, but, as I was saying, my awareness is limited, and I would be interested to hear about any such languages.

I'm sure this is a topic which is well-known in the programming languages field, but through some reading and searching I haven't found it yet.

$\endgroup$
1
$\begingroup$

Your (let (x) 'x) will just return the atom x, and obviously all of those are the same. That you define variables called x (and give them no value whatsoever, and never use them) is just confusing the issue. I.e., your code is a roundabout way to write (eq 'x 'x).

How could a name "escape" from a lexical scope? The scope is precisely where the name has a particular meaning, an "escape" would just expand that scope somehow.

E.g. in Steelbank Common Lisp:

* (let (x) 'x)
; in: LET (X)
;     (LET (X)
;       'X)
; 
; caught STYLE-WARNING:
;   The variable X is defined but never used.
; 
; compilation unit finished
;   caught 1 STYLE-WARNING condition
X

enter code here

| cite | improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Thanks for your reply. I don't mean to limit consideration to Common Lisp or Lisp family languages. The question is counterfactual in the sense that the languages which I know about don't actually support the idea of distinct symbols by the same name in different scopes. I'm interested in the question in a more general, possibly abstract sense. $\endgroup$ – Robert Dodier Jan 16 at 18:45
  • 1
    $\begingroup$ The only « escape » i could think of is something akin to a closure, which seems similar to option 3 in OP $\endgroup$ – D. Ben Knoble Jan 17 at 18:21
  • 1
    $\begingroup$ @D.BenKnoble Yes, it's essentially a closure containing a general expression instead of a function. By the way, if you know of any languages which implement closures for general expressions, and/or languages which distinguish symbols by the same name in different scopes, I would be interested to know which ones they are so that I can read up. $\endgroup$ – Robert Dodier Jan 19 at 1:41
  • $\begingroup$ @RobertDodier, C++, for one (namespaces). $\endgroup$ – vonbrand Feb 3 at 17:16
  • $\begingroup$ @D.BenKnoble, no escape there. The lexical scope covers the closure. $\endgroup$ – vonbrand Feb 3 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.