open Z3open Z3.Arithmeticopen Z3.Booleanopen Z3.Solverlet () = (* Create context *) let ctx = mk_context [] in (* Create integer variable *) let x = Integer.mk_const_s ctx "x" in let zero = Integer.mk_numeral_i ctx 0 in (* Create constraint: x > 0 *) let constraint_ = mk_gt ctx x zero in (* Create solver and add constraint *) let solver = mk_solver ctx None in Solver.add solver [constraint_]; (* Check satisfiability *) match check solver [] with | SATISFIABLE -> Printf.printf "SAT\n"; (match get_model solver with | Some model -> Printf.printf "Model: %s\n" (Model.to_string model) | None -> ()) | UNSATISFIABLE -> Printf.printf "UNSAT\n" | UNKNOWN -> Printf.printf "UNKNOWN\n"
open Z3.Arithmetic.Integeropen Z3.Booleanlet x = mk_const_s ctx "x" inlet y = mk_const_s ctx "y" inlet zero = mk_numeral_i ctx 0 in(* Comparisons *)let eq = mk_eq ctx x y inlet lt = mk_lt ctx x y inlet le = mk_le ctx x y inlet gt = mk_gt ctx x zero inlet ge = mk_ge ctx x zero in
let basic_tests (ctx : context) = Printf.printf "BasicTests\n"; let fname = mk_string ctx "f" in let x = mk_string ctx "x" in let y = mk_string ctx "y" in let bs = Boolean.mk_sort ctx in let domain = [bs; bs] in let f = FuncDecl.mk_func_decl ctx fname domain bs in let fapp = mk_app ctx f [ Expr.mk_const ctx x bs; Expr.mk_const ctx y bs ] in let trivial_eq = mk_eq ctx fapp fapp in let goal = mk_goal ctx true false false in Goal.add goal [trivial_eq]; let solver = mk_solver ctx None in List.iter (fun a -> Solver.add solver [a]) (Goal.get_formulas goal); if check solver [] != SATISFIABLE then raise (TestFailedException "") else Printf.printf "Test passed.\n"
let solver = mk_solver ctx None inSolver.add solver [constraint_];match check solver [] with| SATISFIABLE -> (match get_model solver with | Some model -> (* Evaluate with model completion *) (match Model.eval model x true with | Some value -> Printf.printf "x = %s\n" (Expr.to_string value) | None -> Printf.printf "x is unassigned\n"); (* Get all constant interpretations *) for i = 0 to Model.get_num_consts model - 1 do let decl = Model.get_const_decl model i in let interp = Model.get_const_interp model decl in match interp with | Some v -> Printf.printf "%s = %s\n" (FuncDecl.get_name decl |> Symbol.to_string) (Expr.to_string v) | None -> () done | None -> ())| _ -> ()
open Z3.BitVectorlet ctx = mk_context [] in(* 8-bit variables *)let x = mk_const_s ctx "x" 8 inlet y = mk_const_s ctx "y" 8 in(* Constants *)let ff = mk_numeral ctx "255" 8 inlet zero = mk_numeral ctx "0" 8 in(* Operations *)let sum = mk_add ctx x y inlet and_op = mk_and ctx x y inlet shift = mk_shl ctx x y in(* Comparisons *)let ult = mk_ult ctx x y in (* Unsigned less than *)let slt = mk_slt ctx x y in (* Signed less than *)(* Constraints *)let solver = Solver.mk_solver ctx None inSolver.add solver [ Boolean.mk_eq ctx sum ff; mk_ugt ctx x y (* Unsigned greater than *)];
let solver = mk_solver ctx None inSolver.add solver [constraint_];let _ = check solver [] inlet stats = Solver.get_statistics solver inPrintf.printf "Statistics:\n%s\n" (Statistics.to_string stats);(* Access specific statistics *)for i = 0 to Statistics.get_size stats - 1 do let key = Statistics.get_key stats i in if Statistics.is_uint stats i then Printf.printf "%s: %d\n" key (Statistics.get_uint stats i) else if Statistics.is_double stats i then Printf.printf "%s: %f\n" key (Statistics.get_double stats i)done