modified: ../../src/de/dhbwstuttgart/core/JavaTXCompiler.java

Variancenvererbung eingefuegt
	modified:   ../../src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java
Variancenvererbung geloescht
	modified:   ../../src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java
	modified:   ../../test/typeinference/UnifyTest.java
This commit is contained in:
Martin Plümicke 2018-05-17 22:51:41 +02:00
parent bc2cab8c95
commit 8baff8f376
4 changed files with 56 additions and 33 deletions

View File

@ -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<Set<UnifyPair>> result = unify.unifySequential(xConsSet, finiteClosure, logFile);
//Set<Set<UnifyPair>> 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<UnifyPair> eq) {
Set<PlaceholderType> usedTPH = new HashSet<>();
Set<PlaceholderType> phSet = eq.stream().map(x -> {
Set<PlaceholderType> 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<PlaceholderType> phSetVariance = new ArrayList<>(phSet);
phSetVariance.removeIf(x -> (x.getVariance() == 0));
while(!phSetVariance.isEmpty()) {
PlaceholderType a = phSetVariance.remove(0);
usedTPH.add(a);
//HashMap<PlaceholderType,Integer> ht = new HashMap<>();
//ht.put(a, a.getVariance());
Set<UnifyPair> 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<String, TypePlaceholder> generateTPHMap(ConstraintSet<Pair> constraints) {
HashMap<String, TypePlaceholder> ret = new HashMap<>();

View File

@ -87,11 +87,14 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
rules = new RuleSet(logFile);
}
/**
* Vererbt alle Variancen
* @param eq The set of constraints
*/
/* PL 2018-05- 17 verschoben nach JavaTXCompiler
private void varianceInheritance(Set<UnifyPair> eq) {
Set<PlaceholderType> usedTPH = new HashSet<>();
Set<PlaceholderType> phSet = eq.stream().map(x -> {
Set<PlaceholderType> pair = new HashSet<>();
if (x.getLhsType() instanceof PlaceholderType) pair.add((PlaceholderType)x.getLhsType());
@ -103,36 +106,20 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
phSetVariance.removeIf(x -> (x.getVariance() == 0));
while(!phSetVariance.isEmpty()) {
PlaceholderType a = phSetVariance.remove(0);
HashMap<PlaceholderType,Integer> 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<PlaceholderType,Integer> ht = new HashMap<>();
//ht.put(a, a.getVariance());
Set<UnifyPair> 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<Set<UnifyPair>> compute() {
Set<Set<UnifyPair>> res = unify(eq, fc, parallel);
@ -156,7 +143,8 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
/*
* Variancen auf alle Gleichungen vererben
*/
varianceInheritance(eq);
//PL 2018-05-17 nach JavaTXCompiler verschoben
//varianceInheritance(eq);
/*
* ? extends ? extends Theta rausfiltern

View File

@ -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;

View File

@ -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 {