Java Code Examples for kodkod.ast.BinaryFormula#op()

The following examples show how to use kodkod.ast.BinaryFormula#op() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PrenexNFConverter.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 2
Source File: FormulaFlattener.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
    if (op == IFF || (negated && op == AND) || (!negated && (op == OR || op == IMPLIES))) { // can't
                                                                                           // break
                                                                                           // down
                                                                                           // further
                                                                                           // in
                                                                                           // these
                                                                                           // cases
        addConjunct(bf);
    } else { // will break down further
        if (negated && op == IMPLIES) { // !(a => b) = !(!a || b) = a && !b
            negated = !negated;
            bf.left().accept(this);
            negated = !negated;
            bf.right().accept(this);
        } else {
            bf.left().accept(this);
            bf.right().accept(this);
        }
    }
}
 
Example 3
Source File: FormulaFlattener.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * 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)
 */
public final void visit(BinaryFormula bf) { 
	if (visited(bf)) return;
	final FormulaOperator op = bf.op();
	if (op==IFF || (negated && op==AND) || (!negated && (op==OR || op==IMPLIES))) { // can't break down further in these cases
		addConjunct(bf);
	} else { // will break down further
		if (negated && op==IMPLIES) { // !(a => b) = !(!a || b) = a && !b
			negated = !negated;
			bf.left().accept(this);
			negated = !negated;
			bf.right().accept(this);
		} else {
			bf.left().accept(this);
			bf.right().accept(this);
		}
	}
}
 
Example 4
Source File: PrettyPrinter.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * @ensures appends the tokenization of the given node to this.tokens
 */
@Override
public void visit(BinaryFormula node) {
    final FormulaOperator op = node.op();
    final boolean pleft = parenthesize(op, node.left());
    if (pleft)
        indent++;
    visitChild(node.left(), pleft);
    if (pleft)
        indent--;
    infix(op);
    newline();
    final boolean pright = parenthesize(op, node.right());
    if (pright)
        indent++;
    visitChild(node.right(), pright);
    if (pright)
        indent--;
}
 
Example 5
Source File: FOL2BoolTranslator.java    From kodkod with MIT License 6 votes vote down vote up
/** 
 * Calls lookup(binFormula) and returns the cached value, if any.  
 * If a translation has not been cached, translates the formula,
 * calls cache(...) on it and returns it.
 * @return let t = lookup(binFormula) | some t => t, 
 * 	cache(binFormula, binFormula.op(binFormula.left.accept(this), binFormula.right.accept(this))
 */
public final BooleanValue visit(BinaryFormula binFormula) {
	BooleanValue ret = lookup(binFormula);
	if (ret!=null) return ret;

	final BooleanValue left = binFormula.left().accept(this);
	final BooleanValue right = binFormula.right().accept(this);
	final FormulaOperator op = binFormula.op();
	final BooleanFactory f = interpreter.factory();

	switch(op) {
	case AND		: ret = f.and(left, right); break;
	case OR			: ret = f.or(left, right); break;
	case IMPLIES	: ret = f.implies(left, right); break;
	case IFF		: ret = f.iff(left, right); break;
	default : 
		throw new IllegalArgumentException("Unknown operator: " + op);
	}
	return cache(binFormula, ret);
}
 
Example 6
Source File: Skolemizer.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * If not cached, visits the formula's children with appropriate settings
 * for the negated flag and the skolemDepth parameter.
 * @see kodkod.ast.visitor.AbstractReplacer#visit(kodkod.ast.BinaryFormula)
 */
public final Formula visit(BinaryFormula bf) {
	Formula ret = lookup(bf);
	if (ret!=null) return ret;			
	final FormulaOperator op = bf.op();
	final int oldDepth = skolemDepth;
	if (op==IFF || (negated && op==AND) || (!negated && (op==OR || op==IMPLIES))) { // cannot skolemize in these cases
		skolemDepth = -1;
	}
	final Formula left, right;
	if (negated && op==IMPLIES) { // !(a => b) = !(!a || b) = a && !b
		negated = !negated;
		left = bf.left().accept(this);
		negated = !negated;
		right = bf.right().accept(this);
	} else {
		left = bf.left().accept(this);
		right = bf.right().accept(this);
	}
	skolemDepth = oldDepth;
	ret = (left==bf.left()&&right==bf.right()) ? bf : left.compose(op, right);
	return source(cache(bf,ret),bf);
}
 
Example 7
Source File: Nodes.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 8
Source File: PrettyPrinter.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
/** @effects appends the tokenization of the given node to this.tokens */
public void visit(BinaryFormula node) {
	if (displayed(node)) return;
	final boolean oldTop = top;
	final FormulaOperator op = node.op();
	if (op==IFF || (negated && op==AND) || (!negated && (op==OR || op==IMPLIES))) { // not top in these cases
		top = false;
	}
	final boolean pleft = parenthesize(op, node.left());
	final boolean flip = (negated && op==IMPLIES);
	
	if (pleft) indent++;
	negated = negated ^ flip;
	visitChild(node.left(), pleft);
	if (pleft) indent--;
	infix(op);
	if (top) newline();
	final boolean pright =  parenthesize(op, node.right());
	if (pright) indent++;
	negated = negated ^ flip;
	visitChild(node.right(), pright);
	if (pright) indent--;
	top = oldTop;
}
 
Example 9
Source File: A4Solution.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 10
Source File: A4Solution.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 11
Source File: TranslateKodkodToJava.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visit(BinaryFormula x) {
    String newname = makename(x);
    if (newname == null)
        return;
    String left = make(x.left());
    String right = make(x.right());
    switch (x.op()) {
        case AND :
            file.printf("Formula %s=%s.and(%s);%n", newname, left, right);
            break;
        case OR :
            file.printf("Formula %s=%s.or(%s);%n", newname, left, right);
            break;
        case IMPLIES :
            file.printf("Formula %s=%s.implies(%s);%n", newname, left, right);
            break;
        case IFF :
            file.printf("Formula %s=%s.iff(%s);%n", newname, left, right);
            break;
        default :
            throw new RuntimeException("Unknown kodkod operator \"" + x.op() + "\" encountered");
    }
}
 
Example 12
Source File: Nodes.java    From kodkod with MIT License 5 votes vote down vote up
/**
    * 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 conjunction 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);
}
 
Example 13
Source File: TrivialProof.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * If this formula should be visited, then we visit its children only if they
 * could have contributed to the unsatisfiability of the top-level formula. For
 * example, let binFormula = "p && q", binFormula simplified to FALSE, p
 * simplified to FALSE and q was not simplified, then only p should be visited
 * since p caused binFormula's reduction to FALSE.
 */
@Override
public void visit(BinaryFormula binFormula) {
    if (visited(binFormula))
        return;
    relevant.add(binFormula);

    final Formula l = binFormula.left(), r = binFormula.right();
    final Boolean lval = constNodes.get(l), rval = constNodes.get(r);
    final boolean lvisit, rvisit;

    switch (binFormula.op()) {
        case AND :
            lvisit = (lval == Boolean.FALSE || (lval == null && rval != Boolean.FALSE));
            rvisit = (rval != Boolean.TRUE && lval != Boolean.FALSE);
            break;
        case OR :
            lvisit = (lval == Boolean.TRUE || (lval == null && rval != Boolean.TRUE));
            rvisit = (rval != Boolean.FALSE && lval != Boolean.TRUE);
            break;
        case IMPLIES : // !l || r
            lvisit = (lval == Boolean.FALSE || (lval == null && rval != Boolean.TRUE));
            rvisit = (rval != Boolean.FALSE && lval != Boolean.FALSE);
            break;
        case IFF : // (!l || r) && (l || !r)
            lvisit = rvisit = true;
            break;
        default :
            throw new IllegalArgumentException("Unknown operator: " + binFormula.op());
    }

    if (lvisit) {
        l.accept(this);
    }
    if (rvisit) {
        r.accept(this);
    }
}
 
Example 14
Source File: PrettyPrinter.java    From kodkod with MIT License 5 votes vote down vote up
/** @ensures appends the tokenization of the given node to this.tokens */
public void visit(BinaryFormula node) {
	final FormulaOperator op = node.op();
	final boolean pleft = parenthesize(op, node.left());
	if (pleft) indent++;
	visitChild(node.left(), pleft);
	if (pleft) indent--;
	infix(op);
	newline();
	final boolean pright =  parenthesize(op, node.right());
	if (pright) indent++;
	visitChild(node.right(), pright);
	if (pright) indent--;
}
 
Example 15
Source File: PartialCannonicalizer.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public Formula visit(BinaryFormula formula) { 
	Formula ret = lookup(formula);
	if (ret!=null) return ret;
	final FormulaOperator op = formula.op();
	if (op==FormulaOperator.AND) {
		final Set<Formula> conjuncts = kodkod.util.nodes.Nodes.roots(formula);
		if (conjuncts.size()>2) { 
			return cache(formula, Formula.and(conjuncts).accept(this));
		}
	}
	
	final Formula left = formula.left().accept(this);
	final Formula right = formula.right().accept(this);
	
	ret = simplify(op, left, right);
	
	if (ret==null) {
	
		final int hash = hash(op, left, right);
		for(Iterator<PartialCannonicalizer.Holder<Formula>> itr = formulas.get(hash); itr.hasNext(); ) {
			final Formula next = itr.next().obj;
			if (next instanceof BinaryFormula) { 
				final BinaryFormula hit = (BinaryFormula) next;
				if (hit.op()==op && hit.left()==left && hit.right()==right) { 
					return cache(formula, hit);
				}
			}
		}

		ret = left==formula.left()&&right==formula.right() ? formula : left.compose(op, right);
		formulas.add(new PartialCannonicalizer.Holder<Formula>(ret, hash));
	}
	
	return cache(formula,ret);
}
 
Example 16
Source File: Nodes.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns an unmodifiable set consisting of the children of the given formula, if the formula is a binary or an nary conjunction.  Otherwise
 * returns a singleton set containing the formula itself.
 * @return  an unmodifiable set consisting of children of the given formula, if the formula is a binary or an nary conjunction.  Otherwise
 * returns a singleton set containing the formula itself.
 */
public static Set<Formula> conjuncts(Formula formula) { 
	if (formula instanceof BinaryFormula) { 
		final BinaryFormula bin = (BinaryFormula) formula;
		if (bin.op()==FormulaOperator.AND) {
			final Formula left = bin.left(), right = bin.right();
			if (left==right) return Collections.singleton(left);
			else return new AbstractSet<Formula>() {
				@Override
				public boolean contains(Object o) { return left==o || right==o; }
				@Override
				public Iterator<Formula> iterator() { return Containers.iterate(left, right); }
				@Override
				public int size() { return 2;	}
				
			};
		}
	} else if (formula instanceof NaryFormula) { 
		final NaryFormula nf = (NaryFormula) formula;
		if (nf.op()==FormulaOperator.AND) { 
			final LinkedHashSet<Formula> children = new LinkedHashSet<Formula>(1+(nf.size()*4)/3);
			for(Formula child : nf) { children.add(child); }
			return Collections.unmodifiableSet(children);
		}
	} 
	
	return Collections.singleton(formula);
	
}
 
Example 17
Source File: Nodes.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 18
Source File: HOLTranslator.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
@Override
public Proc visit(BinaryFormula binFormula) {
    if (annotated.isFOLNode(binFormula))
        return new Proc.FOL(bounds, binFormula);
    Proc left, right;
    if (binFormula.op() == FormulaOperator.OR) {
        left = toProc(binFormula.left());
        right = toProc(binFormula.right());
    } else {
        left = binFormula.left().accept(this);
        right = binFormula.right().accept(this);
    }
    return left.compose(binFormula.op(), right);
}
 
Example 19
Source File: FOL2BoolTranslator.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Calls lookup(binFormula) and returns the cached value, if any. If a
 * translation has not been cached, translates the formula, calls cache(...) on
 * it and returns it.
 *
 * @return let t = lookup(binFormula) | some t => t, cache(binFormula,
 *         binFormula.op(binFormula.left.accept(this),
 *         binFormula.right.accept(this))
 */
@Override
public final BooleanValue visit(BinaryFormula binFormula) {
    BooleanValue ret = lookup(binFormula);
    if (ret != null)
        return ret;

    final BooleanValue left = binFormula.left().accept(this);
    final BooleanValue right = binFormula.right().accept(this);
    final FormulaOperator op = binFormula.op();
    final BooleanFactory f = interpreter.factory();

    switch (op) {
        case AND :
            ret = f.and(left, right);
            break;
        case OR :
            ret = f.or(left, right);
            break;
        case IMPLIES :
            ret = f.implies(left, right);
            break;
        case IFF :
            ret = f.iff(left, right);
            break;
        default :
            throw new IllegalArgumentException("Unknown operator: " + op);
    }
    return cache(binFormula, ret);
}
 
Example 20
Source File: Simplifier.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public Formula visit(BinaryFormula expr) { 
	Formula ret = lookup(expr);
	if (ret!=null) return ret;
	final FormulaOperator op = expr.op();
	final Formula left = expr.left().accept(this);
	final Formula right = expr.right().accept(this);
	
	ret = simplify(op, left, right);
	
	if (ret==null) {
		ret = left==expr.left()&&right==expr.right() ? expr : left.compose(op, right);
	}
	
	return cache(expr,ret);
}