Reflective capabilities of R7RS Scheme <p>The R7RS report on the programming language Scheme describes two ways of running Scheme code in a Scheme system:</p> <p>1) A scheme system can run a program as described in section 5.1 in the report.</p> <p>2) A scheme system can offer a read-eval-print-loop, in which Scheme code is interpreted interactively.</p> <p>My question is on how these two ways of running Scheme code can be reflected inside a Scheme system with what's included in the R7RS report.</p> <p>There is the eval library procedure <code>eval</code>, which executes Scheme code inside a running Scheme system so <code>eval</code> looks like what I am searching for.</p> <p>However, the only guaranteed mutable environment I can plug in <code>eval</code> is the environment returned by the <code>interaction-environment</code> repl library procedure. With this, however, I cannot reliably simulate a REPL (point 2) from above) because a REPL allows the import form, which the <code>eval</code> procedure does not have to.</p> <p>Also, I cannot use the interaction environment for <code>eval</code>ing a full Scheme program by other reasons: It is generally not empty, in particular it contains all bindings of <code>(scheme base)</code>. </p> <p>For realising 1) inside a running Scheme system, the eval library procedure <code>environment</code> looks promising as it allows to import libraries beforehand (which is part of running programs). However, the environment is immutable, so I cannot evaluate <code>define</code>s inside the environment. A way out would be to wrap the body of the program to be run in a <code>lambda</code> form so that <code>define</code> would define local variables. However, this also doesn't work: Inside a <code>lambda</code> form all defines have to come at the beginning of the body (which is not true for the top-level of a Scheme program) and inside a <code>lambda</code> form library bindings can be lexically overwritten, which is not possible by top-level bindings.</p> <p>As Scheme is Turing-complete, I can, of course, simulate a Scheme system inside a running Scheme system but I am wondering whether it is possible just by using the <code>eval</code> procedure. One reason for my interest is that <code>eval</code> might be optimized (e.g. by a JIT compiler backend), so using this procedure might give near native speed (compared to writing a simple interpreter by hand). </p>