예제 #1
0
        public static IEnumerable <ProofState> EvalPredicateStmt(PredicateStmt predicate, ProofState state)
        {
            Contract.Requires <ArgumentNullException>(predicate != null, "predicate");

            var newPredicate = SimpExpr.SimpTacticExpr(state, predicate);
            var copy         = state.Copy();

            copy.AddStatement(newPredicate);
            copy.NeedVerify = true;
            yield return(copy);
        }
예제 #2
0
        public override IEnumerable <ProofState> EvalInit(Statement statement, ProofState state0)
        {
            Contract.Assume(statement is WhileStmt);
            var whileStmt = statement as WhileStmt;

            if (whileStmt != null)
            {
                var tryEval = EvalExpr.EvalTacticExpression(state0, whileStmt.Guard);
                if (tryEval == null)
                {
                    yield break;
                }

                if (!(tryEval is LiteralExpr && (tryEval as LiteralExpr).Value is bool))
                {
                    var state = state0.Copy();
                    var st    = SimpExpr.SimpTacticExpr(state, statement);
                    state.NeedVerify = true;
                    state.AddStatement(st);
                    yield return(state);

                    yield break;
                }
                else
                {
                    var state     = state0.Copy();
                    var whileCtrl = this.Copy();

                    whileCtrl._guard = whileStmt.Guard;
                    whileCtrl._body  = whileStmt.Body.Body;

                    if ((bool)(tryEval as LiteralExpr).Value)
                    {
                        // insert the control frame
                        var dummyBody = new List <Statement> {
                            whileStmt
                        };
                        whileCtrl.InitBasicFrameCtrl(dummyBody, true, null, VerifyN);
                        state.AddNewFrame(whileCtrl);

                        //insert the body frame
                        var bodyFrame = new DefaultTacticFrameCtrl();
                        bodyFrame.InitBasicFrameCtrl(whileCtrl._body, whileCtrl.IsPartial, null, VerifyN);
                        state.AddNewFrame(bodyFrame);
                    }

                    yield return(state);
                }
            }
        }
예제 #3
0
        public override IEnumerable <ProofState> EvalInit(Statement statement, ProofState state)
        {
            Contract.Assume(statement != null);
            Contract.Assume(statement is TacticCasesBlockStmt);

            var         stmt = statement as TacticCasesBlockStmt;
            NameSegment caseVar;

            //get guards
            Debug.Assert(stmt != null, "stmt != null");
            var guard = stmt.Guard as ParensExpression;

            if (guard == null)
            {
                caseVar = stmt.Guard as NameSegment;
            }
            else
            {
                caseVar = guard.E as NameSegment;
            }

            var srcVar = SimpExpr.SimpTacticExpr(state, caseVar) as NameSegment;

            if (srcVar == null)
            {
                state.ReportTacticError(statement.Tok, Printer.ExprToString(caseVar) + " is not a valid variable.");
                yield break;
            }

            var datatype = state.GetDafnyVarType(srcVar.Name).AsDatatype;

            if (datatype == null)
            {
                state.ReportTacticError(statement.Tok, Printer.ExprToString(caseVar) + " is not an inductive datatype variable.");
                yield break;
            }

            if (statement.Attributes != null && statement.Attributes.Name.Equals("vars"))
            {
                _nameVars = new List <string>();
                var attrs = statement.Attributes.Args;
                foreach (var attr in attrs)
                {
                    var segment = attr as NameSegment;
                    if (segment != null && !state.ContainDafnyVar(segment))
                    {
                        _nameVars.Add(segment.Name);
                    }
                }
            }
            //generate a test program to check which cases need to apply tacny
            bool[] ctorFlags;
            InitCtorFlags(datatype, out ctorFlags);

            List <Func <int, List <Statement> > > fList = new List <Func <int, List <Statement> > >();

            int i;

            for (i = 0; i < datatype.Ctors.Count; i++)
            {
                fList.Add(GenerateAssumeFalseStmtAsStmtList);
            }

            //var matchStmt = GenerateMatchStmt(state.TacticApplication.Tok.line, srcVar.Copy(), datatype, fList);
            var matchStmt = GenerateMatchStmt(TacnyDriver.TacticCodeTokLine, srcVar.Copy(), datatype, fList);

            _matchStmt = matchStmt;
            var matchCtrl = this;

            //use a dummystmt to creat a frame for match, note that this stmts is never be evaluated
            var dummystmt = new List <Statement>();

            dummystmt.Add(stmt);
            dummystmt.Add(stmt);

            _matchStmt = matchStmt;
            matchCtrl.InitBasicFrameCtrl(dummystmt, state.IsCurFramePartial(), null, VerifyN);
            state.AddNewFrame(matchCtrl);

            //push a frame for the first case
            var caseCtrl = new DefaultTacticFrameCtrl();

            caseCtrl.InitBasicFrameCtrl(stmt.Body.Body, state.IsCurFramePartial(), null, VerifyN);
            state.AddNewFrame(caseCtrl);

            foreach (var tmp in matchStmt.Cases[0].CasePatterns)
            {
                state.AddDafnyVar(tmp.Var.Name, new ProofState.VariableData {
                    Variable = tmp.Var, Type = tmp.Var.Type
                });
            }

            //with this flag set to true, dafny will check the case brnach before evaluates any tacny code
            state.NeedVerify = true;
            yield return(state);
        }
예제 #4
0
파일: TAssert.cs 프로젝트: ggrov/dafny
        public override IEnumerable <ProofState> EvalInit(Statement statement, ProofState state0)
        {
            state0.InAsserstion = true;
            Func <ProofState, IEnumerable <ProofState> > tassertFramePatch =
                ps => {
                bool dummy;
                //set partial so that the assert-assume frame can be popped
                this.IsPartial = true;
                this._pass     = true;
                ps.MarkCurFrameAsTerminated(false, out dummy);
                return(ps.ApplyPatch());// this will call the parent patch handler
            };

            var dummyBody = new List <Statement> {
                statement
            };

            InitBasicFrameCtrl(dummyBody, false, null, VerifyN, tassertFramePatch);
            state0.AddNewFrame(this);


            var assertFrame = new DefaultTacticFrameCtrl();
            Func <ProofState, IEnumerable <ProofState> > assertFramePatch =
                ps =>
            {
                bool dummy;
                //set partial so that the assert-assume frame can be popped
                assertFrame.IsPartial = true;
                ps.MarkCurFrameAsTerminated(false, out dummy);
                return(ps.ApplyPatch());// this will call the patch handler in tassert
            };

            assertFrame.InitBasicFrameCtrl(dummyBody, false, null, VerifyN, tassertFramePatch);
            assertFrame.IncCounter();
            state0.AddNewFrame(assertFrame);

            var st = SimpExpr.SimpTacticExpr(state0, (statement as TacticAssertStmt).Expr);
            // insert the simplified assert, followed by assume false so that the prover don't need to worry about the
            // upcoming vcs.

            var asserts = new List <Statement>();

            asserts.Add(
                new AssertStmt(new Token(TacnyDriver.TacticCodeTokLine, 0)
            {
                val = "assert"
            },
                               new Token(TacnyDriver.TacticCodeTokLine, 0)
            {
                val = ";"
            }, st, null, null));
            asserts.Add(
                new AssumeStmt(new Token(TacnyDriver.TacticCodeTokLine, 0)
            {
                val = "assume"
            },
                               new Token(TacnyDriver.TacticCodeTokLine, 0)
            {
                val = ";"
            },
                               new Microsoft.Dafny.LiteralExpr(new Token(TacnyDriver.TacticCodeTokLine, 0)
            {
                val = "false"
            }, false),
                               null));

            state0.AddStatements(asserts);

            state0.NeedVerify = true;

            yield return(state0);
        }
예제 #5
0
        public override IEnumerable <ProofState> EvalInit(Statement statement, ProofState state0)
        {
            bool partial = true;
            List <Tuple <Expression, List <Statement> > > guardBodyList = new List <Tuple <Expression, List <Statement> > >();

            int counter = 0;

            guardBodyList = GetGuadBodyList(statement, guardBodyList);
            Contract.Assert(guardBodyList.Count > 0);

            var tryEval = EvalExpr.EvalTacticExpression(state0, guardBodyList[0].Item1);

            if (tryEval == null)
            {
                yield break;
            }
            //check whether the top level of the first guard is tactic level or object level
            if (!(tryEval is LiteralExpr && (tryEval as LiteralExpr).Value is bool))
            {
                var state = state0.Copy();
                var st    = SimpExpr.SimpTacticExpr(state, statement);
                state.NeedVerify = true;
                state.AddStatement(st);
                yield return(state);

                yield break;
            }
            else
            {
                // tactic if
                foreach (var item in guardBodyList)
                {
                    if (item.Item1 == null && counter == 0) //else branch and no other branch is satisfied.
                    {
                        counter++;
                        var state    = state0.Copy();
                        var ifChoice = this.Copy();
                        ifChoice.InitBasicFrameCtrl(item.Item2, partial, null, VerifyN);
                        state.AddNewFrame(ifChoice);

                        yield return(state);
                    }
                    else
                    {
                        var res = EvalExpr.EvalTacticExpression(state0, item.Item1);
                        if (res == null)
                        {
                            yield break;
                        }
                        if (res is LiteralExpr &&
                            (res as LiteralExpr).Value is bool &&
                            (bool)(res as LiteralExpr).Value)
                        {
                            counter++;
                            var state    = state0.Copy();
                            var ifChoice = this.Copy();
                            ifChoice.InitBasicFrameCtrl(item.Item2, partial, null, VerifyN);
                            state.AddNewFrame(ifChoice);
                            yield return(state);
                        }
                    }
                }
                //no condition can be found, then do nothing
                if (counter == 0)
                {
                    yield return(state0.Copy());
                }
            }
        }
예제 #6
0
        public override IEnumerable <ProofState> EvalInit(Statement statement, ProofState state)
        {
            Contract.Assume(statement != null);
            Contract.Assume(statement is TacticForallStmt);

            _stmt = statement as TacticForallStmt;

            Contract.Assert(_stmt != null);

            // do basic simplification
            // maybe do a check and throw an error instead?
            // fixme: error returns null!
            //var e = (ForallExpr) SimpTacticExpr.SimpTacExpr(state0, _stmt.Spec);
            var e = (ForallExpr)SimpExpr.SimpTacticExpr(state, _stmt.Spec);

            // var e = _stmt.Spec as ForallExpr;
            // to rename expressions
            RenameVar rn = new RenameVar();
            // to rename in the body of statement
            RenameVar     rnBody   = new RenameVar();
            List <String> usedVars = state.GetAllDafnyVars().Keys.ToList();

            usedVars.AddRange(state.GetAllTVars().Keys.ToList());

            //List<String> tmp = new List<string>();
            AllVars.DeclaredVars(_stmt.Body.Body[0], ref usedVars);



            if (_stmt.Attributes != null && _stmt.Attributes.Name.Equals("vars"))
            {
                var attrs = _stmt.Attributes.Args;
                for (int i = 0; i < attrs.Count; i++)
                {
                    // todo: should really report an errors if first condition does not hold
                    var segment = attrs[i] as NameSegment;
                    if (segment != null && i < e.BoundVars.Count)
                    {
                        NameSegment ns = segment;
                        String      fv;
                        if (GenFreeVar(ns.Name, usedVars, out fv))
                        {
                            rnBody.AddRename(ns.Name, fv);
                        }
                        rn.AddRename(e.BoundVars[i].Name, fv);
                    } // else we should have an error
                    _vars = new List <BoundVar>();
                    foreach (BoundVar bv in e.BoundVars)
                    {
                        _vars.Add(rn.CloneBoundVar(bv));
                    }
                }
            }
            else
            {
                _vars = e.BoundVars;
            }

            foreach (var tmp in _vars)
            {
                state.AddDafnyVar(tmp.Name, new ProofState.VariableData {
                    Variable = tmp, Type = tmp.Type
                });
            }


            // we could even break  _ens into a set of all conjunctions?
            // what about forall x (forall y) x
            var expr = e.Term as BinaryExpr;

            if (expr != null && (expr.Op.Equals(BinaryExpr.Opcode.Imp)))
            {
                var be = expr;
                _range = rn.CloneExpr(be.E0);
                var t = new MaybeFreeExpression(rn.CloneExpr(be.E1));
                var l = new List <MaybeFreeExpression> {
                    t
                };
                _ens = l;
            }
            else
            {
                _range = new LiteralExpr(_stmt.Tok, true);
                var t = new MaybeFreeExpression(rn.CloneExpr(e.Term));
                var l = new List <MaybeFreeExpression> {
                    t
                };
                _ens = l;
            }

            // Note that we do not need to rename variables in the body (unless the variables in vars is changed)
            InitBasicFrameCtrl(new List <Statement>(), state.IsCurFramePartial(), null, VerifyN);

            state.AddNewFrame(this);

            var bodyFrame = new DefaultTacticFrameCtrl();

            var newBody = rnBody.CloneBlockStmt(_stmt.Body);

            bodyFrame.InitBasicFrameCtrl(newBody.Body, state.IsCurFramePartial(), null, VerifyN);
            bodyFrame.IsPartial = IsPartial;
            state.AddNewFrame(bodyFrame);

            yield return(state);
        }