Skip to main content

Solver

Struct Solver 

Source
pub struct Solver<'st, B> { /* private fields */ }
Expand description

The Solver type is the primary entrypoint to interaction with the solver. Checking for validity of a set of assertions requires:

// 1. Set up storage (TODO: document)
let st = smtlib::Storage::new();
// 2. Set up the backend (in this case z3)
let backend = smtlib::backend::z3_binary::Z3Binary::new("z3")?;
// 3. Set up the solver
let mut solver = smtlib::Solver::new(&st, backend)?;
// 4. Declare the necessary constants
let x = Int::new_const(&st, "x");
// 5. Add assertions to the solver
solver.assert(x._eq(12))?;
// 6. Check for validity, and optionally construct a model
let sat_res = solver.check_sat_with_model()?;
// 7. In this case we expect sat, and thus want to extract the model
let model = sat_res.expect_sat()?;
// 8. Interpret the result by extract the values of constants which
//    satisfy the assertions.
match model.eval(x) {
    Some(x) => println!("This is the value of x: {x}"),
    None => panic!("Oh no! This should never happen, as x was part of an assert"),
}

Implementations§

Source§

impl<'st, B> Solver<'st, B>
where B: Backend,

Source

pub fn new(st: &'st Storage, backend: B) -> Result<Self, Error>

Construct a new solver provided with the backend to use.

The read more about which backends are available, check out the documentation of the backend module.

Source

pub fn st(&self) -> &'st Storage

Get the smtlib storage.

Source

pub fn set_logger(&mut self, logger: impl Logger)

Set the logger for the solver. This is useful for debugging or tracing purposes.

Source

pub fn set_timeout(&mut self, ms: usize) -> Result<(), Error>

Set the timeout for the solver. The timeout is in milliseconds.

Source

pub fn set_opt_priority( &mut self, priority: OptimizationPriority, ) -> Result<(), Error>

Set the :opt.priority option to the given value

Source

pub fn set_logic(&mut self, logic: Logic) -> Result<(), Error>

Explicitly sets the logic for the solver. For some backends this is not required, as they will infer what ever logic fits the current program.

To read more about logics read the documentation of Logic.

Source

pub fn run_command( &mut self, cmd: Command<'st>, ) -> Result<GeneralResponse<'st>, Error>

Runs the given command on the solver, and returns the result.

Source

pub fn assert(&mut self, b: Bool<'st>) -> Result<(), Error>

Adds the constraint of b as an assertion to the solver. To check for satisfiability call Solver::check_sat or Solver::check_sat_with_model.

Source

pub fn maximize<S>(&mut self, g: S) -> Result<(), Error>
where S: Sorted<'st>,

Adds the optimization goal of g as a goal to the solver.

Source

pub fn assert_soft(&mut self, b: Bool<'st>) -> Result<(), Error>

Adds soft-constraint b to the solver.

Source

pub fn minimize<S>(&mut self, g: S) -> Result<(), Error>
where S: Sorted<'st>,

Adds the optimization goal of g as a goal to the solver.

Source

pub fn check_sat(&mut self) -> Result<SatResult, Error>

Checks for satisfiability of the assertions sent to the solver using Solver::assert.

If you are interested in producing a model satisfying the assertions check out Solver::check_sat.

Source

pub fn check_sat_with_model(&mut self) -> Result<SatResultWithModel<'st>, Error>

Checks for satisfiability of the assertions sent to the solver using Solver::assert, and produces a model in case of sat.

If you are not interested in the produced model, check out Solver::check_sat.

Source

pub fn check_sat_assuming( &mut self, assumptions: &[(Const<'st, Bool<'st>>, bool)], ) -> Result<SatResult, Error>

Checks for satisfiability of the assertions sent to the solver using Solver::assert as well as the ones provided in the assumptions. A satisfying model can be fetched using Solver::get_model in case of sat.

check_sat_assuming preserves the context and the given assumptions do not affect later calls to Solver::check_sat.

Source

pub fn get_model(&mut self) -> Result<Model<'st>, Error>

Produces the model for satisfying the assertions. If you are looking to retrieve a model after calling Solver::check_sat, consider using Solver::check_sat_with_model instead.

NOTE: This must only be called after having called Solver::check_sat and it returning SatResult::Sat.

Source

pub fn declare_fun(&mut self, fun: &Fun<'st>) -> Result<(), Error>

Declares a function to the solver. For more details refer to the funs module.

Source

pub fn eval<S>(&mut self, term: S) -> Result<S::Inner, Error>
where S: Sorted<'st>, S::Inner: From<STerm<'st>>,

Evaluate the given term

Source

pub fn simplify(&mut self, t: Dynamic<'st>) -> Result<&'st Term<'st>, Error>

Simplifies the given term

Source

pub fn scope<T>( &mut self, f: impl FnOnce(&mut Solver<'st, B>) -> Result<T, Error>, ) -> Result<T, Error>

Start a new scope, execute the given closure, and then pop the scope.

A scope is a way to group a set of assertions together, and then later rollback all the assertions to the state before the scope was started.

Trait Implementations§

Source§

impl<'st, B: Debug> Debug for Solver<'st, B>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'st, B> !RefUnwindSafe for Solver<'st, B>

§

impl<'st, B> !Send for Solver<'st, B>

§

impl<'st, B> !Sync for Solver<'st, B>

§

impl<'st, B> !UnwindSafe for Solver<'st, B>

§

impl<'st, B> Freeze for Solver<'st, B>
where Driver<'st, B>: Freeze,

§

impl<'st, B> Unpin for Solver<'st, B>
where Driver<'st, B>: Unpin,

§

impl<'st, B> UnsafeUnpin for Solver<'st, B>
where Driver<'st, B>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<'st, T> IntoWithStorage<'st, T> for T

Source§

fn into_with_storage(self, _st: &'st Storage) -> T

Construct a STerm with the presence of Storage
Source§

impl<D> OwoColorize for D

Source§

fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>
where C: Color,

Set the foreground color generically Read more
Source§

fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>
where C: Color,

Set the background color generically. Read more
Source§

fn black(&self) -> FgColorDisplay<'_, Black, Self>

Change the foreground color to black
Source§

fn on_black(&self) -> BgColorDisplay<'_, Black, Self>

Change the background color to black
Source§

fn red(&self) -> FgColorDisplay<'_, Red, Self>

Change the foreground color to red
Source§

fn on_red(&self) -> BgColorDisplay<'_, Red, Self>

Change the background color to red
Source§

fn green(&self) -> FgColorDisplay<'_, Green, Self>

Change the foreground color to green
Source§

fn on_green(&self) -> BgColorDisplay<'_, Green, Self>

Change the background color to green
Source§

fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>

Change the foreground color to yellow
Source§

fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>

Change the background color to yellow
Source§

fn blue(&self) -> FgColorDisplay<'_, Blue, Self>

Change the foreground color to blue
Source§

fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>

Change the background color to blue
Source§

fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to magenta
Source§

fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to magenta
Source§

fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to purple
Source§

fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to purple
Source§

fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>

Change the foreground color to cyan
Source§

fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>

Change the background color to cyan
Source§

fn white(&self) -> FgColorDisplay<'_, White, Self>

Change the foreground color to white
Source§

fn on_white(&self) -> BgColorDisplay<'_, White, Self>

Change the background color to white
Source§

fn default_color(&self) -> FgColorDisplay<'_, Default, Self>

Change the foreground color to the terminal default
Source§

fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>

Change the background color to the terminal default
Source§

fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>

Change the foreground color to bright black
Source§

fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>

Change the background color to bright black
Source§

fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>

Change the foreground color to bright red
Source§

fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>

Change the background color to bright red
Source§

fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>

Change the foreground color to bright green
Source§

fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>

Change the background color to bright green
Source§

fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>

Change the foreground color to bright yellow
Source§

fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>

Change the background color to bright yellow
Source§

fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>

Change the foreground color to bright blue
Source§

fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>

Change the background color to bright blue
Source§

fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright magenta
Source§

fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright magenta
Source§

fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright purple
Source§

fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright purple
Source§

fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>

Change the foreground color to bright cyan
Source§

fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>

Change the background color to bright cyan
Source§

fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>

Change the foreground color to bright white
Source§

fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>

Change the background color to bright white
Source§

fn bold(&self) -> BoldDisplay<'_, Self>

Make the text bold
Source§

fn dimmed(&self) -> DimDisplay<'_, Self>

Make the text dim
Source§

fn italic(&self) -> ItalicDisplay<'_, Self>

Make the text italicized
Source§

fn underline(&self) -> UnderlineDisplay<'_, Self>

Make the text underlined
Make the text blink
Make the text blink (but fast!)
Source§

fn reversed(&self) -> ReversedDisplay<'_, Self>

Swap the foreground and background colors
Source§

fn hidden(&self) -> HiddenDisplay<'_, Self>

Hide the text
Source§

fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>

Cross out the text
Source§

fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the foreground color at runtime. Only use if you do not know which color will be used at compile-time. If the color is constant, use either OwoColorize::fg or a color-specific method, such as OwoColorize::green, Read more
Source§

fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the background color at runtime. Only use if you do not know what color to use at compile-time. If the color is constant, use either OwoColorize::bg or a color-specific method, such as OwoColorize::on_yellow, Read more
Source§

fn fg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the foreground color to a specific RGB value.
Source§

fn bg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the background color to a specific RGB value.
Source§

fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>

Sets the foreground color to an RGB value.
Source§

fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>

Sets the background color to an RGB value.
Source§

fn style(&self, style: Style) -> Styled<&Self>

Apply a runtime-determined style
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.