Now that you’ve learned how to create modules that other developers can use, we should look at how to make modules that other developers want to use , by ensuring that our code is well documented. Haddock [8] is the most popular way to document Haskell programs. Haddock generates HTML-formatted documentation from your source code and specially formatted comments. All of the documentation on Hackage [9] is generated using Haddock. In this section, you’ll get a quick introduction to using Haddock to document your modules. We won’t cover all of the functionality of Haddock here, so be sure to read the official documentation to learn more about how you can document your code.
Let’s start by running cabal haddock , generating some documentation on our project in its current state. When you run this command, you’ll see quite a lot of output as haddock tells us about all of the things that are undocumented. In the end, you should see cabal print out a message telling you that it’s generated the documentation at some long path. The specific path will depend on your operating system, user name, and the version of ghc you are using, but it might look something like this example:
| | Documentation created: |
| | /home/user/module-examples/dist-newstyle/build/x86_64-linux/ |
| | ghc-9.2.2/module-examples-0.1.0.0/doc/html/module-examples/index.html |
If you open this path in a web browser, you’ll see a page with a list of all of the modules you’ve added to the project. You can click some of the links and see that Haddock has generated some documentation with the information available. By default, Haddock will generate a file per module, and each file will have an entry for everything that’s exported by the module. Unfortunately, without any comments, the generated documentation isn’t all that useful. Let’s start adding some doc comments to HaskellBook.Examples.Introduction.CreatingModules so that we can get more useful documentation.
We’ll start by adding some module-level documentation at the top of the file:
| | -- | |
| | -- This module serves as an example of how you can create a |
| | -- module. This comment will be placed at the top of the generated |
| | -- documentation. |
| | |
| | {-# LANGUAGE RecordWildCards #-} |
| | module HaskellBook.Examples.Introduction.CreatingModules |
| | ( Name (..) |
| | , Salutation (..) |
| | , GreetingMessage (..) |
| | , defaultMessage |
| | , formatMessage |
| | , testMessage |
| | ) where |
You’ll notice in this example that our module-level documentation looks like a typical Haskell comment, except that we’ve added a line with a pipe character ( | ) at the start of our comment. In general, Haddock comments are normal Haskell comments that start with a pipe character. You can use either single-line comments, like in the example, or multiline comments. Let’s rewrite our documentation with a multiline Haddock comment so you can see how that looks as well:
| | {- | |
| | This module serves as an example of how you can create a module. This |
| | comment will be placed at the top of the generated documentation. |
| | -} |
There’s no benefit to choosing one style of comment over another, and you can pick whichever you prefer.
We can use the same style of comment to add documentation to functions and variables as well. Let’s start by adding some documentation to testMessage :
| | {-| |
| | A test message that you can use to see how messages are formatted. |
| | -} |
| | testMessage :: String |
| | testMessage = |
| | formatMessage $ defaultMessage { greetingFrom = [ Name "test example" ] } |
This comment is okay . It tells the user what to expect, but it would be a lot more useful if they could see what the actual test message is. Let’s add some extra formatting to show the user what they’d expect to see if they typed testMessage into ghci :
| | {-| |
| | A test message that you can use to see how messages are formatted: |
| | |
| | >>> testMessage |
| | "Hello Friend, from: test example" |
| | -} |
The three arrows ( >>> ) tell Haddock to render the next line as though it were typed into a REPL, and to treat the next line as output from the REPL. This lets you show users how to use your code with examples they can try themselves.
You’re not limited to code that might be run in a REPL. You can also include more general code snippets in your documentation using the at-symbol ( @ ) to mark the beginning and end of a code block. We can use this to add some documentation for defaultMessage that makes it more clear what the defaults are. Let’s try this:
| | {- | |
| | A default greeting message that isn't attributed to anyone: |
| | |
| | @ |
| | GreetingMessage |
| | { greetingSalutation = Salutation "Hello" |
| | , greetingTo = Name "Friend" |
| | , greetingFrom = [] |
| | } |
| | @ |
| | -} |
| | defaultMessage :: GreetingMessage |
Unfortunately, if you look at the generated documentation after adding this comment, you’ll notice that it isn’t quite what we’d hoped. The code block that’s generated looks almost right, but the strings "Hello" and "Friend" have lost their quotes and instead become broken links. This is happening because Haddock treats quoted strings as references that it should follow. Specifically, surrounding something with double quotes ( " ) tells Haddock to treat it as a reference to a module. Surrounding something with single quotes ( ’ ) makes it a reference to a function or type. We can insert literal quotes by escaping them with a backslash ( \ ). Let’s take advantage of this ability to link to things by making the type constructor and field selectors in our example link to their definitions in the documentation:
| | {- | |
| | A default greeting message that isn't attributed to anyone: |
| | |
| | @ |
| | 'GreetingMessage' |
| | { 'greetingSalutation' = 'Salutation' \"Hello\" |
| | , 'greetingTo' = 'Name' \"Friend\" |
| | , 'greetingFrom' = [] |
| | } |
| | @ |
| | -} |
| | defaultMessage :: GreetingMessage |
Finally, since our documentation is now pointing users to GreetingMessage , let’s document it as well. We’ll add some documentation for the type, plus documentation for each of the individual fields in the record:
| | {- | |
| | A GreetingMessage contains all of the information needed to generate a |
| | greeting using 'formatMessage'. You can get a default greeting without |
| | attribution from 'defaultMessage'. This makes it convenient to use |
| | record update syntax to construct a new greeting: |
| | |
| | >>> formatMessage defaultMessage { greetingFrom = [ Name "A Haskeller"] } |
| | "Hello Friend, from: A Haskeller" |
| | -} |
| | data GreetingMessage = GreetingMessage |
| | { greetingSalutation :: Salutation |
| | -- ^ A 'Salutation', like \"Hello\" |
| | , greetingTo :: Name |
| | -- ^ 'Name' of the person that should be greeted |
| | , greetingFrom :: [ Name ] |
| | -- ^ 'Name's of the people who are sending the greeting |
| | } |
In this example, you’ll notice that we’ve introduced a new type of comment. We can use a comment starting with a caret symbol ( ^ ) to add comments for specific fields in a record. These comments can appear at the end of the line with the field they are documenting, or immediately after it. If you prefer, you can also use the pipe comment notation, putting a comment before each record field:
| | data GreetingMessage = GreetingMessage |
| | { -- | A 'Salutation', like \"Hello\" |
| | greetingSalutation :: Salutation |
| | , -- | 'Name' of the person that should be greeted |
| | greetingTo :: Name |
| | , -- | 'Name's of the people who are sending the greeting |
| | greetingFrom :: [ Name ] |
| | } |
Knowing these basics, you can start to add documentation to all of the modules you write, making it easier for anyone (including you, in the future) who wants to use your modules to do so without needing to review all of the source code. You can read the official Haddock documentation [10] for a more complete overview of the supported syntax for writing documentation.