[Haskell-cafe] Help using QuickCheck
Benjamin Franksen
benjamin.franksen at bessy.de
Fri Oct 21 15:16:36 EDT 2005
On Friday 21 October 2005 20:24, Andrew Pimlott wrote:
> On Fri, Oct 21, 2005 at 07:58:10PM +0200, Benjamin Franksen wrote:
> > I am asking help with using QuickCheck. I tried everything I could
> > think of, but invariably as soon as I start invoking the quickCheck
> > function on my property it fails with
> >
> > <interactive>:1:0:
> > No instances for (Arbitrary (OrdSeq Int), Show (OrdSeq Int))
> > arising from use of `quickCheck' at <interactive>:1:0-20
> > Probable fix:
> > add an instance declaration for (Arbitrary (OrdSeq Int), Show
> > (OrdSeq Int))
> > In the definition of `it': it = quickCheck prop_DeleteInsert
> >
> > The property is quite a simple one:
> >
> > prop_DeleteInsert x xs = delete x (insert x xs) == xs
> > where types = xs :: OrdSeq Int
> >
> > My code compiles just fine with ghc.
>
> It compiles (and runs) fine as a function on OrdSeqs, with type
>
> prop_DeleteInsert :: Int -> OrdSeq Int -> Bool
>
> But as an argument to quickCheck, it needs be in the class Testable,
> which boils down to the requirement that Int and "OrdSeq Int" are
> both in the class Arbitrary. So you need to write something like
>
> instance Arbitrary a => Arbitrary (OrdSeq a) where ...
>
> This is confusing because QuickCheck is clever with type classes to
> check "ordinary" boolean functions. But in fact, your property is
> equivalent to
>
> prop_DeleteInsert = forall arbitrary $ \x ->
> forall arbitrary $ \xs ->
> delete x (insert x xs) == (xs :: OrdSeq
> Int)
>
> And QuickCheck doesn't know how to create arbitrary OrdSeqs unless
> you tell it.
Yes, now as you say it it seems quite obvious. I was mislead by the
other error message, which I took to be the more important one, because
it appeared first. Bad habits...
> > I downloaded the haskell script 'quickCheck' and modified the line
> > where ghci is executed like this:
> >
> > system ("/usr/bin/ghci -cpp -package QuickCheck "++options opts'++"
> > <hugsin")
> >
> > (I am using ghc 6.4.1, btw). Running 'quickCheck
> > Data/FingerSearchTreeUtil.hs' gives me:
> >
> > <interactive>:1:0:
> > Failed to load interface for `QuickCheck':
> > Could not find module `QuickCheck':
> > it is a member of package util-1.0, which is hidden
> >
> > Strange. Replacing 'ChickCheck' with 'util' gives me the above
> > error message about "No instance for (Show (IO ()))..."
>
> In package QuickCheck, the module is called "Test.QuickCheck" (so
> "import Test.QuickCheck"), whereas in package util, it is called just
> "QuickCheck" (but this is deprecated).
Thanks a lot for this explanation! Taking a look at the script, I see
"QuickCheck.quickCheck" being written to the program that ghci gets as
input. Changing it to "Test.QuickCheck.quickCheck" indeed fixes the
problem.
Thanks everyone for helping,
Ben
More information about the Haskell-Cafe
mailing list