From 0fa1b091906c3ce78a051daded68ff3ea9d67c58 Mon Sep 17 00:00:00 2001 From: Johanna Stuber Date: Mon, 15 Feb 2021 16:07:59 +0100 Subject: [PATCH] use SortedSet for quantifiedVariables in TypeAbstraction so instantiation is predictable --- .../kit/typicalc/model/TypeInferenceResult.java | 3 +-- .../kit/typicalc/model/type/TypeAbstraction.java | 15 +++++---------- .../edu/kit/typicalc/model/type/TypeVariable.java | 14 +++++++++----- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/main/java/edu/kit/typicalc/model/TypeInferenceResult.java b/src/main/java/edu/kit/typicalc/model/TypeInferenceResult.java index 68da474..14e0bf6 100644 --- a/src/main/java/edu/kit/typicalc/model/TypeInferenceResult.java +++ b/src/main/java/edu/kit/typicalc/model/TypeInferenceResult.java @@ -32,8 +32,7 @@ public class TypeInferenceResult { protected TypeInferenceResult(List 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); } diff --git a/src/main/java/edu/kit/typicalc/model/type/TypeAbstraction.java b/src/main/java/edu/kit/typicalc/model/type/TypeAbstraction.java index 4323ca7..6aecb1d 100644 --- a/src/main/java/edu/kit/typicalc/model/type/TypeAbstraction.java +++ b/src/main/java/edu/kit/typicalc/model/type/TypeAbstraction.java @@ -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 quantifiedVariables; + private final SortedSet 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 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 varsToBeQuantified = type.getFreeTypeVariables(); typeAssumptions.forEach((var, abs) -> varsToBeQuantified.removeAll(abs.getFreeTypeVariables())); - this.quantifiedVariables = varsToBeQuantified; + this.quantifiedVariables = new TreeSet<>(varsToBeQuantified); } /** diff --git a/src/main/java/edu/kit/typicalc/model/type/TypeVariable.java b/src/main/java/edu/kit/typicalc/model/type/TypeVariable.java index ae9d8bb..e84d60f 100644 --- a/src/main/java/edu/kit/typicalc/model/type/TypeVariable.java +++ b/src/main/java/edu/kit/typicalc/model/type/TypeVariable.java @@ -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 { 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); + } }