Java Code Examples for kodkod.ast.Expression#arity()

The following examples show how to use kodkod.ast.Expression#arity() . 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: A4Solution.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Add a new sig to this solution and associate it with the given expression
 * (and if s.isTopLevel then add this expression into Sig.UNIV). <br>
 * The expression must contain only constant Relations or Relations that are
 * already bound in this solution. <br>
 * (If the sig was already added by a previous call to addSig(), then this call
 * will return immediately without altering what it is associated with)
 */
void addSig(Sig s, Expression expr) throws ErrorFatal {
    if (solved)
        throw new ErrorFatal("Cannot add an additional sig since solve() has completed.");
    if (expr.arity() != 1)
        throw new ErrorFatal("Sig " + s + " must be associated with a unary relational value.");
    if (a2k.containsKey(s))
        return;
    a2k.put(s, expr);
    sigs.add(s);
    if (s.isTopLevel())
        a2k.put(UNIV, a2k.get(UNIV).union(expr));
}
 
Example 2
Source File: A4Solution.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Add a new field to this solution and associate it with the given expression.
 * <br>
 * The expression must contain only constant Relations or Relations that are
 * already bound in this solution. <br>
 * (If the field was already added by a previous call to addField(), then this
 * call will return immediately without altering what it is associated with)
 */
void addField(Field f, Expression expr) throws ErrorFatal {
    if (solved)
        throw new ErrorFatal("Cannot add an additional field since solve() has completed.");
    if (expr.arity() != f.type().arity())
        throw new ErrorFatal("Field " + f + " must be associated with an " + f.type().arity() + "-ary relational value.");
    if (a2k.containsKey(f))
        return;
    a2k.put(f, expr);
}
 
Example 3
Source File: A4Solution.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Add a new skolem to this solution and associate it with the given expression.
 * <br>
 * The expression must contain only constant Relations or Relations that are
 * already bound in this solution.
 */
private ExprVar addSkolem(String label, Type type, Expression expr) throws Err {
    if (solved)
        throw new ErrorFatal("Cannot add an additional skolem since solve() has completed.");
    int a = type.arity();
    if (a < 1)
        throw new ErrorFatal("Skolem " + label + " must be associated with a relational value.");
    if (a != expr.arity())
        throw new ErrorFatal("Skolem " + label + " must be associated with an " + a + "-ary relational value.");
    ExprVar v = ExprVar.make(Pos.UNKNOWN, label, type);
    a2k.put(v, expr);
    skolems.add(v);
    return v;
}
 
Example 4
Source File: ListEncoding.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns a formula stating that the given expression is acyclic.
 * @return {f: Formula | f <=> no ^expr & iden}
 * @throws IllegalArgumentException  expr.arity != 2
 */
public Formula acyclic(Expression expr) {
	if (expr instanceof Relation)
		return ((Relation)expr).acyclic();  // special handling for relations that enables better symmetry breaking
	
	if (expr.arity() != 2) throw new IllegalArgumentException();
	return expr.closure().intersection(Expression.IDEN).no();
}
 
Example 5
Source File: ListEncoding.java    From kodkod with MIT License 5 votes vote down vote up
/**
   * Returns a formula stating that the given relation is a total function
   * with the specified domain and range.
   * @return {f: Formula | f <=> expr in domain->range && all v: domain | one v.expr }
   * @throws NullPointerException  domain = null || range = null
   * @throws IllegalArgumentException  domain.arity != 1 || range.arity != 1
   * @throws IllegalArgumentException  this.arity != 2
   */
  public Formula function(Expression expr, Expression domain, Expression range) {
  	if (expr instanceof Relation)
  		return ((Relation)expr).function(domain, range);  // special handling for relations that enables better symmetry breaking
  	
  	if (domain.arity() != 1 || range.arity() != 1)
	throw new IllegalArgumentException("invalid arity: " + domain + " or " + range);
// expr in domain->range 
final Formula domainConstraint = expr.in(domain.product(range));
// all v: domain | one v.expr
final Variable v = Variable.unary("v"+expr.hashCode());
final Formula funConstraint = v.join(expr).one().forAll(v.oneOf(domain));
// expr in domain->range && all v: domain | targetMult v.relation
return domainConstraint.and(funConstraint);
  }