Search in sources :

Example 26 with IntExpression

use of kodkod.ast.IntExpression in project org.alloytools.alloy by AlloyTools.

the class KK method main.

public static void main(String[] args) throws Exception {
    Relation x6 = Relation.unary("R");
    int[] ints = new int[] { 0, 1, 2 };
    List<Object> atomlist = new LinkedList<Object>();
    atomlist.add("R$0");
    atomlist.add("R$1");
    atomlist.add("R$2");
    for (int i : ints) atomlist.add(i);
    Universe universe = new Universe(atomlist);
    TupleFactory factory = universe.factory();
    Bounds bounds = new Bounds(universe);
    TupleSet x6_upper = factory.noneOf(1);
    x6_upper.add(factory.tuple("R$0"));
    x6_upper.add(factory.tuple("R$1"));
    x6_upper.add(factory.tuple("R$2"));
    bounds.bound(x6, x6_upper);
    for (int i : ints) {
        bounds.boundExactly(i, factory.setOf(i));
    }
    Formula x11 = x6.some();
    IntExpression x5 = x6.count();
    Formula x9 = x11.implies(x5.gt(IntConstant.constant(0)));
    Formula x7 = x9.not();
    Solver solver = new Solver();
    solver.options().setSolver(SATFactory.DefaultSAT4J);
    solver.options().setBitwidth(2);
    // solver.options().setFlatten(false);
    solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);
    solver.options().setSymmetryBreaking(20);
    solver.options().setSkolemDepth(0);
    System.out.println("Solving...");
    System.out.println(PrettyPrinter.print(x7, 0));
    System.out.println(bounds);
    Solution sol = solver.solve(x7, bounds);
    System.out.println(sol.toString());
    Instance inst = sol.instance();
    Evaluator ev = new Evaluator(inst);
    System.out.println(ev.evaluate(x6.some()));
    System.out.println(ev.evaluate(x5));
    System.out.println(ev.evaluate(x5.gt(IntConstant.constant(0))));
    System.out.println(ev.evaluate(x6.some().implies(x5.gt(IntConstant.constant(0))).not()));
    System.out.println(ev.evaluate(x7));
}
Also used : TupleSet(kodkod.instance.TupleSet) Solver(kodkod.engine.Solver) Instance(kodkod.instance.Instance) Bounds(kodkod.instance.Bounds) IntExpression(kodkod.ast.IntExpression) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe) Evaluator(kodkod.engine.Evaluator) LinkedList(java.util.LinkedList) Formula(kodkod.ast.Formula) Relation(kodkod.ast.Relation) Solution(kodkod.engine.Solution)

Example 27 with IntExpression

use of kodkod.ast.IntExpression in project org.alloytools.alloy by AlloyTools.

the class Skolemizer method visit.

/**
 * @see kodkod.ast.visitor.AbstractReplacer#visit(kodkod.ast.SumExpression)
 */
@Override
public final IntExpression visit(SumExpression intExpr) {
    IntExpression ret = lookup(intExpr);
    if (ret != null)
        return ret;
    // skolemDepth
    final Environment<Expression, Expression> oldRepEnv = repEnv;
    // < 0
    // at
    // this
    // point
    final Decls decls = visit(intExpr.decls());
    final IntExpression expr = intExpr.intExpr().accept(this);
    ret = (decls == intExpr.decls() && expr == intExpr.intExpr()) ? intExpr : expr.sum(decls);
    repEnv = oldRepEnv;
    return cache(intExpr, ret);
}
Also used : Decls(kodkod.ast.Decls) SumExpression(kodkod.ast.SumExpression) IntExpression(kodkod.ast.IntExpression) Expression(kodkod.ast.Expression) IntExpression(kodkod.ast.IntExpression)

Example 28 with IntExpression

use of kodkod.ast.IntExpression in project org.alloytools.alloy by AlloyTools.

the class BugTests method testFelix_03062008.

public final void testFelix_03062008() {
    Relation x0 = Relation.unary("Int/min");
    Relation x1 = Relation.unary("Int/zero");
    Relation x2 = Relation.unary("Int/max");
    Relation x3 = Relation.nary("Int/next", 2);
    Relation x4 = Relation.unary("seq/Int");
    List<String> atomlist = Arrays.asList("-1", "-2", "-3", "-4", "-5", "-6", "-7", "-8", "0", "1", "2", "3", "4", "5", "6", "7");
    Universe universe = new Universe(atomlist);
    TupleFactory factory = universe.factory();
    Bounds bounds = new Bounds(universe);
    TupleSet x0_upper = factory.noneOf(1);
    x0_upper.add(factory.tuple("-8"));
    bounds.boundExactly(x0, x0_upper);
    TupleSet x1_upper = factory.noneOf(1);
    x1_upper.add(factory.tuple("0"));
    bounds.boundExactly(x1, x1_upper);
    TupleSet x2_upper = factory.noneOf(1);
    x2_upper.add(factory.tuple("7"));
    bounds.boundExactly(x2, x2_upper);
    TupleSet x3_upper = factory.noneOf(2);
    x3_upper.add(factory.tuple("-8").product(factory.tuple("-7")));
    x3_upper.add(factory.tuple("-7").product(factory.tuple("-6")));
    x3_upper.add(factory.tuple("-6").product(factory.tuple("-5")));
    x3_upper.add(factory.tuple("-5").product(factory.tuple("-4")));
    x3_upper.add(factory.tuple("-4").product(factory.tuple("-3")));
    x3_upper.add(factory.tuple("-3").product(factory.tuple("-2")));
    x3_upper.add(factory.tuple("-2").product(factory.tuple("-1")));
    x3_upper.add(factory.tuple("-1").product(factory.tuple("0")));
    x3_upper.add(factory.tuple("0").product(factory.tuple("1")));
    x3_upper.add(factory.tuple("1").product(factory.tuple("2")));
    x3_upper.add(factory.tuple("2").product(factory.tuple("3")));
    x3_upper.add(factory.tuple("3").product(factory.tuple("4")));
    x3_upper.add(factory.tuple("4").product(factory.tuple("5")));
    x3_upper.add(factory.tuple("5").product(factory.tuple("6")));
    x3_upper.add(factory.tuple("6").product(factory.tuple("7")));
    bounds.boundExactly(x3, x3_upper);
    TupleSet x4_upper = factory.noneOf(1);
    x4_upper.add(factory.tuple("0"));
    bounds.boundExactly(x4, x4_upper);
    bounds.boundExactly(-8, factory.range(factory.tuple("-8"), factory.tuple("-8")));
    bounds.boundExactly(-7, factory.range(factory.tuple("-7"), factory.tuple("-7")));
    bounds.boundExactly(-6, factory.range(factory.tuple("-6"), factory.tuple("-6")));
    bounds.boundExactly(-5, factory.range(factory.tuple("-5"), factory.tuple("-5")));
    bounds.boundExactly(-4, factory.range(factory.tuple("-4"), factory.tuple("-4")));
    bounds.boundExactly(-3, factory.range(factory.tuple("-3"), factory.tuple("-3")));
    bounds.boundExactly(-2, factory.range(factory.tuple("-2"), factory.tuple("-2")));
    bounds.boundExactly(-1, factory.range(factory.tuple("-1"), factory.tuple("-1")));
    bounds.boundExactly(0, factory.range(factory.tuple("0"), factory.tuple("0")));
    bounds.boundExactly(1, factory.range(factory.tuple("1"), factory.tuple("1")));
    bounds.boundExactly(2, factory.range(factory.tuple("2"), factory.tuple("2")));
    bounds.boundExactly(3, factory.range(factory.tuple("3"), factory.tuple("3")));
    bounds.boundExactly(4, factory.range(factory.tuple("4"), factory.tuple("4")));
    bounds.boundExactly(5, factory.range(factory.tuple("5"), factory.tuple("5")));
    bounds.boundExactly(6, factory.range(factory.tuple("6"), factory.tuple("6")));
    bounds.boundExactly(7, factory.range(factory.tuple("7"), factory.tuple("7")));
    Variable x9 = Variable.unary("x");
    Decls x8 = x9.oneOf(Expression.INTS);
    Variable x14 = Variable.unary("p_t'");
    Expression x15 = Expression.INTS.difference(x9);
    Decls x13 = x14.oneOf(x15);
    Variable x20 = Variable.unary("p_v");
    Decls x19 = x20.oneOf(Expression.INTS);
    Formula x21 = x14.in(x9);
    Expression x18 = x21.comprehension(x19);
    IntExpression x17 = x18.count();
    IntExpression x22 = IntConstant.constant(0);
    Formula x16 = x17.gt(x22);
    Formula x12 = x16.forAll(x13);
    Formula x11 = x12.not();
    Formula x7 = x11.forAll(x8);
    Formula x25 = x0.eq(x0);
    Formula x26 = x2.eq(x2);
    Formula x24 = x25.and(x26);
    Formula x27 = x3.eq(x3);
    Formula x23 = x24.and(x27);
    Formula x6 = x7.and(x23);
    Formula x29 = x1.eq(x1);
    Formula x30 = x4.eq(x4);
    Formula x28 = x29.and(x30);
    Formula x5 = x6.and(x28);
    Solver solver = new Solver();
    solver.options().setSolver(SATFactory.DefaultSAT4J);
    solver.options().setBitwidth(4);
    // solver.options().setFlatten(false);
    solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);
    solver.options().setSymmetryBreaking(20);
    // System.out.println(PrettyPrinter.print(x5, 2));
    solver.options().setSkolemDepth(0);
    Solution sol1 = solver.solve(x5, bounds);
    assertNotNull(sol1.instance());
    solver.options().setSkolemDepth(1);
    Solution sol2 = solver.solve(x5, bounds);
    assertNotNull(sol2.instance());
}
Also used : TupleSet(kodkod.instance.TupleSet) Solver(kodkod.engine.Solver) Variable(kodkod.ast.Variable) Decls(kodkod.ast.Decls) Bounds(kodkod.instance.Bounds) IntExpression(kodkod.ast.IntExpression) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe) Formula(kodkod.ast.Formula) Relation(kodkod.ast.Relation) IntExpression(kodkod.ast.IntExpression) Expression(kodkod.ast.Expression) Solution(kodkod.engine.Solution)

Example 29 with IntExpression

use of kodkod.ast.IntExpression in project org.alloytools.alloy by AlloyTools.

the class IntTest method testComparisonOps.

/**
 * Tests all comparison ops for this.solver.options and range of vals.
 *
 * @requires this.solver.options.intEncoding = binary
 * @requires vals contains int expressions that represent all integers allowed
 *           by this.solver.options, in proper sequence
 */
private final void testComparisonOps(IntExpression[] vals) {
    final Options options = solver.options();
    final IntRange range = options.integers();
    final int min = range.min(), max = range.max();
    for (int i = min; i <= max; i++) {
        IntExpression vi = vals[i - min];
        for (int j = min; j <= max; j++) {
            IntExpression vj = vals[j - min];
            testCompOp(EQ, vi, vj, i, j, i == j);
            testCompOp(LT, vi, vj, i, j, i < j);
            testCompOp(LTE, vi, vj, i, j, i <= j);
            testCompOp(GT, vi, vj, i, j, i > j);
            testCompOp(GTE, vi, vj, i, j, i >= j);
        }
    }
}
Also used : Options(kodkod.engine.config.Options) IntExpression(kodkod.ast.IntExpression) IntRange(kodkod.util.ints.IntRange)

Example 30 with IntExpression

use of kodkod.ast.IntExpression in project org.alloytools.alloy by AlloyTools.

the class IntTest method test2sComplementBinOps.

/**
 * Tests all binary ops for this.solver.options and range of vals.
 *
 * @requires this.solver.options.intEncoding = binary
 * @requires vals contains int expressions that represent all integers allowed
 *           by this.solver.options, in proper sequence
 */
private final void test2sComplementBinOps(IntExpression[] vals) {
    final Options options = solver.options();
    final IntRange range = options.integers();
    final int min = range.min(), max = range.max();
    final int mask = ~(-1 << options.bitwidth());
    final int shiftmask = ~(-1 << (32 - Integer.numberOfLeadingZeros(options.bitwidth() - 1)));
    for (int i = min; i <= max; i++) {
        IntExpression vi = vals[i - min];
        for (int j = min; j <= max; j++) {
            IntExpression vj = vals[j - min];
            testBinOp(PLUS, vi, vj, i, j, i + j, mask);
            testBinOp(MINUS, vi, vj, i, j, i - j, mask);
            testBinOp(MULTIPLY, vi, vj, i, j, i * j, mask);
            if (j != 0) {
                testBinOp(DIVIDE, vi, vj, i, j, i / j, mask);
                testBinOp(MODULO, vi, vj, i, j, i % j, mask);
            }
            testBinOp(AND, vi, vj, i, j, i & j, mask);
            testBinOp(OR, vi, vj, i, j, i | j, mask);
            testBinOp(XOR, vi, vj, i, j, i ^ j, mask);
            testBinOp(SHL, vi, vj, i, j, i << (j & shiftmask), i << j, mask);
            testBinOp(SHR, vi, vj, i, j, (i & mask) >>> (j & shiftmask), mask);
            testBinOp(SHA, vi, vj, i, j, i >> (j & shiftmask), mask);
        }
    }
}
Also used : Options(kodkod.engine.config.Options) IntExpression(kodkod.ast.IntExpression) IntRange(kodkod.util.ints.IntRange)

Aggregations

IntExpression (kodkod.ast.IntExpression)32 Formula (kodkod.ast.Formula)16 Expression (kodkod.ast.Expression)13 BinaryIntExpression (kodkod.ast.BinaryIntExpression)11 IfIntExpression (kodkod.ast.IfIntExpression)11 NaryIntExpression (kodkod.ast.NaryIntExpression)11 UnaryIntExpression (kodkod.ast.UnaryIntExpression)11 Decls (kodkod.ast.Decls)10 BinaryExpression (kodkod.ast.BinaryExpression)8 Variable (kodkod.ast.Variable)8 Solution (kodkod.engine.Solution)8 QuantifiedFormula (kodkod.ast.QuantifiedFormula)7 Relation (kodkod.ast.Relation)7 TupleFactory (kodkod.instance.TupleFactory)6 TupleSet (kodkod.instance.TupleSet)6 SumExpression (kodkod.ast.SumExpression)5 Solver (kodkod.engine.Solver)5 Bounds (kodkod.instance.Bounds)5 Universe (kodkod.instance.Universe)5 ConstantExpression (kodkod.ast.ConstantExpression)4