Inference/src/TypeSystem/EquivalenceClass.cs
/// <summary>
/// To add a new type to the class equivalence.
/// </summary>
/// <param name="te">The type to be added</param>
/// <param name="unification">Indicates if the kind of unification (equivalent, incremental or override).</param>
/// <param name="previouslyUnified">To detect infinite loops. The previously unified pairs of type expressions.</param>
/// <returns>If the substitution has been correctly applied</returns>
To add a new type to the class equivalence.
To add a new type to the class equivalence .
public bool add(TypeExpression te, SortOfUnification unification, IList<Pair<TypeExpression, TypeExpression>> previouslyUnified) {
            TypeVariable tv = te as TypeVariable;
            if (tv != null) { 
                if (tv.Substitution != null) 
                    if (!this.add(tv.EquivalenceClass.Substitution, unification, previouslyUnified)) 
                        return false; 
                this.typeVariables[tv.Variable] = tv;
                if (tv.EquivalenceClass != null) {  
                    IList<int> keys = new List<int>(tv.EquivalenceClass.TypeVariables.Keys);
                    foreach (int pairKey in keys) {
                        if (!this.typeVariables.ContainsKey(pairKey)) {
                            this.typeVariables[pairKey] = tv.EquivalenceClass.TypeVariables[pairKey];
                            this.add(tv.EquivalenceClass.TypeVariables[pairKey], unification, previouslyUnified);
                        }
                    }
                }
                tv.EquivalenceClass = this;
                return true;
            }
            if (this.substitution != null) {
                if (unification == SortOfUnification.Equivalent) {
                    if ( !(bool)this.substitution.AcceptOperation(new EquivalentOperation(te), null) )
                        return false;
                    if (te.HasTypeVariables())
                        return te.Unify(this.substitution, unification, previouslyUnified);
                    return true;
                }
                if (unification == SortOfUnification.Incremental) {
                    this.substitution = UnionType.collect(this.substitution, te);
                    return true;
                }
                this.substitution = te;
                return true;
            }
            substitution = te;
            return true;
        }
public bool add ( TypeExpression te , SortOfUnification unification , IList < Pair < TypeExpression , TypeExpression > > previouslyUnified ) { TypeVariable tv = te as TypeVariable ; if ( tv != null ) { if ( tv . Substitution != null ) if ( ! this . add ( tv . EquivalenceClass . Substitution , unification , previouslyUnified ) ) return false ; this . typeVariables [ tv . Variable ] = tv ; if ( tv . EquivalenceClass != null ) { IList < int > keys = new List < int > ( tv . EquivalenceClass . TypeVariables . Keys ) ; foreach ( int pairKey in keys ) { if ( ! this . typeVariables . ContainsKey ( pairKey ) ) { this . typeVariables [ pairKey ] = tv . EquivalenceClass . TypeVariables [ pairKey ] ; this . add ( tv . EquivalenceClass . TypeVariables [ pairKey ] , unification , previouslyUnified ) ; } } } tv . EquivalenceClass = this ; return true ; } if ( this . substitution != null ) { if ( unification == SortOfUnification . Equivalent ) { if ( ! ( bool ) this . substitution . AcceptOperation ( new EquivalentOperation ( te ) , null ) ) return false ; if ( te . HasTypeVariables ( ) ) return te . Unify ( this . substitution , unification , previouslyUnified ) ; return true ; } if ( unification == SortOfUnification . Incremental ) { this . substitution = UnionType . collect ( this . substitution , te ) ; return true ; } this . substitution = te ; return true ; } substitution = te ; return true ; }
To add a new type to the class equivalence.
To add a new type to the class equivalence .
// * Another type variable // * Tries to add its substitution // * If it has a substitution  // * We try to add it to ourselves // * Both susbstitutions are not the same // * If no error, we add it to the equivalence class // * The parameter already has a equivalence class // * We recursively add all the element of the equivalence class // * Finally, we update the equivalence class of tv // * te is not a type variable // * A substitution already exists // * They must be equivalent // var1=Array(int) must be unified to Array(var1) // * The incremental behaviour implies a union of all the types // * Override unification (the susbstitution is overridden) // * We set the type as a susbstitution
If the substitution has been correctly applied
If the substitution has been correctly applied
te
TypeExpression
The type to be added
The type to be added
unification
SortOfUnification
Indicates if the kind of unification (equivalent, incremental or override).
Indicates if the kind of unification ( equivalent incremental or override ) .
previouslyUnified
IList<Pair<TypeExpression, TypeExpression>>
To detect infinite loops. The previously unified pairs of type expressions.
To detect infinite loops . The previously unified pairs of type expressions .