Exemple #1
0
    private IEnumerable<Expression> ReplaceConstants(ExpressionTree expression, List<Expression> constants, List<IVariable> vars) {
      if (expression == null)
        yield break;
      if (expression.Root == null)
        expression.SetRoot();

      if (expression.IsLeaf()) {
        if (HasConstant(expression.Data, constants)) {
          foreach (var var in vars) {
            if (!ValidateType(var, expression.Parent.TreeToExpression() as BinaryExpr))
              continue;
            var newVal = ExpressionTree.ExpressionToTree(VariableToExpression(var));
            var copy = expression.Root.Copy();
            var newTree = ExpressionTree.FindAndReplaceNode(copy, newVal, expression.Copy());
            yield return newTree.Root.TreeToExpression();
            foreach (var item in ReplaceConstants(newTree.Root, constants, vars))
              yield return item;
          }
        }
      } else {
        foreach (var item in ReplaceConstants(expression.LChild, constants, vars))
          yield return item;
        foreach (var item in ReplaceConstants(expression.RChild, constants, vars))
          yield return item;
      }

    }
Exemple #2
0
 public static IEnumerable <object> EvaluateLeaf(ExpressionTree expt, ProofState state)
 {
     Contract.Requires(expt != null && expt.IsLeaf());
     foreach (var item in Interpreter.EvalTacnyExpression(state, expt.Data))
     {
         yield return(item);
     }
 }
Exemple #3
0
        /// <summary>
        /// Resolve all variables in expression to either literal values
        /// or to orignal declared nameSegments
        /// </summary>
        /// <param name="guard"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public static IEnumerable <ExpressionTree> ResolveExpression(ExpressionTree guard, ProofState state)
        {
            Contract.Requires <ArgumentNullException>(guard != null, "guard");
            Contract.Requires <ArgumentNullException>(state != null, "state");

            if (guard.IsLeaf())
            {
                if (!(guard.Data is NameSegment))
                {
                    yield return(guard.Copy());
                }
                var newGuard = guard.Copy();
                var result   = EvaluateLeaf(newGuard, state);
                foreach (var item in result)
                {
                    Contract.Assert(result != null);
                    Expression newNs = null; // potential encapsulation problems
                    if (item is MemberDecl)
                    {
                        var md = item as MemberDecl;
                        newNs = new StringLiteralExpr(new Token(), md.Name, true);
                    }
                    else if (item is Formal)
                    {
                        var tmp = item as Formal;
                        newNs = new NameSegment(tmp.tok, tmp.Name, null);
                    }
                    else if (item is NameSegment)
                    {
                        newNs = item as NameSegment;
                    }
                    else
                    {
                        newNs = item as Expression; // Dafny.LiteralExpr;
                    }
                    newGuard.Data = newNs;
                    yield return(newGuard);
                }
            }
            else
            {
                foreach (var lChild in ResolveExpression(guard.LChild, state))
                {
                    if (guard.RChild != null)
                    {
                        foreach (var rChild in ResolveExpression(guard.RChild, state))
                        {
                            yield return(new ExpressionTree(guard.Data, null, lChild, rChild));
                        }
                    }
                    else
                    {
                        yield return(new ExpressionTree(guard.Data, null, lChild, null));
                    }
                }
            }
        }
Exemple #4
0
    /// <summary>
    /// Evalutate an expression tree
    /// </summary>
    /// <param name="expt"></param>
    /// <returns></returns>
    protected bool EvaluateGuardTree(ExpressionTree expt) {
      Contract.Requires(expt != null);
      // if the node is leaf, cast it to bool and return
      if (expt.IsLeaf()) {
        var lit = EvaluateLeaf(expt) as LiteralExpr;
        return lit?.Value is bool && (bool)lit.Value;
      }
      // left branch only
      if (expt.LChild != null && expt.RChild == null)
        return EvaluateGuardTree(expt.LChild);
      // 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) as LiteralExpr;
        rhs = EvaluateLeaf(expt.RChild) as LiteralExpr;
        if (lhs.GetType() == rhs.GetType())
          return false;
        var bexp = tcce.NonNull(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((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
      {
        BinaryExpr bexp = tcce.NonNull(expt.Data as BinaryExpr);
        if (bexp.Op == BinaryExpr.Opcode.And)
          return EvaluateGuardTree(expt.LChild) && EvaluateGuardTree(expt.RChild);
        if (bexp.Op == BinaryExpr.Opcode.Or)
          return EvaluateGuardTree(expt.LChild) || EvaluateGuardTree(expt.RChild);
      }
      return false;
    }
Exemple #5
0
        protected Expression EvaluateExpression(ExpressionTree expt, ProofState state)
        {
            Contract.Requires(expt != null);
            if (expt.IsLeaf())
            {
                return(EvaluateLeaf(expt, state) as LiteralExpr);
            }
            var bexp = (BinaryExpr)expt.Data;

            if (BinaryExpr.IsEqualityOp(bexp.Op))
            {
                bool boolVal = EvaluateEqualityExpression(expt, state);
                return(new LiteralExpr(new Token(), boolVal));
            }
            var lhs = EvaluateExpression(expt.LChild, state) as LiteralExpr;
            var rhs = EvaluateExpression(expt.RChild, state) as LiteralExpr;
            // for now asume lhs and rhs are integers
            var l = (BigInteger)lhs?.Value;
            var r = (BigInteger)rhs?.Value;

            BigInteger res = 0;


            switch (bexp.Op)
            {
            case BinaryExpr.Opcode.Sub:
                res = BigInteger.Subtract(l, r);
                break;

            case BinaryExpr.Opcode.Add:
                res = BigInteger.Add(l, r);
                break;

            case BinaryExpr.Opcode.Mul:
                res = BigInteger.Multiply(l, r);
                break;

            case BinaryExpr.Opcode.Div:
                res = BigInteger.Divide(l, r);
                break;
            }

            return(new LiteralExpr(lhs.tok, res));
        }
Exemple #6
0
 public static IEnumerable<object> EvaluateLeaf(ExpressionTree expt, ProofState state) {
   Contract.Requires(expt != null && expt.IsLeaf());
   foreach (var item in Interpreter.EvaluateTacnyExpression(state, expt.Data))
     yield return item;
 }
Exemple #7
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 = (LiteralExpr) EvaluateLeaf(expt.LChild, state).FirstOrDefault();
        rhs = EvaluateLeaf(expt.RChild, state).FirstOrDefault() as LiteralExpr;
        if (lhs?.GetType() == rhs?.GetType())
          return false;
        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;
    }
Exemple #8
0
    protected Expression EvaluateExpression(ExpressionTree expt, ProofState state) {
      Contract.Requires(expt != null);
      if (expt.IsLeaf()) {
        return EvaluateLeaf(expt, state) as LiteralExpr;
      }
      var bexp = (BinaryExpr) expt.Data;
      if (BinaryExpr.IsEqualityOp(bexp.Op)) {
        bool boolVal = EvaluateEqualityExpression(expt, state);
        return new LiteralExpr(new Token(), boolVal);
      }
      var lhs = EvaluateExpression(expt.LChild,state) as LiteralExpr;
      var rhs = EvaluateExpression(expt.RChild, state) as LiteralExpr;
      // for now asume lhs and rhs are integers
      var l = (BigInteger)lhs?.Value;
      var r = (BigInteger)rhs?.Value;

      BigInteger res = 0;


      switch (bexp.Op) {
        case BinaryExpr.Opcode.Sub:
          res = BigInteger.Subtract(l, r);
          break;
        case BinaryExpr.Opcode.Add:
          res = BigInteger.Add(l, r);
          break;
        case BinaryExpr.Opcode.Mul:
          res = BigInteger.Multiply(l, r);
          break;
        case BinaryExpr.Opcode.Div:
          res = BigInteger.Divide(l, r);
          break;
      }

      return new LiteralExpr(lhs.tok, res);
    }
Exemple #9
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);
        }
Exemple #10
0
    private IEnumerable<Expression> ReplaceVar(Dictionary<Expression, List<Expression>> vars, ExpressionTree expression) {
      if (expression == null)
        yield break;
      if (expression.Root == null)
        expression.SetRoot();

      if (expression.IsLeaf()) {
        foreach (var kvp in vars) {

          if (SingletonEquality(expression.Data, kvp.Key)) {
            foreach (var var in kvp.Value) {
              // safeguard against infinite loop
              if (SingletonEquality(kvp.Key, var))
                continue;
              ExpressionTree newVal = RewriteExpression(expression, kvp.Key, var);
              if (newVal == null)
                break;
              var copy = expression.Root.Copy();
              var newTree = ExpressionTree.FindAndReplaceNode(copy, newVal, expression.Copy());
              yield return newTree.Root.TreeToExpression();
              foreach (var item in ReplaceVar(vars, newTree.Root))
                yield return item;
            }
          }
        }
      } else {
        foreach (var item in ReplaceVar(vars, expression.LChild))
          yield return item;
        foreach (var item in ReplaceVar(vars, expression.RChild))
          yield return item;
      }
    }
Exemple #11
0
    protected IEnumerable<Expression> ReplaceOp(Dictionary<BinaryExpr.Opcode, BinaryExpr.Opcode> opCodeMap, ExpressionTree formula) {

      if (formula == null || formula.IsLeaf())
        yield break;

      var expressionTree = formula;
      var binaryExpr = formula.Data as BinaryExpr;
      if (binaryExpr != null) {
        var bexp = binaryExpr;
        if (opCodeMap.ContainsKey(bexp.Op)) {
          expressionTree = expressionTree.Copy();

          expressionTree.Data = new BinaryExpr(binaryExpr.tok, opCodeMap[bexp.Op], bexp.E0, bexp.E1);
          expressionTree = expressionTree.Root;
          yield return expressionTree.Root.TreeToExpression();
        }
      }
      foreach (var item in ReplaceOp(opCodeMap, expressionTree.LChild))
        yield return item;
      foreach (var item in ReplaceOp(opCodeMap, expressionTree.RChild))
        yield return item;
    }
Exemple #12
0
    /// <summary>
    /// Resolve all variables in expression to either literal values
    /// or to orignal declared nameSegments
    /// </summary>
    /// <param name="guard"></param>
    /// <param name="state"></param>
    /// <returns></returns>
    public static IEnumerable<ExpressionTree> ResolveExpression(ExpressionTree guard, ProofState state) {
      Contract.Requires<ArgumentNullException>(guard != null, "guard");
      Contract.Requires<ArgumentNullException>(state != null, "state");

      if (guard.IsLeaf()) {
        if (!(guard.Data is NameSegment)) {
          yield return guard.Copy();
        }
        var newGuard = guard.Copy();
        var result = EvaluateLeaf(newGuard, state);
        foreach (var item in result) {
          Contract.Assert(result != null);
          Expression newNs = null; // potential encapsulation problems
          if (item is MemberDecl) {
            var md = item as MemberDecl;
            newNs = new StringLiteralExpr(new Token(), md.Name, true);
          } else if (item is Formal) {
            var tmp = item as Formal;
            newNs = new NameSegment(tmp.tok, tmp.Name, null);
          } else if (item is NameSegment) {
            newNs = item as NameSegment;
          } else {
            newNs = item as Expression; // Dafny.LiteralExpr;
          }
          newGuard.Data = newNs;
          yield return newGuard;
        }
      } else {
        foreach (var lChild in ResolveExpression(guard.LChild, state)) {
          if (guard.RChild != null) {
            foreach (var rChild in ResolveExpression(guard.RChild, state)) {
              yield return new ExpressionTree(guard.Data, null, lChild, rChild);
            }
          } else {
            yield return new ExpressionTree(guard.Data, null, lChild, null);
          }
        }
      }
    }