On Sun, Aug 8, 2010 at 02:36, Robert Dodier <robert.dodier at gmail.com> wrote:
> ...Maxima variables have dynamic scope, perhaps not suprising
> since it is based on 60's Lisp implementations which also had
> only dynamic scope (right?). Lexical scope was invented later;
> Scheme and Common Lisp have it.
Well, the part about most 60's implementations is correct, but lexical scope
was *not* invented later:
Church's original lambda calculus (1936) had lexical scope, and both the
original Lisp and Algol 58 (late 1950's) had lexical scope (Algol certainly
supported downward funarg, but I'm not sure about upward.) But later Lisp
implementations dropped lexical in favor of dynamic because dynamic is
easier to implement efficiently (no a-list search in the interpreter, and no
heap-allocated activation blocks in the compiler), and the value of lexical
wasn't understood. Simula 67 and Algol 68 I'm pretty sure supported full
lexical scope with functional values. And of course the Simula version (via
classes) showed *some* of the power of lexical scope well before Scheme --
it is what we now call object-oriented programming.
So there was a long history of lexical scope long before Scheme. The value
of Steele + Sussman's Scheme papers (Scheme the Ultimate X) was to show the
*value* and *semantic power* of lexical scoping in a programming language,
and (eventually) to provide an efficient implementation for both interpreted
and compiled code.
Anyway, in Maxima we have an additional problem beyond constructing
closures. We make not just functions, but also *identifiers* into
first-class objects. So what should the value of lambda([x],'x) be? Do we
want eval(lambda([x],'x)(5)) to evaluate to 5? In that case,
lambda([x],'x)(5) can't equal 'x. Maybe that's OK. How do we print the
value of lambda([x],'x)(5)? Maybe something like x$0134 as you say below.
Do we want that variable to be inputtable? If so, what is its lifetime?
Do all x$... variables live forever (since they could be named dynamically
at any time)? That would mean that we could never garbage collect their
values, which seems problematic. Or maybe they only live as long as there
is a mention of x$... somewhere, making it a kind of soft reference.
> What is considered the "right thing to do" here?
> (i.e. lexical variables escaping from their original context.)
> I'm guessing it was worked out decades ago.
>
As far as I know, this has not been worked out in a Maxima-like context.
The clean and conceptually simple solution renames all the instances of the
bound variable (just as in the original lambda-calculus alpha conversion).
And the clean and conceptually simple representation of sum/integral/etc.
nouns is as functionals, e.g. 'diff(x^2,x) really means diffn(
lambda([x],x^2) ), that is it denotes *the same thing* as diffn(
lambda([y],y^2) ) -- see Sussman and Wisdom. But that means that you can no
longer expect part( 'diff(x^2,x), 2 ) to be 'the' variable x. I suspect
that would confuse our users.
-s