Z3
Loading...
Searching...
No Matches
Solver Class Reference
Inheritance diagram for Solver:

Public Member Functions

 __init__ (self, solver=None, ctx=None, logFile=None)
 __del__ (self)
 __enter__ (self)
 __exit__ (self, *exc_info)
 set (self, *args, **keys)
 push (self)
 pop (self, num=1)
 num_scopes (self)
 reset (self)
 assert_exprs (self, *args)
 add (self, *args)
 __iadd__ (self, fml)
 append (self, *args)
 insert (self, *args)
 assert_and_track (self, a, p)
 check (self, *assumptions)
 model (self)
 import_model_converter (self, other)
 interrupt (self)
 unsat_core (self)
 consequences (self, assumptions, variables)
 from_file (self, filename)
 from_string (self, s)
 cube (self, vars=None)
 cube_vars (self)
 root (self, t)
 next (self, t)
 explain_congruent (self, a, b)
 solve_for (self, ts)
 proof (self)
 assertions (self)
 units (self)
 non_units (self)
 trail_levels (self)
 set_initial_value (self, var, value)
 trail (self)
 statistics (self)
 reason_unknown (self)
 help (self)
 param_descrs (self)
 __repr__ (self)
 translate (self, target)
 __copy__ (self)
 __deepcopy__ (self, memo={})
 sexpr (self)
 dimacs (self, include_names=True)
 to_smt2 (self)
Public Member Functions inherited from Z3PPObject
 use_pp (self)

Data Fields

 ctx = _get_ctx(ctx)
int backtrack_level = 4000000000
 solver = None
 cube_vs = AstVector(None, self.ctx)

Additional Inherited Members

Protected Member Functions inherited from Z3PPObject
 _repr_html_ (self)

Detailed Description

Solver API provides methods for implementing the main SMT 2.0 commands:
push, pop, check, get-model, etc.

Definition at line 7078 of file z3py.py.

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self,
solver = None,
ctx = None,
logFile = None )

Definition at line 7084 of file z3py.py.

7084 def __init__(self, solver=None, ctx=None, logFile=None):
7085 assert solver is None or ctx is not None
7086 self.ctx = _get_ctx(ctx)
7087 self.backtrack_level = 4000000000
7088 self.solver = None
7089 if solver is None:
7090 self.solver = Z3_mk_solver(self.ctx.ref())
7091 else:
7092 self.solver = solver
7093 Z3_solver_inc_ref(self.ctx.ref(), self.solver)
7094 if logFile is not None:
7095 self.set("smtlib2_log", logFile)
7096
Z3_solver Z3_API Z3_mk_solver(Z3_context c)
Create a new solver. This solver is a "combined solver" (see combined_solver module) that internally ...
void Z3_API Z3_solver_inc_ref(Z3_context c, Z3_solver s)
Increment the reference counter of the given solver.

◆ __del__()

__del__ ( self)

Definition at line 7097 of file z3py.py.

7097 def __del__(self):
7098 if self.solver is not None and self.ctx.ref() is not None and Z3_solver_dec_ref is not None:
7099 Z3_solver_dec_ref(self.ctx.ref(), self.solver)
7100
void Z3_API Z3_solver_dec_ref(Z3_context c, Z3_solver s)
Decrement the reference counter of the given solver.

Member Function Documentation

◆ __copy__()

__copy__ ( self)

Definition at line 7578 of file z3py.py.

7578 def __copy__(self):
7579 return self.translate(self.ctx)
7580

◆ __deepcopy__()

__deepcopy__ ( self,
memo = {} )

Definition at line 7581 of file z3py.py.

7581 def __deepcopy__(self, memo={}):
7582 return self.translate(self.ctx)
7583

◆ __enter__()

__enter__ ( self)

Definition at line 7101 of file z3py.py.

7101 def __enter__(self):
7102 self.push()
7103 return self
7104

◆ __exit__()

__exit__ ( self,
* exc_info )

Definition at line 7105 of file z3py.py.

7105 def __exit__(self, *exc_info):
7106 self.pop()
7107

◆ __iadd__()

__iadd__ ( self,
fml )

Definition at line 7227 of file z3py.py.

7227 def __iadd__(self, fml):
7228 self.add(fml)
7229 return self
7230

◆ __repr__()

__repr__ ( self)
Return a formatted string with all added constraints.

Definition at line 7561 of file z3py.py.

7561 def __repr__(self):
7562 """Return a formatted string with all added constraints."""
7563 return obj_to_string(self)
7564

◆ add()

add ( self,
* args )
Assert constraints into the solver.

>>> x = Int('x')
>>> s = Solver()
>>> s.add(x > 0, x < 2)
>>> s
[x > 0, x < 2]

Definition at line 7216 of file z3py.py.

7216 def add(self, *args):
7217 """Assert constraints into the solver.
7218
7219 >>> x = Int('x')
7220 >>> s = Solver()
7221 >>> s.add(x > 0, x < 2)
7222 >>> s
7223 [x > 0, x < 2]
7224 """
7225 self.assert_exprs(*args)
7226

Referenced by __iadd__().

◆ append()

append ( self,
* args )
Assert constraints into the solver.

>>> x = Int('x')
>>> s = Solver()
>>> s.append(x > 0, x < 2)
>>> s
[x > 0, x < 2]

Definition at line 7231 of file z3py.py.

7231 def append(self, *args):
7232 """Assert constraints into the solver.
7233
7234 >>> x = Int('x')
7235 >>> s = Solver()
7236 >>> s.append(x > 0, x < 2)
7237 >>> s
7238 [x > 0, x < 2]
7239 """
7240 self.assert_exprs(*args)
7241

◆ assert_and_track()

assert_and_track ( self,
a,
p )
Assert constraint `a` and track it in the unsat core using the Boolean constant `p`.

If `p` is a string, it will be automatically converted into a Boolean constant.

>>> x = Int('x')
>>> p3 = Bool('p3')
>>> s = Solver()
>>> s.set(unsat_core=True)
>>> s.assert_and_track(x > 0,  'p1')
>>> s.assert_and_track(x != 1, 'p2')
>>> s.assert_and_track(x < 0,  p3)
>>> print(s.check())
unsat
>>> c = s.unsat_core()
>>> len(c)
2
>>> Bool('p1') in c
True
>>> Bool('p2') in c
False
>>> p3 in c
True

Definition at line 7253 of file z3py.py.

7253 def assert_and_track(self, a, p):
7254 """Assert constraint `a` and track it in the unsat core using the Boolean constant `p`.
7255
7256 If `p` is a string, it will be automatically converted into a Boolean constant.
7257
7258 >>> x = Int('x')
7259 >>> p3 = Bool('p3')
7260 >>> s = Solver()
7261 >>> s.set(unsat_core=True)
7262 >>> s.assert_and_track(x > 0, 'p1')
7263 >>> s.assert_and_track(x != 1, 'p2')
7264 >>> s.assert_and_track(x < 0, p3)
7265 >>> print(s.check())
7266 unsat
7267 >>> c = s.unsat_core()
7268 >>> len(c)
7269 2
7270 >>> Bool('p1') in c
7271 True
7272 >>> Bool('p2') in c
7273 False
7274 >>> p3 in c
7275 True
7276 """
7277 if isinstance(p, str):
7278 p = Bool(p, self.ctx)
7279 _z3_assert(isinstance(a, BoolRef), "Boolean expression expected")
7280 _z3_assert(isinstance(p, BoolRef) and is_const(p), "Boolean expression expected")
7281 Z3_solver_assert_and_track(self.ctx.ref(), self.solver, a.as_ast(), p.as_ast())
7282
void Z3_API Z3_solver_assert_and_track(Z3_context c, Z3_solver s, Z3_ast a, Z3_ast p)
Assert a constraint a into the solver, and track it (in the unsat) core using the Boolean constant p.

◆ assert_exprs()

assert_exprs ( self,
* args )
Assert constraints into the solver.

>>> x = Int('x')
>>> s = Solver()
>>> s.assert_exprs(x > 0, x < 2)
>>> s
[x > 0, x < 2]

Definition at line 7197 of file z3py.py.

7197 def assert_exprs(self, *args):
7198 """Assert constraints into the solver.
7199
7200 >>> x = Int('x')
7201 >>> s = Solver()
7202 >>> s.assert_exprs(x > 0, x < 2)
7203 >>> s
7204 [x > 0, x < 2]
7205 """
7206 args = _get_args(args)
7207 s = BoolSort(self.ctx)
7208 for arg in args:
7209 if isinstance(arg, Goal) or isinstance(arg, AstVector):
7210 for f in arg:
7211 Z3_solver_assert(self.ctx.ref(), self.solver, f.as_ast())
7212 else:
7213 arg = s.cast(arg)
7214 Z3_solver_assert(self.ctx.ref(), self.solver, arg.as_ast())
7215
void Z3_API Z3_solver_assert(Z3_context c, Z3_solver s, Z3_ast a)
Assert a constraint into the solver.

Referenced by add(), append(), and insert().

◆ assertions()

assertions ( self)
Return an AST vector containing all added constraints.

>>> s = Solver()
>>> s.assertions()
[]
>>> a = Int('a')
>>> s.add(a > 0)
>>> s.add(a < 10)
>>> s.assertions()
[a > 0, a < 10]

Definition at line 7478 of file z3py.py.

7478 def assertions(self):
7479 """Return an AST vector containing all added constraints.
7480
7481 >>> s = Solver()
7482 >>> s.assertions()
7483 []
7484 >>> a = Int('a')
7485 >>> s.add(a > 0)
7486 >>> s.add(a < 10)
7487 >>> s.assertions()
7488 [a > 0, a < 10]
7489 """
7490 return AstVector(Z3_solver_get_assertions(self.ctx.ref(), self.solver), self.ctx)
7491
Z3_ast_vector Z3_API Z3_solver_get_assertions(Z3_context c, Z3_solver s)
Return the set of asserted formulas on the solver.

◆ check()

check ( self,
* assumptions )
Check whether the assertions in the given solver plus the optional assumptions are consistent or not.

>>> x = Int('x')
>>> s = Solver()
>>> s.check()
sat
>>> s.add(x > 0, x < 2)
>>> s.check()
sat
>>> s.model().eval(x)
1
>>> s.add(x < 1)
>>> s.check()
unsat
>>> s.reset()
>>> s.add(2**x == 4)
>>> s.check()
sat

Definition at line 7283 of file z3py.py.

7283 def check(self, *assumptions):
7284 """Check whether the assertions in the given solver plus the optional assumptions are consistent or not.
7285
7286 >>> x = Int('x')
7287 >>> s = Solver()
7288 >>> s.check()
7289 sat
7290 >>> s.add(x > 0, x < 2)
7291 >>> s.check()
7292 sat
7293 >>> s.model().eval(x)
7294 1
7295 >>> s.add(x < 1)
7296 >>> s.check()
7297 unsat
7298 >>> s.reset()
7299 >>> s.add(2**x == 4)
7300 >>> s.check()
7301 sat
7302 """
7303 s = BoolSort(self.ctx)
7304 assumptions = _get_args(assumptions)
7305 num = len(assumptions)
7306 _assumptions = (Ast * num)()
7307 for i in range(num):
7308 _assumptions[i] = s.cast(assumptions[i]).as_ast()
7309 r = Z3_solver_check_assumptions(self.ctx.ref(), self.solver, num, _assumptions)
7310 return CheckSatResult(r)
7311
Z3_lbool Z3_API Z3_solver_check_assumptions(Z3_context c, Z3_solver s, unsigned num_assumptions, Z3_ast const assumptions[])
Check whether the assertions in the given solver and optional assumptions are consistent or not.

◆ consequences()

consequences ( self,
assumptions,
variables )
Determine fixed values for the variables based on the solver state and assumptions.
>>> s = Solver()
>>> a, b, c, d = Bools('a b c d')
>>> s.add(Implies(a,b), Implies(b, c))
>>> s.consequences([a],[b,c,d])
(sat, [Implies(a, b), Implies(a, c)])
>>> s.consequences([Not(c),d],[a,b,c,d])
(sat, [Implies(d, d), Implies(Not(c), Not(c)), Implies(Not(c), Not(b)), Implies(Not(c), Not(a))])

Definition at line 7374 of file z3py.py.

7374 def consequences(self, assumptions, variables):
7375 """Determine fixed values for the variables based on the solver state and assumptions.
7376 >>> s = Solver()
7377 >>> a, b, c, d = Bools('a b c d')
7378 >>> s.add(Implies(a,b), Implies(b, c))
7379 >>> s.consequences([a],[b,c,d])
7380 (sat, [Implies(a, b), Implies(a, c)])
7381 >>> s.consequences([Not(c),d],[a,b,c,d])
7382 (sat, [Implies(d, d), Implies(Not(c), Not(c)), Implies(Not(c), Not(b)), Implies(Not(c), Not(a))])
7383 """
7384 if isinstance(assumptions, list):
7385 _asms = AstVector(None, self.ctx)
7386 for a in assumptions:
7387 _asms.push(a)
7388 assumptions = _asms
7389 if isinstance(variables, list):
7390 _vars = AstVector(None, self.ctx)
7391 for a in variables:
7392 _vars.push(a)
7393 variables = _vars
7394 _z3_assert(isinstance(assumptions, AstVector), "ast vector expected")
7395 _z3_assert(isinstance(variables, AstVector), "ast vector expected")
7396 consequences = AstVector(None, self.ctx)
7397 r = Z3_solver_get_consequences(self.ctx.ref(), self.solver, assumptions.vector,
7398 variables.vector, consequences.vector)
7399 sz = len(consequences)
7400 consequences = [consequences[i] for i in range(sz)]
7401 return CheckSatResult(r), consequences
7402
Z3_lbool Z3_API Z3_solver_get_consequences(Z3_context c, Z3_solver s, Z3_ast_vector assumptions, Z3_ast_vector variables, Z3_ast_vector consequences)
retrieve consequences from solver that determine values of the supplied function symbols.

◆ cube()

cube ( self,
vars = None )
Get set of cubes
The method takes an optional set of variables that restrict which
variables may be used as a starting point for cubing.
If vars is not None, then the first case split is based on a variable in
this set.

Definition at line 7411 of file z3py.py.

7411 def cube(self, vars=None):
7412 """Get set of cubes
7413 The method takes an optional set of variables that restrict which
7414 variables may be used as a starting point for cubing.
7415 If vars is not None, then the first case split is based on a variable in
7416 this set.
7417 """
7418 self.cube_vs = AstVector(None, self.ctx)
7419 if vars is not None:
7420 for v in vars:
7421 self.cube_vs.push(v)
7422 while True:
7423 lvl = self.backtrack_level
7424 self.backtrack_level = 4000000000
7425 r = AstVector(Z3_solver_cube(self.ctx.ref(), self.solver, self.cube_vs.vector, lvl), self.ctx)
7426 if (len(r) == 1 and is_false(r[0])):
7427 return
7428 yield r
7429 if (len(r) == 0):
7430 return
7431
Z3_ast_vector Z3_API Z3_solver_cube(Z3_context c, Z3_solver s, Z3_ast_vector vars, unsigned backtrack_level)
extract a next cube for a solver. The last cube is the constant true or false. The number of (non-con...

◆ cube_vars()

cube_vars ( self)
Access the set of variables that were touched by the most recently generated cube.
This set of variables can be used as a starting point for additional cubes.
The idea is that variables that appear in clauses that are reduced by the most recent
cube are likely more useful to cube on.

Definition at line 7432 of file z3py.py.

7432 def cube_vars(self):
7433 """Access the set of variables that were touched by the most recently generated cube.
7434 This set of variables can be used as a starting point for additional cubes.
7435 The idea is that variables that appear in clauses that are reduced by the most recent
7436 cube are likely more useful to cube on."""
7437 return self.cube_vs
7438

◆ dimacs()

dimacs ( self,
include_names = True )
Return a textual representation of the solver in DIMACS format.

Definition at line 7596 of file z3py.py.

7596 def dimacs(self, include_names=True):
7597 """Return a textual representation of the solver in DIMACS format."""
7598 return Z3_solver_to_dimacs_string(self.ctx.ref(), self.solver, include_names)
7599
Z3_string Z3_API Z3_solver_to_dimacs_string(Z3_context c, Z3_solver s, bool include_names)
Convert a solver into a DIMACS formatted string.

◆ explain_congruent()

explain_congruent ( self,
a,
b )
Explain congruence of a and b relative to the current search state

Definition at line 7455 of file z3py.py.

7455 def explain_congruent(self, a, b):
7456 """Explain congruence of a and b relative to the current search state"""
7457 a = _py2expr(a, self.ctx)
7458 b = _py2expr(b, self.ctx)
7459 return _to_expr_ref(Z3_solver_congruence_explain(self.ctx.ref(), self.solver, a.ast, b.ast), self.ctx)
7460
7461
Z3_ast Z3_API Z3_solver_congruence_explain(Z3_context c, Z3_solver s, Z3_ast a, Z3_ast b)
retrieve explanation for congruence.

◆ from_file()

from_file ( self,
filename )
Parse assertions from a file

Definition at line 7403 of file z3py.py.

7403 def from_file(self, filename):
7404 """Parse assertions from a file"""
7405 Z3_solver_from_file(self.ctx.ref(), self.solver, filename)
7406
void Z3_API Z3_solver_from_file(Z3_context c, Z3_solver s, Z3_string file_name)
load solver assertions from a file.

◆ from_string()

from_string ( self,
s )
Parse assertions from a string

Definition at line 7407 of file z3py.py.

7407 def from_string(self, s):
7408 """Parse assertions from a string"""
7409 Z3_solver_from_string(self.ctx.ref(), self.solver, s)
7410
void Z3_API Z3_solver_from_string(Z3_context c, Z3_solver s, Z3_string str)
load solver assertions from a string.

◆ help()

help ( self)
Display a string describing all available options.

Definition at line 7553 of file z3py.py.

7553 def help(self):
7554 """Display a string describing all available options."""
7555 print(Z3_solver_get_help(self.ctx.ref(), self.solver))
7556
Z3_string Z3_API Z3_solver_get_help(Z3_context c, Z3_solver s)
Return a string describing all solver available parameters.

◆ import_model_converter()

import_model_converter ( self,
other )
Import model converter from other into the current solver

Definition at line 7331 of file z3py.py.

7331 def import_model_converter(self, other):
7332 """Import model converter from other into the current solver"""
7333 Z3_solver_import_model_converter(self.ctx.ref(), other.solver, self.solver)
7334

◆ insert()

insert ( self,
* args )
Assert constraints into the solver.

>>> x = Int('x')
>>> s = Solver()
>>> s.insert(x > 0, x < 2)
>>> s
[x > 0, x < 2]

Definition at line 7242 of file z3py.py.

7242 def insert(self, *args):
7243 """Assert constraints into the solver.
7244
7245 >>> x = Int('x')
7246 >>> s = Solver()
7247 >>> s.insert(x > 0, x < 2)
7248 >>> s
7249 [x > 0, x < 2]
7250 """
7251 self.assert_exprs(*args)
7252

◆ interrupt()

interrupt ( self)
Interrupt the execution of the solver object.
Remarks: This ensures that the interrupt applies only
to the given solver object and it applies only if it is running.

Definition at line 7335 of file z3py.py.

7335 def interrupt(self):
7336 """Interrupt the execution of the solver object.
7337 Remarks: This ensures that the interrupt applies only
7338 to the given solver object and it applies only if it is running.
7339 """
7340 Z3_solver_interrupt(self.ctx.ref(), self.solver)
7341
void Z3_API Z3_solver_interrupt(Z3_context c, Z3_solver s)
Solver local interrupt. Normally you should use Z3_interrupt to cancel solvers because only one solve...

◆ model()

model ( self)
Return a model for the last `check()`.

This function raises an exception if
a model is not available (e.g., last `check()` returned unsat).

>>> s = Solver()
>>> a = Int('a')
>>> s.add(a + 2 == 0)
>>> s.check()
sat
>>> s.model()
[a = -2]

Definition at line 7312 of file z3py.py.

7312 def model(self):
7313 """Return a model for the last `check()`.
7314
7315 This function raises an exception if
7316 a model is not available (e.g., last `check()` returned unsat).
7317
7318 >>> s = Solver()
7319 >>> a = Int('a')
7320 >>> s.add(a + 2 == 0)
7321 >>> s.check()
7322 sat
7323 >>> s.model()
7324 [a = -2]
7325 """
7326 try:
7327 return ModelRef(Z3_solver_get_model(self.ctx.ref(), self.solver), self.ctx)
7328 except Z3Exception:
7329 raise Z3Exception("model is not available")
7330
Z3_model Z3_API Z3_solver_get_model(Z3_context c, Z3_solver s)
Retrieve the model for the last Z3_solver_check or Z3_solver_check_assumptions.

◆ next()

next ( self,
t )
Retrieve congruence closure sibling of the term t relative to the current search state
The function primarily works for SimpleSolver. Terms and variables that are
eliminated during pre-processing are not visible to the congruence closure.

Definition at line 7447 of file z3py.py.

7447 def next(self, t):
7448 """Retrieve congruence closure sibling of the term t relative to the current search state
7449 The function primarily works for SimpleSolver. Terms and variables that are
7450 eliminated during pre-processing are not visible to the congruence closure.
7451 """
7452 t = _py2expr(t, self.ctx)
7453 return _to_expr_ref(Z3_solver_congruence_next(self.ctx.ref(), self.solver, t.ast), self.ctx)
7454
Z3_ast Z3_API Z3_solver_congruence_next(Z3_context c, Z3_solver s, Z3_ast a)
retrieve the next expression in the congruence class. The set of congruent siblings form a cyclic lis...

◆ non_units()

non_units ( self)
Return an AST vector containing all atomic formulas in solver state that are not units.

Definition at line 7497 of file z3py.py.

7497 def non_units(self):
7498 """Return an AST vector containing all atomic formulas in solver state that are not units.
7499 """
7500 return AstVector(Z3_solver_get_non_units(self.ctx.ref(), self.solver), self.ctx)
7501
Z3_ast_vector Z3_API Z3_solver_get_non_units(Z3_context c, Z3_solver s)
Return the set of non units in the solver state.

◆ num_scopes()

num_scopes ( self)
Return the current number of backtracking points.

>>> s = Solver()
>>> s.num_scopes()
0
>>> s.push()
>>> s.num_scopes()
1
>>> s.push()
>>> s.num_scopes()
2
>>> s.pop()
>>> s.num_scopes()
1

Definition at line 7165 of file z3py.py.

7165 def num_scopes(self):
7166 """Return the current number of backtracking points.
7167
7168 >>> s = Solver()
7169 >>> s.num_scopes()
7170 0
7171 >>> s.push()
7172 >>> s.num_scopes()
7173 1
7174 >>> s.push()
7175 >>> s.num_scopes()
7176 2
7177 >>> s.pop()
7178 >>> s.num_scopes()
7179 1
7180 """
7181 return Z3_solver_get_num_scopes(self.ctx.ref(), self.solver)
7182
unsigned Z3_API Z3_solver_get_num_scopes(Z3_context c, Z3_solver s)
Return the number of backtracking points.

◆ param_descrs()

param_descrs ( self)
Return the parameter description set.

Definition at line 7557 of file z3py.py.

7557 def param_descrs(self):
7558 """Return the parameter description set."""
7559 return ParamDescrsRef(Z3_solver_get_param_descrs(self.ctx.ref(), self.solver), self.ctx)
7560
Z3_param_descrs Z3_API Z3_solver_get_param_descrs(Z3_context c, Z3_solver s)
Return the parameter description set for the given solver object.

◆ pop()

pop ( self,
num = 1 )
Backtrack \\c num backtracking points.

>>> x = Int('x')
>>> s = Solver()
>>> s.add(x > 0)
>>> s
[x > 0]
>>> s.push()
>>> s.add(x < 1)
>>> s
[x > 0, x < 1]
>>> s.check()
unsat
>>> s.pop()
>>> s.check()
sat
>>> s
[x > 0]

Definition at line 7143 of file z3py.py.

7143 def pop(self, num=1):
7144 """Backtrack \\c num backtracking points.
7145
7146 >>> x = Int('x')
7147 >>> s = Solver()
7148 >>> s.add(x > 0)
7149 >>> s
7150 [x > 0]
7151 >>> s.push()
7152 >>> s.add(x < 1)
7153 >>> s
7154 [x > 0, x < 1]
7155 >>> s.check()
7156 unsat
7157 >>> s.pop()
7158 >>> s.check()
7159 sat
7160 >>> s
7161 [x > 0]
7162 """
7163 Z3_solver_pop(self.ctx.ref(), self.solver, num)
7164
void Z3_API Z3_solver_pop(Z3_context c, Z3_solver s, unsigned n)
Backtrack n backtracking points.

Referenced by __exit__().

◆ proof()

proof ( self)
Return a proof for the last `check()`. Proof construction must be enabled.

Definition at line 7474 of file z3py.py.

7474 def proof(self):
7475 """Return a proof for the last `check()`. Proof construction must be enabled."""
7476 return _to_expr_ref(Z3_solver_get_proof(self.ctx.ref(), self.solver), self.ctx)
7477
Z3_ast Z3_API Z3_solver_get_proof(Z3_context c, Z3_solver s)
Retrieve the proof for the last Z3_solver_check or Z3_solver_check_assumptions.

◆ push()

push ( self)
Create a backtracking point.

>>> x = Int('x')
>>> s = Solver()
>>> s.add(x > 0)
>>> s
[x > 0]
>>> s.push()
>>> s.add(x < 1)
>>> s
[x > 0, x < 1]
>>> s.check()
unsat
>>> s.pop()
>>> s.check()
sat
>>> s
[x > 0]

Definition at line 7121 of file z3py.py.

7121 def push(self):
7122 """Create a backtracking point.
7123
7124 >>> x = Int('x')
7125 >>> s = Solver()
7126 >>> s.add(x > 0)
7127 >>> s
7128 [x > 0]
7129 >>> s.push()
7130 >>> s.add(x < 1)
7131 >>> s
7132 [x > 0, x < 1]
7133 >>> s.check()
7134 unsat
7135 >>> s.pop()
7136 >>> s.check()
7137 sat
7138 >>> s
7139 [x > 0]
7140 """
7141 Z3_solver_push(self.ctx.ref(), self.solver)
7142
void Z3_API Z3_solver_push(Z3_context c, Z3_solver s)
Create a backtracking point.

Referenced by __enter__().

◆ reason_unknown()

reason_unknown ( self)
Return a string describing why the last `check()` returned `unknown`.

>>> x = Int('x')
>>> s = SimpleSolver()
>>> s.add(x == 2**x)
>>> s.check()
unknown
>>> s.reason_unknown()
'(incomplete (theory arithmetic))'

Definition at line 7540 of file z3py.py.

7540 def reason_unknown(self):
7541 """Return a string describing why the last `check()` returned `unknown`.
7542
7543 >>> x = Int('x')
7544 >>> s = SimpleSolver()
7545 >>> s.add(x == 2**x)
7546 >>> s.check()
7547 unknown
7548 >>> s.reason_unknown()
7549 '(incomplete (theory arithmetic))'
7550 """
7551 return Z3_solver_get_reason_unknown(self.ctx.ref(), self.solver)
7552
Z3_string Z3_API Z3_solver_get_reason_unknown(Z3_context c, Z3_solver s)
Return a brief justification for an "unknown" result (i.e., Z3_L_UNDEF) for the commands Z3_solver_ch...

◆ reset()

reset ( self)
Remove all asserted constraints and backtracking points created using `push()`.

>>> x = Int('x')
>>> s = Solver()
>>> s.add(x > 0)
>>> s
[x > 0]
>>> s.reset()
>>> s
[]

Definition at line 7183 of file z3py.py.

7183 def reset(self):
7184 """Remove all asserted constraints and backtracking points created using `push()`.
7185
7186 >>> x = Int('x')
7187 >>> s = Solver()
7188 >>> s.add(x > 0)
7189 >>> s
7190 [x > 0]
7191 >>> s.reset()
7192 >>> s
7193 []
7194 """
7195 Z3_solver_reset(self.ctx.ref(), self.solver)
7196
void Z3_API Z3_solver_reset(Z3_context c, Z3_solver s)
Remove all assertions from the solver.

◆ root()

root ( self,
t )
Retrieve congruence closure root of the term t relative to the current search state
The function primarily works for SimpleSolver. Terms and variables that are
eliminated during pre-processing are not visible to the congruence closure.

Definition at line 7439 of file z3py.py.

7439 def root(self, t):
7440 """Retrieve congruence closure root of the term t relative to the current search state
7441 The function primarily works for SimpleSolver. Terms and variables that are
7442 eliminated during pre-processing are not visible to the congruence closure.
7443 """
7444 t = _py2expr(t, self.ctx)
7445 return _to_expr_ref(Z3_solver_congruence_root(self.ctx.ref(), self.solver, t.ast), self.ctx)
7446
Z3_ast Z3_API Z3_solver_congruence_root(Z3_context c, Z3_solver s, Z3_ast a)
retrieve the congruence closure root of an expression. The root is retrieved relative to the state wh...

◆ set()

set ( self,
* args,
** keys )
Set a configuration option.
The method `help()` return a string containing all available options.

>>> s = Solver()
>>> # The option MBQI can be set using three different approaches.
>>> s.set(mbqi=True)
>>> s.set('MBQI', True)
>>> s.set(':mbqi', True)

Definition at line 7108 of file z3py.py.

7108 def set(self, *args, **keys):
7109 """Set a configuration option.
7110 The method `help()` return a string containing all available options.
7111
7112 >>> s = Solver()
7113 >>> # The option MBQI can be set using three different approaches.
7114 >>> s.set(mbqi=True)
7115 >>> s.set('MBQI', True)
7116 >>> s.set(':mbqi', True)
7117 """
7118 p = args2params(args, keys, self.ctx)
7119 Z3_solver_set_params(self.ctx.ref(), self.solver, p.params)
7120
void Z3_API Z3_solver_set_params(Z3_context c, Z3_solver s, Z3_params p)
Set the given solver using the given parameters.

◆ set_initial_value()

set_initial_value ( self,
var,
value )
initialize the solver's state by setting the initial value of var to value

Definition at line 7510 of file z3py.py.

7510 def set_initial_value(self, var, value):
7511 """initialize the solver's state by setting the initial value of var to value
7512 """
7513 s = var.sort()
7514 value = s.cast(value)
7515 Z3_solver_set_initial_value(self.ctx.ref(), self.solver, var.ast, value.ast)
7516
void Z3_API Z3_solver_set_initial_value(Z3_context c, Z3_solver s, Z3_ast v, Z3_ast val)
provide an initialization hint to the solver. The initialization hint is used to calibrate an initial...

◆ sexpr()

sexpr ( self)
Return a formatted string (in Lisp-like format) with all added constraints.
We say the string is in s-expression format.

>>> x = Int('x')
>>> s = Solver()
>>> s.add(x > 0)
>>> s.add(x < 2)
>>> r = s.sexpr()

Definition at line 7584 of file z3py.py.

7584 def sexpr(self):
7585 """Return a formatted string (in Lisp-like format) with all added constraints.
7586 We say the string is in s-expression format.
7587
7588 >>> x = Int('x')
7589 >>> s = Solver()
7590 >>> s.add(x > 0)
7591 >>> s.add(x < 2)
7592 >>> r = s.sexpr()
7593 """
7594 return Z3_solver_to_string(self.ctx.ref(), self.solver)
7595
Z3_string Z3_API Z3_solver_to_string(Z3_context c, Z3_solver s)
Convert a solver into a string.

◆ solve_for()

solve_for ( self,
ts )
Retrieve a solution for t relative to linear equations maintained in the current state.

Definition at line 7462 of file z3py.py.

7462 def solve_for(self, ts):
7463 """Retrieve a solution for t relative to linear equations maintained in the current state."""
7464 vars = AstVector(ctx=self.ctx);
7465 terms = AstVector(ctx=self.ctx);
7466 guards = AstVector(ctx=self.ctx);
7467 for t in ts:
7468 t = _py2expr(t, self.ctx)
7469 vars.push(t)
7470 Z3_solver_solve_for(self.ctx.ref(), self.solver, vars.vector, terms.vector, guards.vector)
7471 return [(vars[i], terms[i], guards[i]) for i in range(len(vars))]
7472
7473
void Z3_API Z3_solver_solve_for(Z3_context c, Z3_solver s, Z3_ast_vector variables, Z3_ast_vector terms, Z3_ast_vector guards)
retrieve a 'solution' for variables as defined by equalities in maintained by solvers....

◆ statistics()

statistics ( self)
Return statistics for the last `check()`.

>>> s = SimpleSolver()
>>> x = Int('x')
>>> s.add(x > 0)
>>> s.check()
sat
>>> st = s.statistics()
>>> st.get_key_value('final checks')
1
>>> len(st) > 0
True
>>> st[0] != 0
True

Definition at line 7522 of file z3py.py.

7522 def statistics(self):
7523 """Return statistics for the last `check()`.
7524
7525 >>> s = SimpleSolver()
7526 >>> x = Int('x')
7527 >>> s.add(x > 0)
7528 >>> s.check()
7529 sat
7530 >>> st = s.statistics()
7531 >>> st.get_key_value('final checks')
7532 1
7533 >>> len(st) > 0
7534 True
7535 >>> st[0] != 0
7536 True
7537 """
7538 return Statistics(Z3_solver_get_statistics(self.ctx.ref(), self.solver), self.ctx)
7539
Z3_stats Z3_API Z3_solver_get_statistics(Z3_context c, Z3_solver s)
Return statistics for the given solver.

◆ to_smt2()

to_smt2 ( self)
return SMTLIB2 formatted benchmark for solver's assertions

Definition at line 7600 of file z3py.py.

7600 def to_smt2(self):
7601 """return SMTLIB2 formatted benchmark for solver's assertions"""
7602 es = self.assertions()
7603 sz = len(es)
7604 sz1 = sz
7605 if sz1 > 0:
7606 sz1 -= 1
7607 v = (Ast * sz1)()
7608 for i in range(sz1):
7609 v[i] = es[i].as_ast()
7610 if sz > 0:
7611 e = es[sz1].as_ast()
7612 else:
7613 e = BoolVal(True, self.ctx).as_ast()
7615 self.ctx.ref(), "benchmark generated from python API", "", "unknown", "", sz1, v, e,
7616 )
7617
7618
Z3_string Z3_API Z3_benchmark_to_smtlib_string(Z3_context c, Z3_string name, Z3_string logic, Z3_string status, Z3_string attributes, unsigned num_assumptions, Z3_ast const assumptions[], Z3_ast formula)
Convert the given benchmark into SMT-LIB formatted string.

◆ trail()

trail ( self)
Return trail of the solver state after a check() call.

Definition at line 7517 of file z3py.py.

7517 def trail(self):
7518 """Return trail of the solver state after a check() call.
7519 """
7520 return AstVector(Z3_solver_get_trail(self.ctx.ref(), self.solver), self.ctx)
7521
Z3_ast_vector Z3_API Z3_solver_get_trail(Z3_context c, Z3_solver s)
Return the trail modulo model conversion, in order of decision level The decision level can be retrie...

◆ trail_levels()

trail_levels ( self)
Return trail and decision levels of the solver state after a check() call.

Definition at line 7502 of file z3py.py.

7502 def trail_levels(self):
7503 """Return trail and decision levels of the solver state after a check() call.
7504 """
7505 trail = self.trail()
7506 levels = (ctypes.c_uint * len(trail))()
7507 Z3_solver_get_levels(self.ctx.ref(), self.solver, trail.vector, len(trail), levels)
7508 return trail, levels
7509
void Z3_API Z3_solver_get_levels(Z3_context c, Z3_solver s, Z3_ast_vector literals, unsigned sz, unsigned levels[])
retrieve the decision depth of Boolean literals (variables or their negations). Assumes a check-sat c...

◆ translate()

translate ( self,
target )
Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.

>>> c1 = Context()
>>> c2 = Context()
>>> s1 = Solver(ctx=c1)
>>> s2 = s1.translate(c2)

Definition at line 7565 of file z3py.py.

7565 def translate(self, target):
7566 """Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.
7567
7568 >>> c1 = Context()
7569 >>> c2 = Context()
7570 >>> s1 = Solver(ctx=c1)
7571 >>> s2 = s1.translate(c2)
7572 """
7573 if z3_debug():
7574 _z3_assert(isinstance(target, Context), "argument must be a Z3 context")
7575 solver = Z3_solver_translate(self.ctx.ref(), self.solver, target.ref())
7576 return Solver(solver, target)
7577
Z3_solver Z3_API Z3_solver_translate(Z3_context source, Z3_solver s, Z3_context target)
Copy a solver s from the context source to the context target.

◆ units()

units ( self)
Return an AST vector containing all currently inferred units.

Definition at line 7492 of file z3py.py.

7492 def units(self):
7493 """Return an AST vector containing all currently inferred units.
7494 """
7495 return AstVector(Z3_solver_get_units(self.ctx.ref(), self.solver), self.ctx)
7496
Z3_ast_vector Z3_API Z3_solver_get_units(Z3_context c, Z3_solver s)
Return the set of units modulo model conversion.

◆ unsat_core()

unsat_core ( self)
Return a subset (as an AST vector) of the assumptions provided to the last check().

These are the assumptions Z3 used in the unsatisfiability proof.
Assumptions are available in Z3. They are used to extract unsatisfiable cores.
They may be also used to "retract" assumptions. Note that, assumptions are not really
"soft constraints", but they can be used to implement them.

>>> p1, p2, p3 = Bools('p1 p2 p3')
>>> x, y       = Ints('x y')
>>> s          = Solver()
>>> s.add(Implies(p1, x > 0))
>>> s.add(Implies(p2, y > x))
>>> s.add(Implies(p2, y < 1))
>>> s.add(Implies(p3, y > -3))
>>> s.check(p1, p2, p3)
unsat
>>> core = s.unsat_core()
>>> len(core)
2
>>> p1 in core
True
>>> p2 in core
True
>>> p3 in core
False
>>> # "Retracting" p2
>>> s.check(p1, p3)
sat

Definition at line 7342 of file z3py.py.

7342 def unsat_core(self):
7343 """Return a subset (as an AST vector) of the assumptions provided to the last check().
7344
7345 These are the assumptions Z3 used in the unsatisfiability proof.
7346 Assumptions are available in Z3. They are used to extract unsatisfiable cores.
7347 They may be also used to "retract" assumptions. Note that, assumptions are not really
7348 "soft constraints", but they can be used to implement them.
7349
7350 >>> p1, p2, p3 = Bools('p1 p2 p3')
7351 >>> x, y = Ints('x y')
7352 >>> s = Solver()
7353 >>> s.add(Implies(p1, x > 0))
7354 >>> s.add(Implies(p2, y > x))
7355 >>> s.add(Implies(p2, y < 1))
7356 >>> s.add(Implies(p3, y > -3))
7357 >>> s.check(p1, p2, p3)
7358 unsat
7359 >>> core = s.unsat_core()
7360 >>> len(core)
7361 2
7362 >>> p1 in core
7363 True
7364 >>> p2 in core
7365 True
7366 >>> p3 in core
7367 False
7368 >>> # "Retracting" p2
7369 >>> s.check(p1, p3)
7370 sat
7371 """
7372 return AstVector(Z3_solver_get_unsat_core(self.ctx.ref(), self.solver), self.ctx)
7373
Z3_ast_vector Z3_API Z3_solver_get_unsat_core(Z3_context c, Z3_solver s)
Retrieve the unsat core for the last Z3_solver_check_assumptions The unsat core is a subset of the as...

Field Documentation

◆ backtrack_level

int backtrack_level = 4000000000

Definition at line 7087 of file z3py.py.

◆ ctx

ctx = _get_ctx(ctx)

Definition at line 7086 of file z3py.py.

Referenced by __del__(), assert_and_track(), assert_exprs(), check(), model(), num_scopes(), pop(), push(), reset(), and set().

◆ cube_vs

cube_vs = AstVector(None, self.ctx)

Definition at line 7418 of file z3py.py.

◆ solver

solver = None

Definition at line 7088 of file z3py.py.

Referenced by __del__(), assert_and_track(), assert_exprs(), check(), model(), num_scopes(), pop(), push(), reset(), and set().