I'm still new to Z3, and hence not sure why I'm getting unsat for the formula below; it should be sat at least for those ts_var arrays, of which each array element (bitvector) (of the 32 array elements) has 1 in a different position of the 32 bits and zeros in all other positions (so the bvxor result will be different). So any advice or hints about what I'm doing wrong?
UPDATE: When I did the implication in exp4 ((=> a!1 a!2)) in the opposite way of what it is in the code, Z3 produced SAT! But this is not what I want. I want to find an array of which its different combinations of 2 elements gives different result when they are XORed together. Which is the implication in the code that still gives unsat.
(assert (exists ((ts_var (Array (_ BitVec 5) (_ BitVec 32))))
(forall ((k (_ BitVec 5)) (l (_ BitVec 5)) (m (_ BitVec 5)) (n (_ BitVec 5)))
(let ((a!1 (and (not (= k l))
(not (= n m))
(=> (= k m) (not (= l n)))
(=> (= l n) (not (= k m)))))
(a!2 (not (= (bvxor (select ts_var k) (select ts_var l))
(bvxor (select ts_var m) (select ts_var n))))))
(=> a!1 a!2)
)
)
)
)
(check-sat)
I originally wrote the code which gave this result using the C-API:
Z3_ast mk_var(Z3_context ctx, const char * name, Z3_sort ty)
{
Z3_symbol s = Z3_mk_string_symbol(ctx, name);
return Z3_mk_const(ctx, s, ty);
}
bv_w_sort = Z3_mk_bv_sort (ctx, 32);
index_w_sort = Z3_mk_bv_sort (ctx, 5);
array_sort = Z3_mk_array_sort(ctx, index_w_sort, bv_w_sort);
ts_var = mk_var(ctx, "ts_var" , array_sort);
fp1 = mk_var(ctx, "fp1" , bv_w_sort);
fp2 = mk_var(ctx, "fp2" , bv_w_sort);
fp1 = Z3_mk_bvxor(ctx, Z3_mk_select(ctx, ts_var, k) , Z3_mk_select(ctx, ts_var, l) );
fp2 = Z3_mk_bvxor(ctx, Z3_mk_select(ctx, ts_var, m) , Z3_mk_select(ctx, ts_var, n) );
cond_uniq = Z3_mk_not (ctx,Z3_mk_eq (ctx, fp1, fp2) );
cond_k_neq_l = Z3_mk_not (ctx,Z3_mk_eq (ctx, k, l));
cond_n_neq_m = Z3_mk_not (ctx,Z3_mk_eq (ctx, n, m));
cond_l_neq_n = Z3_mk_not (ctx,Z3_mk_eq (ctx, l, n));
cond_k_neq_m = Z3_mk_not (ctx,Z3_mk_eq (ctx, k, m));
cond_k_eq_m = Z3_mk_eq (ctx, k, m);
cond_l_eq_n = Z3_mk_eq (ctx, l, n);
cond_imply1 = Z3_mk_implies (ctx, cond_k_eq_m, cond_l_neq_n);
cond_imply2 = Z3_mk_implies (ctx, cond_l_eq_n, cond_k_neq_m);
args[0]= cond_k_neq_l;
args[1]= cond_n_neq_m;
args[2]= cond_imply1;
args[3]= cond_imply2;
exp4 = Z3_mk_and(ctx, 4, args);
bound[0] = (Z3_app) k;
bound[1] = (Z3_app) l;
bound[2] = (Z3_app) m;
bound[3] = (Z3_app) n;
bound4[0]= (Z3_app)ts_var;
exp2 = Z3_mk_implies(ctx, exp4, cond_uniq);
exp1 = Z3_mk_forall_const(ctx, 0, 4, bound, 0, 0, exp2);
q = Z3_mk_exists_const(ctx, 0, 1, bound4, 0, 0, exp1);
Z3_solver_assert(ctx, s, q);
I'm also not sure if I have to use some patterns over variables like suggested here: Does Z3 support variable-only patterns in quantified formulas?
But according to what I read in this tutorial http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.225.8231&rep=rep1&type=pdf It seems OK just not to use any patterns, right?
The way you pick k
, l
, m
, and n
allows symmetries. For instance:
k = 0
l = 1
m = 1
n = 0
satisfies your condition a!1
, but it obviously fails to pick "distinct" elements for ts_var
; which makes a!2
false. Hence your entire query becomes unsat
.
You can replace the definition of your a!1
with the following:
(a!1 (distinct k l m n))
which would concisely state these four variables are all different. With that change, z3 does find a model indeed.