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?
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.
If you modify your Haskell source file while GHCi is still running, use the command ":r" or ":reload" to make GHCi reload it.
Prelude is a module that contains a small set of standard definitions and is included automatically into all Haskell modules.
Introduction. GHCi is GHC's interactive environment, in which Haskell expressions can be interactively evaluated and programs can be interpreted.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With