kodkod.ast.SumExpression Java Examples

The following examples show how to use kodkod.ast.SumExpression. 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: FOL2BoolTranslator.java    From kodkod with MIT License 6 votes vote down vote up
/** 
 * Calls lookup(intExpr) and returns the cached value, if any.  
 * If a translation has not been cached, translates the expression,
 * calls cache(...) on it and returns it.
 * @return let t = lookup(intExpr) | some t => t, 
 * 	cache(intExpr, translate(intExpr))
 */
public final Int visit(SumExpression intExpr) {
	final Int ret = lookup(intExpr);
	if (ret!=null) return ret;
	final List<Int> values = new ArrayList<Int>();
	sum(intExpr.decls(), intExpr.intExpr(), 0, BooleanConstant.TRUE, values);
	for(int sums = values.size(); sums > 1; sums -= sums/2) { 
		final int max = sums-1;
		for(int i = 0; i < max; i += 2) { 
			values.set(i/2, values.get(i).plus(values.get(i+1)));
		}
		if (max%2==0) { // even max => odd number of entries
			values.set(max/2, values.get(max));
		}
	}
	return cache(intExpr, values.isEmpty() ? interpreter.factory().integer(0) : values.get(0));
}
 
Example #2
Source File: Skolemizer.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * @see kodkod.ast.visitor.AbstractReplacer#visit(kodkod.ast.SumExpression)
 */
@Override
public final IntExpression visit(SumExpression intExpr) {
    IntExpression ret = lookup(intExpr);
    if (ret != null)
        return ret;
    final Environment<Expression,Expression> oldRepEnv = repEnv; // skolemDepth
                                                                // < 0
                                                                // at
                                                                // this
                                                                // point
    final Decls decls = visit(intExpr.decls());
    final IntExpression expr = intExpr.intExpr().accept(this);
    ret = (decls == intExpr.decls() && expr == intExpr.intExpr()) ? intExpr : expr.sum(decls);
    repEnv = oldRepEnv;
    return cache(intExpr, ret);
}
 
Example #3
Source File: FOL2BoolTranslator.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Calls lookup(intExpr) and returns the cached value, if any. If a translation
 * has not been cached, translates the expression, calls cache(...) on it and
 * returns it.
 *
 * @return let t = lookup(intExpr) | some t => t, cache(intExpr,
 *         translate(intExpr))
 */
@Override
public final Int visit(SumExpression intExpr) {
    final Int ret = lookup(intExpr);
    if (ret != null)
        return ret;
    final List<Int> values = new ArrayList<Int>();
    sum(intExpr.decls(), intExpr.intExpr(), 0, BooleanConstant.TRUE, values);
    for (int sums = values.size(); sums > 1; sums -= sums / 2) {
        final int max = sums - 1;
        for (int i = 0; i < max; i += 2) {
            values.set(i / 2, values.get(i).plus(values.get(i + 1)));
        }
        if (max % 2 == 0) { // even max => odd number of entries
            values.set(max / 2, values.get(max));
        }
    }
    if (values.isEmpty()) {
        return cache(intExpr, interpreter.factory().integer(0));
    } else {
        Int sumValue = values.get(0);
        return cache(intExpr, sumValue);
    }
}
 
Example #4
Source File: TranslateKodkodToJava.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visit(SumExpression x) {
    String newname = makename(x);
    if (newname == null)
        return;
    String d = make(x.decls());
    String f = make(x.intExpr());
    file.printf("IntExpression %s=%s.sum(%s);%n", newname, f, d);
}
 
Example #5
Source File: PrettyPrinter.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
/** @effects this.tokens' = concat[ this.tokens,  "sum",
 *   tokenize[node.decls], "|", tokenize[ node.intExpr ],  ]*/
public void visit(SumExpression node) {
	if (displayed(node)) return;
	final boolean oldTop = notTop();
	keyword("sum");
	node.decls().accept(this);
	infix("|");
	node.intExpr().accept(this);
	top = oldTop;
}
 
Example #6
Source File: PrettyPrinter.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
/** @return true if the given int expression needs to be parenthesized if a 
 * child of a binary int expression with the given operator */
private boolean parenthesize(IntOperator op, IntExpression child) { 
	return child instanceof SumExpression ||
		   child instanceof IfIntExpression || 
		   child instanceof NaryIntExpression ||
	       (child instanceof BinaryIntExpression && 
	        (!associative(op) || ((BinaryIntExpression)child).op()!=op));
}
 
Example #7
Source File: PrettyPrinter.java    From kodkod with MIT License 5 votes vote down vote up
/** @ensures this.tokens' = concat[ this.tokens,  "sum",
 *   tokenize[node.decls], "|", tokenize[ node.intExpr ],  ]*/
public void visit(SumExpression node) {
	keyword("sum");
	node.decls().accept(this);
	infix("|");
	node.intExpr().accept(this);
}
 
Example #8
Source File: PrettyPrinter.java    From kodkod with MIT License 5 votes vote down vote up
/** @return true if the given int expression needs to be parenthesized if a 
 * child of a binary int expression with the given operator */
private boolean parenthesize(IntOperator op, IntExpression child) { 
	return child instanceof SumExpression ||
		   child instanceof IfIntExpression || 
		   child instanceof NaryIntExpression ||
	       (child instanceof BinaryIntExpression && 
	        (!associative(op) || ((BinaryIntExpression)child).op()!=op));
}
 
Example #9
Source File: Skolemizer.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * @see kodkod.ast.visitor.AbstractReplacer#visit(kodkod.ast.SumExpression)
 */
@Override
public final IntExpression visit(SumExpression intExpr) {
	IntExpression ret = lookup(intExpr);
	if (ret!=null) return ret;	
	final Environment<Expression> oldRepEnv = repEnv; // skolemDepth < 0 at this point
	final Decls decls  = visit((Decls)intExpr.decls());
	final IntExpression expr = intExpr.intExpr().accept(this);
	ret =  (decls==intExpr.decls() && expr==intExpr.intExpr()) ? intExpr : expr.sum(decls);
	repEnv = oldRepEnv;
	return cache(intExpr,ret);
}
 
Example #10
Source File: AbstractCollector.java    From kodkod with MIT License 5 votes vote down vote up
/** 
 * Calls lookup(intExpr) 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(intExpr) | 
 *          x != null => x,  
 *          cache(intExpr, intExpr.decls.accept(this) + intExpr.intExpr.accept(this)) 
 */
public Set<T> visit(SumExpression intExpr) {
	Set<T> ret = lookup(intExpr);
	if (ret!=null) return ret;		
	ret = newSet();
	ret.addAll(intExpr.decls().accept(this));
	ret.addAll(intExpr.intExpr().accept(this));
	return cache(intExpr, ret);
}
 
Example #11
Source File: AbstractReplacer.java    From kodkod with MIT License 5 votes vote down vote up
/** 
* Calls lookup(intExpr) and returns the cached value, if any.  
* If a replacement has not been cached, visits the expression's 
* children.  If nothing changes, the argument is cached and
* returned, otherwise a replacement expression is cached and returned.
* @return { c: IntExpression | [[c]] = sum intExpr.decls.accept(this) | intExpr.intExpr.accept(this) }
*/
  public IntExpression visit(SumExpression intExpr) {
IntExpression ret = lookup(intExpr);
if (ret!=null) return ret;
	
final Decls decls  = intExpr.decls().accept(this);
final IntExpression expr = intExpr.intExpr().accept(this);
ret =  (decls==intExpr.decls() && expr==intExpr.intExpr()) ? 
		intExpr : expr.sum(decls);
return cache(intExpr,ret);
  }
 
Example #12
Source File: PrettyPrinter.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * @ensures this.tokens' = concat[ this.tokens, "sum", tokenize[node.decls],
 *          "|", tokenize[ node.intExpr ], ]
 */
@Override
public void visit(SumExpression node) {
    keyword("sum");
    node.decls().accept(this);
    infix("|");
    node.intExpr().accept(this);
}
 
Example #13
Source File: AbstractReplacer.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Calls lookup(intExpr) and returns the cached value, if any. If a replacement
 * has not been cached, visits the expression's children. If nothing changes,
 * the argument is cached and returned, otherwise a replacement expression is
 * cached and returned.
 *
 * @return { c: IntExpression | [[c]] = sum intExpr.decls.accept(delegate) |
 *         intExpr.intExpr.accept(delegate) }
 */
@Override
public IntExpression visit(SumExpression intExpr) {
    IntExpression ret = lookup(intExpr);
    if (ret != null)
        return ret;

    final Decls decls = intExpr.decls().accept(delegate);
    final IntExpression expr = intExpr.intExpr().accept(delegate);
    ret = (decls == intExpr.decls() && expr == intExpr.intExpr()) ? intExpr : expr.sum(decls);
    return cache(intExpr, ret);
}
 
Example #14
Source File: AbstractVoidVisitor.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Visits the children of the given sum expression if this.visited(intExpr)
 * returns false. Otherwise does nothing.
 *
 * @ensures intExpr.decls.accept(this) && intExpr.intExpr.accept(this)
 */
@Override
public void visit(SumExpression intExpr) {
    if (visited(intExpr))
        return;
    intExpr.decls().accept(this);
    intExpr.intExpr().accept(this);
}
 
Example #15
Source File: AbstractCollector.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Calls lookup(intExpr) 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(intExpr) | x != null => x, cache(intExpr,
 *         intExpr.decls.accept(this) + intExpr.intExpr.accept(this))
 */
@Override
public Set<T> visit(SumExpression intExpr) {
    Set<T> ret = lookup(intExpr);
    if (ret != null)
        return ret;
    ret = newSet();
    ret.addAll(intExpr.decls().accept(this));
    ret.addAll(intExpr.intExpr().accept(this));
    return cache(intExpr, ret);
}
 
Example #16
Source File: PrettyPrinter.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(SumExpression intExpr) {
    visit(intExpr, "sum", intExpr.decls(), intExpr.intExpr());
}
 
Example #17
Source File: AnnotatedNode.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
@Override
public Boolean visit(SumExpression intExpr) {
    return visit(intExpr, intExpr.decls(), intExpr.intExpr());
}
 
Example #18
Source File: AnnotatedNode.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
@Override
public Boolean visit(SumExpression e) {
    return checkVisitedThenAccumA(e, Boolean.FALSE, e.decls(), e.intExpr());
}
 
Example #19
Source File: AspectReturnVisitor.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
@Override
public I visit(SumExpression intExpr) {
    start(intExpr);
    return end(intExpr, visitor.visit(intExpr));
}
 
Example #20
Source File: HOLTranslator.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
@Override
public IntExpression visit(SumExpression intExpr) {
    return intExpr;
}
 
Example #21
Source File: AbstractVoidVisitor.java    From kodkod with MIT License 4 votes vote down vote up
/**
 * Visits the children of the given sum expression  if
 * this.visited(intExpr) returns false.  Otherwise does nothing.
 * @ensures intExpr.decls.accept(this) && intExpr.intExpr.accept(this)
 */
public void visit(SumExpression intExpr) {
	if (visited(intExpr)) return;
	intExpr.decls().accept(this);
	intExpr.intExpr().accept(this);
}
 
Example #22
Source File: AnnotatedNode.java    From kodkod with MIT License 4 votes vote down vote up
public Boolean visit(SumExpression intExpr) {
	return visit(intExpr, intExpr.decls(), intExpr.intExpr());
}
 
Example #23
Source File: VoidVisitor.java    From org.alloytools.alloy with Apache License 2.0 2 votes vote down vote up
/**
 * Visits the given sum expression.
 */
public void visit(SumExpression intExpr);
 
Example #24
Source File: PrettyPrinter.java    From org.alloytools.alloy with Apache License 2.0 2 votes vote down vote up
/**
 * @return true if the given int expression needs to be parenthesized if a child
 *         of a binary int expression with the given operator
 */
private boolean parenthesize(IntOperator op, IntExpression child) {
    return child instanceof SumExpression || child instanceof IfIntExpression || child instanceof NaryIntExpression || (child instanceof BinaryIntExpression && (!associative(op) || ((BinaryIntExpression) child).op() != op));
}
 
Example #25
Source File: ReturnVisitor.java    From org.alloytools.alloy with Apache License 2.0 2 votes vote down vote up
/**
 * Visits the given sum expression and returns the result.
 *
 * @return the result of visiting <code>intExpr</code>
 */
public I visit(SumExpression intExpr);
 
Example #26
Source File: FreeVariableCollector.java    From kodkod with MIT License 2 votes vote down vote up
/** 
 * Calls lookup(intExpr) and returns the cached value, if any.  
 * If no cached value exists, computes, caches and returns the set
 * of free variables in intExpr.  
 * @return let x = lookup(intExpr), d = intExpr.declarations, e = intExpr.intExpr | 
 *          x != null => x,  
 *          cache(intExpr, 
 *            (e.accept(this) - d.children.variable) + 
 *            {v: Variable | some i: [0..d.size) | 
 *             v in d.declarations[i].accept(this) - d.declarations[0..i).variable } ) 
 */
@Override
public Set<Variable> visit(SumExpression intExpr) {
	return visit(intExpr, intExpr.decls(), intExpr.intExpr());
}
 
Example #27
Source File: AbstractDetector.java    From org.alloytools.alloy with Apache License 2.0 2 votes vote down vote up
/**
 * Calls lookup(intExpr) and returns the cached value, if any. If no cached
 * value exists, visits each child, caches the disjunction of the children's
 * return values and returns it.
 *
 * @return let x = lookup(intExpr) | x != null => x, cache(intExpr,
 *         intExpr.decls.accept(this) || intExpr.intExpr.accept(this))
 */
@Override
public Boolean visit(SumExpression intExpr) {
    final Boolean ret = lookup(intExpr);
    return (ret != null) ? ret : cache(intExpr, intExpr.decls().accept(this) || intExpr.intExpr().accept(this));
}
 
Example #28
Source File: AbstractDetector.java    From kodkod with MIT License 2 votes vote down vote up
/** 
 * Calls lookup(intExpr) and returns the cached value, if any.  
 * If no cached value exists, visits each child, caches the
 * disjunction of the children's return values and returns it. 
 * @return let x = lookup(intExpr) | 
 *          x != null => x,  
 *          cache(intExpr, intExpr.decls.accept(this) || intExpr.intExpr.accept(this)) 
 */
public Boolean visit(SumExpression intExpr) {
	final Boolean ret = lookup(intExpr);
	return (ret!=null) ? ret : cache(intExpr, intExpr.decls().accept(this) || intExpr.intExpr().accept(this));
}
 
Example #29
Source File: FreeVariableCollector.java    From org.alloytools.alloy with Apache License 2.0 2 votes vote down vote up
/**
 * Calls lookup(intExpr) and returns the cached value, if any. If no cached
 * value exists, computes, caches and returns the set of free variables in
 * intExpr.
 *
 * @return let x = lookup(intExpr), d = intExpr.declarations, e =
 *         intExpr.intExpr | x != null => x, cache(intExpr, (e.accept(this) -
 *         d.children.variable) + {v: Variable | some i: [0..d.size) | v in
 *         d.declarations[i].accept(this) - d.declarations[0..i).variable } )
 */
@Override
public Set<Variable> visit(SumExpression intExpr) {
    return visit(intExpr, intExpr.decls(), intExpr.intExpr());
}
 
Example #30
Source File: ReturnVisitor.java    From kodkod with MIT License 2 votes vote down vote up
/**
   * Visits the given sum expression and returns the result.
* @return the result of visiting <code>intExpr</code> 
   */
  public I visit(SumExpression intExpr);