Search in sources :

Example 6 with Int

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

the class FOL2BoolTranslator method visit.

/**
 * Calls lookup(intComp) and returns the cached value, if any. If a translation
 * has not been cached, translates the formula, calls cache(...) on it and
 * returns it. This is the only place where <code>Int</code>s are turned into
 * formulas, so that's where the overflow circuits of individual
 * <code>Int</code>s are built into the translated formula.
 *
 * @return let t = lookup(intComp) | some t => t, cache(intComp,
 *         intComp.left.accept(this) intComp.op intComp.right.accept(this))
 */
@Override
public final BooleanValue visit(IntComparisonFormula intComp) {
    BooleanValue ret = lookup(intComp);
    if (ret != null)
        return ret;
    final Int left = intComp.left().accept(this);
    final Int right = intComp.right().accept(this);
    switch(intComp.op()) {
        case EQ:
            ret = left.eq(right, env);
            break;
        case NEQ:
            ret = left.neq(right, env);
            break;
        case LT:
            ret = left.lt(right, env);
            break;
        case LTE:
            ret = left.lte(right, env);
            break;
        case GT:
            ret = left.gt(right, env);
            break;
        case GTE:
            ret = left.gte(right, env);
            break;
        default:
            throw new IllegalArgumentException("Unknown operator: " + intComp.op());
    }
    return cache(intComp, ret);
}
Also used : BooleanValue(kodkod.engine.bool.BooleanValue) Int(kodkod.engine.bool.Int)

Example 7 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.condition.accept(this).choice(intExpr.then.accept(this),
 *         intExpr.else.accept(this)))
 */
@Override
public final Int visit(IfIntExpression intExpr) {
    Int ret = lookup(intExpr);
    if (ret != null)
        return ret;
    final BooleanValue condition = intExpr.condition().accept(this);
    final Int thenExpr = intExpr.thenExpr().accept(this);
    final Int elseExpr = intExpr.elseExpr().accept(this);
    ret = thenExpr.choice(condition, elseExpr);
    return cache(intExpr, ret);
}
Also used : BooleanValue(kodkod.engine.bool.BooleanValue) Int(kodkod.engine.bool.Int)

Example 8 with Int

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

the class FOL2BoolTranslator method sum.

/**
 * Translates the given sum expression as follows (where A_0...A_|A| stand for
 * boolean variables that represent the tuples of the expression A, etc.): let
 * sum = "sum a: A, b: B, ..., x: X | IE(a, b, ..., x) " | sum a: A, b: B, ...,
 * x: X | if (a in A && b in B && ... && x in X) then IE(a, b, ..., x) else 0 }.
 *
 * @param decls intexpr declarations
 * @param formula the formula body
 * @param currentDecl currently processed declaration; should be 0 initially
 * @param declConstraints the constraints implied by the declarations; should be
 *            Boolean.TRUE intially
 * @param values integer values computed so far
 */
private final void sum(Decls decls, IntExpression expr, int currentDecl, BooleanValue declConstraints, List<Int> values) {
    final BooleanFactory factory = interpreter.factory();
    if (decls.size() == currentDecl) {
        Int intExpr = expr.accept(this);
        Int newInt = intExpr.choice(declConstraints, factory.integer(0));
        values.add(newInt);
        return;
    }
    final Decl decl = decls.get(currentDecl);
    final BooleanMatrix declTransl = visit(decl);
    final BooleanMatrix groundValue = factory.matrix(declTransl.dimensions());
    env = env.extend(decl.variable(), decl.expression(), groundValue);
    for (IndexedEntry<BooleanValue> entry : declTransl) {
        groundValue.set(entry.index(), BooleanConstant.TRUE);
        sum(decls, expr, currentDecl + 1, factory.and(entry.value(), declConstraints), values);
        groundValue.set(entry.index(), BooleanConstant.FALSE);
    }
    env = env.parent();
}
Also used : BooleanValue(kodkod.engine.bool.BooleanValue) Decl(kodkod.ast.Decl) BooleanMatrix(kodkod.engine.bool.BooleanMatrix) BooleanFactory(kodkod.engine.bool.BooleanFactory) Int(kodkod.engine.bool.Int)

Example 9 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(BinaryIntExpression intExpr) {
    Int ret = lookup(intExpr);
    if (ret != null)
        return ret;
    final Int left = intExpr.left().accept(this);
    final Int right = intExpr.right().accept(this);
    switch(intExpr.op()) {
        case PLUS:
            ret = left.plus(right);
            break;
        case MINUS:
            ret = left.minus(right);
            break;
        case MULTIPLY:
            ret = left.multiply(right);
            break;
        case DIVIDE:
            ret = left.divide(right);
            break;
        case MODULO:
            ret = left.modulo(right);
            break;
        case AND:
            ret = left.and(right);
            break;
        case OR:
            ret = left.or(right);
            break;
        case XOR:
            ret = left.xor(right);
            break;
        case SHL:
            ret = left.shl(right);
            break;
        case SHR:
            ret = left.shr(right);
            break;
        case SHA:
            ret = left.sha(right);
            break;
        default:
            throw new IllegalArgumentException("Unknown operator: " + intExpr.op());
    }
    return cache(intExpr, ret);
}
Also used : Int(kodkod.engine.bool.Int)

Example 10 with Int

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

the class Translator method evaluate.

/**
 * Evalutes the given intexpression to an {@link kodkod.engine.bool.Int} using
 * the provided instance and options.
 *
 * @return an {@link kodkod.engine.bool.Int} representing the value of the
 *         intExpr with respect to the specified instance and options.
 * @throws NullPointerException formula = null || instance = null || options =
 *             null
 * @throws UnboundLeafException the expression refers to an undeclared variable
 *             or a relation not mapped by the instance
 * @throws HigherOrderDeclException the expression contains a higher order
 *             declaration
 */
public static Int evaluate(IntExpression intExpr, Instance instance, Options options) {
    LeafInterpreter interpreter = LeafInterpreter.exact(instance, options);
    Int ret = (Int) FOL2BoolTranslator.translate(annotate(intExpr), interpreter);
    // ret.setOverflowFlag(overflow);
    return 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