Z3 Ocaml API递归函数

时间:2018-05-11 22:30:44

标签: api recursion ocaml z3

假设我想检查公式x + y = z(x,y,z整数)是否可以满足。使用Z3我可以输入如下内容:

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

我可以等效地使用Ocaml api并编写以下代码:

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";

我想用z3的ocaml api来检查它的可满足性 以下阶乘实现,以便我得到x的值(如果它存在)。

(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)

不幸的是,我找不到使用z3的ml api的递归定义的例子。

0 个答案:

没有答案