Kurt Kurtov
Kurt Kurtov

Reputation: 65

Z3 set default value of array to zero

I am trying to solve models for array expressions, where default values for array is equal to 0.

For example, I am trying to solve this example, but I get unknown results all the time

(declare-const arr (Array Int Int))
(declare-const arr2 (Array Int Int))
(declare-const a Int)
(declare-const b Int)

(assert (forall ((x Int)) (= (select arr x) 0)))

(assert (> a 0))
(assert (<= a 10))

(assert (= arr2 (store arr a 1337)))

(assert (> b 0))
(assert (<= b 10))


(assert (= (select arr2 b) 0))

(check-sat)
(get-model)

Upvotes: 4

Views: 692

Answers (1)

alias
alias

Reputation: 30525

Patrick's advice on not using quantifiers is spot on! They'll make your life harder. However, you're in luck, because z3 supports constant-arrays for your use case, which is quite common. The syntax is:

(assert (= arr ((as const (Array Int Int)) 0)))

This makes sure arr will have all its entries as 0; no quantification needed and z3 handles it internally just fine.

So, your benchmark will be:

(declare-const arr (Array Int Int))
(declare-const arr2 (Array Int Int))
(declare-const a Int)
(declare-const b Int)

(assert (= arr ((as const (Array Int Int)) 0)))

(assert (> a 0))
(assert (<= a 10))

(assert (= arr2 (store arr a 1337)))

(assert (> b 0))
(assert (<= b 10))


(assert (= (select arr2 b) 0))

(check-sat)
(get-model)

which is solved in no time. This way, you can have the entire array start with 0, and modify the range you're interested in; which can depend on variables as usual and is not required to be known ahead of time.

Upvotes: 5

Related Questions