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

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self,
m,
ctx )

Definition at line 6489 of file z3py.py.

6489 def __init__(self, m, ctx):
6490 assert ctx is not None
6491 self.model = m
6492 self.ctx = ctx
6493 Z3_model_inc_ref(self.ctx.ref(), self.model)
6494
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 6495 of file z3py.py.

6495 def __del__(self):
6496 if self.ctx.ref() is not None and Z3_model_dec_ref is not None:
6497 Z3_model_dec_ref(self.ctx.ref(), self.model)
6498
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 6825 of file z3py.py.

6825 def __copy__(self):
6826 return self.translate(self.ctx)
6827

◆ __deepcopy__()

__deepcopy__ ( self,
memo = {} )

Definition at line 6828 of file z3py.py.

6828 def __deepcopy__(self, memo={}):
6829 return self.translate(self.ctx)
6830
6831

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

6707 def __getitem__(self, idx):
6708 """If `idx` is an integer, then the declaration at position `idx` in the model `self` is returned.
6709 If `idx` is a declaration, then the actual interpretation is returned.
6710
6711 The elements can be retrieved using position or the actual declaration.
6712
6713 >>> f = Function('f', IntSort(), IntSort())
6714 >>> x = Int('x')
6715 >>> s = Solver()
6716 >>> s.add(x > 0, x < 2, f(x) == 0)
6717 >>> s.check()
6718 sat
6719 >>> m = s.model()
6720 >>> len(m)
6721 2
6722 >>> m[0]
6723 x
6724 >>> m[1]
6725 f
6726 >>> m[x]
6727 1
6728 >>> m[f]
6729 [else -> 0]
6730 >>> for d in m: print("%s -> %s" % (d, m[d]))
6731 x -> 1
6732 f -> [else -> 0]
6733 """
6734 if _is_int(idx):
6735 if idx >= len(self):
6736 raise IndexError
6737 num_consts = Z3_model_get_num_consts(self.ctx.ref(), self.model)
6738 if (idx < num_consts):
6739 return FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, idx), self.ctx)
6740 else:
6741 return FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, idx - num_consts), self.ctx)
6742 if isinstance(idx, FuncDeclRef):
6743 return self.get_interp(idx)
6744 if is_const(idx):
6745 return self.get_interp(idx.decl())
6746 if isinstance(idx, SortRef):
6747 return self.get_universe(idx)
6748 if z3_debug():
6749 _z3_assert(False, "Integer, Z3 declaration, or Z3 constant expected")
6750 return None
6751
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 6563 of file z3py.py.

6563 def __len__(self):
6564 """Return the number of constant and function declarations in the model `self`.
6565
6566 >>> f = Function('f', IntSort(), IntSort())
6567 >>> x = Int('x')
6568 >>> s = Solver()
6569 >>> s.add(x > 0, f(x) != x)
6570 >>> s.check()
6571 sat
6572 >>> m = s.model()
6573 >>> len(m)
6574 2
6575 """
6576 num_consts = int(Z3_model_get_num_consts(self.ctx.ref(), self.model))
6577 num_funcs = int(Z3_model_get_num_funcs(self.ctx.ref(), self.model))
6578 return num_consts + num_funcs
6579
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 6499 of file z3py.py.

6499 def __repr__(self):
6500 return obj_to_string(self)
6501

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

6752 def decls(self):
6753 """Return a list with all symbols that have an interpretation in the model `self`.
6754 >>> f = Function('f', IntSort(), IntSort())
6755 >>> x = Int('x')
6756 >>> s = Solver()
6757 >>> s.add(x > 0, x < 2, f(x) == 0)
6758 >>> s.check()
6759 sat
6760 >>> m = s.model()
6761 >>> m.decls()
6762 [x, f]
6763 """
6764 r = []
6765 for i in range(Z3_model_get_num_consts(self.ctx.ref(), self.model)):
6766 r.append(FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, i), self.ctx))
6767 for i in range(Z3_model_get_num_funcs(self.ctx.ref(), self.model)):
6768 r.append(FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, i), self.ctx))
6769 return r
6770

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

6506 def eval(self, t, model_completion=False):
6507 """Evaluate the expression `t` in the model `self`.
6508 If `model_completion` is enabled, then a default interpretation is automatically added
6509 for symbols that do not have an interpretation in the model `self`.
6510
6511 >>> x = Int('x')
6512 >>> s = Solver()
6513 >>> s.add(x > 0, x < 2)
6514 >>> s.check()
6515 sat
6516 >>> m = s.model()
6517 >>> m.eval(x + 1)
6518 2
6519 >>> m.eval(x == 1)
6520 True
6521 >>> y = Int('y')
6522 >>> m.eval(y + x)
6523 1 + y
6524 >>> m.eval(y)
6525 y
6526 >>> m.eval(y, model_completion=True)
6527 0
6528 >>> # Now, m contains an interpretation for y
6529 >>> m.eval(y + x)
6530 1
6531 """
6532 r = (Ast * 1)()
6533 if Z3_model_eval(self.ctx.ref(), self.model, t.as_ast(), model_completion, r):
6534 return _to_expr_ref(r[0], self.ctx)
6535 raise Z3Exception("failed to evaluate expression in the model")
6536
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 6537 of file z3py.py.

6537 def evaluate(self, t, model_completion=False):
6538 """Alias for `eval`.
6539
6540 >>> x = Int('x')
6541 >>> s = Solver()
6542 >>> s.add(x > 0, x < 2)
6543 >>> s.check()
6544 sat
6545 >>> m = s.model()
6546 >>> m.evaluate(x + 1)
6547 2
6548 >>> m.evaluate(x == 1)
6549 True
6550 >>> y = Int('y')
6551 >>> m.evaluate(y + x)
6552 1 + y
6553 >>> m.evaluate(y)
6554 y
6555 >>> m.evaluate(y, model_completion=True)
6556 0
6557 >>> # Now, m contains an interpretation for y
6558 >>> m.evaluate(y + x)
6559 1
6560 """
6561 return self.eval(t, model_completion)
6562

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

6580 def get_interp(self, decl):
6581 """Return the interpretation for a given declaration or constant.
6582
6583 >>> f = Function('f', IntSort(), IntSort())
6584 >>> x = Int('x')
6585 >>> s = Solver()
6586 >>> s.add(x > 0, x < 2, f(x) == 0)
6587 >>> s.check()
6588 sat
6589 >>> m = s.model()
6590 >>> m[x]
6591 1
6592 >>> m[f]
6593 [else -> 0]
6594 """
6595 if z3_debug():
6596 _z3_assert(isinstance(decl, FuncDeclRef) or is_const(decl), "Z3 declaration expected")
6597 if is_const(decl):
6598 decl = decl.decl()
6599 try:
6600 if decl.arity() == 0:
6601 _r = Z3_model_get_const_interp(self.ctx.ref(), self.model, decl.ast)
6602 if _r.value is None:
6603 return None
6604 r = _to_expr_ref(_r, self.ctx)
6605 if is_as_array(r):
6606 fi = self.get_interp(get_as_array_func(r))
6607 if fi is None:
6608 return fi
6609 e = fi.else_value()
6610 if e is None:
6611 return fi
6612 if fi.arity() != 1:
6613 return fi
6614 srt = decl.range()
6615 dom = srt.domain()
6616 e = K(dom, e)
6617 i = 0
6618 sz = fi.num_entries()
6619 n = fi.arity()
6620 while i < sz:
6621 fe = fi.entry(i)
6622 e = Store(e, fe.arg_value(0), fe.value())
6623 i += 1
6624 return e
6625 else:
6626 return r
6627 else:
6628 return FuncInterp(Z3_model_get_func_interp(self.ctx.ref(), self.model, decl.ast), self.ctx)
6629 except Z3Exception:
6630 return None
6631
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 6647 of file z3py.py.

6647 def get_sort(self, idx):
6648 """Return the uninterpreted sort at position `idx` < self.num_sorts().
6649
6650 >>> A = DeclareSort('A')
6651 >>> B = DeclareSort('B')
6652 >>> a1, a2 = Consts('a1 a2', A)
6653 >>> b1, b2 = Consts('b1 b2', B)
6654 >>> s = Solver()
6655 >>> s.add(a1 != a2, b1 != b2)
6656 >>> s.check()
6657 sat
6658 >>> m = s.model()
6659 >>> m.num_sorts()
6660 2
6661 >>> m.get_sort(0)
6662 A
6663 >>> m.get_sort(1)
6664 B
6665 """
6666 if idx >= self.num_sorts():
6667 raise IndexError
6668 return _to_sort_ref(Z3_model_get_sort(self.ctx.ref(), self.model, idx), self.ctx)
6669
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 6687 of file z3py.py.

6687 def get_universe(self, s):
6688 """Return the interpretation for the uninterpreted sort `s` in the model `self`.
6689
6690 >>> A = DeclareSort('A')
6691 >>> a, b = Consts('a b', A)
6692 >>> s = Solver()
6693 >>> s.add(a != b)
6694 >>> s.check()
6695 sat
6696 >>> m = s.model()
6697 >>> m.get_universe(A)
6698 [A!val!1, A!val!0]
6699 """
6700 if z3_debug():
6701 _z3_assert(isinstance(s, SortRef), "Z3 sort expected")
6702 try:
6703 return AstVector(Z3_model_get_sort_universe(self.ctx.ref(), self.model, s.ast), self.ctx)
6704 except Z3Exception:
6705 return None
6706
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 6632 of file z3py.py.

6632 def num_sorts(self):
6633 """Return the number of uninterpreted sorts that contain an interpretation in the model `self`.
6634
6635 >>> A = DeclareSort('A')
6636 >>> a, b = Consts('a b', A)
6637 >>> s = Solver()
6638 >>> s.add(a != b)
6639 >>> s.check()
6640 sat
6641 >>> m = s.model()
6642 >>> m.num_sorts()
6643 1
6644 """
6645 return int(Z3_model_get_num_sorts(self.ctx.ref(), self.model))
6646
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 6801 of file z3py.py.

6801 def project(self, vars, fml):
6802 """Perform model-based projection on fml with respect to vars.
6803 Assume that the model satisfies fml. Then compute a projection fml_p, such
6804 that vars do not occur free in fml_p, fml_p is true in the model and
6805 fml_p => exists vars . fml
6806 """
6807 ctx = self.ctx.ref()
6808 _vars = (Ast * len(vars))()
6809 for i in range(len(vars)):
6810 _vars[i] = vars[i].as_ast()
6811 return _to_expr_ref(Z3_qe_model_project(ctx, self.model, len(vars), _vars, fml.ast), self.ctx)
6812

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

6813 def project_with_witness(self, vars, fml):
6814 """Perform model-based projection, but also include realizer terms for the projected variables"""
6815 ctx = self.ctx.ref()
6816 _vars = (Ast * len(vars))()
6817 for i in range(len(vars)):
6818 _vars[i] = vars[i].as_ast()
6819 defs = AstMap()
6820 result = Z3_qe_model_project_with_witness(ctx, self.model, len(vars), _vars, fml.ast, defs.map)
6821 result = _to_expr_ref(result, self.ctx)
6822 return result, defs
6823
6824

◆ sexpr()

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

Definition at line 6502 of file z3py.py.

6502 def sexpr(self):
6503 """Return a textual representation of the s-expression representing the model."""
6504 return Z3_model_to_string(self.ctx.ref(), self.model)
6505
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 6670 of file z3py.py.

6670 def sorts(self):
6671 """Return all uninterpreted sorts that have an interpretation in the model `self`.
6672
6673 >>> A = DeclareSort('A')
6674 >>> B = DeclareSort('B')
6675 >>> a1, a2 = Consts('a1 a2', A)
6676 >>> b1, b2 = Consts('b1 b2', B)
6677 >>> s = Solver()
6678 >>> s.add(a1 != a2, b1 != b2)
6679 >>> s.check()
6680 sat
6681 >>> m = s.model()
6682 >>> m.sorts()
6683 [A, B]
6684 """
6685 return [self.get_sort(i) for i in range(self.num_sorts())]
6686

◆ translate()

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

Definition at line 6793 of file z3py.py.

6793 def translate(self, target):
6794 """Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.
6795 """
6796 if z3_debug():
6797 _z3_assert(isinstance(target, Context), "argument must be a Z3 context")
6798 model = Z3_model_translate(self.ctx.ref(), self.model, target.ref())
6799 return ModelRef(model, target)
6800
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 6771 of file z3py.py.

6771 def update_value(self, x, value):
6772 """Update the interpretation of a constant"""
6773 if is_expr(x):
6774 x = x.decl()
6775 if is_func_decl(x) and x.arity() != 0 and isinstance(value, FuncInterp):
6776 fi1 = value.f
6777 fi2 = Z3_add_func_interp(x.ctx_ref(), self.model, x.ast, value.else_value().ast);
6778 fi2 = FuncInterp(fi2, x.ctx)
6779 for i in range(value.num_entries()):
6780 e = value.entry(i)
6781 n = Z3_func_entry_get_num_args(x.ctx_ref(), e.entry)
6782 v = AstVector()
6783 for j in range(n):
6784 v.push(e.arg_value(j))
6785 val = Z3_func_entry_get_value(x.ctx_ref(), e.entry)
6786 Z3_func_interp_add_entry(x.ctx_ref(), fi2.f, v.vector, val)
6787 return
6788 if not is_func_decl(x) or x.arity() != 0:
6789 raise Z3Exception("Expecting 0-ary function or constant expression")
6790 value = _py2expr(value)
6791 Z3_add_const_interp(x.ctx_ref(), self.model, x.ast, value.ast)
6792
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