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 __deepcopy__ (self, memo={})
 
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 4822 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 4830 of file z3py.py.

4830  def __init__(self, models=True, unsat_cores=False, proofs=False, ctx=None, goal=None):
4831  if __debug__:
4832  _z3_assert(goal is None or ctx is not None, "If goal is different from None, then ctx must be also different from None")
4833  self.ctx = _get_ctx(ctx)
4834  self.goal = goal
4835  if self.goal is None:
4836  self.goal = Z3_mk_goal(self.ctx.ref(), models, unsat_cores, proofs)
4837  Z3_goal_inc_ref(self.ctx.ref(), self.goal)
4838 
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 4842 of file z3py.py.

4842  def __del__(self):
4843  if self.goal is not None and self.ctx.ref() is not None:
4844  Z3_goal_dec_ref(self.ctx.ref(), self.goal)
4845 
void Z3_API Z3_goal_dec_ref(Z3_context c, Z3_goal g)
Decrement the reference counter of the given goal.

Member Function Documentation

◆ __deepcopy__()

def __deepcopy__ (   self,
  memo = {} 
)

Definition at line 4839 of file z3py.py.

4839  def __deepcopy__(self, memo={}):
4840  return Goal(False, False, False, self.ctx, self.goal)
4841 

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

4950  def __getitem__(self, arg):
4951  """Return a constraint in the goal `self`.
4952 
4953  >>> g = Goal()
4954  >>> x, y = Ints('x y')
4955  >>> g.add(x == 0, y > x)
4956  >>> g[0]
4957  x == 0
4958  >>> g[1]
4959  y > x
4960  """
4961  if arg >= len(self):
4962  raise IndexError
4963  return self.get(arg)
4964 

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

4924  def __len__(self):
4925  """Return the number of constraints in the goal `self`.
4926 
4927  >>> g = Goal()
4928  >>> len(g)
4929  0
4930  >>> x, y = Ints('x y')
4931  >>> g.add(x == 0, y > x)
4932  >>> len(g)
4933  2
4934  """
4935  return self.size()
4936 

◆ __repr__()

def __repr__ (   self)

Definition at line 5013 of file z3py.py.

5013  def __repr__(self):
5014  return obj_to_string(self)
5015 

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

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

5002  def add(self, *args):
5003  """Add constraints.
5004 
5005  >>> x = Int('x')
5006  >>> g = Goal()
5007  >>> g.add(x > 0, x < 2)
5008  >>> g
5009  [x > 0, x < 2]
5010  """
5011  self.assert_exprs(*args)
5012 

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

4980  def append(self, *args):
4981  """Add constraints.
4982 
4983  >>> x = Int('x')
4984  >>> g = Goal()
4985  >>> g.append(x > 0, x < 2)
4986  >>> g
4987  [x > 0, x < 2]
4988  """
4989  self.assert_exprs(*args)
4990 

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

5063  def as_expr(self):
5064  """Return goal `self` as a single Z3 expression.
5065 
5066  >>> x = Int('x')
5067  >>> g = Goal()
5068  >>> g.as_expr()
5069  True
5070  >>> g.add(x > 1)
5071  >>> g.as_expr()
5072  x > 1
5073  >>> g.add(x < 10)
5074  >>> g.as_expr()
5075  And(x > 1, x < 10)
5076  """
5077  sz = len(self)
5078  if sz == 0:
5079  return BoolVal(True, self.ctx)
5080  elif sz == 1:
5081  return self.get(0)
5082  else:
5083  return And([ self.get(i) for i in range(len(self)) ], self.ctx)
5084 
expr range(expr const &lo, expr const &hi)
Definition: z3++.h:2813
def And(args)
Definition: z3py.py:1578
def BoolVal(val, ctx=None)
Definition: z3py.py:1452

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

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

4965  def assert_exprs(self, *args):
4966  """Assert constraints into the goal.
4967 
4968  >>> x = Int('x')
4969  >>> g = Goal()
4970  >>> g.assert_exprs(x > 0, x < 2)
4971  >>> g
4972  [x > 0, x < 2]
4973  """
4974  args = _get_args(args)
4975  s = BoolSort(self.ctx)
4976  for arg in args:
4977  arg = s.cast(arg)
4978  Z3_goal_assert(self.ctx.ref(), self.goal, arg.as_ast())
4979 
void Z3_API Z3_goal_assert(Z3_context c, Z3_goal g, Z3_ast a)
Add a new formula a to the given goal. The formula is split according to the following procedure that...
def BoolSort(ctx=None)
Definition: z3py.py:1435

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

4846  def depth(self):
4847  """Return the depth of the goal `self`. The depth corresponds to the number of tactics applied to `self`.
4848 
4849  >>> x, y = Ints('x y')
4850  >>> g = Goal()
4851  >>> g.add(x == 0, y >= x + 1)
4852  >>> g.depth()
4853  0
4854  >>> r = Then('simplify', 'solve-eqs')(g)
4855  >>> # r has 1 subgoal
4856  >>> len(r)
4857  1
4858  >>> r[0].depth()
4859  2
4860  """
4861  return int(Z3_goal_depth(self.ctx.ref(), self.goal))
4862 
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 4937 of file z3py.py.

4937  def get(self, i):
4938  """Return a constraint in the goal `self`.
4939 
4940  >>> g = Goal()
4941  >>> x, y = Ints('x y')
4942  >>> g.add(x == 0, y > x)
4943  >>> g.get(0)
4944  x == 0
4945  >>> g.get(1)
4946  y > x
4947  """
4948  return _to_expr_ref(Z3_goal_formula(self.ctx.ref(), self.goal, i), self.ctx)
4949 
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 4863 of file z3py.py.

4863  def inconsistent(self):
4864  """Return `True` if `self` contains the `False` constraints.
4865 
4866  >>> x, y = Ints('x y')
4867  >>> g = Goal()
4868  >>> g.inconsistent()
4869  False
4870  >>> g.add(x == 0, x == 1)
4871  >>> g
4872  [x == 0, x == 1]
4873  >>> g.inconsistent()
4874  False
4875  >>> g2 = Tactic('propagate-values')(g)[0]
4876  >>> g2.inconsistent()
4877  True
4878  """
4879  return Z3_goal_inconsistent(self.ctx.ref(), self.goal)
4880 
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 4991 of file z3py.py.

4991  def insert(self, *args):
4992  """Add constraints.
4993 
4994  >>> x = Int('x')
4995  >>> g = Goal()
4996  >>> g.insert(x > 0, x < 2)
4997  >>> g
4998  [x > 0, x < 2]
4999  """
5000  self.assert_exprs(*args)
5001 

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

Referenced by Goal.precision().

4881  def prec(self):
4882  """Return the precision (under-approximation, over-approximation, or precise) of the goal `self`.
4883 
4884  >>> g = Goal()
4885  >>> g.prec() == Z3_GOAL_PRECISE
4886  True
4887  >>> x, y = Ints('x y')
4888  >>> g.add(x == y + 1)
4889  >>> g.prec() == Z3_GOAL_PRECISE
4890  True
4891  >>> t = With(Tactic('add-bounds'), add_bound_lower=0, add_bound_upper=10)
4892  >>> g2 = t(g)[0]
4893  >>> g2
4894  [x == y + 1, x <= 10, x >= 0, y <= 10, y >= 0]
4895  >>> g2.prec() == Z3_GOAL_PRECISE
4896  False
4897  >>> g2.prec() == Z3_GOAL_UNDER
4898  True
4899  """
4900  return Z3_goal_precision(self.ctx.ref(), self.goal)
4901 
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 4902 of file z3py.py.

4902  def precision(self):
4903  """Alias for `prec()`.
4904 
4905  >>> g = Goal()
4906  >>> g.precision() == Z3_GOAL_PRECISE
4907  True
4908  """
4909  return self.prec()
4910 

◆ sexpr()

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

Definition at line 5016 of file z3py.py.

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

5016  def sexpr(self):
5017  """Return a textual representation of the s-expression representing the goal."""
5018  return Z3_goal_to_string(self.ctx.ref(), self.goal)
5019 
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 5043 of file z3py.py.

5043  def simplify(self, *arguments, **keywords):
5044  """Return a new simplified goal.
5045 
5046  This method is essentially invoking the simplify tactic.
5047 
5048  >>> g = Goal()
5049  >>> x = Int('x')
5050  >>> g.add(x + 1 >= 2)
5051  >>> g
5052  [x + 1 >= 2]
5053  >>> g2 = g.simplify()
5054  >>> g2
5055  [x >= 1]
5056  >>> # g was not modified
5057  >>> g
5058  [x + 1 >= 2]
5059  """
5060  t = Tactic('simplify')
5061  return t.apply(self, *arguments, **keywords)[0]
5062 
def simplify(a, arguments, keywords)
Utils.
Definition: z3py.py:7712

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

4911  def size(self):
4912  """Return the number of constraints in the goal `self`.
4913 
4914  >>> g = Goal()
4915  >>> g.size()
4916  0
4917  >>> x, y = Ints('x y')
4918  >>> g.add(x == 0, y > x)
4919  >>> g.size()
4920  2
4921  """
4922  return int(Z3_goal_size(self.ctx.ref(), self.goal))
4923 
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 5020 of file z3py.py.

5020  def translate(self, target):
5021  """Copy goal `self` to context `target`.
5022 
5023  >>> x = Int('x')
5024  >>> g = Goal()
5025  >>> g.add(x > 10)
5026  >>> g
5027  [x > 10]
5028  >>> c2 = Context()
5029  >>> g2 = g.translate(c2)
5030  >>> g2
5031  [x > 10]
5032  >>> g.ctx == main_ctx()
5033  True
5034  >>> g2.ctx == c2
5035  True
5036  >>> g2.ctx == main_ctx()
5037  False
5038  """
5039  if __debug__:
5040  _z3_assert(isinstance(target, Context), "target must be a context")
5041  return Goal(goal=Z3_goal_translate(self.ctx.ref(), self.goal, target.ref()), ctx=target)
5042 
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

Definition at line 4833 of file z3py.py.

Referenced by Probe.__call__(), Fixedpoint.__deepcopy__(), Optimize.__deepcopy__(), ApplyResult.__deepcopy__(), Tactic.__deepcopy__(), Probe.__deepcopy__(), Fixedpoint.__del__(), Optimize.__del__(), ApplyResult.__del__(), Tactic.__del__(), Probe.__del__(), Probe.__eq__(), Probe.__ge__(), ApplyResult.__getitem__(), Probe.__gt__(), Probe.__le__(), ApplyResult.__len__(), Probe.__lt__(), Probe.__ne__(), Fixedpoint.add_cover(), Fixedpoint.add_rule(), Optimize.add_soft(), Tactic.apply(), ApplyResult.as_expr(), Fixedpoint.assert_exprs(), Optimize.assert_exprs(), Optimize.assertions(), Optimize.check(), ApplyResult.convert_model(), Optimize.from_file(), Optimize.from_string(), Fixedpoint.get_answer(), Fixedpoint.get_assertions(), Fixedpoint.get_cover_delta(), Fixedpoint.get_ground_sat_answer(), Fixedpoint.get_num_levels(), Fixedpoint.get_rule_names_along_trace(), Fixedpoint.get_rules(), Fixedpoint.get_rules_along_trace(), Fixedpoint.help(), Optimize.help(), Tactic.help(), Optimize.maximize(), Optimize.minimize(), Optimize.model(), Optimize.objectives(), Fixedpoint.param_descrs(), Optimize.param_descrs(), Tactic.param_descrs(), Fixedpoint.parse_file(), Fixedpoint.parse_string(), Fixedpoint.pop(), Optimize.pop(), Fixedpoint.push(), Optimize.push(), Fixedpoint.query(), Fixedpoint.query_from_lvl(), Fixedpoint.reason_unknown(), Optimize.reason_unknown(), Fixedpoint.register_relation(), Fixedpoint.set(), Optimize.set(), Fixedpoint.set_predicate_representation(), Fixedpoint.sexpr(), Optimize.sexpr(), ApplyResult.sexpr(), Tactic.solver(), Fixedpoint.statistics(), Optimize.statistics(), Solver.to_smt2(), Fixedpoint.to_string(), and Fixedpoint.update_rule().

◆ goal

goal

Definition at line 4834 of file z3py.py.