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

912  def __eq__(self, other):
913  """Return a Z3 expression that represents the constraint `self == other`.
914 
915  If `other` is `None`, then this method simply returns `False`.
916 
917  >>> a = Int('a')
918  >>> b = Int('b')
919  >>> a == b
920  a == b
921  >>> a is None
922  False
923  """
924  if other is None:
925  return False
926  a, b = _coerce_exprs(self, other)
927  return BoolRef(Z3_mk_eq(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
928 
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 929 of file z3py.py.

929  def __hash__(self):
930  """ Hash code. """
931  return AstRef.__hash__(self)
932 

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

933  def __ne__(self, other):
934  """Return a Z3 expression that represents the constraint `self != other`.
935 
936  If `other` is `None`, then this method simply returns `True`.
937 
938  >>> a = Int('a')
939  >>> b = Int('b')
940  >>> a != b
941  a != b
942  >>> a is not None
943  True
944  """
945  if other is None:
946  return True
947  a, b = _coerce_exprs(self, other)
948  _args, sz = _to_ast_array((a, b))
949  return BoolRef(Z3_mk_distinct(self.ctx_ref(), 2, _args), self.ctx)
950 
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 985 of file z3py.py.

985  def arg(self, idx):
986  """Return argument `idx` of the application `self`.
987 
988  This method assumes that `self` is a function application with at least `idx+1` arguments.
989 
990  >>> a = Int('a')
991  >>> b = Int('b')
992  >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
993  >>> t = f(a, b, 0)
994  >>> t.arg(0)
995  a
996  >>> t.arg(1)
997  b
998  >>> t.arg(2)
999  0
1000  """
1001  if z3_debug():
1002  _z3_assert(is_app(self), "Z3 application expected")
1003  _z3_assert(idx < self.num_args(), "Invalid argument index")
1004  return _to_expr_ref(Z3_get_app_arg(self.ctx_ref(), self.as_ast(), idx), self.ctx)
1005 
def is_app(a)
Definition: z3py.py:1137
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:56

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

883  def as_ast(self):
884  return self.ast
885 

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

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

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

954  def decl(self):
955  """Return the Z3 function declaration associated with a Z3 application.
956 
957  >>> f = Function('f', IntSort(), IntSort())
958  >>> a = Int('a')
959  >>> t = f(a)
960  >>> eq(t.decl(), f)
961  True
962  >>> (a + 1).decl()
963  +
964  """
965  if z3_debug():
966  _z3_assert(is_app(self), "Z3 application expected")
967  return FuncDeclRef(Z3_get_app_decl(self.ctx_ref(), self.as_ast()), self.ctx)
968 
def is_app(a)
Definition: z3py.py:1137
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:56

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

886  def get_id(self):
887  return Z3_get_ast_id(self.ctx_ref(), self.as_ast())
888 
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 969 of file z3py.py.

969  def num_args(self):
970  """Return the number of arguments of a Z3 application.
971 
972  >>> a = Int('a')
973  >>> b = Int('b')
974  >>> (a + b).num_args()
975  2
976  >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
977  >>> t = f(a, b, 0)
978  >>> t.num_args()
979  3
980  """
981  if z3_debug():
982  _z3_assert(is_app(self), "Z3 application expected")
983  return int(Z3_get_app_num_args(self.ctx_ref(), self.as_ast()))
984 
def is_app(a)
Definition: z3py.py:1137
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:56

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

◆ params()

def params (   self)

Definition at line 951 of file z3py.py.

951  def params(self):
952  return self.decl().params()
953 

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

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

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

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