Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Z3 from string #226

Merged
merged 2 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion z3-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5566,7 +5566,7 @@ extern "C" {
///
/// - [`Z3_solver_from_file`]
/// - [`Z3_solver_to_string`]
pub fn Z3_solver_from_string(c: Z3_context, s: Z3_solver, file_name: Z3_string);
pub fn Z3_solver_from_string(c: Z3_context, s: Z3_solver, c_str: Z3_string);

/// Return the set of asserted formulas on the solver.
pub fn Z3_solver_get_assertions(c: Z3_context, s: Z3_solver) -> Z3_ast_vector;
Expand Down
9 changes: 9 additions & 0 deletions z3/src/optimize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ impl<'ctx> Optimize<'ctx> {
unsafe { Self::wrap(ctx, Z3_mk_optimize(ctx.z3_ctx)) }
}

/// Parse an SMT-LIB2 string with assertions, soft constraints and optimization objectives.
/// Add the parsed constraints and objectives to the optimizer.
pub fn from_string<T: Into<Vec<u8>>>(&self, source_string: T) {
let source_cstring = CString::new(source_string).unwrap();
unsafe {
Z3_optimize_from_string(self.ctx.z3_ctx, self.z3_opt, source_cstring.as_ptr());
}
}

/// Get this optimizers 's context.
pub fn get_context(&self) -> &'ctx Context {
self.ctx
Expand Down
11 changes: 10 additions & 1 deletion z3/src/solver.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ast;
use ast::Ast;
use std::ffi::CStr;
use std::ffi::{CStr, CString};
use std::fmt;
use z3_sys::*;
use Context;
Expand Down Expand Up @@ -52,6 +52,15 @@ impl<'ctx> Solver<'ctx> {
unsafe { Self::wrap(ctx, Z3_mk_solver(ctx.z3_ctx)) }
}

/// Parse an SMT-LIB2 string with assertions, soft constraints and optimization objectives.
/// Add the parsed constraints and objectives to the solver.
pub fn from_string<T: Into<Vec<u8>>>(&self, source_string: T) {
let source_cstring = CString::new(source_string).unwrap();
unsafe {
Z3_solver_from_string(self.ctx.z3_ctx, self.z3_slv, source_cstring.as_ptr());
}
}

/// Create a new solver customized for the given logic.
/// It returns `None` if the logic is unknown or unsupported.
pub fn new_for_logic<S: Into<Symbol>>(ctx: &'ctx Context, logic: S) -> Option<Solver<'ctx>> {
Expand Down
33 changes: 33 additions & 0 deletions z3/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,22 @@ fn test_ast_translate() {
assert_eq!(slv.check(), SatResult::Unsat);
}

#[test]
fn test_solver_new_from_smtlib2() {
let cfg = Config::new();
let ctx = Context::new(&cfg);
let problem = r#"
(declare -const x Real)
(declare -const y Real)
(declare -const z Real)
(assert (=( -(+(* 3 x) (* 2 y)) z) 1))
(assert (=(+( -(* 2 x) (* 2 y)) (* 4 z)) -2))
"#;
let solver = Solver::new(&ctx);
solver.from_string(problem);
assert_eq!(solver.check(), SatResult::Sat);
}

#[test]
fn test_solver_translate() {
let cfg = Config::new();
Expand Down Expand Up @@ -679,6 +695,23 @@ fn test_optimize_unknown() {
assert!(optimize.get_reason_unknown().is_some());
}

#[test]
fn test_optimize_new_from_smtlib2() {
let _ = env_logger::try_init();
let cfg = Config::new();
let ctx = Context::new(&cfg);
let problem = r#"
(declare -const x Real)
(declare -const y Real)
(declare -const z Real)
(assert (=( -(+(* 3 x) (* 2 y)) z) 1))
(assert (=(+( -(* 2 x) (* 2 y)) (* 4 z)) -2))
"#;
let optimize = Optimize::new(&ctx);
optimize.from_string(problem);
assert_eq!(optimize.check(&[]), SatResult::Sat);
}

#[test]
fn test_get_unsat_core() {
let _ = env_logger::try_init();
Expand Down