From b5b5b5d9c9f5422df79bed63b4b3f10a998988cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Pl=C3=BCmicke?= Date: Fri, 9 Mar 2018 01:23:02 +0100 Subject: [PATCH] modified: ../../src/de/dhbwstuttgart/typeinference/constraints/Pair.java modified: ../../src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java modified: ../../src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java Varinace in UnfyPair eingefuegt und Abfrage in computeCartesianRecursive eingefuegt Pruefen, ob Varianve bei Subst und andrene erhalten bleibt. --- .../typeinference/constraints/Pair.java | 1 + .../typeinference/unify/TypeUnifyTask.java | 93 ++++++++++++++----- .../typeinference/unify/model/UnifyPair.java | 9 ++ 3 files changed, 81 insertions(+), 22 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/constraints/Pair.java b/src/de/dhbwstuttgart/typeinference/constraints/Pair.java index ab0cb3ea..eb322274 100644 --- a/src/de/dhbwstuttgart/typeinference/constraints/Pair.java +++ b/src/de/dhbwstuttgart/typeinference/constraints/Pair.java @@ -11,6 +11,7 @@ public class Pair implements Serializable public final RefTypeOrTPHOrWildcardOrGeneric TA2; private PairOperator eOperator = PairOperator.SMALLER; + public Pair(RefTypeOrTPHOrWildcardOrGeneric TA1, RefTypeOrTPHOrWildcardOrGeneric TA2 ) { diff --git a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index 9ebea45e..4f422d12 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -312,18 +312,42 @@ public class TypeUnifyTask extends RecursiveTask>> { ArrayList> nextSetasList = new ArrayList<>(nextSet); Set> result = new HashSet<>(); int i = 0; - Set a_next; - if (nextSetasList.size()>1) - a_next = oup.min(nextSetasList.iterator()); - else a_next = nextSetasList.iterator().next(); + byte variance = nextSetasList.iterator().next().iterator().next().getVariance(); + Set a_next = null; + if (nextSetasList.size()>1) { + if (variance == 1) { + a_next = oup.max(nextSetasList.iterator()); + } + else if (variance == -1) { + a_next = oup.min(nextSetasList.iterator()); + } + else if (variance == 0) { + a_next = nextSetasList.iterator().next(); + } + } + else { + a_next = nextSetasList.iterator().next(); + } while (nextSetasList.size() != 0) { Set a = a_next; //writeLog("nextSet: " + nextSetasList.toString()+ "\n"); - nextSetasList.remove(a); - if (nextSetasList.size() > 0) - if (nextSetasList.size()>1) - a_next = oup.min(nextSetasList.iterator()); - else a_next = nextSetasList.iterator().next(); + nextSetasList.remove(a); + if (nextSetasList.size() > 0) { + if (nextSetasList.size()>1) { + if (variance == 1) { + a_next = oup.max(nextSetasList.iterator()); + } + else if (variance == -1) { + a_next = oup.min(nextSetasList.iterator()); + } + else { + a_next = nextSetasList.iterator().next(); + } + } + else { + a_next = nextSetasList.iterator().next(); + } + } //PL 2018-03-01 //TODO: 1. Maximum und Minimum unterscheiden //TODO: 2. compare noch für alle Elmemente die nicht X =. ty sind erweitern @@ -338,10 +362,30 @@ public class TypeUnifyTask extends RecursiveTask>> { else { result.addAll(computeCartesianRecursive(elems, remainingSets, eq, fc, parallel)); } - if (!result.isEmpty()) //&& (oup.compare(a, a_next) == -1)) break; - if (a.equals(a_next) || - (oup.compare(a, a_next) == -1)) + if (!result.isEmpty()) { + if (variance == 1) { + if (a.equals(a_next) || (oup.compare(a, a_next) == 1)) { + System.out.print(""); break; + } + else { + System.out.print(""); + } + } + else { if (variance == -1) { + if (a.equals(a_next) || (oup.compare(a, a_next) == -1)) { + System.out.print(""); + break; + } + else { + System.out.print(""); + } + } + else if (variance == 0) { + break; + } + } + } } return result; } @@ -528,7 +572,7 @@ public class TypeUnifyTask extends RecursiveTask>> { if(pairOp == PairOperator.SMALLERDOT && lhsType instanceof PlaceholderType) { //System.out.println(pair); if (first) { //writeLog(pair.toString()+"\n"); - Set> x1 = unifyCase1((PlaceholderType) pair.getLhsType(), pair.getRhsType(), fc); + Set> x1 = unifyCase1((PlaceholderType) pair.getLhsType(), pair.getRhsType(), pair.getVariance(), fc); //System.out.println(x1); result.get(0).add(x1); } @@ -544,7 +588,7 @@ public class TypeUnifyTask extends RecursiveTask>> { // Case 2: (a <.? ? ext Theta') else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType && rhsType instanceof ExtendsType) if (first) { //writeLog(pair.toString()+"\n"); - result.get(1).add(unifyCase2((PlaceholderType) pair.getLhsType(), (ExtendsType) pair.getRhsType(), fc)); + result.get(1).add(unifyCase2((PlaceholderType) pair.getLhsType(), (ExtendsType) pair.getRhsType(), pair.getVariance(), fc)); } else { Set s1 = new HashSet<>(); @@ -557,7 +601,7 @@ public class TypeUnifyTask extends RecursiveTask>> { // Case 3: (a <.? ? sup Theta') else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType && rhsType instanceof SuperType) if (first) { //writeLog(pair.toString()+"\n"); - result.get(2).add(unifyCase3((PlaceholderType) lhsType, (SuperType) rhsType, fc)); + result.get(2).add(unifyCase3((PlaceholderType) lhsType, (SuperType) rhsType, pair.getVariance(), fc)); } else { Set s1 = new HashSet<>(); @@ -575,7 +619,7 @@ public class TypeUnifyTask extends RecursiveTask>> { // Case 5: (Theta <. a) else if(pairOp == PairOperator.SMALLERDOT && rhsType instanceof PlaceholderType) if (first) { //writeLog(pair.toString()+"\n"); - result.get(4).add(unifyCase5(lhsType, (PlaceholderType) rhsType, fc)); + result.get(4).add(unifyCase5(lhsType, (PlaceholderType) rhsType, pair.getVariance(), fc)); } else { Set s1 = new HashSet<>(); @@ -599,7 +643,7 @@ public class TypeUnifyTask extends RecursiveTask>> { else if(pairOp == PairOperator.SMALLERDOTWC && rhsType instanceof PlaceholderType) if (first) { //writeLog(pair.toString()+"\n"); result.get(7).add( - unifyCase8(lhsType, (PlaceholderType) rhsType, fc)); + unifyCase8(lhsType, (PlaceholderType) rhsType, pair.getVariance(), fc)); } else { Set s1 = new HashSet<>(); @@ -627,7 +671,7 @@ public class TypeUnifyTask extends RecursiveTask>> { /** * Cartesian product Case 1: (a <. Theta') */ - protected Set> unifyCase1(PlaceholderType a, UnifyType thetaPrime, IFiniteClosure fc) { + protected Set> unifyCase1(PlaceholderType a, UnifyType thetaPrime, byte variance, IFiniteClosure fc) { Set> result = new HashSet<>(); boolean allGen = thetaPrime.getTypeParams().size() > 0; @@ -704,6 +748,7 @@ public class TypeUnifyTask extends RecursiveTask>> { resultPrime.add(new UnifyPair(a, theta.setTypeParams(new TypeParams(freshTphs.toArray(new UnifyType[0]))), PairOperator.EQUALSDOT)); resultPrime.addAll(substitutionSet); //writeLog("Substitution: " + substitutionSet.toString()); + resultPrime = resultPrime.stream().map(x -> { x.setVariance(variance); return x;}).collect(Collectors.toCollection(HashSet::new)); result.add(resultPrime); //writeLog("Result: " + resultPrime.toString()); //writeLog("MAX: " + oup.max(resultPrime.iterator()).toString()); @@ -719,7 +764,7 @@ public class TypeUnifyTask extends RecursiveTask>> { /** * Cartesian Product Case 2: (a <.? ? ext Theta') */ - private Set> unifyCase2(PlaceholderType a, ExtendsType extThetaPrime, IFiniteClosure fc) { + private Set> unifyCase2(PlaceholderType a, ExtendsType extThetaPrime, byte variance, IFiniteClosure fc) { Set> result = new HashSet<>(); UnifyType aPrime = PlaceholderType.freshPlaceholder(); @@ -732,6 +777,7 @@ public class TypeUnifyTask extends RecursiveTask>> { resultPrime = new HashSet<>(); resultPrime.add(new UnifyPair(a, extAPrime, PairOperator.EQUALSDOT)); resultPrime.add(new UnifyPair(aPrime, thetaPrime, PairOperator.SMALLERDOT)); + resultPrime = resultPrime.stream().map(x -> { x.setVariance(variance); return x;}).collect(Collectors.toCollection(HashSet::new)); result.add(resultPrime); //writeLog("Result: " + resultPrime.toString()); return result; @@ -740,7 +786,7 @@ public class TypeUnifyTask extends RecursiveTask>> { /** * Cartesian Product Case 3: (a <.? ? sup Theta') */ - private Set> unifyCase3(PlaceholderType a, SuperType subThetaPrime, IFiniteClosure fc) { + private Set> unifyCase3(PlaceholderType a, SuperType subThetaPrime, byte variance, IFiniteClosure fc) { Set> result = new HashSet<>(); UnifyType aPrime = PlaceholderType.freshPlaceholder(); @@ -754,6 +800,7 @@ public class TypeUnifyTask extends RecursiveTask>> { resultPrime = new HashSet<>(); resultPrime.add(new UnifyPair(a, supAPrime, PairOperator.EQUALSDOT)); resultPrime.add(new UnifyPair(thetaPrime, aPrime, PairOperator.SMALLERDOT)); + resultPrime = resultPrime.stream().map(x -> { x.setVariance(variance); return x;}).collect(Collectors.toCollection(HashSet::new)); result.add(resultPrime); //writeLog(resultPrime.toString()); @@ -763,7 +810,7 @@ public class TypeUnifyTask extends RecursiveTask>> { /** * Cartesian Product Case 5: (Theta <. a) */ - private Set> unifyCase5(UnifyType theta, PlaceholderType a, IFiniteClosure fc) { + private Set> unifyCase5(UnifyType theta, PlaceholderType a, byte variance, IFiniteClosure fc) { Set> result = new HashSet<>(); boolean allGen = theta.getTypeParams().size() > 0; @@ -786,6 +833,7 @@ public class TypeUnifyTask extends RecursiveTask>> { resultPrime.add(new UnifyPair(a, thetaS, PairOperator.EQUALSDOT)); else resultPrime.add(new UnifyPair(a, thetaS.setTypeParams(new TypeParams(freshTphs)), PairOperator.EQUALSDOT)); + resultPrime = resultPrime.stream().map(x -> { x.setVariance(variance); return x;}).collect(Collectors.toCollection(HashSet::new)); result.add(resultPrime); //writeLog(resultPrime.toString()); } @@ -796,7 +844,7 @@ public class TypeUnifyTask extends RecursiveTask>> { /** * Cartesian Product Case 8: (Theta <.? a) */ - private Set> unifyCase8(UnifyType theta, PlaceholderType a, IFiniteClosure fc) { + private Set> unifyCase8(UnifyType theta, PlaceholderType a, byte variance, IFiniteClosure fc) { Set> result = new HashSet<>(); //for(UnifyType thetaS : fc.grArg(theta)) { Set resultPrime = new HashSet<>(); @@ -814,6 +862,7 @@ public class TypeUnifyTask extends RecursiveTask>> { resultPrime = new HashSet<>(); resultPrime.add(new UnifyPair(a, new SuperType(freshTph), PairOperator.EQUALSDOT)); resultPrime.add(new UnifyPair(freshTph, theta, PairOperator.SMALLERDOT)); + resultPrime = resultPrime.stream().map(x -> { x.setVariance(variance); return x;}).collect(Collectors.toCollection(HashSet::new)); result.add(resultPrime); //writeLog(resultPrime.toString()); //} diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java index 8d038fd3..abb7651c 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java @@ -26,6 +26,8 @@ public class UnifyPair { */ private PairOperator pairOp; + private byte variance = 0; + private final int hashCode; /** @@ -64,6 +66,13 @@ public class UnifyPair { return pairOp; } + public byte getVariance() { + return variance; + } + + public void setVariance(byte v) { + variance = v; + } @Override public boolean equals(Object obj) { if(!(obj instanceof UnifyPair))