Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upHow to define an existential type with a constraint #3474
Comments
This comment has been minimized.
This comment has been minimized.
|
You need to use Church encode it instead: class C a where
f :: a -> Unit
data Any = Any (forall r. (forall a. C a => a -> r) -> r)
instance anyC :: C Any where
f (Any k) = k f |
This comment has been minimized.
This comment has been minimized.
Savelenko
commented
Dec 4, 2018
|
Thanks for the swift reply. This works, but I would very like to understand what's going on. Does this have to do with dictionary passing in the generated code (or rather, not having to store the dictionary in Still, the second compiler error above is unfortunate, as it seemingly states that types |
This comment has been minimized.
This comment has been minimized.
|
@Savelenko Your original type doesn't encode an existential; rather, a universal. I.e the difference between "forall x" and "exists x". The type With the existential, it's the other way around. You "the implementer" get to pick an With your: instance anyC :: C Any where
f (Any (a :: forall a. C a => a)) = f aI wouldn't expect this to work, because you aren't picking a type for the term |
This comment has been minimized.
This comment has been minimized.
|
Closing as there's no issue here. |
Savelenko commentedDec 4, 2018
I am trying to define an existential type with a contract for the hidden type, using a type class. First attempt:
This results in:
I can't even create a value like
u = Any 3, assuming there is an instance ofCforInt. Am I doing something wrong?However, things get more suspicious when I provide a type annotation at (1):
giving the following, at the very least confusing, error: