How to make Data.List.Vector a member of Arbitrary easily?

haskell, quickcheck, typeclass

Solution

Your problem is that you need to parenthesize it, like `instance Arbitrary (V.Vector Double)`, etc. But there's a better way to do it:

instance (Arbitrary a) => Arbitrary (V.Vector a) where
    arbitrary = fmap V.fromList arbitrary

Note that you need the `fmap` because `arbitrary` is a value of type `Gen a`, and so in order to go from `Gen [a]` to `Gen (V.Vector a)` you need to lift `V.fromList` into `Gen`, which you can do since it's a functor.

Problem

I am using the Data.List.Vector in my program, and now I would like to use quickCheck for it. However, there is no instance for that. Since there already is an arbitrary for [Double], I thought I could do something like ``` instance Arbitrary V.Vector Double where arbitrary = V.fromList (arbitrary :: [Double]) ``` alas, GHC does not like this at all: ``` `Arbitrary' is applied to too many type arguments In the instance declaration for `Arbitrary V.Vector Double' ``` I guess I could also just make a bunch of properties that take in a [Double] and uses V.fromList, but that seems tedious.

Original source