[英]Set membership relation in z3
我想在Z3中使用C ++ API定义成员关系。 我想通过以下方式做到这一点:
z3::context C;
z3::sort I = C.int_sort();
z3::sort B = C.bool_sort();
z3::func_decl InSet = C.function("f", I, B);
z3::expr e1 = InSet(C.int_val(2)) == C.bool_val(true);
z3::expr e2 = InSet(C.int_val(3)) == C.bool_val(true);
z3::expr ite = to_expr(C, Z3_mk_ite(C, e1, C.bool_val(true),
Z3_mk_ite(C,e2,C.bool_val(true),C.bool_val(false))));
errs() << Z3_ast_to_string(C,ite);
在这个例子中,集合由整数2和3组成。我确信有更好的方法来定义关系,特别是集合成员关系,但我真的是Z3新手。 有谁知道最好的一个?
在Z3中,集合通常使用谓词(如您所做)或布尔数组进行编码。 在Z3 C API中,有几个用于创建集合表达式的函数: Z3_mk_set_sort
, Z3_mk_empty_set
, Z3_mk_set_union
,......实际上,这些函数正在创建数组表达式。 它们表示一组T
作为从T
到布尔的数组。 他们使用本文中描述的编码。
备注:在Z3中, InSet(C.int_val(2)) == C.bool_val(true)
等效于InSet(C.int_val(2))
。 InSet
函数是谓词。 我们可以编写std::cout << ite
而不是std::cout << Z3_ast_to_string(C, ite)
。
在基于谓词的方法中,我们通常需要使用量词。 在你的例子中,你说2
和3
是集合的元素,但是说没有别的东西是元素,我们需要量词。 我们还需要量词来表示属性,例如:set A
等于集合B
和C
的并集。基于量词的方法更灵活,我们可以说例如A
是包含1
和n
之间的所有元素的集合。 缺点是很容易创建不在Z3可以处理的可判定片段中的公式。 Z3教程描述了其中的一些片段。 这是教程中的一个例子。
;; A, B, C and D are sets of Int
(declare-fun A (Int) Bool)
(declare-fun B (Int) Bool)
(declare-fun C (Int) Bool)
(declare-fun D (Int) Bool)
;; A union B is a subset of C
(assert (forall ((x Int)) (=> (or (A x) (B x)) (C x))))
;; B minus A is not empty
;; That is, there exists an integer e that is B but not in A
(declare-const e Int)
(assert (and (B e) (not (A e))))
;; D is equal to C
(assert (forall ((x Int)) (iff (D x) (C x))))
;; 0, 1 and 2 are in B
(assert (B 0))
(assert (B 1))
(assert (B 2))
(check-sat)
(get-model)
(echo "Is e an element of D?")
(eval (D e))
(echo "Now proving that A is a strict subset of D")
;; This is true if the negation is unsatisfiable
(push)
(assert (not (and
;; A is a subset of D
(forall ((x Int)) (=> (A x) (D x)))
;; but, D has an element that is not in A.
(exists ((x Int)) (and (D x) (not (A x)))))))
(check-sat)
(pop)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.