2015-04-08 49 views
9

考慮下面的測試爲distributivity law between reverse and ++哈斯克爾快速檢查最小的反例

import Test.QuickCheck 

test :: [Int] -> [Int] -> Bool 
test xs ys = reverse (xs ++ ys) == reverse xs ++ reverse ys 

test2 :: (Eq a) => [a] -> [a] -> Bool 
test2 xs ys = reverse (xs ++ ys) == reverse xs ++ reverse ys 

注意有關Int列出了

*Main> quickCheck test 
*** Failed! Falsifiable (after 5 tests and 3 shrinks):  
[1] 
[0] 

然而,測試equatable項目的列表,

*Main> quickCheck test2 
+++ OK, passed 100 tests. 

什麼讓第二次測試通過?

更新在與main = quickCheck test2編譯曖昧類型變量的後續錯誤的提示問題(如答案已經描繪),

No instance for (Eq a0) arising from a use of `test2' 
The type variable `a0' is ambiguous 
Possible fix: add a type signature that fixes these type variable(s) 

回答

14

當你真正評估test2,GHCI必須選擇一個類型a使用。如果沒有更多的信息,GHCi的擴展默認規則默認爲(),對此法律適用。

13
> verboseCheck test2 

Passed: 
[] 
[] 
Passed: 
[] 
[] 
Passed: 
[(),()] 
[()] 
Passed: 
[(),(),()] 
[()] 
Passed: 
[()] 
[(),(),(),()] 
... 

多態參數默認爲(),當然所有這些值都是相等的。