Haskell program coverage

From HaskellWiki
Revision as of 09:18, 15 July 2009 by Dominic (talk | contribs)

Jump to: navigation, search

What is hpc?

Hpc is a tool-kit to record and display Haskell program coverage. Hpc includes tools that instrument Haskell programs to record program coverage, run instrumented programs, and display the coverage information obtained.

Hpc works by applying a source-to-source transformation; this transformation also generates as a by-product a program-index file (.pix) and module-index files (.mix). The transformed program is compiled with a library; in addition to its usual run-time behaviour the program generates a coverage record in a program-ticks file (.tix). If the program is run more than once, coverage data is accumulated to reflect all runs.

Hpc provides coverage information of two kinds: source coverage and boolean-control coverage. Source coverage is the extent to which every part of the program was used, measured at three different levels: declarations (both top-level and local), alternatives (among several equations or case branches) and expressions (at every level). Boolean coverage is the extent to which each of the values True and False is obtained in every syntactic boolean context (ie. guard, condition, qualifier).

Hpc displays both kinds of information in two different ways: textual reports with summary statistics (hpc-report) and sources with colour mark-up (hpc-source).


This version of hpc is available under a BSD-style license for free use by all sectors of the Haskell community. The hpc-trans tool was based on components from the nhc98 compiler or from the hat tracing system, and we gladly acknowledge the contribution of the original authors.

The latest version is version 0.4 and can be found at:



Example textual output from hpc-report

-----<module Main>-----

 67% expressions used (72/106)
 14% boolean coverage (1/7)
     16% guards (1/6), 2 always True, 2 always False, 1 unevaluated
      0% 'if' conditions (0/1), 1 always True
    100% qualifiers (0/0)
 42% alternatives used (3/7)
 88% local declarations used (8/9)
 80% top-level declarations used (4/5)
unused declarations:

Example of HTML output from hpc-markup


The HTML output highlights parts of the program never evaluated; it also highlights boolean conditions for which recorded evaluations are always True or always False.

Example of HTML Summary from hpc-markup

This is an example of the table that provides the summary of coverage, with links the the individually marked-up files.

moduleTop Level DefinitionsAlternativesExpressions
%covered / total%covered / total%covered / total
  module CSG 100 %0/0
100 %0/0
100 %0/0
  module Construct 48 %17/35
52 %25/48
60 %381/635
  module Data 24 %6/25
13 %11/81
39 %254/646
  module Eval 70 %22/31
60 %65/108
57 %361/628
  module Geometry 75 %42/56
69 %45/65
70 %300/427
  module Illumination 61 %11/18
49 %46/93
46 %279/600
  module Intersections 63 %14/22
38 %83/213
38 %382/1001
  module Interval 47 %8/17
41 %16/39
41 %69/165
  module Main 100 %1/1
100 %1/1
100 %6/6
  module Misc 0 %0/1
0 %0/1
0 %0/10
  module Parse 80 %16/20
68 %26/38
72 %192/264
  module Primitives 16 %1/6
16 %1/6
20 %5/24
  module Surface 36 %4/11
24 %13/53
18 %43/231

Hpc toolkit

The Hpc Toolkit has three parts

  • A set of tools for instrumenting Haskell, and interpreting the results on a coverage run.
  • A set of scripts to make using these tools easier.
  • An open common file format, uses by all the tools.

Hpc tools

There are currently three tools provided by hpc, as well as a new option for GHC 6.7.


  • hpc-trans - translates Haskell into instrumented Haskell
  • hpc-report - Read the output of a coverage run, report a summary
  • hpc-markup - Read the output of a coverage run, markup the source

Compiler Options

  • -fhpc - a new GHC option, that automatically instruments Haskell programs on the fly, using a similar algorithm to hpc-trans.

Hpc scripts

  • hpc-build
  • hpc-run

Hpc file formats

There are two file formats used by Hpc externally, and one internally by hpc-trans.

  • tix - Coverage information generated by the execution of an instrumented program.
  • mix - Information about each tick-box inside a module, including style of tick box, and location in the source.
  • pix - used only by hpc-trans, stores a list

Hpc quirks

Hpc (at least with 6.10.1) is not happy with literate haskell or with cpp. Even if your file contains no cpp, hpc will give spurious results. One way round both of these problems is to pre-process the source by hand with something like

ghc -E -optP-P -cpp Foo.hs

and then remove the line

{-# LINE 1 "Foo.hs" #-}

We hope you find this tool-kit useful. If you have any comments or feedback, please feel free to email us.

Andy Gill (andy@galois.com) Colin Runciman (colin@cs.york.ac.uk)