[英]Calculate minimum value of Bitvector in Z3
我有以下代码:(declare-const L4(_ BitVec 6))(declare-const L1(_ BitVec 6))(declare-const L0(_ BitVec 6))(declare-const l2(_ BitVec 6) ))
(assert (= l2 (_ bv8 6)))
;; All is encoding the set that contains {0, 1, 2, 3, 4, 5}
(define-const All (_ BitVec 6) #b111111)
;; Empty is encoding the empty set
(define-const Empty (_ BitVec 6) #b000000)
(define-fun LT_l ((S (_ BitVec 6)) (l (_ BitVec 6))) Bool
;; True if for all x in S x < l
(= (bvand (bvshl All l) S) Empty))
(define-fun is_in ((e (_ BitVec 6)) (S (_ BitVec 6))) Bool
;; True if e is an element of the "set" S.
(not (= (bvand (bvshl (_ bv1 6) e) S) Empty)))
(define-fun is_minimal ((e (_ BitVec 6)) (S (_ BitVec 6))) Bool
;; True if e is the minimal element of S
(and (is_in e S) ;; S contains e
;; (1 << e) - 1 represents the set of elements that are smaller than e
(= (bvand (bvsub (bvshl (_ bv1 6) e) (_ bv1 6)) S) Empty)))
;; To encode that forall x in L0 and forall y in L1. x < y
(define-fun LT ((L0 (_ BitVec 6)) (L1 (_ BitVec 6))) Bool
; True if forall x in L0 and forall y in L1, x < y
(or (= L0 Empty)
(= L1 Empty)
(exists ((min (_ BitVec 6))) (and (is_minimal min L1) (LT_l L0 min)))))
(assert (not (= L0 Empty)))
(assert (not (= L1 Empty)))
(assert (not (= L4 Empty)))
(assert (LT_l L4 l2))
(assert (LT L0 L1))
(check-sat)
(get-model)
(assert (LT L1 L0))
(check-sat)
当我运行此代码时,我得到的模型是:sat(模型(define-fun min!0()(_ BitVec 6)#b000011)(define-fun L1()(_ BitVec 6)#b001000)(define-fun L0()(_ BitVec 6)#b000100)(定义乐趣L4()(_ BitVec 6)#b000100)(定义乐趣l2()(_ BitVec 6)#b001000))
min
的结果为何是:
(define-fun min!0 () (_ BitVec 6)
#b000011)
而不是b001000,因为L1的最小值是这个值,而不是b000011。
有人可以解释我吗?
最后,我定义了函数Lt_l来检查S x <l中是否所有x,但是现在我想做GT_l来检查S l <x中是否所有x。 我有以下代码:
(define-fun GT_l ((S (_ BitVec 6)) (l (_ BitVec 6))) Bool
(= (bvand (bvneg (bvshl (_ bv0 6) l)) S) Empty))
但这为什么行不通呢?
谢谢
在您的示例中,您使用位向量表示集合。 例如,位向量#b101000
表示集合{5, 3}
#b101000
{5, 3}
。 输出(define-fun L1 () (_ BitVec 6) #b001000)
实质上是说L1
是“集合” {3}
。 一种可能的混淆是,位向量用于表示集合和元素。 位向量min!0
表示一个元素。 输出(define-fun min!0 () (_ BitVec 6) #b000011)
表示min!0
是值3
,它的确是L1
的“最小值”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.