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

Constructor & Destructor Documentation

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

Definition at line 4544 of file z3py.py.

4544  def __init__(self, models=True, unsat_cores=False, proofs=False, ctx=None, goal=None):
4545  if __debug__:
4546  _z3_assert(goal == None or ctx != None, "If goal is different from None, then ctx must be also different from None")
4547  self.ctx = _get_ctx(ctx)
4548  self.goal = goal
4549  if self.goal == None:
4550  self.goal = Z3_mk_goal(self.ctx.ref(), models, unsat_cores, proofs)
4551  Z3_goal_inc_ref(self.ctx.ref(), self.goal)
4552 
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:4544
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 4553 of file z3py.py.

4553  def __del__(self):
4554  if self.goal != None:
4555  Z3_goal_dec_ref(self.ctx.ref(), self.goal)
4556 
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:4553

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

4661  def __getitem__(self, arg):
4662  """Return a constraint in the goal `self`.
4663 
4664  >>> g = Goal()
4665  >>> x, y = Ints('x y')
4666  >>> g.add(x == 0, y > x)
4667  >>> g[0]
4668  x == 0
4669  >>> g[1]
4670  y > x
4671  """
4672  if arg >= len(self):
4673  raise IndexError
4674  return self.get(arg)
4675 
def get(self, i)
Definition: z3py.py:4648
def __getitem__(self, arg)
Definition: z3py.py:4661
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 4635 of file z3py.py.

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

4635  def __len__(self):
4636  """Return the number of constraints in the goal `self`.
4637 
4638  >>> g = Goal()
4639  >>> len(g)
4640  0
4641  >>> x, y = Ints('x y')
4642  >>> g.add(x == 0, y > x)
4643  >>> len(g)
4644  2
4645  """
4646  return self.size()
4647 
def size(self)
Definition: z3py.py:4622
def __len__(self)
Definition: z3py.py:4635
def __repr__ (   self)

Definition at line 4724 of file z3py.py.

4724  def __repr__(self):
4725  return obj_to_string(self)
4726 
def __repr__(self)
Definition: z3py.py:4724
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 4713 of file z3py.py.

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

4691  def append(self, *args):
4692  """Add constraints.
4693 
4694  >>> x = Int('x')
4695  >>> g = Goal()
4696  >>> g.append(x > 0, x < 2)
4697  >>> g
4698  [x > 0, x < 2]
4699  """
4700  self.assert_exprs(*args)
4701 
def append(self, args)
Definition: z3py.py:4691
def assert_exprs(self, args)
Definition: z3py.py:4676
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 4774 of file z3py.py.

4774  def as_expr(self):
4775  """Return goal `self` as a single Z3 expression.
4776 
4777  >>> x = Int('x')
4778  >>> g = Goal()
4779  >>> g.as_expr()
4780  True
4781  >>> g.add(x > 1)
4782  >>> g.as_expr()
4783  x > 1
4784  >>> g.add(x < 10)
4785  >>> g.as_expr()
4786  And(x > 1, x < 10)
4787  """
4788  sz = len(self)
4789  if sz == 0:
4790  return BoolVal(True, self.ctx)
4791  elif sz == 1:
4792  return self.get(0)
4793  else:
4794  return And([ self.get(i) for i in range(len(self)) ])
4795 
def BoolVal
Definition: z3py.py:1342
def And(args)
Definition: z3py.py:1468
def get(self, i)
Definition: z3py.py:4648
def as_expr(self)
Definition: z3py.py:4774
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 4676 of file z3py.py.

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

4676  def assert_exprs(self, *args):
4677  """Assert constraints into the goal.
4678 
4679  >>> x = Int('x')
4680  >>> g = Goal()
4681  >>> g.assert_exprs(x > 0, x < 2)
4682  >>> g
4683  [x > 0, x < 2]
4684  """
4685  args = _get_args(args)
4686  s = BoolSort(self.ctx)
4687  for arg in args:
4688  arg = s.cast(arg)
4689  Z3_goal_assert(self.ctx.ref(), self.goal, arg.as_ast())
4690 
def BoolSort
Definition: z3py.py:1325
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:4676
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 4557 of file z3py.py.

4557  def depth(self):
4558  """Return the depth of the goal `self`. The depth corresponds to the number of tactics applied to `self`.
4559 
4560  >>> x, y = Ints('x y')
4561  >>> g = Goal()
4562  >>> g.add(x == 0, y >= x + 1)
4563  >>> g.depth()
4564  0
4565  >>> r = Then('simplify', 'solve-eqs')(g)
4566  >>> # r has 1 subgoal
4567  >>> len(r)
4568  1
4569  >>> r[0].depth()
4570  2
4571  """
4572  return int(Z3_goal_depth(self.ctx.ref(), self.goal))
4573 
def depth(self)
Definition: z3py.py:4557
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 4648 of file z3py.py.

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

4648  def get(self, i):
4649  """Return a constraint in the goal `self`.
4650 
4651  >>> g = Goal()
4652  >>> x, y = Ints('x y')
4653  >>> g.add(x == 0, y > x)
4654  >>> g.get(0)
4655  x == 0
4656  >>> g.get(1)
4657  y > x
4658  """
4659  return _to_expr_ref(Z3_goal_formula(self.ctx.ref(), self.goal, i), self.ctx)
4660 
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:4648
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 4574 of file z3py.py.

4574  def inconsistent(self):
4575  """Return `True` if `self` contains the `False` constraints.
4576 
4577  >>> x, y = Ints('x y')
4578  >>> g = Goal()
4579  >>> g.inconsistent()
4580  False
4581  >>> g.add(x == 0, x == 1)
4582  >>> g
4583  [x == 0, x == 1]
4584  >>> g.inconsistent()
4585  False
4586  >>> g2 = Tactic('propagate-values')(g)[0]
4587  >>> g2.inconsistent()
4588  True
4589  """
4590  return Z3_goal_inconsistent(self.ctx.ref(), self.goal)
4591 
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:4574
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 4702 of file z3py.py.

4702  def insert(self, *args):
4703  """Add constraints.
4704 
4705  >>> x = Int('x')
4706  >>> g = Goal()
4707  >>> g.insert(x > 0, x < 2)
4708  >>> g
4709  [x > 0, x < 2]
4710  """
4711  self.assert_exprs(*args)
4712 
def insert(self, args)
Definition: z3py.py:4702
def assert_exprs(self, args)
Definition: z3py.py:4676
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 4592 of file z3py.py.

Referenced by Goal.precision().

4592  def prec(self):
4593  """Return the precision (under-approximation, over-approximation, or precise) of the goal `self`.
4594 
4595  >>> g = Goal()
4596  >>> g.prec() == Z3_GOAL_PRECISE
4597  True
4598  >>> x, y = Ints('x y')
4599  >>> g.add(x == y + 1)
4600  >>> g.prec() == Z3_GOAL_PRECISE
4601  True
4602  >>> t = With(Tactic('add-bounds'), add_bound_lower=0, add_bound_upper=10)
4603  >>> g2 = t(g)[0]
4604  >>> g2
4605  [x == y + 1, x <= 10, x >= 0, y <= 10, y >= 0]
4606  >>> g2.prec() == Z3_GOAL_PRECISE
4607  False
4608  >>> g2.prec() == Z3_GOAL_UNDER
4609  True
4610  """
4611  return Z3_goal_precision(self.ctx.ref(), self.goal)
4612 
def prec(self)
Definition: z3py.py:4592
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 4613 of file z3py.py.

4613  def precision(self):
4614  """Alias for `prec()`.
4615 
4616  >>> g = Goal()
4617  >>> g.precision() == Z3_GOAL_PRECISE
4618  True
4619  """
4620  return self.prec()
4621 
def prec(self)
Definition: z3py.py:4592
def precision(self)
Definition: z3py.py:4613
def sexpr (   self)
Return a textual representation of the s-expression representing the goal.

Definition at line 4727 of file z3py.py.

Referenced by Fixedpoint.__repr__().

4727  def sexpr(self):
4728  """Return a textual representation of the s-expression representing the goal."""
4729  return Z3_goal_to_string(self.ctx.ref(), self.goal)
4730 
def sexpr(self)
Definition: z3py.py:4727
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 4754 of file z3py.py.

4754  def simplify(self, *arguments, **keywords):
4755  """Return a new simplified goal.
4756 
4757  This method is essentially invoking the simplify tactic.
4758 
4759  >>> g = Goal()
4760  >>> x = Int('x')
4761  >>> g.add(x + 1 >= 2)
4762  >>> g
4763  [x + 1 >= 2]
4764  >>> g2 = g.simplify()
4765  >>> g2
4766  [x >= 1]
4767  >>> # g was not modified
4768  >>> g
4769  [x + 1 >= 2]
4770  """
4771  t = Tactic('simplify')
4772  return t.apply(self, *arguments, **keywords)[0]
4773 
def simplify(self, arguments, keywords)
Definition: z3py.py:4754
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 4622 of file z3py.py.

Referenced by Goal.__len__().

4622  def size(self):
4623  """Return the number of constraints in the goal `self`.
4624 
4625  >>> g = Goal()
4626  >>> g.size()
4627  0
4628  >>> x, y = Ints('x y')
4629  >>> g.add(x == 0, y > x)
4630  >>> g.size()
4631  2
4632  """
4633  return int(Z3_goal_size(self.ctx.ref(), self.goal))
4634 
def size(self)
Definition: z3py.py:4622
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 4731 of file z3py.py.

4731  def translate(self, target):
4732  """Copy goal `self` to context `target`.
4733 
4734  >>> x = Int('x')
4735  >>> g = Goal()
4736  >>> g.add(x > 10)
4737  >>> g
4738  [x > 10]
4739  >>> c2 = Context()
4740  >>> g2 = g.translate(c2)
4741  >>> g2
4742  [x > 10]
4743  >>> g.ctx == main_ctx()
4744  True
4745  >>> g2.ctx == c2
4746  True
4747  >>> g2.ctx == main_ctx()
4748  False
4749  """
4750  if __debug__:
4751  _z3_assert(isinstance(target, Context), "target must be a context")
4752  return Goal(goal=Z3_goal_translate(self.ctx.ref(), self.goal, target.ref()), ctx=target)
4753 
def translate(self, target)
Definition: z3py.py:4731
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