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

Constructor & Destructor Documentation

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

Definition at line 4604 of file z3py.py.

4604  def __init__(self, models=True, unsat_cores=False, proofs=False, ctx=None, goal=None):
4605  if __debug__:
4606  _z3_assert(goal == None or ctx != None, "If goal is different from None, then ctx must be also different from None")
4607  self.ctx = _get_ctx(ctx)
4608  self.goal = goal
4609  if self.goal == None:
4610  self.goal = Z3_mk_goal(self.ctx.ref(), models, unsat_cores, proofs)
4611  Z3_goal_inc_ref(self.ctx.ref(), self.goal)
4612 
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.
def __init__(self, models=True, unsat_cores=False, proofs=False, ctx=None, goal=None)
Definition: z3py.py:4604
def __del__ (   self)

Definition at line 4613 of file z3py.py.

4613  def __del__(self):
4614  if self.goal != None:
4615  Z3_goal_dec_ref(self.ctx.ref(), self.goal)
4616 
def __del__(self)
Definition: z3py.py:4613
void Z3_API Z3_goal_dec_ref(Z3_context c, Z3_goal g)
Decrement the reference counter of the given goal.

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

4721  def __getitem__(self, arg):
4722  """Return a constraint in the goal `self`.
4723 
4724  >>> g = Goal()
4725  >>> x, y = Ints('x y')
4726  >>> g.add(x == 0, y > x)
4727  >>> g[0]
4728  x == 0
4729  >>> g[1]
4730  y > x
4731  """
4732  if arg >= len(self):
4733  raise IndexError
4734  return self.get(arg)
4735 
def get(self, i)
Definition: z3py.py:4708
def __getitem__(self, arg)
Definition: z3py.py:4721
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 4695 of file z3py.py.

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

4695  def __len__(self):
4696  """Return the number of constraints in the goal `self`.
4697 
4698  >>> g = Goal()
4699  >>> len(g)
4700  0
4701  >>> x, y = Ints('x y')
4702  >>> g.add(x == 0, y > x)
4703  >>> len(g)
4704  2
4705  """
4706  return self.size()
4707 
def size(self)
Definition: z3py.py:4682
def __len__(self)
Definition: z3py.py:4695
def __repr__ (   self)

Definition at line 4784 of file z3py.py.

4784  def __repr__(self):
4785  return obj_to_string(self)
4786 
def __repr__(self)
Definition: z3py.py:4784
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 4773 of file z3py.py.

4773  def add(self, *args):
4774  """Add constraints.
4775 
4776  >>> x = Int('x')
4777  >>> g = Goal()
4778  >>> g.add(x > 0, x < 2)
4779  >>> g
4780  [x > 0, x < 2]
4781  """
4782  self.assert_exprs(*args)
4783 
def add(self, args)
Definition: z3py.py:4773
def assert_exprs(self, args)
Definition: z3py.py:4736
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 4751 of file z3py.py.

4751  def append(self, *args):
4752  """Add constraints.
4753 
4754  >>> x = Int('x')
4755  >>> g = Goal()
4756  >>> g.append(x > 0, x < 2)
4757  >>> g
4758  [x > 0, x < 2]
4759  """
4760  self.assert_exprs(*args)
4761 
def append(self, args)
Definition: z3py.py:4751
def assert_exprs(self, args)
Definition: z3py.py:4736
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 4834 of file z3py.py.

4834  def as_expr(self):
4835  """Return goal `self` as a single Z3 expression.
4836 
4837  >>> x = Int('x')
4838  >>> g = Goal()
4839  >>> g.as_expr()
4840  True
4841  >>> g.add(x > 1)
4842  >>> g.as_expr()
4843  x > 1
4844  >>> g.add(x < 10)
4845  >>> g.as_expr()
4846  And(x > 1, x < 10)
4847  """
4848  sz = len(self)
4849  if sz == 0:
4850  return BoolVal(True, self.ctx)
4851  elif sz == 1:
4852  return self.get(0)
4853  else:
4854  return And([ self.get(i) for i in range(len(self)) ])
4855 
def And(args)
Definition: z3py.py:1489
def get(self, i)
Definition: z3py.py:4708
def as_expr(self)
Definition: z3py.py:4834
def BoolVal(val, ctx=None)
Definition: z3py.py:1363
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 4736 of file z3py.py.

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

4736  def assert_exprs(self, *args):
4737  """Assert constraints into the goal.
4738 
4739  >>> x = Int('x')
4740  >>> g = Goal()
4741  >>> g.assert_exprs(x > 0, x < 2)
4742  >>> g
4743  [x > 0, x < 2]
4744  """
4745  args = _get_args(args)
4746  s = BoolSort(self.ctx)
4747  for arg in args:
4748  arg = s.cast(arg)
4749  Z3_goal_assert(self.ctx.ref(), self.goal, arg.as_ast())
4750 
def assert_exprs(self, args)
Definition: z3py.py:4736
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:1346
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 4617 of file z3py.py.

4617  def depth(self):
4618  """Return the depth of the goal `self`. The depth corresponds to the number of tactics applied to `self`.
4619 
4620  >>> x, y = Ints('x y')
4621  >>> g = Goal()
4622  >>> g.add(x == 0, y >= x + 1)
4623  >>> g.depth()
4624  0
4625  >>> r = Then('simplify', 'solve-eqs')(g)
4626  >>> # r has 1 subgoal
4627  >>> len(r)
4628  1
4629  >>> r[0].depth()
4630  2
4631  """
4632  return int(Z3_goal_depth(self.ctx.ref(), self.goal))
4633 
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...
def depth(self)
Definition: z3py.py:4617
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 4708 of file z3py.py.

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

4708  def get(self, i):
4709  """Return a constraint in the goal `self`.
4710 
4711  >>> g = Goal()
4712  >>> x, y = Ints('x y')
4713  >>> g.add(x == 0, y > x)
4714  >>> g.get(0)
4715  x == 0
4716  >>> g.get(1)
4717  y > x
4718  """
4719  return _to_expr_ref(Z3_goal_formula(self.ctx.ref(), self.goal, i), self.ctx)
4720 
def get(self, i)
Definition: z3py.py:4708
Z3_ast Z3_API Z3_goal_formula(Z3_context c, Z3_goal g, unsigned idx)
Return a formula from the given goal.
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 4634 of file z3py.py.

4634  def inconsistent(self):
4635  """Return `True` if `self` contains the `False` constraints.
4636 
4637  >>> x, y = Ints('x y')
4638  >>> g = Goal()
4639  >>> g.inconsistent()
4640  False
4641  >>> g.add(x == 0, x == 1)
4642  >>> g
4643  [x == 0, x == 1]
4644  >>> g.inconsistent()
4645  False
4646  >>> g2 = Tactic('propagate-values')(g)[0]
4647  >>> g2.inconsistent()
4648  True
4649  """
4650  return Z3_goal_inconsistent(self.ctx.ref(), self.goal)
4651 
Z3_bool Z3_API Z3_goal_inconsistent(Z3_context c, Z3_goal g)
Return true if the given goal contains the formula false.
def inconsistent(self)
Definition: z3py.py:4634
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 4762 of file z3py.py.

4762  def insert(self, *args):
4763  """Add constraints.
4764 
4765  >>> x = Int('x')
4766  >>> g = Goal()
4767  >>> g.insert(x > 0, x < 2)
4768  >>> g
4769  [x > 0, x < 2]
4770  """
4771  self.assert_exprs(*args)
4772 
def insert(self, args)
Definition: z3py.py:4762
def assert_exprs(self, args)
Definition: z3py.py:4736
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 4652 of file z3py.py.

Referenced by Goal.precision().

4652  def prec(self):
4653  """Return the precision (under-approximation, over-approximation, or precise) of the goal `self`.
4654 
4655  >>> g = Goal()
4656  >>> g.prec() == Z3_GOAL_PRECISE
4657  True
4658  >>> x, y = Ints('x y')
4659  >>> g.add(x == y + 1)
4660  >>> g.prec() == Z3_GOAL_PRECISE
4661  True
4662  >>> t = With(Tactic('add-bounds'), add_bound_lower=0, add_bound_upper=10)
4663  >>> g2 = t(g)[0]
4664  >>> g2
4665  [x == y + 1, x <= 10, x >= 0, y <= 10, y >= 0]
4666  >>> g2.prec() == Z3_GOAL_PRECISE
4667  False
4668  >>> g2.prec() == Z3_GOAL_UNDER
4669  True
4670  """
4671  return Z3_goal_precision(self.ctx.ref(), self.goal)
4672 
def prec(self)
Definition: z3py.py:4652
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...
def precision (   self)
Alias for `prec()`.

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

Definition at line 4673 of file z3py.py.

4673  def precision(self):
4674  """Alias for `prec()`.
4675 
4676  >>> g = Goal()
4677  >>> g.precision() == Z3_GOAL_PRECISE
4678  True
4679  """
4680  return self.prec()
4681 
def prec(self)
Definition: z3py.py:4652
def precision(self)
Definition: z3py.py:4673
def sexpr (   self)
Return a textual representation of the s-expression representing the goal.

Definition at line 4787 of file z3py.py.

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

4787  def sexpr(self):
4788  """Return a textual representation of the s-expression representing the goal."""
4789  return Z3_goal_to_string(self.ctx.ref(), self.goal)
4790 
def sexpr(self)
Definition: z3py.py:4787
Z3_string Z3_API Z3_goal_to_string(Z3_context c, 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 4814 of file z3py.py.

4814  def simplify(self, *arguments, **keywords):
4815  """Return a new simplified goal.
4816 
4817  This method is essentially invoking the simplify tactic.
4818 
4819  >>> g = Goal()
4820  >>> x = Int('x')
4821  >>> g.add(x + 1 >= 2)
4822  >>> g
4823  [x + 1 >= 2]
4824  >>> g2 = g.simplify()
4825  >>> g2
4826  [x >= 1]
4827  >>> # g was not modified
4828  >>> g
4829  [x + 1 >= 2]
4830  """
4831  t = Tactic('simplify')
4832  return t.apply(self, *arguments, **keywords)[0]
4833 
def simplify(self, arguments, keywords)
Definition: z3py.py:4814
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 4682 of file z3py.py.

Referenced by Goal.__len__().

4682  def size(self):
4683  """Return the number of constraints in the goal `self`.
4684 
4685  >>> g = Goal()
4686  >>> g.size()
4687  0
4688  >>> x, y = Ints('x y')
4689  >>> g.add(x == 0, y > x)
4690  >>> g.size()
4691  2
4692  """
4693  return int(Z3_goal_size(self.ctx.ref(), self.goal))
4694 
def size(self)
Definition: z3py.py:4682
unsigned Z3_API Z3_goal_size(Z3_context c, 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 4791 of file z3py.py.

4791  def translate(self, target):
4792  """Copy goal `self` to context `target`.
4793 
4794  >>> x = Int('x')
4795  >>> g = Goal()
4796  >>> g.add(x > 10)
4797  >>> g
4798  [x > 10]
4799  >>> c2 = Context()
4800  >>> g2 = g.translate(c2)
4801  >>> g2
4802  [x > 10]
4803  >>> g.ctx == main_ctx()
4804  True
4805  >>> g2.ctx == c2
4806  True
4807  >>> g2.ctx == main_ctx()
4808  False
4809  """
4810  if __debug__:
4811  _z3_assert(isinstance(target, Context), "target must be a context")
4812  return Goal(goal=Z3_goal_translate(self.ctx.ref(), self.goal, target.ref()), ctx=target)
4813 
def translate(self, target)
Definition: z3py.py:4791
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
goal