use of kodkod.engine.bool.BooleanMatrix in project org.alloytools.alloy by AlloyTools.
the class BooleanMatrixTest method testClosure.
public final void testClosure() {
BooleanMatrix mF44 = f.matrix(Dimensions.square(4, 2));
assertTrue(equivalent(mF44, mF44.closure()));
mF44.set(0, vars[0]);
mF44.set(9, vars[9]);
assertTrue(equivalent(mF44, mF44.closure()));
mF44.set(2, vars[2]);
BooleanValue[] result = new BooleanValue[mF44.dimensions().capacity()];
for (int i = 0; i < result.length; i++) {
result[i] = FALSE;
}
result[0] = vars[0];
result[1] = f.and(vars[2], vars[9]);
result[1] = f.or(result[1], f.and(vars[0], result[1]));
result[2] = vars[2];
result[9] = vars[9];
assertTrue(equivalent(mF44.closure(), result));
mF44.set(7, vars[7]);
result[7] = vars[7];
result[3] = f.and(vars[2], f.and(vars[9], vars[7]));
result[11] = f.and(vars[7], vars[9]);
assertTrue(equivalent(mF44.closure(), result));
// System.out.println(mF44.closure());
}
use of kodkod.engine.bool.BooleanMatrix 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(ExprToIntCast intExpr) {
Int ret = lookup(intExpr);
if (ret != null)
return ret;
vars = vars.createNested();
BooleanMatrix expr = intExpr.expression().accept(this);
switch(intExpr.op()) {
case CARDINALITY:
ret = expr.cardinality();
break;
case SUM:
final IntSet ints = interpreter.ints();
ret = sum(expr, ints.iterator(), 0, ints.size() - 1);
break;
default:
throw new IllegalArgumentException("unknown operator: " + intExpr.op());
}
for (Variable v : vars) ret.defCond().addVar(v);
vars = vars.parent();
return cache(intExpr, ret);
}
use of kodkod.engine.bool.BooleanMatrix in project org.alloytools.alloy by AlloyTools.
the class FOL2BoolTranslator method visit.
/**
* Calls lookup(binExpr) 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(binExpr) | some t => t, let op =
* (binExpr.op).(UNION->or + INTERSECTION->and + DIFFERENCE->difference
* + OVERRIDE->override + JOIN->dot + PRODUCT->cross) | cache(binExpr,
* op(binExpr.left.accept(this), binExpr.right.accept(this)))
*/
@Override
public BooleanMatrix visit(BinaryExpression binExpr) {
BooleanMatrix ret = lookup(binExpr);
if (ret != null)
return ret;
final BooleanMatrix left = binExpr.left().accept(this);
final BooleanMatrix right = binExpr.right().accept(this);
final ExprOperator op = binExpr.op();
switch(op) {
case UNION:
ret = left.or(right);
break;
case INTERSECTION:
ret = left.and(right);
break;
case DIFFERENCE:
ret = left.difference(right);
break;
case OVERRIDE:
ret = left.override(right);
break;
case JOIN:
ret = left.dot(right);
break;
case PRODUCT:
ret = left.cross(right);
break;
default:
throw new IllegalArgumentException("Unknown operator: " + op);
}
return cache(binExpr, ret);
}
use of kodkod.engine.bool.BooleanMatrix 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();
}
use of kodkod.engine.bool.BooleanMatrix 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();
}
Aggregations