Z3
Loading...
Searching...
No Matches
Solver Class Reference
Inheritance diagram for Solver:

Public Member Functions

 __init__ (self, solver=None, ctx=None, logFile=None)
 __del__ (self)
 __enter__ (self)
 __exit__ (self, *exc_info)
 set (self, *args, **keys)
 push (self)
 pop (self, num=1)
 num_scopes (self)
 reset (self)
 assert_exprs (self, *args)
 add (self, *args)
 __iadd__ (self, fml)
 append (self, *args)
 insert (self, *args)
 assert_and_track (self, a, p)
 check (self, *assumptions)
 model (self)
 import_model_converter (self, other)
 interrupt (self)
 unsat_core (self)
 consequences (self, assumptions, variables)
 from_file (self, filename)
 from_string (self, s)
 cube (self, vars=None)
 cube_vars (self)
 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 = _get_ctx(ctx)
int backtrack_level = 4000000000
 solver = None
 cube_vs = AstVector(None, self.ctx)

Additional Inherited Members

Protected Member Functions inherited from Z3PPObject
 _repr_html_ (self)

Detailed Description

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

Definition at line 7681 of file z3py.py.

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 7687 of file z3py.py.

7687 def __init__(self, solver=None, ctx=None, logFile=None):
7688 assert solver is None or ctx is not None
7689 self.ctx = _get_ctx(ctx)
7690 self.backtrack_level = 4000000000
7691 self.solver = None
7692 if solver is None:
7693 self.solver = Z3_mk_solver(self.ctx.ref())
7694 else:
7695 self.solver = solver
7696 Z3_solver_inc_ref(self.ctx.ref(), self.solver)
7697 if logFile is not None:
7698 self.set("smtlib2_log", logFile)
7699
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 7700 of file z3py.py.

7700 def __del__(self):
7701 if self.solver is not None and self.ctx.ref() is not None and Z3_solver_dec_ref is not None:
7702 Z3_solver_dec_ref(self.ctx.ref(), self.solver)
7703
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 8181 of file z3py.py.

8181 def __copy__(self):
8182 return self.translate(self.ctx)
8183

◆ __deepcopy__()

__deepcopy__ ( self,
memo = {} )

Definition at line 8184 of file z3py.py.

8184 def __deepcopy__(self, memo={}):
8185 return self.translate(self.ctx)
8186

◆ __enter__()

__enter__ ( self)

Definition at line 7704 of file z3py.py.

7704 def __enter__(self):
7705 self.push()
7706 return self
7707

◆ __exit__()

__exit__ ( self,
* exc_info )

Definition at line 7708 of file z3py.py.

7708 def __exit__(self, *exc_info):
7709 self.pop()
7710

◆ __iadd__()

__iadd__ ( self,
fml )

Definition at line 7830 of file z3py.py.

7830 def __iadd__(self, fml):
7831 self.add(fml)
7832 return self
7833

◆ __repr__()

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

Definition at line 8164 of file z3py.py.

8164 def __repr__(self):
8165 """Return a formatted string with all added constraints."""
8166 return obj_to_string(self)
8167

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

7819 def add(self, *args):
7820 """Assert constraints into the solver.
7821
7822 >>> x = Int('x')
7823 >>> s = Solver()
7824 >>> s.add(x > 0, x < 2)
7825 >>> s
7826 [x > 0, x < 2]
7827 """
7828 self.assert_exprs(*args)
7829

Referenced by __iadd__().

◆ append()

append ( self,
* args )
Assert constraints into the solver.

>>> x = Int('x')
>>> s = Solver()
>>> s.append(x > 0, x < 2)
>>> s
[x > 0, x < 2]

Definition at line 7834 of file z3py.py.

7834 def append(self, *args):
7835 """Assert constraints into the solver.
7836
7837 >>> x = Int('x')
7838 >>> s = Solver()
7839 >>> s.append(x > 0, x < 2)
7840 >>> s
7841 [x > 0, x < 2]
7842 """
7843 self.assert_exprs(*args)
7844

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

7856 def assert_and_track(self, a, p):
7857 """Assert constraint `a` and track it in the unsat core using the Boolean constant `p`.
7858
7859 If `p` is a string, it will be automatically converted into a Boolean constant.
7860
7861 >>> x = Int('x')
7862 >>> p3 = Bool('p3')
7863 >>> s = Solver()
7864 >>> s.set(unsat_core=True)
7865 >>> s.assert_and_track(x > 0, 'p1')
7866 >>> s.assert_and_track(x != 1, 'p2')
7867 >>> s.assert_and_track(x < 0, p3)
7868 >>> print(s.check())
7869 unsat
7870 >>> c = s.unsat_core()
7871 >>> len(c)
7872 2
7873 >>> Bool('p1') in c
7874 True
7875 >>> Bool('p2') in c
7876 False
7877 >>> p3 in c
7878 True
7879 """
7880 if isinstance(p, str):
7881 p = Bool(p, self.ctx)
7882 _z3_assert(isinstance(a, BoolRef), "Boolean expression expected")
7883 _z3_assert(isinstance(p, BoolRef) and is_const(p), "Boolean expression expected")
7884 Z3_solver_assert_and_track(self.ctx.ref(), self.solver, a.as_ast(), p.as_ast())
7885
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 7800 of file z3py.py.

7800 def assert_exprs(self, *args):
7801 """Assert constraints into the solver.
7802
7803 >>> x = Int('x')
7804 >>> s = Solver()
7805 >>> s.assert_exprs(x > 0, x < 2)
7806 >>> s
7807 [x > 0, x < 2]
7808 """
7809 args = _get_args(args)
7810 s = BoolSort(self.ctx)
7811 for arg in args:
7812 if isinstance(arg, Goal) or isinstance(arg, AstVector):
7813 for f in arg:
7814 Z3_solver_assert(self.ctx.ref(), self.solver, f.as_ast())
7815 else:
7816 arg = s.cast(arg)
7817 Z3_solver_assert(self.ctx.ref(), self.solver, arg.as_ast())
7818
void Z3_API Z3_solver_assert(Z3_context c, Z3_solver s, Z3_ast a)
Assert a constraint into the solver.

Referenced by add(), append(), and insert().

◆ assertions()

assertions ( self)
Return an AST vector containing all added constraints.

>>> s = Solver()
>>> s.assertions()
[]
>>> a = Int('a')
>>> s.add(a > 0)
>>> s.add(a < 10)
>>> s.assertions()
[a > 0, a < 10]

Definition at line 8081 of file z3py.py.

8081 def assertions(self):
8082 """Return an AST vector containing all added constraints.
8083
8084 >>> s = Solver()
8085 >>> s.assertions()
8086 []
8087 >>> a = Int('a')
8088 >>> s.add(a > 0)
8089 >>> s.add(a < 10)
8090 >>> s.assertions()
8091 [a > 0, a < 10]
8092 """
8093 return AstVector(Z3_solver_get_assertions(self.ctx.ref(), self.solver), self.ctx)
8094
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 7886 of file z3py.py.

7886 def check(self, *assumptions):
7887 """Check whether the assertions in the given solver plus the optional assumptions are consistent or not.
7888
7889 >>> x = Int('x')
7890 >>> s = Solver()
7891 >>> s.check()
7892 sat
7893 >>> s.add(x > 0, x < 2)
7894 >>> s.check()
7895 sat
7896 >>> s.model().eval(x)
7897 1
7898 >>> s.add(x < 1)
7899 >>> s.check()
7900 unsat
7901 >>> s.reset()
7902 >>> s.add(2**x == 4)
7903 >>> s.check()
7904 sat
7905 """
7906 s = BoolSort(self.ctx)
7907 assumptions = _get_args(assumptions)
7908 num = len(assumptions)
7909 _assumptions = (Ast * num)()
7910 for i in range(num):
7911 _assumptions[i] = s.cast(assumptions[i]).as_ast()
7912 r = Z3_solver_check_assumptions(self.ctx.ref(), self.solver, num, _assumptions)
7913 return CheckSatResult(r)
7914
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 8058 of file z3py.py.

8058 def congruence_explain(self, a, b):
8059 """Explain congruence of a and b relative to the current search state"""
8060 a = _py2expr(a, self.ctx)
8061 b = _py2expr(b, self.ctx)
8062 return _to_expr_ref(Z3_solver_congruence_explain(self.ctx.ref(), self.solver, a.ast, b.ast), self.ctx)
8063
8064
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 8050 of file z3py.py.

8050 def congruence_next(self, t):
8051 """Retrieve congruence closure sibling of the term t relative to the current search state
8052 The function primarily works for SimpleSolver. Terms and variables that are
8053 eliminated during pre-processing are not visible to the congruence closure.
8054 """
8055 t = _py2expr(t, self.ctx)
8056 return _to_expr_ref(Z3_solver_congruence_next(self.ctx.ref(), self.solver, t.ast), self.ctx)
8057
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 8042 of file z3py.py.

8042 def congruence_root(self, t):
8043 """Retrieve congruence closure root of the term t relative to the current search state
8044 The function primarily works for SimpleSolver. Terms and variables that are
8045 eliminated during pre-processing are not visible to the congruence closure.
8046 """
8047 t = _py2expr(t, self.ctx)
8048 return _to_expr_ref(Z3_solver_congruence_root(self.ctx.ref(), self.solver, t.ast), self.ctx)
8049
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 7977 of file z3py.py.

7977 def consequences(self, assumptions, variables):
7978 """Determine fixed values for the variables based on the solver state and assumptions.
7979 >>> s = Solver()
7980 >>> a, b, c, d = Bools('a b c d')
7981 >>> s.add(Implies(a,b), Implies(b, c))
7982 >>> s.consequences([a],[b,c,d])
7983 (sat, [Implies(a, b), Implies(a, c)])
7984 >>> s.consequences([Not(c),d],[a,b,c,d])
7985 (sat, [Implies(d, d), Implies(Not(c), Not(c)), Implies(Not(c), Not(b)), Implies(Not(c), Not(a))])
7986 """
7987 if isinstance(assumptions, list):
7988 _asms = AstVector(None, self.ctx)
7989 for a in assumptions:
7990 _asms.push(a)
7991 assumptions = _asms
7992 if isinstance(variables, list):
7993 _vars = AstVector(None, self.ctx)
7994 for a in variables:
7995 _vars.push(a)
7996 variables = _vars
7997 _z3_assert(isinstance(assumptions, AstVector), "ast vector expected")
7998 _z3_assert(isinstance(variables, AstVector), "ast vector expected")
7999 consequences = AstVector(None, self.ctx)
8000 r = Z3_solver_get_consequences(self.ctx.ref(), self.solver, assumptions.vector,
8001 variables.vector, consequences.vector)
8002 sz = len(consequences)
8003 consequences = [consequences[i] for i in range(sz)]
8004 return CheckSatResult(r), consequences
8005
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 8014 of file z3py.py.

8014 def cube(self, vars=None):
8015 """Get set of cubes
8016 The method takes an optional set of variables that restrict which
8017 variables may be used as a starting point for cubing.
8018 If vars is not None, then the first case split is based on a variable in
8019 this set.
8020 """
8021 self.cube_vs = AstVector(None, self.ctx)
8022 if vars is not None:
8023 for v in vars:
8024 self.cube_vs.push(v)
8025 while True:
8026 lvl = self.backtrack_level
8027 self.backtrack_level = 4000000000
8028 r = AstVector(Z3_solver_cube(self.ctx.ref(), self.solver, self.cube_vs.vector, lvl), self.ctx)
8029 if (len(r) == 1 and is_false(r[0])):
8030 return
8031 yield r
8032 if (len(r) == 0):
8033 return
8034
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 8035 of file z3py.py.

8035 def cube_vars(self):
8036 """Access the set of variables that were touched by the most recently generated cube.
8037 This set of variables can be used as a starting point for additional cubes.
8038 The idea is that variables that appear in clauses that are reduced by the most recent
8039 cube are likely more useful to cube on."""
8040 return self.cube_vs
8041

◆ dimacs()

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

Definition at line 8192 of file z3py.py.

8192 def dimacs(self, include_names=True):
8193 """Return a textual representation of the solver in DIMACS format."""
8194 return Z3_solver_to_dimacs_string(self.ctx.ref(), self.solver, include_names)
8195
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 8006 of file z3py.py.

8006 def from_file(self, filename):
8007 """Parse assertions from a file"""
8008 Z3_solver_from_file(self.ctx.ref(), self.solver, filename)
8009
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 8010 of file z3py.py.

8010 def from_string(self, s):
8011 """Parse assertions from a string"""
8012 Z3_solver_from_string(self.ctx.ref(), self.solver, s)
8013
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 8156 of file z3py.py.

8156 def help(self):
8157 """Display a string describing all available options."""
8158 print(Z3_solver_get_help(self.ctx.ref(), self.solver))
8159
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 7934 of file z3py.py.

7934 def import_model_converter(self, other):
7935 """Import model converter from other into the current solver"""
7936 Z3_solver_import_model_converter(self.ctx.ref(), other.solver, self.solver)
7937

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

7845 def insert(self, *args):
7846 """Assert constraints into the solver.
7847
7848 >>> x = Int('x')
7849 >>> s = Solver()
7850 >>> s.insert(x > 0, x < 2)
7851 >>> s
7852 [x > 0, x < 2]
7853 """
7854 self.assert_exprs(*args)
7855

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

7938 def interrupt(self):
7939 """Interrupt the execution of the solver object.
7940 Remarks: This ensures that the interrupt applies only
7941 to the given solver object and it applies only if it is running.
7942 """
7943 Z3_solver_interrupt(self.ctx.ref(), self.solver)
7944
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 7915 of file z3py.py.

7915 def model(self):
7916 """Return a model for the last `check()`.
7917
7918 This function raises an exception if
7919 a model is not available (e.g., last `check()` returned unsat).
7920
7921 >>> s = Solver()
7922 >>> a = Int('a')
7923 >>> s.add(a + 2 == 0)
7924 >>> s.check()
7925 sat
7926 >>> s.model()
7927 [a = -2]
7928 """
7929 try:
7930 return ModelRef(Z3_solver_get_model(self.ctx.ref(), self.solver), self.ctx)
7931 except Z3Exception:
7932 raise Z3Exception("model is not available")
7933
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.

◆ non_units()

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

Definition at line 8100 of file z3py.py.

8100 def non_units(self):
8101 """Return an AST vector containing all atomic formulas in solver state that are not units.
8102 """
8103 return AstVector(Z3_solver_get_non_units(self.ctx.ref(), self.solver), self.ctx)
8104
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 7768 of file z3py.py.

7768 def num_scopes(self):
7769 """Return the current number of backtracking points.
7770
7771 >>> s = Solver()
7772 >>> s.num_scopes()
7773 0
7774 >>> s.push()
7775 >>> s.num_scopes()
7776 1
7777 >>> s.push()
7778 >>> s.num_scopes()
7779 2
7780 >>> s.pop()
7781 >>> s.num_scopes()
7782 1
7783 """
7784 return Z3_solver_get_num_scopes(self.ctx.ref(), self.solver)
7785
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 8160 of file z3py.py.

8160 def param_descrs(self):
8161 """Return the parameter description set."""
8162 return ParamDescrsRef(Z3_solver_get_param_descrs(self.ctx.ref(), self.solver), self.ctx)
8163
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 7746 of file z3py.py.

7746 def pop(self, num=1):
7747 """Backtrack \\c num backtracking points.
7748
7749 >>> x = Int('x')
7750 >>> s = Solver()
7751 >>> s.add(x > 0)
7752 >>> s
7753 [x > 0]
7754 >>> s.push()
7755 >>> s.add(x < 1)
7756 >>> s
7757 [x > 0, x < 1]
7758 >>> s.check()
7759 unsat
7760 >>> s.pop()
7761 >>> s.check()
7762 sat
7763 >>> s
7764 [x > 0]
7765 """
7766 Z3_solver_pop(self.ctx.ref(), self.solver, num)
7767
void Z3_API Z3_solver_pop(Z3_context c, Z3_solver s, unsigned n)
Backtrack n backtracking points.

Referenced by __exit__().

◆ proof()

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

Definition at line 8077 of file z3py.py.

8077 def proof(self):
8078 """Return a proof for the last `check()`. Proof construction must be enabled."""
8079 return _to_expr_ref(Z3_solver_get_proof(self.ctx.ref(), self.solver), self.ctx)
8080
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 7724 of file z3py.py.

7724 def push(self):
7725 """Create a backtracking point.
7726
7727 >>> x = Int('x')
7728 >>> s = Solver()
7729 >>> s.add(x > 0)
7730 >>> s
7731 [x > 0]
7732 >>> s.push()
7733 >>> s.add(x < 1)
7734 >>> s
7735 [x > 0, x < 1]
7736 >>> s.check()
7737 unsat
7738 >>> s.pop()
7739 >>> s.check()
7740 sat
7741 >>> s
7742 [x > 0]
7743 """
7744 Z3_solver_push(self.ctx.ref(), self.solver)
7745
void Z3_API Z3_solver_push(Z3_context c, Z3_solver s)
Create a backtracking point.

Referenced by __enter__().

◆ reason_unknown()

reason_unknown ( self)
Return a string describing why the last `check()` returned `unknown`.

>>> x = Int('x')
>>> s = SimpleSolver()
>>> s.add(x == 2**x)
>>> s.check()
unknown
>>> s.reason_unknown()
'(incomplete (theory arithmetic))'

Definition at line 8143 of file z3py.py.

8143 def reason_unknown(self):
8144 """Return a string describing why the last `check()` returned `unknown`.
8145
8146 >>> x = Int('x')
8147 >>> s = SimpleSolver()
8148 >>> s.add(x == 2**x)
8149 >>> s.check()
8150 unknown
8151 >>> s.reason_unknown()
8152 '(incomplete (theory arithmetic))'
8153 """
8154 return Z3_solver_get_reason_unknown(self.ctx.ref(), self.solver)
8155
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 7786 of file z3py.py.

7786 def reset(self):
7787 """Remove all asserted constraints and backtracking points created using `push()`.
7788
7789 >>> x = Int('x')
7790 >>> s = Solver()
7791 >>> s.add(x > 0)
7792 >>> s
7793 [x > 0]
7794 >>> s.reset()
7795 >>> s
7796 []
7797 """
7798 Z3_solver_reset(self.ctx.ref(), self.solver)
7799
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 7711 of file z3py.py.

7711 def set(self, *args, **keys):
7712 """Set a configuration option.
7713 The method `help()` return a string containing all available options.
7714
7715 >>> s = Solver()
7716 >>> # The option MBQI can be set using three different approaches.
7717 >>> s.set(mbqi=True)
7718 >>> s.set('MBQI', True)
7719 >>> s.set(':mbqi', True)
7720 """
7721 p = args2params(args, keys, self.ctx)
7722 Z3_solver_set_params(self.ctx.ref(), self.solver, p.params)
7723
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 8113 of file z3py.py.

8113 def set_initial_value(self, var, value):
8114 """initialize the solver's state by setting the initial value of var to value
8115 """
8116 s = var.sort()
8117 value = s.cast(value)
8118 Z3_solver_set_initial_value(self.ctx.ref(), self.solver, var.ast, value.ast)
8119
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 8187 of file z3py.py.

8187 def sexpr(self):
8188 """Return a formatted string (in Lisp-like format) with all added constraints.
8189 """
8190 return Z3_solver_to_string(self.ctx.ref(), self.solver)
8191
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 8214 of file z3py.py.

8214 def solutions(self, t):
8215 """Returns an iterator over solutions that satisfy the constraints.
8216
8217 The parameter `t` is an expression whose values should be returned.
8218
8219 >>> s = Solver()
8220 >>> x, y, z = Ints("x y z")
8221 >>> s.add(x * x == 4)
8222 >>> print(list(s.solutions(x)))
8223 [-2, 2]
8224 >>> s.reset()
8225 >>> s.add(x >= 0, x < 10)
8226 >>> print(list(s.solutions(x)))
8227 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
8228 >>> s.reset()
8229 >>> s.add(x >= 0, y < 10, y == 2*x)
8230 >>> print(list(s.solutions([x, y])))
8231 [[0, 0], [1, 2], [2, 4], [3, 6], [4, 8]]
8232 """
8233 s = Solver()
8234 s.add(self.assertions())
8235 t = _get_args(t)
8236 if isinstance(t, (list, tuple)):
8237 while s.check() == sat:
8238 result = [s.model().eval(t_, model_completion=True) for t_ in t]
8239 yield result
8240 s.add(Or(t_ != result_ for t_, result_ in zip(t, result)))
8241 else:
8242 while s.check() == sat:
8243 result = s.model().eval(t, model_completion=True)
8244 yield result
8245 s.add(t != result)
8246
8247

◆ solve_for()

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

Definition at line 8065 of file z3py.py.

8065 def solve_for(self, ts):
8066 """Retrieve a solution for t relative to linear equations maintained in the current state."""
8067 vars = AstVector(ctx=self.ctx);
8068 terms = AstVector(ctx=self.ctx);
8069 guards = AstVector(ctx=self.ctx);
8070 for t in ts:
8071 t = _py2expr(t, self.ctx)
8072 vars.push(t)
8073 Z3_solver_solve_for(self.ctx.ref(), self.solver, vars.vector, terms.vector, guards.vector)
8074 return [(vars[i], terms[i], guards[i]) for i in range(len(vars))]
8075
8076
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 8125 of file z3py.py.

8125 def statistics(self):
8126 """Return statistics for the last `check()`.
8127
8128 >>> s = SimpleSolver()
8129 >>> x = Int('x')
8130 >>> s.add(x > 0)
8131 >>> s.check()
8132 sat
8133 >>> st = s.statistics()
8134 >>> st.get_key_value('final checks')
8135 1
8136 >>> len(st) > 0
8137 True
8138 >>> st[0] != 0
8139 True
8140 """
8141 return Statistics(Z3_solver_get_statistics(self.ctx.ref(), self.solver), self.ctx)
8142
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 8196 of file z3py.py.

8196 def to_smt2(self):
8197 """return SMTLIB2 formatted benchmark for solver's assertions"""
8198 es = self.assertions()
8199 sz = len(es)
8200 sz1 = sz
8201 if sz1 > 0:
8202 sz1 -= 1
8203 v = (Ast * sz1)()
8204 for i in range(sz1):
8205 v[i] = es[i].as_ast()
8206 if sz > 0:
8207 e = es[sz1].as_ast()
8208 else:
8209 e = BoolVal(True, self.ctx).as_ast()
8211 self.ctx.ref(), "benchmark generated from python API", "", "unknown", "", sz1, v, e,
8212 )
8213
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 8120 of file z3py.py.

8120 def trail(self):
8121 """Return trail of the solver state after a check() call.
8122 """
8123 return AstVector(Z3_solver_get_trail(self.ctx.ref(), self.solver), self.ctx)
8124
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 8105 of file z3py.py.

8105 def trail_levels(self):
8106 """Return trail and decision levels of the solver state after a check() call.
8107 """
8108 trail = self.trail()
8109 levels = (ctypes.c_uint * len(trail))()
8110 Z3_solver_get_levels(self.ctx.ref(), self.solver, trail.vector, len(trail), levels)
8111 return trail, levels
8112
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 8168 of file z3py.py.

8168 def translate(self, target):
8169 """Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.
8170
8171 >>> c1 = Context()
8172 >>> c2 = Context()
8173 >>> s1 = Solver(ctx=c1)
8174 >>> s2 = s1.translate(c2)
8175 """
8176 if z3_debug():
8177 _z3_assert(isinstance(target, Context), "argument must be a Z3 context")
8178 solver = Z3_solver_translate(self.ctx.ref(), self.solver, target.ref())
8179 return Solver(solver, target)
8180
Z3_solver Z3_API Z3_solver_translate(Z3_context source, Z3_solver s, Z3_context target)
Copy a solver s from the context source to the context target.

◆ units()

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

Definition at line 8095 of file z3py.py.

8095 def units(self):
8096 """Return an AST vector containing all currently inferred units.
8097 """
8098 return AstVector(Z3_solver_get_units(self.ctx.ref(), self.solver), self.ctx)
8099
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 7945 of file z3py.py.

7945 def unsat_core(self):
7946 """Return a subset (as an AST vector) of the assumptions provided to the last check().
7947
7948 These are the assumptions Z3 used in the unsatisfiability proof.
7949 Assumptions are available in Z3. They are used to extract unsatisfiable cores.
7950 They may be also used to "retract" assumptions. Note that, assumptions are not really
7951 "soft constraints", but they can be used to implement them.
7952
7953 >>> p1, p2, p3 = Bools('p1 p2 p3')
7954 >>> x, y = Ints('x y')
7955 >>> s = Solver()
7956 >>> s.add(Implies(p1, x > 0))
7957 >>> s.add(Implies(p2, y > x))
7958 >>> s.add(Implies(p2, y < 1))
7959 >>> s.add(Implies(p3, y > -3))
7960 >>> s.check(p1, p2, p3)
7961 unsat
7962 >>> core = s.unsat_core()
7963 >>> len(core)
7964 2
7965 >>> p1 in core
7966 True
7967 >>> p2 in core
7968 True
7969 >>> p3 in core
7970 False
7971 >>> # "Retracting" p2
7972 >>> s.check(p1, p3)
7973 sat
7974 """
7975 return AstVector(Z3_solver_get_unsat_core(self.ctx.ref(), self.solver), self.ctx)
7976
Z3_ast_vector Z3_API Z3_solver_get_unsat_core(Z3_context c, Z3_solver s)
Retrieve the unsat core for the last Z3_solver_check_assumptions The unsat core is a subset of the as...

Field Documentation

◆ backtrack_level

int backtrack_level = 4000000000

Definition at line 7690 of file z3py.py.

◆ ctx

ctx = _get_ctx(ctx)

Definition at line 7689 of file z3py.py.

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

◆ cube_vs

cube_vs = AstVector(None, self.ctx)

Definition at line 8021 of file z3py.py.

◆ solver

solver = None

Definition at line 7691 of file z3py.py.

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