Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell / GHCi - loading modules from different directories

My haskell application has the following directory structure:

src/     utils/Utils.hs     subsystem/Subsystem.hs 

The Subsystem module imports Utils module. I would like to hand test this code in GHCi.

The problem is GHCi seems to be only looking for modules available in '.' (current directory), so I copied Utils.hs to subsystem folder and was able to hand-test Subsytem.hs. Is there a better way to do this? For example I would like to start GHCi in the src directory and let it search for modules in ./utils and ./subsystem directories. Can I specify a module path to GHCi?

like image 288
simon Avatar asked Jul 07 '11 05:07

simon


People also ask

How do I import modules into Haskell?

The syntax for importing modules in a Haskell script is import <module name>. This must be done before defining any functions, so imports are usually done at the top of the file. One script can, of course, import several modules. Just put each import statement into a separate line.

How do I load a Haskell file into GHCi?

If you modify your Haskell source file while GHCi is still running, use the command ":r" or ":reload" to make GHCi reload it.

What is Prelude in GHCi?

Prelude is a module that contains a small set of standard definitions and is included automatically into all Haskell modules.

What does GHCi mean in Haskell?

Introduction. GHCi is GHC's interactive environment, in which Haskell expressions can be interactively evaluated and programs can be interpreted.


2 Answers

You can tell GHCi where to search for modules by using the -i option:

ghci Foo.Bar -isrc 

This will load src/Foo/Bar.hs into GHCi. This way, you can also specify two different directories like this:

ghci Bar.hs -i.:config  

It will look for the dependencies in ./ and ./config/ .

See the GHC user's guide for more information about the module search path.

like image 167
hammar Avatar answered Nov 10 '22 11:11

hammar


By default, when GHC looks for modules, it interprets Foo.Bar as Foo/Bar.hs. So if you have a single project, you could have a module Utils as Utils.hs in the top-level directory, and a module Utils.Fishcakes as Utils/Fishcakes.hs. Note that Utils.hs can exist alongside a directory named Utils, or both can exist independently. A common style tends to be using the top-level module to simply re-export things from modules below it in the hierarchy, but this isn't required. The GHC User Guide covers the above behavior, as well as describing what other options are supported.

As far as I know, in most cases code will either use the above default structure, will use some other structure specified as part of a cabal build, or will expect to be installed as a library.

like image 35
C. A. McCann Avatar answered Nov 10 '22 10:11

C. A. McCann