diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java index d8354f53..64313fb7 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java @@ -47,14 +47,13 @@ public class Unify { /* * Step 4: Create possible typings + * + * "Manche Autoren identifizieren die Paare (a, (b,c)) und ((a,b),c) + * mit dem geordneten Tripel (a,b,c), wodurch das kartesische Produkt auch assoziativ wird." - Wikipedia */ - // Sets that originate from pair pattern matching - // Sets of the "second level" - List>> pairSetsSet = calculatePairSets(eq2s, fc); - - // The sets of the "first level" - Set> sets = new HashSet>(); + // All Sets + List> sets = new ArrayList>(); if(eq1s.size() != 0) sets.add(eq1s); // Add Eq1' @@ -67,26 +66,23 @@ public class Unify { if(bufferSet.size() != 0) sets.add(bufferSet); + // Sets that originate from pair pattern matching + // Sets of the "second level" + sets.addAll(calculatePairSets(eq2s, fc)); + + /* Up to here, no cartesian products are calculated. * Around here, filters for pairs and sets can be applied */ ISetOperations setOps = new GuavaSetOperations(); - // Calculate the inner cartesian products - // Cartesian products of the second level - - // AddAll -> nur add - for(List> pairSets : pairSetsSet) // Prüfen ob addAll stimmt oder ob hier eigentlich nur 1 set sein sollte - sets.add(setOps.cartesianProduct(pairSets).stream().map(x -> new HashSet<>(x)).collect(Collectors.toSet())); - - System.out.println(sets); - - // Calculate the outer cartesian products - // Cartesian products of the first level - Set> eqsSet = setOps.cartesianProduct(new ArrayList<>(sets)); + // Calculate the cartesian products - System.out.println(eqsSet); + Set> result = setOps.cartesianProduct(sets).stream().map(x -> new HashSet(x)).collect(Collectors.toCollection(HashSet::new)); + + System.out.println(result); + /* * Step 5: Substitution */ @@ -196,11 +192,8 @@ public class Unify { } - protected List>> calculatePairSets(Set eq2s, IFiniteClosure fc) { - List>> result = new ArrayList>>(); - for(int i = 0; i < 8; i++) - result.add(new ArrayList>()); - + protected List> calculatePairSets(Set eq2s, IFiniteClosure fc) { + List> result = new ArrayList>(); for(MPair pair : eq2s) { @@ -223,15 +216,14 @@ public class Unify { Set set = new HashSet<>(); for(Type theta : fc.smArg(rhsType)) set.add(new MPair(lhsType, theta, PairOperator.EQUALSDOT)); - - result.get(2).add(set); + result.add(set); } // Case 4: (a <.? Theta') else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType) { Set set = new HashSet<>(); set.add(new MPair(lhsType, ((ExtendsType) rhsType).getExtendedType(), PairOperator.EQUALSDOT)); - result.get(3).add(set); + result.add(set); } // Case 5: (Theta <. a) @@ -239,7 +231,7 @@ public class Unify { Set set = new HashSet<>(); for(Type thetaS : fc.greater(lhsType)) set.add(new MPair(rhsType, thetaS, PairOperator.EQUALSDOT)); - result.get(4).add(set); + result.add(set); } // Case 6: (? ext Theta <.? a) @@ -247,7 +239,7 @@ public class Unify { Set set = new HashSet<>(); for(Type thetaS : fc.grArg(lhsType)) set.add(new MPair(rhsType, thetaS, PairOperator.EQUALSDOT)); - result.get(5).add(set); + result.add(set); } // Case 7: (? sup Theta <.? a) @@ -260,10 +252,10 @@ public class Unify { Set set = new HashSet<>(); for(Type thetaS : fc.grArg(lhsType)) set.add(new MPair(rhsType, thetaS, PairOperator.EQUALSDOT)); - result.get(7).add(set); + result.add(set); } } - return result.stream().filter(x -> !x.isEmpty()).collect(Collectors.toList()); + return result; } } diff --git a/test/unify/UnifyTest.java b/test/unify/UnifyTest.java index 38d5a1b9..bab837df 100644 --- a/test/unify/UnifyTest.java +++ b/test/unify/UnifyTest.java @@ -18,7 +18,7 @@ public class UnifyTest extends Unify { FiniteClosureBuilder fcb = new FiniteClosureBuilder(); Set eq = new HashSet(); - fcb.add(tf.getSimpleType("Number"), tf.getSimpleType("Object")); + //fcb.add(tf.getSimpleType("Number"), tf.getSimpleType("Object")); fcb.add(tf.getSimpleType("Integer"), tf.getSimpleType("Number")); fcb.add(tf.getSimpleType("Double"), tf.getSimpleType("Number")); @@ -26,15 +26,15 @@ public class UnifyTest extends Unify { // Vector <. Vector // Vector - // A <. Number + // A <. Integer // Double <. B // B <. Object eq.add(new MPair(tf.getSimpleType("Vector", tf.getSimpleType("Integer")), tf.getSimpleType("Vector", "A"), PairOperator.SMALLERDOT)); eq.add(new MPair(tf.getSimpleType("Vector", tf.getSimpleType("Integer")), tf.getSimpleType("Vector", "C"), PairOperator.SMALLERDOT)); - eq.add(new MPair(tf.getPlaceholderType("A"), tf.getSimpleType("Number"), PairOperator.SMALLERDOT)); + eq.add(new MPair(tf.getPlaceholderType("A"), tf.getSimpleType("Integer"), PairOperator.SMALLERDOT)); //eq.add(new MPair(tf.getPlaceholderType("A"), tf.getPlaceholderType("C"), PairOperator.SMALLERDOT)); eq.add(new MPair(tf.getSimpleType("Double"), tf.getPlaceholderType("B"), PairOperator.SMALLERDOT)); - eq.add(new MPair(tf.getPlaceholderType("B"), tf.getSimpleType("Object"), PairOperator.EQUALSDOT)); + //eq.add(new MPair(tf.getPlaceholderType("B"), tf.getSimpleType("Object"), PairOperator.EQUALSDOT)); this.unify(eq, fc);