Search code examples
haskelltestingautomated-testsproperty-based-testinghaskell-hedgehog

Difference between generating random input through 'Gen' or through 'forAll' in Hedgehog


Suppose, I want to test the following associativity property for Sum with the help of hedgehog library in Haskell:

a <> (b <> c) ≡ (a <> b) <> c

I have actually two ways to generate random input.

1. Generate all in Gen (using Gen's Applicative and Monad instances)

genTriple :: Get (Int, Int, Int)
genTriple = liftA3 (,,) Gen.enumBounded Gen.enumBounded Gen.enumBounded

prop_assoc :: Property
prop_assoc = property $ do
  (a, b, c) <- forAll genTriple
  (Sum a <> Sum b) <> Sum c === Sum a <> (Sum b <> Sum c)

2. Generating each field under forAll

prop_assoc :: Property
prop_assoc = property $ do
  a <- forAll Gen.enumBounded
  b <- forAll Gen.enumBounded
  c <- forAll Gen.enumBounded
  (Sum a <> Sum b) <> Sum c === Sum a <> (Sum b <> Sum c)

I wonder, what is the difference between two approaches? Does it somehow affect performance or parallelization or randomness?


Solution

  • Package maintainers answered this question under corresponding GitHub issue: