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 kind (self)
 
def num_args (self)
 
def arg (self, idx)
 
def children (self)
 
def update (self, args)
 
def from_string (self, s)
 
def serialize (self)
 
- Public Member Functions inherited from AstRef
def __init__
 
def __del__ (self)
 
def __deepcopy__
 
def __str__ (self)
 
def __repr__ (self)
 
def __eq__ (self, other)
 
def __hash__ (self)
 
def __nonzero__ (self)
 
def __bool__ (self)
 
def sexpr (self)
 
def as_ast (self)
 
def get_id (self)
 
def ctx_ref (self)
 
def eq (self, other)
 
def translate (self, target)
 
def __copy__ (self)
 
def hash (self)
 
def py_value (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 1018 of file z3py.py.

Member Function Documentation

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

Definition at line 1058 of file z3py.py.

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

1058  def __eq__(self, other):
1059  """Return a Z3 expression that represents the constraint `self == other`.
1060 
1061  If `other` is `None`, then this method simply returns `False`.
1062 
1063  >>> a = Int('a')
1064  >>> b = Int('b')
1065  >>> a == b
1066  a == b
1067  >>> a is None
1068  False
1069  """
1070  if other is None:
1071  return False
1072  a, b = _coerce_exprs(self, other)
1073  return BoolRef(Z3_mk_eq(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
1074 
def __eq__(self, other)
Definition: z3py.py:1058
def ctx_ref(self)
Definition: z3py.py:427
Z3_ast Z3_API Z3_mk_eq(Z3_context c, Z3_ast l, Z3_ast r)
Create an AST node representing l = r.
def __hash__ (   self)
Hash code. 

Definition at line 1075 of file z3py.py.

1075  def __hash__(self):
1076  """ Hash code. """
1077  return AstRef.__hash__(self)
1078 
def __hash__(self)
Definition: z3py.py:1075
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 1079 of file z3py.py.

1079  def __ne__(self, other):
1080  """Return a Z3 expression that represents the constraint `self != other`.
1081 
1082  If `other` is `None`, then this method simply returns `True`.
1083 
1084  >>> a = Int('a')
1085  >>> b = Int('b')
1086  >>> a != b
1087  a != b
1088  >>> a is not None
1089  True
1090  """
1091  if other is None:
1092  return True
1093  a, b = _coerce_exprs(self, other)
1094  _args, sz = _to_ast_array((a, b))
1095  return BoolRef(Z3_mk_distinct(self.ctx_ref(), 2, _args), self.ctx)
1096 
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]).
def __ne__(self, other)
Definition: z3py.py:1079
def ctx_ref(self)
Definition: z3py.py:427
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 1138 of file z3py.py.

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

1138  def arg(self, idx):
1139  """Return argument `idx` of the application `self`.
1140 
1141  This method assumes that `self` is a function application with at least `idx+1` arguments.
1142 
1143  >>> a = Int('a')
1144  >>> b = Int('b')
1145  >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
1146  >>> t = f(a, b, 0)
1147  >>> t.arg(0)
1148  a
1149  >>> t.arg(1)
1150  b
1151  >>> t.arg(2)
1152  0
1153  """
1154  if z3_debug():
1155  _z3_assert(is_app(self), "Z3 application expected")
1156  _z3_assert(idx < self.num_args(), "Invalid argument index")
1157  return _to_expr_ref(Z3_get_app_arg(self.ctx_ref(), self.as_ast(), idx), self.ctx)
1158 
def as_ast(self)
Definition: z3py.py:419
def is_app(a)
Definition: z3py.py:1368
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 num_args(self)
Definition: z3py.py:1122
def z3_debug()
Definition: z3py.py:70
def arg(self, idx)
Definition: z3py.py:1138
def ctx_ref(self)
Definition: z3py.py:427
def as_ast (   self)

Definition at line 1029 of file z3py.py.

Referenced by ExprRef.update().

1029  def as_ast(self):
1030  return self.ast
1031 
def as_ast(self)
Definition: z3py.py:1029
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]

Definition at line 1159 of file z3py.py.

1159  def children(self):
1160  """Return a list containing the children of the given expression
1161 
1162  >>> a = Int('a')
1163  >>> b = Int('b')
1164  >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
1165  >>> t = f(a, b, 0)
1166  >>> t.children()
1167  [a, b, 0]
1168  """
1169  if is_app(self):
1170  return [self.arg(i) for i in range(self.num_args())]
1171  else:
1172  return []
1173 
expr range(expr const &lo, expr const &hi)
Definition: z3++.h:4343
def is_app(a)
Definition: z3py.py:1368
def num_args(self)
Definition: z3py.py:1122
def arg(self, idx)
Definition: z3py.py:1138
def children(self)
Definition: z3py.py:1159
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 1100 of file z3py.py.

Referenced by ExprRef.params().

1100  def decl(self):
1101  """Return the Z3 function declaration associated with a Z3 application.
1102 
1103  >>> f = Function('f', IntSort(), IntSort())
1104  >>> a = Int('a')
1105  >>> t = f(a)
1106  >>> eq(t.decl(), f)
1107  True
1108  >>> (a + 1).decl()
1109  +
1110  """
1111  if z3_debug():
1112  _z3_assert(is_app(self), "Z3 application expected")
1113  return FuncDeclRef(Z3_get_app_decl(self.ctx_ref(), self.as_ast()), self.ctx)
1114 
Function Declarations.
Definition: z3py.py:775
def as_ast(self)
Definition: z3py.py:419
def is_app(a)
Definition: z3py.py:1368
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 decl(self)
Definition: z3py.py:1100
def z3_debug()
Definition: z3py.py:70
def ctx_ref(self)
Definition: z3py.py:427
def from_string (   self,
  s 
)

Definition at line 1198 of file z3py.py.

1198  def from_string(self, s):
1199  pass
1200 
def from_string(self, s)
Definition: z3py.py:1198
def get_id (   self)

Definition at line 1032 of file z3py.py.

1032  def get_id(self):
1033  return Z3_get_ast_id(self.ctx_ref(), self.as_ast())
1034 
def as_ast(self)
Definition: z3py.py:419
def get_id(self)
Definition: z3py.py:1032
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. Thus, two ast nodes created by the same context and having the same children and same function symbols have the same identifiers. Ast nodes created in the same context, but having different children or different functions have different identifiers. Variables and quantifiers are also assigned different identifiers according to their structure.
def ctx_ref(self)
Definition: z3py.py:427
def kind (   self)
Return the Z3 internal kind of a function application.

Definition at line 1115 of file z3py.py.

Referenced by ExprRef.sort_kind().

1115  def kind(self):
1116  """Return the Z3 internal kind of a function application."""
1117  if z3_debug():
1118  _z3_assert(is_app(self), "Z3 application expected")
1119  return Z3_get_decl_kind(self.ctx_ref(), Z3_get_app_decl(self.ctx_ref(), self.ast))
1120 
1121 
Z3_decl_kind Z3_API Z3_get_decl_kind(Z3_context c, Z3_func_decl d)
Return declaration kind corresponding to declaration.
def is_app(a)
Definition: z3py.py:1368
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:70
def kind(self)
Definition: z3py.py:1115
def ctx_ref(self)
Definition: z3py.py:427
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 1122 of file z3py.py.

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

1122  def num_args(self):
1123  """Return the number of arguments of a Z3 application.
1124 
1125  >>> a = Int('a')
1126  >>> b = Int('b')
1127  >>> (a + b).num_args()
1128  2
1129  >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
1130  >>> t = f(a, b, 0)
1131  >>> t.num_args()
1132  3
1133  """
1134  if z3_debug():
1135  _z3_assert(is_app(self), "Z3 application expected")
1136  return int(Z3_get_app_num_args(self.ctx_ref(), self.as_ast()))
1137 
def as_ast(self)
Definition: z3py.py:419
def is_app(a)
Definition: z3py.py:1368
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 num_args(self)
Definition: z3py.py:1122
def z3_debug()
Definition: z3py.py:70
def ctx_ref(self)
Definition: z3py.py:427
def params (   self)

Definition at line 1097 of file z3py.py.

1097  def params(self):
1098  return self.decl().params()
1099 
def decl(self)
Definition: z3py.py:1100
def params(self)
Definition: z3py.py:1097
def serialize (   self)

Definition at line 1201 of file z3py.py.

1201  def serialize(self):
1202  s = Solver()
1203  f = Function('F', self.sort(), BoolSort(self.ctx))
1204  s.add(f(self))
1205  return s.sexpr()
1206 
def BoolSort
Definition: z3py.py:1824
def Function(name, sig)
Definition: z3py.py:920
def serialize(self)
Definition: z3py.py:1201
def sort(self)
Definition: z3py.py:1035
def sort (   self)
Return the sort of expression `self`.

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

Definition at line 1035 of file z3py.py.

Referenced by ArrayRef.domain(), ArrayRef.domain_n(), ArithRef.is_int(), ArithRef.is_real(), ArrayRef.range(), ExprRef.serialize(), BitVecRef.size(), and ExprRef.sort_kind().

1035  def sort(self):
1036  """Return the sort of expression `self`.
1037 
1038  >>> x = Int('x')
1039  >>> (x + 1).sort()
1040  Int
1041  >>> y = Real('y')
1042  >>> (x + y).sort()
1043  Real
1044  """
1045  return _sort(self.ctx, self.as_ast())
1046 
def as_ast(self)
Definition: z3py.py:419
def sort(self)
Definition: z3py.py:1035
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 1047 of file z3py.py.

1047  def sort_kind(self):
1048  """Shorthand for `self.sort().kind()`.
1049 
1050  >>> a = Array('a', IntSort(), IntSort())
1051  >>> a.sort_kind() == Z3_ARRAY_SORT
1052  True
1053  >>> a.sort_kind() == Z3_INT_SORT
1054  False
1055  """
1056  return self.sort().kind()
1057 
def sort_kind(self)
Definition: z3py.py:1047
def sort(self)
Definition: z3py.py:1035
def kind(self)
Definition: z3py.py:1115
def update (   self,
  args 
)
Update the arguments of the expression.

Return a new expression with the same function declaration and updated arguments.
The number of new arguments must match the current number of arguments.

>>> f = Function('f', IntSort(), IntSort(), IntSort())
>>> a = Int('a')
>>> b = Int('b')
>>> c = Int('c')
>>> t = f(a, b)
>>> t.update(c, c)
f(c, c)

Definition at line 1174 of file z3py.py.

1174  def update(self, *args):
1175  """Update the arguments of the expression.
1176 
1177  Return a new expression with the same function declaration and updated arguments.
1178  The number of new arguments must match the current number of arguments.
1179 
1180  >>> f = Function('f', IntSort(), IntSort(), IntSort())
1181  >>> a = Int('a')
1182  >>> b = Int('b')
1183  >>> c = Int('c')
1184  >>> t = f(a, b)
1185  >>> t.update(c, c)
1186  f(c, c)
1187  """
1188  if z3_debug():
1189  _z3_assert(is_app(self), "Z3 application expected")
1190  _z3_assert(len(args) == self.num_args(), "Number of arguments does not match")
1191  _z3_assert(all([is_expr(arg) for arg in args]), "Z3 expressions expected")
1192  num = len(args)
1193  _args = (Ast * num)()
1194  for i in range(num):
1195  _args[i] = args[i].as_ast()
1196  return _to_expr_ref(Z3_update_term(self.ctx_ref(), self.as_ast(), num, _args), self.ctx)
1197 
def as_ast(self)
Definition: z3py.py:1029
expr range(expr const &lo, expr const &hi)
Definition: z3++.h:4343
def as_ast(self)
Definition: z3py.py:419
def is_app(a)
Definition: z3py.py:1368
Z3_ast Z3_API Z3_update_term(Z3_context c, Z3_ast a, unsigned num_args, Z3_ast const args[])
Update the arguments of term a using the arguments args. The number of arguments num_args should coin...
def num_args(self)
Definition: z3py.py:1122
def z3_debug()
Definition: z3py.py:70
def ctx_ref(self)
Definition: z3py.py:427
def is_expr(a)
Definition: z3py.py:1345
def update(self, args)
Definition: z3py.py:1174