Hello, I haven't really been contributing to this thread, so please take my opinion with a grain of salt. But it does appear to me that we should support capturing a lexical environment, as Mark and David describe.
So I took a look at ice-9/eval.scm to see how difficult it would be to implement. Offhand, it doesn't look bad: the eval function there already passes around environment objects, so if it hit this special form, it would simply return its environment object (probably packaged up in a record so it would print nicely). Restarting it is also simple: call eval on an expression with the given environment. The environment objects already contain all of the information needed to evaluate expressions, so I don't think there is very much to do there. The part that seems more interesting to me is that Guile's evaluator attempts to memoize an entire expression before evaluating any of it, which I understand is impossible with Lilypond. I assume Lilypond handles this by bundling the Lilypond code into a string (or some other object), letting the memoizer look at that, and then later doing the actual expansion. David, is this how you handle that? Noah On Tue, Dec 13, 2011 at 2:15 PM, Mark H Weaver <m...@netris.org> wrote: > Andy Wingo <wi...@pobox.com> writes: >> On Tue 13 Dec 2011 18:28, Mark H Weaver <m...@netris.org> writes: >> >>> >> (let ((xxx 2)) >>> >> #{ #(set! xxx (1+ xxx)) #}) >> >>> In the general case, Lilypond needs to _execute_ the outer Scheme code >>> before the parser/evaluator is able to even _see_ the inner Scheme code, >>> because it needs to parse/evaluate the Lily code in between the two, and >>> we've already established that parsing cannot be not be done without >>> runtime information. >> >> What does it mean to execute a `(let ((xxx 2))' ? I think I need to >> read the thread again, because I am really not getting it. > > Well, this example is a bit too simple to illustrate the point. > Let's consider a slightly more complex example: > > (let ((xxx (foobar 1 2))) > #{ #(begin (set! xxx (1+ xxx)) > (let ((yyy (foobar 3 4))) > #{ #(set! yyy (+ xxx yyy)) #} )) #} ) > > In this case, Lilypond would need to start by evaluating: > > (let ((xxx (foobar 1 2))) > (capture-lexical-environment)) > > which entails evaluating (foobar 1 2), extending the lexical environment > with a binding for "xxx", and then returning a new lexical environment > object. > > Then Lilypond would then continue to parse/evaluate the Lilypond code > beginning with #{, which in the general case must be done at the same > time as execution. When it finds the #( it enters Scheme mode again, > so it would then pass the lexical environment object from the previous > step to "local-eval" with the following expression: > > (begin (set! xxx (1+ xxx)) > (let ((yyy (foobar 3 4))) > (capture-lexical-environment))) > > which entails mutating "xxx", evaluating (foobar 3 4) and extending the > lexical environment again (which should now contain both xxx and yyy), > and then returning a new lexical environment object. And so on. > > Does this make sense? > >>> How difficult would it be to implement this? >> >> Dunno. I was thinking that we could have a special form to return a >> list of the identifiers in scope. > > I don't think this is sufficient. The special form must return a > lexical environment object that contains everything needed by a > "local-eval" procedure (which we should also provide) to evaluate > arbitrary scheme code within that lexical environment. > > The key is that we must create the lexical environment object before we > know anything about the code that will later be passed to "local-eval". > > Thanks, > Mark >