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]

Flags

Automatic Flags
NameDescriptionDefault
more-tests

Run tests that pull in specific versions of other packages

Disabled
old-text-tests

Run tests that exhibits bug in old text package. Only works with older version of GHC.

Disabled

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

Downloads

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

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1, 0.1.1, 0.1.1.1, 0.1.1.2, 0.1.2, 0.2, 0.2.0.1, 0.3, 0.4, 0.4.1, 0.4.1.1, 0.4.1.2, 0.4.2, 0.4.2.1, 0.4.2.2, 0.4.2.4, 0.4.3.0, 0.4.4.0, 0.4.5.0, 0.4.6.0, 0.4.6.1, 0.5, 0.5.0.1, 0.5.0.2, 0.5.0.3
Change log ChangeLog.md
Dependencies base (>=4.9 && <4.21), containers, ghc (>=8.0.2 && <9.11), mtl, template-haskell, transformers [details]
License MIT
Copyright 2017 Joachim Breitner
Author Joachim Breitner
Maintainer mail@joachim-breitner.de
Revised Revision 2 made by JoachimBreitner at 2024-07-09T10:13:07Z
Category Testing, Compiler Plugin
Home page https://github.com/nomeata/inspection-testing
Source repo head: git clone git://github.com/nomeata/inspection-testing.git
Uploaded by JoachimBreitner at 2023-12-29T09:33:07Z
Distributions Arch:0.5.0.3, Debian:0.4.2.4, LTSHaskell:0.5.0.3, NixOS:0.5.0.3, Stackage:0.5.0.3
Reverse Dependencies 3 direct, 38 indirect [details]
Downloads 18549 total (161 in the last 30 days)
Rating 2.5 (votes: 4) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2023-12-29 [all 1 reports]

Readme for inspection-testing-0.5.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).

Synopsis

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

{-# LANGUAGE TemplateHaskell #-}
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:
    LHS:
        bad1 :: Int
        bad1 = I# 4#

    RHS:
        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 a a use of certian functions
  • checking the absence of allocation (generally useful)
  • checking the absence of typeclass-overloaded code

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

It does not seem to do anything (on GHC < 8.4)

Add this line to your module:

{-# OPTIONS_GHC -O -fplugin Test.Inspection.Plugin #-}

Can I comment or help?

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