Search in sources :

Example 16 with Decl

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

the class HOLSome4AllTest method testE8.

@Test
public void testE8() {
    // UNSAT: some s: ints - (-1) |
    // s > 3 || (all ns: set Node | #ns > s)
    Formula cnd = si.gt(I3);
    QuantifiedFormula allQF = (QuantifiedFormula) ns.count().gt(si).forAll(ns.setOf(Node));
    Decl someDecls = s.oneOf(Expression.INTS.difference(M1.toExpression()));
    {
        Formula f = cnd.or(allQF).forSome(someDecls);
        assertFalse(solve(f).sat());
    }
    // same thing, but inner flipped
    // UNSAT: some s: ints - (-1) |
    // s > 3 || (all ns: set Node | #ns > s)
    {
        Formula f = cnd.or(flip(allQF)).forSome(someDecls);
        assertFalse(solve(f).sat());
    }
}
Also used : QuantifiedFormula(kodkod.ast.QuantifiedFormula) Formula(kodkod.ast.Formula) Decl(kodkod.ast.Decl) QuantifiedFormula(kodkod.ast.QuantifiedFormula) Test(org.junit.Test)

Example 17 with Decl

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

the class HOLSome4AllTest method testE9.

@Test
public void testE9() {
    // UNSAT: some s: ints - (-1) |
    // s > 3 || (some ns: set Node | #ns > s + 3)
    Formula cnd = si.gt(I3);
    QuantifiedFormula innerSomeQF = (QuantifiedFormula) ns.count().gt(si.plus(I3)).forSome(ns.setOf(Node));
    Decl someDecls = s.oneOf(Expression.INTS.difference(M1.toExpression()));
    Formula f = cnd.or(innerSomeQF).forSome(someDecls);
    assertFalse(solve(f).sat());
}
Also used : QuantifiedFormula(kodkod.ast.QuantifiedFormula) Formula(kodkod.ast.Formula) Decl(kodkod.ast.Decl) QuantifiedFormula(kodkod.ast.QuantifiedFormula) Test(org.junit.Test)

Example 18 with Decl

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

the class AbstractReplacer method visit.

/**
 * Calls lookup(decls) and returns the cached value, if any. If a replacement
 * has not been cached, visits each of the children's variable and expression.
 * If nothing changes, the argument is cached and returned, otherwise a
 * replacement Decls object is cached and returned.
 *
 * @return { d: Decls | d.size = decls.size && all i: [0..d.size) |
 *         d.declarations[i] = decls.declarations[i].accept(delegate) }
 */
@Override
public Decls visit(Decls decls) {
    Decls ret = lookup(decls);
    if (ret != null)
        return ret;
    Decls visitedDecls = null;
    boolean allSame = true;
    for (Decl decl : decls) {
        Decls newDecl = visit(decl);
        if (newDecl != decl)
            allSame = false;
        visitedDecls = (visitedDecls == null) ? newDecl : visitedDecls.and(newDecl);
    }
    ret = allSame ? decls : visitedDecls;
    return cache(decls, ret);
}
Also used : Decls(kodkod.ast.Decls) Decl(kodkod.ast.Decl)

Example 19 with Decl

use of kodkod.ast.Decl 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 20 with Decl

use of kodkod.ast.Decl 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)

Aggregations

Decl (kodkod.ast.Decl)32 Formula (kodkod.ast.Formula)12 Variable (kodkod.ast.Variable)12 QuantifiedFormula (kodkod.ast.QuantifiedFormula)9 Decls (kodkod.ast.Decls)7 Expression (kodkod.ast.Expression)6 IntExpression (kodkod.ast.IntExpression)6 Relation (kodkod.ast.Relation)6 SumExpression (kodkod.ast.SumExpression)5 Solution (kodkod.engine.Solution)5 Test (org.junit.Test)5 BinaryFormula (kodkod.ast.BinaryFormula)4 BooleanFactory (kodkod.engine.bool.BooleanFactory)4 BooleanMatrix (kodkod.engine.bool.BooleanMatrix)4 BooleanValue (kodkod.engine.bool.BooleanValue)4 Instance (kodkod.instance.Instance)4 HashSet (java.util.HashSet)3 ComparisonFormula (kodkod.ast.ComparisonFormula)3 IntComparisonFormula (kodkod.ast.IntComparisonFormula)3 MultiplicityFormula (kodkod.ast.MultiplicityFormula)3