Z3
Loading...
Searching...
No Matches
AstVector Class Reference
Inheritance diagram for AstVector:

Public Member Functions

 __init__ (self, v=None, ctx=None)
 __del__ (self)
 __len__ (self)
 __getitem__ (self, i)
 __setitem__ (self, i, v)
 push (self, v)
 resize (self, sz)
 __contains__ (self, item)
 translate (self, other_ctx)
 __copy__ (self)
 __deepcopy__ (self, memo={})
 __repr__ (self)
 sexpr (self)
Public Member Functions inherited from Z3PPObject
 use_pp (self)

Data Fields

 vector = None
 ctx = _get_ctx(ctx)

Additional Inherited Members

Protected Member Functions inherited from Z3PPObject
 _repr_html_ (self)

Detailed Description

A collection (vector) of ASTs.

Definition at line 6586 of file z3py.py.

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self,
v = None,
ctx = None )

Definition at line 6589 of file z3py.py.

6589 def __init__(self, v=None, ctx=None):
6590 self.vector = None
6591 if v is None:
6592 self.ctx = _get_ctx(ctx)
6593 self.vector = Z3_mk_ast_vector(self.ctx.ref())
6594 else:
6595 self.vector = v
6596 assert ctx is not None
6597 self.ctx = ctx
6598 Z3_ast_vector_inc_ref(self.ctx.ref(), self.vector)
6599
void Z3_API Z3_ast_vector_inc_ref(Z3_context c, Z3_ast_vector v)
Increment the reference counter of the given AST vector.
Z3_ast_vector Z3_API Z3_mk_ast_vector(Z3_context c)
Return an empty AST vector.

◆ __del__()

__del__ ( self)

Definition at line 6600 of file z3py.py.

6600 def __del__(self):
6601 if self.vector is not None and self.ctx.ref() is not None and Z3_ast_vector_dec_ref is not None:
6602 Z3_ast_vector_dec_ref(self.ctx.ref(), self.vector)
6603
void Z3_API Z3_ast_vector_dec_ref(Z3_context c, Z3_ast_vector v)
Decrement the reference counter of the given AST vector.

Member Function Documentation

◆ __contains__()

__contains__ ( self,
item )
Return `True` if the vector contains `item`.

>>> x = Int('x')
>>> A = AstVector()
>>> x in A
False
>>> A.push(x)
>>> x in A
True
>>> (x+1) in A
False
>>> A.push(x+1)
>>> (x+1) in A
True
>>> A
[x, x + 1]

Definition at line 6711 of file z3py.py.

6711 def __contains__(self, item):
6712 """Return `True` if the vector contains `item`.
6713
6714 >>> x = Int('x')
6715 >>> A = AstVector()
6716 >>> x in A
6717 False
6718 >>> A.push(x)
6719 >>> x in A
6720 True
6721 >>> (x+1) in A
6722 False
6723 >>> A.push(x+1)
6724 >>> (x+1) in A
6725 True
6726 >>> A
6727 [x, x + 1]
6728 """
6729 for elem in self:
6730 if elem.eq(item):
6731 return True
6732 return False
6733

◆ __copy__()

__copy__ ( self)

Definition at line 6750 of file z3py.py.

6750 def __copy__(self):
6751 return self.translate(self.ctx)
6752

◆ __deepcopy__()

__deepcopy__ ( self,
memo = {} )

Definition at line 6753 of file z3py.py.

6753 def __deepcopy__(self, memo={}):
6754 return self.translate(self.ctx)
6755

◆ __getitem__()

__getitem__ ( self,
i )
Return the AST at position `i`.

>>> A = AstVector()
>>> A.push(Int('x') + 1)
>>> A.push(Int('y'))
>>> A[0]
x + 1
>>> A[1]
y
>>> ctx = Context()
>>> A = AstVector(ctx=ctx)
>>> sorts = [BoolSort(ctx), IntSort(ctx), RealSort(ctx), BitVecSort(8, ctx),
...          ArraySort(IntSort(ctx), BoolSort(ctx)), DeclareSort('S', ctx)]
>>> for s in sorts: A.push(s)
>>> all(isinstance(A[i], type(s)) and A[i].eq(s)
...     for i, s in enumerate(sorts))
True
>>> A[-1].eq(sorts[-1])
True
>>> all(a.eq(s) for a, s in zip(A[:], sorts))
True
>>> A.push(Int('z', ctx))
>>> A.push(Function('f', IntSort(ctx), IntSort(ctx)))
>>> is_func_decl(A[-1])
True
>>> [is_expr(a) for a in A]
[False, False, False, False, False, False, True, False]
>>> ctx = Context()
>>> B = A.translate(ctx)
>>> all(B[i].eq(s.translate(ctx)) for i, s in enumerate(sorts))
True

Definition at line 6617 of file z3py.py.

6617 def __getitem__(self, i):
6618 """Return the AST at position `i`.
6619
6620 >>> A = AstVector()
6621 >>> A.push(Int('x') + 1)
6622 >>> A.push(Int('y'))
6623 >>> A[0]
6624 x + 1
6625 >>> A[1]
6626 y
6627 >>> ctx = Context()
6628 >>> A = AstVector(ctx=ctx)
6629 >>> sorts = [BoolSort(ctx), IntSort(ctx), RealSort(ctx), BitVecSort(8, ctx),
6630 ... ArraySort(IntSort(ctx), BoolSort(ctx)), DeclareSort('S', ctx)]
6631 >>> for s in sorts: A.push(s)
6632 >>> all(isinstance(A[i], type(s)) and A[i].eq(s)
6633 ... for i, s in enumerate(sorts))
6634 True
6635 >>> A[-1].eq(sorts[-1])
6636 True
6637 >>> all(a.eq(s) for a, s in zip(A[:], sorts))
6638 True
6639 >>> A.push(Int('z', ctx))
6640 >>> A.push(Function('f', IntSort(ctx), IntSort(ctx)))
6641 >>> is_func_decl(A[-1])
6642 True
6643 >>> [is_expr(a) for a in A]
6644 [False, False, False, False, False, False, True, False]
6645 >>> ctx = Context()
6646 >>> B = A.translate(ctx)
6647 >>> all(B[i].eq(s.translate(ctx)) for i, s in enumerate(sorts))
6648 True
6649 """
6650
6651 if isinstance(i, int):
6652 if i < 0:
6653 i += self.__len__()
6654
6655 if i >= self.__len__():
6656 raise IndexError
6657 return _to_ast_ref(Z3_ast_vector_get(self.ctx.ref(), self.vector, i), self.ctx)
6658
6659 elif isinstance(i, slice):
6660 result = []
6661 for ii in range(*i.indices(self.__len__())):
6662 result.append(_to_ast_ref(
6663 Z3_ast_vector_get(self.ctx.ref(), self.vector, ii),
6664 self.ctx,
6665 ))
6666 return result
6667
Z3_ast Z3_API Z3_ast_vector_get(Z3_context c, Z3_ast_vector v, unsigned i)
Return the AST at position i in the AST vector v.

◆ __len__()

__len__ ( self)
Return the size of the vector `self`.

>>> A = AstVector()
>>> len(A)
0
>>> A.push(Int('x'))
>>> A.push(Int('x'))
>>> len(A)
2

Definition at line 6604 of file z3py.py.

6604 def __len__(self):
6605 """Return the size of the vector `self`.
6606
6607 >>> A = AstVector()
6608 >>> len(A)
6609 0
6610 >>> A.push(Int('x'))
6611 >>> A.push(Int('x'))
6612 >>> len(A)
6613 2
6614 """
6615 return int(Z3_ast_vector_size(self.ctx.ref(), self.vector))
6616
unsigned Z3_API Z3_ast_vector_size(Z3_context c, Z3_ast_vector v)
Return the size of the given AST vector.

Referenced by __getitem__(), and __setitem__().

◆ __repr__()

__repr__ ( self)

Definition at line 6756 of file z3py.py.

6756 def __repr__(self):
6757 return obj_to_string(self)
6758

◆ __setitem__()

__setitem__ ( self,
i,
v )
Update AST at position `i`.

>>> A = AstVector()
>>> A.push(Int('x') + 1)
>>> A.push(Int('y'))
>>> A[0]
x + 1
>>> A[0] = Int('x')
>>> A[0]
x

Definition at line 6668 of file z3py.py.

6668 def __setitem__(self, i, v):
6669 """Update AST at position `i`.
6670
6671 >>> A = AstVector()
6672 >>> A.push(Int('x') + 1)
6673 >>> A.push(Int('y'))
6674 >>> A[0]
6675 x + 1
6676 >>> A[0] = Int('x')
6677 >>> A[0]
6678 x
6679 """
6680 if i < 0:
6681 i += self.__len__()
6682 if i < 0 or i >= self.__len__():
6683 raise IndexError
6684 Z3_ast_vector_set(self.ctx.ref(), self.vector, i, v.as_ast())
6685
void Z3_API Z3_ast_vector_set(Z3_context c, Z3_ast_vector v, unsigned i, Z3_ast a)
Update position i of the AST vector v with the AST a.

◆ push()

push ( self,
v )
Add `v` in the end of the vector.

>>> A = AstVector()
>>> len(A)
0
>>> A.push(Int('x'))
>>> len(A)
1

Definition at line 6686 of file z3py.py.

6686 def push(self, v):
6687 """Add `v` in the end of the vector.
6688
6689 >>> A = AstVector()
6690 >>> len(A)
6691 0
6692 >>> A.push(Int('x'))
6693 >>> len(A)
6694 1
6695 """
6696 Z3_ast_vector_push(self.ctx.ref(), self.vector, v.as_ast())
6697
void Z3_API Z3_ast_vector_push(Z3_context c, Z3_ast_vector v, Z3_ast a)
Add the AST a in the end of the AST vector v. The size of v is increased by one.

Referenced by Solver.__enter__().

◆ resize()

resize ( self,
sz )
Resize the vector to `sz` elements.

>>> A = AstVector()
>>> A.resize(10)
>>> len(A)
10
>>> for i in range(10): A[i] = Int('x')
>>> A[5]
x

Definition at line 6698 of file z3py.py.

6698 def resize(self, sz):
6699 """Resize the vector to `sz` elements.
6700
6701 >>> A = AstVector()
6702 >>> A.resize(10)
6703 >>> len(A)
6704 10
6705 >>> for i in range(10): A[i] = Int('x')
6706 >>> A[5]
6707 x
6708 """
6709 Z3_ast_vector_resize(self.ctx.ref(), self.vector, sz)
6710
void Z3_API Z3_ast_vector_resize(Z3_context c, Z3_ast_vector v, unsigned n)
Resize the AST vector v.

◆ sexpr()

sexpr ( self)
Return a textual representation of the s-expression representing the vector.

Definition at line 6759 of file z3py.py.

6759 def sexpr(self):
6760 """Return a textual representation of the s-expression representing the vector."""
6761 return Z3_ast_vector_to_string(self.ctx.ref(), self.vector)
6762
Z3_string Z3_API Z3_ast_vector_to_string(Z3_context c, Z3_ast_vector v)
Convert AST vector into a string.

◆ translate()

translate ( self,
other_ctx )
Copy vector `self` to context `other_ctx`.

>>> x = Int('x')
>>> A = AstVector()
>>> A.push(x)
>>> c2 = Context()
>>> B = A.translate(c2)
>>> B
[x]

Definition at line 6734 of file z3py.py.

6734 def translate(self, other_ctx):
6735 """Copy vector `self` to context `other_ctx`.
6736
6737 >>> x = Int('x')
6738 >>> A = AstVector()
6739 >>> A.push(x)
6740 >>> c2 = Context()
6741 >>> B = A.translate(c2)
6742 >>> B
6743 [x]
6744 """
6745 return AstVector(
6746 Z3_ast_vector_translate(self.ctx.ref(), self.vector, other_ctx.ref()),
6747 ctx=other_ctx,
6748 )
6749
Z3_ast_vector Z3_API Z3_ast_vector_translate(Z3_context s, Z3_ast_vector v, Z3_context t)
Translate the AST vector v from context s into an AST vector in context t.

Referenced by __copy__(), FuncInterp.__copy__(), ModelRef.__copy__(), __deepcopy__(), FuncInterp.__deepcopy__(), and ModelRef.__deepcopy__().

Field Documentation

◆ ctx

◆ vector

vector = None

Definition at line 6590 of file z3py.py.

Referenced by __del__(), __getitem__(), __len__(), __setitem__(), push(), resize(), sexpr(), and translate().