Beispiel #1
0
        private void IsDatatype(Statement st, ref List <Solution> solution_list)
        {
            List <Expression> call_arguments = null;
            IVariable         lv             = null;
            IVariable         declaration    = null;

            Dafny.LiteralExpr lit  = null;
            Dafny.Type        type = null;

            InitArgs(st, out lv, out call_arguments);
            Contract.Assert(tcce.OfSize(call_arguments, 1), Util.Error.MkErr(st, 0, 1, call_arguments.Count));

            NameSegment argument = call_arguments[0] as NameSegment;

            Contract.Assert(argument != null, Util.Error.MkErr(st, 1, typeof(NameSegment)));

            declaration = GetLocalValueByName(argument) as IVariable;
            Contract.Assert(declaration != null, Util.Error.MkErr(st, 1, typeof(IVariable)));

            if (declaration.Type == null)
            {
                type = globalContext.GetVariableType(declaration.Name);
            }
            else
            {
                type = declaration.Type;
            }
            // type of the argument is unknown thus it's not a datatype
            if (type != null && type.IsDatatype)
            {
                lit = new Dafny.LiteralExpr(st.Tok, true);
            }
            else
            {
                // check if the argument is a nested data type
                Dafny.UserDefinedType udt = type as Dafny.UserDefinedType;
                if (udt != null)
                {
                    if (globalContext.datatypes.ContainsKey(udt.Name))
                    {
                        lit = new Dafny.LiteralExpr(st.Tok, true);
                    }
                    else
                    {
                        lit = new Dafny.LiteralExpr(st.Tok, false);
                    }
                }
                else
                {
                    lit = new Dafny.LiteralExpr(st.Tok, false);
                }
            }

            Contract.Assert(lit != null);
            localContext.AddLocal(lv, lit);
        }
Beispiel #2
0
        public static void SetVerifyFalseAttr(MemberDecl memb)
        {
            var args = new List <Expression>();
            var f    = new Microsoft.Dafny.LiteralExpr(new Token(Interpreter.TACNY_CODE_TOK_LINE, 0)
            {
                val = "false"
            }, false);

            args.Add(f);
            Attributes newattr = new Attributes("verify", args, memb.Attributes);

            memb.Attributes = newattr;
        }
Beispiel #3
0
        private void IsDatatype(Statement st, ref List<Solution> solution_list)
        {
            List<Expression> call_arguments = null;
            IVariable lv = null;
            IVariable declaration = null;
            Dafny.LiteralExpr lit =null;
            Dafny.Type type = null;

            InitArgs(st, out lv, out call_arguments);
            Contract.Assert(tcce.OfSize(call_arguments, 1), Util.Error.MkErr(st, 0, 1, call_arguments.Count));
            
            NameSegment argument = call_arguments[0] as NameSegment;
            Contract.Assert(argument != null, Util.Error.MkErr(st, 1, typeof(NameSegment)));

            declaration = GetLocalValueByName(argument) as IVariable;
            Contract.Assert(declaration != null, Util.Error.MkErr(st, 1, typeof(IVariable)));

            if (declaration.Type == null)
                type = globalContext.GetVariableType(declaration.Name);
            else
                type = declaration.Type;
            // type of the argument is unknown thus it's not a datatype
            if (type != null && type.IsDatatype)
                lit = new Dafny.LiteralExpr(st.Tok, true);
            else
            {
                // check if the argument is a nested data type
                Dafny.UserDefinedType udt = type as Dafny.UserDefinedType;
                if (udt != null)
                {
                    if(globalContext.datatypes.ContainsKey(udt.Name))
                        lit = new Dafny.LiteralExpr(st.Tok, true);
                    else
                        lit = new Dafny.LiteralExpr(st.Tok, false);
                }
                else
                    lit = new Dafny.LiteralExpr(st.Tok, false);
            }

            Contract.Assert(lit != null);
            localContext.AddLocal(lv, lit);
        }
 public override void Leave(LiteralExpr e)
 {
 }
 public override void Visit(LiteralExpr e)
 {
 }
Beispiel #6
0
        public static bool EvaluateEqualityExpression(ExpressionTree expt, ProofState state)
        {
            Contract.Requires(expt != null);
            // if the node is leaf, cast it to bool and return
            if (expt.IsLeaf())
            {
                var lit = EvaluateLeaf(expt, state) as LiteralExpr;
                return(lit?.Value is bool && (bool)lit.Value);
            }
            // left branch only
            if (expt.LChild != null && expt.RChild == null)
            {
                return(EvaluateEqualityExpression(expt.LChild, state));
            }
            // if there is no more nesting resolve the expression
            if (expt.LChild.IsLeaf() && expt.RChild.IsLeaf())
            {
                LiteralExpr lhs = null;
                LiteralExpr rhs = null;
                lhs = EvaluateLeaf(expt.LChild, state).FirstOrDefault() as LiteralExpr;
                rhs = EvaluateLeaf(expt.RChild, state).FirstOrDefault() as LiteralExpr;
                Contract.Assert(lhs != null && rhs != null);
                var bexp = expt.Data as BinaryExpr;
                int res  = -1;
                if (lhs?.Value is BigInteger)
                {
                    var l = (BigInteger)lhs.Value;
                    var r = (BigInteger)rhs?.Value;
                    res = l.CompareTo(r);
                }
                else if (lhs?.Value is string)
                {
                    var l = (string)lhs.Value;
                    var r = rhs?.Value as string;
                    res = string.Compare(l, r, StringComparison.Ordinal);
                }
                else if (lhs?.Value is bool)
                {
                    res = ((bool)lhs.Value).CompareTo(rhs?.Value != null && (bool)rhs?.Value);
                }

                switch (bexp.Op)
                {
                case BinaryExpr.Opcode.Eq:
                    return(res == 0);

                case BinaryExpr.Opcode.Neq:
                    return(res != 0);

                case BinaryExpr.Opcode.Ge:
                    return(res >= 0);

                case BinaryExpr.Opcode.Gt:
                    return(res > 0);

                case BinaryExpr.Opcode.Le:
                    return(res <= 0);

                case BinaryExpr.Opcode.Lt:
                    return(res < 0);
                }
            }
            else
            {
                // evaluate a nested expression

                var bexp = expt.Data as BinaryExpr;
                switch (bexp.Op)
                {
                case BinaryExpr.Opcode.And:
                    return(EvaluateEqualityExpression(expt.LChild, state) &&
                           EvaluateEqualityExpression(expt.RChild, state));

                case BinaryExpr.Opcode.Or:
                    return(EvaluateEqualityExpression(expt.LChild, state) ||
                           EvaluateEqualityExpression(expt.RChild, state));
                }
            }
            return(false);
        }