-
Notifications
You must be signed in to change notification settings - Fork 28
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
Support precise type inference for element indexing #104
Comments
Actually a simpler approach may be possible, although not completely equivalent. But it would be a cleaner first step. The idea is to just introduce a new form of function-like type, call it We get ambiguous constraints when intersected indexed types appear on the LHS, but these will not actually arise from type inference, given the scheme above. |
can i say for the |
So the main algorithm is as follows:
So I only need to write code in the two new functions, also inside the I still have one question, where should we call the two functions? |
For the other algorithm you mentioned, do I still need to write the two functions? |
No, that's not at all the semantics I proposed above. Please read again:
Also for now let's only focus on arrays.
It seems you have forgotten or you misremember the explanations I gave you in our last meeting. Did you take notes? I proposed op add the "indexed by" and "indexed in" lists as mutable
Let's not consider that one for now. |
Things to be discussed in the meeting tmr (just want to drop it here so more convenient to take look) The Remaining Cases: (2). Defined things but not being assigned a value for string, float, etc... , I found (3). For the (4). How to match this case The Report: |
The goal is to support precise type inference for the indexing operator
a[i]
.For instance, if
a
turns out to have typeArray<int>
, thena[0]
should have typeint | undefined
; but ifa[0]
turns out to have a more precise type such as[int, bool, nat]
, thena[0]
should have typeint
.(Note: the
... | undefined
return type for imprecise indexing type inference is in the process of being implemented in mahzoun99#1.)The way to do this is as follows.
We should add some meta information in type variables, in addition to upper and lower bounds:
A list of "indexed in" info
(A, R)
, which specifies what type the type variable is used to index intoA
and that the type resulting from this operation isR
.A list of "indexed by" info
(T, R)
, which specifies what type the type variable is indexed into by some concreteT
and that the type resulting from this operation isR
.For example, if
a: ?A
andi: ?I
, then typinga[i]
will result in creating a new type variable?R
and adding(?A, ?R)
to the "indexed in" info of type variable?I
.Then, the job of type inference is simply making sure that all
?I::(?A, ?R)
and?A::(?I, ?R)
info remain consistent, by propagating concrete bounds that are added to the relevant type variables.More specifically: when constraining a type variable with a new concrete lower bound
B
in therec
function ofConstraintSolver.scala
, we should check:its "indexed in" info. For each such
(?A, ?R)
, we should add(B, ?R)
to it, and while doing so we should make sure that this(B, ?R)
is consistent with the existing lower bounds in?A
;its "indexed by" info. For each such
(T, ?R)
, we should make sure thatB
is consistent with it.Example: if add a lower bound
1 | 2
to type variableI0
which has "indexed in" info of(?A0, ?R0)
, then I add(1 | 2, ?R0)
as "indexed by" into in?A0
. Then, if[S, T, U]
is a lower bound on?A0
, I should look at what the index1 | 2
yields from that tuple, in this caseT
andU
, and add these as lower bounds inR0
.There is one subtlety to deal with, though: if the
(?A, ?R)
info we want to register into some?I
has a higher level than?A
, we need to extrude its components to the correct level, using theextrude
method.The text was updated successfully, but these errors were encountered: