use SortedSet for quantifiedVariables in TypeAbstraction so instantiation is predictable

This commit is contained in:
Johanna Stuber 2021-02-15 16:07:59 +01:00
parent a76b1b3c8d
commit 0fa1b09190
3 changed files with 15 additions and 17 deletions

View File

@ -32,8 +32,7 @@ public class TypeInferenceResult {
protected TypeInferenceResult(List<Substitution> substitutions, TypeVariable typeVar) {
mgu = new ArrayList<>(substitutions);
findMGU();
mgu.sort(Comparator.comparing((Substitution o) -> o.getVariable().getKind()).
thenComparingInt(o -> o.getVariable().getIndex()));
mgu.sort(Comparator.comparing(Substitution::getVariable));
finalType = findFinalType(typeVar);
}

View File

@ -3,12 +3,7 @@ package edu.kit.typicalc.model.type;
import edu.kit.typicalc.model.TypeVariableFactory;
import edu.kit.typicalc.model.term.VarTerm;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.*;
/**
* Models a type abstraction with its type and the type variables bound by the for-all
@ -17,7 +12,7 @@ import java.util.Set;
public class TypeAbstraction {
private final Type type;
private final Set<TypeVariable> quantifiedVariables;
private final SortedSet<TypeVariable> quantifiedVariables;
/**
* Initializes a new type abstraction with its type and the type variables bound by
* the for-all quantifier.
@ -26,7 +21,7 @@ public class TypeAbstraction {
*/
public TypeAbstraction(Type type, Set<TypeVariable> quantifiedVariables) {
this.type = type;
this.quantifiedVariables = quantifiedVariables;
this.quantifiedVariables = new TreeSet<>(quantifiedVariables);
}
/**
@ -36,7 +31,7 @@ public class TypeAbstraction {
*/
public TypeAbstraction(Type type) {
this.type = type;
this.quantifiedVariables = Collections.emptySet();
this.quantifiedVariables = new TreeSet<>();
}
/**
@ -50,7 +45,7 @@ public class TypeAbstraction {
this.type = type;
Set<TypeVariable> varsToBeQuantified = type.getFreeTypeVariables();
typeAssumptions.forEach((var, abs) -> varsToBeQuantified.removeAll(abs.getFreeTypeVariables()));
this.quantifiedVariables = varsToBeQuantified;
this.quantifiedVariables = new TreeSet<>(varsToBeQuantified);
}
/**

View File

@ -3,15 +3,12 @@ package edu.kit.typicalc.model.type;
import edu.kit.typicalc.model.UnificationError;
import edu.kit.typicalc.util.Result;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.*;
/**
* Models a type variable
*/
public class TypeVariable extends Type {
public class TypeVariable extends Type implements Comparable<TypeVariable> {
private final TypeVariableKind kind;
private final int index;
@ -147,4 +144,11 @@ public class TypeVariable extends Type {
public int hashCode() {
return Objects.hash(kind, index);
}
@Override
public int compareTo(TypeVariable var) {
return Comparator.comparing(TypeVariable::getKind)
.thenComparing(TypeVariable::getIndex)
.compare(this, var);
}
}