diff --git a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index c848a068..2c2ca6b2 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -312,7 +312,14 @@ public class TypeUnifyTask extends RecursiveTask>> { ArrayList>> remainingSets = new ArrayList<>(topLevelSets); Set> nextSet = remainingSets.remove(0); writeLog("nextSet: " + nextSet.toString()); - List> nextSetasList = oup.sortedCopy(nextSet);//new ArrayList<>(nextSet); + List> nextSetasList =new ArrayList<>(nextSet); + try { + //List> + nextSetasList = oup.sortedCopy(nextSet);//new ArrayList<>(nextSet); + } + catch (java.lang.IllegalArgumentException e) { + System.out.print(""); + } Set> result = new HashSet<>(); int variance = 0; Optional xi = nextSetasList.stream().map(x -> x.stream().filter(y -> y.getLhsType() instanceof PlaceholderType) @@ -615,8 +622,20 @@ public class TypeUnifyTask extends RecursiveTask>> { // Init all 8 cases for(int i = 0; i < 8; i++) result.add(new HashSet<>()); + ArrayList eq2sprime = new ArrayList<>(eq2s); + Iterator eq2sprimeit = eq2sprime.iterator(); + ArrayList eq2sAsList = new ArrayList<>(); + while(eq2sprimeit.hasNext()) {// alle mit Variance != 0 nach vorne schieben + UnifyPair up = eq2sprimeit.next(); + if ((up.getLhsType() instanceof PlaceholderType && ((PlaceholderType)up.getLhsType()).getVariance() != 0) + || (up.getRhsType() instanceof PlaceholderType && ((PlaceholderType)up.getRhsType()).getVariance() != 0)) { + eq2sAsList.add(up); + eq2s.remove(up); + } + } + eq2sAsList.addAll(eq2s); Boolean first = true; - for(UnifyPair pair : eq2s) { + for(UnifyPair pair : eq2sAsList) { PairOperator pairOp = pair.getPairOp(); UnifyType lhsType = pair.getLhsType(); UnifyType rhsType = pair.getRhsType(); diff --git a/src/de/dhbwstuttgart/typeinference/unify/distributeVariance.java b/src/de/dhbwstuttgart/typeinference/unify/distributeVariance.java new file mode 100644 index 00000000..21fffde5 --- /dev/null +++ b/src/de/dhbwstuttgart/typeinference/unify/distributeVariance.java @@ -0,0 +1,19 @@ +package de.dhbwstuttgart.typeinference.unify; + +import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; + +public class distributeVariance extends visitUnifyTypeVisitor { + + @Override + public PlaceholderType visit(PlaceholderType phty, Integer ht) { + if (ht != 0) { + if (phty.getVariance() == 0) { + phty.setVariance(ht); + } + else if (phty.getVariance() != ht) { + phty.setVariance(0); + } + } + return phty; + } +} diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java b/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java index 21e8a66b..8d9b62df 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java @@ -7,6 +7,7 @@ import java.util.HashSet; import java.util.Random; import java.util.Set; +import de.dhbwstuttgart.typeinference.unify.distributeVariance; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.interfaces.UnifyTypeVisitor; @@ -119,8 +120,11 @@ public final class PlaceholderType extends UnifyType{ @Override UnifyType apply(Unifier unif) { - if(unif.hasSubstitute(this)) - return unif.getSubstitute(this); + if(unif.hasSubstitute(this)) { + UnifyType ret = unif.getSubstitute(this); + ret.accept(new distributeVariance(), this.getVariance()); + return ret; + } return this; } diff --git a/src/de/dhbwstuttgart/typeinference/unify/visitUnifyTypeVisitor.java b/src/de/dhbwstuttgart/typeinference/unify/visitUnifyTypeVisitor.java new file mode 100644 index 00000000..9ea3dc59 --- /dev/null +++ b/src/de/dhbwstuttgart/typeinference/unify/visitUnifyTypeVisitor.java @@ -0,0 +1,47 @@ +package de.dhbwstuttgart.typeinference.unify; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.HashMap; +import java.util.stream.Collectors; + +import de.dhbwstuttgart.typeinference.unify.interfaces.UnifyTypeVisitor; +import de.dhbwstuttgart.typeinference.unify.model.ExtendsType; +import de.dhbwstuttgart.typeinference.unify.model.FunNType; +import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; +import de.dhbwstuttgart.typeinference.unify.model.ReferenceType; +import de.dhbwstuttgart.typeinference.unify.model.SuperType; +import de.dhbwstuttgart.typeinference.unify.model.TypeParams; + +public class visitUnifyTypeVisitor implements UnifyTypeVisitor { + + public ReferenceType visit(ReferenceType refty, T ht) { + return new ReferenceType(refty.getName(), + new TypeParams( + Arrays.stream(refty.getTypeParams().get()) + .map(x -> x.accept(this, ht)) + .collect(Collectors.toCollection(ArrayList::new)))); + } + + public PlaceholderType visit(PlaceholderType phty, T ht) { + return phty; + } + + public FunNType visit(FunNType funnty, T ht) { + return FunNType.getFunNType( + new TypeParams( + Arrays.stream(funnty.getTypeParams().get()) + .map(x -> x.accept(this, ht)) + .collect(Collectors.toCollection(ArrayList::new))) + ); + } + + public SuperType visit(SuperType suty, T ht) { + return new SuperType(suty.getWildcardedType().accept(this, ht)); + } + + public ExtendsType visit(ExtendsType extty, T ht) { + return new ExtendsType(extty.getWildcardedType().accept(this, ht)); + } +}