public CatFxnType LocalComposeTypes(CatFxnType left, CatFxnType right)
        {
            // Make sure that the variables on the left function and the variables
            // on the right function are different
            CatVarRenamer renamer = new CatVarRenamer();

            left = renamer.Rename(left.AddImplicitRhoVariables());
            renamer.ResetNames();
            right = renamer.Rename(right.AddImplicitRhoVariables());

            Log("==");
            Log("Composing : " + left.ToString());
            Log("with      : " + right.ToString());

            Log("Adding constraints");

            Relation rLeft  = FxnTypeToRelation(left);
            Relation rRight = FxnTypeToRelation(right);

            //TODO: remove
            //rLeft.UnrollRecursiveRelations();
            //rRight.UnrollRecursiveRelations();

            Relation result = new Relation(rLeft.GetLeft(), rRight.GetRight());

            AddTopLevelConstraints(rLeft.GetRight(), rRight.GetLeft());
            AddConstraint(CreateVar("result$"), result);

            Log("Constraints");
            ComputeConstraintLists();
            LogConstraints();

            Log("Unifiers");
            ComputeUnifiers();
            foreach (string sVar in GetConstrainedVars())
            {
                Constraint u = GetUnifierFor(sVar);
                Log("var: " + sVar + " = " + u);
            }

            Log("Composed Type");
            Constraint c = GetResolvedUnifierFor("result$");

            if (!(c is Relation))
            {
                throw new Exception("Resolved type is not a relation");
            }

            // TODO: remove
            // Relation r = c as Relation;
            // r.RollupRecursiveRelations();

            CatKind    k  = ConstraintToCatKind(c);
            CatFxnType ft = k as CatFxnType;

            Log("raw type    : " + ft.ToString());
            Log("pretty type : " + ft.ToPrettyString());
            Log("==");

            CheckConstraintQueueEmpty();

            // Check if the relation was valid, and thus the function type
            if (!ft.IsValid())
            {
                throw new Exception("invalid function type: " + ft.ToString());
            }

            return(ft);
        }
 public static bool AreRelationsEqual(Relation r1, Relation r2)
 {
     return(r1.Equals(r2, new VarNameMap()));
 }
 public void SetParent(Relation parent)
 {
     Trace.Assert(mParent == null);
     mParent = parent;
 }
        private bool CanRollupRelation(Constraint child)
        {
            VarNameMap map = new VarNameMap();

            if (!(child is Relation))
            {
                return(false);
            }

            Relation childRel = child as Relation;

            int n = GetLeft().GetCount();

            if (n != childRel.GetLeft().GetCount())
            {
                return(false);
            }
            for (int i = 0; i < n; ++i)
            {
                Constraint tmp      = GetLeft(i);
                Constraint childTmp = childRel.GetLeft(i);

                if (tmp != child)
                {
                    if (!tmp.Equals(childTmp, map))
                    {
                        return(false);
                    }
                }
                else
                {
                    if (!(childTmp is RecursiveRelation))
                    {
                        return(false);
                    }
                }
            }
            n = GetRight().GetCount();
            if (n != childRel.GetRight().GetCount())
            {
                return(false);
            }
            for (int i = 0; i < n; ++i)
            {
                Constraint tmp      = GetRight(i);
                Constraint childTmp = childRel.GetRight(i);

                if (tmp != child)
                {
                    if (!tmp.Equals(childTmp, map))
                    {
                        return(false);
                    }
                }
                else
                {
                    if (!(childTmp is RecursiveRelation))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Example #5
0
        public Constraint ResolveRelationConstraint(Constraint c, Stack <Constraint> visited,
                                                    Relation parent, VarNameList topVars, VarNameList allVars)
        {
            VarNameList nonGenerics = parent.GetNonGenericsForChildren();

            if (c is Var)
            {
                Var        v = c as Var;
                Constraint u = Resolve(v, visited, parent);

                if (u is Relation)
                {
                    Relation r = u as Relation;
                    // Make sure we don't add variables to the non-generics
                    // list which occured in a duplicate.
                    if (!topVars.Contains(v))
                    {
                        VarNameList subVars = r.GetAllVarNames();
                        foreach (string s in subVars)
                        {
                            if (allVars.Contains(s))
                            {
                                nonGenerics.Add(s);
                            }
                        }
                        allVars.AddRange(subVars);
                    }
                    else
                    {
                        Log("duplicate of variable " + v.ToString() + ", with unifier " + r.ToString());
                        QueueForRenamingOfGenerics(r);
                    }
                }
                else if (u is Var)
                {
                    nonGenerics.Add(u as Var);
                }

                topVars.Add(v);
                return(u);
            }
            else
            {
                Constraint u = Resolve(c, visited, parent);

                // non-vars should not resolve to vars
                Trace.Assert(!(u is Var));

                if (u is Relation)
                {
                    Relation    r       = u as Relation;
                    VarNameList subVars = r.GetAllVarNames();
                    foreach (string s in subVars)
                    {
                        if (allVars.Contains(s))
                        {
                            nonGenerics.Add(s);
                        }
                    }
                    allVars.AddRange(subVars);
                }

                return(u);
            }
        }
Example #6
0
 private void QueueForRenamingOfGenerics(Relation rel)
 {
     mGenericRenamingQueue.Add(rel);
 }