use of kodkod.ast.Decl in project org.alloytools.alloy by AlloyTools.
the class Skolemizer method domainConstraint.
/**
* Returns a formula that properly constrains the given skolem's domain.
*
* @requires !nonSkolems.isEmpty()
* @return a formula that properly constrains the given skolem's domain.
*/
private Formula domainConstraint(Decl skolemDecl, Relation skolem) {
final Iterator<DeclInfo> itr = nonSkolems.iterator();
Decls rangeDecls = null;
while (itr.hasNext()) {
Decl d = itr.next().decl;
Decl dd = d.variable().oneOf(d.expression());
rangeDecls = rangeDecls != null ? rangeDecls.and(dd) : dd;
}
// System.out.println(skolemDecl.expression());
Expression skolemDomain = skolem;
for (int i = 0, max = skolemDecl.variable().arity(); i < max; i++) {
skolemDomain = skolemDomain.join(Expression.UNIV);
}
return skolemDomain.in(Formula.TRUE.comprehension(rangeDecls));
}
use of kodkod.ast.Decl in project org.alloytools.alloy by AlloyTools.
the class HOLTranslator method visit.
@Override
public Proc visit(QuantifiedFormula qf) {
assertNotSkolemizable(qf);
Formula qfFlipped = qf.body().quantify(qf.quantifier().opposite, qf.decls(), qf.domain());
Proc body = toProc(qfFlipped);
boolean firstOrder = true;
for (Decl decl : qf.decls()) if (decl.multiplicity() != Multiplicity.ONE) {
firstOrder = false;
break;
}
if (firstOrder && body instanceof FOL && noNewHOLSkolems(((FOL) body).bounds.skolems(), bounds.skolems()))
return new Proc.FOL(bounds, qf);
else
return new Proc.Some4All(bounds, qf, body);
}
use of kodkod.ast.Decl 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.ast.Decl 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();
}
use of kodkod.ast.Decl in project org.alloytools.alloy by AlloyTools.
the class EvaluatorTest method testQuantifiedFormula.
public final void testQuantifiedFormula() {
final Variable p = Variable.unary("p"), q = Variable.unary("q");
final Decl pdecl = p.oneOf(person), qdecl = q.oneOf(person);
final Decls pqdecls = pdecl.and(qdecl);
// all p: Person | some p.spouse = true
assertTrue(eval(p.join(spouse).some().forAll(pdecl)));
// all p, q: Person | (p.spouse = q) => ! (q in p.shaken) = true
assertTrue(eval((p.join(spouse).eq(q).implies(q.in(p.join(shaken)).not()).forAll(pqdecls))));
// some p: Person | no p.shaken = true
assertTrue(eval(p.join(shaken).no().forSome(pdecl)));
// all p: Person | some q: Person | p.shaken = q = false
assertFalse(eval((p.join(shaken).eq(q).forSome(qdecl)).forAll(pdecl)));
// some p, q: Person | !(p = q) && (p.shaken = q.shaken) = true
assertTrue(eval(p.eq(q).not().and(p.join(shaken).eq(q.join(shaken))).forSome(pqdecls)));
// some p: Person | all q: Person-p | p in q.shaken = false
assertFalse(eval((p.in(q.join(shaken)).forAll(q.oneOf(person.difference(p)))).forSome(pdecl)));
}
Aggregations