Subject: Re: A question about lexical binding
From: rpw3@rpw3.org (Rob Warnock)
Date: Wed, 15 Oct 2008 08:35:49 -0500
Newsgroups: comp.lang.lisp
Message-ID: <_eKdnVubRvEobGjVnZ2dnUVZ_vzinZ2d@speakeasy.net>
jurgen_defurne  <jurgen.defurne@pandora.be> wrote:
+---------------
| It seems to me that implementing lexical binding rests upon the
| possibility of replacing the dependency for finding something in the
| environment by name, by finding something by number.
+---------------

No, that's not the proper distinction. You most certainly *can* have
[and most simple Scheme interpreters *do* have!] lexical environments
which are searched by name.

The proper distiction you're looking for is that the lexical
environment for a function is "captured" (recorded) at the time
the function is first *created* [which is the time when its
defining lambda expression is evaluated], and it is *that*
environment that is later searched for looking up the values
of variables, *not* the (dynamic) global environment. [Or more
precisely, the lexical environment is searched *before* the
global environment.]

Said another way, every function instance (closure) is a tuple of
a lambda body, a list of the lambda's arguments, and a captured
environment -- the environment that was current when the defining
lambda expression was evaluated, yielding a function (closure).
When the function is later called, the saved environment is
extracted from the function instance (the closure) and is
(temporarily) made the current environment, then new bindings
pairing up the lambda's formal arguments with the function's
actual call-time argument values are made, and added to the
(now-)current environment. The lambda body is then executed,
with the extended environment (the args plus the saved closure
environment). When the function returns its value(s), the current
lexical environment is *abandoned*, and the previous (caller's)
environment is restored as the current now-current environment.

+---------------
| There seem to two ways here to proceed. As shown in SICP, it is
| possible to separate analysis from execution.
+---------------

That is true [and desirable for efficiency], but it is *not* necessary!

+---------------
| What I found critical here in my understanding of lexical binding,
| is that it depends upon compilation, and that it is very difficult
| (maybe impossible ?) to build an interpreter with lexical binding.
| Am I correct here?
+---------------

No, sorry, you are quite incorrect here. It is thoroughly possible
(and both easy & quite common!) to have a "pure" interpreter with
lexical binding.


-Rob

p.s. As mentioned in a parallel reply thread, Christian Queinnec's
"Lisp in Small Pieces" (or "Les Languages Lisp", en Francais, or
the revised 2007 edition "Principes d'implantation de Scheme et Lisp")
is practically required reading for anyone seriously delving into
the internals workings of Scheme or Lisp interpreters/compilers.

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607