[plt-scheme] Question about eval and environments

From: Robby Findler (robby at cs.uchicago.edu)
Date: Thu Oct 25 15:04:58 EDT 2007

The in-long answer has to do with compilation. If you can make
assumptions about your program (eg, you can assume that no one refers
to a variable by its name after compilation) then you can do a better
job with the compilation of those variables (ie turn them into static
offsets in a stack or similar). In Scheme you can even tell more. You
know, at compile time, if a variable may be mutated -- just look for a
set!. If you don't find one, you can do even fancier tricks. If, on
the other hand, you could reify the lexical environment as an assoc
list, then you lose that.

Of course, there is always even more to the answer. For example, in
PLT Scheme, we implement debugging support by instrumenting the
program with extra code that will be invoked when the debugger kicks
in. That's how the debugger can report information about the program.
If you just want to be able to change values of variables while
debugging, that's presumably a something that could be done by
inserting assignment expressions into your program, ie the debugger
could support that.

Robby

On 10/25/07, Mike J. Bell <ckimyt at gmail.com> wrote:
> Ok, will do.
>
> How about "in long?"  Why can't eval bind to an environment that encompasses
> the lexical closure I made?
>
> Mike
>
>
> On 10/25/07, Chongkai Zhu <czhu at cs.utah.edu> wrote:
> > Mike J. Bell wrote:
> > > I don't think this is PLT specific, so I apologize if this should be
> > > on a more general scheme list.
> > >
> > > I'm interested in adding a generic hook to evaluate a piece of code at
> > > an arbitrary point in my program.  This would be useful for debugging
> > > purposes.  For instance:
> > >
> > > (let ((x 5))
> > >   ;(do a bunch of stuff with x)
> > >   (write (eval (read))))
> > >
> > > Obviously this isn't very useful outside of a loop or other more
> > > complicated idea, but this is just a toy to show the problem.  When
> > > (read) runs, I would like to type in something like:
> > >
> > > (set! x 7)
> > >
> > > This doesn't work.  The error is that 'x' is unbound.  So I tried:
> > > ...
> > >   (write (eval (read) (interaction-environment))))
> > >
> > > and that didn't work either.  It seems like I can't get to the
> > > environment that contains the lexical closure created by the initial
> > > (let...).
> > >
> > > Is there any way around this?  I supposed I could write my own (mini)
> > > version of eval, but that seems pretty extreme.  How can I get access
> > > to the lexical environment at the point (eval) is executed so I can
> > > inject arbitrary code?
> >
> > In short, you can't. So try some other way to do the debug, say using
> > the debug tool.
> >
> > >
> > > Thanks for any tips!!!
> > >
> > > Mike
> > >
> > > --
> > > Mike J. Bell on gmail
> > >
> ------------------------------------------------------------------------
> > >
> > > _________________________________________________
> > >   For list-related administrative tasks:
> > >   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> > >
> >
> >
>
>
>
> --
> Mike J. Bell on gmail
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
>


Posted on the users mailing list.