简体   繁体   中英

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?

Package maintainers answered this question under corresponding GitHub issue:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM