From fc3331d810923399be4f1ed81217a192a01d66bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Pl=C3=BCmicke?= Date: Tue, 6 Nov 2018 23:35:57 +0100 Subject: [PATCH] modified: src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java modified: src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java OderConstraints in Unify integriert --- .../typeinference/typeAlgo/TYPEStmt.java | 3 +- .../typeinference/unify/TypeUnifyTask.java | 83 ++++++++++++++----- 2 files changed, 65 insertions(+), 21 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 609600cd1..df57fa5ec 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -228,7 +228,8 @@ public class TYPEStmt implements StatementVisitor{ binary.operation.equals(BinaryExpr.Operator.ADD)|| binary.operation.equals(BinaryExpr.Operator.SUB)){ Set> numericAdditionOrStringConcatenation = new HashSet<>(); - + - Auf importierte Typen einschraenken + - pruefen, ob die Typen richtig bestimmt werden. //Zuerst der Fall für Numerische AusdrücPairOpnumericeratorke, das sind Mul, Mod und Div immer: //see: https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.17 //Expression muss zu Numeric Convertierbar sein. also von Numeric erben diff --git a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index 6a2ef3959..5ee0a27d3 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -159,12 +159,20 @@ public class TypeUnifyTask extends RecursiveTask>> { } */ - protected Set> computeOld() { - Set> res = unify(eq, fc, parallel); + protected Set> compute() { + Set neweq = new HashSet<>(eq); + /* 1-elementige Oder-Constraints werden in und-Constraints umgewandelt */ + oderConstraints.stream() + .filter(x -> x.size()==1) + .map(y -> y.stream().findFirst().get()).forEach(x -> neweq.addAll(x)); + ArrayList>> remainingOderconstraints = oderConstraints.stream() + .filter(x -> x.size()>1) + .collect(Collectors.toCollection(ArrayList::new)); + Set> res = unify(neweq, remainingOderconstraints, fc, parallel); if (isUndefinedPairSetSet(res)) { return new HashSet<>(); } else return res; } - +/* @Override protected Set> compute() { Set> fstElems = new HashSet<>(); @@ -173,7 +181,9 @@ public class TypeUnifyTask extends RecursiveTask>> { if (isUndefinedPairSetSet(res)) { return new HashSet<>(); } else return res; } - +*/ + + public Set> computeCartesianRecursiveOderConstraints(Set> fstElems, List>> topLevelSets, IFiniteClosure fc, boolean parallel) { //ArrayList>> remainingSets = new ArrayList<>(topLevelSets); fstElems.addAll(topLevelSets.stream() @@ -186,7 +196,7 @@ public class TypeUnifyTask extends RecursiveTask>> { if (remainingSets.isEmpty()) {//Alle Elemente sind 1-elementig Set eq = new HashSet<>(); fstElems.stream().forEach(x -> eq.addAll(x)); - Set> result = unify(eq, fc, parallel); + Set> result = unify(eq, new ArrayList<>(), fc, parallel); return result; } Set> nextSet = remainingSets.remove(0); @@ -289,7 +299,7 @@ public class TypeUnifyTask extends RecursiveTask>> { System.out.println(noou); Set eq = new HashSet<>(); elems.stream().forEach(x -> eq.addAll(x)); - res = unify(eq, fc, parallel); + res = unify(eq, new ArrayList<>(), fc, parallel); } else {//duerfte gar nicht mehr vorkommen PL 2018-04-03 res = computeCartesianRecursiveOderConstraints(elems, remainingSets, fc, parallel); @@ -428,18 +438,21 @@ public class TypeUnifyTask extends RecursiveTask>> { * @param fc The finite closure * @return The set of all principal type unifiers */ - protected Set> unify(Set eq, IFiniteClosure fc, boolean parallel) { + protected Set> unify(Set eq, List>> oderConstraints, IFiniteClosure fc, boolean parallel) { //Set aas = eq.stream().filter(x -> x.getLhsType().getName().equals("AA") //&& x.getPairOp().equals(PairOperator.SMALLERDOT) // ).collect(Collectors.toCollection(HashSet::new)); //writeLog(nOfUnify.toString() + " AA: " + aas.toString()); //if (aas.isEmpty()) { // System.out.println(""); //} + + //.collect(Collectors.toCollection(HashSet::new))); /* * Step 1: Repeated application of reduce, adapt, erase, swap */ nOfUnify++; writeLog(nOfUnify.toString() + " Unifikation: " + eq.toString()); + writeLog(nOfUnify.toString() + " Oderconstraints: " + oderConstraints.toString()); //eq = eq.stream().map(x -> {x.setVariance((byte)-1); return x;}).collect(Collectors.toCollection(HashSet::new)); /* @@ -505,7 +518,7 @@ public class TypeUnifyTask extends RecursiveTask>> { Set undefinedPairs = new HashSet<>(); if (printtag) System.out.println("eq2s " + eq2s); //writeLog("BufferSet: " + bufferSet.toString()+"\n"); - Set>>> secondLevelSets = calculatePairSets(eq2s, fc, undefinedPairs); + Set>>> secondLevelSets = calculatePairSets(eq2s, oderConstraints, fc, undefinedPairs); //PL 2017-09-20: Im calculatePairSets wird möglicherweise O .< java.lang.Integer //nicht ausgewertet Faculty Beispiel im 1. Schritt //PL 2017-10-03 geloest, muesste noch mit FCs mit kleineren @@ -561,12 +574,12 @@ public class TypeUnifyTask extends RecursiveTask>> { //Aufruf von computeCartesianRecursive ANFANG - return computeCartesianRecursive(new HashSet<>(), new ArrayList<>(topLevelSets), eq, fc, parallel); + return computeCartesianRecursive(new HashSet<>(), new ArrayList<>(topLevelSets), eq, oderConstraints, fc, parallel); } - Set> unify2(Set> setToFlatten, Set eq, IFiniteClosure fc, boolean parallel) { + Set> unify2(Set> setToFlatten, Set eq, List>> oderConstraints, IFiniteClosure fc, boolean parallel) { //Aufruf von computeCartesianRecursive ENDE //keine Ahnung woher das kommt @@ -634,12 +647,12 @@ public class TypeUnifyTask extends RecursiveTask>> { eqPrimePrimeSet.add(eqPrime); } else if(eqPrimePrime.isPresent()) { - Set> unifyres = unify(eqPrimePrime.get(), fc, false); + Set> unifyres = unify(eqPrimePrime.get(), oderConstraints, fc, false); eqPrimePrimeSet.addAll(unifyres); } else { - Set> unifyres = unify(eqPrime, fc, false); + Set> unifyres = unify(eqPrime, oderConstraints, fc, false); eqPrimePrimeSet.addAll(unifyres); @@ -668,7 +681,7 @@ public class TypeUnifyTask extends RecursiveTask>> { - Set> computeCartesianRecursive(Set> fstElems, ArrayList>> topLevelSets, Set eq, IFiniteClosure fc, boolean parallel) { + Set> computeCartesianRecursive(Set> fstElems, ArrayList>> topLevelSets, Set eq, List>> oderConstraints, IFiniteClosure fc, boolean parallel) { //ArrayList>> remainingSets = new ArrayList<>(topLevelSets); fstElems.addAll(topLevelSets.stream() .filter(x -> x.size()==1) @@ -678,7 +691,7 @@ public class TypeUnifyTask extends RecursiveTask>> { .filter(x -> x.size()>1) .collect(Collectors.toCollection(ArrayList::new)); if (remainingSets.isEmpty()) {//Alle Elemente sind 1-elementig - Set> result = unify2(fstElems, eq, fc, parallel); + Set> result = unify2(fstElems, eq, oderConstraints, fc, parallel); return result; } Set> nextSet = remainingSets.remove(0); @@ -775,7 +788,7 @@ public class TypeUnifyTask extends RecursiveTask>> { elems.add(a); //if (remainingSets.isEmpty()) {//muss immer gegeben sein, weil nur 1 Element der topLevelSets mehr als ein Elemet enthaelt //writeLog("Vor unify2 Aufruf: " + eq.toString()); - Set> res = unify2(elems, eq, fc, parallel); + Set> res = unify2(elems, eq, oderConstraints, fc, parallel); if (!isUndefinedPairSetSet(res) && isUndefinedPairSetSet(result)) { //wenn korrektes Ergebnis gefunden alle Fehlerfaelle loeschen result = res; @@ -1174,15 +1187,17 @@ public class TypeUnifyTask extends RecursiveTask>> { * from the pairs that matched the case. Each generated set contains singleton sets or sets with few elements * (as in case 1 where sigma is added to the innermost set). */ - protected Set>>> calculatePairSets(Set eq2s, IFiniteClosure fc, Set undefined) { - List>>> result = new ArrayList<>(8); + protected Set>>> calculatePairSets(Set eq2s, List>> oderConstraints, IFiniteClosure fc, Set undefined) { + List>>> result = new ArrayList<>(9); - // Init all 8 cases - for(int i = 0; i < 8; i++) + // Init all 8 cases + 9. Case: oderConstraints + for(int i = 0; i < 9; i++) result.add(new HashSet<>()); + ArrayList eq2sprime = new ArrayList<>(eq2s); Iterator eq2sprimeit = eq2sprime.iterator(); ArrayList eq2sAsList = new ArrayList<>(); + Boolean first = true; while(eq2sprimeit.hasNext()) {// alle mit Variance != 0 nach vorne schieben UnifyPair up = eq2sprimeit.next(); if ((up.getLhsType() instanceof PlaceholderType && ((PlaceholderType)up.getLhsType()).getVariance() != 0) @@ -1191,8 +1206,36 @@ public class TypeUnifyTask extends RecursiveTask>> { eq2s.remove(up); } } + if (eq2sAsList.isEmpty()) { + List>> oderConstraintsVariance = oderConstraints.stream() //Alle Elemente rauswerfen, die Variance 0 haben oder keine TPH in LHS oder RHS sind + .filter(x -> x.stream() + .filter(y -> + y.stream().filter(z -> ((z.getLhsType() instanceof PlaceholderType) + && (((PlaceholderType)(z.getLhsType())).getVariance() != 0)) + || ((z.getRhsType() instanceof PlaceholderType) + && (((PlaceholderType)(z.getRhsType())).getVariance() != 0)) + ).findFirst().isPresent() + ).findFirst().isPresent()).collect(Collectors.toList()); + if (!oderConstraintsVariance.isEmpty()) { + Set> ret = oderConstraintsVariance.get(0); + oderConstraints.remove(ret); + result.get(8).add(ret); + first = false; + } + } + eq2sAsList.addAll(eq2s); - Boolean first = true; + + if (eq2sAsList.isEmpty() && first) {//Alle eq2s sind empty und alle oderConstraints mit Variance != 0 sind bearbeitet + if (!oderConstraints.isEmpty()) { + result.get(8).add(oderConstraints.remove(0)); + first = false; + } + } + + - Bei allen die Abhaengigkeit der Elemente aus eq2sAsList als evtl. als Substitution + hinzufuegen + for(UnifyPair pair : eq2sAsList) { PairOperator pairOp = pair.getPairOp(); UnifyType lhsType = pair.getLhsType();