commenting and refactoring

This commit is contained in:
Florian Steurer 2016-04-12 11:12:57 +02:00
parent 1e96811127
commit ea32cd5680
5 changed files with 57 additions and 30 deletions

View File

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

View File

@ -105,7 +105,7 @@ public class FiniteClosure implements IFiniteClosure {
if (!sigma2Opt.isPresent())
continue;
Unifier sigma2 = sigma2Opt.get();
sigma2.swapPlaceholderSubstitutions(typePrime.getTypeParams().toArray());
sigma2.swapPlaceholderSubstitutions(typePrime.getTypeParams());
if(type.equals(theta2))
continue;
Set<UnifyType> theta1s = smaller(theta2);
@ -205,7 +205,7 @@ public class FiniteClosure implements IFiniteClosure {
continue;
Unifier sigma2 = sigma2Opt.get();
sigma2.swapPlaceholderSubstitutions(typePrime.getTypeParams().toArray());
sigma2.swapPlaceholderSubstitutions(typePrime.getTypeParams());
Set<UnifyType> theta1s = greater(theta2);
for (UnifyType theta1 : theta1s) {
// Because only the most general type is calculated, sigma1 = sigma2

View File

@ -1,6 +1,5 @@
package de.dhbwstuttgart.typeinference.unify.model;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

View File

@ -5,43 +5,61 @@ import java.util.Iterator;
import de.dhbwstuttgart.typeinference.Menge;
/**
* The generic or non-generic parameters of a type e.g. <T> for List<T>
* @author Florian Steurer
*/
public final class TypeParams implements Iterable<UnifyType>{
/**
* The array which backs the type parameters.
*/
private final UnifyType[] typeParams;
/**
* Creates a new set of type parameters.
* @param types The type parameters.
*/
public TypeParams(Menge<UnifyType> types){
typeParams = new UnifyType[types.size()];
for(int i=0;i<types.size();i++){
for(int i=0;i<types.size();i++)
typeParams[i] = types.get(i);
}
}
/**
* Creates a new set of type parameters.
* @param types The type parameters.
*/
public TypeParams(UnifyType... types) {
typeParams = types;
}
/**
* True if every parameter in this object is a placeholder type, false otherwise.
*/
public boolean arePlaceholders() {
for(UnifyType t : typeParams)
if(!(t instanceof PlaceholderType))
return false;
return true;
}
@Override
public String toString() {
String res = "";
for(UnifyType t : typeParams)
res += t + ",";
return "<" + res.substring(0, res.length()-1) + ">";
return Arrays.stream(typeParams).allMatch(x -> x instanceof PlaceholderType);
}
/**
* Returns the number of the type parameters in this object.
* @return number of type parameters, always positive (including 0).
*/
public int size() {
return typeParams.length;
}
/**
* Returns true if this object has size() of zero, false otherwise.
*/
public boolean empty() {
return typeParams.length == 0;
}
/**
* Applies a unifier to every parameter in this object.
* @param unif The applied unifier.
* @return A new type parameter object, where the unifier was applied to every parameter.
*/
public TypeParams apply(Unifier unif) {
UnifyType[] newParams = new UnifyType[typeParams.length];
for(int i = 0; i < typeParams.length; i++)
@ -49,6 +67,10 @@ public final class TypeParams implements Iterable<UnifyType>{
return new TypeParams(newParams);
}
/**
* True if the PlaceholderType t is a parameter of this object, or if any parameter
* contains t (arbitrary depth of recursion), false otherwise.
*/
public boolean occurs(PlaceholderType t) {
for(UnifyType p : typeParams)
if(p instanceof PlaceholderType)
@ -60,26 +82,24 @@ public final class TypeParams implements Iterable<UnifyType>{
return false;
}
public boolean contains(UnifyType t) {
for(UnifyType t1 : typeParams)
if(t1.equals(t1))
return true;
return false;
}
/**
* Returns the i-th parameter of this object.
* @throws ArrayOutOfBoundsException if i > this.size()-1.
*/
public UnifyType get(int i) {
return typeParams[i];
}
/**
* Sets the the type t as the i-th parameter and returns a new object
* that equals this object, except for the i-th type.
* @throws ArrayOutOfBoundsException if i > this.size()-1.
*/
public TypeParams set(UnifyType t, int idx) {
UnifyType[] newparams = Arrays.copyOf(typeParams, typeParams.length);
newparams[idx] = t;
return new TypeParams(newparams);
}
public UnifyType[] toArray() {
return Arrays.copyOf(typeParams, typeParams.length);
}
@Override
public Iterator<UnifyType> iterator() {
@ -107,5 +127,13 @@ public final class TypeParams implements Iterable<UnifyType>{
return true;
}
@Override
public String toString() {
String res = "";
for(UnifyType t : typeParams)
res += t + ",";
return "<" + res.substring(0, res.length()-1) + ">";
}
}

View File

@ -86,7 +86,7 @@ public class Unifier implements Function<UnifyType, UnifyType>, Iterable<Entry<P
* 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(Iterable<UnifyType> targetParams) {
for(UnifyType tph : targetParams) {
if(!(tph instanceof PlaceholderType))
continue;