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

Public Member Functions

def __init__
 
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 4559 of file z3py.py.

Constructor & Destructor Documentation

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

Definition at line 4567 of file z3py.py.

4567  def __init__(self, models=True, unsat_cores=False, proofs=False, ctx=None, goal=None):
4568  if __debug__:
4569  _z3_assert(goal == None or ctx != None, "If goal is different from None, then ctx must be also different from None")
4570  self.ctx = _get_ctx(ctx)
4571  self.goal = goal
4572  if self.goal == None:
4573  self.goal = Z3_mk_goal(self.ctx.ref(), models, unsat_cores, proofs)
4574  Z3_goal_inc_ref(self.ctx.ref(), self.goal)
4575 
void Z3_API Z3_goal_inc_ref(__in Z3_context c, __in Z3_goal g)
Increment the reference counter of the given goal.
def __init__
Definition: z3py.py:4567
Z3_goal Z3_API Z3_mk_goal(__in Z3_context c, __in Z3_bool models, __in Z3_bool unsat_cores, __in Z3_bool proofs)
Create a goal (aka problem). A goal is essentially a set of formulas, that can be solved and/or trans...
def __del__ (   self)

Definition at line 4576 of file z3py.py.

4576  def __del__(self):
4577  if self.goal != None:
4578  Z3_goal_dec_ref(self.ctx.ref(), self.goal)
4579 
void Z3_API Z3_goal_dec_ref(__in Z3_context c, __in Z3_goal g)
Decrement the reference counter of the given goal.
def __del__(self)
Definition: z3py.py:4576

Member Function Documentation

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

4684  def __getitem__(self, arg):
4685  """Return a constraint in the goal `self`.
4686 
4687  >>> g = Goal()
4688  >>> x, y = Ints('x y')
4689  >>> g.add(x == 0, y > x)
4690  >>> g[0]
4691  x == 0
4692  >>> g[1]
4693  y > x
4694  """
4695  if arg >= len(self):
4696  raise IndexError
4697  return self.get(arg)
4698 
def get(self, i)
Definition: z3py.py:4671
def __getitem__(self, arg)
Definition: z3py.py:4684
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 4658 of file z3py.py.

Referenced by AstVector.__getitem__(), and AstVector.__setitem__().

4658  def __len__(self):
4659  """Return the number of constraints in the goal `self`.
4660 
4661  >>> g = Goal()
4662  >>> len(g)
4663  0
4664  >>> x, y = Ints('x y')
4665  >>> g.add(x == 0, y > x)
4666  >>> len(g)
4667  2
4668  """
4669  return self.size()
4670 
def size(self)
Definition: z3py.py:4645
def __len__(self)
Definition: z3py.py:4658
def __repr__ (   self)

Definition at line 4747 of file z3py.py.

4747  def __repr__(self):
4748  return obj_to_string(self)
4749 
def __repr__(self)
Definition: z3py.py:4747
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 4736 of file z3py.py.

4736  def add(self, *args):
4737  """Add constraints.
4738 
4739  >>> x = Int('x')
4740  >>> g = Goal()
4741  >>> g.add(x > 0, x < 2)
4742  >>> g
4743  [x > 0, x < 2]
4744  """
4745  self.assert_exprs(*args)
4746 
def add(self, args)
Definition: z3py.py:4736
def assert_exprs(self, args)
Definition: z3py.py:4699
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 4714 of file z3py.py.

4714  def append(self, *args):
4715  """Add constraints.
4716 
4717  >>> x = Int('x')
4718  >>> g = Goal()
4719  >>> g.append(x > 0, x < 2)
4720  >>> g
4721  [x > 0, x < 2]
4722  """
4723  self.assert_exprs(*args)
4724 
def append(self, args)
Definition: z3py.py:4714
def assert_exprs(self, args)
Definition: z3py.py:4699
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 4797 of file z3py.py.

4797  def as_expr(self):
4798  """Return goal `self` as a single Z3 expression.
4799 
4800  >>> x = Int('x')
4801  >>> g = Goal()
4802  >>> g.as_expr()
4803  True
4804  >>> g.add(x > 1)
4805  >>> g.as_expr()
4806  x > 1
4807  >>> g.add(x < 10)
4808  >>> g.as_expr()
4809  And(x > 1, x < 10)
4810  """
4811  sz = len(self)
4812  if sz == 0:
4813  return BoolVal(True, self.ctx)
4814  elif sz == 1:
4815  return self.get(0)
4816  else:
4817  return And([ self.get(i) for i in range(len(self)) ])
4818 
def BoolVal
Definition: z3py.py:1353
def And(args)
Definition: z3py.py:1479
def get(self, i)
Definition: z3py.py:4671
def as_expr(self)
Definition: z3py.py:4797
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 4699 of file z3py.py.

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

4699  def assert_exprs(self, *args):
4700  """Assert constraints into the goal.
4701 
4702  >>> x = Int('x')
4703  >>> g = Goal()
4704  >>> g.assert_exprs(x > 0, x < 2)
4705  >>> g
4706  [x > 0, x < 2]
4707  """
4708  args = _get_args(args)
4709  s = BoolSort(self.ctx)
4710  for arg in args:
4711  arg = s.cast(arg)
4712  Z3_goal_assert(self.ctx.ref(), self.goal, arg.as_ast())
4713 
def BoolSort
Definition: z3py.py:1336
void Z3_API Z3_goal_assert(__in Z3_context c, __in Z3_goal g, __in Z3_ast a)
Add a new formula a to the given goal.
def assert_exprs(self, args)
Definition: z3py.py:4699
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 4580 of file z3py.py.

4580  def depth(self):
4581  """Return the depth of the goal `self`. The depth corresponds to the number of tactics applied to `self`.
4582 
4583  >>> x, y = Ints('x y')
4584  >>> g = Goal()
4585  >>> g.add(x == 0, y >= x + 1)
4586  >>> g.depth()
4587  0
4588  >>> r = Then('simplify', 'solve-eqs')(g)
4589  >>> # r has 1 subgoal
4590  >>> len(r)
4591  1
4592  >>> r[0].depth()
4593  2
4594  """
4595  return int(Z3_goal_depth(self.ctx.ref(), self.goal))
4596 
def depth(self)
Definition: z3py.py:4580
unsigned Z3_API Z3_goal_depth(__in Z3_context c, __in Z3_goal g)
Return the depth of the given goal. It tracks how many transformations were applied to it...
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 4671 of file z3py.py.

Referenced by Goal.__getitem__(), and Goal.as_expr().

4671  def get(self, i):
4672  """Return a constraint in the goal `self`.
4673 
4674  >>> g = Goal()
4675  >>> x, y = Ints('x y')
4676  >>> g.add(x == 0, y > x)
4677  >>> g.get(0)
4678  x == 0
4679  >>> g.get(1)
4680  y > x
4681  """
4682  return _to_expr_ref(Z3_goal_formula(self.ctx.ref(), self.goal, i), self.ctx)
4683 
Z3_ast Z3_API Z3_goal_formula(__in Z3_context c, __in Z3_goal g, __in unsigned idx)
Return a formula from the given goal.
def get(self, i)
Definition: z3py.py:4671
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 4597 of file z3py.py.

4597  def inconsistent(self):
4598  """Return `True` if `self` contains the `False` constraints.
4599 
4600  >>> x, y = Ints('x y')
4601  >>> g = Goal()
4602  >>> g.inconsistent()
4603  False
4604  >>> g.add(x == 0, x == 1)
4605  >>> g
4606  [x == 0, x == 1]
4607  >>> g.inconsistent()
4608  False
4609  >>> g2 = Tactic('propagate-values')(g)[0]
4610  >>> g2.inconsistent()
4611  True
4612  """
4613  return Z3_goal_inconsistent(self.ctx.ref(), self.goal)
4614 
Z3_bool Z3_API Z3_goal_inconsistent(__in Z3_context c, __in Z3_goal g)
Return true if the given goal contains the formula false.
def inconsistent(self)
Definition: z3py.py:4597
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 4725 of file z3py.py.

4725  def insert(self, *args):
4726  """Add constraints.
4727 
4728  >>> x = Int('x')
4729  >>> g = Goal()
4730  >>> g.insert(x > 0, x < 2)
4731  >>> g
4732  [x > 0, x < 2]
4733  """
4734  self.assert_exprs(*args)
4735 
def insert(self, args)
Definition: z3py.py:4725
def assert_exprs(self, args)
Definition: z3py.py:4699
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 4615 of file z3py.py.

Referenced by Goal.precision().

4615  def prec(self):
4616  """Return the precision (under-approximation, over-approximation, or precise) of the goal `self`.
4617 
4618  >>> g = Goal()
4619  >>> g.prec() == Z3_GOAL_PRECISE
4620  True
4621  >>> x, y = Ints('x y')
4622  >>> g.add(x == y + 1)
4623  >>> g.prec() == Z3_GOAL_PRECISE
4624  True
4625  >>> t = With(Tactic('add-bounds'), add_bound_lower=0, add_bound_upper=10)
4626  >>> g2 = t(g)[0]
4627  >>> g2
4628  [x == y + 1, x <= 10, x >= 0, y <= 10, y >= 0]
4629  >>> g2.prec() == Z3_GOAL_PRECISE
4630  False
4631  >>> g2.prec() == Z3_GOAL_UNDER
4632  True
4633  """
4634  return Z3_goal_precision(self.ctx.ref(), self.goal)
4635 
def prec(self)
Definition: z3py.py:4615
Z3_goal_prec Z3_API Z3_goal_precision(__in Z3_context c, __in Z3_goal g)
Return the "precision" of the given goal. Goals can be transformed using over and under approximation...
def precision (   self)
Alias for `prec()`.

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

Definition at line 4636 of file z3py.py.

4636  def precision(self):
4637  """Alias for `prec()`.
4638 
4639  >>> g = Goal()
4640  >>> g.precision() == Z3_GOAL_PRECISE
4641  True
4642  """
4643  return self.prec()
4644 
def prec(self)
Definition: z3py.py:4615
def precision(self)
Definition: z3py.py:4636
def sexpr (   self)
Return a textual representation of the s-expression representing the goal.

Definition at line 4750 of file z3py.py.

Referenced by Fixedpoint.__repr__().

4750  def sexpr(self):
4751  """Return a textual representation of the s-expression representing the goal."""
4752  return Z3_goal_to_string(self.ctx.ref(), self.goal)
4753 
def sexpr(self)
Definition: z3py.py:4750
Z3_string Z3_API Z3_goal_to_string(__in Z3_context c, __in Z3_goal g)
Convert a goal into a string.
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 4777 of file z3py.py.

4777  def simplify(self, *arguments, **keywords):
4778  """Return a new simplified goal.
4779 
4780  This method is essentially invoking the simplify tactic.
4781 
4782  >>> g = Goal()
4783  >>> x = Int('x')
4784  >>> g.add(x + 1 >= 2)
4785  >>> g
4786  [x + 1 >= 2]
4787  >>> g2 = g.simplify()
4788  >>> g2
4789  [x >= 1]
4790  >>> # g was not modified
4791  >>> g
4792  [x + 1 >= 2]
4793  """
4794  t = Tactic('simplify')
4795  return t.apply(self, *arguments, **keywords)[0]
4796 
def simplify(self, arguments, keywords)
Definition: z3py.py:4777
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 4645 of file z3py.py.

Referenced by Goal.__len__().

4645  def size(self):
4646  """Return the number of constraints in the goal `self`.
4647 
4648  >>> g = Goal()
4649  >>> g.size()
4650  0
4651  >>> x, y = Ints('x y')
4652  >>> g.add(x == 0, y > x)
4653  >>> g.size()
4654  2
4655  """
4656  return int(Z3_goal_size(self.ctx.ref(), self.goal))
4657 
def size(self)
Definition: z3py.py:4645
unsigned Z3_API Z3_goal_size(__in Z3_context c, __in Z3_goal g)
Return the number of formulas in the given goal.
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 4754 of file z3py.py.

4754  def translate(self, target):
4755  """Copy goal `self` to context `target`.
4756 
4757  >>> x = Int('x')
4758  >>> g = Goal()
4759  >>> g.add(x > 10)
4760  >>> g
4761  [x > 10]
4762  >>> c2 = Context()
4763  >>> g2 = g.translate(c2)
4764  >>> g2
4765  [x > 10]
4766  >>> g.ctx == main_ctx()
4767  True
4768  >>> g2.ctx == c2
4769  True
4770  >>> g2.ctx == main_ctx()
4771  False
4772  """
4773  if __debug__:
4774  _z3_assert(isinstance(target, Context), "target must be a context")
4775  return Goal(goal=Z3_goal_translate(self.ctx.ref(), self.goal, target.ref()), ctx=target)
4776 
def translate(self, target)
Definition: z3py.py:4754
Z3_goal Z3_API Z3_goal_translate(__in Z3_context source, __in Z3_goal g, __in Z3_context target)
Copy a goal g from the context source to a the context target.

Field Documentation

ctx
goal