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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

◆ serialize()

serialize (   self)

Definition at line 1204 of file z3py.py.

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

◆ 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, BitVecRef, ArrayRef, DatatypeRef, FiniteDomainRef, FPRef, and SeqRef.

Definition at line 1038 of file z3py.py.

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

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

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

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

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