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)
 
 congruence_root (self, t)
 
 congruence_next (self, t)
 
 congruence_explain (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)
 
 solutions (self, t)
 
- 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 7544 of file z3py.py.

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 7550 of file z3py.py.

7550 def __init__(self, solver=None, ctx=None, logFile=None):
7551 assert solver is None or ctx is not None
7552 self.ctx = _get_ctx(ctx)
7553 self.backtrack_level = 4000000000
7554 self.solver = None
7555 if solver is None:
7556 self.solver = Z3_mk_solver(self.ctx.ref())
7557 else:
7558 self.solver = solver
7559 Z3_solver_inc_ref(self.ctx.ref(), self.solver)
7560 if logFile is not None:
7561 self.set("smtlib2_log", logFile)
7562
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 7563 of file z3py.py.

7563 def __del__(self):
7564 if self.solver is not None and self.ctx.ref() is not None and Z3_solver_dec_ref is not None:
7565 Z3_solver_dec_ref(self.ctx.ref(), self.solver)
7566
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 8044 of file z3py.py.

8044 def __copy__(self):
8045 return self.translate(self.ctx)
8046

◆ __deepcopy__()

__deepcopy__ (   self,
  memo = {} 
)

Definition at line 8047 of file z3py.py.

8047 def __deepcopy__(self, memo={}):
8048 return self.translate(self.ctx)
8049

◆ __enter__()

__enter__ (   self)

Definition at line 7567 of file z3py.py.

7567 def __enter__(self):
7568 self.push()
7569 return self
7570

◆ __exit__()

__exit__ (   self,
*  exc_info 
)

Definition at line 7571 of file z3py.py.

7571 def __exit__(self, *exc_info):
7572 self.pop()
7573

◆ __iadd__()

__iadd__ (   self,
  fml 
)

Definition at line 7693 of file z3py.py.

7693 def __iadd__(self, fml):
7694 self.add(fml)
7695 return self
7696

◆ __repr__()

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

Definition at line 8027 of file z3py.py.

8027 def __repr__(self):
8028 """Return a formatted string with all added constraints."""
8029 return obj_to_string(self)
8030

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

7682 def add(self, *args):
7683 """Assert constraints into the solver.
7684
7685 >>> x = Int('x')
7686 >>> s = Solver()
7687 >>> s.add(x > 0, x < 2)
7688 >>> s
7689 [x > 0, x < 2]
7690 """
7691 self.assert_exprs(*args)
7692

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

7697 def append(self, *args):
7698 """Assert constraints into the solver.
7699
7700 >>> x = Int('x')
7701 >>> s = Solver()
7702 >>> s.append(x > 0, x < 2)
7703 >>> s
7704 [x > 0, x < 2]
7705 """
7706 self.assert_exprs(*args)
7707

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

7719 def assert_and_track(self, a, p):
7720 """Assert constraint `a` and track it in the unsat core using the Boolean constant `p`.
7721
7722 If `p` is a string, it will be automatically converted into a Boolean constant.
7723
7724 >>> x = Int('x')
7725 >>> p3 = Bool('p3')
7726 >>> s = Solver()
7727 >>> s.set(unsat_core=True)
7728 >>> s.assert_and_track(x > 0, 'p1')
7729 >>> s.assert_and_track(x != 1, 'p2')
7730 >>> s.assert_and_track(x < 0, p3)
7731 >>> print(s.check())
7732 unsat
7733 >>> c = s.unsat_core()
7734 >>> len(c)
7735 2
7736 >>> Bool('p1') in c
7737 True
7738 >>> Bool('p2') in c
7739 False
7740 >>> p3 in c
7741 True
7742 """
7743 if isinstance(p, str):
7744 p = Bool(p, self.ctx)
7745 _z3_assert(isinstance(a, BoolRef), "Boolean expression expected")
7746 _z3_assert(isinstance(p, BoolRef) and is_const(p), "Boolean expression expected")
7747 Z3_solver_assert_and_track(self.ctx.ref(), self.solver, a.as_ast(), p.as_ast())
7748
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 7663 of file z3py.py.

7663 def assert_exprs(self, *args):
7664 """Assert constraints into the solver.
7665
7666 >>> x = Int('x')
7667 >>> s = Solver()
7668 >>> s.assert_exprs(x > 0, x < 2)
7669 >>> s
7670 [x > 0, x < 2]
7671 """
7672 args = _get_args(args)
7673 s = BoolSort(self.ctx)
7674 for arg in args:
7675 if isinstance(arg, Goal) or isinstance(arg, AstVector):
7676 for f in arg:
7677 Z3_solver_assert(self.ctx.ref(), self.solver, f.as_ast())
7678 else:
7679 arg = s.cast(arg)
7680 Z3_solver_assert(self.ctx.ref(), self.solver, arg.as_ast())
7681
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 7944 of file z3py.py.

7944 def assertions(self):
7945 """Return an AST vector containing all added constraints.
7946
7947 >>> s = Solver()
7948 >>> s.assertions()
7949 []
7950 >>> a = Int('a')
7951 >>> s.add(a > 0)
7952 >>> s.add(a < 10)
7953 >>> s.assertions()
7954 [a > 0, a < 10]
7955 """
7956 return AstVector(Z3_solver_get_assertions(self.ctx.ref(), self.solver), self.ctx)
7957
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 7749 of file z3py.py.

7749 def check(self, *assumptions):
7750 """Check whether the assertions in the given solver plus the optional assumptions are consistent or not.
7751
7752 >>> x = Int('x')
7753 >>> s = Solver()
7754 >>> s.check()
7755 sat
7756 >>> s.add(x > 0, x < 2)
7757 >>> s.check()
7758 sat
7759 >>> s.model().eval(x)
7760 1
7761 >>> s.add(x < 1)
7762 >>> s.check()
7763 unsat
7764 >>> s.reset()
7765 >>> s.add(2**x == 4)
7766 >>> s.check()
7767 sat
7768 """
7769 s = BoolSort(self.ctx)
7770 assumptions = _get_args(assumptions)
7771 num = len(assumptions)
7772 _assumptions = (Ast * num)()
7773 for i in range(num):
7774 _assumptions[i] = s.cast(assumptions[i]).as_ast()
7775 r = Z3_solver_check_assumptions(self.ctx.ref(), self.solver, num, _assumptions)
7776 return CheckSatResult(r)
7777
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.

◆ congruence_explain()

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

Definition at line 7921 of file z3py.py.

7921 def congruence_explain(self, a, b):
7922 """Explain congruence of a and b relative to the current search state"""
7923 a = _py2expr(a, self.ctx)
7924 b = _py2expr(b, self.ctx)
7925 return _to_expr_ref(Z3_solver_congruence_explain(self.ctx.ref(), self.solver, a.ast, b.ast), self.ctx)
7926
7927
Z3_ast Z3_API Z3_solver_congruence_explain(Z3_context c, Z3_solver s, Z3_ast a, Z3_ast b)
retrieve explanation for congruence.

◆ congruence_next()

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

7913 def congruence_next(self, t):
7914 """Retrieve congruence closure sibling of the term t relative to the current search state
7915 The function primarily works for SimpleSolver. Terms and variables that are
7916 eliminated during pre-processing are not visible to the congruence closure.
7917 """
7918 t = _py2expr(t, self.ctx)
7919 return _to_expr_ref(Z3_solver_congruence_next(self.ctx.ref(), self.solver, t.ast), self.ctx)
7920
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...

◆ congruence_root()

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

7905 def congruence_root(self, t):
7906 """Retrieve congruence closure root of the term t relative to the current search state
7907 The function primarily works for SimpleSolver. Terms and variables that are
7908 eliminated during pre-processing are not visible to the congruence closure.
7909 """
7910 t = _py2expr(t, self.ctx)
7911 return _to_expr_ref(Z3_solver_congruence_root(self.ctx.ref(), self.solver, t.ast), self.ctx)
7912
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...

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

7840 def consequences(self, assumptions, variables):
7841 """Determine fixed values for the variables based on the solver state and assumptions.
7842 >>> s = Solver()
7843 >>> a, b, c, d = Bools('a b c d')
7844 >>> s.add(Implies(a,b), Implies(b, c))
7845 >>> s.consequences([a],[b,c,d])
7846 (sat, [Implies(a, b), Implies(a, c)])
7847 >>> s.consequences([Not(c),d],[a,b,c,d])
7848 (sat, [Implies(d, d), Implies(Not(c), Not(c)), Implies(Not(c), Not(b)), Implies(Not(c), Not(a))])
7849 """
7850 if isinstance(assumptions, list):
7851 _asms = AstVector(None, self.ctx)
7852 for a in assumptions:
7853 _asms.push(a)
7854 assumptions = _asms
7855 if isinstance(variables, list):
7856 _vars = AstVector(None, self.ctx)
7857 for a in variables:
7858 _vars.push(a)
7859 variables = _vars
7860 _z3_assert(isinstance(assumptions, AstVector), "ast vector expected")
7861 _z3_assert(isinstance(variables, AstVector), "ast vector expected")
7862 consequences = AstVector(None, self.ctx)
7863 r = Z3_solver_get_consequences(self.ctx.ref(), self.solver, assumptions.vector,
7864 variables.vector, consequences.vector)
7865 sz = len(consequences)
7866 consequences = [consequences[i] for i in range(sz)]
7867 return CheckSatResult(r), consequences
7868
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 7877 of file z3py.py.

7877 def cube(self, vars=None):
7878 """Get set of cubes
7879 The method takes an optional set of variables that restrict which
7880 variables may be used as a starting point for cubing.
7881 If vars is not None, then the first case split is based on a variable in
7882 this set.
7883 """
7884 self.cube_vs = AstVector(None, self.ctx)
7885 if vars is not None:
7886 for v in vars:
7887 self.cube_vs.push(v)
7888 while True:
7889 lvl = self.backtrack_level
7890 self.backtrack_level = 4000000000
7891 r = AstVector(Z3_solver_cube(self.ctx.ref(), self.solver, self.cube_vs.vector, lvl), self.ctx)
7892 if (len(r) == 1 and is_false(r[0])):
7893 return
7894 yield r
7895 if (len(r) == 0):
7896 return
7897
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 7898 of file z3py.py.

7898 def cube_vars(self):
7899 """Access the set of variables that were touched by the most recently generated cube.
7900 This set of variables can be used as a starting point for additional cubes.
7901 The idea is that variables that appear in clauses that are reduced by the most recent
7902 cube are likely more useful to cube on."""
7903 return self.cube_vs
7904

◆ dimacs()

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

Definition at line 8055 of file z3py.py.

8055 def dimacs(self, include_names=True):
8056 """Return a textual representation of the solver in DIMACS format."""
8057 return Z3_solver_to_dimacs_string(self.ctx.ref(), self.solver, include_names)
8058
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.

◆ from_file()

from_file (   self,
  filename 
)
Parse assertions from a file

Definition at line 7869 of file z3py.py.

7869 def from_file(self, filename):
7870 """Parse assertions from a file"""
7871 Z3_solver_from_file(self.ctx.ref(), self.solver, filename)
7872
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 7873 of file z3py.py.

7873 def from_string(self, s):
7874 """Parse assertions from a string"""
7875 Z3_solver_from_string(self.ctx.ref(), self.solver, s)
7876
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 8019 of file z3py.py.

8019 def help(self):
8020 """Display a string describing all available options."""
8021 print(Z3_solver_get_help(self.ctx.ref(), self.solver))
8022
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 7797 of file z3py.py.

7797 def import_model_converter(self, other):
7798 """Import model converter from other into the current solver"""
7799 Z3_solver_import_model_converter(self.ctx.ref(), other.solver, self.solver)
7800

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

7708 def insert(self, *args):
7709 """Assert constraints into the solver.
7710
7711 >>> x = Int('x')
7712 >>> s = Solver()
7713 >>> s.insert(x > 0, x < 2)
7714 >>> s
7715 [x > 0, x < 2]
7716 """
7717 self.assert_exprs(*args)
7718

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

7801 def interrupt(self):
7802 """Interrupt the execution of the solver object.
7803 Remarks: This ensures that the interrupt applies only
7804 to the given solver object and it applies only if it is running.
7805 """
7806 Z3_solver_interrupt(self.ctx.ref(), self.solver)
7807
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 7778 of file z3py.py.

7778 def model(self):
7779 """Return a model for the last `check()`.
7780
7781 This function raises an exception if
7782 a model is not available (e.g., last `check()` returned unsat).
7783
7784 >>> s = Solver()
7785 >>> a = Int('a')
7786 >>> s.add(a + 2 == 0)
7787 >>> s.check()
7788 sat
7789 >>> s.model()
7790 [a = -2]
7791 """
7792 try:
7793 return ModelRef(Z3_solver_get_model(self.ctx.ref(), self.solver), self.ctx)
7794 except Z3Exception:
7795 raise Z3Exception("model is not available")
7796
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().

◆ non_units()

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

Definition at line 7963 of file z3py.py.

7963 def non_units(self):
7964 """Return an AST vector containing all atomic formulas in solver state that are not units.
7965 """
7966 return AstVector(Z3_solver_get_non_units(self.ctx.ref(), self.solver), self.ctx)
7967
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 7631 of file z3py.py.

7631 def num_scopes(self):
7632 """Return the current number of backtracking points.
7633
7634 >>> s = Solver()
7635 >>> s.num_scopes()
7636 0
7637 >>> s.push()
7638 >>> s.num_scopes()
7639 1
7640 >>> s.push()
7641 >>> s.num_scopes()
7642 2
7643 >>> s.pop()
7644 >>> s.num_scopes()
7645 1
7646 """
7647 return Z3_solver_get_num_scopes(self.ctx.ref(), self.solver)
7648
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 8023 of file z3py.py.

8023 def param_descrs(self):
8024 """Return the parameter description set."""
8025 return ParamDescrsRef(Z3_solver_get_param_descrs(self.ctx.ref(), self.solver), self.ctx)
8026
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 7609 of file z3py.py.

7609 def pop(self, num=1):
7610 """Backtrack \\c num backtracking points.
7611
7612 >>> x = Int('x')
7613 >>> s = Solver()
7614 >>> s.add(x > 0)
7615 >>> s
7616 [x > 0]
7617 >>> s.push()
7618 >>> s.add(x < 1)
7619 >>> s
7620 [x > 0, x < 1]
7621 >>> s.check()
7622 unsat
7623 >>> s.pop()
7624 >>> s.check()
7625 sat
7626 >>> s
7627 [x > 0]
7628 """
7629 Z3_solver_pop(self.ctx.ref(), self.solver, num)
7630
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 7940 of file z3py.py.

7940 def proof(self):
7941 """Return a proof for the last `check()`. Proof construction must be enabled."""
7942 return _to_expr_ref(Z3_solver_get_proof(self.ctx.ref(), self.solver), self.ctx)
7943
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 7587 of file z3py.py.

7587 def push(self):
7588 """Create a backtracking point.
7589
7590 >>> x = Int('x')
7591 >>> s = Solver()
7592 >>> s.add(x > 0)
7593 >>> s
7594 [x > 0]
7595 >>> s.push()
7596 >>> s.add(x < 1)
7597 >>> s
7598 [x > 0, x < 1]
7599 >>> s.check()
7600 unsat
7601 >>> s.pop()
7602 >>> s.check()
7603 sat
7604 >>> s
7605 [x > 0]
7606 """
7607 Z3_solver_push(self.ctx.ref(), self.solver)
7608
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 8006 of file z3py.py.

8006 def reason_unknown(self):
8007 """Return a string describing why the last `check()` returned `unknown`.
8008
8009 >>> x = Int('x')
8010 >>> s = SimpleSolver()
8011 >>> s.add(x == 2**x)
8012 >>> s.check()
8013 unknown
8014 >>> s.reason_unknown()
8015 '(incomplete (theory arithmetic))'
8016 """
8017 return Z3_solver_get_reason_unknown(self.ctx.ref(), self.solver)
8018
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 7649 of file z3py.py.

7649 def reset(self):
7650 """Remove all asserted constraints and backtracking points created using `push()`.
7651
7652 >>> x = Int('x')
7653 >>> s = Solver()
7654 >>> s.add(x > 0)
7655 >>> s
7656 [x > 0]
7657 >>> s.reset()
7658 >>> s
7659 []
7660 """
7661 Z3_solver_reset(self.ctx.ref(), self.solver)
7662
void Z3_API Z3_solver_reset(Z3_context c, Z3_solver s)
Remove all assertions from the solver.

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

7574 def set(self, *args, **keys):
7575 """Set a configuration option.
7576 The method `help()` return a string containing all available options.
7577
7578 >>> s = Solver()
7579 >>> # The option MBQI can be set using three different approaches.
7580 >>> s.set(mbqi=True)
7581 >>> s.set('MBQI', True)
7582 >>> s.set(':mbqi', True)
7583 """
7584 p = args2params(args, keys, self.ctx)
7585 Z3_solver_set_params(self.ctx.ref(), self.solver, p.params)
7586
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 7976 of file z3py.py.

7976 def set_initial_value(self, var, value):
7977 """initialize the solver's state by setting the initial value of var to value
7978 """
7979 s = var.sort()
7980 value = s.cast(value)
7981 Z3_solver_set_initial_value(self.ctx.ref(), self.solver, var.ast, value.ast)
7982
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.

Definition at line 8050 of file z3py.py.

8050 def sexpr(self):
8051 """Return a formatted string (in Lisp-like format) with all added constraints.
8052 """
8053 return Z3_solver_to_string(self.ctx.ref(), self.solver)
8054
Z3_string Z3_API Z3_solver_to_string(Z3_context c, Z3_solver s)
Convert a solver into a string.

◆ solutions()

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

8077 def solutions(self, t):
8078 """Returns an iterator over solutions that satisfy the constraints.
8079
8080 The parameter `t` is an expression whose values should be returned.
8081
8082 >>> s = Solver()
8083 >>> x, y, z = Ints("x y z")
8084 >>> s.add(x * x == 4)
8085 >>> print(list(s.solutions(x)))
8086 [-2, 2]
8087 >>> s.reset()
8088 >>> s.add(x >= 0, x < 10)
8089 >>> print(list(s.solutions(x)))
8090 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
8091 >>> s.reset()
8092 >>> s.add(x >= 0, y < 10, y == 2*x)
8093 >>> print(list(s.solutions([x, y])))
8094 [[0, 0], [1, 2], [2, 4], [3, 6], [4, 8]]
8095 """
8096 s = Solver()
8097 s.add(self.assertions())
8098 t = _get_args(t)
8099 if isinstance(t, (list, tuple)):
8100 while s.check() == sat:
8101 result = [s.model().eval(t_, model_completion=True) for t_ in t]
8102 yield result
8103 s.add(Or(t_ != result_ for t_, result_ in zip(t, result)))
8104 else:
8105 while s.check() == sat:
8106 result = s.model().eval(t, model_completion=True)
8107 yield result
8108 s.add(t != result)
8109
8110

◆ solve_for()

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

Definition at line 7928 of file z3py.py.

7928 def solve_for(self, ts):
7929 """Retrieve a solution for t relative to linear equations maintained in the current state."""
7930 vars = AstVector(ctx=self.ctx);
7931 terms = AstVector(ctx=self.ctx);
7932 guards = AstVector(ctx=self.ctx);
7933 for t in ts:
7934 t = _py2expr(t, self.ctx)
7935 vars.push(t)
7936 Z3_solver_solve_for(self.ctx.ref(), self.solver, vars.vector, terms.vector, guards.vector)
7937 return [(vars[i], terms[i], guards[i]) for i in range(len(vars))]
7938
7939
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 7988 of file z3py.py.

7988 def statistics(self):
7989 """Return statistics for the last `check()`.
7990
7991 >>> s = SimpleSolver()
7992 >>> x = Int('x')
7993 >>> s.add(x > 0)
7994 >>> s.check()
7995 sat
7996 >>> st = s.statistics()
7997 >>> st.get_key_value('final checks')
7998 1
7999 >>> len(st) > 0
8000 True
8001 >>> st[0] != 0
8002 True
8003 """
8004 return Statistics(Z3_solver_get_statistics(self.ctx.ref(), self.solver), self.ctx)
8005
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 8059 of file z3py.py.

8059 def to_smt2(self):
8060 """return SMTLIB2 formatted benchmark for solver's assertions"""
8061 es = self.assertions()
8062 sz = len(es)
8063 sz1 = sz
8064 if sz1 > 0:
8065 sz1 -= 1
8066 v = (Ast * sz1)()
8067 for i in range(sz1):
8068 v[i] = es[i].as_ast()
8069 if sz > 0:
8070 e = es[sz1].as_ast()
8071 else:
8072 e = BoolVal(True, self.ctx).as_ast()
8074 self.ctx.ref(), "benchmark generated from python API", "", "unknown", "", sz1, v, e,
8075 )
8076
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 7983 of file z3py.py.

7983 def trail(self):
7984 """Return trail of the solver state after a check() call.
7985 """
7986 return AstVector(Z3_solver_get_trail(self.ctx.ref(), self.solver), self.ctx)
7987
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 7968 of file z3py.py.

7968 def trail_levels(self):
7969 """Return trail and decision levels of the solver state after a check() call.
7970 """
7971 trail = self.trail()
7972 levels = (ctypes.c_uint * len(trail))()
7973 Z3_solver_get_levels(self.ctx.ref(), self.solver, trail.vector, len(trail), levels)
7974 return trail, levels
7975
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 8031 of file z3py.py.

8031 def translate(self, target):
8032 """Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.
8033
8034 >>> c1 = Context()
8035 >>> c2 = Context()
8036 >>> s1 = Solver(ctx=c1)
8037 >>> s2 = s1.translate(c2)
8038 """
8039 if z3_debug():
8040 _z3_assert(isinstance(target, Context), "argument must be a Z3 context")
8041 solver = Z3_solver_translate(self.ctx.ref(), self.solver, target.ref())
8042 return Solver(solver, target)
8043
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 7958 of file z3py.py.

7958 def units(self):
7959 """Return an AST vector containing all currently inferred units.
7960 """
7961 return AstVector(Z3_solver_get_units(self.ctx.ref(), self.solver), self.ctx)
7962
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 7808 of file z3py.py.

7808 def unsat_core(self):
7809 """Return a subset (as an AST vector) of the assumptions provided to the last check().
7810
7811 These are the assumptions Z3 used in the unsatisfiability proof.
7812 Assumptions are available in Z3. They are used to extract unsatisfiable cores.
7813 They may be also used to "retract" assumptions. Note that, assumptions are not really
7814 "soft constraints", but they can be used to implement them.
7815
7816 >>> p1, p2, p3 = Bools('p1 p2 p3')
7817 >>> x, y = Ints('x y')
7818 >>> s = Solver()
7819 >>> s.add(Implies(p1, x > 0))
7820 >>> s.add(Implies(p2, y > x))
7821 >>> s.add(Implies(p2, y < 1))
7822 >>> s.add(Implies(p3, y > -3))
7823 >>> s.check(p1, p2, p3)
7824 unsat
7825 >>> core = s.unsat_core()
7826 >>> len(core)
7827 2
7828 >>> p1 in core
7829 True
7830 >>> p2 in core
7831 True
7832 >>> p3 in core
7833 False
7834 >>> # "Retracting" p2
7835 >>> s.check(p1, p3)
7836 sat
7837 """
7838 return AstVector(Z3_solver_get_unsat_core(self.ctx.ref(), self.solver), self.ctx)
7839
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 7553 of file z3py.py.

◆ ctx

ctx

Definition at line 7552 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(), FiniteSetSortRef.cast(), 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(), ExprRef.update(), DatatypeRef.update_field(), ParamsRef.validate(), FuncEntry.value(), QuantifierRef.var_name(), and QuantifierRef.var_sort().

◆ cube_vs

cube_vs

Definition at line 7884 of file z3py.py.

◆ solver

solver