如何使用Z3 SMT-LIB获得Z3和Z4组的表示

时间:2013-11-20 20:56:31

标签: z3

组Z3具有以下总和表

enter image description here

使用以下Z3 SMT-LIB代码可以获得Z3的表示:

(set-option :mbqi true)
(declare-sort S)
(declare-fun f (S S) S)
(declare-const a S)
(declare-const b S)
(declare-const c S)
(assert (forall ((x S) (y S))
            (= (f x y)  (f y x))))
(assert (forall ((x S))
            (= (f x a) x)))
(assert (= (f b b) c))
(assert (= (f b c) a))
(assert (= (f c c) b))
(check-sat)
(get-model)

相应的输出是

sat 
(model 
 ;; universe for S: 
 ;; S!val!1 S!val!0 S!val!2 
 ;; ----------- 
 ;; definitions for universe elements: 
 (declare-fun S!val!1 () S) 
 (declare-fun S!val!0 () S) 
 (declare-fun S!val!2 () S) 
 ;; cardinality constraint: 
 (forall ((x S)) (or (= x S!val!1) (= x S!val!0) (= x S!val!2))) 
 ;; ----------- 
 (define-fun b () S S!val!0) 
 (define-fun c () S S!val!1) 
 (define-fun a () S S!val!2) 
 (define-fun f ((x!1 S) (x!2 S)) S 
 (ite (and (= x!1 S!val!0) (= x!2 S!val!0)) S!val!1 
 (ite (and (= x!1 S!val!0) (= x!2 S!val!1)) S!val!2 
 (ite (and (= x!1 S!val!1) (= x!2 S!val!1)) S!val!0 
 (ite (and (= x!1 S!val!1) (= x!2 S!val!0)) S!val!2 
 (ite (and (= x!1 S!val!0) (= x!2 S!val!2)) S!val!0 
 (ite (and (= x!1 S!val!2) (= x!2 S!val!0)) S!val!0 
 (ite (and (= x!1 S!val!2) (= x!2 S!val!1)) S!val!1 
 (ite (and (= x!1 S!val!1) (= x!2 S!val!2)) S!val!1 
   x!1))))))))) 
)

在线运行此代码here

Z4组有以下总和表:

enter image description here

使用以下Z3 SMT-LIB代码可以获得Z4的表示:

(set-option :mbqi true)
(declare-sort S)
(declare-fun f (S S) S)
(declare-const a S)
(declare-const b S)
(declare-const c S)
(declare-const d S)
(assert (forall ((x S) (y S))
            (= (f x y)  (f y x))))
(assert (forall ((x S))
            (= (f x a) x)))
(assert (= (f b b) c))
(assert (= (f b c) d))
(assert (= (f b d) a))
(assert (= (f c c) a))
(assert (= (f c d) b))
(assert (= (f d d) c))
(check-sat)
(get-model)

相应的输出是:

sat 
(model 
;; universe for S: ;; S!val!1 S!val!3 S!val!0 S!val!2 
;; ----------- 
;; definitions for universe elements: 
(declare-fun S!val!1 () S) 
(declare-fun S!val!3 () S) 
(declare-fun S!val!0 () S) 
(declare-fun S!val!2 () S) 
;; cardinality constraint: 
(forall ((x S)) (or (= x S!val!1) (= x S!val!3) (= x S!val!0) (= x S!val!2))) 
;; ----------- 
(define-fun b () S S!val!0) 
(define-fun c () S S!val!1) 
(define-fun d () S S!val!2) 
(define-fun a () S S!val!3) 
(define-fun f ((x!1 S) (x!2 S)) S 
  (ite (and (= x!1 S!val!0) (= x!2 S!val!0)) S!val!1 
  (ite (and (= x!1 S!val!0) (= x!2 S!val!1)) S!val!2 
  (ite (and (= x!1 S!val!0) (= x!2 S!val!2)) S!val!3 
  (ite (and (= x!1 S!val!1) (= x!2 S!val!1)) S!val!3 
  (ite (and (= x!1 S!val!1) (= x!2 S!val!2)) S!val!0 
  (ite (and (= x!1 S!val!2) (= x!2 S!val!2)) S!val!1 
  (ite (and (= x!1 S!val!1) (= x!2 S!val!0)) S!val!2 
  (ite (and (= x!1 S!val!2) (= x!2 S!val!0)) S!val!3 
  (ite (and (= x!1 S!val!2) (= x!2 S!val!1)) S!val!0 
  (ite (and (= x!1 S!val!2) (= x!2 S!val!3)) S!val!2 
  (ite (and (= x!1 S!val!3) (= x!2 S!val!2)) S!val!2 
  (ite (and (= x!1 S!val!3) (= x!2 S!val!1)) S!val!1 
  (ite (and (= x!1 S!val!1) (= x!2 S!val!3)) S!val!1 
  (ite (and (= x!1 S!val!3) (= x!2 S!val!0)) S!val!0 
  (ite (and (= x!1 S!val!0) (= x!2 S!val!3)) S!val!0 
  x!1)))))))))))))))) 
)

在线运行此代码here

问题是如何在原始代码中插入Z3生成的函数f,以便可以进行组Z3和Z4的计算。非常感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用命令eval来评估Z3生成的模型中的表达式。 例如,您可以在(get-model)之后添加以下命令。

(eval (f b b))
(eval c)
(eval (f b (f c b)))

如果您想将Z3生成的定义转换为可以在其他系统或语言中进行评估的函数,那么我认为您应该使用Z3编程API(例如,Python)。以下是z3.py的示例。

    >>> f = Function('f', IntSort(), IntSort())
    >>> s = Solver()
    >>> s.add(f(0) == 1, f(1) == 1, f(2) == 0)
    >>> s.check()
    sat
    >>> m = s.model()
    >>> m[f]
    [0 -> 1, 1 -> 1, 2 -> 0, else -> 1]
    >>> m[f].num_entries()
    3
    >>> m[f].entry(0)
    [0, 1]
    >>> m[f].entry(1)
    [1, 1]
    >>> m[f].entry(2)
    [2, 0]