From da49e425151cfab75d743080ae75ebe69ff5bdf1 Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Tue, 3 May 2016 13:42:14 +0200 Subject: [PATCH] fixed long running times for lambda tests --- .../typeinference/unify/RuleSet.java | 34 +++++++++++++------ .../typeinference/unify/TypeUnifyTask.java | 34 ++++++++++++++----- 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java index 118c0925..efa7f55e 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java @@ -10,20 +10,20 @@ import java.util.Set; import java.util.Stack; import java.util.stream.Collectors; -import junit.framework.Assert; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet; import de.dhbwstuttgart.typeinference.unify.model.ExtendsType; import de.dhbwstuttgart.typeinference.unify.model.FunNType; -import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.ReferenceType; import de.dhbwstuttgart.typeinference.unify.model.SuperType; -import de.dhbwstuttgart.typeinference.unify.model.UnifyType; -import de.dhbwstuttgart.typeinference.unify.model.WildcardType; import de.dhbwstuttgart.typeinference.unify.model.TypeParams; import de.dhbwstuttgart.typeinference.unify.model.Unifier; -import de.dhbwstuttgart.typeinference.unify.model.PairOperator; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; +import de.dhbwstuttgart.typeinference.unify.model.UnifyType; +import de.dhbwstuttgart.typeinference.unify.model.WildcardType; +import junit.framework.Assert; /** * Implementation of the type inference rules. @@ -789,12 +789,18 @@ public class RuleSet implements IRuleSet{ if(!(lhsType instanceof ExtendsType) || !(rhsType instanceof PlaceholderType)) return Optional.empty(); - UnifyType freshTph = PlaceholderType.freshPlaceholder(); UnifyType extendedType = ((ExtendsType)lhsType).getExtendedType(); + boolean isGen = extendedType instanceof PlaceholderType && !((PlaceholderType) extendedType).isGenerated(); + Set result = new HashSet<>(); - result.add(new UnifyPair(rhsType, new ExtendsType(freshTph), PairOperator.EQUALSDOT)); - result.add(new UnifyPair(extendedType, freshTph, PairOperator.SMALLERDOT)); + if(isGen) + result.add(new UnifyPair(rhsType, lhsType, PairOperator.EQUALSDOT)); + else { + UnifyType freshTph = PlaceholderType.freshPlaceholder(); + result.add(new UnifyPair(rhsType, new ExtendsType(freshTph), PairOperator.EQUALSDOT)); + result.add(new UnifyPair(extendedType, freshTph, PairOperator.SMALLERDOT)); + } return Optional.of(result); } @@ -809,12 +815,18 @@ public class RuleSet implements IRuleSet{ if(!(lhsType instanceof SuperType) || !(rhsType instanceof PlaceholderType)) return Optional.empty(); - UnifyType freshTph = PlaceholderType.freshPlaceholder(); UnifyType superedType = ((SuperType)lhsType).getSuperedType(); + boolean isGen = superedType instanceof PlaceholderType && !((PlaceholderType) superedType).isGenerated(); + Set result = new HashSet<>(); - result.add(new UnifyPair(rhsType, new SuperType(freshTph), PairOperator.EQUALSDOT)); - result.add(new UnifyPair(freshTph, superedType, PairOperator.SMALLERDOT)); + if(isGen) + result.add(new UnifyPair(rhsType, lhsType, PairOperator.EQUALSDOT)); + else { + UnifyType freshTph = PlaceholderType.freshPlaceholder(); + result.add(new UnifyPair(rhsType, new SuperType(freshTph), PairOperator.EQUALSDOT)); + result.add(new UnifyPair(freshTph, superedType, PairOperator.SMALLERDOT)); + } return Optional.of(result); } diff --git a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index aef441ba..178f7bc1 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -425,6 +425,13 @@ public class TypeUnifyTask extends RecursiveTask>> { protected Set> unifyCase1(PlaceholderType a, UnifyType thetaPrime, IFiniteClosure fc) { Set> result = new HashSet<>(); + boolean allGen = true; + for(UnifyType t : thetaPrime.getTypeParams()) + if(t instanceof PlaceholderType && !((PlaceholderType) t).isGenerated()) { + allGen = false; + break; + } + Set cs = fc.getAllTypesByName(thetaPrime.getName()); cs.add(thetaPrime); @@ -442,11 +449,7 @@ public class TypeUnifyTask extends RecursiveTask>> { candidateParams.add(fc.grArg(param)); for(TypeParams tp : permuteParams(candidateParams)) - try { thetaQPrimes.add(c.setTypeParams(tp)); - } catch(Exception e) { - System.out.println("f"); - } } for(UnifyType tqp : thetaQPrimes) { @@ -466,14 +469,16 @@ public class TypeUnifyTask extends RecursiveTask>> { for(UnifyType theta : smaller) { Set resultPrime = new HashSet<>(); - for(int i = 0; i < theta.getTypeParams().size(); i++) { + for(int i = 0; !allGen && i < theta.getTypeParams().size(); i++) { if(freshTphs.size()-1 < i) freshTphs.add(PlaceholderType.freshPlaceholder()); resultPrime.add(new UnifyPair(freshTphs.get(i), theta.getTypeParams().get(i), PairOperator.SMALLERDOTWC)); } - UnifyType freshTheta = theta.setTypeParams(new TypeParams(freshTphs.toArray(new UnifyType[0]))); - resultPrime.add(new UnifyPair(a, freshTheta, PairOperator.EQUALSDOT)); + if(allGen) + resultPrime.add(new UnifyPair(a, theta, PairOperator.EQUALSDOT)); + else + resultPrime.add(new UnifyPair(a, theta.setTypeParams(new TypeParams(freshTphs.toArray(new UnifyType[0]))), PairOperator.EQUALSDOT)); resultPrime.addAll(substitutionSet); result.add(resultPrime); } @@ -543,16 +548,27 @@ public class TypeUnifyTask extends RecursiveTask>> { */ protected Set> unifyCase5(UnifyType theta, PlaceholderType a, IFiniteClosure fc) { Set> result = new HashSet<>(); + + boolean allGen = true; + for(UnifyType t : theta.getTypeParams()) + if(t instanceof PlaceholderType && !((PlaceholderType) t).isGenerated()) { + allGen = false; + break; + } + for(UnifyType thetaS : fc.greater(theta)) { Set resultPrime = new HashSet<>(); UnifyType[] freshTphs = new UnifyType[thetaS.getTypeParams().size()]; - for(int i = 0; i < freshTphs.length; i++) { + for(int i = 0; !allGen && i < freshTphs.length; i++) { freshTphs[i] = PlaceholderType.freshPlaceholder(); resultPrime.add(new UnifyPair(thetaS.getTypeParams().get(i), freshTphs[i], PairOperator.SMALLERDOTWC)); } - resultPrime.add(new UnifyPair(a, thetaS.setTypeParams(new TypeParams(freshTphs)), PairOperator.EQUALSDOT)); + if(allGen) + resultPrime.add(new UnifyPair(a, thetaS, PairOperator.EQUALSDOT)); + else + resultPrime.add(new UnifyPair(a, thetaS.setTypeParams(new TypeParams(freshTphs)), PairOperator.EQUALSDOT)); result.add(resultPrime); }