I have a configurable function that works on values that implement Generic class. I need to check it against several values, with several configurations. My first idea was to use the Array monad to obtain a Cartesian product of the set of values and the set of configurations. But the values must belong to a range of types in order for the test suite to be comprehensive. So how can I put my values, being of different types, into an array?
In plain JavaScript arrays can contain anything, so the problem does not exist. In Haskell, I would create a type such as data Wrapped = forall α. Generic α ⇒ Wrapped α, but it does require a few language extensions. I tried going this way in PureScript and it does not even parse. Is there a way to make it work?
Or you can alternatively do it with just regular functions:
newtype GenericBox =
GenericBox (forall r. (forall a rep. Generic a rep => a -> r) -> r)
mkGenericBox :: forall a rep. Generic a rep => a -> GenericBox
mkGenericBox x = GenericBox \k -> k x
runGenericBox :: forall r.
GenericBox ->
(forall a rep. Generic a rep => a -> r) ->
r
runGenericBox (GenericBox k) = k
I love your example, which is way more elegant than workarounds for this problem that I’ve used in the past! Would you mind elaborating on how you would use the exists library to do a similar thing? I can’t seem to figure out where to put the type class constraints in place.