Z3
Loading...
Searching...
No Matches
ExprRef Class Reference

Expressions. More...

Inheritance diagram for ExprRef:

Public Member Functions

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

Additional Inherited Members

Data Fields inherited from AstRef
 ast = ast
 ctx = _get_ctx(ctx)
Protected Member Functions inherited from Z3PPObject
 _repr_html_ (self)

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

Member Function Documentation

◆ __eq__()

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

1060 def __eq__(self, other):
1061 """Return a Z3 expression that represents the constraint `self == other`.
1062
1063 If `other` is `None`, then this method simply returns `False`.
1064
1065 >>> a = Int('a')
1066 >>> b = Int('b')
1067 >>> a == b
1068 a == b
1069 >>> a is None
1070 False
1071 """
1072 if other is None:
1073 return False
1074 a, b = _coerce_exprs(self, other)
1075 return BoolRef(Z3_mk_eq(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
1076
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__().

◆ __hash__()

__hash__ ( self)
Hash code. 

Definition at line 1077 of file z3py.py.

1077 def __hash__(self):
1078 """ Hash code. """
1079 return AstRef.__hash__(self)
1080

◆ __ne__()

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

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

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

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

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

◆ as_ast()

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

Reimplemented from AstRef.

Reimplemented in PatternRef, and QuantifierRef.

Definition at line 1031 of file z3py.py.

1031 def as_ast(self):
1032 return self.ast
1033

Referenced by update().

◆ children()

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

1161 def children(self):
1162 """Return a list containing the children of the given expression
1163
1164 >>> a = Int('a')
1165 >>> b = Int('b')
1166 >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
1167 >>> t = f(a, b, 0)
1168 >>> t.children()
1169 [a, b, 0]
1170 """
1171 if is_app(self):
1172 return [self.arg(i) for i in range(self.num_args())]
1173 else:
1174 return []
1175

◆ decl()

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

1102 def decl(self):
1103 """Return the Z3 function declaration associated with a Z3 application.
1104
1105 >>> f = Function('f', IntSort(), IntSort())
1106 >>> a = Int('a')
1107 >>> t = f(a)
1108 >>> eq(t.decl(), f)
1109 True
1110 >>> (a + 1).decl()
1111 +
1112 """
1113 if z3_debug():
1114 _z3_assert(is_app(self), "Z3 application expected")
1115 return FuncDeclRef(Z3_get_app_decl(self.ctx_ref(), self.as_ast()), self.ctx)
1116
Z3_func_decl Z3_API Z3_get_app_decl(Z3_context c, Z3_app a)
Return the declaration of a constant or function application.

Referenced by params().

◆ from_string()

from_string ( self,
s )

Definition at line 1200 of file z3py.py.

1200 def from_string(self, s):
1201 pass
1202

◆ get_id()

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

Reimplemented from AstRef.

Reimplemented in PatternRef, and QuantifierRef.

Definition at line 1034 of file z3py.py.

1034 def get_id(self):
1035 return Z3_get_ast_id(self.ctx_ref(), self.as_ast())
1036
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....

◆ kind()

kind ( self)
Return the Z3 internal kind of a function application.

Definition at line 1117 of file z3py.py.

1117 def kind(self):
1118 """Return the Z3 internal kind of a function application."""
1119 if z3_debug():
1120 _z3_assert(is_app(self), "Z3 application expected")
1121 return Z3_get_decl_kind(self.ctx_ref(), Z3_get_app_decl(self.ctx_ref(), self.ast))
1122
1123
Z3_decl_kind Z3_API Z3_get_decl_kind(Z3_context c, Z3_func_decl d)
Return declaration kind corresponding to declaration.

Referenced by sort_kind().

◆ num_args()

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

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

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

◆ params()

params ( self)

Definition at line 1099 of file z3py.py.

1099 def params(self):
1100 return self.decl().params()
1101

Referenced by params().

◆ serialize()

serialize ( self)

Definition at line 1203 of file z3py.py.

1203 def serialize(self):
1204 s = Solver()
1205 f = Function('F', self.sort(), BoolSort(self.ctx))
1206 s.add(f(self))
1207 return s.sexpr()
1208

◆ sort()

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

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

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

Definition at line 1037 of file z3py.py.

1037 def sort(self):
1038 """Return the sort of expression `self`.
1039
1040 >>> x = Int('x')
1041 >>> (x + 1).sort()
1042 Int
1043 >>> y = Real('y')
1044 >>> (x + y).sort()
1045 Real
1046 """
1047 return _sort(self.ctx, self.as_ast())
1048

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

◆ sort_kind()

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

1049 def sort_kind(self):
1050 """Shorthand for `self.sort().kind()`.
1051
1052 >>> a = Array('a', IntSort(), IntSort())
1053 >>> a.sort_kind() == Z3_ARRAY_SORT
1054 True
1055 >>> a.sort_kind() == Z3_INT_SORT
1056 False
1057 """
1058 return self.sort().kind()
1059

◆ update()

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

1176 def update(self, *args):
1177 """Update the arguments of the expression.
1178
1179 Return a new expression with the same function declaration and updated arguments.
1180 The number of new arguments must match the current number of arguments.
1181
1182 >>> f = Function('f', IntSort(), IntSort(), IntSort())
1183 >>> a = Int('a')
1184 >>> b = Int('b')
1185 >>> c = Int('c')
1186 >>> t = f(a, b)
1187 >>> t.update(c, c)
1188 f(c, c)
1189 """
1190 if z3_debug():
1191 _z3_assert(is_app(self), "Z3 application expected")
1192 _z3_assert(len(args) == self.num_args(), "Number of arguments does not match")
1193 _z3_assert(all([is_expr(arg) for arg in args]), "Z3 expressions expected")
1194 num = len(args)
1195 _args = (Ast * num)()
1196 for i in range(num):
1197 _args[i] = args[i].as_ast()
1198 return _to_expr_ref(Z3_update_term(self.ctx_ref(), self.as_ast(), num, _args), self.ctx)
1199
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...