adapt2 rule implemented

This commit is contained in:
Aldaron7 2018-05-12 14:59:48 +02:00
parent 6f85c2b376
commit d9ab487253
2 changed files with 78 additions and 5 deletions

View File

@ -8,10 +8,14 @@ import java.util.Set;
import java.util.stream.Collectors;
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
import de.dhbwstuttgart.typeinference.unify.MartelliMontanariUnify;
import de.dhbwstuttgart.typeinference.unify.RuleSet;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
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.TypeParams;
import de.dhbwstuttgart.typeinference.unify.model.Unifier;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
import javafx.util.Pair;
@ -27,8 +31,77 @@ public class RuleSetStrucType extends RuleSet {
}
public Optional<Set<UnifyPair>> adapt2(Set<UnifyPair> pairs, IFiniteClosure fc) {
// TODO
return null;
// Filter SMALLERDOT Pairs
final Set<UnifyPair> pairsSmallerDot = pairs.stream().filter(p -> PairOperator.SMALLERDOT.equals(p.getPairOp()))
.collect(Collectors.toSet());
List<Pair<UnifyPair, UnifyPair>> tupleOfPotentialLinkings = new ArrayList<>();
for (UnifyPair pair : pairsSmallerDot) {
// check outerLeftPair pair has Form (D<theta.1,..,theta.n> <. TPH S1)
UnifyType typeD = pair.getLhsType();
if (!(pair.getRhsType() instanceof PlaceholderType) || !(typeD instanceof ReferenceType)) {
continue;
}
// filter potential outer Pairs for Linking
pairsSmallerDot.stream()
// check outerRightPair p has Form (TPH Sn <. D'<theta'.1,...,theta'.m>)
.filter(p -> (p.getLhsType() instanceof PlaceholderType)
&& (p.getRhsType() instanceof ReferenceType))
.filter(p -> {
UnifyType typeD1 = p.getRhsType();
// check ! (D<theta.1,..,theta.n> <=* D'<theta'.1,..,theta'.m>)
if (fc.getAncestors(typeD).contains(typeD1))
return false;
Optional<UnifyType> opt = fc.getLeftHandedType(typeD.getName());
if (!opt.isPresent())
return false;
UnifyType typeDgen = opt.get();
Set<UnifyType> greater = fc.getAncestors(typeDgen);
opt = greater.stream().filter(x -> x.getName().equals(typeD1.getName())).findAny();
// check (D<T1,..,Tn> <=* D'<ty1',...,tym'>)
if (!opt.isPresent())
return false;
// UnifyType typeD1gen = opt.get();
return true;
}).map(p -> new Pair<>(pair, p)).forEach(tupleOfPotentialLinkings::add);
}
// Build Linking between the Pairs. Empty if no Linking is possible.
for (Pair<UnifyPair, UnifyPair> tuple : tupleOfPotentialLinkings) {
UnifyPair outerLeftPair = tuple.getKey();
UnifyPair outerRightPair = tuple.getValue();
Optional<Set<UnifyPair>> opt = StrucTypeUnifyUtils.linkPairs(outerLeftPair, outerRightPair,
pairsSmallerDot);
if (!opt.isPresent())
continue;
// Linking has been found
UnifyType typeD = outerLeftPair.getLhsType();
UnifyType typeD1 = outerRightPair.getRhsType();
// present already checked s.o.
UnifyType typeDgen = fc.getLeftHandedType(typeD.getName()).get();
// present already checked s.o.
UnifyType typeD1gen = fc.getAncestors(typeDgen).stream().filter(x -> x.getName().equals(typeD1.getName()))
.findAny().get();
TypeParams typeDParams = typeD.getTypeParams();
TypeParams typeDgenParams = typeDgen.getTypeParams();
Unifier unifD1gen = Unifier.identity();
for (int i = 0; i < typeDParams.size(); i++)
unifD1gen.add((PlaceholderType) typeDgenParams.get(i), typeDParams.get(i));
Optional<Unifier> sigmaopt = new MartelliMontanariUnify().unify(unifD1gen.apply(typeD1gen), typeD);
if (!sigmaopt.isPresent())
return Optional.empty();
// replace outerLeftPair.lhs and outerRightPair.rhs witch new Types
Unifier sigma = sigmaopt.get();
pairs.remove(outerLeftPair);
pairs.remove(outerRightPair);
pairs.add(UnifyTypeFactory.generateSmallerDotPair(sigma.apply(typeD), outerLeftPair.getRhsType()));
pairs.add(UnifyTypeFactory.generateSmallerDotPair(outerRightPair.getLhsType(), sigma.apply(typeD1)));
return Optional.of(pairs);
}
// No Linking found
return Optional.empty();
}
public boolean erase1(UnifyPair pair, IFiniteClosure fc) {
@ -60,7 +133,7 @@ public class RuleSetStrucType extends RuleSet {
continue;
}
// System.out.println("Try to find tuple of potential linkings for pair " + pair);
tupleOfPotentialLinkings.addAll(pairsSmallerDot.stream().filter(p -> {
pairsSmallerDot.stream().filter(p -> {
// System.out.println(" Pair pair: " + pair);
// System.out.println("Pair p: " + p);
// System.out.println("p.lhs instnceof PH: " + (p.getLhsType() instanceof PlaceholderType));
@ -69,7 +142,7 @@ public class RuleSetStrucType extends RuleSet {
}).map(p -> {
// System.out.println("Pair to map: " + p);
return new Pair<>(pair, p);
}).collect(Collectors.toList()));
}).forEach(tupleOfPotentialLinkings::add);
// System.out.println("tuple of potential linkings: " + tupleOfPotentialLinkings);
}
// Build Linking between the Pairs. Empty if no Linking is possible.

View File

@ -51,7 +51,7 @@ public class StrucTypeUnify {
continue;
}
// no rule applied
// none of the above rules applied
resultSet.add(pair);
}