How do I write a Hedgehog generator for a wrapped function?

127 views Asked by At

I'm currently working through the book Thinking with Types. In the third chapter Variance, the author provides an exercise in which the reader should implement the functor instance for T1.

newtype T1 a = T1 (Int -> a)

I found a couple of ways to make this type check, e.g.,

instance Functor T1 where
  fmap f (T1 a) = T1 (f . a)

…and

instance Functor T1 where
  fmap f (T1 a) = T1 (f <$> a)

Of course just making the instance type check doesn't mean that the instance respects the functor laws. To try and test that the instance I write is correct (and thus show that at least one of the instances written above are wrong), I'd like to write some property-based tests.

I would like to use the hedgehog-classes library to write my tests. The library provides the lawsCheck and functorLaws functions that I believe I will need. I imagine I could test my instances with the following line in GHCi

> lawsCheck (functorLaws genT1)

What I'm missing is how to write genT1, which I imagine would generate one (or perhaps several? Should it actually be genT1List?) random T1 value(s). Do I need to use something like hedgehog-fn to generate arbitrary functions so I can make some random (Int -> a) values for T1 to wrap? If so, how?

0

There are 0 answers