Search in sources :

Example 16 with BooleanValue

use of kodkod.engine.bool.BooleanValue 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 17 with BooleanValue

use of kodkod.engine.bool.BooleanValue 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 18 with BooleanValue

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

the class FOL2BoolTranslator method some.

/**
 * Translates the given existentially quantified formula as follows (where
 * A_0...A_|A| stand for boolean variables that represent the tuples of the
 * expression A, etc.): let quantFormula = "some a: A, b: B, ..., x: X | F(a, b,
 * ..., x)" | (A_0 && B_0 && ... && X_0 && translate(F(A_0, B_0, ..., X_0))) ||
 * ... || (A_|A| && B_|B| && ... && X_|X| && translate(F(A_|A|, B_|B|, ...,
 * X_|X|)) If the noOverflow option is specified, then the translation looks
 * like: let quantFormula = "some a: A, b: B, ..., x: X | F(a, b, ..., x)" |
 * (A_0 && B_0 && ... && X_0 && !of(F(A_0, B_0, ..., X_0)) && translate(F(A_0,
 * B_0, ..., X_0))) || ... || (A_|A| && B_|B| && ... && X_|X| && !of(F(A_|A|,
 * B_|B|, ..., X_|X|)) && translate(F(A_|A|, B_|B|, ..., X_|X|)) where
 * of(F(A_|a|, B_|b|, ..., X_|x|)) is the portion of the overflow circuit
 * generated by the translation of F(A_|a|, B_|b|, ..., X_|x|) contributed by
 * arithmetic operations over only the integer variables of this quantifier
 *
 * @param decls formula 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 acc the accumulator that contains the top level conjunction; should be
 *            an empty OR accumulator initially
 * @ensures the given accumulator contains the translation of the formula "some
 *          decls | formula"
 */
private void some(Decls decls, Formula formula, int currentDecl, BooleanValue declConstraints, BooleanAccumulator acc) {
    if (acc.isShortCircuited())
        return;
    final BooleanFactory factory = interpreter.factory();
    if (decls.size() == currentDecl) {
        BooleanValue formulaCircuit = formula.accept(this);
        BooleanValue finalCircuit = factory.and(declConstraints, formulaCircuit);
        acc.add(finalCircuit);
        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, Quantifier.SOME);
    for (IndexedEntry<BooleanValue> entry : declTransl) {
        groundValue.set(entry.index(), BooleanConstant.TRUE);
        some(decls, formula, currentDecl + 1, factory.and(entry.value(), declConstraints), acc);
        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)

Example 19 with BooleanValue

use of kodkod.engine.bool.BooleanValue 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 20 with BooleanValue

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

the class Translator method translateIncrementalNonTrivial.

/**
 * @requires checkIncrementalBounds(bounds, transl)
 * @requires checkIncrementalOptions(transl.options)
 * @requires !transl.trivial()
 * @return see {@link #translateIncremental(Formula, Bounds, Options)}
 */
private static Translation.Incremental translateIncrementalNonTrivial(Formula formula, Bounds bounds, Translation.Incremental transl) {
    final Options tOptions = transl.options();
    final Bounds tBounds = transl.bounds();
    // save the set of relations bound in the pre-state
    final Set<Relation> oldRelations = new LinkedHashSet<Relation>(tBounds.relations());
    // skolemization (below) may also cause extra relations to be added.
    for (Relation r : bounds.relations()) {
        tBounds.bound(r, bounds.lowerBound(r), bounds.upperBound(r));
    }
    final AnnotatedNode<Formula> annotated = (transl.options().skolemDepth() < 0) ? annotate(formula) : skolemize(annotate(formula), tBounds, tOptions);
    // extend the interpreter with variable allocations for new relations,
    // either from given bounds
    // or those introduced by skolemization
    final LeafInterpreter interpreter = transl.interpreter();
    interpreter.extend(setDifference(tBounds.relations(), oldRelations), tBounds.lowerBounds(), tBounds.upperBounds());
    final BooleanValue circuit = FOL2BoolTranslator.translate(annotated, interpreter);
    if (circuit == BooleanConstant.FALSE) {
        // release the old solver and state, and return a fresh trivially
        // false incremental translation.
        transl.incrementer().solver().free();
        return new Translation.Incremental(tBounds, tOptions, transl.symmetries(), LeafInterpreter.empty(tBounds.universe(), tOptions), Bool2CNFTranslator.translateIncremental(BooleanConstant.FALSE, tOptions.solver()));
    } else if (circuit == BooleanConstant.TRUE) {
        // must add any newly allocated primary variables to the solver for
        // interpretation to work correctly
        final int maxVar = interpreter.factory().maxVariable();
        final int cnfVar = transl.cnf().numberOfVariables();
        if (maxVar > cnfVar) {
            transl.cnf().addVariables(maxVar - cnfVar);
        }
    } else {
        // circuit is a formula; add its CNF representation to
        // transl.incrementer.solver()
        Bool2CNFTranslator.translateIncremental((BooleanFormula) circuit, interpreter.factory().maxVariable(), transl.incrementer());
    }
    return transl;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Options(kodkod.engine.config.Options) BooleanFormula(kodkod.engine.bool.BooleanFormula) Formula(kodkod.ast.Formula) Relation(kodkod.ast.Relation) Bounds(kodkod.instance.Bounds) BooleanValue(kodkod.engine.bool.BooleanValue) BooleanFormula(kodkod.engine.bool.BooleanFormula)

Aggregations

BooleanValue (kodkod.engine.bool.BooleanValue)29 BooleanMatrix (kodkod.engine.bool.BooleanMatrix)13 BooleanFactory (kodkod.engine.bool.BooleanFactory)8 BooleanAccumulator (kodkod.engine.bool.BooleanAccumulator)7 Decl (kodkod.ast.Decl)4 Int (kodkod.engine.bool.Int)4 Formula (kodkod.ast.Formula)3 BinaryFormula (kodkod.ast.BinaryFormula)2 ComparisonFormula (kodkod.ast.ComparisonFormula)2 ConstantFormula (kodkod.ast.ConstantFormula)2 FixFormula (kodkod.ast.FixFormula)2 IntComparisonFormula (kodkod.ast.IntComparisonFormula)2 MultiplicityFormula (kodkod.ast.MultiplicityFormula)2 NaryFormula (kodkod.ast.NaryFormula)2 NotFormula (kodkod.ast.NotFormula)2 QuantifiedFormula (kodkod.ast.QuantifiedFormula)2 Relation (kodkod.ast.Relation)2 ExprCompOperator (kodkod.ast.operator.ExprCompOperator)2 FormulaOperator (kodkod.ast.operator.FormulaOperator)2 IntIterator (kodkod.util.ints.IntIterator)2