[Haskell-cafe] role constraints

Ben Franksen ben.franksen at online.de
Mon Dec 10 23:48:57 UTC 2018


I have a question about roles for type variables. I have a number of
data types that more or less represent partial bijections. They take two
type parameters that represent domain and range. The phantom type
parameters are extremely useful: code that shuffles these things around
will fail to type check if domains and ranges don't match up exactly.
However, there are some situations where I know more about the semantics
than the type checker and need to be able to coerce the phantom
parameters. No problem, that's what coerce is for...

Except, most of the code is polymorphic over the data type, requiring
only a few class constraints. And this polymorphism forces the type
checker to assume representational roles for the type parameters. Which
means I can't use coerce. Instead I have to use unsafeCoerce which I
would like to avoid.

Is there a way to express, as a constraint, that a certain parameter has
a phantom role? What I want to say is: you are only allowed to
instantiate the type variable 'a :: * -> * -> *' with data type 'A' if
'x' and 'y' in 'A x y' have both role phantom. And what I get is that I
can call coerce to modify the parameters 'x' and 'y'.



More information about the Haskell-Cafe mailing list