Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 如何在main中使用quickcheck_Haskell_Quickcheck - Fatal编程技术网

Haskell 如何在main中使用quickcheck

Haskell 如何在main中使用quickcheck,haskell,quickcheck,Haskell,Quickcheck,我正在为我编写的二进制搜索函数编写一个测试 module Tests where import Data.List (sort) import Test.QuickCheck import BinarySearch (binarySearch) prop_equals_elem x xs = (binarySearch x $ sort xs) == (x `elem` xs) args = Args {replay = Nothing, maxSuccess = 200, maxDisc

我正在为我编写的二进制搜索函数编写一个测试

module Tests where

import Data.List (sort)
import Test.QuickCheck
import BinarySearch (binarySearch)

prop_equals_elem x xs = (binarySearch x $ sort xs) == (x `elem` xs)

args = Args {replay = Nothing, maxSuccess = 200, maxDiscard=200, maxSize=200, chatty = False}

main = do
    quickCheck (prop_equals_elem :: (Ord a) => a -> [a] -> Bool)
在ghci中使用quickCheck可以很好地工作,但是当我尝试运行main时,它会给出错误

Tests.hs:12:5:
    Ambiguous type variable `a0' in the constraints:
      (Arbitrary a0) arising from a use of `quickCheckWith'
          at Tests.hs:12:5-18
      (Show a0) arising from a use of `quickCheckWith'
          at Tests.hs:12:5-18
      (Ord a0) arising from an expression type signature
          at Tests.hs:12:26-72

为什么这主要不起作用,但在ghci中起作用?

这可能是由以下原因造成的

当测试这样的函数时,需要使用具体的元素类型。由于扩展规则,GHCi将元素类型默认为
()
,但在正常编译代码时不会发生这种情况,因此GHC告诉您它无法确定要使用哪种元素类型

例如,您可以在测试中使用
Int
<代码>()对于测试这个函数是非常无用的,因为所有元素都是相同的

quickCheck (prop_equals_elem :: Int -> [Int] -> Bool)

如果它适用于
Int
,则由于参数性,它应适用于任何类型。

运行快速检查测试时,快速检查需要知道如何生成数据。这里,您只告诉它您的代码应该使用任意类型的
Ord
type类,这还不足以让它开始测试。因此出现了关于不明确类型类的错误

如果您只需要一个任意的
Ord
实例,就像这里显示的那样,那么像
Int
这样的东西对于您的测试来说是一个不错的选择。这是一种线性排列的简单类型。因此,请尝试在
main
中将您的类型固定为
Int
,如下所示:

quickCheck (prop_equals_elem :: Int -> [Int] -> Bool)

至于它在GHCi中工作的原因,答案是默认设置。GHCi尽可能将类型变量默认为
()
,以避免在您真正不关心某个值的情况下出现虚假错误。实际上,这是一个糟糕的选择:只使用
()
类型进行测试不会测试任何有趣的东西!同样,显式类型签名更好。

QuickCheck可以从给定类型中选择任意元素,但不能选择实例化typeclass的任意类型。这将是一个有趣的增强,也可以考虑使用。