forked from JavaTX/JavaCompilerCore
refactoring and commenting
This commit is contained in:
parent
55f288022a
commit
1e96811127
@ -25,7 +25,7 @@ public class MartelliMontanariUnify implements IUnify {
|
|||||||
public Optional<Unifier> unify(Set<UnifyType> terms) {
|
public Optional<Unifier> unify(Set<UnifyType> terms) {
|
||||||
// Sets with less than 2 terms are trivially unified
|
// Sets with less than 2 terms are trivially unified
|
||||||
if(terms.size() < 2)
|
if(terms.size() < 2)
|
||||||
return Optional.of(Unifier.Identity());
|
return Optional.of(Unifier.identity());
|
||||||
|
|
||||||
// For the the set of terms {t1,...,tn},
|
// For the the set of terms {t1,...,tn},
|
||||||
// build a list of equations {(t1 = t2), (t2 = t3), (t3 = t4), ....}
|
// 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.
|
// Start with the identity unifier. Substitutions will be added later.
|
||||||
Unifier mgu = Unifier.Identity();
|
Unifier mgu = Unifier.identity();
|
||||||
|
|
||||||
// Apply rules while possible
|
// Apply rules while possible
|
||||||
int idx = 0;
|
int idx = 0;
|
||||||
@ -91,7 +91,7 @@ public class MartelliMontanariUnify implements IUnify {
|
|||||||
|
|
||||||
// SUBST - Rule
|
// SUBST - Rule
|
||||||
if(lhsType instanceof PlaceholderType) {
|
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));
|
termsList = termsList.stream().map(mgu::apply).collect(Collectors.toCollection(ArrayList::new));
|
||||||
idx = idx+1 == termsList.size() ? 0 : idx+1;
|
idx = idx+1 == termsList.size() ? 0 : idx+1;
|
||||||
continue;
|
continue;
|
||||||
|
@ -417,9 +417,9 @@ public class RuleSet implements IRuleSet{
|
|||||||
TypeParams typeDParams = typeD.getTypeParams();
|
TypeParams typeDParams = typeD.getTypeParams();
|
||||||
TypeParams typeDgenParams = typeDgen.getTypeParams();
|
TypeParams typeDgenParams = typeDgen.getTypeParams();
|
||||||
|
|
||||||
Unifier unif = Unifier.Identity();
|
Unifier unif = Unifier.identity();
|
||||||
for(int i = 0; i < typeDParams.size(); i++)
|
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));
|
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));
|
Unifier unif = new Unifier((PlaceholderType) typeDgenParams.get(0), typeDParams.get(0));
|
||||||
for(int i = 1; i < typeDParams.size(); i++)
|
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));
|
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));
|
Unifier unif = new Unifier((PlaceholderType) typeSupDsgenParams.get(0), typeDParams.get(0));
|
||||||
for(int i = 1; i < typeDParams.size(); i++)
|
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));
|
return Optional.of(new UnifyPair(unif.apply(newLhs), newRhs, PairOperator.SMALLERDOTWC));
|
||||||
}
|
}
|
||||||
|
@ -377,7 +377,7 @@ public class Unify {
|
|||||||
Unifier unifier = opt.get();
|
Unifier unifier = opt.get();
|
||||||
unifier.swapPlaceholderSubstitutions(thetaPrime.getTypeParams().toArray());
|
unifier.swapPlaceholderSubstitutions(thetaPrime.getTypeParams().toArray());
|
||||||
Set<UnifyPair> substitutionSet = new HashSet<>();
|
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));
|
substitutionSet.add(new UnifyPair(sigma.getKey(), sigma.getValue(), PairOperator.EQUALSDOT));
|
||||||
|
|
||||||
List<UnifyType> freshTphs = new ArrayList<>();
|
List<UnifyType> freshTphs = new ArrayList<>();
|
||||||
|
@ -372,21 +372,6 @@ public class FiniteClosure implements IFiniteClosure {
|
|||||||
return result;
|
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
|
@Override
|
||||||
public Set<UnifyType> getAllTypesByName(String typeName) {
|
public Set<UnifyType> getAllTypesByName(String typeName) {
|
||||||
if(!strInheritanceGraph.containsKey(typeName))
|
if(!strInheritanceGraph.containsKey(typeName))
|
||||||
|
@ -1,15 +1,26 @@
|
|||||||
package de.dhbwstuttgart.typeinference.unify.model;
|
package de.dhbwstuttgart.typeinference.unify.model;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.Set;
|
|
||||||
import java.util.function.Function;
|
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<>();
|
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) {
|
public Unifier(PlaceholderType source, UnifyType target) {
|
||||||
substitutions.put(source, target);
|
substitutions.put(source, target);
|
||||||
}
|
}
|
||||||
@ -20,13 +31,23 @@ public class Unifier implements Function<UnifyType, UnifyType> /*, Set<MPair>*/
|
|||||||
protected Unifier() {
|
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();
|
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);
|
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())
|
for(PlaceholderType pt : substitutions.keySet())
|
||||||
substitutions.put(pt, substitutions.get(pt).apply(tempU));
|
substitutions.put(pt, substitutions.get(pt).apply(tempU));
|
||||||
substitutions.put(source, target);
|
substitutions.put(source, target);
|
||||||
@ -37,26 +58,39 @@ public class Unifier implements Function<UnifyType, UnifyType> /*, Set<MPair>*/
|
|||||||
return t.apply(this);
|
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) {
|
public UnifyPair apply(UnifyPair p) {
|
||||||
return new UnifyPair(this.apply(p.getLhsType()), this.apply(p.getRhsType()), p.getPairOp());
|
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) {
|
public boolean hasSubstitute(PlaceholderType t) {
|
||||||
return substitutions.containsKey(t);
|
return substitutions.containsKey(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the type that will replace the typevariable t if the unifier is applied.
|
||||||
|
*/
|
||||||
public UnifyType getSubstitute(PlaceholderType t) {
|
public UnifyType getSubstitute(PlaceholderType t) {
|
||||||
return substitutions.get(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) {
|
public void swapPlaceholderSubstitutions(UnifyType... targetParams) {
|
||||||
for(UnifyType tph : targetParams) {
|
for(UnifyType tph : targetParams) {
|
||||||
if(!(tph instanceof PlaceholderType))
|
if(!(tph instanceof PlaceholderType))
|
||||||
continue;
|
continue;
|
||||||
|
// Swap a substitutions (a -> b) if a is an element of the target params.
|
||||||
if(substitutions.containsKey(tph) && substitutions.get(tph) instanceof PlaceholderType) {
|
if(substitutions.containsKey(tph) && substitutions.get(tph) instanceof PlaceholderType) {
|
||||||
PlaceholderType newLhs = (PlaceholderType) substitutions.get(tph);
|
PlaceholderType newLhs = (PlaceholderType) substitutions.get(tph);
|
||||||
substitutions.remove(tph);
|
substitutions.remove(tph);
|
||||||
@ -75,5 +109,10 @@ public class Unifier implements Function<UnifyType, UnifyType> /*, Set<MPair>*/
|
|||||||
result += " }";
|
result += " }";
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterator<Entry<PlaceholderType, UnifyType>> iterator() {
|
||||||
|
return substitutions.entrySet().iterator();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user