Search in sources :

Example 21 with Decl

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

the class Skolemizer method domainConstraint.

/**
 * Returns a formula that properly constrains the given skolem's domain.
 *
 * @requires !nonSkolems.isEmpty()
 * @return a formula that properly constrains the given skolem's domain.
 */
private Formula domainConstraint(Decl skolemDecl, Relation skolem) {
    final Iterator<DeclInfo> itr = nonSkolems.iterator();
    Decls rangeDecls = null;
    while (itr.hasNext()) {
        Decl d = itr.next().decl;
        Decl dd = d.variable().oneOf(d.expression());
        rangeDecls = rangeDecls != null ? rangeDecls.and(dd) : dd;
    }
    // System.out.println(skolemDecl.expression());
    Expression skolemDomain = skolem;
    for (int i = 0, max = skolemDecl.variable().arity(); i < max; i++) {
        skolemDomain = skolemDomain.join(Expression.UNIV);
    }
    return skolemDomain.in(Formula.TRUE.comprehension(rangeDecls));
}
Also used : Decls(kodkod.ast.Decls) SumExpression(kodkod.ast.SumExpression) IntExpression(kodkod.ast.IntExpression) Expression(kodkod.ast.Expression) Decl(kodkod.ast.Decl)

Example 22 with Decl

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

the class HOLTranslator method visit.

@Override
public Proc visit(QuantifiedFormula qf) {
    assertNotSkolemizable(qf);
    Formula qfFlipped = qf.body().quantify(qf.quantifier().opposite, qf.decls(), qf.domain());
    Proc body = toProc(qfFlipped);
    boolean firstOrder = true;
    for (Decl decl : qf.decls()) if (decl.multiplicity() != Multiplicity.ONE) {
        firstOrder = false;
        break;
    }
    if (firstOrder && body instanceof FOL && noNewHOLSkolems(((FOL) body).bounds.skolems(), bounds.skolems()))
        return new Proc.FOL(bounds, qf);
    else
        return new Proc.Some4All(bounds, qf, body);
}
Also used : ConstantFormula(kodkod.ast.ConstantFormula) ComparisonFormula(kodkod.ast.ComparisonFormula) NaryFormula(kodkod.ast.NaryFormula) BinaryFormula(kodkod.ast.BinaryFormula) MultiplicityFormula(kodkod.ast.MultiplicityFormula) QuantifiedFormula(kodkod.ast.QuantifiedFormula) NotFormula(kodkod.ast.NotFormula) Formula(kodkod.ast.Formula) FixFormula(kodkod.ast.FixFormula) IntComparisonFormula(kodkod.ast.IntComparisonFormula) FOL(kodkod.engine.hol.Proc.FOL) Decl(kodkod.ast.Decl) FOL(kodkod.engine.hol.Proc.FOL) Some4All(kodkod.engine.hol.Proc.Some4All)

Example 23 with Decl

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

the class FOL2BoolTranslator method comprehension.

/**
 * Translates the given comprehension as follows (where A_0...A_|A| stand for
 * boolean variables that represent the tuples of the expression A, etc.): let
 * comprehension = "{ a: A, b: B, ..., x: X | F(a, b, ..., x) }" | { a: A, b: B,
 * ..., x: X | a in A && b in B && ... && x in X && F(a, b, ..., x) }.
 *
 * @param decls the declarations comprehension
 * @param param formula the body of the comprehension
 * @param currentDecl currently processed declaration; should be 0 initially
 * @param declConstraints the constraints implied by the declarations; should be
 *            Boolean.TRUE intially
 * @param partialIndex partial index into the provided matrix; should be 0
 *            initially
 * @param matrix boolean matrix that will retain the final results; should be an
 *            empty matrix of dimensions universe.size^decls.length initially
 * @ensures the given matrix contains the translation of the comprehension "{
 *          decls | formula }"
 */
private final void comprehension(Decls decls, Formula formula, int currentDecl, BooleanValue declConstraints, int partialIndex, BooleanMatrix matrix) {
    final BooleanFactory factory = interpreter.factory();
    if (currentDecl == decls.size()) {
        // TODO: what about this and overflow???
        matrix.set(partialIndex, factory.and(declConstraints, formula.accept(this)));
        return;
    }
    final Decl decl = decls.get(currentDecl);
    final BooleanMatrix declTransl = visit(decl);
    final int position = (int) StrictMath.pow(interpreter.universe().size(), decls.size() - currentDecl - 1);
    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);
        comprehension(decls, formula, currentDecl + 1, factory.and(entry.value(), declConstraints), partialIndex + entry.index() * position, matrix);
        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 24 with Decl

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

the class FOL2BoolTranslator method all.

/**
 * Translates the given universally quantified formula as follows (where
 * A_0...A_|A| stand for boolean variables that represent the tuples of the
 * expression A, etc.): let quantFormula = "all 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 = "all 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.FALSE initially
 * @param acc the accumulator that contains the top level conjunction; should be
 *            an empty AND accumulator initially
 * @ensures the given accumulator contains the translation of the formula "all
 *          decls | formula"
 */
private void all(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.or(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.ALL);
    for (IndexedEntry<BooleanValue> entry : declTransl) {
        groundValue.set(entry.index(), BooleanConstant.TRUE);
        all(decls, formula, currentDecl + 1, factory.or(factory.not(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 25 with Decl

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

the class EvaluatorTest method testQuantifiedFormula.

public final void testQuantifiedFormula() {
    final Variable p = Variable.unary("p"), q = Variable.unary("q");
    final Decl pdecl = p.oneOf(person), qdecl = q.oneOf(person);
    final Decls pqdecls = pdecl.and(qdecl);
    // all p: Person | some p.spouse = true
    assertTrue(eval(p.join(spouse).some().forAll(pdecl)));
    // all p, q: Person | (p.spouse = q) => ! (q in p.shaken) = true
    assertTrue(eval((p.join(spouse).eq(q).implies(q.in(p.join(shaken)).not()).forAll(pqdecls))));
    // some p: Person | no p.shaken = true
    assertTrue(eval(p.join(shaken).no().forSome(pdecl)));
    // all p: Person | some q: Person | p.shaken = q = false
    assertFalse(eval((p.join(shaken).eq(q).forSome(qdecl)).forAll(pdecl)));
    // some p, q: Person | !(p = q) && (p.shaken = q.shaken) = true
    assertTrue(eval(p.eq(q).not().and(p.join(shaken).eq(q.join(shaken))).forSome(pqdecls)));
    // some p: Person | all q: Person-p | p in q.shaken = false
    assertFalse(eval((p.in(q.join(shaken)).forAll(q.oneOf(person.difference(p)))).forSome(pdecl)));
}
Also used : Variable(kodkod.ast.Variable) Decls(kodkod.ast.Decls) Decl(kodkod.ast.Decl)

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