14 using System.Collections.Generic;
17 using TypeSystem.Operations;
19 namespace TypeSystem {
22 private IDictionary<int, TypeVariable> typeVariables =
new Dictionary<int, TypeVariable>();
38 get {
return typeVariables; }
45 get {
return substitution; }
55 throw new ArgumentException(
"I need a type variable, not a " + te);
60 private static int numero = 0;
77 this.typeVariables[tv.Variable] = tv;
79 IList<int> keys =
new List<int>(tv.EquivalenceClass.TypeVariables.Keys);
80 foreach (
int pairKey
in keys) {
81 if (!this.typeVariables.ContainsKey(pairKey)) {
83 this.typeVariables[pairKey] = tv.EquivalenceClass.TypeVariables[pairKey];
84 this.add(tv.EquivalenceClass.TypeVariables[pairKey], unification, previouslyUnified);
89 tv.EquivalenceClass =
this;
93 if (this.substitution != null) {
101 return te.
Unify(
this.substitution, unification, previouslyUnified);
106 this.substitution = UnionType.collect(this.substitution, te);
110 this.substitution = te;
119 private string cachedToString =
"";
128 ICollection<int> keys = typeVariables.Keys;
130 foreach (
int key
in keys)
133 if (i < keys.Count - 1)
138 return sb.ToString();
144 #region UpdateEquivalenceClass()
157 newEquivalenceClass.substitution = this.substitution;
159 if (newEquivalenceClass.substitution != null)
160 newEquivalenceClass.substitution.ReplaceTypeVariables(typeVariableMappings);
162 newEquivalenceClass.typeVariables =
new Dictionary<int, TypeVariable>();
163 foreach (KeyValuePair<int, TypeVariable> oldPair
in this.typeVariables) {
165 if (typeVariableMappings.ContainsKey(oldPair.Value)) {
167 TypeVariable newTypeVariable = typeVariableMappings[oldPair.Value];
168 newEquivalenceClass.typeVariables[newTypeVariable.Variable] = newTypeVariable;
170 newTypeVariable.EquivalenceClass = newEquivalenceClass;
171 newTypeVariable.ValidTypeExpression =
false;
174 newEquivalenceClass.typeVariables[oldPair.Key] = oldPair.Value;
177 if (newEquivalenceClass.substitution != null)
178 newEquivalenceClass.substitution.UpdateEquivalenceClass(typeVariableMappings,
new List<TypeExpression>());
184 #region CloneTypeVariables()
185 internal void CloneTypeVariables(IDictionary<int, TypeVariable> clonedTypeVariables, IList<EquivalenceClass> equivalenceClasses, MethodType methodAnalyzed) {
197 if (equivalenceClasses.Contains(
this))
200 equivalenceClasses.Add(
this);
202 if (this.substitution!=null)
203 this.substitution.Clone(clonedTypeVariables, equivalenceClasses, methodAnalyzed);
205 foreach (KeyValuePair<int, TypeVariable> pair
in this.typeVariables)
206 pair.Value.Clone(clonedTypeVariables, equivalenceClasses, methodAnalyzed);
218 public bool Remove(TypeVariable toRemove) {
219 return this.typeVariables.Remove(toRemove.Variable);
TypeExpression Substitution
A equivalence class could be bounded to a unique no variable type
EquivalenceClass(TypeExpression te)
bool Remove(TypeVariable toRemove)
When loops are detected, it is necesary to suppress a new extra variable returned in the return type ...
IDictionary< int, TypeVariable > TypeVariables
All the type variables that must be substituted with the same value
Abstract class that represents all different types.
Represents a generic type expression
System.Text.StringBuilder StringBuilder
No location information is provided within this class, because the exec methods invoked by the proper...
virtual bool HasTypeVariables()
To know if the type expression has some type variables and requieres unification The default implemen...
void UpdateEquivalenceClass(IDictionary< TypeVariable, TypeVariable > typeVariableMappings)
Replaces the equivalence class substituting the old type variables for the new ones. The substitution is not altered. Since equivalence classes and type variables have a bidirectional association, the new equivalence classes will make type variables update their new equivalence classes.
override string ToString()
The ToString of its substitution
bool add(TypeExpression te, SortOfUnification unification, IList< Pair< TypeExpression, TypeExpression >> previouslyUnified)
To add a new type to the class equivalence.
TypeExpression Substitution
Gets the substitution; null if it does not exist
abstract bool Unify(TypeExpression te, SortOfUnification unification, IList< Pair< TypeExpression, TypeExpression >> previouslyUnified)
Requires the implicit object to be a subtype of the type parameter
EquivalenceClass EquivalenceClass
The equivalence class of the type variable (see the dragon book)