liveBook · Manning

Counterexample

As explained in Learn Haskell by Example

A counterexample is an input that causes a tested property to fail, demonstrating that the property does not hold universally.

A counterexample is an input that makes a tested property fail. In property-based testing, it demonstrates that the property does not hold universally and gives a concrete starting point for debugging. section 9.3.1

A useful counterexample workflow is:

  • Generate inputs and evaluate the property. section 9.3.1
  • Inspect the failing input to identify the behavior that violates the property. section 9.3.1
  • Shrink the input and recheck it repeatedly until no smaller failing value remains. section 9.4.3
  • Use the smaller, repeatable case to classify and fix the underlying bug or incorrect property. section 9.3.1

A failing rotation property

Testing the symmetry property s == rot13 (rot13 s) finds the counterexample "a" after two tests and one shrink. The reduced input exposes a single-character rotation problem more clearly than a long random string. section 9.3.1

ghci> symbols = upperAlphabet ++ lowerAlphabet ++ digits
ghci> rot13' = map $ (\ch -> if ch `elem` symbols then alphabetRot symbols
   13 ch else ch)
ghci> prop_rot13Symm s = s == rot13' (rot13' s)
ghci> quickCheck prop_rot13Symm
*** Failed! Falsified (after 2 tests and 1 shrink):
"a"

This QuickCheck session shows a property failing with the compact counterexample "a". section 9.3.1

How Learn Haskell by Example covers it

How deep it goes
Defines failure and explains deterministic shrinking down to a minimal falsifying input, including the Arbitrary shrink function.
Examples
Uses a failing rot13 symmetry test, an alphabet-rotation test, and shrink results for characters and associative maps.
What it assumes
Assumes properties and generated test inputs, while introducing shrinking and Arbitrary-driven value reduction.
What you can do after
Diagnose property or implementation bugs with smaller, repeatable failing inputs.
How it connects
Connects property-based testing with QuickCheck, shrinking, and the Arbitrary type class.

Also explained in

Compare how 2 books explain counterexample →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.