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

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 5636 of file z3py.py.

5636 def __init__(self, models=True, unsat_cores=False, proofs=False, ctx=None, goal=None):
5637 if z3_debug():
5638 _z3_assert(goal is None or ctx is not None,
5639 "If goal is different from None, then ctx must be also different from None")
5640 self.ctx = _get_ctx(ctx)
5641 self.goal = goal
5642 if self.goal is None:
5643 self.goal = Z3_mk_goal(self.ctx.ref(), models, unsat_cores, proofs)
5644 Z3_goal_inc_ref(self.ctx.ref(), self.goal)
5645
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 5646 of file z3py.py.

5646 def __del__(self):
5647 if self.goal is not None and self.ctx.ref() is not None and Z3_goal_dec_ref is not None:
5648 Z3_goal_dec_ref(self.ctx.ref(), self.goal)
5649
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 5881 of file z3py.py.

5881 def __copy__(self):
5882 return self.translate(self.ctx)
5883

◆ __deepcopy__()

__deepcopy__ ( self,
memo = {} )

Definition at line 5884 of file z3py.py.

5884 def __deepcopy__(self, memo={}):
5885 return self.translate(self.ctx)
5886

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

5755 def __getitem__(self, arg):
5756 """Return a constraint in the goal `self`.
5757
5758 >>> g = Goal()
5759 >>> x, y = Ints('x y')
5760 >>> g.add(x == 0, y > x)
5761 >>> g[0]
5762 x == 0
5763 >>> g[1]
5764 y > x
5765 """
5766 if arg >= len(self):
5767 raise IndexError
5768 return self.get(arg)
5769

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

5729 def __len__(self):
5730 """Return the number of constraints in the goal `self`.
5731
5732 >>> g = Goal()
5733 >>> len(g)
5734 0
5735 >>> x, y = Ints('x y')
5736 >>> g.add(x == 0, y > x)
5737 >>> len(g)
5738 2
5739 """
5740 return self.size()
5741

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

◆ __repr__()

__repr__ ( self)

Definition at line 5847 of file z3py.py.

5847 def __repr__(self):
5848 return obj_to_string(self)
5849

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

5807 def add(self, *args):
5808 """Add constraints.
5809
5810 >>> x = Int('x')
5811 >>> g = Goal()
5812 >>> g.add(x > 0, x < 2)
5813 >>> g
5814 [x > 0, x < 2]
5815 """
5816 self.assert_exprs(*args)
5817

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

5785 def append(self, *args):
5786 """Add constraints.
5787
5788 >>> x = Int('x')
5789 >>> g = Goal()
5790 >>> g.append(x > 0, x < 2)
5791 >>> g
5792 [x > 0, x < 2]
5793 """
5794 self.assert_exprs(*args)
5795

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

5907 def as_expr(self):
5908 """Return goal `self` as a single Z3 expression.
5909
5910 >>> x = Int('x')
5911 >>> g = Goal()
5912 >>> g.as_expr()
5913 True
5914 >>> g.add(x > 1)
5915 >>> g.as_expr()
5916 x > 1
5917 >>> g.add(x < 10)
5918 >>> g.as_expr()
5919 And(x > 1, x < 10)
5920 """
5921 sz = len(self)
5922 if sz == 0:
5923 return BoolVal(True, self.ctx)
5924 elif sz == 1:
5925 return self.get(0)
5926 else:
5927 return And([self.get(i) for i in range(len(self))], self.ctx)
5928

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

5770 def assert_exprs(self, *args):
5771 """Assert constraints into the goal.
5772
5773 >>> x = Int('x')
5774 >>> g = Goal()
5775 >>> g.assert_exprs(x > 0, x < 2)
5776 >>> g
5777 [x > 0, x < 2]
5778 """
5779 args = _get_args(args)
5780 s = BoolSort(self.ctx)
5781 for arg in args:
5782 arg = s.cast(arg)
5783 Z3_goal_assert(self.ctx.ref(), self.goal, arg.as_ast())
5784
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 5818 of file z3py.py.

5818 def convert_model(self, model):
5819 """Retrieve model from a satisfiable goal
5820 >>> a, b = Ints('a b')
5821 >>> g = Goal()
5822 >>> g.add(Or(a == 0, a == 1), Or(b == 0, b == 1), a > b)
5823 >>> t = Then(Tactic('split-clause'), Tactic('solve-eqs'))
5824 >>> r = t(g)
5825 >>> r[0]
5826 [Or(b == 0, b == 1), Not(0 <= b)]
5827 >>> r[1]
5828 [Or(b == 0, b == 1), Not(1 <= b)]
5829 >>> # Remark: the subgoal r[0] is unsatisfiable
5830 >>> # Creating a solver for solving the second subgoal
5831 >>> s = Solver()
5832 >>> s.add(r[1])
5833 >>> s.check()
5834 sat
5835 >>> s.model()
5836 [b = 0]
5837 >>> # Model s.model() does not assign a value to `a`
5838 >>> # It is a model for subgoal `r[1]`, but not for goal `g`
5839 >>> # The method convert_model creates a model for `g` from a model for `r[1]`.
5840 >>> r[1].convert_model(s.model())
5841 [b = 0, a = 1]
5842 """
5843 if z3_debug():
5844 _z3_assert(isinstance(model, ModelRef), "Z3 Model expected")
5845 return ModelRef(Z3_goal_convert_model(self.ctx.ref(), self.goal, model.model), self.ctx)
5846
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 5650 of file z3py.py.

5650 def depth(self):
5651 """Return the depth of the goal `self`.
5652 The depth corresponds to the number of tactics applied to `self`.
5653
5654 >>> x, y = Ints('x y')
5655 >>> g = Goal()
5656 >>> g.add(x == 0, y >= x + 1)
5657 >>> g.depth()
5658 0
5659 >>> r = Then('simplify', 'solve-eqs')(g)
5660 >>> # r has 1 subgoal
5661 >>> len(r)
5662 1
5663 >>> r[0].depth()
5664 2
5665 """
5666 return int(Z3_goal_depth(self.ctx.ref(), self.goal))
5667
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 5854 of file z3py.py.

5854 def dimacs(self, include_names=True):
5855 """Return a textual representation of the goal in DIMACS format."""
5856 return Z3_goal_to_dimacs_string(self.ctx.ref(), self.goal, include_names)
5857
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 5742 of file z3py.py.

5742 def get(self, i):
5743 """Return a constraint in the goal `self`.
5744
5745 >>> g = Goal()
5746 >>> x, y = Ints('x y')
5747 >>> g.add(x == 0, y > x)
5748 >>> g.get(0)
5749 x == 0
5750 >>> g.get(1)
5751 y > x
5752 """
5753 return _to_expr_ref(Z3_goal_formula(self.ctx.ref(), self.goal, i), self.ctx)
5754
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 5668 of file z3py.py.

5668 def inconsistent(self):
5669 """Return `True` if `self` contains the `False` constraints.
5670
5671 >>> x, y = Ints('x y')
5672 >>> g = Goal()
5673 >>> g.inconsistent()
5674 False
5675 >>> g.add(x == 0, x == 1)
5676 >>> g
5677 [x == 0, x == 1]
5678 >>> g.inconsistent()
5679 False
5680 >>> g2 = Tactic('propagate-values')(g)[0]
5681 >>> g2.inconsistent()
5682 True
5683 """
5684 return Z3_goal_inconsistent(self.ctx.ref(), self.goal)
5685
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 5796 of file z3py.py.

5796 def insert(self, *args):
5797 """Add constraints.
5798
5799 >>> x = Int('x')
5800 >>> g = Goal()
5801 >>> g.insert(x > 0, x < 2)
5802 >>> g
5803 [x > 0, x < 2]
5804 """
5805 self.assert_exprs(*args)
5806

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

5686 def prec(self):
5687 """Return the precision (under-approximation, over-approximation, or precise) of the goal `self`.
5688
5689 >>> g = Goal()
5690 >>> g.prec() == Z3_GOAL_PRECISE
5691 True
5692 >>> x, y = Ints('x y')
5693 >>> g.add(x == y + 1)
5694 >>> g.prec() == Z3_GOAL_PRECISE
5695 True
5696 >>> t = With(Tactic('add-bounds'), add_bound_lower=0, add_bound_upper=10)
5697 >>> g2 = t(g)[0]
5698 >>> g2
5699 [x == y + 1, x <= 10, x >= 0, y <= 10, y >= 0]
5700 >>> g2.prec() == Z3_GOAL_PRECISE
5701 False
5702 >>> g2.prec() == Z3_GOAL_UNDER
5703 True
5704 """
5705 return Z3_goal_precision(self.ctx.ref(), self.goal)
5706
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 5707 of file z3py.py.

5707 def precision(self):
5708 """Alias for `prec()`.
5709
5710 >>> g = Goal()
5711 >>> g.precision() == Z3_GOAL_PRECISE
5712 True
5713 """
5714 return self.prec()
5715

◆ sexpr()

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

Definition at line 5850 of file z3py.py.

5850 def sexpr(self):
5851 """Return a textual representation of the s-expression representing the goal."""
5852 return Z3_goal_to_string(self.ctx.ref(), self.goal)
5853
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 5887 of file z3py.py.

5887 def simplify(self, *arguments, **keywords):
5888 """Return a new simplified goal.
5889
5890 This method is essentially invoking the simplify tactic.
5891
5892 >>> g = Goal()
5893 >>> x = Int('x')
5894 >>> g.add(x + 1 >= 2)
5895 >>> g
5896 [x + 1 >= 2]
5897 >>> g2 = g.simplify()
5898 >>> g2
5899 [x >= 1]
5900 >>> # g was not modified
5901 >>> g
5902 [x + 1 >= 2]
5903 """
5904 t = Tactic("simplify")
5905 return t.apply(self, *arguments, **keywords)[0]
5906

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

5716 def size(self):
5717 """Return the number of constraints in the goal `self`.
5718
5719 >>> g = Goal()
5720 >>> g.size()
5721 0
5722 >>> x, y = Ints('x y')
5723 >>> g.add(x == 0, y > x)
5724 >>> g.size()
5725 2
5726 """
5727 return int(Z3_goal_size(self.ctx.ref(), self.goal))
5728
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 5858 of file z3py.py.

5858 def translate(self, target):
5859 """Copy goal `self` to context `target`.
5860
5861 >>> x = Int('x')
5862 >>> g = Goal()
5863 >>> g.add(x > 10)
5864 >>> g
5865 [x > 10]
5866 >>> c2 = Context()
5867 >>> g2 = g.translate(c2)
5868 >>> g2
5869 [x > 10]
5870 >>> g.ctx == main_ctx()
5871 True
5872 >>> g2.ctx == c2
5873 True
5874 >>> g2.ctx == main_ctx()
5875 False
5876 """
5877 if z3_debug():
5878 _z3_assert(isinstance(target, Context), "target must be a context")
5879 return Goal(goal=Z3_goal_translate(self.ctx.ref(), self.goal, target.ref()), ctx=target)
5880
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 5640 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