From ea32cd5680c10c0f91a5d96ddcbf755a37c5f3c0 Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Tue, 12 Apr 2016 11:12:57 +0200 Subject: [PATCH] commenting and refactoring --- .../typeinference/unify/Unify.java | 2 +- .../unify/model/FiniteClosure.java | 4 +- .../unify/model/PlaceholderType.java | 1 - .../typeinference/unify/model/TypeParams.java | 78 +++++++++++++------ .../typeinference/unify/model/Unifier.java | 2 +- 5 files changed, 57 insertions(+), 30 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/Unify.java b/src/de/dhbwstuttgart/typeinference/unify/Unify.java index 0bb0723f..4d727591 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/Unify.java +++ b/src/de/dhbwstuttgart/typeinference/unify/Unify.java @@ -375,7 +375,7 @@ public class Unify { continue; Unifier unifier = opt.get(); - unifier.swapPlaceholderSubstitutions(thetaPrime.getTypeParams().toArray()); + unifier.swapPlaceholderSubstitutions(thetaPrime.getTypeParams()); Set substitutionSet = new HashSet<>(); for (Entry sigma : unifier) substitutionSet.add(new UnifyPair(sigma.getKey(), sigma.getValue(), PairOperator.EQUALSDOT)); diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java index fd946eb0..c4d1658b 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java @@ -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 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 theta1s = greater(theta2); for (UnifyType theta1 : theta1s) { // Because only the most general type is calculated, sigma1 = sigma2 diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java b/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java index ddb7d273..9b032b4a 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java @@ -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; diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java b/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java index f5b7686b..aa534b60 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java @@ -5,43 +5,61 @@ import java.util.Iterator; import de.dhbwstuttgart.typeinference.Menge; +/** + * The generic or non-generic parameters of a type e.g. for List + * @author Florian Steurer + */ public final class TypeParams implements Iterable{ + /** + * 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 types){ typeParams = new UnifyType[types.size()]; - for(int i=0;i"; + 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{ 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{ 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 iterator() { @@ -107,5 +127,13 @@ public final class TypeParams implements Iterable{ return true; } + + @Override + public String toString() { + String res = ""; + for(UnifyType t : typeParams) + res += t + ","; + return "<" + res.substring(0, res.length()-1) + ">"; + } } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java b/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java index 61302430..1a3988db 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java @@ -86,7 +86,7 @@ public class Unifier implements Function, Iterable targetParams) { for(UnifyType tph : targetParams) { if(!(tph instanceof PlaceholderType)) continue;