From ab7f56db6f300ac36d71e62bb436a440744cc0a0 Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Mon, 21 Dec 2015 15:44:13 +0100 Subject: [PATCH] martelli montanari unification (unfinished) --- .../unify/interfaces/IUnify.java | 14 ++++ .../unifynew/MartelliMontanariUnify.java | 80 +++++++++++++++++++ .../typeinference/unifynew/Unifier.java | 7 ++ 3 files changed, 101 insertions(+) create mode 100644 src/de/dhbwstuttgart/typeinference/unify/interfaces/IUnify.java create mode 100644 src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java 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 000000000..34e397384 --- /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 000000000..3631ed24c --- /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 ecd56cf13..4528a9346 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) {