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 convert_model (self, model)
 
def __repr__ (self)
 
def sexpr (self)
 
def dimacs (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 5079 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 5087 of file z3py.py.

5087  def __init__(self, models=True, unsat_cores=False, proofs=False, ctx=None, goal=None):
5088  if __debug__:
5089  _z3_assert(goal is None or ctx is not None, "If goal is different from None, then ctx must be also different from None")
5090  self.ctx = _get_ctx(ctx)
5091  self.goal = goal
5092  if self.goal is None:
5093  self.goal = Z3_mk_goal(self.ctx.ref(), models, unsat_cores, proofs)
5094  Z3_goal_inc_ref(self.ctx.ref(), self.goal)
5095 
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 5099 of file z3py.py.

5099  def __del__(self):
5100  if self.goal is not None and self.ctx.ref() is not None:
5101  Z3_goal_dec_ref(self.ctx.ref(), self.goal)
5102 
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 5333 of file z3py.py.

5333  def __copy__(self):
5334  return self.translate(self.ctx)
5335 

◆ __deepcopy__() [1/2]

def __deepcopy__ (   self,
  memo = {} 
)

Definition at line 5096 of file z3py.py.

5096  def __deepcopy__(self, memo={}):
5097  return Goal(False, False, False, self.ctx, self.goal)
5098 

◆ __deepcopy__() [2/2]

def __deepcopy__ (   self)

Definition at line 5336 of file z3py.py.

5336  def __deepcopy__(self):
5337  return self.translate(self.ctx)
5338 

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

5207  def __getitem__(self, arg):
5208  """Return a constraint in the goal `self`.
5209 
5210  >>> g = Goal()
5211  >>> x, y = Ints('x y')
5212  >>> g.add(x == 0, y > x)
5213  >>> g[0]
5214  x == 0
5215  >>> g[1]
5216  y > x
5217  """
5218  if arg >= len(self):
5219  raise IndexError
5220  return self.get(arg)
5221 

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

5181  def __len__(self):
5182  """Return the number of constraints in the goal `self`.
5183 
5184  >>> g = Goal()
5185  >>> len(g)
5186  0
5187  >>> x, y = Ints('x y')
5188  >>> g.add(x == 0, y > x)
5189  >>> len(g)
5190  2
5191  """
5192  return self.size()
5193 

◆ __repr__()

def __repr__ (   self)

Definition at line 5299 of file z3py.py.

5299  def __repr__(self):
5300  return obj_to_string(self)
5301 

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

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

5259  def add(self, *args):
5260  """Add constraints.
5261 
5262  >>> x = Int('x')
5263  >>> g = Goal()
5264  >>> g.add(x > 0, x < 2)
5265  >>> g
5266  [x > 0, x < 2]
5267  """
5268  self.assert_exprs(*args)
5269 

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

5237  def append(self, *args):
5238  """Add constraints.
5239 
5240  >>> x = Int('x')
5241  >>> g = Goal()
5242  >>> g.append(x > 0, x < 2)
5243  >>> g
5244  [x > 0, x < 2]
5245  """
5246  self.assert_exprs(*args)
5247 

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

5359  def as_expr(self):
5360  """Return goal `self` as a single Z3 expression.
5361 
5362  >>> x = Int('x')
5363  >>> g = Goal()
5364  >>> g.as_expr()
5365  True
5366  >>> g.add(x > 1)
5367  >>> g.as_expr()
5368  x > 1
5369  >>> g.add(x < 10)
5370  >>> g.as_expr()
5371  And(x > 1, x < 10)
5372  """
5373  sz = len(self)
5374  if sz == 0:
5375  return BoolVal(True, self.ctx)
5376  elif sz == 1:
5377  return self.get(0)
5378  else:
5379  return And([ self.get(i) for i in range(len(self)) ], self.ctx)
5380 
expr range(expr const &lo, expr const &hi)
Definition: z3++.h:3244
def And(args)
Definition: z3py.py:1655
def BoolVal(val, ctx=None)
Definition: z3py.py:1529

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

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

5222  def assert_exprs(self, *args):
5223  """Assert constraints into the goal.
5224 
5225  >>> x = Int('x')
5226  >>> g = Goal()
5227  >>> g.assert_exprs(x > 0, x < 2)
5228  >>> g
5229  [x > 0, x < 2]
5230  """
5231  args = _get_args(args)
5232  s = BoolSort(self.ctx)
5233  for arg in args:
5234  arg = s.cast(arg)
5235  Z3_goal_assert(self.ctx.ref(), self.goal, arg.as_ast())
5236 
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:1512

◆ convert_model()

def convert_model (   self,
  model 
)
Retrieve model from a satisfiable goal
>>> a, b = Ints('a b')
>>> g = Goal()
>>> g.add(Or(a == 0, a == 1), Or(b == 0, b == 1), a > b)
>>> t = Then(Tactic('split-clause'), Tactic('solve-eqs'))
>>> r = t(g)
>>> r[0]
[Or(b == 0, b == 1), Not(0 <= b)]
>>> r[1]
[Or(b == 0, b == 1), Not(1 <= b)]
>>> # Remark: the subgoal r[0] is unsatisfiable
>>> # Creating a solver for solving the second subgoal
>>> s = Solver()
>>> s.add(r[1])
>>> s.check()
sat
>>> s.model()
[b = 0]
>>> # Model s.model() does not assign a value to `a`
>>> # It is a model for subgoal `r[1]`, but not for goal `g`
>>> # The method convert_model creates a model for `g` from a model for `r[1]`.
>>> r[1].convert_model(s.model())
[b = 0, a = 1]

Definition at line 5270 of file z3py.py.

5270  def convert_model(self, model):
5271  """Retrieve model from a satisfiable goal
5272  >>> a, b = Ints('a b')
5273  >>> g = Goal()
5274  >>> g.add(Or(a == 0, a == 1), Or(b == 0, b == 1), a > b)
5275  >>> t = Then(Tactic('split-clause'), Tactic('solve-eqs'))
5276  >>> r = t(g)
5277  >>> r[0]
5278  [Or(b == 0, b == 1), Not(0 <= b)]
5279  >>> r[1]
5280  [Or(b == 0, b == 1), Not(1 <= b)]
5281  >>> # Remark: the subgoal r[0] is unsatisfiable
5282  >>> # Creating a solver for solving the second subgoal
5283  >>> s = Solver()
5284  >>> s.add(r[1])
5285  >>> s.check()
5286  sat
5287  >>> s.model()
5288  [b = 0]
5289  >>> # Model s.model() does not assign a value to `a`
5290  >>> # It is a model for subgoal `r[1]`, but not for goal `g`
5291  >>> # The method convert_model creates a model for `g` from a model for `r[1]`.
5292  >>> r[1].convert_model(s.model())
5293  [b = 0, a = 1]
5294  """
5295  if __debug__:
5296  _z3_assert(isinstance(model, ModelRef), "Z3 Model expected")
5297  return ModelRef(Z3_goal_convert_model(self.ctx.ref(), self.goal, model.model), self.ctx)
5298 
Z3_model Z3_API Z3_goal_convert_model(Z3_context c, Z3_goal g, Z3_model m)
Convert a model of the formulas of a goal to a model of an original goal. The model may be null...

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

5103  def depth(self):
5104  """Return the depth of the goal `self`. The depth corresponds to the number of tactics applied to `self`.
5105 
5106  >>> x, y = Ints('x y')
5107  >>> g = Goal()
5108  >>> g.add(x == 0, y >= x + 1)
5109  >>> g.depth()
5110  0
5111  >>> r = Then('simplify', 'solve-eqs')(g)
5112  >>> # r has 1 subgoal
5113  >>> len(r)
5114  1
5115  >>> r[0].depth()
5116  2
5117  """
5118  return int(Z3_goal_depth(self.ctx.ref(), self.goal))
5119 
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...

◆ dimacs()

def dimacs (   self)
Return a textual representation of the goal in DIMACS format.

Definition at line 5306 of file z3py.py.

5306  def dimacs(self):
5307  """Return a textual representation of the goal in DIMACS format."""
5308  return Z3_goal_to_dimacs_string(self.ctx.ref(), self.goal)
5309 
Z3_string Z3_API Z3_goal_to_dimacs_string(Z3_context c, Z3_goal g)
Convert a goal into a DIMACS formatted string. The goal must be in CNF. You can convert a goal to CNF...

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

5194  def get(self, i):
5195  """Return a constraint in the goal `self`.
5196 
5197  >>> g = Goal()
5198  >>> x, y = Ints('x y')
5199  >>> g.add(x == 0, y > x)
5200  >>> g.get(0)
5201  x == 0
5202  >>> g.get(1)
5203  y > x
5204  """
5205  return _to_expr_ref(Z3_goal_formula(self.ctx.ref(), self.goal, i), self.ctx)
5206 
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 5120 of file z3py.py.

5120  def inconsistent(self):
5121  """Return `True` if `self` contains the `False` constraints.
5122 
5123  >>> x, y = Ints('x y')
5124  >>> g = Goal()
5125  >>> g.inconsistent()
5126  False
5127  >>> g.add(x == 0, x == 1)
5128  >>> g
5129  [x == 0, x == 1]
5130  >>> g.inconsistent()
5131  False
5132  >>> g2 = Tactic('propagate-values')(g)[0]
5133  >>> g2.inconsistent()
5134  True
5135  """
5136  return Z3_goal_inconsistent(self.ctx.ref(), self.goal)
5137 
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 5248 of file z3py.py.

5248  def insert(self, *args):
5249  """Add constraints.
5250 
5251  >>> x = Int('x')
5252  >>> g = Goal()
5253  >>> g.insert(x > 0, x < 2)
5254  >>> g
5255  [x > 0, x < 2]
5256  """
5257  self.assert_exprs(*args)
5258 

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

Referenced by Goal.precision().

5138  def prec(self):
5139  """Return the precision (under-approximation, over-approximation, or precise) of the goal `self`.
5140 
5141  >>> g = Goal()
5142  >>> g.prec() == Z3_GOAL_PRECISE
5143  True
5144  >>> x, y = Ints('x y')
5145  >>> g.add(x == y + 1)
5146  >>> g.prec() == Z3_GOAL_PRECISE
5147  True
5148  >>> t = With(Tactic('add-bounds'), add_bound_lower=0, add_bound_upper=10)
5149  >>> g2 = t(g)[0]
5150  >>> g2
5151  [x == y + 1, x <= 10, x >= 0, y <= 10, y >= 0]
5152  >>> g2.prec() == Z3_GOAL_PRECISE
5153  False
5154  >>> g2.prec() == Z3_GOAL_UNDER
5155  True
5156  """
5157  return Z3_goal_precision(self.ctx.ref(), self.goal)
5158 
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 5159 of file z3py.py.

5159  def precision(self):
5160  """Alias for `prec()`.
5161 
5162  >>> g = Goal()
5163  >>> g.precision() == Z3_GOAL_PRECISE
5164  True
5165  """
5166  return self.prec()
5167 

◆ sexpr()

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

Definition at line 5302 of file z3py.py.

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

5302  def sexpr(self):
5303  """Return a textual representation of the s-expression representing the goal."""
5304  return Z3_goal_to_string(self.ctx.ref(), self.goal)
5305 
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 5339 of file z3py.py.

5339  def simplify(self, *arguments, **keywords):
5340  """Return a new simplified goal.
5341 
5342  This method is essentially invoking the simplify tactic.
5343 
5344  >>> g = Goal()
5345  >>> x = Int('x')
5346  >>> g.add(x + 1 >= 2)
5347  >>> g
5348  [x + 1 >= 2]
5349  >>> g2 = g.simplify()
5350  >>> g2
5351  [x >= 1]
5352  >>> # g was not modified
5353  >>> g
5354  [x + 1 >= 2]
5355  """
5356  t = Tactic('simplify')
5357  return t.apply(self, *arguments, **keywords)[0]
5358 
def simplify(a, arguments, keywords)
Utils.
Definition: z3py.py:8080

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

5168  def size(self):
5169  """Return the number of constraints in the goal `self`.
5170 
5171  >>> g = Goal()
5172  >>> g.size()
5173  0
5174  >>> x, y = Ints('x y')
5175  >>> g.add(x == 0, y > x)
5176  >>> g.size()
5177  2
5178  """
5179  return int(Z3_goal_size(self.ctx.ref(), self.goal))
5180 
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 5310 of file z3py.py.

5310  def translate(self, target):
5311  """Copy goal `self` to context `target`.
5312 
5313  >>> x = Int('x')
5314  >>> g = Goal()
5315  >>> g.add(x > 10)
5316  >>> g
5317  [x > 10]
5318  >>> c2 = Context()
5319  >>> g2 = g.translate(c2)
5320  >>> g2
5321  [x > 10]
5322  >>> g.ctx == main_ctx()
5323  True
5324  >>> g2.ctx == c2
5325  True
5326  >>> g2.ctx == main_ctx()
5327  False
5328  """
5329  if __debug__:
5330  _z3_assert(isinstance(target, Context), "target must be a context")
5331  return Goal(goal=Z3_goal_translate(self.ctx.ref(), self.goal, target.ref()), ctx=target)
5332 
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 5090 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(), 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(), Optimize.unsat_core(), and Fixedpoint.update_rule().

◆ goal

goal

Definition at line 5091 of file z3py.py.