Instabiler Zustand reduce funktionert nicht

This commit is contained in:
Martin Plümicke 2018-02-23 00:37:59 +01:00
parent 419e4a6d0e
commit 8e198679c7
4 changed files with 99 additions and 23 deletions

View File

@ -24,7 +24,7 @@ public class FCGenerator {
for(ClassOrInterface cly : availableClasses){
pairs.addAll(getSuperTypes(cly, availableClasses));
}
System.out.println(pairs);
System.out.println("Pairs: " + pairs);
return pairs;
}

View File

@ -25,12 +25,25 @@ import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
import de.dhbwstuttgart.typeinference.unify.model.WildcardType;
import java.io.FileWriter;
import java.io.IOException;
/**
* Implementation of the type inference rules.
* @author Florian Steurer
*
*/
public class RuleSet implements IRuleSet{
public class RuleSet implements IRuleSet{
FileWriter logFile;
RuleSet() {
super();
}
RuleSet(FileWriter logFile) {
this.logFile = logFile;
}
@Override
public Optional<UnifyPair> reduceUp(UnifyPair pair) {
@ -236,10 +249,17 @@ public class RuleSet implements IRuleSet{
ReferenceType lhsSType = (ReferenceType) c;
ReferenceType rhsSType = (ReferenceType) d;
try {
logFile.write("PAIR Rules: " + pair + "\n");
logFile.flush();
}
catch (IOException e) { }
if(lhsSType.getTypeParams().empty() || lhsSType.getTypeParams().size() != rhsSType.getTypeParams().size())
return Optional.empty();
UnifyType cFromFc = fc.getLeftHandedType(c.getName()).orElse(null);
2018-02-23: liefert Vector<Vector<Integer>>: Das kann nicht sein.
//NOCHMAL UEBERPRUEFEN
//PL 18-02-09 Eingfuegt Anfang
@ -255,14 +275,27 @@ public class RuleSet implements IRuleSet{
}
//PL 18-02-09 Eingfuegt ENDE
try {
logFile.write("cFromFc: " + cFromFc);
logFile.flush();
}
catch (IOException e) { }
if(cFromFc == null || !cFromFc.getTypeParams().arePlaceholders())
return Optional.empty();
UnifyType dFromFc = fc.getAncestors(cFromFc).stream().filter(x -> x.getName().equals(d.getName())).findAny().orElse(null);
try {
logFile.write("cFromFc: " + cFromFc);
logFile.flush();
}
catch (IOException e) { }
if(dFromFc == null || !dFromFc.getTypeParams().arePlaceholders() || dFromFc.getTypeParams().size() != cFromFc.getTypeParams().size())
return Optional.empty();
System.out.println("cFromFc: " + cFromFc);
System.out.println("dFromFc: " + dFromFc);
int[] pi = pi(cFromFc.getTypeParams(), dFromFc.getTypeParams());
if(pi.length == 0)

View File

@ -26,15 +26,23 @@ import de.dhbwstuttgart.typeinference.unify.model.Unifier;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/**
* Implementation of the type unification algorithm
* @author Florian Steurer
*/
public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
private static final long serialVersionUID = 1L;
private static int i = 0;
private boolean printtag = false;
public static final String rootDirectory = System.getProperty("user.dir")+"/test/logFiles/";
FileWriter logFile;
/**
* The implementation of setOps that will be used during the unification
@ -49,7 +57,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
/**
* The implementation of the rules that will be used during the unification.
*/
protected IRuleSet rules = new RuleSet();
protected IRuleSet rules;
protected Set<UnifyPair> eq;
@ -61,6 +69,13 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
this.eq = eq;
this.fc = fc;
this.parallel = parallel;
try {
logFile = new FileWriter(new File(rootDirectory+"log"));
logFile.write("xxx");
logFile.flush();
rules = new RuleSet(logFile);
}
catch (IOException e) { }
}
@Override
@ -74,11 +89,11 @@ 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, IFiniteClosure fc, boolean parallel) {
/*
* Step 1: Repeated application of reduce, adapt, erase, swap
*/
System.out.println("Unifikation: " + eq);
if (printtag) System.out.println("Unifikation: " + eq);
Set<UnifyPair> eq0 = applyTypeUnificationRules(eq, fc);
/*
@ -122,17 +137,17 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
// Sets that originate from pair pattern matching
// Sets of the "second level"
Set<UnifyPair> undefinedPairs = new HashSet<>();
System.out.println("eq2s " + eq2s);
if (printtag) System.out.println("eq2s " + eq2s);
Set<Set<Set<Set<UnifyPair>>>> secondLevelSets = calculatePairSets(eq2s, 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
//Typen getestet werden.
System.out.println("secondLevelSets:" +secondLevelSets);
if (printtag) System.out.println("secondLevelSets:" +secondLevelSets);
// If pairs occured that did not match one of the cartesian product cases,
// those pairs are contradictory and the unification is impossible.
if(!undefinedPairs.isEmpty()) {
System.out.println(undefinedPairs);
if (printtag) System.out.println("UndefinedPairs; " + undefinedPairs);
return new HashSet<>();
}
@ -160,7 +175,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
topLevelSets.add(flat);
}
//System.out.println(topLevelSets);
System.out.println();
//System.out.println();
//Aufruf von computeCartesianRecursive ANFANG
@ -173,7 +188,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
//Set<Set<UnifyPair>> unify2(Set<Set<UnifyPair>> setToFlatten, Set<UnifyPair> eq, IFiniteClosure fc, boolean parallel) {
//Aufruf von computeCartesianRecursive ENDE
//keine Ahnung woher das kommt
//Set<Set<UnifyPair>> setToFlatten = topLevelSets.stream().map(x -> x.iterator().next()).collect(Collectors.toCollection(HashSet::new));
//Muss auskommentiert werden, wenn computeCartesianRecursive ANFANG
@ -223,16 +238,31 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
}
}
else { // sequentiell (Step 6b is included)
System.out.println("nextStep: " + eqPrime);
if (printtag) System.out.println("nextStep: " + eqPrimePrime);
if (eqPrime.equals(eq)) //PL 2017-09-29 auskommentiert und durch
//(!eqPrimePrime.isPresent()) //PL 2071-09-29 dies ersetzt
//Begruendung: Wenn in der Substitution keine Veraenderung
//(!eqPrimePrime.isPresent()) erfolgt ist, ist das Ergebnis erzielt.
eqPrimePrimeSet.add(eqPrime);
else if(eqPrimePrime.isPresent())
eqPrimePrimeSet.addAll(unify(eqPrimePrime.get(), fc, false));
else
eqPrimePrimeSet.addAll(unify(eqPrime, fc, false));
else if(eqPrimePrime.isPresent()) {
Set<Set<UnifyPair>> unifyres = unify(eqPrimePrime.get(), fc, false);
try {
logFile.write(unifyres.toString()+"\n");
//logFile.flush();
}
catch (IOException e) { }
eqPrimePrimeSet.addAll(unifyres);
}
else {
Set<Set<UnifyPair>> unifyres = unify(eqPrime, fc, false);
try {
//logFile.write(unifyres.toString()+"\n");
logFile.flush();
}
catch (IOException e) { }
eqPrimePrimeSet.addAll(unifyres);
}
}
//Muss auskommentiert werden, wenn computeCartesianRecursive ANFANG
}
@ -352,6 +382,12 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
}
// Reduce1, Reduce2, ReduceExt, ReduceSup, ReduceEq
try {
logFile.write("PAIR1 " + pair + "\n");
logFile.flush();
}
catch (IOException e) { }
Optional<Set<UnifyPair>> optSet = rules.reduce1(pair, fc);
optSet = optSet.isPresent() ? optSet : rules.reduce2(pair);
optSet = optSet.isPresent() ? optSet : rules.reduceExt(pair, fc);
@ -375,6 +411,11 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
}
// Adapt, AdaptExt, AdaptSup
try {
logFile.write("PAIR2 " + pair + "\n");
logFile.flush();
}
catch (IOException e) { }
opt = rules.adapt(pair, fc);
opt = opt.isPresent() ? opt : rules.adaptExt(pair, fc);
opt = opt.isPresent() ? opt : rules.adaptSup(pair, fc);
@ -445,7 +486,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
// Case 1: (a <. Theta')
if(pairOp == PairOperator.SMALLERDOT && lhsType instanceof PlaceholderType) {
//System.out.println(pair);
if (first) {System.out.println(pair);
if (first) {if (printtag) System.out.println(pair);
Set<Set<UnifyPair>> x1 = unifyCase1((PlaceholderType) pair.getLhsType(), pair.getRhsType(), fc);
//System.out.println(x1);
result.get(0).add(x1);
@ -461,7 +502,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
}
// Case 2: (a <.? ? ext Theta')
else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType && rhsType instanceof ExtendsType)
if (first) {System.out.println(pair);
if (first) {if (printtag) System.out.println(pair);
result.get(1).add(unifyCase2((PlaceholderType) pair.getLhsType(), (ExtendsType) pair.getRhsType(), fc));
}
else {
@ -474,7 +515,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
// Case 3: (a <.? ? sup Theta')
else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType && rhsType instanceof SuperType)
if (first) {System.out.println(pair);
if (first) {if (printtag) System.out.println(pair);
result.get(2).add(unifyCase3((PlaceholderType) lhsType, (SuperType) rhsType, fc));
}
else {
@ -492,7 +533,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
// Case 5: (Theta <. a)
else if(pairOp == PairOperator.SMALLERDOT && rhsType instanceof PlaceholderType)
if (first) {System.out.println(pair);
if (first) {if (printtag) System.out.println(pair);
result.get(4).add(unifyCase5(lhsType, (PlaceholderType) rhsType, fc));
}
else {
@ -515,8 +556,9 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
// Case 8: (Theta <.? a)
else if(pairOp == PairOperator.SMALLERDOTWC && rhsType instanceof PlaceholderType)
if (first) {System.out.println(pair);
result.get(7).add(unifyCase8(lhsType, (PlaceholderType) rhsType, fc));
if (first) {if (printtag) System.out.println(pair);
result.get(7).add(
unifyCase8(lhsType, (PlaceholderType) rhsType, fc));
}
else {
Set<UnifyPair> s1 = new HashSet<>();

View File

@ -37,6 +37,7 @@ public class FiniteClosure implements IFiniteClosure {
* The initial pairs of that define the inheritance tree
*/
private Set<UnifyPair> pairs;
2018-02-23: Muessten hier NUR die initiale Piars rein?
/**
* Creates a new instance using the inheritance tree defined in the pairs.