Question

How do I calculate attributed values? Here's an example:

(declare-fun x () bool)
(declare-fun y () bool)
(declare-fun z () bool)
(assert (AND x (OR y z)))

With this I would get 2 models:

x=true and y=true
x=true and z=true

Now, what I want is something like this:

(declare-fun x () bool)
(declare-fun y () bool)
(declare-fun z () bool)
(declare-fun x.val () Int)
(declare-fun y.val () Int)
(declare-fun z.val () Int)
(assert (= x.val 2))
(assert (= y.val 3))
(assert (= z.val 5))
(assert (AND x (OR y z)))
(assert (> sum 6))

So, I would like to get the model where the sum of the attributes is larger than 6:

x=true and z=true

Maybe working with arrays is a way to achieve this...

Was it helpful?

Solution

I’m not sure if I understood your question correctly. It seems you want to associate an (integer) attribute to each Boolean variable. So, each variable is a pair: a Boolean value, and an integer attribute. I’m assuming that by sum , you meant the sum of the attributes of the variables assigned to true. If that is the case, you can model it in Z3 in the following way:

;; Enable model construction
(set-option :produce-models true)

;; Declare a new type (sort) that is a pair (Bool, Int).
;; Given a variable x of type/sort WBool, we can write
;;  - (value x) for getting its Boolean value
;;  - (attr x)  for getting the integer "attribute" value
(declare-datatypes () ((WBool (mk-wbool (value Bool) (attr Int)))))

;; Now, we declare a macro int-value that returns (attr x) if
;; (value x) is true, and 0 otherwise
(define-fun int-value ((x WBool)) Int
  (ite (value x) (attr x) 0))

(declare-fun x () WBool)
(declare-fun y () WBool)
(declare-fun z () WBool)

;; Set the attribute values for x, y and z
(assert (= (attr x) 2))
(assert (= (attr y) 3))
(assert (= (attr z) 5))

;; Assert Boolean constraint on x, y and z.
(assert (and (value x) (or (value y) (value z))))

;; Assert that the sum of the attributes of the variables assigned to true is greater than 6.
(assert (> (+ (int-value x) (int-value y) (int-value z)) 6))
(check-sat)
(get-model)

(assert (not (value z)))
(check-sat)

OTHER TIPS

With three variables, I imagine it would be something like:

(define-fun cond_add ((cond Bool) (x Int) (sum Int)) Int
  (ite cond (+ sum x) sum))
(declare-fun sum () Int)
(assert (= sum (cond_add x x.val (cond_add y y.val (cond_add z z.val 0)))))
(assert (> sum 6))

Here I define a macro cond_add to add a variable to an accumulator when a corresponding condition holds. And sum is defined to account for conditional sum of x.val, y.val and z.val based on truth values of x, y and z.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top