[plt-scheme] Differences in "load" from within Dr. Scheme vs. fromcompiled code?

From: Alex Mitchell (alexm at nus.edu.sg)
Date: Thu May 17 05:59:36 EDT 2007

Hi Matthew,

Thanks for your reply. Yes, this seems to have been exactly what the
problem was. I've solved it by moving my "environment.scm" file into a
library, and using a '(lib ...)' path to require the file.

thanks,
Alex

-----Original Message-----
From: Matthew Flatt [mailto:mflatt at cs.utah.edu] 
Sent: Thursday, May 17, 2007 5:16 AM
To: Alex Mitchell
Cc: plt-scheme at list.cs.brown.edu
Subject: Re: [plt-scheme] Differences in "load" from within Dr. Scheme
vs. fromcompiled code?

The problem is that modules in an executable live in a kind of virtual
filesystem that is separate from the regular filesystem. So, when
"external.scm" requires "environment.scm" when run via the executable,
it gets "environment.scm" from source, instead of the module that's
built into the executable.

To put it another way, imagine that you build the executable in
"~/src". Originally, then, "environment.scm" means
"~/src/environment.scm". Copy the executable and "external.scm" to
another directory, "~/run", and run from there. Now "external.scm" is
trying to load "~/run/environment.scm", which is different from the
module that's in the executable. The point is that it's not obvious
that "~/run/environment.scm" should have anything to do with
"~/src/environment.scm".

With `(lib ...)' paths, the virtual filesystem and real filesystem can
be spliced together, because `(lib ...)' is a kind of absolute
reference. But it doesn't work out with plain and relative paths.

Matthew

At Wed, 16 May 2007 16:22:51 +0800, "Alex Mitchell" wrote:
> I'm having problems accessing binding in the namespace of a module
from
> an externally loaded file. I'm wondering if anyone can point out where
> I'm going wrong.
> 
> I'm loading an external file using (load ...) from within a module.
> Within the external file, I'm calling a function in the module to set
> the value of a variable, something like this:
> 
> In environment.scm:
> 
> (module environment mzscheme
>   (require (lib "file.ss"))
>   
>   ; set a flag
>   (define flag #f)
>   (provide set-flag!)
>   (define (set-flag! in-flag)
>     (display "set-flag! called")
>     (newline)
>     (set! flag in-flag))
>   
>   ; load external file
>   (define (loadit)
>       (load "external.scm")
>       (display "flag: ")
>       (display flag)
>       (newline)
>     )
> 
>   ; start
>   (provide run)
>   (define (run)
>     (loadit)
>     )
> )
> 
> In external.scm:
> 
> (require "environment.scm")
> 
> ; set flag
> (display "before call")
> (newline)
> (set-flag! #t)
> 
> All of this is called from another file, main.scm:
> 
> (module main mzscheme
>   (require "environment.scm")
>   (run)
> )
> 
> This works fine within Dr. Scheme, ie. the flag is set to #t. However,
> when I compile the module and then run the resulting .exe file, the
> value of the variable seems to retain its initial value (its value is
> printed as #f), even though the function call was successful.
> 
> Note that to make this work, I had to require the "environment" module
> within the external file, and had to require the "environment" module
> from another module (main). If I didn't require the "environment"
module
> in external.scm, then the external file couldn't access the set-flag!
> function, and if I just compiled environment.scm to a .exe, then the
> "environment" module was executed twice, presumably once as
> "environment" and once and "environment.scm"... 
> 
> I'm guessing that, for some reason, in the compiled version the
external
> file creates a separate namespace for the module when its loaded. Does
> this sound possible? Anyone know how to avoid this, ie. to make the
> bindings in the module namespace available to the loaded file?
(Perhaps
> I'm going about this completely the wrong way?)
> 
> thanks,
> Alex
> 
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme


Posted on the users mailing list.