How to write a Haskell program

From HaskellWiki

(If you can help update this page, please do!)


A developers' guide to creating a new Haskell project or program, and working in the Haskell developer ecosystem.

Note: for learning the Haskell language itself we recommend these resources.

Recommended tools

Almost all new Haskell projects use the following tools. Each is intrinsically useful, but using a set of common tools also helps everyone by increasing productivity, and you're more likely to get patches.

Revision control

Use git or darcs unless you have a specific reason not to. Both are lightweight distributed revision control system. Darcs is written in Haskell. They are the two most popular DVCSes in the Haskell world. If you want to encourage contributions from other Haskell hackers then git is best. For git, GitHub is very popular. Darcs hosting is available on hub.darcs.net.

Toolchain

Built with Cabal

Haskell's build system is called Cabal. You should read at least "How to package Haskell code".

Use GHCup to provision your Haskell toolchain with cabal-install, GHC and the Haskell Language Server.

Documentation

For libraries, use Haddock. Haddock generates HTML documentation with links to source. You can it with cabal haddock --haddock-hyperlink-sources. Use the --open to have open the generated documentation in your default web browser.

Testing

The Tasty test framework is recommended due to its versatility and plugins. You can write several kind of tests with it:

Doctests are supported with the doctest package, allowing you to write executable comments for your functions.

To get started with property testing, take a look at Introduction to QuickCheck and The Design and Use of QuickCheck.

Distribution

The standard mechanism for distributing Haskell libraries and applications is Hackage. Hackage can host your cabalised tarball releases, and link to any library dependencies your code has. Users will find and install your packages by adding them to their cabal file, and your package will be integrated into Haskell search engines, like Hoogle and Flora

Target Environment

If possible, depend on libraries that are provided by the current stable Stackage package versions. Ideally your package should be able to build with users that keep current with Stackage.

Create and Build your project

You can create a project with cabal init. Its interactive wizard will create the structure of the project for you.

Now build it! With the Haskell toolchain provided by GHCup, you can run cabal build

If you project contains an executable, you can use cabal run and your program will run.