2

This is a pretty technical question about conforming implementations of the ANSI Lisp spec:

In Section 2.3.4, it says that a token read as a symbol that's not available in the current package is interred as a new symbol in the current package.

In Section 3.1.2.1, it says that if a symbol isn't bound, an error is given.

What happens to unbound symbols between iterations of the REPL? Will a conforming implementation remove unbound symbols from the current package, or will it keep interning symbols until the REPL occupies all available memory?

sds
  • 58,617
  • 29
  • 161
  • 278

1 Answers1

5

Once you intern a symbol, it stays interned until you explicitly unintern it. And if you type the name of an interned symbol, that same symbol is returned.

The reader doesn't care whether a symbol is bound or not. That only becomes relevant if you try to evaluate the symbol. But symbols can be used without being evaluated, for instance as part of quoted data, or when the program calls READ itself.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • If a symbol is not used (referenced) anywhere (has no symbol-value, no symbol-function, no property lists, is not mentioned in a function, etc etc etc), would a conforming implementation be allowed to garbage collect it on the basis that there would be no visible difference between that and the specified behaviour. (I'm thinking along the lines of [C++'s as-if rule](https://en.cppreference.com/w/cpp/language/as_if)). --- In other words: May a conformant Lisp GC unused symbols if that would "not change the observable behavior of the program"? – Allison Lock Apr 26 '20 at 09:29
  • 1
    @GavinLock: Not really, because the GC can never know that some future bit of code might not do `(find-symbol ...)` on the name of the symbol. If either the system could *prove* that no such introspection code would ever happen by any path, or you warranted to it that it never would, then it could start excising things (not just symbols, of course) that it could prove were not used: that's what a tree-shaker does. –  Apr 26 '20 at 10:12
  • @tfb That make sense - `find-symbol`, `do-symbols`, `with-package-iterator`, etc etc mean that GCing an unused symbol certainly *could* change the observable behaviour of a program. Thanks. – Allison Lock Apr 26 '20 at 11:07
  • @GavinLock: Yes. I think that packages are really some of the roots for GC, and anything reachable from a package is therefore itself reachable in normal usage. –  Apr 26 '20 at 13:03
  • @GavinLock MacLisp had an option for this called `GCTWA`: Garbage Collect Truly Worthless Atoms. But it was off by default. CL implementations might have similar implementation-dependent options, usually useful when saving an image. – Barmar Apr 26 '20 at 14:09
  • This means that SBCL, CMUCL, and CLISP are all non-conforming implementations. If I enter `foo` into the REPL, it says it's not bound but then performing `(find-symbol "foo")` returns nil which means somewhere between read and eval, the symbol foo was automatically uninterned. – Daniel Smith Apr 27 '20 at 16:05
  • 1
    Try `(find-symbol "FOO")`. Symbols are converted to uppercase by default. – Barmar Apr 27 '20 at 16:28