refactoring and commenting

This commit is contained in:
Florian Steurer 2016-04-12 10:54:17 +02:00
parent 55f288022a
commit 1e96811127
5 changed files with 59 additions and 35 deletions

View File

@ -25,7 +25,7 @@ public class MartelliMontanariUnify implements IUnify {
public Optional<Unifier> unify(Set<UnifyType> terms) {
// Sets with less than 2 terms are trivially unified
if(terms.size() < 2)
return Optional.of(Unifier.Identity());
return Optional.of(Unifier.identity());
// For the the set of terms {t1,...,tn},
// build a list of equations {(t1 = t2), (t2 = t3), (t3 = t4), ....}
@ -39,7 +39,7 @@ public class MartelliMontanariUnify implements IUnify {
}
// Start with the identity unifier. Substitutions will be added later.
Unifier mgu = Unifier.Identity();
Unifier mgu = Unifier.identity();
// Apply rules while possible
int idx = 0;
@ -91,7 +91,7 @@ public class MartelliMontanariUnify implements IUnify {
// SUBST - Rule
if(lhsType instanceof PlaceholderType) {
mgu.Add((PlaceholderType) lhsType, rhsType);
mgu.add((PlaceholderType) lhsType, rhsType);
termsList = termsList.stream().map(mgu::apply).collect(Collectors.toCollection(ArrayList::new));
idx = idx+1 == termsList.size() ? 0 : idx+1;
continue;

View File

@ -417,9 +417,9 @@ public class RuleSet implements IRuleSet{
TypeParams typeDParams = typeD.getTypeParams();
TypeParams typeDgenParams = typeDgen.getTypeParams();
Unifier unif = Unifier.Identity();
Unifier unif = Unifier.identity();
for(int i = 0; i < typeDParams.size(); i++)
unif.Add((PlaceholderType) typeDgenParams.get(i), typeDParams.get(i));
unif.add((PlaceholderType) typeDgenParams.get(i), typeDParams.get(i));
return Optional.of(new UnifyPair(unif.apply(newLhs), typeDs, PairOperator.SMALLERDOT));
}
@ -465,7 +465,7 @@ public class RuleSet implements IRuleSet{
Unifier unif = new Unifier((PlaceholderType) typeDgenParams.get(0), typeDParams.get(0));
for(int i = 1; i < typeDParams.size(); i++)
unif.Add((PlaceholderType) typeDgenParams.get(i), typeDParams.get(i));
unif.add((PlaceholderType) typeDgenParams.get(i), typeDParams.get(i));
return Optional.of(new UnifyPair(unif.apply(newLhs), typeExtDs, PairOperator.SMALLERDOTWC));
}
@ -517,7 +517,7 @@ public class RuleSet implements IRuleSet{
Unifier unif = new Unifier((PlaceholderType) typeSupDsgenParams.get(0), typeDParams.get(0));
for(int i = 1; i < typeDParams.size(); i++)
unif.Add((PlaceholderType) typeSupDsgenParams.get(i), typeDParams.get(i));
unif.add((PlaceholderType) typeSupDsgenParams.get(i), typeDParams.get(i));
return Optional.of(new UnifyPair(unif.apply(newLhs), newRhs, PairOperator.SMALLERDOTWC));
}

View File

@ -377,7 +377,7 @@ public class Unify {
Unifier unifier = opt.get();
unifier.swapPlaceholderSubstitutions(thetaPrime.getTypeParams().toArray());
Set<UnifyPair> substitutionSet = new HashSet<>();
for (Entry<PlaceholderType, UnifyType> sigma : unifier.getSubstitutions())
for (Entry<PlaceholderType, UnifyType> sigma : unifier)
substitutionSet.add(new UnifyPair(sigma.getKey(), sigma.getValue(), PairOperator.EQUALSDOT));
List<UnifyType> freshTphs = new ArrayList<>();

View File

@ -372,21 +372,6 @@ public class FiniteClosure implements IFiniteClosure {
return result;
}
public boolean isGenericType(UnifyType t) {
if(t.getTypeParams().size() == 0)
return true;
if(!strInheritanceGraph.containsKey(t.getName()))
return false;
for(UnifyPair pair : pairs)
if(pair.getLhsType().equals(t))
return true;
return false;
}
@Override
public Set<UnifyType> getAllTypesByName(String typeName) {
if(!strInheritanceGraph.containsKey(typeName))

View File

@ -1,15 +1,26 @@
package de.dhbwstuttgart.typeinference.unify.model;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Function;
public class Unifier implements Function<UnifyType, UnifyType> /*, Set<MPair>*/ { // TODO set implementieren
/**
* A set of substitutions (s -> t) that is an applicable function to types and pairs.
* @author Florian Steurer
*/
public class Unifier implements Function<UnifyType, UnifyType>, Iterable<Entry<PlaceholderType, UnifyType>> {
/**
* The set of substitutions that unify a type-term
*/
private HashMap<PlaceholderType, UnifyType> substitutions = new HashMap<>();
/**
* Creates a new instance with a single substitution (source -> target).
* @param The type that is replaced
* @param The type that replaces
*/
public Unifier(PlaceholderType source, UnifyType target) {
substitutions.put(source, target);
}
@ -20,13 +31,23 @@ public class Unifier implements Function<UnifyType, UnifyType> /*, Set<MPair>*/
protected Unifier() {
}
public static Unifier Identity() {
/**
* Creates an unifier that is the identity function (thus has no substitutions).
*/
public static Unifier identity() {
return new Unifier();
}
public void Add(PlaceholderType source, UnifyType target) {
/**
* Adds a substitution to the unifier from (source -> target)
* @param The type that is replaced
* @param The type that replaces
*/
public void add(PlaceholderType source, UnifyType target) {
Unifier tempU = new Unifier(source, target);
// Every new substitution must be applied to previously added substitutions
// otherwise the unifier needs to be applied multiple times to unify two terms
for(PlaceholderType pt : substitutions.keySet())
substitutions.put(pt, substitutions.get(pt).apply(tempU));
substitutions.put(source, target);
@ -37,26 +58,39 @@ public class Unifier implements Function<UnifyType, UnifyType> /*, Set<MPair>*/
return t.apply(this);
}
/**
* Applies the unifier to the two terms of the pair.
* @return A new pair where the left and right-hand side are applied
*/
public UnifyPair apply(UnifyPair p) {
return new UnifyPair(this.apply(p.getLhsType()), this.apply(p.getRhsType()), p.getPairOp());
}
/**
* True if the typevariable t will be substituted if the unifier is applied.
* false otherwise.
*/
public boolean hasSubstitute(PlaceholderType t) {
return substitutions.containsKey(t);
}
/**
* Returns the type that will replace the typevariable t if the unifier is applied.
*/
public UnifyType getSubstitute(PlaceholderType t) {
return substitutions.get(t);
}
public Set<Entry<PlaceholderType, UnifyType>> getSubstitutions() {
return substitutions.entrySet();
}
/**
* Garantuees that if there is a substitutions (a -> b) in this unifier,
* a is not an element of the targetParams. Substitutions that do not
* satisfy this condition, are swapped.
*/
public void swapPlaceholderSubstitutions(UnifyType... targetParams) {
for(UnifyType tph : targetParams) {
if(!(tph instanceof PlaceholderType))
continue;
// Swap a substitutions (a -> b) if a is an element of the target params.
if(substitutions.containsKey(tph) && substitutions.get(tph) instanceof PlaceholderType) {
PlaceholderType newLhs = (PlaceholderType) substitutions.get(tph);
substitutions.remove(tph);
@ -75,5 +109,10 @@ public class Unifier implements Function<UnifyType, UnifyType> /*, Set<MPair>*/
result += " }";
return result;
}
@Override
public Iterator<Entry<PlaceholderType, UnifyType>> iterator() {
return substitutions.entrySet().iterator();
}
}