Exemplo n.º 1
0
        internal bool IsTVar(Expression expr)
        {
            string key = null;

            if (expr is TacticLiteralExpr)
            {
                key = (string)((TacticLiteralExpr)expr).Value;
            }
            else if (expr is NameSegment)
            {
                key = (expr as NameSegment).Name;
            }

            return(key == null ? false : _state.ContainTVal(key));
        }
Exemplo n.º 2
0
        public override IEnumerable <ProofState> Generate(Statement statement, ProofState state)
        {
            AssignSuchThatStmt suchThat = null;

            if (statement is AssignSuchThatStmt)
            {
                suchThat = (AssignSuchThatStmt)statement;
            }
            else
            {
                state.ReportTacticError(statement.Tok, "Unexpected statement for suchthat(:|)");
                yield break;
            }

            var nameExpr = suchThat.Lhss[0];

            if (nameExpr is IdentifierExpr)
            {
                var id = nameExpr as IdentifierExpr;
                name = id.Name;
            }
            else if (nameExpr is NameSegment)
            {
                var id = nameExpr as NameSegment;
                if (!state.ContainTVal(id.Name))
                {
                    state.ReportTacticError(statement.Tok, "Fail to register variable " + id.Name);
                    yield break;
                }
                else
                {
                    name = id.Name;
                }
            }
            else
            {
                state.ReportTacticError(statement.Tok, "Fail to register variable.");
                yield break;
            }

            string errInfo;
            //  var expr = Expr.SimpExpr.SimpTacticExpr(state, suchThat.Expr);
            var expr = suchThat.Expr;

            /*
             * if (!CheckExpr(expr, out errInfo)) {
             * state.ReportTacticError(statement.Tok, "Unexpceted expression in suchthat statement: " + errInfo);
             * yield break;
             * }*/
            Expression pos, neg, pred;

            if (!RewriteExpr(expr as BinaryExpr, out pos, out neg, out pred))
            {
                state.ReportTacticError(expr.tok, "Syntax error in Suchthat expression.");
                yield break;
            }
            if (pos != null)
            {
                pos = EvalExpr.EvalTacticExpression(state, pos);
                if (pos == null)
                {
                    yield break;
                }
            }

            if (neg != null)
            {
                neg = EvalExpr.EvalTacticExpression(state, neg);
                if (neg == null)
                {
                    yield break;
                }
            }

            if (pos == null)
            {
                state.ReportTacticError(statement.Tok, "Suchthat expression is evaluated as an empty sequence.");
                yield break;
            }
            if (pos is SeqDisplayExpr)
            {
                if (neg != null && !(neg is SeqDisplayExpr))
                {
                    state.ReportTacticError(statement.Tok, "Unexpceted expression in suchthat statement.");
                    yield break;
                }
                var eles = EvalExpr.RemoveDup((pos as SeqDisplayExpr).Elements);
                if (eles.Count == 0)
                {
                    state.ReportTacticError(statement.Tok, "The expression is evaluated as an empty set.");
                    yield break;
                }

                foreach (var item in eles)
                {
                    var copy = state.Copy();
                    copy.UpdateTacticVar(name, item);

                    if (neg != null)
                    {
                        var inNeg = EvalExpr.EvalTacticExpression(state,
                                                                  new BinaryExpr(new Token(TacnyDriver.TacticCodeTokLine, 0),
                                                                                 BinaryExpr.Opcode.In, item, neg));
                        if (inNeg is LiteralExpr && (inNeg as LiteralExpr).Value is bool)
                        {
                            if ((bool)(inNeg as LiteralExpr).Value)
                            {
                                continue;
                            }
                        }
                        else
                        {
                            throw new Exception("A unhandled error orrurs when evaluating a suchtaht statement");
                        }
                    }
                    if (pred != null)
                    {
                        var value = new BinaryExpr(new Token(TacnyDriver.TacticCodeTokLine, 0),
                                                   BinaryExpr.Opcode.Eq, suchThat.Lhss[0].Copy(), item);
                        var candidate = new BinaryExpr(new Token(TacnyDriver.TacticCodeTokLine, 0), BinaryExpr.Opcode.Add, value, pred);
                        var res       = EvalExpr.EvalTacticExpression(copy, pred);
                        Console.WriteLine(Printer.ExprToString(res));
                        if (res is LiteralExpr && (res as LiteralExpr).Value is bool)
                        {
                            if (!(bool)(res as LiteralExpr).Value)
                            {
                                continue;
                            }
                        }
                        else
                        {
                            throw new Exception("A unhandled error orrurs when evaluating a suchtaht statement");
                        }
                    }

                    yield return(copy);
                }
            }
            else
            {
                state.ReportTacticError(statement.Tok, "Unexpceted expression in suchthat statement.");
                yield break;
            }
        }
Exemplo n.º 3
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);
                }
            }
        }