Java Code Examples for kodkod.ast.Variable#declare()

The following examples show how to use kodkod.ast.Variable#declare() . 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: AbstractReplacer.java    From org.alloytools.alloy with Apache License 2.0 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(delegate)
 *         && d.multiplicity = decl.multiplicity && d.expression =
 *         declaration.expression.accept(delegate)
 */
@Override
public Decl visit(Decl decl) {
    Decl ret = lookup(decl);
    if (ret != null)
        return ret;

    final Variable variable = (Variable) decl.variable().accept(delegate);
    final Expression expression = decl.expression().accept(delegate);
    ret = (variable == decl.variable() && expression == decl.expression()) ? decl : variable.declare(decl.multiplicity(), expression);
    return cache(decl, ret);
}
 
Example 2
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 3
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 4
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 5
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));
	
}
 
Example 6
Source File: SkolemizationTest.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
private final void testSkolems(Multiplicity mult) {
    final Variable va = Variable.unary("va");
    final Variable vb = Variable.unary("vb");
    final Set<String> skolems = new HashSet<String>(4);

    Decl da = va.declare(mult, r1a);
    Decl db = vb.declare(mult, r1b);

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

    Instance inst = solve(va.in(r1b.join(r2b)).forAll(da).not());
    assertSkolems(bounds, inst, skolems);

    inst = solve((r2b.some().implies(va.in(r1b.join(r2b)).forAll(da))).not());
    assertSkolems(bounds, inst, skolems);

    inst = solve(va.in(r1b.join(r2b)).forSome(da));
    assertSkolems(bounds, inst, skolems);

    inst = solve(va.in(r1b.join(r2b)).forSome(da).and(va.in(r1b).not().forAll(mult == Multiplicity.ONE ? da : va.oneOf(r1a))));
    assertSkolems(bounds, inst, skolems);
    final Expression e0 = va.join(r2a);
    final Formula f0 = e0.some().forSome(va.oneOf(r1a));
    final Formula f1 = e0.no().forAll(va.oneOf(r2a.join(r1b)));

    inst = solve(f0.and(f1));
    assertSkolems(bounds, inst, skolems);

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

    inst = solve(va.in(vb.join(r2b)).forSome(da.and(vb.oneOf(va.join(r2a)))));
    assertSkolems(bounds, inst, skolems);

    inst = solve((va.in(vb.join(r2b)).forAll(vb.oneOf(va.join(r2a))).forAll(da)).not());
    assertSkolems(bounds, inst, skolems);

    inst = solve(va.in(vb.join(r2b)).forAll(da.and(db)).not());
    assertSkolems(bounds, inst, skolems);

    inst = solve(va.in(vb.join(r2b)).forSome(da.and(db)));
    assertSkolems(bounds, inst, skolems);

    inst = solve(va.in(r1b.join(r2b)).forSome(da).and(r1b.in(vb).forAll(db).not()));
    assertSkolems(bounds, inst, skolems);

}
 
Example 7
Source File: SkolemizationTest.java    From kodkod with MIT License 4 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 8
Source File: JenaTranslator.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
private Pair<Formula, Decls> scopeInternal(Formula f, Expression dynamicBinding, Collection<Variable> projectedVars, Collection<Variable> outerBound) {
	Decls ds = null, eds = null;
	Set<Variable> used = ASTUtils.gatherVariables(f);
	if (dynamicBinding != null) {
		used.addAll(ASTUtils.gatherVariables(dynamicBinding));
	}
	List<Variable> orderedVars = sortVars(used);
	for(Variable v : orderedVars) {
		Expression r;
		if (context.getDomain(v) != null ) {
			r = context.getDomain(v).bound();
			if (r == null) {
				r = NULL;
			}
		} else {
			r = NULL;
		}
		if (outerBound.contains(v)) {	
			continue;
		}
		Decl d = v.declare(Multiplicity.ONE, context.getStaticBinding().contains(v)? r: r.union(NULL));
		if (! projectedVars.contains(v)) {
			eds = eds==null? d: eds.and(d);
		} else if (ds == null) {
			ds = d;
		} else {
			ds = ds.and(d);
		}
		if (! context.getStaticBinding().contains(v)) {
			if (context.getDynamicBinding() == null) {
				f = f.and(v.eq(NULL));					
			} else {
				f = f.and(varExpr(v).in(context.getDynamicBinding()).or(v.eq(NULL)));
			}
		}
	}

	if (eds != null) {
		f = f.forSome(eds);
	}
	
	if (DEBUG) System.err.println("scope: " + eds + " -- " + ds);
	
	return Pair.make(f, ds);
}
 
Example 9
Source File: SkolemizationTest.java    From kodkod with MIT License 2 votes vote down vote up
private final void testSkolems(Multiplicity mult) {
	final Variable va = Variable.unary("va");
	final Variable vb = Variable.unary("vb");
	final Set<String> skolems = new HashSet<String>(4);
	
	Decl da = va.declare(mult, r1a);
	Decl db = vb.declare(mult, r1b);
	
	skolems.add("$"+va.name());
	
	Instance inst = solve(va.in(r1b.join(r2b)).forAll(da).not());
	assertSkolems(bounds, inst, skolems);
	
	inst = solve((r2b.some().implies(va.in(r1b.join(r2b)).forAll(da))).not());
	assertSkolems(bounds, inst, skolems);
	
	inst = solve(va.in(r1b.join(r2b)).forSome(da));
	assertSkolems(bounds, inst, skolems);
	
	inst = solve(va.in(r1b.join(r2b)).forSome(da).and(va.in(r1b).not().forAll(mult==Multiplicity.ONE ? da : va.oneOf(r1a))));
	assertSkolems(bounds, inst, skolems);
	final Expression e0 = va.join(r2a);
	final Formula f0 = e0.some().forSome(va.oneOf(r1a));
	final Formula f1 = e0.no().forAll(va.oneOf(r2a.join(r1b)));
	
	inst = solve(f0.and(f1));
	assertSkolems(bounds, inst, skolems);
	
	skolems.add("$"+vb.name());
	
	inst = solve(va.in(vb.join(r2b)).forSome(da.and(vb.oneOf(va.join(r2a)))));
	assertSkolems(bounds, inst, skolems);
	
	inst = solve((va.in(vb.join(r2b)).forAll(vb.oneOf(va.join(r2a))).forAll(da)).not());
	assertSkolems(bounds, inst, skolems);
	
	inst = solve(va.in(vb.join(r2b)).forAll(da.and(db)).not());
	assertSkolems(bounds, inst, skolems);
	
	inst = solve(va.in(vb.join(r2b)).forSome(da.and(db)));
	assertSkolems(bounds, inst, skolems);
	
	inst = solve(va.in(r1b.join(r2b)).forSome(da).and(r1b.in(vb).forAll(db).not()));
	assertSkolems(bounds, inst, skolems);
	
	
	
}