Beispiel #1
0
        ///////////////////////////////////////////////////////////////////////////

        private static string TypeToString(Type t)
        {
            Contract.Requires(t != null);
            Contract.Ensures(Contract.Result <string>() != null);

            return(SMTLibExprLineariser.TypeToString(t));
        }
 public SMTLibOpLineariser(SMTLibExprLineariser ExprLineariser, TextWriter wr)
 {
     Contract.Requires(ExprLineariser != null);
     Contract.Requires(wr != null);
     this.ExprLineariser = ExprLineariser;
     this.wr             = wr;
 }
        public override bool Visit(VCExprNAry node, bool arg)
        {
            Contract.Requires(node != null);

            if (node.Op is VCExprStoreOp)
            {
                RegisterStore(node);
            }
            else if (node.Op is VCExprSelectOp)
            {
                RegisterSelect(node);
            }
            else
            {
                VCExprBoogieFunctionOp op = node.Op as VCExprBoogieFunctionOp;
                if (op != null &&
                    !(op.Func is DatatypeConstructor) && !(op.Func is DatatypeMembership) && !(op.Func is DatatypeSelector) &&
                    !KnownFunctions.Contains(op.Func))
                {
                    Function f = op.Func;
                    Contract.Assert(f != null);

                    var builtin = SMTLibExprLineariser.ExtractBuiltin(f);
                    if (builtin == null)
                    {
                        string printedName = Namer.GetQuotedName(f, f.Name);
                        Contract.Assert(printedName != null);

                        Contract.Assert(f.OutParams.Count == 1);
                        var    argTypes = f.InParams.Cast <Variable>().MapConcat(p => TypeToStringReg(p.TypedIdent.Type), " ");
                        string decl;
                        if (RegisteredRelations.Contains(op.Func))
                        {
                            decl = "(declare-rel " + printedName + " (" + argTypes + ") " + ")";
                        }
                        else
                        {
                            decl = "(declare-fun " + printedName + " (" + argTypes + ") " + TypeToStringReg(f.OutParams[0].TypedIdent.Type) + ")";
                        }
                        AddDeclaration(decl);
                    }
                    KnownFunctions.Add(f);
                }
                else
                {
                    var lab = node.Op as VCExprLabelOp;
                    if (lab != null && !KnownLBL.Contains(lab.label))
                    {
                        KnownLBL.Add(lab.label);
                        var name = SMTLibNamer.QuoteId(SMTLibNamer.LabelVar(lab.label));
                        AddDeclaration("(declare-fun " + name + " () Bool)");
                    }
                }
            }

            return(base.Visit(node, arg));
        }
        public static string ToString(VCExpr e, UniqueNamer namer, SMTLibProverOptions opts, ISet <VCExprVar> namedAssumes = null, IList <string> optReqs = null, ISet <VCExprVar> tryAssumes = null)
        {
            Contract.Requires(e != null);
            Contract.Requires(namer != null);
            Contract.Ensures(Contract.Result <string>() != null);

            StringWriter         sw  = new StringWriter();
            SMTLibExprLineariser lin = new SMTLibExprLineariser(sw, namer, opts, namedAssumes, optReqs);

            Contract.Assert(lin != null);
            lin.Linearise(e, LineariserOptions.Default);
            return(cce.NonNull(sw.ToString()));
        }
Beispiel #5
0
        private void RegisterSelect(VCExprNAry node)
        {
            RegisterType(node[0].Type);

            if (CommandLineOptions.Clo.UseArrayTheory)
            {
                return;
            }

            string name = SMTLibExprLineariser.SelectOpName(node);

            name = Namer.GetQuotedName(name, name);

            if (!KnownSelectFunctions.Contains(name))
            {
                string decl = "(declare-fun " + name + " (" + node.MapConcat(n => TypeToString(n.Type), " ") + ") " +
                              TypeToString(node.Type) + ")";
                AddDeclaration(decl);
                KnownSelectFunctions.Add(name);
            }
        }
Beispiel #6
0
        public override bool Visit(VCExprNAry node, bool arg)
        {
            Contract.Requires(node != null);

            if (node.Op is VCExprStoreOp)
            {
                RegisterStore(node);
            }
            else if (node.Op is VCExprSelectOp)
            {
                RegisterSelect(node);
            }
            else if (node.Op is VCExprSoftOp)
            {
                var exprVar = node[0] as VCExprVar;
                AddDeclaration(string.Format("(declare-fun {0} () Bool)", exprVar.Name));
                AddDeclaration(string.Format("(assert-soft {0} :weight {1})", exprVar.Name, ((VCExprSoftOp)node.Op).Weight));
            }
            else if (node.Op.Equals(VCExpressionGenerator.NamedAssumeOp))
            {
                var exprVar = node[0] as VCExprVar;
                AddDeclaration(string.Format("(declare-fun {0} () Bool)", exprVar.Name));
                if (CommandLineOptions.Clo.PrintNecessaryAssumes)
                {
                    AddDeclaration(string.Format("(assert (! {0} :named {1}))", exprVar.Name, "aux$$" + exprVar.Name));
                }
            }
            else
            {
                VCExprBoogieFunctionOp op = node.Op as VCExprBoogieFunctionOp;
                if (op != null &&
                    !(op.Func is DatatypeConstructor) && !(op.Func is DatatypeMembership) && !(op.Func is DatatypeSelector) &&
                    !KnownFunctions.Contains(op.Func))
                {
                    Function f = op.Func;
                    Contract.Assert(f != null);

                    var builtin = SMTLibExprLineariser.ExtractBuiltin(f);
                    if (builtin == null)
                    {
                        string printedName = Namer.GetQuotedName(f, f.Name);
                        Contract.Assert(printedName != null);

                        Contract.Assert(f.OutParams.Count == 1);
                        var    argTypes = f.InParams.Cast <Variable>().MapConcat(p => TypeToStringReg(p.TypedIdent.Type), " ");
                        string decl;
                        if (RegisteredRelations.Contains(op.Func))
                        {
                            decl = "(declare-rel " + printedName + " (" + argTypes + ") " + ")";
                        }
                        else
                        {
                            decl = "(declare-fun " + printedName + " (" + argTypes + ") " + TypeToStringReg(f.OutParams[0].TypedIdent.Type) + ")";
                        }
                        AddDeclaration(decl);
                        if (declHandler != null)
                        {
                            declHandler.FuncDecl(f);
                        }
                    }
                    KnownFunctions.Add(f);
                }
            }

            return(base.Visit(node, arg));
        }
Beispiel #7
0
        private void RegisterStore(VCExprNAry node)
        {
            RegisterType(node.Type); // this is the map type, registering it should register also the index and value types

            if (CommandLineOptions.Clo.UseArrayTheory)
            {
                return;
            }

            string name = SMTLibExprLineariser.StoreOpName(node);

            name = Namer.GetQuotedName(name, name);

            if (!KnownStoreFunctions.Contains(name))
            {
                string decl = "(declare-fun " + name + " (" + node.MapConcat(n => TypeToString(n.Type), " ") + ") " +
                              TypeToString(node.Type) + ")";
                AddDeclaration(decl);

                if (CommandLineOptions.Clo.TypeEncodingMethod == CommandLineOptions.TypeEncoding.Monomorphic)
                {
                    var sel = SMTLibExprLineariser.SelectOpName(node);
                    sel = Namer.GetQuotedName(sel, sel);

                    if (!KnownSelectFunctions.Contains(sel))
                    {
                        // need to declare it before reference
                        var    args    = node.SkipEnd(1);
                        var    ret     = node.Last();
                        string seldecl = "(declare-fun " + sel + " (" + args.MapConcat(n => TypeToString(n.Type), " ") + ") " +
                                         TypeToString(ret.Type) + ")";
                        AddDeclaration(seldecl);
                        KnownSelectFunctions.Add(sel);
                    }

                    string ax1 = "(assert (forall (";
                    string ax2 = "(assert (forall (";

                    string argX = "", argY = "";
                    string dist = "";
                    for (int i = 0; i < node.Arity; i++)
                    {
                        var t = " " + TypeToString(node[i].Type);
                        var x = " ?x" + i;
                        var y = " ?y" + i;
                        ax1 += " (" + x + t + ")";
                        ax2 += " (" + x + t + ")";
                        if (i != 0 && i != node.Arity - 1)
                        {
                            argX += x;
                            argY += y;
                            ax2  += " (" + y + t + ")";
                            dist += " (not (=" + x + y + "))";
                        }
                    }

                    string v = " ?x" + (node.Arity - 1);
                    ax1 += ") (! (= (" + sel + " (" + name + " ?x0" + argX + v + ")" + argX + ") " + v + ")";
                    ax1 += " :weight 0)))";

                    if (node.Arity > 3)
                    {
                        dist = "(or " + dist + ")";
                    }
                    ax2 += ") (! (=> " + dist + " (= (" + sel + " (" + name + " ?x0" + argX + v + ")" + argY + ") (" + sel + " ?x0" +
                           argY + ")))";
                    ax2 += " :weight 0)))";

                    AddDeclaration(ax1);
                    AddDeclaration(ax2);
                }

                KnownStoreFunctions.Add(name);
            }

            //
        }