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

Public Member Functions

 __init__ (self, m, ctx)
 __del__ (self)
 __repr__ (self)
 sexpr (self)
 eval (self, t, model_completion=False)
 evaluate (self, t, model_completion=False)
 __len__ (self)
 get_interp (self, decl)
 num_sorts (self)
 get_sort (self, idx)
 sorts (self)
 get_universe (self, s)
 __getitem__ (self, idx)
 decls (self)
 update_value (self, x, value)
 translate (self, target)
 project (self, vars, fml)
 project_with_witness (self, vars, fml)
 __copy__ (self)
 __deepcopy__ (self, memo={})
Public Member Functions inherited from Z3PPObject
 use_pp (self)

Data Fields

 model = m
 ctx = ctx

Additional Inherited Members

Protected Member Functions inherited from Z3PPObject
 _repr_html_ (self)

Detailed Description

Model/Solution of a satisfiability problem (aka system of constraints).

Definition at line 6521 of file z3py.py.

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self,
m,
ctx )

Definition at line 6524 of file z3py.py.

6524 def __init__(self, m, ctx):
6525 assert ctx is not None
6526 self.model = m
6527 self.ctx = ctx
6528 Z3_model_inc_ref(self.ctx.ref(), self.model)
6529
void Z3_API Z3_model_inc_ref(Z3_context c, Z3_model m)
Increment the reference counter of the given model.

◆ __del__()

__del__ ( self)

Definition at line 6530 of file z3py.py.

6530 def __del__(self):
6531 if self.ctx.ref() is not None and Z3_model_dec_ref is not None:
6532 Z3_model_dec_ref(self.ctx.ref(), self.model)
6533
void Z3_API Z3_model_dec_ref(Z3_context c, Z3_model m)
Decrement the reference counter of the given model.

Member Function Documentation

◆ __copy__()

__copy__ ( self)

Definition at line 6860 of file z3py.py.

6860 def __copy__(self):
6861 return self.translate(self.ctx)
6862

◆ __deepcopy__()

__deepcopy__ ( self,
memo = {} )

Definition at line 6863 of file z3py.py.

6863 def __deepcopy__(self, memo={}):
6864 return self.translate(self.ctx)
6865
6866

◆ __getitem__()

__getitem__ ( self,
idx )
If `idx` is an integer, then the declaration at position `idx` in the model `self` is returned.
If `idx` is a declaration, then the actual interpretation is returned.

The elements can be retrieved using position or the actual declaration.

>>> f = Function('f', IntSort(), IntSort())
>>> x = Int('x')
>>> s = Solver()
>>> s.add(x > 0, x < 2, f(x) == 0)
>>> s.check()
sat
>>> m = s.model()
>>> len(m)
2
>>> m[0]
x
>>> m[1]
f
>>> m[x]
1
>>> m[f]
[else -> 0]
>>> for d in m: print("%s -> %s" % (d, m[d]))
x -> 1
f -> [else -> 0]

Definition at line 6742 of file z3py.py.

6742 def __getitem__(self, idx):
6743 """If `idx` is an integer, then the declaration at position `idx` in the model `self` is returned.
6744 If `idx` is a declaration, then the actual interpretation is returned.
6745
6746 The elements can be retrieved using position or the actual declaration.
6747
6748 >>> f = Function('f', IntSort(), IntSort())
6749 >>> x = Int('x')
6750 >>> s = Solver()
6751 >>> s.add(x > 0, x < 2, f(x) == 0)
6752 >>> s.check()
6753 sat
6754 >>> m = s.model()
6755 >>> len(m)
6756 2
6757 >>> m[0]
6758 x
6759 >>> m[1]
6760 f
6761 >>> m[x]
6762 1
6763 >>> m[f]
6764 [else -> 0]
6765 >>> for d in m: print("%s -> %s" % (d, m[d]))
6766 x -> 1
6767 f -> [else -> 0]
6768 """
6769 if _is_int(idx):
6770 if idx >= len(self):
6771 raise IndexError
6772 num_consts = Z3_model_get_num_consts(self.ctx.ref(), self.model)
6773 if (idx < num_consts):
6774 return FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, idx), self.ctx)
6775 else:
6776 return FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, idx - num_consts), self.ctx)
6777 if isinstance(idx, FuncDeclRef):
6778 return self.get_interp(idx)
6779 if is_const(idx):
6780 return self.get_interp(idx.decl())
6781 if isinstance(idx, SortRef):
6782 return self.get_universe(idx)
6783 if z3_debug():
6784 _z3_assert(False, "Integer, Z3 declaration, or Z3 constant expected")
6785 return None
6786
Z3_func_decl Z3_API Z3_model_get_func_decl(Z3_context c, Z3_model m, unsigned i)
Return the declaration of the i-th function in the given model.
unsigned Z3_API Z3_model_get_num_consts(Z3_context c, Z3_model m)
Return the number of constants assigned by the given model.
Z3_func_decl Z3_API Z3_model_get_const_decl(Z3_context c, Z3_model m, unsigned i)
Return the i-th constant in the given model.

◆ __len__()

__len__ ( self)
Return the number of constant and function declarations in the model `self`.

>>> f = Function('f', IntSort(), IntSort())
>>> x = Int('x')
>>> s = Solver()
>>> s.add(x > 0, f(x) != x)
>>> s.check()
sat
>>> m = s.model()
>>> len(m)
2

Definition at line 6598 of file z3py.py.

6598 def __len__(self):
6599 """Return the number of constant and function declarations in the model `self`.
6600
6601 >>> f = Function('f', IntSort(), IntSort())
6602 >>> x = Int('x')
6603 >>> s = Solver()
6604 >>> s.add(x > 0, f(x) != x)
6605 >>> s.check()
6606 sat
6607 >>> m = s.model()
6608 >>> len(m)
6609 2
6610 """
6611 num_consts = int(Z3_model_get_num_consts(self.ctx.ref(), self.model))
6612 num_funcs = int(Z3_model_get_num_funcs(self.ctx.ref(), self.model))
6613 return num_consts + num_funcs
6614
unsigned Z3_API Z3_model_get_num_funcs(Z3_context c, Z3_model m)
Return the number of function interpretations in the given model.

◆ __repr__()

__repr__ ( self)

Definition at line 6534 of file z3py.py.

6534 def __repr__(self):
6535 return obj_to_string(self)
6536

◆ decls()

decls ( self)
Return a list with all symbols that have an interpretation in the model `self`.
>>> f = Function('f', IntSort(), IntSort())
>>> x = Int('x')
>>> s = Solver()
>>> s.add(x > 0, x < 2, f(x) == 0)
>>> s.check()
sat
>>> m = s.model()
>>> m.decls()
[x, f]

Definition at line 6787 of file z3py.py.

6787 def decls(self):
6788 """Return a list with all symbols that have an interpretation in the model `self`.
6789 >>> f = Function('f', IntSort(), IntSort())
6790 >>> x = Int('x')
6791 >>> s = Solver()
6792 >>> s.add(x > 0, x < 2, f(x) == 0)
6793 >>> s.check()
6794 sat
6795 >>> m = s.model()
6796 >>> m.decls()
6797 [x, f]
6798 """
6799 r = []
6800 for i in range(Z3_model_get_num_consts(self.ctx.ref(), self.model)):
6801 r.append(FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, i), self.ctx))
6802 for i in range(Z3_model_get_num_funcs(self.ctx.ref(), self.model)):
6803 r.append(FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, i), self.ctx))
6804 return r
6805

◆ eval()

eval ( self,
t,
model_completion = False )
Evaluate the expression `t` in the model `self`.
If `model_completion` is enabled, then a default interpretation is automatically added
for symbols that do not have an interpretation in the model `self`.

>>> x = Int('x')
>>> s = Solver()
>>> s.add(x > 0, x < 2)
>>> s.check()
sat
>>> m = s.model()
>>> m.eval(x + 1)
2
>>> m.eval(x == 1)
True
>>> y = Int('y')
>>> m.eval(y + x)
1 + y
>>> m.eval(y)
y
>>> m.eval(y, model_completion=True)
0
>>> # Now, m contains an interpretation for y
>>> m.eval(y + x)
1

Definition at line 6541 of file z3py.py.

6541 def eval(self, t, model_completion=False):
6542 """Evaluate the expression `t` in the model `self`.
6543 If `model_completion` is enabled, then a default interpretation is automatically added
6544 for symbols that do not have an interpretation in the model `self`.
6545
6546 >>> x = Int('x')
6547 >>> s = Solver()
6548 >>> s.add(x > 0, x < 2)
6549 >>> s.check()
6550 sat
6551 >>> m = s.model()
6552 >>> m.eval(x + 1)
6553 2
6554 >>> m.eval(x == 1)
6555 True
6556 >>> y = Int('y')
6557 >>> m.eval(y + x)
6558 1 + y
6559 >>> m.eval(y)
6560 y
6561 >>> m.eval(y, model_completion=True)
6562 0
6563 >>> # Now, m contains an interpretation for y
6564 >>> m.eval(y + x)
6565 1
6566 """
6567 r = (Ast * 1)()
6568 if Z3_model_eval(self.ctx.ref(), self.model, t.as_ast(), model_completion, r):
6569 return _to_expr_ref(r[0], self.ctx)
6570 raise Z3Exception("failed to evaluate expression in the model")
6571
bool Z3_API Z3_model_eval(Z3_context c, Z3_model m, Z3_ast t, bool model_completion, Z3_ast *v)
Evaluate the AST node t in the given model. Return true if succeeded, and store the result in v.

Referenced by evaluate().

◆ evaluate()

evaluate ( self,
t,
model_completion = False )
Alias for `eval`.

>>> x = Int('x')
>>> s = Solver()
>>> s.add(x > 0, x < 2)
>>> s.check()
sat
>>> m = s.model()
>>> m.evaluate(x + 1)
2
>>> m.evaluate(x == 1)
True
>>> y = Int('y')
>>> m.evaluate(y + x)
1 + y
>>> m.evaluate(y)
y
>>> m.evaluate(y, model_completion=True)
0
>>> # Now, m contains an interpretation for y
>>> m.evaluate(y + x)
1

Definition at line 6572 of file z3py.py.

6572 def evaluate(self, t, model_completion=False):
6573 """Alias for `eval`.
6574
6575 >>> x = Int('x')
6576 >>> s = Solver()
6577 >>> s.add(x > 0, x < 2)
6578 >>> s.check()
6579 sat
6580 >>> m = s.model()
6581 >>> m.evaluate(x + 1)
6582 2
6583 >>> m.evaluate(x == 1)
6584 True
6585 >>> y = Int('y')
6586 >>> m.evaluate(y + x)
6587 1 + y
6588 >>> m.evaluate(y)
6589 y
6590 >>> m.evaluate(y, model_completion=True)
6591 0
6592 >>> # Now, m contains an interpretation for y
6593 >>> m.evaluate(y + x)
6594 1
6595 """
6596 return self.eval(t, model_completion)
6597

◆ get_interp()

get_interp ( self,
decl )
Return the interpretation for a given declaration or constant.

>>> f = Function('f', IntSort(), IntSort())
>>> x = Int('x')
>>> s = Solver()
>>> s.add(x > 0, x < 2, f(x) == 0)
>>> s.check()
sat
>>> m = s.model()
>>> m[x]
1
>>> m[f]
[else -> 0]

Definition at line 6615 of file z3py.py.

6615 def get_interp(self, decl):
6616 """Return the interpretation for a given declaration or constant.
6617
6618 >>> f = Function('f', IntSort(), IntSort())
6619 >>> x = Int('x')
6620 >>> s = Solver()
6621 >>> s.add(x > 0, x < 2, f(x) == 0)
6622 >>> s.check()
6623 sat
6624 >>> m = s.model()
6625 >>> m[x]
6626 1
6627 >>> m[f]
6628 [else -> 0]
6629 """
6630 if z3_debug():
6631 _z3_assert(isinstance(decl, FuncDeclRef) or is_const(decl), "Z3 declaration expected")
6632 if is_const(decl):
6633 decl = decl.decl()
6634 try:
6635 if decl.arity() == 0:
6636 _r = Z3_model_get_const_interp(self.ctx.ref(), self.model, decl.ast)
6637 if _r.value is None:
6638 return None
6639 r = _to_expr_ref(_r, self.ctx)
6640 if is_as_array(r):
6641 fi = self.get_interp(get_as_array_func(r))
6642 if fi is None:
6643 return fi
6644 e = fi.else_value()
6645 if e is None:
6646 return fi
6647 if fi.arity() != 1:
6648 return fi
6649 srt = decl.range()
6650 dom = srt.domain()
6651 e = K(dom, e)
6652 i = 0
6653 sz = fi.num_entries()
6654 n = fi.arity()
6655 while i < sz:
6656 fe = fi.entry(i)
6657 e = Store(e, fe.arg_value(0), fe.value())
6658 i += 1
6659 return e
6660 else:
6661 return r
6662 else:
6663 return FuncInterp(Z3_model_get_func_interp(self.ctx.ref(), self.model, decl.ast), self.ctx)
6664 except Z3Exception:
6665 return None
6666
Z3_ast Z3_API Z3_model_get_const_interp(Z3_context c, Z3_model m, Z3_func_decl a)
Return the interpretation (i.e., assignment) of constant a in the model m. Return NULL,...
Z3_func_interp Z3_API Z3_model_get_func_interp(Z3_context c, Z3_model m, Z3_func_decl f)
Return the interpretation of the function f in the model m. Return NULL, if the model does not assign...

Referenced by __getitem__(), and get_interp().

◆ get_sort()

get_sort ( self,
idx )
Return the uninterpreted sort at position `idx` < self.num_sorts().

>>> A = DeclareSort('A')
>>> B = DeclareSort('B')
>>> a1, a2 = Consts('a1 a2', A)
>>> b1, b2 = Consts('b1 b2', B)
>>> s = Solver()
>>> s.add(a1 != a2, b1 != b2)
>>> s.check()
sat
>>> m = s.model()
>>> m.num_sorts()
2
>>> m.get_sort(0)
A
>>> m.get_sort(1)
B

Definition at line 6682 of file z3py.py.

6682 def get_sort(self, idx):
6683 """Return the uninterpreted sort at position `idx` < self.num_sorts().
6684
6685 >>> A = DeclareSort('A')
6686 >>> B = DeclareSort('B')
6687 >>> a1, a2 = Consts('a1 a2', A)
6688 >>> b1, b2 = Consts('b1 b2', B)
6689 >>> s = Solver()
6690 >>> s.add(a1 != a2, b1 != b2)
6691 >>> s.check()
6692 sat
6693 >>> m = s.model()
6694 >>> m.num_sorts()
6695 2
6696 >>> m.get_sort(0)
6697 A
6698 >>> m.get_sort(1)
6699 B
6700 """
6701 if idx >= self.num_sorts():
6702 raise IndexError
6703 return _to_sort_ref(Z3_model_get_sort(self.ctx.ref(), self.model, idx), self.ctx)
6704
Z3_sort Z3_API Z3_model_get_sort(Z3_context c, Z3_model m, unsigned i)
Return a uninterpreted sort that m assigns an interpretation.

Referenced by sorts().

◆ get_universe()

get_universe ( self,
s )
Return the interpretation for the uninterpreted sort `s` in the model `self`.

>>> A = DeclareSort('A')
>>> a, b = Consts('a b', A)
>>> s = Solver()
>>> s.add(a != b)
>>> s.check()
sat
>>> m = s.model()
>>> m.get_universe(A)
[A!val!1, A!val!0]

Definition at line 6722 of file z3py.py.

6722 def get_universe(self, s):
6723 """Return the interpretation for the uninterpreted sort `s` in the model `self`.
6724
6725 >>> A = DeclareSort('A')
6726 >>> a, b = Consts('a b', A)
6727 >>> s = Solver()
6728 >>> s.add(a != b)
6729 >>> s.check()
6730 sat
6731 >>> m = s.model()
6732 >>> m.get_universe(A)
6733 [A!val!1, A!val!0]
6734 """
6735 if z3_debug():
6736 _z3_assert(isinstance(s, SortRef), "Z3 sort expected")
6737 try:
6738 return AstVector(Z3_model_get_sort_universe(self.ctx.ref(), self.model, s.ast), self.ctx)
6739 except Z3Exception:
6740 return None
6741
Z3_ast_vector Z3_API Z3_model_get_sort_universe(Z3_context c, Z3_model m, Z3_sort s)
Return the finite set of distinct values that represent the interpretation for sort s.

Referenced by __getitem__().

◆ num_sorts()

num_sorts ( self)
Return the number of uninterpreted sorts that contain an interpretation in the model `self`.

>>> A = DeclareSort('A')
>>> a, b = Consts('a b', A)
>>> s = Solver()
>>> s.add(a != b)
>>> s.check()
sat
>>> m = s.model()
>>> m.num_sorts()
1

Definition at line 6667 of file z3py.py.

6667 def num_sorts(self):
6668 """Return the number of uninterpreted sorts that contain an interpretation in the model `self`.
6669
6670 >>> A = DeclareSort('A')
6671 >>> a, b = Consts('a b', A)
6672 >>> s = Solver()
6673 >>> s.add(a != b)
6674 >>> s.check()
6675 sat
6676 >>> m = s.model()
6677 >>> m.num_sorts()
6678 1
6679 """
6680 return int(Z3_model_get_num_sorts(self.ctx.ref(), self.model))
6681
unsigned Z3_API Z3_model_get_num_sorts(Z3_context c, Z3_model m)
Return the number of uninterpreted sorts that m assigns an interpretation to.

Referenced by get_sort(), and sorts().

◆ project()

project ( self,
vars,
fml )
Perform model-based projection on fml with respect to vars.
Assume that the model satisfies fml. Then compute a projection fml_p, such
that vars do not occur free in fml_p, fml_p is true in the model and
fml_p => exists vars . fml

Definition at line 6836 of file z3py.py.

6836 def project(self, vars, fml):
6837 """Perform model-based projection on fml with respect to vars.
6838 Assume that the model satisfies fml. Then compute a projection fml_p, such
6839 that vars do not occur free in fml_p, fml_p is true in the model and
6840 fml_p => exists vars . fml
6841 """
6842 ctx = self.ctx.ref()
6843 _vars = (Ast * len(vars))()
6844 for i in range(len(vars)):
6845 _vars[i] = vars[i].as_ast()
6846 return _to_expr_ref(Z3_qe_model_project(ctx, self.model, len(vars), _vars, fml.ast), self.ctx)
6847

◆ project_with_witness()

project_with_witness ( self,
vars,
fml )
Perform model-based projection, but also include realizer terms for the projected variables

Definition at line 6848 of file z3py.py.

6848 def project_with_witness(self, vars, fml):
6849 """Perform model-based projection, but also include realizer terms for the projected variables"""
6850 ctx = self.ctx.ref()
6851 _vars = (Ast * len(vars))()
6852 for i in range(len(vars)):
6853 _vars[i] = vars[i].as_ast()
6854 defs = AstMap()
6855 result = Z3_qe_model_project_with_witness(ctx, self.model, len(vars), _vars, fml.ast, defs.map)
6856 result = _to_expr_ref(result, self.ctx)
6857 return result, defs
6858
6859

◆ sexpr()

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

Definition at line 6537 of file z3py.py.

6537 def sexpr(self):
6538 """Return a textual representation of the s-expression representing the model."""
6539 return Z3_model_to_string(self.ctx.ref(), self.model)
6540
Z3_string Z3_API Z3_model_to_string(Z3_context c, Z3_model m)
Convert the given model into a string.

◆ sorts()

sorts ( self)
Return all uninterpreted sorts that have an interpretation in the model `self`.

>>> A = DeclareSort('A')
>>> B = DeclareSort('B')
>>> a1, a2 = Consts('a1 a2', A)
>>> b1, b2 = Consts('b1 b2', B)
>>> s = Solver()
>>> s.add(a1 != a2, b1 != b2)
>>> s.check()
sat
>>> m = s.model()
>>> m.sorts()
[A, B]

Definition at line 6705 of file z3py.py.

6705 def sorts(self):
6706 """Return all uninterpreted sorts that have an interpretation in the model `self`.
6707
6708 >>> A = DeclareSort('A')
6709 >>> B = DeclareSort('B')
6710 >>> a1, a2 = Consts('a1 a2', A)
6711 >>> b1, b2 = Consts('b1 b2', B)
6712 >>> s = Solver()
6713 >>> s.add(a1 != a2, b1 != b2)
6714 >>> s.check()
6715 sat
6716 >>> m = s.model()
6717 >>> m.sorts()
6718 [A, B]
6719 """
6720 return [self.get_sort(i) for i in range(self.num_sorts())]
6721

◆ translate()

translate ( self,
target )
Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.

Definition at line 6828 of file z3py.py.

6828 def translate(self, target):
6829 """Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.
6830 """
6831 if z3_debug():
6832 _z3_assert(isinstance(target, Context), "argument must be a Z3 context")
6833 model = Z3_model_translate(self.ctx.ref(), self.model, target.ref())
6834 return ModelRef(model, target)
6835
Z3_model Z3_API Z3_model_translate(Z3_context c, Z3_model m, Z3_context dst)
translate model from context c to context dst.

Referenced by __copy__(), and __deepcopy__().

◆ update_value()

update_value ( self,
x,
value )
Update the interpretation of a constant

Definition at line 6806 of file z3py.py.

6806 def update_value(self, x, value):
6807 """Update the interpretation of a constant"""
6808 if is_expr(x):
6809 x = x.decl()
6810 if is_func_decl(x) and x.arity() != 0 and isinstance(value, FuncInterp):
6811 fi1 = value.f
6812 fi2 = Z3_add_func_interp(x.ctx_ref(), self.model, x.ast, value.else_value().ast);
6813 fi2 = FuncInterp(fi2, x.ctx)
6814 for i in range(value.num_entries()):
6815 e = value.entry(i)
6816 n = Z3_func_entry_get_num_args(x.ctx_ref(), e.entry)
6817 v = AstVector()
6818 for j in range(n):
6819 v.push(e.arg_value(j))
6820 val = Z3_func_entry_get_value(x.ctx_ref(), e.entry)
6821 Z3_func_interp_add_entry(x.ctx_ref(), fi2.f, v.vector, val)
6822 return
6823 if not is_func_decl(x) or x.arity() != 0:
6824 raise Z3Exception("Expecting 0-ary function or constant expression")
6825 value = _py2expr(value)
6826 Z3_add_const_interp(x.ctx_ref(), self.model, x.ast, value.ast)
6827
Z3_func_interp Z3_API Z3_add_func_interp(Z3_context c, Z3_model m, Z3_func_decl f, Z3_ast default_value)
Create a fresh func_interp object, add it to a model for a specified function. It has reference count...
unsigned Z3_API Z3_func_entry_get_num_args(Z3_context c, Z3_func_entry e)
Return the number of arguments in a Z3_func_entry object.
Z3_ast Z3_API Z3_func_entry_get_value(Z3_context c, Z3_func_entry e)
Return the value of this point.
void Z3_API Z3_add_const_interp(Z3_context c, Z3_model m, Z3_func_decl f, Z3_ast a)
Add a constant interpretation.
void Z3_API Z3_func_interp_add_entry(Z3_context c, Z3_func_interp fi, Z3_ast_vector args, Z3_ast value)
add a function entry to a function interpretation.

Field Documentation

◆ ctx

◆ model