Z3
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines
Public Member Functions | Data Fields
Solver Class Reference
+ Inheritance diagram for Solver:

Public Member Functions

def __init__
def __del__
def set
def push
def pop
def reset
def assert_exprs
def add
def append
def insert
def assert_and_track
def check
def model
def unsat_core
def proof
def assertions
def statistics
def reason_unknown
def help
def param_descrs
def __repr__
def sexpr
def to_smt2

Data Fields

 ctx
 solver

Detailed Description

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

Definition at line 5775 of file z3py.py.


Constructor & Destructor Documentation

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

Definition at line 5778 of file z3py.py.

05778 
05779     def __init__(self, solver=None, ctx=None):
05780         assert solver == None or ctx != None
05781         self.ctx    = _get_ctx(ctx)
05782         self.solver = None
05783         if solver == None:
05784             self.solver = Z3_mk_solver(self.ctx.ref())
05785         else:
05786             self.solver = solver
05787         Z3_solver_inc_ref(self.ctx.ref(), self.solver)

def __del__ (   self)

Definition at line 5788 of file z3py.py.

05788 
05789     def __del__(self):
05790         if self.solver != None:
05791             Z3_solver_dec_ref(self.ctx.ref(), self.solver)


Member Function Documentation

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

Definition at line 6080 of file z3py.py.

06080 
06081     def __repr__(self):
06082         """Return a formatted string with all added constraints."""
06083         return obj_to_string(self)

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

05881 
05882     def add(self, *args):
05883         """Assert constraints into the solver.
05884         
05885         >>> x = Int('x')
05886         >>> s = Solver()
05887         >>> s.add(x > 0, x < 2)
05888         >>> s
05889         [x > 0, x < 2]
05890         """
05891         self.assert_exprs(*args)

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

05892 
05893     def append(self, *args):
05894         """Assert constraints into the solver.
05895         
05896         >>> x = Int('x')
05897         >>> s = Solver()
05898         >>> s.append(x > 0, x < 2)
05899         >>> s
05900         [x > 0, x < 2]
05901         """
05902         self.assert_exprs(*args)

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

05914 
05915     def assert_and_track(self, a, p):
05916         """Assert constraint `a` and track it in the unsat core using the Boolean constant `p`.
05917         
05918         If `p` is a string, it will be automatically converted into a Boolean constant.
05919         
05920         >>> x = Int('x')
05921         >>> p3 = Bool('p3')
05922         >>> s = Solver()
05923         >>> s.set(unsat_core=True)
05924         >>> s.assert_and_track(x > 0,  'p1')
05925         >>> s.assert_and_track(x != 1, 'p2')
05926         >>> s.assert_and_track(x < 0,  p3)
05927         >>> print(s.check())
05928         unsat
05929         >>> c = s.unsat_core()
05930         >>> len(c)
05931         2
05932         >>> Bool('p1') in c
05933         True
05934         >>> Bool('p2') in c
05935         False
05936         >>> p3 in c
05937         True
05938         """
05939         if isinstance(p, str):
05940             p = Bool(p, self.ctx)
05941         _z3_assert(isinstance(a, BoolRef), "Boolean expression expected")
05942         _z3_assert(isinstance(p, BoolRef) and is_const(p), "Boolean expression expected")
05943         Z3_solver_assert_and_track(self.ctx.ref(), self.solver, a.as_ast(), p.as_ast())

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

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

05862 
05863     def assert_exprs(self, *args):
05864         """Assert constraints into the solver.
05865         
05866         >>> x = Int('x')
05867         >>> s = Solver()
05868         >>> s.assert_exprs(x > 0, x < 2)
05869         >>> s
05870         [x > 0, x < 2]
05871         """
05872         args = _get_args(args)
05873         s    = BoolSort(self.ctx)
05874         for arg in args:
05875             if isinstance(arg, Goal) or isinstance(arg, AstVector):
05876                 for f in arg:
05877                     Z3_solver_assert(self.ctx.ref(), self.solver, f.as_ast())
05878             else:
05879                 arg = s.cast(arg)
05880                 Z3_solver_assert(self.ctx.ref(), self.solver, arg.as_ast())

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

Referenced by Solver.to_smt2().

06027 
06028     def assertions(self):
06029         """Return an AST vector containing all added constraints.
06030         
06031         >>> s = Solver()
06032         >>> s.assertions()
06033         []
06034         >>> a = Int('a')
06035         >>> s.add(a > 0)
06036         >>> s.add(a < 10)
06037         >>> s.assertions()
06038         [a > 0, a < 10]
06039         """
06040         return AstVector(Z3_solver_get_assertions(self.ctx.ref(), self.solver), self.ctx)

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()
[x = 1]
>>> s.add(x < 1)
>>> s.check()
unsat
>>> s.reset()
>>> s.add(2**x == 4)
>>> s.check()
unknown

Definition at line 5944 of file z3py.py.

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

05944 
05945     def check(self, *assumptions):
05946         """Check whether the assertions in the given solver plus the optional assumptions are consistent or not.
05947         
05948         >>> x = Int('x')
05949         >>> s = Solver()
05950         >>> s.check()
05951         sat
05952         >>> s.add(x > 0, x < 2)
05953         >>> s.check()
05954         sat
05955         >>> s.model()
05956         [x = 1]
05957         >>> s.add(x < 1)
05958         >>> s.check()
05959         unsat
05960         >>> s.reset()
05961         >>> s.add(2**x == 4)
05962         >>> s.check()
05963         unknown
05964         """
05965         assumptions = _get_args(assumptions)
05966         num = len(assumptions)
05967         _assumptions = (Ast * num)()
05968         for i in range(num):
05969             _assumptions[i] = assumptions[i].as_ast()
05970         r = Z3_solver_check_assumptions(self.ctx.ref(), self.solver, num, _assumptions)
05971         return CheckSatResult(r)

def help (   self)
Display a string describing all available options.

Definition at line 6072 of file z3py.py.

Referenced by Solver.set().

06072 
06073     def help(self):
06074         """Display a string describing all available options."""
06075         print(Z3_solver_get_help(self.ctx.ref(), self.solver))

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

05903 
05904     def insert(self, *args):
05905         """Assert constraints into the solver.
05906         
05907         >>> x = Int('x')
05908         >>> s = Solver()
05909         >>> s.insert(x > 0, x < 2)
05910         >>> s
05911         [x > 0, x < 2]
05912         """
05913         self.assert_exprs(*args)

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

05972 
05973     def model(self):
05974         """Return a model for the last `check()`. 
05975         
05976         This function raises an exception if 
05977         a model is not available (e.g., last `check()` returned unsat).
05978 
05979         >>> s = Solver()
05980         >>> a = Int('a')
05981         >>> s.add(a + 2 == 0)
05982         >>> s.check()
05983         sat
05984         >>> s.model()
05985         [a = -2]
05986         """
05987         try:
05988             return ModelRef(Z3_solver_get_model(self.ctx.ref(), self.solver), self.ctx)
05989         except Z3Exception:
05990             raise Z3Exception("model is not available")

def param_descrs (   self)
Return the parameter description set.

Definition at line 6076 of file z3py.py.

06076 
06077     def param_descrs(self):
06078         """Return the parameter description set."""
06079         return ParamDescrsRef(Z3_solver_get_param_descrs(self.ctx.ref(), self.solver), self.ctx)

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

05826 
05827     def pop(self, num=1):
05828         """Backtrack \c num backtracking points.
05829         
05830         >>> x = Int('x')
05831         >>> s = Solver()
05832         >>> s.add(x > 0)
05833         >>> s
05834         [x > 0]
05835         >>> s.push()
05836         >>> s.add(x < 1)
05837         >>> s
05838         [x > 0, x < 1]
05839         >>> s.check()
05840         unsat
05841         >>> s.pop()
05842         >>> s.check()
05843         sat
05844         >>> s
05845         [x > 0]
05846         """
05847         Z3_solver_pop(self.ctx.ref(), self.solver, num)

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

Definition at line 6023 of file z3py.py.

06023 
06024     def proof(self):
06025         """Return a proof for the last `check()`. Proof construction must be enabled."""
06026         return _to_expr_ref(Z3_solver_get_proof(self.ctx.ref(), self.solver), self.ctx)

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

Referenced by Solver.reset().

05804 
05805     def push(self):
05806         """Create a backtracking point.
05807 
05808         >>> x = Int('x')
05809         >>> s = Solver()
05810         >>> s.add(x > 0)
05811         >>> s
05812         [x > 0]
05813         >>> s.push()
05814         >>> s.add(x < 1)
05815         >>> s
05816         [x > 0, x < 1]
05817         >>> s.check()
05818         unsat
05819         >>> s.pop()
05820         >>> s.check()
05821         sat
05822         >>> s
05823         [x > 0]
05824         """
05825         Z3_solver_push(self.ctx.ref(), self.solver)

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

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

Definition at line 6059 of file z3py.py.

06059 
06060     def reason_unknown(self):
06061         """Return a string describing why the last `check()` returned `unknown`.
06062         
06063         >>> x = Int('x')
06064         >>> s = SimpleSolver()
06065         >>> s.add(2**x == 4)
06066         >>> s.check()
06067         unknown
06068         >>> s.reason_unknown()
06069         '(incomplete (theory arithmetic))'
06070         """
06071         return Z3_solver_get_reason_unknown(self.ctx.ref(), self.solver)
    
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 5848 of file z3py.py.

05848 
05849     def reset(self):
05850         """Remove all asserted constraints and backtracking points created using `push()`.
05851         
05852         >>> x = Int('x')
05853         >>> s = Solver()
05854         >>> s.add(x > 0)
05855         >>> s
05856         [x > 0]
05857         >>> s.reset()
05858         >>> s
05859         []
05860         """
05861         Z3_solver_reset(self.ctx.ref(), self.solver)
    
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 5792 of file z3py.py.

05792 
05793     def set(self, *args, **keys):
05794         """Set a configuration option. The method `help()` return a string containing all available options.
05795         
05796         >>> s = Solver()
05797         >>> # The option MBQI can be set using three different approaches.
05798         >>> s.set(mbqi=True)
05799         >>> s.set('MBQI', True)
05800         >>> s.set(':mbqi', True)
05801         """
05802         p = args2params(args, keys, self.ctx)
05803         Z3_solver_set_params(self.ctx.ref(), self.solver, p.params)

def sexpr (   self)
Return a formatted string (in Lisp-like format) with all added constraints. We say the string is in s-expression format.

>>> x = Int('x')
>>> s = Solver()
>>> s.add(x > 0)
>>> s.add(x < 2)
>>> r = s.sexpr()

Definition at line 6084 of file z3py.py.

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

06084 
06085     def sexpr(self):
06086         """Return a formatted string (in Lisp-like format) with all added constraints. We say the string is in s-expression format.
06087         
06088         >>> x = Int('x')
06089         >>> s = Solver()
06090         >>> s.add(x > 0)
06091         >>> s.add(x < 2)
06092         >>> r = s.sexpr()
06093         """
06094         return Z3_solver_to_string(self.ctx.ref(), self.solver)

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

06041 
06042     def statistics(self):
06043         """Return statistics for the last `check()`.
06044         
06045         >>> s = SimpleSolver()
06046         >>> x = Int('x')
06047         >>> s.add(x > 0)
06048         >>> s.check()
06049         sat
06050         >>> st = s.statistics()
06051         >>> st.get_key_value('final checks')
06052         1
06053         >>> len(st) > 0
06054         True
06055         >>> st[0] != 0
06056         True
06057         """
06058         return Statistics(Z3_solver_get_statistics(self.ctx.ref(), self.solver), self.ctx)

def to_smt2 (   self)
return SMTLIB2 formatted benchmark for solver's assertions

Definition at line 6095 of file z3py.py.

06095 
06096     def to_smt2(self):
06097         """return SMTLIB2 formatted benchmark for solver's assertions"""
06098         es = self.assertions()
06099         sz = len(es)
06100         sz1 = sz
06101         if sz1 > 0:
06102             sz1 -= 1
06103         v = (Ast * sz1)()
06104         for i in range(sz1):
06105             v[i] = es[i].as_ast()
06106         if sz > 0:
06107             e = es[sz1].as_ast()
06108         else:
06109             e = BoolVal(True, self.ctx).as_ast()
06110         return Z3_benchmark_to_smtlib_string(self.ctx.ref(), "benchmark generated from python API", "", "unknown", "", sz1, v, e)

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

05991 
05992     def unsat_core(self):
05993         """Return a subset (as an AST vector) of the assumptions provided to the last check().
05994         
05995         These are the assumptions Z3 used in the unsatisfiability proof.
05996         Assumptions are available in Z3. They are used to extract unsatisfiable cores. 
05997         They may be also used to "retract" assumptions. Note that, assumptions are not really 
05998         "soft constraints", but they can be used to implement them. 
05999 
06000         >>> p1, p2, p3 = Bools('p1 p2 p3')
06001         >>> x, y       = Ints('x y')
06002         >>> s          = Solver()
06003         >>> s.add(Implies(p1, x > 0))
06004         >>> s.add(Implies(p2, y > x))
06005         >>> s.add(Implies(p2, y < 1))
06006         >>> s.add(Implies(p3, y > -3))
06007         >>> s.check(p1, p2, p3)
06008         unsat
06009         >>> core = s.unsat_core()
06010         >>> len(core)
06011         2
06012         >>> p1 in core
06013         True
06014         >>> p2 in core
06015         True
06016         >>> p3 in core
06017         False
06018         >>> # "Retracting" p2
06019         >>> s.check(p1, p3)
06020         sat
06021         """
06022         return AstVector(Z3_solver_get_unsat_core(self.ctx.ref(), self.solver), self.ctx)


Field Documentation

ctx

Definition at line 5778 of file z3py.py.

Referenced by ArithRef::__add__(), BitVecRef::__add__(), BitVecRef::__and__(), FuncDeclRef::__call__(), ArithRef::__div__(), BitVecRef::__div__(), ExprRef::__eq__(), Probe::__eq__(), ArithRef::__ge__(), BitVecRef::__ge__(), Probe::__ge__(), ArrayRef::__getitem__(), ApplyResult::__getitem__(), ArithRef::__gt__(), BitVecRef::__gt__(), Probe::__gt__(), BitVecRef::__invert__(), ArithRef::__le__(), BitVecRef::__le__(), Probe::__le__(), BitVecRef::__lshift__(), ArithRef::__lt__(), BitVecRef::__lt__(), Probe::__lt__(), ArithRef::__mod__(), BitVecRef::__mod__(), ArithRef::__mul__(), BitVecRef::__mul__(), ExprRef::__ne__(), Probe::__ne__(), ArithRef::__neg__(), BitVecRef::__neg__(), BitVecRef::__or__(), ArithRef::__pow__(), ArithRef::__radd__(), BitVecRef::__radd__(), BitVecRef::__rand__(), ArithRef::__rdiv__(), BitVecRef::__rdiv__(), BitVecRef::__rlshift__(), ArithRef::__rmod__(), BitVecRef::__rmod__(), ArithRef::__rmul__(), BitVecRef::__rmul__(), BitVecRef::__ror__(), ArithRef::__rpow__(), BitVecRef::__rrshift__(), BitVecRef::__rshift__(), ArithRef::__rsub__(), BitVecRef::__rsub__(), BitVecRef::__rxor__(), ArithRef::__sub__(), BitVecRef::__sub__(), BitVecRef::__xor__(), DatatypeSortRef::accessor(), Fixedpoint::add_rule(), Optimize::add_soft(), Tactic::apply(), AlgebraicNumRef::approx(), ExprRef::arg(), ApplyResult::as_expr(), Solver::assert_and_track(), Solver::assert_exprs(), Fixedpoint::assert_exprs(), Solver::assertions(), QuantifierRef::body(), BoolSortRef::cast(), DatatypeSortRef::constructor(), ApplyResult::convert_model(), ExprRef::decl(), RatNumRef::denominator(), FuncDeclRef::domain(), ArraySortRef::domain(), Fixedpoint::get_answer(), Fixedpoint::get_assertions(), Fixedpoint::get_cover_delta(), Fixedpoint::get_rules(), SortRef::kind(), ArrayRef::mk_default(), Solver::model(), Optimize::model(), SortRef::name(), FuncDeclRef::name(), QuantifierRef::no_pattern(), RatNumRef::numerator(), Solver::param_descrs(), Fixedpoint::param_descrs(), Optimize::param_descrs(), Tactic::param_descrs(), Fixedpoint::parse_file(), Fixedpoint::parse_string(), QuantifierRef::pattern(), Solver::proof(), Fixedpoint::query(), FuncDeclRef::range(), ArraySortRef::range(), DatatypeSortRef::recognizer(), Solver::set(), Fixedpoint::set(), Optimize::set(), Tactic::solver(), ExprRef::sort(), BoolRef::sort(), QuantifierRef::sort(), ArithRef::sort(), BitVecRef::sort(), ArrayRef::sort(), DatatypeRef::sort(), Solver::statistics(), Fixedpoint::statistics(), Optimize::statistics(), Solver::to_smt2(), Solver::unsat_core(), Fixedpoint::update_rule(), QuantifierRef::var_name(), and QuantifierRef::var_sort().

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines