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

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

1079 def __hash__(self):
1080 """ Hash code. """
1081 return AstRef.__hash__(self)
1082

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

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

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

1033 def as_ast(self):
1034 return self.ast
1035

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

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

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

1104 def decl(self):
1105 """Return the Z3 function declaration associated with a Z3 application.
1106
1107 >>> f = Function('f', IntSort(), IntSort())
1108 >>> a = Int('a')
1109 >>> t = f(a)
1110 >>> eq(t.decl(), f)
1111 True
1112 >>> (a + 1).decl()
1113 +
1114 """
1115 if z3_debug():
1116 _z3_assert(is_app(self), "Z3 application expected")
1117 return FuncDeclRef(Z3_get_app_decl(self.ctx_ref(), self.as_ast()), self.ctx)
1118
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 1202 of file z3py.py.

1202 def from_string(self, s):
1203 pass
1204

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

1036 def get_id(self):
1037 return Z3_get_ast_id(self.ctx_ref(), self.as_ast())
1038
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 1119 of file z3py.py.

1119 def kind(self):
1120 """Return the Z3 internal kind of a function application."""
1121 if z3_debug():
1122 _z3_assert(is_app(self), "Z3 application expected")
1123 return Z3_get_decl_kind(self.ctx_ref(), Z3_get_app_decl(self.ctx_ref(), self.ast))
1124
1125
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 1126 of file z3py.py.

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

1101 def params(self):
1102 return self.decl().params()
1103

Referenced by params().

◆ serialize()

serialize ( self)

Definition at line 1205 of file z3py.py.

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

◆ 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, FiniteFieldRef, FiniteSetRef, FPRef, QuantifierRef, and SeqRef.

Definition at line 1039 of file z3py.py.

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

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

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

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

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