**/
@SuppressWarnings("unchecked")
static final BooleanMatrix approximate(AnnotatedNode<Expression> annotated, LeafInterpreter interpreter, Environment<BooleanMatrix> env) {
final FOL2BoolTranslator approximator = new FOL2BoolTranslator(new FOL2BoolCache(annotated), interpreter, env) {
public final BooleanMatrix visit(BinaryExpression binExpr) {
final BooleanMatrix ret = lookup(binExpr);
if (ret!=null) return ret;
switch(binExpr.op()){
case DIFFERENCE : return cache(binExpr, binExpr.left().accept(this));
case OVERRIDE : return cache(binExpr, binExpr.left().accept(this).or(binExpr.right().accept(this)));
default : return super.visit(binExpr);
}
}
public final BooleanMatrix visit(Comprehension cexpr) {
final BooleanMatrix ret = lookup(cexpr);
return ret!=null ? ret : cache(cexpr, super.visit((Comprehension)Formula.TRUE.comprehension(cexpr.decls())));
}
public BooleanMatrix visit(IfExpression ifExpr) {
final BooleanMatrix ret = lookup(ifExpr);
return ret!=null ? ret : cache(ifExpr, ifExpr.thenExpr().accept(this).or(ifExpr.elseExpr().accept(this)));
}
public BooleanMatrix visit(IntToExprCast castExpr) {
BooleanMatrix ret = lookup(castExpr);
if (ret!=null) return ret;
switch(castExpr.op()) {
case INTCAST : return cache(castExpr, Expression.INTS.accept(this));
case BITSETCAST :
final BooleanFactory factory = super.interpreter.factory();
ret = factory.matrix(Dimensions.square(super.interpreter.universe().size(), 1));
final IntSet ints = super.interpreter.ints();
final int msb = factory.bitwidth()-1;
// handle all bits but the sign bit
for(int i = 0; i < msb; i++) {
int pow2 = 1<<i;
if (ints.contains(pow2)) {
ret.set(super.interpreter.interpret(pow2), BooleanConstant.TRUE);
}
}
// handle the sign bit
if (ints.contains(-1<<msb)) {
ret.set(super.interpreter.interpret(-1<<msb), BooleanConstant.TRUE);
}
return cache(castExpr, ret);
default : throw new IllegalArgumentException("Unknown operator: " + castExpr.op());
}