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

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

1075 def __hash__(self):
1076 """ Hash code. """
1077 return AstRef.__hash__(self)
1078

◆ __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 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]).

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

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

1029 def as_ast(self):
1030 return self.ast
1031

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 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

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

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

1198 def from_string(self, s):
1199 pass
1200

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

1032 def get_id(self):
1033 return Z3_get_ast_id(self.ctx_ref(), self.as_ast())
1034
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 1115 of file z3py.py.

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.

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

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

1097 def params(self):
1098 return self.decl().params()
1099

Referenced by params().

◆ serialize()

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

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

Definition at line 1035 of file z3py.py.

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

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 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

◆ 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 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
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...