Hspec & QuickCheck - 模棱两可的类型变量 a0?
Hspec & QuickCheck - Ambiguous type variable a0?
我在 Haskell 中编写了一个函数,它接受一个任意元素列表和 returns(映射)一个元组列表。每个元组包含原始元素和一个分数,列表中的所有分数加起来为 1(因此我只使用 1 ``div`` length xs
计算一次分数并将其应用于所有元素)。这是代码:
uniform :: [a] -> [(a, Int)]
uniform xs = map (\x -> (x, prob)) xs
where prob = 1 `div` (length xs)
(免责声明:这实际上是一个稍微简化的版本,但我产生了完全相同的行为,所以希望这就足够了)。
我正在尝试使用 Hspec 和 Quickcheck 通过基于 属性 的测试来解决这个问题:
spec = do
describe "uniform" $ do
it "produces a uniform distribution summing to 1" $ property $
let totalProbability ((_, p):xs) = p + (totalProbability xs)
in (\xs -> (totalProbability $ uniform xs) `shouldBe` 1)
然而,当我 运行 这个时,我得到这个错误:
• Ambiguous type variable ‘a0’ arising from a use of ‘property’
prevents the constraint ‘(Arbitrary a0)’ from being solved.
Probable fix: use a type annotation to specify what ‘a0’ should be.
These potential instances exist:
instance (Arbitrary a, Arbitrary b) => Arbitrary (Either a b)
-- Defined in ‘Test.QuickCheck.Arbitrary’
instance Arbitrary Ordering
-- Defined in ‘Test.QuickCheck.Arbitrary’
instance Arbitrary Integer
-- Defined in ‘Test.QuickCheck.Arbitrary’
...plus 19 others
...plus 62 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the second argument of ‘($)’, namely
‘property
$ let totalProbability ((_, p) : xs) = p + (totalProbability xs)
in (\ xs -> (totalProbability $ uniform xs) `shouldBe` 1)’
In a stmt of a 'do' block:
it "produces a uniform distribution summing to 1"
$ property
$ let totalProbability ((_, p) : xs) = p + (totalProbability xs)
in (\ xs -> (totalProbability $ uniform xs) `shouldBe` 1)
In the second argument of ‘($)’, namely
‘do it "produces a uniform distribution summing to 1"
$ property
$ let totalProbability ((_, p) : xs) = ...
in (\ xs -> (totalProbability $ uniform xs) `shouldBe` 1)’
|
12 |它 "produces a uniform distribution summing to 1" $ 属性 $
| ^^^^^^^^^^...
我猜我在某个地方没有给 QuickCheck 关于如何生成测试值的足够信息,但我不确定从这里去哪里。
如有任何帮助,我们将不胜感激。
谢谢!
您需要为 xs
指定类型:这是一个字符串列表吗?整数?布尔值?这样 QuickCheck 就可以生成该类型的随机样本。
你可以这样写,比如:
...
in (\xs -> (totalProbability $ uniform (xs :: [Int])) `shouldBe` 1)
我在 Haskell 中编写了一个函数,它接受一个任意元素列表和 returns(映射)一个元组列表。每个元组包含原始元素和一个分数,列表中的所有分数加起来为 1(因此我只使用 1 ``div`` length xs
计算一次分数并将其应用于所有元素)。这是代码:
uniform :: [a] -> [(a, Int)]
uniform xs = map (\x -> (x, prob)) xs
where prob = 1 `div` (length xs)
(免责声明:这实际上是一个稍微简化的版本,但我产生了完全相同的行为,所以希望这就足够了)。
我正在尝试使用 Hspec 和 Quickcheck 通过基于 属性 的测试来解决这个问题:
spec = do
describe "uniform" $ do
it "produces a uniform distribution summing to 1" $ property $
let totalProbability ((_, p):xs) = p + (totalProbability xs)
in (\xs -> (totalProbability $ uniform xs) `shouldBe` 1)
然而,当我 运行 这个时,我得到这个错误:
• Ambiguous type variable ‘a0’ arising from a use of ‘property’
prevents the constraint ‘(Arbitrary a0)’ from being solved.
Probable fix: use a type annotation to specify what ‘a0’ should be.
These potential instances exist:
instance (Arbitrary a, Arbitrary b) => Arbitrary (Either a b)
-- Defined in ‘Test.QuickCheck.Arbitrary’
instance Arbitrary Ordering
-- Defined in ‘Test.QuickCheck.Arbitrary’
instance Arbitrary Integer
-- Defined in ‘Test.QuickCheck.Arbitrary’
...plus 19 others
...plus 62 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the second argument of ‘($)’, namely
‘property
$ let totalProbability ((_, p) : xs) = p + (totalProbability xs)
in (\ xs -> (totalProbability $ uniform xs) `shouldBe` 1)’
In a stmt of a 'do' block:
it "produces a uniform distribution summing to 1"
$ property
$ let totalProbability ((_, p) : xs) = p + (totalProbability xs)
in (\ xs -> (totalProbability $ uniform xs) `shouldBe` 1)
In the second argument of ‘($)’, namely
‘do it "produces a uniform distribution summing to 1"
$ property
$ let totalProbability ((_, p) : xs) = ...
in (\ xs -> (totalProbability $ uniform xs) `shouldBe` 1)’
| 12 |它 "produces a uniform distribution summing to 1" $ 属性 $ | ^^^^^^^^^^...
我猜我在某个地方没有给 QuickCheck 关于如何生成测试值的足够信息,但我不确定从这里去哪里。
如有任何帮助,我们将不胜感激。
谢谢!
您需要为 xs
指定类型:这是一个字符串列表吗?整数?布尔值?这样 QuickCheck 就可以生成该类型的随机样本。
你可以这样写,比如:
...
in (\xs -> (totalProbability $ uniform (xs :: [Int])) `shouldBe` 1)