diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java new file mode 100644 index 00000000..34e39738 --- /dev/null +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java @@ -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 terms); +} diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java b/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java new file mode 100644 index 00000000..3631ed24 --- /dev/null +++ b/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java @@ -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 terms) { + // TODO Auto-generated method stub + return null; + } + + private boolean delete(MPair pair) { + return pair.getRhsType().equals(pair.getLhsType()); + } + + private Set decompose(MPair pair) { + Set 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; + } +} diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Unifier.java b/src/de/dhbwstuttgart/typeinference/unifynew/Unifier.java index ecd56cf1..4528a934 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Unifier.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Unifier.java @@ -13,6 +13,13 @@ public class Unifier implements Function { this.source = source; this.target = target; } + + /** + * Identity function as an "unifier". + */ + public Unifier() { + + } @Override public Type apply(Type t) {