前段时间我问我如何使用 Z3 来获取涉及集合的约束的模型(有没有办法使用 Z3 来获取涉及集合的约束的模型?)。为此,扩展数组理论在我的情况下效果很好。
现在我在序列(操作长度、成员资格、(不)等式,也许是切片)和地图方面遇到了同样的问题。即公理化导致与集合相同的问题。我也一直在考虑使用扩展数组理论对序列和映射进行编码,但我还没有想出一个好的方法来做到这一点。
有没有人知道如何编码序列和地图以获得准确的模型?
前段时间我问我如何使用 Z3 来获取涉及集合的约束的模型(有没有办法使用 Z3 来获取涉及集合的约束的模型?)。为此,扩展数组理论在我的情况下效果很好。
现在我在序列(操作长度、成员资格、(不)等式,也许是切片)和地图方面遇到了同样的问题。即公理化导致与集合相同的问题。我也一直在考虑使用扩展数组理论对序列和映射进行编码,但我还没有想出一个好的方法来做到这一点。
有没有人知道如何编码序列和地图以获得准确的模型?
在 Z3 中,数组本质上是映射。这是一个关于如何从整数列表到整数创建“数组”的示例。
(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)
对于序列,我们可以使用量词对其进行编码。对于许多可判定的片段,Z3 是完整的。其中大部分都在Z3 教程中进行了描述。这是一种可能的编码。
;; 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)