Z3
 
Loading...
Searching...
No Matches
Public Member Functions | Data Fields
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
 
 backtrack_level
 
 solver
 
 cube_vs
 

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

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 7077 of file z3py.py.

7077 def __init__(self, solver=None, ctx=None, logFile=None):
7078 assert solver is None or ctx is not None
7079 self.ctx = _get_ctx(ctx)
7080 self.backtrack_level = 4000000000
7081 self.solver = None
7082 if solver is None:
7083 self.solver = Z3_mk_solver(self.ctx.ref())
7084 else:
7085 self.solver = solver
7086 Z3_solver_inc_ref(self.ctx.ref(), self.solver)
7087 if logFile is not None:
7088 self.set("smtlib2_log", logFile)
7089
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 7090 of file z3py.py.

7090 def __del__(self):
7091 if self.solver is not None and self.ctx.ref() is not None and Z3_solver_dec_ref is not None:
7092 Z3_solver_dec_ref(self.ctx.ref(), self.solver)
7093
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 7571 of file z3py.py.

7571 def __copy__(self):
7572 return self.translate(self.ctx)
7573

◆ __deepcopy__()

__deepcopy__ (   self,
  memo = {} 
)

Definition at line 7574 of file z3py.py.

7574 def __deepcopy__(self, memo={}):
7575 return self.translate(self.ctx)
7576

◆ __enter__()

__enter__ (   self)

Definition at line 7094 of file z3py.py.

7094 def __enter__(self):
7095 self.push()
7096 return self
7097

◆ __exit__()

__exit__ (   self,
exc_info 
)

Definition at line 7098 of file z3py.py.

7098 def __exit__(self, *exc_info):
7099 self.pop()
7100

◆ __iadd__()

__iadd__ (   self,
  fml 
)

Definition at line 7220 of file z3py.py.

7220 def __iadd__(self, fml):
7221 self.add(fml)
7222 return self
7223

◆ __repr__()

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

Definition at line 7554 of file z3py.py.

7554 def __repr__(self):
7555 """Return a formatted string with all added constraints."""
7556 return obj_to_string(self)
7557

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

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

Referenced by Solver.__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 7224 of file z3py.py.

7224 def append(self, *args):
7225 """Assert constraints into the solver.
7226
7227 >>> x = Int('x')
7228 >>> s = Solver()
7229 >>> s.append(x > 0, x < 2)
7230 >>> s
7231 [x > 0, x < 2]
7232 """
7233 self.assert_exprs(*args)
7234

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

7246 def assert_and_track(self, a, p):
7247 """Assert constraint `a` and track it in the unsat core using the Boolean constant `p`.
7248
7249 If `p` is a string, it will be automatically converted into a Boolean constant.
7250
7251 >>> x = Int('x')
7252 >>> p3 = Bool('p3')
7253 >>> s = Solver()
7254 >>> s.set(unsat_core=True)
7255 >>> s.assert_and_track(x > 0, 'p1')
7256 >>> s.assert_and_track(x != 1, 'p2')
7257 >>> s.assert_and_track(x < 0, p3)
7258 >>> print(s.check())
7259 unsat
7260 >>> c = s.unsat_core()
7261 >>> len(c)
7262 2
7263 >>> Bool('p1') in c
7264 True
7265 >>> Bool('p2') in c
7266 False
7267 >>> p3 in c
7268 True
7269 """
7270 if isinstance(p, str):
7271 p = Bool(p, self.ctx)
7272 _z3_assert(isinstance(a, BoolRef), "Boolean expression expected")
7273 _z3_assert(isinstance(p, BoolRef) and is_const(p), "Boolean expression expected")
7274 Z3_solver_assert_and_track(self.ctx.ref(), self.solver, a.as_ast(), p.as_ast())
7275
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 7190 of file z3py.py.

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

Referenced by Goal.add(), Solver.add(), Goal.append(), Solver.append(), Goal.insert(), and Solver.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 7471 of file z3py.py.

7471 def assertions(self):
7472 """Return an AST vector containing all added constraints.
7473
7474 >>> s = Solver()
7475 >>> s.assertions()
7476 []
7477 >>> a = Int('a')
7478 >>> s.add(a > 0)
7479 >>> s.add(a < 10)
7480 >>> s.assertions()
7481 [a > 0, a < 10]
7482 """
7483 return AstVector(Z3_solver_get_assertions(self.ctx.ref(), self.solver), self.ctx)
7484
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 7276 of file z3py.py.

7276 def check(self, *assumptions):
7277 """Check whether the assertions in the given solver plus the optional assumptions are consistent or not.
7278
7279 >>> x = Int('x')
7280 >>> s = Solver()
7281 >>> s.check()
7282 sat
7283 >>> s.add(x > 0, x < 2)
7284 >>> s.check()
7285 sat
7286 >>> s.model().eval(x)
7287 1
7288 >>> s.add(x < 1)
7289 >>> s.check()
7290 unsat
7291 >>> s.reset()
7292 >>> s.add(2**x == 4)
7293 >>> s.check()
7294 sat
7295 """
7296 s = BoolSort(self.ctx)
7297 assumptions = _get_args(assumptions)
7298 num = len(assumptions)
7299 _assumptions = (Ast * num)()
7300 for i in range(num):
7301 _assumptions[i] = s.cast(assumptions[i]).as_ast()
7302 r = Z3_solver_check_assumptions(self.ctx.ref(), self.solver, num, _assumptions)
7303 return CheckSatResult(r)
7304
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 7367 of file z3py.py.

7367 def consequences(self, assumptions, variables):
7368 """Determine fixed values for the variables based on the solver state and assumptions.
7369 >>> s = Solver()
7370 >>> a, b, c, d = Bools('a b c d')
7371 >>> s.add(Implies(a,b), Implies(b, c))
7372 >>> s.consequences([a],[b,c,d])
7373 (sat, [Implies(a, b), Implies(a, c)])
7374 >>> s.consequences([Not(c),d],[a,b,c,d])
7375 (sat, [Implies(d, d), Implies(Not(c), Not(c)), Implies(Not(c), Not(b)), Implies(Not(c), Not(a))])
7376 """
7377 if isinstance(assumptions, list):
7378 _asms = AstVector(None, self.ctx)
7379 for a in assumptions:
7380 _asms.push(a)
7381 assumptions = _asms
7382 if isinstance(variables, list):
7383 _vars = AstVector(None, self.ctx)
7384 for a in variables:
7385 _vars.push(a)
7386 variables = _vars
7387 _z3_assert(isinstance(assumptions, AstVector), "ast vector expected")
7388 _z3_assert(isinstance(variables, AstVector), "ast vector expected")
7389 consequences = AstVector(None, self.ctx)
7390 r = Z3_solver_get_consequences(self.ctx.ref(), self.solver, assumptions.vector,
7391 variables.vector, consequences.vector)
7392 sz = len(consequences)
7393 consequences = [consequences[i] for i in range(sz)]
7394 return CheckSatResult(r), consequences
7395
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 7404 of file z3py.py.

7404 def cube(self, vars=None):
7405 """Get set of cubes
7406 The method takes an optional set of variables that restrict which
7407 variables may be used as a starting point for cubing.
7408 If vars is not None, then the first case split is based on a variable in
7409 this set.
7410 """
7411 self.cube_vs = AstVector(None, self.ctx)
7412 if vars is not None:
7413 for v in vars:
7414 self.cube_vs.push(v)
7415 while True:
7416 lvl = self.backtrack_level
7417 self.backtrack_level = 4000000000
7418 r = AstVector(Z3_solver_cube(self.ctx.ref(), self.solver, self.cube_vs.vector, lvl), self.ctx)
7419 if (len(r) == 1 and is_false(r[0])):
7420 return
7421 yield r
7422 if (len(r) == 0):
7423 return
7424
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 7425 of file z3py.py.

7425 def cube_vars(self):
7426 """Access the set of variables that were touched by the most recently generated cube.
7427 This set of variables can be used as a starting point for additional cubes.
7428 The idea is that variables that appear in clauses that are reduced by the most recent
7429 cube are likely more useful to cube on."""
7430 return self.cube_vs
7431

◆ dimacs()

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

Definition at line 7589 of file z3py.py.

7589 def dimacs(self, include_names=True):
7590 """Return a textual representation of the solver in DIMACS format."""
7591 return Z3_solver_to_dimacs_string(self.ctx.ref(), self.solver, include_names)
7592
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 7448 of file z3py.py.

7448 def explain_congruent(self, a, b):
7449 """Explain congruence of a and b relative to the current search state"""
7450 a = _py2expr(a, self.ctx)
7451 b = _py2expr(b, self.ctx)
7452 return _to_expr_ref(Z3_solver_congruence_explain(self.ctx.ref(), self.solver, a.ast, b.ast), self.ctx)
7453
7454
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 7396 of file z3py.py.

7396 def from_file(self, filename):
7397 """Parse assertions from a file"""
7398 Z3_solver_from_file(self.ctx.ref(), self.solver, filename)
7399
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 7400 of file z3py.py.

7400 def from_string(self, s):
7401 """Parse assertions from a string"""
7402 Z3_solver_from_string(self.ctx.ref(), self.solver, s)
7403
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 7546 of file z3py.py.

7546 def help(self):
7547 """Display a string describing all available options."""
7548 print(Z3_solver_get_help(self.ctx.ref(), self.solver))
7549
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 7324 of file z3py.py.

7324 def import_model_converter(self, other):
7325 """Import model converter from other into the current solver"""
7326 Z3_solver_import_model_converter(self.ctx.ref(), other.solver, self.solver)
7327

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

7235 def insert(self, *args):
7236 """Assert constraints into the solver.
7237
7238 >>> x = Int('x')
7239 >>> s = Solver()
7240 >>> s.insert(x > 0, x < 2)
7241 >>> s
7242 [x > 0, x < 2]
7243 """
7244 self.assert_exprs(*args)
7245

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

7328 def interrupt(self):
7329 """Interrupt the execution of the solver object.
7330 Remarks: This ensures that the interrupt applies only
7331 to the given solver object and it applies only if it is running.
7332 """
7333 Z3_solver_interrupt(self.ctx.ref(), self.solver)
7334
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 7305 of file z3py.py.

7305 def model(self):
7306 """Return a model for the last `check()`.
7307
7308 This function raises an exception if
7309 a model is not available (e.g., last `check()` returned unsat).
7310
7311 >>> s = Solver()
7312 >>> a = Int('a')
7313 >>> s.add(a + 2 == 0)
7314 >>> s.check()
7315 sat
7316 >>> s.model()
7317 [a = -2]
7318 """
7319 try:
7320 return ModelRef(Z3_solver_get_model(self.ctx.ref(), self.solver), self.ctx)
7321 except Z3Exception:
7322 raise Z3Exception("model is not available")
7323
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.

Referenced by ModelRef.__del__(), ModelRef.__getitem__(), ModelRef.__len__(), ModelRef.decls(), ModelRef.eval(), ModelRef.get_interp(), ModelRef.get_sort(), ModelRef.get_universe(), ModelRef.num_sorts(), ModelRef.project(), ModelRef.project_with_witness(), ModelRef.sexpr(), ModelRef.translate(), and ModelRef.update_value().

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

7440 def next(self, t):
7441 """Retrieve congruence closure sibling of the term t relative to the current search state
7442 The function primarily works for SimpleSolver. Terms and variables that are
7443 eliminated during pre-processing are not visible to the congruence closure.
7444 """
7445 t = _py2expr(t, self.ctx)
7446 return _to_expr_ref(Z3_solver_congruence_next(self.ctx.ref(), self.solver, t.ast), self.ctx)
7447
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 7490 of file z3py.py.

7490 def non_units(self):
7491 """Return an AST vector containing all atomic formulas in solver state that are not units.
7492 """
7493 return AstVector(Z3_solver_get_non_units(self.ctx.ref(), self.solver), self.ctx)
7494
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 7158 of file z3py.py.

7158 def num_scopes(self):
7159 """Return the current number of backtracking points.
7160
7161 >>> s = Solver()
7162 >>> s.num_scopes()
7163 0
7164 >>> s.push()
7165 >>> s.num_scopes()
7166 1
7167 >>> s.push()
7168 >>> s.num_scopes()
7169 2
7170 >>> s.pop()
7171 >>> s.num_scopes()
7172 1
7173 """
7174 return Z3_solver_get_num_scopes(self.ctx.ref(), self.solver)
7175
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 7550 of file z3py.py.

7550 def param_descrs(self):
7551 """Return the parameter description set."""
7552 return ParamDescrsRef(Z3_solver_get_param_descrs(self.ctx.ref(), self.solver), self.ctx)
7553
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 7136 of file z3py.py.

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

Referenced by Solver.__exit__().

◆ proof()

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

Definition at line 7467 of file z3py.py.

7467 def proof(self):
7468 """Return a proof for the last `check()`. Proof construction must be enabled."""
7469 return _to_expr_ref(Z3_solver_get_proof(self.ctx.ref(), self.solver), self.ctx)
7470
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 7114 of file z3py.py.

7114 def push(self):
7115 """Create a backtracking point.
7116
7117 >>> x = Int('x')
7118 >>> s = Solver()
7119 >>> s.add(x > 0)
7120 >>> s
7121 [x > 0]
7122 >>> s.push()
7123 >>> s.add(x < 1)
7124 >>> s
7125 [x > 0, x < 1]
7126 >>> s.check()
7127 unsat
7128 >>> s.pop()
7129 >>> s.check()
7130 sat
7131 >>> s
7132 [x > 0]
7133 """
7134 Z3_solver_push(self.ctx.ref(), self.solver)
7135
void Z3_API Z3_solver_push(Z3_context c, Z3_solver s)
Create a backtracking point.

Referenced by Solver.__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 7533 of file z3py.py.

7533 def reason_unknown(self):
7534 """Return a string describing why the last `check()` returned `unknown`.
7535
7536 >>> x = Int('x')
7537 >>> s = SimpleSolver()
7538 >>> s.add(x == 2**x)
7539 >>> s.check()
7540 unknown
7541 >>> s.reason_unknown()
7542 '(incomplete (theory arithmetic))'
7543 """
7544 return Z3_solver_get_reason_unknown(self.ctx.ref(), self.solver)
7545
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 7176 of file z3py.py.

7176 def reset(self):
7177 """Remove all asserted constraints and backtracking points created using `push()`.
7178
7179 >>> x = Int('x')
7180 >>> s = Solver()
7181 >>> s.add(x > 0)
7182 >>> s
7183 [x > 0]
7184 >>> s.reset()
7185 >>> s
7186 []
7187 """
7188 Z3_solver_reset(self.ctx.ref(), self.solver)
7189
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 7432 of file z3py.py.

7432 def root(self, t):
7433 """Retrieve congruence closure root of the term t relative to the current search state
7434 The function primarily works for SimpleSolver. Terms and variables that are
7435 eliminated during pre-processing are not visible to the congruence closure.
7436 """
7437 t = _py2expr(t, self.ctx)
7438 return _to_expr_ref(Z3_solver_congruence_root(self.ctx.ref(), self.solver, t.ast), self.ctx)
7439
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 7101 of file z3py.py.

7101 def set(self, *args, **keys):
7102 """Set a configuration option.
7103 The method `help()` return a string containing all available options.
7104
7105 >>> s = Solver()
7106 >>> # The option MBQI can be set using three different approaches.
7107 >>> s.set(mbqi=True)
7108 >>> s.set('MBQI', True)
7109 >>> s.set(':mbqi', True)
7110 """
7111 p = args2params(args, keys, self.ctx)
7112 Z3_solver_set_params(self.ctx.ref(), self.solver, p.params)
7113
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 7503 of file z3py.py.

7503 def set_initial_value(self, var, value):
7504 """initialize the solver's state by setting the initial value of var to value
7505 """
7506 s = var.sort()
7507 value = s.cast(value)
7508 Z3_solver_set_initial_value(self.ctx.ref(), self.solver, var.ast, value.ast)
7509
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 7577 of file z3py.py.

7577 def sexpr(self):
7578 """Return a formatted string (in Lisp-like format) with all added constraints.
7579 We say the string is in s-expression format.
7580
7581 >>> x = Int('x')
7582 >>> s = Solver()
7583 >>> s.add(x > 0)
7584 >>> s.add(x < 2)
7585 >>> r = s.sexpr()
7586 """
7587 return Z3_solver_to_string(self.ctx.ref(), self.solver)
7588
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 7455 of file z3py.py.

7455 def solve_for(self, ts):
7456 """Retrieve a solution for t relative to linear equations maintained in the current state."""
7457 vars = AstVector(ctx=self.ctx);
7458 terms = AstVector(ctx=self.ctx);
7459 guards = AstVector(ctx=self.ctx);
7460 for t in ts:
7461 t = _py2expr(t, self.ctx)
7462 vars.push(t)
7463 Z3_solver_solve_for(self.ctx.ref(), self.solver, vars.vector, terms.vector, guards.vector)
7464 return [(vars[i], terms[i], guards[i]) for i in range(len(vars))]
7465
7466
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 7515 of file z3py.py.

7515 def statistics(self):
7516 """Return statistics for the last `check()`.
7517
7518 >>> s = SimpleSolver()
7519 >>> x = Int('x')
7520 >>> s.add(x > 0)
7521 >>> s.check()
7522 sat
7523 >>> st = s.statistics()
7524 >>> st.get_key_value('final checks')
7525 1
7526 >>> len(st) > 0
7527 True
7528 >>> st[0] != 0
7529 True
7530 """
7531 return Statistics(Z3_solver_get_statistics(self.ctx.ref(), self.solver), self.ctx)
7532
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 7593 of file z3py.py.

7593 def to_smt2(self):
7594 """return SMTLIB2 formatted benchmark for solver's assertions"""
7595 es = self.assertions()
7596 sz = len(es)
7597 sz1 = sz
7598 if sz1 > 0:
7599 sz1 -= 1
7600 v = (Ast * sz1)()
7601 for i in range(sz1):
7602 v[i] = es[i].as_ast()
7603 if sz > 0:
7604 e = es[sz1].as_ast()
7605 else:
7606 e = BoolVal(True, self.ctx).as_ast()
7608 self.ctx.ref(), "benchmark generated from python API", "", "unknown", "", sz1, v, e,
7609 )
7610
7611
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 7510 of file z3py.py.

7510 def trail(self):
7511 """Return trail of the solver state after a check() call.
7512 """
7513 return AstVector(Z3_solver_get_trail(self.ctx.ref(), self.solver), self.ctx)
7514
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 7495 of file z3py.py.

7495 def trail_levels(self):
7496 """Return trail and decision levels of the solver state after a check() call.
7497 """
7498 trail = self.trail()
7499 levels = (ctypes.c_uint * len(trail))()
7500 Z3_solver_get_levels(self.ctx.ref(), self.solver, trail.vector, len(trail), levels)
7501 return trail, levels
7502
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 7558 of file z3py.py.

7558 def translate(self, target):
7559 """Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.
7560
7561 >>> c1 = Context()
7562 >>> c2 = Context()
7563 >>> s1 = Solver(ctx=c1)
7564 >>> s2 = s1.translate(c2)
7565 """
7566 if z3_debug():
7567 _z3_assert(isinstance(target, Context), "argument must be a Z3 context")
7568 solver = Z3_solver_translate(self.ctx.ref(), self.solver, target.ref())
7569 return Solver(solver, target)
7570
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.

Referenced by AstRef.__copy__(), Goal.__copy__(), AstVector.__copy__(), FuncInterp.__copy__(), ModelRef.__copy__(), Goal.__deepcopy__(), AstVector.__deepcopy__(), FuncInterp.__deepcopy__(), and ModelRef.__deepcopy__().

◆ units()

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

Definition at line 7485 of file z3py.py.

7485 def units(self):
7486 """Return an AST vector containing all currently inferred units.
7487 """
7488 return AstVector(Z3_solver_get_units(self.ctx.ref(), self.solver), self.ctx)
7489
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 7335 of file z3py.py.

7335 def unsat_core(self):
7336 """Return a subset (as an AST vector) of the assumptions provided to the last check().
7337
7338 These are the assumptions Z3 used in the unsatisfiability proof.
7339 Assumptions are available in Z3. They are used to extract unsatisfiable cores.
7340 They may be also used to "retract" assumptions. Note that, assumptions are not really
7341 "soft constraints", but they can be used to implement them.
7342
7343 >>> p1, p2, p3 = Bools('p1 p2 p3')
7344 >>> x, y = Ints('x y')
7345 >>> s = Solver()
7346 >>> s.add(Implies(p1, x > 0))
7347 >>> s.add(Implies(p2, y > x))
7348 >>> s.add(Implies(p2, y < 1))
7349 >>> s.add(Implies(p3, y > -3))
7350 >>> s.check(p1, p2, p3)
7351 unsat
7352 >>> core = s.unsat_core()
7353 >>> len(core)
7354 2
7355 >>> p1 in core
7356 True
7357 >>> p2 in core
7358 True
7359 >>> p3 in core
7360 False
7361 >>> # "Retracting" p2
7362 >>> s.check(p1, p3)
7363 sat
7364 """
7365 return AstVector(Z3_solver_get_unsat_core(self.ctx.ref(), self.solver), self.ctx)
7366
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

backtrack_level

Definition at line 7080 of file z3py.py.

◆ ctx

ctx

Definition at line 7079 of file z3py.py.

Referenced by ArithRef.__add__(), BitVecRef.__add__(), BitVecRef.__and__(), FuncDeclRef.__call__(), AstMap.__contains__(), AstRef.__copy__(), Goal.__copy__(), AstVector.__copy__(), FuncInterp.__copy__(), ModelRef.__copy__(), AstRef.__deepcopy__(), Datatype.__deepcopy__(), ParamsRef.__deepcopy__(), ParamDescrsRef.__deepcopy__(), Goal.__deepcopy__(), AstVector.__deepcopy__(), AstMap.__deepcopy__(), FuncEntry.__deepcopy__(), FuncInterp.__deepcopy__(), ModelRef.__deepcopy__(), Statistics.__deepcopy__(), Context.__del__(), AstRef.__del__(), ScopedConstructor.__del__(), ScopedConstructorList.__del__(), ParamsRef.__del__(), ParamDescrsRef.__del__(), Goal.__del__(), AstVector.__del__(), AstMap.__del__(), FuncEntry.__del__(), FuncInterp.__del__(), ModelRef.__del__(), Statistics.__del__(), Solver.__del__(), ArithRef.__div__(), BitVecRef.__div__(), ExprRef.__eq__(), ArithRef.__ge__(), BitVecRef.__ge__(), AstVector.__getitem__(), ModelRef.__getitem__(), Statistics.__getitem__(), AstMap.__getitem__(), ArithRef.__gt__(), BitVecRef.__gt__(), BitVecRef.__invert__(), ArithRef.__le__(), BitVecRef.__le__(), AstVector.__len__(), AstMap.__len__(), ModelRef.__len__(), Statistics.__len__(), BitVecRef.__lshift__(), ArithRef.__lt__(), BitVecRef.__lt__(), ArithRef.__mod__(), BitVecRef.__mod__(), BoolRef.__mul__(), ArithRef.__mul__(), BitVecRef.__mul__(), ExprRef.__ne__(), ArithRef.__neg__(), BitVecRef.__neg__(), BitVecRef.__or__(), ArithRef.__pow__(), ArithRef.__radd__(), BitVecRef.__radd__(), BitVecRef.__rand__(), ArithRef.__rdiv__(), BitVecRef.__rdiv__(), ParamsRef.__repr__(), ParamDescrsRef.__repr__(), AstMap.__repr__(), Statistics.__repr__(), BitVecRef.__rlshift__(), ArithRef.__rmod__(), BitVecRef.__rmod__(), ArithRef.__rmul__(), BitVecRef.__rmul__(), BitVecRef.__ror__(), ArithRef.__rpow__(), BitVecRef.__rrshift__(), BitVecRef.__rshift__(), ArithRef.__rsub__(), BitVecRef.__rsub__(), BitVecRef.__rxor__(), AstVector.__setitem__(), AstMap.__setitem__(), ArithRef.__sub__(), BitVecRef.__sub__(), BitVecRef.__xor__(), DatatypeSortRef.accessor(), ExprRef.arg(), FuncEntry.arg_value(), FuncInterp.arity(), Goal.as_expr(), Solver.assert_and_track(), Goal.assert_exprs(), Solver.assert_exprs(), QuantifierRef.body(), Solver.check(), Goal.convert_model(), AstRef.ctx_ref(), ExprRef.decl(), ModelRef.decls(), ArrayRef.default(), RatNumRef.denominator(), Goal.depth(), Goal.dimacs(), FuncDeclRef.domain(), ArraySortRef.domain_n(), FuncInterp.else_value(), FuncInterp.entry(), AstMap.erase(), ModelRef.eval(), Goal.get(), ParamDescrsRef.get_documentation(), ModelRef.get_interp(), Statistics.get_key_value(), ParamDescrsRef.get_kind(), ParamDescrsRef.get_name(), ModelRef.get_sort(), ModelRef.get_universe(), Goal.inconsistent(), AstMap.keys(), Statistics.keys(), Solver.model(), SortRef.name(), QuantifierRef.no_pattern(), FuncEntry.num_args(), FuncInterp.num_entries(), Solver.num_scopes(), ModelRef.num_sorts(), FuncDeclRef.params(), QuantifierRef.pattern(), AlgebraicNumRef.poly(), Solver.pop(), Goal.prec(), ModelRef.project(), ModelRef.project_with_witness(), Solver.push(), AstVector.push(), QuantifierRef.qid(), FuncDeclRef.range(), ArraySortRef.range(), DatatypeSortRef.recognizer(), Context.ref(), AstMap.reset(), Solver.reset(), AstVector.resize(), Solver.set(), ParamsRef.set(), Goal.sexpr(), AstVector.sexpr(), ModelRef.sexpr(), ParamDescrsRef.size(), Goal.size(), QuantifierRef.skolem_id(), AstVector.translate(), AstRef.translate(), Goal.translate(), ModelRef.translate(), ParamsRef.validate(), FuncEntry.value(), QuantifierRef.var_name(), and QuantifierRef.var_sort().

◆ cube_vs

cube_vs

Definition at line 7411 of file z3py.py.

◆ solver

solver