Z3
Public Member Functions | Data Fields
Solver Class Reference
+ Inheritance diagram for Solver:

Public Member Functions

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

Data Fields

 ctx
 
 backtrack_level
 
 solver
 
 cube_vs
 

Detailed Description

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

Definition at line 7159 of file z3py.py.

Constructor & Destructor Documentation

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

Definition at line 7165 of file z3py.py.

7165  def __init__(self, solver=None, ctx=None, logFile=None):
7166  assert solver is None or ctx is not None
7167  self.ctx = _get_ctx(ctx)
7168  self.backtrack_level = 4000000000
7169  self.solver = None
7170  if solver is None:
7171  self.solver = Z3_mk_solver(self.ctx.ref())
7172  else:
7173  self.solver = solver
7174  Z3_solver_inc_ref(self.ctx.ref(), self.solver)
7175  if logFile is not None:
7176  self.set("smtlib2_log", logFile)
7177 
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.
def __init__
Definition: z3py.py:7165
backtrack_level
Definition: z3py.py:7168
def set(self, args, keys)
Definition: z3py.py:7189
def __del__ (   self)

Definition at line 7178 of file z3py.py.

7178  def __del__(self):
7179  if self.solver is not None and self.ctx.ref() is not None and Z3_solver_dec_ref is not None:
7180  Z3_solver_dec_ref(self.ctx.ref(), self.solver)
7181 
def __del__(self)
Definition: z3py.py:7178
void Z3_API Z3_solver_dec_ref(Z3_context c, Z3_solver s)
Decrement the reference counter of the given solver.

Member Function Documentation

def __copy__ (   self)

Definition at line 7659 of file z3py.py.

7659  def __copy__(self):
7660  return self.translate(self.ctx)
7661 
def translate(self, target)
Definition: z3py.py:7646
def __copy__(self)
Definition: z3py.py:7659
def __deepcopy__ (   self,
  memo = {} 
)

Definition at line 7662 of file z3py.py.

7662  def __deepcopy__(self, memo={}):
7663  return self.translate(self.ctx)
7664 
def translate(self, target)
Definition: z3py.py:7646
def __deepcopy__
Definition: z3py.py:7662
def __enter__ (   self)

Definition at line 7182 of file z3py.py.

7182  def __enter__(self):
7183  self.push()
7184  return self
7185 
def push(self)
Definition: z3py.py:7202
def __enter__(self)
Definition: z3py.py:7182
def __exit__ (   self,
  exc_info 
)

Definition at line 7186 of file z3py.py.

7186  def __exit__(self, *exc_info):
7187  self.pop()
7188 
def __exit__(self, exc_info)
Definition: z3py.py:7186
def pop
Definition: z3py.py:7224
def __iadd__ (   self,
  fml 
)

Definition at line 7308 of file z3py.py.

7308  def __iadd__(self, fml):
7309  self.add(fml)
7310  return self
7311 
def __iadd__(self, fml)
Definition: z3py.py:7308
def add(self, args)
Definition: z3py.py:7297
def __repr__ (   self)
Return a formatted string with all added constraints.

Definition at line 7642 of file z3py.py.

7642  def __repr__(self):
7643  """Return a formatted string with all added constraints."""
7644  return obj_to_string(self)
7645 
def __repr__(self)
Definition: z3py.py:7642
def 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 7297 of file z3py.py.

Referenced by Solver.__iadd__(), Fixedpoint.__iadd__(), and Optimize.__iadd__().

7297  def add(self, *args):
7298  """Assert constraints into the solver.
7299 
7300  >>> x = Int('x')
7301  >>> s = Solver()
7302  >>> s.add(x > 0, x < 2)
7303  >>> s
7304  [x > 0, x < 2]
7305  """
7306  self.assert_exprs(*args)
7307 
def assert_exprs(self, args)
Definition: z3py.py:7278
def add(self, args)
Definition: z3py.py:7297
def 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 7312 of file z3py.py.

7312  def append(self, *args):
7313  """Assert constraints into the solver.
7314 
7315  >>> x = Int('x')
7316  >>> s = Solver()
7317  >>> s.append(x > 0, x < 2)
7318  >>> s
7319  [x > 0, x < 2]
7320  """
7321  self.assert_exprs(*args)
7322 
def assert_exprs(self, args)
Definition: z3py.py:7278
def append(self, args)
Definition: z3py.py:7312
def 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 7334 of file z3py.py.

7334  def assert_and_track(self, a, p):
7335  """Assert constraint `a` and track it in the unsat core using the Boolean constant `p`.
7336 
7337  If `p` is a string, it will be automatically converted into a Boolean constant.
7338 
7339  >>> x = Int('x')
7340  >>> p3 = Bool('p3')
7341  >>> s = Solver()
7342  >>> s.set(unsat_core=True)
7343  >>> s.assert_and_track(x > 0, 'p1')
7344  >>> s.assert_and_track(x != 1, 'p2')
7345  >>> s.assert_and_track(x < 0, p3)
7346  >>> print(s.check())
7347  unsat
7348  >>> c = s.unsat_core()
7349  >>> len(c)
7350  2
7351  >>> Bool('p1') in c
7352  True
7353  >>> Bool('p2') in c
7354  False
7355  >>> p3 in c
7356  True
7357  """
7358  if isinstance(p, str):
7359  p = Bool(p, self.ctx)
7360  _z3_assert(isinstance(a, BoolRef), "Boolean expression expected")
7361  _z3_assert(isinstance(p, BoolRef) and is_const(p), "Boolean expression expected")
7362  Z3_solver_assert_and_track(self.ctx.ref(), self.solver, a.as_ast(), p.as_ast())
7363 
def is_const(a)
Definition: z3py.py:1394
def assert_and_track(self, a, p)
Definition: z3py.py:7334
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...
def Bool
Definition: z3py.py:1861
def 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 7278 of file z3py.py.

Referenced by Solver.add(), Fixedpoint.add(), Optimize.add(), Solver.append(), Fixedpoint.append(), and Fixedpoint.insert().

7278  def assert_exprs(self, *args):
7279  """Assert constraints into the solver.
7280 
7281  >>> x = Int('x')
7282  >>> s = Solver()
7283  >>> s.assert_exprs(x > 0, x < 2)
7284  >>> s
7285  [x > 0, x < 2]
7286  """
7287  args = _get_args(args)
7288  s = BoolSort(self.ctx)
7289  for arg in args:
7290  if isinstance(arg, Goal) or isinstance(arg, AstVector):
7291  for f in arg:
7292  Z3_solver_assert(self.ctx.ref(), self.solver, f.as_ast())
7293  else:
7294  arg = s.cast(arg)
7295  Z3_solver_assert(self.ctx.ref(), self.solver, arg.as_ast())
7296 
def BoolSort
Definition: z3py.py:1824
def assert_exprs(self, args)
Definition: z3py.py:7278
void Z3_API Z3_solver_assert(Z3_context c, Z3_solver s, Z3_ast a)
Assert a constraint into the solver.
def 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 7559 of file z3py.py.

Referenced by Solver.solutions(), and Solver.to_smt2().

7559  def assertions(self):
7560  """Return an AST vector containing all added constraints.
7561 
7562  >>> s = Solver()
7563  >>> s.assertions()
7564  []
7565  >>> a = Int('a')
7566  >>> s.add(a > 0)
7567  >>> s.add(a < 10)
7568  >>> s.assertions()
7569  [a > 0, a < 10]
7570  """
7571  return AstVector(Z3_solver_get_assertions(self.ctx.ref(), self.solver), self.ctx)
7572 
Z3_ast_vector Z3_API Z3_solver_get_assertions(Z3_context c, Z3_solver s)
Return the set of asserted formulas on the solver.
def assertions(self)
Definition: z3py.py:7559
def 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 7364 of file z3py.py.

Referenced by Solver.model(), Solver.proof(), Solver.reason_unknown(), Solver.statistics(), Solver.trail(), Solver.trail_levels(), and Solver.unsat_core().

7364  def check(self, *assumptions):
7365  """Check whether the assertions in the given solver plus the optional assumptions are consistent or not.
7366 
7367  >>> x = Int('x')
7368  >>> s = Solver()
7369  >>> s.check()
7370  sat
7371  >>> s.add(x > 0, x < 2)
7372  >>> s.check()
7373  sat
7374  >>> s.model().eval(x)
7375  1
7376  >>> s.add(x < 1)
7377  >>> s.check()
7378  unsat
7379  >>> s.reset()
7380  >>> s.add(2**x == 4)
7381  >>> s.check()
7382  sat
7383  """
7384  s = BoolSort(self.ctx)
7385  assumptions = _get_args(assumptions)
7386  num = len(assumptions)
7387  _assumptions = (Ast * num)()
7388  for i in range(num):
7389  _assumptions[i] = s.cast(assumptions[i]).as_ast()
7390  r = Z3_solver_check_assumptions(self.ctx.ref(), self.solver, num, _assumptions)
7391  return CheckSatResult(r)
7392 
def BoolSort
Definition: z3py.py:1824
expr range(expr const &lo, expr const &hi)
Definition: z3++.h:4343
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...
def check(self, assumptions)
Definition: z3py.py:7364
def 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 7455 of file z3py.py.

7455  def consequences(self, assumptions, variables):
7456  """Determine fixed values for the variables based on the solver state and assumptions.
7457  >>> s = Solver()
7458  >>> a, b, c, d = Bools('a b c d')
7459  >>> s.add(Implies(a,b), Implies(b, c))
7460  >>> s.consequences([a],[b,c,d])
7461  (sat, [Implies(a, b), Implies(a, c)])
7462  >>> s.consequences([Not(c),d],[a,b,c,d])
7463  (sat, [Implies(d, d), Implies(Not(c), Not(c)), Implies(Not(c), Not(b)), Implies(Not(c), Not(a))])
7464  """
7465  if isinstance(assumptions, list):
7466  _asms = AstVector(None, self.ctx)
7467  for a in assumptions:
7468  _asms.push(a)
7469  assumptions = _asms
7470  if isinstance(variables, list):
7471  _vars = AstVector(None, self.ctx)
7472  for a in variables:
7473  _vars.push(a)
7474  variables = _vars
7475  _z3_assert(isinstance(assumptions, AstVector), "ast vector expected")
7476  _z3_assert(isinstance(variables, AstVector), "ast vector expected")
7477  consequences = AstVector(None, self.ctx)
7478  r = Z3_solver_get_consequences(self.ctx.ref(), self.solver, assumptions.vector,
7479  variables.vector, consequences.vector)
7480  sz = len(consequences)
7481  consequences = [consequences[i] for i in range(sz)]
7482  return CheckSatResult(r), consequences
7483 
expr range(expr const &lo, expr const &hi)
Definition: z3++.h:4343
def consequences(self, assumptions, variables)
Definition: z3py.py:7455
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.
def 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 7492 of file z3py.py.

7492  def cube(self, vars=None):
7493  """Get set of cubes
7494  The method takes an optional set of variables that restrict which
7495  variables may be used as a starting point for cubing.
7496  If vars is not None, then the first case split is based on a variable in
7497  this set.
7498  """
7499  self.cube_vs = AstVector(None, self.ctx)
7500  if vars is not None:
7501  for v in vars:
7502  self.cube_vs.push(v)
7503  while True:
7504  lvl = self.backtrack_level
7505  self.backtrack_level = 4000000000
7506  r = AstVector(Z3_solver_cube(self.ctx.ref(), self.solver, self.cube_vs.vector, lvl), self.ctx)
7507  if (len(r) == 1 and is_false(r[0])):
7508  return
7509  yield r
7510  if (len(r) == 0):
7511  return
7512 
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...
def is_false
Definition: z3py.py:1740
def cube
Definition: z3py.py:7492
backtrack_level
Definition: z3py.py:7168
def 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 7513 of file z3py.py.

7513  def cube_vars(self):
7514  """Access the set of variables that were touched by the most recently generated cube.
7515  This set of variables can be used as a starting point for additional cubes.
7516  The idea is that variables that appear in clauses that are reduced by the most recent
7517  cube are likely more useful to cube on."""
7518  return self.cube_vs
7519 
def cube_vars(self)
Definition: z3py.py:7513
def dimacs (   self,
  include_names = True 
)
Return a textual representation of the solver in DIMACS format.

Definition at line 7670 of file z3py.py.

7670  def dimacs(self, include_names=True):
7671  """Return a textual representation of the solver in DIMACS format."""
7672  return Z3_solver_to_dimacs_string(self.ctx.ref(), self.solver, include_names)
7673 
def dimacs
Definition: z3py.py:7670
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.
def explain_congruent (   self,
  a,
  b 
)
Explain congruence of a and b relative to the current search state

Definition at line 7536 of file z3py.py.

7536  def explain_congruent(self, a, b):
7537  """Explain congruence of a and b relative to the current search state"""
7538  a = _py2expr(a, self.ctx)
7539  b = _py2expr(b, self.ctx)
7540  return _to_expr_ref(Z3_solver_congruence_explain(self.ctx.ref(), self.solver, a.ast, b.ast), self.ctx)
7541 
7542 
def explain_congruent(self, a, b)
Definition: z3py.py:7536
Z3_ast Z3_API Z3_solver_congruence_explain(Z3_context c, Z3_solver s, Z3_ast a, Z3_ast b)
retrieve explanation for congruence.
def from_file (   self,
  filename 
)
Parse assertions from a file

Definition at line 7484 of file z3py.py.

7484  def from_file(self, filename):
7485  """Parse assertions from a file"""
7486  Z3_solver_from_file(self.ctx.ref(), self.solver, filename)
7487 
void Z3_API Z3_solver_from_file(Z3_context c, Z3_solver s, Z3_string file_name)
load solver assertions from a file.
def from_file(self, filename)
Definition: z3py.py:7484
def from_string (   self,
  s 
)
Parse assertions from a string

Definition at line 7488 of file z3py.py.

7488  def from_string(self, s):
7489  """Parse assertions from a string"""
7490  Z3_solver_from_string(self.ctx.ref(), self.solver, s)
7491 
void Z3_API Z3_solver_from_string(Z3_context c, Z3_solver s, Z3_string str)
load solver assertions from a string.
def from_string(self, s)
Definition: z3py.py:7488
def help (   self)
Display a string describing all available options.

Definition at line 7634 of file z3py.py.

Referenced by Solver.set().

7634  def help(self):
7635  """Display a string describing all available options."""
7636  print(Z3_solver_get_help(self.ctx.ref(), self.solver))
7637 
def help(self)
Definition: z3py.py:7634
Z3_string Z3_API Z3_solver_get_help(Z3_context c, Z3_solver s)
Return a string describing all solver available parameters.
def import_model_converter (   self,
  other 
)
Import model converter from other into the current solver

Definition at line 7412 of file z3py.py.

7412  def import_model_converter(self, other):
7413  """Import model converter from other into the current solver"""
7414  Z3_solver_import_model_converter(self.ctx.ref(), other.solver, self.solver)
7415 
void Z3_API Z3_solver_import_model_converter(Z3_context ctx, Z3_solver src, Z3_solver dst)
Ad-hoc method for importing model conversion from solver.
def import_model_converter(self, other)
Definition: z3py.py:7412
def 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 7323 of file z3py.py.

7323  def insert(self, *args):
7324  """Assert constraints into the solver.
7325 
7326  >>> x = Int('x')
7327  >>> s = Solver()
7328  >>> s.insert(x > 0, x < 2)
7329  >>> s
7330  [x > 0, x < 2]
7331  """
7332  self.assert_exprs(*args)
7333 
def assert_exprs(self, args)
Definition: z3py.py:7278
def insert(self, args)
Definition: z3py.py:7323
def 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 7416 of file z3py.py.

7416  def interrupt(self):
7417  """Interrupt the execution of the solver object.
7418  Remarks: This ensures that the interrupt applies only
7419  to the given solver object and it applies only if it is running.
7420  """
7421  Z3_solver_interrupt(self.ctx.ref(), self.solver)
7422 
def interrupt(self)
Definition: z3py.py:7416
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...
def 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 7393 of file z3py.py.

Referenced by FuncInterp.translate().

7393  def model(self):
7394  """Return a model for the last `check()`.
7395 
7396  This function raises an exception if
7397  a model is not available (e.g., last `check()` returned unsat).
7398 
7399  >>> s = Solver()
7400  >>> a = Int('a')
7401  >>> s.add(a + 2 == 0)
7402  >>> s.check()
7403  sat
7404  >>> s.model()
7405  [a = -2]
7406  """
7407  try:
7408  return ModelRef(Z3_solver_get_model(self.ctx.ref(), self.solver), self.ctx)
7409  except Z3Exception:
7410  raise Z3Exception("model is not available")
7411 
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.
def model(self)
Definition: z3py.py:7393
def 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 7528 of file z3py.py.

7528  def next(self, t):
7529  """Retrieve congruence closure sibling of the term t relative to the current search state
7530  The function primarily works for SimpleSolver. Terms and variables that are
7531  eliminated during pre-processing are not visible to the congruence closure.
7532  """
7533  t = _py2expr(t, self.ctx)
7534  return _to_expr_ref(Z3_solver_congruence_next(self.ctx.ref(), self.solver, t.ast), self.ctx)
7535 
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...
def next(self, t)
Definition: z3py.py:7528
def non_units (   self)
Return an AST vector containing all atomic formulas in solver state that are not units.

Definition at line 7578 of file z3py.py.

7578  def non_units(self):
7579  """Return an AST vector containing all atomic formulas in solver state that are not units.
7580  """
7581  return AstVector(Z3_solver_get_non_units(self.ctx.ref(), self.solver), self.ctx)
7582 
def non_units(self)
Definition: z3py.py:7578
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.
def 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 7246 of file z3py.py.

7246  def num_scopes(self):
7247  """Return the current number of backtracking points.
7248 
7249  >>> s = Solver()
7250  >>> s.num_scopes()
7251  0
7252  >>> s.push()
7253  >>> s.num_scopes()
7254  1
7255  >>> s.push()
7256  >>> s.num_scopes()
7257  2
7258  >>> s.pop()
7259  >>> s.num_scopes()
7260  1
7261  """
7262  return Z3_solver_get_num_scopes(self.ctx.ref(), self.solver)
7263 
unsigned Z3_API Z3_solver_get_num_scopes(Z3_context c, Z3_solver s)
Return the number of backtracking points.
def num_scopes(self)
Definition: z3py.py:7246
def param_descrs (   self)
Return the parameter description set.

Definition at line 7638 of file z3py.py.

7638  def param_descrs(self):
7639  """Return the parameter description set."""
7640  return ParamDescrsRef(Z3_solver_get_param_descrs(self.ctx.ref(), self.solver), self.ctx)
7641 
def param_descrs(self)
Definition: z3py.py:7638
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.
def 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 7224 of file z3py.py.

Referenced by Solver.__exit__(), and Optimize.__exit__().

7224  def pop(self, num=1):
7225  """Backtrack \\c num backtracking points.
7226 
7227  >>> x = Int('x')
7228  >>> s = Solver()
7229  >>> s.add(x > 0)
7230  >>> s
7231  [x > 0]
7232  >>> s.push()
7233  >>> s.add(x < 1)
7234  >>> s
7235  [x > 0, x < 1]
7236  >>> s.check()
7237  unsat
7238  >>> s.pop()
7239  >>> s.check()
7240  sat
7241  >>> s
7242  [x > 0]
7243  """
7244  Z3_solver_pop(self.ctx.ref(), self.solver, num)
7245 
void Z3_API Z3_solver_pop(Z3_context c, Z3_solver s, unsigned n)
Backtrack n backtracking points.
def pop
Definition: z3py.py:7224
def proof (   self)
Return a proof for the last `check()`. Proof construction must be enabled.

Definition at line 7555 of file z3py.py.

7555  def proof(self):
7556  """Return a proof for the last `check()`. Proof construction must be enabled."""
7557  return _to_expr_ref(Z3_solver_get_proof(self.ctx.ref(), self.solver), self.ctx)
7558 
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.
def proof(self)
Definition: z3py.py:7555
def 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 7202 of file z3py.py.

Referenced by Solver.__enter__(), Optimize.__enter__(), and Solver.reset().

7202  def push(self):
7203  """Create a backtracking point.
7204 
7205  >>> x = Int('x')
7206  >>> s = Solver()
7207  >>> s.add(x > 0)
7208  >>> s
7209  [x > 0]
7210  >>> s.push()
7211  >>> s.add(x < 1)
7212  >>> s
7213  [x > 0, x < 1]
7214  >>> s.check()
7215  unsat
7216  >>> s.pop()
7217  >>> s.check()
7218  sat
7219  >>> s
7220  [x > 0]
7221  """
7222  Z3_solver_push(self.ctx.ref(), self.solver)
7223 
void Z3_API Z3_solver_push(Z3_context c, Z3_solver s)
Create a backtracking point.
def push(self)
Definition: z3py.py:7202
def 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 7621 of file z3py.py.

7621  def reason_unknown(self):
7622  """Return a string describing why the last `check()` returned `unknown`.
7623 
7624  >>> x = Int('x')
7625  >>> s = SimpleSolver()
7626  >>> s.add(x == 2**x)
7627  >>> s.check()
7628  unknown
7629  >>> s.reason_unknown()
7630  '(incomplete (theory arithmetic))'
7631  """
7632  return Z3_solver_get_reason_unknown(self.ctx.ref(), self.solver)
7633 
def reason_unknown(self)
Definition: z3py.py:7621
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...
def 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 7264 of file z3py.py.

7264  def reset(self):
7265  """Remove all asserted constraints and backtracking points created using `push()`.
7266 
7267  >>> x = Int('x')
7268  >>> s = Solver()
7269  >>> s.add(x > 0)
7270  >>> s
7271  [x > 0]
7272  >>> s.reset()
7273  >>> s
7274  []
7275  """
7276  Z3_solver_reset(self.ctx.ref(), self.solver)
7277 
void Z3_API Z3_solver_reset(Z3_context c, Z3_solver s)
Remove all assertions from the solver.
def reset(self)
Definition: z3py.py:7264
def 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 7520 of file z3py.py.

7520  def root(self, t):
7521  """Retrieve congruence closure root of the term t relative to the current search state
7522  The function primarily works for SimpleSolver. Terms and variables that are
7523  eliminated during pre-processing are not visible to the congruence closure.
7524  """
7525  t = _py2expr(t, self.ctx)
7526  return _to_expr_ref(Z3_solver_congruence_root(self.ctx.ref(), self.solver, t.ast), self.ctx)
7527 
def root(self, t)
Definition: z3py.py:7520
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...
def 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 7189 of file z3py.py.

7189  def set(self, *args, **keys):
7190  """Set a configuration option.
7191  The method `help()` return a string containing all available options.
7192 
7193  >>> s = Solver()
7194  >>> # The option MBQI can be set using three different approaches.
7195  >>> s.set(mbqi=True)
7196  >>> s.set('MBQI', True)
7197  >>> s.set(':mbqi', True)
7198  """
7199  p = args2params(args, keys, self.ctx)
7200  Z3_solver_set_params(self.ctx.ref(), self.solver, p.params)
7201 
void Z3_API Z3_solver_set_params(Z3_context c, Z3_solver s, Z3_params p)
Set the given solver using the given parameters.
def args2params
Definition: z3py.py:5695
def set(self, args, keys)
Definition: z3py.py:7189
def set_initial_value (   self,
  var,
  value 
)
initialize the solver's state by setting the initial value of var to value

Definition at line 7591 of file z3py.py.

7591  def set_initial_value(self, var, value):
7592  """initialize the solver's state by setting the initial value of var to value
7593  """
7594  s = var.sort()
7595  value = s.cast(value)
7596  Z3_solver_set_initial_value(self.ctx.ref(), self.solver, var.ast, value.ast)
7597 
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...
def set_initial_value(self, var, value)
Definition: z3py.py:7591
def sexpr (   self)
Return a formatted string (in Lisp-like format) with all added constraints.

Definition at line 7665 of file z3py.py.

Referenced by Fixedpoint.__repr__(), and Optimize.__repr__().

7665  def sexpr(self):
7666  """Return a formatted string (in Lisp-like format) with all added constraints.
7667  """
7668  return Z3_solver_to_string(self.ctx.ref(), self.solver)
7669 
def sexpr(self)
Definition: z3py.py:7665
Z3_string Z3_API Z3_solver_to_string(Z3_context c, Z3_solver s)
Convert a solver into a string.
def solutions (   self,
  t 
)
Returns an iterator over solutions that satisfy the constraints.

The parameter `t` is an expression whose values should be returned.

>>> s = Solver()
>>> x, y, z = Ints("x y z")
>>> s.add(x * x == 4)
>>> print(list(s.solutions(x)))
[-2, 2]
>>> s.reset()
>>> s.add(x >= 0, x < 10)
>>> print(list(s.solutions(x)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> s.reset()
>>> s.add(x >= 0, y < 10, y == 2*x)
>>> print(list(s.solutions([x, y])))
[[0, 0], [1, 2], [2, 4], [3, 6], [4, 8]]

Definition at line 7692 of file z3py.py.

7692  def solutions(self, t):
7693  """Returns an iterator over solutions that satisfy the constraints.
7694 
7695  The parameter `t` is an expression whose values should be returned.
7696 
7697  >>> s = Solver()
7698  >>> x, y, z = Ints("x y z")
7699  >>> s.add(x * x == 4)
7700  >>> print(list(s.solutions(x)))
7701  [-2, 2]
7702  >>> s.reset()
7703  >>> s.add(x >= 0, x < 10)
7704  >>> print(list(s.solutions(x)))
7705  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
7706  >>> s.reset()
7707  >>> s.add(x >= 0, y < 10, y == 2*x)
7708  >>> print(list(s.solutions([x, y])))
7709  [[0, 0], [1, 2], [2, 4], [3, 6], [4, 8]]
7710  """
7711  s = Solver()
7712  s.add(self.assertions())
7713  t = _get_args(t)
7714  if isinstance(t, (list, tuple)):
7715  while s.check() == sat:
7716  result = [s.model().eval(t_, model_completion=True) for t_ in t]
7717  yield result
7718  s.add(*(t_ != result_ for t_, result_ in zip(t, result)))
7719  else:
7720  while s.check() == sat:
7721  result = s.model().eval(t, model_completion=True)
7722  yield result
7723  s.add(t != result)
7724 
7725 
def solutions(self, t)
Definition: z3py.py:7692
def assertions(self)
Definition: z3py.py:7559
def solve_for (   self,
  ts 
)
Retrieve a solution for t relative to linear equations maintained in the current state.

Definition at line 7543 of file z3py.py.

7543  def solve_for(self, ts):
7544  """Retrieve a solution for t relative to linear equations maintained in the current state."""
7545  vars = AstVector(ctx=self.ctx);
7546  terms = AstVector(ctx=self.ctx);
7547  guards = AstVector(ctx=self.ctx);
7548  for t in ts:
7549  t = _py2expr(t, self.ctx)
7550  vars.push(t)
7551  Z3_solver_solve_for(self.ctx.ref(), self.solver, vars.vector, terms.vector, guards.vector)
7552  return [(vars[i], terms[i], guards[i]) for i in range(len(vars))]
7553 
7554 
expr range(expr const &lo, expr const &hi)
Definition: z3++.h:4343
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. At this point, only linear solution are supported. The solution to variables may be presented in triangular form, such that variables used in solutions themselves have solutions.
def solve_for(self, ts)
Definition: z3py.py:7543
def 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 7603 of file z3py.py.

7603  def statistics(self):
7604  """Return statistics for the last `check()`.
7605 
7606  >>> s = SimpleSolver()
7607  >>> x = Int('x')
7608  >>> s.add(x > 0)
7609  >>> s.check()
7610  sat
7611  >>> st = s.statistics()
7612  >>> st.get_key_value('final checks')
7613  1
7614  >>> len(st) > 0
7615  True
7616  >>> st[0] != 0
7617  True
7618  """
7619  return Statistics(Z3_solver_get_statistics(self.ctx.ref(), self.solver), self.ctx)
7620 
Z3_stats Z3_API Z3_solver_get_statistics(Z3_context c, Z3_solver s)
Return statistics for the given solver.
Statistics.
Definition: z3py.py:6974
def statistics(self)
Definition: z3py.py:7603
def to_smt2 (   self)
return SMTLIB2 formatted benchmark for solver's assertions

Definition at line 7674 of file z3py.py.

7674  def to_smt2(self):
7675  """return SMTLIB2 formatted benchmark for solver's assertions"""
7676  es = self.assertions()
7677  sz = len(es)
7678  sz1 = sz
7679  if sz1 > 0:
7680  sz1 -= 1
7681  v = (Ast * sz1)()
7682  for i in range(sz1):
7683  v[i] = es[i].as_ast()
7684  if sz > 0:
7685  e = es[sz1].as_ast()
7686  else:
7687  e = BoolVal(True, self.ctx).as_ast()
7689  self.ctx.ref(), "benchmark generated from python API", "", "unknown", "", sz1, v, e,
7690  )
7691 
def BoolVal
Definition: z3py.py:1842
expr range(expr const &lo, expr const &hi)
Definition: z3++.h:4343
def to_smt2(self)
Definition: z3py.py:7674
def assertions(self)
Definition: z3py.py:7559
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.
def trail (   self)
Return trail of the solver state after a check() call.

Definition at line 7598 of file z3py.py.

7598  def trail(self):
7599  """Return trail of the solver state after a check() call.
7600  """
7601  return AstVector(Z3_solver_get_trail(self.ctx.ref(), self.solver), self.ctx)
7602 
def trail(self)
Definition: z3py.py:7598
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...
def trail_levels (   self)
Return trail and decision levels of the solver state after a check() call.

Definition at line 7583 of file z3py.py.

7583  def trail_levels(self):
7584  """Return trail and decision levels of the solver state after a check() call.
7585  """
7586  trail = self.trail()
7587  levels = (ctypes.c_uint * len(trail))()
7588  Z3_solver_get_levels(self.ctx.ref(), self.solver, trail.vector, len(trail), levels)
7589  return trail, levels
7590 
def trail_levels(self)
Definition: z3py.py:7583
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...
def trail(self)
Definition: z3py.py:7598
def 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 7646 of file z3py.py.

Referenced by Solver.__copy__(), and Solver.__deepcopy__().

7646  def translate(self, target):
7647  """Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.
7648 
7649  >>> c1 = Context()
7650  >>> c2 = Context()
7651  >>> s1 = Solver(ctx=c1)
7652  >>> s2 = s1.translate(c2)
7653  """
7654  if z3_debug():
7655  _z3_assert(isinstance(target, Context), "argument must be a Z3 context")
7656  solver = Z3_solver_translate(self.ctx.ref(), self.solver, target.ref())
7657  return Solver(solver, target)
7658 
def translate(self, target)
Definition: z3py.py:7646
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.
def z3_debug()
Definition: z3py.py:70
def units (   self)
Return an AST vector containing all currently inferred units.

Definition at line 7573 of file z3py.py.

7573  def units(self):
7574  """Return an AST vector containing all currently inferred units.
7575  """
7576  return AstVector(Z3_solver_get_units(self.ctx.ref(), self.solver), self.ctx)
7577 
def units(self)
Definition: z3py.py:7573
Z3_ast_vector Z3_API Z3_solver_get_units(Z3_context c, Z3_solver s)
Return the set of units modulo model conversion.
def 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 7423 of file z3py.py.

7423  def unsat_core(self):
7424  """Return a subset (as an AST vector) of the assumptions provided to the last check().
7425 
7426  These are the assumptions Z3 used in the unsatisfiability proof.
7427  Assumptions are available in Z3. They are used to extract unsatisfiable cores.
7428  They may be also used to "retract" assumptions. Note that, assumptions are not really
7429  "soft constraints", but they can be used to implement them.
7430 
7431  >>> p1, p2, p3 = Bools('p1 p2 p3')
7432  >>> x, y = Ints('x y')
7433  >>> s = Solver()
7434  >>> s.add(Implies(p1, x > 0))
7435  >>> s.add(Implies(p2, y > x))
7436  >>> s.add(Implies(p2, y < 1))
7437  >>> s.add(Implies(p3, y > -3))
7438  >>> s.check(p1, p2, p3)
7439  unsat
7440  >>> core = s.unsat_core()
7441  >>> len(core)
7442  2
7443  >>> p1 in core
7444  True
7445  >>> p2 in core
7446  True
7447  >>> p3 in core
7448  False
7449  >>> # "Retracting" p2
7450  >>> s.check(p1, p3)
7451  sat
7452  """
7453  return AstVector(Z3_solver_get_unsat_core(self.ctx.ref(), self.solver), self.ctx)
7454 
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...
def unsat_core(self)
Definition: z3py.py:7423

Field Documentation

backtrack_level

Definition at line 7168 of file z3py.py.

ctx

Definition at line 7167 of file z3py.py.

Referenced by Solver.__copy__(), Solver.__deepcopy__(), Fixedpoint.__deepcopy__(), Optimize.__deepcopy__(), ApplyResult.__deepcopy__(), Simplifier.__deepcopy__(), Tactic.__deepcopy__(), Probe.__deepcopy__(), Probe.__eq__(), Probe.__ge__(), ApplyResult.__getitem__(), Probe.__gt__(), Probe.__le__(), Probe.__lt__(), Probe.__ne__(), Simplifier.add(), Fixedpoint.add_rule(), Optimize.add_soft(), Tactic.apply(), ApplyResult.as_expr(), Solver.assert_and_track(), Optimize.assert_and_track(), Solver.assert_exprs(), Fixedpoint.assert_exprs(), Optimize.assert_exprs(), Solver.assertions(), Optimize.assertions(), Solver.check(), Solver.consequences(), Solver.explain_congruent(), ParserContext.from_string(), Fixedpoint.get_answer(), Fixedpoint.get_assertions(), Fixedpoint.get_cover_delta(), Fixedpoint.get_ground_sat_answer(), Fixedpoint.get_rule_names_along_trace(), Fixedpoint.get_rules(), Fixedpoint.get_rules_along_trace(), Solver.model(), Optimize.model(), Solver.next(), Solver.non_units(), Optimize.objectives(), Solver.param_descrs(), Fixedpoint.param_descrs(), Optimize.param_descrs(), Simplifier.param_descrs(), Tactic.param_descrs(), Fixedpoint.parse_file(), Fixedpoint.parse_string(), Solver.proof(), Fixedpoint.query(), Solver.root(), Solver.set(), Fixedpoint.set(), Optimize.set(), Optimize.set_on_model(), Solver.solve_for(), Tactic.solver(), Solver.statistics(), Fixedpoint.statistics(), Optimize.statistics(), Solver.to_smt2(), Solver.trail(), Solver.units(), Solver.unsat_core(), Optimize.unsat_core(), Fixedpoint.update_rule(), and Simplifier.using_params().

cube_vs

Definition at line 7499 of file z3py.py.

Referenced by Solver.cube_vars().

solver