Smt2-lib:为什么我在`declare-const + assert`和`define-fun`之间的行为有所不同?

时间:2019-02-14 07:27:10

标签: z3 smt model-checking

我有一个以smt2-lib格式编写的z3模型。我注意到当我使用时:

(declare-const flat1 (Seq Dummy))
(assert (= flat1 (unroll dummyFormula1)))

当我使用模型时坐着:

(define-fun flat1 () (Seq Dummy) (unroll dummyFormula1))

该模型报告为未知。 为什么差异如此重要?如果有帮助,我可以制作模型的最低版本。

编辑#1 -最小示例

由于this bug,请确保使用github master上的z3运行它。您可以在下面用A)B)指示的两个版本之间进行切换。

(set-option :produce-models true)

; --------------- Basic Definitions -------------------

(declare-datatype Dummy (A B))

(declare-datatype Formula
  ((Base (forB Dummy))
   (And  (andB1 Formula) (andB2 Formula))
   (Or   (orB1 Formula) (orB2 Formula))
   (Not  (notB Formula))))

(declare-const dummyFormula1 Formula)
(assert (= dummyFormula1 (Base A)))

(declare-const dummyFormula2 Formula)
(assert (= dummyFormula2 (And (Base A) (Base A))))

; --------------- Some functions -----------------------

(define-fun
  in_list ((o Dummy) (l (Seq Dummy))) Bool
  (seq.contains l (seq.unit o)))

(define-fun
  permutation ((l1 (Seq Dummy)) (l2 (Seq Dummy))) Bool
  (forall ((o Dummy)) (= (in_list o l1) (in_list o l2))))

(define-fun-rec unroll ((f Formula)) (Seq Dummy)
  (match f
    (((Base j)    (seq.unit j))
     ((And f1 f2) (seq.++ (unroll f1) (unroll f2)))
     ((Or  f1 f2) (seq.++ (unroll f1) (unroll f2)))
     ((Not f1)    (unroll f1)))))

; -------------- The question -------------------------

;; Here are two versions that should express the same idea, but try commenting
;; the first one and uncommenting the second one!

;; A)

(declare-const flat1 (Seq Dummy))
(assert (= flat1 (unroll dummyFormula1)))

;; B)

; (define-fun flat1 () (Seq Dummy) (unroll dummyFormula1))
; -----------------------------------------------------

(declare-const flat2 (Seq Dummy))
(assert (= flat2 (unroll dummyFormula2)))

(assert (permutation flat1 flat2))

; --------------- Verify -------------------
(check-sat)
(get-model)

1 个答案:

答案 0 :(得分:2)

不用说z3的内部原理就很难说。但我想指出的是,尽管两种结构非常相似,但还是有细微的差别。

如果您查看标准(http://smtlib.cs.uiowa.edu/papers/smt-lib-reference-v2.6-r2017-07-18.pdf)的第62页,它将显示:

(define-fun f ((x1 σ1) · · · (xn σn)) σ t)

  with n ≥ 0 and t not containing f is semantically equivalent to the command sequence

(declare-fun f (σ1 · · · σn) σ)
(assert (forall ((x1 σ1) · · · (xn σn)) (= ( f x1 · · · xn) t)).

因此,当您使用define-fun格式时,您将明确地输入一个量化公式。当您手动使用declare-const / assert时,这种量化不存在。

现在您可以争辩说,您的情况下没有 参数,因此应该没有区别,我会同意您的看法。但是您还使用了matchdefine-fun-rec等相当新的功能,因此显然z3在这里绊倒了。既然您已经有了一个最小的示例,那么为什么不将其发布到z3 github的问题站点并在那里获得一些反馈。我怀疑宏查找程序可能遗漏了一个案例,无法实例化此特定案例,但确实很难说,并且可能还有充分的理由。

如果您确实在此处发帖并获得了很好的答案,请更新此问题,以便我们知道发生了什么事!