use of kodkod.engine.bool.BooleanValue in project org.alloytools.alloy by AlloyTools.
the class Translator method toBoolean.
// private HOLTranslation toHOLTransl(AnnotatedNode<Formula> annotated,
// SymmetryBreaker breaker) {
// checkHOLOptions(options);
//
// // AnnotatedNode<Formula> fl = FormulaFlattener.flatten(optimized, true);
// //TODO: need this?
// //AnnotatedNode<Formula> nnf1 = NNFConverter.toNNF(optimized,
// options.reporter());
// AnnotatedNode<Formula> nnf = FullNegationPropagator.toNNF(annotated,
// options.reporter());
// //AnnotatedNode<Formula> pnf = PrenexNFConverter.toPNF(nnf);
// AnnotatedNode<Formula> skl = Skolemizer.skolemize(nnf, bounds, options);
//
// return HOL2ProcTranslator.translate(skl, bounds, options);
// }
/**
* Translates the given annotated formula to a circuit, conjoins the circuit
* with an SBP generated by the given symmetry breaker, and returns its
* {@linkplain Translation} to CNF. The SBP breaks any symmetries that could not
* be broken during the
* {@linkplain #optimizeFormulaAndBounds(AnnotatedNode, SymmetryBreaker) formula
* and bounds optimization} step.
*
* @requires SAT(annotated.node, this.bounds, this.options) iff
* SAT(this.originalFormula, this.originalBounds, this.options)
* @requires breaker.bounds = this.bounds
* @ensures this.options.logTranslation => some this.log'
* @ensures this.options.reporter().translatingToBoolean(annotated.node(),
* this.bounds)
* @ensures this.options.reporter().generatingSBP()
* @return the translation of annotated.node with respect to this.bounds
*/
private Translation toBoolean(AnnotatedNode<Formula> annotated, SymmetryBreaker breaker) {
options.reporter().translatingToBoolean(annotated.node(), bounds);
final LeafInterpreter interpreter = LeafInterpreter.exact(bounds, options, incremental);
final BooleanFactory factory = interpreter.factory();
if (logging) {
assert !incremental;
final TranslationLogger logger = options.logTranslation() == 1 ? new MemoryLogger(annotated, bounds) : new FileLogger(annotated, bounds);
BooleanAccumulator circuit = FOL2BoolTranslator.translate(annotated, interpreter, logger);
final TranslationLog log = logger.log();
if (circuit.isShortCircuited()) {
return trivial(circuit.op().shortCircuit(), log);
} else if (circuit.size() == 0) {
return trivial(circuit.op().identity(), log);
}
circuit.add(breaker.generateSBP(interpreter, options));
return toCNF((BooleanFormula) factory.accumulate(circuit), interpreter, log);
} else {
BooleanValue circuit = (BooleanValue) FOL2BoolTranslator.translate(annotated, interpreter);
if (circuit.op() == Operator.CONST) {
return trivial((BooleanConstant) circuit, null);
}
return toCNF((BooleanFormula) factory.and(circuit, breaker.generateSBP(interpreter, options)), interpreter, null);
}
}
use of kodkod.engine.bool.BooleanValue in project org.alloytools.alloy by AlloyTools.
the class FOL2BoolTranslator method comprehension.
/**
* Translates the given comprehension as follows (where A_0...A_|A| stand for
* boolean variables that represent the tuples of the expression A, etc.): let
* comprehension = "{ a: A, b: B, ..., x: X | F(a, b, ..., x) }" | { a: A, b: B,
* ..., x: X | a in A && b in B && ... && x in X && F(a, b, ..., x) }.
*
* @param decls the declarations comprehension
* @param param formula the body of the comprehension
* @param currentDecl currently processed declaration; should be 0 initially
* @param declConstraints the constraints implied by the declarations; should be
* Boolean.TRUE intially
* @param partialIndex partial index into the provided matrix; should be 0
* initially
* @param matrix boolean matrix that will retain the final results; should be an
* empty matrix of dimensions universe.size^decls.length initially
* @ensures the given matrix contains the translation of the comprehension "{
* decls | formula }"
*/
private final void comprehension(Decls decls, Formula formula, int currentDecl, BooleanValue declConstraints, int partialIndex, BooleanMatrix matrix) {
final BooleanFactory factory = interpreter.factory();
if (currentDecl == decls.size()) {
// TODO: what about this and overflow???
matrix.set(partialIndex, factory.and(declConstraints, formula.accept(this)));
return;
}
final Decl decl = decls.get(currentDecl);
final BooleanMatrix declTransl = visit(decl);
final int position = (int) StrictMath.pow(interpreter.universe().size(), decls.size() - currentDecl - 1);
final BooleanMatrix groundValue = factory.matrix(declTransl.dimensions());
env = env.extend(decl.variable(), decl.expression(), groundValue);
for (IndexedEntry<BooleanValue> entry : declTransl) {
groundValue.set(entry.index(), BooleanConstant.TRUE);
comprehension(decls, formula, currentDecl + 1, factory.and(entry.value(), declConstraints), partialIndex + entry.index() * position, matrix);
groundValue.set(entry.index(), BooleanConstant.FALSE);
}
env = env.parent();
}
use of kodkod.engine.bool.BooleanValue in project org.alloytools.alloy by AlloyTools.
the class FOL2BoolTranslator method visit.
/**
* 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);
}
use of kodkod.engine.bool.BooleanValue in project org.alloytools.alloy by AlloyTools.
the class FOL2BoolTranslator method visit.
/**
* Calls lookup(compFormula) 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(compFormula) | some t => t, let op =
* (binExpr.op).(SUBSET->subset + EQUALS->eq) | cache(compFormula,
* op(compFormula.left.accept(this), compFormula.right.accept(this)))
*/
@Override
public final BooleanValue visit(ComparisonFormula compFormula) {
BooleanValue ret = lookup(compFormula);
if (ret != null)
return ret;
final BooleanMatrix left = compFormula.left().accept(this);
final BooleanMatrix right = compFormula.right().accept(this);
final ExprCompOperator op = compFormula.op();
switch(op) {
case SUBSET:
ret = left.subset(right, env);
break;
case EQUALS:
ret = left.eq(right, env);
break;
default:
throw new IllegalArgumentException("Unknown operator: " + compFormula.op());
}
return cache(compFormula, ret);
}
use of kodkod.engine.bool.BooleanValue in project org.alloytools.alloy by AlloyTools.
the class FOL2BoolTranslator method all.
/**
* Translates the given universally quantified formula as follows (where
* A_0...A_|A| stand for boolean variables that represent the tuples of the
* expression A, etc.): let quantFormula = "all a: A, b: B, ..., x: X | F(a, b,
* ..., x)" | (A_0 && B_0 && ... && X_0 => translate(F(A_0, B_0, ..., X_0))) &&
* ... && (A_|A| && B_|B| && ... && X_|X| => translate(F(A_|A|, B_|B|, ...,
* X_|X|))) If the noOverflow option is specified, then the translation looks
* like: let quantFormula = "all a: A, b: B, ..., x: X | F(a, b, ..., x)" | (A_0
* && B_0 && ... && X_0 => (!of(F(A_0, B_0, ..., X_0)) => translate(F(A_0, B_0,
* ..., X_0)))) && ... && (A_|A| && B_|B| && ... && X_|X| => (!of(F(A_|A|,
* B_|B|, ..., X_|X|)) => translate(F(A_|A|, B_|B|, ..., X_|X|)))) where
* of(F(A_|a|, B_|b|, ..., X_|x|)) is the portion of the overflow circuit
* generated by the translation of F(A_|a|, B_|b|, ..., X_|x|) contributed by
* arithmetic operations over only the integer variables of this quantifier
*
* @param decls formula declarations
* @param formula the formula body
* @param currentDecl currently processed declaration; should be 0 initially
* @param declConstraints the constraints implied by the declarations; should be
* Boolean.FALSE initially
* @param acc the accumulator that contains the top level conjunction; should be
* an empty AND accumulator initially
* @ensures the given accumulator contains the translation of the formula "all
* decls | formula"
*/
private void all(Decls decls, Formula formula, int currentDecl, BooleanValue declConstraints, BooleanAccumulator acc) {
if (acc.isShortCircuited())
return;
final BooleanFactory factory = interpreter.factory();
if (decls.size() == currentDecl) {
BooleanValue formulaCircuit = formula.accept(this);
BooleanValue finalCircuit = factory.or(declConstraints, formulaCircuit);
acc.add(finalCircuit);
return;
}
final Decl decl = decls.get(currentDecl);
final BooleanMatrix declTransl = visit(decl);
final BooleanMatrix groundValue = factory.matrix(declTransl.dimensions());
env = env.extend(decl.variable(), decl.expression(), groundValue, Quantifier.ALL);
for (IndexedEntry<BooleanValue> entry : declTransl) {
groundValue.set(entry.index(), BooleanConstant.TRUE);
all(decls, formula, currentDecl + 1, factory.or(factory.not(entry.value()), declConstraints), acc);
groundValue.set(entry.index(), BooleanConstant.FALSE);
}
env = env.parent();
}
Aggregations