Eval in Haskell
oleg@pobox.com
oleg@pobox.com
Sun, 1 Jun 2003 16:16:36 -0700 (PDT)
Tomasz Zielonka wrote:
> I don't know how it works in Python, but in perl the code in eval is
> executed in current lexical scope.
Eval of the kind
let x = 1 in eval "x"
is plainly an abomination. Such an eval all but precludes compilation
because the compiler is not free to alpha-rename lexical bindings any
more and cannot eliminate bindings by inlining. Incidentally, the eval
in Scheme does not (and cannot, in general) use let-bindings. Scheme's
eval may consult previously declared top-level bindings -- but only
given an appropriate flag (whose existence is entirely optional).
Incidentally, restricting eval to top-level or "standard" bindings is
not a significant limitation. It is, in general, a very good practice
to apply eval to closed expressions only. For example,
let x = 1 in (eval "\x->x") x
or
y = 1
main = (eval "let x = " ++ (show y) ++ " in x" ) >>= putStrLn
or (if we need to pass many parameters)
y = 1
z = 2
do
writeFile "Conf.hs" $ makeBindings [["y", show y],["z", show z]];
result <- eval "import Conf;y+z";
putStrLn result
Thus if we restrict all parameters for eval to be in the class
Show/Read, we can implement such an eval right now.
P.S. It has crossed my mind to suggest Scheme as a driver for the
regression tests -- using Ashley Yakeley's HScheme system. I know of
several commercial projects that use Scheme to drive regression tests
for Java projects (Scheme being implemented in Java itself).