From 12abb0b34d1f1964679f31410b6763b399855230 Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Thu, 14 Apr 2016 14:54:59 +0200 Subject: [PATCH] reduced cases of cart product --- .../typeinference/unify/RuleSet.java | 53 +++++++++++++++++++ .../typeinference/unify/TypeUnify.java | 34 +++++++----- .../unify/interfaces/IRuleSet.java | 19 +++++++ 3 files changed, 94 insertions(+), 12 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java index a9f82eff..118c0925 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java @@ -765,4 +765,57 @@ public class RuleSet implements IRuleSet{ return Optional.of(result); } + + @Override + public Optional reduceTph(UnifyPair pair) { + if(pair.getPairOp() != PairOperator.SMALLERDOTWC) + return Optional.empty(); + + UnifyType lhsType = pair.getLhsType(); + UnifyType rhsType = pair.getRhsType(); + if(!(lhsType instanceof PlaceholderType) || !(rhsType instanceof ReferenceType)) + return Optional.empty(); + + return Optional.of(new UnifyPair(lhsType, rhsType, PairOperator.EQUALSDOT)); + } + + @Override + public Optional> reduceTphExt(UnifyPair pair) { + if(pair.getPairOp() != PairOperator.SMALLERDOTWC) + return Optional.empty(); + + UnifyType lhsType = pair.getLhsType(); + UnifyType rhsType = pair.getRhsType(); + if(!(lhsType instanceof ExtendsType) || !(rhsType instanceof PlaceholderType)) + return Optional.empty(); + + UnifyType freshTph = PlaceholderType.freshPlaceholder(); + UnifyType extendedType = ((ExtendsType)lhsType).getExtendedType(); + + Set result = new HashSet<>(); + result.add(new UnifyPair(rhsType, new ExtendsType(freshTph), PairOperator.EQUALSDOT)); + result.add(new UnifyPair(extendedType, freshTph, PairOperator.SMALLERDOT)); + + return Optional.of(result); + } + + @Override + public Optional> reduceTphSup(UnifyPair pair) { + if(pair.getPairOp() != PairOperator.SMALLERDOTWC) + return Optional.empty(); + + UnifyType lhsType = pair.getLhsType(); + UnifyType rhsType = pair.getRhsType(); + if(!(lhsType instanceof SuperType) || !(rhsType instanceof PlaceholderType)) + return Optional.empty(); + + UnifyType freshTph = PlaceholderType.freshPlaceholder(); + UnifyType superedType = ((SuperType)lhsType).getSuperedType(); + + Set result = new HashSet<>(); + 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/TypeUnify.java b/src/de/dhbwstuttgart/typeinference/unify/TypeUnify.java index 7f1c857b..84bc7522 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/TypeUnify.java +++ b/src/de/dhbwstuttgart/typeinference/unify/TypeUnify.java @@ -248,6 +248,9 @@ public class TypeUnify { opt = opt.isPresent() ? opt : rules.reduceWildcardUpLow(pair); opt = opt.isPresent() ? opt : rules.reduceWildcardLeft(pair); + // Reduce TPH + opt = opt.isPresent() ? opt : rules.reduceTph(pair); + // One of the rules has been applied if(opt.isPresent()) { swapAddOrErase(opt.get(), fc, eqQueue); @@ -261,6 +264,10 @@ public class TypeUnify { optSet = optSet.isPresent() ? optSet : rules.reduceSup(pair, fc); optSet = optSet.isPresent() ? optSet : rules.reduceEq(pair); + // ReduceTphExt, ReduceTphSup + optSet = optSet.isPresent() ? optSet : rules.reduceTphExt(pair); + optSet = optSet.isPresent() ? optSet : rules.reduceTphSup(pair); + // One of the rules has been applied if(optSet.isPresent()) { optSet.get().forEach(x -> swapAddOrErase(x, fc, eqQueue)); @@ -347,21 +354,24 @@ public class TypeUnify { else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType && rhsType instanceof SuperType) result.get(2).add(unifyCase3((PlaceholderType) lhsType, (SuperType) rhsType, fc)); + // Case 4 was replaced by an inference rule // Case 4: (a <.? Theta') - else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType) - result.get(3).add(unifyCase4((PlaceholderType) lhsType, rhsType, fc)); + //else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType) + // result.get(3).add(unifyCase4((PlaceholderType) lhsType, rhsType, fc)); // Case 5: (Theta <. a) else if(pairOp == PairOperator.SMALLERDOT && rhsType instanceof PlaceholderType) result.get(4).add(unifyCase5(lhsType, (PlaceholderType) rhsType, fc)); + // Case 6 was replaced by an inference rule. // Case 6: (? ext Theta <.? a) - else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof ExtendsType && rhsType instanceof PlaceholderType) - result.get(5).add(unifyCase6((ExtendsType) lhsType, (PlaceholderType) rhsType, fc)); + //else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof ExtendsType && rhsType instanceof PlaceholderType) + // result.get(5).add(unifyCase6((ExtendsType) lhsType, (PlaceholderType) rhsType, fc)); + // Case 7 was replaced by an inference rule // Case 7: (? sup Theta <.? a) - else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof SuperType && rhsType instanceof PlaceholderType) - result.get(6).add(unifyCase7((SuperType) lhsType, (PlaceholderType) rhsType, fc)); + //else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof SuperType && rhsType instanceof PlaceholderType) + // result.get(6).add(unifyCase7((SuperType) lhsType, (PlaceholderType) rhsType, fc)); // Case 8: (Theta <.? a) else if(pairOp == PairOperator.SMALLERDOTWC && rhsType instanceof PlaceholderType) @@ -402,8 +412,8 @@ public class TypeUnify { * TODO Optimierungsmöglichkeit: * * An dieser Stelle gibt es Raum für Optimierung. - * Z.B. resultiert (a <. C) durch Permutation der Parameter mit grArg und smaller in: - * (a = C, b' <.? ? extends Number) + * Z.B. resultiert (a <. C) durch Permutation der Parameter mit grArg und smaller in: + * (a = C, b' <.? ? extends Integer) * (a = C, b' <.? ? extends Integer) * (a = C, b' <.? Integer) * usw... @@ -472,8 +482,8 @@ public class TypeUnify { UnifyType thetaPrime = extThetaPrime.getExtendedType(); //for(UnifyType theta : fc.smArg(subThetaPrime)) { Set resultPrime = new HashSet<>(); - resultPrime.add(new UnifyPair(a, aPrime, PairOperator.EQUALSDOT)); - resultPrime.add(new UnifyPair(aPrime, thetaPrime, PairOperator.SMALLERDOT)); + //resultPrime.add(new UnifyPair(a, aPrime, PairOperator.EQUALSDOT)); + resultPrime.add(new UnifyPair(a, thetaPrime, PairOperator.SMALLERDOT)); result.add(resultPrime); resultPrime = new HashSet<>(); @@ -547,8 +557,8 @@ public class TypeUnify { UnifyType thetaPrime = subThetaPrime.getSuperedType(); //for(UnifyType theta : fc.smArg(subThetaPrime)) { Set resultPrime = new HashSet<>(); - resultPrime.add(new UnifyPair(a, aPrime, PairOperator.EQUALSDOT)); - resultPrime.add(new UnifyPair(thetaPrime, aPrime, PairOperator.SMALLERDOT)); + //resultPrime.add(new UnifyPair(a, aPrime, PairOperator.EQUALSDOT)); + resultPrime.add(new UnifyPair(thetaPrime, a, PairOperator.SMALLERDOT)); result.add(resultPrime); resultPrime = new HashSet<>(); diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java index 714cb989..62c9649f 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IRuleSet.java @@ -31,6 +31,25 @@ public interface IRuleSet { public Optional reduceWildcardUpLow(UnifyPair pair); public Optional reduceWildcardLeft(UnifyPair pair); + /* + * Additional Rules which replace cases of the cartesian product + */ + + /** + * Rule that replaces the fourth case of the cartesian product where (a <.? Theta) + */ + public Optional reduceTph(UnifyPair pair); + + /** + * Rule that replaces the sixth case of the cartesian product where (? ext Theta <.? a) + */ + public Optional> reduceTphExt(UnifyPair pair); + + /** + * Rule that replaces the fourth case of the cartesian product where (? sup Theta <.? a) + */ + public Optional> reduceTphSup(UnifyPair pair); + /* * FunN Rules */