-
Notifications
You must be signed in to change notification settings - Fork 152
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
Develop a more principled type inference algorithm #3601
Comments
Ideally, we want to support:
|
The SimpleSub algorithm provides some nice inspiration:
In summary, this algorithm is quite similar to the usual HM-style Algorithm W, but tracking subtyping constraints rather than performing unification. The results are then encoded using union and intersection types, roughly,
followed by a few simplifications passes to produce compact types. We can use this to infer a general solution for the sorts, then can find all the maximal models (in the sense of the current type inference algorithm) by enumerating the monomorphizations and interpreting ∧/∨ as meet/join in the sort poset. |
Translation from Sort Inference to SimpleSubDisregarding ambiguities, we can apply the SimpleSub algorithm to our problem as follows:
This translation is just conceptual - the actual implementation can work with the existing To extend this to handle ambiguities, we begin by performing type inference on each unambiguous section of the parse-forest, but parametric over any contained ambiguities. Specifically,
Once we have this local information for each unambiguous section, it in effect shows us all the typing-constraints that a particular section of the parse-forest places on the variables. We can then use this to prune the parse-forest by subsituting in the inferred types of ambiguities, forming a set of all possibilities at each ambiguity point. |
Parametric Sorts with Non-Structural SubtypingConsider types as finite trees built up from a finite number of type constructors. There are two types of subtyping orders you can have over such types:
On first glance, I believed this algorithm would support parametric sorts quite easily. However, it in fact only does so in a limited context, namely
K, on the other hand, supports completely arbitrary subsorting regardless of constructor or arity. As far as I'm aware, there is no standard algorithm which can actually handle this in full generality. As a fallback then, if we eventually want to fully support parametric sorts, we'll likely still need to rely on a constraint solver when we run into these sort of constraints. I'll try to design things though so that we can still handle most cases directly with the inference algorithm and only pass off some small required formulas to Z3. |
When this is concluded we should close this issue: #3551 |
Performance issues with the C-semantics |
#3673's description includes a more flushed out explanation of the proposal above |
It seems like the "fall back to Z3" strategy we discussed previously will be viable; @Scott-Guest will look into this once there is an end-to-end implementation for the partial deterministic inferencer. @radumereuta mentions that we should keep consistency in mind as well as correctness - @Scott-Guest has a script to make sure that the inference results are the same as in Z3; if results are different it needs to be checked why that's the case. Z3 might not be correct! |
In order to improve performance and eventually add support for more powerful types, we should develop a robust type inference algorithm and only rely on the current Z3 implementation as a fallback.
The text was updated successfully, but these errors were encountered: