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 __copy__ (self)
 
def __deepcopy__ (self)
 
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 4844 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 4852 of file z3py.py.

4852  def __init__(self, models=True, unsat_cores=False, proofs=False, ctx=None, goal=None):
4853  if __debug__:
4854  _z3_assert(goal is None or ctx is not None, "If goal is different from None, then ctx must be also different from None")
4855  self.ctx = _get_ctx(ctx)
4856  self.goal = goal
4857  if self.goal is None:
4858  self.goal = Z3_mk_goal(self.ctx.ref(), models, unsat_cores, proofs)
4859  Z3_goal_inc_ref(self.ctx.ref(), self.goal)
4860 
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 4864 of file z3py.py.

4864  def __del__(self):
4865  if self.goal is not None and self.ctx.ref() is not None:
4866  Z3_goal_dec_ref(self.ctx.ref(), self.goal)
4867 
void Z3_API Z3_goal_dec_ref(Z3_context c, Z3_goal g)
Decrement the reference counter of the given goal.

Member Function Documentation

◆ __copy__()

def __copy__ (   self)

Definition at line 5065 of file z3py.py.

5065  def __copy__(self):
5066  return self.translate(self.ctx)
5067 

◆ __deepcopy__() [1/2]

def __deepcopy__ (   self,
  memo = {} 
)

Definition at line 4861 of file z3py.py.

4861  def __deepcopy__(self, memo={}):
4862  return Goal(False, False, False, self.ctx, self.goal)
4863 

◆ __deepcopy__() [2/2]

def __deepcopy__ (   self)

Definition at line 5068 of file z3py.py.

5068  def __deepcopy__(self):
5069  return self.translate(self.ctx)
5070 

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

4972  def __getitem__(self, arg):
4973  """Return a constraint in the goal `self`.
4974 
4975  >>> g = Goal()
4976  >>> x, y = Ints('x y')
4977  >>> g.add(x == 0, y > x)
4978  >>> g[0]
4979  x == 0
4980  >>> g[1]
4981  y > x
4982  """
4983  if arg >= len(self):
4984  raise IndexError
4985  return self.get(arg)
4986 

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

4946  def __len__(self):
4947  """Return the number of constraints in the goal `self`.
4948 
4949  >>> g = Goal()
4950  >>> len(g)
4951  0
4952  >>> x, y = Ints('x y')
4953  >>> g.add(x == 0, y > x)
4954  >>> len(g)
4955  2
4956  """
4957  return self.size()
4958 

◆ __repr__()

def __repr__ (   self)

Definition at line 5035 of file z3py.py.

5035  def __repr__(self):
5036  return obj_to_string(self)
5037 

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

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

5024  def add(self, *args):
5025  """Add constraints.
5026 
5027  >>> x = Int('x')
5028  >>> g = Goal()
5029  >>> g.add(x > 0, x < 2)
5030  >>> g
5031  [x > 0, x < 2]
5032  """
5033  self.assert_exprs(*args)
5034 

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

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

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

5091  def as_expr(self):
5092  """Return goal `self` as a single Z3 expression.
5093 
5094  >>> x = Int('x')
5095  >>> g = Goal()
5096  >>> g.as_expr()
5097  True
5098  >>> g.add(x > 1)
5099  >>> g.as_expr()
5100  x > 1
5101  >>> g.add(x < 10)
5102  >>> g.as_expr()
5103  And(x > 1, x < 10)
5104  """
5105  sz = len(self)
5106  if sz == 0:
5107  return BoolVal(True, self.ctx)
5108  elif sz == 1:
5109  return self.get(0)
5110  else:
5111  return And([ self.get(i) for i in range(len(self)) ], self.ctx)
5112 
expr range(expr const &lo, expr const &hi)
Definition: z3++.h:2868
def And(args)
Definition: z3py.py:1592
def BoolVal(val, ctx=None)
Definition: z3py.py:1466

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

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

4987  def assert_exprs(self, *args):
4988  """Assert constraints into the goal.
4989 
4990  >>> x = Int('x')
4991  >>> g = Goal()
4992  >>> g.assert_exprs(x > 0, x < 2)
4993  >>> g
4994  [x > 0, x < 2]
4995  """
4996  args = _get_args(args)
4997  s = BoolSort(self.ctx)
4998  for arg in args:
4999  arg = s.cast(arg)
5000  Z3_goal_assert(self.ctx.ref(), self.goal, arg.as_ast())
5001 
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:1449

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

4868  def depth(self):
4869  """Return the depth of the goal `self`. The depth corresponds to the number of tactics applied to `self`.
4870 
4871  >>> x, y = Ints('x y')
4872  >>> g = Goal()
4873  >>> g.add(x == 0, y >= x + 1)
4874  >>> g.depth()
4875  0
4876  >>> r = Then('simplify', 'solve-eqs')(g)
4877  >>> # r has 1 subgoal
4878  >>> len(r)
4879  1
4880  >>> r[0].depth()
4881  2
4882  """
4883  return int(Z3_goal_depth(self.ctx.ref(), self.goal))
4884 
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 4959 of file z3py.py.

4959  def get(self, i):
4960  """Return a constraint in the goal `self`.
4961 
4962  >>> g = Goal()
4963  >>> x, y = Ints('x y')
4964  >>> g.add(x == 0, y > x)
4965  >>> g.get(0)
4966  x == 0
4967  >>> g.get(1)
4968  y > x
4969  """
4970  return _to_expr_ref(Z3_goal_formula(self.ctx.ref(), self.goal, i), self.ctx)
4971 
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 4885 of file z3py.py.

4885  def inconsistent(self):
4886  """Return `True` if `self` contains the `False` constraints.
4887 
4888  >>> x, y = Ints('x y')
4889  >>> g = Goal()
4890  >>> g.inconsistent()
4891  False
4892  >>> g.add(x == 0, x == 1)
4893  >>> g
4894  [x == 0, x == 1]
4895  >>> g.inconsistent()
4896  False
4897  >>> g2 = Tactic('propagate-values')(g)[0]
4898  >>> g2.inconsistent()
4899  True
4900  """
4901  return Z3_goal_inconsistent(self.ctx.ref(), self.goal)
4902 
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 5013 of file z3py.py.

5013  def insert(self, *args):
5014  """Add constraints.
5015 
5016  >>> x = Int('x')
5017  >>> g = Goal()
5018  >>> g.insert(x > 0, x < 2)
5019  >>> g
5020  [x > 0, x < 2]
5021  """
5022  self.assert_exprs(*args)
5023 

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

Referenced by Goal.precision().

4903  def prec(self):
4904  """Return the precision (under-approximation, over-approximation, or precise) of the goal `self`.
4905 
4906  >>> g = Goal()
4907  >>> g.prec() == Z3_GOAL_PRECISE
4908  True
4909  >>> x, y = Ints('x y')
4910  >>> g.add(x == y + 1)
4911  >>> g.prec() == Z3_GOAL_PRECISE
4912  True
4913  >>> t = With(Tactic('add-bounds'), add_bound_lower=0, add_bound_upper=10)
4914  >>> g2 = t(g)[0]
4915  >>> g2
4916  [x == y + 1, x <= 10, x >= 0, y <= 10, y >= 0]
4917  >>> g2.prec() == Z3_GOAL_PRECISE
4918  False
4919  >>> g2.prec() == Z3_GOAL_UNDER
4920  True
4921  """
4922  return Z3_goal_precision(self.ctx.ref(), self.goal)
4923 
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 4924 of file z3py.py.

4924  def precision(self):
4925  """Alias for `prec()`.
4926 
4927  >>> g = Goal()
4928  >>> g.precision() == Z3_GOAL_PRECISE
4929  True
4930  """
4931  return self.prec()
4932 

◆ sexpr()

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

Definition at line 5038 of file z3py.py.

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

5038  def sexpr(self):
5039  """Return a textual representation of the s-expression representing the goal."""
5040  return Z3_goal_to_string(self.ctx.ref(), self.goal)
5041 
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 5071 of file z3py.py.

5071  def simplify(self, *arguments, **keywords):
5072  """Return a new simplified goal.
5073 
5074  This method is essentially invoking the simplify tactic.
5075 
5076  >>> g = Goal()
5077  >>> x = Int('x')
5078  >>> g.add(x + 1 >= 2)
5079  >>> g
5080  [x + 1 >= 2]
5081  >>> g2 = g.simplify()
5082  >>> g2
5083  [x >= 1]
5084  >>> # g was not modified
5085  >>> g
5086  [x + 1 >= 2]
5087  """
5088  t = Tactic('simplify')
5089  return t.apply(self, *arguments, **keywords)[0]
5090 
def simplify(a, arguments, keywords)
Utils.
Definition: z3py.py:7793

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

4933  def size(self):
4934  """Return the number of constraints in the goal `self`.
4935 
4936  >>> g = Goal()
4937  >>> g.size()
4938  0
4939  >>> x, y = Ints('x y')
4940  >>> g.add(x == 0, y > x)
4941  >>> g.size()
4942  2
4943  """
4944  return int(Z3_goal_size(self.ctx.ref(), self.goal))
4945 
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 5042 of file z3py.py.

5042  def translate(self, target):
5043  """Copy goal `self` to context `target`.
5044 
5045  >>> x = Int('x')
5046  >>> g = Goal()
5047  >>> g.add(x > 10)
5048  >>> g
5049  [x > 10]
5050  >>> c2 = Context()
5051  >>> g2 = g.translate(c2)
5052  >>> g2
5053  [x > 10]
5054  >>> g.ctx == main_ctx()
5055  True
5056  >>> g2.ctx == c2
5057  True
5058  >>> g2.ctx == main_ctx()
5059  False
5060  """
5061  if __debug__:
5062  _z3_assert(isinstance(target, Context), "target must be a context")
5063  return Goal(goal=Z3_goal_translate(self.ctx.ref(), self.goal, target.ref()), ctx=target)
5064 
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 the context target.

Field Documentation

◆ ctx

ctx

Definition at line 4855 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 4856 of file z3py.py.