Exemplo n.º 1
0
        public SingletonEmbedding(TypeEmbedder owner, Symbol symbol)
        {
            Contract.Requires(owner != null);
            Contract.Requires(symbol != null && symbol.IsNonVarConstant);
            Owner = owner;
            bool wasAdded;

            if (symbol.Kind == SymbolKind.BaseCnstSymb)
            {
                var bc = (BaseCnstSymb)symbol;
                if (bc.CnstKind == CnstKind.Numeric && ((Rational)bc.Raw).IsInteger)
                {
                    var r = Index.MkApply(symbol, TermIndex.EmptyArgs, out wasAdded);
                    Type = Index.MkApply(Index.RangeSymbol, new Term[] { r, r }, out wasAdded);
                }
                else
                {
                    Type = Index.MkApply(symbol, TermIndex.EmptyArgs, out wasAdded);
                }
            }
            else
            {
                Type = Index.MkApply(symbol, TermIndex.EmptyArgs, out wasAdded);
            }

            singletonCon = Context.MkConstructor(
                string.Format(CreatorName, symbol.PrintableName),
                string.Format(TesterName, symbol.PrintableName));

            Representation = Context.MkDatatypeSort(string.Format(SortName, symbol.PrintableName), new Z3Con[] { singletonCon });
            CreationFun    = singletonCon.ConstructorDecl;
            TesterFun      = singletonCon.TesterDecl;
            DefaultMember  = new Tuple <Term, Z3Expr>(Value, CreationFun.Apply());
        }
Exemplo n.º 2
0
        public StringEmbedding(TypeEmbedder owner, uint cost)
        {
            Contract.Requires(owner != null);
            Owner        = owner;
            EncodingCost = cost;
            bool wasAdded;

            Type = Index.MkApply(Index.SymbolTable.GetSortSymbol(BaseSortKind.String), TermIndex.EmptyArgs, out wasAdded);

            charSort = Context.MkBitVecSort(CharWidth);

            //// Converts bit vectors to strings.
            charBoxing = Context.MkConstructor(
                CharBoxingName,
                IsCharName,
                new string[] { CharUnboxingName },
                new Z3Sort[] { charSort });

            //// Append a char to a string
            appStr = Context.MkConstructor(
                AppStrName,
                IsMultiStrName,
                new string[] { AppPrefixName, AppSuffixName },
                new Z3Sort[] { null, charSort },
                new uint[] { 0, 0 });

            neStrSort = Context.MkDatatypeSort(NeStrSortName, new Z3Con[] { charBoxing, appStr });

            isChar       = charBoxing.TesterDecl;
            charUnboxing = charBoxing.AccessorDecls[0];
            isMultiStr   = appStr.TesterDecl;
            appPrefix    = appStr.AccessorDecls[0];
            appSuffix    = appStr.AccessorDecls[1];

            //// Functions for building strings.
            strBoxing = Context.MkConstructor(
                StrBoxingName,
                IsNeStrName,
                new string[] { StrUnboxingName },
                new Z3Sort[] { neStrSort });

            emptyStr = Context.MkConstructor(
                EmptyStrName,
                IsEmptyStrName,
                null,
                null,
                null);

            strSort     = Context.MkDatatypeSort(StrSortName, new Z3Con[] { emptyStr, strBoxing });
            isNeStr     = strBoxing.TesterDecl;
            strUnboxing = strBoxing.AccessorDecls[0];
            isEmptyStr  = emptyStr.TesterDecl;

            Representation = strSort;
            DefaultMember  = new Tuple <Term, Z3Expr>(Index.EmptyStringValue, emptyStr.ConstructorDecl.Apply());
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        public IntegerEmbedding(TypeEmbedder owner, uint cost)
        {
            Contract.Requires(owner != null);
            Owner          = owner;
            EncodingCost   = cost;
            Representation = Context.MkIntSort();
            bool wasAdded;

            Type          = Index.MkApply(Index.SymbolTable.GetSortSymbol(BaseSortKind.Integer), TermIndex.EmptyArgs, out wasAdded);
            DefaultMember = new Tuple <Term, Z3Expr>(Index.MkCnst(Rational.Zero, out wasAdded), Context.MkNumeral(0, Representation));
        }
Exemplo n.º 5
0
        public PosIntegerEmbedding(TypeEmbedder owner, uint cost)
        {
            Contract.Requires(owner != null);
            Owner        = owner;
            EncodingCost = cost;
            bool wasAdded;

            Type           = Index.MkApply(Index.SymbolTable.GetSortSymbol(BaseSortKind.PosInteger), TermIndex.EmptyArgs, out wasAdded);
            boxingCon      = Context.MkConstructor(BoxingName, TesterName, new string[] { UnboxingName }, new Z3Sort[] { Context.MkIntSort() });
            Representation = Context.MkDatatypeSort(SortName, new Z3Con[] { boxingCon });
            BoxingFun      = boxingCon.ConstructorDecl;
            UnboxingFun    = boxingCon.AccessorDecls[0];
            TesterFun      = boxingCon.TesterDecl;
            DefaultMember  = new Tuple <Term, Z3Expr>(Index.MkCnst(Rational.One, out wasAdded), BoxingFun.Apply(Context.MkInt(1)));
        }
Exemplo n.º 6
0
        public EnumEmbedding(TypeEmbedder owner, Term type, string name)
        {
            Contract.Requires(owner != null);
            Contract.Requires(type != null);
            Owner = owner;

            uint id;

            Type = type;
            foreach (var t in type.Enumerate(x => x.Args))
            {
                if (t.Symbol.Arity == 0)
                {
                    Contract.Assert(t.Symbol.Kind == SymbolKind.BaseCnstSymb || t.Symbol.Kind == SymbolKind.UserCnstSymb);
                    id = (uint)valToSymb.Count;
                    valToSymb.Add(id, t.Symbol);
                    symbToVal.Add(t.Symbol, id);
                }
            }

            var size = ((uint)valToSymb.Count).MostSignificantOne();

            Contract.Assert(((uint)valToSymb.Count).PopulationCount() == 1);
            bvSort = Context.MkBitVecSort(size);

            bool wasAdded;

            boxingCon = Context.MkConstructor(
                string.Format(BoxingName, name),
                string.Format(TesterName, name),
                new string[] { string.Format(UnboxingName, name) },
                new Z3Sort[] { bvSort });

            Representation = Context.MkDatatypeSort(string.Format(SortName, name), new Z3Con[] { boxingCon });
            BoxingFun      = boxingCon.ConstructorDecl;
            UnboxingFun    = boxingCon.AccessorDecls[0];
            TesterFun      = boxingCon.TesterDecl;
            DefaultMember  = new Tuple <Term, Z3Expr>(
                Index.MkApply(valToSymb[0], TermIndex.EmptyArgs, out wasAdded),
                BoxingFun.Apply(Context.MkBV(0, size)));
        }
Exemplo n.º 7
0
        public IntRangeEmbedding(TypeEmbedder owner, BigInteger lower, BigInteger upper)
        {
            Contract.Requires(owner != null);
            Contract.Requires(lower <= upper);
            Owner = owner;
            Lower = lower;
            Upper = upper;
            var width = upper - lower + 1;

            Contract.Assert(width > 1 && width.IsPowerOfTwo);
            bvSort = Context.MkBitVecSort(width.MostSignificantOne());

            bool wasAdded;

            Type = Index.MkApply(Index.RangeSymbol,
                                 new Term[]
            {
                Index.MkCnst(new Rational(lower, BigInteger.One), out wasAdded),
                Index.MkCnst(new Rational(upper, BigInteger.One), out wasAdded)
            },
                                 out wasAdded);

            boxingCon = Context.MkConstructor(
                string.Format(BoxingName, lower, upper),
                string.Format(TesterName, lower, upper),
                new string[] { string.Format(UnboxingName, lower, upper) },
                new Z3Sort[] { bvSort });

            Representation = Context.MkDatatypeSort(string.Format(SortName, lower, upper), new Z3Con[] { boxingCon });
            BoxingFun      = boxingCon.ConstructorDecl;
            UnboxingFun    = boxingCon.AccessorDecls[0];
            TesterFun      = boxingCon.TesterDecl;
            DefaultMember  = new Tuple <Term, Z3Expr>(
                Index.MkCnst(new Rational(lower, BigInteger.One), out wasAdded),
                BoxingFun.Apply(Context.MkBV(0, bvSort.Size)));

            z3Lower = Context.MkInt(Lower.ToString());
        }
Exemplo n.º 8
0
        public UnionEmbedding(TypeEmbedder owner, Term unnType, Map <Term, Tuple <uint, UserSymbol> > sortIndices)
        {
            Contract.Requires(owner != null);
            Owner          = owner;
            Name           = string.Format("Unn_{0}", sortIndices[unnType].Item1);
            Type           = unnType;
            CanonicalUnion = new AppFreeCanUnn(unnType);
            var unn = CanonicalUnion;

            Z3Con con;
            LinkedList <ITypeEmbedding> intFacts, cnstFacts;
            LinkedList <Z3Con>          allConstructors = new LinkedList <Z3Con>();

            owner.GetFactorizations(unnType, out intFacts, out cnstFacts);
            foreach (var te in intFacts)
            {
                rngBoxings.Add(
                    te.Type,
                    con = Context.MkConstructor(
                        string.Format("Box_{0}_{1}", Name, te.Representation.Name),
                        string.Format("IsBox_{0}_{1}", Name, te.Representation.Name),
                        new string[] { string.Format("Unbox_{0}_{1}", Name, te.Representation.Name) },
                        new Z3Sort[] { te.Representation }));
                allConstructors.AddLast(con);
            }

            foreach (var te in cnstFacts)
            {
                con = Context.MkConstructor(
                    string.Format("Box_{0}_{1}", Name, te.Representation.Name),
                    string.Format("IsBox_{0}_{1}", Name, te.Representation.Name),
                    new string[] { string.Format("Unbox_{0}_{1}", Name, te.Representation.Name) },
                    new Z3Sort[] { te.Representation });
                allConstructors.AddLast(con);

                foreach (var t in te.Type.Enumerate(x => x.Args))
                {
                    if (t.Symbol.Arity == 0)
                    {
                        otherBoxings.Add(t.Symbol, con);
                    }
                }
            }

            bool wasAdded;

            foreach (var m in unn.NonRangeMembers)
            {
                if (m.Kind == SymbolKind.BaseSortSymb)
                {
                    var te = Owner.GetEmbedding(Index.MkApply(m, TermIndex.EmptyArgs, out wasAdded));
                    otherBoxings.Add(
                        m,
                        con = Context.MkConstructor(
                            string.Format("Box_{0}_{1}", Name, te.Representation.Name),
                            string.Format("IsBox_{0}_{1}", Name, te.Representation.Name),
                            new string[] { string.Format("Unbox_{0}_{1}", Name, te.Representation.Name) },
                            new Z3Sort[] { te.Representation }));
                    allConstructors.AddLast(con);
                }
                else if (m.Kind == SymbolKind.UserSortSymb)
                {
                    otherBoxings.Add(
                        m,
                        con = Context.MkConstructor(
                            string.Format("Box_{0}_{1}", Name, ((UserSortSymb)m).DataSymbol.FullName),
                            string.Format("IsBox_{0}_{1}", Name, ((UserSortSymb)m).DataSymbol.FullName),
                            new string[] { string.Format("Unbox_{0}_{1}", Name, ((UserSortSymb)m).DataSymbol.FullName) },
                            new Z3Sort[] { null },
                            new uint[] { sortIndices[Index.MkApply(m, TermIndex.EmptyArgs, out wasAdded)].Item1 }));
                    allConstructors.AddLast(con);
                }
            }

            Boxers = allConstructors.ToArray(allConstructors.Count);
        }
Exemplo n.º 9
0
        private void CreateTypeEmbedder()
        {
            var  conf = (Configuration)Source.Config.CompilerData;
            Cnst value;
            var  costMap = new Map <BaseSortKind, uint>((x, y) => (int)x - (int)y);

            if (conf.TryGetSetting(Configuration.Solver_RealCostSetting, out value))
            {
                costMap[BaseSortKind.Real] = (uint)((Rational)value.Raw).Numerator;
            }
            else
            {
                costMap[BaseSortKind.Real] = 10;
            }

            if (conf.TryGetSetting(Configuration.Solver_StringCostSetting, out value))
            {
                costMap[BaseSortKind.String] = (uint)((Rational)value.Raw).Numerator;
            }
            else
            {
                costMap[BaseSortKind.String] = 10;
            }

            if (conf.TryGetSetting(Configuration.Solver_IntegerCostSetting, out value))
            {
                costMap[BaseSortKind.Integer] = (uint)((Rational)value.Raw).Numerator;
            }
            else
            {
                costMap[BaseSortKind.Integer] = 11;
            }

            if (conf.TryGetSetting(Configuration.Solver_NaturalCostSetting, out value))
            {
                costMap[BaseSortKind.Natural] = (uint)((Rational)value.Raw).Numerator;
            }
            else
            {
                costMap[BaseSortKind.Natural] = 12;
            }

            if (conf.TryGetSetting(Configuration.Solver_PosIntegerCostSetting, out value))
            {
                costMap[BaseSortKind.PosInteger] = (uint)((Rational)value.Raw).Numerator;
            }
            else
            {
                costMap[BaseSortKind.PosInteger] = 13;
            }

            if (conf.TryGetSetting(Configuration.Solver_NegIntegerCostSetting, out value))
            {
                costMap[BaseSortKind.NegInteger] = (uint)((Rational)value.Raw).Numerator;
            }
            else
            {
                costMap[BaseSortKind.NegInteger] = 13;
            }

            TypeEmbedder = new TypeEmbedder(PartialModel.Index, Context, costMap);
        }