Is there a way to use Z3 to get models for constraints involving sequences and maps?

十年热恋 提交于 2019-12-11 05:47:53

问题


Some time ago I asked how I could use Z3 to get models for constraints involving sets (Is there a way to use Z3 to get models for constraints involving sets?). For this the extended array theory works well in my case.

Now I have got the same issue with sequences (with operations length, membership, (in-)equality, perhaps slicing) and maps. I.e. axiomatization leads to the same problem as for sets. I have been thinking of encoding sequences and maps using the extended array theory as well but I have not yet been able to come up with a good way to do this.

Does anyone have an idea on how sequences and maps could be encoded to get accurate models?


回答1:


In Z3, arrays are essentially maps. Here is an example on how to create an "array" from list of integers to integers.

(declare-const a (Array (List Int) Int))
(declare-const l1 (List Int))
(declare-const l2 (List Int))
(assert (= (select a l1) 0))
(assert (= (select a l2) 0))
(check-sat)
(get-model)

For sequences, we can encode them using quantifiers. Z3 is complete for many decidable fragments. Most of them are described in the Z3 tutorial. Here is a possible encoding.

;; In this example, we are encoding sequences of T.
;; Let us make T == Int
(define-sort T () Int)

;; We represent a sequence as a pair: function + length
(declare-fun S1-data (Int) T)
(declare-const S1-len  Int)

(declare-fun S2-data (Int) T)
(declare-const S2-len  Int)

(declare-fun S3-data (Int) T)
(declare-const S3-len  Int)

;; This encoding has one limitation, we can't have sequences of sequences; nor have sequences as arguments of functions.

;; Here is how we assert that the sequences S1 and S2 are equal.
(push)
(assert (= S1-len S2-len)) 
(assert (forall ((i Int)) (=> (and (<= 0 i) (< i S1-len)) (= (S1-data i) (S2-data i)))))
;; To make the example more interesting, let us assume S1-len > 0
(assert (> S1-len 0))
(check-sat)
(get-model)
(pop)

;; Here is how we say that sequence S3 is the concatenation of sequences S1 and S2.
(push)
(assert (= S3-len (+ S1-len S2-len)))
(assert (forall ((i Int)) (=> (and (<= 0 i) (< i S1-len)) (= (S3-data i) (S1-data i)))))
(assert (forall ((i Int)) (=> (and (<= 0 i) (< i S2-len)) (= (S3-data (+ i S1-len)) (S2-data i)))))
;; let us assert that S1-len and S2-len > 1
(assert (> S1-len 1))
(assert (> S2-len 1))
;; let us also assert that S3(0) != S3(1)
(assert (not (= (S3-data 0) (S3-data 1))))
(check-sat)
(get-model)
(pop)

;; Here is how we encode that sequence S2 is sequence S1 with one extra element a
(push)
(declare-const a T)
(assert (> a 10))
(assert (= S2-len (+ 1 S1-len)))
(assert (= (S2-data S1-len) a))
(assert (forall ((i Int)) (=> (and (<= 0 i) (< i S1-len)) (= (S2-data i) (S1-data i)))))
;; let us also assert that S1-len > 1
(assert (> S1-len 1))
(check-sat)
(get-model)
(pop)


来源:https://stackoverflow.com/questions/19287770/is-there-a-way-to-use-z3-to-get-models-for-constraints-involving-sequences-and-m

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!