Re: A question about lexical binding
- From: Kaz Kylheku <kkylheku@xxxxxxxxx>
- Date: Thu, 16 Oct 2008 00:16:04 +0000 (UTC)
On 2008-10-14, jurgen_defurne <jurgen.defurne@xxxxxxxxxx> wrote:
For a couple of years (well, since I started learning Lisp and Scheme
in my spare time) already I am trying to fathom what the real meaning
of lexical scope is. I know how to use them, I program Perl at day,
and I know the extent of lexical variables, and I can use them to
build closures.
However, I am currently going over SICP, and I am building an
evaluator in Perl, to make the distance between what is being
evaluated and the implementation larger. The meta-circular evaluator
from SICP uses a deep binding environment. I have built such a thing
too for my system.
However, deep binding has the problem that depending upon the caller,
a function can behave different because values are searched for by
name in the environment ? Am I correct ?
The proposed solution is lexical binding. The difficulty I had in
understanding lexical binding is that at first sight it did not seem
different from deep binding.
Well, under the mere faculty of sight, they sometimes may be indistinguishable.
;; dynamic: B refers to A
(defvar x) ; A
(lambda () x) ; B
;; lexical: B refers to A
(let (x) ; A
(lambda () x)) ; B
X refers to X either way, what's the diff, right?
The static view doesn't always reveal what happens when the function is
instantiated more than once. Or what might happen if the definition wasn't
lexically apparent to the reference.
As usual, that which is visible (i.e. syntax) doesn't reveal semantics,
or potential semantics.
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.
In a simple interpreter, lexical binding can be implemented in exactly the same
way as dynamic binding.
Variable references of both kinds (dynamic and lexical) can be implemented
by naively searching the chain of dynamic environment frames, starting from the
innermost nesting and proceeding outward, for a variable until a match is found
by name.
Whether you get lexical or dynamic scope depends on how you terminate the
search and how (or whether) you make closures.
If you terminate the search upon encountering a frame which is marked a a
toplevel frame, you have lexical references; references only ``see'' physically
enclosing definitions. This can be implemented by having an attribute bit in
the environment frame which says ``this frame has no lexical parent; it is a
toplevel frame''. You set this bit whenever you create an evaluation
environment for a toplevel form, or whenever you create an evaluation frame for
a function call.
To have full lexical scope, you need closures also. You achieve these by
retaining the activation chain along with the anonymous function. When that
function is called, that chain is installed as its environment for resolving
variables.
On the other hand, if your variable references do a full search of all of the
frames, not stopping at the top-level bit, then they have indefinite scope
rather than lexical scope. Furthermore, if your lambda operator doesn't retain
a reference to any frames, then you have dynamic extent: when you leave an
evaluation frame, the frame is popped off the stack, and no more references
remain to it anywhere. Dynamic extent plus indefinite scope give you dynamic
scope. An anonymous function has no environment of its own, so it simply
searches that of its caller.
Compilation of lexical environments for better performance is an optimization,
not a necessary implementation strategy.
correct here ? And do I finally understand deep binding, lexical
binding and the difference between them ?
The key is the semantic difference in how these variables behave, and
consequently how you use them, not how they are implemented in some
meta-circular evaluator.
I.e. the way you ``get'' lexical and dynamic scoping is by writing Lisp
programs that appropriately take advantage of lexical and dynamic variables.
.
- Follow-Ups:
- Re: A question about lexical binding
- From: Pascal J. Bourguignon
- Re: A question about lexical binding
- References:
- A question about lexical binding
- From: jurgen_defurne
- A question about lexical binding
- Prev by Date: Re: Textmining with Lisp
- Next by Date: Re: Textmining with Lisp
- Previous by thread: Re: A question about lexical binding
- Next by thread: Re: A question about lexical binding
- Index(es):
Relevant Pages
|