Search in sources :

Example 1 with BinaryFormula

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

the class Nodes method roots.

/**
 * Returns the roots of the given formula. In other words, breaks up the given
 * formula into its conjunctive components, {f0, ..., fk}, such that, for all
 * 0<=i<=k, f<sub>i</sub> is not a conjunction and [[f0 && ... && fk]] <=>
 * [[formula]].
 *
 * @return subformulas, {f0, ..., fk}, of the given formula such that, for all
 *         0<=i<=k, f<sub>i</sub> is not a conjuction and [[f0 && ... && fk]]
 *         <=> [[formula]].
 */
public static Set<Formula> roots(Formula formula) {
    final List<Formula> formulas = new LinkedList<Formula>();
    formulas.add(formula);
    final ListIterator<Formula> itr = formulas.listIterator();
    while (itr.hasNext()) {
        final Formula f = itr.next();
        if (f instanceof BinaryFormula) {
            final BinaryFormula bin = (BinaryFormula) f;
            if (bin.op() == FormulaOperator.AND) {
                itr.remove();
                itr.add(bin.left());
                itr.add(bin.right());
                itr.previous();
                itr.previous();
            }
        } else if (f instanceof NaryFormula) {
            final NaryFormula nf = (NaryFormula) f;
            if (nf.op() == FormulaOperator.AND) {
                itr.remove();
                for (Formula child : nf) {
                    itr.add(child);
                }
                for (int i = nf.size(); i > 0; i--) {
                    itr.previous();
                }
            }
        }
    }
    return new LinkedHashSet<Formula>(formulas);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) BinaryFormula(kodkod.ast.BinaryFormula) Formula(kodkod.ast.Formula) NaryFormula(kodkod.ast.NaryFormula) BinaryFormula(kodkod.ast.BinaryFormula) NaryFormula(kodkod.ast.NaryFormula) LinkedList(java.util.LinkedList)

Example 2 with BinaryFormula

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

the class A4Solution method k2pos.

/**
 * Associates the Kodkod formula to a particular Alloy Expr (if the Kodkod
 * formula is not already associated with an Alloy Expr or Alloy Pos)
 */
Formula k2pos(Formula formula, Expr expr) throws Err {
    if (solved)
        throw new ErrorFatal("Cannot alter the k->pos mapping since solve() has completed.");
    if (formula == null || expr == null || k2pos.containsKey(formula))
        return formula;
    k2pos.put(formula, expr);
    if (formula instanceof BinaryFormula) {
        BinaryFormula b = (BinaryFormula) formula;
        if (b.op() == FormulaOperator.AND) {
            k2pos(b.left(), expr);
            k2pos(b.right(), expr);
        }
    }
    return formula;
}
Also used : ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) BinaryFormula(kodkod.ast.BinaryFormula)

Example 3 with BinaryFormula

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

the class A4Solution method k2pos.

/**
 * Associates the Kodkod formula to a particular Alloy Pos (if the Kodkod
 * formula is not already associated with an Alloy Expr or Alloy Pos)
 */
Formula k2pos(Formula formula, Pos pos) throws Err {
    if (solved)
        throw new ErrorFatal("Cannot alter the k->pos mapping since solve() has completed.");
    if (formula == null || pos == null || pos == Pos.UNKNOWN || k2pos.containsKey(formula))
        return formula;
    k2pos.put(formula, pos);
    if (formula instanceof BinaryFormula) {
        BinaryFormula b = (BinaryFormula) formula;
        if (b.op() == FormulaOperator.AND) {
            k2pos(b.left(), pos);
            k2pos(b.right(), pos);
        }
    }
    return formula;
}
Also used : ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) BinaryFormula(kodkod.ast.BinaryFormula)

Example 4 with BinaryFormula

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

the class FullNegationPropagator method visit.

/**
 * Visits the formula's children with appropriate settings for the negated flag
 * if bf has not been visited before.
 *
 * @see kodkod.ast.visitor.AbstractVoidVisitor#visit(kodkod.ast.BinaryFormula)
 */
@Override
public final void visit(BinaryFormula bf) {
    if (visited(bf))
        return;
    final FormulaOperator op = bf.op();
    switch(op) {
        case AND:
            if (!negated) {
                // left && right
                bf.left().accept(this);
                bf.right().accept(this);
            } else {
                // !(left && right) --> !left || !right
                FullNegationPropagator fne1 = new FullNegationPropagator(shared, annotations);
                bf.left().not().accept(fne1);
                FullNegationPropagator fne2 = new FullNegationPropagator(shared, annotations);
                bf.right().not().accept(fne2);
                addConjunct(Formula.and(fne1.conjuncts).or(Formula.and(fne2.conjuncts)), false, bf);
                hasChanged = true;
            }
            break;
        case OR:
            if (!negated) {
                // left || right
                FullNegationPropagator fne1 = new FullNegationPropagator(shared, annotations);
                bf.left().accept(fne1);
                FullNegationPropagator fne2 = new FullNegationPropagator(shared, annotations);
                bf.right().accept(fne2);
                if (!fne1.hasChanged && !fne2.hasChanged) {
                    addConjunct(bf);
                } else {
                    addConjunct(Formula.and(fne1.conjuncts).or(Formula.and(fne2.conjuncts)), false, bf);
                    hasChanged = true;
                }
            } else {
                // !(left || right) --> !left && !right
                bf.left().accept(this);
                bf.right().accept(this);
                hasChanged = true;
            }
            break;
        case IMPLIES:
            if (!negated) {
                // left => right --> !left || right
                FullNegationPropagator fne1 = new FullNegationPropagator(shared, annotations);
                bf.left().not().accept(fne1);
                FullNegationPropagator fne2 = new FullNegationPropagator(shared, annotations);
                bf.right().accept(fne2);
                addConjunct(Formula.and(fne1.conjuncts).or(Formula.and(fne2.conjuncts)), false, bf);
            } else {
                // !(left => right) --> left && !right
                negated = false;
                bf.left().accept(this);
                negated = true;
                bf.right().accept(this);
            }
            hasChanged = true;
            break;
        case IFF:
            FullNegationPropagator fne1 = new FullNegationPropagator(shared, annotations);
            FullNegationPropagator fne2 = new FullNegationPropagator(shared, annotations);
            if (!negated) {
                // a <=> b --> (a && b) || (!a && !b)
                bf.left().and(bf.right()).accept(fne1);
                bf.left().not().and(bf.right().not()).accept(fne2);
            } else {
                // !(a = b) --> (a && !b) || (!a && b)
                Formula orLhs = bf.left().and(bf.right().not());
                orLhs.accept(fne1);
                Formula orRhs = bf.left().not().and(bf.right());
                orRhs.accept(fne2);
            }
            addConjunct(Formula.and(fne1.conjuncts).or(Formula.and(fne2.conjuncts)), false, bf);
            hasChanged = true;
            break;
        default:
            addConjunct(bf);
    }
}
Also used : BinaryFormula(kodkod.ast.BinaryFormula) MultiplicityFormula(kodkod.ast.MultiplicityFormula) QuantifiedFormula(kodkod.ast.QuantifiedFormula) ConstantFormula(kodkod.ast.ConstantFormula) NotFormula(kodkod.ast.NotFormula) ComparisonFormula(kodkod.ast.ComparisonFormula) NaryFormula(kodkod.ast.NaryFormula) Formula(kodkod.ast.Formula) FixFormula(kodkod.ast.FixFormula) IntComparisonFormula(kodkod.ast.IntComparisonFormula) FormulaOperator(kodkod.ast.operator.FormulaOperator)

Example 5 with BinaryFormula

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

the class PrenexNFConverter method visit.

@Override
public Formula visit(BinaryFormula bf) {
    Formula ans;
    switch(bf.op()) {
        case AND:
        case OR:
            Formula left = bf.left().accept(this);
            Formula right = bf.right().accept(this);
            Pair p = new Pair(left, right);
            if (p.hasNoQuant() && left == bf.left() && right == bf.right())
                ans = bf;
            else
                ans = p.compose(bf.op());
            break;
        case IMPLIES:
            ans = bf.left().not().or(bf.right()).accept(this);
            break;
        case IFF:
            ans = bf.left().and(bf.right()).or(bf.left().not().and(bf.right().not())).accept(this);
            break;
        default:
            throw new IllegalStateException("Unknown BinaryFormula operator: " + bf.op());
    }
    return saveMapping(ans, bf);
}
Also used : BinaryFormula(kodkod.ast.BinaryFormula) QuantifiedFormula(kodkod.ast.QuantifiedFormula) Formula(kodkod.ast.Formula) NotFormula(kodkod.ast.NotFormula) NaryFormula(kodkod.ast.NaryFormula)

Aggregations

BinaryFormula (kodkod.ast.BinaryFormula)9 NaryFormula (kodkod.ast.NaryFormula)7 Formula (kodkod.ast.Formula)6 NotFormula (kodkod.ast.NotFormula)5 QuantifiedFormula (kodkod.ast.QuantifiedFormula)5 ComparisonFormula (kodkod.ast.ComparisonFormula)4 IntComparisonFormula (kodkod.ast.IntComparisonFormula)4 MultiplicityFormula (kodkod.ast.MultiplicityFormula)4 ConstantFormula (kodkod.ast.ConstantFormula)3 FixFormula (kodkod.ast.FixFormula)3 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)2 FormulaOperator (kodkod.ast.operator.FormulaOperator)2 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 BinaryExpression (kodkod.ast.BinaryExpression)1 BinaryIntExpression (kodkod.ast.BinaryIntExpression)1 Comprehension (kodkod.ast.Comprehension)1 ConstantExpression (kodkod.ast.ConstantExpression)1 Decl (kodkod.ast.Decl)1 Decls (kodkod.ast.Decls)1