Eval in Haskell

Lauri Alanko la@iki.fi
Mon, 2 Jun 2003 02:52:55 +0300

[Moving to cafe, this is only barely Haskell-related.]

On Sun, Jun 01, 2003 at 04:16:36PM -0700, oleg@pobox.com wrote:
> Eval of the kind
> 	let x = 1 in eval "x"
> is plainly an abomination.

I agree, though not because of the optimization issues but rather
as a matter of principle: a closed variable should be _closed_, it
should not be visible to anything but the body of the let-expression
that binds it. And an externally acquired eval-able expression is
definitely "anything but".

Nevertheless, this abomination is supported even by some scheme

guile> (define local-environment (procedure->syntax (lambda (exp env) env)))
guile> (define (eval-where-x-bound exp)
... (let ((x 'foo)) (local-eval exp (local-environment))))      
guile> (eval-where-x-bound '(list 'x x))
(x foo)

> 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.

This depends entirely on what you want to achive with eval, so I don't
think the "in general" is justified. If you just want to evaluate closed
expressions whose results are printable and readable, then you really
just have an embedded interpreter which happens to read the same
language as your source code.

On the other hand, it is common for perl programs and especially shell
scripts to eval configuration files with the explicit purpose that the
configuration file may alter variables which are bound in the main
program. For such usage, it is essential that the main program and the
evaled file access the same enviroment.

(Naturally a configuration file shouldn't be allowed to mess with
_everything_ in the main program, but I don't think perl allows detailed
adjustment of the bindings in the environment. Some schemes do, though.)

Lauri Alanko