Dynamic Binding

James J. Besemer jb at cascade-sys.com
Thu Apr 25 20:36:54 EDT 2002


Paul Foley wrote:

> ? JB:
> ? The original semantics (as formally defined in Lisp itself) called
> for
> ? dynamic binding.  Most original implementations did it that way,
>
> ISTM reading somewhere that the dynamic scoping in early
> implementations was actually the result of an error;

Nah.  There were typos in the specs in the original 1.5 document but
they had nothing to do with dynamic vs. static.

The original, landmark Lisp 1.5 Programmer's Manual clearly and
explicitly defines the language semantics to have dynamic scope.  This
specification appears informally at the end of chapter I and with
greater detail in Appendix B, from which I summarize:

     "Apply" takes the arglist from a lambda form and pairwise
     associates each arg with the corresponding values given in the
     function call.  Then the resulting list of pairs is prepended
     to the "AList," the master symbol table of name-value pairs
     for the interpreter.  Then the expression is passed to "Eval"
     to reduce to a value.  In evaluating atoms, Eval searches the
     AList sequentially for each name and naturally retrieves the
     innermost binding.  Pairs may be put on the AList at the top
     level of the interpreter, resulting in global variables.

Furthermore, I SPECULATE that dynamic scope was motivated in part by the
very concept of lambda binding.  Mathematically and on paper there was
no distinction between static and dynamic.  But in an implementation,
lambdas had to be able to freely nest, with inner bindings taking
precedence over whatever outer bindings there may be in a given
circumstance.  That's how lambda notation worked and it's essentially
the DEFINITION of dynamic binding.  Functions were simply names bound to
a lambda form, so they worked the same way.

So I think it's fair to say that "dynamic scope" originally was an
intrinsic and fundamental part of Lisp.

Paul Foley still:

> Where do you think dynamic scope is called for?

I never intended to put myself into a position to defend dynamic scope.
My only point was that it's how Lisp originally worked.

However, to answer your question, dynamic scope may be appropriate when
simplicity of implementation is of utmost importance.  It's trivial to
implement; you can almost argue that it's elegant.  It gives you the
basics (globals that may be overridden by locals) in a fairly general
fashion.  The fact that callee functions can access caller functions'
variables is a side effect of distinctly dubious utility, though I bet
$1 Larry Wall has an example or two somewhere how to exploit it for some
nefarious purpose.

Naive implementors continually reinvent it precisely because it's easy
and obvious.  Only later do some discover it's weaknesses and sense the
need for something better.  I don't mean to suggest that McCarthy was
naive; 35-40 years ago he was forging the way, exploring virgin
territory.  Naive was Larry Wall inventing Perl decades later, initially
without any local variables, then adding dynamic scope ("local()") and
only some time after that adding a form of truly local variables
("my()").  Ugh.

Reminds me of a variation of a famous saying: Some people see farther
than others because they stand on the TOES of the giants who preceded
them.  ;o)

Tim Peters wrote:

> [James J. Besemer]
> > The original semantics (as formally defined in Lisp itself
> > [McCarthy]) called for dynamic binding.
>
> It seems this would have been a surprise to McCarthy <wink>:
>
>     http://www-formal.stanford.edu/jmc/history/lisp/node4.html
>
> See paragraph "d. Free variables".  When a user bumped into a function
> where
> the difference between dynamic and lexical scoping mattered, and
> lexical
> scoping was what they needed, "I must confess that I regarded this
> difficulty as just a bug".  The understanding of these issues was much
>
> muddier at the time.

I disagree with your interpretation.

Rather he is saying that the implementation DID use dynamic "scoping,"
that it worked just fine in most circumstances and that when people
encountered a situation where "lexical scoping" would help McCarthy
regarded their "difficulty" simply to be a bug.

I presume he means that the bug was in their code.  "It hurts when arg
names conflict with your globals?  Don't DO that."

Tim Peters again:

> Dynamic scoping is attractive
> for implementation reasons; everyone who succumbs eventually regrets
> it...

I think that's a fair statement.

Christopher Browne wrote:

> Most of the time, lexical scope is likely to be more useful.  But
> there are places where dynamic scope is to be preferred.

Agreed.

Regards

--jb

--
James J. Besemer  503-280-0838 voice
http://cascade-sys.com  503-280-0375 fax
mailto:jb at cascade-sys.com



--
James J. Besemer  503-280-0838 voice
http://cascade-sys.com  503-280-0375 fax
mailto:jb at cascade-sys.com







More information about the Python-list mailing list