unifyTest

This commit is contained in:
Florian Steurer 2015-11-23 01:03:01 +01:00
parent 7ff9554d78
commit 97e0e2fc72
2 changed files with 51 additions and 8 deletions

View File

@ -30,14 +30,13 @@ import de.dhbwstuttgart.typinference.unify.model.Type;
*/ */
public class Unify { public class Unify {
public Menge<Menge<Pair>> unify(Menge<MPair> eq, IFiniteClosure fc) { public Menge<Menge<Pair>> unify(Set<MPair> eq, IFiniteClosure fc) {
/* /*
* Step 1: Repeated application of reduce, adapt, erase, swap * Step 1: Repeated application of reduce, adapt, erase, swap
*/ */
Set<MPair> eq0 = applyTypeUnificationRules(eq, fc); Set<MPair> eq0 = applyTypeUnificationRules(eq, fc);
/* /*
* Step 2 and 3: Create a subset eq1s of pairs where both sides are TPH and eq2s of the other pairs * Step 2 and 3: Create a subset eq1s of pairs where both sides are TPH and eq2s of the other pairs
*/ */
@ -52,7 +51,7 @@ public class Unify {
// Sets that originate from pair pattern matching // Sets that originate from pair pattern matching
// Sets of the "second level" // Sets of the "second level"
List<List<Set<MPair>>> pairSets = calculatePairSets(eq2s, fc); List<List<Set<MPair>>> pairSetsSet = calculatePairSets(eq2s, fc);
// The sets of the "first level" // The sets of the "first level"
Set<Set<MPair>> sets = new HashSet<Set<MPair>>(); Set<Set<MPair>> sets = new HashSet<Set<MPair>>();
@ -70,15 +69,15 @@ public class Unify {
// Calculate the inner cartesian products // Calculate the inner cartesian products
// Cartesian products of the second level // Cartesian products of the second level
for(List<Set<MPair>> pairSet : pairSets) // Prüfen ob addAll stimmt oder ob hier eigentlich nur 1 set sein sollte for(List<Set<MPair>> pairSets : pairSetsSet) // Prüfen ob addAll stimmt oder ob hier eigentlich nur 1 set sein sollte
setOps.cartesianProduct(pairSet).forEach(x -> sets.add(new HashSet<MPair>(x))); setOps.cartesianProduct(pairSets).forEach(x -> sets.add(new HashSet<MPair>(x)));
// Calculate the outer cartesian products // Calculate the outer cartesian products
// Cartesian products of the first level // Cartesian products of the first level
Set<List<MPair>> eqsSet = setOps.cartesianProduct(new ArrayList<>(sets)); Set<List<MPair>> eqsSet = setOps.cartesianProduct(new ArrayList<>(sets));
/* /*
* Step 5: Repeated substitution * Step 5: Substitution
*/ */
@ -122,7 +121,7 @@ public class Unify {
* Apply rules until the queue is empty * Apply rules until the queue is empty
*/ */
while(!eqQueue.isEmpty()) { while(!eqQueue.isEmpty()) {
MPair pair = eqQueue.getFirst(); MPair pair = eqQueue.pollFirst();
// ReduceUp, ReduceLow, ReduceUpLow // ReduceUp, ReduceLow, ReduceUpLow
Optional<MPair> opt = rules.reduceUpLow(pair); Optional<MPair> opt = rules.reduceUpLow(pair);

View File

@ -1,7 +1,51 @@
package unify; package unify;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
import de.dhbwstuttgart.typeinference.unifynew.Unify; import de.dhbwstuttgart.typeinference.unifynew.Unify;
import de.dhbwstuttgart.typinference.unify.model.MPair;
import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator;
public class UnifyTest extends Unify { public class UnifyTest extends Unify {
@Test
public void unifyTest() {
TypeFactory tf = new TypeFactory();
FiniteClosureBuilder fcb = new FiniteClosureBuilder();
Set<MPair> eq = new HashSet<MPair>();
fcb.add(tf.getSimpleType("Number"), tf.getSimpleType("Object"));
fcb.add(tf.getSimpleType("Integer"), tf.getSimpleType("Number"));
fcb.add(tf.getSimpleType("Double"), tf.getSimpleType("Number"));
IFiniteClosure fc = fcb.getCollectionExample();
// Vector<Integer> <. Vector<A>
// A <. Number
// 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.getPlaceholderType("A"), tf.getSimpleType("Number"), 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));
this.unify(eq, fc);
}
@Test
public void applyTypeUnificationRulesTest() {
}
@Test
public void calculatePairSetsTest() {
}
} }