Z3
Public Member Functions
ExprRef Class Reference

Expressions. More...

+ Inheritance diagram for ExprRef:

Public Member Functions

def as_ast (self)
 
def get_id (self)
 
def sort (self)
 
def sort_kind (self)
 
def __eq__ (self, other)
 
def __hash__ (self)
 
def __ne__ (self, other)
 
def params (self)
 
def decl (self)
 
def num_args (self)
 
def arg (self, idx)
 
def children (self)
 
- Public Member Functions inherited from AstRef
def __init__ (self, ast, ctx=None)
 
def __del__ (self)
 
def __deepcopy__ (self, memo={})
 
def __str__ (self)
 
def __repr__ (self)
 
def __nonzero__ (self)
 
def __bool__ (self)
 
def sexpr (self)
 
def ctx_ref (self)
 
def eq (self, other)
 
def translate (self, target)
 
def __copy__ (self)
 
def hash (self)
 
- Public Member Functions inherited from Z3PPObject
def use_pp (self)
 

Additional Inherited Members

- Data Fields inherited from AstRef
 ast
 
 ctx
 

Detailed Description

Expressions.

Constraints, formulas and terms are expressions in Z3.

Expressions are ASTs. Every expression has a sort.
There are three main kinds of expressions:
function applications, quantifiers and bounded variables.
A constant is a function application with 0 arguments.
For quantifier free problems, all expressions are
function applications.

Definition at line 875 of file z3py.py.

Member Function Documentation

◆ __eq__()

def __eq__ (   self,
  other 
)
Return a Z3 expression that represents the constraint `self == other`.

If `other` is `None`, then this method simply returns `False`.

>>> a = Int('a')
>>> b = Int('b')
>>> a == b
a == b
>>> a is None
False

Reimplemented from AstRef.

Definition at line 914 of file z3py.py.

914  def __eq__(self, other):
915  """Return a Z3 expression that represents the constraint `self == other`.
916 
917  If `other` is `None`, then this method simply returns `False`.
918 
919  >>> a = Int('a')
920  >>> b = Int('b')
921  >>> a == b
922  a == b
923  >>> a is None
924  False
925  """
926  if other is None:
927  return False
928  a, b = _coerce_exprs(self, other)
929  return BoolRef(Z3_mk_eq(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
930 
Z3_ast Z3_API Z3_mk_eq(Z3_context c, Z3_ast l, Z3_ast r)
Create an AST node representing l = r.

Referenced by CheckSatResult.__ne__(), and Probe.__ne__().

◆ __hash__()

def __hash__ (   self)
Hash code. 

Reimplemented from AstRef.

Definition at line 931 of file z3py.py.

931  def __hash__(self):
932  """ Hash code. """
933  return AstRef.__hash__(self)
934 

◆ __ne__()

def __ne__ (   self,
  other 
)
Return a Z3 expression that represents the constraint `self != other`.

If `other` is `None`, then this method simply returns `True`.

>>> a = Int('a')
>>> b = Int('b')
>>> a != b
a != b
>>> a is not None
True

Definition at line 935 of file z3py.py.

935  def __ne__(self, other):
936  """Return a Z3 expression that represents the constraint `self != other`.
937 
938  If `other` is `None`, then this method simply returns `True`.
939 
940  >>> a = Int('a')
941  >>> b = Int('b')
942  >>> a != b
943  a != b
944  >>> a is not None
945  True
946  """
947  if other is None:
948  return True
949  a, b = _coerce_exprs(self, other)
950  _args, sz = _to_ast_array((a, b))
951  return BoolRef(Z3_mk_distinct(self.ctx_ref(), 2, _args), self.ctx)
952 
Z3_ast Z3_API Z3_mk_distinct(Z3_context c, unsigned num_args, Z3_ast const args[])
Create an AST node representing distinct(args[0], ..., args[num_args-1]).

◆ arg()

def arg (   self,
  idx 
)
Return argument `idx` of the application `self`.

This method assumes that `self` is a function application with at least `idx+1` arguments.

>>> a = Int('a')
>>> b = Int('b')
>>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
>>> t = f(a, b, 0)
>>> t.arg(0)
a
>>> t.arg(1)
b
>>> t.arg(2)
0

Definition at line 987 of file z3py.py.

987  def arg(self, idx):
988  """Return argument `idx` of the application `self`.
989 
990  This method assumes that `self` is a function application with at least `idx+1` arguments.
991 
992  >>> a = Int('a')
993  >>> b = Int('b')
994  >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
995  >>> t = f(a, b, 0)
996  >>> t.arg(0)
997  a
998  >>> t.arg(1)
999  b
1000  >>> t.arg(2)
1001  0
1002  """
1003  if z3_debug():
1004  _z3_assert(is_app(self), "Z3 application expected")
1005  _z3_assert(idx < self.num_args(), "Invalid argument index")
1006  return _to_expr_ref(Z3_get_app_arg(self.ctx_ref(), self.as_ast(), idx), self.ctx)
1007 
def is_app(a)
Definition: z3py.py:1139
Z3_ast Z3_API Z3_get_app_arg(Z3_context c, Z3_app a, unsigned i)
Return the i-th argument of the given application.
def z3_debug()
Definition: z3py.py:58

Referenced by AstRef.__bool__(), and ExprRef.children().

◆ as_ast()

def as_ast (   self)
Return a pointer to the corresponding C Z3_ast object.

Reimplemented from AstRef.

Reimplemented in QuantifierRef, and PatternRef.

Definition at line 885 of file z3py.py.

885  def as_ast(self):
886  return self.ast
887 

◆ children()

def children (   self)
Return a list containing the children of the given expression

>>> a = Int('a')
>>> b = Int('b')
>>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
>>> t = f(a, b, 0)
>>> t.children()
[a, b, 0]

Reimplemented in QuantifierRef.

Definition at line 1008 of file z3py.py.

1008  def children(self):
1009  """Return a list containing the children of the given expression
1010 
1011  >>> a = Int('a')
1012  >>> b = Int('b')
1013  >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
1014  >>> t = f(a, b, 0)
1015  >>> t.children()
1016  [a, b, 0]
1017  """
1018  if is_app(self):
1019  return [self.arg(i) for i in range(self.num_args())]
1020  else:
1021  return []
1022 
expr range(expr const &lo, expr const &hi)
Definition: z3++.h:3370
def is_app(a)
Definition: z3py.py:1139

◆ decl()

def decl (   self)
Return the Z3 function declaration associated with a Z3 application.

>>> f = Function('f', IntSort(), IntSort())
>>> a = Int('a')
>>> t = f(a)
>>> eq(t.decl(), f)
True
>>> (a + 1).decl()
+

Definition at line 956 of file z3py.py.

956  def decl(self):
957  """Return the Z3 function declaration associated with a Z3 application.
958 
959  >>> f = Function('f', IntSort(), IntSort())
960  >>> a = Int('a')
961  >>> t = f(a)
962  >>> eq(t.decl(), f)
963  True
964  >>> (a + 1).decl()
965  +
966  """
967  if z3_debug():
968  _z3_assert(is_app(self), "Z3 application expected")
969  return FuncDeclRef(Z3_get_app_decl(self.ctx_ref(), self.as_ast()), self.ctx)
970 
def is_app(a)
Definition: z3py.py:1139
Z3_func_decl Z3_API Z3_get_app_decl(Z3_context c, Z3_app a)
Return the declaration of a constant or function application.
def z3_debug()
Definition: z3py.py:58

Referenced by ExprRef.params().

◆ get_id()

def get_id (   self)
Return unique identifier for object. It can be used for hash-tables and maps.

Reimplemented from AstRef.

Reimplemented in QuantifierRef, and PatternRef.

Definition at line 888 of file z3py.py.

888  def get_id(self):
889  return Z3_get_ast_id(self.ctx_ref(), self.as_ast())
890 
unsigned Z3_API Z3_get_ast_id(Z3_context c, Z3_ast t)
Return a unique identifier for t. The identifier is unique up to structural equality....

◆ num_args()

def num_args (   self)
Return the number of arguments of a Z3 application.

>>> a = Int('a')
>>> b = Int('b')
>>> (a + b).num_args()
2
>>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
>>> t = f(a, b, 0)
>>> t.num_args()
3

Definition at line 971 of file z3py.py.

971  def num_args(self):
972  """Return the number of arguments of a Z3 application.
973 
974  >>> a = Int('a')
975  >>> b = Int('b')
976  >>> (a + b).num_args()
977  2
978  >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
979  >>> t = f(a, b, 0)
980  >>> t.num_args()
981  3
982  """
983  if z3_debug():
984  _z3_assert(is_app(self), "Z3 application expected")
985  return int(Z3_get_app_num_args(self.ctx_ref(), self.as_ast()))
986 
def is_app(a)
Definition: z3py.py:1139
unsigned Z3_API Z3_get_app_num_args(Z3_context c, Z3_app a)
Return the number of argument of an application. If t is an constant, then the number of arguments is...
def z3_debug()
Definition: z3py.py:58

Referenced by AstRef.__bool__(), ExprRef.arg(), FuncEntry.arg_value(), FuncEntry.as_list(), and ExprRef.children().

◆ params()

def params (   self)

Definition at line 953 of file z3py.py.

953  def params(self):
954  return self.decl().params()
955 

◆ sort()

def sort (   self)
Return the sort of expression `self`.

>>> x = Int('x')
>>> (x + 1).sort()
Int
>>> y = Real('y')
>>> (x + y).sort()
Real

Reimplemented in SeqRef, FPRef, FiniteDomainRef, DatatypeRef, ArrayRef, BitVecRef, ArithRef, QuantifierRef, and BoolRef.

Definition at line 891 of file z3py.py.

891  def sort(self):
892  """Return the sort of expression `self`.
893 
894  >>> x = Int('x')
895  >>> (x + 1).sort()
896  Int
897  >>> y = Real('y')
898  >>> (x + y).sort()
899  Real
900  """
901  return _sort(self.ctx, self.as_ast())
902 

Referenced by QuantifierRef.__getitem__(), FPNumRef.as_string(), ArrayRef.domain(), FPRef.ebits(), ArithRef.is_int(), ArithRef.is_real(), ArrayRef.range(), FPRef.sbits(), BitVecRef.size(), and ExprRef.sort_kind().

◆ sort_kind()

def sort_kind (   self)
Shorthand for `self.sort().kind()`.

>>> a = Array('a', IntSort(), IntSort())
>>> a.sort_kind() == Z3_ARRAY_SORT
True
>>> a.sort_kind() == Z3_INT_SORT
False

Definition at line 903 of file z3py.py.

903  def sort_kind(self):
904  """Shorthand for `self.sort().kind()`.
905 
906  >>> a = Array('a', IntSort(), IntSort())
907  >>> a.sort_kind() == Z3_ARRAY_SORT
908  True
909  >>> a.sort_kind() == Z3_INT_SORT
910  False
911  """
912  return self.sort().kind()
913