diff --git a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java index 9ce84a43..e18e6156 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java @@ -376,8 +376,8 @@ public class RuleSet implements IRuleSet{ if(!(typeDs instanceof ReferenceType)) return Optional.empty(); - if(typeD.getTypeParams().size() == 0 || typeDs.getTypeParams().size() == 0) - return Optional.empty(); + /*if(typeD.getTypeParams().size() == 0 || typeDs.getTypeParams().size() == 0) + return Optional.empty();*/ if(typeD.getName().equals(typeDs.getName())) return Optional.empty(); @@ -402,8 +402,8 @@ public class RuleSet implements IRuleSet{ TypeParams typeDParams = typeD.getTypeParams(); TypeParams typeDgenParams = typeDgen.getTypeParams(); - Unifier unif = new Unifier((PlaceholderType) typeDgenParams.get(0), typeDParams.get(0)); - for(int i = 1; i < typeDParams.size(); i++) + Unifier unif = Unifier.Identity(); + for(int i = 0; i < typeDParams.size(); i++) unif.Add((PlaceholderType) typeDgenParams.get(i), typeDParams.get(i)); return Optional.of(new UnifyPair(unif.apply(newLhs), typeDs, PairOperator.SMALLERDOT)); diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/FunNType.java b/src/de/dhbwstuttgart/typeinference/unify/model/FunNType.java index e62fd6ce..cb431cf9 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/FunNType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/FunNType.java @@ -6,17 +6,28 @@ import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; public class FunNType extends UnifyType { - public FunNType(TypeParams p) { + protected FunNType(TypeParams p) { super("FuN", p); - if(p.size() == 0) - throw new IllegalArgumentException("Function types need at least one type parameter"); } + public static FunNType getFunNType(TypeParams tp) { + if(!validateTypeParams(tp)) + throw new IllegalArgumentException("Invalid TypeParams for a FunNType: " + tp); + return new FunNType(tp); + } + + private static boolean validateTypeParams(TypeParams tp) { + if(tp.size() == 0) + return false; + for(UnifyType t : tp) + if(t instanceof WildcardType) + return false; + return true; + } + @Override public UnifyType setTypeParams(TypeParams newTp) { - if(newTp.size() == 0) - throw new IllegalArgumentException("Function types need at least one type parameter"); - return new FunNType(newTp); + return getFunNType(newTp); } public int getN() { diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/Node.java b/src/de/dhbwstuttgart/typeinference/unify/model/Node.java index 7010ec32..d338d16b 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/Node.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/Node.java @@ -4,7 +4,7 @@ import java.util.HashSet; import java.util.Set; import java.util.stream.Collectors; -public class Node { +class Node { private T content; private HashSet> predecessors = new HashSet<>(); diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java b/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java index a46ee6d3..33f9daec 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java @@ -45,7 +45,7 @@ public class Unifier implements Function /*, Set*/ return substitutions.containsKey(t); } - public UnifyType getSubstitute(UnifyType t) { + public UnifyType getSubstitute(PlaceholderType t) { return substitutions.get(t); } diff --git a/test/unify/UnifyTest.java b/test/unify/UnifyTest.java index 0ee466e4..3d091f5e 100644 --- a/test/unify/UnifyTest.java +++ b/test/unify/UnifyTest.java @@ -380,13 +380,39 @@ public class UnifyTest extends Unify { //System.out.println(actual); //Assert.assertEquals(actual, expected); - - } @Test public void unifyTestComplex() { + /* + * INIT + */ + TypeFactory tf = new TypeFactory(); + FiniteClosureBuilder fcb = new FiniteClosureBuilder(); + + UnifyType tphT = tf.getPlaceholderType("T"); + UnifyType vector = tf.getSimpleType("Vector", tphT); + UnifyType number = tf.getSimpleType("Number"); + UnifyType integer = tf.getSimpleType("Integer"); + UnifyType object = tf.getSimpleType("Object"); + UnifyType matrix = tf.getSimpleType("Matrix"); + UnifyType vectorvectorint = tf.getSimpleType("Vector", tf.getSimpleType("Vector", integer)); + + fcb.add(integer, number); + fcb.add(matrix, vectorvectorint); + fcb.add(vector, object); + + IFiniteClosure fc = fcb.getFiniteClosure(); + + Set eq = new HashSet(); + eq.add(new UnifyPair(tf.getSimpleType("Matrix"), tf.getSimpleType("Vector", tf.getPlaceholderType("a")), PairOperator.SMALLERDOT)); + + Set> expected = new HashSet<>(); + Set> actual = unify(eq, fc); + + System.out.println("Test Matrix:"); + System.out.println(actual); } @Test