You need to arbitrarily choose between Firstand Second, and then generate a suitable first / second component.
Choosing between a finite number of options can be quickly done with oneOf.
instance (Arbitrary a, Arbitrary b) => Arbitrary (Sum a b) where
arbitrary = oneOf [ First <$> arbitrary, Second <$> arbitrary ]
The above is equivalent to the next lower-level instance, which you may find more understandable at first.
instance (Arbitrary a, Arbitrary b) => Arbitrary (Sum a b) where
arbitrary = do
which <- arbitrary -- a random Bool
if which
then do
a <- arbitrary
return (First a)
else do
b <- arbitrary
return (Second b)
source
share