This commit is contained in:
Florian Steurer 2015-11-09 14:39:26 +01:00
parent 01ef0e9385
commit c83697dedb
4 changed files with 37 additions and 3 deletions

View File

@ -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<MPair> apply(MPair pair);
public interface IUnifier extends Function<MPair, MPair>{
}

View File

@ -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);
}
}

View File

@ -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();

View File

@ -0,0 +1,7 @@
package unify;
import de.dhbwstuttgart.typeinference.unifynew.Unify;
public class UnifyTest extends Unify {
}