Beispiel #1
0
 public CardVar(UserSymbol symbol, bool isLFPCard)
 {
     Contract.Requires(symbol != null);
     Symbol    = symbol;
     Range     = CardRange.All;
     IsLFPCard = isLFPCard;
 }
Beispiel #2
0
        private CardExpr MkExpr(UserSymbol symb, Tuple <DependencyNode, CardVar, CardVar> data, int start, int len)
        {
            bool     isAnyArg;
            CardExpr prod;
            ConSymb  con;

            prod = null;
            for (int i = start; i < start + len; ++i)
            {
                if (symb.Kind == SymbolKind.MapSymb)
                {
                    isAnyArg = ((MapSymb)symb).IsAnyArg(i);
                }
                else
                {
                    con      = (ConSymb)symb;
                    isAnyArg = con.IsNew ? con.IsAnyArg(i) : true;
                }

                if (isAnyArg)
                {
                    prod *= MkAnyUnion(data.Item1, symb.CanonicalForm[i]);
                }
                else
                {
                    prod *= MkRelationalUnion(data.Item1, symb.CanonicalForm[i]);
                }
            }

            Contract.Assert(prod != null);
            return(prod);
        }
Beispiel #3
0
            private ShapeNode[] AddRefinement(UserSymbol s)
            {
                if (refinements == null)
                {
                    refinements = new Map <UserSymbol, ShapeNode[]>(Symbol.Compare);
                }

                ShapeNode[] children;
                if (!refinements.TryFindValue(s, out children))
                {
                    children = new ShapeNode[s.Arity];
                    refinements.Add(s, children);
                }

                for (int i = 0; i < s.Arity; ++i)
                {
                    //// Some components may be null if the children were only partially expanded
                    //// to create don't care nodes.
                    if (children[i] == null)
                    {
                        children[i] = new ShapeNode();
                    }
                }

                return(children);
            }
Beispiel #4
0
        public ConstructorEmbedding(TypeEmbedder owner, UserSymbol conOrMap, Map <Term, Tuple <uint, UserSymbol> > sortIndices)
        {
            Contract.Requires(owner != null && conOrMap != null);
            Contract.Requires(conOrMap.IsDataConstructor);
            Constructor = conOrMap;
            Owner       = owner;

            bool wasAdded;

            Type = Index.MkApply(
                conOrMap.Kind == SymbolKind.ConSymb ? ((ConSymb)conOrMap).SortSymbol : ((MapSymb)conOrMap).SortSymbol,
                TermIndex.EmptyArgs,
                out wasAdded);

            var fldNames = new string[conOrMap.Arity];
            var fldSorts = new Z3Sort[conOrMap.Arity];
            var fldRefs  = new uint[conOrMap.Arity];

            IEnumerable <Field> flds;

            if (conOrMap.Kind == SymbolKind.ConSymb)
            {
                flds = ((ConDecl)(conOrMap.Definitions.First().Node)).Fields;
            }
            else
            {
                var mapDecl = (MapDecl)(conOrMap.Definitions.First().Node);
                flds = mapDecl.Dom.Concat(mapDecl.Cod);
            }

            int i = 0;
            Tuple <uint, UserSymbol> sortData;
            Term argType;

            foreach (var f in flds)
            {
                argType     = Index.GetCanonicalTerm(conOrMap, i);
                fldNames[i] = string.Format("Get_{0}_{1}", conOrMap.FullName, string.IsNullOrEmpty(f.Name) ? i.ToString() : f.Name);
                if (sortIndices.TryFindValue(argType, out sortData))
                {
                    fldSorts[i] = null;
                    fldRefs[i]  = sortData.Item1;
                }
                else
                {
                    fldSorts[i] = owner.GetEmbedding(argType).Representation;
                    fldRefs[i]  = 0;
                }

                ++i;
            }

            Z3Constructor = Context.MkConstructor(conOrMap.FullName, "Is" + conOrMap.FullName, fldNames, fldSorts, fldRefs);
        }
Beispiel #5
0
            public void AddRefinement(UserSymbol s, ShapeNode[] children)
            {
                Contract.Requires(s != null && children != null);
                Contract.Assert(refinements == null || !refinements.ContainsKey(s));
                if (refinements == null)
                {
                    refinements = new Map <UserSymbol, ShapeNode[]>(Symbol.Compare);
                }

                refinements.Add(s, children);
            }
Beispiel #6
0
        private bool ValidateUse_UserFunc(FuncTerm ft, Namespace space, out UserSymbol symbol, List <Flag> flags, bool allowDerived = false)
        {
            Contract.Assert(ft.Function is Id);
            var result = true;
            var id     = (Id)ft.Function;

            if (!Resolve(id.Name, "constructor", id, space, x => x.IsDataConstructor, out symbol, flags))
            {
                return(false);
            }
            else if (symbol.Arity != ft.Args.Count)
            {
                var flag = new Flag(
                    SeverityKind.Error,
                    ft,
                    Constants.BadSyntax.ToString(string.Format("{0} got {1} arguments but needs {2}", symbol.FullName, ft.Args.Count, symbol.Arity)),
                    Constants.BadSyntax.Code);
                flags.Add(flag);
                result = false;
            }

            if (symbol.Kind == SymbolKind.ConSymb && !allowDerived && !((ConSymb)symbol).IsNew)
            {
                flags.Add(new Flag(
                              SeverityKind.Error,
                              ft,
                              Constants.ModelNewnessError.ToString(),
                              Constants.ModelNewnessError.Code));
                result = false;
            }

            var i = 0;

            foreach (var a in ft.Args)
            {
                ++i;
                if (a.NodeKind != NodeKind.Compr)
                {
                    continue;
                }

                var flag = new Flag(
                    SeverityKind.Error,
                    ft,
                    Constants.BadSyntax.ToString(string.Format("comprehension not allowed in argument {1} of {0}", symbol == null ? id.Name : symbol.FullName, i)),
                    Constants.BadSyntax.Code);
                flags.Add(flag);
                result = false;
            }

            return(result);
        }
Beispiel #7
0
        private static string ToAliasName(UserSymbol symb, bool removeRenaming, string aliasPrefix, int id)
        {
            string name = string.Format("{0}__0x{1:x}", ToEscapedName(symb.Name), id);
            var    ns   = symb.Namespace;

            while (ns.Parent != null && (!removeRenaming || ns.Parent.Parent != null))
            {
                name = ToEscapedName(ns.Name) + "__" + name;
                ns   = ns.Parent;
            }

            return(string.Format("{0}__{1}", ToEscapedName(aliasPrefix), name));
        }
Beispiel #8
0
        private bool Resolve(
            string id,
            string kind,
            Node n,
            Namespace space,
            Predicate <UserSymbol> validator,
            out UserSymbol symbol,
            List <Flag> flags,
            bool filterLookup = false)
        {
            UserSymbol other = null;

            symbol = Index.SymbolTable.Resolve(id, out other, space, filterLookup ? validator : null);
            if (symbol == null || !validator(symbol))
            {
                var flag = new Flag(
                    SeverityKind.Error,
                    n,
                    Constants.UndefinedSymbol.ToString(kind, id),
                    Constants.UndefinedSymbol.Code);
                flags.Add(flag);
                return(false);
            }
            else if (other != null)
            {
                var flag = new Flag(
                    SeverityKind.Error,
                    n,
                    Constants.AmbiguousSymbol.ToString(
                        "identifier",
                        id,
                        string.Format("({0}, {1}): {2}",
                                      symbol.Definitions.First <AST <Node> >().Node.Span.StartLine,
                                      symbol.Definitions.First <AST <Node> >().Node.Span.StartCol,
                                      symbol.FullName),
                        string.Format("({0}, {1}): {2}",
                                      other.Definitions.First <AST <Node> >().Node.Span.StartLine,
                                      other.Definitions.First <AST <Node> >().Node.Span.StartCol,
                                      other.FullName)),
                    Constants.AmbiguousSymbol.Code);
                flags.Add(flag);
                return(false);
            }

            return(true);
        }
Beispiel #9
0
        private static string ToIdString(UserSymbol symb, Namespace parent)
        {
            //// If this is in the root namespace, then the string is always just the name.
            if (symb.Namespace.Parent == null)
            {
                return(symb.Name);
            }

            //// Otherwise, factor out the namespace included in the parent.
            string name = symb.Name;
            var    ns   = symb.Namespace;

            while (ns != parent)
            {
                name = ns.Name + "." + name;
                ns   = ns.Parent;
            }

            return(name);
        }
Beispiel #10
0
        private static string ToIdString(UserSymbol symb, bool removeRenaming)
        {
            //// If this is in the root namespace, then the string is always just the name.
            if (symb.Namespace.Parent == null)
            {
                return(symb.Name);
            }

            //// Otherwise, drop the topmost renaming if requested.
            string name = symb.Name;
            var    ns   = symb.Namespace;

            while (ns.Parent != null && (!removeRenaming || ns.Parent.Parent != null))
            {
                name = ns.Name + "." + name;
                ns   = ns.Parent;
            }

            return(name);
        }
Beispiel #11
0
        private IEnumerable <UserSymbol> EnumerateNewConstructors(UserSymbol symb)
        {
            switch (symb.Kind)
            {
            case SymbolKind.MapSymb:
                yield return(symb);

                break;

            case SymbolKind.ConSymb:
                if (((ConSymb)symb).IsNew)
                {
                    yield return(symb);
                }
                break;

            case SymbolKind.UnnSymb:
                foreach (var s in symb.CanonicalForm[0].NonRangeMembers)
                {
                    switch (s.Kind)
                    {
                    case SymbolKind.UserSortSymb:
                        yield return(((UserSortSymb)s).DataSymbol);

                        break;

                    default:
                        break;
                    }
                }

                break;

            default:
                throw new NotImplementedException();
            }
        }
Beispiel #12
0
        /// <summary>
        /// Factorizes constants into Enum, IntRange, and Singleton embeddings.
        /// </summary>
        private void FactorizeConstants(UserSymbol s, int index)
        {
            Term type;

            if (typeToFactors.ContainsKey(Index.GetCanonicalTerm(s, index)))
            {
                return;
            }

            bool           wasAdded;
            var            unn = s.CanonicalForm[index];
            ITypeEmbedding factor;

            //// First, factorize ranges into IntRange and Singleton s.t. each IntRange contains
            //// 2^n constants for n > 0.
            BigInteger lower, upper, aligned, width;
            LinkedList <ITypeEmbedding> intFactors = new LinkedList <ITypeEmbedding>();

            foreach (var r in unn.RangeMembers)
            {
                lower = r.Key;
                upper = r.Value;
                while (lower <= upper)
                {
                    width = upper - lower + 1;
                    if (width == 1)
                    {
                        type = MkRngType(lower, upper);
                        if (!typeToEmbedding.TryFindValue(type, out factor))
                        {
                            Register(factor = new SingletonEmbedding(this, type.Args[0].Symbol));
                        }

                        intFactors.AddLast(factor);
                        break;
                    }
                    else
                    {
                        aligned = lower + BigInteger.Pow(2, (int)width.MostSignificantOne()) - 1;
                        type    = MkRngType(lower, aligned);
                        if (!typeToEmbedding.TryFindValue(type, out factor))
                        {
                            Register(factor = new IntRangeEmbedding(this, lower, aligned));
                        }

                        lower = aligned + 1;
                        intFactors.AddLast(factor);
                    }
                }
            }

            //// Second, factorize non-integers into Enum and Singleton s.t. each Enum contains 2^n constants for n > 0.
            //// Need to know the number of constants that will be factorized.
            uint cnstsToFac = 0;
            LinkedList <ITypeEmbedding> cnstFactors = new LinkedList <ITypeEmbedding>();

            foreach (var m in unn.NonRangeMembers)
            {
                if (m.Kind != SymbolKind.BaseCnstSymb && m.Kind != SymbolKind.UserCnstSymb)
                {
                    continue;
                }

                ++cnstsToFac;
            }

            if (cnstsToFac != 0)
            {
                type = null;
                uint i           = 0;
                uint amountToFac = ((uint)1) << (int)cnstsToFac.MostSignificantOne();
                foreach (var m in unn.NonRangeMembers)
                {
                    if (m.Kind != SymbolKind.BaseCnstSymb && m.Kind != SymbolKind.UserCnstSymb)
                    {
                        continue;
                    }

                    ++i;
                    type = type == null
                            ? Index.MkApply(m, TermIndex.EmptyArgs, out wasAdded)
                            : Index.MkApply(
                        Index.TypeUnionSymbol,
                        new Term[] { Index.MkApply(m, TermIndex.EmptyArgs, out wasAdded), type },
                        out wasAdded);

                    if (i < amountToFac)
                    {
                        continue;
                    }
                    else if (amountToFac == 1)
                    {
                        if (!typeToEmbedding.TryFindValue(type, out factor))
                        {
                            Register(factor = new SingletonEmbedding(this, m));
                        }

                        cnstFactors.AddLast(factor);
                        break;
                    }
                    else
                    {
                        if (!typeToEmbedding.TryFindValue(type, out factor))
                        {
                            Register(factor = new EnumEmbedding(this, type, string.Format("{0}_{1}_{2}", s.FullName, index, cnstFactors.Count)));
                        }

                        cnstFactors.AddLast(factor);
                        cnstsToFac -= amountToFac;
                        if (cnstsToFac == 0)
                        {
                            break;
                        }

                        i           = 0;
                        type        = null;
                        amountToFac = ((uint)1) << (int)cnstsToFac.MostSignificantOne();
                    }
                }
            }

            typeToFactors.Add(
                Index.GetCanonicalTerm(s, index),
                new Tuple <LinkedList <ITypeEmbedding>, LinkedList <ITypeEmbedding> >(intFactors, cnstFactors));
        }