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

1027 def __eq__(self, other):
1028 """Return a Z3 expression that represents the constraint `self == other`.
1029
1030 If `other` is `None`, then this method simply returns `False`.
1031
1032 >>> a = Int('a')
1033 >>> b = Int('b')
1034 >>> a == b
1035 a == b
1036 >>> a is None
1037 False
1038 """
1039 if other is None:
1040 return False
1041 a, b = _coerce_exprs(self, other)
1042 return BoolRef(Z3_mk_eq(self.ctx_ref(), a.as_ast(), b.as_ast()), self.ctx)
1043
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 1044 of file z3py.py.

1044 def __hash__(self):
1045 """ Hash code. """
1046 return AstRef.__hash__(self)
1047

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

1048 def __ne__(self, other):
1049 """Return a Z3 expression that represents the constraint `self != other`.
1050
1051 If `other` is `None`, then this method simply returns `True`.
1052
1053 >>> a = Int('a')
1054 >>> b = Int('b')
1055 >>> a != b
1056 a != b
1057 >>> a is not None
1058 True
1059 """
1060 if other is None:
1061 return True
1062 a, b = _coerce_exprs(self, other)
1063 _args, sz = _to_ast_array((a, b))
1064 return BoolRef(Z3_mk_distinct(self.ctx_ref(), 2, _args), self.ctx)
1065
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 1107 of file z3py.py.

1107 def arg(self, idx):
1108 """Return argument `idx` of the application `self`.
1109
1110 This method assumes that `self` is a function application with at least `idx+1` arguments.
1111
1112 >>> a = Int('a')
1113 >>> b = Int('b')
1114 >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
1115 >>> t = f(a, b, 0)
1116 >>> t.arg(0)
1117 a
1118 >>> t.arg(1)
1119 b
1120 >>> t.arg(2)
1121 0
1122 """
1123 if z3_debug():
1124 _z3_assert(is_app(self), "Z3 application expected")
1125 _z3_assert(idx < self.num_args(), "Invalid argument index")
1126 return _to_expr_ref(Z3_get_app_arg(self.ctx_ref(), self.as_ast(), idx), self.ctx)
1127
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 998 of file z3py.py.

998 def as_ast(self):
999 return self.ast
1000

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

1128 def children(self):
1129 """Return a list containing the children of the given expression
1130
1131 >>> a = Int('a')
1132 >>> b = Int('b')
1133 >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
1134 >>> t = f(a, b, 0)
1135 >>> t.children()
1136 [a, b, 0]
1137 """
1138 if is_app(self):
1139 return [self.arg(i) for i in range(self.num_args())]
1140 else:
1141 return []
1142

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

1069 def decl(self):
1070 """Return the Z3 function declaration associated with a Z3 application.
1071
1072 >>> f = Function('f', IntSort(), IntSort())
1073 >>> a = Int('a')
1074 >>> t = f(a)
1075 >>> eq(t.decl(), f)
1076 True
1077 >>> (a + 1).decl()
1078 +
1079 """
1080 if z3_debug():
1081 _z3_assert(is_app(self), "Z3 application expected")
1082 return FuncDeclRef(Z3_get_app_decl(self.ctx_ref(), self.as_ast()), self.ctx)
1083
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 1143 of file z3py.py.

1143 def from_string(self, s):
1144 pass
1145

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

1001 def get_id(self):
1002 return Z3_get_ast_id(self.ctx_ref(), self.as_ast())
1003
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 1084 of file z3py.py.

1084 def kind(self):
1085 """Return the Z3 internal kind of a function application."""
1086 if z3_debug():
1087 _z3_assert(is_app(self), "Z3 application expected")
1088 return Z3_get_decl_kind(self.ctx_ref(), Z3_get_app_decl(self.ctx_ref(), self.ast))
1089
1090
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 1091 of file z3py.py.

1091 def num_args(self):
1092 """Return the number of arguments of a Z3 application.
1093
1094 >>> a = Int('a')
1095 >>> b = Int('b')
1096 >>> (a + b).num_args()
1097 2
1098 >>> f = Function('f', IntSort(), IntSort(), IntSort(), IntSort())
1099 >>> t = f(a, b, 0)
1100 >>> t.num_args()
1101 3
1102 """
1103 if z3_debug():
1104 _z3_assert(is_app(self), "Z3 application expected")
1105 return int(Z3_get_app_num_args(self.ctx_ref(), self.as_ast()))
1106
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 1066 of file z3py.py.

1066 def params(self):
1067 return self.decl().params()
1068

Referenced by params().

◆ serialize()

serialize ( self)

Definition at line 1146 of file z3py.py.

1146 def serialize(self):
1147 s = Solver()
1148 f = Function('F', self.sort(), BoolSort(self.ctx))
1149 s.add(f(self))
1150 return s.sexpr()
1151

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

1004 def sort(self):
1005 """Return the sort of expression `self`.
1006
1007 >>> x = Int('x')
1008 >>> (x + 1).sort()
1009 Int
1010 >>> y = Real('y')
1011 >>> (x + y).sort()
1012 Real
1013 """
1014 return _sort(self.ctx, self.as_ast())
1015

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

1016 def sort_kind(self):
1017 """Shorthand for `self.sort().kind()`.
1018
1019 >>> a = Array('a', IntSort(), IntSort())
1020 >>> a.sort_kind() == Z3_ARRAY_SORT
1021 True
1022 >>> a.sort_kind() == Z3_INT_SORT
1023 False
1024 """
1025 return self.sort().kind()
1026