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)
 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 997 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 1037 of file z3py.py.

1037 def __eq__(self, other):
1038 """Return a Z3 expression that represents the constraint `self == other`.
1039
1040 If `other` is `None`, then this method simply returns `False`.
1041
1042 >>> a = Int('a')
1043 >>> b = Int('b')
1044 >>> a == b
1045 a == b
1046 >>> a is None
1047 False
1048 """
1049 if other is None:
1050 return False
1051 a, b = _coerce_exprs(self, other)
1052 return BoolRef(Z3_mk_eq(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
1053
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 1054 of file z3py.py.

1054 def __hash__(self):
1055 """ Hash code. """
1056 return AstRef.__hash__(self)
1057

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

1058 def __ne__(self, other):
1059 """Return a Z3 expression that represents the constraint `self != other`.
1060
1061 If `other` is `None`, then this method simply returns `True`.
1062
1063 >>> a = Int('a')
1064 >>> b = Int('b')
1065 >>> a != b
1066 a != b
1067 >>> a is not None
1068 True
1069 """
1070 if other is None:
1071 return True
1072 a, b = _coerce_exprs(self, other)
1073 _args, sz = _to_ast_array((a, b))
1074 return BoolRef(Z3_mk_distinct(self.ctx_ref(), 2, _args), self.ctx)
1075
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 1117 of file z3py.py.

1117 def arg(self, idx):
1118 """Return argument `idx` of the application `self`.
1119
1120 This method assumes that `self` is a function application with at least `idx+1` arguments.
1121
1122 >>> a = Int('a')
1123 >>> b = Int('b')
1124 >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
1125 >>> t = f(a, b, 0)
1126 >>> t.arg(0)
1127 a
1128 >>> t.arg(1)
1129 b
1130 >>> t.arg(2)
1131 0
1132 """
1133 if z3_debug():
1134 _z3_assert(is_app(self), "Z3 application expected")
1135 _z3_assert(idx < self.num_args(), "Invalid argument index")
1136 return _to_expr_ref(Z3_get_app_arg(self.ctx_ref(), self.as_ast(), idx), self.ctx)
1137
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 1008 of file z3py.py.

1008 def as_ast(self):
1009 return self.ast
1010

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

1138 def children(self):
1139 """Return a list containing the children of the given expression
1140
1141 >>> a = Int('a')
1142 >>> b = Int('b')
1143 >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
1144 >>> t = f(a, b, 0)
1145 >>> t.children()
1146 [a, b, 0]
1147 """
1148 if is_app(self):
1149 return [self.arg(i) for i in range(self.num_args())]
1150 else:
1151 return []
1152

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

1079 def decl(self):
1080 """Return the Z3 function declaration associated with a Z3 application.
1081
1082 >>> f = Function('f', IntSort(), IntSort())
1083 >>> a = Int('a')
1084 >>> t = f(a)
1085 >>> eq(t.decl(), f)
1086 True
1087 >>> (a + 1).decl()
1088 +
1089 """
1090 if z3_debug():
1091 _z3_assert(is_app(self), "Z3 application expected")
1092 return FuncDeclRef(Z3_get_app_decl(self.ctx_ref(), self.as_ast()), self.ctx)
1093
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 1153 of file z3py.py.

1153 def from_string(self, s):
1154 pass
1155

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

1011 def get_id(self):
1012 return Z3_get_ast_id(self.ctx_ref(), self.as_ast())
1013
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 1094 of file z3py.py.

1094 def kind(self):
1095 """Return the Z3 internal kind of a function application."""
1096 if z3_debug():
1097 _z3_assert(is_app(self), "Z3 application expected")
1098 return Z3_get_decl_kind(self.ctx_ref(), Z3_get_app_decl(self.ctx_ref(), self.ast))
1099
1100
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 1101 of file z3py.py.

1101 def num_args(self):
1102 """Return the number of arguments of a Z3 application.
1103
1104 >>> a = Int('a')
1105 >>> b = Int('b')
1106 >>> (a + b).num_args()
1107 2
1108 >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
1109 >>> t = f(a, b, 0)
1110 >>> t.num_args()
1111 3
1112 """
1113 if z3_debug():
1114 _z3_assert(is_app(self), "Z3 application expected")
1115 return int(Z3_get_app_num_args(self.ctx_ref(), self.as_ast()))
1116
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(), and children().

◆ params()

params ( self)

Definition at line 1076 of file z3py.py.

1076 def params(self):
1077 return self.decl().params()
1078

Referenced by params().

◆ serialize()

serialize ( self)

Definition at line 1156 of file z3py.py.

1156 def serialize(self):
1157 s = Solver()
1158 f = Function('F', self.sort(), BoolSort(self.ctx))
1159 s.add(f(self))
1160 return s.sexpr()
1161

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

1014 def sort(self):
1015 """Return the sort of expression `self`.
1016
1017 >>> x = Int('x')
1018 >>> (x + 1).sort()
1019 Int
1020 >>> y = Real('y')
1021 >>> (x + y).sort()
1022 Real
1023 """
1024 return _sort(self.ctx, self.as_ast())
1025

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

1026 def sort_kind(self):
1027 """Shorthand for `self.sort().kind()`.
1028
1029 >>> a = Array('a', IntSort(), IntSort())
1030 >>> a.sort_kind() == Z3_ARRAY_SORT
1031 True
1032 >>> a.sort_kind() == Z3_INT_SORT
1033 False
1034 """
1035 return self.sort().kind()
1036