This is the mail archive of the
kawa@sourceware.org
mailing list for the Kawa project.
Re: difference between using eval from the interactive prompt and from a file?
- From: Per Bothner <per at bothner dot com>
- To: alex mitchell <lexaay at gmail dot com>
- Cc: "kawa at sourceware dot org" <kawa at sourceware dot org>
- Date: Fri, 11 Nov 2011 09:55:12 -0800
- Subject: Re: difference between using eval from the interactive prompt and from a file?
- References: <CAE2A29A.255AF%lexaay@gmail.com>
On 11/10/2011 05:59 PM, alex mitchell wrote:
I know that eval is generally discouraged, but I've been looking at using
it for what (I think) is a valid reason. However, I've immediately run
into a problem.
I notice that this code:
(define (myproc)
(display "myproc\n"))
(if (environment-bound? (interaction-environment) 'myproc)
(eval '(myproc))
(display "not bound\n"))
gives the result "myproc" if I type the code into the kawa prompt
interactively, but gives the result "not bound" if I save it in a file,
say "evaltest.scm", and then run "kawa evaltest.scm". Is there a reason
why, when running from a file, eval can't see myproc? I'm guessing it has
something to do with the way the file is compiled, and when top-level
definitions are evaluated. Anyone have any insight or suggestions?
It works if you do:
kawa -f evaltest.scm
The -f flag means to read and evaluate the file line-by-line (or
command-by-command), similar to the way the load works.
If you just say kawa evaltest.scm it will compile the file as a single
module, and then evaluate it. This causes different behavior in cases
like this. The reason is that evaluating a module should not IMO implicitly
change the global environment - that contradicts the purpose of a module
system, which is to separate the namespaces. Instead, when you load a
module into a REPL, the module's exported bindings are added to the REPL
*after* the load completes, not during. See the evalModule2 method in
gnu/expr/ModuleExp.java.
This behavior is not ideal. However, it does have some advantages.
For example compiling a file and then saying "kawa evaltest"
behaves compatibly, which I think is desirable.
On 11/11/2011 02:02 AM, Helmut Eller wrote:
R7RS section 5.1 Programs:
Expressions occurring at the top level of a program are
interpreted imperatively; they are executed in order when
the program is invoked or loaded, and typically perform
some kind of initialization.
So I think this is one more case where Kawa is not standard conform.
Ah, but when you say "kawa evaltest.scm" evaltest.scm is treated as a
module, not a program. If you want evaltest.scm to be treated as a
R7RS "program" use the -f flag. (Of course most of the time it
doesn't matter - in Kawa certain modules are often also programs,
or at least behave the same way when loaded.)
So this is not an example of Kawa non-conformance.
--
--Per Bothner
per@bothner.com http://per.bothner.com/