modified: ../../../../main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java

modified:   ../../../../main/java/de/dhbwstuttgart/typeinference/constraints/Constraint.java
	modified:   ../../../../main/java/de/dhbwstuttgart/typeinference/constraints/ConstraintSet.java
	modified:   ../../../../main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java
	modified:   ../../../../main/java/de/dhbwstuttgart/typeinference/unify/RuleSet.java
	modified:   ../../../../main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java
	modified:   ../../../../main/java/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java
	modified:   ../../../../main/java/de/dhbwstuttgart/typeinference/unify/model/OrderingUnifyPair.java
Fuer Variance = 1 neue Version korrekt, konnte aber noch Fehler entbhalten
This commit is contained in:
pl@gohorb.ba-horb.de 2020-05-04 17:02:15 +02:00
parent 9d4980d9a1
commit 6deeb4105f
8 changed files with 129 additions and 70 deletions

View File

@ -153,11 +153,11 @@ public class UnifyTypeFactory {
return constraints.map(UnifyTypeFactory::convert);
}
//never used
//NEVER USED
public static Constraint<UnifyPair> convert(Constraint<Pair> constraint){
Boolean isInherited = constraint.isInherited();
Constraint<UnifyPair> unifyPairConstraint = constraint.stream().map(UnifyTypeFactory::convert).collect(Collectors.toCollection(Constraint::new));
unifyPairConstraint.setIsInherited(isInherited);
Constraint<UnifyPair> unifyPairConstraint = constraint.stream()
.map(UnifyTypeFactory::convert)
.collect(Collectors.toCollection( () -> new Constraint<UnifyPair> (constraint.isInherited(), convert(constraint.getExtendConstraint()))));
return unifyPairConstraint;
}

View File

@ -7,7 +7,9 @@ import java.util.HashSet;
import java.util.Set;
public class Constraint<A> extends HashSet<A> {
private static final long serialVersionUID = 1L;
private Boolean isInherited = false;//wird nur für die Method-Constraints benoetigt
private Constraint<A> extendConstraint = null;
public Constraint() {
super();
@ -17,6 +19,11 @@ public class Constraint<A> extends HashSet<A> {
this.isInherited = isInherited;
}
public Constraint(Boolean isInherited, Constraint<A> extendConstraint) {
this.isInherited = isInherited;
this.extendConstraint = extendConstraint;
}
public void setIsInherited(Boolean isInherited) {
this.isInherited = isInherited;
}
@ -25,7 +32,21 @@ public class Constraint<A> extends HashSet<A> {
return isInherited;
}
public String toString() {
return super.toString() + " isInherited = " + isInherited + "\n";
public Constraint<A> getExtendConstraint() {
return extendConstraint;
}
public void setExtendConstraint(Constraint<A> c) {
extendConstraint = c;
}
public String toString() {
return super.toString() + " isInherited = " + isInherited + "\nextendsContraint: " +
(extendConstraint != null ? extendConstraint.toStringBase() : "null" ) + "\n" ;
}
public String toStringBase() {
return super.toString();
}
}

View File

@ -45,20 +45,49 @@ public class ConstraintSet<A> {
return new GuavaSetOperations().cartesianProduct(allConstraints);
}
public <B> ConstraintSet<B> map(Function<? super A,? extends B> o) {
public <B> ConstraintSet<B> map(Function<? super A, ? extends B> o) {
Hashtable<Constraint<A>,Constraint<B>> CSA2CSB = new Hashtable<>();
ConstraintSet<B> ret = new ConstraintSet<>();
ret.undConstraints = undConstraints.stream().map(o).collect(Collectors.toCollection(Constraint<B>::new));
List<Set<Constraint<B>>> newOder = new ArrayList<>();
/*
for(Set<Constraint<A>> oderConstraint : oderConstraints){
oderConstraint.forEach(as -> {
Constraint<B> newConst = as.stream()
.map(o)
.collect(Collectors.toCollection(
() -> new Constraint<B> (as.isInherited())));
CSA2CSB.put(as, newConst);} );
}
*/
for(Set<Constraint<A>> oderConstraint : oderConstraints){
newOder.add(
oderConstraint.parallelStream().map((Constraint<A> as) -> {
Boolean isInherited = as.isInherited();
Constraint<B> newConst = as.stream().map(o).collect(Collectors.toCollection(Constraint<B>::new));
newConst.setIsInherited(isInherited);
Constraint<B> newConst = as.stream()
.map(o)
.collect(Collectors.toCollection((as.getExtendConstraint() != null)
? () -> new Constraint<B> (as.isInherited(),
as.getExtendConstraint().stream().map(o).collect(Collectors.toCollection(Constraint::new)))
: () -> new Constraint<B> (as.isInherited())
));
//CSA2CSB.put(as, newConst);
return newConst;
/*
Constraint<B> bs = CSA2CSB.get(as);
if (as.getExtendConstraint() != null) {
bs.setExtendConstraint(CSA2CSB.get(as.getExtendConstraint()));
}
return bs;
*/
}).collect(Collectors.toSet())
);
}
ret.oderConstraints = newOder;
return ret;
}

View File

@ -16,8 +16,10 @@ import de.dhbwstuttgart.typeinference.assumptions.FunNClass;
import de.dhbwstuttgart.typeinference.assumptions.MethodAssumption;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.*;
import de.dhbwstuttgart.typeinference.unify.model.ExtendsType;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
import java.util.*;
import java.util.stream.Collectors;
@ -168,16 +170,23 @@ public class TYPEStmt implements StatementVisitor{
Set<Constraint<Pair>> methodConstraints = new HashSet<>();
for(MethodAssumption m : this.getMethods(methodCall.name, methodCall.arglist, info)){
GenericsResolver resolver = getResolverInstance();
methodConstraints.add(generateConstraint(methodCall, m, info, resolver));
/* geloescht PL 2020-05-01
resolver = getResolverInstance();
Constraint<Pair> oneMethodConstraint = generateConstraint(methodCall, m, info, resolver);
oneMethodConstraint = oneMethodConstraint.stream().map(x -> (x.TA1 instanceof TypePlaceholder && x.GetOperator() == PairOperator.EQUALSDOT &&
!(x.TA2 instanceof TypePlaceholder)) ?
new Pair(x.TA1, new ExtendsWildcardType(x.TA2, x.TA2.getOffset()), PairOperator.EQUALSDOT) :
x).collect(Collectors.toCollection(() -> new Constraint<Pair>(true)));
methodConstraints.add(oneMethodConstraint);
*/
// geloescht PL 2020-05-01
//resolver = getResolverInstance();
//oneMethodConstraint = generateConstraint(methodCall, m, info, resolver);
//Boolean oneMethodConstraint_isInherited = oneMethodConstraint.isInherited();
Constraint<Pair> extendsOneMethodConstraint = oneMethodConstraint.stream()
.map(x -> (x.TA1 instanceof TypePlaceholder &&
x.GetOperator() == PairOperator.EQUALSDOT &&
!(x.TA2 instanceof TypePlaceholder))
? new Pair(x.TA1, new ExtendsWildcardType(x.TA2, x.TA2.getOffset()), PairOperator.EQUALSDOT)
: x)
.collect(Collectors.toCollection(() -> new Constraint<Pair>(true)));
oneMethodConstraint.setExtendConstraint(extendsOneMethodConstraint);
extendsOneMethodConstraint.setExtendConstraint(oneMethodConstraint);
methodConstraints.add(extendsOneMethodConstraint);
}
if(methodConstraints.size()<1){
throw new TypeinferenceException("Methode "+methodCall.name+" ist nicht vorhanden!",methodCall.getOffset());

View File

@ -680,7 +680,12 @@ public class RuleSet implements IRuleSet{
result1 = result1.stream().map(x -> uni.apply(pair,x)).collect(Collectors.toCollection(LinkedList::new));
Function<? super Constraint<UnifyPair>,? extends Constraint<UnifyPair>> applyUni = b -> b.stream().map(
x -> uni.apply(pair,x)).collect(Collectors.toCollection(() -> new Constraint<UnifyPair>(b.isInherited())));
x -> uni.apply(pair,x)).collect(Collectors.toCollection((b.getExtendConstraint() != null)
? () -> new Constraint<UnifyPair>(
b.isInherited(),
b.getExtendConstraint().stream().map(x -> uni.apply(pair,x)).collect(Collectors.toCollection(Constraint::new)))
: () -> new Constraint<UnifyPair>(b.isInherited())
));
oderConstraints.replaceAll(oc -> oc.stream().map(applyUni).collect(Collectors.toCollection(HashSet::new)));
/*
oderConstraints = oderConstraints.stream().map(

View File

@ -588,8 +588,9 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
return result;
}
Set<? extends Set<UnifyPair>> nextSet = remainingSets.remove(0);
writeLog("nextSet: " + nextSet.toString());
//writeLog("nextSet: " + nextSet.toString());
List<Set<UnifyPair>> nextSetasList =new ArrayList<>(nextSet);
/*
try {
//List<Set<UnifyPair>>
//nextSetasList = oup.sortedCopy(nextSet);//new ArrayList<>(nextSet);
@ -597,6 +598,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
catch (java.lang.IllegalArgumentException e) {
System.out.print("");
}
*/
Set<Set<UnifyPair>> result = new HashSet<>();
int variance = 0;
@ -654,9 +656,9 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
System.out.print("");
if (nextSetasList.iterator().next().stream().filter(x -> x.getLhsType().getName().equals("D")).findFirst().isPresent() && nextSetasList.size()>1)
System.out.print("");
writeLog("nextSetasList: " + nextSetasList.toString());
//writeLog("nextSetasList: " + nextSetasList.toString());
Set<UnifyPair> nextSetElem = nextSetasList.get(0);
writeLog("BasePair1: " + nextSetElem + " " + nextSetElem.iterator().next().getBasePair());
//writeLog("BasePair1: " + nextSetElem + " " + nextSetElem.iterator().next().getBasePair());
/* sameEqSet-Bestimmung: Wenn a = ty \in nextSet dann enthaelt sameEqSet alle Paare a < ty1 oder ty2 < a aus fstElems */
Set<UnifyPair> sameEqSet = new HashSet<>();
@ -700,52 +702,15 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
List<Set<UnifyPair>> nextSetasListOderConstraints = new ArrayList<>();
//List<Set<UnifyPair>> nextSetasListRestMin = new ArrayList<>();
//List<Set<UnifyPair>> nextSetasListRestOder = new ArrayList<>();
writeLog("qextSet: " + nextSet.toString());
writeLog("WhileAnfangNextSetasList: " + nextSetasList.toString());
writeLog("nextSet: " + nextSet.toString());
writeLog("nextSetasList: " + nextSetasList.toString());
if (variance == 1) {
a = oup.max(nextSetasList.iterator());
Set<UnifyPair> a_final = a = oup.max(nextSetasList.iterator());
nextSetasList.remove(a);
if (oderConstraint && nextSetasListOderConstraints.isEmpty()) {
Set<UnifyPair> a_final = a;
Optional<UnifyPair> opt_a_rec = a
.stream()
.filter(x -> x.getGroundBasePair().getLhsType() instanceof PlaceholderType &&
! (x.getRhsType() instanceof PlaceholderType) &&
x.getPairOp() == PairOperator.EQUALSDOT).findAny();
if (opt_a_rec.isPresent()) {
a_rec = opt_a_rec.get();
PlaceholderType phd;
if ((phd = ((PlaceholderType)a_rec.getGroundBasePair().getLhsType())).isWildcardable()) {
Set<UnifyPair> a_super = a.stream()
.map(x -> new UnifyPair(x.getLhsType(), x.getRhsType(), x.getPairOp()))
.collect(Collectors.toCollection(() -> new Constraint<>(((Constraint<UnifyPair>)a_final).isInherited())));
a_super.remove(a_rec);
a_super.add(new UnifyPair(a_rec.getLhsType(), new SuperType(a_rec.getRhsType()), a_rec.getPairOp()));
Set<UnifyPair> a_extends = a.stream()
.map(x -> new UnifyPair(x.getLhsType(), x.getRhsType(), x.getPairOp()))
.collect(Collectors.toCollection(() -> new Constraint<>(((Constraint<UnifyPair>)a_final).isInherited())));
a_extends.remove(a_rec);
a_extends.add(new UnifyPair(a_rec.getLhsType(), new ExtendsType(a_rec.getRhsType()), a_rec.getPairOp()));
if (phd.isInnerType()) {
nextSetasList.add(a_super);
nextSetasListOderConstraints.add(a_super);
nextSetasList.add(a);
nextSetasListOderConstraints.add(a);
a = a_extends;
}
else {
nextSetasList.add(a_extends);
nextSetasListOderConstraints.add(a_extends);
nextSetasList.add(a);
nextSetasListOderConstraints.add(a);
a = a_super;
}
}
}
}
else {
nextSetasListOderConstraints.remove(a);
}
if (oderConstraint) {
nextSetasListOderConstraints.add(((Constraint<UnifyPair>)a).getExtendConstraint());
}
writeLog("nextSetasListOderConstraints: " + nextSetasListOderConstraints);
nextSetasListRest = new ArrayList<>(nextSetasList);
Iterator<Set<UnifyPair>> nextSetasListItRest = new ArrayList<Set<UnifyPair>>(nextSetasListRest).iterator();
while (nextSetasListItRest.hasNext()) {

View File

@ -655,6 +655,7 @@ implements IFiniteClosure {
*/
public int compare (UnifyType left, UnifyType right, PairOperator pairop) {
//try {logFile.write("left: "+ left + " right: " + right + " pairop: " + pairop);} catch (IOException ie) {}
if (left.getName().equals("Matrix") || right.getName().equals("Matrix"))
System.out.println("");
/*
@ -746,7 +747,10 @@ implements IFiniteClosure {
//Gleichungen der Form a <./=. Theta oder Theta <./=. a oder a <./=. b sind ok.
long greaterLen = greaterRes.stream().filter(delFun).count();
if (greaterLen == 0) return 1;
else return 0;
else {
//try {logFile.write("0 left: "+ left + " right: " + right + " pairop: " + pairop);} catch (IOException ie) {}
return 0;
}
}
}
}

View File

@ -242,8 +242,8 @@ public class OrderingUnifyPair extends OrderingExtend<Set<UnifyPair>> {
.filter(x -> (x.getPairOp() == PairOperator.SMALLERDOT))
.collect(Collectors.toCollection(HashSet::new));
/* synchronized(this) {
/*
synchronized(this) {
try {
((FiniteClosure)fc).logFile.write("leftBase: " + leftBase.toString() +"\n");
((FiniteClosure)fc).logFile.write("rightBase: " + rightBase.toString() +"\n\n");
@ -262,6 +262,7 @@ public class OrderingUnifyPair extends OrderingExtend<Set<UnifyPair>> {
}
*/
Integer compareEq;
if (lefteqOder.size() == 1 && righteqOder.size() == 1 && lefteqRet.size() == 1 && righteqRet.size() == 1) {
Match m = new Match();
@ -274,26 +275,51 @@ public class OrderingUnifyPair extends OrderingExtend<Set<UnifyPair>> {
return new UnifyPair(x.getRhsType(), leftElem.getRhsType(), PairOperator.EQUALSDOT);})
.collect(Collectors.toCollection(ArrayList::new));
if (m.match(matchList).isPresent()) {
//try { ((FiniteClosure)fc).logFile.write("result1: -1 \n\n"); } catch (IOException ie) {}
return -1;
}
else {
//try { ((FiniteClosure)fc).logFile.write("result1: 0 \n\n"); } catch (IOException ie) {}
return 0;
}
} else if (compareEq == 1) {
ArrayList<UnifyPair> matchList =
leftleOder.stream().map(x -> {
UnifyPair rightElem = rightleOder.stream()
.filter(y -> y.getGroundBasePair().getLhsType().equals(x.getGroundBasePair().getLhsType()))
.filter(y ->
y.getGroundBasePair().getLhsType().equals(x.getGroundBasePair().getLhsType()))
.findAny().get();
return new UnifyPair(x.getRhsType(), rightElem.getRhsType(), PairOperator.EQUALSDOT);})
.collect(Collectors.toCollection(ArrayList::new));
if (m.match(matchList).isPresent()) {
//try { ((FiniteClosure)fc).logFile.write("result2: 1 \n\n"); } catch (IOException ie) {}
return 1;
}
else {
//try { ((FiniteClosure)fc).logFile.write("result2: 0 \n\n"); } catch (IOException ie) {}
return 0;
}
} else {
/*
synchronized(this) {
try {
((FiniteClosure)fc).logFile.write("leftBase: " + leftBase.toString() +"\n");
((FiniteClosure)fc).logFile.write("rightBase: " + rightBase.toString() +"\n\n");
((FiniteClosure)fc).logFile.write("left: " + left.toString() +"\n");
((FiniteClosure)fc).logFile.write("right: " + right.toString() +"\n\n");
((FiniteClosure)fc).logFile.write("lefteqOder: " + lefteqOder.toString() +"\n");
((FiniteClosure)fc).logFile.write("righteqOder: " + righteqOder.toString() +"\n\n");
((FiniteClosure)fc).logFile.write("lefteqRet: " + lefteqRet.toString() +"\n");
((FiniteClosure)fc).logFile.write("righteqRet: " + righteqRet.toString() +"\n\n");
((FiniteClosure)fc).logFile.write("leftleOder: " + leftleOder.toString() +"\n");
((FiniteClosure)fc).logFile.write("rightleOder: " + rightleOder.toString() +"\n\n");
((FiniteClosure)fc).logFile.write("result3: 0 \n\n");
((FiniteClosure)fc).logFile.flush();
}
catch (IOException ie) {
}
}
*/
return 0;
}
}