Z3
Loading...
Searching...
No Matches
Goal Class Reference
Inheritance diagram for Goal:

Public Member Functions

 __init__ (self, models=True, unsat_cores=False, proofs=False, ctx=None, goal=None)
 __del__ (self)
 depth (self)
 inconsistent (self)
 prec (self)
 precision (self)
 size (self)
 __len__ (self)
 get (self, i)
 __getitem__ (self, arg)
 assert_exprs (self, *args)
 append (self, *args)
 insert (self, *args)
 add (self, *args)
 convert_model (self, model)
 __repr__ (self)
 sexpr (self)
 dimacs (self, include_names=True)
 translate (self, target)
 __copy__ (self)
 __deepcopy__ (self, memo={})
 simplify (self, *arguments, **keywords)
 as_expr (self)
Public Member Functions inherited from Z3PPObject
 use_pp (self)

Data Fields

 ctx = _get_ctx(ctx)
 goal = goal

Additional Inherited Members

Protected Member Functions inherited from Z3PPObject
 _repr_html_ (self)

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

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 6167 of file z3py.py.

6167 def __init__(self, models=True, unsat_cores=False, proofs=False, ctx=None, goal=None):
6168 if z3_debug():
6169 _z3_assert(goal is None or ctx is not None,
6170 "If goal is different from None, then ctx must be also different from None")
6171 self.ctx = _get_ctx(ctx)
6172 self.goal = goal
6173 if self.goal is None:
6174 self.goal = Z3_mk_goal(self.ctx.ref(), models, unsat_cores, proofs)
6175 Z3_goal_inc_ref(self.ctx.ref(), self.goal)
6176
void Z3_API Z3_goal_inc_ref(Z3_context c, Z3_goal g)
Increment the reference counter of the given goal.
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...

◆ __del__()

__del__ ( self)

Definition at line 6177 of file z3py.py.

6177 def __del__(self):
6178 if self.goal is not None and self.ctx.ref() is not None and Z3_goal_dec_ref is not None:
6179 Z3_goal_dec_ref(self.ctx.ref(), self.goal)
6180
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__()

__copy__ ( self)

Definition at line 6414 of file z3py.py.

6414 def __copy__(self):
6415 return self.translate(self.ctx)
6416

◆ __deepcopy__()

__deepcopy__ ( self,
memo = {} )

Definition at line 6417 of file z3py.py.

6417 def __deepcopy__(self, memo={}):
6418 return self.translate(self.ctx)
6419

◆ __getitem__()

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

6286 def __getitem__(self, arg):
6287 """Return a constraint in the goal `self`.
6288
6289 >>> g = Goal()
6290 >>> x, y = Ints('x y')
6291 >>> g.add(x == 0, y > x)
6292 >>> g[0]
6293 x == 0
6294 >>> g[1]
6295 y > x
6296 """
6297 if arg < 0:
6298 arg += len(self)
6299 if arg < 0 or arg >= len(self):
6300 raise IndexError
6301 return self.get(arg)
6302

◆ __len__()

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

6260 def __len__(self):
6261 """Return the number of constraints in the goal `self`.
6262
6263 >>> g = Goal()
6264 >>> len(g)
6265 0
6266 >>> x, y = Ints('x y')
6267 >>> g.add(x == 0, y > x)
6268 >>> len(g)
6269 2
6270 """
6271 return self.size()
6272

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

◆ __repr__()

__repr__ ( self)

Definition at line 6380 of file z3py.py.

6380 def __repr__(self):
6381 return obj_to_string(self)
6382

◆ add()

add ( self,
* args )
Add constraints.

>>> x = Int('x')
>>> g = Goal()
>>> g.add(x > 0, x < 2)
>>> g
[x > 0, x < 2]

Definition at line 6340 of file z3py.py.

6340 def add(self, *args):
6341 """Add constraints.
6342
6343 >>> x = Int('x')
6344 >>> g = Goal()
6345 >>> g.add(x > 0, x < 2)
6346 >>> g
6347 [x > 0, x < 2]
6348 """
6349 self.assert_exprs(*args)
6350

Referenced by Solver.__iadd__().

◆ append()

append ( self,
* args )
Add constraints.

>>> x = Int('x')
>>> g = Goal()
>>> g.append(x > 0, x < 2)
>>> g
[x > 0, x < 2]

Definition at line 6318 of file z3py.py.

6318 def append(self, *args):
6319 """Add constraints.
6320
6321 >>> x = Int('x')
6322 >>> g = Goal()
6323 >>> g.append(x > 0, x < 2)
6324 >>> g
6325 [x > 0, x < 2]
6326 """
6327 self.assert_exprs(*args)
6328

◆ as_expr()

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

6440 def as_expr(self):
6441 """Return goal `self` as a single Z3 expression.
6442
6443 >>> x = Int('x')
6444 >>> g = Goal()
6445 >>> g.as_expr()
6446 True
6447 >>> g.add(x > 1)
6448 >>> g.as_expr()
6449 x > 1
6450 >>> g.add(x < 10)
6451 >>> g.as_expr()
6452 And(x > 1, x < 10)
6453 """
6454 sz = len(self)
6455 if sz == 0:
6456 return BoolVal(True, self.ctx)
6457 elif sz == 1:
6458 return self.get(0)
6459 else:
6460 return And([self.get(i) for i in range(len(self))], self.ctx)
6461

◆ assert_exprs()

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

6303 def assert_exprs(self, *args):
6304 """Assert constraints into the goal.
6305
6306 >>> x = Int('x')
6307 >>> g = Goal()
6308 >>> g.assert_exprs(x > 0, x < 2)
6309 >>> g
6310 [x > 0, x < 2]
6311 """
6312 args = _get_args(args)
6313 s = BoolSort(self.ctx)
6314 for arg in args:
6315 arg = s.cast(arg)
6316 Z3_goal_assert(self.ctx.ref(), self.goal, arg.as_ast())
6317
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...

Referenced by add(), Solver.add(), append(), Solver.append(), insert(), and Solver.insert().

◆ convert_model()

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

6351 def convert_model(self, model):
6352 """Retrieve model from a satisfiable goal
6353 >>> a, b = Ints('a b')
6354 >>> g = Goal()
6355 >>> g.add(Or(a == 0, a == 1), Or(b == 0, b == 1), a > b)
6356 >>> t = Then(Tactic('split-clause'), Tactic('solve-eqs'))
6357 >>> r = t(g)
6358 >>> r[0]
6359 [Or(b == 0, b == 1), Not(0 <= b)]
6360 >>> r[1]
6361 [Or(b == 0, b == 1), Not(1 <= b)]
6362 >>> # Remark: the subgoal r[0] is unsatisfiable
6363 >>> # Creating a solver for solving the second subgoal
6364 >>> s = Solver()
6365 >>> s.add(r[1])
6366 >>> s.check()
6367 sat
6368 >>> s.model()
6369 [b = 0]
6370 >>> # Model s.model() does not assign a value to `a`
6371 >>> # It is a model for subgoal `r[1]`, but not for goal `g`
6372 >>> # The method convert_model creates a model for `g` from a model for `r[1]`.
6373 >>> r[1].convert_model(s.model())
6374 [b = 0, a = 1]
6375 """
6376 if z3_debug():
6377 _z3_assert(isinstance(model, ModelRef), "Z3 Model expected")
6378 return ModelRef(Z3_goal_convert_model(self.ctx.ref(), self.goal, model.model), self.ctx)
6379
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()

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

6181 def depth(self):
6182 """Return the depth of the goal `self`.
6183 The depth corresponds to the number of tactics applied to `self`.
6184
6185 >>> x, y = Ints('x y')
6186 >>> g = Goal()
6187 >>> g.add(x == 0, y >= x + 1)
6188 >>> g.depth()
6189 0
6190 >>> r = Then('simplify', 'solve-eqs')(g)
6191 >>> # r has 1 subgoal
6192 >>> len(r)
6193 1
6194 >>> r[0].depth()
6195 2
6196 """
6197 return int(Z3_goal_depth(self.ctx.ref(), self.goal))
6198
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()

dimacs ( self,
include_names = True )
Return a textual representation of the goal in DIMACS format.

Definition at line 6387 of file z3py.py.

6387 def dimacs(self, include_names=True):
6388 """Return a textual representation of the goal in DIMACS format."""
6389 return Z3_goal_to_dimacs_string(self.ctx.ref(), self.goal, include_names)
6390
Z3_string Z3_API Z3_goal_to_dimacs_string(Z3_context c, Z3_goal g, bool include_names)
Convert a goal into a DIMACS formatted string. The goal must be in CNF. You can convert a goal to CNF...

◆ get()

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

6273 def get(self, i):
6274 """Return a constraint in the goal `self`.
6275
6276 >>> g = Goal()
6277 >>> x, y = Ints('x y')
6278 >>> g.add(x == 0, y > x)
6279 >>> g.get(0)
6280 x == 0
6281 >>> g.get(1)
6282 y > x
6283 """
6284 return _to_expr_ref(Z3_goal_formula(self.ctx.ref(), self.goal, i), self.ctx)
6285
Z3_ast Z3_API Z3_goal_formula(Z3_context c, Z3_goal g, unsigned idx)
Return a formula from the given goal.

Referenced by __getitem__(), and as_expr().

◆ inconsistent()

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

6199 def inconsistent(self):
6200 """Return `True` if `self` contains the `False` constraints.
6201
6202 >>> x, y = Ints('x y')
6203 >>> g = Goal()
6204 >>> g.inconsistent()
6205 False
6206 >>> g.add(x == 0, x == 1)
6207 >>> g
6208 [x == 0, x == 1]
6209 >>> g.inconsistent()
6210 False
6211 >>> g2 = Tactic('propagate-values')(g)[0]
6212 >>> g2.inconsistent()
6213 True
6214 """
6215 return Z3_goal_inconsistent(self.ctx.ref(), self.goal)
6216
bool Z3_API Z3_goal_inconsistent(Z3_context c, Z3_goal g)
Return true if the given goal contains the formula false.

◆ insert()

insert ( self,
* args )
Add constraints.

>>> x = Int('x')
>>> g = Goal()
>>> g.insert(x > 0, x < 2)
>>> g
[x > 0, x < 2]

Definition at line 6329 of file z3py.py.

6329 def insert(self, *args):
6330 """Add constraints.
6331
6332 >>> x = Int('x')
6333 >>> g = Goal()
6334 >>> g.insert(x > 0, x < 2)
6335 >>> g
6336 [x > 0, x < 2]
6337 """
6338 self.assert_exprs(*args)
6339

◆ prec()

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

6217 def prec(self):
6218 """Return the precision (under-approximation, over-approximation, or precise) of the goal `self`.
6219
6220 >>> g = Goal()
6221 >>> g.prec() == Z3_GOAL_PRECISE
6222 True
6223 >>> x, y = Ints('x y')
6224 >>> g.add(x == y + 1)
6225 >>> g.prec() == Z3_GOAL_PRECISE
6226 True
6227 >>> t = With(Tactic('add-bounds'), add_bound_lower=0, add_bound_upper=10)
6228 >>> g2 = t(g)[0]
6229 >>> g2
6230 [x == y + 1, x <= 10, x >= 0, y <= 10, y >= 0]
6231 >>> g2.prec() == Z3_GOAL_PRECISE
6232 False
6233 >>> g2.prec() == Z3_GOAL_UNDER
6234 True
6235 """
6236 return Z3_goal_precision(self.ctx.ref(), self.goal)
6237
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...

Referenced by precision().

◆ precision()

precision ( self)
Alias for `prec()`.

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

Definition at line 6238 of file z3py.py.

6238 def precision(self):
6239 """Alias for `prec()`.
6240
6241 >>> g = Goal()
6242 >>> g.precision() == Z3_GOAL_PRECISE
6243 True
6244 """
6245 return self.prec()
6246

◆ sexpr()

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

Definition at line 6383 of file z3py.py.

6383 def sexpr(self):
6384 """Return a textual representation of the s-expression representing the goal."""
6385 return Z3_goal_to_string(self.ctx.ref(), self.goal)
6386
Z3_string Z3_API Z3_goal_to_string(Z3_context c, Z3_goal g)
Convert a goal into a string.

◆ simplify()

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

6420 def simplify(self, *arguments, **keywords):
6421 """Return a new simplified goal.
6422
6423 This method is essentially invoking the simplify tactic.
6424
6425 >>> g = Goal()
6426 >>> x = Int('x')
6427 >>> g.add(x + 1 >= 2)
6428 >>> g
6429 [x + 1 >= 2]
6430 >>> g2 = g.simplify()
6431 >>> g2
6432 [x >= 1]
6433 >>> # g was not modified
6434 >>> g
6435 [x + 1 >= 2]
6436 """
6437 t = Tactic("simplify")
6438 return t.apply(self, *arguments, **keywords)[0]
6439

◆ size()

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

6247 def size(self):
6248 """Return the number of constraints in the goal `self`.
6249
6250 >>> g = Goal()
6251 >>> g.size()
6252 0
6253 >>> x, y = Ints('x y')
6254 >>> g.add(x == 0, y > x)
6255 >>> g.size()
6256 2
6257 """
6258 return int(Z3_goal_size(self.ctx.ref(), self.goal))
6259
unsigned Z3_API Z3_goal_size(Z3_context c, Z3_goal g)
Return the number of formulas in the given goal.

Referenced by __len__().

◆ translate()

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

6391 def translate(self, target):
6392 """Copy goal `self` to context `target`.
6393
6394 >>> x = Int('x')
6395 >>> g = Goal()
6396 >>> g.add(x > 10)
6397 >>> g
6398 [x > 10]
6399 >>> c2 = Context()
6400 >>> g2 = g.translate(c2)
6401 >>> g2
6402 [x > 10]
6403 >>> g.ctx == main_ctx()
6404 True
6405 >>> g2.ctx == c2
6406 True
6407 >>> g2.ctx == main_ctx()
6408 False
6409 """
6410 if z3_debug():
6411 _z3_assert(isinstance(target, Context), "target must be a context")
6412 return Goal(goal=Z3_goal_translate(self.ctx.ref(), self.goal, target.ref()), ctx=target)
6413
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.

Referenced by AstVector.__copy__(), FuncInterp.__copy__(), __copy__(), ModelRef.__copy__(), AstVector.__deepcopy__(), FuncInterp.__deepcopy__(), __deepcopy__(), and ModelRef.__deepcopy__().

Field Documentation

◆ ctx

ctx = _get_ctx(ctx)

Definition at line 6171 of file z3py.py.

Referenced by AstMap.__contains__(), AstVector.__copy__(), FuncInterp.__copy__(), __copy__(), ModelRef.__copy__(), AstMap.__deepcopy__(), AstVector.__deepcopy__(), FuncEntry.__deepcopy__(), FuncInterp.__deepcopy__(), __deepcopy__(), ModelRef.__deepcopy__(), Statistics.__deepcopy__(), AstMap.__del__(), AstVector.__del__(), FuncEntry.__del__(), FuncInterp.__del__(), __del__(), ModelRef.__del__(), Solver.__del__(), Statistics.__del__(), AstMap.__getitem__(), AstVector.__getitem__(), ModelRef.__getitem__(), Statistics.__getitem__(), AstMap.__len__(), AstVector.__len__(), ModelRef.__len__(), Statistics.__len__(), AstMap.__repr__(), Statistics.__repr__(), AstMap.__setitem__(), AstVector.__setitem__(), FuncEntry.arg_value(), FuncInterp.arity(), as_expr(), Solver.assert_and_track(), assert_exprs(), Solver.assert_exprs(), Solver.check(), convert_model(), ModelRef.decls(), depth(), dimacs(), FuncInterp.else_value(), FuncInterp.entry(), AstMap.erase(), ModelRef.eval(), get(), ModelRef.get_interp(), Statistics.get_key_value(), ModelRef.get_sort(), ModelRef.get_universe(), inconsistent(), AstMap.keys(), Statistics.keys(), Solver.model(), FuncEntry.num_args(), FuncInterp.num_entries(), Solver.num_scopes(), ModelRef.num_sorts(), Solver.pop(), prec(), ModelRef.project(), ModelRef.project_with_witness(), AstVector.push(), Solver.push(), AstMap.reset(), Solver.reset(), AstVector.resize(), Solver.set(), AstVector.sexpr(), sexpr(), ModelRef.sexpr(), size(), AstVector.translate(), translate(), ModelRef.translate(), and FuncEntry.value().

◆ goal

goal = goal