Z3
 
Loading...
Searching...
No Matches
Public Member Functions | Data Fields
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)
 
 __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)
 

Data Fields

 ctx
 
 ast
 
- Data Fields inherited from AstRef
 ast
 
 ctx
 

Additional Inherited Members

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

Reimplemented from AstRef.

Definition at line 1066 of file z3py.py.

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

Reimplemented from AstRef.

Definition at line 1083 of file z3py.py.

1083 def __hash__(self):
1084 """ Hash code. """
1085 return AstRef.__hash__(self)
1086

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

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

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

◆ as_ast()

as_ast (   self)

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

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

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

1108 def decl(self):
1109 """Return the Z3 function declaration associated with a Z3 application.
1110
1111 >>> f = Function('f', IntSort(), IntSort())
1112 >>> a = Int('a')
1113 >>> t = f(a)
1114 >>> eq(t.decl(), f)
1115 True
1116 >>> (a + 1).decl()
1117 +
1118 """
1119 if z3_debug():
1120 _z3_assert(is_app(self), "Z3 application expected")
1121 return FuncDeclRef(Z3_get_app_decl(self.ctx_ref(), self.as_ast()), self.ctx)
1122
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 ExprRef.params().

◆ from_string()

from_string (   self,
  s 
)

Definition at line 1206 of file z3py.py.

1206 def from_string(self, s):
1207 pass
1208

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

1040 def get_id(self):
1041 return Z3_get_ast_id(self.ctx_ref(), self.as_ast())
1042
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 1123 of file z3py.py.

1123 def kind(self):
1124 """Return the Z3 internal kind of a function application."""
1125 if z3_debug():
1126 _z3_assert(is_app(self), "Z3 application expected")
1127 return Z3_get_decl_kind(self.ctx_ref(), Z3_get_app_decl(self.ctx_ref(), self.ast))
1128
1129
Z3_decl_kind Z3_API Z3_get_decl_kind(Z3_context c, Z3_func_decl d)
Return declaration kind corresponding to declaration.

Referenced by ArithSortRef.is_int(), ArithSortRef.is_real(), and ExprRef.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 1130 of file z3py.py.

1130 def num_args(self):
1131 """Return the number of arguments of a Z3 application.
1132
1133 >>> a = Int('a')
1134 >>> b = Int('b')
1135 >>> (a + b).num_args()
1136 2
1137 >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
1138 >>> t = f(a, b, 0)
1139 >>> t.num_args()
1140 3
1141 """
1142 if z3_debug():
1143 _z3_assert(is_app(self), "Z3 application expected")
1144 return int(Z3_get_app_num_args(self.ctx_ref(), self.as_ast()))
1145
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__(), ExprRef.arg(), FuncEntry.arg_value(), FuncEntry.as_list(), ExprRef.children(), and ExprRef.update().

◆ params()

params (   self)

Definition at line 1105 of file z3py.py.

1105 def params(self):
1106 return self.decl().params()
1107

Referenced by ParamsRef.__deepcopy__(), ParamsRef.__del__(), ParamsRef.__repr__(), ExprRef.params(), ParamsRef.set(), and ParamsRef.validate().

◆ serialize()

serialize (   self)

Definition at line 1209 of file z3py.py.

1209 def serialize(self):
1210 s = Solver()
1211 f = Function('F', self.sort(), BoolSort(self.ctx))
1212 s.add(f(self))
1213 return s.sexpr()
1214

◆ 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 BoolRef, QuantifierRef, ArithRef, ComplexRef, BitVecRef, ArrayRef, FiniteSetRef, DatatypeRef, FiniteDomainRef, FPRef, SeqRef, and FiniteFieldRef.

Definition at line 1043 of file z3py.py.

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

Referenced by ArrayRef.domain(), ArrayRef.domain_n(), ArithRef.is_int(), ArithRef.is_real(), ArrayRef.range(), BitVecRef.size(), and ExprRef.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 1055 of file z3py.py.

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

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

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

Field Documentation

◆ ast

ast

◆ ctx

ctx

Definition at line 1053 of file z3py.py.

Referenced by ArithRef.__add__(), BitVecRef.__add__(), BitVecRef.__and__(), FuncDeclRef.__call__(), AstMap.__contains__(), AstRef.__copy__(), Goal.__copy__(), AstVector.__copy__(), FuncInterp.__copy__(), ModelRef.__copy__(), AstRef.__deepcopy__(), Datatype.__deepcopy__(), ParamsRef.__deepcopy__(), ParamDescrsRef.__deepcopy__(), Goal.__deepcopy__(), AstVector.__deepcopy__(), AstMap.__deepcopy__(), FuncEntry.__deepcopy__(), FuncInterp.__deepcopy__(), ModelRef.__deepcopy__(), Statistics.__deepcopy__(), Context.__del__(), AstRef.__del__(), ScopedConstructor.__del__(), ScopedConstructorList.__del__(), ParamsRef.__del__(), ParamDescrsRef.__del__(), Goal.__del__(), AstVector.__del__(), AstMap.__del__(), FuncEntry.__del__(), FuncInterp.__del__(), ModelRef.__del__(), Statistics.__del__(), Solver.__del__(), ArithRef.__div__(), BitVecRef.__div__(), ExprRef.__eq__(), ArithRef.__ge__(), BitVecRef.__ge__(), AstVector.__getitem__(), ModelRef.__getitem__(), Statistics.__getitem__(), AstMap.__getitem__(), ArithRef.__gt__(), BitVecRef.__gt__(), BitVecRef.__invert__(), ArithRef.__le__(), BitVecRef.__le__(), AstVector.__len__(), AstMap.__len__(), ModelRef.__len__(), Statistics.__len__(), BitVecRef.__lshift__(), ArithRef.__lt__(), BitVecRef.__lt__(), ArithRef.__mod__(), BitVecRef.__mod__(), BoolRef.__mul__(), ArithRef.__mul__(), BitVecRef.__mul__(), ExprRef.__ne__(), ArithRef.__neg__(), BitVecRef.__neg__(), BitVecRef.__or__(), ArithRef.__pow__(), ArithRef.__radd__(), BitVecRef.__radd__(), BitVecRef.__rand__(), ArithRef.__rdiv__(), BitVecRef.__rdiv__(), ParamsRef.__repr__(), ParamDescrsRef.__repr__(), AstMap.__repr__(), Statistics.__repr__(), BitVecRef.__rlshift__(), ArithRef.__rmod__(), BitVecRef.__rmod__(), ArithRef.__rmul__(), BitVecRef.__rmul__(), BitVecRef.__ror__(), ArithRef.__rpow__(), BitVecRef.__rrshift__(), BitVecRef.__rshift__(), ArithRef.__rsub__(), BitVecRef.__rsub__(), BitVecRef.__rxor__(), AstVector.__setitem__(), AstMap.__setitem__(), ArithRef.__sub__(), BitVecRef.__sub__(), BitVecRef.__xor__(), DatatypeSortRef.accessor(), ExprRef.arg(), FuncEntry.arg_value(), FuncInterp.arity(), Goal.as_expr(), Solver.assert_and_track(), Goal.assert_exprs(), Solver.assert_exprs(), QuantifierRef.body(), FiniteSetSortRef.cast(), Solver.check(), Goal.convert_model(), AstRef.ctx_ref(), ExprRef.decl(), ModelRef.decls(), ArrayRef.default(), RatNumRef.denominator(), Goal.depth(), Goal.dimacs(), FuncDeclRef.domain(), ArraySortRef.domain_n(), FuncInterp.else_value(), FuncInterp.entry(), AstMap.erase(), ModelRef.eval(), Goal.get(), ParamDescrsRef.get_documentation(), ModelRef.get_interp(), Statistics.get_key_value(), ParamDescrsRef.get_kind(), ParamDescrsRef.get_name(), ModelRef.get_sort(), ModelRef.get_universe(), Goal.inconsistent(), AstMap.keys(), Statistics.keys(), Solver.model(), SortRef.name(), QuantifierRef.no_pattern(), FuncEntry.num_args(), FuncInterp.num_entries(), Solver.num_scopes(), ModelRef.num_sorts(), FuncDeclRef.params(), QuantifierRef.pattern(), AlgebraicNumRef.poly(), Solver.pop(), Goal.prec(), ModelRef.project(), ModelRef.project_with_witness(), Solver.push(), AstVector.push(), QuantifierRef.qid(), FuncDeclRef.range(), ArraySortRef.range(), DatatypeSortRef.recognizer(), Context.ref(), AstMap.reset(), Solver.reset(), AstVector.resize(), Solver.set(), ParamsRef.set(), Goal.sexpr(), AstVector.sexpr(), ModelRef.sexpr(), ParamDescrsRef.size(), Goal.size(), QuantifierRef.skolem_id(), AstVector.translate(), AstRef.translate(), Goal.translate(), ModelRef.translate(), ExprRef.update(), DatatypeRef.update_field(), ParamsRef.validate(), FuncEntry.value(), QuantifierRef.var_name(), and QuantifierRef.var_sort().