diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnifier.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnifier.java index ca0d6ce2..da18fa8f 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnifier.java +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnifier.java @@ -1,9 +1,9 @@ package de.dhbwstuttgart.typeinference.unify.interfaces; -import java.util.Optional; +import java.util.function.Function; import de.dhbwstuttgart.typinference.unify.model.MPair; -public interface IUnifier { - public Optional apply(MPair pair); +public interface IUnifier extends Function{ + } diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java index 2047b150..1313b6fa 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java @@ -13,8 +13,11 @@ import de.dhbwstuttgart.typeinference.Pair; import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet; +import de.dhbwstuttgart.typeinference.unify.interfaces.IUnifier; import de.dhbwstuttgart.typinference.unify.model.MPair; import de.dhbwstuttgart.typinference.unify.model.PlaceholderType; +import de.dhbwstuttgart.typinference.unify.model.SimpleType; +import de.dhbwstuttgart.typinference.unify.model.Type; /** * Implementierung des Unifikationsalgorithmus. @@ -142,4 +145,11 @@ public class Unify { collection.add(pair2); } + + private void test() { + Type t = new PlaceholderType("T"); + Type subst = new SimpleType("Subst"); + + IUnifier u = x -> x.substitute(t, subst); + } } diff --git a/src/de/dhbwstuttgart/typinference/unify/model/MPair.java b/src/de/dhbwstuttgart/typinference/unify/model/MPair.java index 0088d02a..9a76f2a9 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/MPair.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/MPair.java @@ -66,6 +66,23 @@ public class MPair { && other.getRhsType().equals(rhs); } + /** + * Substitutes the occurrences of Type t on the left or right side of the pair with the Type subst. + * @param t Type to be replaced. + * @param subst The type replacing t. + * @return A pair where occurrences of t are replaced by subst. + */ + public MPair substitute(Type t, Type subst) { + Type newlhs = lhs; + if(lhs.equals(t)) newlhs = subst; + + Type newrhs = rhs; + if(rhs.equals(t)) newrhs = subst; + + if(newlhs == lhs && newrhs == rhs) return this; + return new MPair(newlhs, newrhs, pairOp); + } + @Override public int hashCode() { return 17 + 31 * lhs.hashCode() + 31 * rhs.hashCode() + 31 * pairOp.hashCode(); diff --git a/test/unify/UnifyTest.java b/test/unify/UnifyTest.java new file mode 100644 index 00000000..747374b2 --- /dev/null +++ b/test/unify/UnifyTest.java @@ -0,0 +1,7 @@ +package unify; + +import de.dhbwstuttgart.typeinference.unifynew.Unify; + +public class UnifyTest extends Unify { + +}