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

The following examples show how to use kodkod.ast.BinaryFormula#right() . 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: 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 3
Source File: AbstractReplacer.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
 * replacement has not been cached, visits the formula's children. If nothing
 * changes, the argument is cached and returned, otherwise a replacement formula
 * is cached and returned.
 *
 * @return { b: BinaryFormula | b.left = binExpr.left.accept(delegate) &&
 *         b.right = binExpr.right.accept(delegate) && b.op = binExpr.op }
 */
@Override
public Formula visit(BinaryFormula binFormula) {
    Formula ret = lookup(binFormula);
    if (ret != null)
        return ret;

    final Formula left = binFormula.left().accept(delegate);
    final Formula right = binFormula.right().accept(delegate);
    ret = (left == binFormula.left() && right == binFormula.right()) ? binFormula : left.compose(binFormula.op(), right);
    return cache(binFormula, ret);
}
 
Example 4
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 5
Source File: Skolemizer.java    From org.alloytools.alloy with Apache License 2.0 5 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)
 */
@Override
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 6
Source File: AbstractReplacer.java    From kodkod with MIT License 5 votes vote down vote up
/** 
 * Calls lookup(binFormula) and returns the cached value, if any.  
 * If a replacement has not been cached, visits the formula's 
 * children.  If nothing changes, the argument is cached and
 * returned, otherwise a replacement formula is cached and returned.
 * @return { b: BinaryFormula | b.left = binExpr.left.accept(this) &&
 *                              b.right = binExpr.right.accept(this) && b.op = binExpr.op }
 */
public Formula visit(BinaryFormula binFormula) {
	Formula ret = lookup(binFormula);
	if (ret!=null) return ret;
	
	final Formula left  = binFormula.left().accept(this);
	final Formula right = binFormula.right().accept(this);
	ret = (left==binFormula.left() && right==binFormula.right()) ? 
		  binFormula : left.compose(binFormula.op(), right);     
	return cache(binFormula,ret);
}
 
Example 7
Source File: TrivialProof.java    From kodkod with MIT License 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.
 */
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 8
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 9
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 10
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);
}
 
Example 11
Source File: Nodes.java    From org.alloytools.alloy with Apache License 2.0 4 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);

}