Haskell/Standalone programs

So you want to build a simple application -- a piece of standalone software -- with Haskell.

The Main moduleEdit

The basic requirement behind this is to have a module Main with an entry point called main and of type IO ().

 -- thingamie.hs
 module Main where
 
 main = putStrLn "Bonjour, world!"

Using GHC, you may compile and run this file as follows:

 $ ghc --make -o bonjourWorld thingamie.hs
 $ ./bonjourWorld
 Bonjour, world!

Voilà! You now have a standalone application built in Haskell.

Other modulesEdit

Invariably your program will grow to be complicated enough that you want to split it across different files. Here is an example of an application which uses two modules.

-- hello.hs
module Hello where
 
hello = "Bonjour, world!"
-- thingamie.hs
module Main where
 
import Hello
 
main = putStrLn hello

We can compile this fancy new program in the same way. Note that the --make flag to ghc is rather handy because it tells ghc to automatically detect dependencies in the files you are compiling. That is, since thingamie.hs imports a module 'Hello', ghc will search the haskell files in the current directory for files that implement Hello and also compile that. If Hello depends on yet other modules, ghc will automatically detect those dependencies as well.

 $ ghc --make -o bonjourWorld thingamie.hs
 $ ./bonjourWorld
 Bonjour, world!

If you want to search in other places for source files, including a nested structure of files and directories, you can add the starting point for the dependency search with the -i flag. This flag takes multiple, colon-separated directory names as its argument.

As a contrived example, the following program has three files all stored in a src/ directory. The directory structure looks like:

HaskellProgram/
   src/
      Main.hs
      GUI/
           Interface.hs
      Functions/
           Mathematics.hs

The Main module imports its dependencies by searching a path analogous to the module name — so that import GUI.Interface would search for GUI/Interface (with the appropriate file extension).

To compile this program from within the HaskellProgram directory, invoke ghc with:

 $ ghc --make -isrc -o sillyprog Main.hs
Last modified on 7 April 2014, at 15:08