subst rule

This commit is contained in:
Florian Steurer 2015-12-26 16:52:18 +01:00
parent 3ff45a01de
commit 8eecda2a8f
2 changed files with 50 additions and 4 deletions

View File

@ -2,9 +2,12 @@ package de.dhbwstuttgart.typeinference.unifynew;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.Queue;
import java.util.Set;
import java.util.stream.Collectors;
import junit.framework.Assert;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
@ -516,9 +519,42 @@ public class RuleSet implements IRuleSet{
}
@Override
public Set<MPair> subst(Set<MPair> pair) {
// TODO Auto-generated method stub
return null;
public Set<MPair> subst(Set<MPair> pairs) {
HashSet<Type> allTypes = new HashSet<>();
pairs.forEach(x -> { allTypes.add(x.getLhsType()); allTypes.add(x.getRhsType()); });
Queue<MPair> result = new LinkedList<MPair>(pairs);
for(int i = 0; i < result.size(); i++) {
MPair pair = result.poll();
Type lhsType;
Type rhsType;
if(pair.getPairOp() == PairOperator.EQUALSDOT
&& ((lhsType = pair.getLhsType()) instanceof PlaceholderType)
&& !((rhsType = pair.getRhsType()) instanceof PlaceholderType)
&& occursInSet(lhsType, allTypes)
&& !occurs(lhsType, rhsType)) {
Unifier uni = new Unifier(lhsType, rhsType);
result = result.stream().map(uni::apply).collect(Collectors.toCollection(LinkedList::new));
}
result.add(pair);
}
return new HashSet<>(result);
}
private boolean occursInSet(Type t, Set<Type> types) {
int origSize = types.size();
types.add(t);
return types.size() == origSize;
}
private boolean occurs(Type t1, Type t2) {
TypeParams t2Params = t2.getTypeParams();
for(Type t2Param : t2Params)
if(t1.equals(t2Param) || occurs(t1, t2Param))
return true;
return false;
}
}

View File

@ -88,11 +88,21 @@ public class Unify {
*/
/*
* TODO
* Im Paper wird Eq'' genannt, es wird also von einer Menge in einer Menge in einer Menge ausgegangen.
* Durch das flache Kartesische Produkt gibt es hier aber nur Mengen in Mengen.
* Richtig so?
*/
/*
* Step 6: a) Restart for pairs where subst was applied
* b) Union over everything
*/
/*
* Step 7: Filter result for solved pairs
*/