In the R6RS schema, is there a way to get the current environment for use with eval?

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

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

 (let ((x 4) (y 5)) (eval '(+ xy) ???)) 
+6
source share
2 answers

No, there is no such thing in R6RS. Some rare implementations may support something similar, but overwhelmingly (including eval in other languages!) This is not possible.

The reason for this is simple: it interrupts compilation because it causes the two functions to differ based on local names, and in some cases may also prohibit simple optimizations. For example, if there is something that you can fill in for your own ??? , then the compiler will have to have two bindings, even if they can be optimized. In those rare cases, when it is possible, everything that is used in ??? (which eval is just used in some languages) starts another compilation that displays known bindings to their values. (There some strange behavior with eval is mentioned literally in the JS code in some browsers, which is actually the same problem.)

+6
source

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

Update. Starting with Guile 2.0.5, you can use local-eval , which allows your eval expression to use local (lexical) ones. But, as Eli says, most circuit implementations do not support this.

+2
source

Source: https://habr.com/ru/post/892048/


All Articles