Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Z3 and SMT-LIB to get a maximum of two values

Tags:

z3

smt

How do I get the maximum of a formula using smt-lib2?

I want something like this:

(declare-fun x () Int)
(declare-fun y () Int)
(declare-fun z () Int)
(assert (= x 2))
(assert (= y 4))
(assert (= z (max x y))
(check-sat)
(get-model)
(exit)

Of course, 'max' is unknown to smtlibv2. So, how can this be done?

like image 540
John Smith Avatar asked Nov 28 '11 14:11

John Smith


2 Answers

In Z3, you can easily define a macro max and use it for getting maximum of two values:

(define-fun max ((x Int) (y Int)) Int
  (ite (< x y) y x))

There is another trick to model max using uninterpreted functions, which will be helpful to use with Z3 API:

(declare-fun max (Int Int) Int)
(assert (forall ((x Int) (y Int))
    (= (max x y) (ite (< x y) y x))))

Note that you have to set (set-option :macro-finder true), so Z3 is able to replace universal quantifiers with body of the function when checking satisfiability.

like image 127
pad Avatar answered Sep 25 '22 06:09

pad


You've got abs, and per basic math max(a,b) = (a+b+abs(a-b))/2

like image 27
MSalters Avatar answered Sep 21 '22 06:09

MSalters