Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Haskell syntax to import modules in subdirectories?

Tags:

haskell

hunit

What is Haskell's syntax for importing modules in another directory?

I'm getting started with Haskell and want to practice writing simple functions TDD style with HUnit. I'm having trouble figuring out how to structure my files, though. The example that comes with HUnit seems to be a flat directory structure.

I'd like to have my tests and HUnit code in a different folder than my actual code. I'd appreciate a quick example import statement and a suggestion as to how I might structure my files.

If it matters, I'm using GHCi and NotePad++ to do my coding right now.

like image 486
Josh Earl Avatar asked Feb 17 '11 21:02

Josh Earl


People also ask

What is a qualified import in Haskell?

A qualified import allows using functions with the same name imported from several modules, e.g. map from the Prelude and map from Data.

How do I load a Haskell file?

Open a command window and navigate to the directory where you want to keep your Haskell source files. Run Haskell by typing ghci or ghci MyFile. hs. (The "i" in "GHCi" stands for "interactive", as opposed to compiling and producing an executable file.)


2 Answers

You don't actually do it from the Haskell source code; instead you tell the compiler where to look. The usual method is in the .cabal file. See the cabal user guide for details. You want the "hs-source-dirs" parameter.

Alternatively you can pass the path directly to the compiler. However, Cabal is the better method.

Each pathname in the "hs-source-dirs" parameter specifies a root of a module hierarchy. Basically if you import a module called "Data.Foo.Bar" then the compiler looks for a file with the relative pathname "Data/Foo/Bar.hs" in each directory given by "hs-source-dirs" and imports the first one it finds.

like image 193
Paul Johnson Avatar answered Sep 30 '22 22:09

Paul Johnson


Paul's answer is spot on, but I just wanted to expand on the idea of passing the path directly to the compiler for a quick and easy solution, mainly for running scripts with runhaskell or runghc.

All you need to do is pass the -i flag to the compiler with a colon-delimited list of directories. The compiler will then check those directories for the source files of the imported modules.

So, for example, if you have directory structure like so:

home/ |-- user/     |-- haskell/         |-- Module1.hs         |-- foo/             |-- Module2.hs 

And you would like Module2 to be able to import Module1, then inside Module2.hs add your import statement as usual:

import Module1 

Then when you execute Module2.hs using runhaskell you would run it like so:

$ cd /home/user/haskell/foo $ runhaskell -i/home/user/haskell Module2.hs 

Reference:

  • 4.7.3. The Search Path
like image 42
J.C. Yamokoski Avatar answered Sep 30 '22 21:09

J.C. Yamokoski