/// <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);
        }
 public override Expression Convert <Expression>(IExpressionEncoder <Variable, Expression> encoder)
 {
     return(encoder.CompoundExpressionFor(ExpressionType.Int32,
                                          ExpressionOperator.Addition,
                                          encoder.VariableFor(var),
                                          encoder.ConstantFor(value)));
 }
Ejemplo n.º 3
0
        public AI.IExpr Term(AI.Rational r, AI.IVariable var)
        {
            long up   = r.Numerator;
            long down = r.Denominator;

            Expression upAsExp   = encoder.ConstantFor(up);
            Expression downAsExp = encoder.ConstantFor(down);

            Expression asFrac = encoder.CompoundExpressionFor(ExpressionType.Int32, ExpressionOperator.Division, upAsExp, downAsExp);

            // We assume AI.IVariable being a Var<Exp>
            Var <Expression> v = var as Var <Expression>;

            if (v == null)
            {
                throw new AbstractInterpretationException();
            }

            Expression newVar = Converter.Unbox <Expression>(v);

            return(Converter.Box(encoder.CompoundExpressionFor(ExpressionType.Int32, ExpressionOperator.Multiplication, asFrac, newVar), decoder));
        }
Ejemplo n.º 4
0
        public static TExpr ToExpression <TVar, TExpr>(this Rational value, IExpressionEncoder <TVar, TExpr> encoder)
        {
            if (value.IsInteger)
            {
                return(encoder.ConstantFor((long)value));
            }
            if (value.IsPlusInfinity)
            {
                return(encoder.CompoundFor(ExpressionType.Int32, ExpressionOperator.Div,
                                           encoder.ConstantFor(1L), encoder.ConstantFor(0L)));
            }
            if (value.IsMinusInfinity)
            {
                return(encoder.CompoundFor(ExpressionType.Int32, ExpressionOperator.Div,
                                           encoder.ConstantFor(-1L), encoder.ConstantFor(0L)));
            }

            TExpr l = encoder.ConstantFor(value.Up);
            TExpr r = encoder.ConstantFor(value.Down);

            return(encoder.CompoundFor(ExpressionType.Int32, ExpressionOperator.Div, l, r));
        }
 public override Expression Convert <Expression>(IExpressionEncoder <Variable, Expression> encoder)
 {
     return(encoder.ConstantFor(value));
 }