modified: src/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java

modified:   src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java
	modified:   src/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java
This commit is contained in:
Martin Plümicke 2018-03-05 20:04:10 +01:00
parent 831bf1fe17
commit bb016e6417
3 changed files with 60 additions and 21 deletions

View File

@ -5,6 +5,7 @@ import java.util.Set;
import de.dhbwstuttgart.typeinference.unify.model.ExtendsType; import de.dhbwstuttgart.typeinference.unify.model.ExtendsType;
import de.dhbwstuttgart.typeinference.unify.model.FunNType; import de.dhbwstuttgart.typeinference.unify.model.FunNType;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
import de.dhbwstuttgart.typeinference.unify.model.ReferenceType; import de.dhbwstuttgart.typeinference.unify.model.ReferenceType;
import de.dhbwstuttgart.typeinference.unify.model.SuperType; import de.dhbwstuttgart.typeinference.unify.model.SuperType;
@ -62,5 +63,5 @@ public interface IFiniteClosure {
public Set<UnifyType> getChildren(UnifyType t); public Set<UnifyType> getChildren(UnifyType t);
public Set<UnifyType> getAllTypesByName(String typeName); public Set<UnifyType> getAllTypesByName(String typeName);
public int compare(UnifyType rhsType, UnifyType rhsType2); public int compare(UnifyType rhsType, UnifyType rhsType2, PairOperator pairop);
} }

View File

@ -435,7 +435,11 @@ public class FiniteClosure extends Ordering<UnifyType> implements IFiniteClosure
} }
public int compare (UnifyType left, UnifyType right) { public int compare (UnifyType left, UnifyType right) {
UnifyPair up = new UnifyPair(left, right, PairOperator.SMALLERDOT); return compare(left, right, PairOperator.SMALLERDOT);
}
public int compare (UnifyType left, UnifyType right, PairOperator pairop) {
UnifyPair up = new UnifyPair(left, right, pairop);
TypeUnifyTask unifyTask = new TypeUnifyTask(); TypeUnifyTask unifyTask = new TypeUnifyTask();
HashSet<UnifyPair> hs = new HashSet<>(); HashSet<UnifyPair> hs = new HashSet<>();
hs.add(up); hs.add(up);
@ -443,7 +447,7 @@ public class FiniteClosure extends Ordering<UnifyType> implements IFiniteClosure
long smallerLen = smallerRes.stream().filter(x -> !(x.getLhsType() instanceof PlaceholderType && x.getRhsType() instanceof PlaceholderType)).count(); long smallerLen = smallerRes.stream().filter(x -> !(x.getLhsType() instanceof PlaceholderType && x.getRhsType() instanceof PlaceholderType)).count();
if (smallerLen == 0) return -1; if (smallerLen == 0) return -1;
else { else {
up = new UnifyPair(right, left, PairOperator.SMALLERDOT); up = new UnifyPair(right, left, pairop);
//TypeUnifyTask unifyTask = new TypeUnifyTask(); //TypeUnifyTask unifyTask = new TypeUnifyTask();
hs = new HashSet<>(); hs = new HashSet<>();
hs.add(up); hs.add(up);

View File

@ -31,10 +31,10 @@ public class OrderingUnifyPair extends Ordering<Set<UnifyPair>> {
if (left == null || right == null) if (left == null || right == null)
System.out.println("Fehler"); System.out.println("Fehler");
if (left.getLhsType() instanceof PlaceholderType) { if (left.getLhsType() instanceof PlaceholderType) {
return fc.compare(left.getRhsType(), right.getRhsType()); return fc.compare(left.getRhsType(), right.getRhsType(), left.getPairOp());
} }
else { else {
return fc.compare(left.getLhsType(), right.getLhsType()); return fc.compare(left.getLhsType(), right.getLhsType(), left.getPairOp());
} }
} }
@ -66,12 +66,43 @@ public class OrderingUnifyPair extends Ordering<Set<UnifyPair>> {
.filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)) .filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT))
.collect(Collectors.toCollection(HashSet::new)); .collect(Collectors.toCollection(HashSet::new));
Set<UnifyPair> leftle = left.stream() Set<UnifyPair> leftle = left.stream()
.filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.SMALLERDOTWC)) .filter(x -> ((x.getLhsType() instanceof PlaceholderType || x.getRhsType() instanceof PlaceholderType)
&& x.getPairOp() == PairOperator.SMALLERDOT))
.collect(Collectors.toCollection(HashSet::new)); .collect(Collectors.toCollection(HashSet::new));
Set<UnifyPair> rightle = right.stream() Set<UnifyPair> rightle = right.stream()
.filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.SMALLERDOTWC)) .filter(x -> ((x.getLhsType() instanceof PlaceholderType || x.getRhsType() instanceof PlaceholderType)
&& x.getPairOp() == PairOperator.SMALLERDOT))
.collect(Collectors.toCollection(HashSet::new)); .collect(Collectors.toCollection(HashSet::new));
if (lefteq.size() > 1) { Set<UnifyPair> leftlewc = left.stream()
.filter(x -> ((x.getLhsType() instanceof PlaceholderType || x.getRhsType() instanceof PlaceholderType)
&& x.getPairOp() == PairOperator.SMALLERDOTWC))
.collect(Collectors.toCollection(HashSet::new));
Set<UnifyPair> rightlewc = right.stream()
.filter(x -> ((x.getLhsType() instanceof PlaceholderType || x.getRhsType() instanceof PlaceholderType)
&& x.getPairOp() == PairOperator.SMALLERDOTWC))
.collect(Collectors.toCollection(HashSet::new));
//Fall 2 und 3
if (lefteq.size() == 1 && leftle.size() == 1 && righteq.size() == 0 && rightle.size() == 1) {
return 1;
}
//Fall 2 und 3
if (lefteq.size() == 0 && leftle.size() == 1 && righteq.size() == 1 && rightle.size() == 1) {
return -1;
}
//Fall 5
if (lefteq.size() == 1 && leftle.size() == 0 && righteq.size() == 1 && rightle.size() == 1) {
return -1;
}
//Fall 5
if (lefteq.size() == 1 && leftle.size() == 1 && righteq.size() == 1 && rightle.size() == 0) {
return 1;
}
//Fall 5
if (lefteq.size() == 1 && leftle.size() == 1 && righteq.size() == 1 && rightle.size() == 1) {
return 0;
}
// Nur Paare a =. Theta
if (leftle.size() == 0 && rightle.size() == 0 && leftlewc.size() == 0 && rightlewc.size() ==0) {
Stream<UnifyPair> lseq = lefteq.stream(); //left.filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)); Stream<UnifyPair> lseq = lefteq.stream(); //left.filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT));
Stream<UnifyPair> rseq = righteq.stream(); //right.filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT)); Stream<UnifyPair> rseq = righteq.stream(); //right.filter(x -> (x.getLhsType() instanceof PlaceholderType && x.getPairOp() == PairOperator.EQUALSDOT));
BinaryOperator<HashMap<UnifyType,UnifyPair>> combiner = (x,y) -> { x.putAll(y); return x;}; BinaryOperator<HashMap<UnifyType,UnifyPair>> combiner = (x,y) -> { x.putAll(y); return x;};
@ -81,7 +112,8 @@ public class OrderingUnifyPair extends Ordering<Set<UnifyPair>> {
if (!si.isPresent()) return 0; if (!si.isPresent()) return 0;
else return si.get(); else return si.get();
} }
if (lefteq.size() == 1 && righteq.size() == 1) { //Fall 1 und 4
if (lefteq.size() == 1 && righteq.size() == 1 && (leftlewc.size() > 0 || rightlewc.size() > 0)) {
//TODO 2018-03-02: hier muessen mehree =. eingefuegt werden und alle weiteren Faelle pruefen //TODO 2018-03-02: hier muessen mehree =. eingefuegt werden und alle weiteren Faelle pruefen
UnifyPair lseq = lefteq.iterator().next(); UnifyPair lseq = lefteq.iterator().next();
UnifyPair rseq = righteq.iterator().next(); UnifyPair rseq = righteq.iterator().next();
@ -95,26 +127,28 @@ public class OrderingUnifyPair extends Ordering<Set<UnifyPair>> {
Pair<Integer, Set<UnifyPair>> int_Unifier = compare(lseq.getRhsType(), rseq.getRhsType()); Pair<Integer, Set<UnifyPair>> int_Unifier = compare(lseq.getRhsType(), rseq.getRhsType());
Unifier uni = new Unifier(); Unifier uni = new Unifier();
int_Unifier.getValue().stream().forEach(x -> uni.add((PlaceholderType) x.getLhsType(), x.getRhsType())); int_Unifier.getValue().stream().forEach(x -> uni.add((PlaceholderType) x.getLhsType(), x.getRhsType()));
if (leftle.size() == 0) return int_Unifier.getKey(); if (lseq.equals(rseq) || leftlewc.size() == 0 || rightlewc.size() == 0) return int_Unifier.getKey();
else { else {
Stream <UnifyPair> lslestr = leftle.stream().map(uni::apply); Set <UnifyPair> lsleuni = leftlewc.stream().map(uni::apply).collect(Collectors.toCollection(HashSet::new));
Stream <UnifyPair> rslestr = rightle.stream().map(uni::apply); Set <UnifyPair> rsleuni = rightlewc.stream().map(uni::apply).collect(Collectors.toCollection(HashSet::new));
BinaryOperator<HashMap<UnifyType,UnifyPair>> combiner = (x,y) -> { x.putAll(y); return x;}; BinaryOperator<HashMap<UnifyType,UnifyPair>> combiner = (x,y) -> { x.putAll(y); return x;};
HashMap<UnifyType,UnifyPair> hm; HashMap<UnifyType,UnifyPair> hm;
Optional<Integer> si; Optional<Integer> si;
if (leftle.iterator().next().getLhsType() instanceof PlaceholderType) { //1. Fall
hm = rslestr.reduce(new HashMap<UnifyType,UnifyPair>(), (x, y)-> { x.put(y.getLhsType(),y); return x; }, combiner); if (leftlewc.iterator().next().getLhsType() instanceof PlaceholderType) {
lslestr = lslestr.filter(x -> !(hm.get(x.getLhsType()) == null)); hm = rsleuni.stream().reduce(new HashMap<UnifyType,UnifyPair>(), (x, y)-> { x.put(y.getLhsType(),y); return x; }, combiner);
si = lslestr.map(x -> compare(x, hm.get(x.getLhsType()))).reduce((x,y)-> { if (x == y) return x; else return 0; } ); Stream<UnifyPair> lslewcstr = lsleuni.stream().filter(x -> !(hm.get(x.getLhsType()) == null));
} si = lslewcstr.map(x -> compare(x, hm.get(x.getLhsType()))).reduce((x,y)-> { if (x == y) return x; else return 0; } );
}
//4. Fall
else { else {
hm = rslestr.reduce(new HashMap<UnifyType,UnifyPair>(), (x, y)-> { x.put(y.getRhsType(),y); return x; }, combiner); hm = rsleuni.stream().reduce(new HashMap<UnifyType,UnifyPair>(), (x, y)-> { x.put(y.getRhsType(),y); return x; }, combiner);
rslestr = rslestr.filter(x -> !(hm.get(x.getRhsType()) == null)); Stream<UnifyPair> lslewcstr = lsleuni.stream().filter(x -> !(hm.get(x.getRhsType()) == null));
si = rslestr.map(x -> compare(x, hm.get(x.getRhsType()))).reduce((x,y)-> { if (x == y) return x; else return 0; } ); si = lslewcstr.map(x -> compare(x, hm.get(x.getRhsType()))).reduce((x,y)-> { if (x == y) return x; else return 0; } );
} }
if (!si.isPresent()) return 0; if (!si.isPresent()) return 0;
else return si.get(); else if (si.get() == int_Unifier.getKey()) return int_Unifier.getKey(); else return 0;
} }
} }