Пример #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;
      }

    }
Пример #2
0
 public ExpressionTree(Expression data, ExpressionTree parent, ExpressionTree lChild, ExpressionTree rChild, ExpressionTree root) {
   Data = data;
   Parent = parent;
   LChild = lChild;
   RChild = rChild;
   Root = root;
 }
Пример #3
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);
     }
 }
Пример #4
0
 public ExpressionTree(Expression data, ExpressionTree parent, ExpressionTree lChild, ExpressionTree rChild, ExpressionTree root)
 {
     Data   = data;
     Parent = parent;
     LChild = lChild;
     RChild = rChild;
     Root   = root;
 }
Пример #5
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));
                    }
                }
            }
        }
Пример #6
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;
    }
Пример #7
0
 public void SetRoot()
 {
     Root = FindRoot();
     if (IsLeaf())
     {
         return;
     }
     LChild.SetRoot();
     RChild?.SetRoot();
 }
Пример #8
0
        private void ReplaceTerm(Expression old_singleton, Expression new_term, ExpressionTree formula, ref List <Expression> nexp)
        {
            Contract.Requires(nexp != null);
            Contract.Requires(old_singleton != null);
            Contract.Requires(new_term != null);
            NameSegment curNs = null;
            NameSegment oldNs = null;

            if (formula == null)
            {
                return;
            }

            if (formula.isLeaf())
            {
                if (formula.data.GetType() == old_singleton.GetType() && formula.was_replaced == false)
                {
                    if (formula.data is NameSegment)
                    {
                        curNs = (NameSegment)formula.data;
                        oldNs = (NameSegment)old_singleton;
                    }
                    else if (formula.data is UnaryOpExpr)
                    {
                        curNs = (NameSegment)((UnaryOpExpr)formula.data).E;
                        oldNs = (NameSegment)((UnaryOpExpr)old_singleton).E;
                    }
                    else
                    {
                        Contract.Assert(false, Util.Error.MkErr(formula.data, -1));
                    }

                    if (curNs.Name == oldNs.Name)
                    {
                        ExpressionTree nt = formula.Copy();
                        nt.data = new_term;

                        if (nt.parent.lChild == nt)
                        {
                            nt.parent.lChild = nt;
                        }
                        else
                        {
                            nt.parent.rChild = nt;
                        }

                        nexp.Add(nt.root.TreeToExpression());
                    }
                }
                return;
            }
            ReplaceTerm(old_singleton, new_term, formula.lChild, ref nexp);
            ReplaceTerm(old_singleton, new_term, formula.rChild, ref nexp);
        }
Пример #9
0
        private void ReplaceTerm(Expression old_singleton, Expression new_term, ExpressionTree formula, ref List<Expression> nexp)
        {
            Contract.Requires(nexp != null);
            Contract.Requires(old_singleton != null);
            Contract.Requires(new_term != null);
            NameSegment curNs = null;
            NameSegment oldNs = null;

            if (formula == null)
                return;

            if (formula.isLeaf())
            {
                if (formula.data.GetType() == old_singleton.GetType() && formula.was_replaced == false)
                {
                    if (formula.data is NameSegment)
                    {
                        curNs = (NameSegment)formula.data;
                        oldNs = (NameSegment)old_singleton;

                    }
                    else if (formula.data is UnaryOpExpr)
                    {
                        curNs = (NameSegment)((UnaryOpExpr)formula.data).E;
                        oldNs = (NameSegment)((UnaryOpExpr)old_singleton).E;
                    }
                    else
                        Contract.Assert(false, Util.Error.MkErr(formula.data, -1));

                    if (curNs.Name == oldNs.Name)
                    {
                        ExpressionTree nt = formula.Copy();
                        nt.data = new_term;

                        if (nt.parent.lChild == nt)
                            nt.parent.lChild = nt;
                        else
                            nt.parent.rChild = nt;

                        nexp.Add(nt.root.TreeToExpression());
                    }
                }
                return;
            }
            ReplaceTerm(old_singleton, new_term, formula.lChild, ref nexp);
            ReplaceTerm(old_singleton, new_term, formula.rChild, ref nexp);
        }
Пример #10
0
    /// <summary>
    /// Extract the loop guard from the statement
    /// </summary>
    /// <param name="st"></param>
    /// <returns></returns>
    protected Expression ExtractGuard(Statement st) {
      Contract.Requires(st != null);

      IfStmt ifStmt;
      WhileStmt whileStmt;
      Expression guardWrapper;
      // extract the guard statement
      if ((ifStmt = st as IfStmt) != null)
        guardWrapper = ifStmt.Guard;
      else if ((whileStmt = st as WhileStmt) != null)
        guardWrapper = whileStmt.Guard;
      else
        return null;
      Guard = ExpressionTree.ExpressionToTree(guardWrapper);

      return guardWrapper;
    }
Пример #11
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));
        }
Пример #12
0
 private IEnumerable<Expression> SubstVar(ExpressionTree formula, MapDisplayExpr args) {
   var varMap = new Dictionary<Expression, List<Expression>>();
   foreach (var pair in args.Elements) {
     Contract.Assert(pair.A is NameSegment, Error.MkErr(formula.TreeToExpression(), 1, "Variable"));
     foreach (var p1 in ResolveExpression(pair.A)) {
       Expression key = null;
       if (p1 is Expression)
         key = p1 as Expression;
       else if (p1 is IVariable)
         key = VariableToExpression(p1 as IVariable);
       else
         Contract.Assert(false, "Sum Tin Wong");
       Contract.Assert(key != null, Error.MkErr(formula.TreeToExpression(), 1, "Variable"));
       var tempList = ResolveExpression(pair.B).Select(p2 => p2 is IVariable ? VariableToExpression((IVariable) p2) : p2 as Expression).ToList();
       if (key != null)
         varMap.Add(key, tempList);
     }
   }
   return ReplaceVar(varMap, formula);
 }
Пример #13
0
        public static bool IsResolvable(ExpressionTree expt, ProofState state)
        {
            Contract.Requires(expt.IsRoot());
            var leafs = expt.GetLeafData();

            if (leafs == null)
            {
                throw new ArgumentNullException(nameof(leafs));
            }

            foreach (var leaf in leafs)
            {
                if (leaf is NameSegment)
                {
                    var ns = leaf as NameSegment;
                    if (state.ContainTacnyVal(ns))
                    {
                        var local = state.GetTacnyVarValue(ns);
                        if (local is ApplySuffix || local is IVariable || local is NameSegment || local is Statement)
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
                else if (!(leaf is LiteralExpr))
                {
                    if (leaf is ApplySuffix)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Пример #14
0
        public static ExpressionTree FindAndReplaceNode(ExpressionTree tree, ExpressionTree newNode, ExpressionTree oldNode)
        {
            var node = oldNode;// tree.FindNode(oldNode);

            node.SetRoot();
            if (node.IsRoot())
            {
                return(newNode.CopyNode());
            }
            if (node.IsLeftChild())
            {
                node.Parent.LChild = newNode;
            }
            else if (node.IsRightChild())
            {
                node.Parent.RChild = newNode;
            }
            else
            {
                return(tree);
            }

            return(node.Root.Copy());
        }
Пример #15
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);
        }
Пример #16
0
        public ExpressionTree ExptToTree(Expression exp)
        {
            Contract.Requires(exp != null);
            Contract.Ensures(Contract.Result <ExpressionTree>() != null);
            ExpressionTree node;
            var            binaryExpr = exp as TacnyBinaryExpr;

            if (binaryExpr != null)
            {
                var e = binaryExpr;
                node = new ExpressionTree(e)
                {
                    LChild = ExpressionToTree(e.E0),
                    RChild = ExpressionToTree(e.E1)
                };
            }
            else if (exp is BinaryExpr)
            {
                var e = (BinaryExpr)exp;
                node = new ExpressionTree(e)
                {
                    LChild = ExpressionToTree(e.E0),
                    RChild = ExpressionToTree(e.E1)
                };
            }
            else if (exp is QuantifierExpr)
            {
                var e = (QuantifierExpr)exp;
                node = new ExpressionTree(e)
                {
                    LChild = ExpressionToTree(e.Term)
                };
            }
            else if (exp is ParensExpression)
            {
                var e = (ParensExpression)exp;
                node = new ExpressionTree(e)
                {
                    LChild = ExpressionToTree(e.E)
                };
            }
            else if (exp is ChainingExpression)
            {
                var e = (ChainingExpression)exp;
                node = new ExpressionTree(e)
                {
                    LChild = ExpressionToTree(e.E)
                };
            }
            else if (exp is NegationExpression)
            {
                var e = (NegationExpression)exp;
                node = new ExpressionTree(e)
                {
                    LChild = ExpressionToTree(e.E)
                };
            }
            else if (exp is SeqSelectExpr)
            {
                var e = (SeqSelectExpr)exp;
                node = new ExpressionTree(e)
                {
                    LChild = ExpressionToTree(e.E0)
                };
                if (e.E1 != null)
                {
                    node.RChild = ExpressionToTree(e.E1);
                }
            }
            else
            {
                node = new ExpressionTree(exp);
            }

            if (node.LChild != null)
            {
                node.LChild.Parent = node;
            }
            if (node.RChild != null)
            {
                node.RChild.Parent = node;
            }
            return(node);
        }
Пример #17
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;
    }
Пример #18
0
 public ExpressionTree(Expression data, ExpressionTree parent, ExpressionTree lChild, ExpressionTree rChild)
     : this(data, parent, lChild, rChild, null) { }
Пример #19
0
 public static ExpressionTree ExpressionToTree(Expression exp) {
   var expt = new ExpressionTree().ExptToTree(exp);
   expt.SetRoot();
   return expt;
 }
Пример #20
0
 public void SetRoot() {
   Root = FindRoot();
   if (IsLeaf()) return;
   LChild.SetRoot();
   RChild?.SetRoot();
 }
Пример #21
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);
          }
        }
      }
    }
Пример #22
0
/*
    private bool HasExpression(Expression constant, List<Expression> constants) {

      if (!(constant is LiteralExpr || constant is ExprDotName || constant is NameSegment))
        return false;

      return constants.Exists(j => SingletonEquality(j, constant));
    }
*/



    private ExpressionTree RewriteExpression(ExpressionTree expt, Expression key, Expression value) {
      if (key is LiteralExpr || key is ExprDotName)
        return new ExpressionTree(value);
      var segment = key as NameSegment;
      if (segment != null) {
        var expr = expt.TreeToExpression();
        var ac = Copy();
        ac.AddLocal(new LocalVariable(segment.tok, segment.tok, segment.Name, new ObjectType(), true), value);
        foreach (var item in ac.ResolveExpression(expr))
        {
          if (item is Expression) {
            return new ExpressionTree(item as Expression);// ExpressionTree.ExpressionToTree(item as Expression);
          }
          return null;
        }
      }
      // could not rewrite the value return default
      return null;
    }
Пример #23
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;
      }
    }
Пример #24
0
    public static ExpressionTree FindAndReplaceNode(ExpressionTree tree, ExpressionTree newNode, ExpressionTree oldNode) {
      var node = oldNode;// tree.FindNode(oldNode);
      node.SetRoot();
      if (node.IsRoot()) {
        return newNode.CopyNode();
      }
      if (node.IsLeftChild()) {
        node.Parent.LChild = newNode;
      } else if (node.IsRightChild()) {
        node.Parent.RChild = newNode;
      } else {
        return tree;
      }

      return node.Root.Copy();
    }
Пример #25
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);
    }
Пример #26
0
 public ExpressionTree(Expression data, ExpressionTree parent)
     : this(data, parent, null, null) { }
Пример #27
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;
 }
Пример #28
0
    public ExpressionTree ExptToTree(Expression exp) {
      Contract.Requires(exp != null);
      Contract.Ensures(Contract.Result<ExpressionTree>() != null);
      ExpressionTree node;
      var binaryExpr = exp as TacnyBinaryExpr;
      if (binaryExpr != null) {
        var e = binaryExpr;
        node = new ExpressionTree(e) {
          LChild = ExpressionToTree(e.E0),
          RChild = ExpressionToTree(e.E1)
        };
      } else if (exp is BinaryExpr) {
        var e = (BinaryExpr)exp;
        node = new ExpressionTree(e) {
          LChild = ExpressionToTree(e.E0),
          RChild = ExpressionToTree(e.E1)
        };

      } else if (exp is QuantifierExpr) {
        var e = (QuantifierExpr)exp;
        node = new ExpressionTree(e) { LChild = ExpressionToTree(e.Term) };
      } else if (exp is ParensExpression) {
        var e = (ParensExpression)exp;
        node = new ExpressionTree(e) { LChild = ExpressionToTree(e.E) };
      } else if (exp is ChainingExpression) {
        var e = (ChainingExpression)exp;
        node = new ExpressionTree(e) { LChild = ExpressionToTree(e.E) };
      } else if (exp is NegationExpression) {
        var e = (NegationExpression)exp;
        node = new ExpressionTree(e) { LChild = ExpressionToTree(e.E) };
      } else if (exp is SeqSelectExpr) {
        var e = (SeqSelectExpr)exp;
        node = new ExpressionTree(e) { LChild = ExpressionToTree(e.E0) };
        if (e.E1 != null) {
          node.RChild = ExpressionToTree(e.E1);
        }
      } else {
        node = new ExpressionTree(exp);
      }

      if (node.LChild != null)
        node.LChild.Parent = node;
      if (node.RChild != null)
        node.RChild.Parent = node;
      return node;
    }
Пример #29
0
    public static bool IsResolvable(ExpressionTree expt, ProofState state) {
      Contract.Requires(expt.IsRoot());
      var leafs = expt.GetLeafData();
      if (leafs == null) throw new ArgumentNullException(nameof(leafs));

      foreach (var leaf in leafs) {
        if (leaf is NameSegment) {
          var ns = leaf as NameSegment;
          if (state.ContainTacnyVal(ns)) {
            var local = state.GetTacnyVarValue(ns);
            if (local is ApplySuffix || local is IVariable || local is NameSegment || local is Statement)
              return false;
          } else {
            return false;
          }
        } else if (!(leaf is LiteralExpr)) {
          if (leaf is ApplySuffix) {
            return false;
          }
        }
      }
      return true;
    }
Пример #30
0
 public ExpressionTree(Expression data, ExpressionTree parent, ExpressionTree lChild, ExpressionTree rChild)
     : this(data, parent, lChild, rChild, null)
 {
 }
Пример #31
0
    private IEnumerable<Expression> SubstOp(ExpressionTree formula, MapDisplayExpr args) {
      var opcodeMap = new Dictionary<BinaryExpr.Opcode, BinaryExpr.Opcode>();

      foreach (var pair in args.Elements) {
        var op1String = pair.A as LiteralExpr;
        Contract.Assert(op1String != null);
        var op2String = pair.B as LiteralExpr;
        Contract.Assert(op2String != null);
        opcodeMap.Add(StringToOp(op1String?.Value.ToString()), StringToOp(op2String?.Value.ToString()));
      }
      return ReplaceOp(opcodeMap, formula);
    }
Пример #32
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;
    }
Пример #33
0
 public ExpressionTree(Expression data, ExpressionTree parent)
     : this(data, parent, null, null)
 {
 }
Пример #34
0
    public ExpressionTree FindNode(ExpressionTree node) {
      Contract.Requires(node != null);
      if (Data.Equals(node.Data))
        return this;
      if (LChild != null) {
        var tmp = LChild.FindNode(node);
        if (tmp != null)
          return tmp;
      }
      if (RChild != null) {
        var tmp = RChild.FindNode(node);
        if (tmp != null)
          return tmp;
      }

      return null;
    }
Пример #35
0
    protected static bool IsResolvable(ExpressionTree guard, ProofState state) {
      Contract.Requires<ArgumentNullException>(guard != null, "guard");

      return ExpressionTree.IsResolvable(guard, state);
    }