[Haskell-beginners] generating arbitrary parameter lists

Amy de Buitléir amy at nualeargais.ie
Thu Sep 23 17:56:39 EDT 2010


Whenever I want to use quickCheck to test some function with multiple
input parameters, I end up writing boilerplate code that looks like
the example below. I have a feeling I'm missing out on some good
Haskell tricks. In particular, writing a function like arbXYZ for
every combination of parameter types that I need to test is a bit
repetitive. Is there a better way?

Thank you in advance,
Amy

----- code example -----

functionIWantToTest :: Double -> Int -> Bool -> Double
functionIWantToTest x y z = 27  -- pretend we're doing something useful

arbXYZ :: Gen (Double, Int, Bool)
arbXYZ = do
  x <- choose (0.0, 1.0)
  y <- choose (1, 10)
  z <- arbitrary
  return (x, y, z)

conditionIWantToCheck :: (Double, Int, Bool) -> Bool
conditionIWantToCheck (x, y, z) = True -- pretend we're checking something

prop_i_want_to_check :: Property
prop_i_want_to_check = forAll arbXYZ conditionIWantToCheck


More information about the Beginners mailing list