<A HREF="manual_c.htm"><img align=center src="contents.gif" ALT="Contents"></A> Up Previous Next

Import chasing +i,-i


Import chasing is a simple, but flexible mechanism for dealing with programs that involve multiple modules. It works in a natural way, using the information in import statements at the beginning of modules, and is particularly useful for large programs, or for programs that use standard Hugs libraries.

For example, consider a module Demo.hs that requires the facilities provided by the STArray library. This dependency might be reflected by including the following import statement at the beginning of Demo.hs:

 import STArray
Now, if we try to load this module into Hugs, then the system will automatically search for the STArray library and load it into Hugs, before Demo.hs is loaded. In fact, the STArray library module also begins with some import statements:
 import ST
 import Array
So, Hugs will actually load the ST and Array libraries first, then the STArray library, and only then will it try to read the rest of Demo.hs:
 Prelude> :load Demo
 Reading file "Demo.hs":
 Reading file "/hugs/libhugs/STArray.hs":
 Reading file "/hugs/libhugs/ST.hs":
 Reading file "/hugs/lib/Array.hs":
 Reading file "/hugs/libhugs/STArray.hs":
 Reading file "Demo.hs":
 Demo>
Initially, the interpreter reads only the first part of any module loaded into the system, upto and including any import statements. Only one module is allowed in each file; files with no module declaration are assumed to declare the Main module. If there are no imports, or if the modules specified as imports have already been loaded, then the system carries on and loads the module as normal. On the other hand, if the module includes import statements for modules that have not already been loaded, then the interpreter postpones the task of reading the current module until all of the specified imports have been successfully loaded. This explains why Demo.hs and STArray.hs are read twice in the example above; first to determine which imports are required, and then to read in the rest of the file once the necessary imports have been loaded.

The list of directories and filenames that Hugs tries in an attempt to locate the source for a module Mod named in an import statement can be specified by:

 [ (dir,"Mod"++suf) | dir <- [d] ++ path ++ [""],
                      suf <- ["", ".hs", ".lhs"]]
The search starts in the directory d where the file containing the import statement was found, then tries each of the directories in the current path (as defined by the -P option), represented here by path, and ends with "", which gives a search relative to the current directory. The fact that the search starts in d is particularly important because it means that you can load a multi-file program into Hugs without having to change to the directory where its source code is located. For example, suppose that /tmp contains the files, A.hs, B.hs, and C.hs, that B imports A, and that C imports B. Now, regardless of the current working directory, you can load the whole program with the command :load /tmp/C; the import in C will be taken as a reference to /tmp/B.hs, while the import in that file will be taken as a reference to /tmp/A.hs.

Import chasing is often very useful, but you should also be aware of its limitations:

Import chasing is usually enabled by default (setting +i), but it can also be disabled using the -i option.