inspection-testing: GHC plugin to do inspection testing

[ compiler-plugin, library, mit, testing ] [ Propose Tags ]

Some carefully crafted libraries make promises to their users beyond functionality and performance.

Examples are: Fusion libraries promise intermediate data structures to be eliminated. Generic programming libraries promise that the generic implementation is identical to the hand-written one. Some libraries may promise allocation-free or branch-free code.

Conventionally, the modus operandi in all these cases is that the library author manually inspects the (intermediate or final) code produced by the compiler. This is not only tedious, but makes it very likely that some change, either in the library itself or the surrounding eco-system, breaks the library’s promised without anyone noticing.

This package provides a disciplined way of specifying such properties, and have them checked by the compiler. This way, this checking can be part of the ususal development cycle and regressions caught early.

See the documentation in Test.Inspection or the project webpage for more examples and more information.

[Skip to Readme]
Versions [faq] 0.1, 0.1.1,,, 0.1.2, 0.2,, 0.3, 0.4, 0.4.1,,, 0.4.2,,
Change log
Dependencies base (>=4.9 && <4.13), containers, ghc (>=8.0.2 && <8.7), mtl, template-haskell, transformers [details]
License MIT
Copyright 2017 Joachim Breitner
Author Joachim Breitner
Revised Revision 1 made by JoachimBreitner at Wed Sep 26 13:31:08 UTC 2018
Category Testing, Compiler Plugin
Home page
Source repo head: git clone git://
Uploaded by JoachimBreitner at Sat Jul 7 13:27:55 UTC 2018
Distributions LTSHaskell:, NixOS:, Stackage:
Downloads 4367 total (604 in the last 30 days)
Rating 2.5 (votes: 3) [estimated by rule of succession]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs available [build log]
Last success reported on 2018-07-07 [all 1 reports]





Run tests that pull in specific versions of other packages


Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info


Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

For package maintainers and hackage trustees

Readme for inspection-testing-0.3

[back to package description]

Inspection Testing for Haskell

This GHC plugin allows you to embed assertions about the intermediate code into your Haskell code, and have them checked by GHC. This is called inspection testing (as it automates what you do when you manually inspect the intermediate code).


See the Test.Inspection module for the documentation, but there really isn't much more to it than:

{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -O -fplugin Test.Inspection.Plugin #-}
module Simple where

import Test.Inspection
import Data.Maybe

lhs, rhs :: (a -> b) -> Maybe a -> Bool
lhs f x = isNothing (fmap f x)
rhs f Nothing = True
rhs f (Just _) = False

inspect $ 'lhs === 'rhs

If you compile this, you will reassurringly read:

$ ghc Simple.hs
[1 of 1] Compiling Simple           ( Simple.hs, Simple.o )
examples/Simple.hs:14:1: lhs === rhs passed.
inspection testing successful
      expected successes: 1

See the examples/ directory for more examples of working proofs.

If an assertion fails, for example

bad1, bad2 :: Int
bad1 = 2 + 2
bad2 = 5

inspect $ 'bad1 === 'bad2

then the compiler will tell you so, and abort the compilation:

$ ghc Simple.hs -dsuppress-idinfo
[5 of 5] Compiling Simple           ( examples/Simple.hs, examples/Simple.o )
examples/Simple.hs:14:1: lhs === rhs passed.
examples/Simple.hs:20:1: bad1 === bad2 failed:
        bad1 :: Int
        bad1 = I# 4#

        bad2 :: Int
        bad2 = I# 5#

examples/Simple.hs: error:
    inspection testing unsuccessful
          expected successes: 1
         unexpected failures: 1

What can I check for?

Currently, inspection-testing supports

  • checking two definitions to be equal (useful in the context of generic programming)
  • checking the absence of a certain type (useful in the context of list or stream fusion)
  • checking the absence of allocation (generally useful)

In general, the checks need to be placed in the same module as the checked-definition.

Possible further applications includes

  • checking that all recursive functions are (efficiently called) join-points
  • asserting strictness properties (e.g. in Data.Map.Strict)
  • peforming some of these checks only within recursive loops

Let me know if you need any of these, or have further ideas.

Help, I am drowning in Core!

inspection-testing prints the Core more or less like GHC would, and the same flags can be used to control the level of detail. In particular, you might want to pass to GHC a selection of the following flags:

-dsuppress-idinfo -dsuppress-coercions -dsuppress-type-applications
-dsuppress-module-prefixes -dsuppress-type-signatures -dsuppress-uniques

Can I comment or help?

Sure! We can use the GitHub issue tracker for discussions, and obviously contributions are welcome.