From 0ae777b4ac1e6818751fb22e33b6d54847d600d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Pl=C3=BCmicke?= Date: Fri, 20 Apr 2018 23:52:12 +0200 Subject: [PATCH] modified: src/de/dhbwstuttgart/core/JavaTXCompiler.java varianceInheritance nach TypeUnifyTask.java verschoben Kommentar eingefuegt TODO: 1. compare-Funktinen nochmals ueberpruefen 2. Undef-Pairs in JavaTXiCompiler.java abfangen 3. ? extends bei allen Argumenttypen und Returntypen rauslassen --- src/de/dhbwstuttgart/core/JavaTXCompiler.java | 39 +------------- .../typeinference/unify/TypeUnifyTask.java | 53 ++++++++++++++++++- .../unify/model/OrderingUnifyPair.java | 1 + 3 files changed, 55 insertions(+), 38 deletions(-) diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 5ef594134..c8bf0018f 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -23,6 +23,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.inheritVariance; import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; @@ -143,7 +144,6 @@ public class JavaTXCompiler { } return x; }).collect(Collectors.toCollection(HashSet::new)); - varianceInferitance(xConsSet); Set> result = unify.unifySequential(xConsSet, finiteClosure, logFile); //Set> result = unify.unify(xConsSet, finiteClosure); System.out.println("RESULT: " + result); @@ -158,42 +158,7 @@ public class JavaTXCompiler { new ResultSet(UnifyTypeFactory.convert(unifyPairs, generateTPHMap(cons))))).collect(Collectors.toList()); } - private void varianceInferitance(Set xConsSet) { - Set phSet = xConsSet.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); - xConsSet.stream().forEach(x -> { 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()); - } - }; - } - ); - xConsSet.stream().forEach(x -> { 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()); - } - }; - } - ); - } - } + 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 e590c3026..c5e8281ca 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -86,7 +86,53 @@ public class TypeUnifyTask extends RecursiveTask>> { this.logFile = logFile; rules = new RuleSet(logFile); } - + + /** + * Vererbt alle Variancen + * @param eq The set of constraints + */ + private void varianceInheritance(Set eq) { + 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); + 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()); + //} + }; + }); + } +} @Override protected Set> compute() { return unify(eq, fc, parallel); @@ -105,6 +151,11 @@ 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)); + /* + * Variancen auf alle Gleichungen vererben + */ + varianceInheritance(eq); + /* * ? extends ? extends Theta rausfiltern */ diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java b/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java index 9d948cdc1..fa6b114d2 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java @@ -149,6 +149,7 @@ public class OrderingUnifyPair extends Ordering> { if (rseq.getRhsType().getName().equals("Object")) return -1; } if (leftlewc.size() == rightlewc.size()) { + //TODO: Hier wird bei Wildcards nicht das richtige compare aufgerufen PL 18-04-20 Pair> int_Unifier = compare(lseq.getRhsType(), rseq.getRhsType()); Unifier uni = new Unifier(); int_Unifier.getValue().stream().forEach(x -> uni.add((PlaceholderType) x.getLhsType(), x.getRhsType()));