public static ReductionResult withConstraints(List<ConstraintFormula> constraints) { ReductionResult reductionResult = new ReductionResult(); reductionResult.constraintFormulas.addAll(constraints); return reductionResult; }
/** * Takes a compatibility assertion about an expression or type, called a constraint formula, and reduces it to a * set of bounds on inference variables. Often, a constraint formula reduces to other constraint formulas, * which must be recursively reduced. A procedure is followed to identify these additional constraint formulas and, * ultimately, to express via a bound set the conditions under which the choices for inferred types would render * each constraint formula true. */ public BoundSet reduce(TypeSolver typeSolver) { List<ConstraintFormula> constraints = new LinkedList<>(constraintFormulas); BoundSet boundSet = BoundSet.empty(); while (constraints.size() > 0) { ConstraintFormula constraintFormula = constraints.remove(0); ConstraintFormula.ReductionResult reductionResult = constraintFormula.reduce(boundSet); constraints.addAll(reductionResult.getConstraintFormulas()); boundSet.incorporate(reductionResult.getBoundSet(), typeSolver); } return boundSet; }
return ReductionResult.trueResult();
return ReductionResult.trueResult(); } else { return ReductionResult.falseResult(); return ReductionResult.empty().withConstraint(new TypeCompatibleWithType(typeSolver, s, T)); return ReductionResult.oneConstraint(new ExpressionCompatibleWithType(typeSolver, enclosedExpr.getInner(), T)); return ReductionResult.bounds(B3); return ReductionResult.withConstraints( new ExpressionCompatibleWithType(typeSolver, conditionalExpr.getThenExpr(), T), new ExpressionCompatibleWithType(typeSolver, conditionalExpr.getElseExpr(), T)); return ReductionResult.falseResult(); return ReductionResult.falseResult(); return ReductionResult.falseResult(); for (Expression e : resultExpressions) { if (!ExpressionHelper.isCompatibleInAssignmentContext(e, R, typeSolver)) { return ReductionResult.falseResult(); Expression e = ((ExpressionStmt)lambdaExpr.getBody()).getExpression(); if (!ExpressionHelper.isCompatibleInAssignmentContext(e, R, typeSolver)) { return ReductionResult.falseResult(); return ReductionResult.withConstraints(constraints);
return ReductionResult.trueResult(); } else { return ReductionResult.falseResult(); return ReductionResult.empty().withConstraint(new TypeCompatibleWithType(typeSolver, s, T)); return ReductionResult.oneConstraint(new ExpressionCompatibleWithType(typeSolver, enclosedExpr.getInner(), T)); return ReductionResult.bounds(B3); return ReductionResult.withConstraints( new ExpressionCompatibleWithType(typeSolver, conditionalExpr.getThenExpr(), T), new ExpressionCompatibleWithType(typeSolver, conditionalExpr.getElseExpr(), T)); return ReductionResult.falseResult(); return ReductionResult.falseResult(); return ReductionResult.falseResult(); for (Expression e : resultExpressions) { if (!ExpressionHelper.isCompatibleInAssignmentContext(e, R, typeSolver)) { return ReductionResult.falseResult(); Expression e = ((ExpressionStmt)lambdaExpr.getBody()).getExpression(); if (!ExpressionHelper.isCompatibleInAssignmentContext(e, R, typeSolver)) { return ReductionResult.falseResult(); return ReductionResult.withConstraints(constraints);
return ReductionResult.trueResult(); } else { return ReductionResult.falseResult(); return ReductionResult.empty().withConstraint(new TypeCompatibleWithType(typeSolver, s, T)); return ReductionResult.oneConstraint(new ExpressionCompatibleWithType(typeSolver, enclosedExpr.getInner(), T)); return ReductionResult.bounds(B3); return ReductionResult.withConstraints( new ExpressionCompatibleWithType(typeSolver, conditionalExpr.getThenExpr(), T), new ExpressionCompatibleWithType(typeSolver, conditionalExpr.getElseExpr(), T)); return ReductionResult.falseResult(); return ReductionResult.falseResult(); return ReductionResult.falseResult(); for (Expression e : resultExpressions) { if (!ExpressionHelper.isCompatibleInAssignmentContext(e, R, typeSolver)) { return ReductionResult.falseResult(); Expression e = ((ExpressionStmt)lambdaExpr.getBody()).getExpression(); if (!ExpressionHelper.isCompatibleInAssignmentContext(e, R, typeSolver)) { return ReductionResult.falseResult(); return ReductionResult.withConstraints(constraints);
return ReductionResult.trueResult(); } else { return ReductionResult.falseResult(); return ReductionResult.trueResult(); return ReductionResult.falseResult(); return ReductionResult.oneBound(new SubtypeOfBound(S, T)); return ReductionResult.oneBound(new SubtypeOfBound(S, T)); return ReductionResult.oneConstraint(new TypeSubtypeOfType(typeSolver, S, T.asTypeVariable().asTypeParameter().getLowerBound())); return ReductionResult.falseResult();
return ReductionResult.trueResult(); } else { return ReductionResult.falseResult(); return ReductionResult.trueResult(); return ReductionResult.falseResult(); return ReductionResult.oneBound(new SubtypeOfBound(S, T)); return ReductionResult.oneBound(new SubtypeOfBound(S, T)); return ReductionResult.oneConstraint(new TypeSubtypeOfType(typeSolver, S, T.asTypeVariable().asTypeParameter().getLowerBound())); return ReductionResult.falseResult();
return ReductionResult.trueResult(); } else { return ReductionResult.falseResult(); return ReductionResult.falseResult(); return ReductionResult.oneBound(new SameAsBound(S, T)); return ReductionResult.oneBound(new SameAsBound(S, T)); ReductionResult res = ReductionResult.empty(); List<ResolvedType> Bs = S.asReferenceType().typeParametersValues(); List<ResolvedType> As = T.asReferenceType().typeParametersValues(); for (int i = 0; i < Bs.size(); i++) { res = res.withConstraint(new TypeSameAsType(Bs.get(i), As.get(i))); return ReductionResult.oneConstraint(new TypeSameAsType( S.asArrayType().getComponentType(), T.asArrayType().getComponentType())); return ReductionResult.falseResult();
return ReductionResult.trueResult(); } else { return ReductionResult.falseResult(); return ReductionResult.trueResult(); return ReductionResult.falseResult(); return ReductionResult.oneBound(new SubtypeOfBound(S, T)); return ReductionResult.oneBound(new SubtypeOfBound(S, T)); return ReductionResult.oneConstraint(new TypeSubtypeOfType(typeSolver, S, T.asTypeVariable().asTypeParameter().getLowerBound())); return ReductionResult.falseResult();
return ReductionResult.trueResult(); } else { return ReductionResult.falseResult(); return ReductionResult.falseResult(); return ReductionResult.oneBound(new SameAsBound(S, T)); return ReductionResult.oneBound(new SameAsBound(S, T)); ReductionResult res = ReductionResult.empty(); List<ResolvedType> Bs = S.asReferenceType().typeParametersValues(); List<ResolvedType> As = T.asReferenceType().typeParametersValues(); for (int i = 0; i < Bs.size(); i++) { res = res.withConstraint(new TypeSameAsType(Bs.get(i), As.get(i))); return ReductionResult.oneConstraint(new TypeSameAsType( S.asArrayType().getComponentType(), T.asArrayType().getComponentType())); return ReductionResult.falseResult();
return ReductionResult.trueResult(); } else { return ReductionResult.falseResult(); return ReductionResult.falseResult(); return ReductionResult.oneBound(new SameAsBound(S, T)); return ReductionResult.oneBound(new SameAsBound(S, T)); ReductionResult res = ReductionResult.empty(); List<ResolvedType> Bs = S.asReferenceType().typeParametersValues(); List<ResolvedType> As = T.asReferenceType().typeParametersValues(); for (int i = 0; i < Bs.size(); i++) { res = res.withConstraint(new TypeSameAsType(Bs.get(i), As.get(i))); return ReductionResult.oneConstraint(new TypeSameAsType( S.asArrayType().getComponentType(), T.asArrayType().getComponentType())); return ReductionResult.falseResult();
return ReductionResult.trueResult(); } else { return ReductionResult.falseResult(); ReflectionTypeSolver typeSolver = new ReflectionTypeSolver(); ResolvedType sFirst = new ReferenceTypeImpl(typeSolver.solveType(s.asPrimitive().getBoxTypeQName()), typeSolver); return ReductionResult.oneConstraint(new TypeCompatibleWithType(typeSolver, sFirst, t)); ReflectionTypeSolver typeSolver = new ReflectionTypeSolver(); ResolvedType tFirst = new ReferenceTypeImpl(typeSolver.solveType(t.asPrimitive().getBoxTypeQName()), typeSolver); return ReductionResult.oneConstraint(new TypeSameAsType(s, tFirst)); return ReductionResult.trueResult(); return ReductionResult.empty().withConstraint(new TypeSubtypeOfType(typeSolver, s, t));
return ReductionResult.trueResult(); } else { return ReductionResult.falseResult(); ReflectionTypeSolver typeSolver = new ReflectionTypeSolver(); ResolvedType sFirst = new ReferenceTypeImpl(typeSolver.solveType(s.asPrimitive().getBoxTypeQName()), typeSolver); return ReductionResult.oneConstraint(new TypeCompatibleWithType(typeSolver, sFirst, t)); ReflectionTypeSolver typeSolver = new ReflectionTypeSolver(); ResolvedType tFirst = new ReferenceTypeImpl(typeSolver.solveType(t.asPrimitive().getBoxTypeQName()), typeSolver); return ReductionResult.oneConstraint(new TypeSameAsType(s, tFirst)); return ReductionResult.trueResult(); return ReductionResult.empty().withConstraint(new TypeSubtypeOfType(typeSolver, s, t));
return ReductionResult.trueResult(); } else { return ReductionResult.falseResult(); ReflectionTypeSolver typeSolver = new ReflectionTypeSolver(); ResolvedType sFirst = new ReferenceTypeImpl(typeSolver.solveType(s.asPrimitive().getBoxTypeQName()), typeSolver); return ReductionResult.oneConstraint(new TypeCompatibleWithType(typeSolver, sFirst, t)); ReflectionTypeSolver typeSolver = new ReflectionTypeSolver(); ResolvedType tFirst = new ReferenceTypeImpl(typeSolver.solveType(t.asPrimitive().getBoxTypeQName()), typeSolver); return ReductionResult.oneConstraint(new TypeSameAsType(s, tFirst)); return ReductionResult.trueResult(); return ReductionResult.empty().withConstraint(new TypeSubtypeOfType(typeSolver, s, t));
return ReductionResult.trueResult();
return ReductionResult.trueResult();
/** * From JLS 18.1.2 * * From Collections.singleton("hi"), we have the constraint formula ‹"hi" → α›. * Through reduction, this will become the constraint formula: ‹String <: α›. */ @Test public void testExpressionCompatibleWithTypeReduce1() { ResolvedTypeParameterDeclaration tp = EasyMock.createMock(ResolvedTypeParameterDeclaration.class); Expression e = new StringLiteralExpr("hi"); InferenceVariable inferenceVariable = new InferenceVariable("α", tp); ExpressionCompatibleWithType formula = new ExpressionCompatibleWithType(typeSolver, e, inferenceVariable); ConstraintFormula.ReductionResult res1 = formula.reduce(BoundSet.empty()); assertEquals( ConstraintFormula.ReductionResult.empty().withConstraint(new TypeCompatibleWithType(typeSolver, stringType, inferenceVariable)), res1); assertEquals( ConstraintFormula.ReductionResult.empty().withConstraint(new TypeSubtypeOfType(typeSolver, stringType, inferenceVariable)), res1.getConstraint(0).reduce(BoundSet.empty())); }
/** * Takes a compatibility assertion about an expression or type, called a constraint formula, and reduces it to a * set of bounds on inference variables. Often, a constraint formula reduces to other constraint formulas, * which must be recursively reduced. A procedure is followed to identify these additional constraint formulas and, * ultimately, to express via a bound set the conditions under which the choices for inferred types would render * each constraint formula true. */ public BoundSet reduce(TypeSolver typeSolver) { List<ConstraintFormula> constraints = new LinkedList<>(constraintFormulas); BoundSet boundSet = BoundSet.empty(); while (constraints.size() > 0) { ConstraintFormula constraintFormula = constraints.remove(0); ConstraintFormula.ReductionResult reductionResult = constraintFormula.reduce(boundSet); constraints.addAll(reductionResult.getConstraintFormulas()); boundSet.incorporate(reductionResult.getBoundSet(), typeSolver); } return boundSet; }
/** * Takes a compatibility assertion about an expression or type, called a constraint formula, and reduces it to a * set of bounds on inference variables. Often, a constraint formula reduces to other constraint formulas, * which must be recursively reduced. A procedure is followed to identify these additional constraint formulas and, * ultimately, to express via a bound set the conditions under which the choices for inferred types would render * each constraint formula true. */ public BoundSet reduce(TypeSolver typeSolver) { List<ConstraintFormula> constraints = new LinkedList<>(constraintFormulas); BoundSet boundSet = BoundSet.empty(); while (constraints.size() > 0) { ConstraintFormula constraintFormula = constraints.remove(0); ConstraintFormula.ReductionResult reductionResult = constraintFormula.reduce(boundSet); constraints.addAll(reductionResult.getConstraintFormulas()); boundSet.incorporate(reductionResult.getBoundSet(), typeSolver); } return boundSet; }