Search in sources :

Example 56 with Formula

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

the class FormulaFlattener method apply.

/**
 * Returns the result of applying this visitor to the given annotated formula.
 *
 * @return the result of applying this visitor to the given annotated formula.
 */
final AnnotatedNode<Formula> apply(AnnotatedNode<Formula> annotated) {
    annotated.node().accept(this);
    final List<Formula> roots = new ArrayList<Formula>(conjuncts.size());
    roots.addAll(conjuncts.keySet());
    for (Iterator<Map.Entry<Formula, Node>> itr = conjuncts.entrySet().iterator(); itr.hasNext(); ) {
        final Map.Entry<Formula, Node> entry = itr.next();
        final Node source = annotated.sourceOf(entry.getValue());
        if (entry.getKey() == source) {
            itr.remove();
        } else {
            entry.setValue(source);
        }
    }
    return AnnotatedNode.annotate(Formula.and(roots), conjuncts);
}
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) IntComparisonFormula(kodkod.ast.IntComparisonFormula) AnnotatedNode(kodkod.util.nodes.AnnotatedNode) Node(kodkod.ast.Node) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Example 57 with Formula

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

the class FormulaFlattener method flatten.

/**
 * Flattens the given formula into a set of conjuncts by pushing negations
 * through quantifier-free formulas, if breakupQuantifiers is false. Otherwise,
 * pushes the negations through all formulas, breaking up universal quantifiers
 * whenever possible. The source map of the returned annotated node reflects the
 * source relationships from the descendants of the returned formula to the
 * sources of the corresponding descendants of annotated.node.
 *
 * @return a map that binds each flattened conjuncts to the corresponding
 *         subformula of annotated.node
 */
public static AnnotatedNode<Formula> flatten(AnnotatedNode<Formula> annotated, boolean breakupQuantifiers) {
    final FormulaFlattener flat = new FormulaFlattener(annotated.sharedNodes(), breakupQuantifiers);
    annotated.node().accept(flat);
    final List<Formula> roots = new ArrayList<Formula>(flat.conjuncts.size());
    roots.addAll(flat.conjuncts.keySet());
    for (Iterator<Map.Entry<Formula, Node>> itr = flat.conjuncts.entrySet().iterator(); itr.hasNext(); ) {
        final Map.Entry<Formula, Node> entry = itr.next();
        final Node source = annotated.sourceOf(entry.getValue());
        if (entry.getKey() == source) {
            itr.remove();
        } else {
            entry.setValue(source);
        }
    }
    return AnnotatedNode.annotate(Formula.and(roots), flat.conjuncts);
}
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) IntComparisonFormula(kodkod.ast.IntComparisonFormula) AnnotatedNode(kodkod.util.nodes.AnnotatedNode) Node(kodkod.ast.Node) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Example 58 with Formula

use of kodkod.ast.Formula 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 59 with Formula

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

the class PrenexNFConverter method toPNF.

public static AnnotatedNode<Formula> toPNF(AnnotatedNode<Formula> annotated) {
    final PrenexNFConverter pnfConv = new PrenexNFConverter(annotated.sharedNodes());
    List<Formula> conj = new ArrayList<Formula>();
    for (Formula f : Nodes.allConjuncts(annotated.node(), null)) conj.add(f.accept(pnfConv));
    Formula ans = Formula.and(conj);
    final List<Formula> roots = new ArrayList<Formula>(pnfConv.annotations.size());
    roots.addAll(pnfConv.annotations.keySet());
    for (Iterator<Map.Entry<Formula, Node>> itr = pnfConv.annotations.entrySet().iterator(); itr.hasNext(); ) {
        final Map.Entry<Formula, Node> entry = itr.next();
        final Node source = annotated.sourceOf(entry.getValue());
        if (entry.getKey() == source) {
            itr.remove();
        } else {
            entry.setValue(source);
        }
    }
    return AnnotatedNode.annotate(ans, pnfConv.annotations);
}
Also used : BinaryFormula(kodkod.ast.BinaryFormula) QuantifiedFormula(kodkod.ast.QuantifiedFormula) Formula(kodkod.ast.Formula) NotFormula(kodkod.ast.NotFormula) NaryFormula(kodkod.ast.NaryFormula) Node(kodkod.ast.Node) AnnotatedNode(kodkod.util.nodes.AnnotatedNode) ArrayList(java.util.ArrayList) IdentityHashMap(java.util.IdentityHashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 60 with Formula

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

the class PrenexNFConverter method visit.

@Override
public Formula visit(NotFormula not) {
    Formula sub = not.formula().accept(this);
    Pair p = new Pair(sub, null);
    Formula ans;
    if (p.hasNoQuant() && sub == not.formula())
        ans = not;
    else if (p.hasNoQuant())
        ans = sub.not();
    else
        ans = pushNegation(p.leftQF);
    return saveMapping(ans, not);
}
Also used : BinaryFormula(kodkod.ast.BinaryFormula) QuantifiedFormula(kodkod.ast.QuantifiedFormula) Formula(kodkod.ast.Formula) NotFormula(kodkod.ast.NotFormula) NaryFormula(kodkod.ast.NaryFormula)

Aggregations

Formula (kodkod.ast.Formula)346 Variable (kodkod.ast.Variable)151 Solution (kodkod.engine.Solution)101 Expression (kodkod.ast.Expression)95 Bounds (kodkod.instance.Bounds)83 QuantifiedFormula (kodkod.ast.QuantifiedFormula)72 Solver (kodkod.engine.Solver)67 BinaryFormula (kodkod.ast.BinaryFormula)50 NaryFormula (kodkod.ast.NaryFormula)49 Relation (kodkod.ast.Relation)45 NotFormula (kodkod.ast.NotFormula)43 ComparisonFormula (kodkod.ast.ComparisonFormula)40 IntExpression (kodkod.ast.IntExpression)40 IntComparisonFormula (kodkod.ast.IntComparisonFormula)39 MultiplicityFormula (kodkod.ast.MultiplicityFormula)39 Universe (kodkod.instance.Universe)37 ArrayList (java.util.ArrayList)35 TupleFactory (kodkod.instance.TupleFactory)35 ConstantFormula (kodkod.ast.ConstantFormula)29 TupleSet (kodkod.instance.TupleSet)29