2015-04-08 15 views
9

Rozważmy następujące testy dla distributivity law between reverse and ++,Haskell QuickCheck minimalny licznik przykład

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 

Uwaga na listach Int że

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

Jednakże test na listach equatable przedmiotów,

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

Co sprawia, że ​​drugi test przechodzi?

Aktualizacja Na kompilacji z main = quickCheck test2, kolejny błąd na zmienną typu niejednoznacznej wskazówki problem (jak już przedstawiono w odpowiedzi),

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) 

Odpowiedz

14

Kiedy faktycznie ocenić test2, GHCi musi wybrać typ a używać. Bez większej ilości informacji, rozszerzone domyślne reguły GHCi sprawiają, że domyślnie jest to (), dla której prawo jest prawdziwe.

13
> verboseCheck test2 

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

Parametr polimorficzny przyjmuje wartość domyślną () i oczywiście wszystkie takie wartości są równe.

Powiązane problemy