bugKawa - Bugs: bug #43285, Require vs. load

 
 

bug #43285: Require vs. load

Submitted by:  Helmut Eller <ellerh>
Submitted on:  Tue 23 Sep 2014 05:49:29 PM UTC  
 
Category: NoneSeverity: 3 - Normal
Item Group: NoneStatus: Invalid
Privacy: PublicAssigned to: Per Bothner <bothner>
Open/Closed: Closed

Add a New Comment(Rich Markup)
   

You are not logged in

Please log in, so followups can be emailed to you.

 

Tue 23 Sep 2014 07:20:21 PM UTC, comment #4:

By "lexical scope of x.scm" I mean file-level or module-level scope, similar to a r7rs library. If you read the r7rs specification section 5.6.1 page 28 you see "After all cond-expand library declarations are expanded, a new environment is constructed for the library consisting of all imported bindings." The module-level scope is equivalent to this environment. Note since this environment is local to the module/library, it can be treated as a purely lexical scope. Think of it as an implicit file-level letrec*, but with the option of exporting a subset of the bindings to another module. This scope is "inside" the traditional top-level scope.

"I would say that foo is defined in the top-level scope (as it's neither inside a module nor inside a lambda)."

It's defined in the module/library scope of y.scm. When you (require "y.scm") (or equivalently use R7RS import) the file y.scm is treated as an implicit module/library.

("module" == "library" in this case. The former is Kawa/R6RS terminology, the latter is R7RS terminology.)

Per Bothner <bothner>
Project AdministratorIn charge of this item.
Tue 23 Sep 2014 06:42:14 PM UTC, comment #3:

I only understand half of your terminology. What is the "lexical scope of x.scm"? I would say that foo is defined in the top-level scope (as it's neither inside a module nor inside a lambda).

Helmut Eller <ellerh>
Tue 23 Sep 2014 06:28:32 PM UTC, comment #2:

"It might be possible to add the exported bindings to the dynamic environment before evaluating the module body"

Note that wouldn't help in this case, since x.scm doesn't re-export the imported binding foo - you'd have to explicitly export it.

Per Bothner <bothner>
Project AdministratorIn charge of this item.
Tue 23 Sep 2014 06:16:49 PM UTC, comment #1:

It's not a bug, as I see it.

When you evaluate x.scm (without using -f or the load function) it is parsed in module-at-a-time mode. The entire file is compiled into a module, which is then loaded. When x.scm requires y.scm, the definition of foo is added to the lexical scope of x.scm, not the dynamic environment. When x.scm is evaluated, it loads z.scm, which looks for foo in the dynamic environment, where it is not.

There is a special hack for the interactive top-level: After evaluating a require, all the of exported bindings are added to the dynamic environment.

It might be possible to add the exported bindings to the dynamic environment before evaluating the module body, though maybe not in all cases, and it's a bit tricky. (See the code in ModuleExp.evalModule2.)

Note if you run x.scm in line-at-a-time mode (e.g by using the -f flag) things work as expected.

Per Bothner <bothner>
Project AdministratorIn charge of this item.
Tue 23 Sep 2014 05:49:29 PM UTC, original submission:

Not sure if this is a bug, but I find this situation surprising.

Suppose we have three files x.scm, y.scm, z.scm with the following
content:

i.e. the file y.scm defines a function foo which is used in file
z.scm; x.scm loads the other two files.

Then Kawa (version 1.14.1 (revision 7749:8062M)) runs into this
problem:

Helmut Eller <ellerh>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach File(s):
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -unavailable- added by bothner (Posted a comment)
  • -unavailable- added by ellerh (Submitted the item)
  •  

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only logged-in users can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

     

     

    Follow 3 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Fri 08 Apr 2016 05:22:35 PM UTCbothnerOpen/ClosedOpen=>Closed
    Tue 23 Sep 2014 06:16:49 PM UTCbothnerStatusNone=>Invalid
      Assigned toNone=>bothner

    Back to the top


    Powered by Savane 3.1-cleanup1