Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell, QuickCheck, falsify a (wrong) property:

Is there a way to falsify this (wrong) property:

prop :: Eq a => [a] -> Bool
prop xs = reverse xs == xs

When i Use QuickCheck and later VerboseCheck it gives 100 different forms of:

[(),(),(),(),(),(),(),(),(),(),(),(),(),(),()]

Passed:
and the final result is:

+++ OK, passed 100 tests.
like image 473
Jack Avatar asked Dec 26 '22 02:12

Jack


1 Answers

It just so happens that

  1. If you try to evaluate that in GHCi, it has to choose a particular instance type of Eq a to use, and with the ExtendedDefaultRules extension normally enabled in GHCi, it chooses ().
  2. For the type (), because it has only one (non-bottom) value, the proposition is actually true.

The simplest fix is to choose (almost) any other type by providing a type annotation:

Prelude Test.QuickCheck> quickCheck (prop :: [Int] -> Bool)
*** Failed! Falsifiable (after 4 tests and 3 shrinks): 
[0,1]
like image 69
Ørjan Johansen Avatar answered Jan 06 '23 10:01

Ørjan Johansen