forked from JavaTX/JavaCompilerCore
Unfifier
This commit is contained in:
parent
01ef0e9385
commit
c83697dedb
@ -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>{
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
|
7
test/unify/UnifyTest.java
Normal file
7
test/unify/UnifyTest.java
Normal file
@ -0,0 +1,7 @@
|
||||
package unify;
|
||||
|
||||
import de.dhbwstuttgart.typeinference.unifynew.Unify;
|
||||
|
||||
public class UnifyTest extends Unify {
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user