Beispiel #1
0
        public AI.IFunApp And(AI.IExpr p, AI.IExpr q)
        {
            Expression e1 = Converter.Unbox <Expression>(p);
            Expression e2 = Converter.Unbox <Expression>(q);

            return(Converter.Box(encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.And, e1, e2), decoder));
        }
 public override Expression Convert <Expression>(IExpressionEncoder <Variable, Expression> encoder)
 {
     return(encoder.CompoundExpressionFor(ExpressionType.Int32,
                                          ExpressionOperator.Addition,
                                          encoder.VariableFor(var),
                                          encoder.ConstantFor(value)));
 }
        /// <summary>
        /// Construct left - right, or left iff right == 0
        /// </summary>
        public static Expression SmartSubtraction <Variable, Expression>(this IExpressionEncoder <Variable, Expression> encoder,
                                                                         Expression left, Expression right, IExpressionDecoder <Variable, Expression> decoder)
        {
            Contract.Requires(decoder != null);
            Contract.Requires(encoder != null);

            int value;

            if (decoder.IsConstantInt(right, out value) && value == 0)
            {
                return(left);
            }

            return(encoder.CompoundExpressionFor(ExpressionType.Int32, ExpressionOperator.Subtraction, left, right));
        }
        /// <summary>
        /// Perform some rewritings to the boxed expressions, so that is is simplified, and it is nicer for a human reader
        /// </summary>
        public static BoxedExpression MakeItPrettier <Variable>(this BoxedExpression be, IExpressionDecoder <BoxedVariable <Variable>, BoxedExpression> decoder, IExpressionEncoder <BoxedVariable <Variable>, BoxedExpression> encoder)
        {
            Contract.Requires(encoder != null);

            var result = be;

            // 0. Do not do any work on MinValue <= exp, as -exp <= -MinValue is wrong
            BinaryOperator  bop;
            BoxedExpression left, right;
            int             maybeMinInt; long maybeMinLong;

            if (be.IsBinaryExpression(out bop, out left, out right) &&
                (bop == BinaryOperator.Cle || bop == BinaryOperator.Clt || bop == BinaryOperator.Cle_Un || bop == BinaryOperator.Clt_Un) &&
                ((left.IsConstantInt(out maybeMinInt) && maybeMinInt == Int32.MinValue) || (left.IsConstantInt64(out maybeMinLong) && maybeMinLong == Int64.MinValue))
                )
            {
                return(result);
            }

            // 1. If it is a polynomial, do some arithmetic simplification, puts all the variables on one side, etc.
            Polynomial <BoxedVariable <Variable>, BoxedExpression> pol;

            if (Polynomial <BoxedVariable <Variable>, BoxedExpression> .TryToPolynomialForm(be, decoder, out pol))
            {
                if (pol.IsTautology)
                {
                    return(encoder.ConstantFor(true));
                }

                // m1 - m2 == 0 becomes m1 == m2
                BoxedVariable <Variable> x, y;
                Rational k;
                if (pol.TryMatch_XMinusYEqualk3(out x, out y, out k) && k.IsZero)
                {
                    var xExp = encoder.VariableFor(x);
                    var yExp = encoder.VariableFor(y);
                    return(encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.Equal, xExp, yExp));
                }

                result = pol.ToPureExpression(encoder);
            }

            // To do: add some more pretty printing there

            result = MakeItPrettier(result, x => encoder.ConstantFor(x));

            return(result);
        }
Beispiel #5
0
 public void Assign(Expression x, Expression exp, INumericalAbstractDomainQuery <Variable, Expression> preState)
 {
     embedded = UnderlyingPolyhedra.Constrain(embedded, Converter.Box(encoder.CompoundExpressionFor(ExpressionType.Bool, ExpressionOperator.Equal, x, exp), decoder));
 }