Gradual Typing Reading Notes [000T]
Gradual Typing Reading Notes [000T]
Rules for gradual type inference [000V]
Rules for gradual type inference [000V]
In section 3 of Gradual Typing with Unification-based Inference, the authors discuss some examples where the naive “well-typed after substitution” approach fails. That implies some rules that gradual type inference should follow.
In the following example codes, α is a type variable not in the parametric polymorphism sense, but in the unification-based type inference sense, that is, a variable to be substituted with a gradual type after type inference.
Static conservativity
Static conservativity
let f(g: α) = g 1
f 1The above program would not pass static typecheck, since there is no possible substitution for α. Thus, even if substituting α with ? make the program pass gradual typecheck, such a substitution should not be chosen.
The rule here is that a program can be gradually well-typed if and only if it can be statically well-typed.
Constraint-informativeness preservation
Constraint-informativeness preservation
let x: α = x + 1Substituting α with ? would make the above program pass gradual typecheck, but a more appropriate choice is int.
The rule here is that when a more precise type is possible, a less precise one should not be chosen as the inferred type.
Minimal adequate solution
Minimal adequate solution
let f(x: ?) =
let y: α = x in ySubstituting α with some more precise type such as int won’t trigger a type error during gradual typecheck, since they are consistent with ?. However, doing so blocks the possibility to, for example, apply the function to a boolean. The only appropriate substitution for α is ?.
The rule here is that when multiple types all make the program gradually well-typed, then the chosen type should be less precise than them and be consistent with them, that is, they are all materializations of the chosen type.
Notes on Gradual Type Inference
Notes on Gradual Type Inference
ArticleDynamic Type Inference for Gradual Hindley–Milner Typing [msi-Dynamictypeinference-2019]
ArticleDynamic Type Inference for Gradual Hindley–Milner Typing [msi-Dynamictypeinference-2019]
My feeling is that the problems described in section 1.3 and section 1.4 of this paper are somehow not as severe as they might initially appear. The problem is basically that some programs left you so-called undecided type variables, which can be instantiated to any type; these undecided type variables are usually introduced due to the presence of the type, which hides useful information from other parts of the program. This brings two problems:
- If you fill in the undecided type variables with some concrete types, the program will reduce to a value, while some other choices of types will lead to divergence.
- Even if you fill in the undecided type variables with the type, that would allow the program even if there are no static types that make the program execution successful.
Actually, both of the problems seems to be the inherent problems of gradual typing — if you hide too much information using types, the inference will not be able to give useful results. (Maybe that is why the authors choose a “dynamic inference” approach.) For the first problem, I don’t think there are good solutions (maybe see papers by Castagna et al. recently for how they intersect the dynamic type with the static types to get more precise inference results) other than the approach of the second problem. For the second problem, I think the core of the erroneous example by the authors is that the static type system of their language is not strong enough: add union types and the problem will be solved for their example. Plus, I think the point of using dynamic types is, sometimes, to allow some programs that are not statically well-typed but are still “safe”.