Exemplo n.º 1
0
        public static CatFxnType Infer(CatExpr f)
        {
            if (!Config.gbTypeChecking)
            {
                return(null);
            }

            if (f.Count == 0)
            {
                if (Config.gbVerboseInference)
                {
                    Log("type is ( -> )");
                }
                return(CatFxnType.Create("( -> )"));
            }
            else if (f.Count == 1)
            {
                Function x = f[0];
                if (Config.gbVerboseInference)
                {
                    OutputInferredType(x.GetFxnType());
                }
                return(x.GetFxnType());
            }
            else
            {
                Function   x  = f[0];
                CatFxnType ft = x.GetFxnType();
                if (Config.gbVerboseInference)
                {
                    Log("initial term = " + x.GetName() + " : " + x.GetFxnTypeString());
                }

                for (int i = 1; i < f.Count; ++i)
                {
                    if (ft == null)
                    {
                        return(ft);
                    }
                    Function y = f[i];
                    if (Config.gbVerboseInference)
                    {
                        Log("Composing accumulated terms with next term");
                        string s = "previous terms = { ";
                        for (int j = 0; j < i; ++j)
                        {
                            s += f[j].GetName() + " ";
                        }
                        Log(s + "} : " + ft.ToString());
                        Log("next term = " + y.GetName() + " : " + y.GetFxnTypeString());
                    }

                    ft = ComposeTypes(ft, y.GetFxnType());

                    if (ft == null)
                    {
                        return(null);
                    }
                }
                return(ft);
            }
        }
Exemplo n.º 2
0
        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;
        }
Exemplo n.º 3
0
        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);
        }