[Haskell-cafe] CoArbitrary

Tony Morris tonymorris at gmail.com
Sat Feb 9 08:07:54 CET 2013


Hello,
In the QuickCheck library, there is a type-class called CoArbitrary. It
is defined like so:

class CoArbitrary a where
  coarbitrary :: a -> Gen b -> Gen b -- Gen is a monad

Its purpose is to allow generation of functions. In other words, for
taking Gen x -> Gen (a -> x), which could be done rather degenerately
(fmap const) but QuickCheck constrains with (CoArbitrary a) to perturb
the resulting value instead of ignoring (const) it.

It has always puzzled me in the general sense of thinking about the
scenario: f x -> f (a -> x) and whether the CoArbitrary is a
good/general solution, perhaps for the specific case of f=Gen or maybe
even more generally.

-- approximate
(a -> f x -> f x) -- e.g. CoArbitrary to perturb the result
-> f x -- e.g. Gen x
-> f (a -> x) -- e.g. Gen (a -> x)

So I often wonder about what might be a better (and perhaps more
general) constraint to produce functions f x -> f (a -> x) for a given
Monad f. I was wondering if there is an existing abstraction (or paper)
that might point me in that direction. It is a problem that I encounter
on occasion in general programming and I am using Arbitrary/CoArbitrary
as an example to help make my point, but I am dissatisfied (for reasons
that I am unsure about) with the solution provided by CoArbitrary.

What about other monads? For example, what is a general constraint to be
placed to permit a function Maybe x -> Maybe (a -> x) that does not
simply "const" away the argument.

I hope I have phrased this in a way to make the point. I found it a bit
difficult to articulate and I do wonder (hope!) that others encounter
similar scenarios. Thanks for any tips!

-- 
Tony Morris
http://tmorris.net/




More information about the Haskell-Cafe mailing list