I’m working on a simple dataflow based system (imagine it like a LabView editor/runtime) written in Java. The user can wire blocks together in an editor and I need type inference to ensure the dataflow graph is correct, however, most type inference examples are written in mathematical notations, ML, Scala, Perl, etc., which I don’t “speak”.
I read about the Hindley-Milner algorithm and found this document with a nice example I could implement. It works on a set of T1 = T2 like constraints. However, my dataflow graphs translate to T1 >= T2 like constraints (or T2 extends T1, or covariance, or T1 <: T2 as I saw it in various articles). No lambdas just type variables (used in generic functions like T merge(T in1, T in2)) and concrete types.
To recap the HM algorithm:
Type = {TypeVariable, ConcreteType}
TypeRelation = {LeftType, RightType}
Substitution = {OldType, NewType}
TypeRelations = set of TypeRelation
Substitutions = set of Substitution
1) Initialize TypeRelations to the constraints, Initialize Substitutions to empty
2) Take a TypeRelation
3) If LeftType and RightType are both TypeVariables or are concrete
types with LeftType <: RightType Then do nothing
4) If only LeftType is a TypeVariable Then
replace all occurrences of RightType in TypeRelations and Substitutions
put LeftType, RightType into Substitutions
5) If only RightType is a TypeVariable then
replace all occurrences of LeftType in TypeRelations and Substitutions
put RightType, LeftType into Substitutions
6) Else fail
How can I change the original HM algorithm to work with these kind of relations instead of simple equality relations?
Java-ish example or explanation would be much appreciated.
I read at least 20 articles and found one (Francois Pottier: Type inference in presence of subtyping: from theory to practice) which I could use:
Input:
Helper functions:
ExtendsOrEquals can tell about two concrete types if the first extends or equals the second, e.g., (String, Object) == true, (Object, String) == false.
Union computes the common subtype of two concrete types if possible, e.g., (Object, Serializable) == Object&Serializable, (Integer, String) == null.
Intersection computes the nearest supertype of two concrete types, e.g., (List, Set) == Collection, (Integer, String) == Object.
SubC is the structural decomposition function, which in this simple case will just return a singleton list containing a new TypeRelation of its parameters.
Tracking structures:
UpperBounds keeps track of types which may be supertypes of a type variable, LowerBounds keeps track of types which may be subtypes of the type variable. Reflexives keeps track of the relations between pairs type variables to help in the bound-rewriting of the algorithm.
The algorithm is as follows:
A basic example:
The relations of this expression:
1.) rel is (String, T); Case 3 is activated. Because Reflexives is empty, the LowerBounds of T is set to String. No UpperBounds for T is present, therefore, TypeRelations remains unchanged.
2.) rel is (Integer, T); Case 3 is activated again. Reflexives is still empty, the Lower bound of T is set to the intersection of String and Integer, yielding Object, Still no upper bounds for T and no changes in TypeRelations
3.) rel is T >= U. Case 1 is activated. Because Reflexives is empty, the Upper Bounds of T is combined with the Upper bounds of U, which remains empty. Then the lower bounds U is set to the lower bounds ot T, yielding Object >= U. The TypeRelation(T, U) is addet to Reflexives.
4.) the algorithm terminates. From the bounds Object >= T and Object >= U
In another example, a type conflict is demonstrated:
The relations:
Steps 1.) and 2.) are the same as above.
3.) rel is T >= U. Case 2 is activated. The case tries to union the Upper Bound of T (which is Object at this point) with Integer, that fails and the algorithm fails.
Extensions to the Type system
Adding generic types to the type system needs an extension in the main cases and in the SubC function.
Some ideas: