Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do I save my Haskell "modules"?

I put some functions in a file. Where on my PC should I save this file so that I can easily load my functions?

I am using the Haskell Platform on a Windows 64-bit computer.

like image 249
Dynamic Avatar asked Oct 28 '11 19:10

Dynamic


2 Answers

I usually put my modules in the same directory tree, and start up ghci at the root directory of the tree. Then modules can import each other, and I can easily :load modules into ghci interactively.

$ ghci

.... loading ....

Prelude> :load directory/subdirectory/mymodule.hs
like image 85
Matt Fenwick Avatar answered Oct 09 '22 12:10

Matt Fenwick


If you want your modules to be accessible from a few different projects, I'd recommend to create a cabal package for them and install it using cabal install. Publishing to hackage is not required - cabal install without arguments looks for .cabal file in the current directory and installs the corresponding package.

If you want your modules to be accessible from a single project - the usual practice of organizing your sources in a hierarchical folder tree applies to Haskell as well. Let me show an example:

Hello/World.hs
Foo/Bar.hs
Quux.hs
Hello.hs

Hello/World.hs should have module Hello.World where in the header. The main module should have module Main, but actual file name can be anything (e.g. Quux.hs). In Foo/Bar.hs you can use import Hello.World. When you load Foo/Bar.hs in ghci, the current directory should be the root of your tree, or else it will not find Hello.World. You can pass module names instead of file names to ghci: e.g. ghci Hello.World will work.

Here are the documentation:

http://haskell.org/ghc/docs/latest/html/users_guide/separate-compilation.html

http://haskell.org/ghc/docs/latest/html/users_guide/packages.html

like image 34
nponeccop Avatar answered Oct 09 '22 13:10

nponeccop