Java Code Examples for kodkod.ast.Expression#union()

The following examples show how to use kodkod.ast.Expression#union() . 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: Bigconfig.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the connectedSites predicate.
 *
 * @return pred connectedSites(sites: set Site) { -- all sites in the given set
 *         are connected to each other all s: sites | sites - s in
 *         ((site.s).^link).site }
 */
public Formula connectedSites(Expression sites) {
    final Variable s = Variable.unary("s");
    Expression closed;
    if (closureApprox > 0) {
        closed = link;
        for (int i = 1; i < closureApprox; i *= 2) {
            closed = closed.union(closed.join(closed));
        }
    } else {
        closed = link.closure();
    }

    final Expression sreachable = site.join(s).join(closed).join(site);
    final Formula f = sites.difference(s).in(sreachable);
    return f.forAll(s.oneOf(sites));
}
 
Example 2
Source File: Bigconfig.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * Returns the connectedSites predicate.
 * @return pred connectedSites(sites: set Site) {
 *  -- all sites in the given set are connected to each other  
 *  all s: sites | sites - s in ((site.s).^link).site
 *  }
 */
public Formula connectedSites(Expression sites) {
	final Variable s = Variable.unary("s");
	Expression closed;
	if (closureApprox > 0) {
		closed = link;
		for(int i = 1; i < closureApprox; i *= 2) {
			closed = closed.union(closed.join(closed));
		}
	} else {
		closed = link.closure();
	}
	
	final Expression sreachable = site.join(s).join(closed).join(site);
	final Formula f = sites.difference(s).in(sreachable);
	return f.forAll(s.oneOf(sites));
}
 
Example 3
Source File: Transpose4x4UnaryL.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Converts the given integer array to a binary relation representation. 
 * @return 0->val[0] + 1->val[1] + ... + (val.length-1)->val[val.length-1]
 */
final static Expression toExpr(int[] val) {
	final Expression[] exprs = new Expression[val.length];
	for(int i = 0; i < val.length; i++)
		exprs[i] = ints[i].product(ints[val[i]]);
	return Expression.union(exprs);
}
 
Example 4
Source File: JenaTranslator.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Expression bound() {
	Expression e = domains[0].bound();
	for(int i = 1; i < domains.length; i++) {
		e = e.union(domains[i].bound());
	}
	return e;
}
 
Example 5
Source File: RegressionTests.java    From kodkod with MIT License 5 votes vote down vote up
@Test
	public final void testMarceloSimplified_041912() {
		
		final Relation d2 = Relation.unary("Domain_2");
		final Relation a1 = Relation.unary("Address_1");
		final Relation a2 = Relation.unary("Address_2");
		final Relation a3 = Relation.unary("Address_3");
		
		final Expression e = Expression.union(a1, a2, a3);
		
		final Expression dstBinding = 
			Expression.union(Expression.product(d2, a1, a3), Expression.product(d2, a3, a3));
		final Formula f = a3.in(e.product(a1).override(d2.join(dstBinding)).join(e));
		
		final Universe u = new Universe("a1", "a2", "a3", "d2");
		final TupleFactory tf = u.factory();
		final Bounds b = new Bounds(u);
		b.boundExactly(a1, tf.setOf("a1"));
		b.boundExactly(a2, tf.setOf("a2"));
		b.boundExactly(a3, tf.setOf("a3"));
		b.bound(d2, tf.setOf("d2"));
		
		final Solver solver = new Solver();
		solver.options().setSolver(SATFactory.MiniSat);
		
//		System.out.println(f);
//		System.out.println(b);
		
		final Solution sol = solver.solve(f, b);

//		System.out.println(sol);
		assertNotNull(sol.instance());
	}
 
Example 6
Source File: ListSynth.java    From kodkod with MIT License 5 votes vote down vote up
Expression meaning() {
	return Expression.union(
			nil.product(nil), 
			headStx.product(thisList.join(head)),
			nearNode0Stx.product(nearNode0()), 
			midNode0Stx.product(midNode0()),
			farNode0Stx.product(farNode0()));
}
 
Example 7
Source File: Transpose4x4UnaryLR.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Converts the given integer array to a binary relation representation. 
 * @return 0->val[0] + 1->val[1] + ... + (val.length-1)->val[val.length-1]
 */
final static Expression toExpr(int[] val) {
	final Expression[] exprs = new Expression[val.length];
	for(int i = 0; i < val.length; i++)
		exprs[i] = ints[i].product(ints[val[i]]);
	return Expression.union(exprs);
}
 
Example 8
Source File: Transpose4x4UnaryLR.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Encodes the result of reading a subarray of length 4 from the given array, starting at the given index.
 * @requires m.arity = 2 and pos.arity = 1
 * @return 0->m[pos] + 1->m[pos+1] + 2->[pos+2] + 3->[pos+3]
 */
final Expression rd4(Expression m, Expression pos) {
	return Expression.union(ints[0].product(get(m, pos)), 
							ints[1].product(get(m, add(pos, 1))), 
							ints[2].product(get(m, add(pos, 2))), 
							ints[3].product(get(m, add(pos, 3))));
}
 
Example 9
Source File: Transpose4x4UnaryLR.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Encodes the shufps SIMD instruction.
 * @requires xmm1.arity = 2 and xmm2.arity = 2 and imm8.length = 4 
 * @requires all i: [0..3] | imm8[i].arity = 1
 * @return 0->xmm1[imm8[0]] + 1->xmm1[imm8[1]] + 2->xmm2[imm8[2]] + 3->xmm2[imm8[3]]
 */
final Expression shufps(Expression xmm1, Expression xmm2, Expression[] imm8) {
	return Expression.union(ints[0].product(get(xmm1, imm8[0])), 
							ints[1].product(get(xmm1, imm8[1])), 
							ints[2].product(get(xmm2, imm8[2])), 
							ints[3].product(get(xmm2, imm8[3])));
}
 
Example 10
Source File: Bounds.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
public Expression ts2expr(TupleSet tset) {
    if (tset == null)
        return Expression.NONE;
    Expression tsetExpr = null;
    for (Tuple t : tset) {
        Expression tupleExpr = null;
        for (int i = 0; i < t.arity(); i++) {
            Expression atomRel = ensureAtomExpr(t.atom(i));
            tupleExpr = tupleExpr == null ? atomRel : tupleExpr.product(atomRel);
        }
        tsetExpr = tsetExpr == null ? tupleExpr : tsetExpr.union(tupleExpr);
    }
    return (tsetExpr == null) ? Expression.NONE : tsetExpr;
}
 
Example 11
Source File: Transpose4x4UnaryL.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Encodes the result of reading a subarray of length 4 from the given array, starting at the given index.
 * @requires m.arity = 2 and pos.arity = 1
 * @return 0->m[pos] + 1->m[pos+1] + 2->[pos+2] + 3->[pos+3]
 */
final Expression rd4(Expression m, Expression pos) {
	return Expression.union(ints[0].product(get(m, pos)), 
							ints[1].product(get(m, add(pos, 1))), 
							ints[2].product(get(m, add(pos, 2))), 
							ints[3].product(get(m, add(pos, 3))));
}
 
Example 12
Source File: Transpose4x4UnaryL.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Encodes the shufps SIMD instruction.
 * @requires xmm1.arity = 2 and xmm2.arity = 2 and imm8.length = 4 
 * @requires all i: [0..3] | imm8[i].arity = 1
 * @return 0->xmm1[imm8[0]] + 1->xmm1[imm8[1]] + 2->xmm2[imm8[2]] + 3->xmm2[imm8[3]]
 */
final Expression shufps(Expression xmm1, Expression xmm2, Expression[] imm8) {
	return Expression.union(ints[0].product(get(xmm1, imm8[0])), 
							ints[1].product(get(xmm1, imm8[1])), 
							ints[2].product(get(xmm2, imm8[2])), 
							ints[3].product(get(xmm2, imm8[3])));
}
 
Example 13
Source File: BoundsComputer.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/** Allocate relations for SubsetSig top-down. */
private Expression allocateSubsetSig(SubsetSig sig) throws Err {
    // We must not visit the same SubsetSig more than once, so if we've been
    // here already, then return the old value right away
    Expression sum = sol.a2k(sig);
    if (sum != null && sum != Expression.NONE)
        return sum;
    // Recursively form the union of all parent expressions
    TupleSet ts = factory.noneOf(1);
    for (Sig parent : sig.parents) {
        Expression p = (parent instanceof PrimSig) ? sol.a2k(parent) : allocateSubsetSig((SubsetSig) parent);
        ts.addAll(sol.query(true, p, false));
        if (sum == null)
            sum = p;
        else
            sum = sum.union(p);
    }
    // If subset is exact, then just use the "sum" as is
    if (sig.exact) {
        sol.addSig(sig, sum);
        return sum;
    }
    // Allocate a relation for this subset sig, then bound it
    rep.bound("Sig " + sig + " in " + ts + "\n");
    Relation r = sol.addRel(sig.label, null, ts);
    sol.addSig(sig, r);
    // Add a constraint that it is INDEED a subset of the union of its
    // parents
    sol.addFormula(r.in(sum), sig.isSubset);
    return r;
}
 
Example 14
Source File: BoundsComputer.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
/**
 * If ex is a simple combination of Relations, then return that combination,
 * else return null.
 */
private Expression sim(Expr ex) {
    while (ex instanceof ExprUnary) {
        ExprUnary u = (ExprUnary) ex;
        if (u.op != ExprUnary.Op.NOOP && u.op != ExprUnary.Op.EXACTLYOF)
            break;
        ex = u.sub;
    }
    if (ex instanceof ExprBinary) {
        ExprBinary b = (ExprBinary) ex;
        if (b.op == ExprBinary.Op.ARROW || b.op == ExprBinary.Op.PLUS || b.op == ExprBinary.Op.JOIN) {
            Expression left = sim(b.left);
            if (left == null)
                return null;
            Expression right = sim(b.right);
            if (right == null)
                return null;
            if (b.op == ExprBinary.Op.ARROW)
                return left.product(right);
            if (b.op == ExprBinary.Op.PLUS)
                return left.union(right);
            else
                return left.join(right);
        }
    }
    if (ex instanceof ExprConstant) {
        switch (((ExprConstant) ex).op) {
            case EMPTYNESS :
                return Expression.NONE;
        }
    }
    if (ex == Sig.NONE)
        return Expression.NONE;
    if (ex == Sig.SIGINT)
        return Expression.INTS;
    if (ex instanceof Sig)
        return sol.a2k((Sig) ex);
    if (ex instanceof Field)
        return sol.a2k((Field) ex);
    return null;
}
 
Example 15
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
public final void testFelix_11122006() {
    Relation x0 = Relation.nary("Q", 1);
    Relation x1 = Relation.nary("B", 1);
    Relation x2 = Relation.nary("A", 1);
    Relation x3 = Relation.nary("QQ", 3);

    List<String> atomlist = Arrays.asList("A", "B", "Q");
    Universe universe = new Universe(atomlist);
    TupleFactory factory = universe.factory();
    Bounds bounds = new Bounds(universe);

    TupleSet x0_upper = factory.noneOf(1);
    x0_upper.add(factory.tuple("Q"));
    bounds.boundExactly(x0, x0_upper);

    TupleSet x1_upper = factory.noneOf(1);
    x1_upper.add(factory.tuple("B"));
    bounds.boundExactly(x1, x1_upper);

    TupleSet x2_upper = factory.noneOf(1);
    x2_upper.add(factory.tuple("A"));
    bounds.boundExactly(x2, x2_upper);

    TupleSet x3_upper = factory.noneOf(3);
    x3_upper.add(factory.tuple("Q").product(factory.tuple("A")).product(factory.tuple("A")));
    x3_upper.add(factory.tuple("Q").product(factory.tuple("B")).product(factory.tuple("B")));
    bounds.bound(x3, x3_upper);

    Expression x7 = x2.product(x2);
    Expression x8 = x0.join(x3);
    Formula x6 = x7.in(x8);
    Formula x5 = x6.not();

    Expression x18 = x1.product(x1);
    Expression x17 = x7.union(x18);
    Expression x16 = x0.product(x17);

    Formula x15 = x3.in(x16);
    Formula x4 = x5.and(x15);

    Solver solver = new Solver();

    solver.options().setSolver(SATFactory.DefaultSAT4J);
    solver.options().setBitwidth(4);
    solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);

    // System.out.println(bounds);
    // System.out.println(x4);
    Solution sol = solver.solve(x4, bounds);
    assertEquals(sol.outcome(), Solution.Outcome.SATISFIABLE);
    // System.out.println(sol.toString());
}
 
Example 16
Source File: RegressionTests.java    From kodkod with MIT License 4 votes vote down vote up
@Test
public final void testBGP_03172011() {

	Relation x5 = Relation.unary("s012");
	Relation x8 = Relation.unary("zero");
	Relation x9 = Relation.unary("one");
	Relation x12 = Relation.nary("next", 2);

	Universe universe = new Universe(Arrays.asList( "0", "1", "2", "3"));
	TupleFactory factory = universe.factory();
	Bounds bounds = new Bounds(universe);

	bounds.boundExactly(x5, factory.setOf("0","1","2"));
	bounds.boundExactly(x8, factory.setOf("0"));
	bounds.bound(x9, factory.setOf("1"), factory.setOf("1","2"));

	TupleSet x12_upper = factory.noneOf(2);
	x12_upper.add(factory.tuple("1","2"));
	x12_upper.add(factory.tuple("2","3"));
	bounds.boundExactly(x12, x12_upper);


	Variable x714 = Variable.unary("x714");
	Decls x713 =  x714.oneOf(x8.union(x9));

	Variable x720 = Variable.unary("x720");
	Expression x723 = x8.union(x9);
	Expression x724 = x9.join(x12);
	Expression x722 = x723.union(x724); 
	Expression x721 = x722.difference(x714);
	Decls x719 = x720.oneOf(x721);

	Variable x727 = Variable.unary("x727");
	Expression x732 = x714.union(x720);
	Expression x728 = x5.difference(x732);
	Decls x726 = x727.oneOf(x728);

	Variable x735 = Variable.unary("x735");
	Decls x734 = x735.oneOf(x8);

	Variable x893 = Variable.unary("x893");
	Decls x892 = x893.oneOf(x727);
	Formula x894 = x720.no();
	Formula x891 = x894.forAll(x892);

	Formula x712 = x891.forSome(x713.and(x719).and(x726).and(x734));
	Formula x267 = Formula.FALSE.or(x712); 

	Solver solver = new Solver();
	solver.options().setSolver(SATFactory.MiniSat);
	solver.options().setBitwidth(4);
	solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);
	solver.options().setSymmetryBreaking(20);
	solver.options().setSkolemDepth(0);

	final Solution sol = solver.solve(x267, bounds);
	assertEquals(sol.outcome(), Solution.Outcome.TRIVIALLY_UNSATISFIABLE);
}
 
Example 17
Source File: RegressionTests.java    From kodkod with MIT License 4 votes vote down vote up
@Test
public final void testFelix_11122006() {
	Relation x0 = Relation.nary("Q", 1);
	Relation x1 = Relation.nary("B", 1);
	Relation x2 = Relation.nary("A", 1);
	Relation x3 = Relation.nary("QQ", 3);

	List<String> atomlist = Arrays.asList("A", "B", "Q");
	Universe universe = new Universe(atomlist);
	TupleFactory factory = universe.factory();
	Bounds bounds = new Bounds(universe);

	TupleSet x0_upper = factory.noneOf(1);
	x0_upper.add(factory.tuple("Q"));
	bounds.boundExactly(x0, x0_upper);

	TupleSet x1_upper = factory.noneOf(1);
	x1_upper.add(factory.tuple("B"));
	bounds.boundExactly(x1, x1_upper);

	TupleSet x2_upper = factory.noneOf(1);
	x2_upper.add(factory.tuple("A"));
	bounds.boundExactly(x2, x2_upper);

	TupleSet x3_upper = factory.noneOf(3);
	x3_upper.add(factory.tuple("Q").product(factory.tuple("A")).product(factory.tuple("A")));
	x3_upper.add(factory.tuple("Q").product(factory.tuple("B")).product(factory.tuple("B")));
	bounds.bound(x3, x3_upper);

	Expression x7=x2.product(x2);
	Expression x8=x0.join(x3);
	Formula x6=x7.in(x8);
	Formula x5=x6.not();

	Expression x18=x1.product(x1);
	Expression x17=x7.union(x18);
	Expression x16=x0.product(x17);

	Formula x15=x3.in(x16);
	Formula x4=x5.and(x15);

	Solver solver = new Solver();

	solver.options().setSolver(SATFactory.DefaultSAT4J);
	solver.options().setBitwidth(4);
	solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);

	//		System.out.println(bounds);
	//		System.out.println(x4);
	Solution sol = solver.solve(x4,bounds);
	assertEquals(sol.outcome(), Solution.Outcome.SATISFIABLE);
	//		System.out.println(sol.toString());
}
 
Example 18
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
public final void testBGP_03172011() {

        Relation x5 = Relation.unary("s012");
        Relation x8 = Relation.unary("zero");
        Relation x9 = Relation.unary("one");
        Relation x12 = Relation.nary("next", 2);

        Universe universe = new Universe(Arrays.asList("0", "1", "2", "3"));
        TupleFactory factory = universe.factory();
        Bounds bounds = new Bounds(universe);

        bounds.boundExactly(x5, factory.setOf("0", "1", "2"));
        bounds.boundExactly(x8, factory.setOf("0"));
        bounds.bound(x9, factory.setOf("1"), factory.setOf("1", "2"));

        TupleSet x12_upper = factory.noneOf(2);
        x12_upper.add(factory.tuple("1", "2"));
        x12_upper.add(factory.tuple("2", "3"));
        bounds.boundExactly(x12, x12_upper);

        Variable x714 = Variable.unary("x714");
        Decls x713 = x714.oneOf(x8.union(x9));

        Variable x720 = Variable.unary("x720");
        Expression x723 = x8.union(x9);
        Expression x724 = x9.join(x12);
        Expression x722 = x723.union(x724);
        Expression x721 = x722.difference(x714);
        Decls x719 = x720.oneOf(x721);

        Variable x727 = Variable.unary("x727");
        Expression x732 = x714.union(x720);
        Expression x728 = x5.difference(x732);
        Decls x726 = x727.oneOf(x728);

        Variable x735 = Variable.unary("x735");
        Decls x734 = x735.oneOf(x8);

        Variable x893 = Variable.unary("x893");
        Decls x892 = x893.oneOf(x727);
        Formula x894 = x720.no();
        Formula x891 = x894.forAll(x892);

        Formula x712 = x891.forSome(x713.and(x719).and(x726).and(x734));
        Formula x267 = Formula.FALSE.or(x712);

        Solver solver = new Solver();
        solver.options().setSolver(SATFactory.MiniSat);
        solver.options().setBitwidth(4);
        // solver.options().setFlatten(false);
        solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);
        solver.options().setSymmetryBreaking(20);
        solver.options().setSkolemDepth(0);

        final Solution sol = solver.solve(x267, bounds);
        assertEquals(sol.outcome(), Solution.Outcome.TRIVIALLY_UNSATISFIABLE);
    }
 
Example 19
Source File: JenaTranslator.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void visit(OpTable arg0) {	
	Set<Variable> vars = HashSetFactory.make();
	Formula f = null;
	for(Iterator<Binding> bs = arg0.getTable().rows(); bs.hasNext(); ) {
		Formula rf = null;
		Binding b = bs.next();
		for(Var jv : arg0.getTable().getVars()) {
			if (b.get(jv) != null) {
				Expression value = toTerm(b.get(jv));
				Variable var = context.getVars().get(jv.getName());
				vars.add(var);
				Formula ef = var.eq(value);
				rf = rf==null? ef: rf.and(ef);
			}
		}
		f = f==null? rf: f.or(rf);
	}
	
	context.setCurrentQuery(f==null? Formula.TRUE: f);

	if (context.getStaticBinding() != null) {
		context.getStaticBinding().addAll(vars);
	} else {
		context.setStaticBinding(vars);
	}
	
	Expression bound = null;
	for(Variable v : vars) {
		bound = bound==null? varExpr(v): bound.union(varExpr(v));
	}
	if (bound != null) {
		if (context.getDynamicBinding() != null) {
			context.setDynamicBinding(context.getDynamicBinding().union(bound));
		} else {
			context.setDynamicBinding(bound);
		}
	}
	
	context.getCurrentContinuation().next(context, context.getCurrentQuery());
}