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

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 7049 of file z3py.py.

7049 def __init__(self, solver=None, ctx=None, logFile=None):
7050 assert solver is None or ctx is not None
7051 self.ctx = _get_ctx(ctx)
7052 self.backtrack_level = 4000000000
7053 self.solver = None
7054 if solver is None:
7055 self.solver = Z3_mk_solver(self.ctx.ref())
7056 else:
7057 self.solver = solver
7058 Z3_solver_inc_ref(self.ctx.ref(), self.solver)
7059 if logFile is not None:
7060 self.set("smtlib2_log", logFile)
7061
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 7062 of file z3py.py.

7062 def __del__(self):
7063 if self.solver is not None and self.ctx.ref() is not None and Z3_solver_dec_ref is not None:
7064 Z3_solver_dec_ref(self.ctx.ref(), self.solver)
7065
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 7543 of file z3py.py.

7543 def __copy__(self):
7544 return self.translate(self.ctx)
7545

◆ __deepcopy__()

__deepcopy__ ( self,
memo = {} )

Definition at line 7546 of file z3py.py.

7546 def __deepcopy__(self, memo={}):
7547 return self.translate(self.ctx)
7548

◆ __enter__()

__enter__ ( self)

Definition at line 7066 of file z3py.py.

7066 def __enter__(self):
7067 self.push()
7068 return self
7069

◆ __exit__()

__exit__ ( self,
* exc_info )

Definition at line 7070 of file z3py.py.

7070 def __exit__(self, *exc_info):
7071 self.pop()
7072

◆ __iadd__()

__iadd__ ( self,
fml )

Definition at line 7192 of file z3py.py.

7192 def __iadd__(self, fml):
7193 self.add(fml)
7194 return self
7195

◆ __repr__()

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

Definition at line 7526 of file z3py.py.

7526 def __repr__(self):
7527 """Return a formatted string with all added constraints."""
7528 return obj_to_string(self)
7529

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

7181 def add(self, *args):
7182 """Assert constraints into the solver.
7183
7184 >>> x = Int('x')
7185 >>> s = Solver()
7186 >>> s.add(x > 0, x < 2)
7187 >>> s
7188 [x > 0, x < 2]
7189 """
7190 self.assert_exprs(*args)
7191

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

7196 def append(self, *args):
7197 """Assert constraints into the solver.
7198
7199 >>> x = Int('x')
7200 >>> s = Solver()
7201 >>> s.append(x > 0, x < 2)
7202 >>> s
7203 [x > 0, x < 2]
7204 """
7205 self.assert_exprs(*args)
7206

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

7218 def assert_and_track(self, a, p):
7219 """Assert constraint `a` and track it in the unsat core using the Boolean constant `p`.
7220
7221 If `p` is a string, it will be automatically converted into a Boolean constant.
7222
7223 >>> x = Int('x')
7224 >>> p3 = Bool('p3')
7225 >>> s = Solver()
7226 >>> s.set(unsat_core=True)
7227 >>> s.assert_and_track(x > 0, 'p1')
7228 >>> s.assert_and_track(x != 1, 'p2')
7229 >>> s.assert_and_track(x < 0, p3)
7230 >>> print(s.check())
7231 unsat
7232 >>> c = s.unsat_core()
7233 >>> len(c)
7234 2
7235 >>> Bool('p1') in c
7236 True
7237 >>> Bool('p2') in c
7238 False
7239 >>> p3 in c
7240 True
7241 """
7242 if isinstance(p, str):
7243 p = Bool(p, self.ctx)
7244 _z3_assert(isinstance(a, BoolRef), "Boolean expression expected")
7245 _z3_assert(isinstance(p, BoolRef) and is_const(p), "Boolean expression expected")
7246 Z3_solver_assert_and_track(self.ctx.ref(), self.solver, a.as_ast(), p.as_ast())
7247
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 7162 of file z3py.py.

7162 def assert_exprs(self, *args):
7163 """Assert constraints into the solver.
7164
7165 >>> x = Int('x')
7166 >>> s = Solver()
7167 >>> s.assert_exprs(x > 0, x < 2)
7168 >>> s
7169 [x > 0, x < 2]
7170 """
7171 args = _get_args(args)
7172 s = BoolSort(self.ctx)
7173 for arg in args:
7174 if isinstance(arg, Goal) or isinstance(arg, AstVector):
7175 for f in arg:
7176 Z3_solver_assert(self.ctx.ref(), self.solver, f.as_ast())
7177 else:
7178 arg = s.cast(arg)
7179 Z3_solver_assert(self.ctx.ref(), self.solver, arg.as_ast())
7180
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 7443 of file z3py.py.

7443 def assertions(self):
7444 """Return an AST vector containing all added constraints.
7445
7446 >>> s = Solver()
7447 >>> s.assertions()
7448 []
7449 >>> a = Int('a')
7450 >>> s.add(a > 0)
7451 >>> s.add(a < 10)
7452 >>> s.assertions()
7453 [a > 0, a < 10]
7454 """
7455 return AstVector(Z3_solver_get_assertions(self.ctx.ref(), self.solver), self.ctx)
7456
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()
unknown

Definition at line 7248 of file z3py.py.

7248 def check(self, *assumptions):
7249 """Check whether the assertions in the given solver plus the optional assumptions are consistent or not.
7250
7251 >>> x = Int('x')
7252 >>> s = Solver()
7253 >>> s.check()
7254 sat
7255 >>> s.add(x > 0, x < 2)
7256 >>> s.check()
7257 sat
7258 >>> s.model().eval(x)
7259 1
7260 >>> s.add(x < 1)
7261 >>> s.check()
7262 unsat
7263 >>> s.reset()
7264 >>> s.add(2**x == 4)
7265 >>> s.check()
7266 unknown
7267 """
7268 s = BoolSort(self.ctx)
7269 assumptions = _get_args(assumptions)
7270 num = len(assumptions)
7271 _assumptions = (Ast * num)()
7272 for i in range(num):
7273 _assumptions[i] = s.cast(assumptions[i]).as_ast()
7274 r = Z3_solver_check_assumptions(self.ctx.ref(), self.solver, num, _assumptions)
7275 return CheckSatResult(r)
7276
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 7339 of file z3py.py.

7339 def consequences(self, assumptions, variables):
7340 """Determine fixed values for the variables based on the solver state and assumptions.
7341 >>> s = Solver()
7342 >>> a, b, c, d = Bools('a b c d')
7343 >>> s.add(Implies(a,b), Implies(b, c))
7344 >>> s.consequences([a],[b,c,d])
7345 (sat, [Implies(a, b), Implies(a, c)])
7346 >>> s.consequences([Not(c),d],[a,b,c,d])
7347 (sat, [Implies(d, d), Implies(Not(c), Not(c)), Implies(Not(c), Not(b)), Implies(Not(c), Not(a))])
7348 """
7349 if isinstance(assumptions, list):
7350 _asms = AstVector(None, self.ctx)
7351 for a in assumptions:
7352 _asms.push(a)
7353 assumptions = _asms
7354 if isinstance(variables, list):
7355 _vars = AstVector(None, self.ctx)
7356 for a in variables:
7357 _vars.push(a)
7358 variables = _vars
7359 _z3_assert(isinstance(assumptions, AstVector), "ast vector expected")
7360 _z3_assert(isinstance(variables, AstVector), "ast vector expected")
7361 consequences = AstVector(None, self.ctx)
7362 r = Z3_solver_get_consequences(self.ctx.ref(), self.solver, assumptions.vector,
7363 variables.vector, consequences.vector)
7364 sz = len(consequences)
7365 consequences = [consequences[i] for i in range(sz)]
7366 return CheckSatResult(r), consequences
7367
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 7376 of file z3py.py.

7376 def cube(self, vars=None):
7377 """Get set of cubes
7378 The method takes an optional set of variables that restrict which
7379 variables may be used as a starting point for cubing.
7380 If vars is not None, then the first case split is based on a variable in
7381 this set.
7382 """
7383 self.cube_vs = AstVector(None, self.ctx)
7384 if vars is not None:
7385 for v in vars:
7386 self.cube_vs.push(v)
7387 while True:
7388 lvl = self.backtrack_level
7389 self.backtrack_level = 4000000000
7390 r = AstVector(Z3_solver_cube(self.ctx.ref(), self.solver, self.cube_vs.vector, lvl), self.ctx)
7391 if (len(r) == 1 and is_false(r[0])):
7392 return
7393 yield r
7394 if (len(r) == 0):
7395 return
7396
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 7397 of file z3py.py.

7397 def cube_vars(self):
7398 """Access the set of variables that were touched by the most recently generated cube.
7399 This set of variables can be used as a starting point for additional cubes.
7400 The idea is that variables that appear in clauses that are reduced by the most recent
7401 cube are likely more useful to cube on."""
7402 return self.cube_vs
7403

◆ dimacs()

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

Definition at line 7561 of file z3py.py.

7561 def dimacs(self, include_names=True):
7562 """Return a textual representation of the solver in DIMACS format."""
7563 return Z3_solver_to_dimacs_string(self.ctx.ref(), self.solver, include_names)
7564
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 7420 of file z3py.py.

7420 def explain_congruent(self, a, b):
7421 """Explain congruence of a and b relative to the current search state"""
7422 a = _py2expr(a, self.ctx)
7423 b = _py2expr(b, self.ctx)
7424 return _to_expr_ref(Z3_solver_congruence_explain(self.ctx.ref(), self.solver, a.ast, b.ast), self.ctx)
7425
7426
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 7368 of file z3py.py.

7368 def from_file(self, filename):
7369 """Parse assertions from a file"""
7370 Z3_solver_from_file(self.ctx.ref(), self.solver, filename)
7371
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 7372 of file z3py.py.

7372 def from_string(self, s):
7373 """Parse assertions from a string"""
7374 Z3_solver_from_string(self.ctx.ref(), self.solver, s)
7375
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 7518 of file z3py.py.

7518 def help(self):
7519 """Display a string describing all available options."""
7520 print(Z3_solver_get_help(self.ctx.ref(), self.solver))
7521
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 7296 of file z3py.py.

7296 def import_model_converter(self, other):
7297 """Import model converter from other into the current solver"""
7298 Z3_solver_import_model_converter(self.ctx.ref(), other.solver, self.solver)
7299

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

7207 def insert(self, *args):
7208 """Assert constraints into the solver.
7209
7210 >>> x = Int('x')
7211 >>> s = Solver()
7212 >>> s.insert(x > 0, x < 2)
7213 >>> s
7214 [x > 0, x < 2]
7215 """
7216 self.assert_exprs(*args)
7217

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

7300 def interrupt(self):
7301 """Interrupt the execution of the solver object.
7302 Remarks: This ensures that the interrupt applies only
7303 to the given solver object and it applies only if it is running.
7304 """
7305 Z3_solver_interrupt(self.ctx.ref(), self.solver)
7306
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 7277 of file z3py.py.

7277 def model(self):
7278 """Return a model for the last `check()`.
7279
7280 This function raises an exception if
7281 a model is not available (e.g., last `check()` returned unsat).
7282
7283 >>> s = Solver()
7284 >>> a = Int('a')
7285 >>> s.add(a + 2 == 0)
7286 >>> s.check()
7287 sat
7288 >>> s.model()
7289 [a = -2]
7290 """
7291 try:
7292 return ModelRef(Z3_solver_get_model(self.ctx.ref(), self.solver), self.ctx)
7293 except Z3Exception:
7294 raise Z3Exception("model is not available")
7295
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 7412 of file z3py.py.

7412 def next(self, t):
7413 """Retrieve congruence closure sibling of the term t relative to the current search state
7414 The function primarily works for SimpleSolver. Terms and variables that are
7415 eliminated during pre-processing are not visible to the congruence closure.
7416 """
7417 t = _py2expr(t, self.ctx)
7418 return _to_expr_ref(Z3_solver_congruence_next(self.ctx.ref(), self.solver, t.ast), self.ctx)
7419
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 7462 of file z3py.py.

7462 def non_units(self):
7463 """Return an AST vector containing all atomic formulas in solver state that are not units.
7464 """
7465 return AstVector(Z3_solver_get_non_units(self.ctx.ref(), self.solver), self.ctx)
7466
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 7130 of file z3py.py.

7130 def num_scopes(self):
7131 """Return the current number of backtracking points.
7132
7133 >>> s = Solver()
7134 >>> s.num_scopes()
7135 0
7136 >>> s.push()
7137 >>> s.num_scopes()
7138 1
7139 >>> s.push()
7140 >>> s.num_scopes()
7141 2
7142 >>> s.pop()
7143 >>> s.num_scopes()
7144 1
7145 """
7146 return Z3_solver_get_num_scopes(self.ctx.ref(), self.solver)
7147
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 7522 of file z3py.py.

7522 def param_descrs(self):
7523 """Return the parameter description set."""
7524 return ParamDescrsRef(Z3_solver_get_param_descrs(self.ctx.ref(), self.solver), self.ctx)
7525
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 7108 of file z3py.py.

7108 def pop(self, num=1):
7109 """Backtrack \\c num backtracking points.
7110
7111 >>> x = Int('x')
7112 >>> s = Solver()
7113 >>> s.add(x > 0)
7114 >>> s
7115 [x > 0]
7116 >>> s.push()
7117 >>> s.add(x < 1)
7118 >>> s
7119 [x > 0, x < 1]
7120 >>> s.check()
7121 unsat
7122 >>> s.pop()
7123 >>> s.check()
7124 sat
7125 >>> s
7126 [x > 0]
7127 """
7128 Z3_solver_pop(self.ctx.ref(), self.solver, num)
7129
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 7439 of file z3py.py.

7439 def proof(self):
7440 """Return a proof for the last `check()`. Proof construction must be enabled."""
7441 return _to_expr_ref(Z3_solver_get_proof(self.ctx.ref(), self.solver), self.ctx)
7442
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 7086 of file z3py.py.

7086 def push(self):
7087 """Create a backtracking point.
7088
7089 >>> x = Int('x')
7090 >>> s = Solver()
7091 >>> s.add(x > 0)
7092 >>> s
7093 [x > 0]
7094 >>> s.push()
7095 >>> s.add(x < 1)
7096 >>> s
7097 [x > 0, x < 1]
7098 >>> s.check()
7099 unsat
7100 >>> s.pop()
7101 >>> s.check()
7102 sat
7103 >>> s
7104 [x > 0]
7105 """
7106 Z3_solver_push(self.ctx.ref(), self.solver)
7107
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(2**x == 4)
>>> s.check()
unknown
>>> s.reason_unknown()
'(incomplete (theory arithmetic))'

Definition at line 7505 of file z3py.py.

7505 def reason_unknown(self):
7506 """Return a string describing why the last `check()` returned `unknown`.
7507
7508 >>> x = Int('x')
7509 >>> s = SimpleSolver()
7510 >>> s.add(2**x == 4)
7511 >>> s.check()
7512 unknown
7513 >>> s.reason_unknown()
7514 '(incomplete (theory arithmetic))'
7515 """
7516 return Z3_solver_get_reason_unknown(self.ctx.ref(), self.solver)
7517
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 7148 of file z3py.py.

7148 def reset(self):
7149 """Remove all asserted constraints and backtracking points created using `push()`.
7150
7151 >>> x = Int('x')
7152 >>> s = Solver()
7153 >>> s.add(x > 0)
7154 >>> s
7155 [x > 0]
7156 >>> s.reset()
7157 >>> s
7158 []
7159 """
7160 Z3_solver_reset(self.ctx.ref(), self.solver)
7161
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 7404 of file z3py.py.

7404 def root(self, t):
7405 """Retrieve congruence closure root of the term t relative to the current search state
7406 The function primarily works for SimpleSolver. Terms and variables that are
7407 eliminated during pre-processing are not visible to the congruence closure.
7408 """
7409 t = _py2expr(t, self.ctx)
7410 return _to_expr_ref(Z3_solver_congruence_root(self.ctx.ref(), self.solver, t.ast), self.ctx)
7411
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 7073 of file z3py.py.

7073 def set(self, *args, **keys):
7074 """Set a configuration option.
7075 The method `help()` return a string containing all available options.
7076
7077 >>> s = Solver()
7078 >>> # The option MBQI can be set using three different approaches.
7079 >>> s.set(mbqi=True)
7080 >>> s.set('MBQI', True)
7081 >>> s.set(':mbqi', True)
7082 """
7083 p = args2params(args, keys, self.ctx)
7084 Z3_solver_set_params(self.ctx.ref(), self.solver, p.params)
7085
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 7475 of file z3py.py.

7475 def set_initial_value(self, var, value):
7476 """initialize the solver's state by setting the initial value of var to value
7477 """
7478 s = var.sort()
7479 value = s.cast(value)
7480 Z3_solver_set_initial_value(self.ctx.ref(), self.solver, var.ast, value.ast)
7481
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 7549 of file z3py.py.

7549 def sexpr(self):
7550 """Return a formatted string (in Lisp-like format) with all added constraints.
7551 We say the string is in s-expression format.
7552
7553 >>> x = Int('x')
7554 >>> s = Solver()
7555 >>> s.add(x > 0)
7556 >>> s.add(x < 2)
7557 >>> r = s.sexpr()
7558 """
7559 return Z3_solver_to_string(self.ctx.ref(), self.solver)
7560
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 7427 of file z3py.py.

7427 def solve_for(self, ts):
7428 """Retrieve a solution for t relative to linear equations maintained in the current state."""
7429 vars = AstVector(ctx=self.ctx);
7430 terms = AstVector(ctx=self.ctx);
7431 guards = AstVector(ctx=self.ctx);
7432 for t in ts:
7433 t = _py2expr(t, self.ctx)
7434 vars.push(t)
7435 Z3_solver_solve_for(self.ctx.ref(), self.solver, vars.vector, terms.vector, guards.vector)
7436 return [(vars[i], terms[i], guards[i]) for i in range(len(vars))]
7437
7438
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 7487 of file z3py.py.

7487 def statistics(self):
7488 """Return statistics for the last `check()`.
7489
7490 >>> s = SimpleSolver()
7491 >>> x = Int('x')
7492 >>> s.add(x > 0)
7493 >>> s.check()
7494 sat
7495 >>> st = s.statistics()
7496 >>> st.get_key_value('final checks')
7497 1
7498 >>> len(st) > 0
7499 True
7500 >>> st[0] != 0
7501 True
7502 """
7503 return Statistics(Z3_solver_get_statistics(self.ctx.ref(), self.solver), self.ctx)
7504
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 7565 of file z3py.py.

7565 def to_smt2(self):
7566 """return SMTLIB2 formatted benchmark for solver's assertions"""
7567 es = self.assertions()
7568 sz = len(es)
7569 sz1 = sz
7570 if sz1 > 0:
7571 sz1 -= 1
7572 v = (Ast * sz1)()
7573 for i in range(sz1):
7574 v[i] = es[i].as_ast()
7575 if sz > 0:
7576 e = es[sz1].as_ast()
7577 else:
7578 e = BoolVal(True, self.ctx).as_ast()
7580 self.ctx.ref(), "benchmark generated from python API", "", "unknown", "", sz1, v, e,
7581 )
7582
7583
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 7482 of file z3py.py.

7482 def trail(self):
7483 """Return trail of the solver state after a check() call.
7484 """
7485 return AstVector(Z3_solver_get_trail(self.ctx.ref(), self.solver), self.ctx)
7486
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 7467 of file z3py.py.

7467 def trail_levels(self):
7468 """Return trail and decision levels of the solver state after a check() call.
7469 """
7470 trail = self.trail()
7471 levels = (ctypes.c_uint * len(trail))()
7472 Z3_solver_get_levels(self.ctx.ref(), self.solver, trail.vector, len(trail), levels)
7473 return trail, levels
7474
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 7530 of file z3py.py.

7530 def translate(self, target):
7531 """Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.
7532
7533 >>> c1 = Context()
7534 >>> c2 = Context()
7535 >>> s1 = Solver(ctx=c1)
7536 >>> s2 = s1.translate(c2)
7537 """
7538 if z3_debug():
7539 _z3_assert(isinstance(target, Context), "argument must be a Z3 context")
7540 solver = Z3_solver_translate(self.ctx.ref(), self.solver, target.ref())
7541 return Solver(solver, target)
7542
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 7457 of file z3py.py.

7457 def units(self):
7458 """Return an AST vector containing all currently inferred units.
7459 """
7460 return AstVector(Z3_solver_get_units(self.ctx.ref(), self.solver), self.ctx)
7461
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 7307 of file z3py.py.

7307 def unsat_core(self):
7308 """Return a subset (as an AST vector) of the assumptions provided to the last check().
7309
7310 These are the assumptions Z3 used in the unsatisfiability proof.
7311 Assumptions are available in Z3. They are used to extract unsatisfiable cores.
7312 They may be also used to "retract" assumptions. Note that, assumptions are not really
7313 "soft constraints", but they can be used to implement them.
7314
7315 >>> p1, p2, p3 = Bools('p1 p2 p3')
7316 >>> x, y = Ints('x y')
7317 >>> s = Solver()
7318 >>> s.add(Implies(p1, x > 0))
7319 >>> s.add(Implies(p2, y > x))
7320 >>> s.add(Implies(p2, y < 1))
7321 >>> s.add(Implies(p3, y > -3))
7322 >>> s.check(p1, p2, p3)
7323 unsat
7324 >>> core = s.unsat_core()
7325 >>> len(core)
7326 2
7327 >>> p1 in core
7328 True
7329 >>> p2 in core
7330 True
7331 >>> p3 in core
7332 False
7333 >>> # "Retracting" p2
7334 >>> s.check(p1, p3)
7335 sat
7336 """
7337 return AstVector(Z3_solver_get_unsat_core(self.ctx.ref(), self.solver), self.ctx)
7338
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 7052 of file z3py.py.

◆ ctx

ctx = _get_ctx(ctx)

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

◆ solver

solver = None

Definition at line 7053 of file z3py.py.

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