<A target="_top" HREF="manual_contents.html"><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 Array library. This dependency might be reflected by including the following import statement at the beginning of Demo.hs:

 import Array
Now, if we try to load this module into Hugs, then the system will automatically search for the Array library and load it into Hugs, before Demo.hs is loaded. In fact, the Array library module also begins with some import statements:
 import Ix
 import List( (\\) )
So, Hugs will actually load the Ix and List libraries first, then the Array library, and only then will it try to read the rest of Demo.hs:
 Prelude> :load Demo
 Reading file "Demo.hs":
 Reading file "/Hugs/lib/Array.hs":
 Reading file "/Hugs/lib/Ix.hs":
 Reading file "/Hugs/lib/List.hs":
 Reading file "/Hugs/lib/Array.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 Array.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 limitation: import chasing assumes a direct mapping from module names to the names of the files that they are stored in. If A imports B, then the code for B must be in a file called either B, B.hs, or B.lhs, and must be located in one of the directories specified above.

On rare occasions, it is useful to specify a particular pathname as the target for an import statement; Hugs allows string literals to be used as module identifiers for this purpose:

 import "../TypeChecker/Types.hs" as Types
Note, however, that this is a nonstandard feature of Hugs, and that it is not valid Haskell syntax. You should also be aware that Hugs uses the names of files in deciding whether a particular import has already been loaded, so you should avoid situations where a single file is referred to by more than one name. For example, you should not assume that Hugs will be able to determine whether Demo.hs and ./Demo.hs are references to the same file. Import chasing is usually enabled by default (setting +i), but it can also be disabled using the -i option.