diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 1790a195..74c080e4 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -24,6 +24,7 @@ import de.dhbwstuttgart.typeinference.result.ResultSet; import de.dhbwstuttgart.typeinference.typeAlgo.TYPE; import de.dhbwstuttgart.typeinference.unify.RuleSet; import de.dhbwstuttgart.typeinference.unify.TypeUnify; +import de.dhbwstuttgart.typeinference.unify.distributeVariance; import de.dhbwstuttgart.typeinference.unify.inheritVariance; import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; @@ -166,6 +167,7 @@ public class JavaTXCompiler { } return y; } ) .collect(Collectors.toCollection(HashSet::new)); + varianceInheritance(xConsSet); Set> result = unify.unifySequential(xConsSet, finiteClosure, logFile); //Set> result = unify.unify(xConsSet, finiteClosure); System.out.println("RESULT: " + result); @@ -180,7 +182,36 @@ public class JavaTXCompiler { new ResultSet(UnifyTypeFactory.convert(unifyPairs, generateTPHMap(cons))))).collect(Collectors.toList()); } - + /** + * Vererbt alle Variancen + * @param eq The set of constraints + */ + private void varianceInheritance(Set eq) { + Set usedTPH = new HashSet<>(); + Set phSet = eq.stream().map(x -> { + Set pair = new HashSet<>(); + if (x.getLhsType() instanceof PlaceholderType) pair.add((PlaceholderType)x.getLhsType()); + if (x.getRhsType() instanceof PlaceholderType) pair.add((PlaceholderType)x.getRhsType()); + return pair; + }).reduce(new HashSet<>(), (a,b) -> { a.addAll(b); return a;} , (c,d) -> { c.addAll(d); return c;}); + + ArrayList phSetVariance = new ArrayList<>(phSet); + phSetVariance.removeIf(x -> (x.getVariance() == 0)); + while(!phSetVariance.isEmpty()) { + PlaceholderType a = phSetVariance.remove(0); + usedTPH.add(a); + //HashMap ht = new HashMap<>(); + //ht.put(a, a.getVariance()); + Set eq1 = new HashSet<>(eq); + eq1.removeIf(x -> !(x.getLhsType() instanceof PlaceholderType && ((PlaceholderType)x.getLhsType()).equals(a))); + eq1.stream().forEach(x -> { x.getRhsType().accept(new distributeVariance(), a.getVariance());}); + eq1 = new HashSet<>(eq); + eq1.removeIf(x -> !(x.getRhsType() instanceof PlaceholderType && ((PlaceholderType)x.getRhsType()).equals(a))); + eq1.stream().forEach(x -> { x.getLhsType().accept(new distributeVariance(), a.getVariance());}); + phSetVariance = new ArrayList<>(phSet); + phSetVariance.removeIf(x -> (x.getVariance() == 0 || usedTPH.contains(x))); + } +} private Map generateTPHMap(ConstraintSet constraints) { HashMap ret = new HashMap<>(); diff --git a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index aec5653c..710f2095 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -87,11 +87,14 @@ public class TypeUnifyTask extends RecursiveTask>> { rules = new RuleSet(logFile); } + /** * Vererbt alle Variancen * @param eq The set of constraints */ + /* PL 2018-05- 17 verschoben nach JavaTXCompiler private void varianceInheritance(Set eq) { + Set usedTPH = new HashSet<>(); Set phSet = eq.stream().map(x -> { Set pair = new HashSet<>(); if (x.getLhsType() instanceof PlaceholderType) pair.add((PlaceholderType)x.getLhsType()); @@ -103,36 +106,20 @@ public class TypeUnifyTask extends RecursiveTask>> { phSetVariance.removeIf(x -> (x.getVariance() == 0)); while(!phSetVariance.isEmpty()) { PlaceholderType a = phSetVariance.remove(0); - HashMap ht = new HashMap<>(); - ht.put(a, a.getVariance()); - eq.stream().forEach(x -> { - x.getLhsType().accept(new inheritVariance(), ht); - if (x.getLhsType() instanceof PlaceholderType - && ((PlaceholderType)x.getLhsType()).equals(a)//) { - //((PlaceholderType)x.getLhsType()).setVariance(a.getVariance()); - //if - && x.getRhsType() instanceof PlaceholderType - && ((PlaceholderType)x.getRhsType()).getVariance() == 0) { - ((PlaceholderType)x.getRhsType()).setVariance(a.getVariance()); - phSetVariance.add((PlaceholderType)x.getRhsType()); - //} - }; - }); - eq.stream().forEach(x -> { - x.getRhsType().accept(new inheritVariance(), ht); - if (x.getRhsType() instanceof PlaceholderType - && ((PlaceholderType)x.getRhsType()).equals(a)//) { - //((PlaceholderType)x.getRhsType()).setVariance(a.getVariance()); - //if - && x.getLhsType() instanceof PlaceholderType - && ((PlaceholderType)x.getLhsType()).getVariance() == 0) { - ((PlaceholderType)x.getLhsType()).setVariance(a.getVariance()); - phSetVariance.add((PlaceholderType)x.getLhsType()); - //} - }; - }); + usedTPH.add(a); + //HashMap ht = new HashMap<>(); + //ht.put(a, a.getVariance()); + Set eq1 = new HashSet<>(eq); + eq1.removeIf(x -> !(x.getLhsType() instanceof PlaceholderType && ((PlaceholderType)x.getLhsType()).equals(a))); + eq1.stream().forEach(x -> { x.getRhsType().accept(new distributeVariance(), a.getVariance());}); + eq1 = new HashSet<>(eq); + eq1.removeIf(x -> !(x.getRhsType() instanceof PlaceholderType && ((PlaceholderType)x.getRhsType()).equals(a))); + eq1.stream().forEach(x -> { x.getLhsType().accept(new distributeVariance(), a.getVariance());}); + phSetVariance = new ArrayList<>(phSet); + phSetVariance.removeIf(x -> (x.getVariance() == 0 || usedTPH.contains(x))); } } +*/ @Override protected Set> compute() { Set> res = unify(eq, fc, parallel); @@ -156,7 +143,8 @@ public class TypeUnifyTask extends RecursiveTask>> { /* * Variancen auf alle Gleichungen vererben */ - varianceInheritance(eq); + //PL 2018-05-17 nach JavaTXCompiler verschoben + //varianceInheritance(eq); /* * ? extends ? extends Theta rausfiltern diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java b/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java index efd83b5d..bbd3b3e1 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java @@ -136,7 +136,9 @@ public final class PlaceholderType extends UnifyType{ UnifyType apply(Unifier unif) { if(unif.hasSubstitute(this)) { UnifyType ret = unif.getSubstitute(this); - ret.accept(new distributeVariance(), this.getVariance()); + //PL 2018-05-17 Auskommentierung muesste korrekt sein, + //bereits in JavaTXComplier Variancen gesetzt werden. + //ret.accept(new distributeVariance(), this.getVariance()); return ret; } return this; diff --git a/test/typeinference/UnifyTest.java b/test/typeinference/UnifyTest.java index ac7ef221..2d6064cd 100644 --- a/test/typeinference/UnifyTest.java +++ b/test/typeinference/UnifyTest.java @@ -34,10 +34,12 @@ public class UnifyTest { execute(new File(rootDirectory+"LambdaField.jav")); } */ + /* @Test public void lambda2() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Lambda2.jav")); } + */ /* @Test public void lambda3() throws IOException, ClassNotFoundException { @@ -65,14 +67,14 @@ public class UnifyTest { } */ - /* + @Test public void matrix() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Matrix.jav")); //JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"Matrix.jav")); //compiler.generateBytecode(); } - */ + /* @Test public void vector() throws IOException, ClassNotFoundException {