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

Constructor & Destructor Documentation

def __init__ (   self,
  m,
  ctx 
)

Definition at line 5297 of file z3py.py.

5297  def __init__(self, m, ctx):
5298  assert ctx != None
5299  self.model = m
5300  self.ctx = ctx
5301  Z3_model_inc_ref(self.ctx.ref(), self.model)
5302 
void Z3_API Z3_model_inc_ref(__in Z3_context c, __in Z3_model m)
Increment the reference counter of the given model.
def __init__(self, m, ctx)
Definition: z3py.py:5297
def __del__ (   self)

Definition at line 5303 of file z3py.py.

5303  def __del__(self):
5304  Z3_model_dec_ref(self.ctx.ref(), self.model)
5305 
def __del__(self)
Definition: z3py.py:5303
void Z3_API Z3_model_dec_ref(__in Z3_context c, __in 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 5489 of file z3py.py.

5489  def __getitem__(self, idx):
5490  """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.
5491 
5492  The elements can be retrieved using position or the actual declaration.
5493 
5494  >>> f = Function('f', IntSort(), IntSort())
5495  >>> x = Int('x')
5496  >>> s = Solver()
5497  >>> s.add(x > 0, x < 2, f(x) == 0)
5498  >>> s.check()
5499  sat
5500  >>> m = s.model()
5501  >>> len(m)
5502  2
5503  >>> m[0]
5504  x
5505  >>> m[1]
5506  f
5507  >>> m[x]
5508  1
5509  >>> m[f]
5510  [1 -> 0, else -> 0]
5511  >>> for d in m: print("%s -> %s" % (d, m[d]))
5512  x -> 1
5513  f -> [1 -> 0, else -> 0]
5514  """
5515  if isinstance(idx, int):
5516  if idx >= len(self):
5517  raise IndexError
5518  num_consts = Z3_model_get_num_consts(self.ctx.ref(), self.model)
5519  if (idx < num_consts):
5520  return FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, idx), self.ctx)
5521  else:
5522  return FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, idx - num_consts), self.ctx)
5523  if isinstance(idx, FuncDeclRef):
5524  return self.get_interp(idx)
5525  if is_const(idx):
5526  return self.get_interp(idx.decl())
5527  if isinstance(idx, SortRef):
5528  return self.get_universe(idx)
5529  if __debug__:
5530  _z3_assert(False, "Integer, Z3 declaration, or Z3 constant expected")
5531  return None
5532 
Function Declarations.
Definition: z3py.py:591
Z3_func_decl Z3_API Z3_model_get_func_decl(__in Z3_context c, __in Z3_model m, __in unsigned i)
Return the declaration of the i-th function in the given model.
def get_universe(self, s)
Definition: z3py.py:5469
def is_const(a)
Definition: z3py.py:1006
def __getitem__(self, idx)
Definition: z3py.py:5489
Z3_func_decl Z3_API Z3_model_get_const_decl(__in Z3_context c, __in Z3_model m, __in unsigned i)
Return the i-th constant in the given model.
unsigned Z3_API Z3_model_get_num_consts(__in Z3_context c, __in Z3_model m)
Return the number of constants assigned by the given model.
def get_interp(self, decl)
Definition: z3py.py:5383
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 5368 of file z3py.py.

5368  def __len__(self):
5369  """Return the number of constant and function declarations in the model `self`.
5370 
5371  >>> f = Function('f', IntSort(), IntSort())
5372  >>> x = Int('x')
5373  >>> s = Solver()
5374  >>> s.add(x > 0, f(x) != x)
5375  >>> s.check()
5376  sat
5377  >>> m = s.model()
5378  >>> len(m)
5379  2
5380  """
5381  return int(Z3_model_get_num_consts(self.ctx.ref(), self.model)) + int(Z3_model_get_num_funcs(self.ctx.ref(), self.model))
5382 
def __len__(self)
Definition: z3py.py:5368
unsigned Z3_API Z3_model_get_num_funcs(__in Z3_context c, __in Z3_model m)
Return the number of function interpretations in the given model.
unsigned Z3_API Z3_model_get_num_consts(__in Z3_context c, __in Z3_model m)
Return the number of constants assigned by the given model.
def __repr__ (   self)

Definition at line 5306 of file z3py.py.

5306  def __repr__(self):
5307  return obj_to_string(self)
5308 
def __repr__(self)
Definition: z3py.py:5306
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 5533 of file z3py.py.

5533  def decls(self):
5534  """Return a list with all symbols that have an interpreation in the model `self`.
5535  >>> f = Function('f', IntSort(), IntSort())
5536  >>> x = Int('x')
5537  >>> s = Solver()
5538  >>> s.add(x > 0, x < 2, f(x) == 0)
5539  >>> s.check()
5540  sat
5541  >>> m = s.model()
5542  >>> m.decls()
5543  [x, f]
5544  """
5545  r = []
5546  for i in range(Z3_model_get_num_consts(self.ctx.ref(), self.model)):
5547  r.append(FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, i), self.ctx))
5548  for i in range(Z3_model_get_num_funcs(self.ctx.ref(), self.model)):
5549  r.append(FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, i), self.ctx))
5550  return r
5551 
Function Declarations.
Definition: z3py.py:591
Z3_func_decl Z3_API Z3_model_get_func_decl(__in Z3_context c, __in Z3_model m, __in unsigned i)
Return the declaration of the i-th function in the given model.
unsigned Z3_API Z3_model_get_num_funcs(__in Z3_context c, __in Z3_model m)
Return the number of function interpretations in the given model.
Z3_func_decl Z3_API Z3_model_get_const_decl(__in Z3_context c, __in Z3_model m, __in unsigned i)
Return the i-th constant in the given model.
unsigned Z3_API Z3_model_get_num_consts(__in Z3_context c, __in Z3_model m)
Return the number of constants assigned by the given model.
def decls(self)
Definition: z3py.py:5533
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 5313 of file z3py.py.

5313  def eval(self, t, model_completion=False):
5314  """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`.
5315 
5316  >>> x = Int('x')
5317  >>> s = Solver()
5318  >>> s.add(x > 0, x < 2)
5319  >>> s.check()
5320  sat
5321  >>> m = s.model()
5322  >>> m.eval(x + 1)
5323  2
5324  >>> m.eval(x == 1)
5325  True
5326  >>> y = Int('y')
5327  >>> m.eval(y + x)
5328  1 + y
5329  >>> m.eval(y)
5330  y
5331  >>> m.eval(y, model_completion=True)
5332  0
5333  >>> # Now, m contains an interpretation for y
5334  >>> m.eval(y + x)
5335  1
5336  """
5337  r = (Ast * 1)()
5338  if Z3_model_eval(self.ctx.ref(), self.model, t.as_ast(), model_completion, r):
5339  return _to_expr_ref(r[0], self.ctx)
5340  raise Z3Exception("failed to evaluate expression in the model")
5341 
Z3_bool Z3_API Z3_model_eval(__in Z3_context c, __in Z3_model m, __in Z3_ast t, __in Z3_bool model_completion, __out 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
Definition: z3py.py:5313
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 5342 of file z3py.py.

5342  def evaluate(self, t, model_completion=False):
5343  """Alias for `eval`.
5344 
5345  >>> x = Int('x')
5346  >>> s = Solver()
5347  >>> s.add(x > 0, x < 2)
5348  >>> s.check()
5349  sat
5350  >>> m = s.model()
5351  >>> m.evaluate(x + 1)
5352  2
5353  >>> m.evaluate(x == 1)
5354  True
5355  >>> y = Int('y')
5356  >>> m.evaluate(y + x)
5357  1 + y
5358  >>> m.evaluate(y)
5359  y
5360  >>> m.evaluate(y, model_completion=True)
5361  0
5362  >>> # Now, m contains an interpretation for y
5363  >>> m.evaluate(y + x)
5364  1
5365  """
5366  return self.eval(t, model_completion)
5367 
def evaluate
Definition: z3py.py:5342
def eval
Definition: z3py.py:5313
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 5383 of file z3py.py.

Referenced by ModelRef.__getitem__().

5383  def get_interp(self, decl):
5384  """Return the interpretation for a given declaration or constant.
5385 
5386  >>> f = Function('f', IntSort(), IntSort())
5387  >>> x = Int('x')
5388  >>> s = Solver()
5389  >>> s.add(x > 0, x < 2, f(x) == 0)
5390  >>> s.check()
5391  sat
5392  >>> m = s.model()
5393  >>> m[x]
5394  1
5395  >>> m[f]
5396  [1 -> 0, else -> 0]
5397  """
5398  if __debug__:
5399  _z3_assert(isinstance(decl, FuncDeclRef) or is_const(decl), "Z3 declaration expected")
5400  if is_const(decl):
5401  decl = decl.decl()
5402  try:
5403  if decl.arity() == 0:
5404  r = _to_expr_ref(Z3_model_get_const_interp(self.ctx.ref(), self.model, decl.ast), self.ctx)
5405  if is_as_array(r):
5406  return self.get_interp(get_as_array_func(r))
5407  else:
5408  return r
5409  else:
5410  return FuncInterp(Z3_model_get_func_interp(self.ctx.ref(), self.model, decl.ast), self.ctx)
5411  except Z3Exception:
5412  return None
5413 
Z3_ast Z3_API Z3_model_get_const_interp(__in Z3_context c, __in Z3_model m, __in 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(__in Z3_context c, __in Z3_model m, __in Z3_func_decl f)
Return the interpretation of the function f in the model m. Return NULL, if the model does not assign...
def is_const(a)
Definition: z3py.py:1006
def get_as_array_func(n)
Definition: z3py.py:5556
def get_interp(self, decl)
Definition: z3py.py:5383
def is_as_array(n)
Definition: z3py.py:5552
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 5429 of file z3py.py.

5429  def get_sort(self, idx):
5430  """Return the unintepreted sort at position `idx` < self.num_sorts().
5431 
5432  >>> A = DeclareSort('A')
5433  >>> B = DeclareSort('B')
5434  >>> a1, a2 = Consts('a1 a2', A)
5435  >>> b1, b2 = Consts('b1 b2', B)
5436  >>> s = Solver()
5437  >>> s.add(a1 != a2, b1 != b2)
5438  >>> s.check()
5439  sat
5440  >>> m = s.model()
5441  >>> m.num_sorts()
5442  2
5443  >>> m.get_sort(0)
5444  A
5445  >>> m.get_sort(1)
5446  B
5447  """
5448  if idx >= self.num_sorts():
5449  raise IndexError
5450  return _to_sort_ref(Z3_model_get_sort(self.ctx.ref(), self.model, idx), self.ctx)
5451 
def num_sorts(self)
Definition: z3py.py:5414
def get_sort(self, idx)
Definition: z3py.py:5429
Z3_sort Z3_API Z3_model_get_sort(__in Z3_context c, __in Z3_model m, __in unsigned i)
Return a uninterpreted sort that m assigns an interpretation.
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 5469 of file z3py.py.

Referenced by ModelRef.__getitem__().

5469  def get_universe(self, s):
5470  """Return the intepretation for the uninterpreted sort `s` in the model `self`.
5471 
5472  >>> A = DeclareSort('A')
5473  >>> a, b = Consts('a b', A)
5474  >>> s = Solver()
5475  >>> s.add(a != b)
5476  >>> s.check()
5477  sat
5478  >>> m = s.model()
5479  >>> m.get_universe(A)
5480  [A!val!0, A!val!1]
5481  """
5482  if __debug__:
5483  _z3_assert(isinstance(s, SortRef), "Z3 sort expected")
5484  try:
5485  return AstVector(Z3_model_get_sort_universe(self.ctx.ref(), self.model, s.ast), self.ctx)
5486  except Z3Exception:
5487  return None
5488 
Z3_ast_vector Z3_API Z3_model_get_sort_universe(__in Z3_context c, __in Z3_model m, __in 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:5469
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 5414 of file z3py.py.

Referenced by ModelRef.get_sort().

5414  def num_sorts(self):
5415  """Return the number of unintepreted sorts that contain an interpretation in the model `self`.
5416 
5417  >>> A = DeclareSort('A')
5418  >>> a, b = Consts('a b', A)
5419  >>> s = Solver()
5420  >>> s.add(a != b)
5421  >>> s.check()
5422  sat
5423  >>> m = s.model()
5424  >>> m.num_sorts()
5425  1
5426  """
5427  return int(Z3_model_get_num_sorts(self.ctx.ref(), self.model))
5428 
def num_sorts(self)
Definition: z3py.py:5414
unsigned Z3_API Z3_model_get_num_sorts(__in Z3_context c, __in Z3_model m)
Return the number of uninterpreted sorts that m assigs an interpretation to.
def sexpr (   self)
Return a textual representation of the s-expression representing the model.

Definition at line 5309 of file z3py.py.

Referenced by Fixedpoint.__repr__().

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

5452  def sorts(self):
5453  """Return all uninterpreted sorts that have an interpretation in the model `self`.
5454 
5455  >>> A = DeclareSort('A')
5456  >>> B = DeclareSort('B')
5457  >>> a1, a2 = Consts('a1 a2', A)
5458  >>> b1, b2 = Consts('b1 b2', B)
5459  >>> s = Solver()
5460  >>> s.add(a1 != a2, b1 != b2)
5461  >>> s.check()
5462  sat
5463  >>> m = s.model()
5464  >>> m.sorts()
5465  [A, B]
5466  """
5467  return [ self.get_sort(i) for i in range(self.num_sorts()) ]
5468 
def sorts(self)
Definition: z3py.py:5452
def num_sorts(self)
Definition: z3py.py:5414
def get_sort(self, idx)
Definition: z3py.py:5429

Field Documentation

ctx
model