QuickCheck:为什么没有通过测试的函数以及使用什么来代替?

Dam*_*les 7 haskell quickcheck haskell-hedgehog

为什么没有QuickCheck类似于hedgehog's的函数success?我特别想知道如何翻译如下属性:

prop_specialPair :: Property
prop_specialPair = property $ do
  (_, xs) <- forAll specialPair
  case xs of
    x:_ -> x /== 3
    _   -> success

Run Code Online (Sandbox Code Playgroud)

QuickCheck如果我使用=/=,然后我被迫返回类型的东西Property,而且似乎有没有固定函数返回一个通过性。

所以我要么不得不求助于Bool类型:

prop_specialPair :: SpecialPair -> Bool
prop_specialPair SpecialPair { xs } =
  case xs of
    x:_ -> x == 3
    _   -> True
Run Code Online (Sandbox Code Playgroud)

或者对 使用非常尴尬的编码success,例如:

prop_specialPair :: SpecialPair -> Property
prop_specialPair SpecialPair { xs } =
  case xs of
    x:_ -> x =/= 3
    _   -> True === True
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来表达上面的属性QuickCheck

Fyo*_*kin 8

您可以使用类中的property函数Testable和各种Testable实例来创建Property具有您需要的任何特征的函数。

例如,property ()总是成功。再举一个例子,property (b :: Bool)成功 iff b == True

所以你可以这样做,例如:

prop_specialPair :: SpecialPair -> Property
prop_specialPair SpecialPair { xs } =
  case xs of
    x:_ -> x =/= 3
    _   -> property ()
Run Code Online (Sandbox Code Playgroud)

或者您可以通过使用Testable Result实例和值使其更加明确succeeded :: Result

prop_specialPair :: SpecialPair -> Property
prop_specialPair SpecialPair { xs } =
  case xs of
    x:_ -> x =/= 3
    _   -> property succeeded
Run Code Online (Sandbox Code Playgroud)