From baee0024e9b1670b7c15e978bad160042601a4be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Pl=C3=BCmicke?= Date: Thu, 5 Apr 2018 18:09:24 +0200 Subject: [PATCH] modified: ../../src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java Zu beginn von unify werden doppelte wildcard konstruktionen entfernt Variableneumbenennung bei smaller in Fall 1 weider entfernt modified: ../../src/de/dhbwstuttgart/typeinference/unify/interfaces/IMatch.java modified: ../../src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java modified: ../../src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java modified: ../../src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java In computeGreater unify durch match ersetzt modified: ../../src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java modified: ../../src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java modified: ../../src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java modified: ../../src/de/dhbwstuttgart/typeinference/unify/model/WildcardType.java Abfrage ob eine doppelte wildcard konstruktion vorhanden ist eingefuegt --- .../typeinference/unify/TypeUnifyTask.java | 39 ++++++++++++------- .../unify/interfaces/IMatch.java | 16 +++----- .../unify/interfaces/IUnify.java | 8 ++-- .../unify/model/ExtendsType.java | 5 ++- .../unify/model/FiniteClosure.java | 14 +++++-- .../unify/model/OrderingUnifyPair.java | 6 +-- .../typeinference/unify/model/UnifyPair.java | 4 ++ .../typeinference/unify/model/UnifyType.java | 4 ++ .../unify/model/WildcardType.java | 5 +++ 9 files changed, 64 insertions(+), 37 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index 06783d9c..3435edd3 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -104,6 +104,19 @@ public class TypeUnifyTask extends RecursiveTask>> { */ writeLog("Unifikation: " + eq.toString()); //eq = eq.stream().map(x -> {x.setVariance((byte)-1); return x;}).collect(Collectors.toCollection(HashSet::new)); + + /* + * ? extends ? extends Theta rausfiltern + */ + Set doubleExt = eq.stream().filter(x -> (x.doubleExtended())).map(x -> { x.setUndefinedPair(); return x;}) + .collect(Collectors.toCollection(HashSet::new)); + if (doubleExt.size() > 0) { + Set> ret = new HashSet<>(); + ret.add(doubleExt); + return ret; + } + + Set eq0 = applyTypeUnificationRules(eq, fc); /* @@ -301,7 +314,7 @@ public class TypeUnifyTask extends RecursiveTask>> { * Step 7: Filter empty sets; */ eqPrimePrimeSet = eqPrimePrimeSet.stream().filter(x -> isSolvedForm(x) || this.isUndefinedPairSet(x)).collect(Collectors.toCollection(HashSet::new)); - if (!eqPrimePrimeSet.isEmpty()) + if (!eqPrimePrimeSet.isEmpty() && !isUndefinedPairSetSet(eqPrimePrimeSet)) writeLog("Result " + eqPrimePrimeSet.toString()); return eqPrimePrimeSet; } @@ -469,17 +482,14 @@ public class TypeUnifyTask extends RecursiveTask>> { protected boolean isUndefinedPairSet(Set s) { - Falsch! - Optional res = s.stream().map(x -> x.isUndefinedPair()).reduce((x,y)-> (x == y)); - if (res.isPresent()) { return res.get(); } - else { return false; } + Boolean ret = s.stream().map(x -> x.isUndefinedPair()).reduce(true, (x,y)-> (x && y)); + return ret; } protected boolean isUndefinedPairSetSet(Set> s) { if (s.size() ==1) { - Optional res = s.stream().map(x -> isUndefinedPairSet(x)).reduce((x,y)-> (x == y)); - if (res.isPresent()) { return res.get(); } - else { return false; } + Boolean ret = isUndefinedPairSet(s.stream().findFirst().get()); + return ret; } return false; @@ -853,13 +863,14 @@ public class TypeUnifyTask extends RecursiveTask>> { for (UnifyType tq : thetaQs) { Set smaller = fc.smaller(unifier.apply(tq)); //eingefuegt PL 2018-03-29 Anfang ? ext. theta hinzufuegen - Set smaller_ext = smaller.stream() + Set smaller_ext = smaller.stream().filter(x -> !(x instanceof ExtendsType) && !(x instanceof SuperType)) .map(x -> { - BinaryOperator> combiner = (aa,b) -> { aa.putAll(b); return aa;}; - HashMap hm = x.getInvolvedPlaceholderTypes().stream() - .reduce(new HashMap(), - (aa, b)-> { aa.put(b,PlaceholderType.freshPlaceholder()); return aa; }, combiner); - return new ExtendsType (x.accept(new freshPlaceholder(), hm));}).collect(Collectors.toCollection(HashSet::new)); + //BinaryOperator> combiner = (aa,b) -> { aa.putAll(b); return aa;}; //Variablenumbenennung rausgenommen + //HashMap hm = x.getInvolvedPlaceholderTypes().stream() + // .reduce(new HashMap(), + // (aa, b)-> { aa.put(b,PlaceholderType.freshPlaceholder()); return aa; }, combiner); + return new ExtendsType (x);})//.accept(new freshPlaceholder(), hm));} + .collect(Collectors.toCollection(HashSet::new)); smaller.addAll(smaller_ext); //eingefuegt PL 2018-03-29 Ende ? ext. theta hinzufuegen for(UnifyType theta : smaller) { diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IMatch.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IMatch.java index 5e201dd7..ab88f33b 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IMatch.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IMatch.java @@ -13,23 +13,17 @@ import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; /** * Match * @author Martin Pluemicke + * abgeleitet aus IUnify.java */ public interface IMatch { /** - * Finds the most general unifier sigma of the set {t1,...,tn} so that - * sigma(t1) = sigma(t2) = ... = sigma(tn). - * @param terms The set of terms to be unified - * @return An optional of the most general unifier if it exists or an empty optional if there is no unifier. + * Finds the most general matcher sigma of the set {t1 =. t1',...,tn =. tn'} so that + * sigma(t1) = t1' , ... sigma(tn) = tn'. + * @param terms The set of terms to be matched + * @return An optional of the most general matcher if it exists or an empty optional if there is no matcher. */ public Optional match(ArrayList termsList); - - /** - * Finds the most general unifier sigma of the set {t1,...,tn} so that - * sigma(t1) = sigma(t2) = ... = sigma(tn). - * @param terms The set of terms to be unified - * @return An optional of the most general unifier if it exists or an empty optional if there is no unifier. - */ } diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java index 524608f5..57b41736 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java @@ -15,16 +15,16 @@ import de.dhbwstuttgart.typeinference.unify.model.Unifier; public interface IUnify { /** - * Finds the most general unifier sigma of the set {t1,...,tn} so that - * sigma(t1) = sigma(t2) = ... = sigma(tn). + * Finds the most general unifier sigma of the set {t1 =. t1',...,tn =. tn'} so that + * sigma(t1) = sigma(t1') , ... sigma(tn) = sigma(tn'). * @param terms The set of terms to be unified * @return An optional of the most general unifier if it exists or an empty optional if there is no unifier. */ public Optional unify(Set terms); /** - * Finds the most general unifier sigma of the set {t1,...,tn} so that - * sigma(t1) = sigma(t2) = ... = sigma(tn). + * Finds the most general unifier sigma of the set {t1 =. t1',...,tn =. tn'} so that + * sigma(t1) = sigma(t1') , ... sigma(tn) = sigma(tn'). * @param terms The set of terms to be unified * @return An optional of the most general unifier if it exists or an empty optional if there is no unifier. */ diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java b/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java index a7ff3af8..6c44d2ec 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/ExtendsType.java @@ -22,7 +22,10 @@ public final class ExtendsType extends WildcardType { * @param extendedType The extended type e.g. Integer in "? extends Integer" */ public ExtendsType(UnifyType extendedType) { - super("? extends " + extendedType.getName(), extendedType); + super("? extends " + extendedType.getName(), extendedType); + if (extendedType instanceof ExtendsType) { + System.out.print(""); + } } /** diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java index 2e4e7bc3..4aa4ffb8 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java @@ -10,8 +10,8 @@ import java.util.stream.Collectors; import com.google.common.collect.Ordering; -//PL 18-02-05 Unifier durch Matcher ersetzt -//mus greater noch erstezt werden +//PL 18-02-05/18-04-05 Unifier durch Matcher ersetzt +//muss greater noch ersetzt werden ja erledigt 18--04-05 import de.dhbwstuttgart.typeinference.unify.MartelliMontanariUnify; import de.dhbwstuttgart.typeinference.unify.Match; @@ -196,7 +196,9 @@ public class FiniteClosure extends Ordering implements IFiniteClosure protected Set computeGreater(Set types) { HashSet result = new HashSet<>(); - IUnify unify = new MartelliMontanariUnify(); + //PL 18-04-05 Unifier durch Matcher ersetzt + //IUnify unify = new MartelliMontanariUnify(); + Match match = new Match(); for(UnifyType t : types) { @@ -218,7 +220,11 @@ public class FiniteClosure extends Ordering implements IFiniteClosure Set> candidates = strInheritanceGraph.get(t.getName()); for(Node candidate : candidates) { UnifyType theta1 = candidate.getContent(); - Optional optSigma = unify.unify(theta1, t); + //PL 18-04-05 Unifier durch Matcher ersetzt ANFANG + ArrayList termList= new ArrayList(); + termList.add(new UnifyPair(theta1,t, PairOperator.EQUALSDOT)); + Optional optSigma = match.match(termList); + //PL 18-04-05 Unifier durch Matcher ersetzt ENDE if(!optSigma.isPresent()) continue; diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java b/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java index 505c3c2b..9a018fb0 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java @@ -97,9 +97,9 @@ public class OrderingUnifyPair extends Ordering> { .collect(Collectors.toCollection(HashSet::new)); //System.out.println(left.toString()); //Fall 2 und 3 - if (lefteq.iterator().next().getLhsType().getName().equals("AJO")) { - System.out.print(""); - } + //if (lefteq.iterator().next().getLhsType().getName().equals("AJO")) { + // System.out.print(""); + //} if (lefteq.size() == 1 && leftle.size() == 1 && righteq.size() == 0 && rightle.size() == 1) { return 1; } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java index b3d0c85a..dd24c28d 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java @@ -134,6 +134,10 @@ public class UnifyPair { return ret; } + public Boolean doubleExtended() { + return lhs.doubleExtended() || rhs.doubleExtended(); + } + @Override public boolean equals(Object obj) { if(!(obj instanceof UnifyPair)) diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java index 555083e8..5beef2bf 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java @@ -102,6 +102,10 @@ public abstract class UnifyType { ret.addAll(typeParams.getInvolvedPlaceholderTypes()); return ret; } + + public Boolean doubleExtended() {//default + return false; + } @Override public int hashCode() { diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/WildcardType.java b/src/de/dhbwstuttgart/typeinference/unify/model/WildcardType.java index 2bc51460..e320bb9f 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/WildcardType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/WildcardType.java @@ -40,6 +40,11 @@ public abstract class WildcardType extends UnifyType { return wildcardedType.getTypeParams(); } + @Override + public Boolean doubleExtended () {//This is an error + return (wildcardedType instanceof WildcardType); + } + @Override public int hashCode() { return wildcardedType.hashCode() + getName().hashCode() + 17;