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

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 6284 of file z3py.py.

6284 def __init__(self, models=True, unsat_cores=False, proofs=False, ctx=None, goal=None):
6285 if z3_debug():
6286 _z3_assert(goal is None or ctx is not None,
6287 "If goal is different from None, then ctx must be also different from None")
6288 self.ctx = _get_ctx(ctx)
6289 self.goal = goal
6290 if self.goal is None:
6291 self.goal = Z3_mk_goal(self.ctx.ref(), models, unsat_cores, proofs)
6292 Z3_goal_inc_ref(self.ctx.ref(), self.goal)
6293
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 6294 of file z3py.py.

6294 def __del__(self):
6295 if self.goal is not None and self.ctx.ref() is not None and Z3_goal_dec_ref is not None:
6296 Z3_goal_dec_ref(self.ctx.ref(), self.goal)
6297
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 6531 of file z3py.py.

6531 def __copy__(self):
6532 return self.translate(self.ctx)
6533

◆ __deepcopy__()

__deepcopy__ ( self,
memo = {} )

Definition at line 6534 of file z3py.py.

6534 def __deepcopy__(self, memo={}):
6535 return self.translate(self.ctx)
6536

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

6403 def __getitem__(self, arg):
6404 """Return a constraint in the goal `self`.
6405
6406 >>> g = Goal()
6407 >>> x, y = Ints('x y')
6408 >>> g.add(x == 0, y > x)
6409 >>> g[0]
6410 x == 0
6411 >>> g[1]
6412 y > x
6413 """
6414 if arg < 0:
6415 arg += len(self)
6416 if arg < 0 or arg >= len(self):
6417 raise IndexError
6418 return self.get(arg)
6419

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

6377 def __len__(self):
6378 """Return the number of constraints in the goal `self`.
6379
6380 >>> g = Goal()
6381 >>> len(g)
6382 0
6383 >>> x, y = Ints('x y')
6384 >>> g.add(x == 0, y > x)
6385 >>> len(g)
6386 2
6387 """
6388 return self.size()
6389

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

◆ __repr__()

__repr__ ( self)

Definition at line 6497 of file z3py.py.

6497 def __repr__(self):
6498 return obj_to_string(self)
6499

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

6457 def add(self, *args):
6458 """Add constraints.
6459
6460 >>> x = Int('x')
6461 >>> g = Goal()
6462 >>> g.add(x > 0, x < 2)
6463 >>> g
6464 [x > 0, x < 2]
6465 """
6466 self.assert_exprs(*args)
6467

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

6435 def append(self, *args):
6436 """Add constraints.
6437
6438 >>> x = Int('x')
6439 >>> g = Goal()
6440 >>> g.append(x > 0, x < 2)
6441 >>> g
6442 [x > 0, x < 2]
6443 """
6444 self.assert_exprs(*args)
6445

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

6557 def as_expr(self):
6558 """Return goal `self` as a single Z3 expression.
6559
6560 >>> x = Int('x')
6561 >>> g = Goal()
6562 >>> g.as_expr()
6563 True
6564 >>> g.add(x > 1)
6565 >>> g.as_expr()
6566 x > 1
6567 >>> g.add(x < 10)
6568 >>> g.as_expr()
6569 And(x > 1, x < 10)
6570 """
6571 sz = len(self)
6572 if sz == 0:
6573 return BoolVal(True, self.ctx)
6574 elif sz == 1:
6575 return self.get(0)
6576 else:
6577 return And([self.get(i) for i in range(len(self))], self.ctx)
6578

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

6420 def assert_exprs(self, *args):
6421 """Assert constraints into the goal.
6422
6423 >>> x = Int('x')
6424 >>> g = Goal()
6425 >>> g.assert_exprs(x > 0, x < 2)
6426 >>> g
6427 [x > 0, x < 2]
6428 """
6429 args = _get_args(args)
6430 s = BoolSort(self.ctx)
6431 for arg in args:
6432 arg = s.cast(arg)
6433 Z3_goal_assert(self.ctx.ref(), self.goal, arg.as_ast())
6434
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 6468 of file z3py.py.

6468 def convert_model(self, model):
6469 """Retrieve model from a satisfiable goal
6470 >>> a, b = Ints('a b')
6471 >>> g = Goal()
6472 >>> g.add(Or(a == 0, a == 1), Or(b == 0, b == 1), a > b)
6473 >>> t = Then(Tactic('split-clause'), Tactic('solve-eqs'))
6474 >>> r = t(g)
6475 >>> r[0]
6476 [Or(b == 0, b == 1), Not(0 <= b)]
6477 >>> r[1]
6478 [Or(b == 0, b == 1), Not(1 <= b)]
6479 >>> # Remark: the subgoal r[0] is unsatisfiable
6480 >>> # Creating a solver for solving the second subgoal
6481 >>> s = Solver()
6482 >>> s.add(r[1])
6483 >>> s.check()
6484 sat
6485 >>> s.model()
6486 [b = 0]
6487 >>> # Model s.model() does not assign a value to `a`
6488 >>> # It is a model for subgoal `r[1]`, but not for goal `g`
6489 >>> # The method convert_model creates a model for `g` from a model for `r[1]`.
6490 >>> r[1].convert_model(s.model())
6491 [b = 0, a = 1]
6492 """
6493 if z3_debug():
6494 _z3_assert(isinstance(model, ModelRef), "Z3 Model expected")
6495 return ModelRef(Z3_goal_convert_model(self.ctx.ref(), self.goal, model.model), self.ctx)
6496
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 6298 of file z3py.py.

6298 def depth(self):
6299 """Return the depth of the goal `self`.
6300 The depth corresponds to the number of tactics applied to `self`.
6301
6302 >>> x, y = Ints('x y')
6303 >>> g = Goal()
6304 >>> g.add(x == 0, y >= x + 1)
6305 >>> g.depth()
6306 0
6307 >>> r = Then('simplify', 'solve-eqs')(g)
6308 >>> # r has 1 subgoal
6309 >>> len(r)
6310 1
6311 >>> r[0].depth()
6312 2
6313 """
6314 return int(Z3_goal_depth(self.ctx.ref(), self.goal))
6315
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 6504 of file z3py.py.

6504 def dimacs(self, include_names=True):
6505 """Return a textual representation of the goal in DIMACS format."""
6506 return Z3_goal_to_dimacs_string(self.ctx.ref(), self.goal, include_names)
6507
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 6390 of file z3py.py.

6390 def get(self, i):
6391 """Return a constraint in the goal `self`.
6392
6393 >>> g = Goal()
6394 >>> x, y = Ints('x y')
6395 >>> g.add(x == 0, y > x)
6396 >>> g.get(0)
6397 x == 0
6398 >>> g.get(1)
6399 y > x
6400 """
6401 return _to_expr_ref(Z3_goal_formula(self.ctx.ref(), self.goal, i), self.ctx)
6402
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 6316 of file z3py.py.

6316 def inconsistent(self):
6317 """Return `True` if `self` contains the `False` constraints.
6318
6319 >>> x, y = Ints('x y')
6320 >>> g = Goal()
6321 >>> g.inconsistent()
6322 False
6323 >>> g.add(x == 0, x == 1)
6324 >>> g
6325 [x == 0, x == 1]
6326 >>> g.inconsistent()
6327 False
6328 >>> g2 = Tactic('propagate-values')(g)[0]
6329 >>> g2.inconsistent()
6330 True
6331 """
6332 return Z3_goal_inconsistent(self.ctx.ref(), self.goal)
6333
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 6446 of file z3py.py.

6446 def insert(self, *args):
6447 """Add constraints.
6448
6449 >>> x = Int('x')
6450 >>> g = Goal()
6451 >>> g.insert(x > 0, x < 2)
6452 >>> g
6453 [x > 0, x < 2]
6454 """
6455 self.assert_exprs(*args)
6456

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

6334 def prec(self):
6335 """Return the precision (under-approximation, over-approximation, or precise) of the goal `self`.
6336
6337 >>> g = Goal()
6338 >>> g.prec() == Z3_GOAL_PRECISE
6339 True
6340 >>> x, y = Ints('x y')
6341 >>> g.add(x == y + 1)
6342 >>> g.prec() == Z3_GOAL_PRECISE
6343 True
6344 >>> t = With(Tactic('add-bounds'), add_bound_lower=0, add_bound_upper=10)
6345 >>> g2 = t(g)[0]
6346 >>> g2
6347 [x == y + 1, x <= 10, x >= 0, y <= 10, y >= 0]
6348 >>> g2.prec() == Z3_GOAL_PRECISE
6349 False
6350 >>> g2.prec() == Z3_GOAL_UNDER
6351 True
6352 """
6353 return Z3_goal_precision(self.ctx.ref(), self.goal)
6354
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 6355 of file z3py.py.

6355 def precision(self):
6356 """Alias for `prec()`.
6357
6358 >>> g = Goal()
6359 >>> g.precision() == Z3_GOAL_PRECISE
6360 True
6361 """
6362 return self.prec()
6363

◆ sexpr()

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

Definition at line 6500 of file z3py.py.

6500 def sexpr(self):
6501 """Return a textual representation of the s-expression representing the goal."""
6502 return Z3_goal_to_string(self.ctx.ref(), self.goal)
6503
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 6537 of file z3py.py.

6537 def simplify(self, *arguments, **keywords):
6538 """Return a new simplified goal.
6539
6540 This method is essentially invoking the simplify tactic.
6541
6542 >>> g = Goal()
6543 >>> x = Int('x')
6544 >>> g.add(x + 1 >= 2)
6545 >>> g
6546 [x + 1 >= 2]
6547 >>> g2 = g.simplify()
6548 >>> g2
6549 [x >= 1]
6550 >>> # g was not modified
6551 >>> g
6552 [x + 1 >= 2]
6553 """
6554 t = Tactic("simplify")
6555 return t.apply(self, *arguments, **keywords)[0]
6556

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

6364 def size(self):
6365 """Return the number of constraints in the goal `self`.
6366
6367 >>> g = Goal()
6368 >>> g.size()
6369 0
6370 >>> x, y = Ints('x y')
6371 >>> g.add(x == 0, y > x)
6372 >>> g.size()
6373 2
6374 """
6375 return int(Z3_goal_size(self.ctx.ref(), self.goal))
6376
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 6508 of file z3py.py.

6508 def translate(self, target):
6509 """Copy goal `self` to context `target`.
6510
6511 >>> x = Int('x')
6512 >>> g = Goal()
6513 >>> g.add(x > 10)
6514 >>> g
6515 [x > 10]
6516 >>> c2 = Context()
6517 >>> g2 = g.translate(c2)
6518 >>> g2
6519 [x > 10]
6520 >>> g.ctx == main_ctx()
6521 True
6522 >>> g2.ctx == c2
6523 True
6524 >>> g2.ctx == main_ctx()
6525 False
6526 """
6527 if z3_debug():
6528 _z3_assert(isinstance(target, Context), "target must be a context")
6529 return Goal(goal=Z3_goal_translate(self.ctx.ref(), self.goal, target.ref()), ctx=target)
6530
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 6288 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