Search code examples
haskellfunctional-programmingfunctor

Is Gen an instance of Functor?


I am learning about functors in Haskell, and I would like to know whether QuickCheck's Gen is an instance of Functor? Any insights are appreciated.


Solution

  • Yes. This is described in the documentation for Gen: under the instances section, it shows Functor Gen.

    It is implemented as [src]:

    instance Functor Gen where
      fmap f (MkGen h) =
        MkGen (\r n -> f (h r n))

    MkGen is the data constructor of Gen. It contains a function of type QCGen -> Int -> a. What we thus basically do is create a function \r n -> f (h r n) that thus will "post-process" the result of h r n.