Search code examples
haskellquickcheck

Haskell - QuickCheck: Checking Equality in TestTree


I am trying to write a TestTree in which I check the following axiom for my data type

type Deque a = [a]

My approach is the following

prop1 :: TestTree
prop1 = QC.testProperty "read_empty" $
    peekFront [] == Nothing

However, I am getting the following error message:

* Ambiguous type variable `a0' arising from a use of `=='
      prevents the constraint `(Eq a0)' from being solved.
      Probable fix: use a type annotation to specify what `a0' should be.
      Potentially matching instances:
        instance Eq Ordering -- Defined in `GHC.Classes'
        instance Eq DependencyType
          -- Defined in `tasty-1.5:Test.Tasty.Core'
        ...plus 29 others
        ...plus 14 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)

Maybe it is because I haven't defined a type for the content of the list, such as [Int] or so. And the compiler doesn't know if what's inside will be of class Eq and therefore not comparable to Eq.

But how can I tell the compiler, that this type will be a Maybe a with Eq a?


Solution

  • Maybe it is because I haven't defined a type for the content of the list, such as [Int] or so. And the compiler doesn't know if what's inside will be of class Eq and therefore not comparable to Eq.

    Exactly, QuickCheck cannot generate values of an unspecified type, so you have to pick one for a. The common pick for that purpose is Int. There's a variety of ways you can specify the type, for example

    prop1 :: TestTree
    prop1 = QC.testProperty "read_empty" $ peekFront ([] :: [Int]) == Nothing
    

    or

    {-# LANGUAGE TypeApplications #-}
    module <...> where
    
    <...>
    
    prop1 :: TestTree
    prop1 = QC.testProperty "read_empty" $ peekFront [] == Nothing @Int