5

Let's say I want to check if the formula x+y=z (x,y,z integers) is satisfiable. Using Z3 i could input something like:

(declare-fun x () Int)
(declare-fun y () Int)
(declare-fun z () Int)
(assert (= z (+ x y)))
(check-sat)

I could equivalently use the OCaml api and write the following code:

let ctx=Z3.mk_context  [("model", "true"); ("proof", "false")] in 
let v1=(Z3.Arithmetic.Integer.mk_const_s ctx "x") in
let v2=(Z3.Arithmetic.Integer.mk_const_s ctx "y") in
let res=(Z3.Arithmetic.Integer.mk_const_s ctx "z") in
let sum=Z3.Arithmetic.mk_add ctx [ v1 ; v2] in 
let phi=Z3.Boolean.mk_eq ctx sum res in
let solver = (Z3.Solver.mk_solver ctx None) in
let _= Z3.Solver.add solver [phi] in
let is_sat=Z3.Solver.check solver [] in 
match is_sat with 
      | UNSATISFIABLE -> Printf.printf "unsat";
      | SATISFIABLE -> Printf.printf "sat";
      | UNKNOWN -> Printf.printf "unknown";

I would like to use the ocaml api of z3 to check the satisfiability of the following factorial implementation, so that I get a value for x (if it exists).

(declare-fun x () Int)
(define-fun-rec f ((x Int)) Int
                  (ite (> x 1)
                       (* (f (- x 1))
                            x)
                       1))
(assert (= x (f 10)))
(check-sat)
(get-model)

Unfortunately, I can't find an example of recursive definitions using the ml api of z3.

ghilesZ
  • 1,502
  • 1
  • 18
  • 30
sh0t
  • 51
  • 3
  • Are you asking how to write recursive definitions, in general, in OCaml? Would that answer your question, perhaps? Is there something special about doing it with Z3? – Mars May 12 '18 at 03:41
  • I edited the question in a way I think it's clearer. – sh0t May 12 '18 at 15:41

0 Answers0