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

Constructor & Destructor Documentation

def __init__ (   self,
  m,
  ctx 
)

Definition at line 5334 of file z3py.py.

5334  def __init__(self, m, ctx):
5335  assert ctx != None
5336  self.model = m
5337  self.ctx = ctx
5338  Z3_model_inc_ref(self.ctx.ref(), self.model)
5339 
def __init__(self, m, ctx)
Definition: z3py.py:5334
void Z3_API Z3_model_inc_ref(Z3_context c, Z3_model m)
Increment the reference counter of the given model.
def __del__ (   self)

Definition at line 5340 of file z3py.py.

5340  def __del__(self):
5341  Z3_model_dec_ref(self.ctx.ref(), self.model)
5342 
def __del__(self)
Definition: z3py.py:5340
void Z3_API Z3_model_dec_ref(Z3_context c, Z3_model m)
Decrement the reference counter of the given model.

Member Function Documentation

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

5526  def __getitem__(self, idx):
5527  """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.
5528 
5529  The elements can be retrieved using position or the actual declaration.
5530 
5531  >>> f = Function('f', IntSort(), IntSort())
5532  >>> x = Int('x')
5533  >>> s = Solver()
5534  >>> s.add(x > 0, x < 2, f(x) == 0)
5535  >>> s.check()
5536  sat
5537  >>> m = s.model()
5538  >>> len(m)
5539  2
5540  >>> m[0]
5541  x
5542  >>> m[1]
5543  f
5544  >>> m[x]
5545  1
5546  >>> m[f]
5547  [1 -> 0, else -> 0]
5548  >>> for d in m: print("%s -> %s" % (d, m[d]))
5549  x -> 1
5550  f -> [1 -> 0, else -> 0]
5551  """
5552  if isinstance(idx, int):
5553  if idx >= len(self):
5554  raise IndexError
5555  num_consts = Z3_model_get_num_consts(self.ctx.ref(), self.model)
5556  if (idx < num_consts):
5557  return FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, idx), self.ctx)
5558  else:
5559  return FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, idx - num_consts), self.ctx)
5560  if isinstance(idx, FuncDeclRef):
5561  return self.get_interp(idx)
5562  if is_const(idx):
5563  return self.get_interp(idx.decl())
5564  if isinstance(idx, SortRef):
5565  return self.get_universe(idx)
5566  if __debug__:
5567  _z3_assert(False, "Integer, Z3 declaration, or Z3 constant expected")
5568  return None
5569 
Function Declarations.
Definition: z3py.py:591
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 get_universe(self, s)
Definition: z3py.py:5506
def is_const(a)
Definition: z3py.py:1006
def __getitem__(self, idx)
Definition: z3py.py:5526
unsigned Z3_API Z3_model_get_num_consts(Z3_context c, Z3_model m)
Return the number of constants assigned by the given model.
def get_interp(self, decl)
Definition: z3py.py:5420
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 5405 of file z3py.py.

5405  def __len__(self):
5406  """Return the number of constant and function declarations in the model `self`.
5407 
5408  >>> f = Function('f', IntSort(), IntSort())
5409  >>> x = Int('x')
5410  >>> s = Solver()
5411  >>> s.add(x > 0, f(x) != x)
5412  >>> s.check()
5413  sat
5414  >>> m = s.model()
5415  >>> len(m)
5416  2
5417  """
5418  return int(Z3_model_get_num_consts(self.ctx.ref(), self.model)) + int(Z3_model_get_num_funcs(self.ctx.ref(), self.model))
5419 
def __len__(self)
Definition: z3py.py:5405
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.
def __repr__ (   self)

Definition at line 5343 of file z3py.py.

5343  def __repr__(self):
5344  return obj_to_string(self)
5345 
def __repr__(self)
Definition: z3py.py:5343
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 5570 of file z3py.py.

5570  def decls(self):
5571  """Return a list with all symbols that have an interpreation in the model `self`.
5572  >>> f = Function('f', IntSort(), IntSort())
5573  >>> x = Int('x')
5574  >>> s = Solver()
5575  >>> s.add(x > 0, x < 2, f(x) == 0)
5576  >>> s.check()
5577  sat
5578  >>> m = s.model()
5579  >>> m.decls()
5580  [x, f]
5581  """
5582  r = []
5583  for i in range(Z3_model_get_num_consts(self.ctx.ref(), self.model)):
5584  r.append(FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, i), self.ctx))
5585  for i in range(Z3_model_get_num_funcs(self.ctx.ref(), self.model)):
5586  r.append(FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, i), self.ctx))
5587  return r
5588 
Function Declarations.
Definition: z3py.py:591
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.
def decls(self)
Definition: z3py.py:5570
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 5350 of file z3py.py.

5350  def eval(self, t, model_completion=False):
5351  """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`.
5352 
5353  >>> x = Int('x')
5354  >>> s = Solver()
5355  >>> s.add(x > 0, x < 2)
5356  >>> s.check()
5357  sat
5358  >>> m = s.model()
5359  >>> m.eval(x + 1)
5360  2
5361  >>> m.eval(x == 1)
5362  True
5363  >>> y = Int('y')
5364  >>> m.eval(y + x)
5365  1 + y
5366  >>> m.eval(y)
5367  y
5368  >>> m.eval(y, model_completion=True)
5369  0
5370  >>> # Now, m contains an interpretation for y
5371  >>> m.eval(y + x)
5372  1
5373  """
5374  r = (Ast * 1)()
5375  if Z3_model_eval(self.ctx.ref(), self.model, t.as_ast(), model_completion, r):
5376  return _to_expr_ref(r[0], self.ctx)
5377  raise Z3Exception("failed to evaluate expression in the model")
5378 
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...
def eval(self, t, model_completion=False)
Definition: z3py.py:5350
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 5379 of file z3py.py.

5379  def evaluate(self, t, model_completion=False):
5380  """Alias for `eval`.
5381 
5382  >>> x = Int('x')
5383  >>> s = Solver()
5384  >>> s.add(x > 0, x < 2)
5385  >>> s.check()
5386  sat
5387  >>> m = s.model()
5388  >>> m.evaluate(x + 1)
5389  2
5390  >>> m.evaluate(x == 1)
5391  True
5392  >>> y = Int('y')
5393  >>> m.evaluate(y + x)
5394  1 + y
5395  >>> m.evaluate(y)
5396  y
5397  >>> m.evaluate(y, model_completion=True)
5398  0
5399  >>> # Now, m contains an interpretation for y
5400  >>> m.evaluate(y + x)
5401  1
5402  """
5403  return self.eval(t, model_completion)
5404 
def eval(self, t, model_completion=False)
Definition: z3py.py:5350
def evaluate(self, t, model_completion=False)
Definition: z3py.py:5379
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 5420 of file z3py.py.

Referenced by ModelRef.__getitem__().

5420  def get_interp(self, decl):
5421  """Return the interpretation for a given declaration or constant.
5422 
5423  >>> f = Function('f', IntSort(), IntSort())
5424  >>> x = Int('x')
5425  >>> s = Solver()
5426  >>> s.add(x > 0, x < 2, f(x) == 0)
5427  >>> s.check()
5428  sat
5429  >>> m = s.model()
5430  >>> m[x]
5431  1
5432  >>> m[f]
5433  [1 -> 0, else -> 0]
5434  """
5435  if __debug__:
5436  _z3_assert(isinstance(decl, FuncDeclRef) or is_const(decl), "Z3 declaration expected")
5437  if is_const(decl):
5438  decl = decl.decl()
5439  try:
5440  if decl.arity() == 0:
5441  r = _to_expr_ref(Z3_model_get_const_interp(self.ctx.ref(), self.model, decl.ast), self.ctx)
5442  if is_as_array(r):
5443  return self.get_interp(get_as_array_func(r))
5444  else:
5445  return r
5446  else:
5447  return FuncInterp(Z3_model_get_func_interp(self.ctx.ref(), self.model, decl.ast), self.ctx)
5448  except Z3Exception:
5449  return None
5450 
def is_const(a)
Definition: z3py.py:1006
def get_as_array_func(n)
Definition: z3py.py:5593
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 get_interp(self, decl)
Definition: z3py.py:5420
def is_as_array(n)
Definition: z3py.py:5589
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 5466 of file z3py.py.

5466  def get_sort(self, idx):
5467  """Return the unintepreted sort at position `idx` < self.num_sorts().
5468 
5469  >>> A = DeclareSort('A')
5470  >>> B = DeclareSort('B')
5471  >>> a1, a2 = Consts('a1 a2', A)
5472  >>> b1, b2 = Consts('b1 b2', B)
5473  >>> s = Solver()
5474  >>> s.add(a1 != a2, b1 != b2)
5475  >>> s.check()
5476  sat
5477  >>> m = s.model()
5478  >>> m.num_sorts()
5479  2
5480  >>> m.get_sort(0)
5481  A
5482  >>> m.get_sort(1)
5483  B
5484  """
5485  if idx >= self.num_sorts():
5486  raise IndexError
5487  return _to_sort_ref(Z3_model_get_sort(self.ctx.ref(), self.model, idx), self.ctx)
5488 
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.
def num_sorts(self)
Definition: z3py.py:5451
def get_sort(self, idx)
Definition: z3py.py:5466
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 5506 of file z3py.py.

Referenced by ModelRef.__getitem__().

5506  def get_universe(self, s):
5507  """Return the intepretation for the uninterpreted sort `s` in the model `self`.
5508 
5509  >>> A = DeclareSort('A')
5510  >>> a, b = Consts('a b', A)
5511  >>> s = Solver()
5512  >>> s.add(a != b)
5513  >>> s.check()
5514  sat
5515  >>> m = s.model()
5516  >>> m.get_universe(A)
5517  [A!val!0, A!val!1]
5518  """
5519  if __debug__:
5520  _z3_assert(isinstance(s, SortRef), "Z3 sort expected")
5521  try:
5522  return AstVector(Z3_model_get_sort_universe(self.ctx.ref(), self.model, s.ast), self.ctx)
5523  except Z3Exception:
5524  return None
5525 
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...
def get_universe(self, s)
Definition: z3py.py:5506
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 5451 of file z3py.py.

Referenced by ModelRef.get_sort().

5451  def num_sorts(self):
5452  """Return the number of unintepreted sorts that contain an interpretation in the model `self`.
5453 
5454  >>> A = DeclareSort('A')
5455  >>> a, b = Consts('a b', A)
5456  >>> s = Solver()
5457  >>> s.add(a != b)
5458  >>> s.check()
5459  sat
5460  >>> m = s.model()
5461  >>> m.num_sorts()
5462  1
5463  """
5464  return int(Z3_model_get_num_sorts(self.ctx.ref(), self.model))
5465 
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.
def num_sorts(self)
Definition: z3py.py:5451
def sexpr (   self)
Return a textual representation of the s-expression representing the model.

Definition at line 5346 of file z3py.py.

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

5346  def sexpr(self):
5347  """Return a textual representation of the s-expression representing the model."""
5348  return Z3_model_to_string(self.ctx.ref(), self.model)
5349 
def sexpr(self)
Definition: z3py.py:5346
Z3_string Z3_API Z3_model_to_string(Z3_context c, Z3_model m)
Convert the given model into a string.
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 5489 of file z3py.py.

5489  def sorts(self):
5490  """Return all uninterpreted sorts that have an interpretation in the model `self`.
5491 
5492  >>> A = DeclareSort('A')
5493  >>> B = DeclareSort('B')
5494  >>> a1, a2 = Consts('a1 a2', A)
5495  >>> b1, b2 = Consts('b1 b2', B)
5496  >>> s = Solver()
5497  >>> s.add(a1 != a2, b1 != b2)
5498  >>> s.check()
5499  sat
5500  >>> m = s.model()
5501  >>> m.sorts()
5502  [A, B]
5503  """
5504  return [ self.get_sort(i) for i in range(self.num_sorts()) ]
5505 
def sorts(self)
Definition: z3py.py:5489
def num_sorts(self)
Definition: z3py.py:5451
def get_sort(self, idx)
Definition: z3py.py:5466

Field Documentation

ctx
model