Personal tools

How to write a Haskell program

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
(justifcation for recommending darcs)
(Deleted section. Link to don's old blog was broken and article was missing on Don's new blog.)
(39 intermediate revisions by 19 users not shown)
Line 1: Line 1:
A guide to creating a new Haskell project or program.
+
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 [http://haskell.org/haskellwiki/Tutorials#Introductions_to_Haskell these resources].''
   
 
== Recommended tools ==
 
== Recommended tools ==
Line 10: Line 10:
 
=== Revision control ===
 
=== Revision control ===
   
Use [http://darcs.net Darcs] unless you have a specific reason not to.
+
Use [http://git-scm.com/ git] or [http://darcs.net darcs] unless you have a specific reason not to. Both are lightweight distributed revision control systems (and darcs is written in Haskell). Both have massive market share in the Haskell world. If you want to encourage contributions from other Haskell hackers then git or darcs are the best. Darcs hosting is available on [http://hub.darcs.net/ hub.darcs.net]. For git, [http://github.com/ github] is very popular.
It's much more powerful than most competing systems (and it's written in Haskell). Darcs has massive market share in the Haskell world, if you want to encourage contributions from other Haskell hackers darcs is probably best.
 
   
 
=== Build system ===
 
=== Build system ===
Line 16: Line 16:
 
[[Image:Cabal-With-Text-small.png|frame|Built with Cabal]]
 
[[Image:Cabal-With-Text-small.png|frame|Built with Cabal]]
   
Use [http://haskell.org/cabal Cabal].
+
Use [http://haskell.org/cabal/ Cabal].
You should read at least the start of section 2 of the [http://www.haskell.org/ghc/docs/latest/html/Cabal/index.html Cabal User's Guide].
+
You should read at least the start of section 2 of the [http://www.haskell.org/cabal/users-guide/ Cabal User's Guide].
  +
  +
You should use [http://haskell.org/cabal/download.html cabal-install] as a front-end for installing your Cabal library. Cabal-install provides commands not only for building libraries but also for installing them from, and uploading them to, Hackage. As a bonus, for almost all programs, it's faster than using Setup.hs scripts directly, since no time is wasted compiling the scripts. (This does not apply for programs that use custom Setup.hs scripts, since those need to be compiled even when using cabal-install.)
  +
  +
cabal-install is widely available, as part of the [http://haskell.org/platform Haskell Platform], so you can probably assume your users will have it too.
   
 
=== Documentation ===
 
=== Documentation ===
   
For libraries, use [http://haskell.org/haddock Haddock]. We recommend
+
For libraries, use [http://haskell.org/haddock/ Haddock]. We recommend
using the latest version of haddock.
+
using the version of Haddock that ships with the Haskell Platform. Haddock generates [http://hackage.haskell.org/packages/archive/base/4.3.1.0/doc/html/Prelude.html nice markup], with links to source.
   
 
=== Testing ===
 
=== Testing ===
   
You can use [http://www.md.chalmers.se/~rjmh/QuickCheck/ QuickCheck] or [http://www.mail-archive.com/haskell@haskell.org/msg19215.html SmallCheck] to test pure code. To test impure code, use [http://hunit.sourceforge.net/ HUnit].
+
You can use [http://hackage.haskell.org/package/QuickCheck QuickCheck] or [http://www.mail-archive.com/haskell@haskell.org/msg19215.html SmallCheck] to test pure code. To test impure code, use [http://hackage.haskell.org/cgi-bin/hackage-scripts/package/HUnit HUnit]. See [http://hackage.haskell.org/packages/archive/hashable/1.1.2.2/hashable.cabal this Cabal file] for an example of how to include tests in your Cabal package.
   
To get started, try [[Introduction to QuickCheck]]. For a slightly more advanced introduction, [http://blog.codersbase.com/2006/09/01/simple-unit-testing-in-haskell/ Simple Unit Testing in Haskell] is a blog article about creating a testing framework for QuickCheck using some Template Haskell.
+
To get started, try [[Introduction to QuickCheck]]. For a slightly more advanced introduction, [http://blog.codersbase.com/2006/09/simple-unit-testing-in-haskell.html Simple Unit Testing in Haskell] is a blog article about creating a testing framework for QuickCheck using some Template Haskell. For HUnit, see [[HUnit 1.0 User's Guide]]
   
 
=== Distribution ===
 
=== Distribution ===
   
The new standard mechanism for distributing Haskell libraries and
+
The standard mechanism for distributing Haskell libraries and
 
applications is [http://hackage.haskell.org/packages/hackage.html Hackage]. Hackage can
 
applications is [http://hackage.haskell.org/packages/hackage.html Hackage]. Hackage can
 
host your cabalised tarball releases, and link to any library
 
host your cabalised tarball releases, and link to any library
dependencies your code has.
+
dependencies your code has. Users will find and install your packages via "cabal install", and your package will be integrated into Haskell search engines, like [http://www.haskell.org/hoogle/ hoogle]
  +
  +
=== Target Environment ===
  +
  +
If at all possible, depend on libraries that are provided by the [http://haskell.org/platform Haskell Platform], and libraries that in turn build against the Haskell Platform. This set of libraries is designed to be widely available, so your end users will be able to build your software.
   
 
== Structure of a simple project ==
 
== Structure of a simple project ==
Line 57: Line 57:
 
install it and release.
 
install it and release.
   
The new tool 'mkcabal' automates all this for you, but you should
+
''Note'': The new tool "cabal init" automates all this for you, but you should
 
understand all the parts even so.
 
understand all the parts even so.
   
Line 79: Line 79:
 
$ cat > Haq.hs
 
$ cat > Haq.hs
 
--
 
--
-- Copyright (c) 2006 Don Stewart - http://www.cse.unsw.edu.au/~dons
+
-- Copyright (c) 2006 Don Stewart - http://www.cse.unsw.edu.au/~dons/
 
-- GPL version 2 or later (see http://www.gnu.org/copyleft/gpl.html)
 
-- GPL version 2 or later (see http://www.gnu.org/copyleft/gpl.html)
 
--
 
--
Line 91: Line 91:
 
</haskell>
 
</haskell>
   
=== Stick it in darcs ===
+
=== Stick it in version control ===
   
 
Place the source under revision control (you may need to enter your e-mail address first, to identify you as maintainer of this source):
 
Place the source under revision control (you may need to enter your e-mail address first, to identify you as maintainer of this source):
Line 103: Line 103:
 
hunk ./Haq.hs 1
 
hunk ./Haq.hs 1
 
+--
 
+--
+-- Copyright (c) 2006 Don Stewart - http://www.cse.unsw.edu.au/~dons
+
+-- Copyright (c) 2006 Don Stewart - http://www.cse.unsw.edu.au/~dons/
 
+-- GPL version 2 or later (see http://www.gnu.org/copyleft/gpl.html)
 
+-- GPL version 2 or later (see http://www.gnu.org/copyleft/gpl.html)
 
+--
 
+--
Line 139: Line 139:
 
Author: Don Stewart
 
Author: Don Stewart
 
Maintainer: dons@cse.unsw.edu.au
 
Maintainer: dons@cse.unsw.edu.au
Build-Depends: base
 
 
Build-Type: Simple
 
Build-Type: Simple
  +
Cabal-Version: >=1.2
   
Executable: haq
+
Executable haq
Main-is: Haq.hs
+
Main-is: Haq.hs
ghc-options: -O
+
Build-Depends: base >= 3 && < 5
 
</code>
 
</code>
   
 
(If your package uses other packages, e.g. <tt>haskell98</tt>, you'll need to add them to the <tt>Build-Depends:</tt> field as a comma separated list.)
 
(If your package uses other packages, e.g. <tt>haskell98</tt>, you'll need to add them to the <tt>Build-Depends:</tt> field as a comma separated list.)
Add a <tt>Setup.lhs</tt> that will actually do the building:
+
Add a <tt>Setup.hs</tt> that will actually do the building:
   
 
<haskell>
 
<haskell>
$ cat > Setup.lhs
+
$ cat > Setup.hs
#! /usr/bin/env runhaskell
+
import Distribution.Simple
+
main = defaultMain
> import Distribution.Simple
 
> main = defaultMain
 
 
</haskell>
 
</haskell>
Cabal allows either <tt>Setup.hs</tt> or <tt>Setup.lhs</tt>, but we recommend that you write the setup file this way so that Unix shells can execute it directly.
+
Cabal allows either <tt>Setup.hs</tt> or <tt>Setup.lhs</tt>.
   
 
Now would also be a good time to add a LICENSE file and a README file. Examples are in the tarball for HNop.
 
Now would also be a good time to add a LICENSE file and a README file. Examples are in the tarball for HNop.
Line 162: Line 162:
   
 
<code>
 
<code>
$ darcs add haq.cabal Setup.lhs LICENSE README
+
$ darcs add haq.cabal Setup.hs LICENSE README
 
$ darcs record --all
 
$ darcs record --all
 
What is the patch name? Add a build system
 
What is the patch name? Add a build system
Line 171: Line 171:
 
=== Build your project ===
 
=== Build your project ===
   
Now build it!
+
Now build it! There are two methods of accessing Cabal functionality: through your Setup.hs script or through cabal-install. In most cases, cabal-install is now the preferred method.
  +
  +
Building using cabal-install:
  +
  +
<code>
  +
$ cabal install --prefix=$HOME --user
  +
</code>
  +
  +
Building using the traditional Setup.hs method:
   
 
<code>
 
<code>
$ runhaskell Setup.lhs configure --prefix=$HOME
+
$ runhaskell Setup configure --prefix=$HOME --user
$ runhaskell Setup.lhs build
+
$ runhaskell Setup build
$ runhaskell Setup.lhs install
+
$ runhaskell Setup install
 
</code>
 
</code>
   
Line 199: Line 199:
 
Generate some API documentation into dist/doc/*
 
Generate some API documentation into dist/doc/*
   
  +
Using cabal install:
 
<code>
 
<code>
$ runhaskell Setup.lhs haddock
+
$ cabal haddock
  +
</code>
  +
  +
Traditional method:
  +
<code>
  +
$ runhaskell Setup haddock
 
</code>
 
</code>
   
Line 222: Line 223:
   
 
No output? Make sure you have actually installed haddock. It is a separate program, not something that comes with Cabal. Note that the stylized comment in the source gets picked up by Haddock.
 
No output? Make sure you have actually installed haddock. It is a separate program, not something that comes with Cabal. Note that the stylized comment in the source gets picked up by Haddock.
  +
  +
=== (Optional) Improve your code: HLint ===
  +
  +
[http://hackage.haskell.org/package/hlint HLint] can be a valuable tool for improving your coding style, particularly if you're new to Haskell. Let's run it now.
  +
  +
<code>
  +
$ hlint .
  +
./Haq.hs:11:1: Warning: Eta reduce
  +
Found:
  +
haqify s = "Haq! " ++ s
  +
Why not:
  +
haqify = ("Haq! " ++)
  +
</code>
  +
  +
The existing code will work, but let's follow that suggestion. Open Haq.hs in your favourite editor and change the line:
  +
  +
<haskell>
  +
where haqify s = "Haq! " ++ s
  +
</haskell>
  +
  +
to:
  +
  +
<haskell>
  +
where haqify = ("Haq! " ++)
  +
</haskell>
   
 
=== Add some automated testing: QuickCheck ===
 
=== Add some automated testing: QuickCheck ===
  +
  +
==== QuickCheck v1 ====
   
 
We'll use QuickCheck to specify a simple property of our Haq.hs code. Create a tests module, Tests.hs, with some QuickCheck boilerplate:
 
We'll use QuickCheck to specify a simple property of our Haq.hs code. Create a tests module, Tests.hs, with some QuickCheck boilerplate:
Line 280: Line 308:
   
 
Great!
 
Great!
  +
  +
==== QuickCheck v2 ====
  +
  +
If you're using version 2 of QuickCheck, the code in the previous section needs some minor modifications:
  +
  +
<haskell>
  +
$ cat > Tests.hs
  +
import Char
  +
import List
  +
import Test.QuickCheck
  +
import Text.Printf
  +
  +
main = mapM_ (\(s,a) -> printf "%-25s: " s >> a) tests
  +
  +
-- reversing twice a finite list, is the same as identity
  +
prop_reversereverse s = (reverse . reverse) s == id s
  +
where _ = s :: [Int]
  +
  +
-- Dropping the "Haq! " string is the same as identity
  +
prop_haq s = drop (length "Haq! ") (haqify s) == id s
  +
where haqify s = "Haq! " ++ s
  +
  +
tests = [("reverse.reverse/id", quickCheck prop_reversereverse)
  +
,("drop.haq/id", quickCheck prop_haq)]
  +
</haskell>
  +
  +
To run the test:
  +
  +
<code>
  +
$ runhaskell Tests.hs
  +
reverse.reverse/id : +++ OK, passed 100 tests.
  +
drop.haq/id : +++ OK, passed 100 tests.
  +
</code>
  +
  +
Success!
   
 
=== Running the test suite from darcs ===
 
=== Running the test suite from darcs ===
   
We can arrange for darcs to run the test suite on every commit:
+
We can arrange for darcs to run the test suite on every commit that is run with the flag --test:
   
 
<code>
 
<code>
Line 302: Line 365:
 
<code>
 
<code>
 
$ darcs add Tests.hs
 
$ darcs add Tests.hs
$ darcs record --all
+
$ darcs record --all --test
 
What is the patch name? Add testsuite
 
What is the patch name? Add testsuite
 
Do you want to add a long comment? [yn]n
 
Do you want to add a long comment? [yn]n
Line 313: Line 376:
 
</code>
 
</code>
   
Excellent: now, patches must pass the test suite before they can be committed.
+
Excellent: now, patches must pass the test suite before they can be committed provided the --test flag is passed.
   
 
=== Tag the stable version, create a tarball, and sell it! ===
 
=== Tag the stable version, create a tarball, and sell it! ===
Line 330: Line 393:
 
===== Using Cabal =====
 
===== Using Cabal =====
   
Since the code is cabalised, we can create a tarball with Cabal
+
Since the code is cabalised, we can create a tarball with cabal-install
directly:
+
directly (you can also use <tt>runhaskell Setup.hs sdist</tt>, but you need <tt>tar</tt> on your system [http://thread.gmane.org/gmane.comp.lang.haskell.cafe/60617/focus=60653]):
   
 
<code>
 
<code>
$ runhaskell Setup.lhs sdist
+
$ cabal sdist
 
Building source dist for haq-0.0...
 
Building source dist for haq-0.0...
 
Source tarball created: dist/haq-0.0.tar.gz
 
Source tarball created: dist/haq-0.0.tar.gz
Line 365: Line 428:
 
$ tar xzf haq-0.0.tar.gz
 
$ tar xzf haq-0.0.tar.gz
 
$ cd haq-0.0
 
$ cd haq-0.0
$ runhaskell Setup.lhs configure
+
$ cabal configure
$ runhaskell Setup.lhs build
+
$ cabal build
 
</code>
 
</code>
 
and for packages containing libraries,
 
and for packages containing libraries,
 
<code>
 
<code>
$ runhaskell Setup.lhs haddock
+
$ cabal haddock
 
</code>
 
</code>
   
Line 384: Line 447:
 
$ ls
 
$ ls
 
Haq.hs Tests.hs dist haq.cabal
 
Haq.hs Tests.hs dist haq.cabal
Setup.lhs _darcs haq-0.0.tar.gz
+
Setup.hs _darcs haq-0.0.tar.gz
   
 
== Libraries ==
 
== Libraries ==
Line 417: Line 480:
 
Author: Don Stewart
 
Author: Don Stewart
 
Maintainer: dons@cse.unsw.edu.au
 
Maintainer: dons@cse.unsw.edu.au
Build-Depends: base
+
Build-Type: Simple
Exposed-modules: Data.LTree
+
Cabal-Version: >=1.2
ghc-options: -Wall -O
+
  +
Library
  +
Build-Depends: base >= 3 && < 5
  +
Exposed-modules: Data.LTree
  +
ghc-options: -Wall
   
 
We can thus build our library:
 
We can thus build our library:
   
$ runhaskell Setup.lhs configure --prefix=$HOME
+
$ cabal configure --prefix=$HOME --user
$ runhaskell Setup.lhs build
+
$ cabal build
 
Preprocessing library ltree-0.1...
 
Preprocessing library ltree-0.1...
 
Building ltree-0.1...
 
Building ltree-0.1...
Line 432: Line 495:
 
and our library has been created as a object archive. Now install it:
 
and our library has been created as a object archive. Now install it:
   
$ runhaskell Setup.lhs install
+
$ cabal install
 
Installing: /home/dons/lib/ltree-0.1/ghc-6.6 & /home/dons/bin ltree-0.1...
 
Installing: /home/dons/lib/ltree-0.1/ghc-6.6 & /home/dons/bin ltree-0.1...
 
Registering ltree-0.1...
 
Registering ltree-0.1...
Line 439: Line 502:
 
Writing new package config file... done.
 
Writing new package config file... done.
   
And we're done! You can use your new library from, for example, ghci:
+
And we're done!
  +
To try it out, first make sure that your working directory is anything but the source directory of your library:
  +
  +
$ cd ..
  +
  +
And then use your new library from, for example, ghci:
   
 
$ ghci -package ltree
 
$ ghci -package ltree
Line 457: Line 520:
   
 
You can also set up Cabal to run configure scripts, among other features. For more information consult the
 
You can also set up Cabal to run configure scripts, among other features. For more information consult the
[http://www.haskell.org/ghc/docs/latest/html/Cabal/index.html Cabal documentation].
+
[http://www.haskell.org/cabal/users-guide/ Cabal user guide].
   
 
== Automation ==
 
== Automation ==
   
A tool to automatically populate a new cabal project is available
+
A tool to automatically populate a new cabal project is available:
(beta!):
 
   
darcs get http://code.haskell.org/~dons/code/mkcabal
+
cabal init
   
 
Usage is:
 
Usage is:
   
 
<code>
 
<code>
$ mkcabal
+
$ cabal init
Project name: haq
+
Package name [default "haq"]?
What license ["GPL","LGPL","BSD3","BSD4","PublicDomain","AllRightsReserved"] ["BSD3"]:
+
Package version [default "0.1"]?
What kind of project [Executable,Library] [Executable]:
+
Please choose a license:
Is this your name? - "Don Stewart " [Y/n]:
+
1) GPL
Is this your email address? - "<dons@cse.unsw.edu.au>" [Y/n]:
+
2) GPL-2
Created Setup.lhs and haq.cabal
+
3) GPL-3
$ ls
+
4) LGPL
Haq.hs LICENSE Setup.lhs _darcs dist haq.cabal
+
5) LGPL-2.1
</code>
+
6) LGPL-3
  +
* 7) BSD3
  +
8) BSD4
  +
9) MIT
  +
10) PublicDomain
  +
11) AllRightsReserved
  +
12) OtherLicense
  +
13) Other (specify)
  +
Your choice [default "BSD3"]?
  +
Author name? Henry Laxen
  +
Maintainer email? nadine.and.henry@pobox.com
  +
Project homepage/repo URL? http://somewhere.com/haq/
  +
Project synopsis? A wonderful little module
  +
Project category:
  +
1) Codec
  +
2) Concurrency
  +
3) Control
  +
4) Data
  +
5) Database
  +
6) Development
  +
7) Distribution
  +
8) Game
  +
9) Graphics
  +
10) Language
  +
11) Math
  +
12) Network
  +
13) Sound
  +
14) System
  +
15) Testing
  +
16) Text
  +
17) Web
  +
18) Other (specify)
  +
Your choice? 3
  +
What does the package build:
  +
1) Library
  +
2) Executable
  +
Your choice? 1
  +
Generating LICENSE...
  +
Generating Setup.hs...
  +
Generating haq.cabal...
   
which will fill out some stub Cabal files for the project 'haq'.
+
You may want to edit the .cabal file and add a Description field.
 
To create an entirely new project tree:
 
 
<code>
 
$ mkcabal --init-project
 
Project name: haq
 
What license ["GPL","LGPL","BSD3","BSD4","PublicDomain","AllRightsReserved"] ["BSD3"]:
 
What kind of project [Executable,Library] [Executable]:
 
Is this your name? - "Don Stewart " [Y/n]:
 
Is this your email address? - "<dons@cse.unsw.edu.au>" [Y/n]:
 
Created new project directory: haq
 
$ cd haq
 
$ ls
 
Haq.hs LICENSE README Setup.lhs haq.cabal
 
 
</code>
 
</code>
   
Line 499: Line 562:
   
 
It's important to release your code as stable, tagged tarballs. Don't
 
It's important to release your code as stable, tagged tarballs. Don't
just [http://awayrepl.blogspot.com/2006/11/we-dont-do-releases.html rely on darcs for distribution].
+
just [http://jackunrue.blogspot.com/2006/11/don-do-releases.html rely on darcs for distribution].
   
 
* '''darcs dist''' generates tarballs directly from a darcs repository
 
* '''darcs dist''' generates tarballs directly from a darcs repository
Line 535: Line 598:
 
A Darcs repository can be published simply by making it available from a
 
A Darcs repository can be published simply by making it available from a
 
web page.
 
web page.
  +
  +
There is also a (minimal) Github equivalent for Darcs at [http://hub.darcs.net/ hub.darcs.net].
   
 
== Web page ==
 
== Web page ==
Line 586: Line 651:
   
 
Monad transformers are very useful for programming in the large,
 
Monad transformers are very useful for programming in the large,
encapsulating state, and controlling side effects. To learn more about this approach, try [http://uebb.cs.tu-berlin.de/~magr/pub/Transformers.en.html Monad Transformers Step by Step].
+
encapsulating state, and controlling side effects. To learn more about this approach, try [http://www.grabmueller.de/martin/www/pub/Transformers.en.html Monad Transformers Step by Step].
   
 
== Publicity ==
 
== Publicity ==
Line 611: Line 676:
 
Blog about it! Blog about your new code on [http://planet.haskell.org Planet Haskell].
 
Blog about it! Blog about your new code on [http://planet.haskell.org Planet Haskell].
 
Write about your project in your blog, then email the [http://planet.haskell.org/ Planet Haskell] maintainer (ibid on [[IRC channel|#haskell]]) the RSS feed url for your blog
 
Write about your project in your blog, then email the [http://planet.haskell.org/ Planet Haskell] maintainer (ibid on [[IRC channel|#haskell]]) the RSS feed url for your blog
 
== Example ==
 
 
[http://www.cse.unsw.edu.au/~dons/blog/2006/12/11#release-a-library-today A complete example] of writing, packaging and releasing a new Haskell library under this process has been documented.
 
   
 
[[Category:Community]]
 
[[Category:Community]]

Revision as of 10:20, 9 December 2012

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.

Contents

1 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.

1.1 Revision control

Use git or darcs unless you have a specific reason not to. Both are lightweight distributed revision control systems (and darcs is written in Haskell). Both have massive market share in the Haskell world. If you want to encourage contributions from other Haskell hackers then git or darcs are the best. Darcs hosting is available on hub.darcs.net. For git, github is very popular.

1.2 Build system

(thumbnail)
Built with Cabal

Use Cabal. You should read at least the start of section 2 of the Cabal User's Guide.

You should use cabal-install as a front-end for installing your Cabal library. Cabal-install provides commands not only for building libraries but also for installing them from, and uploading them to, Hackage. As a bonus, for almost all programs, it's faster than using Setup.hs scripts directly, since no time is wasted compiling the scripts. (This does not apply for programs that use custom Setup.hs scripts, since those need to be compiled even when using cabal-install.)

cabal-install is widely available, as part of the Haskell Platform, so you can probably assume your users will have it too.

1.3 Documentation

For libraries, use Haddock. We recommend using the version of Haddock that ships with the Haskell Platform. Haddock generates nice markup, with links to source.

1.4 Testing

You can use QuickCheck or SmallCheck to test pure code. To test impure code, use HUnit. See this Cabal file for an example of how to include tests in your Cabal package.

To get started, try Introduction to QuickCheck. For a slightly more advanced introduction, Simple Unit Testing in Haskell is a blog article about creating a testing framework for QuickCheck using some Template Haskell. For HUnit, see HUnit 1.0 User's Guide

1.5 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 via "cabal install", and your package will be integrated into Haskell search engines, like hoogle

1.6 Target Environment

If at all possible, depend on libraries that are provided by the Haskell Platform, and libraries that in turn build against the Haskell Platform. This set of libraries is designed to be widely available, so your end users will be able to build your software.

2 Structure of a simple project

The basic structure of a new Haskell project can be adopted from HNop, the minimal Haskell project. It consists of the following files, for the mythical project "haq".

  • Haq.hs -- the main haskell source file
  • haq.cabal -- the cabal build description
  • Setup.hs -- build script itself
  • _darcs -- revision control
  • README -- info
  • LICENSE -- license

Of course, you can elaborate on this, with subdirectories and multiple modules. See Structure of a Haskell project for an example of a larger project's directory structure.

Here is a transcript that shows how you'd create a minimal darcs and cabalised Haskell project for the cool new Haskell program "haq", build it, install it and release.

Note: The new tool "cabal init" automates all this for you, but you should understand all the parts even so.

We will now walk through the creation of the infrastructure for a simple Haskell executable. Advice for libraries follows after.

2.1 Create a directory

Create somewhere for the source:

$ mkdir haq
$ cd haq

2.2 Write some Haskell source

Write your program:

$ cat > Haq.hs
--
-- Copyright (c) 2006 Don Stewart - http://www.cse.unsw.edu.au/~dons/
-- GPL version 2 or later (see http://www.gnu.org/copyleft/gpl.html)
--
import System.Environment
 
-- | 'main' runs the main program
main :: IO ()
main = getArgs >>= print . haqify . head
 
haqify s = "Haq! " ++ s

2.3 Stick it in version control

Place the source under revision control (you may need to enter your e-mail address first, to identify you as maintainer of this source):

$ darcs init
$ darcs add Haq.hs 
$ darcs record
addfile ./Haq.hs
Shall I record this change? (1/?)  [ynWsfqadjkc], or ? for help: y
hunk ./Haq.hs 1
+--
+-- Copyright (c) 2006 Don Stewart - http://www.cse.unsw.edu.au/~dons/
+-- GPL version 2 or later (see http://www.gnu.org/copyleft/gpl.html)
+--
+import System.Environment
+
+-- | 'main' runs the main program
+main :: IO ()
+main = getArgs >>= print . haqify . head
+
+haqify s = "Haq! " ++ s
Shall I record this change? (2/?)  [ynWsfqadjkc], or ? for help: y
What is the patch name? Import haq source
Do you want to add a long comment? [yn]n
Finished recording patch 'Import haq source'

And we can see that darcs is now running the show:

$ ls
Haq.hs _darcs

2.4 Add a build system

Create a .cabal file describing how to build your project:

$ cat > haq.cabal
Name:                haq
Version:             0.0
Description:         Super cool mega lambdas
License:             GPL
License-file:        LICENSE
Author:              Don Stewart
Maintainer:          dons@cse.unsw.edu.au
Build-Type:          Simple
Cabal-Version:       >=1.2
Executable haq
  Main-is:           Haq.hs
  Build-Depends:     base >= 3 && < 5

(If your package uses other packages, e.g. haskell98, you'll need to add them to the Build-Depends: field as a comma separated list.) Add a Setup.hs that will actually do the building:

$ cat > Setup.hs
import Distribution.Simple
main = defaultMain

Cabal allows either Setup.hs or Setup.lhs.

Now would also be a good time to add a LICENSE file and a README file. Examples are in the tarball for HNop.

Record your changes:

$ darcs add haq.cabal Setup.hs LICENSE README
$ darcs record --all
What is the patch name? Add a build system
Do you want to add a long comment? [yn]n
Finished recording patch 'Add a build system'

2.5 Build your project

Now build it! There are two methods of accessing Cabal functionality: through your Setup.hs script or through cabal-install. In most cases, cabal-install is now the preferred method.

Building using cabal-install:

$ cabal install --prefix=$HOME --user

Building using the traditional Setup.hs method:

$ runhaskell Setup configure --prefix=$HOME --user
$ runhaskell Setup build
$ runhaskell Setup install

This will install your newly minted haq program in $HOME/bin.

2.6 Run it

And now you can run your cool project:

$ haq me
"Haq! me"

You can also run it in-place, even if you skip the install phase:

$ dist/build/haq/haq you
"Haq! you"

2.7 Build some haddock documentation

Generate some API documentation into dist/doc/*

Using cabal install:

$ cabal haddock

Traditional method: $ runhaskell Setup haddock

which generates files in dist/doc/ including:

$ w3m -dump dist/doc/html/haq/Main.html

haq Contents Index
Main
Synopsis
main :: IO ()
Documentation
main :: IO ()
main runs the main program
Produced by Haddock version 0.7

No output? Make sure you have actually installed haddock. It is a separate program, not something that comes with Cabal. Note that the stylized comment in the source gets picked up by Haddock.

2.8 (Optional) Improve your code: HLint

HLint can be a valuable tool for improving your coding style, particularly if you're new to Haskell. Let's run it now.

$ hlint .
./Haq.hs:11:1: Warning: Eta reduce
Found:
  haqify s = "Haq! " ++ s
Why not:
  haqify = ("Haq! " ++)

The existing code will work, but let's follow that suggestion. Open Haq.hs in your favourite editor and change the line:

    where haqify s = "Haq! " ++ s

to:

    where haqify = ("Haq! " ++)

2.9 Add some automated testing: QuickCheck

2.9.1 QuickCheck v1

We'll use QuickCheck to specify a simple property of our Haq.hs code. Create a tests module, Tests.hs, with some QuickCheck boilerplate:

$ cat > Tests.hs
import Char
import List
import Test.QuickCheck
import Text.Printf
 
main  = mapM_ (\(s,a) -> printf "%-25s: " s >> a) tests
 
instance Arbitrary Char where
    arbitrary     = choose ('\0', '\128')
    coarbitrary c = variant (ord c `rem` 4)

Now let's write a simple property:

$ cat >> Tests.hs 
-- reversing twice a finite list, is the same as identity
prop_reversereverse s = (reverse . reverse) s == id s
    where _ = s :: [Int]
 
-- and add this to the tests list
tests  = [("reverse.reverse/id", test prop_reversereverse)]

We can now run this test, and have QuickCheck generate the test data:

$ runhaskell Tests.hs
reverse.reverse/id       : OK, passed 100 tests.

Let's add a test for the 'haqify' function:

-- Dropping the "Haq! " string is the same as identity
prop_haq s = drop (length "Haq! ") (haqify s) == id s
    where haqify s = "Haq! " ++ s
 
tests  = [("reverse.reverse/id", test prop_reversereverse)
        ,("drop.haq/id",        test prop_haq)]

and let's test that:

$ runhaskell Tests.hs
reverse.reverse/id       : OK, passed 100 tests.
drop.haq/id              : OK, passed 100 tests.

Great!

2.9.2 QuickCheck v2

If you're using version 2 of QuickCheck, the code in the previous section needs some minor modifications:

$ cat > Tests.hs
import Char
import List
import Test.QuickCheck
import Text.Printf
 
main  = mapM_ (\(s,a) -> printf "%-25s: " s >> a) tests
 
-- reversing twice a finite list, is the same as identity
prop_reversereverse s = (reverse . reverse) s == id s
    where _ = s :: [Int]
 
-- Dropping the "Haq! " string is the same as identity
prop_haq s = drop (length "Haq! ") (haqify s) == id s
    where haqify s = "Haq! " ++ s
 
tests  = [("reverse.reverse/id", quickCheck prop_reversereverse)
        ,("drop.haq/id",        quickCheck prop_haq)]

To run the test:

$ runhaskell Tests.hs
reverse.reverse/id       : +++ OK, passed 100 tests.
drop.haq/id              : +++ OK, passed 100 tests.

Success!

2.10 Running the test suite from darcs

We can arrange for darcs to run the test suite on every commit that is run with the flag --test:

$ darcs setpref test "runhaskell Tests.hs"
Changing value of test from  to 'runhaskell Tests.hs'

will run the full set of QuickChecks. If your test requires it, you may need to ensure other things are built too -- for example:darcs setpref test "alex Tokens.x;happy Grammar.y;runhaskell Tests.hs". You will encounter that this way a darcs patch is also accepted if a QuickCheck test fails. You have two choices to work around this:

  • Use
    quickCheck'
    from the package QuickCheck-2 and call
    exitWithFailure
    if it return
    False
    .
  • Keep the test program as it is, and implement the failure on the shell level:
runhaskell Tests.hs | tee test.log && if grep Falsifiable test.log >/dev/null; then exit 1; fi

Let's commit a new patch:

$ darcs add Tests.hs
$ darcs record --all --test
What is the patch name? Add testsuite
Do you want to add a long comment? [yn]n
Running test...
reverse.reverse/id       : OK, passed 100 tests.
drop.haq/id              : OK, passed 100 tests.
Test ran successfully.
Looks like a good patch.
Finished recording patch 'Add testsuite'

Excellent: now, patches must pass the test suite before they can be committed provided the --test flag is passed.

2.11 Tag the stable version, create a tarball, and sell it!

Tag the stable version:

$ darcs tag
What is the version name? 0.0
Finished tagging patch 'TAG 0.0'

2.11.1 Create a tarball

You can do this using either Cabal or darcs, or even an explicit tar command.

2.11.1.1 Using Cabal

Since the code is cabalised, we can create a tarball with cabal-install directly (you can also use runhaskell Setup.hs sdist, but you need tar on your system [1]):

$ cabal sdist
Building source dist for haq-0.0...
Source tarball created: dist/haq-0.0.tar.gz

This has the advantage that Cabal will do a bit more checking, and ensure that the tarball has the structure that HackageDB expects. Note that it does require the LICENSE file to exist. It packages up the files needed to build the project; to include other files (such as Test.hs in the above example, and our README), we need to add:

extra-source-files: Tests.hs README

to the .cabal file to have everything included.

2.11.1.2 Using darcs

Alternatively, you can use darcs:

$ darcs dist -d haq-0.0
Created dist as haq-0.0.tar.gz

And you're all set up!

2.11.2 Check that your source package is complete

Just to make sure everything works, try building the source package in some temporary directory:

$ tar xzf haq-0.0.tar.gz
$ cd haq-0.0
$ cabal configure
$ cabal build

and for packages containing libraries,

$ cabal haddock

2.11.3 Upload your package to Hackage

Whichever of the above methods you've used to create your package, you can upload it to the Hackage package collection via a web interface. You may wish to use the package checking interface there first, and fix things it warns about, before uploading your package.

2.12 Summary

The following files were created:

   $ ls
   Haq.hs           Tests.hs         dist             haq.cabal
   Setup.hs         _darcs           haq-0.0.tar.gz

3 Libraries

The process for creating a Haskell library is almost identical. The differences are as follows, for the hypothetical "ltree" library:

3.1 Hierarchical source

The source should live under a directory path that fits into the existing module layout guide. So we would create the following directory structure, for the module Data.LTree:

   $ mkdir Data
   $ cat > Data/LTree.hs 
   module Data.LTree where

So our Data.LTree module lives in Data/LTree.hs

3.2 The Cabal file

Cabal files for libraries list the publically visible modules, and have no executable section:

   $ cat > ltree.cabal 
   Name:                ltree
   Version:             0.1
   Description:         Lambda tree implementation
   License:             BSD3
   License-file:        LICENSE
   Author:              Don Stewart
   Maintainer:          dons@cse.unsw.edu.au
   Build-Type:          Simple
   Cabal-Version:       >=1.2
   
   Library
     Build-Depends:     base >= 3 && < 5
     Exposed-modules:   Data.LTree
     ghc-options:       -Wall

We can thus build our library:

   $ cabal configure --prefix=$HOME --user
   $ cabal build    
   Preprocessing library ltree-0.1...
   Building ltree-0.1...
   [1 of 1] Compiling Data.LTree       ( Data/LTree.hs, dist/build/Data/LTree.o )
   /usr/bin/ar: creating dist/build/libHSltree-0.1.a

and our library has been created as a object archive. Now install it:

   $ cabal install
   Installing: /home/dons/lib/ltree-0.1/ghc-6.6 & /home/dons/bin ltree-0.1...
   Registering ltree-0.1...
   Reading package info from ".installed-pkg-config" ... done.
   Saving old package config file... done.
   Writing new package config file... done.

And we're done! To try it out, first make sure that your working directory is anything but the source directory of your library:

   $ cd ..

And then use your new library from, for example, ghci:

   $ ghci -package ltree
   Prelude> :m + Data.LTree
   Prelude Data.LTree> 

The new library is in scope, and ready to go.

3.3 More complex build systems

For larger projects, you may want to store source trees in subdirectories. This can be done simply by creating a directory -- for example, "src" -- into which you will put your src tree.

To have Cabal find this code, you add the following line to your Cabal file:

   hs-source-dirs: src

You can also set up Cabal to run configure scripts, among other features. For more information consult the Cabal user guide.

4 Automation

A tool to automatically populate a new cabal project is available:

   cabal init

Usage is:

$ cabal init
Package name [default "haq"]? 
Package version [default "0.1"]? 
Please choose a license:
   1) GPL
   2) GPL-2
   3) GPL-3
   4) LGPL
   5) LGPL-2.1
   6) LGPL-3
 * 7) BSD3
   8) BSD4
   9) MIT
  10) PublicDomain
  11) AllRightsReserved
  12) OtherLicense
  13) Other (specify)
Your choice [default "BSD3"]? 
Author name? Henry Laxen
Maintainer email? nadine.and.henry@pobox.com
Project homepage/repo URL? http://somewhere.com/haq/
Project synopsis? A wonderful little module
Project category:
   1) Codec
   2) Concurrency
   3) Control
   4) Data
   5) Database
   6) Development
   7) Distribution
   8) Game
   9) Graphics
  10) Language
  11) Math
  12) Network
  13) Sound
  14) System
  15) Testing
  16) Text
  17) Web
  18) Other (specify)
Your choice? 3
What does the package build:
   1) Library
   2) Executable
Your choice? 1
Generating LICENSE...
Generating Setup.hs...
Generating haq.cabal...
You may want to edit the .cabal file and add a Description field.

5 Licenses

Code for the common base library package must be BSD licensed. Otherwise, it is entirely up to you as the author. Choose a licence (inspired by this). Check the licences of things you use (both other Haskell packages and C libraries), since these may impose conditions you must follow. Use the same licence as related projects, where possible. The Haskell community is split into 2 camps, roughly: those who release everything under BSD, and (L)GPLers. Some Haskellers recommend avoiding LGPL, due to cross-module optimisation issues. Like many licensing questions, this advice is controversial. Several Haskell projects (wxHaskell, HaXml, etc) use the LGPL with an extra permissive clause which gets round the cross-module optimisation problem.

6 Releases

It's important to release your code as stable, tagged tarballs. Don't just rely on darcs for distribution.

  • darcs dist generates tarballs directly from a darcs repository

For example:

$ cd fps
$ ls       
Data      LICENSE   README    Setup.hs  TODO      _darcs    cbits dist      fps.cabal tests
$ darcs dist -d fps-0.8
Created dist as fps-0.8.tar.gz

You can now just post your fps-0.8.tar.gz

You can also have darcs do the equivalent of 'daily snapshots' for you by using a post-hook.

put the following in _darcs/prefs/defaults:

 apply posthook darcs dist
 apply run-posthook

Advice:

  • Tag each release using darcs tag. For example:
$ darcs tag 0.8
Finished tagging patch 'TAG 0.8'

Then people can darcs pull --partial -t 0.8, to get just the tagged version (and not the entire history).

7 Hosting

Hosting for repos is available from the Haskell community server:

   http://community.haskell.org/

A Darcs repository can be published simply by making it available from a web page.

There is also a (minimal) Github equivalent for Darcs at hub.darcs.net.

8 Web page

Create a web page documenting your project! An easy way to do this is to add a project specific page to the Haskell wiki

9 The user experience

When developing a new Haskell library, it is important to remember how the user expects to be able to build and use a library.

9.1 Introductory information and build guide

A typical library user expects to:

  1. Visit Haskell.org
  2. Find the library/program they are looking for:
    1. if not found, try mailing list;
    2. if it is hidden, try improving the documentation on haskell.org;
    3. if it does not exist, try contributing code and documentation)
  3. Download
  4. Build and install
  5. Enjoy

Each of these steps can pose potential road blocks, and code authors can do a lot to help code users avoid such blocks. Steps 1..2 may be easy enough, and many coders and users are mainly concerned with step 5. Steps 3..4 are the ones that often get in the way. In particular, the following questions should have clear answers:

  • Which is the latest version?
  • What state is it in?
  • What are its aims?
  • Where is the documentation?
  • Which is the right version for given OS and Haskell implementation?
  • How is it packaged, and what tools are needed to get and unpack it?
  • How is it installed, and what tools are needed to install it?
  • How do we handle dependencies?
  • How do we provide/acquire the knowledge and tool-chains needed?

The best place to answer these questions is a README file, distributed with the library or application, and often accompanied with similar text on a more extensive web page.

9.2 Tutorials

Generated haddock documentation is usually not enough to help new programmers learn how to use a library. You must also provide accompanying examples, and even tutorials about the library.

Please consider providing example code for your library or application. The code should be type-correct and well-commented.

10 Program structure

Monad transformers are very useful for programming in the large, encapsulating state, and controlling side effects. To learn more about this approach, try Monad Transformers Step by Step.

11 Publicity

The best code in the world is meaningless if nobody knows about it. The process to follow once you've tagged and released your code is:

11.1 Join the community

If you haven't already, join the community. The best way to do this is to subscribe to at least haskell-cafe@ and haskell@ mailing lists. Joining the #haskell IRC channel is also an excellent idea.

11.2 Announce your project on haskell@

Most important: announce your project releases to the haskell@haskell.org mailing list. Tag your email subject line with "ANNOUNCE: ...". This ensure it will then make it into the Haskell Weekly News. To be doubly sure, you can email the release text to the HWN editor.

11.3 Add your code to the public collections

  • Add your library or application to the Libraries and tools page, under the relevant category, so people can find it.
  • If your release is a Cabal package, add it to the Hackage database (Haskell's CPAN wanna-be).

11.4 Blog about it

Blog about it! Blog about your new code on Planet Haskell. Write about your project in your blog, then email the Planet Haskell maintainer (ibid on #haskell) the RSS feed url for your blog