Property-based testing for Lean 4.
import Hedgehog
open Hedgehog
def reverseReverse : Property Id := property do
let xs ← forAll (Gen.list (.linear 0 100) Gen.alpha)
xs.reverse.reverse === xs
#eval check reverseReverse "reverse reverse"In Lean a property can be proved, and a proof beats any number of tests. Hedgehog is for where there is no proof, or not yet: it checks the property on generated inputs and shrinks a failure to a small counterexample.
- Integrated shrinking, the idea from Erlang QuickCheck: a generator carries its own shrinks, so there is no shrinker to write and a shrunk value keeps the generator's invariants.
- Systems with state, tested against a model, for the bugs that only emerge from chaos: commands as open types, or transitions as one closed type.
The guide is USAGE. The examples are in Examples/, each with a bug
to find; lake exe examples runs them.
In lakefile.toml, on Lean 4.35.0 or later:
[[require]]
name = "hedgehog"
git = "https://github.com/hedgehogqa/lean-hedgehog"
rev = "v0.2.1"lean-hedgehog was written with strong assistance from AI coding agents, with humans leading the ideas. This would not exist without haskell-hedgehog, written by hand.
- Write you some QuickCheck (2016): notes from learning how such a library works.
- Gens N' Roses: Appetite for Reduction (YOW! 2017): Jacob Stanley on the design.
lean-hedgehog is dedicated to Jacob Stanley, author and co-founder of Hedgehog, who passed away on 9 April 2023.