예제 #1
0
        public override Cmd VisitAssignCmd(AssignCmd node)
        {
            if (node.Lhss.Count != node.Rhss.Count)
            {
                throw new ProofGenUnexpectedStateException(typeof(BasicCmdIsaVisitor),
                                                           "different number of lhs and rhs");
            }

            if (node.Lhss.Count != 1)
            {
                throw new NotImplementedException("Parallel assignments are not supported.");
            }

            /*
             * var lhsResults = node.Lhss.Select(lhs => Translate(lhs)).ToList();
             * var rhsResults = node.Rhss.Select(rhs => Translate(rhs)).ToList();
             * IList<Term> results = new List<Term>();
             * lhsResults.ZipDo(rhsResults, (lhs, rhs) => results.Add(new TermTuple(new List<Term>() { lhs, rhs })));
             */

            var lhs = Translate(node.Lhss[0]);
            var rhs = Translate(node.Rhss[0]);

            ReturnResult(IsaBoogieTerm.Assign(lhs, rhs));

            return(node);
        }
예제 #2
0
        public override Cmd VisitAssumeCmd(AssumeCmd node)
        {
            var result = Translate(node.Expr);

            ReturnResult(IsaBoogieTerm.Assume(result));

            return(node);
        }
예제 #3
0
        public override Expr VisitOldExpr(OldExpr node)
        {
            var body = Translate(node.Expr);

            ReturnResult(IsaBoogieTerm.Old(body));

            return(node);
        }
        public Term Visit(BinaryOperator binaryOperator)
        {
            if (_args.Count != 2)
            {
                throw new ExprArgException();
            }

            return(IsaBoogieTerm.Binop(binaryOperator.Op, _args[0], _args[1]));
        }
        public Term Visit(UnaryOperator unaryOperator)
        {
            if (_args.Count != 1)
            {
                throw new ExprArgException();
            }

            return(IsaBoogieTerm.Unop(unaryOperator.Op, _args[0]));
        }
예제 #6
0
        public override QuantifierExpr VisitQuantifierExpr(QuantifierExpr node)
        {
            if (!(node is ForallExpr || node is ExistsExpr))
            {
                throw new ProofGenUnexpectedStateException(GetType(), "can only handle forall and exists quantifiers");
            }

            var isForall = IsForall(node);

            //Quantifers with multiple bound variables are desugared into multiple quantifiers expressions with single variables
            foreach (var boundVar in node.Dummies)
            {
                boogieVarTranslation.VarTranslation.AddBoundVariable(boundVar);
            }
            foreach (var boundTyVar in node.TypeParameters)
            {
                boogieVarTranslation.TypeVarTranslation.AddBoundVariable(boundTyVar);
            }

            var numValVarBefore = boogieVarTranslation.VarTranslation.NumBoundVariables();
            var numTyVarBefore  = boogieVarTranslation.TypeVarTranslation.NumBoundVariables();

            var result = Translate(node.Body);

            if (numValVarBefore != boogieVarTranslation.VarTranslation.NumBoundVariables() ||
                numTyVarBefore != boogieVarTranslation.TypeVarTranslation.NumBoundVariables())
            {
                throw new ProofGenUnexpectedStateException(GetType(),
                                                           "quantifier levels not the same before and after");
            }

            for (var i = node.Dummies.Count - 1; i >= 0; i--)
            {
                boogieVarTranslation.VarTranslation.DropLastBoundVariable();
                var boundVar     = node.Dummies[i];
                var boundVarType = typeIsaVisitor.Translate(boundVar.TypedIdent.Type);
                result = IsaBoogieTerm.Quantifier(isForall, boundVarType, result);
            }

            for (var i = node.TypeParameters.Count - 1; i >= 0; i--)
            {
                boogieVarTranslation.TypeVarTranslation.DropLastBoundVariable();
                result = IsaBoogieTerm.TypeQuantifier(isForall, result);
            }

            ReturnResult(result);
            return(node);
        }
        public Term Visit(FunctionCall functionCall)
        {
            if (_args.Count != functionCall.ArgumentCount)
            {
                throw new ExprArgException();
            }

            var typeInstIsa = new List <Term>();

            foreach (var typeVar in _typeInst.FormalTypeParams)
            {
                var t = typeIsaVisitor.Translate(_typeInst[typeVar]);
                typeInstIsa.Add(t);
            }

            return(IsaBoogieTerm.FunCall(functionCall.FunctionName, typeInstIsa, _args));
        }
예제 #8
0
        public override Cmd VisitCallCmd(CallCmd node)
        {
            if (node.TypeParameters != null && node.TypeParameters.FormalTypeParams.Any())
            {
                throw new NotImplementedException("Do not support procedures with type parameters");
            }

            var argsTerm = node.Ins.Select(e => Translate(e)).ToList();
            var outsTerm = node.Outs.Select(e =>
            {
                if (boogieVarTranslation.VarTranslation.TryTranslateVariableId(e.Decl, out Term id, out _))
                {
                    return(id);
                }

                throw new ProofGenUnexpectedStateException("Could not retrieve id of variable");
            }).ToList();

            ReturnResult(IsaBoogieTerm.ProcCall(node.Proc.Name, argsTerm, outsTerm));

            return(node);
        }
        public IProgramAccessor GetIsaProgram(
            string theoryName,
            string procName,
            BoogieMethodData methodData,
            IsaProgramGeneratorConfig config,
            IVariableTranslationFactory varTranslationFactory,
            CFGRepr cfg,
            out IList <OuterDecl> decls,
            bool generateMembershipLemmas = true,
            bool onlyGlobalData           = false
            )
        {
            this.varTranslationFactory = varTranslationFactory;
            varTranslation             = varTranslationFactory.CreateTranslation();
            cmdIsaVisitor = new MultiCmdIsaVisitor(varTranslationFactory);

            /*
             * Term program = IsaBoogieTerm.Program(IsaCommonTerms.TermIdentFromName(funcs.name),
             *  new TermList(new List<Term>()),
             *  new TermList(new List<Term>()),
             *  IsaCommonTerms.TermIdentFromName(axiomsDecl.name),
             *  new List<Term>() { method });
             *
             * var list = new List<Tuple<IList<Term>, Term>>
             * {
             *  new Tuple<IList<Term>, Term>(new List<Term>(), program)
             * };
             */

            //OuterDecl programDefinition = new DefDecl("ProgramM", new Tuple<IList<Term>, Term>(new List<Term>(), program));

            decls = new List <OuterDecl>();
            var isaGlobalProgramRepr = new IsaGlobalProgramRepr(
                FunctionDeclarationsName(),
                AxiomDeclarationsName(),
                VariableDeclarationsName("globals"),
                VariableDeclarationsName("constants")
                );
            var globalsMax = methodData.Constants.Count() + methodData.GlobalVars.Count() - 1;
            // assume single versioning and order on constants, globals, params, locals
            var localsMin = globalsMax + 1;

            if (globalsMax < 0)
            {
                globalsMax = 0;
            }

            MembershipLemmaManager membershipLemmaManager;

            if (onlyGlobalData)
            {
                membershipLemmaManager = new MembershipLemmaManager(
                    isaGlobalProgramRepr, globalsMax, varTranslationFactory, theoryName);
            }
            else
            {
                var outEdges       = GetOutEdgesIsa(procName, cfg, out var edgeLemmas);
                var blockInfo      = BlockToInfo(theoryName, procName, cfg, edgeLemmas);
                var isaProgramRepr = new IsaProgramRepr(
                    isaGlobalProgramRepr,
                    PreconditionDeclarationName(),
                    PostconditionDeclarationName(),
                    VariableDeclarationsName("params"),
                    VariableDeclarationsName("locals"),
                    cfgName,
                    procDefName);
                membershipLemmaManager = new MembershipLemmaManager(config, isaProgramRepr, blockInfo,
                                                                    Tuple.Create(globalsMax, localsMin), varTranslationFactory, theoryName);

                var nodesToBlocks = GetNodeToBlocksIsa(cfg, blockInfo.BlockCmdsDefs);

                decls.AddRange(blockInfo.BlockCmdsDefs.Values);

                Term entry         = new IntConst(BigNum.FromInt(cfg.GetUniqueIntLabel(cfg.entry)));
                var  methodBodyCFG =
                    IsaBoogieTerm.MethodCFGBody(
                        entry, IsaCommonTerms.TermIdentFromName(outEdges.Name),
                        IsaCommonTerms.TermIdentFromName(nodesToBlocks.Name)
                        );

                var methodBodyDecl = GetMethodBodyCFGDecl(procName, methodBodyCFG);
                decls.AddRange(
                    new List <OuterDecl>
                {
                    outEdges, nodesToBlocks, methodBodyDecl
                });

                decls.AddRange(blockInfo.BlockCmdsLemmas.Values);
                decls.AddRange(blockInfo.BlockOutEdgesLemmas.Values);

                if (config.specsConfig != SpecsConfig.None)
                {
                    OuterDecl preconditions;
                    OuterDecl postconditions;

                    if (config.specsConfig == SpecsConfig.AllPreCheckedPost)
                    {
                        preconditions  = GetExprListIsa(PreconditionDeclarationName(), methodData.Preconditions.Select(pre => pre.Item1));
                        postconditions = GetExprListIsa(PostconditionDeclarationName(), methodData.Postconditions.Where(post => !post.Item2).Select(post => post.Item1));
                    }
                    else
                    {
                        preconditions  = GetExprListIsa(PreconditionDeclarationName(), methodData.Preconditions);
                        postconditions = GetExprListIsa(PostconditionDeclarationName(), methodData.Postconditions);
                    }

                    decls.Add(preconditions);
                    decls.Add(postconditions);
                }

                if (config.generateParamsAndLocals)
                {
                    decls.Add(GetVariableDeclarationsIsa("params", methodData.InParams));
                    decls.Add(GetVariableDeclarationsIsa("locals", methodData.Locals));
                }

                /* membership lemmas might still be added even if the parameter and local variable definitions are not generated
                 * at this point (since the variable context may still be different, which requires other lookup lemmas)
                 */
                if (generateMembershipLemmas)
                {
                    membershipLemmaManager.AddVariableMembershipLemmas(methodData.InParams, VarKind.ParamOrLocal);
                    membershipLemmaManager.AddVariableMembershipLemmas(methodData.Locals, VarKind.ParamOrLocal);
                }
            }

            if (config.generateAxioms)
            {
                decls.Add(GetAxioms(methodData.Axioms));
                if (generateMembershipLemmas)
                {
                    membershipLemmaManager.AddAxiomMembershipLemmas(methodData.Axioms);
                }
            }

            if (config.generateFunctions)
            {
                decls.Add(GetFunctionDeclarationsIsa(methodData.Functions));
                if (generateMembershipLemmas)
                {
                    membershipLemmaManager.AddFunctionMembershipLemmas(methodData.Functions);
                }
            }

            if (config.generateGlobalsAndConstants)
            {
                decls.Add(GetVariableDeclarationsIsa("globals", methodData.GlobalVars));
                decls.Add(GetVariableDeclarationsIsa("constants", methodData.Constants));
            }

            if (generateMembershipLemmas)
            {
                membershipLemmaManager.AddVariableMembershipLemmas(methodData.GlobalVars, VarKind.Global);
                membershipLemmaManager.AddVariableMembershipLemmas(methodData.Constants, VarKind.Constant);
                decls.AddRange(membershipLemmaManager.OuterDecls());
            }

            if (config.specsConfig != SpecsConfig.None)
            {
                DefDecl methodDef = MethodDefinition(membershipLemmaManager, methodData, config.specsConfig);
                decls.Add(methodDef);
            }

            return(membershipLemmaManager);
        }
예제 #10
0
 public override Expr VisitLiteralExpr(LiteralExpr node)
 {
     ReturnResult(IsaBoogieTerm.ExprFromLiteral(IsaBoogieTerm.Literal(node)));
     return(node);
 }