In R6RS Scheme, is there a way to get the current environment for use with eval?
Asked Answered
C

2

7

Is there any way in R6RS Scheme to obtain the current environment and then pass it as the second argument to eval?

For example, what should the question marks be for the following expression to return 9?

(let ((x 4)
      (y 5))
  (eval '(+ x y) ???))
Cuss answered 5/7, 2011 at 15:3 Comment(0)
G
7

No, there is no such thing in R6RS. Some rare implementations might support something like that, but in the overwhelming majority (including eval in other languages!) this cannot be done.

The reason for that is simple: it breaks compilation, since it leads to making two functions distinguishable based on local names, and in some cases can also prohibit simple optimizations. For example, if there is something that you can fill in for your ??? then the compiler will need to have two bindings even when they could be optimized away. In those rare cases where it is possible, whatever is used in the ??? (which in some languages is just plain use of eval) triggers a different compilation, one that maps the known bindings to their values. (There's some weird behavior with eval being mentioned literally in JS code in some browsers, which is effectively the same issue.)

Glop answered 5/7, 2011 at 15:22 Comment(2)
@amoe Kernel programming language uses such eval.Osteophyte
@artemonster: no, kernel's eval is very different than JS's.Glop
O
2

Lexical variables are never part of an environment from the perspective of eval. So, there is no way for eval to touch the lexical variables x and y, in your example.

Update: As of Guile 2.0.5, you can use local-eval, which will indeed allow your eval expression to use local (lexical) variables. But like Eli says, most Scheme implementations don't support this.

Orangeman answered 5/7, 2011 at 15:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.