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, memo={})
 
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 5128 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 5136 of file z3py.py.

5136  def __init__(self, models=True, unsat_cores=False, proofs=False, ctx=None, goal=None):
5137  if z3_debug():
5138  _z3_assert(goal is None or ctx is not None, "If goal is different from None, then ctx must be also different from None")
5139  self.ctx = _get_ctx(ctx)
5140  self.goal = goal
5141  if self.goal is None:
5142  self.goal = Z3_mk_goal(self.ctx.ref(), models, unsat_cores, proofs)
5143  Z3_goal_inc_ref(self.ctx.ref(), self.goal)
5144 
Z3_goal Z3_API Z3_mk_goal(Z3_context c, bool models, bool unsat_cores, bool proofs)
Create a goal (aka problem). A goal is essentially a set of formulas, that can be solved and/or trans...
def z3_debug()
Definition: z3py.py:58
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 5148 of file z3py.py.

5148  def __del__(self):
5149  if self.goal is not None and self.ctx.ref() is not None:
5150  Z3_goal_dec_ref(self.ctx.ref(), self.goal)
5151 
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 5382 of file z3py.py.

5382  def __copy__(self):
5383  return self.translate(self.ctx)
5384 

◆ __deepcopy__() [1/2]

def __deepcopy__ (   self,
  memo = {} 
)

Definition at line 5145 of file z3py.py.

5145  def __deepcopy__(self, memo={}):
5146  return Goal(False, False, False, self.ctx, self.goal)
5147 

◆ __deepcopy__() [2/2]

def __deepcopy__ (   self,
  memo = {} 
)

Definition at line 5385 of file z3py.py.

5385  def __deepcopy__(self, memo={}):
5386  return self.translate(self.ctx)
5387 

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

5256  def __getitem__(self, arg):
5257  """Return a constraint in the goal `self`.
5258 
5259  >>> g = Goal()
5260  >>> x, y = Ints('x y')
5261  >>> g.add(x == 0, y > x)
5262  >>> g[0]
5263  x == 0
5264  >>> g[1]
5265  y > x
5266  """
5267  if arg >= len(self):
5268  raise IndexError
5269  return self.get(arg)
5270 

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

5230  def __len__(self):
5231  """Return the number of constraints in the goal `self`.
5232 
5233  >>> g = Goal()
5234  >>> len(g)
5235  0
5236  >>> x, y = Ints('x y')
5237  >>> g.add(x == 0, y > x)
5238  >>> len(g)
5239  2
5240  """
5241  return self.size()
5242 

◆ __repr__()

def __repr__ (   self)

Definition at line 5348 of file z3py.py.

5348  def __repr__(self):
5349  return obj_to_string(self)
5350 

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

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

5308  def add(self, *args):
5309  """Add constraints.
5310 
5311  >>> x = Int('x')
5312  >>> g = Goal()
5313  >>> g.add(x > 0, x < 2)
5314  >>> g
5315  [x > 0, x < 2]
5316  """
5317  self.assert_exprs(*args)
5318 

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

5286  def append(self, *args):
5287  """Add constraints.
5288 
5289  >>> x = Int('x')
5290  >>> g = Goal()
5291  >>> g.append(x > 0, x < 2)
5292  >>> g
5293  [x > 0, x < 2]
5294  """
5295  self.assert_exprs(*args)
5296 

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

5408  def as_expr(self):
5409  """Return goal `self` as a single Z3 expression.
5410 
5411  >>> x = Int('x')
5412  >>> g = Goal()
5413  >>> g.as_expr()
5414  True
5415  >>> g.add(x > 1)
5416  >>> g.as_expr()
5417  x > 1
5418  >>> g.add(x < 10)
5419  >>> g.as_expr()
5420  And(x > 1, x < 10)
5421  """
5422  sz = len(self)
5423  if sz == 0:
5424  return BoolVal(True, self.ctx)
5425  elif sz == 1:
5426  return self.get(0)
5427  else:
5428  return And([ self.get(i) for i in range(len(self)) ], self.ctx)
5429 
expr range(expr const &lo, expr const &hi)
Definition: z3++.h:3358
def And(args)
Definition: z3py.py:1672
def BoolVal(val, ctx=None)
Definition: z3py.py:1540

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

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

5271  def assert_exprs(self, *args):
5272  """Assert constraints into the goal.
5273 
5274  >>> x = Int('x')
5275  >>> g = Goal()
5276  >>> g.assert_exprs(x > 0, x < 2)
5277  >>> g
5278  [x > 0, x < 2]
5279  """
5280  args = _get_args(args)
5281  s = BoolSort(self.ctx)
5282  for arg in args:
5283  arg = s.cast(arg)
5284  Z3_goal_assert(self.ctx.ref(), self.goal, arg.as_ast())
5285 
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:1523

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

5319  def convert_model(self, model):
5320  """Retrieve model from a satisfiable goal
5321  >>> a, b = Ints('a b')
5322  >>> g = Goal()
5323  >>> g.add(Or(a == 0, a == 1), Or(b == 0, b == 1), a > b)
5324  >>> t = Then(Tactic('split-clause'), Tactic('solve-eqs'))
5325  >>> r = t(g)
5326  >>> r[0]
5327  [Or(b == 0, b == 1), Not(0 <= b)]
5328  >>> r[1]
5329  [Or(b == 0, b == 1), Not(1 <= b)]
5330  >>> # Remark: the subgoal r[0] is unsatisfiable
5331  >>> # Creating a solver for solving the second subgoal
5332  >>> s = Solver()
5333  >>> s.add(r[1])
5334  >>> s.check()
5335  sat
5336  >>> s.model()
5337  [b = 0]
5338  >>> # Model s.model() does not assign a value to `a`
5339  >>> # It is a model for subgoal `r[1]`, but not for goal `g`
5340  >>> # The method convert_model creates a model for `g` from a model for `r[1]`.
5341  >>> r[1].convert_model(s.model())
5342  [b = 0, a = 1]
5343  """
5344  if z3_debug():
5345  _z3_assert(isinstance(model, ModelRef), "Z3 Model expected")
5346  return ModelRef(Z3_goal_convert_model(self.ctx.ref(), self.goal, model.model), self.ctx)
5347 
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...
def z3_debug()
Definition: z3py.py:58

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

5152  def depth(self):
5153  """Return the depth of the goal `self`. The depth corresponds to the number of tactics applied to `self`.
5154 
5155  >>> x, y = Ints('x y')
5156  >>> g = Goal()
5157  >>> g.add(x == 0, y >= x + 1)
5158  >>> g.depth()
5159  0
5160  >>> r = Then('simplify', 'solve-eqs')(g)
5161  >>> # r has 1 subgoal
5162  >>> len(r)
5163  1
5164  >>> r[0].depth()
5165  2
5166  """
5167  return int(Z3_goal_depth(self.ctx.ref(), self.goal))
5168 
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 5355 of file z3py.py.

5355  def dimacs(self):
5356  """Return a textual representation of the goal in DIMACS format."""
5357  return Z3_goal_to_dimacs_string(self.ctx.ref(), self.goal)
5358 
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 5243 of file z3py.py.

5243  def get(self, i):
5244  """Return a constraint in the goal `self`.
5245 
5246  >>> g = Goal()
5247  >>> x, y = Ints('x y')
5248  >>> g.add(x == 0, y > x)
5249  >>> g.get(0)
5250  x == 0
5251  >>> g.get(1)
5252  y > x
5253  """
5254  return _to_expr_ref(Z3_goal_formula(self.ctx.ref(), self.goal, i), self.ctx)
5255 
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 5169 of file z3py.py.

5169  def inconsistent(self):
5170  """Return `True` if `self` contains the `False` constraints.
5171 
5172  >>> x, y = Ints('x y')
5173  >>> g = Goal()
5174  >>> g.inconsistent()
5175  False
5176  >>> g.add(x == 0, x == 1)
5177  >>> g
5178  [x == 0, x == 1]
5179  >>> g.inconsistent()
5180  False
5181  >>> g2 = Tactic('propagate-values')(g)[0]
5182  >>> g2.inconsistent()
5183  True
5184  """
5185  return Z3_goal_inconsistent(self.ctx.ref(), self.goal)
5186 
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 5297 of file z3py.py.

5297  def insert(self, *args):
5298  """Add constraints.
5299 
5300  >>> x = Int('x')
5301  >>> g = Goal()
5302  >>> g.insert(x > 0, x < 2)
5303  >>> g
5304  [x > 0, x < 2]
5305  """
5306  self.assert_exprs(*args)
5307 

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

Referenced by Goal.precision().

5187  def prec(self):
5188  """Return the precision (under-approximation, over-approximation, or precise) of the goal `self`.
5189 
5190  >>> g = Goal()
5191  >>> g.prec() == Z3_GOAL_PRECISE
5192  True
5193  >>> x, y = Ints('x y')
5194  >>> g.add(x == y + 1)
5195  >>> g.prec() == Z3_GOAL_PRECISE
5196  True
5197  >>> t = With(Tactic('add-bounds'), add_bound_lower=0, add_bound_upper=10)
5198  >>> g2 = t(g)[0]
5199  >>> g2
5200  [x == y + 1, x <= 10, x >= 0, y <= 10, y >= 0]
5201  >>> g2.prec() == Z3_GOAL_PRECISE
5202  False
5203  >>> g2.prec() == Z3_GOAL_UNDER
5204  True
5205  """
5206  return Z3_goal_precision(self.ctx.ref(), self.goal)
5207 
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 5208 of file z3py.py.

5208  def precision(self):
5209  """Alias for `prec()`.
5210 
5211  >>> g = Goal()
5212  >>> g.precision() == Z3_GOAL_PRECISE
5213  True
5214  """
5215  return self.prec()
5216 

◆ sexpr()

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

Definition at line 5351 of file z3py.py.

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

5351  def sexpr(self):
5352  """Return a textual representation of the s-expression representing the goal."""
5353  return Z3_goal_to_string(self.ctx.ref(), self.goal)
5354 
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 5388 of file z3py.py.

5388  def simplify(self, *arguments, **keywords):
5389  """Return a new simplified goal.
5390 
5391  This method is essentially invoking the simplify tactic.
5392 
5393  >>> g = Goal()
5394  >>> x = Int('x')
5395  >>> g.add(x + 1 >= 2)
5396  >>> g
5397  [x + 1 >= 2]
5398  >>> g2 = g.simplify()
5399  >>> g2
5400  [x >= 1]
5401  >>> # g was not modified
5402  >>> g
5403  [x + 1 >= 2]
5404  """
5405  t = Tactic('simplify')
5406  return t.apply(self, *arguments, **keywords)[0]
5407 
def simplify(a, arguments, keywords)
Utils.
Definition: z3py.py:8149

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

5217  def size(self):
5218  """Return the number of constraints in the goal `self`.
5219 
5220  >>> g = Goal()
5221  >>> g.size()
5222  0
5223  >>> x, y = Ints('x y')
5224  >>> g.add(x == 0, y > x)
5225  >>> g.size()
5226  2
5227  """
5228  return int(Z3_goal_size(self.ctx.ref(), self.goal))
5229 
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 5359 of file z3py.py.

5359  def translate(self, target):
5360  """Copy goal `self` to context `target`.
5361 
5362  >>> x = Int('x')
5363  >>> g = Goal()
5364  >>> g.add(x > 10)
5365  >>> g
5366  [x > 10]
5367  >>> c2 = Context()
5368  >>> g2 = g.translate(c2)
5369  >>> g2
5370  [x > 10]
5371  >>> g.ctx == main_ctx()
5372  True
5373  >>> g2.ctx == c2
5374  True
5375  >>> g2.ctx == main_ctx()
5376  False
5377  """
5378  if z3_debug():
5379  _z3_assert(isinstance(target, Context), "target must be a context")
5380  return Goal(goal=Z3_goal_translate(self.ctx.ref(), self.goal, target.ref()), ctx=target)
5381 
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.
def z3_debug()
Definition: z3py.py:58

Field Documentation

◆ ctx

ctx

Definition at line 5139 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(), Optimize.assert_and_track(), 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(), Optimize.pop(), 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 5140 of file z3py.py.