Search in sources :

Example 11 with Int

use of kodkod.engine.bool.Int in project org.alloytools.alloy by AlloyTools.

the class FOL2BoolTranslator method visit.

/**
 * Calls lookup(intExpr) and returns the cached value, if any. If a translation
 * has not been cached, translates the expression, calls cache(...) on it and
 * returns it.
 *
 * @return let t = lookup(intExpr) | some t => t, cache(intExpr,
 *         translate(intExpr))
 */
@Override
public final Int visit(SumExpression intExpr) {
    final Int ret = lookup(intExpr);
    if (ret != null)
        return ret;
    final List<Int> values = new ArrayList<Int>();
    sum(intExpr.decls(), intExpr.intExpr(), 0, BooleanConstant.TRUE, values);
    for (int sums = values.size(); sums > 1; sums -= sums / 2) {
        final int max = sums - 1;
        for (int i = 0; i < max; i += 2) {
            values.set(i / 2, values.get(i).plus(values.get(i + 1)));
        }
        if (max % 2 == 0) {
            // even max => odd number of entries
            values.set(max / 2, values.get(max));
        }
    }
    if (values.isEmpty()) {
        return cache(intExpr, interpreter.factory().integer(0));
    } else {
        Int sumValue = values.get(0);
        return cache(intExpr, sumValue);
    }
}
Also used : ArrayList(java.util.ArrayList) Int(kodkod.engine.bool.Int)

Example 12 with Int

use of kodkod.engine.bool.Int in project org.alloytools.alloy by AlloyTools.

the class FOL2BoolTranslator method visit.

/**
 * Calls lookup(intExpr) and returns the cached value, if any. If a translation
 * has not been cached, translates the expression, calls cache(...) on it and
 * returns it.
 *
 * @return let t = lookup(intExpr) | some t => t, cache(intExpr,
 *         intExpr.left.accept(this) intExpr.op intExpr.right.accept(this))
 */
@Override
public final Int visit(NaryIntExpression intExpr) {
    Int ret = lookup(intExpr);
    if (ret != null)
        return ret;
    final Int first = intExpr.child(0).accept(this);
    final Int[] rest = new Int[intExpr.size() - 1];
    for (int i = 0; i < rest.length; i++) {
        rest[i] = intExpr.child(i + 1).accept(this);
    }
    switch(intExpr.op()) {
        case PLUS:
            ret = first.plus(rest);
            break;
        case MULTIPLY:
            ret = first.multiply(rest);
            break;
        case AND:
            ret = first.and(rest);
            break;
        case OR:
            ret = first.or(rest);
            break;
        default:
            throw new IllegalArgumentException("Unknown nary operator: " + intExpr.op());
    }
    return cache(intExpr, ret);
}
Also used : Int(kodkod.engine.bool.Int)

Aggregations

Int (kodkod.engine.bool.Int)12 BooleanMatrix (kodkod.engine.bool.BooleanMatrix)4 BooleanValue (kodkod.engine.bool.BooleanValue)4 BooleanFactory (kodkod.engine.bool.BooleanFactory)2 IntSet (kodkod.util.ints.IntSet)2 ArrayList (java.util.ArrayList)1 Decl (kodkod.ast.Decl)1 Variable (kodkod.ast.Variable)1 IntIterator (kodkod.util.ints.IntIterator)1