reduced cases of cart product

This commit is contained in:
Florian Steurer 2016-04-14 14:54:59 +02:00
parent e26f87afba
commit 12abb0b34d
3 changed files with 94 additions and 12 deletions

View File

@ -765,4 +765,57 @@ public class RuleSet implements IRuleSet{
return Optional.of(result);
}
@Override
public Optional<UnifyPair> 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<Set<UnifyPair>> 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<UnifyPair> 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<Set<UnifyPair>> 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<UnifyPair> 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);
}
}

View File

@ -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<Integer>) durch Permutation der Parameter mit grArg und smaller in:
* (a = C<b'>, b' <.? ? extends Number)
* Z.B. resultiert (a <. C<? extends Integer>) durch Permutation der Parameter mit grArg und smaller in:
* (a = C<b'>, b' <.? ? extends Integer)
* (a = C<b'>, b' <.? ? extends Integer)
* (a = C<b'>, b' <.? Integer)
* usw...
@ -472,8 +482,8 @@ public class TypeUnify {
UnifyType thetaPrime = extThetaPrime.getExtendedType();
//for(UnifyType theta : fc.smArg(subThetaPrime)) {
Set<UnifyPair> 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<UnifyPair> 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<>();

View File

@ -31,6 +31,25 @@ public interface IRuleSet {
public Optional<UnifyPair> reduceWildcardUpLow(UnifyPair pair);
public Optional<UnifyPair> 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<UnifyPair> reduceTph(UnifyPair pair);
/**
* Rule that replaces the sixth case of the cartesian product where (? ext Theta <.? a)
*/
public Optional<Set<UnifyPair>> reduceTphExt(UnifyPair pair);
/**
* Rule that replaces the fourth case of the cartesian product where (? sup Theta <.? a)
*/
public Optional<Set<UnifyPair>> reduceTphSup(UnifyPair pair);
/*
* FunN Rules
*/