コード例 #1
0
        /// <summary>
        /// This function modifies the function
        /// </summary>
        public void RemoveImplicitRhoVariables(CatFxnType ft)
        {
            foreach (CatKind k in ft.GetChildKinds())
            {
                if (k is CatFxnType)
                {
                    RemoveImplicitRhoVariables(k as CatFxnType);
                }
            }

            if (!ft.GetCons().IsEmpty() && !ft.GetProd().IsEmpty())
            {
                CatKind k1 = ft.GetCons().GetBottom();
                CatKind k2 = ft.GetProd().GetBottom();

                // Does both consumption and production share the same
                // stack variable at the bottom, if so, then we might have
                // an implicit Rho variables
                if (k1 is CatStackVar && k1.Equals(k2))
                {
                    // try removing the stack variable
                    ft.GetCons().GetKinds().RemoveAt(0);
                    ft.GetProd().GetKinds().RemoveAt(0);

                    // is the variable used anywhere else?
                    // if so, then we have to restore it
                    // otherwise we leave it taken away
                    if (DoesVarExist(k1))
                    {
                        ft.GetCons().GetKinds().Insert(0, k1);
                        ft.GetProd().GetKinds().Insert(0, k2);
                    }
                }
            }
        }
コード例 #2
0
ファイル: CatKinds.cs プロジェクト: tangentforks/catlang
        public override bool IsSubtypeOf(CatKind k)
        {
            if (k.IsAny())
            {
                return(true);
            }
            if (k is CatStackVar)
            {
                return(true);
            }
            if (!(k is CatTypeVector))
            {
                return(false);
            }
            CatTypeVector v1 = this;
            CatTypeVector v2 = k as CatTypeVector;

            while (!v1.IsEmpty() && !v2.IsEmpty())
            {
                CatKind t1 = v1.GetTop();
                CatKind t2 = v2.GetTop();
                if (!t1.IsSubtypeOf(t2))
                {
                    return(false);
                }
                v1 = v1.GetRest();
                v2 = v2.GetRest();
            }
            // v1 has to be at least as long to be a subtype
            if (v1.IsEmpty() && !v2.IsEmpty())
            {
                return(false);
            }
            return(true);
        }
コード例 #3
0
        public static string MethodToTypeString(MethodBase m)
        {
            string s = "('R ";

            if (HasThisType(m))
            {
                s += CatKind.TypeToString(m.DeclaringType) + " ";
            }

            foreach (ParameterInfo pi in m.GetParameters())
            {
                s += CatKind.TypeToString(pi.ParameterType) + " ";
            }

            s += "-> 'R";

            if (HasThisType(m))
            {
                s += " this";
            }

            if (HasReturnType(m))
            {
                s += " " + CatKind.TypeToString(GetReturnType(m));
            }

            s += ")";

            return(s);
        }
コード例 #4
0
ファイル: CatVarRenamer.cs プロジェクト: tangentforks/catlang
 public CatKind Rename(CatKind k)
 {
     if (k is CatFxnType)
     {
         return(Rename(k as CatFxnType));
     }
     else if (k is CatTypeKind)
     {
         return(Rename(k as CatTypeKind));
     }
     else if (k is CatStackVar)
     {
         return(Rename(k as CatStackVar));
     }
     else if (k is CatTypeVector)
     {
         return(Rename(k as CatTypeVector));
     }
     else if (k is CatCustomKind)
     {
         return(k);
     }
     else if (k is CatRecursiveType)
     {
         return(k);
     }
     else
     {
         throw new Exception(k.ToString() + " is an unrecognized kind");
     }
 }
コード例 #5
0
 public PushValue(T x)
 {
     mValue      = new CatMetaValue <T>(x);
     msName      = mValue.GetData().ToString();
     msValueType = CatKind.TypeNameFromObject(x);
     mpFxnType   = CatFxnType.Create("( -> " + msValueType + ")");
 }
コード例 #6
0
ファイル: CatKinds.cs プロジェクト: tangentforks/catlang
        public override bool Equals(CatKind k)
        {
            if (!(k is CatTypeVector))
            {
                return(false);
            }
            CatTypeVector v1 = this;
            CatTypeVector v2 = k as CatTypeVector;

            while (!v1.IsEmpty() && !v2.IsEmpty())
            {
                CatKind t1 = v1.GetTop();
                CatKind t2 = v2.GetTop();
                if (!t1.Equals(t2))
                {
                    return(false);
                }
                v1 = v1.GetRest();
                v2 = v2.GetRest();
            }
            if (!v1.IsEmpty())
            {
                return(false);
            }
            if (!v2.IsEmpty())
            {
                return(false);
            }
            return(true);
        }
コード例 #7
0
        public int GetMaxConsumption()
        {
            int nCnt = 0;

            List <CatKind> list = mCons.GetKinds();

            for (int i = list.Count - 1; i >= 0; --i)
            {
                CatKind k = list[i];
                if (k is CatStackVar)
                {
                    if ((i == 0) && k.Equals(mProd.GetBottom()))
                    {
                        return(nCnt);
                    }
                    else
                    {
                        return(-1);
                    }
                }
                nCnt++;
            }

            return(nCnt);
        }
コード例 #8
0
ファイル: CatKinds.cs プロジェクト: tangentforks/catlang
 public override bool IsSubtypeOf(CatKind k)
 {
     if (k is CatSimpleTypeKind)
     {
         return(GetSuperType().IsSubtypeOf(k));
     }
     return(this.Equals(k));
 }
コード例 #9
0
 public bool IsFreeVar(CatKind var)
 {
     if (!var.IsKindVar())
     {
         return(false);
     }
     return(mpFreeVars.ContainsKey(var.ToString()));
 }
コード例 #10
0
ファイル: CatKinds.cs プロジェクト: tangentforks/catlang
 public override bool Equals(CatKind k)
 {
     if (!(k is CatTypeVar))
     {
         return(false);
     }
     return(ToString().CompareTo(k.ToString()) == 0);
 }
コード例 #11
0
ファイル: CatKinds.cs プロジェクト: tangentforks/catlang
 public override bool Equals(CatKind k)
 {
     if (!(k is CatStackVar))
     {
         return(false);
     }
     return(k.ToString() == this.ToString());
 }
コード例 #12
0
ファイル: CatKinds.cs プロジェクト: tangentforks/catlang
 public virtual bool IsSubtypeOf(CatKind k)
 {
     if (k.ToString().Equals("any"))
     {
         return(true);
     }
     return(this.Equals(k));
 }
コード例 #13
0
        public CatClass(CatClass o, string s, CatKind k)
        {
            foreach (KeyValuePair <string, CatKind> kvp in o.GetFields())
            {
                mpFields.Add(kvp.Key, kvp.Value);
            }

            mpFields.Add(s, k);
        }
コード例 #14
0
        /// <summary>
        /// This is a raw equivalency check: no normalization is done.
        /// To comparse function type normally you would use CompareFxnTypes,
        /// which in turn calls this function.
        /// </summary>
        public override bool Equals(CatKind k)
        {
            if (!(k is CatFxnType))
            {
                return(false);
            }
            CatFxnType f = k as CatFxnType;

            return(GetCons().Equals(f.GetCons()) && GetProd().Equals(f.GetProd()) &&
                   HasSideEffects() == f.HasSideEffects());
        }
コード例 #15
0
ファイル: CatKinds.cs プロジェクト: tangentforks/catlang
 public void PushKindBottom(CatKind k)
 {
     Trace.Assert(k != null);
     if (k is CatTypeVector)
     {
         mList.InsertRange(0, (k as CatTypeVector).GetKinds());
     }
     else
     {
         mList.Insert(0, k);
     }
 }
コード例 #16
0
ファイル: CatVarRenamer.cs プロジェクト: tangentforks/catlang
 public static bool DoesVarOccurIn(CatKind k, CatFxnType ft, CatFxnType except)
 {
     if (!k.IsKindVar())
     {
         return(false);
     }
     if (k == except)
     {
         return(false);
     }
     return(DoesVarOccurIn(k, ft.GetCons(), except) || DoesVarOccurIn(k, ft.GetProd(), except));
 }
コード例 #17
0
 public bool DoesVarExist(CatKind k)
 {
     Trace.Assert(k.IsKindVar());
     foreach (CatKind tmp in GetDescendantKinds())
     {
         if (tmp.Equals(k))
         {
             return(true);
         }
     }
     return(false);
 }
コード例 #18
0
ファイル: CatKinds.cs プロジェクト: tangentforks/catlang
 public void Add(CatKind k)
 {
     Trace.Assert(k != null);
     if (k is CatTypeVector)
     {
         mList.AddRange((k as CatTypeVector).GetKinds());
     }
     else
     {
         mList.Add(k);
     }
 }
コード例 #19
0
        /*
         * public CatFxnType GetUnquotedFxnType()
         * {
         *  CatKind k = GetUnquotedKind();
         *  if (!(k is CatFxnType))
         *      throw new Exception("illegal type for a quoted function, should produce a single function : " + mpFxnType.ToString());
         *  return k as CatFxnType;
         * }
         */

        public CatKind GetUnquotedKind()
        {
            if (mpFxnType.GetCons().GetKinds().Count != 0)
            {
                throw new Exception("illegal type for a quoted function, should have no consumption : " + mpFxnType.ToString());
            }
            if (mpFxnType.GetProd().GetKinds().Count != 1)
            {
                throw new Exception("illegal type for a quoted function, should have a single production : " + mpFxnType.ToString());
            }
            CatKind k = mpFxnType.GetProd().GetKinds()[0];

            return(k);
        }
コード例 #20
0
ファイル: CatKinds.cs プロジェクト: tangentforks/catlang
        public override bool Equals(CatKind k)
        {
            if (k == this)
            {
                return(true);
            }
            if (!(k is CatMetaValue <T>))
            {
                return(false);
            }
            CatMetaValue <T> tmp = k as CatMetaValue <T>;

            return(tmp.GetData().Equals(mData));
        }
コード例 #21
0
ファイル: CatKinds.cs プロジェクト: tangentforks/catlang
 public override bool Equals(CatKind k)
 {
     if (!(k is CatCustomKind))
     {
         return(false);
     }
     if ((k as CatCustomKind).GetId() == GetId())
     {
         Trace.Assert(k == this);
         return(true);
     }
     else
     {
         return(false);
     }
 }
コード例 #22
0
ファイル: CatVarScopes.cs プロジェクト: tangentforks/catlang
 public bool IsFreeVar(CatFxnType context, CatKind k)
 {
     // Is it contained in a function outside of the context?
     // Find all scopes associated with the kind
     if (!k.IsKindVar())
     {
         return(false);
     }
     foreach (CatFxnType tmp in GetAssociatedScopes(k))
     {
         if (!tmp.DescendentOf(context))
         {
             return(false);
         }
     }
     return(true);
 }
コード例 #23
0
ファイル: CatVarRenamer.cs プロジェクト: tangentforks/catlang
        public CatStackKind Rename(CatStackVar s)
        {
            string sName = s.ToString();

            if (mNames.ContainsKey(sName))
            {
                CatKind tmp = mNames[sName];
                if (!(tmp is CatStackKind))
                {
                    throw new Exception(sName + " is not a stack kind");
                }
                return(tmp as CatStackKind);
            }

            CatStackVar var = GenerateNewVar(sName) as CatStackVar;

            mNames.Add(sName, var);
            return(var);
        }
コード例 #24
0
 public Constraint CatKindToConstraint(CatKind k)
 {
     if (k is CatTypeVector)
     {
         return(TypeVectorToConstraintVector(k as CatTypeVector));
     }
     else if (k is CatFxnType)
     {
         return(FxnTypeToRelation(k as CatFxnType));
     }
     else if (k.IsKindVar())
     {
         return(KindVarToConstraintVar(k));
     }
     else
     {
         return(new Constant(k.ToIdString()));
     }
 }
コード例 #25
0
ファイル: CatVarRenamer.cs プロジェクト: tangentforks/catlang
        public static bool DoesVarOccurIn(CatKind k, CatTypeVector vec, CatFxnType except)
        {
            foreach (CatKind tmp in vec.GetKinds())
            {
                if (tmp.IsKindVar() && tmp.Equals(k))
                {
                    return(true);
                }

                if (tmp is CatFxnType)
                {
                    if (DoesVarOccurIn(k, tmp as CatFxnType, except))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
コード例 #26
0
ファイル: CatVarRenamer.cs プロジェクト: tangentforks/catlang
        public static CatFxnType RenameFreeVars(CatFxnType left, CatFxnType right, CatFxnType ft)
        {
            CatTypeVarList vars = ft.GetAllVars();

            foreach (string s in vars.Keys)
            {
                CatKind k = vars[s];
                if (IsFreeVar(k, left, right, ft))
                {
                    if (k is CatTypeVar)
                    {
                        vars[s] = CatTypeVar.CreateUnique();
                    }
                    else
                    {
                        vars[s] = CatStackVar.CreateUnique();
                    }
                }
            }
            return(RenameVars(ft, vars));
        }
コード例 #27
0
        public static string ToPrettyString(CatTypeVector vec, Dictionary <string, string> dic)
        {
            string s = "";

            for (int i = 0; i < vec.GetKinds().Count; ++i)
            {
                if (i > 0)
                {
                    s += " ";
                }
                CatKind k = vec.GetKinds()[i];
                if (k.IsKindVar())
                {
                    if (!dic.ContainsKey(k.ToString()))
                    {
                        string sNew = IntToPrettyString(dic.Count);
                        if (k is CatStackVar)
                        {
                            sNew = sNew.ToUpper();
                        }
                        dic.Add(k.ToString(), sNew);
                    }

                    s += dic[k.ToString()];
                }
                else if (k is CatFxnType)
                {
                    s += "(" + ToPrettyString(k as CatFxnType, dic) + ")";
                }
                else if (k is CatTypeVector)
                {
                    s += ToPrettyString(k as CatFxnType, dic);
                }
                else
                {
                    s += k.ToString();
                }
            }
            return(s);
        }
コード例 #28
0
        public static CatFxnType Unquote(CatFxnType ft)
        {
            if (ft == null)
            {
                return(null);
            }
            if (ft.GetCons().GetKinds().Count > 0)
            {
                throw new Exception("Can't unquote a function type with a consumption size greater than zero");
            }
            if (ft.GetProd().GetKinds().Count != 1)
            {
                throw new Exception("Can't unquote a function type which does not produce a single function");
            }
            CatKind k = ft.GetProd().GetKinds()[0];

            if (!(k is CatFxnType))
            {
                throw new Exception("Can't unquote a function type which does not produce a single function");
            }
            return(k as CatFxnType);
        }
コード例 #29
0
        public override bool IsSubtypeOf(CatKind k)
        {
            if (k.IsAny() || k.IsDynFxn())
            {
                return(IsRuntimePolymorphic());
            }
            if (k is CatTypeVar)
            {
                return(true);
            }
            if (!(k is CatFxnType))
            {
                return(false);
            }
            CatFxnType f   = k as CatFxnType;
            bool       ret = GetCons().IsSubtypeOf(f.GetCons()) && GetProd().IsSubtypeOf(f.GetProd());

            if (HasSideEffects())
            {
                ret = ret && f.HasSideEffects();
            }
            return(ret);
        }
コード例 #30
0
ファイル: CatKinds.cs プロジェクト: tangentforks/catlang
        public override bool IsSubtypeOf(CatKind k)
        {
            if (Equals(k))
            {
                return(true);
            }

            // meta_int is a subtype of int
            // and meta_bool is a subtype of bool
            // and so on.
            if (msName.IndexOf("meta_") == 0)
            {
                string s = k.ToString();
                if (s.Length == 0)
                {
                    throw new Exception("missing type name");
                }
                if (msName.IndexOf(s) == 5)
                {
                    return(true);
                }
            }
            return(false);
        }