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 7146 of file z3py.py.

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 7152 of file z3py.py.

7152 def __init__(self, solver=None, ctx=None, logFile=None):
7153 assert solver is None or ctx is not None
7154 self.ctx = _get_ctx(ctx)
7155 self.backtrack_level = 4000000000
7156 self.solver = None
7157 if solver is None:
7158 self.solver = Z3_mk_solver(self.ctx.ref())
7159 else:
7160 self.solver = solver
7161 Z3_solver_inc_ref(self.ctx.ref(), self.solver)
7162 if logFile is not None:
7163 self.set("smtlib2_log", logFile)
7164
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 7165 of file z3py.py.

7165 def __del__(self):
7166 if self.solver is not None and self.ctx.ref() is not None and Z3_solver_dec_ref is not None:
7167 Z3_solver_dec_ref(self.ctx.ref(), self.solver)
7168
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 7646 of file z3py.py.

7646 def __copy__(self):
7647 return self.translate(self.ctx)
7648

◆ __deepcopy__()

__deepcopy__ ( self,
memo = {} )

Definition at line 7649 of file z3py.py.

7649 def __deepcopy__(self, memo={}):
7650 return self.translate(self.ctx)
7651

◆ __enter__()

__enter__ ( self)

Definition at line 7169 of file z3py.py.

7169 def __enter__(self):
7170 self.push()
7171 return self
7172

◆ __exit__()

__exit__ ( self,
* exc_info )

Definition at line 7173 of file z3py.py.

7173 def __exit__(self, *exc_info):
7174 self.pop()
7175

◆ __iadd__()

__iadd__ ( self,
fml )

Definition at line 7295 of file z3py.py.

7295 def __iadd__(self, fml):
7296 self.add(fml)
7297 return self
7298

◆ __repr__()

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

Definition at line 7629 of file z3py.py.

7629 def __repr__(self):
7630 """Return a formatted string with all added constraints."""
7631 return obj_to_string(self)
7632

◆ 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 7284 of file z3py.py.

7284 def add(self, *args):
7285 """Assert constraints into the solver.
7286
7287 >>> x = Int('x')
7288 >>> s = Solver()
7289 >>> s.add(x > 0, x < 2)
7290 >>> s
7291 [x > 0, x < 2]
7292 """
7293 self.assert_exprs(*args)
7294

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 7299 of file z3py.py.

7299 def append(self, *args):
7300 """Assert constraints into the solver.
7301
7302 >>> x = Int('x')
7303 >>> s = Solver()
7304 >>> s.append(x > 0, x < 2)
7305 >>> s
7306 [x > 0, x < 2]
7307 """
7308 self.assert_exprs(*args)
7309

◆ 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 7321 of file z3py.py.

7321 def assert_and_track(self, a, p):
7322 """Assert constraint `a` and track it in the unsat core using the Boolean constant `p`.
7323
7324 If `p` is a string, it will be automatically converted into a Boolean constant.
7325
7326 >>> x = Int('x')
7327 >>> p3 = Bool('p3')
7328 >>> s = Solver()
7329 >>> s.set(unsat_core=True)
7330 >>> s.assert_and_track(x > 0, 'p1')
7331 >>> s.assert_and_track(x != 1, 'p2')
7332 >>> s.assert_and_track(x < 0, p3)
7333 >>> print(s.check())
7334 unsat
7335 >>> c = s.unsat_core()
7336 >>> len(c)
7337 2
7338 >>> Bool('p1') in c
7339 True
7340 >>> Bool('p2') in c
7341 False
7342 >>> p3 in c
7343 True
7344 """
7345 if isinstance(p, str):
7346 p = Bool(p, self.ctx)
7347 _z3_assert(isinstance(a, BoolRef), "Boolean expression expected")
7348 _z3_assert(isinstance(p, BoolRef) and is_const(p), "Boolean expression expected")
7349 Z3_solver_assert_and_track(self.ctx.ref(), self.solver, a.as_ast(), p.as_ast())
7350
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 7265 of file z3py.py.

7265 def assert_exprs(self, *args):
7266 """Assert constraints into the solver.
7267
7268 >>> x = Int('x')
7269 >>> s = Solver()
7270 >>> s.assert_exprs(x > 0, x < 2)
7271 >>> s
7272 [x > 0, x < 2]
7273 """
7274 args = _get_args(args)
7275 s = BoolSort(self.ctx)
7276 for arg in args:
7277 if isinstance(arg, Goal) or isinstance(arg, AstVector):
7278 for f in arg:
7279 Z3_solver_assert(self.ctx.ref(), self.solver, f.as_ast())
7280 else:
7281 arg = s.cast(arg)
7282 Z3_solver_assert(self.ctx.ref(), self.solver, arg.as_ast())
7283
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 7546 of file z3py.py.

7546 def assertions(self):
7547 """Return an AST vector containing all added constraints.
7548
7549 >>> s = Solver()
7550 >>> s.assertions()
7551 []
7552 >>> a = Int('a')
7553 >>> s.add(a > 0)
7554 >>> s.add(a < 10)
7555 >>> s.assertions()
7556 [a > 0, a < 10]
7557 """
7558 return AstVector(Z3_solver_get_assertions(self.ctx.ref(), self.solver), self.ctx)
7559
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 7351 of file z3py.py.

7351 def check(self, *assumptions):
7352 """Check whether the assertions in the given solver plus the optional assumptions are consistent or not.
7353
7354 >>> x = Int('x')
7355 >>> s = Solver()
7356 >>> s.check()
7357 sat
7358 >>> s.add(x > 0, x < 2)
7359 >>> s.check()
7360 sat
7361 >>> s.model().eval(x)
7362 1
7363 >>> s.add(x < 1)
7364 >>> s.check()
7365 unsat
7366 >>> s.reset()
7367 >>> s.add(2**x == 4)
7368 >>> s.check()
7369 sat
7370 """
7371 s = BoolSort(self.ctx)
7372 assumptions = _get_args(assumptions)
7373 num = len(assumptions)
7374 _assumptions = (Ast * num)()
7375 for i in range(num):
7376 _assumptions[i] = s.cast(assumptions[i]).as_ast()
7377 r = Z3_solver_check_assumptions(self.ctx.ref(), self.solver, num, _assumptions)
7378 return CheckSatResult(r)
7379
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 7442 of file z3py.py.

7442 def consequences(self, assumptions, variables):
7443 """Determine fixed values for the variables based on the solver state and assumptions.
7444 >>> s = Solver()
7445 >>> a, b, c, d = Bools('a b c d')
7446 >>> s.add(Implies(a,b), Implies(b, c))
7447 >>> s.consequences([a],[b,c,d])
7448 (sat, [Implies(a, b), Implies(a, c)])
7449 >>> s.consequences([Not(c),d],[a,b,c,d])
7450 (sat, [Implies(d, d), Implies(Not(c), Not(c)), Implies(Not(c), Not(b)), Implies(Not(c), Not(a))])
7451 """
7452 if isinstance(assumptions, list):
7453 _asms = AstVector(None, self.ctx)
7454 for a in assumptions:
7455 _asms.push(a)
7456 assumptions = _asms
7457 if isinstance(variables, list):
7458 _vars = AstVector(None, self.ctx)
7459 for a in variables:
7460 _vars.push(a)
7461 variables = _vars
7462 _z3_assert(isinstance(assumptions, AstVector), "ast vector expected")
7463 _z3_assert(isinstance(variables, AstVector), "ast vector expected")
7464 consequences = AstVector(None, self.ctx)
7465 r = Z3_solver_get_consequences(self.ctx.ref(), self.solver, assumptions.vector,
7466 variables.vector, consequences.vector)
7467 sz = len(consequences)
7468 consequences = [consequences[i] for i in range(sz)]
7469 return CheckSatResult(r), consequences
7470
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 7479 of file z3py.py.

7479 def cube(self, vars=None):
7480 """Get set of cubes
7481 The method takes an optional set of variables that restrict which
7482 variables may be used as a starting point for cubing.
7483 If vars is not None, then the first case split is based on a variable in
7484 this set.
7485 """
7486 self.cube_vs = AstVector(None, self.ctx)
7487 if vars is not None:
7488 for v in vars:
7489 self.cube_vs.push(v)
7490 while True:
7491 lvl = self.backtrack_level
7492 self.backtrack_level = 4000000000
7493 r = AstVector(Z3_solver_cube(self.ctx.ref(), self.solver, self.cube_vs.vector, lvl), self.ctx)
7494 if (len(r) == 1 and is_false(r[0])):
7495 return
7496 yield r
7497 if (len(r) == 0):
7498 return
7499
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 7500 of file z3py.py.

7500 def cube_vars(self):
7501 """Access the set of variables that were touched by the most recently generated cube.
7502 This set of variables can be used as a starting point for additional cubes.
7503 The idea is that variables that appear in clauses that are reduced by the most recent
7504 cube are likely more useful to cube on."""
7505 return self.cube_vs
7506

◆ dimacs()

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

Definition at line 7664 of file z3py.py.

7664 def dimacs(self, include_names=True):
7665 """Return a textual representation of the solver in DIMACS format."""
7666 return Z3_solver_to_dimacs_string(self.ctx.ref(), self.solver, include_names)
7667
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 7523 of file z3py.py.

7523 def explain_congruent(self, a, b):
7524 """Explain congruence of a and b relative to the current search state"""
7525 a = _py2expr(a, self.ctx)
7526 b = _py2expr(b, self.ctx)
7527 return _to_expr_ref(Z3_solver_congruence_explain(self.ctx.ref(), self.solver, a.ast, b.ast), self.ctx)
7528
7529
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 7471 of file z3py.py.

7471 def from_file(self, filename):
7472 """Parse assertions from a file"""
7473 Z3_solver_from_file(self.ctx.ref(), self.solver, filename)
7474
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 7475 of file z3py.py.

7475 def from_string(self, s):
7476 """Parse assertions from a string"""
7477 Z3_solver_from_string(self.ctx.ref(), self.solver, s)
7478
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 7621 of file z3py.py.

7621 def help(self):
7622 """Display a string describing all available options."""
7623 print(Z3_solver_get_help(self.ctx.ref(), self.solver))
7624
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 7399 of file z3py.py.

7399 def import_model_converter(self, other):
7400 """Import model converter from other into the current solver"""
7401 Z3_solver_import_model_converter(self.ctx.ref(), other.solver, self.solver)
7402

◆ 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 7310 of file z3py.py.

7310 def insert(self, *args):
7311 """Assert constraints into the solver.
7312
7313 >>> x = Int('x')
7314 >>> s = Solver()
7315 >>> s.insert(x > 0, x < 2)
7316 >>> s
7317 [x > 0, x < 2]
7318 """
7319 self.assert_exprs(*args)
7320

◆ 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 7403 of file z3py.py.

7403 def interrupt(self):
7404 """Interrupt the execution of the solver object.
7405 Remarks: This ensures that the interrupt applies only
7406 to the given solver object and it applies only if it is running.
7407 """
7408 Z3_solver_interrupt(self.ctx.ref(), self.solver)
7409
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 7380 of file z3py.py.

7380 def model(self):
7381 """Return a model for the last `check()`.
7382
7383 This function raises an exception if
7384 a model is not available (e.g., last `check()` returned unsat).
7385
7386 >>> s = Solver()
7387 >>> a = Int('a')
7388 >>> s.add(a + 2 == 0)
7389 >>> s.check()
7390 sat
7391 >>> s.model()
7392 [a = -2]
7393 """
7394 try:
7395 return ModelRef(Z3_solver_get_model(self.ctx.ref(), self.solver), self.ctx)
7396 except Z3Exception:
7397 raise Z3Exception("model is not available")
7398
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 7515 of file z3py.py.

7515 def next(self, t):
7516 """Retrieve congruence closure sibling of the term t relative to the current search state
7517 The function primarily works for SimpleSolver. Terms and variables that are
7518 eliminated during pre-processing are not visible to the congruence closure.
7519 """
7520 t = _py2expr(t, self.ctx)
7521 return _to_expr_ref(Z3_solver_congruence_next(self.ctx.ref(), self.solver, t.ast), self.ctx)
7522
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 7565 of file z3py.py.

7565 def non_units(self):
7566 """Return an AST vector containing all atomic formulas in solver state that are not units.
7567 """
7568 return AstVector(Z3_solver_get_non_units(self.ctx.ref(), self.solver), self.ctx)
7569
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 7233 of file z3py.py.

7233 def num_scopes(self):
7234 """Return the current number of backtracking points.
7235
7236 >>> s = Solver()
7237 >>> s.num_scopes()
7238 0
7239 >>> s.push()
7240 >>> s.num_scopes()
7241 1
7242 >>> s.push()
7243 >>> s.num_scopes()
7244 2
7245 >>> s.pop()
7246 >>> s.num_scopes()
7247 1
7248 """
7249 return Z3_solver_get_num_scopes(self.ctx.ref(), self.solver)
7250
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 7625 of file z3py.py.

7625 def param_descrs(self):
7626 """Return the parameter description set."""
7627 return ParamDescrsRef(Z3_solver_get_param_descrs(self.ctx.ref(), self.solver), self.ctx)
7628
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 7211 of file z3py.py.

7211 def pop(self, num=1):
7212 """Backtrack \\c num backtracking points.
7213
7214 >>> x = Int('x')
7215 >>> s = Solver()
7216 >>> s.add(x > 0)
7217 >>> s
7218 [x > 0]
7219 >>> s.push()
7220 >>> s.add(x < 1)
7221 >>> s
7222 [x > 0, x < 1]
7223 >>> s.check()
7224 unsat
7225 >>> s.pop()
7226 >>> s.check()
7227 sat
7228 >>> s
7229 [x > 0]
7230 """
7231 Z3_solver_pop(self.ctx.ref(), self.solver, num)
7232
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 7542 of file z3py.py.

7542 def proof(self):
7543 """Return a proof for the last `check()`. Proof construction must be enabled."""
7544 return _to_expr_ref(Z3_solver_get_proof(self.ctx.ref(), self.solver), self.ctx)
7545
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 7189 of file z3py.py.

7189 def push(self):
7190 """Create a backtracking point.
7191
7192 >>> x = Int('x')
7193 >>> s = Solver()
7194 >>> s.add(x > 0)
7195 >>> s
7196 [x > 0]
7197 >>> s.push()
7198 >>> s.add(x < 1)
7199 >>> s
7200 [x > 0, x < 1]
7201 >>> s.check()
7202 unsat
7203 >>> s.pop()
7204 >>> s.check()
7205 sat
7206 >>> s
7207 [x > 0]
7208 """
7209 Z3_solver_push(self.ctx.ref(), self.solver)
7210
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 7608 of file z3py.py.

7608 def reason_unknown(self):
7609 """Return a string describing why the last `check()` returned `unknown`.
7610
7611 >>> x = Int('x')
7612 >>> s = SimpleSolver()
7613 >>> s.add(x == 2**x)
7614 >>> s.check()
7615 unknown
7616 >>> s.reason_unknown()
7617 '(incomplete (theory arithmetic))'
7618 """
7619 return Z3_solver_get_reason_unknown(self.ctx.ref(), self.solver)
7620
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 7251 of file z3py.py.

7251 def reset(self):
7252 """Remove all asserted constraints and backtracking points created using `push()`.
7253
7254 >>> x = Int('x')
7255 >>> s = Solver()
7256 >>> s.add(x > 0)
7257 >>> s
7258 [x > 0]
7259 >>> s.reset()
7260 >>> s
7261 []
7262 """
7263 Z3_solver_reset(self.ctx.ref(), self.solver)
7264
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 7507 of file z3py.py.

7507 def root(self, t):
7508 """Retrieve congruence closure root of the term t relative to the current search state
7509 The function primarily works for SimpleSolver. Terms and variables that are
7510 eliminated during pre-processing are not visible to the congruence closure.
7511 """
7512 t = _py2expr(t, self.ctx)
7513 return _to_expr_ref(Z3_solver_congruence_root(self.ctx.ref(), self.solver, t.ast), self.ctx)
7514
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 7176 of file z3py.py.

7176 def set(self, *args, **keys):
7177 """Set a configuration option.
7178 The method `help()` return a string containing all available options.
7179
7180 >>> s = Solver()
7181 >>> # The option MBQI can be set using three different approaches.
7182 >>> s.set(mbqi=True)
7183 >>> s.set('MBQI', True)
7184 >>> s.set(':mbqi', True)
7185 """
7186 p = args2params(args, keys, self.ctx)
7187 Z3_solver_set_params(self.ctx.ref(), self.solver, p.params)
7188
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 7578 of file z3py.py.

7578 def set_initial_value(self, var, value):
7579 """initialize the solver's state by setting the initial value of var to value
7580 """
7581 s = var.sort()
7582 value = s.cast(value)
7583 Z3_solver_set_initial_value(self.ctx.ref(), self.solver, var.ast, value.ast)
7584
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 7652 of file z3py.py.

7652 def sexpr(self):
7653 """Return a formatted string (in Lisp-like format) with all added constraints.
7654 We say the string is in s-expression format.
7655
7656 >>> x = Int('x')
7657 >>> s = Solver()
7658 >>> s.add(x > 0)
7659 >>> s.add(x < 2)
7660 >>> r = s.sexpr()
7661 """
7662 return Z3_solver_to_string(self.ctx.ref(), self.solver)
7663
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 7530 of file z3py.py.

7530 def solve_for(self, ts):
7531 """Retrieve a solution for t relative to linear equations maintained in the current state."""
7532 vars = AstVector(ctx=self.ctx);
7533 terms = AstVector(ctx=self.ctx);
7534 guards = AstVector(ctx=self.ctx);
7535 for t in ts:
7536 t = _py2expr(t, self.ctx)
7537 vars.push(t)
7538 Z3_solver_solve_for(self.ctx.ref(), self.solver, vars.vector, terms.vector, guards.vector)
7539 return [(vars[i], terms[i], guards[i]) for i in range(len(vars))]
7540
7541
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 7590 of file z3py.py.

7590 def statistics(self):
7591 """Return statistics for the last `check()`.
7592
7593 >>> s = SimpleSolver()
7594 >>> x = Int('x')
7595 >>> s.add(x > 0)
7596 >>> s.check()
7597 sat
7598 >>> st = s.statistics()
7599 >>> st.get_key_value('final checks')
7600 1
7601 >>> len(st) > 0
7602 True
7603 >>> st[0] != 0
7604 True
7605 """
7606 return Statistics(Z3_solver_get_statistics(self.ctx.ref(), self.solver), self.ctx)
7607
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 7668 of file z3py.py.

7668 def to_smt2(self):
7669 """return SMTLIB2 formatted benchmark for solver's assertions"""
7670 es = self.assertions()
7671 sz = len(es)
7672 sz1 = sz
7673 if sz1 > 0:
7674 sz1 -= 1
7675 v = (Ast * sz1)()
7676 for i in range(sz1):
7677 v[i] = es[i].as_ast()
7678 if sz > 0:
7679 e = es[sz1].as_ast()
7680 else:
7681 e = BoolVal(True, self.ctx).as_ast()
7683 self.ctx.ref(), "benchmark generated from python API", "", "unknown", "", sz1, v, e,
7684 )
7685
7686
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 7585 of file z3py.py.

7585 def trail(self):
7586 """Return trail of the solver state after a check() call.
7587 """
7588 return AstVector(Z3_solver_get_trail(self.ctx.ref(), self.solver), self.ctx)
7589
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 7570 of file z3py.py.

7570 def trail_levels(self):
7571 """Return trail and decision levels of the solver state after a check() call.
7572 """
7573 trail = self.trail()
7574 levels = (ctypes.c_uint * len(trail))()
7575 Z3_solver_get_levels(self.ctx.ref(), self.solver, trail.vector, len(trail), levels)
7576 return trail, levels
7577
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 7633 of file z3py.py.

7633 def translate(self, target):
7634 """Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.
7635
7636 >>> c1 = Context()
7637 >>> c2 = Context()
7638 >>> s1 = Solver(ctx=c1)
7639 >>> s2 = s1.translate(c2)
7640 """
7641 if z3_debug():
7642 _z3_assert(isinstance(target, Context), "argument must be a Z3 context")
7643 solver = Z3_solver_translate(self.ctx.ref(), self.solver, target.ref())
7644 return Solver(solver, target)
7645
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 7560 of file z3py.py.

7560 def units(self):
7561 """Return an AST vector containing all currently inferred units.
7562 """
7563 return AstVector(Z3_solver_get_units(self.ctx.ref(), self.solver), self.ctx)
7564
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 7410 of file z3py.py.

7410 def unsat_core(self):
7411 """Return a subset (as an AST vector) of the assumptions provided to the last check().
7412
7413 These are the assumptions Z3 used in the unsatisfiability proof.
7414 Assumptions are available in Z3. They are used to extract unsatisfiable cores.
7415 They may be also used to "retract" assumptions. Note that, assumptions are not really
7416 "soft constraints", but they can be used to implement them.
7417
7418 >>> p1, p2, p3 = Bools('p1 p2 p3')
7419 >>> x, y = Ints('x y')
7420 >>> s = Solver()
7421 >>> s.add(Implies(p1, x > 0))
7422 >>> s.add(Implies(p2, y > x))
7423 >>> s.add(Implies(p2, y < 1))
7424 >>> s.add(Implies(p3, y > -3))
7425 >>> s.check(p1, p2, p3)
7426 unsat
7427 >>> core = s.unsat_core()
7428 >>> len(core)
7429 2
7430 >>> p1 in core
7431 True
7432 >>> p2 in core
7433 True
7434 >>> p3 in core
7435 False
7436 >>> # "Retracting" p2
7437 >>> s.check(p1, p3)
7438 sat
7439 """
7440 return AstVector(Z3_solver_get_unsat_core(self.ctx.ref(), self.solver), self.ctx)
7441
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 7155 of file z3py.py.

◆ ctx

ctx = _get_ctx(ctx)

Definition at line 7154 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 7486 of file z3py.py.

◆ solver

solver = None

Definition at line 7156 of file z3py.py.

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