[Haskell-cafe] role constraints
rae at cs.brynmawr.edu
Tue Dec 11 02:04:40 UTC 2018
You can use quantified constraints to get some approximation of the behavior you want.
> foo :: forall a. (forall x y. Coercible (a x) (a y)) => ... a ...
This means, effectively, that a's parameter's role must be phantom. I've seen some bug reports fly by that talk about situations like these and how the solver sometimes isn't the best in these scenarios, but this approach just might work for you.
> On Dec 10, 2018, at 6:48 PM, Ben Franksen <ben.franksen at online.de> wrote:
> 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'.
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> Only members subscribed via the mailman list are allowed to post.
More information about the Haskell-Cafe