kodkod.ast.Decl Java Examples

The following examples show how to use kodkod.ast.Decl. 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: EvaluatorTest.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
public final void testComprehension() {
    final Variable[] vars = new Variable[3];
    final Decl[] decls = new Decl[3];
    for (int i = 0; i < 3; i++) {
        Variable v = Variable.unary("v" + i);
        Decl d = v.oneOf(person);
        vars[i] = v;
        decls[i] = d;
    }

    // {v0: Person | no v0.shaken} = univ - shaken.Person
    assertEquals(eval(vars[0].join(shaken).no().comprehension(decls[0])), eval(univ.difference(shaken.join(person))));
    // {v0, v1: Person | v1 in v0.shaken} = shaken
    assertEquals(eval(vars[1].in(vars[0].join(shaken)).comprehension(decls[0].and(decls[1]))), value(shaken));
    // {v0, v1, v2: Person | no v1.shaken} = Person->(univ -
    // shaken.Person)->Person
    assertEquals(eval(vars[1].join(shaken).no().comprehension(decls[0].and(decls[1]).and(decls[2]))), eval(person.product(univ.difference(shaken.join(person))).product(person)));
}
 
Example #2
Source File: SkolemizationTest.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
public final void testDeepSkolems() {
    solver.options().setSkolemDepth(3);
    testDeepSkolems(Multiplicity.ONE);
    testDeepSkolems(Multiplicity.LONE);
    testDeepSkolems(Multiplicity.SOME);
    testDeepSkolems(Multiplicity.SET);
    final Variable va = Variable.unary("va");
    final Variable vb = Variable.unary("vb");
    Decl da1 = va.oneOf(r1a);
    Decl db = vb.oneOf(r1b);
    final Formula f0 = va.in(vb.join(r2b));
    final Formula f1 = f0.forAll(db).not().forAll(da1);
    final Formula f2 = f0.forSome(db).forSome(da1);
    Instance inst = solve(f1.and(f2));
    assertEquals(bounds.relations().size() + 3, inst.relations().size());
}
 
Example #3
Source File: Skolemizer.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * 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));
}
 
Example #4
Source File: FOL2BoolTranslator.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Translates the given sum expression as follows (where A_0...A_|A| stand for
 * boolean variables that represent the tuples of the expression A, etc.): let
 * sum = "sum a: A, b: B, ..., x: X | IE(a, b, ..., x) " | sum a: A, b: B, ...,
 * x: X | if (a in A && b in B && ... && x in X) then IE(a, b, ..., x) else 0 }.
 *
 * @param decls intexpr 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.TRUE intially
 * @param values integer values computed so far
 */
private final void sum(Decls decls, IntExpression expr, int currentDecl, BooleanValue declConstraints, List<Int> values) {
    final BooleanFactory factory = interpreter.factory();
    if (decls.size() == currentDecl) {
        Int intExpr = expr.accept(this);
        Int newInt = intExpr.choice(declConstraints, factory.integer(0));
        values.add(newInt);
        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);
    for (IndexedEntry<BooleanValue> entry : declTransl) {
        groundValue.set(entry.index(), BooleanConstant.TRUE);
        sum(decls, expr, currentDecl + 1, factory.and(entry.value(), declConstraints), values);
        groundValue.set(entry.index(), BooleanConstant.FALSE);
    }
    env = env.parent();
}
 
Example #5
Source File: FOL2BoolTranslator.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * Translates the given existentially quantified formula as follows 
 * (where A_0...A_|A| stand for boolean variables that represent the 
 * tuples of the expression A, etc.):
 * let quantFormula = "some 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|))
 * @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.TRUE initially
 * @param acc the accumulator that contains the top level conjunction; should be an empty OR accumulator initially
 * @ensures the given accumulator contains the translation of the formula "some decls | formula"
 */
private void some(Decls decls, Formula formula, int currentDecl, BooleanValue declConstraints, BooleanAccumulator acc) {
	if (acc.isShortCircuited()) return;
	final BooleanFactory factory = interpreter.factory();

	if (decls.size()==currentDecl) {
		acc.add(factory.and(declConstraints, formula.accept(this)));
		return;
	}

	final Decl decl = decls.get(currentDecl);
	final BooleanMatrix declTransl = visit(decl);
	final BooleanMatrix groundValue = factory.matrix(declTransl.dimensions());
	env = env.extend(decl.variable(), groundValue);
	for(IndexedEntry<BooleanValue> entry : declTransl) {
		groundValue.set(entry.index(), BooleanConstant.TRUE);
		some(decls, formula, currentDecl+1, factory.and(entry.value(), declConstraints), acc);
		groundValue.set(entry.index(), BooleanConstant.FALSE);	
	}
	env = env.parent();

}
 
Example #6
Source File: EvaluatorTest.java    From kodkod with MIT License 6 votes vote down vote up
@Test
public final void testComprehension() {
	final Variable[] vars = new Variable[3];
	final Decl[] decls = new Decl[3];
	for (int i = 0; i < 3; i++) {
		Variable v = Variable.unary("v"+i);
		Decl d = v.oneOf(person);
		vars[i] = v;
		decls[i] = d;
	}

	// {v0: Person | no v0.shaken} = univ - shaken.Person
	assertEquals(eval(vars[0].join(shaken).no().comprehension(decls[0])), eval(univ.difference(shaken.join(person))));
	// {v0, v1: Person | v1 in v0.shaken} = shaken
	assertEquals(eval(vars[1].in(vars[0].join(shaken)).comprehension(decls[0].and(decls[1]))),
			value(shaken));
	// {v0, v1, v2: Person | no v1.shaken} = Person->(univ - shaken.Person)->Person
	assertEquals(eval(vars[1].join(shaken).no().comprehension(decls[0].and(decls[1]).and(decls[2]))),
			eval(person.product(univ.difference(shaken.join(person))).product(person)));
}
 
Example #7
Source File: FOL2BoolTranslator.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * Translates the given sum expression as follows 
 * (where A_0...A_|A| stand for boolean variables that represent the 
 * tuples of the expression A, etc.):
 * let sum = "sum a: A, b: B, ..., x: X | IE(a, b, ..., x) " |
 *     sum a: A, b: B, ..., x: X | if (a in A && b in B && ... && x in X) then IE(a, b, ..., x) else 0 }.
 * @param decls intexpr 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.TRUE initially
 * @param values integer values computed so far
 */
private final void sum(Decls decls, IntExpression expr, int currentDecl, BooleanValue declConstraints,
		List<Int> values) {
	final BooleanFactory factory = interpreter.factory();
	if (decls.size()==currentDecl) {
		values.add( expr.accept(this).choice(declConstraints, factory.integer(0)) );
		return;
	}

	final Decl decl = decls.get(currentDecl);
	final BooleanMatrix declTransl = visit(decl);
	final BooleanMatrix groundValue = factory.matrix(declTransl.dimensions());
	env = env.extend(decl.variable(), groundValue);
	for(IndexedEntry<BooleanValue> entry : declTransl) {
		groundValue.set(entry.index(), BooleanConstant.TRUE);
		sum(decls, expr, currentDecl+1, factory.and(entry.value(), declConstraints), values);
		groundValue.set(entry.index(), BooleanConstant.FALSE);	
	}
	env = env.parent();
}
 
Example #8
Source File: FOL2BoolTranslator.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * 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|))
 * @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) {
		acc.add(factory.or(declConstraints, formula.accept(this)));
		return;
	}

	final Decl decl = decls.get(currentDecl);
	final BooleanMatrix declTransl = visit(decl);
	final BooleanMatrix groundValue = factory.matrix(declTransl.dimensions());
	env = env.extend(decl.variable(), groundValue);
	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();
	
}
 
Example #9
Source File: FOL2BoolTranslator.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * 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 initially
 * @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()) {
		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(), 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();
}
 
Example #10
Source File: EvaluatorTest.java    From kodkod with MIT License 6 votes vote down vote up
@Test
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)));
}
 
Example #11
Source File: TranslateKodkodToJava.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visit(Decl x) {
    String newname = makename(x);
    if (newname == null)
        return;
    String v = make(x.variable());
    String e = make(x.expression());
    switch (x.multiplicity()) {
        case LONE :
            file.printf("Decls %s=%s.loneOf(%s);%n", newname, v, e);
            break;
        case ONE :
            file.printf("Decls %s=%s.oneOf(%s);%n", newname, v, e);
            break;
        case SOME :
            file.printf("Decls %s=%s.someOf(%s);%n", newname, v, e);
            break;
        case SET :
            file.printf("Decls %s=%s.setOf(%s);%n", newname, v, e);
            break;
        default :
            throw new RuntimeException("Unknown kodkod multiplicity \"" + x.multiplicity() + "\" encountered");
    }
}
 
Example #12
Source File: SkolemizationTest.java    From kodkod with MIT License 6 votes vote down vote up
@Test
public final void testDeepSkolems() {
	solver.options().setSkolemDepth(3);
	testDeepSkolems(Multiplicity.ONE);
	testDeepSkolems(Multiplicity.LONE);
	testDeepSkolems(Multiplicity.SOME);
	testDeepSkolems(Multiplicity.SET);
	final Variable va = Variable.unary("va");
	final Variable vb = Variable.unary("vb");
	Decl da1 = va.oneOf(r1a);
	Decl db = vb.oneOf(r1b);
	final Formula f0 = va.in(vb.join(r2b));
	final Formula f1 = f0.forAll(db).not().forAll(da1);
	final Formula f2 = f0.forSome(db).forSome(da1);
	Instance inst = solve(f1.and(f2));
	assertEquals(bounds.relations().size()+3, inst.relations().size());
}
 
Example #13
Source File: AbstractReplacer.java    From kodkod with MIT License 6 votes vote down vote up
/** 
 * Calls lookup(decls) and returns the cached value, if any.  
 * If a replacement has not been cached, visits each of the children's 
 * variable and expression.  If nothing changes, the argument is cached and
 * returned, otherwise a replacement Decls object is cached and returned.
 * @return { d: Decls | d.size = decls.size && 
 *                      all i: [0..d.size) | d.declarations[i] = decls.declarations[i].accept(this) } 
 */
public Decls visit(Decls decls) { 
	Decls ret = lookup(decls);
	if (ret!=null) return ret;
	
	Decls visitedDecls = null;
	boolean allSame = true;
	for(Decl decl : decls) {
		Decls newDecl = visit(decl);
		if (newDecl != decl) 
			allSame = false;
		visitedDecls = (visitedDecls==null) ? newDecl : visitedDecls.and(newDecl);
	}
	ret = allSame ? decls : visitedDecls;
	return cache(decls, ret);
}
 
Example #14
Source File: FOL2BoolTranslator.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #15
Source File: AnnotatedNode.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Visits the given comprehension, quantified formula, or sum expression. The
 * method returns TRUE if the creator body contains any variable not bound by
 * the decls; otherwise returns FALSE.
 */
private Boolean visit(Node creator, Decls decls, Node body) {
    Boolean ret = lookup(creator);
    if (ret != null)
        return ret;
    boolean retVal = false;
    for (Decl decl : decls) {
        retVal = decl.expression().accept(this) || retVal;
        varsInScope.push(decl.variable());
    }
    retVal = ((Boolean) body.accept(this)) || retVal;
    for (int i = decls.size(); i > 0; i--) {
        varsInScope.pop();
    }
    return cache(creator, retVal);
}
 
Example #16
Source File: AbstractReplacer.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Calls lookup(decls) and returns the cached value, if any. If a replacement
 * has not been cached, visits each of the children's variable and expression.
 * If nothing changes, the argument is cached and returned, otherwise a
 * replacement Decls object is cached and returned.
 *
 * @return { d: Decls | d.size = decls.size && all i: [0..d.size) |
 *         d.declarations[i] = decls.declarations[i].accept(delegate) }
 */
@Override
public Decls visit(Decls decls) {
    Decls ret = lookup(decls);
    if (ret != null)
        return ret;

    Decls visitedDecls = null;
    boolean allSame = true;
    for (Decl decl : decls) {
        Decls newDecl = visit(decl);
        if (newDecl != decl)
            allSame = false;
        visitedDecls = (visitedDecls == null) ? newDecl : visitedDecls.and(newDecl);
    }
    ret = allSame ? decls : visitedDecls;
    return cache(decls, ret);
}
 
Example #17
Source File: AbstractVoidVisitor.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Visits all the children of the given declarations node if
 * this.visited(decls) returns false.  Otherwise does nothing.
 * @ensures all d: declarations.declarations | d.variable.accept(this) && d.expression.accept(this)
 */
public void visit(Decls decls) {
	if (visited(decls)) return;
	for (Decl decl : decls) {
		decl.accept(this);
	}
}
 
Example #18
Source File: AbstractReplacer.java    From kodkod with MIT License 5 votes vote down vote up
/** 
 * Calls lookup(decl) and returns the cached value, if any.  
 * If a replacement has not been cached, visits the declaration's 
 * variable and expression.  If nothing changes, the argument is cached and
 * returned, otherwise a replacement Decl object is cached and returned.
 * @return { d: Declaration |  d.variable = declaration.variable.accept(this) && 
 *                             d.multiplicity = decl.multiplicity && 
 *                             d.expression = declaration.expression.accept(this) 
 */
public Decl visit(Decl decl) {
	Decl ret = lookup(decl);
	if (ret!=null) return ret;
	
	final Variable variable = (Variable) decl.variable().accept(this);
	final Expression expression = decl.expression().accept(this);
	ret = (variable==decl.variable() && expression==decl.expression()) ?
		  decl : variable.declare(decl.multiplicity(), expression); 
	return cache(decl,ret);
}
 
Example #19
Source File: FOL2BoolTranslator.java    From kodkod with MIT License 5 votes vote down vote up
/** 
 * Calls lookup(decl) and returns the cached value, if any.  
 * If a translation has not been cached, translates decl.expression,
 * calls cache(...) on it and returns it.
 * @return let t = lookup(decl) | 
 *   some t => t, cache(decl, decl.expression.accept(this))
 */
public final BooleanMatrix visit(Decl decl) {
	BooleanMatrix matrix = lookup(decl);
	if (matrix!=null) return matrix;
	if (decl.multiplicity()!=Multiplicity.ONE)
		throw new HigherOrderDeclException(decl);
	return cache(decl, decl.expression().accept(this));
}
 
Example #20
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
private final void doTestAleks_03102013() {
    Relation r = Relation.unary("R");
    Relation s = Relation.binary("f");
    Variable v = Variable.unary("e");
    Decl decl = v.oneOf(r);
    Expression shared = v.join(s);
    Formula expr = (shared.difference(shared)).one().forAll(decl);

    Formula fin = expr.and(expr.not());

    List<Object> atomlist = new LinkedList<Object>();
    atomlist.add("R$0");
    atomlist.add("R$1");
    atomlist.add("R$2");

    Universe universe = new Universe(atomlist);
    TupleFactory factory = universe.factory();
    Bounds bounds = new Bounds(universe);

    bounds.bound(r, factory.allOf(1));
    bounds.bound(s, factory.allOf(2));

    Solver solver = new Solver();
    solver.options().setSolver(SATFactory.DefaultSAT4J);
    solver.options().setBitwidth(4);
    solver.options().setSkolemDepth(0);
    solver.options().setLogTranslation(0);
    Solution sol = solver.solve(fin, bounds);
    assertNull(sol.instance());
}
 
Example #21
Source File: HOLSome4AllTest.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
@Test
public void testE9i() {
    // SAT: some s: ints |
    // s > 3 || (some ns: set Node | #ns > s + 3)
    Formula cnd = si.gt(I3);
    QuantifiedFormula innerSomeQF = (QuantifiedFormula) ns.count().gt(si.plus(I3)).forSome(ns.setOf(Node));
    Decl someDecls = s.oneOf(Expression.INTS);
    Formula f = cnd.or(innerSomeQF).forSome(someDecls);
    Solution sol = solve(f);
    assertTrue(sol.sat());
    assertEquals(-1, evalS(sol));
}
 
Example #22
Source File: SkolemizationTest.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
private final void testDeepSkolems(Multiplicity mult) {
    final Variable va = Variable.unary("va");
    final Variable vb = Variable.unary("vb");
    final Variable vc = Variable.unary("vc");
    final Variable vd = Variable.unary("vd");
    final Set<String> skolems = new HashSet<String>(4);

    Decl da1 = va.oneOf(r1a);
    Decl db = vb.declare(mult, r1b);
    Decl dc = vc.declare(mult, r1a);
    Decl dc1 = vc.oneOf(r1a);
    Decl dd = vd.declare(mult, r1b);
    Decl dd1 = vd.oneOf(r1b);

    skolems.add("$" + vb.name());

    Instance inst = solve(va.in(vb.join(r2b)).forSome(db).forAll(da1));
    assertSkolems(bounds, inst, skolems);

    skolems.add("$" + vc.name());
    inst = solve(va.in(vb.join(r2b).union(vd.join(r2b)).union(vc)).forSome(db).forAll(da1).forSome(dc).forAll(dd1));
    assertSkolems(bounds, inst, skolems);

    inst = solve(va.in(vb.join(r2b).union(vd.join(r2b)).union(vc)).forSome(db).forSome(dc).forAll(da1.and(dd1)));
    assertSkolems(bounds, inst, skolems);

    skolems.add("$" + vd.name());
    inst = solve(va.in(vb.join(r2b).union(vd.join(r2b)).union(vc)).forSome(db).forAll(da1).forSome(dc).not().forAll(dd).not());

    assertSkolems(bounds, inst, skolems);

    inst = solve(va.in(vb.join(r2b).union(vd.join(r2b)).union(vc)).forAll(dc).forAll(db).forSome(da1).not().forAll(dd1));
    skolems.remove("$" + vd.name());
    assertSkolems(bounds, inst, skolems);

    inst = solve(va.in(vb.join(r2b).union(vd.join(r2b)).union(vc)).forSome(db).forAll(dc1).forAll(da1).forAll(dd1));
    skolems.remove("$" + vc.name());
    assertSkolems(bounds, inst, skolems);

}
 
Example #23
Source File: HOLSome4AllTest.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
@Test
public void testE9() {
    // UNSAT: some s: ints - (-1) |
    // s > 3 || (some ns: set Node | #ns > s + 3)
    Formula cnd = si.gt(I3);
    QuantifiedFormula innerSomeQF = (QuantifiedFormula) ns.count().gt(si.plus(I3)).forSome(ns.setOf(Node));
    Decl someDecls = s.oneOf(Expression.INTS.difference(M1.toExpression()));
    Formula f = cnd.or(innerSomeQF).forSome(someDecls);
    assertFalse(solve(f).sat());
}
 
Example #24
Source File: AbstractCollector.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Calls lookup(decls) and returns the cached value, if any.
 * If no cached value exists, visits each child, caches the 
 * union of the sets returned by the children and returns it.
 * @return let x = lookup(decls) | 
 *          x != null => x,  
 *          cache(decls, decls.declarations[0].accept(this) +...+ decls.declarations[decls.size-1].accept(this)) 
 */
public Set<T> visit(Decls decls) {
	Set<T> ret = lookup(decls);
	if (ret!=null) return ret;
	ret = newSet();
	for(Decl d: decls) {
		ret.addAll(d.accept(this));
	}
	return cache(decls, ret);
}
 
Example #25
Source File: AbstractDetector.java    From kodkod with MIT License 5 votes vote down vote up
/** 
 * Calls lookup(decls) 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(decls) | 
 *          x != null => x,  
 *          cache(decls, some d: decls.declarations | d.accept(this)) 
 */
public Boolean visit(Decls decls) {
	final Boolean ret = lookup(decls);
	if (ret!=null) return ret;
	for(Decl d : decls) {
		if (visit(d))
			return cache(decls, true);
	}
	return cache(decls, false);
}
 
Example #26
Source File: Skolemizer.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Constructs a skolem replacer from the given arguments. 
 */
private Skolemizer(AnnotatedNode<Formula> annotated, Bounds bounds, Options options) {
	super(annotated.sharedNodes());

	// only cache intermediate computations for expressions with no free variables
	// and formulas with no free variables and no quantified descendants
	
	for(Node n: annotated.sharedNodes()) {
		final AbstractDetector fvdetect = annotated.freeVariableDetector();
		final AbstractDetector qdetect = annotated.quantifiedFormulaDetector();
		if (!(Boolean)n.accept(fvdetect)) {
			if (!(n instanceof Formula) || !((Boolean)n.accept(qdetect)))
				this.cache.put(n, null);
		}
	}
	this.reporter = options.reporter();
	this.bounds = bounds;
	this.interpreter = LeafInterpreter.overapproximating(bounds, options);
	this.repEnv = Environment.empty();
	this.nonSkolems = new ArrayList<DeclInfo>();
	this.nonSkolemsView = new AbstractList<Decl>() {
		public Decl get(int index) { return nonSkolems.get(index).decl;	}
		public int size() { return nonSkolems.size(); }
	};
	this.topSkolemConstraints = new ArrayList<Formula>();
	this.negated = false;
	this.skolemDepth = options.skolemDepth();
}
 
Example #27
Source File: SkolemizationTest.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
private final void testNoSkolems(Multiplicity mult) {
    final Variable v = Variable.unary("v");
    final Decl d = v.declare(mult, r1a);

    testNoSkolems(d, v.join(r2a).some().forAll(d).not());
    testNoSkolems(d, v.join(r2a).some().forSome(d));

}
 
Example #28
Source File: Skolemizer.java    From kodkod with MIT License 5 votes vote down vote up
/**
	 * 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 = itr.next().decl;
		while(itr.hasNext()) {
			rangeDecls = rangeDecls.and(itr.next().decl);
		}
//		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));	
	}
 
Example #29
Source File: AnnotatedNode.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
@Override
public Boolean visit(Decl decl) {
    Boolean ret = lookup(decl);
    if (ret != null)
        return ret;
    return cache(decl, decl.expression().accept(this));
}
 
Example #30
Source File: SkolemizationTest.java    From kodkod with MIT License 5 votes vote down vote up
private final void testNoSkolems(Multiplicity mult) {
	final Variable v = Variable.unary("v");
	final Decl d = v.declare(mult, r1a);
	
	testNoSkolems(d, v.join(r2a).some().forAll(d).not());
	testNoSkolems(d, v.join(r2a).some().forSome(d));
	
}