nick8325 / quickcheck

Automatic testing of Haskell programs.
Other
713 stars 119 forks source link

CoArbitrary1 #285

Open Zemyla opened 4 years ago

Zemyla commented 4 years ago

We seemingly can't have a CoArbitrary1 and CoArbitrary2 typeclass in Haskell98 compatible libraries because of the lack of RankNTypes. However, this isn't necessarily an insurmountable problem. I figured out a creative way around it.

-- Name is a placeholder, and its constructor is not exported.
data Opaque = Opaque {-# UNPACK #-} !Int {-# UNPACK #-} !QCGen
-- Also no instances defined (except Typeable automatically)

-- Turn a monomorphic coarbitrary function into a polymorphic one.
-- This is the only user-accessible function which mentions Opaque.
coarbOpaque :: (a -> Gen Opaque -> Gen Opaque) -> a -> Gen c -> Gen c
coarbOpaque coa a (MkGen gen) = MkGen $ \n rng -> case unGen (coa a (MkGen Opaque)) n rng of
  Opaque n' rng' -> gen n' rng'

-- And then we can use it instead of the Haskell98-unfriendly (forall x. a -> Gen x -> Gen x).
class CoArbitrary1 f where
  liftCoarbitrary :: (a -> Gen Opaque -> Gen Opaque) -> f a -> Gen c -> Gen c
#ifndef NO_GENERICS
  default liftCoarbitrary :: (Generic1 f, GCoArbitrary1 (Rep1 f) => (a -> Gen Opaque -> Gen Opaque) -> f a -> Gen c -> Gen c
  liftCoarbitrary = genericLiftCoarbitrary

-- Include the GCoArbitrary1 class here. It's rather mechanical.
#endif

-- And the class for CoArbitrary2.
class CoArbitrary2 f where
  liftCoarbitrary2 :: (a -> Gen Opaque -> Gen Opaque) -> (b -> Gen Opaque -> Gen Opaque) -> f a b -> Gen c -> Gen c

-- And instances go here.

I think we need a better name for Opaque, though.

phadej commented 4 years ago

I think Rank2Types are fine. There is at least one use of it already.