Z3
Public Member Functions | Data Fields
ModelRef Class Reference
+ Inheritance diagram for ModelRef:

Public Member Functions

def __init__ (self, m, ctx)
 
def __del__ (self)
 
def __repr__ (self)
 
def sexpr (self)
 
def eval (self, t, model_completion=False)
 
def evaluate (self, t, model_completion=False)
 
def __len__ (self)
 
def get_interp (self, decl)
 
def num_sorts (self)
 
def get_sort (self, idx)
 
def sorts (self)
 
def get_universe (self, s)
 
def __getitem__ (self, idx)
 
def decls (self)
 
- Public Member Functions inherited from Z3PPObject
def use_pp (self)
 

Data Fields

 model
 
 ctx
 

Detailed Description

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

Definition at line 5455 of file z3py.py.

Constructor & Destructor Documentation

§ __init__()

def __init__ (   self,
  m,
  ctx 
)

Definition at line 5458 of file z3py.py.

5458  def __init__(self, m, ctx):
5459  assert ctx is not None
5460  self.model = m
5461  self.ctx = ctx
5462  Z3_model_inc_ref(self.ctx.ref(), self.model)
5463 
void Z3_API Z3_model_inc_ref(Z3_context c, Z3_model m)
Increment the reference counter of the given model.

§ __del__()

def __del__ (   self)

Definition at line 5464 of file z3py.py.

5464  def __del__(self):
5465  if self.ctx.ref() is not None:
5466  Z3_model_dec_ref(self.ctx.ref(), self.model)
5467 
void Z3_API Z3_model_dec_ref(Z3_context c, Z3_model m)
Decrement the reference counter of the given model.

Member Function Documentation

§ __getitem__()

def __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 interpreation 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]
[1 -> 0, else -> 0]
>>> for d in m: print("%s -> %s" % (d, m[d]))
x -> 1
f -> [1 -> 0, else -> 0]

Definition at line 5654 of file z3py.py.

5654  def __getitem__(self, idx):
5655  """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 interpreation is returned.
5656 
5657  The elements can be retrieved using position or the actual declaration.
5658 
5659  >>> f = Function('f', IntSort(), IntSort())
5660  >>> x = Int('x')
5661  >>> s = Solver()
5662  >>> s.add(x > 0, x < 2, f(x) == 0)
5663  >>> s.check()
5664  sat
5665  >>> m = s.model()
5666  >>> len(m)
5667  2
5668  >>> m[0]
5669  x
5670  >>> m[1]
5671  f
5672  >>> m[x]
5673  1
5674  >>> m[f]
5675  [1 -> 0, else -> 0]
5676  >>> for d in m: print("%s -> %s" % (d, m[d]))
5677  x -> 1
5678  f -> [1 -> 0, else -> 0]
5679  """
5680  if _is_int(idx):
5681  if idx >= len(self):
5682  raise IndexError
5683  num_consts = Z3_model_get_num_consts(self.ctx.ref(), self.model)
5684  if (idx < num_consts):
5685  return FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, idx), self.ctx)
5686  else:
5687  return FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, idx - num_consts), self.ctx)
5688  if isinstance(idx, FuncDeclRef):
5689  return self.get_interp(idx)
5690  if is_const(idx):
5691  return self.get_interp(idx.decl())
5692  if isinstance(idx, SortRef):
5693  return self.get_universe(idx)
5694  if __debug__:
5695  _z3_assert(False, "Integer, Z3 declaration, or Z3 constant expected")
5696  return None
5697 
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.
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.
def is_const(a)
Definition: z3py.py:1054
unsigned Z3_API Z3_model_get_num_consts(Z3_context c, Z3_model m)
Return the number of constants assigned by the given model.

§ __len__()

def __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 5530 of file z3py.py.

5530  def __len__(self):
5531  """Return the number of constant and function declarations in the model `self`.
5532 
5533  >>> f = Function('f', IntSort(), IntSort())
5534  >>> x = Int('x')
5535  >>> s = Solver()
5536  >>> s.add(x > 0, f(x) != x)
5537  >>> s.check()
5538  sat
5539  >>> m = s.model()
5540  >>> len(m)
5541  2
5542  """
5543  return int(Z3_model_get_num_consts(self.ctx.ref(), self.model)) + int(Z3_model_get_num_funcs(self.ctx.ref(), self.model))
5544 
unsigned Z3_API Z3_model_get_num_consts(Z3_context c, Z3_model m)
Return the number of constants assigned by the given model.
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__()

def __repr__ (   self)

Definition at line 5468 of file z3py.py.

5468  def __repr__(self):
5469  return obj_to_string(self)
5470 

§ decls()

def decls (   self)
Return a list with all symbols that have an interpreation 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 5698 of file z3py.py.

5698  def decls(self):
5699  """Return a list with all symbols that have an interpreation in the model `self`.
5700  >>> f = Function('f', IntSort(), IntSort())
5701  >>> x = Int('x')
5702  >>> s = Solver()
5703  >>> s.add(x > 0, x < 2, f(x) == 0)
5704  >>> s.check()
5705  sat
5706  >>> m = s.model()
5707  >>> m.decls()
5708  [x, f]
5709  """
5710  r = []
5711  for i in range(Z3_model_get_num_consts(self.ctx.ref(), self.model)):
5712  r.append(FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, i), self.ctx))
5713  for i in range(Z3_model_get_num_funcs(self.ctx.ref(), self.model)):
5714  r.append(FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, i), self.ctx))
5715  return r
5716 
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.
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.
unsigned Z3_API Z3_model_get_num_consts(Z3_context c, Z3_model m)
Return the number of constants assigned by the given model.
unsigned Z3_API Z3_model_get_num_funcs(Z3_context c, Z3_model m)
Return the number of function interpretations in the given model.

§ eval()

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

5475  def eval(self, t, model_completion=False):
5476  """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`.
5477 
5478  >>> x = Int('x')
5479  >>> s = Solver()
5480  >>> s.add(x > 0, x < 2)
5481  >>> s.check()
5482  sat
5483  >>> m = s.model()
5484  >>> m.eval(x + 1)
5485  2
5486  >>> m.eval(x == 1)
5487  True
5488  >>> y = Int('y')
5489  >>> m.eval(y + x)
5490  1 + y
5491  >>> m.eval(y)
5492  y
5493  >>> m.eval(y, model_completion=True)
5494  0
5495  >>> # Now, m contains an interpretation for y
5496  >>> m.eval(y + x)
5497  1
5498  """
5499  r = (Ast * 1)()
5500  if Z3_model_eval(self.ctx.ref(), self.model, t.as_ast(), model_completion, r):
5501  return _to_expr_ref(r[0], self.ctx)
5502  raise Z3Exception("failed to evaluate expression in the model")
5503 
Z3_bool Z3_API Z3_model_eval(Z3_context c, Z3_model m, Z3_ast t, Z3_bool model_completion, Z3_ast *v)
Evaluate the AST node t in the given model. Return Z3_TRUE if succeeded, and store the result in v...

§ evaluate()

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

5504  def evaluate(self, t, model_completion=False):
5505  """Alias for `eval`.
5506 
5507  >>> x = Int('x')
5508  >>> s = Solver()
5509  >>> s.add(x > 0, x < 2)
5510  >>> s.check()
5511  sat
5512  >>> m = s.model()
5513  >>> m.evaluate(x + 1)
5514  2
5515  >>> m.evaluate(x == 1)
5516  True
5517  >>> y = Int('y')
5518  >>> m.evaluate(y + x)
5519  1 + y
5520  >>> m.evaluate(y)
5521  y
5522  >>> m.evaluate(y, model_completion=True)
5523  0
5524  >>> # Now, m contains an interpretation for y
5525  >>> m.evaluate(y + x)
5526  1
5527  """
5528  return self.eval(t, model_completion)
5529 

§ get_interp()

def 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]
[1 -> 0, else -> 0]

Definition at line 5545 of file z3py.py.

5545  def get_interp(self, decl):
5546  """Return the interpretation for a given declaration or constant.
5547 
5548  >>> f = Function('f', IntSort(), IntSort())
5549  >>> x = Int('x')
5550  >>> s = Solver()
5551  >>> s.add(x > 0, x < 2, f(x) == 0)
5552  >>> s.check()
5553  sat
5554  >>> m = s.model()
5555  >>> m[x]
5556  1
5557  >>> m[f]
5558  [1 -> 0, else -> 0]
5559  """
5560  if __debug__:
5561  _z3_assert(isinstance(decl, FuncDeclRef) or is_const(decl), "Z3 declaration expected")
5562  if is_const(decl):
5563  decl = decl.decl()
5564  try:
5565  if decl.arity() == 0:
5566  _r = Z3_model_get_const_interp(self.ctx.ref(), self.model, decl.ast)
5567  if _r.value is None:
5568  return None
5569  r = _to_expr_ref(_r, self.ctx)
5570  if is_as_array(r):
5571  return self.get_interp(get_as_array_func(r))
5572  else:
5573  return r
5574  else:
5575  return FuncInterp(Z3_model_get_func_interp(self.ctx.ref(), self.model, decl.ast), self.ctx)
5576  except Z3Exception:
5577  return None
5578 
def is_const(a)
Definition: z3py.py:1054
def get_as_array_func(n)
Definition: z3py.py:5721
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...
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...
def is_as_array(n)
Definition: z3py.py:5717

§ get_sort()

def get_sort (   self,
  idx 
)
Return the unintepreted 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 5594 of file z3py.py.

5594  def get_sort(self, idx):
5595  """Return the unintepreted sort at position `idx` < self.num_sorts().
5596 
5597  >>> A = DeclareSort('A')
5598  >>> B = DeclareSort('B')
5599  >>> a1, a2 = Consts('a1 a2', A)
5600  >>> b1, b2 = Consts('b1 b2', B)
5601  >>> s = Solver()
5602  >>> s.add(a1 != a2, b1 != b2)
5603  >>> s.check()
5604  sat
5605  >>> m = s.model()
5606  >>> m.num_sorts()
5607  2
5608  >>> m.get_sort(0)
5609  A
5610  >>> m.get_sort(1)
5611  B
5612  """
5613  if idx >= self.num_sorts():
5614  raise IndexError
5615  return _to_sort_ref(Z3_model_get_sort(self.ctx.ref(), self.model, idx), self.ctx)
5616 
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.

§ get_universe()

def get_universe (   self,
  s 
)
Return the intepretation 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!0, A!val!1]

Definition at line 5634 of file z3py.py.

5634  def get_universe(self, s):
5635  """Return the intepretation for the uninterpreted sort `s` in the model `self`.
5636 
5637  >>> A = DeclareSort('A')
5638  >>> a, b = Consts('a b', A)
5639  >>> s = Solver()
5640  >>> s.add(a != b)
5641  >>> s.check()
5642  sat
5643  >>> m = s.model()
5644  >>> m.get_universe(A)
5645  [A!val!0, A!val!1]
5646  """
5647  if __debug__:
5648  _z3_assert(isinstance(s, SortRef), "Z3 sort expected")
5649  try:
5650  return AstVector(Z3_model_get_sort_universe(self.ctx.ref(), self.model, s.ast), self.ctx)
5651  except Z3Exception:
5652  return None
5653 
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...

§ num_sorts()

def num_sorts (   self)
Return the number of unintepreted 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 5579 of file z3py.py.

Referenced by ModelRef.get_sort().

5579  def num_sorts(self):
5580  """Return the number of unintepreted sorts that contain an interpretation in the model `self`.
5581 
5582  >>> A = DeclareSort('A')
5583  >>> a, b = Consts('a b', A)
5584  >>> s = Solver()
5585  >>> s.add(a != b)
5586  >>> s.check()
5587  sat
5588  >>> m = s.model()
5589  >>> m.num_sorts()
5590  1
5591  """
5592  return int(Z3_model_get_num_sorts(self.ctx.ref(), self.model))
5593 
unsigned Z3_API Z3_model_get_num_sorts(Z3_context c, Z3_model m)
Return the number of uninterpreted sorts that m assigs an interpretation to.

§ sexpr()

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

Definition at line 5471 of file z3py.py.

Referenced by Fixedpoint.__repr__(), and Optimize.__repr__().

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

§ sorts()

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

5617  def sorts(self):
5618  """Return all uninterpreted sorts that have an interpretation in the model `self`.
5619 
5620  >>> A = DeclareSort('A')
5621  >>> B = DeclareSort('B')
5622  >>> a1, a2 = Consts('a1 a2', A)
5623  >>> b1, b2 = Consts('b1 b2', B)
5624  >>> s = Solver()
5625  >>> s.add(a1 != a2, b1 != b2)
5626  >>> s.check()
5627  sat
5628  >>> m = s.model()
5629  >>> m.sorts()
5630  [A, B]
5631  """
5632  return [ self.get_sort(i) for i in range(self.num_sorts()) ]
5633 

Field Documentation

§ ctx

ctx

§ model

model

Definition at line 5460 of file z3py.py.