-
Notifications
You must be signed in to change notification settings - Fork 368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Subtyping Reconstruction #409
Comments
Just clarifying, this would also be fine, right?
|
From "Can we fix these problems":
Would introducing Shape interfaces do the trick here? Just food for thought |
Yes, it works in the same way as with any other Int-typed expression on the rhs. More precisely, in this case it is:
Yes, inferring only material interfaces will simplify everything, but there are some other issues. Therefore, we decided to address them separately to keep things manageable. |
I'm concerned about the approach to supporting this feature. The constraint resolution algorithm seems to have a lot of corner cases. For example, when resolving a constraint on a type variable, it goes through all the bounds on the type variable, but then it seems it can eventually generate a new bound on that same variable, which seems to create a non-terminating loop (or a loop that gets cut off arbitrarily). Even more worryingly, it seems hard to ensure the newly generated bounds conform to the invariants we can use to make typing efficient. For example, if every type variable has at most one upper bound, we can take advantage of this to keep subtyping and inference polynomial-time. But the proposed approach seems to enable adding arbitrary upper bounds to type variables, making these problems exponential-time. (I know some of these problems currently arise with smart casts, but there might be ways to fix this, whereas the proposed approach seems to make them unfixable.) This feature seems to be expected to be used by advanced users with low frequency. Given that, would it make sense to look into decidable approaches that require a little more guidance/annotation from the user? For example, here's my stab at adapting the syntax for a known decidable approach:
Here we add this In other words, the That's probably not very clear, so here is how it looks applied to various examples in the KEEP:
(I believe this is known as indexed pattern matching, albeit adapted to subtyping and inheritance rather than cases of an inductive data type.) |
@RossTate would this also basically allow existential types in subexpressions? What I mean is, Kotlin seems to have recently gained the ability to do something like: data class Foo<T>(val list: MutableList<T>, val set: MutableSet<T>)
fun <T> bar(list: MutableList<T>, set: MutableSet<T>) { }
fun Any.baz() = when (this) {
is Foo<*> -> bar(list, set)
else -> {}
} Where internally the type
Does not compile. It sounds like your proposed idea would cover this usecase nicely too by allowing:
Where |
@kyay10 What you're touching upon is type capturing. There's a lot I could go into, but I think I'll just focus on the specific question you asked: yes, what I described would support "opening" the existential type. All of the following would type-check:
and
and
They might be overkill for that particular example, but hopefully you can see how they can generalize to more complex examples. |
Apologies for the tangential conversation @RossTate. Would something like
also be supported? Or would it have to instead be the mouthful of:
If the automatic approach in the KEEP cannot be made polynomial (maybe there's a way to modify the algorithm?), then I would like this more-explicit syntax, especially because it allows that explicit type-capturing. In fact, even if the automatic approach works, I like the explicit syntax anyways since it clarifies what's going on for the reader. |
Yes, your first (more concise) version of |
Here's a proof that subtyping reconstruction makes validation NP-Hard. In particular, given a graph, the following shows how to generate a program that is valid if and only if the graph is 3-colorable.
|
We had internal discussions about the NP-hardness introduced by subtyping reconstruction. We have an idea for overcoming these issues by tracking the consistency of the introduced bounds. For example, in the previous case, something like |
Documenting this proof of undecidability for a subset of the features in consideration as they are currently written (which is not to say they cannot be adapted), which I came up with by adapting an idea from Garrigue and Le Normand's GADTs and Exhaustiveness: Looking for the Impossible.
In particular, Note that the hierarchy I used is not recursive. |
This issue is for discussion of the proposal to introduce Subtyping Reconstruction into the Kotlin language.
The full proposal text is available here: proposals/subtyping-reconstruction.md
The KEEP presents the Subtyping Reconstruction technique that introduces smart casts for generics.
Please, use this issue for the discussion on the substance of the proposal. For minor corrections to the text, please open comment directly in the PR: #410.
The text was updated successfully, but these errors were encountered: