[E-Lang] Bug Report: Scope Handling Inconsistency

Mark S. Miller markm@caplet.com
Mon, 12 Feb 2001 07:30:17 -0800


In my response to Hal, I wrote:

At 11:27 PM Sunday 2/11/01, Mark S. Miller wrote:

>    ? def x :integer := 33
>    # value: 33
>    
>    ? def func() { println(x) }
>    # value: <func>
>
>    ? func()
>    33
>
>    ? var x :char := 'a'
>    # value: a
>
>    ? x := 'b'
>    # value: b
>
>
>                                            The Strange Case
>    
>    ? func()
>    b
>
>Here we see that "func" has looked up the Slot bound to "x" by the most 
>recent assignment, rather than the most recent prior to its own definition.  
>To get this right, "func" actually captures a reference to the outer scope 
>itself as an instance variable, and actually looks up the names at runtime 
>(by sending messages to this scope object).  There are of course many 
>opportunities for transparent compiler optimization here, but when the outer 
>scope is mutable, not as many opportunities as one might expect.  OTOH, the 
>other scope should only be mutable in an interactive context, which is 
>hardly the most demanding for speed.


All the above is correct, that's not where the bug is.  Having been reminded 
of all this, I just realized that the unimplemented Evaluator abstraction 
http://www.erights.org/javadoc/org/erights/e/elang/evm/Evaluator.html, as 
used in the expansion of the proposed "meta eval" syntax, is broken.  It 
assumes that the incoming scope can be adequately represented by an 
immutable name => Slot mapping.  If one used the "meta eval" I specify on 
this page, we'd get:

    ? def x :integer := 33
    # value: 33
    
    ? meta eval(vanillaEvaluator, def func() { println(x) })
    # value: <func>

    ? meta eval(vanillaEvaluator, func())
    33

    ? var x :char := 'a'
    # value: a

    ? x := 'b'
    # value: b

    ? meta eval(vanillaEvaluator, func())
    33

To repair this, I think we need to add the top level scope as an explicit 
argument to Evaluator's eval message (but not to the "meta eval" syntax).

The scary thing is, without Hal's reminder, if I'd implemented it as I'd 
originally planned, it could be a long time before anyone noticed the 
inconsistency.  Thanks!


        Cheers,
        --MarkM