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));
        }
Exemple #2
0
        public static string TypeToString(Type t)
        {
            Contract.Requires(t != null);
            Contract.Ensures(Contract.Result <string>() != null);

            if (t.IsBool)
            {
                return("Bool");
            }
            else if (t.IsInt)
            {
                return("Int");
            }
            else if (t.IsReal)
            {
                return("Real");
            }
            else if (t.IsFloat)
            {
                return("(_ FloatingPoint " + t.FloatExponent + " " + t.FloatSignificand + ")");
            }
            else if (t.IsBv)
            {
                return("(_ BitVec " + t.BvBits + ")");
            }
            else if (t.IsRMode)
            {
                return("RoundingMode");
            }
            else if (t.IsString)
            {
                return("String");
            }
            else if (t.IsRegEx)
            {
                return("(RegEx String)");
            }
            else if (t.IsSeq)
            {
                return("(Seq " + TypeToString(t.AsCtor.Arguments[0]) + ")");
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                TypeToStringHelper(t, sb);
                var s = sb.ToString();
                if (s[0] == '(')
                {
                    return(s);
                }
                else
                {
                    return(SMTLibNamer.QuoteId("T@" + s));
                }
            }
        }
Exemple #3
0
    public SMTLibProcessTheoremProver(ProverOptions options, VCExpressionGenerator gen,
                                      SMTLibProverContext ctx)
    {
      Contract.Requires(options != null);
      Contract.Requires(gen != null);
      Contract.Requires(ctx != null);
      
      InitializeGlobalInformation();
      
      this.options = (SMTLibProverOptions)options;
      this.ctx = ctx;
      this.gen = gen;
      this.usingUnsatCore = false;

      SetupAxiomBuilder(gen);

      Namer = new SMTLibNamer();
      ctx.parent = this;
      this.DeclCollector = new TypeDeclCollector((SMTLibProverOptions)options, Namer);

      if (CommandLineOptions.Clo.PrintFixedPoint != null || CommandLineOptions.Clo.PrintConjectures != null)
      {
          declHandler = new MyDeclHandler();
          DeclCollector.SetDeclHandler(declHandler);
      }

      SetupProcess();

      if (CommandLineOptions.Clo.StratifiedInlining > 0 || CommandLineOptions.Clo.ContractInfer
          || CommandLineOptions.Clo.SecureVcGen != null)
      {
          // Prepare for ApiChecker usage
          if (options.LogFilename != null && currentLogFile == null)
          {
              currentLogFile = OpenOutputFile("");
          }
          PrepareCommon();
      }
    }
            public bool VisitLabelOp(VCExprNAry node, LineariserOptions options)
            {
                if (ExprLineariser.UnderQuantifier > 0 && !options.LabelsBelowQuantifiers)
                {
                    ExprLineariser.Linearise(node[0], options);
                    return(true);
                }

                var op = (VCExprLabelOp)node.Op;

                if (CommandLineOptions.Clo.UseLabels)
                {
                    // Z3 extension
                    //wr.Write("({0} {1} ", op.pos ? "lblpos" : "lblneg", SMTLibNamer.QuoteId(op.label));
                    wr.Write("(! ");
                }

                if (!options.LabelsBelowQuantifiers)
                {
                    wr.Write("({0} {1} ", op.pos ? "and" : "or", SMTLibNamer.QuoteId(SMTLibNamer.LabelVar(op.label)));
                }

                ExprLineariser.Linearise(node[0], options);


                if (!options.LabelsBelowQuantifiers)
                {
                    wr.Write(")");
                }

                if (CommandLineOptions.Clo.UseLabels)
                {
                    wr.Write(" :{0} {1})", op.pos ? "lblpos" : "lblneg", SMTLibNamer.QuoteId(op.label));
                }

                return(true);
            }
        public static string TypeToString(Type t)
        {
            Contract.Requires(t != null);
            Contract.Ensures(Contract.Result <string>() != null);

            if (t.IsBool)
            {
                return("Bool");
            }
            else if (t.IsInt)
            {
                return("Int");
            }
            else if (t.IsReal)
            {
                return("Real");
            }
            else if (t.IsBv)
            {
                return("(_ BitVec " + t.BvBits + ")");
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                TypeToStringHelper(t, sb);
                var s = sb.ToString();
                if (s[0] == '(')
                {
                    return(s);
                }
                else
                {
                    return(SMTLibNamer.QuoteId("T@" + s));
                }
            }
        }
        /////////////////////////////////////////////////////////////////////////////////////

        public bool Visit(VCExprQuantifier node, LineariserOptions options)
        {
            Contract.Assert(node.TypeParameters.Count == 0);

            UnderQuantifier++;
            Namer.PushScope(); try {
                string kind = node.Quan == Quantifier.ALL ? "forall" : "exists";
                wr.Write("({0} (", kind);

                for (int i = 0; i < node.BoundVars.Count; i++)
                {
                    VCExprVar var = node.BoundVars[i];
                    Contract.Assert(var != null);
                    string printedName = Namer.GetQuotedLocalName(var, var.Name);
                    Contract.Assert(printedName != null);
                    wr.Write("({0} {1}) ", printedName, TypeToString(var.Type));
                }

                wr.Write(") ");

                VCQuantifierInfos infos = node.Infos;
                var weight = QKeyValue.FindIntAttribute(infos.attributes, "weight", 1);
                if (!ProverOptions.UseWeights)
                {
                    weight = 1;
                }
                var hasAttrs = node.Triggers.Count > 0 || infos.qid != null || weight != 1 || infos.uniqueId != -1;

                if (hasAttrs)
                {
                    wr.Write("(! ");
                }

                Linearise(node.Body, options);

                if (hasAttrs)
                {
                    wr.Write("\n");
                    if (infos.qid != null)
                    {
                        wr.Write(" :qid {0}\n", SMTLibNamer.QuoteId(infos.qid));
                    }
                    if (weight != 1)
                    {
                        wr.Write(" :weight {0}\n", weight);
                    }
                    if (infos.uniqueId != -1)
                    {
                        wr.Write(" :skolemid |{0}|\n", infos.uniqueId);
                    }
                    WriteTriggers(node.Triggers, options);

                    wr.Write(")");
                }

                wr.Write(")");

                return(true);
            } finally {
                UnderQuantifier--;
                Namer.PopScope();
            }
        }
Exemple #7
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);
                }
                else
                {
                    var lab = node.Op as VCExprLabelOp;
                    if (lab != null && !KnownLBL.Contains(lab.label))
                    {
                        KnownLBL.Add(lab.label);
                        var name = SMTLibNamer.QuoteId(Namer.LabelVar(lab.label));
                        AddDeclaration("(declare-fun " + name + " () Bool)");
                    }
                }
            }

            return(base.Visit(node, arg));
        }
Exemple #8
0
 private SMTLibNamer(SMTLibNamer namer) : base(namer)
 {
 }