Exemple #1
0
 public override IEnumerable <ProofState> ApplyPatch(ProofState state0)
 {
     if (_stmt.Catch != null)
     {
         var frame = new DefaultTacticFrameCtrl();
         frame.InitBasicFrameCtrl(_stmt.Catch.Body, true, null, VerifyN);
         _oriState.AddNewFrame(frame);
     }
     yield return(_oriState);
 }
Exemple #2
0
 public override IEnumerable <ProofState> EvalInit(Statement statement, ProofState state)
 {
     if (statement is InlineTacticBlockStmt)
     {
         var inline = statement as InlineTacticBlockStmt;
         //check whether the tactic name is fresh
         bool isFresh = !(state.Tactics.ContainsKey(inline.name) ||
                          state.ContainInlineTactic(inline.name) ||
                          state.Members.ContainsKey(inline.name));
         if (isFresh)
         {
             state.AddInlineTactic(inline.name, inline);
             var frameCtrl = new DefaultTacticFrameCtrl();
             frameCtrl.InitBasicFrameCtrl(inline.Body.Body, state.IsCurFramePartial(), null, VerifyN);
             state.AddNewFrame(frameCtrl);
             yield return(state);
         }
         else
         {
             state.ReportTacticError(statement.Tok, "Duplicated inline tactic name.");
         }
     }
     else if (statement is UpdateStmt)
     {
         var aps = ((ExprRhs)((UpdateStmt)statement).Rhss[0]).Expr as ApplySuffix;
         //inline tactic is not supposed to have any args
         if (aps.Args == null || aps.Args.Count == 0)
         {
             var inline    = state.GetInlineTactic(Util.GetSignature(aps));
             var frameCtrl = new DefaultTacticFrameCtrl();
             frameCtrl.InitBasicFrameCtrl(inline.Body.Body, state.IsCurFramePartial(), null, VerifyN);
             state.AddNewFrame(frameCtrl);
             yield return(state);
         }
         else
         {
             state.ReportTacticError(statement.Tok, "No argument is allowed in inline tactics.");
         }
     }
 }
Exemple #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);
        }
Exemple #4
0
        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);
        }
Exemple #5
0
        public override IEnumerable <ProofState> EvalInit(Statement statement, ProofState state)
        {
            var tacApsStmt = statement as UpdateStmt;

            if (tacApsStmt != null)
            {
                var aps = ((ExprRhs)tacApsStmt.Rhss[0]).Expr as ApplySuffix;

                var tactic = state.GetTactic(aps) as Tactic;

                var frameCtrl = new DefaultTacticFrameCtrl();
                if (tactic != null)
                {
                    //unfolding preconditions
                    List <Statement> body = new List <Statement>();
                    if (tactic.Req != null)
                    {
                        foreach (var expr in tactic.Req)
                        {
                            body.Add(
                                new TacticAssertStmt(
                                    new Token(TacnyDriver.TacticCodeTokLine, 0)
                            {
                                val = "tassert"
                            },
                                    new Token(TacnyDriver.TacticCodeTokLine, 0)
                            {
                                val = ";"
                            },
                                    expr.E,
                                    null, false));
                        }
                    }
                    body.AddRange(tactic.Body.Body);
                    //unfolding postcondition
                    if (tactic.Ens != null)
                    {
                        foreach (var expr in tactic.Ens)
                        {
                            body.Add(
                                new TacticAssertStmt(
                                    new Token(TacnyDriver.TacticCodeTokLine, 0)
                            {
                                val = "tassert"
                            },
                                    new Token(TacnyDriver.TacticCodeTokLine, 0)
                            {
                                val = ";"
                            },
                                    expr.E,
                                    null, false));
                        }
                    }

                    if (aps != null && aps.Args.Count != tactic.Ins.Count)
                    {
                        state.ReportTacticError(tacApsStmt.Tok,
                                                $"Wrong number of method arguments (got {aps.Args.Count}, expected {tactic.Ins.Count})");
                        yield break;
                    }

                    for (var index = 0; index < aps.Args.Count; index++)
                    {
                        var arg = aps.Args[index];

                        var segment = arg as NameSegment;
                        if (segment != null)
                        {
                            var name = segment.Name;
                            if (state.ContainTVal(name))
                            {
                                // in the case that referring to an exisiting tvar, dereference it
                                arg = state.GetTVarValue(name) as Expression;
                            }
                            else
                            {
                                state.ReportTacticError(tacApsStmt.Tok,
                                                        $"Fail to dereferenen argument({name})");
                                yield break;
                            }
                        }
                        state.AddTacnyVar(tactic.Ins[index].Name, arg);
                    }
                    frameCtrl.InitBasicFrameCtrl(body, true, tacApsStmt.Rhss[0].Attributes, VerifyN, null, tactic.Attributes);
                    if (!frameCtrl.enabled)
                    {
                        yield return(state);

                        yield break;
                    }
                    state.AddNewFrame(frameCtrl);

                    yield return(state);
                }
            }
        }
Exemple #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);
        }