Following to the question published in How expressive can we be with arrays in Z3(Py)? An example, I expressed the following formula in Z3Py:
Exists i::Integer s.t. (0<=i<|arr|) & (avg(arr)+t<arr[i])
This means: whether there is a position i::0<i<|arr|
in the array whose value a[i]
is greater than the average of the array avg(arr)
plus a given threshold t
.
The solution in Z3Py:
t = Int('t')
avg_arr = Int('avg_arr')
len_arr = Int('len_arr')
arr = Array('arr', IntSort(), IntSort())
phi_1 = And(0 <= i, i< len_arr)
phi_2 = (t+avg_arr<arr[i])
phi = Exists(i, And(phi_1, phi_2))
s = Solver()
s.add(phi)
print(s.check())
print(s.model())
Note that, (1) the formula is satisfiable and (2) each time I execute it, I get a different model. For instance, I just got: [avg_a = 0, t = 7718, len_arr = 1, arr = K(Int, 7719)]
.
I have three questions now:
- What does
arr = K(Int, 7719)]
mean? Does this mean the array contains oneInt
element with value7719
? In that case, what does theK
mean? - Of course, this implementation is wrong in the sense that the average and length values are independent from the array itself. How can I implement simple
avg
andlen
functions? - Where is the
i
index in the model given by the solver?
Also, in which sense would this implementation be different using sequences instead of arrays?