4

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)

1 Answers1

5

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.

alias
  • 28,120
  • 2
  • 23
  • 40
  • I like this solution, it brings together the best features of the two worlds. – Patrick Trentin Feb 25 '19 at 16:14
  • 2
    I don't think this feature is part of the SMTLib standard. but I do know that z3, cvc4, and mathsat all support it. So, it's practically the standard! – alias Feb 25 '19 at 16:19
  • The [documentation](http://smtlib.cs.uiowa.edu/theories-ArraysEx.shtml) seems to be particularly lacking on this regard, when compared to BV/FP, unless I am looking at the wrong page. – Patrick Trentin Feb 25 '19 at 16:23
  • 1
    I've found one reference in section 3.6.4 of http://smtlib.cs.uiowa.edu/papers/smt-lib-reference-v2.6-r2017-07-18.pdf, but it's only in an example. (And also uses the term `const-array`, which isn't the syntax supported by Z3 at least.) It'd be nice to send a note to the SMTLib mailing list and ask about it. – alias Feb 25 '19 at 16:52
  • I saw the same thing but as you say it doesn't match your example quite well :) – Patrick Trentin Feb 25 '19 at 17:26