Z3 OCaml API 递归函数

sh0*_*h0t 5 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)
Run Code Online (Sandbox Code Playgroud)

我可以等效地使用 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";
Run Code Online (Sandbox Code Playgroud)

我想使用 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)
Run Code Online (Sandbox Code Playgroud)

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