Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you override Haskell type class instances provided by package code?

I have some old Haskell code that includes QuickCheck test cases. Newer versions of QuickCheck (I've just upgraded to 2.4.0.1) include type class instances for Arbitrary Word8 and others. These did not exist in older 2.0.x versions of Test.QuickCheck.Arbitrary.

While useful in the general sense, the package-provided Arbitrary Word8 generator is not the one I want to use for my test suite:

instance Arbitrary Word8 where
  arbitrary = frequency [(2, oneof [return ctrlFrameDelim, return ctrlEscape, return ctrlXon, return ctrlXoff]),
                         (8, choose (0, 255))]

The above code causes a duplicate instance declaration error at compile-time. I can take this code out and get by with the default generator but I'd like to know the proper way to solve this.

One possible solution I've considered (but not tested) is aliasing Word8 using newtype. That would cause many changes throughout the source so I'm hoping there is a cleaner way.

EDIT: As mentioned in the comments below, the accepted answer was very clean and easy to implement:

newtype EncodedByte = EncodedByte Word8

instance Arbitrary EncodedByte where
  arbitrary = liftM EncodedByte $ frequency [(2, elements [ctrlFrameDelim, ctrlEscape, ctrlXon, ctrlXoff]),
                                             (8, choose (0, 255))]
like image 417
David Joyner Avatar asked Apr 13 '11 01:04

David Joyner


1 Answers

A newtype alias is the standards solution here. In most cases, which might not include yours, this isn't a big deal because the newtype wrapper only needs to appear where you use the Arbitrary typeclass. For example, you might have at some top level:

x <- arbitrary

And instead you'd have

newtype SomeNewType = SNT Word8
instance Arbitrary SomeNewType where ...
....
    SNT x <- arbitrary

What you probably want doesn't exist as a GHC extension - you want explicit importing and exporting of instances. If you had explicit instance imports this would allow:

import Test.QuickCheck hiding (Arbitrary(Word8))

But break lots of code that currently works by implicit imports of instances:

import Test.QuickCheck (quickCheck) -- note the implicit import of Arbitrary(..)
like image 54
Thomas M. DuBuisson Avatar answered Nov 17 '22 06:11

Thomas M. DuBuisson