martelli montanari unification (unfinished)

This commit is contained in:
Florian Steurer 2015-12-21 15:44:13 +01:00
parent 53dd7b019e
commit ab7f56db6f
3 changed files with 101 additions and 0 deletions

View File

@ -0,0 +1,14 @@
package de.dhbwstuttgart.typeinference.unify.interfaces;
import java.util.Set;
import de.dhbwstuttgart.typeinference.unifynew.Unifier;
import de.dhbwstuttgart.typinference.unify.model.MPair;
/**
* Standard unification algorithm (e.g. Robinson, Paterson-Wegman, Martelli-Montanari, Ruzicka-Privara or Suciu)
* @author Florian Steurer
*/
public interface IUnify {
public Unifier unify(Set<MPair> terms);
}

View File

@ -0,0 +1,80 @@
package de.dhbwstuttgart.typeinference.unifynew;
import java.util.HashSet;
import java.util.Set;
import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify;
import de.dhbwstuttgart.typinference.unify.model.MPair;
import de.dhbwstuttgart.typinference.unify.model.MPair.PairOperator;
import de.dhbwstuttgart.typinference.unify.model.Type;
import de.dhbwstuttgart.typinference.unify.model.TypeParams;
/**
* Implementation of the Martelli-Montanari unification algorithm.
* @author Florian Steurer
*/
public class MartelliMontanariUnify implements IUnify {
@Override
public Unifier unify(Set<MPair> terms) {
// TODO Auto-generated method stub
return null;
}
private boolean delete(MPair pair) {
return pair.getRhsType().equals(pair.getLhsType());
}
private Set<MPair> decompose(MPair pair) {
Set<MPair> result = new HashSet<>();
Type rhs = pair.getRhsType();
Type lhs = pair.getLhsType();
if(!rhs.getName().equals(lhs.getName()) || rhs.getTypeParams().size() != lhs.getTypeParams().size())
return null; // conflict
TypeParams rhsTypeParams = rhs.getTypeParams();
TypeParams lhsTypeParams = lhs.getTypeParams();
for(int i = 0; i < rhsTypeParams.size(); i++)
result.add(new MPair(rhsTypeParams.get(i), lhsTypeParams.get(i), PairOperator.EQUALSDOT));
return result;
}
private MPair swap(MPair pair) {
Type rhs = pair.getRhsType();
Type lhs = pair.getLhsType();
if(lhs.getTypeParams().size() != 0 && rhs.getTypeParams().size() == 0)
return new MPair(rhs, lhs, PairOperator.EQUALSDOT);
return pair;
}
private Unifier eliminate(MPair pair) {
Type rhs = pair.getRhsType();
Type lhs = pair.getLhsType();
TypeParams rhsTypeParams = rhs.getTypeParams();
for(Type t : rhsTypeParams)
if(lhs.equals(t))
return new Unifier(); //identity-"unifier"
return new Unifier(lhs, rhs);
}
private boolean check(MPair pair) {
Type rhs = pair.getRhsType();
Type lhs = pair.getLhsType();
TypeParams rhsTypeParams = rhs.getTypeParams();
for(Type t : rhsTypeParams)
if(lhs.equals(t))
return false;
return true;
}
}

View File

@ -13,6 +13,13 @@ public class Unifier implements Function<Type, Type> {
this.source = source; this.source = source;
this.target = target; this.target = target;
} }
/**
* Identity function as an "unifier".
*/
public Unifier() {
}
@Override @Override
public Type apply(Type t) { public Type apply(Type t) {