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

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 5769 of file z3py.py.

5769 def __init__(self, models=True, unsat_cores=False, proofs=False, ctx=None, goal=None):
5770 if z3_debug():
5771 _z3_assert(goal is None or ctx is not None,
5772 "If goal is different from None, then ctx must be also different from None")
5773 self.ctx = _get_ctx(ctx)
5774 self.goal = goal
5775 if self.goal is None:
5776 self.goal = Z3_mk_goal(self.ctx.ref(), models, unsat_cores, proofs)
5777 Z3_goal_inc_ref(self.ctx.ref(), self.goal)
5778
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 5779 of file z3py.py.

5779 def __del__(self):
5780 if self.goal is not None and self.ctx.ref() is not None and Z3_goal_dec_ref is not None:
5781 Z3_goal_dec_ref(self.ctx.ref(), self.goal)
5782
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 6014 of file z3py.py.

6014 def __copy__(self):
6015 return self.translate(self.ctx)
6016

◆ __deepcopy__()

__deepcopy__ ( self,
memo = {} )

Definition at line 6017 of file z3py.py.

6017 def __deepcopy__(self, memo={}):
6018 return self.translate(self.ctx)
6019

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

5888 def __getitem__(self, arg):
5889 """Return a constraint in the goal `self`.
5890
5891 >>> g = Goal()
5892 >>> x, y = Ints('x y')
5893 >>> g.add(x == 0, y > x)
5894 >>> g[0]
5895 x == 0
5896 >>> g[1]
5897 y > x
5898 """
5899 if arg >= len(self):
5900 raise IndexError
5901 return self.get(arg)
5902

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

5862 def __len__(self):
5863 """Return the number of constraints in the goal `self`.
5864
5865 >>> g = Goal()
5866 >>> len(g)
5867 0
5868 >>> x, y = Ints('x y')
5869 >>> g.add(x == 0, y > x)
5870 >>> len(g)
5871 2
5872 """
5873 return self.size()
5874

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

◆ __repr__()

__repr__ ( self)

Definition at line 5980 of file z3py.py.

5980 def __repr__(self):
5981 return obj_to_string(self)
5982

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

5940 def add(self, *args):
5941 """Add constraints.
5942
5943 >>> x = Int('x')
5944 >>> g = Goal()
5945 >>> g.add(x > 0, x < 2)
5946 >>> g
5947 [x > 0, x < 2]
5948 """
5949 self.assert_exprs(*args)
5950

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

5918 def append(self, *args):
5919 """Add constraints.
5920
5921 >>> x = Int('x')
5922 >>> g = Goal()
5923 >>> g.append(x > 0, x < 2)
5924 >>> g
5925 [x > 0, x < 2]
5926 """
5927 self.assert_exprs(*args)
5928

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

6040 def as_expr(self):
6041 """Return goal `self` as a single Z3 expression.
6042
6043 >>> x = Int('x')
6044 >>> g = Goal()
6045 >>> g.as_expr()
6046 True
6047 >>> g.add(x > 1)
6048 >>> g.as_expr()
6049 x > 1
6050 >>> g.add(x < 10)
6051 >>> g.as_expr()
6052 And(x > 1, x < 10)
6053 """
6054 sz = len(self)
6055 if sz == 0:
6056 return BoolVal(True, self.ctx)
6057 elif sz == 1:
6058 return self.get(0)
6059 else:
6060 return And([self.get(i) for i in range(len(self))], self.ctx)
6061

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

5903 def assert_exprs(self, *args):
5904 """Assert constraints into the goal.
5905
5906 >>> x = Int('x')
5907 >>> g = Goal()
5908 >>> g.assert_exprs(x > 0, x < 2)
5909 >>> g
5910 [x > 0, x < 2]
5911 """
5912 args = _get_args(args)
5913 s = BoolSort(self.ctx)
5914 for arg in args:
5915 arg = s.cast(arg)
5916 Z3_goal_assert(self.ctx.ref(), self.goal, arg.as_ast())
5917
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 5951 of file z3py.py.

5951 def convert_model(self, model):
5952 """Retrieve model from a satisfiable goal
5953 >>> a, b = Ints('a b')
5954 >>> g = Goal()
5955 >>> g.add(Or(a == 0, a == 1), Or(b == 0, b == 1), a > b)
5956 >>> t = Then(Tactic('split-clause'), Tactic('solve-eqs'))
5957 >>> r = t(g)
5958 >>> r[0]
5959 [Or(b == 0, b == 1), Not(0 <= b)]
5960 >>> r[1]
5961 [Or(b == 0, b == 1), Not(1 <= b)]
5962 >>> # Remark: the subgoal r[0] is unsatisfiable
5963 >>> # Creating a solver for solving the second subgoal
5964 >>> s = Solver()
5965 >>> s.add(r[1])
5966 >>> s.check()
5967 sat
5968 >>> s.model()
5969 [b = 0]
5970 >>> # Model s.model() does not assign a value to `a`
5971 >>> # It is a model for subgoal `r[1]`, but not for goal `g`
5972 >>> # The method convert_model creates a model for `g` from a model for `r[1]`.
5973 >>> r[1].convert_model(s.model())
5974 [b = 0, a = 1]
5975 """
5976 if z3_debug():
5977 _z3_assert(isinstance(model, ModelRef), "Z3 Model expected")
5978 return ModelRef(Z3_goal_convert_model(self.ctx.ref(), self.goal, model.model), self.ctx)
5979
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 5783 of file z3py.py.

5783 def depth(self):
5784 """Return the depth of the goal `self`.
5785 The depth corresponds to the number of tactics applied to `self`.
5786
5787 >>> x, y = Ints('x y')
5788 >>> g = Goal()
5789 >>> g.add(x == 0, y >= x + 1)
5790 >>> g.depth()
5791 0
5792 >>> r = Then('simplify', 'solve-eqs')(g)
5793 >>> # r has 1 subgoal
5794 >>> len(r)
5795 1
5796 >>> r[0].depth()
5797 2
5798 """
5799 return int(Z3_goal_depth(self.ctx.ref(), self.goal))
5800
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 5987 of file z3py.py.

5987 def dimacs(self, include_names=True):
5988 """Return a textual representation of the goal in DIMACS format."""
5989 return Z3_goal_to_dimacs_string(self.ctx.ref(), self.goal, include_names)
5990
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 5875 of file z3py.py.

5875 def get(self, i):
5876 """Return a constraint in the goal `self`.
5877
5878 >>> g = Goal()
5879 >>> x, y = Ints('x y')
5880 >>> g.add(x == 0, y > x)
5881 >>> g.get(0)
5882 x == 0
5883 >>> g.get(1)
5884 y > x
5885 """
5886 return _to_expr_ref(Z3_goal_formula(self.ctx.ref(), self.goal, i), self.ctx)
5887
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 5801 of file z3py.py.

5801 def inconsistent(self):
5802 """Return `True` if `self` contains the `False` constraints.
5803
5804 >>> x, y = Ints('x y')
5805 >>> g = Goal()
5806 >>> g.inconsistent()
5807 False
5808 >>> g.add(x == 0, x == 1)
5809 >>> g
5810 [x == 0, x == 1]
5811 >>> g.inconsistent()
5812 False
5813 >>> g2 = Tactic('propagate-values')(g)[0]
5814 >>> g2.inconsistent()
5815 True
5816 """
5817 return Z3_goal_inconsistent(self.ctx.ref(), self.goal)
5818
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 5929 of file z3py.py.

5929 def insert(self, *args):
5930 """Add constraints.
5931
5932 >>> x = Int('x')
5933 >>> g = Goal()
5934 >>> g.insert(x > 0, x < 2)
5935 >>> g
5936 [x > 0, x < 2]
5937 """
5938 self.assert_exprs(*args)
5939

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

5819 def prec(self):
5820 """Return the precision (under-approximation, over-approximation, or precise) of the goal `self`.
5821
5822 >>> g = Goal()
5823 >>> g.prec() == Z3_GOAL_PRECISE
5824 True
5825 >>> x, y = Ints('x y')
5826 >>> g.add(x == y + 1)
5827 >>> g.prec() == Z3_GOAL_PRECISE
5828 True
5829 >>> t = With(Tactic('add-bounds'), add_bound_lower=0, add_bound_upper=10)
5830 >>> g2 = t(g)[0]
5831 >>> g2
5832 [x == y + 1, x <= 10, x >= 0, y <= 10, y >= 0]
5833 >>> g2.prec() == Z3_GOAL_PRECISE
5834 False
5835 >>> g2.prec() == Z3_GOAL_UNDER
5836 True
5837 """
5838 return Z3_goal_precision(self.ctx.ref(), self.goal)
5839
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 5840 of file z3py.py.

5840 def precision(self):
5841 """Alias for `prec()`.
5842
5843 >>> g = Goal()
5844 >>> g.precision() == Z3_GOAL_PRECISE
5845 True
5846 """
5847 return self.prec()
5848

◆ sexpr()

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

Definition at line 5983 of file z3py.py.

5983 def sexpr(self):
5984 """Return a textual representation of the s-expression representing the goal."""
5985 return Z3_goal_to_string(self.ctx.ref(), self.goal)
5986
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 6020 of file z3py.py.

6020 def simplify(self, *arguments, **keywords):
6021 """Return a new simplified goal.
6022
6023 This method is essentially invoking the simplify tactic.
6024
6025 >>> g = Goal()
6026 >>> x = Int('x')
6027 >>> g.add(x + 1 >= 2)
6028 >>> g
6029 [x + 1 >= 2]
6030 >>> g2 = g.simplify()
6031 >>> g2
6032 [x >= 1]
6033 >>> # g was not modified
6034 >>> g
6035 [x + 1 >= 2]
6036 """
6037 t = Tactic("simplify")
6038 return t.apply(self, *arguments, **keywords)[0]
6039

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

5849 def size(self):
5850 """Return the number of constraints in the goal `self`.
5851
5852 >>> g = Goal()
5853 >>> g.size()
5854 0
5855 >>> x, y = Ints('x y')
5856 >>> g.add(x == 0, y > x)
5857 >>> g.size()
5858 2
5859 """
5860 return int(Z3_goal_size(self.ctx.ref(), self.goal))
5861
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 5991 of file z3py.py.

5991 def translate(self, target):
5992 """Copy goal `self` to context `target`.
5993
5994 >>> x = Int('x')
5995 >>> g = Goal()
5996 >>> g.add(x > 10)
5997 >>> g
5998 [x > 10]
5999 >>> c2 = Context()
6000 >>> g2 = g.translate(c2)
6001 >>> g2
6002 [x > 10]
6003 >>> g.ctx == main_ctx()
6004 True
6005 >>> g2.ctx == c2
6006 True
6007 >>> g2.ctx == main_ctx()
6008 False
6009 """
6010 if z3_debug():
6011 _z3_assert(isinstance(target, Context), "target must be a context")
6012 return Goal(goal=Z3_goal_translate(self.ctx.ref(), self.goal, target.ref()), ctx=target)
6013
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 5773 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