use of org.eclipse.n4js.ts.types.InferenceVariable in project n4js by eclipse.
the class BoundSet method combineInvInvWithProperType.
/**
* Given two type bounds `α = U` and `β = T` with α ≠ β, will return a new constraint `β = T[α:=U]` if
* <ul>
* <li>U is proper, and
* <li>T mentions α.
* </ul>
* Otherwise, <code>null</code> is returned.
*/
private TypeConstraint combineInvInvWithProperType(TypeBound boundWithProperRHS, TypeBound boundOther) {
final InferenceVariable alpha = boundWithProperRHS.left;
final TypeRef U = boundWithProperRHS.right;
final TypeRef T = boundOther.right;
if (TypeUtils.isProper(U) && TypeUtils.getReferencedTypeVars(T).contains(alpha)) {
final InferenceVariable beta = boundOther.left;
// returns T[α:=U]
final TypeRef T_subst = substituteInferenceVariable(T, alpha, U);
// performance tweak: avoid unnecessary growth of bounds
removeBound(boundOther);
return new TypeConstraint(typeRef(beta), T_subst, INV);
}
return null;
}
use of org.eclipse.n4js.ts.types.InferenceVariable in project n4js by eclipse.
the class InferenceContext method newInferenceVariablesFor.
/**
* Introduces newly generated inference variables for each type parameter of the given function type (if generic)
* and returns a non-generic function type in which all type variables owned by the given function type are replaced
* by those inference variables; simply returns the given function type unchanged if it already is non-generic.
* Returns given function type unchanged in case of error (so returned function type may actually be generic, but
* only in error cases).
* <p>
* Example: given a function type such as
*
* <pre>
* {function<T,S>(G<T,string>):S}
* </pre>
*
* this method will add two new type variables α', β' and return
*
* <pre>
* {function(G<α',string>):β'}
* </pre>
*
* Note that the returned function type is non-generic.
*/
public FunctionTypeExprOrRef newInferenceVariablesFor(FunctionTypeExprOrRef funTypeRef) {
if (!funTypeRef.isGeneric())
return funTypeRef;
// NOTE: typeParam may contain null entries!
final List<TypeVariable> typeParams = funTypeRef.getTypeVars();
final InferenceVariable[] newInfVars = newInferenceVariables(typeParams.size(), true);
final List<? extends TypeRef> newInfVarsRefs = Stream.of(newInfVars).map(TypeUtils::createTypeRef).collect(Collectors.toList());
// new, empty RE
final RuleEnvironment G_params2infVars = RuleEnvironmentExtensions.newRuleEnvironment(G);
RuleEnvironmentExtensions.addTypeMappings(G_params2infVars, typeParams, newInfVarsRefs);
final TypeArgument left_withInfVars = ts.substTypeVariables(G_params2infVars, funTypeRef).getValue();
if (left_withInfVars instanceof FunctionTypeExprOrRef)
return (FunctionTypeExprOrRef) left_withInfVars;
// in case of substitution error: return original funTypeRef
return funTypeRef;
}
use of org.eclipse.n4js.ts.types.InferenceVariable in project n4js by eclipse.
the class InferenceContext method newInferenceVariable.
private InferenceVariable newInferenceVariable(boolean internal) {
final InferenceVariable iv = TypesFactory.eINSTANCE.createInferenceVariable();
final String name = internal ? "_" + unusedNameGeneratorInternal.next() : unusedNameGenerator.next();
iv.setName(name);
addInferenceVariables(internal, iv);
return iv;
}
use of org.eclipse.n4js.ts.types.InferenceVariable in project n4js by eclipse.
the class Reducer method mightBeSubtypeOf.
private boolean mightBeSubtypeOf(FunctionTypeExprOrRef left, FunctionTypeExprOrRef right) {
// step 1: replace all inference variables by UnknownTypeRef
final TypeRef unknown = TypeRefsFactory.eINSTANCE.createUnknownTypeRef();
final RuleEnvironment G_temp = RuleEnvironmentExtensions.newRuleEnvironment(G);
for (InferenceVariable iv : ic.getInferenceVariables()) {
RuleEnvironmentExtensions.addTypeMapping(G_temp, iv, unknown);
}
final TypeArgument leftSubst = ts.substTypeVariables(G_temp, left).getValue();
final TypeArgument rightSubst = ts.substTypeVariables(G_temp, right).getValue();
// step 2: now, perform subtype check reusing existing logic
return ts.subtypeSucceeded(G, leftSubst, rightSubst);
}
Aggregations