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

Public Member Functions

def __init__ (self, models=True, unsat_cores=False, proofs=False, ctx=None, goal=None)
 
def __del__ (self)
 
def depth (self)
 
def inconsistent (self)
 
def prec (self)
 
def precision (self)
 
def size (self)
 
def __len__ (self)
 
def get (self, i)
 
def __getitem__ (self, arg)
 
def assert_exprs (self, args)
 
def append (self, args)
 
def insert (self, args)
 
def add (self, args)
 
def __repr__ (self)
 
def sexpr (self)
 
def translate (self, target)
 
def simplify (self, arguments, keywords)
 
def as_expr (self)
 
- Public Member Functions inherited from Z3PPObject
def use_pp (self)
 

Data Fields

 ctx
 
 goal
 

Detailed Description

Goal is a collection of constraints we want to find a solution or show to be unsatisfiable (infeasible).

Goals are processed using Tactics. A Tactic transforms a goal into a set of subgoals.
A goal has a solution if one of its subgoals has a solution.
A goal is unsatisfiable if all subgoals are unsatisfiable.

Definition at line 4719 of file z3py.py.

Constructor & Destructor Documentation

§ __init__()

def __init__ (   self,
  models = True,
  unsat_cores = False,
  proofs = False,
  ctx = None,
  goal = None 
)

Definition at line 4727 of file z3py.py.

4727  def __init__(self, models=True, unsat_cores=False, proofs=False, ctx=None, goal=None):
4728  if __debug__:
4729  _z3_assert(goal is None or ctx is not None, "If goal is different from None, then ctx must be also different from None")
4730  self.ctx = _get_ctx(ctx)
4731  self.goal = goal
4732  if self.goal is None:
4733  self.goal = Z3_mk_goal(self.ctx.ref(), models, unsat_cores, proofs)
4734  Z3_goal_inc_ref(self.ctx.ref(), self.goal)
4735 
Z3_goal Z3_API Z3_mk_goal(Z3_context c, Z3_bool models, Z3_bool unsat_cores, Z3_bool proofs)
Create a goal (aka problem). A goal is essentially a set of formulas, that can be solved and/or trans...
void Z3_API Z3_goal_inc_ref(Z3_context c, Z3_goal g)
Increment the reference counter of the given goal.

§ __del__()

def __del__ (   self)

Definition at line 4736 of file z3py.py.

4736  def __del__(self):
4737  if self.goal is not None and self.ctx.ref() is not None:
4738  Z3_goal_dec_ref(self.ctx.ref(), self.goal)
4739 
void Z3_API Z3_goal_dec_ref(Z3_context c, Z3_goal g)
Decrement the reference counter of the given goal.

Member Function Documentation

§ __getitem__()

def __getitem__ (   self,
  arg 
)
Return a constraint in the goal `self`.

>>> g = Goal()
>>> x, y = Ints('x y')
>>> g.add(x == 0, y > x)
>>> g[0]
x == 0
>>> g[1]
y > x

Definition at line 4844 of file z3py.py.

4844  def __getitem__(self, arg):
4845  """Return a constraint in the goal `self`.
4846 
4847  >>> g = Goal()
4848  >>> x, y = Ints('x y')
4849  >>> g.add(x == 0, y > x)
4850  >>> g[0]
4851  x == 0
4852  >>> g[1]
4853  y > x
4854  """
4855  if arg >= len(self):
4856  raise IndexError
4857  return self.get(arg)
4858 

§ __len__()

def __len__ (   self)
Return the number of constraints in the goal `self`.

>>> g = Goal()
>>> len(g)
0
>>> x, y = Ints('x y')
>>> g.add(x == 0, y > x)
>>> len(g)
2

Definition at line 4818 of file z3py.py.

4818  def __len__(self):
4819  """Return the number of constraints in the goal `self`.
4820 
4821  >>> g = Goal()
4822  >>> len(g)
4823  0
4824  >>> x, y = Ints('x y')
4825  >>> g.add(x == 0, y > x)
4826  >>> len(g)
4827  2
4828  """
4829  return self.size()
4830 

§ __repr__()

def __repr__ (   self)

Definition at line 4907 of file z3py.py.

4907  def __repr__(self):
4908  return obj_to_string(self)
4909 

§ add()

def add (   self,
  args 
)
Add constraints.

>>> x = Int('x')
>>> g = Goal()
>>> g.add(x > 0, x < 2)
>>> g
[x > 0, x < 2]

Definition at line 4896 of file z3py.py.

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

4896  def add(self, *args):
4897  """Add constraints.
4898 
4899  >>> x = Int('x')
4900  >>> g = Goal()
4901  >>> g.add(x > 0, x < 2)
4902  >>> g
4903  [x > 0, x < 2]
4904  """
4905  self.assert_exprs(*args)
4906 

§ append()

def append (   self,
  args 
)
Add constraints.

>>> x = Int('x')
>>> g = Goal()
>>> g.append(x > 0, x < 2)
>>> g
[x > 0, x < 2]

Definition at line 4874 of file z3py.py.

4874  def append(self, *args):
4875  """Add constraints.
4876 
4877  >>> x = Int('x')
4878  >>> g = Goal()
4879  >>> g.append(x > 0, x < 2)
4880  >>> g
4881  [x > 0, x < 2]
4882  """
4883  self.assert_exprs(*args)
4884 

§ as_expr()

def as_expr (   self)
Return goal `self` as a single Z3 expression.

>>> x = Int('x')
>>> g = Goal()
>>> g.as_expr()
True
>>> g.add(x > 1)
>>> g.as_expr()
x > 1
>>> g.add(x < 10)
>>> g.as_expr()
And(x > 1, x < 10)

Definition at line 4957 of file z3py.py.

4957  def as_expr(self):
4958  """Return goal `self` as a single Z3 expression.
4959 
4960  >>> x = Int('x')
4961  >>> g = Goal()
4962  >>> g.as_expr()
4963  True
4964  >>> g.add(x > 1)
4965  >>> g.as_expr()
4966  x > 1
4967  >>> g.add(x < 10)
4968  >>> g.as_expr()
4969  And(x > 1, x < 10)
4970  """
4971  sz = len(self)
4972  if sz == 0:
4973  return BoolVal(True, self.ctx)
4974  elif sz == 1:
4975  return self.get(0)
4976  else:
4977  return And([ self.get(i) for i in range(len(self)) ], self.ctx)
4978 
def And(args)
Definition: z3py.py:1550
def BoolVal(val, ctx=None)
Definition: z3py.py:1424

§ assert_exprs()

def assert_exprs (   self,
  args 
)
Assert constraints into the goal.

>>> x = Int('x')
>>> g = Goal()
>>> g.assert_exprs(x > 0, x < 2)
>>> g
[x > 0, x < 2]

Definition at line 4859 of file z3py.py.

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

4859  def assert_exprs(self, *args):
4860  """Assert constraints into the goal.
4861 
4862  >>> x = Int('x')
4863  >>> g = Goal()
4864  >>> g.assert_exprs(x > 0, x < 2)
4865  >>> g
4866  [x > 0, x < 2]
4867  """
4868  args = _get_args(args)
4869  s = BoolSort(self.ctx)
4870  for arg in args:
4871  arg = s.cast(arg)
4872  Z3_goal_assert(self.ctx.ref(), self.goal, arg.as_ast())
4873 
void Z3_API Z3_goal_assert(Z3_context c, Z3_goal g, Z3_ast a)
Add a new formula a to the given goal.
def BoolSort(ctx=None)
Definition: z3py.py:1407

§ depth()

def depth (   self)
Return the depth of the goal `self`. The depth corresponds to the number of tactics applied to `self`.

>>> x, y = Ints('x y')
>>> g = Goal()
>>> g.add(x == 0, y >= x + 1)
>>> g.depth()
0
>>> r = Then('simplify', 'solve-eqs')(g)
>>> # r has 1 subgoal
>>> len(r)
1
>>> r[0].depth()
2

Definition at line 4740 of file z3py.py.

4740  def depth(self):
4741  """Return the depth of the goal `self`. The depth corresponds to the number of tactics applied to `self`.
4742 
4743  >>> x, y = Ints('x y')
4744  >>> g = Goal()
4745  >>> g.add(x == 0, y >= x + 1)
4746  >>> g.depth()
4747  0
4748  >>> r = Then('simplify', 'solve-eqs')(g)
4749  >>> # r has 1 subgoal
4750  >>> len(r)
4751  1
4752  >>> r[0].depth()
4753  2
4754  """
4755  return int(Z3_goal_depth(self.ctx.ref(), self.goal))
4756 
unsigned Z3_API Z3_goal_depth(Z3_context c, Z3_goal g)
Return the depth of the given goal. It tracks how many transformations were applied to it...

§ get()

def get (   self,
  i 
)
Return a constraint in the goal `self`.

>>> g = Goal()
>>> x, y = Ints('x y')
>>> g.add(x == 0, y > x)
>>> g.get(0)
x == 0
>>> g.get(1)
y > x

Definition at line 4831 of file z3py.py.

4831  def get(self, i):
4832  """Return a constraint in the goal `self`.
4833 
4834  >>> g = Goal()
4835  >>> x, y = Ints('x y')
4836  >>> g.add(x == 0, y > x)
4837  >>> g.get(0)
4838  x == 0
4839  >>> g.get(1)
4840  y > x
4841  """
4842  return _to_expr_ref(Z3_goal_formula(self.ctx.ref(), self.goal, i), self.ctx)
4843 
Z3_ast Z3_API Z3_goal_formula(Z3_context c, Z3_goal g, unsigned idx)
Return a formula from the given goal.

§ inconsistent()

def inconsistent (   self)
Return `True` if `self` contains the `False` constraints.

>>> x, y = Ints('x y')
>>> g = Goal()
>>> g.inconsistent()
False
>>> g.add(x == 0, x == 1)
>>> g
[x == 0, x == 1]
>>> g.inconsistent()
False
>>> g2 = Tactic('propagate-values')(g)[0]
>>> g2.inconsistent()
True

Definition at line 4757 of file z3py.py.

4757  def inconsistent(self):
4758  """Return `True` if `self` contains the `False` constraints.
4759 
4760  >>> x, y = Ints('x y')
4761  >>> g = Goal()
4762  >>> g.inconsistent()
4763  False
4764  >>> g.add(x == 0, x == 1)
4765  >>> g
4766  [x == 0, x == 1]
4767  >>> g.inconsistent()
4768  False
4769  >>> g2 = Tactic('propagate-values')(g)[0]
4770  >>> g2.inconsistent()
4771  True
4772  """
4773  return Z3_goal_inconsistent(self.ctx.ref(), self.goal)
4774 
Z3_bool Z3_API Z3_goal_inconsistent(Z3_context c, Z3_goal g)
Return true if the given goal contains the formula false.

§ insert()

def insert (   self,
  args 
)
Add constraints.

>>> x = Int('x')
>>> g = Goal()
>>> g.insert(x > 0, x < 2)
>>> g
[x > 0, x < 2]

Definition at line 4885 of file z3py.py.

4885  def insert(self, *args):
4886  """Add constraints.
4887 
4888  >>> x = Int('x')
4889  >>> g = Goal()
4890  >>> g.insert(x > 0, x < 2)
4891  >>> g
4892  [x > 0, x < 2]
4893  """
4894  self.assert_exprs(*args)
4895 

§ prec()

def prec (   self)
Return the precision (under-approximation, over-approximation, or precise) of the goal `self`.

>>> g = Goal()
>>> g.prec() == Z3_GOAL_PRECISE
True
>>> x, y = Ints('x y')
>>> g.add(x == y + 1)
>>> g.prec() == Z3_GOAL_PRECISE
True
>>> t  = With(Tactic('add-bounds'), add_bound_lower=0, add_bound_upper=10)
>>> g2 = t(g)[0]
>>> g2
[x == y + 1, x <= 10, x >= 0, y <= 10, y >= 0]
>>> g2.prec() == Z3_GOAL_PRECISE
False
>>> g2.prec() == Z3_GOAL_UNDER
True

Definition at line 4775 of file z3py.py.

Referenced by Goal.precision().

4775  def prec(self):
4776  """Return the precision (under-approximation, over-approximation, or precise) of the goal `self`.
4777 
4778  >>> g = Goal()
4779  >>> g.prec() == Z3_GOAL_PRECISE
4780  True
4781  >>> x, y = Ints('x y')
4782  >>> g.add(x == y + 1)
4783  >>> g.prec() == Z3_GOAL_PRECISE
4784  True
4785  >>> t = With(Tactic('add-bounds'), add_bound_lower=0, add_bound_upper=10)
4786  >>> g2 = t(g)[0]
4787  >>> g2
4788  [x == y + 1, x <= 10, x >= 0, y <= 10, y >= 0]
4789  >>> g2.prec() == Z3_GOAL_PRECISE
4790  False
4791  >>> g2.prec() == Z3_GOAL_UNDER
4792  True
4793  """
4794  return Z3_goal_precision(self.ctx.ref(), self.goal)
4795 
Z3_goal_prec Z3_API Z3_goal_precision(Z3_context c, Z3_goal g)
Return the "precision" of the given goal. Goals can be transformed using over and under approximation...

§ precision()

def precision (   self)
Alias for `prec()`.

>>> g = Goal()
>>> g.precision() == Z3_GOAL_PRECISE
True

Definition at line 4796 of file z3py.py.

4796  def precision(self):
4797  """Alias for `prec()`.
4798 
4799  >>> g = Goal()
4800  >>> g.precision() == Z3_GOAL_PRECISE
4801  True
4802  """
4803  return self.prec()
4804 

§ sexpr()

def sexpr (   self)
Return a textual representation of the s-expression representing the goal.

Definition at line 4910 of file z3py.py.

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

4910  def sexpr(self):
4911  """Return a textual representation of the s-expression representing the goal."""
4912  return Z3_goal_to_string(self.ctx.ref(), self.goal)
4913 
Z3_string Z3_API Z3_goal_to_string(Z3_context c, Z3_goal g)
Convert a goal into a string.

§ simplify()

def simplify (   self,
  arguments,
  keywords 
)
Return a new simplified goal.

This method is essentially invoking the simplify tactic.

>>> g = Goal()
>>> x = Int('x')
>>> g.add(x + 1 >= 2)
>>> g
[x + 1 >= 2]
>>> g2 = g.simplify()
>>> g2
[x >= 1]
>>> # g was not modified
>>> g
[x + 1 >= 2]

Definition at line 4937 of file z3py.py.

4937  def simplify(self, *arguments, **keywords):
4938  """Return a new simplified goal.
4939 
4940  This method is essentially invoking the simplify tactic.
4941 
4942  >>> g = Goal()
4943  >>> x = Int('x')
4944  >>> g.add(x + 1 >= 2)
4945  >>> g
4946  [x + 1 >= 2]
4947  >>> g2 = g.simplify()
4948  >>> g2
4949  [x >= 1]
4950  >>> # g was not modified
4951  >>> g
4952  [x + 1 >= 2]
4953  """
4954  t = Tactic('simplify')
4955  return t.apply(self, *arguments, **keywords)[0]
4956 
def simplify(a, arguments, keywords)
Utils.
Definition: z3py.py:7468

§ size()

def size (   self)
Return the number of constraints in the goal `self`.

>>> g = Goal()
>>> g.size()
0
>>> x, y = Ints('x y')
>>> g.add(x == 0, y > x)
>>> g.size()
2

Definition at line 4805 of file z3py.py.

4805  def size(self):
4806  """Return the number of constraints in the goal `self`.
4807 
4808  >>> g = Goal()
4809  >>> g.size()
4810  0
4811  >>> x, y = Ints('x y')
4812  >>> g.add(x == 0, y > x)
4813  >>> g.size()
4814  2
4815  """
4816  return int(Z3_goal_size(self.ctx.ref(), self.goal))
4817 
unsigned Z3_API Z3_goal_size(Z3_context c, Z3_goal g)
Return the number of formulas in the given goal.

§ translate()

def translate (   self,
  target 
)
Copy goal `self` to context `target`.

>>> x = Int('x')
>>> g = Goal()
>>> g.add(x > 10)
>>> g
[x > 10]
>>> c2 = Context()
>>> g2 = g.translate(c2)
>>> g2
[x > 10]
>>> g.ctx == main_ctx()
True
>>> g2.ctx == c2
True
>>> g2.ctx == main_ctx()
False

Definition at line 4914 of file z3py.py.

4914  def translate(self, target):
4915  """Copy goal `self` to context `target`.
4916 
4917  >>> x = Int('x')
4918  >>> g = Goal()
4919  >>> g.add(x > 10)
4920  >>> g
4921  [x > 10]
4922  >>> c2 = Context()
4923  >>> g2 = g.translate(c2)
4924  >>> g2
4925  [x > 10]
4926  >>> g.ctx == main_ctx()
4927  True
4928  >>> g2.ctx == c2
4929  True
4930  >>> g2.ctx == main_ctx()
4931  False
4932  """
4933  if __debug__:
4934  _z3_assert(isinstance(target, Context), "target must be a context")
4935  return Goal(goal=Z3_goal_translate(self.ctx.ref(), self.goal, target.ref()), ctx=target)
4936 
Z3_goal Z3_API Z3_goal_translate(Z3_context source, Z3_goal g, Z3_context target)
Copy a goal g from the context source to a the context target.

Field Documentation

§ ctx

ctx

§ goal

goal

Definition at line 4731 of file z3py.py.