I am trying to understand how the Scheme meta-circular evaluator handles quoted expressions differently than symbolic data.
The accepted answer Stack Overflow question What exactly is a symbol in lisp/scheme? defines the "symbol" data object in Scheme:
In Scheme and Racket, a symbol is like an immutable string that happens to be interned
The accepted answer writes that in Scheme, there is a built-in correspondence between identifiers and symbols:
To call a method, you look up the symbol that corresponds to the method name. Lisp/Scheme/Racket makes that really easy, because the language already has a built-in correspondence between identifiers (part of the language's syntax) and symbols (values in the language).
To understand the correspondance, I read the page "A Note on Identifiers" in An Introduction to Scheme and Its Implementation, which says
Scheme identifiers (variable names and special form names and keywords) have almost the same restrictions as Scheme symbol object character sequences, and it's no coincidence. Most implementations of Scheme happen to be written in Scheme, and symbol objects are used in the interpreter or compiler to represent variable names.
Based on the above, I'm wondering if my understanding of what is happening in the following session is correct:
user@host:/home/user $ scheme
MIT/GNU Scheme running under GNU/Linux
Type `^C' (control-C) followed by `H' to obtain information about interrupts.
Copyright (C) 2011 Massachusetts Institute of Technology
This is free software; see the source for copying conditions. There is NO warranty; not even for
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Image saved on Sunday February 7, 2016 at 10:35:34 AM
Release 9.1.1 || Microcode 15.3 || Runtime 15.7 || SF 4.41 || LIAR/x86-64 4.118 || Edwin 3.116
1 ]=> (define a (lambda (i) (+ i 1)))
;Value: a
1 ]=> a
;Value 13: #[compound-procedure 13 a]
1 ]=> (quote a)
;Value: a
1 ]=> (eval a (the-environment))
;Value 13: #[compound-procedure 13 a]
1 ]=> (eval (quote a) (the-environment))
;Value 13: #[compound-procedure 13 a]
1 ]=>
The first
define
statement is a special form captured by the evaluator, which creates a binding for the symbola
to a compound procedure object in the global environment.Writing
a
in the top-level causes the evaluator to receive the symbol object'a
, which evaluates to the compound-procedure object that'a
points to in the global environment.Writing
(quote a)
in the top-level causes the evaluator to receive a list of symbols('quote 'a))
; this expression is a special form captured by the evaluator, which evaluates to the quoted expression, namely the symbol object'a
.Writing
(eval a (the-environment))
causes the evaluator to receive a list of symbols('eval 'a ...)
(ignoring the environment). The evaluator performs a lookup for'eval
, which yields the eval compiled procedure object, a lookup for'a
, which yields the compound-procedure. Finally, the top-level evaluator applies the eval procedure to its arguments, since a compound-procedure is self-evaluating (not true in Scheme48), the final value of the expression is the compound-procedure itself.Writing
(eval (quote a) (the-environment))
causes the evaluator to receive a list of symbols('eval ('quote 'a) ...)
. The evaluator performs a lookup for'eval
, which yields the eval compiled procedure object. It evaluates the expression('quote 'a)
which yields the symbol object'a
. Finally, the top-level evaluator applies the eval procedure to'a
, which is a symbol object and therefore invokes an environment lookup that yields the compound procedure.
Does this explanation correctly describe (at a high level) how a Scheme interpreter might differentiate between symbol objects and identifiers in the language? Are there fundamental misunderstandings in these descriptions?
1 ]=>
. Perhaps I should say, "how a scheme interpreter might treat identifiers at the top-level, using symbols internally? – Palaeographyread
function to scan the text and produce a data structure. Then interpret purely that data structure. Only if you write a Scheme implementation from scratch do you deal with that yourself; but even then, you separate the reading from the interpretation in the same way. – Gripper