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

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self,
m,
ctx )

Definition at line 6423 of file z3py.py.

6423 def __init__(self, m, ctx):
6424 assert ctx is not None
6425 self.model = m
6426 self.ctx = ctx
6427 Z3_model_inc_ref(self.ctx.ref(), self.model)
6428
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 6429 of file z3py.py.

6429 def __del__(self):
6430 if self.ctx.ref() is not None and Z3_model_dec_ref is not None:
6431 Z3_model_dec_ref(self.ctx.ref(), self.model)
6432
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 6759 of file z3py.py.

6759 def __copy__(self):
6760 return self.translate(self.ctx)
6761

◆ __deepcopy__()

__deepcopy__ ( self,
memo = {} )

Definition at line 6762 of file z3py.py.

6762 def __deepcopy__(self, memo={}):
6763 return self.translate(self.ctx)
6764
6765

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

6641 def __getitem__(self, idx):
6642 """If `idx` is an integer, then the declaration at position `idx` in the model `self` is returned.
6643 If `idx` is a declaration, then the actual interpretation is returned.
6644
6645 The elements can be retrieved using position or the actual declaration.
6646
6647 >>> f = Function('f', IntSort(), IntSort())
6648 >>> x = Int('x')
6649 >>> s = Solver()
6650 >>> s.add(x > 0, x < 2, f(x) == 0)
6651 >>> s.check()
6652 sat
6653 >>> m = s.model()
6654 >>> len(m)
6655 2
6656 >>> m[0]
6657 x
6658 >>> m[1]
6659 f
6660 >>> m[x]
6661 1
6662 >>> m[f]
6663 [else -> 0]
6664 >>> for d in m: print("%s -> %s" % (d, m[d]))
6665 x -> 1
6666 f -> [else -> 0]
6667 """
6668 if _is_int(idx):
6669 if idx >= len(self):
6670 raise IndexError
6671 num_consts = Z3_model_get_num_consts(self.ctx.ref(), self.model)
6672 if (idx < num_consts):
6673 return FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, idx), self.ctx)
6674 else:
6675 return FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, idx - num_consts), self.ctx)
6676 if isinstance(idx, FuncDeclRef):
6677 return self.get_interp(idx)
6678 if is_const(idx):
6679 return self.get_interp(idx.decl())
6680 if isinstance(idx, SortRef):
6681 return self.get_universe(idx)
6682 if z3_debug():
6683 _z3_assert(False, "Integer, Z3 declaration, or Z3 constant expected")
6684 return None
6685
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 6497 of file z3py.py.

6497 def __len__(self):
6498 """Return the number of constant and function declarations in the model `self`.
6499
6500 >>> f = Function('f', IntSort(), IntSort())
6501 >>> x = Int('x')
6502 >>> s = Solver()
6503 >>> s.add(x > 0, f(x) != x)
6504 >>> s.check()
6505 sat
6506 >>> m = s.model()
6507 >>> len(m)
6508 2
6509 """
6510 num_consts = int(Z3_model_get_num_consts(self.ctx.ref(), self.model))
6511 num_funcs = int(Z3_model_get_num_funcs(self.ctx.ref(), self.model))
6512 return num_consts + num_funcs
6513
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 6433 of file z3py.py.

6433 def __repr__(self):
6434 return obj_to_string(self)
6435

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

6686 def decls(self):
6687 """Return a list with all symbols that have an interpretation in the model `self`.
6688 >>> f = Function('f', IntSort(), IntSort())
6689 >>> x = Int('x')
6690 >>> s = Solver()
6691 >>> s.add(x > 0, x < 2, f(x) == 0)
6692 >>> s.check()
6693 sat
6694 >>> m = s.model()
6695 >>> m.decls()
6696 [x, f]
6697 """
6698 r = []
6699 for i in range(Z3_model_get_num_consts(self.ctx.ref(), self.model)):
6700 r.append(FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, i), self.ctx))
6701 for i in range(Z3_model_get_num_funcs(self.ctx.ref(), self.model)):
6702 r.append(FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, i), self.ctx))
6703 return r
6704

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

6440 def eval(self, t, model_completion=False):
6441 """Evaluate the expression `t` in the model `self`.
6442 If `model_completion` is enabled, then a default interpretation is automatically added
6443 for symbols that do not have an interpretation in the model `self`.
6444
6445 >>> x = Int('x')
6446 >>> s = Solver()
6447 >>> s.add(x > 0, x < 2)
6448 >>> s.check()
6449 sat
6450 >>> m = s.model()
6451 >>> m.eval(x + 1)
6452 2
6453 >>> m.eval(x == 1)
6454 True
6455 >>> y = Int('y')
6456 >>> m.eval(y + x)
6457 1 + y
6458 >>> m.eval(y)
6459 y
6460 >>> m.eval(y, model_completion=True)
6461 0
6462 >>> # Now, m contains an interpretation for y
6463 >>> m.eval(y + x)
6464 1
6465 """
6466 r = (Ast * 1)()
6467 if Z3_model_eval(self.ctx.ref(), self.model, t.as_ast(), model_completion, r):
6468 return _to_expr_ref(r[0], self.ctx)
6469 raise Z3Exception("failed to evaluate expression in the model")
6470
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 6471 of file z3py.py.

6471 def evaluate(self, t, model_completion=False):
6472 """Alias for `eval`.
6473
6474 >>> x = Int('x')
6475 >>> s = Solver()
6476 >>> s.add(x > 0, x < 2)
6477 >>> s.check()
6478 sat
6479 >>> m = s.model()
6480 >>> m.evaluate(x + 1)
6481 2
6482 >>> m.evaluate(x == 1)
6483 True
6484 >>> y = Int('y')
6485 >>> m.evaluate(y + x)
6486 1 + y
6487 >>> m.evaluate(y)
6488 y
6489 >>> m.evaluate(y, model_completion=True)
6490 0
6491 >>> # Now, m contains an interpretation for y
6492 >>> m.evaluate(y + x)
6493 1
6494 """
6495 return self.eval(t, model_completion)
6496

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

6514 def get_interp(self, decl):
6515 """Return the interpretation for a given declaration or constant.
6516
6517 >>> f = Function('f', IntSort(), IntSort())
6518 >>> x = Int('x')
6519 >>> s = Solver()
6520 >>> s.add(x > 0, x < 2, f(x) == 0)
6521 >>> s.check()
6522 sat
6523 >>> m = s.model()
6524 >>> m[x]
6525 1
6526 >>> m[f]
6527 [else -> 0]
6528 """
6529 if z3_debug():
6530 _z3_assert(isinstance(decl, FuncDeclRef) or is_const(decl), "Z3 declaration expected")
6531 if is_const(decl):
6532 decl = decl.decl()
6533 try:
6534 if decl.arity() == 0:
6535 _r = Z3_model_get_const_interp(self.ctx.ref(), self.model, decl.ast)
6536 if _r.value is None:
6537 return None
6538 r = _to_expr_ref(_r, self.ctx)
6539 if is_as_array(r):
6540 fi = self.get_interp(get_as_array_func(r))
6541 if fi is None:
6542 return fi
6543 e = fi.else_value()
6544 if e is None:
6545 return fi
6546 if fi.arity() != 1:
6547 return fi
6548 srt = decl.range()
6549 dom = srt.domain()
6550 e = K(dom, e)
6551 i = 0
6552 sz = fi.num_entries()
6553 n = fi.arity()
6554 while i < sz:
6555 fe = fi.entry(i)
6556 e = Store(e, fe.arg_value(0), fe.value())
6557 i += 1
6558 return e
6559 else:
6560 return r
6561 else:
6562 return FuncInterp(Z3_model_get_func_interp(self.ctx.ref(), self.model, decl.ast), self.ctx)
6563 except Z3Exception:
6564 return None
6565
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 6581 of file z3py.py.

6581 def get_sort(self, idx):
6582 """Return the uninterpreted sort at position `idx` < self.num_sorts().
6583
6584 >>> A = DeclareSort('A')
6585 >>> B = DeclareSort('B')
6586 >>> a1, a2 = Consts('a1 a2', A)
6587 >>> b1, b2 = Consts('b1 b2', B)
6588 >>> s = Solver()
6589 >>> s.add(a1 != a2, b1 != b2)
6590 >>> s.check()
6591 sat
6592 >>> m = s.model()
6593 >>> m.num_sorts()
6594 2
6595 >>> m.get_sort(0)
6596 A
6597 >>> m.get_sort(1)
6598 B
6599 """
6600 if idx >= self.num_sorts():
6601 raise IndexError
6602 return _to_sort_ref(Z3_model_get_sort(self.ctx.ref(), self.model, idx), self.ctx)
6603
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 6621 of file z3py.py.

6621 def get_universe(self, s):
6622 """Return the interpretation for the uninterpreted sort `s` in the model `self`.
6623
6624 >>> A = DeclareSort('A')
6625 >>> a, b = Consts('a b', A)
6626 >>> s = Solver()
6627 >>> s.add(a != b)
6628 >>> s.check()
6629 sat
6630 >>> m = s.model()
6631 >>> m.get_universe(A)
6632 [A!val!1, A!val!0]
6633 """
6634 if z3_debug():
6635 _z3_assert(isinstance(s, SortRef), "Z3 sort expected")
6636 try:
6637 return AstVector(Z3_model_get_sort_universe(self.ctx.ref(), self.model, s.ast), self.ctx)
6638 except Z3Exception:
6639 return None
6640
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 6566 of file z3py.py.

6566 def num_sorts(self):
6567 """Return the number of uninterpreted sorts that contain an interpretation in the model `self`.
6568
6569 >>> A = DeclareSort('A')
6570 >>> a, b = Consts('a b', A)
6571 >>> s = Solver()
6572 >>> s.add(a != b)
6573 >>> s.check()
6574 sat
6575 >>> m = s.model()
6576 >>> m.num_sorts()
6577 1
6578 """
6579 return int(Z3_model_get_num_sorts(self.ctx.ref(), self.model))
6580
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 6735 of file z3py.py.

6735 def project(self, vars, fml):
6736 """Perform model-based projection on fml with respect to vars.
6737 Assume that the model satisfies fml. Then compute a projection fml_p, such
6738 that vars do not occur free in fml_p, fml_p is true in the model and
6739 fml_p => exists vars . fml
6740 """
6741 ctx = self.ctx.ref()
6742 _vars = (Ast * len(vars))()
6743 for i in range(len(vars)):
6744 _vars[i] = vars[i].as_ast()
6745 return _to_expr_ref(Z3_qe_model_project(ctx, self.model, len(vars), _vars, fml.ast), self.ctx)
6746

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

6747 def project_with_witness(self, vars, fml):
6748 """Perform model-based projection, but also include realizer terms for the projected variables"""
6749 ctx = self.ctx.ref()
6750 _vars = (Ast * len(vars))()
6751 for i in range(len(vars)):
6752 _vars[i] = vars[i].as_ast()
6753 defs = AstMap()
6754 result = Z3_qe_model_project_with_witness(ctx, self.model, len(vars), _vars, fml.ast, defs.map)
6755 result = _to_expr_ref(result, self.ctx)
6756 return result, defs
6757
6758

◆ sexpr()

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

Definition at line 6436 of file z3py.py.

6436 def sexpr(self):
6437 """Return a textual representation of the s-expression representing the model."""
6438 return Z3_model_to_string(self.ctx.ref(), self.model)
6439
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 6604 of file z3py.py.

6604 def sorts(self):
6605 """Return all uninterpreted sorts that have an interpretation in the model `self`.
6606
6607 >>> A = DeclareSort('A')
6608 >>> B = DeclareSort('B')
6609 >>> a1, a2 = Consts('a1 a2', A)
6610 >>> b1, b2 = Consts('b1 b2', B)
6611 >>> s = Solver()
6612 >>> s.add(a1 != a2, b1 != b2)
6613 >>> s.check()
6614 sat
6615 >>> m = s.model()
6616 >>> m.sorts()
6617 [A, B]
6618 """
6619 return [self.get_sort(i) for i in range(self.num_sorts())]
6620

◆ translate()

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

Definition at line 6727 of file z3py.py.

6727 def translate(self, target):
6728 """Translate `self` to the context `target`. That is, return a copy of `self` in the context `target`.
6729 """
6730 if z3_debug():
6731 _z3_assert(isinstance(target, Context), "argument must be a Z3 context")
6732 model = Z3_model_translate(self.ctx.ref(), self.model, target.ref())
6733 return ModelRef(model, target)
6734
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 6705 of file z3py.py.

6705 def update_value(self, x, value):
6706 """Update the interpretation of a constant"""
6707 if is_expr(x):
6708 x = x.decl()
6709 if is_func_decl(x) and x.arity() != 0 and isinstance(value, FuncInterp):
6710 fi1 = value.f
6711 fi2 = Z3_add_func_interp(x.ctx_ref(), self.model, x.ast, value.else_value().ast);
6712 fi2 = FuncInterp(fi2, x.ctx)
6713 for i in range(value.num_entries()):
6714 e = value.entry(i)
6715 n = Z3_func_entry_get_num_args(x.ctx_ref(), e.entry)
6716 v = AstVector()
6717 for j in range(n):
6718 v.push(e.arg_value(j))
6719 val = Z3_func_entry_get_value(x.ctx_ref(), e.entry)
6720 Z3_func_interp_add_entry(x.ctx_ref(), fi2.f, v.vector, val)
6721 return
6722 if not is_func_decl(x) or x.arity() != 0:
6723 raise Z3Exception("Expecting 0-ary function or constant expression")
6724 value = _py2expr(value)
6725 Z3_add_const_interp(x.ctx_ref(), self.model, x.ast, value.ast)
6726
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