Search in sources :

Example 6 with NaryFormula

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

the class AbstractCollector method visit.

/**
 * Calls lookup(formula) and returns the cached value, if any. If no cached
 * value exists, visits each child, caches the union of the children's return
 * values and returns it.
 *
 * @return let x = lookup(formula) | x != null => x, cache(formula,
 *         formula.child(0).accept(this) + .. +
 *         formula.child(formula.size()-1).accept(this))
 */
@Override
public Set<T> visit(NaryFormula formula) {
    Set<T> ret = lookup(formula);
    if (ret != null)
        return ret;
    ret = newSet();
    for (Formula child : formula) {
        ret.addAll(child.accept(this));
    }
    return cache(formula, ret);
}
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)

Example 7 with NaryFormula

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

the class Nodes method allConjuncts.

public static List<Formula> allConjuncts(Formula formula, List<Formula> acc) {
    List<Formula> ans = acc != null ? acc : new ArrayList<Formula>();
    if (formula instanceof BinaryFormula) {
        final BinaryFormula bin = (BinaryFormula) formula;
        if (bin.op() == FormulaOperator.AND) {
            allConjuncts(bin.left(), ans);
            allConjuncts(bin.right(), ans);
            return ans;
        }
    }
    if (formula instanceof NaryFormula) {
        final NaryFormula nf = (NaryFormula) formula;
        if (nf.op() == FormulaOperator.AND) {
            for (Formula child : nf) {
                allConjuncts(child, ans);
            }
            return ans;
        }
    }
    ans.add(formula);
    return ans;
}
Also used : BinaryFormula(kodkod.ast.BinaryFormula) Formula(kodkod.ast.Formula) NaryFormula(kodkod.ast.NaryFormula) BinaryFormula(kodkod.ast.BinaryFormula) NaryFormula(kodkod.ast.NaryFormula)

Example 8 with NaryFormula

use of kodkod.ast.NaryFormula 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.NaryFormula)
 */
@Override
public final void visit(NaryFormula nf) {
    if (visited(nf))
        return;
    final FormulaOperator op = nf.op();
    if (negated && op == AND) {
        List<Formula> formulas = new LinkedList<Formula>();
        for (Formula f : nf) {
            FullNegationPropagator fne = new FullNegationPropagator(shared, annotations);
            f.not().accept(fne);
            formulas.add(Formula.and(fne.conjuncts));
        }
        addConjunct(Formula.or(formulas), false, nf);
        hasChanged = true;
    } else if (!negated && op == OR) {
        List<Formula> formulas = new LinkedList<Formula>();
        boolean changed = false;
        for (Formula f : nf) {
            FullNegationPropagator fne = new FullNegationPropagator(shared, annotations);
            f.accept(fne);
            changed = changed || fne.hasChanged;
            formulas.add(Formula.and(fne.conjuncts));
        }
        if (changed) {
            addConjunct(Formula.or(formulas), false, nf);
            hasChanged = true;
        } else {
            addConjunct(nf);
        }
    } else {
        for (Formula f : nf) {
            f.accept(this);
        }
        hasChanged = negated;
    }
}
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) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) LinkedList(java.util.LinkedList)

Example 9 with NaryFormula

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

the class PrenexNFConverter method visit.

@Override
public Formula visit(NaryFormula formula) {
    final ArrayList<Formula> children = new ArrayList<Formula>(formula.size());
    boolean allSame = true;
    boolean noQuant = true;
    for (Formula ch : formula) {
        Formula ch2 = ch.accept(this);
        allSame = allSame && ch == ch2;
        noQuant = noQuant && !(ch2 instanceof QuantifiedFormula);
        children.add(ch2);
    }
    Formula ans;
    if (allSame && noQuant) {
        ans = formula;
    } else {
        ans = children.get(0);
        for (int i = 1; i < children.size(); i++) ans = new Pair(ans, children.get(i)).compose(formula.op());
    }
    return saveMapping(ans, formula);
}
Also used : BinaryFormula(kodkod.ast.BinaryFormula) QuantifiedFormula(kodkod.ast.QuantifiedFormula) Formula(kodkod.ast.Formula) NotFormula(kodkod.ast.NotFormula) NaryFormula(kodkod.ast.NaryFormula) ArrayList(java.util.ArrayList) QuantifiedFormula(kodkod.ast.QuantifiedFormula)

Example 10 with NaryFormula

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

the class HOLTranslator method visit.

@Override
public Proc visit(NaryFormula formula) {
    if (annotated.isFOLNode(formula))
        return new Proc.FOL(bounds, formula);
    Proc ans = null;
    for (Formula f : formula) {
        Proc p = formula.op() == FormulaOperator.OR ? toProc(f) : f.accept(this);
        ans = ans == null ? p : ans.compose(formula.op(), p);
    }
    return ans;
}
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)

Aggregations

BinaryFormula (kodkod.ast.BinaryFormula)10 NaryFormula (kodkod.ast.NaryFormula)10 Formula (kodkod.ast.Formula)9 NotFormula (kodkod.ast.NotFormula)8 QuantifiedFormula (kodkod.ast.QuantifiedFormula)8 ComparisonFormula (kodkod.ast.ComparisonFormula)7 IntComparisonFormula (kodkod.ast.IntComparisonFormula)7 MultiplicityFormula (kodkod.ast.MultiplicityFormula)7 ConstantFormula (kodkod.ast.ConstantFormula)6 FixFormula (kodkod.ast.FixFormula)6 FormulaOperator (kodkod.ast.operator.FormulaOperator)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)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