modified: src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java

modified:   src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java
OderConstraints in Unify integriert
This commit is contained in:
Martin Plümicke 2018-11-06 23:35:57 +01:00
parent d08708878f
commit fc3331d810
2 changed files with 65 additions and 21 deletions

View File

@ -228,7 +228,8 @@ public class TYPEStmt implements StatementVisitor{
binary.operation.equals(BinaryExpr.Operator.ADD)||
binary.operation.equals(BinaryExpr.Operator.SUB)){
Set<Constraint<Pair>> 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

View File

@ -159,12 +159,20 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
}
*/
protected Set<Set<UnifyPair>> computeOld() {
Set<Set<UnifyPair>> res = unify(eq, fc, parallel);
protected Set<Set<UnifyPair>> compute() {
Set<UnifyPair> 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<Set<Set<UnifyPair>>> remainingOderconstraints = oderConstraints.stream()
.filter(x -> x.size()>1)
.collect(Collectors.toCollection(ArrayList::new));
Set<Set<UnifyPair>> res = unify(neweq, remainingOderconstraints, fc, parallel);
if (isUndefinedPairSetSet(res)) { return new HashSet<>(); }
else return res;
}
/*
@Override
protected Set<Set<UnifyPair>> compute() {
Set<Set<UnifyPair>> fstElems = new HashSet<>();
@ -173,7 +181,9 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
if (isUndefinedPairSetSet(res)) { return new HashSet<>(); }
else return res;
}
*/
public Set<Set<UnifyPair>> computeCartesianRecursiveOderConstraints(Set<Set<UnifyPair>> fstElems, List<Set<Set<UnifyPair>>> topLevelSets, IFiniteClosure fc, boolean parallel) {
//ArrayList<Set<Set<UnifyPair>>> remainingSets = new ArrayList<>(topLevelSets);
fstElems.addAll(topLevelSets.stream()
@ -186,7 +196,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
if (remainingSets.isEmpty()) {//Alle Elemente sind 1-elementig
Set<UnifyPair> eq = new HashSet<>();
fstElems.stream().forEach(x -> eq.addAll(x));
Set<Set<UnifyPair>> result = unify(eq, fc, parallel);
Set<Set<UnifyPair>> result = unify(eq, new ArrayList<>(), fc, parallel);
return result;
}
Set<Set<UnifyPair>> nextSet = remainingSets.remove(0);
@ -289,7 +299,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
System.out.println(noou);
Set<UnifyPair> 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<Set<Set<UnifyPair>>> {
* @param fc The finite closure
* @return The set of all principal type unifiers
*/
protected Set<Set<UnifyPair>> unify(Set<UnifyPair> eq, IFiniteClosure fc, boolean parallel) {
protected Set<Set<UnifyPair>> unify(Set<UnifyPair> eq, List<Set<Set<UnifyPair>>> oderConstraints, IFiniteClosure fc, boolean parallel) {
//Set<UnifyPair> 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<Set<UnifyPair>>> {
Set<UnifyPair> undefinedPairs = new HashSet<>();
if (printtag) System.out.println("eq2s " + eq2s);
//writeLog("BufferSet: " + bufferSet.toString()+"\n");
Set<Set<Set<Set<UnifyPair>>>> secondLevelSets = calculatePairSets(eq2s, fc, undefinedPairs);
Set<Set<Set<Set<UnifyPair>>>> 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<Set<Set<UnifyPair>>> {
//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<Set<UnifyPair>> unify2(Set<Set<UnifyPair>> setToFlatten, Set<UnifyPair> eq, IFiniteClosure fc, boolean parallel) {
Set<Set<UnifyPair>> unify2(Set<Set<UnifyPair>> setToFlatten, Set<UnifyPair> eq, List<Set<Set<UnifyPair>>> oderConstraints, IFiniteClosure fc, boolean parallel) {
//Aufruf von computeCartesianRecursive ENDE
//keine Ahnung woher das kommt
@ -634,12 +647,12 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
eqPrimePrimeSet.add(eqPrime);
}
else if(eqPrimePrime.isPresent()) {
Set<Set<UnifyPair>> unifyres = unify(eqPrimePrime.get(), fc, false);
Set<Set<UnifyPair>> unifyres = unify(eqPrimePrime.get(), oderConstraints, fc, false);
eqPrimePrimeSet.addAll(unifyres);
}
else {
Set<Set<UnifyPair>> unifyres = unify(eqPrime, fc, false);
Set<Set<UnifyPair>> unifyres = unify(eqPrime, oderConstraints, fc, false);
eqPrimePrimeSet.addAll(unifyres);
@ -668,7 +681,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
Set<Set<UnifyPair>> computeCartesianRecursive(Set<Set<UnifyPair>> fstElems, ArrayList<Set<Set<UnifyPair>>> topLevelSets, Set<UnifyPair> eq, IFiniteClosure fc, boolean parallel) {
Set<Set<UnifyPair>> computeCartesianRecursive(Set<Set<UnifyPair>> fstElems, ArrayList<Set<Set<UnifyPair>>> topLevelSets, Set<UnifyPair> eq, List<Set<Set<UnifyPair>>> oderConstraints, IFiniteClosure fc, boolean parallel) {
//ArrayList<Set<Set<UnifyPair>>> remainingSets = new ArrayList<>(topLevelSets);
fstElems.addAll(topLevelSets.stream()
.filter(x -> x.size()==1)
@ -678,7 +691,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
.filter(x -> x.size()>1)
.collect(Collectors.toCollection(ArrayList::new));
if (remainingSets.isEmpty()) {//Alle Elemente sind 1-elementig
Set<Set<UnifyPair>> result = unify2(fstElems, eq, fc, parallel);
Set<Set<UnifyPair>> result = unify2(fstElems, eq, oderConstraints, fc, parallel);
return result;
}
Set<Set<UnifyPair>> nextSet = remainingSets.remove(0);
@ -775,7 +788,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
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<Set<UnifyPair>> res = unify2(elems, eq, fc, parallel);
Set<Set<UnifyPair>> 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<Set<Set<UnifyPair>>> {
* 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<Set<Set<Set<UnifyPair>>>> calculatePairSets(Set<UnifyPair> eq2s, IFiniteClosure fc, Set<UnifyPair> undefined) {
List<Set<Set<Set<UnifyPair>>>> result = new ArrayList<>(8);
protected Set<Set<Set<Set<UnifyPair>>>> calculatePairSets(Set<UnifyPair> eq2s, List<Set<Set<UnifyPair>>> oderConstraints, IFiniteClosure fc, Set<UnifyPair> undefined) {
List<Set<Set<Set<UnifyPair>>>> 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<UnifyPair> eq2sprime = new ArrayList<>(eq2s);
Iterator<UnifyPair> eq2sprimeit = eq2sprime.iterator();
ArrayList<UnifyPair> 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<Set<Set<UnifyPair>>> {
eq2s.remove(up);
}
}
if (eq2sAsList.isEmpty()) {
List<Set<Set<UnifyPair>>> 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<Set<UnifyPair>> 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();