From d9ab487253e1fe862517af0f2e70da33b1bb03d8 Mon Sep 17 00:00:00 2001 From: Aldaron7 Date: Sat, 12 May 2018 14:59:48 +0200 Subject: [PATCH] adapt2 rule implemented --- .../strucTypes/RuleSetStrucType.java | 81 ++++++++++++++++++- .../strucTypes/StrucTypeUnify.java | 2 +- 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/src/de/dhbwstuttgart/strucTypes/RuleSetStrucType.java b/src/de/dhbwstuttgart/strucTypes/RuleSetStrucType.java index f470f793..88b9d8dd 100644 --- a/src/de/dhbwstuttgart/strucTypes/RuleSetStrucType.java +++ b/src/de/dhbwstuttgart/strucTypes/RuleSetStrucType.java @@ -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> adapt2(Set pairs, IFiniteClosure fc) { - // TODO - return null; + // Filter SMALLERDOT Pairs + final Set pairsSmallerDot = pairs.stream().filter(p -> PairOperator.SMALLERDOT.equals(p.getPairOp())) + .collect(Collectors.toSet()); + List> tupleOfPotentialLinkings = new ArrayList<>(); + for (UnifyPair pair : pairsSmallerDot) { + // check outerLeftPair pair has Form (D <. 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') + .filter(p -> (p.getLhsType() instanceof PlaceholderType) + && (p.getRhsType() instanceof ReferenceType)) + .filter(p -> { + UnifyType typeD1 = p.getRhsType(); + // check ! (D <=* D') + if (fc.getAncestors(typeD).contains(typeD1)) + return false; + Optional opt = fc.getLeftHandedType(typeD.getName()); + if (!opt.isPresent()) + return false; + + UnifyType typeDgen = opt.get(); + Set greater = fc.getAncestors(typeDgen); + opt = greater.stream().filter(x -> x.getName().equals(typeD1.getName())).findAny(); + // check (D <=* D') + 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 tuple : tupleOfPotentialLinkings) { + UnifyPair outerLeftPair = tuple.getKey(); + UnifyPair outerRightPair = tuple.getValue(); + Optional> 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 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. diff --git a/src/de/dhbwstuttgart/strucTypes/StrucTypeUnify.java b/src/de/dhbwstuttgart/strucTypes/StrucTypeUnify.java index cfd87692..e8ab9ea2 100644 --- a/src/de/dhbwstuttgart/strucTypes/StrucTypeUnify.java +++ b/src/de/dhbwstuttgart/strucTypes/StrucTypeUnify.java @@ -51,7 +51,7 @@ public class StrucTypeUnify { continue; } - // no rule applied + // none of the above rules applied resultSet.add(pair); }